xref: /openbmc/linux/sound/soc/sof/compress.c (revision 015044e9610c8523794ea6cb55d5388bc00ba96a)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 //
3 // Copyright 2021 NXP
4 //
5 // Author: Daniel Baluta <daniel.baluta@nxp.com>
6 
7 #include <sound/soc.h>
8 #include <sound/sof.h>
9 #include <sound/compress_driver.h>
10 #include "sof-audio.h"
11 #include "sof-priv.h"
12 #include "sof-utils.h"
13 
14 static void sof_set_transferred_bytes(struct snd_compr_tstamp *tstamp,
15 				      u64 host_pos, u64 buffer_size)
16 {
17 	u64 prev_pos;
18 	unsigned int copied;
19 
20 	div64_u64_rem(tstamp->copied_total, buffer_size, &prev_pos);
21 
22 	if (host_pos < prev_pos)
23 		copied = (buffer_size - prev_pos) + host_pos;
24 	else
25 		copied = host_pos - prev_pos;
26 
27 	tstamp->copied_total += copied;
28 }
29 
30 static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work)
31 {
32 	struct snd_sof_pcm_stream *sps =
33 		container_of(work, struct snd_sof_pcm_stream,
34 			     period_elapsed_work);
35 
36 	snd_compr_fragment_elapsed(sps->cstream);
37 }
38 
39 void snd_sof_compr_init_elapsed_work(struct work_struct *work)
40 {
41 	INIT_WORK(work, snd_sof_compr_fragment_elapsed_work);
42 }
43 
44 /*
45  * sof compr fragment elapse, this could be called in irq thread context
46  */
47 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream)
48 {
49 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
50 	struct snd_compr_runtime *crtd = cstream->runtime;
51 	struct snd_soc_component *component;
52 	struct snd_compr_tstamp *tstamp;
53 	struct snd_sof_pcm *spcm;
54 
55 	if (!cstream)
56 		return;
57 
58 	tstamp = crtd->private_data;
59 	component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
60 
61 	spcm = snd_sof_find_spcm_dai(component, rtd);
62 	if (!spcm) {
63 		dev_err(component->dev,
64 			"fragment elapsed called for unknown stream!\n");
65 		return;
66 	}
67 
68 	sof_set_transferred_bytes(tstamp, spcm->stream[cstream->direction].posn.host_posn,
69 				  crtd->buffer_size);
70 
71 	/* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */
72 	schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
73 }
74 
75 static int create_page_table(struct snd_soc_component *component,
76 			     struct snd_compr_stream *cstream,
77 			     unsigned char *dma_area, size_t size)
78 {
79 	struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
80 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
81 	int dir = cstream->direction;
82 	struct snd_sof_pcm *spcm;
83 
84 	spcm = snd_sof_find_spcm_dai(component, rtd);
85 	if (!spcm)
86 		return -EINVAL;
87 
88 	return snd_sof_create_page_table(component->dev, dmab,
89 					 spcm->stream[dir].page_table.area, size);
90 }
91 
92 static int sof_compr_open(struct snd_soc_component *component,
93 			  struct snd_compr_stream *cstream)
94 {
95 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
96 	struct snd_compr_runtime *crtd = cstream->runtime;
97 	struct snd_compr_tstamp *tstamp;
98 	struct snd_sof_pcm *spcm;
99 	int dir;
100 
101 	tstamp = kzalloc(sizeof(*tstamp), GFP_KERNEL);
102 	if (!tstamp)
103 		return -ENOMEM;
104 
105 	spcm = snd_sof_find_spcm_dai(component, rtd);
106 	if (!spcm) {
107 		kfree(tstamp);
108 		return -EINVAL;
109 	}
110 
111 	dir = cstream->direction;
112 
113 	if (spcm->stream[dir].cstream) {
114 		kfree(tstamp);
115 		return -EBUSY;
116 	}
117 
118 	spcm->stream[dir].cstream = cstream;
119 	spcm->stream[dir].posn.host_posn = 0;
120 	spcm->stream[dir].posn.dai_posn = 0;
121 	spcm->prepared[dir] = false;
122 
123 	crtd->private_data = tstamp;
124 
125 	return 0;
126 }
127 
128 static int sof_compr_free(struct snd_soc_component *component,
129 			  struct snd_compr_stream *cstream)
130 {
131 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
132 	struct snd_compr_tstamp *tstamp = cstream->runtime->private_data;
133 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
134 	struct sof_ipc_stream stream;
135 	struct sof_ipc_reply reply;
136 	struct snd_sof_pcm *spcm;
137 	int ret = 0;
138 
139 	spcm = snd_sof_find_spcm_dai(component, rtd);
140 	if (!spcm)
141 		return -EINVAL;
142 
143 	stream.hdr.size = sizeof(stream);
144 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
145 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
146 
147 	if (spcm->prepared[cstream->direction]) {
148 		ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd,
149 					 &stream, sizeof(stream),
150 					 &reply, sizeof(reply));
151 		if (!ret)
152 			spcm->prepared[cstream->direction] = false;
153 	}
154 
155 	cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
156 	spcm->stream[cstream->direction].cstream = NULL;
157 	kfree(tstamp);
158 
159 	return ret;
160 }
161 
162 static int sof_compr_set_params(struct snd_soc_component *component,
163 				struct snd_compr_stream *cstream, struct snd_compr_params *params)
164 {
165 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
166 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
167 	struct snd_compr_runtime *crtd = cstream->runtime;
168 	struct sof_ipc_pcm_params_reply ipc_params_reply;
169 	struct snd_compr_tstamp *tstamp;
170 	struct sof_ipc_pcm_params pcm;
171 	struct snd_sof_pcm *spcm;
172 	int ret;
173 
174 	tstamp = crtd->private_data;
175 
176 	spcm = snd_sof_find_spcm_dai(component, rtd);
177 
178 	if (!spcm)
179 		return -EINVAL;
180 
181 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
182 	cstream->dma_buffer.dev.dev = sdev->dev;
183 	ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
184 	if (ret < 0)
185 		return ret;
186 
187 	ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
188 	if (ret < 0)
189 		return ret;
190 
191 	memset(&pcm, 0, sizeof(pcm));
192 
193 	pcm.params.buffer.pages = PFN_UP(crtd->dma_bytes);
194 	pcm.hdr.size = sizeof(pcm);
195 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
196 
197 	pcm.comp_id = spcm->stream[cstream->direction].comp_id;
198 	pcm.params.hdr.size = sizeof(pcm.params);
199 	pcm.params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
200 	pcm.params.buffer.size = crtd->dma_bytes;
201 	pcm.params.direction = cstream->direction;
202 	pcm.params.channels = params->codec.ch_out;
203 	pcm.params.rate = params->codec.sample_rate;
204 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
205 	pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
206 	pcm.params.sample_container_bytes =
207 		snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
208 	pcm.params.host_period_bytes = params->buffer.fragment_size;
209 
210 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
211 				 &ipc_params_reply, sizeof(ipc_params_reply));
212 	if (ret < 0) {
213 		dev_err(component->dev, "error ipc failed\n");
214 		return ret;
215 	}
216 
217 	tstamp->byte_offset = sdev->stream_box.offset + ipc_params_reply.posn_offset;
218 	tstamp->sampling_rate = params->codec.sample_rate;
219 
220 	spcm->prepared[cstream->direction] = true;
221 
222 	return 0;
223 }
224 
225 static int sof_compr_get_params(struct snd_soc_component *component,
226 				struct snd_compr_stream *cstream, struct snd_codec *params)
227 {
228 	/* TODO: we don't query the supported codecs for now, if the
229 	 * application asks for an unsupported codec the set_params() will fail.
230 	 */
231 	return 0;
232 }
233 
234 static int sof_compr_trigger(struct snd_soc_component *component,
235 			     struct snd_compr_stream *cstream, int cmd)
236 {
237 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
238 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
239 	struct sof_ipc_stream stream;
240 	struct sof_ipc_reply reply;
241 	struct snd_sof_pcm *spcm;
242 
243 	spcm = snd_sof_find_spcm_dai(component, rtd);
244 	if (!spcm)
245 		return -EINVAL;
246 
247 	stream.hdr.size = sizeof(stream);
248 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
249 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
250 
251 	switch (cmd) {
252 	case SNDRV_PCM_TRIGGER_START:
253 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
254 		break;
255 	case SNDRV_PCM_TRIGGER_STOP:
256 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
257 		break;
258 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
259 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
260 		break;
261 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
262 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
263 		break;
264 	default:
265 		dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
266 		break;
267 	}
268 
269 	return sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd,
270 				  &stream, sizeof(stream),
271 				  &reply, sizeof(reply));
272 }
273 
274 static int sof_compr_copy(struct snd_soc_component *component,
275 			  struct snd_compr_stream *cstream,
276 			  char __user *buf, size_t count)
277 {
278 	struct snd_compr_runtime *rtd = cstream->runtime;
279 	unsigned int offset, n;
280 	void *ptr;
281 	int ret;
282 
283 	if (count > rtd->buffer_size)
284 		count = rtd->buffer_size;
285 
286 	div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
287 	ptr = rtd->dma_area + offset;
288 	n = rtd->buffer_size - offset;
289 
290 	if (count < n) {
291 		ret = copy_from_user(ptr, buf, count);
292 	} else {
293 		ret = copy_from_user(ptr, buf, n);
294 		ret += copy_from_user(rtd->dma_area, buf + n, count - n);
295 	}
296 
297 	return count - ret;
298 }
299 
300 static int sof_compr_pointer(struct snd_soc_component *component,
301 			     struct snd_compr_stream *cstream,
302 			     struct snd_compr_tstamp *tstamp)
303 {
304 	struct snd_compr_tstamp *pstamp = cstream->runtime->private_data;
305 
306 	tstamp->sampling_rate = pstamp->sampling_rate;
307 	tstamp->copied_total = pstamp->copied_total;
308 
309 	return 0;
310 }
311 
312 struct snd_compress_ops sof_compressed_ops = {
313 	.open		= sof_compr_open,
314 	.free		= sof_compr_free,
315 	.set_params	= sof_compr_set_params,
316 	.get_params	= sof_compr_get_params,
317 	.trigger	= sof_compr_trigger,
318 	.pointer	= sof_compr_pointer,
319 	.copy		= sof_compr_copy,
320 };
321 EXPORT_SYMBOL(sof_compressed_ops);
322