xref: /openbmc/linux/sound/soc/sof/sof-audio.h (revision 967885ee)
1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
2 /*
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * Copyright(c) 2019 Intel Corporation. All rights reserved.
7  *
8  * Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
9  */
10 
11 #ifndef __SOUND_SOC_SOF_AUDIO_H
12 #define __SOUND_SOC_SOF_AUDIO_H
13 
14 #include <linux/workqueue.h>
15 
16 #include <sound/soc.h>
17 #include <sound/control.h>
18 #include <sound/sof/stream.h> /* needs to be included before control.h */
19 #include <sound/sof/control.h>
20 #include <sound/sof/dai.h>
21 #include <sound/sof/topology.h>
22 #include "sof-priv.h"
23 
24 #define SOF_AUDIO_PCM_DRV_NAME	"sof-audio-component"
25 
26 /* max number of FE PCMs before BEs */
27 #define SOF_BE_PCM_BASE		16
28 
29 #define DMA_CHAN_INVALID	0xFFFFFFFF
30 
31 #define WIDGET_IS_DAI(id) ((id) == snd_soc_dapm_dai_in || (id) == snd_soc_dapm_dai_out)
32 
33 /*
34  * Volume fractional word length define to 16 sets
35  * the volume linear gain value to use Qx.16 format
36  */
37 #define VOLUME_FWL	16
38 
39 struct snd_sof_widget;
40 struct snd_sof_route;
41 struct snd_sof_control;
42 
43 struct snd_sof_dai_config_data {
44 	int dai_index;
45 	int dai_data; /* contains DAI-specific information */
46 };
47 
48 /**
49  * struct sof_ipc_pcm_ops - IPC-specific PCM ops
50  * @hw_params: Function pointer for hw_params
51  * @hw_free: Function pointer for hw_free
52  * @trigger: Function pointer for trigger
53  * @dai_link_fixup: Function pointer for DAI link fixup
54  */
55 struct sof_ipc_pcm_ops {
56 	int (*hw_params)(struct snd_soc_component *component, struct snd_pcm_substream *substream,
57 			 struct snd_pcm_hw_params *params,
58 			 struct snd_sof_platform_stream_params *platform_params);
59 	int (*hw_free)(struct snd_soc_component *component, struct snd_pcm_substream *substream);
60 	int (*trigger)(struct snd_soc_component *component,  struct snd_pcm_substream *substream,
61 		       int cmd);
62 	int (*dai_link_fixup)(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
63 };
64 
65 /**
66  * struct sof_ipc_tplg_control_ops - IPC-specific ops for topology kcontrol IO
67  */
68 struct sof_ipc_tplg_control_ops {
69 	bool (*volume_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
70 	int (*volume_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
71 	bool (*switch_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
72 	int (*switch_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
73 	bool (*enum_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
74 	int (*enum_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
75 	int (*bytes_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
76 	int (*bytes_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
77 	int (*bytes_ext_get)(struct snd_sof_control *scontrol,
78 			     const unsigned int __user *binary_data, unsigned int size);
79 	int (*bytes_ext_volatile_get)(struct snd_sof_control *scontrol,
80 				      const unsigned int __user *binary_data, unsigned int size);
81 	int (*bytes_ext_put)(struct snd_sof_control *scontrol,
82 			     const unsigned int __user *binary_data, unsigned int size);
83 	/* update control data based on notification from the DSP */
84 	void (*update)(struct snd_sof_dev *sdev, void *ipc_control_message);
85 };
86 
87 /**
88  * struct sof_ipc_tplg_widget_ops - IPC-specific ops for topology widgets
89  * @ipc_setup: Function pointer for setting up widget IPC params
90  * @ipc_free: Function pointer for freeing widget IPC params
91  * @token_list: List of token ID's that should be parsed for the widget
92  * @token_list_size: number of elements in token_list
93  * @bind_event: Function pointer for binding events to the widget
94  */
95 struct sof_ipc_tplg_widget_ops {
96 	int (*ipc_setup)(struct snd_sof_widget *swidget);
97 	void (*ipc_free)(struct snd_sof_widget *swidget);
98 	enum sof_tokens *token_list;
99 	int token_list_size;
100 	int (*bind_event)(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
101 			  u16 event_type);
102 };
103 
104 /**
105  * struct sof_ipc_tplg_ops - IPC-specific topology ops
106  * @widget: Array of pointers to IPC-specific ops for widgets. This should always be of size
107  *	    SND_SOF_DAPM_TYPE_COUNT i.e one per widget type. Unsupported widget types will be
108  *	    initialized to 0.
109  * @control: Pointer to the IPC-specific ops for topology kcontrol IO
110  * @route_setup: Function pointer for setting up pipeline connections
111  * @token_list: List of all tokens supported by the IPC version. The size of the token_list
112  *		array should be SOF_TOKEN_COUNT. The unused elements in the array will be
113  *		initialized to 0.
114  * @control_setup: Function pointer for setting up kcontrol IPC-specific data
115  * @control_free: Function pointer for freeing kcontrol IPC-specific data
116  * @pipeline_complete: Function pointer for pipeline complete IPC
117  * @widget_setup: Function pointer for setting up setup in the DSP
118  * @widget_free: Function pointer for freeing widget in the DSP
119  * @dai_config: Function pointer for sending DAI config IPC to the DSP
120  */
121 struct sof_ipc_tplg_ops {
122 	const struct sof_ipc_tplg_widget_ops *widget;
123 	const struct sof_ipc_tplg_control_ops *control;
124 	int (*route_setup)(struct snd_sof_dev *sdev, struct snd_sof_route *sroute);
125 	const struct sof_token_info *token_list;
126 	int (*control_setup)(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol);
127 	int (*control_free)(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol);
128 	int (*pipeline_complete)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
129 	int (*widget_setup)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
130 	int (*widget_free)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
131 	int (*dai_config)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
132 			  unsigned int flags, struct snd_sof_dai_config_data *data);
133 };
134 
135 /** struct snd_sof_tuple - Tuple info
136  * @token:	Token ID
137  * @value:	union of a string or a u32 values
138  */
139 struct snd_sof_tuple {
140 	u32 token;
141 	union {
142 		u32 v;
143 		const char *s;
144 	} value;
145 };
146 
147 /*
148  * List of SOF token ID's. The order of ID's does not matter as token arrays are looked up based on
149  * the ID.
150  */
151 enum sof_tokens {
152 	SOF_PCM_TOKENS,
153 	SOF_PIPELINE_TOKENS,
154 	SOF_SCHED_TOKENS,
155 	SOF_ASRC_TOKENS,
156 	SOF_SRC_TOKENS,
157 	SOF_COMP_TOKENS,
158 	SOF_BUFFER_TOKENS,
159 	SOF_VOLUME_TOKENS,
160 	SOF_PROCESS_TOKENS,
161 	SOF_DAI_TOKENS,
162 	SOF_DAI_LINK_TOKENS,
163 	SOF_HDA_TOKENS,
164 	SOF_SSP_TOKENS,
165 	SOF_ALH_TOKENS,
166 	SOF_DMIC_TOKENS,
167 	SOF_DMIC_PDM_TOKENS,
168 	SOF_ESAI_TOKENS,
169 	SOF_SAI_TOKENS,
170 	SOF_AFE_TOKENS,
171 	SOF_CORE_TOKENS,
172 	SOF_COMP_EXT_TOKENS,
173 
174 	/* this should be the last */
175 	SOF_TOKEN_COUNT,
176 };
177 
178 /**
179  * struct sof_topology_token - SOF topology token definition
180  * @token:		Token number
181  * @type:		Token type
182  * @get_token:		Function pointer to parse the token value and save it in a object
183  * @offset:		Offset within an object to save the token value into
184  */
185 struct sof_topology_token {
186 	u32 token;
187 	u32 type;
188 	int (*get_token)(void *elem, void *object, u32 offset);
189 	u32 offset;
190 };
191 
192 struct sof_token_info {
193 	const char *name;
194 	const struct sof_topology_token *tokens;
195 	int count;
196 };
197 
198 /* PCM stream, mapped to FW component  */
199 struct snd_sof_pcm_stream {
200 	u32 comp_id;
201 	struct snd_dma_buffer page_table;
202 	struct sof_ipc_stream_posn posn;
203 	struct snd_pcm_substream *substream;
204 	struct snd_compr_stream *cstream;
205 	struct work_struct period_elapsed_work;
206 	struct snd_soc_dapm_widget_list *list; /* list of connected DAPM widgets */
207 	bool d0i3_compatible; /* DSP can be in D0I3 when this pcm is opened */
208 	/*
209 	 * flag to indicate that the DSP pipelines should be kept
210 	 * active or not while suspending the stream
211 	 */
212 	bool suspend_ignored;
213 };
214 
215 /* ALSA SOF PCM device */
216 struct snd_sof_pcm {
217 	struct snd_soc_component *scomp;
218 	struct snd_soc_tplg_pcm pcm;
219 	struct snd_sof_pcm_stream stream[2];
220 	struct list_head list;	/* list in sdev pcm list */
221 	struct snd_pcm_hw_params params[2];
222 	bool prepared[2]; /* PCM_PARAMS set successfully */
223 };
224 
225 struct snd_sof_led_control {
226 	unsigned int use_led;
227 	unsigned int direction;
228 	int led_value;
229 };
230 
231 /* ALSA SOF Kcontrol device */
232 struct snd_sof_control {
233 	struct snd_soc_component *scomp;
234 	const char *name;
235 	int comp_id;
236 	int min_volume_step; /* min volume step for volume_table */
237 	int max_volume_step; /* max volume step for volume_table */
238 	int num_channels;
239 	unsigned int access;
240 	u32 readback_offset; /* offset to mmapped data if used */
241 	int info_type;
242 	int index; /* pipeline ID */
243 	void *priv; /* private data copied from topology */
244 	size_t priv_size; /* size of private data */
245 	size_t max_size;
246 	void *ipc_control_data;
247 	int max; /* applicable to volume controls */
248 	u32 size;	/* cdata size */
249 	u32 *volume_table; /* volume table computed from tlv data*/
250 
251 	struct list_head list;	/* list in sdev control list */
252 
253 	struct snd_sof_led_control led_ctl;
254 
255 	/* if true, the control's data needs to be updated from Firmware */
256 	bool comp_data_dirty;
257 };
258 
259 /** struct snd_sof_dai_link - DAI link info
260  * @tuples: array of parsed tuples
261  * @num_tuples: number of tuples in the tuples array
262  * @link: Pointer to snd_soc_dai_link
263  * @hw_configs: Pointer to hw configs in topology
264  * @num_hw_configs: Number of hw configs in topology
265  * @default_hw_cfg_id: Default hw config ID
266  * @type: DAI type
267  * @list: item in snd_sof_dev dai_link list
268  */
269 struct snd_sof_dai_link {
270 	struct snd_sof_tuple *tuples;
271 	int num_tuples;
272 	struct snd_soc_dai_link *link;
273 	struct snd_soc_tplg_hw_config *hw_configs;
274 	int num_hw_configs;
275 	int default_hw_cfg_id;
276 	int type;
277 	struct list_head list;
278 };
279 
280 /* ASoC SOF DAPM widget */
281 struct snd_sof_widget {
282 	struct snd_soc_component *scomp;
283 	int comp_id;
284 	int pipeline_id;
285 	int complete;
286 	int use_count; /* use_count will be protected by the PCM mutex held by the core */
287 	int core;
288 	int id;
289 
290 	/*
291 	 * Flag indicating if the widget should be set up dynamically when a PCM is opened.
292 	 * This flag is only set for the scheduler type widget in topology. During topology
293 	 * loading, this flag is propagated to all the widgets belonging to the same pipeline.
294 	 * When this flag is not set, a widget is set up at the time of topology loading
295 	 * and retained until the DSP enters D3. It will need to be set up again when resuming
296 	 * from D3.
297 	 */
298 	bool dynamic_pipeline_widget;
299 
300 	struct snd_soc_dapm_widget *widget;
301 	struct list_head list;	/* list in sdev widget list */
302 	struct snd_sof_widget *pipe_widget;
303 
304 	const guid_t uuid;
305 
306 	int num_tuples;
307 	struct snd_sof_tuple *tuples;
308 
309 	void *private;		/* core does not touch this */
310 };
311 
312 /* ASoC SOF DAPM route */
313 struct snd_sof_route {
314 	struct snd_soc_component *scomp;
315 
316 	struct snd_soc_dapm_route *route;
317 	struct list_head list;	/* list in sdev route list */
318 	struct snd_sof_widget *src_widget;
319 	struct snd_sof_widget *sink_widget;
320 	bool setup;
321 
322 	void *private;
323 };
324 
325 /* ASoC DAI device */
326 struct snd_sof_dai {
327 	struct snd_soc_component *scomp;
328 	const char *name;
329 
330 	int number_configs;
331 	int current_config;
332 	struct list_head list;	/* list in sdev dai list */
333 	void *private;
334 };
335 
336 /*
337  * Kcontrols.
338  */
339 
340 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
341 		       struct snd_ctl_elem_value *ucontrol);
342 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
343 		       struct snd_ctl_elem_value *ucontrol);
344 int snd_sof_volume_info(struct snd_kcontrol *kcontrol,
345 			struct snd_ctl_elem_info *uinfo);
346 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
347 		       struct snd_ctl_elem_value *ucontrol);
348 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
349 		       struct snd_ctl_elem_value *ucontrol);
350 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
351 		     struct snd_ctl_elem_value *ucontrol);
352 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
353 		     struct snd_ctl_elem_value *ucontrol);
354 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
355 		      struct snd_ctl_elem_value *ucontrol);
356 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
357 		      struct snd_ctl_elem_value *ucontrol);
358 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
359 			  const unsigned int __user *binary_data,
360 			  unsigned int size);
361 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
362 			  unsigned int __user *binary_data,
363 			  unsigned int size);
364 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
365 				   unsigned int size);
366 void snd_sof_control_notify(struct snd_sof_dev *sdev,
367 			    struct sof_ipc_ctrl_data *cdata);
368 
369 /*
370  * Topology.
371  * There is no snd_sof_free_topology since topology components will
372  * be freed by snd_soc_unregister_component,
373  */
374 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file);
375 
376 /*
377  * Stream IPC
378  */
379 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
380 			    struct snd_sof_pcm *spcm, int direction,
381 			    struct sof_ipc_stream_posn *posn);
382 
383 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
384 					    const char *name);
385 struct snd_sof_widget *
386 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
387 			   const char *pcm_name, int dir);
388 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
389 				     const char *name);
390 
391 static inline
392 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_soc_component *scomp,
393 					  struct snd_soc_pcm_runtime *rtd)
394 {
395 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
396 
397 	struct snd_sof_pcm *spcm = NULL;
398 
399 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
400 		if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
401 			return spcm;
402 	}
403 
404 	return NULL;
405 }
406 
407 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
408 					   const char *name);
409 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
410 					   unsigned int comp_id,
411 					   int *direction);
412 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
413 void snd_sof_pcm_init_elapsed_work(struct work_struct *work);
414 
415 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
416 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream);
417 void snd_sof_compr_init_elapsed_work(struct work_struct *work);
418 #else
419 static inline void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) { }
420 static inline void snd_sof_compr_init_elapsed_work(struct work_struct *work) { }
421 #endif
422 
423 /*
424  * Mixer IPC
425  */
426 int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set);
427 
428 /* DAI link fixup */
429 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
430 
431 /* PM */
432 int sof_set_up_pipelines(struct snd_sof_dev *sdev, bool verify);
433 int sof_tear_down_pipelines(struct snd_sof_dev *sdev, bool verify);
434 int sof_set_hw_params_upon_resume(struct device *dev);
435 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev);
436 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev);
437 
438 /* Machine driver enumeration */
439 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata);
440 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata);
441 
442 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
443 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
444 
445 /* PCM */
446 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
447 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
448 int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
449 			 struct snd_sof_pcm *spcm);
450 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
451 			struct snd_sof_pcm *spcm, int dir, bool free_widget_list);
452 int get_token_u32(void *elem, void *object, u32 offset);
453 int get_token_u16(void *elem, void *object, u32 offset);
454 int get_token_comp_format(void *elem, void *object, u32 offset);
455 int get_token_dai_type(void *elem, void *object, u32 offset);
456 int get_token_uuid(void *elem, void *object, u32 offset);
457 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
458 			  struct snd_sof_tuple *tuples, int num_tuples,
459 			  size_t object_size, int token_instance_num);
460 #endif
461