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