xref: /linux/sound/soc/sof/compress.c (revision ee8443050b2bf06d80fdd2c78cc25cae2abdedcd)
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"
12*ee844305SPeter Ujfalusi #include "sof-utils.h"
13858f7a5cSDaniel Baluta 
146324cf90SDaniel Baluta static void sof_set_transferred_bytes(struct snd_compr_tstamp *tstamp,
156324cf90SDaniel Baluta 				      u64 host_pos, u64 buffer_size)
166324cf90SDaniel Baluta {
176324cf90SDaniel Baluta 	u64 prev_pos;
186324cf90SDaniel Baluta 	unsigned int copied;
196324cf90SDaniel Baluta 
206324cf90SDaniel Baluta 	div64_u64_rem(tstamp->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 
276324cf90SDaniel Baluta 	tstamp->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 {
496324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
506324cf90SDaniel Baluta 	struct snd_compr_runtime *crtd = cstream->runtime;
51858f7a5cSDaniel Baluta 	struct snd_soc_component *component;
526324cf90SDaniel Baluta 	struct snd_compr_tstamp *tstamp;
53858f7a5cSDaniel Baluta 	struct snd_sof_pcm *spcm;
54858f7a5cSDaniel Baluta 
55858f7a5cSDaniel Baluta 	if (!cstream)
56858f7a5cSDaniel Baluta 		return;
57858f7a5cSDaniel Baluta 
586324cf90SDaniel Baluta 	tstamp = crtd->private_data;
59858f7a5cSDaniel Baluta 	component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
60858f7a5cSDaniel Baluta 
61858f7a5cSDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
62858f7a5cSDaniel Baluta 	if (!spcm) {
63858f7a5cSDaniel Baluta 		dev_err(component->dev,
64858f7a5cSDaniel Baluta 			"fragment elapsed called for unknown stream!\n");
65858f7a5cSDaniel Baluta 		return;
66858f7a5cSDaniel Baluta 	}
67858f7a5cSDaniel Baluta 
686324cf90SDaniel Baluta 	sof_set_transferred_bytes(tstamp, spcm->stream[cstream->direction].posn.host_posn,
696324cf90SDaniel Baluta 				  crtd->buffer_size);
706324cf90SDaniel Baluta 
71858f7a5cSDaniel Baluta 	/* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */
72858f7a5cSDaniel Baluta 	schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
73858f7a5cSDaniel Baluta }
746324cf90SDaniel Baluta 
756324cf90SDaniel Baluta static int create_page_table(struct snd_soc_component *component,
766324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream,
776324cf90SDaniel Baluta 			     unsigned char *dma_area, size_t size)
786324cf90SDaniel Baluta {
796324cf90SDaniel Baluta 	struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
806324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
816324cf90SDaniel Baluta 	int dir = cstream->direction;
826324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
836324cf90SDaniel Baluta 
846324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
856324cf90SDaniel Baluta 	if (!spcm)
866324cf90SDaniel Baluta 		return -EINVAL;
876324cf90SDaniel Baluta 
886324cf90SDaniel Baluta 	return snd_sof_create_page_table(component->dev, dmab,
896324cf90SDaniel Baluta 					 spcm->stream[dir].page_table.area, size);
906324cf90SDaniel Baluta }
916324cf90SDaniel Baluta 
926324cf90SDaniel Baluta int sof_compr_open(struct snd_soc_component *component,
936324cf90SDaniel Baluta 		   struct snd_compr_stream *cstream)
946324cf90SDaniel Baluta {
956324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
966324cf90SDaniel Baluta 	struct snd_compr_runtime *crtd = cstream->runtime;
976324cf90SDaniel Baluta 	struct snd_compr_tstamp *tstamp;
986324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
996324cf90SDaniel Baluta 	int dir;
1006324cf90SDaniel Baluta 
1016324cf90SDaniel Baluta 	tstamp = kzalloc(sizeof(*tstamp), GFP_KERNEL);
1026324cf90SDaniel Baluta 	if (!tstamp)
1036324cf90SDaniel Baluta 		return -ENOMEM;
1046324cf90SDaniel Baluta 
1056324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1066324cf90SDaniel Baluta 	if (!spcm) {
1076324cf90SDaniel Baluta 		kfree(tstamp);
1086324cf90SDaniel Baluta 		return -EINVAL;
1096324cf90SDaniel Baluta 	}
1106324cf90SDaniel Baluta 
1116324cf90SDaniel Baluta 	dir = cstream->direction;
1126324cf90SDaniel Baluta 
1136324cf90SDaniel Baluta 	if (spcm->stream[dir].cstream) {
1146324cf90SDaniel Baluta 		kfree(tstamp);
1156324cf90SDaniel Baluta 		return -EBUSY;
1166324cf90SDaniel Baluta 	}
1176324cf90SDaniel Baluta 
1186324cf90SDaniel Baluta 	spcm->stream[dir].cstream = cstream;
1196324cf90SDaniel Baluta 	spcm->stream[dir].posn.host_posn = 0;
1206324cf90SDaniel Baluta 	spcm->stream[dir].posn.dai_posn = 0;
1216324cf90SDaniel Baluta 	spcm->prepared[dir] = false;
1226324cf90SDaniel Baluta 
1236324cf90SDaniel Baluta 	crtd->private_data = tstamp;
1246324cf90SDaniel Baluta 
1256324cf90SDaniel Baluta 	return 0;
1266324cf90SDaniel Baluta }
1276324cf90SDaniel Baluta 
1286324cf90SDaniel Baluta int sof_compr_free(struct snd_soc_component *component,
1296324cf90SDaniel Baluta 		   struct snd_compr_stream *cstream)
1306324cf90SDaniel Baluta {
1316324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
1326324cf90SDaniel Baluta 	struct snd_compr_tstamp *tstamp = cstream->runtime->private_data;
1336324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
1346324cf90SDaniel Baluta 	struct sof_ipc_stream stream;
1356324cf90SDaniel Baluta 	struct sof_ipc_reply reply;
1366324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1376324cf90SDaniel Baluta 	int ret = 0;
1386324cf90SDaniel Baluta 
1396324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1406324cf90SDaniel Baluta 	if (!spcm)
1416324cf90SDaniel Baluta 		return -EINVAL;
1426324cf90SDaniel Baluta 
1436324cf90SDaniel Baluta 	stream.hdr.size = sizeof(stream);
1446324cf90SDaniel Baluta 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
1456324cf90SDaniel Baluta 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
1466324cf90SDaniel Baluta 
1476324cf90SDaniel Baluta 	if (spcm->prepared[cstream->direction]) {
1486324cf90SDaniel Baluta 		ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd,
1496324cf90SDaniel Baluta 					 &stream, sizeof(stream),
1506324cf90SDaniel Baluta 					 &reply, sizeof(reply));
1516324cf90SDaniel Baluta 		if (!ret)
1526324cf90SDaniel Baluta 			spcm->prepared[cstream->direction] = false;
1536324cf90SDaniel Baluta 	}
1546324cf90SDaniel Baluta 
1556324cf90SDaniel Baluta 	cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
1566324cf90SDaniel Baluta 	spcm->stream[cstream->direction].cstream = NULL;
1576324cf90SDaniel Baluta 	kfree(tstamp);
1586324cf90SDaniel Baluta 
1596324cf90SDaniel Baluta 	return ret;
1606324cf90SDaniel Baluta }
1616324cf90SDaniel Baluta 
1626324cf90SDaniel Baluta int sof_compr_set_params(struct snd_soc_component *component,
1636324cf90SDaniel Baluta 			 struct snd_compr_stream *cstream, struct snd_compr_params *params)
1646324cf90SDaniel Baluta {
1656324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
1666324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
1676324cf90SDaniel Baluta 	struct snd_compr_runtime *crtd = cstream->runtime;
1686324cf90SDaniel Baluta 	struct sof_ipc_pcm_params_reply ipc_params_reply;
1696324cf90SDaniel Baluta 	struct snd_compr_tstamp *tstamp;
1706324cf90SDaniel Baluta 	struct sof_ipc_pcm_params pcm;
1716324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
1726324cf90SDaniel Baluta 	int ret;
1736324cf90SDaniel Baluta 
1746324cf90SDaniel Baluta 	tstamp = crtd->private_data;
1756324cf90SDaniel Baluta 
1766324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
1776324cf90SDaniel Baluta 
1786324cf90SDaniel Baluta 	if (!spcm)
1796324cf90SDaniel Baluta 		return -EINVAL;
1806324cf90SDaniel Baluta 
1816324cf90SDaniel Baluta 	cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
1826324cf90SDaniel Baluta 	cstream->dma_buffer.dev.dev = sdev->dev;
1836324cf90SDaniel Baluta 	ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
1846324cf90SDaniel Baluta 	if (ret < 0)
1856324cf90SDaniel Baluta 		return ret;
1866324cf90SDaniel Baluta 
1876324cf90SDaniel Baluta 	ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
1886324cf90SDaniel Baluta 	if (ret < 0)
1896324cf90SDaniel Baluta 		return ret;
1906324cf90SDaniel Baluta 
1916324cf90SDaniel Baluta 	memset(&pcm, 0, sizeof(pcm));
1926324cf90SDaniel Baluta 
1936324cf90SDaniel Baluta 	pcm.params.buffer.pages = PFN_UP(crtd->dma_bytes);
1946324cf90SDaniel Baluta 	pcm.hdr.size = sizeof(pcm);
1956324cf90SDaniel Baluta 	pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
1966324cf90SDaniel Baluta 
1976324cf90SDaniel Baluta 	pcm.comp_id = spcm->stream[cstream->direction].comp_id;
1986324cf90SDaniel Baluta 	pcm.params.hdr.size = sizeof(pcm.params);
1996324cf90SDaniel Baluta 	pcm.params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
2006324cf90SDaniel Baluta 	pcm.params.buffer.size = crtd->dma_bytes;
2016324cf90SDaniel Baluta 	pcm.params.direction = cstream->direction;
2026324cf90SDaniel Baluta 	pcm.params.channels = params->codec.ch_out;
2036324cf90SDaniel Baluta 	pcm.params.rate = params->codec.sample_rate;
2046324cf90SDaniel Baluta 	pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
2056324cf90SDaniel Baluta 	pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
2066324cf90SDaniel Baluta 	pcm.params.sample_container_bytes =
2076324cf90SDaniel Baluta 		snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
2086324cf90SDaniel Baluta 	pcm.params.host_period_bytes = params->buffer.fragment_size;
2096324cf90SDaniel Baluta 
2106324cf90SDaniel Baluta 	ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm),
2116324cf90SDaniel Baluta 				 &ipc_params_reply, sizeof(ipc_params_reply));
2126324cf90SDaniel Baluta 	if (ret < 0) {
2136324cf90SDaniel Baluta 		dev_err(component->dev, "error ipc failed\n");
2146324cf90SDaniel Baluta 		return ret;
2156324cf90SDaniel Baluta 	}
2166324cf90SDaniel Baluta 
2176324cf90SDaniel Baluta 	tstamp->byte_offset = sdev->stream_box.offset + ipc_params_reply.posn_offset;
2186324cf90SDaniel Baluta 	tstamp->sampling_rate = params->codec.sample_rate;
2196324cf90SDaniel Baluta 
2206324cf90SDaniel Baluta 	spcm->prepared[cstream->direction] = true;
2216324cf90SDaniel Baluta 
2226324cf90SDaniel Baluta 	return 0;
2236324cf90SDaniel Baluta }
2246324cf90SDaniel Baluta 
2256324cf90SDaniel Baluta int sof_compr_get_params(struct snd_soc_component *component,
2266324cf90SDaniel Baluta 			 struct snd_compr_stream *cstream, struct snd_codec *params)
2276324cf90SDaniel Baluta {
2286324cf90SDaniel Baluta 	/* TODO: we don't query the supported codecs for now, if the
2296324cf90SDaniel Baluta 	 * application asks for an unsupported codec the set_params() will fail.
2306324cf90SDaniel Baluta 	 */
2316324cf90SDaniel Baluta 	return 0;
2326324cf90SDaniel Baluta }
2336324cf90SDaniel Baluta 
2346324cf90SDaniel Baluta int sof_compr_trigger(struct snd_soc_component *component,
2356324cf90SDaniel Baluta 		      struct snd_compr_stream *cstream, int cmd)
2366324cf90SDaniel Baluta {
2376324cf90SDaniel Baluta 	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
2386324cf90SDaniel Baluta 	struct snd_soc_pcm_runtime *rtd = cstream->private_data;
2396324cf90SDaniel Baluta 	struct sof_ipc_stream stream;
2406324cf90SDaniel Baluta 	struct sof_ipc_reply reply;
2416324cf90SDaniel Baluta 	struct snd_sof_pcm *spcm;
2426324cf90SDaniel Baluta 
2436324cf90SDaniel Baluta 	spcm = snd_sof_find_spcm_dai(component, rtd);
2446324cf90SDaniel Baluta 	if (!spcm)
2456324cf90SDaniel Baluta 		return -EINVAL;
2466324cf90SDaniel Baluta 
2476324cf90SDaniel Baluta 	stream.hdr.size = sizeof(stream);
2486324cf90SDaniel Baluta 	stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
2496324cf90SDaniel Baluta 	stream.comp_id = spcm->stream[cstream->direction].comp_id;
2506324cf90SDaniel Baluta 
2516324cf90SDaniel Baluta 	switch (cmd) {
2526324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_START:
2536324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
2546324cf90SDaniel Baluta 		break;
2556324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_STOP:
2566324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
2576324cf90SDaniel Baluta 		break;
2586324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
2596324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
2606324cf90SDaniel Baluta 		break;
2616324cf90SDaniel Baluta 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
2626324cf90SDaniel Baluta 		stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
2636324cf90SDaniel Baluta 		break;
2646324cf90SDaniel Baluta 	default:
2656324cf90SDaniel Baluta 		dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
2666324cf90SDaniel Baluta 		break;
2676324cf90SDaniel Baluta 	}
2686324cf90SDaniel Baluta 
2696324cf90SDaniel Baluta 	return sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd,
2706324cf90SDaniel Baluta 				  &stream, sizeof(stream),
2716324cf90SDaniel Baluta 				  &reply, sizeof(reply));
2726324cf90SDaniel Baluta }
2736324cf90SDaniel Baluta 
2746324cf90SDaniel Baluta int sof_compr_copy(struct snd_soc_component *component,
2756324cf90SDaniel Baluta 		   struct snd_compr_stream *cstream,
2766324cf90SDaniel Baluta 		   char __user *buf, size_t count)
2776324cf90SDaniel Baluta {
2786324cf90SDaniel Baluta 	struct snd_compr_runtime *rtd = cstream->runtime;
2796324cf90SDaniel Baluta 	unsigned int offset, n;
2806324cf90SDaniel Baluta 	void *ptr;
2816324cf90SDaniel Baluta 	int ret;
2826324cf90SDaniel Baluta 
2836324cf90SDaniel Baluta 	if (count > rtd->buffer_size)
2846324cf90SDaniel Baluta 		count = rtd->buffer_size;
2856324cf90SDaniel Baluta 
2866324cf90SDaniel Baluta 	div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
2876324cf90SDaniel Baluta 	ptr = rtd->dma_area + offset;
2886324cf90SDaniel Baluta 	n = rtd->buffer_size - offset;
2896324cf90SDaniel Baluta 
2906324cf90SDaniel Baluta 	if (count < n) {
2916324cf90SDaniel Baluta 		ret = copy_from_user(ptr, buf, count);
2926324cf90SDaniel Baluta 	} else {
2936324cf90SDaniel Baluta 		ret = copy_from_user(ptr, buf, n);
2946324cf90SDaniel Baluta 		ret += copy_from_user(rtd->dma_area, buf + n, count - n);
2956324cf90SDaniel Baluta 	}
2966324cf90SDaniel Baluta 
2976324cf90SDaniel Baluta 	return count - ret;
2986324cf90SDaniel Baluta }
2996324cf90SDaniel Baluta 
3006324cf90SDaniel Baluta static int sof_compr_pointer(struct snd_soc_component *component,
3016324cf90SDaniel Baluta 			     struct snd_compr_stream *cstream,
3026324cf90SDaniel Baluta 			     struct snd_compr_tstamp *tstamp)
3036324cf90SDaniel Baluta {
3046324cf90SDaniel Baluta 	struct snd_compr_tstamp *pstamp = cstream->runtime->private_data;
3056324cf90SDaniel Baluta 
3066324cf90SDaniel Baluta 	tstamp->sampling_rate = pstamp->sampling_rate;
3076324cf90SDaniel Baluta 	tstamp->copied_total = pstamp->copied_total;
3086324cf90SDaniel Baluta 
3096324cf90SDaniel Baluta 	return 0;
3106324cf90SDaniel Baluta }
3116324cf90SDaniel Baluta 
3126324cf90SDaniel Baluta struct snd_compress_ops sof_compressed_ops = {
3136324cf90SDaniel Baluta 	.open		= sof_compr_open,
3146324cf90SDaniel Baluta 	.free		= sof_compr_free,
3156324cf90SDaniel Baluta 	.set_params	= sof_compr_set_params,
3166324cf90SDaniel Baluta 	.get_params	= sof_compr_get_params,
3176324cf90SDaniel Baluta 	.trigger	= sof_compr_trigger,
3186324cf90SDaniel Baluta 	.pointer	= sof_compr_pointer,
3196324cf90SDaniel Baluta 	.copy		= sof_compr_copy,
3206324cf90SDaniel Baluta };
3216324cf90SDaniel Baluta EXPORT_SYMBOL(sof_compressed_ops);
322