xref: /openbmc/linux/sound/soc/sof/compress.c (revision 1a01e19278022cd2f7daa7a065ed47c5022dbad9)
1858f7a5cSDaniel Baluta // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2858f7a5cSDaniel Baluta //
3858f7a5cSDaniel Baluta // Copyright 2021 NXP
4858f7a5cSDaniel Baluta //
5858f7a5cSDaniel Baluta // Author: Daniel Baluta <daniel.baluta@nxp.com>
6858f7a5cSDaniel Baluta 
7858f7a5cSDaniel Baluta #include <sound/soc.h>
8858f7a5cSDaniel Baluta #include <sound/sof.h>
9858f7a5cSDaniel Baluta #include <sound/compress_driver.h>
10858f7a5cSDaniel Baluta #include "sof-audio.h"
11858f7a5cSDaniel Baluta #include "sof-priv.h"
12ee844305SPeter Ujfalusi #include "sof-utils.h"
13858f7a5cSDaniel Baluta 
14e3091f0aSDaniel Baluta static void sof_set_transferred_bytes(struct sof_compr_stream *sstream,
156324cf90SDaniel Baluta 				      u64 host_pos, u64 buffer_size)
166324cf90SDaniel Baluta {
176324cf90SDaniel Baluta 	u64 prev_pos;
186324cf90SDaniel Baluta 	unsigned int copied;
196324cf90SDaniel Baluta 
20e3091f0aSDaniel Baluta 	div64_u64_rem(sstream->copied_total, buffer_size, &prev_pos);
216324cf90SDaniel Baluta 
226324cf90SDaniel Baluta 	if (host_pos < prev_pos)
236324cf90SDaniel Baluta 		copied = (buffer_size - prev_pos) + host_pos;
246324cf90SDaniel Baluta 	else
256324cf90SDaniel Baluta 		copied = host_pos - prev_pos;
266324cf90SDaniel Baluta 
27e3091f0aSDaniel Baluta 	sstream->copied_total += copied;
286324cf90SDaniel Baluta }
296324cf90SDaniel Baluta 
30858f7a5cSDaniel Baluta static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work)
31858f7a5cSDaniel Baluta {
32858f7a5cSDaniel Baluta 	struct snd_sof_pcm_stream *sps =
33858f7a5cSDaniel Baluta 		container_of(work, struct snd_sof_pcm_stream,
34858f7a5cSDaniel Baluta 			     period_elapsed_work);
35858f7a5cSDaniel Baluta 
36858f7a5cSDaniel Baluta 	snd_compr_fragment_elapsed(sps->cstream);
37858f7a5cSDaniel Baluta }
38858f7a5cSDaniel Baluta 
39858f7a5cSDaniel Baluta void snd_sof_compr_init_elapsed_work(struct work_struct *work)
40858f7a5cSDaniel Baluta {
41858f7a5cSDaniel Baluta 	INIT_WORK(work, snd_sof_compr_fragment_elapsed_work);
42858f7a5cSDaniel Baluta }
43858f7a5cSDaniel Baluta 
44858f7a5cSDaniel Baluta /*
45858f7a5cSDaniel Baluta  * sof compr fragment elapse, this could be called in irq thread context
46858f7a5cSDaniel Baluta  */
47858f7a5cSDaniel Baluta void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream)
48858f7a5cSDaniel Baluta {
497e4bfcf1SWeiguo Li 	struct snd_soc_pcm_runtime *rtd;
507e4bfcf1SWeiguo Li 	struct snd_compr_runtime *crtd;
51858f7a5cSDaniel Baluta 	struct snd_soc_component *component;
52e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream;
53858f7a5cSDaniel Baluta 	struct snd_sof_pcm *spcm;
54858f7a5cSDaniel Baluta 
55858f7a5cSDaniel Baluta 	if (!cstream)
56858f7a5cSDaniel Baluta 		return;
57858f7a5cSDaniel Baluta 
587e4bfcf1SWeiguo Li 	rtd = cstream->private_data;
597e4bfcf1SWeiguo Li 	crtd = cstream->runtime;
60e3091f0aSDaniel Baluta 	sstream = crtd->private_data;
61858f7a5cSDaniel Baluta 	component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
62858f7a5cSDaniel Baluta 
63858f7a5cSDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
64858f7a5cSDaniel Baluta 	if (!spcm) {
65858f7a5cSDaniel Baluta 		dev_err(component->dev,
66858f7a5cSDaniel Baluta 			"fragment elapsed called for unknown stream!\n");
67858f7a5cSDaniel Baluta 		return;
68858f7a5cSDaniel Baluta 	}
69858f7a5cSDaniel Baluta 
70e3091f0aSDaniel Baluta 	sof_set_transferred_bytes(sstream, spcm->stream[cstream->direction].posn.host_posn,
716324cf90SDaniel Baluta 				  crtd->buffer_size);
726324cf90SDaniel Baluta 
73858f7a5cSDaniel Baluta 	/* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */
74858f7a5cSDaniel Baluta 	schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
75858f7a5cSDaniel Baluta }
766324cf90SDaniel Baluta 
776324cf90SDaniel Baluta static int create_page_table(struct snd_soc_component *component,
786324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream,
796324cf90SDaniel Baluta 			     unsigned char *dma_area, size_t size)
806324cf90SDaniel Baluta {
816324cf90SDaniel Baluta 	struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
826324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
836324cf90SDaniel Baluta 	int dir = cstream->direction;
846324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
856324cf90SDaniel Baluta 
866324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
876324cf90SDaniel Baluta 	if (!spcm)
886324cf90SDaniel Baluta 		return -EINVAL;
896324cf90SDaniel Baluta 
906324cf90SDaniel Baluta 	return snd_sof_create_page_table(component->dev, dmab,
916324cf90SDaniel Baluta 					 spcm->stream[dir].page_table.area, size);
926324cf90SDaniel Baluta }
936324cf90SDaniel Baluta 
94e7c799e7SDaniel Baluta static int sof_compr_open(struct snd_soc_component *component,
956324cf90SDaniel Baluta 			  struct snd_compr_stream *cstream)
966324cf90SDaniel Baluta {
976324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
986324cf90SDaniel Baluta 	struct snd_compr_runtime *crtd = cstream->runtime;
99e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream;
1006324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1016324cf90SDaniel Baluta 	int dir;
1026324cf90SDaniel Baluta 
103e3091f0aSDaniel Baluta 	sstream = kzalloc(sizeof(*sstream), GFP_KERNEL);
104e3091f0aSDaniel Baluta 	if (!sstream)
1056324cf90SDaniel Baluta 		return -ENOMEM;
1066324cf90SDaniel Baluta 
1076324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1086324cf90SDaniel Baluta 	if (!spcm) {
109e3091f0aSDaniel Baluta 		kfree(sstream);
1106324cf90SDaniel Baluta 		return -EINVAL;
1116324cf90SDaniel Baluta 	}
1126324cf90SDaniel Baluta 
1136324cf90SDaniel Baluta 	dir = cstream->direction;
1146324cf90SDaniel Baluta 
1156324cf90SDaniel Baluta 	if (spcm->stream[dir].cstream) {
116e3091f0aSDaniel Baluta 		kfree(sstream);
1176324cf90SDaniel Baluta 		return -EBUSY;
1186324cf90SDaniel Baluta 	}
1196324cf90SDaniel Baluta 
1206324cf90SDaniel Baluta 	spcm->stream[dir].cstream = cstream;
1216324cf90SDaniel Baluta 	spcm->stream[dir].posn.host_posn = 0;
1226324cf90SDaniel Baluta 	spcm->stream[dir].posn.dai_posn = 0;
1236324cf90SDaniel Baluta 	spcm->prepared[dir] = false;
1246324cf90SDaniel Baluta 
125e3091f0aSDaniel Baluta 	crtd->private_data = sstream;
1266324cf90SDaniel Baluta 
1276324cf90SDaniel Baluta 	return 0;
1286324cf90SDaniel Baluta }
1296324cf90SDaniel Baluta 
130e7c799e7SDaniel Baluta static int sof_compr_free(struct snd_soc_component *component,
1316324cf90SDaniel Baluta 			  struct snd_compr_stream *cstream)
1326324cf90SDaniel Baluta {
1336324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
134e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream = cstream->runtime->private_data;
1356324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
1366324cf90SDaniel Baluta 	struct sof_ipc_stream stream;
1376324cf90SDaniel Baluta 	struct sof_ipc_reply reply;
1386324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1396324cf90SDaniel Baluta 	int ret = 0;
1406324cf90SDaniel Baluta 
1416324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1426324cf90SDaniel Baluta 	if (!spcm)
1436324cf90SDaniel Baluta 		return -EINVAL;
1446324cf90SDaniel Baluta 
1456324cf90SDaniel Baluta 	stream.hdr.size = sizeof(stream);
1466324cf90SDaniel Baluta 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
1476324cf90SDaniel Baluta 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
1486324cf90SDaniel Baluta 
1496324cf90SDaniel Baluta 	if (spcm->prepared[cstream->direction]) {
1502a51c0f8SPeter Ujfalusi 		ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
1516324cf90SDaniel Baluta 					 &reply, sizeof(reply));
1526324cf90SDaniel Baluta 		if (!ret)
1536324cf90SDaniel Baluta 			spcm->prepared[cstream->direction] = false;
1546324cf90SDaniel Baluta 	}
1556324cf90SDaniel Baluta 
1566324cf90SDaniel Baluta 	cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
1576324cf90SDaniel Baluta 	spcm->stream[cstream->direction].cstream = NULL;
158e3091f0aSDaniel Baluta 	kfree(sstream);
1596324cf90SDaniel Baluta 
1606324cf90SDaniel Baluta 	return ret;
1616324cf90SDaniel Baluta }
1626324cf90SDaniel Baluta 
163e7c799e7SDaniel Baluta static int sof_compr_set_params(struct snd_soc_component *component,
1646324cf90SDaniel Baluta 				struct snd_compr_stream *cstream, struct snd_compr_params *params)
1656324cf90SDaniel Baluta {
1666324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
1676324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
1686324cf90SDaniel Baluta 	struct snd_compr_runtime *crtd = cstream->runtime;
1696324cf90SDaniel Baluta 	struct sof_ipc_pcm_params_reply ipc_params_reply;
170246b135fSDaniel Baluta 	struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
171246b135fSDaniel Baluta 	struct sof_ipc_fw_version *v = &ready->version;
172e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream;
173d5770daeSDaniel Baluta 	struct sof_ipc_pcm_params *pcm;
1746324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1753f70c360SDaniel Baluta 	size_t ext_data_size;
1766324cf90SDaniel Baluta 	int ret;
1776324cf90SDaniel Baluta 
178246b135fSDaniel Baluta 	if (v->abi_version < SOF_ABI_VER(3, 22, 0)) {
179246b135fSDaniel Baluta 		dev_err(component->dev,
180246b135fSDaniel Baluta 			"Compress params not supported with FW ABI version %d:%d:%d\n",
181246b135fSDaniel Baluta 			SOF_ABI_VERSION_MAJOR(v->abi_version),
182246b135fSDaniel Baluta 			SOF_ABI_VERSION_MINOR(v->abi_version),
183246b135fSDaniel Baluta 			SOF_ABI_VERSION_PATCH(v->abi_version));
184246b135fSDaniel Baluta 		return -EINVAL;
185246b135fSDaniel Baluta 	}
186246b135fSDaniel Baluta 
187e3091f0aSDaniel Baluta 	sstream = crtd->private_data;
1886324cf90SDaniel Baluta 
1896324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1906324cf90SDaniel Baluta 
1916324cf90SDaniel Baluta 	if (!spcm)
1926324cf90SDaniel Baluta 		return -EINVAL;
1936324cf90SDaniel Baluta 
1943f70c360SDaniel Baluta 	ext_data_size = sizeof(params->codec);
1953f70c360SDaniel Baluta 
1963f70c360SDaniel Baluta 	if (sizeof(*pcm) + ext_data_size > sdev->ipc->max_payload_size)
1973f70c360SDaniel Baluta 		return -EINVAL;
1983f70c360SDaniel Baluta 
1993f70c360SDaniel Baluta 	pcm = kzalloc(sizeof(*pcm) + ext_data_size, GFP_KERNEL);
200d5770daeSDaniel Baluta 	if (!pcm)
201d5770daeSDaniel Baluta 		return -ENOMEM;
202d5770daeSDaniel Baluta 
2036324cf90SDaniel Baluta 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
2046324cf90SDaniel Baluta 	cstream->dma_buffer.dev.dev = sdev->dev;
2056324cf90SDaniel Baluta 	ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
2066324cf90SDaniel Baluta 	if (ret < 0)
207d5770daeSDaniel Baluta 		goto out;
2086324cf90SDaniel Baluta 
2096324cf90SDaniel Baluta 	ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
2106324cf90SDaniel Baluta 	if (ret < 0)
211d5770daeSDaniel Baluta 		goto out;
2126324cf90SDaniel Baluta 
213d5770daeSDaniel Baluta 	pcm->params.buffer.pages = PFN_UP(crtd->dma_bytes);
2143f70c360SDaniel Baluta 	pcm->hdr.size = sizeof(*pcm) + ext_data_size;
215d5770daeSDaniel Baluta 	pcm->hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
2166324cf90SDaniel Baluta 
217d5770daeSDaniel Baluta 	pcm->comp_id = spcm->stream[cstream->direction].comp_id;
2183f70c360SDaniel Baluta 	pcm->params.hdr.size = sizeof(pcm->params) + ext_data_size;
219d5770daeSDaniel Baluta 	pcm->params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
220d5770daeSDaniel Baluta 	pcm->params.buffer.size = crtd->dma_bytes;
221d5770daeSDaniel Baluta 	pcm->params.direction = cstream->direction;
222d5770daeSDaniel Baluta 	pcm->params.channels = params->codec.ch_out;
223d5770daeSDaniel Baluta 	pcm->params.rate = params->codec.sample_rate;
224d5770daeSDaniel Baluta 	pcm->params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
225d5770daeSDaniel Baluta 	pcm->params.frame_fmt = SOF_IPC_FRAME_S32_LE;
226d5770daeSDaniel Baluta 	pcm->params.sample_container_bytes =
2276324cf90SDaniel Baluta 		snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
228d5770daeSDaniel Baluta 	pcm->params.host_period_bytes = params->buffer.fragment_size;
2293f70c360SDaniel Baluta 	pcm->params.ext_data_length = ext_data_size;
2306324cf90SDaniel Baluta 
2313f70c360SDaniel Baluta 	memcpy((u8 *)pcm->params.ext_data, &params->codec, ext_data_size);
2323f70c360SDaniel Baluta 
2333f70c360SDaniel Baluta 	ret = sof_ipc_tx_message(sdev->ipc, pcm, sizeof(*pcm) + ext_data_size,
2346324cf90SDaniel Baluta 				 &ipc_params_reply, sizeof(ipc_params_reply));
2356324cf90SDaniel Baluta 	if (ret < 0) {
2366324cf90SDaniel Baluta 		dev_err(component->dev, "error ipc failed\n");
237d5770daeSDaniel Baluta 		goto out;
2386324cf90SDaniel Baluta 	}
2396324cf90SDaniel Baluta 
240e3091f0aSDaniel Baluta 	sstream->sampling_rate = params->codec.sample_rate;
2413ccbe688SLaurentiu Mihalcea 	sstream->channels = params->codec.ch_out;
2423ccbe688SLaurentiu Mihalcea 	sstream->sample_container_bytes = pcm->params.sample_container_bytes;
2436324cf90SDaniel Baluta 
2446324cf90SDaniel Baluta 	spcm->prepared[cstream->direction] = true;
2456324cf90SDaniel Baluta 
246d5770daeSDaniel Baluta out:
247d5770daeSDaniel Baluta 	kfree(pcm);
248d5770daeSDaniel Baluta 
249d5770daeSDaniel Baluta 	return ret;
2506324cf90SDaniel Baluta }
2516324cf90SDaniel Baluta 
252e7c799e7SDaniel Baluta static int sof_compr_get_params(struct snd_soc_component *component,
2536324cf90SDaniel Baluta 				struct snd_compr_stream *cstream, struct snd_codec *params)
2546324cf90SDaniel Baluta {
2556324cf90SDaniel Baluta 	/* TODO: we don't query the supported codecs for now, if the
2566324cf90SDaniel Baluta 	 * application asks for an unsupported codec the set_params() will fail.
2576324cf90SDaniel Baluta 	 */
2586324cf90SDaniel Baluta 	return 0;
2596324cf90SDaniel Baluta }
2606324cf90SDaniel Baluta 
261e7c799e7SDaniel Baluta static int sof_compr_trigger(struct snd_soc_component *component,
2626324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream, int cmd)
2636324cf90SDaniel Baluta {
2646324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
2656324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
2666324cf90SDaniel Baluta 	struct sof_ipc_stream stream;
2676324cf90SDaniel Baluta 	struct sof_ipc_reply reply;
2686324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
2696324cf90SDaniel Baluta 
2706324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
2716324cf90SDaniel Baluta 	if (!spcm)
2726324cf90SDaniel Baluta 		return -EINVAL;
2736324cf90SDaniel Baluta 
2746324cf90SDaniel Baluta 	stream.hdr.size = sizeof(stream);
2756324cf90SDaniel Baluta 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
2766324cf90SDaniel Baluta 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
2776324cf90SDaniel Baluta 
2786324cf90SDaniel Baluta 	switch (cmd) {
2796324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_START:
2806324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
2816324cf90SDaniel Baluta 		break;
2826324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_STOP:
2836324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
2846324cf90SDaniel Baluta 		break;
2856324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2866324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
2876324cf90SDaniel Baluta 		break;
2886324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2896324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
2906324cf90SDaniel Baluta 		break;
2916324cf90SDaniel Baluta 	default:
2926324cf90SDaniel Baluta 		dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
2936324cf90SDaniel Baluta 		break;
2946324cf90SDaniel Baluta 	}
2956324cf90SDaniel Baluta 
2962a51c0f8SPeter Ujfalusi 	return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
2976324cf90SDaniel Baluta 				  &reply, sizeof(reply));
2986324cf90SDaniel Baluta }
2996324cf90SDaniel Baluta 
300272ff882SLaurentiu Mihalcea static int sof_compr_copy_playback(struct snd_compr_runtime *rtd,
3016324cf90SDaniel Baluta 				   char __user *buf, size_t count)
3026324cf90SDaniel Baluta {
3036324cf90SDaniel Baluta 	void *ptr;
304272ff882SLaurentiu Mihalcea 	unsigned int offset, n;
3056324cf90SDaniel Baluta 	int ret;
3066324cf90SDaniel Baluta 
3076324cf90SDaniel Baluta 	div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
3086324cf90SDaniel Baluta 	ptr = rtd->dma_area + offset;
3096324cf90SDaniel Baluta 	n = rtd->buffer_size - offset;
3106324cf90SDaniel Baluta 
3116324cf90SDaniel Baluta 	if (count < n) {
3126324cf90SDaniel Baluta 		ret = copy_from_user(ptr, buf, count);
3136324cf90SDaniel Baluta 	} else {
3146324cf90SDaniel Baluta 		ret = copy_from_user(ptr, buf, n);
3156324cf90SDaniel Baluta 		ret += copy_from_user(rtd->dma_area, buf + n, count - n);
3166324cf90SDaniel Baluta 	}
3176324cf90SDaniel Baluta 
3186324cf90SDaniel Baluta 	return count - ret;
3196324cf90SDaniel Baluta }
3206324cf90SDaniel Baluta 
321*1a01e192SLaurentiu Mihalcea static int sof_compr_copy_capture(struct snd_compr_runtime *rtd,
322*1a01e192SLaurentiu Mihalcea 				  char __user *buf, size_t count)
323*1a01e192SLaurentiu Mihalcea {
324*1a01e192SLaurentiu Mihalcea 	void *ptr;
325*1a01e192SLaurentiu Mihalcea 	unsigned int offset, n;
326*1a01e192SLaurentiu Mihalcea 	int ret;
327*1a01e192SLaurentiu Mihalcea 
328*1a01e192SLaurentiu Mihalcea 	div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
329*1a01e192SLaurentiu Mihalcea 	ptr = rtd->dma_area + offset;
330*1a01e192SLaurentiu Mihalcea 	n = rtd->buffer_size - offset;
331*1a01e192SLaurentiu Mihalcea 
332*1a01e192SLaurentiu Mihalcea 	if (count < n) {
333*1a01e192SLaurentiu Mihalcea 		ret = copy_to_user(buf, ptr, count);
334*1a01e192SLaurentiu Mihalcea 	} else {
335*1a01e192SLaurentiu Mihalcea 		ret = copy_to_user(buf, ptr, n);
336*1a01e192SLaurentiu Mihalcea 		ret += copy_to_user(buf + n, rtd->dma_area, count - n);
337*1a01e192SLaurentiu Mihalcea 	}
338*1a01e192SLaurentiu Mihalcea 
339*1a01e192SLaurentiu Mihalcea 	return count - ret;
340*1a01e192SLaurentiu Mihalcea }
341*1a01e192SLaurentiu Mihalcea 
342272ff882SLaurentiu Mihalcea static int sof_compr_copy(struct snd_soc_component *component,
343272ff882SLaurentiu Mihalcea 			  struct snd_compr_stream *cstream,
344272ff882SLaurentiu Mihalcea 			  char __user *buf, size_t count)
345272ff882SLaurentiu Mihalcea {
346272ff882SLaurentiu Mihalcea 	struct snd_compr_runtime *rtd = cstream->runtime;
347272ff882SLaurentiu Mihalcea 
348272ff882SLaurentiu Mihalcea 	if (count > rtd->buffer_size)
349272ff882SLaurentiu Mihalcea 		count = rtd->buffer_size;
350272ff882SLaurentiu Mihalcea 
351*1a01e192SLaurentiu Mihalcea 	if (cstream->direction == SND_COMPRESS_PLAYBACK)
352272ff882SLaurentiu Mihalcea 		return sof_compr_copy_playback(rtd, buf, count);
353*1a01e192SLaurentiu Mihalcea 	else
354*1a01e192SLaurentiu Mihalcea 		return sof_compr_copy_capture(rtd, buf, count);
355272ff882SLaurentiu Mihalcea }
356272ff882SLaurentiu Mihalcea 
3576324cf90SDaniel Baluta static int sof_compr_pointer(struct snd_soc_component *component,
3586324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream,
3596324cf90SDaniel Baluta 			     struct snd_compr_tstamp *tstamp)
3606324cf90SDaniel Baluta {
361c1a731c7SLaurentiu Mihalcea 	struct snd_sof_pcm *spcm;
362c1a731c7SLaurentiu Mihalcea 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
363e3091f0aSDaniel Baluta 	struct sof_compr_stream *sstream = cstream->runtime->private_data;
3646324cf90SDaniel Baluta 
365c1a731c7SLaurentiu Mihalcea 	spcm = snd_sof_find_spcm_dai(component, rtd);
366c1a731c7SLaurentiu Mihalcea 	if (!spcm)
367c1a731c7SLaurentiu Mihalcea 		return -EINVAL;
368c1a731c7SLaurentiu Mihalcea 
369e3091f0aSDaniel Baluta 	tstamp->sampling_rate = sstream->sampling_rate;
370e3091f0aSDaniel Baluta 	tstamp->copied_total = sstream->copied_total;
371c1a731c7SLaurentiu Mihalcea 	tstamp->pcm_io_frames = div_u64(spcm->stream[cstream->direction].posn.dai_posn,
372c1a731c7SLaurentiu Mihalcea 					sstream->channels * sstream->sample_container_bytes);
3736324cf90SDaniel Baluta 
3746324cf90SDaniel Baluta 	return 0;
3756324cf90SDaniel Baluta }
3766324cf90SDaniel Baluta 
3776324cf90SDaniel Baluta struct snd_compress_ops sof_compressed_ops = {
3786324cf90SDaniel Baluta 	.open		= sof_compr_open,
3796324cf90SDaniel Baluta 	.free		= sof_compr_free,
3806324cf90SDaniel Baluta 	.set_params	= sof_compr_set_params,
3816324cf90SDaniel Baluta 	.get_params	= sof_compr_get_params,
3826324cf90SDaniel Baluta 	.trigger	= sof_compr_trigger,
3836324cf90SDaniel Baluta 	.pointer	= sof_compr_pointer,
3846324cf90SDaniel Baluta 	.copy		= sof_compr_copy,
3856324cf90SDaniel Baluta };
3866324cf90SDaniel Baluta EXPORT_SYMBOL(sof_compressed_ops);
387