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 40cf6e26c7SKuninori 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 1036ae9ca9cSJerome Brunet /* Setup the fifo boundaries */ 1046ae9ca9cSJerome Brunet end = runtime->dma_addr + runtime->dma_bytes - fifo->fifo_block; 1056ae9ca9cSJerome Brunet snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_START, 1066ae9ca9cSJerome Brunet runtime->dma_addr); 1076ae9ca9cSJerome Brunet snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_RD, 1086ae9ca9cSJerome Brunet runtime->dma_addr); 1096ae9ca9cSJerome Brunet snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_END, 1106ae9ca9cSJerome Brunet end); 1116ae9ca9cSJerome Brunet 1126ae9ca9cSJerome Brunet /* Setup the fifo to read all the memory - no skip */ 1136ae9ca9cSJerome Brunet snd_soc_component_update_bits(component, 1146ae9ca9cSJerome Brunet fifo->mem_offset + AIU_MEM_MASKS, 1156ae9ca9cSJerome Brunet AIU_MEM_MASK_CH_RD | AIU_MEM_MASK_CH_MEM, 1166ae9ca9cSJerome Brunet FIELD_PREP(AIU_MEM_MASK_CH_RD, 0xff) | 1176ae9ca9cSJerome Brunet FIELD_PREP(AIU_MEM_MASK_CH_MEM, 0xff)); 1186ae9ca9cSJerome Brunet 1196ae9ca9cSJerome Brunet return 0; 1206ae9ca9cSJerome Brunet } 1216ae9ca9cSJerome Brunet 1226ae9ca9cSJerome Brunet static irqreturn_t aiu_fifo_isr(int irq, void *dev_id) 1236ae9ca9cSJerome Brunet { 1246ae9ca9cSJerome Brunet struct snd_pcm_substream *playback = dev_id; 1256ae9ca9cSJerome Brunet 1266ae9ca9cSJerome Brunet snd_pcm_period_elapsed(playback); 1276ae9ca9cSJerome Brunet 1286ae9ca9cSJerome Brunet return IRQ_HANDLED; 1296ae9ca9cSJerome Brunet } 1306ae9ca9cSJerome Brunet 1316ae9ca9cSJerome Brunet int aiu_fifo_startup(struct snd_pcm_substream *substream, 1326ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 1336ae9ca9cSJerome Brunet { 1346ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 1356ae9ca9cSJerome Brunet int ret; 1366ae9ca9cSJerome Brunet 1376ae9ca9cSJerome Brunet snd_soc_set_runtime_hwparams(substream, fifo->pcm); 1386ae9ca9cSJerome Brunet 1396ae9ca9cSJerome Brunet /* 1406ae9ca9cSJerome Brunet * Make sure the buffer and period size are multiple of the fifo burst 1416ae9ca9cSJerome Brunet * size 1426ae9ca9cSJerome Brunet */ 1436ae9ca9cSJerome Brunet ret = snd_pcm_hw_constraint_step(substream->runtime, 0, 1446ae9ca9cSJerome Brunet SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 1456ae9ca9cSJerome Brunet fifo->fifo_block); 1466ae9ca9cSJerome Brunet if (ret) 1476ae9ca9cSJerome Brunet return ret; 1486ae9ca9cSJerome Brunet 1496ae9ca9cSJerome Brunet ret = snd_pcm_hw_constraint_step(substream->runtime, 0, 1506ae9ca9cSJerome Brunet SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 1516ae9ca9cSJerome Brunet fifo->fifo_block); 1526ae9ca9cSJerome Brunet if (ret) 1536ae9ca9cSJerome Brunet return ret; 1546ae9ca9cSJerome Brunet 1556ae9ca9cSJerome Brunet ret = clk_prepare_enable(fifo->pclk); 1566ae9ca9cSJerome Brunet if (ret) 1576ae9ca9cSJerome Brunet return ret; 1586ae9ca9cSJerome Brunet 1596ae9ca9cSJerome Brunet ret = request_irq(fifo->irq, aiu_fifo_isr, 0, dev_name(dai->dev), 1606ae9ca9cSJerome Brunet substream); 1616ae9ca9cSJerome Brunet if (ret) 1626ae9ca9cSJerome Brunet clk_disable_unprepare(fifo->pclk); 1636ae9ca9cSJerome Brunet 1646ae9ca9cSJerome Brunet return ret; 1656ae9ca9cSJerome Brunet } 1666ae9ca9cSJerome Brunet 1676ae9ca9cSJerome Brunet void aiu_fifo_shutdown(struct snd_pcm_substream *substream, 1686ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 1696ae9ca9cSJerome Brunet { 1706ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 1716ae9ca9cSJerome Brunet 1726ae9ca9cSJerome Brunet free_irq(fifo->irq, substream); 1736ae9ca9cSJerome Brunet clk_disable_unprepare(fifo->pclk); 1746ae9ca9cSJerome Brunet } 1756ae9ca9cSJerome Brunet 1766ae9ca9cSJerome Brunet int aiu_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd, 1776ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 1786ae9ca9cSJerome Brunet { 1796ae9ca9cSJerome Brunet struct snd_card *card = rtd->card->snd_card; 1806ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 1816ae9ca9cSJerome Brunet size_t size = fifo->pcm->buffer_bytes_max; 1826ae9ca9cSJerome Brunet 183*e05cde84SLars-Peter Clausen snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, 1846ae9ca9cSJerome Brunet card->dev, size, size); 1856ae9ca9cSJerome Brunet 1866ae9ca9cSJerome Brunet return 0; 1876ae9ca9cSJerome Brunet } 1886ae9ca9cSJerome Brunet 1896ae9ca9cSJerome Brunet int aiu_fifo_dai_probe(struct snd_soc_dai *dai) 1906ae9ca9cSJerome Brunet { 1916ae9ca9cSJerome Brunet struct aiu_fifo *fifo; 1926ae9ca9cSJerome Brunet 1936ae9ca9cSJerome Brunet fifo = kzalloc(sizeof(*fifo), GFP_KERNEL); 1946ae9ca9cSJerome Brunet if (!fifo) 1956ae9ca9cSJerome Brunet return -ENOMEM; 1966ae9ca9cSJerome Brunet 1976ae9ca9cSJerome Brunet dai->playback_dma_data = fifo; 1986ae9ca9cSJerome Brunet 1996ae9ca9cSJerome Brunet return 0; 2006ae9ca9cSJerome Brunet } 2016ae9ca9cSJerome Brunet 2026ae9ca9cSJerome Brunet int aiu_fifo_dai_remove(struct snd_soc_dai *dai) 2036ae9ca9cSJerome Brunet { 2046ae9ca9cSJerome Brunet kfree(dai->playback_dma_data); 2056ae9ca9cSJerome Brunet 2066ae9ca9cSJerome Brunet return 0; 2076ae9ca9cSJerome Brunet } 2086ae9ca9cSJerome Brunet 209