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