103e786bdSBiju Das // SPDX-License-Identifier: GPL-2.0 203e786bdSBiju Das // 303e786bdSBiju Das // Renesas RZ/G2L ASoC Serial Sound Interface (SSIF-2) Driver 403e786bdSBiju Das // 503e786bdSBiju Das // Copyright (C) 2021 Renesas Electronics Corp. 603e786bdSBiju Das // Copyright (C) 2019 Chris Brandt. 703e786bdSBiju Das // 803e786bdSBiju Das 903e786bdSBiju Das #include <linux/clk.h> 1026ac471cSBiju Das #include <linux/dmaengine.h> 1103e786bdSBiju Das #include <linux/io.h> 1203e786bdSBiju Das #include <linux/module.h> 1303e786bdSBiju Das #include <linux/of_device.h> 1403e786bdSBiju Das #include <linux/pm_runtime.h> 1503e786bdSBiju Das #include <linux/reset.h> 1603e786bdSBiju Das #include <sound/soc.h> 1703e786bdSBiju Das 1803e786bdSBiju Das /* REGISTER OFFSET */ 1903e786bdSBiju Das #define SSICR 0x000 2003e786bdSBiju Das #define SSISR 0x004 2103e786bdSBiju Das #define SSIFCR 0x010 2203e786bdSBiju Das #define SSIFSR 0x014 2303e786bdSBiju Das #define SSIFTDR 0x018 2403e786bdSBiju Das #define SSIFRDR 0x01c 2503e786bdSBiju Das #define SSIOFR 0x020 2603e786bdSBiju Das #define SSISCR 0x024 2703e786bdSBiju Das 2803e786bdSBiju Das /* SSI REGISTER BITS */ 2903e786bdSBiju Das #define SSICR_DWL(x) (((x) & 0x7) << 19) 3003e786bdSBiju Das #define SSICR_SWL(x) (((x) & 0x7) << 16) 3103e786bdSBiju Das 3203e786bdSBiju Das #define SSICR_CKS BIT(30) 3303e786bdSBiju Das #define SSICR_TUIEN BIT(29) 3403e786bdSBiju Das #define SSICR_TOIEN BIT(28) 3503e786bdSBiju Das #define SSICR_RUIEN BIT(27) 3603e786bdSBiju Das #define SSICR_ROIEN BIT(26) 3703e786bdSBiju Das #define SSICR_MST BIT(14) 3803e786bdSBiju Das #define SSICR_BCKP BIT(13) 3903e786bdSBiju Das #define SSICR_LRCKP BIT(12) 4003e786bdSBiju Das #define SSICR_CKDV(x) (((x) & 0xf) << 4) 4103e786bdSBiju Das #define SSICR_TEN BIT(1) 4203e786bdSBiju Das #define SSICR_REN BIT(0) 4303e786bdSBiju Das 4403e786bdSBiju Das #define SSISR_TUIRQ BIT(29) 4503e786bdSBiju Das #define SSISR_TOIRQ BIT(28) 4603e786bdSBiju Das #define SSISR_RUIRQ BIT(27) 4703e786bdSBiju Das #define SSISR_ROIRQ BIT(26) 4803e786bdSBiju Das #define SSISR_IIRQ BIT(25) 4903e786bdSBiju Das 5003e786bdSBiju Das #define SSIFCR_AUCKE BIT(31) 5103e786bdSBiju Das #define SSIFCR_SSIRST BIT(16) 5203e786bdSBiju Das #define SSIFCR_TIE BIT(3) 5303e786bdSBiju Das #define SSIFCR_RIE BIT(2) 5403e786bdSBiju Das #define SSIFCR_TFRST BIT(1) 5503e786bdSBiju Das #define SSIFCR_RFRST BIT(0) 5603e786bdSBiju Das 5703e786bdSBiju Das #define SSIFSR_TDC_MASK 0x3f 5803e786bdSBiju Das #define SSIFSR_TDC_SHIFT 24 5903e786bdSBiju Das #define SSIFSR_RDC_MASK 0x3f 6003e786bdSBiju Das #define SSIFSR_RDC_SHIFT 8 6103e786bdSBiju Das 6203e786bdSBiju Das #define SSIFSR_TDE BIT(16) 6303e786bdSBiju Das #define SSIFSR_RDF BIT(0) 6403e786bdSBiju Das 6503e786bdSBiju Das #define SSIOFR_LRCONT BIT(8) 6603e786bdSBiju Das 6703e786bdSBiju Das #define SSISCR_TDES(x) (((x) & 0x1f) << 8) 6803e786bdSBiju Das #define SSISCR_RDFS(x) (((x) & 0x1f) << 0) 6903e786bdSBiju Das 7003e786bdSBiju Das /* Pre allocated buffers sizes */ 7103e786bdSBiju Das #define PREALLOC_BUFFER (SZ_32K) 7203e786bdSBiju Das #define PREALLOC_BUFFER_MAX (SZ_32K) 7303e786bdSBiju Das 7403e786bdSBiju Das #define SSI_RATES SNDRV_PCM_RATE_8000_48000 /* 8k-44.1kHz */ 7503e786bdSBiju Das #define SSI_FMTS SNDRV_PCM_FMTBIT_S16_LE 7603e786bdSBiju Das #define SSI_CHAN_MIN 2 7703e786bdSBiju Das #define SSI_CHAN_MAX 2 7803e786bdSBiju Das #define SSI_FIFO_DEPTH 32 7903e786bdSBiju Das 8003e786bdSBiju Das struct rz_ssi_priv; 8103e786bdSBiju Das 8203e786bdSBiju Das struct rz_ssi_stream { 8303e786bdSBiju Das struct rz_ssi_priv *priv; 8403e786bdSBiju Das struct snd_pcm_substream *substream; 8503e786bdSBiju Das int fifo_sample_size; /* sample capacity of SSI FIFO */ 8626ac471cSBiju Das int dma_buffer_pos; /* The address for the next DMA descriptor */ 8703e786bdSBiju Das int period_counter; /* for keeping track of periods transferred */ 8803e786bdSBiju Das int sample_width; 8903e786bdSBiju Das int buffer_pos; /* current frame position in the buffer */ 9003e786bdSBiju Das int running; /* 0=stopped, 1=running */ 9103e786bdSBiju Das 9203e786bdSBiju Das int uerr_num; 9303e786bdSBiju Das int oerr_num; 9403e786bdSBiju Das 9526ac471cSBiju Das struct dma_chan *dma_ch; 9626ac471cSBiju Das 9703e786bdSBiju Das int (*transfer)(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm); 9803e786bdSBiju Das }; 9903e786bdSBiju Das 10003e786bdSBiju Das struct rz_ssi_priv { 10103e786bdSBiju Das void __iomem *base; 10203e786bdSBiju Das struct platform_device *pdev; 10303e786bdSBiju Das struct reset_control *rstc; 10403e786bdSBiju Das struct device *dev; 10503e786bdSBiju Das struct clk *sfr_clk; 10603e786bdSBiju Das struct clk *clk; 10703e786bdSBiju Das 10826ac471cSBiju Das phys_addr_t phys; 10903e786bdSBiju Das int irq_int; 11003e786bdSBiju Das int irq_tx; 11103e786bdSBiju Das int irq_rx; 11203e786bdSBiju Das 11303e786bdSBiju Das spinlock_t lock; 11403e786bdSBiju Das 11503e786bdSBiju Das /* 11603e786bdSBiju Das * The SSI supports full-duplex transmission and reception. 11703e786bdSBiju Das * However, if an error occurs, channel reset (both transmission 11803e786bdSBiju Das * and reception reset) is required. 11903e786bdSBiju Das * So it is better to use as half-duplex (playing and recording 12003e786bdSBiju Das * should be done on separate channels). 12103e786bdSBiju Das */ 12203e786bdSBiju Das struct rz_ssi_stream playback; 12303e786bdSBiju Das struct rz_ssi_stream capture; 12403e786bdSBiju Das 12503e786bdSBiju Das /* clock */ 12603e786bdSBiju Das unsigned long audio_mck; 12703e786bdSBiju Das unsigned long audio_clk_1; 12803e786bdSBiju Das unsigned long audio_clk_2; 12903e786bdSBiju Das 13003e786bdSBiju Das bool lrckp_fsync_fall; /* LR clock polarity (SSICR.LRCKP) */ 13103e786bdSBiju Das bool bckp_rise; /* Bit clock polarity (SSICR.BCKP) */ 13226ac471cSBiju Das bool dma_rt; 13303e786bdSBiju Das }; 13403e786bdSBiju Das 13526ac471cSBiju Das static void rz_ssi_dma_complete(void *data); 13626ac471cSBiju Das 13703e786bdSBiju Das static void rz_ssi_reg_writel(struct rz_ssi_priv *priv, uint reg, u32 data) 13803e786bdSBiju Das { 13903e786bdSBiju Das writel(data, (priv->base + reg)); 14003e786bdSBiju Das } 14103e786bdSBiju Das 14203e786bdSBiju Das static u32 rz_ssi_reg_readl(struct rz_ssi_priv *priv, uint reg) 14303e786bdSBiju Das { 14403e786bdSBiju Das return readl(priv->base + reg); 14503e786bdSBiju Das } 14603e786bdSBiju Das 147d40dfb86SBiju Das static void rz_ssi_reg_mask_setl(struct rz_ssi_priv *priv, uint reg, 14803e786bdSBiju Das u32 bclr, u32 bset) 14903e786bdSBiju Das { 15003e786bdSBiju Das u32 val; 15103e786bdSBiju Das 15203e786bdSBiju Das val = readl(priv->base + reg); 15303e786bdSBiju Das val = (val & ~bclr) | bset; 15403e786bdSBiju Das writel(val, (priv->base + reg)); 15503e786bdSBiju Das } 15603e786bdSBiju Das 15703e786bdSBiju Das static inline struct snd_soc_dai * 15803e786bdSBiju Das rz_ssi_get_dai(struct snd_pcm_substream *substream) 15903e786bdSBiju Das { 16003e786bdSBiju Das struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 16103e786bdSBiju Das 16203e786bdSBiju Das return asoc_rtd_to_cpu(rtd, 0); 16303e786bdSBiju Das } 16403e786bdSBiju Das 16503e786bdSBiju Das static inline bool rz_ssi_stream_is_play(struct rz_ssi_priv *ssi, 16603e786bdSBiju Das struct snd_pcm_substream *substream) 16703e786bdSBiju Das { 16803e786bdSBiju Das return substream->stream == SNDRV_PCM_STREAM_PLAYBACK; 16903e786bdSBiju Das } 17003e786bdSBiju Das 17103e786bdSBiju Das static inline struct rz_ssi_stream * 17203e786bdSBiju Das rz_ssi_stream_get(struct rz_ssi_priv *ssi, struct snd_pcm_substream *substream) 17303e786bdSBiju Das { 17403e786bdSBiju Das struct rz_ssi_stream *stream = &ssi->playback; 17503e786bdSBiju Das 17603e786bdSBiju Das if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK) 17703e786bdSBiju Das stream = &ssi->capture; 17803e786bdSBiju Das 17903e786bdSBiju Das return stream; 18003e786bdSBiju Das } 18103e786bdSBiju Das 18226ac471cSBiju Das static inline bool rz_ssi_is_dma_enabled(struct rz_ssi_priv *ssi) 18326ac471cSBiju Das { 1844b14f179SBiju Das return (ssi->playback.dma_ch && (ssi->dma_rt || ssi->capture.dma_ch)); 18526ac471cSBiju Das } 18626ac471cSBiju Das 187962ff7ecSLad Prabhakar static void rz_ssi_set_substream(struct rz_ssi_stream *strm, 188962ff7ecSLad Prabhakar struct snd_pcm_substream *substream) 189962ff7ecSLad Prabhakar { 190962ff7ecSLad Prabhakar struct rz_ssi_priv *ssi = strm->priv; 191962ff7ecSLad Prabhakar unsigned long flags; 192962ff7ecSLad Prabhakar 193962ff7ecSLad Prabhakar spin_lock_irqsave(&ssi->lock, flags); 194962ff7ecSLad Prabhakar strm->substream = substream; 195962ff7ecSLad Prabhakar spin_unlock_irqrestore(&ssi->lock, flags); 196962ff7ecSLad Prabhakar } 197962ff7ecSLad Prabhakar 198e42c903eSLad Prabhakar static bool rz_ssi_stream_is_valid(struct rz_ssi_priv *ssi, 19903e786bdSBiju Das struct rz_ssi_stream *strm) 20003e786bdSBiju Das { 20103e786bdSBiju Das unsigned long flags; 202e42c903eSLad Prabhakar bool ret; 20303e786bdSBiju Das 20403e786bdSBiju Das spin_lock_irqsave(&ssi->lock, flags); 205e42c903eSLad Prabhakar ret = strm->substream && strm->substream->runtime; 20603e786bdSBiju Das spin_unlock_irqrestore(&ssi->lock, flags); 20703e786bdSBiju Das 20803e786bdSBiju Das return ret; 20903e786bdSBiju Das } 21003e786bdSBiju Das 2114f78f3c9SLad Prabhakar static void rz_ssi_stream_init(struct rz_ssi_stream *strm, 21203e786bdSBiju Das struct snd_pcm_substream *substream) 21303e786bdSBiju Das { 21403e786bdSBiju Das struct snd_pcm_runtime *runtime = substream->runtime; 21503e786bdSBiju Das 216962ff7ecSLad Prabhakar rz_ssi_set_substream(strm, substream); 21703e786bdSBiju Das strm->sample_width = samples_to_bytes(runtime, 1); 21826ac471cSBiju Das strm->dma_buffer_pos = 0; 21903e786bdSBiju Das strm->period_counter = 0; 22003e786bdSBiju Das strm->buffer_pos = 0; 22103e786bdSBiju Das 22203e786bdSBiju Das strm->oerr_num = 0; 22303e786bdSBiju Das strm->uerr_num = 0; 22403e786bdSBiju Das strm->running = 0; 22503e786bdSBiju Das 22603e786bdSBiju Das /* fifo init */ 22703e786bdSBiju Das strm->fifo_sample_size = SSI_FIFO_DEPTH; 22803e786bdSBiju Das } 22903e786bdSBiju Das 23003e786bdSBiju Das static void rz_ssi_stream_quit(struct rz_ssi_priv *ssi, 23103e786bdSBiju Das struct rz_ssi_stream *strm) 23203e786bdSBiju Das { 23303e786bdSBiju Das struct snd_soc_dai *dai = rz_ssi_get_dai(strm->substream); 23403e786bdSBiju Das 235962ff7ecSLad Prabhakar rz_ssi_set_substream(strm, NULL); 23603e786bdSBiju Das 23703e786bdSBiju Das if (strm->oerr_num > 0) 23803e786bdSBiju Das dev_info(dai->dev, "overrun = %d\n", strm->oerr_num); 23903e786bdSBiju Das 24003e786bdSBiju Das if (strm->uerr_num > 0) 24103e786bdSBiju Das dev_info(dai->dev, "underrun = %d\n", strm->uerr_num); 24203e786bdSBiju Das } 24303e786bdSBiju Das 24403e786bdSBiju Das static int rz_ssi_clk_setup(struct rz_ssi_priv *ssi, unsigned int rate, 24503e786bdSBiju Das unsigned int channels) 24603e786bdSBiju Das { 24703e786bdSBiju Das static s8 ckdv[16] = { 1, 2, 4, 8, 16, 32, 64, 128, 24803e786bdSBiju Das 6, 12, 24, 48, 96, -1, -1, -1 }; 24903e786bdSBiju Das unsigned int channel_bits = 32; /* System Word Length */ 25003e786bdSBiju Das unsigned long bclk_rate = rate * channels * channel_bits; 25103e786bdSBiju Das unsigned int div; 25203e786bdSBiju Das unsigned int i; 25303e786bdSBiju Das u32 ssicr = 0; 25403e786bdSBiju Das u32 clk_ckdv; 25503e786bdSBiju Das 25603e786bdSBiju Das /* Clear AUCKE so we can set MST */ 25703e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFCR, 0); 25803e786bdSBiju Das 25903e786bdSBiju Das /* Continue to output LRCK pin even when idle */ 26003e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIOFR, SSIOFR_LRCONT); 26103e786bdSBiju Das if (ssi->audio_clk_1 && ssi->audio_clk_2) { 26203e786bdSBiju Das if (ssi->audio_clk_1 % bclk_rate) 26303e786bdSBiju Das ssi->audio_mck = ssi->audio_clk_2; 26403e786bdSBiju Das else 26503e786bdSBiju Das ssi->audio_mck = ssi->audio_clk_1; 26603e786bdSBiju Das } 26703e786bdSBiju Das 26803e786bdSBiju Das /* Clock setting */ 26903e786bdSBiju Das ssicr |= SSICR_MST; 27003e786bdSBiju Das if (ssi->audio_mck == ssi->audio_clk_1) 27103e786bdSBiju Das ssicr |= SSICR_CKS; 27203e786bdSBiju Das if (ssi->bckp_rise) 27303e786bdSBiju Das ssicr |= SSICR_BCKP; 27403e786bdSBiju Das if (ssi->lrckp_fsync_fall) 27503e786bdSBiju Das ssicr |= SSICR_LRCKP; 27603e786bdSBiju Das 27703e786bdSBiju Das /* Determine the clock divider */ 27803e786bdSBiju Das clk_ckdv = 0; 27903e786bdSBiju Das div = ssi->audio_mck / bclk_rate; 28003e786bdSBiju Das /* try to find an match */ 28103e786bdSBiju Das for (i = 0; i < ARRAY_SIZE(ckdv); i++) { 28203e786bdSBiju Das if (ckdv[i] == div) { 28303e786bdSBiju Das clk_ckdv = i; 28403e786bdSBiju Das break; 28503e786bdSBiju Das } 28603e786bdSBiju Das } 28703e786bdSBiju Das 28803e786bdSBiju Das if (i == ARRAY_SIZE(ckdv)) { 28903e786bdSBiju Das dev_err(ssi->dev, "Rate not divisible by audio clock source\n"); 29003e786bdSBiju Das return -EINVAL; 29103e786bdSBiju Das } 29203e786bdSBiju Das 29303e786bdSBiju Das /* 29403e786bdSBiju Das * DWL: Data Word Length = 16 bits 29503e786bdSBiju Das * SWL: System Word Length = 32 bits 29603e786bdSBiju Das */ 29703e786bdSBiju Das ssicr |= SSICR_CKDV(clk_ckdv); 29803e786bdSBiju Das ssicr |= SSICR_DWL(1) | SSICR_SWL(3); 29903e786bdSBiju Das rz_ssi_reg_writel(ssi, SSICR, ssicr); 30003e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFCR, 30103e786bdSBiju Das (SSIFCR_AUCKE | SSIFCR_TFRST | SSIFCR_RFRST)); 30203e786bdSBiju Das 30303e786bdSBiju Das return 0; 30403e786bdSBiju Das } 30503e786bdSBiju Das 30603e786bdSBiju Das static int rz_ssi_start(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm) 30703e786bdSBiju Das { 30803e786bdSBiju Das bool is_play = rz_ssi_stream_is_play(ssi, strm->substream); 30903e786bdSBiju Das u32 ssicr, ssifcr; 31003e786bdSBiju Das 31103e786bdSBiju Das ssicr = rz_ssi_reg_readl(ssi, SSICR); 31203e786bdSBiju Das ssifcr = rz_ssi_reg_readl(ssi, SSIFCR) & ~0xF; 31303e786bdSBiju Das 31403e786bdSBiju Das /* FIFO interrupt thresholds */ 31526ac471cSBiju Das if (rz_ssi_is_dma_enabled(ssi)) 31626ac471cSBiju Das rz_ssi_reg_writel(ssi, SSISCR, 0); 31726ac471cSBiju Das else 31803e786bdSBiju Das rz_ssi_reg_writel(ssi, SSISCR, 31903e786bdSBiju Das SSISCR_TDES(strm->fifo_sample_size / 2 - 1) | 32003e786bdSBiju Das SSISCR_RDFS(0)); 32103e786bdSBiju Das 32203e786bdSBiju Das /* enable IRQ */ 32303e786bdSBiju Das if (is_play) { 32403e786bdSBiju Das ssicr |= SSICR_TUIEN | SSICR_TOIEN; 32503e786bdSBiju Das ssifcr |= SSIFCR_TIE | SSIFCR_RFRST; 32603e786bdSBiju Das } else { 32703e786bdSBiju Das ssicr |= SSICR_RUIEN | SSICR_ROIEN; 32803e786bdSBiju Das ssifcr |= SSIFCR_RIE | SSIFCR_TFRST; 32903e786bdSBiju Das } 33003e786bdSBiju Das 33103e786bdSBiju Das rz_ssi_reg_writel(ssi, SSICR, ssicr); 33203e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFCR, ssifcr); 33303e786bdSBiju Das 33403e786bdSBiju Das /* Clear all error flags */ 33503e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSISR, 33603e786bdSBiju Das (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ | 33703e786bdSBiju Das SSISR_RUIRQ), 0); 33803e786bdSBiju Das 33903e786bdSBiju Das strm->running = 1; 34003e786bdSBiju Das ssicr |= is_play ? SSICR_TEN : SSICR_REN; 34103e786bdSBiju Das rz_ssi_reg_writel(ssi, SSICR, ssicr); 34203e786bdSBiju Das 34303e786bdSBiju Das return 0; 34403e786bdSBiju Das } 34503e786bdSBiju Das 34603e786bdSBiju Das static int rz_ssi_stop(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm) 34703e786bdSBiju Das { 34803e786bdSBiju Das int timeout; 34903e786bdSBiju Das 35003e786bdSBiju Das strm->running = 0; 35103e786bdSBiju Das 35203e786bdSBiju Das /* Disable TX/RX */ 35303e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TEN | SSICR_REN, 0); 35403e786bdSBiju Das 35526ac471cSBiju Das /* Cancel all remaining DMA transactions */ 35626ac471cSBiju Das if (rz_ssi_is_dma_enabled(ssi)) 35726ac471cSBiju Das dmaengine_terminate_async(strm->dma_ch); 35826ac471cSBiju Das 35903e786bdSBiju Das /* Disable irqs */ 36003e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TUIEN | SSICR_TOIEN | 36103e786bdSBiju Das SSICR_RUIEN | SSICR_ROIEN, 0); 36203e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_TIE | SSIFCR_RIE, 0); 36303e786bdSBiju Das 36403e786bdSBiju Das /* Clear all error flags */ 36503e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSISR, 36603e786bdSBiju Das (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ | 36703e786bdSBiju Das SSISR_RUIRQ), 0); 36803e786bdSBiju Das 36903e786bdSBiju Das /* Wait for idle */ 37003e786bdSBiju Das timeout = 100; 37103e786bdSBiju Das while (--timeout) { 3721b5d1d3aSBiju Das if (rz_ssi_reg_readl(ssi, SSISR) & SSISR_IIRQ) 37303e786bdSBiju Das break; 37403e786bdSBiju Das udelay(1); 37503e786bdSBiju Das } 37603e786bdSBiju Das 37703e786bdSBiju Das if (!timeout) 37803e786bdSBiju Das dev_info(ssi->dev, "timeout waiting for SSI idle\n"); 37903e786bdSBiju Das 38003e786bdSBiju Das /* Hold FIFOs in reset */ 38103e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, 0, 38203e786bdSBiju Das SSIFCR_TFRST | SSIFCR_RFRST); 38303e786bdSBiju Das 38403e786bdSBiju Das return 0; 38503e786bdSBiju Das } 38603e786bdSBiju Das 38703e786bdSBiju Das static void rz_ssi_pointer_update(struct rz_ssi_stream *strm, int frames) 38803e786bdSBiju Das { 38903e786bdSBiju Das struct snd_pcm_substream *substream = strm->substream; 39003e786bdSBiju Das struct snd_pcm_runtime *runtime; 39103e786bdSBiju Das int current_period; 39203e786bdSBiju Das 39303e786bdSBiju Das if (!strm->running || !substream || !substream->runtime) 39403e786bdSBiju Das return; 39503e786bdSBiju Das 39603e786bdSBiju Das runtime = substream->runtime; 39703e786bdSBiju Das strm->buffer_pos += frames; 39803e786bdSBiju Das WARN_ON(strm->buffer_pos > runtime->buffer_size); 39903e786bdSBiju Das 40003e786bdSBiju Das /* ring buffer */ 40103e786bdSBiju Das if (strm->buffer_pos == runtime->buffer_size) 40203e786bdSBiju Das strm->buffer_pos = 0; 40303e786bdSBiju Das 40403e786bdSBiju Das current_period = strm->buffer_pos / runtime->period_size; 40503e786bdSBiju Das if (strm->period_counter != current_period) { 40603e786bdSBiju Das snd_pcm_period_elapsed(strm->substream); 40703e786bdSBiju Das strm->period_counter = current_period; 40803e786bdSBiju Das } 40903e786bdSBiju Das } 41003e786bdSBiju Das 41103e786bdSBiju Das static int rz_ssi_pio_recv(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm) 41203e786bdSBiju Das { 41303e786bdSBiju Das struct snd_pcm_substream *substream = strm->substream; 41403e786bdSBiju Das struct snd_pcm_runtime *runtime; 41503e786bdSBiju Das u16 *buf; 41603e786bdSBiju Das int fifo_samples; 41703e786bdSBiju Das int frames_left; 4186570f991SLad Prabhakar int samples; 41903e786bdSBiju Das int i; 42003e786bdSBiju Das 42103e786bdSBiju Das if (!rz_ssi_stream_is_valid(ssi, strm)) 42203e786bdSBiju Das return -EINVAL; 42303e786bdSBiju Das 42403e786bdSBiju Das runtime = substream->runtime; 4256570f991SLad Prabhakar 4267276d3f3SLad Prabhakar do { 42703e786bdSBiju Das /* frames left in this period */ 4286570f991SLad Prabhakar frames_left = runtime->period_size - 4296570f991SLad Prabhakar (strm->buffer_pos % runtime->period_size); 4306570f991SLad Prabhakar if (!frames_left) 43103e786bdSBiju Das frames_left = runtime->period_size; 43203e786bdSBiju Das 43303e786bdSBiju Das /* Samples in RX FIFO */ 43403e786bdSBiju Das fifo_samples = (rz_ssi_reg_readl(ssi, SSIFSR) >> 43503e786bdSBiju Das SSIFSR_RDC_SHIFT) & SSIFSR_RDC_MASK; 43603e786bdSBiju Das 43703e786bdSBiju Das /* Only read full frames at a time */ 4386570f991SLad Prabhakar samples = 0; 43903e786bdSBiju Das while (frames_left && (fifo_samples >= runtime->channels)) { 44003e786bdSBiju Das samples += runtime->channels; 44103e786bdSBiju Das fifo_samples -= runtime->channels; 44203e786bdSBiju Das frames_left--; 44303e786bdSBiju Das } 44403e786bdSBiju Das 44503e786bdSBiju Das /* not enough samples yet */ 4466570f991SLad Prabhakar if (!samples) 4476570f991SLad Prabhakar break; 44803e786bdSBiju Das 44903e786bdSBiju Das /* calculate new buffer index */ 4507276d3f3SLad Prabhakar buf = (u16 *)runtime->dma_area; 45103e786bdSBiju Das buf += strm->buffer_pos * runtime->channels; 45203e786bdSBiju Das 45303e786bdSBiju Das /* Note, only supports 16-bit samples */ 45403e786bdSBiju Das for (i = 0; i < samples; i++) 45503e786bdSBiju Das *buf++ = (u16)(rz_ssi_reg_readl(ssi, SSIFRDR) >> 16); 45603e786bdSBiju Das 45703e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0); 45803e786bdSBiju Das rz_ssi_pointer_update(strm, samples / runtime->channels); 4597276d3f3SLad Prabhakar } while (!frames_left && fifo_samples >= runtime->channels); 46003e786bdSBiju Das 46103e786bdSBiju Das return 0; 46203e786bdSBiju Das } 46303e786bdSBiju Das 46403e786bdSBiju Das static int rz_ssi_pio_send(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm) 46503e786bdSBiju Das { 46603e786bdSBiju Das struct snd_pcm_substream *substream = strm->substream; 46703e786bdSBiju Das struct snd_pcm_runtime *runtime = substream->runtime; 46803e786bdSBiju Das int sample_space; 46903e786bdSBiju Das int samples = 0; 47003e786bdSBiju Das int frames_left; 47103e786bdSBiju Das int i; 47203e786bdSBiju Das u32 ssifsr; 47303e786bdSBiju Das u16 *buf; 47403e786bdSBiju Das 47503e786bdSBiju Das if (!rz_ssi_stream_is_valid(ssi, strm)) 47603e786bdSBiju Das return -EINVAL; 47703e786bdSBiju Das 47803e786bdSBiju Das /* frames left in this period */ 47903e786bdSBiju Das frames_left = runtime->period_size - (strm->buffer_pos % 48003e786bdSBiju Das runtime->period_size); 48103e786bdSBiju Das if (frames_left == 0) 48203e786bdSBiju Das frames_left = runtime->period_size; 48303e786bdSBiju Das 48403e786bdSBiju Das sample_space = strm->fifo_sample_size; 48503e786bdSBiju Das ssifsr = rz_ssi_reg_readl(ssi, SSIFSR); 48603e786bdSBiju Das sample_space -= (ssifsr >> SSIFSR_TDC_SHIFT) & SSIFSR_TDC_MASK; 48703e786bdSBiju Das 48803e786bdSBiju Das /* Only add full frames at a time */ 48903e786bdSBiju Das while (frames_left && (sample_space >= runtime->channels)) { 49003e786bdSBiju Das samples += runtime->channels; 49103e786bdSBiju Das sample_space -= runtime->channels; 49203e786bdSBiju Das frames_left--; 49303e786bdSBiju Das } 49403e786bdSBiju Das 49503e786bdSBiju Das /* no space to send anything right now */ 49603e786bdSBiju Das if (samples == 0) 49703e786bdSBiju Das return 0; 49803e786bdSBiju Das 49903e786bdSBiju Das /* calculate new buffer index */ 50003e786bdSBiju Das buf = (u16 *)(runtime->dma_area); 50103e786bdSBiju Das buf += strm->buffer_pos * runtime->channels; 50203e786bdSBiju Das 50303e786bdSBiju Das /* Note, only supports 16-bit samples */ 50403e786bdSBiju Das for (i = 0; i < samples; i++) 50503e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFTDR, ((u32)(*buf++) << 16)); 50603e786bdSBiju Das 50703e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_TDE, 0); 50803e786bdSBiju Das rz_ssi_pointer_update(strm, samples / runtime->channels); 50903e786bdSBiju Das 51003e786bdSBiju Das return 0; 51103e786bdSBiju Das } 51203e786bdSBiju Das 51303e786bdSBiju Das static irqreturn_t rz_ssi_interrupt(int irq, void *data) 51403e786bdSBiju Das { 51503e786bdSBiju Das struct rz_ssi_stream *strm = NULL; 51603e786bdSBiju Das struct rz_ssi_priv *ssi = data; 51703e786bdSBiju Das u32 ssisr = rz_ssi_reg_readl(ssi, SSISR); 51803e786bdSBiju Das 51903e786bdSBiju Das if (ssi->playback.substream) 52003e786bdSBiju Das strm = &ssi->playback; 52103e786bdSBiju Das else if (ssi->capture.substream) 52203e786bdSBiju Das strm = &ssi->capture; 52303e786bdSBiju Das else 52403e786bdSBiju Das return IRQ_HANDLED; /* Left over TX/RX interrupt */ 52503e786bdSBiju Das 52603e786bdSBiju Das if (irq == ssi->irq_int) { /* error or idle */ 52703e786bdSBiju Das if (ssisr & SSISR_TUIRQ) 52803e786bdSBiju Das strm->uerr_num++; 52903e786bdSBiju Das if (ssisr & SSISR_TOIRQ) 53003e786bdSBiju Das strm->oerr_num++; 53103e786bdSBiju Das if (ssisr & SSISR_RUIRQ) 53203e786bdSBiju Das strm->uerr_num++; 53303e786bdSBiju Das if (ssisr & SSISR_ROIRQ) 53403e786bdSBiju Das strm->oerr_num++; 53503e786bdSBiju Das 53603e786bdSBiju Das if (ssisr & (SSISR_TUIRQ | SSISR_TOIRQ | SSISR_RUIRQ | 53703e786bdSBiju Das SSISR_ROIRQ)) { 53803e786bdSBiju Das /* Error handling */ 53903e786bdSBiju Das /* You must reset (stop/restart) after each interrupt */ 54003e786bdSBiju Das rz_ssi_stop(ssi, strm); 54103e786bdSBiju Das 54203e786bdSBiju Das /* Clear all flags */ 54303e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSISR, SSISR_TOIRQ | 54403e786bdSBiju Das SSISR_TUIRQ | SSISR_ROIRQ | 54503e786bdSBiju Das SSISR_RUIRQ, 0); 54603e786bdSBiju Das 54703e786bdSBiju Das /* Add/remove more data */ 54803e786bdSBiju Das strm->transfer(ssi, strm); 54903e786bdSBiju Das 55003e786bdSBiju Das /* Resume */ 55103e786bdSBiju Das rz_ssi_start(ssi, strm); 55203e786bdSBiju Das } 55303e786bdSBiju Das } 55403e786bdSBiju Das 55503e786bdSBiju Das if (!strm->running) 55603e786bdSBiju Das return IRQ_HANDLED; 55703e786bdSBiju Das 55803e786bdSBiju Das /* tx data empty */ 55903e786bdSBiju Das if (irq == ssi->irq_tx) 56003e786bdSBiju Das strm->transfer(ssi, &ssi->playback); 56103e786bdSBiju Das 56203e786bdSBiju Das /* rx data full */ 56303e786bdSBiju Das if (irq == ssi->irq_rx) { 56403e786bdSBiju Das strm->transfer(ssi, &ssi->capture); 56503e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0); 56603e786bdSBiju Das } 56703e786bdSBiju Das 56803e786bdSBiju Das return IRQ_HANDLED; 56903e786bdSBiju Das } 57003e786bdSBiju Das 57126ac471cSBiju Das static int rz_ssi_dma_slave_config(struct rz_ssi_priv *ssi, 57226ac471cSBiju Das struct dma_chan *dma_ch, bool is_play) 57326ac471cSBiju Das { 57426ac471cSBiju Das struct dma_slave_config cfg; 57526ac471cSBiju Das 57626ac471cSBiju Das memset(&cfg, 0, sizeof(cfg)); 57726ac471cSBiju Das 57826ac471cSBiju Das cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; 57926ac471cSBiju Das cfg.dst_addr = ssi->phys + SSIFTDR; 58026ac471cSBiju Das cfg.src_addr = ssi->phys + SSIFRDR; 58126ac471cSBiju Das cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; 58226ac471cSBiju Das cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; 58326ac471cSBiju Das 58426ac471cSBiju Das return dmaengine_slave_config(dma_ch, &cfg); 58526ac471cSBiju Das } 58626ac471cSBiju Das 58726ac471cSBiju Das static int rz_ssi_dma_transfer(struct rz_ssi_priv *ssi, 58826ac471cSBiju Das struct rz_ssi_stream *strm) 58926ac471cSBiju Das { 59026ac471cSBiju Das struct snd_pcm_substream *substream = strm->substream; 59126ac471cSBiju Das struct dma_async_tx_descriptor *desc; 59226ac471cSBiju Das struct snd_pcm_runtime *runtime; 59326ac471cSBiju Das enum dma_transfer_direction dir; 59426ac471cSBiju Das u32 dma_paddr, dma_size; 59526ac471cSBiju Das int amount; 59626ac471cSBiju Das 59726ac471cSBiju Das if (!rz_ssi_stream_is_valid(ssi, strm)) 59826ac471cSBiju Das return -EINVAL; 59926ac471cSBiju Das 60026ac471cSBiju Das runtime = substream->runtime; 60126ac471cSBiju Das if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) 60226ac471cSBiju Das /* 60326ac471cSBiju Das * Stream is ending, so do not queue up any more DMA 60426ac471cSBiju Das * transfers otherwise we play partial sound clips 60526ac471cSBiju Das * because we can't shut off the DMA quick enough. 60626ac471cSBiju Das */ 60726ac471cSBiju Das return 0; 60826ac471cSBiju Das 60926ac471cSBiju Das dir = rz_ssi_stream_is_play(ssi, substream) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM; 61026ac471cSBiju Das 61126ac471cSBiju Das /* Always transfer 1 period */ 61226ac471cSBiju Das amount = runtime->period_size; 61326ac471cSBiju Das 61426ac471cSBiju Das /* DMA physical address and size */ 61526ac471cSBiju Das dma_paddr = runtime->dma_addr + frames_to_bytes(runtime, 61626ac471cSBiju Das strm->dma_buffer_pos); 61726ac471cSBiju Das dma_size = frames_to_bytes(runtime, amount); 61826ac471cSBiju Das desc = dmaengine_prep_slave_single(strm->dma_ch, dma_paddr, dma_size, 61926ac471cSBiju Das dir, 62026ac471cSBiju Das DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 62126ac471cSBiju Das if (!desc) { 62226ac471cSBiju Das dev_err(ssi->dev, "dmaengine_prep_slave_single() fail\n"); 62326ac471cSBiju Das return -ENOMEM; 62426ac471cSBiju Das } 62526ac471cSBiju Das 62626ac471cSBiju Das desc->callback = rz_ssi_dma_complete; 62726ac471cSBiju Das desc->callback_param = strm; 62826ac471cSBiju Das 62926ac471cSBiju Das if (dmaengine_submit(desc) < 0) { 63026ac471cSBiju Das dev_err(ssi->dev, "dmaengine_submit() fail\n"); 63126ac471cSBiju Das return -EIO; 63226ac471cSBiju Das } 63326ac471cSBiju Das 63426ac471cSBiju Das /* Update DMA pointer */ 63526ac471cSBiju Das strm->dma_buffer_pos += amount; 63626ac471cSBiju Das if (strm->dma_buffer_pos >= runtime->buffer_size) 63726ac471cSBiju Das strm->dma_buffer_pos = 0; 63826ac471cSBiju Das 63926ac471cSBiju Das /* Start DMA */ 64026ac471cSBiju Das dma_async_issue_pending(strm->dma_ch); 64126ac471cSBiju Das 64226ac471cSBiju Das return 0; 64326ac471cSBiju Das } 64426ac471cSBiju Das 64526ac471cSBiju Das static void rz_ssi_dma_complete(void *data) 64626ac471cSBiju Das { 64726ac471cSBiju Das struct rz_ssi_stream *strm = (struct rz_ssi_stream *)data; 64826ac471cSBiju Das 64926ac471cSBiju Das if (!strm->running || !strm->substream || !strm->substream->runtime) 65026ac471cSBiju Das return; 65126ac471cSBiju Das 65226ac471cSBiju Das /* Note that next DMA transaction has probably already started */ 65326ac471cSBiju Das rz_ssi_pointer_update(strm, strm->substream->runtime->period_size); 65426ac471cSBiju Das 65526ac471cSBiju Das /* Queue up another DMA transaction */ 65626ac471cSBiju Das rz_ssi_dma_transfer(strm->priv, strm); 65726ac471cSBiju Das } 65826ac471cSBiju Das 65926ac471cSBiju Das static void rz_ssi_release_dma_channels(struct rz_ssi_priv *ssi) 66026ac471cSBiju Das { 66126ac471cSBiju Das if (ssi->playback.dma_ch) { 66226ac471cSBiju Das dma_release_channel(ssi->playback.dma_ch); 66326ac471cSBiju Das ssi->playback.dma_ch = NULL; 66426ac471cSBiju Das if (ssi->dma_rt) 66526ac471cSBiju Das ssi->dma_rt = false; 66626ac471cSBiju Das } 66726ac471cSBiju Das 66826ac471cSBiju Das if (ssi->capture.dma_ch) { 66926ac471cSBiju Das dma_release_channel(ssi->capture.dma_ch); 67026ac471cSBiju Das ssi->capture.dma_ch = NULL; 67126ac471cSBiju Das } 67226ac471cSBiju Das } 67326ac471cSBiju Das 67426ac471cSBiju Das static int rz_ssi_dma_request(struct rz_ssi_priv *ssi, struct device *dev) 67526ac471cSBiju Das { 67626ac471cSBiju Das ssi->playback.dma_ch = dma_request_chan(dev, "tx"); 6774b14f179SBiju Das if (IS_ERR(ssi->playback.dma_ch)) 6784b14f179SBiju Das ssi->playback.dma_ch = NULL; 6794b14f179SBiju Das 68026ac471cSBiju Das ssi->capture.dma_ch = dma_request_chan(dev, "rx"); 6814b14f179SBiju Das if (IS_ERR(ssi->capture.dma_ch)) 6824b14f179SBiju Das ssi->capture.dma_ch = NULL; 6834b14f179SBiju Das 68426ac471cSBiju Das if (!ssi->playback.dma_ch && !ssi->capture.dma_ch) { 68526ac471cSBiju Das ssi->playback.dma_ch = dma_request_chan(dev, "rt"); 6864b14f179SBiju Das if (IS_ERR(ssi->playback.dma_ch)) { 6874b14f179SBiju Das ssi->playback.dma_ch = NULL; 68826ac471cSBiju Das goto no_dma; 6894b14f179SBiju Das } 69026ac471cSBiju Das 69126ac471cSBiju Das ssi->dma_rt = true; 69226ac471cSBiju Das } 69326ac471cSBiju Das 6944b14f179SBiju Das if (!rz_ssi_is_dma_enabled(ssi)) 6954b14f179SBiju Das goto no_dma; 6964b14f179SBiju Das 69726ac471cSBiju Das if (ssi->playback.dma_ch && 69826ac471cSBiju Das (rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch, true) < 0)) 69926ac471cSBiju Das goto no_dma; 70026ac471cSBiju Das 70126ac471cSBiju Das if (ssi->capture.dma_ch && 70226ac471cSBiju Das (rz_ssi_dma_slave_config(ssi, ssi->capture.dma_ch, false) < 0)) 70326ac471cSBiju Das goto no_dma; 70426ac471cSBiju Das 70526ac471cSBiju Das return 0; 70626ac471cSBiju Das 70726ac471cSBiju Das no_dma: 70826ac471cSBiju Das rz_ssi_release_dma_channels(ssi); 70926ac471cSBiju Das 71026ac471cSBiju Das return -ENODEV; 71126ac471cSBiju Das } 71226ac471cSBiju Das 71303e786bdSBiju Das static int rz_ssi_dai_trigger(struct snd_pcm_substream *substream, int cmd, 71403e786bdSBiju Das struct snd_soc_dai *dai) 71503e786bdSBiju Das { 71603e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai); 71703e786bdSBiju Das struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream); 71826ac471cSBiju Das int ret = 0, i, num_transfer = 1; 71903e786bdSBiju Das 72003e786bdSBiju Das switch (cmd) { 72103e786bdSBiju Das case SNDRV_PCM_TRIGGER_START: 72203e786bdSBiju Das /* Soft Reset */ 72303e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, 0, SSIFCR_SSIRST); 72403e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_SSIRST, 0); 72503e786bdSBiju Das udelay(5); 72603e786bdSBiju Das 7274f78f3c9SLad Prabhakar rz_ssi_stream_init(strm, substream); 72803e786bdSBiju Das 72926ac471cSBiju Das if (ssi->dma_rt) { 73026ac471cSBiju Das bool is_playback; 73126ac471cSBiju Das 73226ac471cSBiju Das is_playback = rz_ssi_stream_is_play(ssi, substream); 73326ac471cSBiju Das ret = rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch, 73426ac471cSBiju Das is_playback); 73526ac471cSBiju Das /* Fallback to pio */ 73626ac471cSBiju Das if (ret < 0) { 73726ac471cSBiju Das ssi->playback.transfer = rz_ssi_pio_send; 73826ac471cSBiju Das ssi->capture.transfer = rz_ssi_pio_recv; 73926ac471cSBiju Das rz_ssi_release_dma_channels(ssi); 74026ac471cSBiju Das } 74126ac471cSBiju Das } 74226ac471cSBiju Das 74326ac471cSBiju Das /* For DMA, queue up multiple DMA descriptors */ 74426ac471cSBiju Das if (rz_ssi_is_dma_enabled(ssi)) 74526ac471cSBiju Das num_transfer = 4; 74626ac471cSBiju Das 74726ac471cSBiju Das for (i = 0; i < num_transfer; i++) { 74803e786bdSBiju Das ret = strm->transfer(ssi, strm); 74903e786bdSBiju Das if (ret) 75003e786bdSBiju Das goto done; 75126ac471cSBiju Das } 75203e786bdSBiju Das 75303e786bdSBiju Das ret = rz_ssi_start(ssi, strm); 75403e786bdSBiju Das break; 75503e786bdSBiju Das case SNDRV_PCM_TRIGGER_STOP: 75603e786bdSBiju Das rz_ssi_stop(ssi, strm); 75703e786bdSBiju Das rz_ssi_stream_quit(ssi, strm); 75803e786bdSBiju Das break; 75903e786bdSBiju Das } 76003e786bdSBiju Das 76103e786bdSBiju Das done: 76203e786bdSBiju Das return ret; 76303e786bdSBiju Das } 76403e786bdSBiju Das 76503e786bdSBiju Das static int rz_ssi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) 76603e786bdSBiju Das { 76703e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai); 76803e786bdSBiju Das 76903e786bdSBiju Das switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) { 7702d4dd776SCharles Keepax case SND_SOC_DAIFMT_BP_FP: 77103e786bdSBiju Das break; 77203e786bdSBiju Das default: 77303e786bdSBiju Das dev_err(ssi->dev, "Codec should be clk and frame consumer\n"); 77403e786bdSBiju Das return -EINVAL; 77503e786bdSBiju Das } 77603e786bdSBiju Das 77703e786bdSBiju Das /* 77803e786bdSBiju Das * set clock polarity 77903e786bdSBiju Das * 78003e786bdSBiju Das * "normal" BCLK = Signal is available at rising edge of BCLK 78103e786bdSBiju Das * "normal" FSYNC = (I2S) Left ch starts with falling FSYNC edge 78203e786bdSBiju Das */ 78303e786bdSBiju Das switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 78403e786bdSBiju Das case SND_SOC_DAIFMT_NB_NF: 78503e786bdSBiju Das ssi->bckp_rise = false; 78603e786bdSBiju Das ssi->lrckp_fsync_fall = false; 78703e786bdSBiju Das break; 78803e786bdSBiju Das case SND_SOC_DAIFMT_NB_IF: 78903e786bdSBiju Das ssi->bckp_rise = false; 79003e786bdSBiju Das ssi->lrckp_fsync_fall = true; 79103e786bdSBiju Das break; 79203e786bdSBiju Das case SND_SOC_DAIFMT_IB_NF: 79303e786bdSBiju Das ssi->bckp_rise = true; 79403e786bdSBiju Das ssi->lrckp_fsync_fall = false; 79503e786bdSBiju Das break; 79603e786bdSBiju Das case SND_SOC_DAIFMT_IB_IF: 79703e786bdSBiju Das ssi->bckp_rise = true; 79803e786bdSBiju Das ssi->lrckp_fsync_fall = true; 79903e786bdSBiju Das break; 80003e786bdSBiju Das default: 80103e786bdSBiju Das return -EINVAL; 80203e786bdSBiju Das } 80303e786bdSBiju Das 80403e786bdSBiju Das /* only i2s support */ 80503e786bdSBiju Das switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 80603e786bdSBiju Das case SND_SOC_DAIFMT_I2S: 80703e786bdSBiju Das break; 80803e786bdSBiju Das default: 80903e786bdSBiju Das dev_err(ssi->dev, "Only I2S mode is supported.\n"); 81003e786bdSBiju Das return -EINVAL; 81103e786bdSBiju Das } 81203e786bdSBiju Das 81303e786bdSBiju Das return 0; 81403e786bdSBiju Das } 81503e786bdSBiju Das 81603e786bdSBiju Das static int rz_ssi_dai_hw_params(struct snd_pcm_substream *substream, 81703e786bdSBiju Das struct snd_pcm_hw_params *params, 81803e786bdSBiju Das struct snd_soc_dai *dai) 81903e786bdSBiju Das { 82003e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai); 82103e786bdSBiju Das unsigned int sample_bits = hw_param_interval(params, 82203e786bdSBiju Das SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min; 82303e786bdSBiju Das unsigned int channels = params_channels(params); 82403e786bdSBiju Das 82503e786bdSBiju Das if (sample_bits != 16) { 82603e786bdSBiju Das dev_err(ssi->dev, "Unsupported sample width: %d\n", 82703e786bdSBiju Das sample_bits); 82803e786bdSBiju Das return -EINVAL; 82903e786bdSBiju Das } 83003e786bdSBiju Das 83103e786bdSBiju Das if (channels != 2) { 83203e786bdSBiju Das dev_err(ssi->dev, "Number of channels not matched: %d\n", 83303e786bdSBiju Das channels); 83403e786bdSBiju Das return -EINVAL; 83503e786bdSBiju Das } 83603e786bdSBiju Das 83703e786bdSBiju Das return rz_ssi_clk_setup(ssi, params_rate(params), 83803e786bdSBiju Das params_channels(params)); 83903e786bdSBiju Das } 84003e786bdSBiju Das 84103e786bdSBiju Das static const struct snd_soc_dai_ops rz_ssi_dai_ops = { 84203e786bdSBiju Das .trigger = rz_ssi_dai_trigger, 843adced680SCharles Keepax .set_fmt = rz_ssi_dai_set_fmt, 84403e786bdSBiju Das .hw_params = rz_ssi_dai_hw_params, 84503e786bdSBiju Das }; 84603e786bdSBiju Das 84703e786bdSBiju Das static const struct snd_pcm_hardware rz_ssi_pcm_hardware = { 84803e786bdSBiju Das .info = SNDRV_PCM_INFO_INTERLEAVED | 84903e786bdSBiju Das SNDRV_PCM_INFO_MMAP | 85003e786bdSBiju Das SNDRV_PCM_INFO_MMAP_VALID, 85103e786bdSBiju Das .buffer_bytes_max = PREALLOC_BUFFER, 85203e786bdSBiju Das .period_bytes_min = 32, 85303e786bdSBiju Das .period_bytes_max = 8192, 85403e786bdSBiju Das .channels_min = SSI_CHAN_MIN, 85503e786bdSBiju Das .channels_max = SSI_CHAN_MAX, 85603e786bdSBiju Das .periods_min = 1, 85703e786bdSBiju Das .periods_max = 32, 85803e786bdSBiju Das .fifo_size = 32 * 2, 85903e786bdSBiju Das }; 86003e786bdSBiju Das 86103e786bdSBiju Das static int rz_ssi_pcm_open(struct snd_soc_component *component, 86203e786bdSBiju Das struct snd_pcm_substream *substream) 86303e786bdSBiju Das { 86403e786bdSBiju Das snd_soc_set_runtime_hwparams(substream, &rz_ssi_pcm_hardware); 86503e786bdSBiju Das 86603e786bdSBiju Das return snd_pcm_hw_constraint_integer(substream->runtime, 86703e786bdSBiju Das SNDRV_PCM_HW_PARAM_PERIODS); 86803e786bdSBiju Das } 86903e786bdSBiju Das 87003e786bdSBiju Das static snd_pcm_uframes_t rz_ssi_pcm_pointer(struct snd_soc_component *component, 87103e786bdSBiju Das struct snd_pcm_substream *substream) 87203e786bdSBiju Das { 87303e786bdSBiju Das struct snd_soc_dai *dai = rz_ssi_get_dai(substream); 87403e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai); 87503e786bdSBiju Das struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream); 87603e786bdSBiju Das 87703e786bdSBiju Das return strm->buffer_pos; 87803e786bdSBiju Das } 87903e786bdSBiju Das 88003e786bdSBiju Das static int rz_ssi_pcm_new(struct snd_soc_component *component, 88103e786bdSBiju Das struct snd_soc_pcm_runtime *rtd) 88203e786bdSBiju Das { 88303e786bdSBiju Das snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV, 88403e786bdSBiju Das rtd->card->snd_card->dev, 88503e786bdSBiju Das PREALLOC_BUFFER, PREALLOC_BUFFER_MAX); 88603e786bdSBiju Das return 0; 88703e786bdSBiju Das } 88803e786bdSBiju Das 88903e786bdSBiju Das static struct snd_soc_dai_driver rz_ssi_soc_dai[] = { 89003e786bdSBiju Das { 89103e786bdSBiju Das .name = "rz-ssi-dai", 89203e786bdSBiju Das .playback = { 89303e786bdSBiju Das .rates = SSI_RATES, 89403e786bdSBiju Das .formats = SSI_FMTS, 89503e786bdSBiju Das .channels_min = SSI_CHAN_MIN, 89603e786bdSBiju Das .channels_max = SSI_CHAN_MAX, 89703e786bdSBiju Das }, 89803e786bdSBiju Das .capture = { 89903e786bdSBiju Das .rates = SSI_RATES, 90003e786bdSBiju Das .formats = SSI_FMTS, 90103e786bdSBiju Das .channels_min = SSI_CHAN_MIN, 90203e786bdSBiju Das .channels_max = SSI_CHAN_MAX, 90303e786bdSBiju Das }, 90403e786bdSBiju Das .ops = &rz_ssi_dai_ops, 90503e786bdSBiju Das }, 90603e786bdSBiju Das }; 90703e786bdSBiju Das 90803e786bdSBiju Das static const struct snd_soc_component_driver rz_ssi_soc_component = { 90903e786bdSBiju Das .name = "rz-ssi", 91003e786bdSBiju Das .open = rz_ssi_pcm_open, 91103e786bdSBiju Das .pointer = rz_ssi_pcm_pointer, 91203e786bdSBiju Das .pcm_construct = rz_ssi_pcm_new, 913f712ff57SCharles Keepax .legacy_dai_naming = 1, 91403e786bdSBiju Das }; 91503e786bdSBiju Das 91603e786bdSBiju Das static int rz_ssi_probe(struct platform_device *pdev) 91703e786bdSBiju Das { 91803e786bdSBiju Das struct rz_ssi_priv *ssi; 91903e786bdSBiju Das struct clk *audio_clk; 92026ac471cSBiju Das struct resource *res; 92103e786bdSBiju Das int ret; 92203e786bdSBiju Das 92303e786bdSBiju Das ssi = devm_kzalloc(&pdev->dev, sizeof(*ssi), GFP_KERNEL); 92403e786bdSBiju Das if (!ssi) 92503e786bdSBiju Das return -ENOMEM; 92603e786bdSBiju Das 92703e786bdSBiju Das ssi->pdev = pdev; 92803e786bdSBiju Das ssi->dev = &pdev->dev; 92926ac471cSBiju Das ssi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); 93003e786bdSBiju Das if (IS_ERR(ssi->base)) 93103e786bdSBiju Das return PTR_ERR(ssi->base); 93203e786bdSBiju Das 93326ac471cSBiju Das ssi->phys = res->start; 93403e786bdSBiju Das ssi->clk = devm_clk_get(&pdev->dev, "ssi"); 93503e786bdSBiju Das if (IS_ERR(ssi->clk)) 93603e786bdSBiju Das return PTR_ERR(ssi->clk); 93703e786bdSBiju Das 93803e786bdSBiju Das ssi->sfr_clk = devm_clk_get(&pdev->dev, "ssi_sfr"); 93903e786bdSBiju Das if (IS_ERR(ssi->sfr_clk)) 94003e786bdSBiju Das return PTR_ERR(ssi->sfr_clk); 94103e786bdSBiju Das 94203e786bdSBiju Das audio_clk = devm_clk_get(&pdev->dev, "audio_clk1"); 94303e786bdSBiju Das if (IS_ERR(audio_clk)) 94403e786bdSBiju Das return dev_err_probe(&pdev->dev, PTR_ERR(audio_clk), 94503e786bdSBiju Das "no audio clk1"); 94603e786bdSBiju Das 94703e786bdSBiju Das ssi->audio_clk_1 = clk_get_rate(audio_clk); 94803e786bdSBiju Das audio_clk = devm_clk_get(&pdev->dev, "audio_clk2"); 94903e786bdSBiju Das if (IS_ERR(audio_clk)) 95003e786bdSBiju Das return dev_err_probe(&pdev->dev, PTR_ERR(audio_clk), 95103e786bdSBiju Das "no audio clk2"); 95203e786bdSBiju Das 95303e786bdSBiju Das ssi->audio_clk_2 = clk_get_rate(audio_clk); 95403e786bdSBiju Das if (!(ssi->audio_clk_1 || ssi->audio_clk_2)) 95503e786bdSBiju Das return dev_err_probe(&pdev->dev, -EINVAL, 95603e786bdSBiju Das "no audio clk1 or audio clk2"); 95703e786bdSBiju Das 95803e786bdSBiju Das ssi->audio_mck = ssi->audio_clk_1 ? ssi->audio_clk_1 : ssi->audio_clk_2; 95903e786bdSBiju Das 96026ac471cSBiju Das /* Detect DMA support */ 96126ac471cSBiju Das ret = rz_ssi_dma_request(ssi, &pdev->dev); 96226ac471cSBiju Das if (ret < 0) { 96326ac471cSBiju Das dev_warn(&pdev->dev, "DMA not available, using PIO\n"); 96403e786bdSBiju Das ssi->playback.transfer = rz_ssi_pio_send; 96503e786bdSBiju Das ssi->capture.transfer = rz_ssi_pio_recv; 96626ac471cSBiju Das } else { 96726ac471cSBiju Das dev_info(&pdev->dev, "DMA enabled"); 96826ac471cSBiju Das ssi->playback.transfer = rz_ssi_dma_transfer; 96926ac471cSBiju Das ssi->capture.transfer = rz_ssi_dma_transfer; 97026ac471cSBiju Das } 97126ac471cSBiju Das 97203e786bdSBiju Das ssi->playback.priv = ssi; 97303e786bdSBiju Das ssi->capture.priv = ssi; 97403e786bdSBiju Das 9750788785cSLad Prabhakar spin_lock_init(&ssi->lock); 9760788785cSLad Prabhakar dev_set_drvdata(&pdev->dev, ssi); 9770788785cSLad Prabhakar 97803e786bdSBiju Das /* Error Interrupt */ 97903e786bdSBiju Das ssi->irq_int = platform_get_irq_byname(pdev, "int_req"); 980767e6f26SLad Prabhakar if (ssi->irq_int < 0) { 981767e6f26SLad Prabhakar rz_ssi_release_dma_channels(ssi); 98291686a39SLad Prabhakar return ssi->irq_int; 983767e6f26SLad Prabhakar } 98403e786bdSBiju Das 98503e786bdSBiju Das ret = devm_request_irq(&pdev->dev, ssi->irq_int, &rz_ssi_interrupt, 98603e786bdSBiju Das 0, dev_name(&pdev->dev), ssi); 987767e6f26SLad Prabhakar if (ret < 0) { 988767e6f26SLad Prabhakar rz_ssi_release_dma_channels(ssi); 98903e786bdSBiju Das return dev_err_probe(&pdev->dev, ret, 99003e786bdSBiju Das "irq request error (int_req)\n"); 991767e6f26SLad Prabhakar } 99203e786bdSBiju Das 99326ac471cSBiju Das if (!rz_ssi_is_dma_enabled(ssi)) { 99403e786bdSBiju Das /* Tx and Rx interrupts (pio only) */ 99503e786bdSBiju Das ssi->irq_tx = platform_get_irq_byname(pdev, "dma_tx"); 99603e786bdSBiju Das if (ssi->irq_tx < 0) 99791686a39SLad Prabhakar return ssi->irq_tx; 99803e786bdSBiju Das 99926ac471cSBiju Das ret = devm_request_irq(&pdev->dev, ssi->irq_tx, 100026ac471cSBiju Das &rz_ssi_interrupt, 0, 100103e786bdSBiju Das dev_name(&pdev->dev), ssi); 100203e786bdSBiju Das if (ret < 0) 100303e786bdSBiju Das return dev_err_probe(&pdev->dev, ret, 100403e786bdSBiju Das "irq request error (dma_tx)\n"); 100503e786bdSBiju Das 100603e786bdSBiju Das ssi->irq_rx = platform_get_irq_byname(pdev, "dma_rx"); 100703e786bdSBiju Das if (ssi->irq_rx < 0) 100891686a39SLad Prabhakar return ssi->irq_rx; 100903e786bdSBiju Das 101026ac471cSBiju Das ret = devm_request_irq(&pdev->dev, ssi->irq_rx, 101126ac471cSBiju Das &rz_ssi_interrupt, 0, 101203e786bdSBiju Das dev_name(&pdev->dev), ssi); 101303e786bdSBiju Das if (ret < 0) 101403e786bdSBiju Das return dev_err_probe(&pdev->dev, ret, 101503e786bdSBiju Das "irq request error (dma_rx)\n"); 101626ac471cSBiju Das } 101703e786bdSBiju Das 101803e786bdSBiju Das ssi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL); 1019767e6f26SLad Prabhakar if (IS_ERR(ssi->rstc)) { 1020*c75ed9f5SBiju Das ret = PTR_ERR(ssi->rstc); 1021*c75ed9f5SBiju Das goto err_reset; 1022767e6f26SLad Prabhakar } 102303e786bdSBiju Das 102403e786bdSBiju Das reset_control_deassert(ssi->rstc); 102503e786bdSBiju Das pm_runtime_enable(&pdev->dev); 1026f04b4fb4SHeiner Kallweit ret = pm_runtime_resume_and_get(&pdev->dev); 1027f04b4fb4SHeiner Kallweit if (ret < 0) { 1028*c75ed9f5SBiju Das dev_err(&pdev->dev, "pm_runtime_resume_and_get failed\n"); 1029*c75ed9f5SBiju Das goto err_pm; 1030f04b4fb4SHeiner Kallweit } 103103e786bdSBiju Das 103203e786bdSBiju Das ret = devm_snd_soc_register_component(&pdev->dev, &rz_ssi_soc_component, 103303e786bdSBiju Das rz_ssi_soc_dai, 103403e786bdSBiju Das ARRAY_SIZE(rz_ssi_soc_dai)); 103503e786bdSBiju Das if (ret < 0) { 1036*c75ed9f5SBiju Das dev_err(&pdev->dev, "failed to register snd component\n"); 1037*c75ed9f5SBiju Das goto err_snd_soc; 1038*c75ed9f5SBiju Das } 103926ac471cSBiju Das 1040*c75ed9f5SBiju Das return 0; 1041*c75ed9f5SBiju Das 1042*c75ed9f5SBiju Das err_snd_soc: 104303e786bdSBiju Das pm_runtime_put(ssi->dev); 1044*c75ed9f5SBiju Das err_pm: 104503e786bdSBiju Das pm_runtime_disable(ssi->dev); 104603e786bdSBiju Das reset_control_assert(ssi->rstc); 1047*c75ed9f5SBiju Das err_reset: 1048*c75ed9f5SBiju Das rz_ssi_release_dma_channels(ssi); 104903e786bdSBiju Das 105003e786bdSBiju Das return ret; 105103e786bdSBiju Das } 105203e786bdSBiju Das 105303e786bdSBiju Das static int rz_ssi_remove(struct platform_device *pdev) 105403e786bdSBiju Das { 105503e786bdSBiju Das struct rz_ssi_priv *ssi = dev_get_drvdata(&pdev->dev); 105603e786bdSBiju Das 105726ac471cSBiju Das rz_ssi_release_dma_channels(ssi); 105826ac471cSBiju Das 105903e786bdSBiju Das pm_runtime_put(ssi->dev); 106003e786bdSBiju Das pm_runtime_disable(ssi->dev); 106103e786bdSBiju Das reset_control_assert(ssi->rstc); 106203e786bdSBiju Das 106303e786bdSBiju Das return 0; 106403e786bdSBiju Das } 106503e786bdSBiju Das 106603e786bdSBiju Das static const struct of_device_id rz_ssi_of_match[] = { 106703e786bdSBiju Das { .compatible = "renesas,rz-ssi", }, 106803e786bdSBiju Das {/* Sentinel */}, 106903e786bdSBiju Das }; 107003e786bdSBiju Das MODULE_DEVICE_TABLE(of, rz_ssi_of_match); 107103e786bdSBiju Das 107203e786bdSBiju Das static struct platform_driver rz_ssi_driver = { 107303e786bdSBiju Das .driver = { 107403e786bdSBiju Das .name = "rz-ssi-pcm-audio", 107503e786bdSBiju Das .of_match_table = rz_ssi_of_match, 107603e786bdSBiju Das }, 107703e786bdSBiju Das .probe = rz_ssi_probe, 107803e786bdSBiju Das .remove = rz_ssi_remove, 107903e786bdSBiju Das }; 108003e786bdSBiju Das 108103e786bdSBiju Das module_platform_driver(rz_ssi_driver); 108203e786bdSBiju Das 108303e786bdSBiju Das MODULE_LICENSE("GPL v2"); 108403e786bdSBiju Das MODULE_DESCRIPTION("Renesas RZ/G2L ASoC Serial Sound Interface Driver"); 108503e786bdSBiju Das MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>"); 1086