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;
11238c042b5SLad Prabhakar int irq_rt;
11303e786bdSBiju Das
11403e786bdSBiju Das spinlock_t lock;
11503e786bdSBiju Das
11603e786bdSBiju Das /*
11703e786bdSBiju Das * The SSI supports full-duplex transmission and reception.
11803e786bdSBiju Das * However, if an error occurs, channel reset (both transmission
11903e786bdSBiju Das * and reception reset) is required.
12003e786bdSBiju Das * So it is better to use as half-duplex (playing and recording
12103e786bdSBiju Das * should be done on separate channels).
12203e786bdSBiju Das */
12303e786bdSBiju Das struct rz_ssi_stream playback;
12403e786bdSBiju Das struct rz_ssi_stream capture;
12503e786bdSBiju Das
12603e786bdSBiju Das /* clock */
12703e786bdSBiju Das unsigned long audio_mck;
12803e786bdSBiju Das unsigned long audio_clk_1;
12903e786bdSBiju Das unsigned long audio_clk_2;
13003e786bdSBiju Das
13103e786bdSBiju Das bool lrckp_fsync_fall; /* LR clock polarity (SSICR.LRCKP) */
13203e786bdSBiju Das bool bckp_rise; /* Bit clock polarity (SSICR.BCKP) */
13326ac471cSBiju Das bool dma_rt;
13403e786bdSBiju Das };
13503e786bdSBiju Das
13626ac471cSBiju Das static void rz_ssi_dma_complete(void *data);
13726ac471cSBiju Das
rz_ssi_reg_writel(struct rz_ssi_priv * priv,uint reg,u32 data)13803e786bdSBiju Das static void rz_ssi_reg_writel(struct rz_ssi_priv *priv, uint reg, u32 data)
13903e786bdSBiju Das {
14003e786bdSBiju Das writel(data, (priv->base + reg));
14103e786bdSBiju Das }
14203e786bdSBiju Das
rz_ssi_reg_readl(struct rz_ssi_priv * priv,uint reg)14303e786bdSBiju Das static u32 rz_ssi_reg_readl(struct rz_ssi_priv *priv, uint reg)
14403e786bdSBiju Das {
14503e786bdSBiju Das return readl(priv->base + reg);
14603e786bdSBiju Das }
14703e786bdSBiju Das
rz_ssi_reg_mask_setl(struct rz_ssi_priv * priv,uint reg,u32 bclr,u32 bset)148d40dfb86SBiju Das static void rz_ssi_reg_mask_setl(struct rz_ssi_priv *priv, uint reg,
14903e786bdSBiju Das u32 bclr, u32 bset)
15003e786bdSBiju Das {
15103e786bdSBiju Das u32 val;
15203e786bdSBiju Das
15303e786bdSBiju Das val = readl(priv->base + reg);
15403e786bdSBiju Das val = (val & ~bclr) | bset;
15503e786bdSBiju Das writel(val, (priv->base + reg));
15603e786bdSBiju Das }
15703e786bdSBiju Das
15803e786bdSBiju Das static inline struct snd_soc_dai *
rz_ssi_get_dai(struct snd_pcm_substream * substream)15903e786bdSBiju Das rz_ssi_get_dai(struct snd_pcm_substream *substream)
16003e786bdSBiju Das {
16103e786bdSBiju Das struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
16203e786bdSBiju Das
16303e786bdSBiju Das return asoc_rtd_to_cpu(rtd, 0);
16403e786bdSBiju Das }
16503e786bdSBiju Das
rz_ssi_stream_is_play(struct rz_ssi_priv * ssi,struct snd_pcm_substream * substream)16603e786bdSBiju Das static inline bool rz_ssi_stream_is_play(struct rz_ssi_priv *ssi,
16703e786bdSBiju Das struct snd_pcm_substream *substream)
16803e786bdSBiju Das {
16903e786bdSBiju Das return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
17003e786bdSBiju Das }
17103e786bdSBiju Das
17203e786bdSBiju Das static inline struct rz_ssi_stream *
rz_ssi_stream_get(struct rz_ssi_priv * ssi,struct snd_pcm_substream * substream)17303e786bdSBiju Das rz_ssi_stream_get(struct rz_ssi_priv *ssi, struct snd_pcm_substream *substream)
17403e786bdSBiju Das {
17503e786bdSBiju Das struct rz_ssi_stream *stream = &ssi->playback;
17603e786bdSBiju Das
17703e786bdSBiju Das if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
17803e786bdSBiju Das stream = &ssi->capture;
17903e786bdSBiju Das
18003e786bdSBiju Das return stream;
18103e786bdSBiju Das }
18203e786bdSBiju Das
rz_ssi_is_dma_enabled(struct rz_ssi_priv * ssi)18326ac471cSBiju Das static inline bool rz_ssi_is_dma_enabled(struct rz_ssi_priv *ssi)
18426ac471cSBiju Das {
1854b14f179SBiju Das return (ssi->playback.dma_ch && (ssi->dma_rt || ssi->capture.dma_ch));
18626ac471cSBiju Das }
18726ac471cSBiju Das
rz_ssi_set_substream(struct rz_ssi_stream * strm,struct snd_pcm_substream * substream)188962ff7ecSLad Prabhakar static void rz_ssi_set_substream(struct rz_ssi_stream *strm,
189962ff7ecSLad Prabhakar struct snd_pcm_substream *substream)
190962ff7ecSLad Prabhakar {
191962ff7ecSLad Prabhakar struct rz_ssi_priv *ssi = strm->priv;
192962ff7ecSLad Prabhakar unsigned long flags;
193962ff7ecSLad Prabhakar
194962ff7ecSLad Prabhakar spin_lock_irqsave(&ssi->lock, flags);
195962ff7ecSLad Prabhakar strm->substream = substream;
196962ff7ecSLad Prabhakar spin_unlock_irqrestore(&ssi->lock, flags);
197962ff7ecSLad Prabhakar }
198962ff7ecSLad Prabhakar
rz_ssi_stream_is_valid(struct rz_ssi_priv * ssi,struct rz_ssi_stream * strm)199e42c903eSLad Prabhakar static bool rz_ssi_stream_is_valid(struct rz_ssi_priv *ssi,
20003e786bdSBiju Das struct rz_ssi_stream *strm)
20103e786bdSBiju Das {
20203e786bdSBiju Das unsigned long flags;
203e42c903eSLad Prabhakar bool ret;
20403e786bdSBiju Das
20503e786bdSBiju Das spin_lock_irqsave(&ssi->lock, flags);
206e42c903eSLad Prabhakar ret = strm->substream && strm->substream->runtime;
20703e786bdSBiju Das spin_unlock_irqrestore(&ssi->lock, flags);
20803e786bdSBiju Das
20903e786bdSBiju Das return ret;
21003e786bdSBiju Das }
21103e786bdSBiju Das
rz_ssi_stream_init(struct rz_ssi_stream * strm,struct snd_pcm_substream * substream)2124f78f3c9SLad Prabhakar static void rz_ssi_stream_init(struct rz_ssi_stream *strm,
21303e786bdSBiju Das struct snd_pcm_substream *substream)
21403e786bdSBiju Das {
21503e786bdSBiju Das struct snd_pcm_runtime *runtime = substream->runtime;
21603e786bdSBiju Das
217962ff7ecSLad Prabhakar rz_ssi_set_substream(strm, substream);
21803e786bdSBiju Das strm->sample_width = samples_to_bytes(runtime, 1);
21926ac471cSBiju Das strm->dma_buffer_pos = 0;
22003e786bdSBiju Das strm->period_counter = 0;
22103e786bdSBiju Das strm->buffer_pos = 0;
22203e786bdSBiju Das
22303e786bdSBiju Das strm->oerr_num = 0;
22403e786bdSBiju Das strm->uerr_num = 0;
22503e786bdSBiju Das strm->running = 0;
22603e786bdSBiju Das
22703e786bdSBiju Das /* fifo init */
22803e786bdSBiju Das strm->fifo_sample_size = SSI_FIFO_DEPTH;
22903e786bdSBiju Das }
23003e786bdSBiju Das
rz_ssi_stream_quit(struct rz_ssi_priv * ssi,struct rz_ssi_stream * strm)23103e786bdSBiju Das static void rz_ssi_stream_quit(struct rz_ssi_priv *ssi,
23203e786bdSBiju Das struct rz_ssi_stream *strm)
23303e786bdSBiju Das {
23403e786bdSBiju Das struct snd_soc_dai *dai = rz_ssi_get_dai(strm->substream);
23503e786bdSBiju Das
236962ff7ecSLad Prabhakar rz_ssi_set_substream(strm, NULL);
23703e786bdSBiju Das
23803e786bdSBiju Das if (strm->oerr_num > 0)
23903e786bdSBiju Das dev_info(dai->dev, "overrun = %d\n", strm->oerr_num);
24003e786bdSBiju Das
24103e786bdSBiju Das if (strm->uerr_num > 0)
24203e786bdSBiju Das dev_info(dai->dev, "underrun = %d\n", strm->uerr_num);
24303e786bdSBiju Das }
24403e786bdSBiju Das
rz_ssi_clk_setup(struct rz_ssi_priv * ssi,unsigned int rate,unsigned int channels)24503e786bdSBiju Das static int rz_ssi_clk_setup(struct rz_ssi_priv *ssi, unsigned int rate,
24603e786bdSBiju Das unsigned int channels)
24703e786bdSBiju Das {
24803e786bdSBiju Das static s8 ckdv[16] = { 1, 2, 4, 8, 16, 32, 64, 128,
24903e786bdSBiju Das 6, 12, 24, 48, 96, -1, -1, -1 };
25003e786bdSBiju Das unsigned int channel_bits = 32; /* System Word Length */
25103e786bdSBiju Das unsigned long bclk_rate = rate * channels * channel_bits;
25203e786bdSBiju Das unsigned int div;
25303e786bdSBiju Das unsigned int i;
25403e786bdSBiju Das u32 ssicr = 0;
25503e786bdSBiju Das u32 clk_ckdv;
25603e786bdSBiju Das
25703e786bdSBiju Das /* Clear AUCKE so we can set MST */
25803e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFCR, 0);
25903e786bdSBiju Das
26003e786bdSBiju Das /* Continue to output LRCK pin even when idle */
26103e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIOFR, SSIOFR_LRCONT);
26203e786bdSBiju Das if (ssi->audio_clk_1 && ssi->audio_clk_2) {
26303e786bdSBiju Das if (ssi->audio_clk_1 % bclk_rate)
26403e786bdSBiju Das ssi->audio_mck = ssi->audio_clk_2;
26503e786bdSBiju Das else
26603e786bdSBiju Das ssi->audio_mck = ssi->audio_clk_1;
26703e786bdSBiju Das }
26803e786bdSBiju Das
26903e786bdSBiju Das /* Clock setting */
27003e786bdSBiju Das ssicr |= SSICR_MST;
27103e786bdSBiju Das if (ssi->audio_mck == ssi->audio_clk_1)
27203e786bdSBiju Das ssicr |= SSICR_CKS;
27303e786bdSBiju Das if (ssi->bckp_rise)
27403e786bdSBiju Das ssicr |= SSICR_BCKP;
27503e786bdSBiju Das if (ssi->lrckp_fsync_fall)
27603e786bdSBiju Das ssicr |= SSICR_LRCKP;
27703e786bdSBiju Das
27803e786bdSBiju Das /* Determine the clock divider */
27903e786bdSBiju Das clk_ckdv = 0;
28003e786bdSBiju Das div = ssi->audio_mck / bclk_rate;
28103e786bdSBiju Das /* try to find an match */
28203e786bdSBiju Das for (i = 0; i < ARRAY_SIZE(ckdv); i++) {
28303e786bdSBiju Das if (ckdv[i] == div) {
28403e786bdSBiju Das clk_ckdv = i;
28503e786bdSBiju Das break;
28603e786bdSBiju Das }
28703e786bdSBiju Das }
28803e786bdSBiju Das
28903e786bdSBiju Das if (i == ARRAY_SIZE(ckdv)) {
29003e786bdSBiju Das dev_err(ssi->dev, "Rate not divisible by audio clock source\n");
29103e786bdSBiju Das return -EINVAL;
29203e786bdSBiju Das }
29303e786bdSBiju Das
29403e786bdSBiju Das /*
29503e786bdSBiju Das * DWL: Data Word Length = 16 bits
29603e786bdSBiju Das * SWL: System Word Length = 32 bits
29703e786bdSBiju Das */
29803e786bdSBiju Das ssicr |= SSICR_CKDV(clk_ckdv);
29903e786bdSBiju Das ssicr |= SSICR_DWL(1) | SSICR_SWL(3);
30003e786bdSBiju Das rz_ssi_reg_writel(ssi, SSICR, ssicr);
30103e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFCR,
30203e786bdSBiju Das (SSIFCR_AUCKE | SSIFCR_TFRST | SSIFCR_RFRST));
30303e786bdSBiju Das
30403e786bdSBiju Das return 0;
30503e786bdSBiju Das }
30603e786bdSBiju Das
rz_ssi_start(struct rz_ssi_priv * ssi,struct rz_ssi_stream * strm)30703e786bdSBiju Das static int rz_ssi_start(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
30803e786bdSBiju Das {
30903e786bdSBiju Das bool is_play = rz_ssi_stream_is_play(ssi, strm->substream);
31003e786bdSBiju Das u32 ssicr, ssifcr;
31103e786bdSBiju Das
31203e786bdSBiju Das ssicr = rz_ssi_reg_readl(ssi, SSICR);
31303e786bdSBiju Das ssifcr = rz_ssi_reg_readl(ssi, SSIFCR) & ~0xF;
31403e786bdSBiju Das
31503e786bdSBiju Das /* FIFO interrupt thresholds */
31626ac471cSBiju Das if (rz_ssi_is_dma_enabled(ssi))
31726ac471cSBiju Das rz_ssi_reg_writel(ssi, SSISCR, 0);
31826ac471cSBiju Das else
31903e786bdSBiju Das rz_ssi_reg_writel(ssi, SSISCR,
32003e786bdSBiju Das SSISCR_TDES(strm->fifo_sample_size / 2 - 1) |
32103e786bdSBiju Das SSISCR_RDFS(0));
32203e786bdSBiju Das
32303e786bdSBiju Das /* enable IRQ */
32403e786bdSBiju Das if (is_play) {
32503e786bdSBiju Das ssicr |= SSICR_TUIEN | SSICR_TOIEN;
32603e786bdSBiju Das ssifcr |= SSIFCR_TIE | SSIFCR_RFRST;
32703e786bdSBiju Das } else {
32803e786bdSBiju Das ssicr |= SSICR_RUIEN | SSICR_ROIEN;
32903e786bdSBiju Das ssifcr |= SSIFCR_RIE | SSIFCR_TFRST;
33003e786bdSBiju Das }
33103e786bdSBiju Das
33203e786bdSBiju Das rz_ssi_reg_writel(ssi, SSICR, ssicr);
33303e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFCR, ssifcr);
33403e786bdSBiju Das
33503e786bdSBiju Das /* Clear all error flags */
33603e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSISR,
33703e786bdSBiju Das (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ |
33803e786bdSBiju Das SSISR_RUIRQ), 0);
33903e786bdSBiju Das
34003e786bdSBiju Das strm->running = 1;
34103e786bdSBiju Das ssicr |= is_play ? SSICR_TEN : SSICR_REN;
34203e786bdSBiju Das rz_ssi_reg_writel(ssi, SSICR, ssicr);
34303e786bdSBiju Das
34403e786bdSBiju Das return 0;
34503e786bdSBiju Das }
34603e786bdSBiju Das
rz_ssi_stop(struct rz_ssi_priv * ssi,struct rz_ssi_stream * strm)34703e786bdSBiju Das static int rz_ssi_stop(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
34803e786bdSBiju Das {
34903e786bdSBiju Das int timeout;
35003e786bdSBiju Das
35103e786bdSBiju Das strm->running = 0;
35203e786bdSBiju Das
35303e786bdSBiju Das /* Disable TX/RX */
35403e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TEN | SSICR_REN, 0);
35503e786bdSBiju Das
35626ac471cSBiju Das /* Cancel all remaining DMA transactions */
35726ac471cSBiju Das if (rz_ssi_is_dma_enabled(ssi))
35826ac471cSBiju Das dmaengine_terminate_async(strm->dma_ch);
35926ac471cSBiju Das
36003e786bdSBiju Das /* Disable irqs */
36103e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TUIEN | SSICR_TOIEN |
36203e786bdSBiju Das SSICR_RUIEN | SSICR_ROIEN, 0);
36303e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_TIE | SSIFCR_RIE, 0);
36403e786bdSBiju Das
36503e786bdSBiju Das /* Clear all error flags */
36603e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSISR,
36703e786bdSBiju Das (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ |
36803e786bdSBiju Das SSISR_RUIRQ), 0);
36903e786bdSBiju Das
37003e786bdSBiju Das /* Wait for idle */
37103e786bdSBiju Das timeout = 100;
37203e786bdSBiju Das while (--timeout) {
3731b5d1d3aSBiju Das if (rz_ssi_reg_readl(ssi, SSISR) & SSISR_IIRQ)
37403e786bdSBiju Das break;
37503e786bdSBiju Das udelay(1);
37603e786bdSBiju Das }
37703e786bdSBiju Das
37803e786bdSBiju Das if (!timeout)
37903e786bdSBiju Das dev_info(ssi->dev, "timeout waiting for SSI idle\n");
38003e786bdSBiju Das
38103e786bdSBiju Das /* Hold FIFOs in reset */
38203e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, 0,
38303e786bdSBiju Das SSIFCR_TFRST | SSIFCR_RFRST);
38403e786bdSBiju Das
38503e786bdSBiju Das return 0;
38603e786bdSBiju Das }
38703e786bdSBiju Das
rz_ssi_pointer_update(struct rz_ssi_stream * strm,int frames)38803e786bdSBiju Das static void rz_ssi_pointer_update(struct rz_ssi_stream *strm, int frames)
38903e786bdSBiju Das {
39003e786bdSBiju Das struct snd_pcm_substream *substream = strm->substream;
39103e786bdSBiju Das struct snd_pcm_runtime *runtime;
39203e786bdSBiju Das int current_period;
39303e786bdSBiju Das
39403e786bdSBiju Das if (!strm->running || !substream || !substream->runtime)
39503e786bdSBiju Das return;
39603e786bdSBiju Das
39703e786bdSBiju Das runtime = substream->runtime;
39803e786bdSBiju Das strm->buffer_pos += frames;
39903e786bdSBiju Das WARN_ON(strm->buffer_pos > runtime->buffer_size);
40003e786bdSBiju Das
40103e786bdSBiju Das /* ring buffer */
40203e786bdSBiju Das if (strm->buffer_pos == runtime->buffer_size)
40303e786bdSBiju Das strm->buffer_pos = 0;
40403e786bdSBiju Das
40503e786bdSBiju Das current_period = strm->buffer_pos / runtime->period_size;
40603e786bdSBiju Das if (strm->period_counter != current_period) {
40703e786bdSBiju Das snd_pcm_period_elapsed(strm->substream);
40803e786bdSBiju Das strm->period_counter = current_period;
40903e786bdSBiju Das }
41003e786bdSBiju Das }
41103e786bdSBiju Das
rz_ssi_pio_recv(struct rz_ssi_priv * ssi,struct rz_ssi_stream * strm)41203e786bdSBiju Das static int rz_ssi_pio_recv(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
41303e786bdSBiju Das {
41403e786bdSBiju Das struct snd_pcm_substream *substream = strm->substream;
41503e786bdSBiju Das struct snd_pcm_runtime *runtime;
41603e786bdSBiju Das u16 *buf;
41703e786bdSBiju Das int fifo_samples;
41803e786bdSBiju Das int frames_left;
4196570f991SLad Prabhakar int samples;
42003e786bdSBiju Das int i;
42103e786bdSBiju Das
42203e786bdSBiju Das if (!rz_ssi_stream_is_valid(ssi, strm))
42303e786bdSBiju Das return -EINVAL;
42403e786bdSBiju Das
42503e786bdSBiju Das runtime = substream->runtime;
4266570f991SLad Prabhakar
4277276d3f3SLad Prabhakar do {
42803e786bdSBiju Das /* frames left in this period */
4296570f991SLad Prabhakar frames_left = runtime->period_size -
4306570f991SLad Prabhakar (strm->buffer_pos % runtime->period_size);
4316570f991SLad Prabhakar if (!frames_left)
43203e786bdSBiju Das frames_left = runtime->period_size;
43303e786bdSBiju Das
43403e786bdSBiju Das /* Samples in RX FIFO */
43503e786bdSBiju Das fifo_samples = (rz_ssi_reg_readl(ssi, SSIFSR) >>
43603e786bdSBiju Das SSIFSR_RDC_SHIFT) & SSIFSR_RDC_MASK;
43703e786bdSBiju Das
43803e786bdSBiju Das /* Only read full frames at a time */
4396570f991SLad Prabhakar samples = 0;
44003e786bdSBiju Das while (frames_left && (fifo_samples >= runtime->channels)) {
44103e786bdSBiju Das samples += runtime->channels;
44203e786bdSBiju Das fifo_samples -= runtime->channels;
44303e786bdSBiju Das frames_left--;
44403e786bdSBiju Das }
44503e786bdSBiju Das
44603e786bdSBiju Das /* not enough samples yet */
4476570f991SLad Prabhakar if (!samples)
4486570f991SLad Prabhakar break;
44903e786bdSBiju Das
45003e786bdSBiju Das /* calculate new buffer index */
4517276d3f3SLad Prabhakar buf = (u16 *)runtime->dma_area;
45203e786bdSBiju Das buf += strm->buffer_pos * runtime->channels;
45303e786bdSBiju Das
45403e786bdSBiju Das /* Note, only supports 16-bit samples */
45503e786bdSBiju Das for (i = 0; i < samples; i++)
45603e786bdSBiju Das *buf++ = (u16)(rz_ssi_reg_readl(ssi, SSIFRDR) >> 16);
45703e786bdSBiju Das
45803e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0);
45903e786bdSBiju Das rz_ssi_pointer_update(strm, samples / runtime->channels);
4607276d3f3SLad Prabhakar } while (!frames_left && fifo_samples >= runtime->channels);
46103e786bdSBiju Das
46203e786bdSBiju Das return 0;
46303e786bdSBiju Das }
46403e786bdSBiju Das
rz_ssi_pio_send(struct rz_ssi_priv * ssi,struct rz_ssi_stream * strm)46503e786bdSBiju Das static int rz_ssi_pio_send(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
46603e786bdSBiju Das {
46703e786bdSBiju Das struct snd_pcm_substream *substream = strm->substream;
46803e786bdSBiju Das struct snd_pcm_runtime *runtime = substream->runtime;
46903e786bdSBiju Das int sample_space;
47003e786bdSBiju Das int samples = 0;
47103e786bdSBiju Das int frames_left;
47203e786bdSBiju Das int i;
47303e786bdSBiju Das u32 ssifsr;
47403e786bdSBiju Das u16 *buf;
47503e786bdSBiju Das
47603e786bdSBiju Das if (!rz_ssi_stream_is_valid(ssi, strm))
47703e786bdSBiju Das return -EINVAL;
47803e786bdSBiju Das
47903e786bdSBiju Das /* frames left in this period */
48003e786bdSBiju Das frames_left = runtime->period_size - (strm->buffer_pos %
48103e786bdSBiju Das runtime->period_size);
48203e786bdSBiju Das if (frames_left == 0)
48303e786bdSBiju Das frames_left = runtime->period_size;
48403e786bdSBiju Das
48503e786bdSBiju Das sample_space = strm->fifo_sample_size;
48603e786bdSBiju Das ssifsr = rz_ssi_reg_readl(ssi, SSIFSR);
48703e786bdSBiju Das sample_space -= (ssifsr >> SSIFSR_TDC_SHIFT) & SSIFSR_TDC_MASK;
48803e786bdSBiju Das
48903e786bdSBiju Das /* Only add full frames at a time */
49003e786bdSBiju Das while (frames_left && (sample_space >= runtime->channels)) {
49103e786bdSBiju Das samples += runtime->channels;
49203e786bdSBiju Das sample_space -= runtime->channels;
49303e786bdSBiju Das frames_left--;
49403e786bdSBiju Das }
49503e786bdSBiju Das
49603e786bdSBiju Das /* no space to send anything right now */
49703e786bdSBiju Das if (samples == 0)
49803e786bdSBiju Das return 0;
49903e786bdSBiju Das
50003e786bdSBiju Das /* calculate new buffer index */
50103e786bdSBiju Das buf = (u16 *)(runtime->dma_area);
50203e786bdSBiju Das buf += strm->buffer_pos * runtime->channels;
50303e786bdSBiju Das
50403e786bdSBiju Das /* Note, only supports 16-bit samples */
50503e786bdSBiju Das for (i = 0; i < samples; i++)
50603e786bdSBiju Das rz_ssi_reg_writel(ssi, SSIFTDR, ((u32)(*buf++) << 16));
50703e786bdSBiju Das
50803e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_TDE, 0);
50903e786bdSBiju Das rz_ssi_pointer_update(strm, samples / runtime->channels);
51003e786bdSBiju Das
51103e786bdSBiju Das return 0;
51203e786bdSBiju Das }
51303e786bdSBiju Das
rz_ssi_interrupt(int irq,void * data)51403e786bdSBiju Das static irqreturn_t rz_ssi_interrupt(int irq, void *data)
51503e786bdSBiju Das {
51603e786bdSBiju Das struct rz_ssi_stream *strm = NULL;
51703e786bdSBiju Das struct rz_ssi_priv *ssi = data;
51803e786bdSBiju Das u32 ssisr = rz_ssi_reg_readl(ssi, SSISR);
51903e786bdSBiju Das
52003e786bdSBiju Das if (ssi->playback.substream)
52103e786bdSBiju Das strm = &ssi->playback;
52203e786bdSBiju Das else if (ssi->capture.substream)
52303e786bdSBiju Das strm = &ssi->capture;
52403e786bdSBiju Das else
52503e786bdSBiju Das return IRQ_HANDLED; /* Left over TX/RX interrupt */
52603e786bdSBiju Das
52703e786bdSBiju Das if (irq == ssi->irq_int) { /* error or idle */
52803e786bdSBiju Das if (ssisr & SSISR_TUIRQ)
52903e786bdSBiju Das strm->uerr_num++;
53003e786bdSBiju Das if (ssisr & SSISR_TOIRQ)
53103e786bdSBiju Das strm->oerr_num++;
53203e786bdSBiju Das if (ssisr & SSISR_RUIRQ)
53303e786bdSBiju Das strm->uerr_num++;
53403e786bdSBiju Das if (ssisr & SSISR_ROIRQ)
53503e786bdSBiju Das strm->oerr_num++;
53603e786bdSBiju Das
53703e786bdSBiju Das if (ssisr & (SSISR_TUIRQ | SSISR_TOIRQ | SSISR_RUIRQ |
53803e786bdSBiju Das SSISR_ROIRQ)) {
53903e786bdSBiju Das /* Error handling */
54003e786bdSBiju Das /* You must reset (stop/restart) after each interrupt */
54103e786bdSBiju Das rz_ssi_stop(ssi, strm);
54203e786bdSBiju Das
54303e786bdSBiju Das /* Clear all flags */
54403e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSISR, SSISR_TOIRQ |
54503e786bdSBiju Das SSISR_TUIRQ | SSISR_ROIRQ |
54603e786bdSBiju Das SSISR_RUIRQ, 0);
54703e786bdSBiju Das
54803e786bdSBiju Das /* Add/remove more data */
54903e786bdSBiju Das strm->transfer(ssi, strm);
55003e786bdSBiju Das
55103e786bdSBiju Das /* Resume */
55203e786bdSBiju Das rz_ssi_start(ssi, strm);
55303e786bdSBiju Das }
55403e786bdSBiju Das }
55503e786bdSBiju Das
55603e786bdSBiju Das if (!strm->running)
55703e786bdSBiju Das return IRQ_HANDLED;
55803e786bdSBiju Das
55903e786bdSBiju Das /* tx data empty */
56003e786bdSBiju Das if (irq == ssi->irq_tx)
56103e786bdSBiju Das strm->transfer(ssi, &ssi->playback);
56203e786bdSBiju Das
56303e786bdSBiju Das /* rx data full */
56403e786bdSBiju Das if (irq == ssi->irq_rx) {
56503e786bdSBiju Das strm->transfer(ssi, &ssi->capture);
56603e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0);
56703e786bdSBiju Das }
56803e786bdSBiju Das
56938c042b5SLad Prabhakar if (irq == ssi->irq_rt) {
57038c042b5SLad Prabhakar struct snd_pcm_substream *substream = strm->substream;
57138c042b5SLad Prabhakar
57238c042b5SLad Prabhakar if (rz_ssi_stream_is_play(ssi, substream)) {
57338c042b5SLad Prabhakar strm->transfer(ssi, &ssi->playback);
57438c042b5SLad Prabhakar } else {
57538c042b5SLad Prabhakar strm->transfer(ssi, &ssi->capture);
57638c042b5SLad Prabhakar rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0);
57738c042b5SLad Prabhakar }
57838c042b5SLad Prabhakar }
57938c042b5SLad Prabhakar
58003e786bdSBiju Das return IRQ_HANDLED;
58103e786bdSBiju Das }
58203e786bdSBiju Das
rz_ssi_dma_slave_config(struct rz_ssi_priv * ssi,struct dma_chan * dma_ch,bool is_play)58326ac471cSBiju Das static int rz_ssi_dma_slave_config(struct rz_ssi_priv *ssi,
58426ac471cSBiju Das struct dma_chan *dma_ch, bool is_play)
58526ac471cSBiju Das {
58626ac471cSBiju Das struct dma_slave_config cfg;
58726ac471cSBiju Das
58826ac471cSBiju Das memset(&cfg, 0, sizeof(cfg));
58926ac471cSBiju Das
59026ac471cSBiju Das cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
59126ac471cSBiju Das cfg.dst_addr = ssi->phys + SSIFTDR;
59226ac471cSBiju Das cfg.src_addr = ssi->phys + SSIFRDR;
59326ac471cSBiju Das cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
59426ac471cSBiju Das cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
59526ac471cSBiju Das
59626ac471cSBiju Das return dmaengine_slave_config(dma_ch, &cfg);
59726ac471cSBiju Das }
59826ac471cSBiju Das
rz_ssi_dma_transfer(struct rz_ssi_priv * ssi,struct rz_ssi_stream * strm)59926ac471cSBiju Das static int rz_ssi_dma_transfer(struct rz_ssi_priv *ssi,
60026ac471cSBiju Das struct rz_ssi_stream *strm)
60126ac471cSBiju Das {
60226ac471cSBiju Das struct snd_pcm_substream *substream = strm->substream;
60326ac471cSBiju Das struct dma_async_tx_descriptor *desc;
60426ac471cSBiju Das struct snd_pcm_runtime *runtime;
60526ac471cSBiju Das enum dma_transfer_direction dir;
60626ac471cSBiju Das u32 dma_paddr, dma_size;
60726ac471cSBiju Das int amount;
60826ac471cSBiju Das
60926ac471cSBiju Das if (!rz_ssi_stream_is_valid(ssi, strm))
61026ac471cSBiju Das return -EINVAL;
61126ac471cSBiju Das
61226ac471cSBiju Das runtime = substream->runtime;
613a267fdd0STakashi Iwai if (runtime->state == SNDRV_PCM_STATE_DRAINING)
61426ac471cSBiju Das /*
61526ac471cSBiju Das * Stream is ending, so do not queue up any more DMA
61626ac471cSBiju Das * transfers otherwise we play partial sound clips
61726ac471cSBiju Das * because we can't shut off the DMA quick enough.
61826ac471cSBiju Das */
61926ac471cSBiju Das return 0;
62026ac471cSBiju Das
62126ac471cSBiju Das dir = rz_ssi_stream_is_play(ssi, substream) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
62226ac471cSBiju Das
62326ac471cSBiju Das /* Always transfer 1 period */
62426ac471cSBiju Das amount = runtime->period_size;
62526ac471cSBiju Das
62626ac471cSBiju Das /* DMA physical address and size */
62726ac471cSBiju Das dma_paddr = runtime->dma_addr + frames_to_bytes(runtime,
62826ac471cSBiju Das strm->dma_buffer_pos);
62926ac471cSBiju Das dma_size = frames_to_bytes(runtime, amount);
63026ac471cSBiju Das desc = dmaengine_prep_slave_single(strm->dma_ch, dma_paddr, dma_size,
63126ac471cSBiju Das dir,
63226ac471cSBiju Das DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
63326ac471cSBiju Das if (!desc) {
63426ac471cSBiju Das dev_err(ssi->dev, "dmaengine_prep_slave_single() fail\n");
63526ac471cSBiju Das return -ENOMEM;
63626ac471cSBiju Das }
63726ac471cSBiju Das
63826ac471cSBiju Das desc->callback = rz_ssi_dma_complete;
63926ac471cSBiju Das desc->callback_param = strm;
64026ac471cSBiju Das
64126ac471cSBiju Das if (dmaengine_submit(desc) < 0) {
64226ac471cSBiju Das dev_err(ssi->dev, "dmaengine_submit() fail\n");
64326ac471cSBiju Das return -EIO;
64426ac471cSBiju Das }
64526ac471cSBiju Das
64626ac471cSBiju Das /* Update DMA pointer */
64726ac471cSBiju Das strm->dma_buffer_pos += amount;
64826ac471cSBiju Das if (strm->dma_buffer_pos >= runtime->buffer_size)
64926ac471cSBiju Das strm->dma_buffer_pos = 0;
65026ac471cSBiju Das
65126ac471cSBiju Das /* Start DMA */
65226ac471cSBiju Das dma_async_issue_pending(strm->dma_ch);
65326ac471cSBiju Das
65426ac471cSBiju Das return 0;
65526ac471cSBiju Das }
65626ac471cSBiju Das
rz_ssi_dma_complete(void * data)65726ac471cSBiju Das static void rz_ssi_dma_complete(void *data)
65826ac471cSBiju Das {
65926ac471cSBiju Das struct rz_ssi_stream *strm = (struct rz_ssi_stream *)data;
66026ac471cSBiju Das
66126ac471cSBiju Das if (!strm->running || !strm->substream || !strm->substream->runtime)
66226ac471cSBiju Das return;
66326ac471cSBiju Das
66426ac471cSBiju Das /* Note that next DMA transaction has probably already started */
66526ac471cSBiju Das rz_ssi_pointer_update(strm, strm->substream->runtime->period_size);
66626ac471cSBiju Das
66726ac471cSBiju Das /* Queue up another DMA transaction */
66826ac471cSBiju Das rz_ssi_dma_transfer(strm->priv, strm);
66926ac471cSBiju Das }
67026ac471cSBiju Das
rz_ssi_release_dma_channels(struct rz_ssi_priv * ssi)67126ac471cSBiju Das static void rz_ssi_release_dma_channels(struct rz_ssi_priv *ssi)
67226ac471cSBiju Das {
67326ac471cSBiju Das if (ssi->playback.dma_ch) {
67426ac471cSBiju Das dma_release_channel(ssi->playback.dma_ch);
67526ac471cSBiju Das ssi->playback.dma_ch = NULL;
67626ac471cSBiju Das if (ssi->dma_rt)
67726ac471cSBiju Das ssi->dma_rt = false;
67826ac471cSBiju Das }
67926ac471cSBiju Das
68026ac471cSBiju Das if (ssi->capture.dma_ch) {
68126ac471cSBiju Das dma_release_channel(ssi->capture.dma_ch);
68226ac471cSBiju Das ssi->capture.dma_ch = NULL;
68326ac471cSBiju Das }
68426ac471cSBiju Das }
68526ac471cSBiju Das
rz_ssi_dma_request(struct rz_ssi_priv * ssi,struct device * dev)68626ac471cSBiju Das static int rz_ssi_dma_request(struct rz_ssi_priv *ssi, struct device *dev)
68726ac471cSBiju Das {
68826ac471cSBiju Das ssi->playback.dma_ch = dma_request_chan(dev, "tx");
6894b14f179SBiju Das if (IS_ERR(ssi->playback.dma_ch))
6904b14f179SBiju Das ssi->playback.dma_ch = NULL;
6914b14f179SBiju Das
69226ac471cSBiju Das ssi->capture.dma_ch = dma_request_chan(dev, "rx");
6934b14f179SBiju Das if (IS_ERR(ssi->capture.dma_ch))
6944b14f179SBiju Das ssi->capture.dma_ch = NULL;
6954b14f179SBiju Das
69626ac471cSBiju Das if (!ssi->playback.dma_ch && !ssi->capture.dma_ch) {
69726ac471cSBiju Das ssi->playback.dma_ch = dma_request_chan(dev, "rt");
6984b14f179SBiju Das if (IS_ERR(ssi->playback.dma_ch)) {
6994b14f179SBiju Das ssi->playback.dma_ch = NULL;
70026ac471cSBiju Das goto no_dma;
7014b14f179SBiju Das }
70226ac471cSBiju Das
70326ac471cSBiju Das ssi->dma_rt = true;
70426ac471cSBiju Das }
70526ac471cSBiju Das
7064b14f179SBiju Das if (!rz_ssi_is_dma_enabled(ssi))
7074b14f179SBiju Das goto no_dma;
7084b14f179SBiju Das
70926ac471cSBiju Das if (ssi->playback.dma_ch &&
71026ac471cSBiju Das (rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch, true) < 0))
71126ac471cSBiju Das goto no_dma;
71226ac471cSBiju Das
71326ac471cSBiju Das if (ssi->capture.dma_ch &&
71426ac471cSBiju Das (rz_ssi_dma_slave_config(ssi, ssi->capture.dma_ch, false) < 0))
71526ac471cSBiju Das goto no_dma;
71626ac471cSBiju Das
71726ac471cSBiju Das return 0;
71826ac471cSBiju Das
71926ac471cSBiju Das no_dma:
72026ac471cSBiju Das rz_ssi_release_dma_channels(ssi);
72126ac471cSBiju Das
72226ac471cSBiju Das return -ENODEV;
72326ac471cSBiju Das }
72426ac471cSBiju Das
rz_ssi_dai_trigger(struct snd_pcm_substream * substream,int cmd,struct snd_soc_dai * dai)72503e786bdSBiju Das static int rz_ssi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
72603e786bdSBiju Das struct snd_soc_dai *dai)
72703e786bdSBiju Das {
72803e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
72903e786bdSBiju Das struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream);
73026ac471cSBiju Das int ret = 0, i, num_transfer = 1;
73103e786bdSBiju Das
73203e786bdSBiju Das switch (cmd) {
73303e786bdSBiju Das case SNDRV_PCM_TRIGGER_START:
73403e786bdSBiju Das /* Soft Reset */
73503e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, 0, SSIFCR_SSIRST);
73603e786bdSBiju Das rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_SSIRST, 0);
73703e786bdSBiju Das udelay(5);
73803e786bdSBiju Das
7394f78f3c9SLad Prabhakar rz_ssi_stream_init(strm, substream);
74003e786bdSBiju Das
74126ac471cSBiju Das if (ssi->dma_rt) {
74226ac471cSBiju Das bool is_playback;
74326ac471cSBiju Das
74426ac471cSBiju Das is_playback = rz_ssi_stream_is_play(ssi, substream);
74526ac471cSBiju Das ret = rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch,
74626ac471cSBiju Das is_playback);
74726ac471cSBiju Das /* Fallback to pio */
74826ac471cSBiju Das if (ret < 0) {
74926ac471cSBiju Das ssi->playback.transfer = rz_ssi_pio_send;
75026ac471cSBiju Das ssi->capture.transfer = rz_ssi_pio_recv;
75126ac471cSBiju Das rz_ssi_release_dma_channels(ssi);
75226ac471cSBiju Das }
75326ac471cSBiju Das }
75426ac471cSBiju Das
75526ac471cSBiju Das /* For DMA, queue up multiple DMA descriptors */
75626ac471cSBiju Das if (rz_ssi_is_dma_enabled(ssi))
75726ac471cSBiju Das num_transfer = 4;
75826ac471cSBiju Das
75926ac471cSBiju Das for (i = 0; i < num_transfer; i++) {
76003e786bdSBiju Das ret = strm->transfer(ssi, strm);
76103e786bdSBiju Das if (ret)
76203e786bdSBiju Das goto done;
76326ac471cSBiju Das }
76403e786bdSBiju Das
76503e786bdSBiju Das ret = rz_ssi_start(ssi, strm);
76603e786bdSBiju Das break;
76703e786bdSBiju Das case SNDRV_PCM_TRIGGER_STOP:
76803e786bdSBiju Das rz_ssi_stop(ssi, strm);
76903e786bdSBiju Das rz_ssi_stream_quit(ssi, strm);
77003e786bdSBiju Das break;
77103e786bdSBiju Das }
77203e786bdSBiju Das
77303e786bdSBiju Das done:
77403e786bdSBiju Das return ret;
77503e786bdSBiju Das }
77603e786bdSBiju Das
rz_ssi_dai_set_fmt(struct snd_soc_dai * dai,unsigned int fmt)77703e786bdSBiju Das static int rz_ssi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
77803e786bdSBiju Das {
77903e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
78003e786bdSBiju Das
78103e786bdSBiju Das switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
7822d4dd776SCharles Keepax case SND_SOC_DAIFMT_BP_FP:
78303e786bdSBiju Das break;
78403e786bdSBiju Das default:
78503e786bdSBiju Das dev_err(ssi->dev, "Codec should be clk and frame consumer\n");
78603e786bdSBiju Das return -EINVAL;
78703e786bdSBiju Das }
78803e786bdSBiju Das
78903e786bdSBiju Das /*
79003e786bdSBiju Das * set clock polarity
79103e786bdSBiju Das *
79203e786bdSBiju Das * "normal" BCLK = Signal is available at rising edge of BCLK
79303e786bdSBiju Das * "normal" FSYNC = (I2S) Left ch starts with falling FSYNC edge
79403e786bdSBiju Das */
79503e786bdSBiju Das switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
79603e786bdSBiju Das case SND_SOC_DAIFMT_NB_NF:
79703e786bdSBiju Das ssi->bckp_rise = false;
79803e786bdSBiju Das ssi->lrckp_fsync_fall = false;
79903e786bdSBiju Das break;
80003e786bdSBiju Das case SND_SOC_DAIFMT_NB_IF:
80103e786bdSBiju Das ssi->bckp_rise = false;
80203e786bdSBiju Das ssi->lrckp_fsync_fall = true;
80303e786bdSBiju Das break;
80403e786bdSBiju Das case SND_SOC_DAIFMT_IB_NF:
80503e786bdSBiju Das ssi->bckp_rise = true;
80603e786bdSBiju Das ssi->lrckp_fsync_fall = false;
80703e786bdSBiju Das break;
80803e786bdSBiju Das case SND_SOC_DAIFMT_IB_IF:
80903e786bdSBiju Das ssi->bckp_rise = true;
81003e786bdSBiju Das ssi->lrckp_fsync_fall = true;
81103e786bdSBiju Das break;
81203e786bdSBiju Das default:
81303e786bdSBiju Das return -EINVAL;
81403e786bdSBiju Das }
81503e786bdSBiju Das
81603e786bdSBiju Das /* only i2s support */
81703e786bdSBiju Das switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
81803e786bdSBiju Das case SND_SOC_DAIFMT_I2S:
81903e786bdSBiju Das break;
82003e786bdSBiju Das default:
82103e786bdSBiju Das dev_err(ssi->dev, "Only I2S mode is supported.\n");
82203e786bdSBiju Das return -EINVAL;
82303e786bdSBiju Das }
82403e786bdSBiju Das
82503e786bdSBiju Das return 0;
82603e786bdSBiju Das }
82703e786bdSBiju Das
rz_ssi_dai_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)82803e786bdSBiju Das static int rz_ssi_dai_hw_params(struct snd_pcm_substream *substream,
82903e786bdSBiju Das struct snd_pcm_hw_params *params,
83003e786bdSBiju Das struct snd_soc_dai *dai)
83103e786bdSBiju Das {
83203e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
83303e786bdSBiju Das unsigned int sample_bits = hw_param_interval(params,
83403e786bdSBiju Das SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
83503e786bdSBiju Das unsigned int channels = params_channels(params);
83603e786bdSBiju Das
83703e786bdSBiju Das if (sample_bits != 16) {
83803e786bdSBiju Das dev_err(ssi->dev, "Unsupported sample width: %d\n",
83903e786bdSBiju Das sample_bits);
84003e786bdSBiju Das return -EINVAL;
84103e786bdSBiju Das }
84203e786bdSBiju Das
84303e786bdSBiju Das if (channels != 2) {
84403e786bdSBiju Das dev_err(ssi->dev, "Number of channels not matched: %d\n",
84503e786bdSBiju Das channels);
84603e786bdSBiju Das return -EINVAL;
84703e786bdSBiju Das }
84803e786bdSBiju Das
84903e786bdSBiju Das return rz_ssi_clk_setup(ssi, params_rate(params),
85003e786bdSBiju Das params_channels(params));
85103e786bdSBiju Das }
85203e786bdSBiju Das
85303e786bdSBiju Das static const struct snd_soc_dai_ops rz_ssi_dai_ops = {
85403e786bdSBiju Das .trigger = rz_ssi_dai_trigger,
855adced680SCharles Keepax .set_fmt = rz_ssi_dai_set_fmt,
85603e786bdSBiju Das .hw_params = rz_ssi_dai_hw_params,
85703e786bdSBiju Das };
85803e786bdSBiju Das
85903e786bdSBiju Das static const struct snd_pcm_hardware rz_ssi_pcm_hardware = {
86003e786bdSBiju Das .info = SNDRV_PCM_INFO_INTERLEAVED |
86103e786bdSBiju Das SNDRV_PCM_INFO_MMAP |
86203e786bdSBiju Das SNDRV_PCM_INFO_MMAP_VALID,
86303e786bdSBiju Das .buffer_bytes_max = PREALLOC_BUFFER,
86403e786bdSBiju Das .period_bytes_min = 32,
86503e786bdSBiju Das .period_bytes_max = 8192,
86603e786bdSBiju Das .channels_min = SSI_CHAN_MIN,
86703e786bdSBiju Das .channels_max = SSI_CHAN_MAX,
86803e786bdSBiju Das .periods_min = 1,
86903e786bdSBiju Das .periods_max = 32,
87003e786bdSBiju Das .fifo_size = 32 * 2,
87103e786bdSBiju Das };
87203e786bdSBiju Das
rz_ssi_pcm_open(struct snd_soc_component * component,struct snd_pcm_substream * substream)87303e786bdSBiju Das static int rz_ssi_pcm_open(struct snd_soc_component *component,
87403e786bdSBiju Das struct snd_pcm_substream *substream)
87503e786bdSBiju Das {
87603e786bdSBiju Das snd_soc_set_runtime_hwparams(substream, &rz_ssi_pcm_hardware);
87703e786bdSBiju Das
87803e786bdSBiju Das return snd_pcm_hw_constraint_integer(substream->runtime,
87903e786bdSBiju Das SNDRV_PCM_HW_PARAM_PERIODS);
88003e786bdSBiju Das }
88103e786bdSBiju Das
rz_ssi_pcm_pointer(struct snd_soc_component * component,struct snd_pcm_substream * substream)88203e786bdSBiju Das static snd_pcm_uframes_t rz_ssi_pcm_pointer(struct snd_soc_component *component,
88303e786bdSBiju Das struct snd_pcm_substream *substream)
88403e786bdSBiju Das {
88503e786bdSBiju Das struct snd_soc_dai *dai = rz_ssi_get_dai(substream);
88603e786bdSBiju Das struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
88703e786bdSBiju Das struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream);
88803e786bdSBiju Das
88903e786bdSBiju Das return strm->buffer_pos;
89003e786bdSBiju Das }
89103e786bdSBiju Das
rz_ssi_pcm_new(struct snd_soc_component * component,struct snd_soc_pcm_runtime * rtd)89203e786bdSBiju Das static int rz_ssi_pcm_new(struct snd_soc_component *component,
89303e786bdSBiju Das struct snd_soc_pcm_runtime *rtd)
89403e786bdSBiju Das {
89503e786bdSBiju Das snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
89603e786bdSBiju Das rtd->card->snd_card->dev,
89703e786bdSBiju Das PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
89803e786bdSBiju Das return 0;
89903e786bdSBiju Das }
90003e786bdSBiju Das
90103e786bdSBiju Das static struct snd_soc_dai_driver rz_ssi_soc_dai[] = {
90203e786bdSBiju Das {
90303e786bdSBiju Das .name = "rz-ssi-dai",
90403e786bdSBiju Das .playback = {
90503e786bdSBiju Das .rates = SSI_RATES,
90603e786bdSBiju Das .formats = SSI_FMTS,
90703e786bdSBiju Das .channels_min = SSI_CHAN_MIN,
90803e786bdSBiju Das .channels_max = SSI_CHAN_MAX,
90903e786bdSBiju Das },
91003e786bdSBiju Das .capture = {
91103e786bdSBiju Das .rates = SSI_RATES,
91203e786bdSBiju Das .formats = SSI_FMTS,
91303e786bdSBiju Das .channels_min = SSI_CHAN_MIN,
91403e786bdSBiju Das .channels_max = SSI_CHAN_MAX,
91503e786bdSBiju Das },
91603e786bdSBiju Das .ops = &rz_ssi_dai_ops,
91703e786bdSBiju Das },
91803e786bdSBiju Das };
91903e786bdSBiju Das
92003e786bdSBiju Das static const struct snd_soc_component_driver rz_ssi_soc_component = {
92103e786bdSBiju Das .name = "rz-ssi",
92203e786bdSBiju Das .open = rz_ssi_pcm_open,
92303e786bdSBiju Das .pointer = rz_ssi_pcm_pointer,
92403e786bdSBiju Das .pcm_construct = rz_ssi_pcm_new,
925f712ff57SCharles Keepax .legacy_dai_naming = 1,
92603e786bdSBiju Das };
92703e786bdSBiju Das
rz_ssi_probe(struct platform_device * pdev)92803e786bdSBiju Das static int rz_ssi_probe(struct platform_device *pdev)
92903e786bdSBiju Das {
93003e786bdSBiju Das struct rz_ssi_priv *ssi;
93103e786bdSBiju Das struct clk *audio_clk;
93226ac471cSBiju Das struct resource *res;
93303e786bdSBiju Das int ret;
93403e786bdSBiju Das
93503e786bdSBiju Das ssi = devm_kzalloc(&pdev->dev, sizeof(*ssi), GFP_KERNEL);
93603e786bdSBiju Das if (!ssi)
93703e786bdSBiju Das return -ENOMEM;
93803e786bdSBiju Das
93903e786bdSBiju Das ssi->pdev = pdev;
94003e786bdSBiju Das ssi->dev = &pdev->dev;
94126ac471cSBiju Das ssi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
94203e786bdSBiju Das if (IS_ERR(ssi->base))
94303e786bdSBiju Das return PTR_ERR(ssi->base);
94403e786bdSBiju Das
94526ac471cSBiju Das ssi->phys = res->start;
94603e786bdSBiju Das ssi->clk = devm_clk_get(&pdev->dev, "ssi");
94703e786bdSBiju Das if (IS_ERR(ssi->clk))
94803e786bdSBiju Das return PTR_ERR(ssi->clk);
94903e786bdSBiju Das
95003e786bdSBiju Das ssi->sfr_clk = devm_clk_get(&pdev->dev, "ssi_sfr");
95103e786bdSBiju Das if (IS_ERR(ssi->sfr_clk))
95203e786bdSBiju Das return PTR_ERR(ssi->sfr_clk);
95303e786bdSBiju Das
95403e786bdSBiju Das audio_clk = devm_clk_get(&pdev->dev, "audio_clk1");
95503e786bdSBiju Das if (IS_ERR(audio_clk))
95603e786bdSBiju Das return dev_err_probe(&pdev->dev, PTR_ERR(audio_clk),
95703e786bdSBiju Das "no audio clk1");
95803e786bdSBiju Das
95903e786bdSBiju Das ssi->audio_clk_1 = clk_get_rate(audio_clk);
96003e786bdSBiju Das audio_clk = devm_clk_get(&pdev->dev, "audio_clk2");
96103e786bdSBiju Das if (IS_ERR(audio_clk))
96203e786bdSBiju Das return dev_err_probe(&pdev->dev, PTR_ERR(audio_clk),
96303e786bdSBiju Das "no audio clk2");
96403e786bdSBiju Das
96503e786bdSBiju Das ssi->audio_clk_2 = clk_get_rate(audio_clk);
96603e786bdSBiju Das if (!(ssi->audio_clk_1 || ssi->audio_clk_2))
96703e786bdSBiju Das return dev_err_probe(&pdev->dev, -EINVAL,
96803e786bdSBiju Das "no audio clk1 or audio clk2");
96903e786bdSBiju Das
97003e786bdSBiju Das ssi->audio_mck = ssi->audio_clk_1 ? ssi->audio_clk_1 : ssi->audio_clk_2;
97103e786bdSBiju Das
97226ac471cSBiju Das /* Detect DMA support */
97326ac471cSBiju Das ret = rz_ssi_dma_request(ssi, &pdev->dev);
97426ac471cSBiju Das if (ret < 0) {
97526ac471cSBiju Das dev_warn(&pdev->dev, "DMA not available, using PIO\n");
97603e786bdSBiju Das ssi->playback.transfer = rz_ssi_pio_send;
97703e786bdSBiju Das ssi->capture.transfer = rz_ssi_pio_recv;
97826ac471cSBiju Das } else {
97926ac471cSBiju Das dev_info(&pdev->dev, "DMA enabled");
98026ac471cSBiju Das ssi->playback.transfer = rz_ssi_dma_transfer;
98126ac471cSBiju Das ssi->capture.transfer = rz_ssi_dma_transfer;
98226ac471cSBiju Das }
98326ac471cSBiju Das
98403e786bdSBiju Das ssi->playback.priv = ssi;
98503e786bdSBiju Das ssi->capture.priv = ssi;
98603e786bdSBiju Das
9870788785cSLad Prabhakar spin_lock_init(&ssi->lock);
9880788785cSLad Prabhakar dev_set_drvdata(&pdev->dev, ssi);
9890788785cSLad Prabhakar
99003e786bdSBiju Das /* Error Interrupt */
99103e786bdSBiju Das ssi->irq_int = platform_get_irq_byname(pdev, "int_req");
992767e6f26SLad Prabhakar if (ssi->irq_int < 0) {
993767e6f26SLad Prabhakar rz_ssi_release_dma_channels(ssi);
99491686a39SLad Prabhakar return ssi->irq_int;
995767e6f26SLad Prabhakar }
99603e786bdSBiju Das
99703e786bdSBiju Das ret = devm_request_irq(&pdev->dev, ssi->irq_int, &rz_ssi_interrupt,
99803e786bdSBiju Das 0, dev_name(&pdev->dev), ssi);
999767e6f26SLad Prabhakar if (ret < 0) {
1000767e6f26SLad Prabhakar rz_ssi_release_dma_channels(ssi);
100103e786bdSBiju Das return dev_err_probe(&pdev->dev, ret,
100203e786bdSBiju Das "irq request error (int_req)\n");
1003767e6f26SLad Prabhakar }
100403e786bdSBiju Das
100526ac471cSBiju Das if (!rz_ssi_is_dma_enabled(ssi)) {
100603e786bdSBiju Das /* Tx and Rx interrupts (pio only) */
100703e786bdSBiju Das ssi->irq_tx = platform_get_irq_byname(pdev, "dma_tx");
100838c042b5SLad Prabhakar ssi->irq_rx = platform_get_irq_byname(pdev, "dma_rx");
100938c042b5SLad Prabhakar if (ssi->irq_tx == -ENXIO && ssi->irq_rx == -ENXIO) {
101038c042b5SLad Prabhakar ssi->irq_rt = platform_get_irq_byname(pdev, "dma_rt");
101138c042b5SLad Prabhakar if (ssi->irq_rt < 0)
101238c042b5SLad Prabhakar return ssi->irq_rt;
101338c042b5SLad Prabhakar
101438c042b5SLad Prabhakar ret = devm_request_irq(&pdev->dev, ssi->irq_rt,
101538c042b5SLad Prabhakar &rz_ssi_interrupt, 0,
101638c042b5SLad Prabhakar dev_name(&pdev->dev), ssi);
101738c042b5SLad Prabhakar if (ret < 0)
101838c042b5SLad Prabhakar return dev_err_probe(&pdev->dev, ret,
1019*ccf09255SLad Prabhakar "irq request error (dma_rt)\n");
102038c042b5SLad Prabhakar } else {
102103e786bdSBiju Das if (ssi->irq_tx < 0)
102291686a39SLad Prabhakar return ssi->irq_tx;
102303e786bdSBiju Das
102438c042b5SLad Prabhakar if (ssi->irq_rx < 0)
102538c042b5SLad Prabhakar return ssi->irq_rx;
102638c042b5SLad Prabhakar
102726ac471cSBiju Das ret = devm_request_irq(&pdev->dev, ssi->irq_tx,
102826ac471cSBiju Das &rz_ssi_interrupt, 0,
102903e786bdSBiju Das dev_name(&pdev->dev), ssi);
103003e786bdSBiju Das if (ret < 0)
103103e786bdSBiju Das return dev_err_probe(&pdev->dev, ret,
103203e786bdSBiju Das "irq request error (dma_tx)\n");
103303e786bdSBiju Das
103426ac471cSBiju Das ret = devm_request_irq(&pdev->dev, ssi->irq_rx,
103526ac471cSBiju Das &rz_ssi_interrupt, 0,
103603e786bdSBiju Das dev_name(&pdev->dev), ssi);
103703e786bdSBiju Das if (ret < 0)
103803e786bdSBiju Das return dev_err_probe(&pdev->dev, ret,
103903e786bdSBiju Das "irq request error (dma_rx)\n");
104026ac471cSBiju Das }
104138c042b5SLad Prabhakar }
104203e786bdSBiju Das
104303e786bdSBiju Das ssi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
1044767e6f26SLad Prabhakar if (IS_ERR(ssi->rstc)) {
1045c75ed9f5SBiju Das ret = PTR_ERR(ssi->rstc);
1046c75ed9f5SBiju Das goto err_reset;
1047767e6f26SLad Prabhakar }
104803e786bdSBiju Das
104903e786bdSBiju Das reset_control_deassert(ssi->rstc);
105003e786bdSBiju Das pm_runtime_enable(&pdev->dev);
1051f04b4fb4SHeiner Kallweit ret = pm_runtime_resume_and_get(&pdev->dev);
1052f04b4fb4SHeiner Kallweit if (ret < 0) {
1053c75ed9f5SBiju Das dev_err(&pdev->dev, "pm_runtime_resume_and_get failed\n");
1054c75ed9f5SBiju Das goto err_pm;
1055f04b4fb4SHeiner Kallweit }
105603e786bdSBiju Das
105703e786bdSBiju Das ret = devm_snd_soc_register_component(&pdev->dev, &rz_ssi_soc_component,
105803e786bdSBiju Das rz_ssi_soc_dai,
105903e786bdSBiju Das ARRAY_SIZE(rz_ssi_soc_dai));
106003e786bdSBiju Das if (ret < 0) {
1061c75ed9f5SBiju Das dev_err(&pdev->dev, "failed to register snd component\n");
1062c75ed9f5SBiju Das goto err_snd_soc;
1063c75ed9f5SBiju Das }
106426ac471cSBiju Das
1065c75ed9f5SBiju Das return 0;
1066c75ed9f5SBiju Das
1067c75ed9f5SBiju Das err_snd_soc:
106803e786bdSBiju Das pm_runtime_put(ssi->dev);
1069c75ed9f5SBiju Das err_pm:
107003e786bdSBiju Das pm_runtime_disable(ssi->dev);
107103e786bdSBiju Das reset_control_assert(ssi->rstc);
1072c75ed9f5SBiju Das err_reset:
1073c75ed9f5SBiju Das rz_ssi_release_dma_channels(ssi);
107403e786bdSBiju Das
107503e786bdSBiju Das return ret;
107603e786bdSBiju Das }
107703e786bdSBiju Das
rz_ssi_remove(struct platform_device * pdev)1078b89438c7SUwe Kleine-König static void rz_ssi_remove(struct platform_device *pdev)
107903e786bdSBiju Das {
108003e786bdSBiju Das struct rz_ssi_priv *ssi = dev_get_drvdata(&pdev->dev);
108103e786bdSBiju Das
108226ac471cSBiju Das rz_ssi_release_dma_channels(ssi);
108326ac471cSBiju Das
108403e786bdSBiju Das pm_runtime_put(ssi->dev);
108503e786bdSBiju Das pm_runtime_disable(ssi->dev);
108603e786bdSBiju Das reset_control_assert(ssi->rstc);
108703e786bdSBiju Das }
108803e786bdSBiju Das
108903e786bdSBiju Das static const struct of_device_id rz_ssi_of_match[] = {
109003e786bdSBiju Das { .compatible = "renesas,rz-ssi", },
109103e786bdSBiju Das {/* Sentinel */},
109203e786bdSBiju Das };
109303e786bdSBiju Das MODULE_DEVICE_TABLE(of, rz_ssi_of_match);
109403e786bdSBiju Das
109503e786bdSBiju Das static struct platform_driver rz_ssi_driver = {
109603e786bdSBiju Das .driver = {
109703e786bdSBiju Das .name = "rz-ssi-pcm-audio",
109803e786bdSBiju Das .of_match_table = rz_ssi_of_match,
109903e786bdSBiju Das },
110003e786bdSBiju Das .probe = rz_ssi_probe,
1101b89438c7SUwe Kleine-König .remove_new = rz_ssi_remove,
110203e786bdSBiju Das };
110303e786bdSBiju Das
110403e786bdSBiju Das module_platform_driver(rz_ssi_driver);
110503e786bdSBiju Das
110603e786bdSBiju Das MODULE_LICENSE("GPL v2");
110703e786bdSBiju Das MODULE_DESCRIPTION("Renesas RZ/G2L ASoC Serial Sound Interface Driver");
110803e786bdSBiju Das MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
1109