1*6ae9ca9cSJerome Brunet // SPDX-License-Identifier: GPL-2.0 2*6ae9ca9cSJerome Brunet // 3*6ae9ca9cSJerome Brunet // Copyright (c) 2020 BayLibre, SAS. 4*6ae9ca9cSJerome Brunet // Author: Jerome Brunet <jbrunet@baylibre.com> 5*6ae9ca9cSJerome Brunet 6*6ae9ca9cSJerome Brunet #include <linux/bitfield.h> 7*6ae9ca9cSJerome Brunet #include <linux/clk.h> 8*6ae9ca9cSJerome Brunet #include <sound/pcm_params.h> 9*6ae9ca9cSJerome Brunet #include <sound/soc.h> 10*6ae9ca9cSJerome Brunet #include <sound/soc-dai.h> 11*6ae9ca9cSJerome Brunet 12*6ae9ca9cSJerome Brunet #include "aiu-fifo.h" 13*6ae9ca9cSJerome Brunet 14*6ae9ca9cSJerome Brunet #define AIU_MEM_START 0x00 15*6ae9ca9cSJerome Brunet #define AIU_MEM_RD 0x04 16*6ae9ca9cSJerome Brunet #define AIU_MEM_END 0x08 17*6ae9ca9cSJerome Brunet #define AIU_MEM_MASKS 0x0c 18*6ae9ca9cSJerome Brunet #define AIU_MEM_MASK_CH_RD GENMASK(7, 0) 19*6ae9ca9cSJerome Brunet #define AIU_MEM_MASK_CH_MEM GENMASK(15, 8) 20*6ae9ca9cSJerome Brunet #define AIU_MEM_CONTROL 0x10 21*6ae9ca9cSJerome Brunet #define AIU_MEM_CONTROL_INIT BIT(0) 22*6ae9ca9cSJerome Brunet #define AIU_MEM_CONTROL_FILL_EN BIT(1) 23*6ae9ca9cSJerome Brunet #define AIU_MEM_CONTROL_EMPTY_EN BIT(2) 24*6ae9ca9cSJerome Brunet 25*6ae9ca9cSJerome Brunet static struct snd_soc_dai *aiu_fifo_dai(struct snd_pcm_substream *ss) 26*6ae9ca9cSJerome Brunet { 27*6ae9ca9cSJerome Brunet struct snd_soc_pcm_runtime *rtd = ss->private_data; 28*6ae9ca9cSJerome Brunet 29*6ae9ca9cSJerome Brunet return rtd->cpu_dai; 30*6ae9ca9cSJerome Brunet } 31*6ae9ca9cSJerome Brunet 32*6ae9ca9cSJerome Brunet snd_pcm_uframes_t aiu_fifo_pointer(struct snd_soc_component *component, 33*6ae9ca9cSJerome Brunet struct snd_pcm_substream *substream) 34*6ae9ca9cSJerome Brunet { 35*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai = aiu_fifo_dai(substream); 36*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 37*6ae9ca9cSJerome Brunet struct snd_pcm_runtime *runtime = substream->runtime; 38*6ae9ca9cSJerome Brunet unsigned int addr; 39*6ae9ca9cSJerome Brunet 40*6ae9ca9cSJerome Brunet snd_soc_component_read(component, fifo->mem_offset + AIU_MEM_RD, 41*6ae9ca9cSJerome Brunet &addr); 42*6ae9ca9cSJerome Brunet 43*6ae9ca9cSJerome Brunet return bytes_to_frames(runtime, addr - (unsigned int)runtime->dma_addr); 44*6ae9ca9cSJerome Brunet } 45*6ae9ca9cSJerome Brunet 46*6ae9ca9cSJerome Brunet static void aiu_fifo_enable(struct snd_soc_dai *dai, bool enable) 47*6ae9ca9cSJerome Brunet { 48*6ae9ca9cSJerome Brunet struct snd_soc_component *component = dai->component; 49*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 50*6ae9ca9cSJerome Brunet unsigned int en_mask = (AIU_MEM_CONTROL_FILL_EN | 51*6ae9ca9cSJerome Brunet AIU_MEM_CONTROL_EMPTY_EN); 52*6ae9ca9cSJerome Brunet 53*6ae9ca9cSJerome Brunet snd_soc_component_update_bits(component, 54*6ae9ca9cSJerome Brunet fifo->mem_offset + AIU_MEM_CONTROL, 55*6ae9ca9cSJerome Brunet en_mask, enable ? en_mask : 0); 56*6ae9ca9cSJerome Brunet } 57*6ae9ca9cSJerome Brunet 58*6ae9ca9cSJerome Brunet int aiu_fifo_trigger(struct snd_pcm_substream *substream, int cmd, 59*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 60*6ae9ca9cSJerome Brunet { 61*6ae9ca9cSJerome Brunet switch (cmd) { 62*6ae9ca9cSJerome Brunet case SNDRV_PCM_TRIGGER_START: 63*6ae9ca9cSJerome Brunet case SNDRV_PCM_TRIGGER_RESUME: 64*6ae9ca9cSJerome Brunet case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 65*6ae9ca9cSJerome Brunet aiu_fifo_enable(dai, true); 66*6ae9ca9cSJerome Brunet break; 67*6ae9ca9cSJerome Brunet case SNDRV_PCM_TRIGGER_SUSPEND: 68*6ae9ca9cSJerome Brunet case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 69*6ae9ca9cSJerome Brunet case SNDRV_PCM_TRIGGER_STOP: 70*6ae9ca9cSJerome Brunet aiu_fifo_enable(dai, false); 71*6ae9ca9cSJerome Brunet break; 72*6ae9ca9cSJerome Brunet default: 73*6ae9ca9cSJerome Brunet return -EINVAL; 74*6ae9ca9cSJerome Brunet } 75*6ae9ca9cSJerome Brunet 76*6ae9ca9cSJerome Brunet return 0; 77*6ae9ca9cSJerome Brunet } 78*6ae9ca9cSJerome Brunet 79*6ae9ca9cSJerome Brunet int aiu_fifo_prepare(struct snd_pcm_substream *substream, 80*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 81*6ae9ca9cSJerome Brunet { 82*6ae9ca9cSJerome Brunet struct snd_soc_component *component = dai->component; 83*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 84*6ae9ca9cSJerome Brunet 85*6ae9ca9cSJerome Brunet snd_soc_component_update_bits(component, 86*6ae9ca9cSJerome Brunet fifo->mem_offset + AIU_MEM_CONTROL, 87*6ae9ca9cSJerome Brunet AIU_MEM_CONTROL_INIT, 88*6ae9ca9cSJerome Brunet AIU_MEM_CONTROL_INIT); 89*6ae9ca9cSJerome Brunet snd_soc_component_update_bits(component, 90*6ae9ca9cSJerome Brunet fifo->mem_offset + AIU_MEM_CONTROL, 91*6ae9ca9cSJerome Brunet AIU_MEM_CONTROL_INIT, 0); 92*6ae9ca9cSJerome Brunet return 0; 93*6ae9ca9cSJerome Brunet } 94*6ae9ca9cSJerome Brunet 95*6ae9ca9cSJerome Brunet int aiu_fifo_hw_params(struct snd_pcm_substream *substream, 96*6ae9ca9cSJerome Brunet struct snd_pcm_hw_params *params, 97*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 98*6ae9ca9cSJerome Brunet { 99*6ae9ca9cSJerome Brunet struct snd_pcm_runtime *runtime = substream->runtime; 100*6ae9ca9cSJerome Brunet struct snd_soc_component *component = dai->component; 101*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 102*6ae9ca9cSJerome Brunet dma_addr_t end; 103*6ae9ca9cSJerome Brunet int ret; 104*6ae9ca9cSJerome Brunet 105*6ae9ca9cSJerome Brunet ret = snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params)); 106*6ae9ca9cSJerome Brunet if (ret < 0) 107*6ae9ca9cSJerome Brunet return ret; 108*6ae9ca9cSJerome Brunet 109*6ae9ca9cSJerome Brunet /* Setup the fifo boundaries */ 110*6ae9ca9cSJerome Brunet end = runtime->dma_addr + runtime->dma_bytes - fifo->fifo_block; 111*6ae9ca9cSJerome Brunet snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_START, 112*6ae9ca9cSJerome Brunet runtime->dma_addr); 113*6ae9ca9cSJerome Brunet snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_RD, 114*6ae9ca9cSJerome Brunet runtime->dma_addr); 115*6ae9ca9cSJerome Brunet snd_soc_component_write(component, fifo->mem_offset + AIU_MEM_END, 116*6ae9ca9cSJerome Brunet end); 117*6ae9ca9cSJerome Brunet 118*6ae9ca9cSJerome Brunet /* Setup the fifo to read all the memory - no skip */ 119*6ae9ca9cSJerome Brunet snd_soc_component_update_bits(component, 120*6ae9ca9cSJerome Brunet fifo->mem_offset + AIU_MEM_MASKS, 121*6ae9ca9cSJerome Brunet AIU_MEM_MASK_CH_RD | AIU_MEM_MASK_CH_MEM, 122*6ae9ca9cSJerome Brunet FIELD_PREP(AIU_MEM_MASK_CH_RD, 0xff) | 123*6ae9ca9cSJerome Brunet FIELD_PREP(AIU_MEM_MASK_CH_MEM, 0xff)); 124*6ae9ca9cSJerome Brunet 125*6ae9ca9cSJerome Brunet return 0; 126*6ae9ca9cSJerome Brunet } 127*6ae9ca9cSJerome Brunet 128*6ae9ca9cSJerome Brunet int aiu_fifo_hw_free(struct snd_pcm_substream *substream, 129*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 130*6ae9ca9cSJerome Brunet { 131*6ae9ca9cSJerome Brunet return snd_pcm_lib_free_pages(substream); 132*6ae9ca9cSJerome Brunet } 133*6ae9ca9cSJerome Brunet 134*6ae9ca9cSJerome Brunet static irqreturn_t aiu_fifo_isr(int irq, void *dev_id) 135*6ae9ca9cSJerome Brunet { 136*6ae9ca9cSJerome Brunet struct snd_pcm_substream *playback = dev_id; 137*6ae9ca9cSJerome Brunet 138*6ae9ca9cSJerome Brunet snd_pcm_period_elapsed(playback); 139*6ae9ca9cSJerome Brunet 140*6ae9ca9cSJerome Brunet return IRQ_HANDLED; 141*6ae9ca9cSJerome Brunet } 142*6ae9ca9cSJerome Brunet 143*6ae9ca9cSJerome Brunet int aiu_fifo_startup(struct snd_pcm_substream *substream, 144*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 145*6ae9ca9cSJerome Brunet { 146*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 147*6ae9ca9cSJerome Brunet int ret; 148*6ae9ca9cSJerome Brunet 149*6ae9ca9cSJerome Brunet snd_soc_set_runtime_hwparams(substream, fifo->pcm); 150*6ae9ca9cSJerome Brunet 151*6ae9ca9cSJerome Brunet /* 152*6ae9ca9cSJerome Brunet * Make sure the buffer and period size are multiple of the fifo burst 153*6ae9ca9cSJerome Brunet * size 154*6ae9ca9cSJerome Brunet */ 155*6ae9ca9cSJerome Brunet ret = snd_pcm_hw_constraint_step(substream->runtime, 0, 156*6ae9ca9cSJerome Brunet SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 157*6ae9ca9cSJerome Brunet fifo->fifo_block); 158*6ae9ca9cSJerome Brunet if (ret) 159*6ae9ca9cSJerome Brunet return ret; 160*6ae9ca9cSJerome Brunet 161*6ae9ca9cSJerome Brunet ret = snd_pcm_hw_constraint_step(substream->runtime, 0, 162*6ae9ca9cSJerome Brunet SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 163*6ae9ca9cSJerome Brunet fifo->fifo_block); 164*6ae9ca9cSJerome Brunet if (ret) 165*6ae9ca9cSJerome Brunet return ret; 166*6ae9ca9cSJerome Brunet 167*6ae9ca9cSJerome Brunet ret = clk_prepare_enable(fifo->pclk); 168*6ae9ca9cSJerome Brunet if (ret) 169*6ae9ca9cSJerome Brunet return ret; 170*6ae9ca9cSJerome Brunet 171*6ae9ca9cSJerome Brunet ret = request_irq(fifo->irq, aiu_fifo_isr, 0, dev_name(dai->dev), 172*6ae9ca9cSJerome Brunet substream); 173*6ae9ca9cSJerome Brunet if (ret) 174*6ae9ca9cSJerome Brunet clk_disable_unprepare(fifo->pclk); 175*6ae9ca9cSJerome Brunet 176*6ae9ca9cSJerome Brunet return ret; 177*6ae9ca9cSJerome Brunet } 178*6ae9ca9cSJerome Brunet 179*6ae9ca9cSJerome Brunet void aiu_fifo_shutdown(struct snd_pcm_substream *substream, 180*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 181*6ae9ca9cSJerome Brunet { 182*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 183*6ae9ca9cSJerome Brunet 184*6ae9ca9cSJerome Brunet free_irq(fifo->irq, substream); 185*6ae9ca9cSJerome Brunet clk_disable_unprepare(fifo->pclk); 186*6ae9ca9cSJerome Brunet } 187*6ae9ca9cSJerome Brunet 188*6ae9ca9cSJerome Brunet int aiu_fifo_pcm_new(struct snd_soc_pcm_runtime *rtd, 189*6ae9ca9cSJerome Brunet struct snd_soc_dai *dai) 190*6ae9ca9cSJerome Brunet { 191*6ae9ca9cSJerome Brunet struct snd_pcm_substream *substream = 192*6ae9ca9cSJerome Brunet rtd->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream; 193*6ae9ca9cSJerome Brunet struct snd_card *card = rtd->card->snd_card; 194*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo = dai->playback_dma_data; 195*6ae9ca9cSJerome Brunet size_t size = fifo->pcm->buffer_bytes_max; 196*6ae9ca9cSJerome Brunet 197*6ae9ca9cSJerome Brunet snd_pcm_lib_preallocate_pages(substream, 198*6ae9ca9cSJerome Brunet SNDRV_DMA_TYPE_DEV, 199*6ae9ca9cSJerome Brunet card->dev, size, size); 200*6ae9ca9cSJerome Brunet 201*6ae9ca9cSJerome Brunet return 0; 202*6ae9ca9cSJerome Brunet } 203*6ae9ca9cSJerome Brunet 204*6ae9ca9cSJerome Brunet int aiu_fifo_dai_probe(struct snd_soc_dai *dai) 205*6ae9ca9cSJerome Brunet { 206*6ae9ca9cSJerome Brunet struct aiu_fifo *fifo; 207*6ae9ca9cSJerome Brunet 208*6ae9ca9cSJerome Brunet fifo = kzalloc(sizeof(*fifo), GFP_KERNEL); 209*6ae9ca9cSJerome Brunet if (!fifo) 210*6ae9ca9cSJerome Brunet return -ENOMEM; 211*6ae9ca9cSJerome Brunet 212*6ae9ca9cSJerome Brunet dai->playback_dma_data = fifo; 213*6ae9ca9cSJerome Brunet 214*6ae9ca9cSJerome Brunet return 0; 215*6ae9ca9cSJerome Brunet } 216*6ae9ca9cSJerome Brunet 217*6ae9ca9cSJerome Brunet int aiu_fifo_dai_remove(struct snd_soc_dai *dai) 218*6ae9ca9cSJerome Brunet { 219*6ae9ca9cSJerome Brunet kfree(dai->playback_dma_data); 220*6ae9ca9cSJerome Brunet 221*6ae9ca9cSJerome Brunet return 0; 222*6ae9ca9cSJerome Brunet } 223*6ae9ca9cSJerome Brunet 224