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