1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
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.
6 * Copyright(c) 2019 Intel Corporation. All rights reserved.
8 * Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
11 #ifndef __SOUND_SOC_SOF_AUDIO_H
12 #define __SOUND_SOC_SOF_AUDIO_H
14 #include <linux/workqueue.h>
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>
24 #define SOF_AUDIO_PCM_DRV_NAME "sof-audio-component"
26 /* max number of FE PCMs before BEs */
27 #define SOF_BE_PCM_BASE 16
29 #define DMA_CHAN_INVALID 0xFFFFFFFF
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))
35 #define SOF_DAI_CLK_INTEL_SSP_MCLK 0
36 #define SOF_DAI_CLK_INTEL_SSP_BCLK 1
46 * Volume fractional word length define to 16 sets
47 * the volume linear gain value to use Qx.16 format
51 #define SOF_TLV_ITEMS 3
53 static inline u32 mixer_to_ipc(unsigned int value, u32 *volume_map, int size)
56 return volume_map[size - 1];
58 return volume_map[value];
61 static inline u32 ipc_to_mixer(u32 value, u32 *volume_map, int size)
65 for (i = 0; i < size; i++) {
66 if (volume_map[i] >= value)
73 struct snd_sof_widget;
75 struct snd_sof_control;
78 struct snd_sof_dai_config_data {
80 int dai_data; /* contains DAI-specific information */
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
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,
97 int (*dai_link_fixup)(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
101 * struct sof_ipc_tplg_control_ops - IPC-specific ops for topology kcontrol IO
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],
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
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;
142 int (*bind_event)(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
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);
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
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
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
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);
193 /** struct snd_sof_tuple - Tuple info
195 * @value: union of a string or a u32 values
197 struct snd_sof_tuple {
206 * List of SOF token ID's. The order of ID's does not matter as token arrays are looked up based on
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,
236 SOF_AUDIO_FMT_NUM_TOKENS,
237 SOF_COPIER_FORMAT_TOKENS,
241 /* this should be the last */
246 * struct sof_topology_token - SOF topology token definition
247 * @token: Token number
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
252 struct sof_topology_token {
255 int (*get_token)(void *elem, void *object, u32 offset);
259 struct sof_token_info {
261 const struct sof_topology_token *tokens;
265 /* PCM stream, mapped to FW component */
266 struct snd_sof_pcm_stream {
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 */
276 * flag to indicate that the DSP pipelines should be kept
277 * active or not while suspending the stream
279 bool suspend_ignored;
282 /* ALSA SOF PCM device */
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 */
292 struct snd_sof_led_control {
293 unsigned int use_led;
294 unsigned int direction;
298 /* ALSA SOF Kcontrol device */
299 struct snd_sof_control {
300 struct snd_soc_component *scomp;
303 int min_volume_step; /* min volume step for volume_table */
304 int max_volume_step; /* max volume step for volume_table */
308 int index; /* pipeline ID */
309 void *priv; /* private data copied from topology */
310 size_t priv_size; /* size of private data */
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*/
317 struct list_head list; /* list in sdev control list */
319 struct snd_sof_led_control led_ctl;
321 /* if true, the control's data needs to be updated from Firmware */
322 bool comp_data_dirty;
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
333 * @list: item in snd_sof_dev dai_link list
335 struct snd_sof_dai_link {
336 struct snd_sof_tuple *tuples;
338 struct snd_soc_dai_link *link;
339 struct snd_soc_tplg_hw_config *hw_configs;
341 int default_hw_cfg_id;
343 struct list_head list;
346 /* ASoC SOF DAPM widget */
347 struct snd_sof_widget {
348 struct snd_soc_component *scomp;
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.
357 * the prepared flag is used to indicate that a widget has been prepared for getting set
361 int use_count; /* use_count will be protected by the PCM mutex held by the core */
363 int id; /* id is the DAPM widget type */
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.
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
378 bool dynamic_pipeline_widget;
380 struct snd_soc_dapm_widget *widget;
381 struct list_head list; /* list in sdev widget list */
382 struct snd_sof_widget *pipe_widget;
388 struct snd_sof_tuple *tuples;
390 void *private; /* core does not touch this */
393 /* ASoC SOF DAPM route */
394 struct snd_sof_route {
395 struct snd_soc_component *scomp;
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;
406 /* ASoC DAI device */
408 struct snd_soc_component *scomp;
413 struct list_head list; /* list in sdev dai list */
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,
442 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
443 unsigned int __user *binary_data,
445 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
447 void snd_sof_control_notify(struct snd_sof_dev *sdev,
448 struct sof_ipc_ctrl_data *cdata);
452 * There is no snd_sof_free_topology since topology components will
453 * be freed by snd_soc_unregister_component,
455 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file);
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);
464 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
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,
473 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_soc_component *scomp,
474 struct snd_soc_pcm_runtime *rtd)
476 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
477 struct snd_sof_pcm *spcm;
479 list_for_each_entry(spcm, &sdev->pcm_list, list) {
480 if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
487 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
489 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
490 unsigned int comp_id,
492 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
493 void snd_sof_pcm_init_elapsed_work(struct work_struct *work);
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);
499 static inline void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) { }
500 static inline void snd_sof_compr_init_elapsed_work(struct work_struct *work) { }
504 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
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);
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);
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);
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,
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);