xref: /openbmc/linux/sound/soc/sof/sof-audio.h (revision c7b655ad)
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_widget;
100 
101 /* ASoC SOF DAPM widget */
102 struct snd_sof_widget {
103 	struct snd_soc_component *scomp;
104 	int comp_id;
105 	int pipeline_id;
106 	int complete;
107 	int use_count; /* use_count will be protected by the PCM mutex held by the core */
108 	int core;
109 	int id;
110 
111 	/*
112 	 * Flag indicating if the widget should be set up dynamically when a PCM is opened.
113 	 * This flag is only set for the scheduler type widget in topology. During topology
114 	 * loading, this flag is propagated to all the widgets belonging to the same pipeline.
115 	 * When this flag is not set, a widget is set up at the time of topology loading
116 	 * and retained until the DSP enters D3. It will need to be set up again when resuming
117 	 * from D3.
118 	 */
119 	bool dynamic_pipeline_widget;
120 
121 	struct snd_soc_dapm_widget *widget;
122 	struct list_head list;	/* list in sdev widget list */
123 	struct snd_sof_widget *pipe_widget;
124 
125 	const guid_t uuid;
126 
127 	int num_tuples;
128 	struct snd_sof_tuple *tuples;
129 
130 	void *private;		/* core does not touch this */
131 };
132 
133 /* ASoC SOF DAPM route */
134 struct snd_sof_route {
135 	struct snd_soc_component *scomp;
136 
137 	struct snd_soc_dapm_route *route;
138 	struct list_head list;	/* list in sdev route list */
139 	struct snd_sof_widget *src_widget;
140 	struct snd_sof_widget *sink_widget;
141 	bool setup;
142 
143 	void *private;
144 };
145 
146 /* ASoC DAI device */
147 struct snd_sof_dai {
148 	struct snd_soc_component *scomp;
149 	const char *name;
150 
151 	struct sof_ipc_comp_dai *comp_dai;
152 	int number_configs;
153 	int current_config;
154 	bool configured; /* DAI configured during BE hw_params */
155 	struct sof_ipc_dai_config *dai_config;
156 	struct list_head list;	/* list in sdev dai list */
157 };
158 
159 /*
160  * Kcontrols.
161  */
162 
163 int snd_sof_volume_get(struct snd_kcontrol *kcontrol,
164 		       struct snd_ctl_elem_value *ucontrol);
165 int snd_sof_volume_put(struct snd_kcontrol *kcontrol,
166 		       struct snd_ctl_elem_value *ucontrol);
167 int snd_sof_volume_info(struct snd_kcontrol *kcontrol,
168 			struct snd_ctl_elem_info *uinfo);
169 int snd_sof_switch_get(struct snd_kcontrol *kcontrol,
170 		       struct snd_ctl_elem_value *ucontrol);
171 int snd_sof_switch_put(struct snd_kcontrol *kcontrol,
172 		       struct snd_ctl_elem_value *ucontrol);
173 int snd_sof_enum_get(struct snd_kcontrol *kcontrol,
174 		     struct snd_ctl_elem_value *ucontrol);
175 int snd_sof_enum_put(struct snd_kcontrol *kcontrol,
176 		     struct snd_ctl_elem_value *ucontrol);
177 int snd_sof_bytes_get(struct snd_kcontrol *kcontrol,
178 		      struct snd_ctl_elem_value *ucontrol);
179 int snd_sof_bytes_put(struct snd_kcontrol *kcontrol,
180 		      struct snd_ctl_elem_value *ucontrol);
181 int snd_sof_bytes_ext_put(struct snd_kcontrol *kcontrol,
182 			  const unsigned int __user *binary_data,
183 			  unsigned int size);
184 int snd_sof_bytes_ext_get(struct snd_kcontrol *kcontrol,
185 			  unsigned int __user *binary_data,
186 			  unsigned int size);
187 int snd_sof_bytes_ext_volatile_get(struct snd_kcontrol *kcontrol, unsigned int __user *binary_data,
188 				   unsigned int size);
189 void snd_sof_control_notify(struct snd_sof_dev *sdev,
190 			    struct sof_ipc_ctrl_data *cdata);
191 
192 /*
193  * Topology.
194  * There is no snd_sof_free_topology since topology components will
195  * be freed by snd_soc_unregister_component,
196  */
197 int snd_sof_load_topology(struct snd_soc_component *scomp, const char *file);
198 int snd_sof_complete_pipeline(struct snd_sof_dev *sdev,
199 			      struct snd_sof_widget *swidget);
200 
201 /*
202  * Stream IPC
203  */
204 int snd_sof_ipc_stream_posn(struct snd_soc_component *scomp,
205 			    struct snd_sof_pcm *spcm, int direction,
206 			    struct sof_ipc_stream_posn *posn);
207 
208 struct snd_sof_widget *snd_sof_find_swidget(struct snd_soc_component *scomp,
209 					    const char *name);
210 struct snd_sof_widget *
211 snd_sof_find_swidget_sname(struct snd_soc_component *scomp,
212 			   const char *pcm_name, int dir);
213 struct snd_sof_dai *snd_sof_find_dai(struct snd_soc_component *scomp,
214 				     const char *name);
215 
216 static inline
217 struct snd_sof_pcm *snd_sof_find_spcm_dai(struct snd_soc_component *scomp,
218 					  struct snd_soc_pcm_runtime *rtd)
219 {
220 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);
221 
222 	struct snd_sof_pcm *spcm = NULL;
223 
224 	list_for_each_entry(spcm, &sdev->pcm_list, list) {
225 		if (le32_to_cpu(spcm->pcm.dai_id) == rtd->dai_link->id)
226 			return spcm;
227 	}
228 
229 	return NULL;
230 }
231 
232 struct snd_sof_pcm *snd_sof_find_spcm_name(struct snd_soc_component *scomp,
233 					   const char *name);
234 struct snd_sof_pcm *snd_sof_find_spcm_comp(struct snd_soc_component *scomp,
235 					   unsigned int comp_id,
236 					   int *direction);
237 void snd_sof_pcm_period_elapsed(struct snd_pcm_substream *substream);
238 void snd_sof_pcm_init_elapsed_work(struct work_struct *work);
239 
240 #if IS_ENABLED(CONFIG_SND_SOC_SOF_COMPRESS)
241 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream);
242 void snd_sof_compr_init_elapsed_work(struct work_struct *work);
243 #else
244 static inline void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) { }
245 static inline void snd_sof_compr_init_elapsed_work(struct work_struct *work) { }
246 #endif
247 
248 /*
249  * Mixer IPC
250  */
251 int snd_sof_ipc_set_get_comp_data(struct snd_sof_control *scontrol, bool set);
252 
253 /* DAI link fixup */
254 int sof_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd, struct snd_pcm_hw_params *params);
255 
256 /* PM */
257 int sof_set_up_pipelines(struct snd_sof_dev *sdev, bool verify);
258 int sof_tear_down_pipelines(struct snd_sof_dev *sdev, bool verify);
259 int sof_set_hw_params_upon_resume(struct device *dev);
260 bool snd_sof_stream_suspend_ignored(struct snd_sof_dev *sdev);
261 bool snd_sof_dsp_only_d0i3_compatible_stream_active(struct snd_sof_dev *sdev);
262 
263 /* Machine driver enumeration */
264 int sof_machine_register(struct snd_sof_dev *sdev, void *pdata);
265 void sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata);
266 
267 int sof_widget_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
268 int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget);
269 
270 /* PCM */
271 int sof_widget_list_setup(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
272 int sof_widget_list_free(struct snd_sof_dev *sdev, struct snd_sof_pcm *spcm, int dir);
273 int sof_pcm_dsp_pcm_free(struct snd_pcm_substream *substream, struct snd_sof_dev *sdev,
274 			 struct snd_sof_pcm *spcm);
275 int sof_pcm_stream_free(struct snd_sof_dev *sdev, struct snd_pcm_substream *substream,
276 			struct snd_sof_pcm *spcm, int dir, bool free_widget_list);
277 int get_token_u32(void *elem, void *object, u32 offset);
278 int get_token_u16(void *elem, void *object, u32 offset);
279 int get_token_comp_format(void *elem, void *object, u32 offset);
280 int get_token_dai_type(void *elem, void *object, u32 offset);
281 int get_token_uuid(void *elem, void *object, u32 offset);
282 #endif
283