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