xref: /linux/sound/soc/meson/aiu-fifo.c (revision 33e02dc69afbd8f1b85a51d74d72f139ba4ca623)
16ae9ca9cSJerome Brunet // SPDX-License-Identifier: GPL-2.0
26ae9ca9cSJerome Brunet //
36ae9ca9cSJerome Brunet // Copyright (c) 2020 BayLibre, SAS.
46ae9ca9cSJerome Brunet // Author: Jerome Brunet <jbrunet@baylibre.com>
56ae9ca9cSJerome Brunet 
66ae9ca9cSJerome Brunet #include <linux/bitfield.h>
76ae9ca9cSJerome Brunet #include <linux/clk.h>
81bcd3266SMartin Blumenstingl #include <linux/dma-mapping.h>
96ae9ca9cSJerome Brunet #include <sound/pcm_params.h>
106ae9ca9cSJerome Brunet #include <sound/soc.h>
116ae9ca9cSJerome Brunet #include <sound/soc-dai.h>
126ae9ca9cSJerome Brunet 
136ae9ca9cSJerome Brunet #include "aiu-fifo.h"
146ae9ca9cSJerome Brunet 
156ae9ca9cSJerome Brunet #define AIU_MEM_START	0x00
166ae9ca9cSJerome Brunet #define AIU_MEM_RD	0x04
176ae9ca9cSJerome Brunet #define AIU_MEM_END	0x08
186ae9ca9cSJerome Brunet #define AIU_MEM_MASKS	0x0c
196ae9ca9cSJerome Brunet #define  AIU_MEM_MASK_CH_RD GENMASK(7, 0)
206ae9ca9cSJerome Brunet #define  AIU_MEM_MASK_CH_MEM GENMASK(15, 8)
216ae9ca9cSJerome Brunet #define AIU_MEM_CONTROL	0x10
226ae9ca9cSJerome Brunet #define  AIU_MEM_CONTROL_INIT BIT(0)
236ae9ca9cSJerome Brunet #define  AIU_MEM_CONTROL_FILL_EN BIT(1)
246ae9ca9cSJerome Brunet #define  AIU_MEM_CONTROL_EMPTY_EN BIT(2)
256ae9ca9cSJerome Brunet 
aiu_fifo_dai(struct snd_pcm_substream * ss)266ae9ca9cSJerome Brunet static struct snd_soc_dai *aiu_fifo_dai(struct snd_pcm_substream *ss)
276ae9ca9cSJerome Brunet {
28*22f5680aSKrzysztof Kozlowski 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(ss);
296ae9ca9cSJerome Brunet 
300d102e68SKuninori Morimoto 	return snd_soc_rtd_to_cpu(rtd, 0);
316ae9ca9cSJerome Brunet }
326ae9ca9cSJerome Brunet 
aiu_fifo_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)336ae9ca9cSJerome Brunet snd_pcm_uframes_t aiu_fifo_pointer(struct snd_soc_component *component,
346ae9ca9cSJerome Brunet 				   struct snd_pcm_substream *substream)
356ae9ca9cSJerome Brunet {
366ae9ca9cSJerome Brunet 	struct snd_soc_dai *dai = aiu_fifo_dai(substream);
37c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
386ae9ca9cSJerome Brunet 	struct snd_pcm_runtime *runtime = substream->runtime;
396ae9ca9cSJerome Brunet 	unsigned int addr;
406ae9ca9cSJerome Brunet 
41cf6e26c7SKuninori Morimoto 	addr = snd_soc_component_read(component, fifo->mem_offset + AIU_MEM_RD);
426ae9ca9cSJerome Brunet 
436ae9ca9cSJerome Brunet 	return bytes_to_frames(runtime, addr - (unsigned int)runtime->dma_addr);
446ae9ca9cSJerome Brunet }
456ae9ca9cSJerome Brunet 
aiu_fifo_enable(struct snd_soc_dai * dai,bool enable)466ae9ca9cSJerome Brunet static void aiu_fifo_enable(struct snd_soc_dai *dai, bool enable)
476ae9ca9cSJerome Brunet {
486ae9ca9cSJerome Brunet 	struct snd_soc_component *component = dai->component;
49c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
506ae9ca9cSJerome Brunet 	unsigned int en_mask = (AIU_MEM_CONTROL_FILL_EN |
516ae9ca9cSJerome Brunet 				AIU_MEM_CONTROL_EMPTY_EN);
526ae9ca9cSJerome Brunet 
536ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
546ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_CONTROL,
556ae9ca9cSJerome Brunet 				      en_mask, enable ? en_mask : 0);
566ae9ca9cSJerome Brunet }
576ae9ca9cSJerome Brunet 
aiu_fifo_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)586ae9ca9cSJerome Brunet int aiu_fifo_trigger(struct snd_pcm_substream *substream, int cmd,
596ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
606ae9ca9cSJerome Brunet {
616ae9ca9cSJerome Brunet 	switch (cmd) {
626ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_START:
636ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_RESUME:
646ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
656ae9ca9cSJerome Brunet 		aiu_fifo_enable(dai, true);
666ae9ca9cSJerome Brunet 		break;
676ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_SUSPEND:
686ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
696ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_STOP:
706ae9ca9cSJerome Brunet 		aiu_fifo_enable(dai, false);
716ae9ca9cSJerome Brunet 		break;
726ae9ca9cSJerome Brunet 	default:
736ae9ca9cSJerome Brunet 		return -EINVAL;
746ae9ca9cSJerome Brunet 	}
756ae9ca9cSJerome Brunet 
766ae9ca9cSJerome Brunet 	return 0;
776ae9ca9cSJerome Brunet }
786ae9ca9cSJerome Brunet 
aiu_fifo_prepare(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)796ae9ca9cSJerome Brunet int aiu_fifo_prepare(struct snd_pcm_substream *substream,
806ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
816ae9ca9cSJerome Brunet {
826ae9ca9cSJerome Brunet 	struct snd_soc_component *component = dai->component;
83c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
846ae9ca9cSJerome Brunet 
856ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
866ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_CONTROL,
876ae9ca9cSJerome Brunet 				      AIU_MEM_CONTROL_INIT,
886ae9ca9cSJerome Brunet 				      AIU_MEM_CONTROL_INIT);
896ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
906ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_CONTROL,
916ae9ca9cSJerome Brunet 				      AIU_MEM_CONTROL_INIT, 0);
926ae9ca9cSJerome Brunet 	return 0;
936ae9ca9cSJerome Brunet }
946ae9ca9cSJerome Brunet 
aiu_fifo_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)956ae9ca9cSJerome Brunet int aiu_fifo_hw_params(struct snd_pcm_substream *substream,
966ae9ca9cSJerome Brunet 		       struct snd_pcm_hw_params *params,
976ae9ca9cSJerome Brunet 		       struct snd_soc_dai *dai)
986ae9ca9cSJerome Brunet {
996ae9ca9cSJerome Brunet 	struct snd_pcm_runtime *runtime = substream->runtime;
1006ae9ca9cSJerome Brunet 	struct snd_soc_component *component = dai->component;
101c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
1026ae9ca9cSJerome Brunet 	dma_addr_t end;
1036ae9ca9cSJerome Brunet 
1046ae9ca9cSJerome Brunet 	/* Setup the fifo boundaries */
1056ae9ca9cSJerome Brunet 	end = runtime->dma_addr + runtime->dma_bytes - fifo->fifo_block;
1066ae9ca9cSJerome Brunet 	snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_START,
1076ae9ca9cSJerome Brunet 				runtime->dma_addr);
1086ae9ca9cSJerome Brunet 	snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_RD,
1096ae9ca9cSJerome Brunet 				runtime->dma_addr);
1106ae9ca9cSJerome Brunet 	snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_END,
1116ae9ca9cSJerome Brunet 				end);
1126ae9ca9cSJerome Brunet 
1136ae9ca9cSJerome Brunet 	/* Setup the fifo to read all the memory - no skip */
1146ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
1156ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_MASKS,
1166ae9ca9cSJerome Brunet 				      AIU_MEM_MASK_CH_RD | AIU_MEM_MASK_CH_MEM,
1176ae9ca9cSJerome Brunet 				      FIELD_PREP(AIU_MEM_MASK_CH_RD, 0xff) |
1186ae9ca9cSJerome Brunet 				      FIELD_PREP(AIU_MEM_MASK_CH_MEM, 0xff));
1196ae9ca9cSJerome Brunet 
1206ae9ca9cSJerome Brunet 	return 0;
1216ae9ca9cSJerome Brunet }
1226ae9ca9cSJerome Brunet 
aiu_fifo_isr(int irq,void * dev_id)1236ae9ca9cSJerome Brunet static irqreturn_t aiu_fifo_isr(int irq, void *dev_id)
1246ae9ca9cSJerome Brunet {
1256ae9ca9cSJerome Brunet 	struct snd_pcm_substream *playback = dev_id;
1266ae9ca9cSJerome Brunet 
1276ae9ca9cSJerome Brunet 	snd_pcm_period_elapsed(playback);
1286ae9ca9cSJerome Brunet 
1296ae9ca9cSJerome Brunet 	return IRQ_HANDLED;
1306ae9ca9cSJerome Brunet }
1316ae9ca9cSJerome Brunet 
aiu_fifo_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1326ae9ca9cSJerome Brunet int aiu_fifo_startup(struct snd_pcm_substream *substream,
1336ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
1346ae9ca9cSJerome Brunet {
135c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
1366ae9ca9cSJerome Brunet 	int ret;
1376ae9ca9cSJerome Brunet 
1386ae9ca9cSJerome Brunet 	snd_soc_set_runtime_hwparams(substream, fifo->pcm);
1396ae9ca9cSJerome Brunet 
1406ae9ca9cSJerome Brunet 	/*
1416ae9ca9cSJerome Brunet 	 * Make sure the buffer and period size are multiple of the fifo burst
1426ae9ca9cSJerome Brunet 	 * size
1436ae9ca9cSJerome Brunet 	 */
1446ae9ca9cSJerome Brunet 	ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
1456ae9ca9cSJerome Brunet 					 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1466ae9ca9cSJerome Brunet 					 fifo->fifo_block);
1476ae9ca9cSJerome Brunet 	if (ret)
1486ae9ca9cSJerome Brunet 		return ret;
1496ae9ca9cSJerome Brunet 
1506ae9ca9cSJerome Brunet 	ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
1516ae9ca9cSJerome Brunet 					 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1526ae9ca9cSJerome Brunet 					 fifo->fifo_block);
1536ae9ca9cSJerome Brunet 	if (ret)
1546ae9ca9cSJerome Brunet 		return ret;
1556ae9ca9cSJerome Brunet 
1566ae9ca9cSJerome Brunet 	ret = clk_prepare_enable(fifo->pclk);
1576ae9ca9cSJerome Brunet 	if (ret)
1586ae9ca9cSJerome Brunet 		return ret;
1596ae9ca9cSJerome Brunet 
1606ae9ca9cSJerome Brunet 	ret = request_irq(fifo->irq, aiu_fifo_isr, 0, dev_name(dai->dev),
1616ae9ca9cSJerome Brunet 			  substream);
1626ae9ca9cSJerome Brunet 	if (ret)
1636ae9ca9cSJerome Brunet 		clk_disable_unprepare(fifo->pclk);
1646ae9ca9cSJerome Brunet 
1656ae9ca9cSJerome Brunet 	return ret;
1666ae9ca9cSJerome Brunet }
1676ae9ca9cSJerome Brunet 
aiu_fifo_shutdown(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1686ae9ca9cSJerome Brunet void aiu_fifo_shutdown(struct snd_pcm_substream *substream,
1696ae9ca9cSJerome Brunet 		       struct snd_soc_dai *dai)
1706ae9ca9cSJerome Brunet {
171c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
1726ae9ca9cSJerome Brunet 
1736ae9ca9cSJerome Brunet 	free_irq(fifo->irq, substream);
1746ae9ca9cSJerome Brunet 	clk_disable_unprepare(fifo->pclk);
1756ae9ca9cSJerome Brunet }
1766ae9ca9cSJerome Brunet 
aiu_fifo_pcm_new(struct snd_soc_pcm_runtime * rtd,struct snd_soc_dai * dai)1776ae9ca9cSJerome Brunet int aiu_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd,
1786ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
1796ae9ca9cSJerome Brunet {
1806ae9ca9cSJerome Brunet 	struct snd_card *card = rtd->card->snd_card;
181c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
1826ae9ca9cSJerome Brunet 	size_t size = fifo->pcm->buffer_bytes_max;
1831bcd3266SMartin Blumenstingl 	int ret;
1841bcd3266SMartin Blumenstingl 
1851bcd3266SMartin Blumenstingl 	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
1861bcd3266SMartin Blumenstingl 	if (ret)
1871bcd3266SMartin Blumenstingl 		return ret;
1886ae9ca9cSJerome Brunet 
189e05cde84SLars-Peter Clausen 	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
1906ae9ca9cSJerome Brunet 				       card->dev, size, size);
1916ae9ca9cSJerome Brunet 
1926ae9ca9cSJerome Brunet 	return 0;
1936ae9ca9cSJerome Brunet }
1946ae9ca9cSJerome Brunet 
aiu_fifo_dai_probe(struct snd_soc_dai * dai)1956ae9ca9cSJerome Brunet int aiu_fifo_dai_probe(struct snd_soc_dai *dai)
1966ae9ca9cSJerome Brunet {
1976ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo;
1986ae9ca9cSJerome Brunet 
1996ae9ca9cSJerome Brunet 	fifo = kzalloc(sizeof(*fifo), GFP_KERNEL);
2006ae9ca9cSJerome Brunet 	if (!fifo)
2016ae9ca9cSJerome Brunet 		return -ENOMEM;
2026ae9ca9cSJerome Brunet 
203c765cedaSKuninori Morimoto 	snd_soc_dai_dma_data_set_playback(dai, fifo);
2046ae9ca9cSJerome Brunet 
2056ae9ca9cSJerome Brunet 	return 0;
2066ae9ca9cSJerome Brunet }
2076ae9ca9cSJerome Brunet 
aiu_fifo_dai_remove(struct snd_soc_dai * dai)2086ae9ca9cSJerome Brunet int aiu_fifo_dai_remove(struct snd_soc_dai *dai)
2096ae9ca9cSJerome Brunet {
210c765cedaSKuninori Morimoto 	struct aiu_fifo *fifo = snd_soc_dai_dma_data_get_playback(dai);
211c765cedaSKuninori Morimoto 
212c765cedaSKuninori Morimoto 	kfree(fifo);
2136ae9ca9cSJerome Brunet 
2146ae9ca9cSJerome Brunet 	return 0;
2156ae9ca9cSJerome Brunet }
216