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"
27 * The ipc4 firmware only supports up to 8 sink or source pins
28 * per widget, because only 3 bits are used for queue(pin) ID
31 #define SOF_WIDGET_MAX_NUM_PINS 8
33 /* The type of a widget pin is either sink or source */
34 #define SOF_PIN_TYPE_SINK 0
35 #define SOF_PIN_TYPE_SOURCE 1
37 /* max number of FE PCMs before BEs */
38 #define SOF_BE_PCM_BASE 16
40 #define DMA_CHAN_INVALID 0xFFFFFFFF
42 #define WIDGET_IS_DAI(id) ((id) == snd_soc_dapm_dai_in || (id) == snd_soc_dapm_dai_out)
43 #define WIDGET_IS_AIF(id) ((id) == snd_soc_dapm_aif_in || (id) == snd_soc_dapm_aif_out)
44 #define WIDGET_IS_AIF_OR_DAI(id) (WIDGET_IS_DAI(id) || WIDGET_IS_AIF(id))
45 #define WIDGET_IS_COPIER(id) (WIDGET_IS_AIF_OR_DAI(id) || (id) == snd_soc_dapm_buffer)
47 #define SOF_DAI_CLK_INTEL_SSP_MCLK 0
48 #define SOF_DAI_CLK_INTEL_SSP_BCLK 1
58 * Volume fractional word length define to 16 sets
59 * the volume linear gain value to use Qx.16 format
63 #define SOF_TLV_ITEMS 3
65 static inline u32 mixer_to_ipc(unsigned int value, u32 *volume_map, int size)
68 return volume_map[size - 1];
70 return volume_map[value];
73 static inline u32 ipc_to_mixer(u32 value, u32 *volume_map, int size)
77 for (i = 0; i < size; i++) {
78 if (volume_map[i] >= value)
85 struct snd_sof_widget;
87 struct snd_sof_control;
91 struct snd_sof_dai_config_data {
93 int dai_data; /* contains DAI-specific information */
97 * struct sof_ipc_pcm_ops - IPC-specific PCM ops
98 * @hw_params: Function pointer for hw_params
99 * @hw_free: Function pointer for hw_free
100 * @trigger: Function pointer for trigger
101 * @dai_link_fixup: Function pointer for DAI link fixup
102 * @pcm_setup: Function pointer for IPC-specific PCM set up that can be used for allocating
103 * additional memory in the SOF PCM stream structure
104 * @pcm_free: Function pointer for PCM free that can be used for freeing any
105 * additional memory in the SOF PCM stream structure
106 * @delay: Function pointer for pcm delay calculation
108 struct sof_ipc_pcm_ops {
109 int (*hw_params)(struct snd_soc_component *component, struct snd_pcm_substream *substream,
110 struct snd_pcm_hw_params *params,
111 struct snd_sof_platform_stream_params *platform_params);
112 int (*hw_free)(struct snd_soc_component *component, struct snd_pcm_substream *substream);
113 int (*trigger)(struct snd_soc_component *component, struct snd_pcm_substream *substream,
115 int (*dai_link_fixup)(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
116 int (*pcm_setup)(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm);
117 void (*pcm_free)(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm);
118 snd_pcm_sframes_t (*delay)(struct snd_soc_component *component,
119 struct snd_pcm_substream *substream);
123 * struct sof_ipc_tplg_control_ops - IPC-specific ops for topology kcontrol IO
125 struct sof_ipc_tplg_control_ops {
126 bool (*volume_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
127 int (*volume_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
128 bool (*switch_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
129 int (*switch_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
130 bool (*enum_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
131 int (*enum_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
132 int (*bytes_put)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
133 int (*bytes_get)(struct snd_sof_control *scontrol, struct snd_ctl_elem_value *ucontrol);
134 int (*bytes_ext_get)(struct snd_sof_control *scontrol,
135 const unsigned int __user *binary_data, unsigned int size);
136 int (*bytes_ext_volatile_get)(struct snd_sof_control *scontrol,
137 const unsigned int __user *binary_data, unsigned int size);
138 int (*bytes_ext_put)(struct snd_sof_control *scontrol,
139 const unsigned int __user *binary_data, unsigned int size);
140 /* update control data based on notification from the DSP */
141 void (*update)(struct snd_sof_dev *sdev, void *ipc_control_message);
142 /* Optional callback to setup kcontrols associated with an swidget */
143 int (*widget_kcontrol_setup)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
144 /* mandatory callback to set up volume table for volume kcontrols */
145 int (*set_up_volume_table)(struct snd_sof_control *scontrol, int tlv[SOF_TLV_ITEMS],
150 * struct sof_ipc_tplg_widget_ops - IPC-specific ops for topology widgets
151 * @ipc_setup: Function pointer for setting up widget IPC params
152 * @ipc_free: Function pointer for freeing widget IPC params
153 * @token_list: List of token ID's that should be parsed for the widget
154 * @token_list_size: number of elements in token_list
155 * @bind_event: Function pointer for binding events to the widget
156 * @ipc_prepare: Optional op for preparing a widget for set up
157 * @ipc_unprepare: Optional op for unpreparing a widget
159 struct sof_ipc_tplg_widget_ops {
160 int (*ipc_setup)(struct snd_sof_widget *swidget);
161 void (*ipc_free)(struct snd_sof_widget *swidget);
162 enum sof_tokens *token_list;
164 int (*bind_event)(struct snd_soc_component *scomp, struct snd_sof_widget *swidget,
166 int (*ipc_prepare)(struct snd_sof_widget *swidget,
167 struct snd_pcm_hw_params *fe_params,
168 struct snd_sof_platform_stream_params *platform_params,
169 struct snd_pcm_hw_params *source_params, int dir);
170 void (*ipc_unprepare)(struct snd_sof_widget *swidget);
174 * struct sof_ipc_tplg_ops - IPC-specific topology ops
175 * @widget: Array of pointers to IPC-specific ops for widgets. This should always be of size
176 * SND_SOF_DAPM_TYPE_COUNT i.e one per widget type. Unsupported widget types will be
178 * @control: Pointer to the IPC-specific ops for topology kcontrol IO
179 * @route_setup: Function pointer for setting up pipeline connections
180 * @route_free: Function pointer for freeing pipeline connections.
181 * @token_list: List of all tokens supported by the IPC version. The size of the token_list
182 * array should be SOF_TOKEN_COUNT. The unused elements in the array will be
184 * @control_setup: Function pointer for setting up kcontrol IPC-specific data
185 * @control_free: Function pointer for freeing kcontrol IPC-specific data
186 * @pipeline_complete: Function pointer for pipeline complete IPC
187 * @widget_setup: Function pointer for setting up setup in the DSP
188 * @widget_free: Function pointer for freeing widget in the DSP
189 * @dai_config: Function pointer for sending DAI config IPC to the DSP
190 * @dai_get_clk: Function pointer for getting the DAI clock setting
191 * @set_up_all_pipelines: Function pointer for setting up all topology pipelines
192 * @tear_down_all_pipelines: Function pointer for tearing down all topology pipelines
193 * @parse_manifest: Function pointer for ipc4 specific parsing of topology manifest
194 * @link_setup: Function pointer for IPC-specific DAI link set up
196 * Note: function pointers (ops) are optional
198 struct sof_ipc_tplg_ops {
199 const struct sof_ipc_tplg_widget_ops *widget;
200 const struct sof_ipc_tplg_control_ops *control;
201 int (*route_setup)(struct snd_sof_dev *sdev, struct snd_sof_route *sroute);
202 int (*route_free)(struct snd_sof_dev *sdev, struct snd_sof_route *sroute);
203 const struct sof_token_info *token_list;
204 int (*control_setup)(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol);
205 int (*control_free)(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol);
206 int (*pipeline_complete)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
207 int (*widget_setup)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
208 int (*widget_free)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
209 int (*dai_config)(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,
210 unsigned int flags, struct snd_sof_dai_config_data *data);
211 int (*dai_get_clk)(struct snd_sof_dev *sdev, struct snd_sof_dai *dai, int clk_type);
212 int (*set_up_all_pipelines)(struct snd_sof_dev *sdev, bool verify);
213 int (*tear_down_all_pipelines)(struct snd_sof_dev *sdev, bool verify);
214 int (*parse_manifest)(struct snd_soc_component *scomp, int index,
215 struct snd_soc_tplg_manifest *man);
216 int (*link_setup)(struct snd_sof_dev *sdev, struct snd_soc_dai_link *link);
219 /** struct snd_sof_tuple - Tuple info
221 * @value: union of a string or a u32 values
223 struct snd_sof_tuple {
232 * List of SOF token ID's. The order of ID's does not matter as token arrays are looked up based on
257 SOF_IN_AUDIO_FORMAT_TOKENS,
258 SOF_OUT_AUDIO_FORMAT_TOKENS,
259 SOF_AUDIO_FORMAT_BUFFER_SIZE_TOKENS,
260 SOF_COPIER_GATEWAY_CFG_TOKENS,
262 SOF_AUDIO_FMT_NUM_TOKENS,
263 SOF_COPIER_FORMAT_TOKENS,
268 /* this should be the last */
273 * struct sof_topology_token - SOF topology token definition
274 * @token: Token number
276 * @get_token: Function pointer to parse the token value and save it in a object
277 * @offset: Offset within an object to save the token value into
279 struct sof_topology_token {
282 int (*get_token)(void *elem, void *object, u32 offset);
286 struct sof_token_info {
288 const struct sof_topology_token *tokens;
293 * struct snd_sof_pcm_stream_pipeline_list - List of pipelines associated with a PCM stream
294 * @count: number of pipeline widgets in the @pipe_widgets array
295 * @pipelines: array of pipelines
297 struct snd_sof_pcm_stream_pipeline_list {
299 struct snd_sof_pipeline **pipelines;
302 /* PCM stream, mapped to FW component */
303 struct snd_sof_pcm_stream {
305 struct snd_dma_buffer page_table;
306 struct sof_ipc_stream_posn posn;
307 struct snd_pcm_substream *substream;
308 struct snd_compr_stream *cstream;
309 struct work_struct period_elapsed_work;
310 struct snd_soc_dapm_widget_list *list; /* list of connected DAPM widgets */
311 bool d0i3_compatible; /* DSP can be in D0I3 when this pcm is opened */
313 * flag to indicate that the DSP pipelines should be kept
314 * active or not while suspending the stream
316 bool suspend_ignored;
317 struct snd_sof_pcm_stream_pipeline_list pipeline_list;
319 /* used by IPC implementation and core does not touch it */
323 /* ALSA SOF PCM device */
325 struct snd_soc_component *scomp;
326 struct snd_soc_tplg_pcm pcm;
327 struct snd_sof_pcm_stream stream[2];
328 struct list_head list; /* list in sdev pcm list */
329 struct snd_pcm_hw_params params[2];
330 bool prepared[2]; /* PCM_PARAMS set successfully */
333 struct snd_sof_led_control {
334 unsigned int use_led;
335 unsigned int direction;
339 /* ALSA SOF Kcontrol device */
340 struct snd_sof_control {
341 struct snd_soc_component *scomp;
344 int min_volume_step; /* min volume step for volume_table */
345 int max_volume_step; /* max volume step for volume_table */
349 int index; /* pipeline ID */
350 void *priv; /* private data copied from topology */
351 size_t priv_size; /* size of private data */
353 void *ipc_control_data;
354 int max; /* applicable to volume controls */
355 u32 size; /* cdata size */
356 u32 *volume_table; /* volume table computed from tlv data*/
358 struct list_head list; /* list in sdev control list */
360 struct snd_sof_led_control led_ctl;
362 /* if true, the control's data needs to be updated from Firmware */
363 bool comp_data_dirty;
366 /** struct snd_sof_dai_link - DAI link info
367 * @tuples: array of parsed tuples
368 * @num_tuples: number of tuples in the tuples array
369 * @link: Pointer to snd_soc_dai_link
370 * @hw_configs: Pointer to hw configs in topology
371 * @num_hw_configs: Number of hw configs in topology
372 * @default_hw_cfg_id: Default hw config ID
374 * @list: item in snd_sof_dev dai_link list
376 struct snd_sof_dai_link {
377 struct snd_sof_tuple *tuples;
379 struct snd_soc_dai_link *link;
380 struct snd_soc_tplg_hw_config *hw_configs;
382 int default_hw_cfg_id;
384 struct list_head list;
387 /* ASoC SOF DAPM widget */
388 struct snd_sof_widget {
389 struct snd_soc_component *scomp;
393 * the prepared flag is used to indicate that a widget has been prepared for getting set
398 struct mutex setup_mutex; /* to protect the swidget setup and free operations */
401 * use_count is protected by the PCM mutex held by the core and the
402 * setup_mutex against non stream domain races (kcontrol access for
408 int id; /* id is the DAPM widget type */
410 * Instance ID is set dynamically when the widget gets set up in the FW. It should be
411 * unique for each module type across all pipelines. This will not be used in SOF_IPC.
416 * Flag indicating if the widget should be set up dynamically when a PCM is opened.
417 * This flag is only set for the scheduler type widget in topology. During topology
418 * loading, this flag is propagated to all the widgets belonging to the same pipeline.
419 * When this flag is not set, a widget is set up at the time of topology loading
420 * and retained until the DSP enters D3. It will need to be set up again when resuming
423 bool dynamic_pipeline_widget;
425 struct snd_soc_dapm_widget *widget;
426 struct list_head list; /* list in sdev widget list */
427 struct snd_sof_pipeline *spipe;
433 struct snd_sof_tuple *tuples;
436 * The allowed range for num_sink/source_pins is [0, SOF_WIDGET_MAX_NUM_PINS].
437 * Widgets may have zero sink or source pins, for example the tone widget has
444 * The sink/source pin binding array, it takes the form of
445 * [widget_name_connected_to_pin0, widget_name_connected_to_pin1, ...],
446 * with the index as the queue ID.
448 * The array is used for special pin binding. Note that even if there
449 * is only one sink/source pin requires special pin binding, pin binding
450 * should be defined for all sink/source pins in topology, for pin(s) that
451 * are not used, give the value "NotConnected".
453 * If pin binding is not defined in topology, nothing to parse in the kernel,
454 * sink_pin_binding and src_pin_binding shall be NULL.
456 char **sink_pin_binding;
457 char **src_pin_binding;
459 struct ida src_queue_ida;
460 struct ida sink_queue_ida;
462 void *private; /* core does not touch this */
465 /** struct snd_sof_pipeline - ASoC SOF pipeline
466 * @pipe_widget: Pointer to the pipeline widget
467 * @started_count: Count of number of PCM's that have started this pipeline
468 * @paused_count: Count of number of PCM's that have started and have currently paused this
470 * @complete: flag used to indicate that pipeline set up is complete.
471 * @list: List item in sdev pipeline_list
473 struct snd_sof_pipeline {
474 struct snd_sof_widget *pipe_widget;
478 struct list_head list;
481 /* ASoC SOF DAPM route */
482 struct snd_sof_route {
483 struct snd_soc_component *scomp;
485 struct snd_soc_dapm_route *route;
486 struct list_head list; /* list in sdev route list */
487 struct snd_sof_widget *src_widget;
488 struct snd_sof_widget *sink_widget;
497 /* ASoC DAI device */
499 struct snd_soc_component *scomp;
504 struct list_head list; /* list in sdev dai list */
512 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
513 struct snd_ctl_elem_value *ucontrol);
514 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
515 struct snd_ctl_elem_value *ucontrol);
516 int snd_sof_volume_info(struct snd_kcontrol *kcontrol,
517 struct snd_ctl_elem_info *uinfo);
518 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
519 struct snd_ctl_elem_value *ucontrol);
520 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
521 struct snd_ctl_elem_value *ucontrol);
522 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
523 struct snd_ctl_elem_value *ucontrol);
524 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
525 struct snd_ctl_elem_value *ucontrol);
526 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
527 struct snd_ctl_elem_value *ucontrol);
528 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
529 struct snd_ctl_elem_value *ucontrol);
530 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
531 const unsigned int __user *binary_data,
533 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
534 unsigned int __user *binary_data,
536 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
538 void snd_sof_control_notify(struct snd_sof_dev *sdev,
539 struct sof_ipc_ctrl_data *cdata);
543 * There is no snd_sof_free_topology since topology components will
544 * be freed by snd_soc_unregister_component,
546 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file);
551 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
552 struct snd_sof_pcm *spcm, int direction,
553 struct sof_ipc_stream_posn *posn);
555 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
557 struct snd_sof_widget *
558 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
559 const char *pcm_name, int dir);
560 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
564 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_soc_component *scomp,
565 struct snd_soc_pcm_runtime *rtd)
567 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
568 struct snd_sof_pcm *spcm;
570 list_for_each_entry(spcm, &sdev->pcm_list, list) {
571 if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
578 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
580 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
581 unsigned int comp_id,
583 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
584 void snd_sof_pcm_init_elapsed_work(struct work_struct *work);
586 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
587 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream);
588 void snd_sof_compr_init_elapsed_work(struct work_struct *work);
590 static inline void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) { }
591 static inline void snd_sof_compr_init_elapsed_work(struct work_struct *work) { }
595 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
598 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev);
599 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev);
601 /* Machine driver enumeration */
602 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata);
603 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata);
605 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
606 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
607 int sof_route_setup(struct snd_sof_dev *sdev, struct snd_soc_dapm_widget *wsource,
608 struct snd_soc_dapm_widget *wsink);
611 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm,
612 struct snd_pcm_hw_params *fe_params,
613 struct snd_sof_platform_stream_params *platform_params,
615 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
616 int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
617 struct snd_sof_pcm *spcm);
618 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
619 struct snd_sof_pcm *spcm, int dir, bool free_widget_list);
620 int get_token_u32(void *elem, void *object, u32 offset);
621 int get_token_u16(void *elem, void *object, u32 offset);
622 int get_token_comp_format(void *elem, void *object, u32 offset);
623 int get_token_dai_type(void *elem, void *object, u32 offset);
624 int get_token_uuid(void *elem, void *object, u32 offset);
625 int get_token_string(void *elem, void *object, u32 offset);
626 int sof_update_ipc_object(struct snd_soc_component *scomp, void *object, enum sof_tokens token_id,
627 struct snd_sof_tuple *tuples, int num_tuples,
628 size_t object_size, int token_instance_num);
629 u32 vol_compute_gain(u32 value, int *tlv);