xref: /linux/sound/soc/meson/aiu-fifo.c (revision cf6e26c71bfdff823fd40945b07666d75f1e1412)
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>
86ae9ca9cSJerome Brunet #include <sound/pcm_params.h>
96ae9ca9cSJerome Brunet #include <sound/soc.h>
106ae9ca9cSJerome Brunet #include <sound/soc-dai.h>
116ae9ca9cSJerome Brunet 
126ae9ca9cSJerome Brunet #include "aiu-fifo.h"
136ae9ca9cSJerome Brunet 
146ae9ca9cSJerome Brunet #define AIU_MEM_START	0x00
156ae9ca9cSJerome Brunet #define AIU_MEM_RD	0x04
166ae9ca9cSJerome Brunet #define AIU_MEM_END	0x08
176ae9ca9cSJerome Brunet #define AIU_MEM_MASKS	0x0c
186ae9ca9cSJerome Brunet #define  AIU_MEM_MASK_CH_RD GENMASK(7, 0)
196ae9ca9cSJerome Brunet #define  AIU_MEM_MASK_CH_MEM GENMASK(15, 8)
206ae9ca9cSJerome Brunet #define AIU_MEM_CONTROL	0x10
216ae9ca9cSJerome Brunet #define  AIU_MEM_CONTROL_INIT BIT(0)
226ae9ca9cSJerome Brunet #define  AIU_MEM_CONTROL_FILL_EN BIT(1)
236ae9ca9cSJerome Brunet #define  AIU_MEM_CONTROL_EMPTY_EN BIT(2)
246ae9ca9cSJerome Brunet 
256ae9ca9cSJerome Brunet static struct snd_soc_dai *aiu_fifo_dai(struct snd_pcm_substream *ss)
266ae9ca9cSJerome Brunet {
276ae9ca9cSJerome Brunet 	struct snd_soc_pcm_runtime *rtd = ss->private_data;
286ae9ca9cSJerome Brunet 
29385a5c60SKuninori Morimoto 	return asoc_rtd_to_cpu(rtd, 0);
306ae9ca9cSJerome Brunet }
316ae9ca9cSJerome Brunet 
326ae9ca9cSJerome Brunet snd_pcm_uframes_t aiu_fifo_pointer(struct snd_soc_component *component,
336ae9ca9cSJerome Brunet 				   struct snd_pcm_substream *substream)
346ae9ca9cSJerome Brunet {
356ae9ca9cSJerome Brunet 	struct snd_soc_dai *dai = aiu_fifo_dai(substream);
366ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo = dai->playback_dma_data;
376ae9ca9cSJerome Brunet 	struct snd_pcm_runtime *runtime = substream->runtime;
386ae9ca9cSJerome Brunet 	unsigned int addr;
396ae9ca9cSJerome Brunet 
40*cf6e26c7SKuninori Morimoto 	addr = snd_soc_component_read(component, fifo->mem_offset + AIU_MEM_RD);
416ae9ca9cSJerome Brunet 
426ae9ca9cSJerome Brunet 	return bytes_to_frames(runtime, addr - (unsigned int)runtime->dma_addr);
436ae9ca9cSJerome Brunet }
446ae9ca9cSJerome Brunet 
456ae9ca9cSJerome Brunet static void aiu_fifo_enable(struct snd_soc_dai *dai, bool enable)
466ae9ca9cSJerome Brunet {
476ae9ca9cSJerome Brunet 	struct snd_soc_component *component = dai->component;
486ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo = dai->playback_dma_data;
496ae9ca9cSJerome Brunet 	unsigned int en_mask = (AIU_MEM_CONTROL_FILL_EN |
506ae9ca9cSJerome Brunet 				AIU_MEM_CONTROL_EMPTY_EN);
516ae9ca9cSJerome Brunet 
526ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
536ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_CONTROL,
546ae9ca9cSJerome Brunet 				      en_mask, enable ? en_mask : 0);
556ae9ca9cSJerome Brunet }
566ae9ca9cSJerome Brunet 
576ae9ca9cSJerome Brunet int aiu_fifo_trigger(struct snd_pcm_substream *substream, int cmd,
586ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
596ae9ca9cSJerome Brunet {
606ae9ca9cSJerome Brunet 	switch (cmd) {
616ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_START:
626ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_RESUME:
636ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
646ae9ca9cSJerome Brunet 		aiu_fifo_enable(dai, true);
656ae9ca9cSJerome Brunet 		break;
666ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_SUSPEND:
676ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
686ae9ca9cSJerome Brunet 	case SNDRV_PCM_TRIGGER_STOP:
696ae9ca9cSJerome Brunet 		aiu_fifo_enable(dai, false);
706ae9ca9cSJerome Brunet 		break;
716ae9ca9cSJerome Brunet 	default:
726ae9ca9cSJerome Brunet 		return -EINVAL;
736ae9ca9cSJerome Brunet 	}
746ae9ca9cSJerome Brunet 
756ae9ca9cSJerome Brunet 	return 0;
766ae9ca9cSJerome Brunet }
776ae9ca9cSJerome Brunet 
786ae9ca9cSJerome Brunet int aiu_fifo_prepare(struct snd_pcm_substream *substream,
796ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
806ae9ca9cSJerome Brunet {
816ae9ca9cSJerome Brunet 	struct snd_soc_component *component = dai->component;
826ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo = dai->playback_dma_data;
836ae9ca9cSJerome Brunet 
846ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
856ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_CONTROL,
866ae9ca9cSJerome Brunet 				      AIU_MEM_CONTROL_INIT,
876ae9ca9cSJerome Brunet 				      AIU_MEM_CONTROL_INIT);
886ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
896ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_CONTROL,
906ae9ca9cSJerome Brunet 				      AIU_MEM_CONTROL_INIT, 0);
916ae9ca9cSJerome Brunet 	return 0;
926ae9ca9cSJerome Brunet }
936ae9ca9cSJerome Brunet 
946ae9ca9cSJerome Brunet int aiu_fifo_hw_params(struct snd_pcm_substream *substream,
956ae9ca9cSJerome Brunet 		       struct snd_pcm_hw_params *params,
966ae9ca9cSJerome Brunet 		       struct snd_soc_dai *dai)
976ae9ca9cSJerome Brunet {
986ae9ca9cSJerome Brunet 	struct snd_pcm_runtime *runtime = substream->runtime;
996ae9ca9cSJerome Brunet 	struct snd_soc_component *component = dai->component;
1006ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo = dai->playback_dma_data;
1016ae9ca9cSJerome Brunet 	dma_addr_t end;
1026ae9ca9cSJerome Brunet 	int ret;
1036ae9ca9cSJerome Brunet 
1046ae9ca9cSJerome Brunet 	ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
1056ae9ca9cSJerome Brunet 	if (ret < 0)
1066ae9ca9cSJerome Brunet 		return ret;
1076ae9ca9cSJerome Brunet 
1086ae9ca9cSJerome Brunet 	/* Setup the fifo boundaries */
1096ae9ca9cSJerome Brunet 	end = runtime->dma_addr + runtime->dma_bytes - fifo->fifo_block;
1106ae9ca9cSJerome Brunet 	snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_START,
1116ae9ca9cSJerome Brunet 				runtime->dma_addr);
1126ae9ca9cSJerome Brunet 	snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_RD,
1136ae9ca9cSJerome Brunet 				runtime->dma_addr);
1146ae9ca9cSJerome Brunet 	snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_END,
1156ae9ca9cSJerome Brunet 				end);
1166ae9ca9cSJerome Brunet 
1176ae9ca9cSJerome Brunet 	/* Setup the fifo to read all the memory - no skip */
1186ae9ca9cSJerome Brunet 	snd_soc_component_update_bits(component,
1196ae9ca9cSJerome Brunet 				      fifo->mem_offset + AIU_MEM_MASKS,
1206ae9ca9cSJerome Brunet 				      AIU_MEM_MASK_CH_RD | AIU_MEM_MASK_CH_MEM,
1216ae9ca9cSJerome Brunet 				      FIELD_PREP(AIU_MEM_MASK_CH_RD, 0xff) |
1226ae9ca9cSJerome Brunet 				      FIELD_PREP(AIU_MEM_MASK_CH_MEM, 0xff));
1236ae9ca9cSJerome Brunet 
1246ae9ca9cSJerome Brunet 	return 0;
1256ae9ca9cSJerome Brunet }
1266ae9ca9cSJerome Brunet 
1276ae9ca9cSJerome Brunet int aiu_fifo_hw_free(struct snd_pcm_substream *substream,
1286ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
1296ae9ca9cSJerome Brunet {
1306ae9ca9cSJerome Brunet 	return snd_pcm_lib_free_pages(substream);
1316ae9ca9cSJerome Brunet }
1326ae9ca9cSJerome Brunet 
1336ae9ca9cSJerome Brunet static irqreturn_t aiu_fifo_isr(int irq, void *dev_id)
1346ae9ca9cSJerome Brunet {
1356ae9ca9cSJerome Brunet 	struct snd_pcm_substream *playback = dev_id;
1366ae9ca9cSJerome Brunet 
1376ae9ca9cSJerome Brunet 	snd_pcm_period_elapsed(playback);
1386ae9ca9cSJerome Brunet 
1396ae9ca9cSJerome Brunet 	return IRQ_HANDLED;
1406ae9ca9cSJerome Brunet }
1416ae9ca9cSJerome Brunet 
1426ae9ca9cSJerome Brunet int aiu_fifo_startup(struct snd_pcm_substream *substream,
1436ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
1446ae9ca9cSJerome Brunet {
1456ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo = dai->playback_dma_data;
1466ae9ca9cSJerome Brunet 	int ret;
1476ae9ca9cSJerome Brunet 
1486ae9ca9cSJerome Brunet 	snd_soc_set_runtime_hwparams(substream, fifo->pcm);
1496ae9ca9cSJerome Brunet 
1506ae9ca9cSJerome Brunet 	/*
1516ae9ca9cSJerome Brunet 	 * Make sure the buffer and period size are multiple of the fifo burst
1526ae9ca9cSJerome Brunet 	 * size
1536ae9ca9cSJerome Brunet 	 */
1546ae9ca9cSJerome Brunet 	ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
1556ae9ca9cSJerome Brunet 					 SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
1566ae9ca9cSJerome Brunet 					 fifo->fifo_block);
1576ae9ca9cSJerome Brunet 	if (ret)
1586ae9ca9cSJerome Brunet 		return ret;
1596ae9ca9cSJerome Brunet 
1606ae9ca9cSJerome Brunet 	ret = snd_pcm_hw_constraint_step(substream->runtime, 0,
1616ae9ca9cSJerome Brunet 					 SNDRV_PCM_HW_PARAM_PERIOD_BYTES,
1626ae9ca9cSJerome Brunet 					 fifo->fifo_block);
1636ae9ca9cSJerome Brunet 	if (ret)
1646ae9ca9cSJerome Brunet 		return ret;
1656ae9ca9cSJerome Brunet 
1666ae9ca9cSJerome Brunet 	ret = clk_prepare_enable(fifo->pclk);
1676ae9ca9cSJerome Brunet 	if (ret)
1686ae9ca9cSJerome Brunet 		return ret;
1696ae9ca9cSJerome Brunet 
1706ae9ca9cSJerome Brunet 	ret = request_irq(fifo->irq, aiu_fifo_isr, 0, dev_name(dai->dev),
1716ae9ca9cSJerome Brunet 			  substream);
1726ae9ca9cSJerome Brunet 	if (ret)
1736ae9ca9cSJerome Brunet 		clk_disable_unprepare(fifo->pclk);
1746ae9ca9cSJerome Brunet 
1756ae9ca9cSJerome Brunet 	return ret;
1766ae9ca9cSJerome Brunet }
1776ae9ca9cSJerome Brunet 
1786ae9ca9cSJerome Brunet void aiu_fifo_shutdown(struct snd_pcm_substream *substream,
1796ae9ca9cSJerome Brunet 		       struct snd_soc_dai *dai)
1806ae9ca9cSJerome Brunet {
1816ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo = dai->playback_dma_data;
1826ae9ca9cSJerome Brunet 
1836ae9ca9cSJerome Brunet 	free_irq(fifo->irq, substream);
1846ae9ca9cSJerome Brunet 	clk_disable_unprepare(fifo->pclk);
1856ae9ca9cSJerome Brunet }
1866ae9ca9cSJerome Brunet 
1876ae9ca9cSJerome Brunet int aiu_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd,
1886ae9ca9cSJerome Brunet 		     struct snd_soc_dai *dai)
1896ae9ca9cSJerome Brunet {
1906ae9ca9cSJerome Brunet 	struct snd_pcm_substream *substream =
1916ae9ca9cSJerome Brunet 		rtd->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream;
1926ae9ca9cSJerome Brunet 	struct snd_card *card = rtd->card->snd_card;
1936ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo = dai->playback_dma_data;
1946ae9ca9cSJerome Brunet 	size_t size = fifo->pcm->buffer_bytes_max;
1956ae9ca9cSJerome Brunet 
1966ae9ca9cSJerome Brunet 	snd_pcm_lib_preallocate_pages(substream,
1976ae9ca9cSJerome Brunet 				      SNDRV_DMA_TYPE_DEV,
1986ae9ca9cSJerome Brunet 				      card->dev, size, size);
1996ae9ca9cSJerome Brunet 
2006ae9ca9cSJerome Brunet 	return 0;
2016ae9ca9cSJerome Brunet }
2026ae9ca9cSJerome Brunet 
2036ae9ca9cSJerome Brunet int aiu_fifo_dai_probe(struct snd_soc_dai *dai)
2046ae9ca9cSJerome Brunet {
2056ae9ca9cSJerome Brunet 	struct aiu_fifo *fifo;
2066ae9ca9cSJerome Brunet 
2076ae9ca9cSJerome Brunet 	fifo = kzalloc(sizeof(*fifo), GFP_KERNEL);
2086ae9ca9cSJerome Brunet 	if (!fifo)
2096ae9ca9cSJerome Brunet 		return -ENOMEM;
2106ae9ca9cSJerome Brunet 
2116ae9ca9cSJerome Brunet 	dai->playback_dma_data = fifo;
2126ae9ca9cSJerome Brunet 
2136ae9ca9cSJerome Brunet 	return 0;
2146ae9ca9cSJerome Brunet }
2156ae9ca9cSJerome Brunet 
2166ae9ca9cSJerome Brunet int aiu_fifo_dai_remove(struct snd_soc_dai *dai)
2176ae9ca9cSJerome Brunet {
2186ae9ca9cSJerome Brunet 	kfree(dai->playback_dma_data);
2196ae9ca9cSJerome Brunet 
2206ae9ca9cSJerome Brunet 	return 0;
2216ae9ca9cSJerome Brunet }
2226ae9ca9cSJerome Brunet 
223