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 * @parse_manifest: Optional function pointer for ipc4 specific parsing of topology manifest
172 */
173 struct sof_ipc_tplg_ops {
174 const struct sof_ipc_tplg_widget_ops *widget;
175 const struct sof_ipc_tplg_control_ops *control;
176 int (*route_setup)(struct snd_sof_dev *sdev, struct snd_sof_route *sroute);
177 int (*route_free)(struct snd_sof_dev *sdev, struct snd_sof_route *sroute);
178 const struct sof_token_info *token_list;
179 int (*control_setup)(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol);
180 int (*control_free)(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol);
181 int (*pipeline_complete)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
182 int (*widget_setup)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
183 int (*widget_free)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
184 int (*dai_config)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
185 unsigned int flags, struct snd_sof_dai_config_data *data);
186 int (*dai_get_clk)(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type);
187 int (*set_up_all_pipelines)(struct snd_sof_dev *sdev, bool verify);
188 int (*tear_down_all_pipelines)(struct snd_sof_dev *sdev, bool verify);
189 int (*parse_manifest)(struct snd_soc_component *scomp, int index,
190 struct snd_soc_tplg_manifest *man);
191 };
192
193 /** struct snd_sof_tuple - Tuple info
194 * @token: Token ID
195 * @value: union of a string or a u32 values
196 */
197 struct snd_sof_tuple {
198 u32 token;
199 union {
200 u32 v;
201 const char *s;
202 } value;
203 };
204
205 /*
206 * List of SOF token ID's. The order of ID's does not matter as token arrays are looked up based on
207 * the ID.
208 */
209 enum sof_tokens {
210 SOF_PCM_TOKENS,
211 SOF_PIPELINE_TOKENS,
212 SOF_SCHED_TOKENS,
213 SOF_ASRC_TOKENS,
214 SOF_SRC_TOKENS,
215 SOF_COMP_TOKENS,
216 SOF_BUFFER_TOKENS,
217 SOF_VOLUME_TOKENS,
218 SOF_PROCESS_TOKENS,
219 SOF_DAI_TOKENS,
220 SOF_DAI_LINK_TOKENS,
221 SOF_HDA_TOKENS,
222 SOF_SSP_TOKENS,
223 SOF_ALH_TOKENS,
224 SOF_DMIC_TOKENS,
225 SOF_DMIC_PDM_TOKENS,
226 SOF_ESAI_TOKENS,
227 SOF_SAI_TOKENS,
228 SOF_AFE_TOKENS,
229 SOF_CORE_TOKENS,
230 SOF_COMP_EXT_TOKENS,
231 SOF_IN_AUDIO_FORMAT_TOKENS,
232 SOF_OUT_AUDIO_FORMAT_TOKENS,
233 SOF_AUDIO_FORMAT_BUFFER_SIZE_TOKENS,
234 SOF_COPIER_GATEWAY_CFG_TOKENS,
235 SOF_COPIER_TOKENS,
236 SOF_AUDIO_FMT_NUM_TOKENS,
237 SOF_COPIER_FORMAT_TOKENS,
238 SOF_GAIN_TOKENS,
239 SOF_ACPDMIC_TOKENS,
240
241 /* this should be the last */
242 SOF_TOKEN_COUNT,
243 };
244
245 /**
246 * struct sof_topology_token - SOF topology token definition
247 * @token: Token number
248 * @type: Token type
249 * @get_token: Function pointer to parse the token value and save it in a object
250 * @offset: Offset within an object to save the token value into
251 */
252 struct sof_topology_token {
253 u32 token;
254 u32 type;
255 int (*get_token)(void *elem, void *object, u32 offset);
256 u32 offset;
257 };
258
259 struct sof_token_info {
260 const char *name;
261 const struct sof_topology_token *tokens;
262 int count;
263 };
264
265 /* PCM stream, mapped to FW component */
266 struct snd_sof_pcm_stream {
267 u32 comp_id;
268 struct snd_dma_buffer page_table;
269 struct sof_ipc_stream_posn posn;
270 struct snd_pcm_substream *substream;
271 struct snd_compr_stream *cstream;
272 struct work_struct period_elapsed_work;
273 struct snd_soc_dapm_widget_list *list; /* list of connected DAPM widgets */
274 bool d0i3_compatible; /* DSP can be in D0I3 when this pcm is opened */
275 /*
276 * flag to indicate that the DSP pipelines should be kept
277 * active or not while suspending the stream
278 */
279 bool suspend_ignored;
280 };
281
282 /* ALSA SOF PCM device */
283 struct snd_sof_pcm {
284 struct snd_soc_component *scomp;
285 struct snd_soc_tplg_pcm pcm;
286 struct snd_sof_pcm_stream stream[2];
287 struct list_head list; /* list in sdev pcm list */
288 struct snd_pcm_hw_params params[2];
289 bool prepared[2]; /* PCM_PARAMS set successfully */
290 };
291
292 struct snd_sof_led_control {
293 unsigned int use_led;
294 unsigned int direction;
295 int led_value;
296 };
297
298 /* ALSA SOF Kcontrol device */
299 struct snd_sof_control {
300 struct snd_soc_component *scomp;
301 const char *name;
302 int comp_id;
303 int min_volume_step; /* min volume step for volume_table */
304 int max_volume_step; /* max volume step for volume_table */
305 int num_channels;
306 unsigned int access;
307 int info_type;
308 int index; /* pipeline ID */
309 void *priv; /* private data copied from topology */
310 size_t priv_size; /* size of private data */
311 size_t max_size;
312 void *ipc_control_data;
313 int max; /* applicable to volume controls */
314 u32 size; /* cdata size */
315 u32 *volume_table; /* volume table computed from tlv data*/
316
317 struct list_head list; /* list in sdev control list */
318
319 struct snd_sof_led_control led_ctl;
320
321 /* if true, the control's data needs to be updated from Firmware */
322 bool comp_data_dirty;
323 };
324
325 /** struct snd_sof_dai_link - DAI link info
326 * @tuples: array of parsed tuples
327 * @num_tuples: number of tuples in the tuples array
328 * @link: Pointer to snd_soc_dai_link
329 * @hw_configs: Pointer to hw configs in topology
330 * @num_hw_configs: Number of hw configs in topology
331 * @default_hw_cfg_id: Default hw config ID
332 * @type: DAI type
333 * @list: item in snd_sof_dev dai_link list
334 */
335 struct snd_sof_dai_link {
336 struct snd_sof_tuple *tuples;
337 int num_tuples;
338 struct snd_soc_dai_link *link;
339 struct snd_soc_tplg_hw_config *hw_configs;
340 int num_hw_configs;
341 int default_hw_cfg_id;
342 int type;
343 struct list_head list;
344 };
345
346 /* ASoC SOF DAPM widget */
347 struct snd_sof_widget {
348 struct snd_soc_component *scomp;
349 int comp_id;
350 int pipeline_id;
351 /*
352 * complete flag is used to indicate that pipeline set up is complete for scheduler type
353 * widgets. It is unused for all other widget types.
354 */
355 int complete;
356 /*
357 * the prepared flag is used to indicate that a widget has been prepared for getting set
358 * up in the DSP.
359 */
360 bool prepared;
361 int use_count; /* use_count will be protected by the PCM mutex held by the core */
362 int core;
363 int id; /* id is the DAPM widget type */
364 /*
365 * Instance ID is set dynamically when the widget gets set up in the FW. It should be
366 * unique for each module type across all pipelines. This will not be used in SOF_IPC.
367 */
368 int instance_id;
369
370 /*
371 * Flag indicating if the widget should be set up dynamically when a PCM is opened.
372 * This flag is only set for the scheduler type widget in topology. During topology
373 * loading, this flag is propagated to all the widgets belonging to the same pipeline.
374 * When this flag is not set, a widget is set up at the time of topology loading
375 * and retained until the DSP enters D3. It will need to be set up again when resuming
376 * from D3.
377 */
378 bool dynamic_pipeline_widget;
379
380 struct snd_soc_dapm_widget *widget;
381 struct list_head list; /* list in sdev widget list */
382 struct snd_sof_widget *pipe_widget;
383 void *module_info;
384
385 const guid_t uuid;
386
387 int num_tuples;
388 struct snd_sof_tuple *tuples;
389
390 void *private; /* core does not touch this */
391 };
392
393 /* ASoC SOF DAPM route */
394 struct snd_sof_route {
395 struct snd_soc_component *scomp;
396
397 struct snd_soc_dapm_route *route;
398 struct list_head list; /* list in sdev route list */
399 struct snd_sof_widget *src_widget;
400 struct snd_sof_widget *sink_widget;
401 bool setup;
402
403 void *private;
404 };
405
406 /* ASoC DAI device */
407 struct snd_sof_dai {
408 struct snd_soc_component *scomp;
409 const char *name;
410
411 int number_configs;
412 int current_config;
413 struct list_head list; /* list in sdev dai list */
414 void *private;
415 };
416
417 /*
418 * Kcontrols.
419 */
420
421 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
422 struct snd_ctl_elem_value *ucontrol);
423 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
424 struct snd_ctl_elem_value *ucontrol);
425 int snd_sof_volume_info(struct snd_kcontrol *kcontrol,
426 struct snd_ctl_elem_info *uinfo);
427 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
428 struct snd_ctl_elem_value *ucontrol);
429 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
430 struct snd_ctl_elem_value *ucontrol);
431 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
432 struct snd_ctl_elem_value *ucontrol);
433 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
434 struct snd_ctl_elem_value *ucontrol);
435 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
436 struct snd_ctl_elem_value *ucontrol);
437 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
438 struct snd_ctl_elem_value *ucontrol);
439 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
440 const unsigned int __user *binary_data,
441 unsigned int size);
442 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
443 unsigned int __user *binary_data,
444 unsigned int size);
445 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
446 unsigned int size);
447 void snd_sof_control_notify(struct snd_sof_dev *sdev,
448 struct sof_ipc_ctrl_data *cdata);
449
450 /*
451 * Topology.
452 * There is no snd_sof_free_topology since topology components will
453 * be freed by snd_soc_unregister_component,
454 */
455 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file);
456
457 /*
458 * Stream IPC
459 */
460 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
461 struct snd_sof_pcm *spcm, int direction,
462 struct sof_ipc_stream_posn *posn);
463
464 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
465 const char *name);
466 struct snd_sof_widget *
467 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
468 const char *pcm_name, int dir);
469 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
470 const char *name);
471
472 static inline
snd_sof_find_spcm_dai(struct snd_soc_component * scomp,struct snd_soc_pcm_runtime * rtd)473 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_soc_component *scomp,
474 struct snd_soc_pcm_runtime *rtd)
475 {
476 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
477 struct snd_sof_pcm *spcm;
478
479 list_for_each_entry(spcm, &sdev->pcm_list, list) {
480 if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
481 return spcm;
482 }
483
484 return NULL;
485 }
486
487 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
488 const char *name);
489 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
490 unsigned int comp_id,
491 int *direction);
492 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
493 void snd_sof_pcm_init_elapsed_work(struct work_struct *work);
494
495 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
496 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream);
497 void snd_sof_compr_init_elapsed_work(struct work_struct *work);
498 #else
snd_sof_compr_fragment_elapsed(struct snd_compr_stream * cstream)499 static inline void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) { }
snd_sof_compr_init_elapsed_work(struct work_struct * work)500 static inline void snd_sof_compr_init_elapsed_work(struct work_struct *work) { }
501 #endif
502
503 /* DAI link fixup */
504 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
505
506 /* PM */
507 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev);
508 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev);
509
510 /* Machine driver enumeration */
511 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata);
512 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata);
513
514 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
515 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
516 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
517 struct snd_soc_dapm_widget *wsink);
518
519 /* PCM */
520 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
521 struct snd_pcm_hw_params *fe_params,
522 struct snd_sof_platform_stream_params *platform_params,
523 int dir);
524 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
525 int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
526 struct snd_sof_pcm *spcm);
527 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
528 struct snd_sof_pcm *spcm, int dir, bool free_widget_list);
529 int get_token_u32(void *elem, void *object, u32 offset);
530 int get_token_u16(void *elem, void *object, u32 offset);
531 int get_token_comp_format(void *elem, void *object, u32 offset);
532 int get_token_dai_type(void *elem, void *object, u32 offset);
533 int get_token_uuid(void *elem, void *object, u32 offset);
534 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
535 struct snd_sof_tuple *tuples, int num_tuples,
536 size_t object_size, int token_instance_num);
537 u32 vol_compute_gain(u32 value, int *tlv);
538 #endif
539