xref: /openbmc/linux/sound/soc/sof/sof-audio.h (revision 0af0f463)
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 /** struct snd_sof_tuple - Tuple info
34  * @token:	Token ID
35  * @value:	union of a string or a u32 values
36  */
37 struct snd_sof_tuple {
38 	u32 token;
39 	union {
40 		u32 v;
41 		const char *s;
42 	} value;
43 };
44 
45 /* PCM stream, mapped to FW component  */
46 struct snd_sof_pcm_stream {
47 	u32 comp_id;
48 	struct snd_dma_buffer page_table;
49 	struct sof_ipc_stream_posn posn;
50 	struct snd_pcm_substream *substream;
51 	struct snd_compr_stream *cstream;
52 	struct work_struct period_elapsed_work;
53 	struct snd_soc_dapm_widget_list *list; /* list of connected DAPM widgets */
54 	bool d0i3_compatible; /* DSP can be in D0I3 when this pcm is opened */
55 	/*
56 	 * flag to indicate that the DSP pipelines should be kept
57 	 * active or not while suspending the stream
58 	 */
59 	bool suspend_ignored;
60 };
61 
62 /* ALSA SOF PCM device */
63 struct snd_sof_pcm {
64 	struct snd_soc_component *scomp;
65 	struct snd_soc_tplg_pcm pcm;
66 	struct snd_sof_pcm_stream stream[2];
67 	struct list_head list;	/* list in sdev pcm list */
68 	struct snd_pcm_hw_params params[2];
69 	bool prepared[2]; /* PCM_PARAMS set successfully */
70 };
71 
72 struct snd_sof_led_control {
73 	unsigned int use_led;
74 	unsigned int direction;
75 	int led_value;
76 };
77 
78 /* ALSA SOF Kcontrol device */
79 struct snd_sof_control {
80 	struct snd_soc_component *scomp;
81 	int comp_id;
82 	int min_volume_step; /* min volume step for volume_table */
83 	int max_volume_step; /* max volume step for volume_table */
84 	int num_channels;
85 	unsigned int access;
86 	u32 readback_offset; /* offset to mmapped data if used */
87 	struct sof_ipc_ctrl_data *control_data;
88 	u32 size;	/* cdata size */
89 	u32 *volume_table; /* volume table computed from tlv data*/
90 
91 	struct list_head list;	/* list in sdev control list */
92 
93 	struct snd_sof_led_control led_ctl;
94 
95 	/* if true, the control's data needs to be updated from Firmware */
96 	bool comp_data_dirty;
97 };
98 
99 /** struct snd_sof_dai_link - DAI link info
100  * @tuples: array of parsed tuples
101  * @num_tuples: number of tuples in the tuples array
102  * @link: Pointer to snd_soc_dai_link
103  * @hw_configs: Pointer to hw configs in topology
104  * @num_hw_configs: Number of hw configs in topology
105  * @default_hw_cfg_id: Default hw config ID
106  * @type: DAI type
107  * @list: item in snd_sof_dev dai_link list
108  */
109 struct snd_sof_dai_link {
110 	struct snd_sof_tuple *tuples;
111 	int num_tuples;
112 	struct snd_soc_dai_link *link;
113 	struct snd_soc_tplg_hw_config *hw_configs;
114 	int num_hw_configs;
115 	int default_hw_cfg_id;
116 	int type;
117 	struct list_head list;
118 };
119 
120 /* ASoC SOF DAPM widget */
121 struct snd_sof_widget {
122 	struct snd_soc_component *scomp;
123 	int comp_id;
124 	int pipeline_id;
125 	int complete;
126 	int use_count; /* use_count will be protected by the PCM mutex held by the core */
127 	int core;
128 	int id;
129 
130 	/*
131 	 * Flag indicating if the widget should be set up dynamically when a PCM is opened.
132 	 * This flag is only set for the scheduler type widget in topology. During topology
133 	 * loading, this flag is propagated to all the widgets belonging to the same pipeline.
134 	 * When this flag is not set, a widget is set up at the time of topology loading
135 	 * and retained until the DSP enters D3. It will need to be set up again when resuming
136 	 * from D3.
137 	 */
138 	bool dynamic_pipeline_widget;
139 
140 	struct snd_soc_dapm_widget *widget;
141 	struct list_head list;	/* list in sdev widget list */
142 	struct snd_sof_widget *pipe_widget;
143 
144 	const guid_t uuid;
145 
146 	int num_tuples;
147 	struct snd_sof_tuple *tuples;
148 
149 	void *private;		/* core does not touch this */
150 };
151 
152 /* ASoC SOF DAPM route */
153 struct snd_sof_route {
154 	struct snd_soc_component *scomp;
155 
156 	struct snd_soc_dapm_route *route;
157 	struct list_head list;	/* list in sdev route list */
158 	struct snd_sof_widget *src_widget;
159 	struct snd_sof_widget *sink_widget;
160 	bool setup;
161 
162 	void *private;
163 };
164 
165 /* ASoC DAI device */
166 struct snd_sof_dai {
167 	struct snd_soc_component *scomp;
168 	const char *name;
169 
170 	int number_configs;
171 	int current_config;
172 	bool configured; /* DAI configured during BE hw_params */
173 	struct list_head list;	/* list in sdev dai list */
174 	void *private;
175 };
176 
177 /*
178  * Kcontrols.
179  */
180 
181 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
182 		       struct snd_ctl_elem_value *ucontrol);
183 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
184 		       struct snd_ctl_elem_value *ucontrol);
185 int snd_sof_volume_info(struct snd_kcontrol *kcontrol,
186 			struct snd_ctl_elem_info *uinfo);
187 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
188 		       struct snd_ctl_elem_value *ucontrol);
189 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
190 		       struct snd_ctl_elem_value *ucontrol);
191 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
192 		     struct snd_ctl_elem_value *ucontrol);
193 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
194 		     struct snd_ctl_elem_value *ucontrol);
195 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
196 		      struct snd_ctl_elem_value *ucontrol);
197 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
198 		      struct snd_ctl_elem_value *ucontrol);
199 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
200 			  const unsigned int __user *binary_data,
201 			  unsigned int size);
202 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
203 			  unsigned int __user *binary_data,
204 			  unsigned int size);
205 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
206 				   unsigned int size);
207 void snd_sof_control_notify(struct snd_sof_dev *sdev,
208 			    struct sof_ipc_ctrl_data *cdata);
209 
210 /*
211  * Topology.
212  * There is no snd_sof_free_topology since topology components will
213  * be freed by snd_soc_unregister_component,
214  */
215 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file);
216 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
217 			      struct snd_sof_widget *swidget);
218 
219 /*
220  * Stream IPC
221  */
222 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
223 			    struct snd_sof_pcm *spcm, int direction,
224 			    struct sof_ipc_stream_posn *posn);
225 
226 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
227 					    const char *name);
228 struct snd_sof_widget *
229 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
230 			   const char *pcm_name, int dir);
231 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
232 				     const char *name);
233 
234 static inline
235 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_soc_component *scomp,
236 					  struct snd_soc_pcm_runtime *rtd)
237 {
238 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
239 
240 	struct snd_sof_pcm *spcm = NULL;
241 
242 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
243 		if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
244 			return spcm;
245 	}
246 
247 	return NULL;
248 }
249 
250 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
251 					   const char *name);
252 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
253 					   unsigned int comp_id,
254 					   int *direction);
255 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
256 void snd_sof_pcm_init_elapsed_work(struct work_struct *work);
257 
258 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
259 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream);
260 void snd_sof_compr_init_elapsed_work(struct work_struct *work);
261 #else
262 static inline void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) { }
263 static inline void snd_sof_compr_init_elapsed_work(struct work_struct *work) { }
264 #endif
265 
266 /*
267  * Mixer IPC
268  */
269 int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set);
270 
271 /* DAI link fixup */
272 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
273 
274 /* PM */
275 int sof_set_up_pipelines(struct snd_sof_dev *sdev, bool verify);
276 int sof_tear_down_pipelines(struct snd_sof_dev *sdev, bool verify);
277 int sof_set_hw_params_upon_resume(struct device *dev);
278 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev);
279 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev);
280 
281 /* Machine driver enumeration */
282 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata);
283 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata);
284 
285 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
286 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
287 
288 /* PCM */
289 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
290 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
291 int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
292 			 struct snd_sof_pcm *spcm);
293 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
294 			struct snd_sof_pcm *spcm, int dir, bool free_widget_list);
295 int get_token_u32(void *elem, void *object, u32 offset);
296 int get_token_u16(void *elem, void *object, u32 offset);
297 int get_token_comp_format(void *elem, void *object, u32 offset);
298 int get_token_dai_type(void *elem, void *object, u32 offset);
299 int get_token_uuid(void *elem, void *object, u32 offset);
300 #endif
301