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