xref: /openbmc/linux/sound/soc/sh/rz-ssi.c (revision e42c903e8bf400728c4ae1f922169b4d28b72efa)
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 #define SSICR_MST		BIT(14)
3203e786bdSBiju Das #define SSICR_CKDV(x)		(((x) & 0xf) << 4)
3303e786bdSBiju Das 
3403e786bdSBiju Das #define SSICR_CKS		BIT(30)
3503e786bdSBiju Das #define SSICR_TUIEN		BIT(29)
3603e786bdSBiju Das #define SSICR_TOIEN		BIT(28)
3703e786bdSBiju Das #define SSICR_RUIEN		BIT(27)
3803e786bdSBiju Das #define SSICR_ROIEN		BIT(26)
3903e786bdSBiju Das #define SSICR_MST		BIT(14)
4003e786bdSBiju Das #define SSICR_BCKP		BIT(13)
4103e786bdSBiju Das #define SSICR_LRCKP		BIT(12)
4203e786bdSBiju Das #define SSICR_CKDV(x)		(((x) & 0xf) << 4)
4303e786bdSBiju Das #define SSICR_TEN		BIT(1)
4403e786bdSBiju Das #define SSICR_REN		BIT(0)
4503e786bdSBiju Das 
4603e786bdSBiju Das #define SSISR_TUIRQ		BIT(29)
4703e786bdSBiju Das #define SSISR_TOIRQ		BIT(28)
4803e786bdSBiju Das #define SSISR_RUIRQ		BIT(27)
4903e786bdSBiju Das #define SSISR_ROIRQ		BIT(26)
5003e786bdSBiju Das #define SSISR_IIRQ		BIT(25)
5103e786bdSBiju Das 
5203e786bdSBiju Das #define SSIFCR_AUCKE		BIT(31)
5303e786bdSBiju Das #define SSIFCR_SSIRST		BIT(16)
5403e786bdSBiju Das #define SSIFCR_TIE		BIT(3)
5503e786bdSBiju Das #define SSIFCR_RIE		BIT(2)
5603e786bdSBiju Das #define SSIFCR_TFRST		BIT(1)
5703e786bdSBiju Das #define SSIFCR_RFRST		BIT(0)
5803e786bdSBiju Das 
5903e786bdSBiju Das #define SSIFSR_TDC_MASK		0x3f
6003e786bdSBiju Das #define SSIFSR_TDC_SHIFT	24
6103e786bdSBiju Das #define SSIFSR_RDC_MASK		0x3f
6203e786bdSBiju Das #define SSIFSR_RDC_SHIFT	8
6303e786bdSBiju Das 
6403e786bdSBiju Das #define SSIFSR_TDC(x)		(((x) & 0x1f) << 24)
6503e786bdSBiju Das #define SSIFSR_TDE		BIT(16)
6603e786bdSBiju Das #define SSIFSR_RDC(x)		(((x) & 0x1f) << 8)
6703e786bdSBiju Das #define SSIFSR_RDF		BIT(0)
6803e786bdSBiju Das 
6903e786bdSBiju Das #define SSIOFR_LRCONT		BIT(8)
7003e786bdSBiju Das 
7103e786bdSBiju Das #define SSISCR_TDES(x)		(((x) & 0x1f) << 8)
7203e786bdSBiju Das #define SSISCR_RDFS(x)		(((x) & 0x1f) << 0)
7303e786bdSBiju Das 
7403e786bdSBiju Das /* Pre allocated buffers sizes */
7503e786bdSBiju Das #define PREALLOC_BUFFER		(SZ_32K)
7603e786bdSBiju Das #define PREALLOC_BUFFER_MAX	(SZ_32K)
7703e786bdSBiju Das 
7803e786bdSBiju Das #define SSI_RATES		SNDRV_PCM_RATE_8000_48000 /* 8k-44.1kHz */
7903e786bdSBiju Das #define SSI_FMTS		SNDRV_PCM_FMTBIT_S16_LE
8003e786bdSBiju Das #define SSI_CHAN_MIN		2
8103e786bdSBiju Das #define SSI_CHAN_MAX		2
8203e786bdSBiju Das #define SSI_FIFO_DEPTH		32
8303e786bdSBiju Das 
8403e786bdSBiju Das struct rz_ssi_priv;
8503e786bdSBiju Das 
8603e786bdSBiju Das struct rz_ssi_stream {
8703e786bdSBiju Das 	struct rz_ssi_priv *priv;
8803e786bdSBiju Das 	struct snd_pcm_substream *substream;
8903e786bdSBiju Das 	int fifo_sample_size;	/* sample capacity of SSI FIFO */
9026ac471cSBiju Das 	int dma_buffer_pos;	/* The address for the next DMA descriptor */
9103e786bdSBiju Das 	int period_counter;	/* for keeping track of periods transferred */
9203e786bdSBiju Das 	int sample_width;
9303e786bdSBiju Das 	int buffer_pos;		/* current frame position in the buffer */
9403e786bdSBiju Das 	int running;		/* 0=stopped, 1=running */
9503e786bdSBiju Das 
9603e786bdSBiju Das 	int uerr_num;
9703e786bdSBiju Das 	int oerr_num;
9803e786bdSBiju Das 
9926ac471cSBiju Das 	struct dma_chan *dma_ch;
10026ac471cSBiju Das 
10103e786bdSBiju Das 	int (*transfer)(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm);
10203e786bdSBiju Das };
10303e786bdSBiju Das 
10403e786bdSBiju Das struct rz_ssi_priv {
10503e786bdSBiju Das 	void __iomem *base;
10603e786bdSBiju Das 	struct platform_device *pdev;
10703e786bdSBiju Das 	struct reset_control *rstc;
10803e786bdSBiju Das 	struct device *dev;
10903e786bdSBiju Das 	struct clk *sfr_clk;
11003e786bdSBiju Das 	struct clk *clk;
11103e786bdSBiju Das 
11226ac471cSBiju Das 	phys_addr_t phys;
11303e786bdSBiju Das 	int irq_int;
11403e786bdSBiju Das 	int irq_tx;
11503e786bdSBiju Das 	int irq_rx;
11603e786bdSBiju Das 
11703e786bdSBiju Das 	spinlock_t lock;
11803e786bdSBiju Das 
11903e786bdSBiju Das 	/*
12003e786bdSBiju Das 	 * The SSI supports full-duplex transmission and reception.
12103e786bdSBiju Das 	 * However, if an error occurs, channel reset (both transmission
12203e786bdSBiju Das 	 * and reception reset) is required.
12303e786bdSBiju Das 	 * So it is better to use as half-duplex (playing and recording
12403e786bdSBiju Das 	 * should be done on separate channels).
12503e786bdSBiju Das 	 */
12603e786bdSBiju Das 	struct rz_ssi_stream playback;
12703e786bdSBiju Das 	struct rz_ssi_stream capture;
12803e786bdSBiju Das 
12903e786bdSBiju Das 	/* clock */
13003e786bdSBiju Das 	unsigned long audio_mck;
13103e786bdSBiju Das 	unsigned long audio_clk_1;
13203e786bdSBiju Das 	unsigned long audio_clk_2;
13303e786bdSBiju Das 
13403e786bdSBiju Das 	bool lrckp_fsync_fall;	/* LR clock polarity (SSICR.LRCKP) */
13503e786bdSBiju Das 	bool bckp_rise;	/* Bit clock polarity (SSICR.BCKP) */
13626ac471cSBiju Das 	bool dma_rt;
13703e786bdSBiju Das };
13803e786bdSBiju Das 
13926ac471cSBiju Das static void rz_ssi_dma_complete(void *data);
14026ac471cSBiju Das 
14103e786bdSBiju Das static void rz_ssi_reg_writel(struct rz_ssi_priv *priv, uint reg, u32 data)
14203e786bdSBiju Das {
14303e786bdSBiju Das 	writel(data, (priv->base + reg));
14403e786bdSBiju Das }
14503e786bdSBiju Das 
14603e786bdSBiju Das static u32 rz_ssi_reg_readl(struct rz_ssi_priv *priv, uint reg)
14703e786bdSBiju Das {
14803e786bdSBiju Das 	return readl(priv->base + reg);
14903e786bdSBiju Das }
15003e786bdSBiju Das 
151d40dfb86SBiju Das static void rz_ssi_reg_mask_setl(struct rz_ssi_priv *priv, uint reg,
15203e786bdSBiju Das 				 u32 bclr, u32 bset)
15303e786bdSBiju Das {
15403e786bdSBiju Das 	u32 val;
15503e786bdSBiju Das 
15603e786bdSBiju Das 	val = readl(priv->base + reg);
15703e786bdSBiju Das 	val = (val & ~bclr) | bset;
15803e786bdSBiju Das 	writel(val, (priv->base + reg));
15903e786bdSBiju Das }
16003e786bdSBiju Das 
16103e786bdSBiju Das static inline struct snd_soc_dai *
16203e786bdSBiju Das rz_ssi_get_dai(struct snd_pcm_substream *substream)
16303e786bdSBiju Das {
16403e786bdSBiju Das 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
16503e786bdSBiju Das 
16603e786bdSBiju Das 	return asoc_rtd_to_cpu(rtd, 0);
16703e786bdSBiju Das }
16803e786bdSBiju Das 
16903e786bdSBiju Das static inline bool rz_ssi_stream_is_play(struct rz_ssi_priv *ssi,
17003e786bdSBiju Das 					 struct snd_pcm_substream *substream)
17103e786bdSBiju Das {
17203e786bdSBiju Das 	return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
17303e786bdSBiju Das }
17403e786bdSBiju Das 
17503e786bdSBiju Das static inline struct rz_ssi_stream *
17603e786bdSBiju Das rz_ssi_stream_get(struct rz_ssi_priv *ssi, struct snd_pcm_substream *substream)
17703e786bdSBiju Das {
17803e786bdSBiju Das 	struct rz_ssi_stream *stream = &ssi->playback;
17903e786bdSBiju Das 
18003e786bdSBiju Das 	if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
18103e786bdSBiju Das 		stream = &ssi->capture;
18203e786bdSBiju Das 
18303e786bdSBiju Das 	return stream;
18403e786bdSBiju Das }
18503e786bdSBiju Das 
18626ac471cSBiju Das static inline bool rz_ssi_is_dma_enabled(struct rz_ssi_priv *ssi)
18726ac471cSBiju Das {
1884b14f179SBiju Das 	return (ssi->playback.dma_ch && (ssi->dma_rt || ssi->capture.dma_ch));
18926ac471cSBiju Das }
19026ac471cSBiju Das 
191*e42c903eSLad Prabhakar static bool rz_ssi_stream_is_valid(struct rz_ssi_priv *ssi,
19203e786bdSBiju Das 				   struct rz_ssi_stream *strm)
19303e786bdSBiju Das {
19403e786bdSBiju Das 	unsigned long flags;
195*e42c903eSLad Prabhakar 	bool ret;
19603e786bdSBiju Das 
19703e786bdSBiju Das 	spin_lock_irqsave(&ssi->lock, flags);
198*e42c903eSLad Prabhakar 	ret = strm->substream && strm->substream->runtime;
19903e786bdSBiju Das 	spin_unlock_irqrestore(&ssi->lock, flags);
20003e786bdSBiju Das 
20103e786bdSBiju Das 	return ret;
20203e786bdSBiju Das }
20303e786bdSBiju Das 
2044f78f3c9SLad Prabhakar static void rz_ssi_stream_init(struct rz_ssi_stream *strm,
20503e786bdSBiju Das 			       struct snd_pcm_substream *substream)
20603e786bdSBiju Das {
20703e786bdSBiju Das 	struct snd_pcm_runtime *runtime = substream->runtime;
20803e786bdSBiju Das 
20903e786bdSBiju Das 	strm->substream = substream;
21003e786bdSBiju Das 	strm->sample_width = samples_to_bytes(runtime, 1);
21126ac471cSBiju Das 	strm->dma_buffer_pos = 0;
21203e786bdSBiju Das 	strm->period_counter = 0;
21303e786bdSBiju Das 	strm->buffer_pos = 0;
21403e786bdSBiju Das 
21503e786bdSBiju Das 	strm->oerr_num = 0;
21603e786bdSBiju Das 	strm->uerr_num = 0;
21703e786bdSBiju Das 	strm->running = 0;
21803e786bdSBiju Das 
21903e786bdSBiju Das 	/* fifo init */
22003e786bdSBiju Das 	strm->fifo_sample_size = SSI_FIFO_DEPTH;
22103e786bdSBiju Das }
22203e786bdSBiju Das 
22303e786bdSBiju Das static void rz_ssi_stream_quit(struct rz_ssi_priv *ssi,
22403e786bdSBiju Das 			       struct rz_ssi_stream *strm)
22503e786bdSBiju Das {
22603e786bdSBiju Das 	struct snd_soc_dai *dai = rz_ssi_get_dai(strm->substream);
22703e786bdSBiju Das 	unsigned long flags;
22803e786bdSBiju Das 
22903e786bdSBiju Das 	spin_lock_irqsave(&ssi->lock, flags);
23003e786bdSBiju Das 	strm->substream = NULL;
23103e786bdSBiju Das 	spin_unlock_irqrestore(&ssi->lock, flags);
23203e786bdSBiju Das 
23303e786bdSBiju Das 	if (strm->oerr_num > 0)
23403e786bdSBiju Das 		dev_info(dai->dev, "overrun = %d\n", strm->oerr_num);
23503e786bdSBiju Das 
23603e786bdSBiju Das 	if (strm->uerr_num > 0)
23703e786bdSBiju Das 		dev_info(dai->dev, "underrun = %d\n", strm->uerr_num);
23803e786bdSBiju Das }
23903e786bdSBiju Das 
24003e786bdSBiju Das static int rz_ssi_clk_setup(struct rz_ssi_priv *ssi, unsigned int rate,
24103e786bdSBiju Das 			    unsigned int channels)
24203e786bdSBiju Das {
24303e786bdSBiju Das 	static s8 ckdv[16] = { 1,  2,  4,  8, 16, 32, 64, 128,
24403e786bdSBiju Das 			       6, 12, 24, 48, 96, -1, -1, -1 };
24503e786bdSBiju Das 	unsigned int channel_bits = 32;	/* System Word Length */
24603e786bdSBiju Das 	unsigned long bclk_rate = rate * channels * channel_bits;
24703e786bdSBiju Das 	unsigned int div;
24803e786bdSBiju Das 	unsigned int i;
24903e786bdSBiju Das 	u32 ssicr = 0;
25003e786bdSBiju Das 	u32 clk_ckdv;
25103e786bdSBiju Das 
25203e786bdSBiju Das 	/* Clear AUCKE so we can set MST */
25303e786bdSBiju Das 	rz_ssi_reg_writel(ssi, SSIFCR, 0);
25403e786bdSBiju Das 
25503e786bdSBiju Das 	/* Continue to output LRCK pin even when idle */
25603e786bdSBiju Das 	rz_ssi_reg_writel(ssi, SSIOFR, SSIOFR_LRCONT);
25703e786bdSBiju Das 	if (ssi->audio_clk_1 && ssi->audio_clk_2) {
25803e786bdSBiju Das 		if (ssi->audio_clk_1 % bclk_rate)
25903e786bdSBiju Das 			ssi->audio_mck = ssi->audio_clk_2;
26003e786bdSBiju Das 		else
26103e786bdSBiju Das 			ssi->audio_mck = ssi->audio_clk_1;
26203e786bdSBiju Das 	}
26303e786bdSBiju Das 
26403e786bdSBiju Das 	/* Clock setting */
26503e786bdSBiju Das 	ssicr |= SSICR_MST;
26603e786bdSBiju Das 	if (ssi->audio_mck == ssi->audio_clk_1)
26703e786bdSBiju Das 		ssicr |= SSICR_CKS;
26803e786bdSBiju Das 	if (ssi->bckp_rise)
26903e786bdSBiju Das 		ssicr |= SSICR_BCKP;
27003e786bdSBiju Das 	if (ssi->lrckp_fsync_fall)
27103e786bdSBiju Das 		ssicr |= SSICR_LRCKP;
27203e786bdSBiju Das 
27303e786bdSBiju Das 	/* Determine the clock divider */
27403e786bdSBiju Das 	clk_ckdv = 0;
27503e786bdSBiju Das 	div = ssi->audio_mck / bclk_rate;
27603e786bdSBiju Das 	/* try to find an match */
27703e786bdSBiju Das 	for (i = 0; i < ARRAY_SIZE(ckdv); i++) {
27803e786bdSBiju Das 		if (ckdv[i] == div) {
27903e786bdSBiju Das 			clk_ckdv = i;
28003e786bdSBiju Das 			break;
28103e786bdSBiju Das 		}
28203e786bdSBiju Das 	}
28303e786bdSBiju Das 
28403e786bdSBiju Das 	if (i == ARRAY_SIZE(ckdv)) {
28503e786bdSBiju Das 		dev_err(ssi->dev, "Rate not divisible by audio clock source\n");
28603e786bdSBiju Das 		return -EINVAL;
28703e786bdSBiju Das 	}
28803e786bdSBiju Das 
28903e786bdSBiju Das 	/*
29003e786bdSBiju Das 	 * DWL: Data Word Length = 16 bits
29103e786bdSBiju Das 	 * SWL: System Word Length = 32 bits
29203e786bdSBiju Das 	 */
29303e786bdSBiju Das 	ssicr |= SSICR_CKDV(clk_ckdv);
29403e786bdSBiju Das 	ssicr |= SSICR_DWL(1) | SSICR_SWL(3);
29503e786bdSBiju Das 	rz_ssi_reg_writel(ssi, SSICR, ssicr);
29603e786bdSBiju Das 	rz_ssi_reg_writel(ssi, SSIFCR,
29703e786bdSBiju Das 			  (SSIFCR_AUCKE | SSIFCR_TFRST | SSIFCR_RFRST));
29803e786bdSBiju Das 
29903e786bdSBiju Das 	return 0;
30003e786bdSBiju Das }
30103e786bdSBiju Das 
30203e786bdSBiju Das static int rz_ssi_start(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
30303e786bdSBiju Das {
30403e786bdSBiju Das 	bool is_play = rz_ssi_stream_is_play(ssi, strm->substream);
30503e786bdSBiju Das 	u32 ssicr, ssifcr;
30603e786bdSBiju Das 
30703e786bdSBiju Das 	ssicr = rz_ssi_reg_readl(ssi, SSICR);
30803e786bdSBiju Das 	ssifcr = rz_ssi_reg_readl(ssi, SSIFCR) & ~0xF;
30903e786bdSBiju Das 
31003e786bdSBiju Das 	/* FIFO interrupt thresholds */
31126ac471cSBiju Das 	if (rz_ssi_is_dma_enabled(ssi))
31226ac471cSBiju Das 		rz_ssi_reg_writel(ssi, SSISCR, 0);
31326ac471cSBiju Das 	else
31403e786bdSBiju Das 		rz_ssi_reg_writel(ssi, SSISCR,
31503e786bdSBiju Das 				  SSISCR_TDES(strm->fifo_sample_size / 2 - 1) |
31603e786bdSBiju Das 				  SSISCR_RDFS(0));
31703e786bdSBiju Das 
31803e786bdSBiju Das 	/* enable IRQ */
31903e786bdSBiju Das 	if (is_play) {
32003e786bdSBiju Das 		ssicr |= SSICR_TUIEN | SSICR_TOIEN;
32103e786bdSBiju Das 		ssifcr |= SSIFCR_TIE | SSIFCR_RFRST;
32203e786bdSBiju Das 	} else {
32303e786bdSBiju Das 		ssicr |= SSICR_RUIEN | SSICR_ROIEN;
32403e786bdSBiju Das 		ssifcr |= SSIFCR_RIE | SSIFCR_TFRST;
32503e786bdSBiju Das 	}
32603e786bdSBiju Das 
32703e786bdSBiju Das 	rz_ssi_reg_writel(ssi, SSICR, ssicr);
32803e786bdSBiju Das 	rz_ssi_reg_writel(ssi, SSIFCR, ssifcr);
32903e786bdSBiju Das 
33003e786bdSBiju Das 	/* Clear all error flags */
33103e786bdSBiju Das 	rz_ssi_reg_mask_setl(ssi, SSISR,
33203e786bdSBiju Das 			     (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ |
33303e786bdSBiju Das 			      SSISR_RUIRQ), 0);
33403e786bdSBiju Das 
33503e786bdSBiju Das 	strm->running = 1;
33603e786bdSBiju Das 	ssicr |= is_play ? SSICR_TEN : SSICR_REN;
33703e786bdSBiju Das 	rz_ssi_reg_writel(ssi, SSICR, ssicr);
33803e786bdSBiju Das 
33903e786bdSBiju Das 	return 0;
34003e786bdSBiju Das }
34103e786bdSBiju Das 
34203e786bdSBiju Das static int rz_ssi_stop(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
34303e786bdSBiju Das {
34403e786bdSBiju Das 	int timeout;
34503e786bdSBiju Das 
34603e786bdSBiju Das 	strm->running = 0;
34703e786bdSBiju Das 
34803e786bdSBiju Das 	/* Disable TX/RX */
34903e786bdSBiju Das 	rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TEN | SSICR_REN, 0);
35003e786bdSBiju Das 
35126ac471cSBiju Das 	/* Cancel all remaining DMA transactions */
35226ac471cSBiju Das 	if (rz_ssi_is_dma_enabled(ssi))
35326ac471cSBiju Das 		dmaengine_terminate_async(strm->dma_ch);
35426ac471cSBiju Das 
35503e786bdSBiju Das 	/* Disable irqs */
35603e786bdSBiju Das 	rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TUIEN | SSICR_TOIEN |
35703e786bdSBiju Das 			     SSICR_RUIEN | SSICR_ROIEN, 0);
35803e786bdSBiju Das 	rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_TIE | SSIFCR_RIE, 0);
35903e786bdSBiju Das 
36003e786bdSBiju Das 	/* Clear all error flags */
36103e786bdSBiju Das 	rz_ssi_reg_mask_setl(ssi, SSISR,
36203e786bdSBiju Das 			     (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ |
36303e786bdSBiju Das 			      SSISR_RUIRQ), 0);
36403e786bdSBiju Das 
36503e786bdSBiju Das 	/* Wait for idle */
36603e786bdSBiju Das 	timeout = 100;
36703e786bdSBiju Das 	while (--timeout) {
3681b5d1d3aSBiju Das 		if (rz_ssi_reg_readl(ssi, SSISR) & SSISR_IIRQ)
36903e786bdSBiju Das 			break;
37003e786bdSBiju Das 		udelay(1);
37103e786bdSBiju Das 	}
37203e786bdSBiju Das 
37303e786bdSBiju Das 	if (!timeout)
37403e786bdSBiju Das 		dev_info(ssi->dev, "timeout waiting for SSI idle\n");
37503e786bdSBiju Das 
37603e786bdSBiju Das 	/* Hold FIFOs in reset */
37703e786bdSBiju Das 	rz_ssi_reg_mask_setl(ssi, SSIFCR, 0,
37803e786bdSBiju Das 			     SSIFCR_TFRST | SSIFCR_RFRST);
37903e786bdSBiju Das 
38003e786bdSBiju Das 	return 0;
38103e786bdSBiju Das }
38203e786bdSBiju Das 
38303e786bdSBiju Das static void rz_ssi_pointer_update(struct rz_ssi_stream *strm, int frames)
38403e786bdSBiju Das {
38503e786bdSBiju Das 	struct snd_pcm_substream *substream = strm->substream;
38603e786bdSBiju Das 	struct snd_pcm_runtime *runtime;
38703e786bdSBiju Das 	int current_period;
38803e786bdSBiju Das 
38903e786bdSBiju Das 	if (!strm->running || !substream || !substream->runtime)
39003e786bdSBiju Das 		return;
39103e786bdSBiju Das 
39203e786bdSBiju Das 	runtime = substream->runtime;
39303e786bdSBiju Das 	strm->buffer_pos += frames;
39403e786bdSBiju Das 	WARN_ON(strm->buffer_pos > runtime->buffer_size);
39503e786bdSBiju Das 
39603e786bdSBiju Das 	/* ring buffer */
39703e786bdSBiju Das 	if (strm->buffer_pos == runtime->buffer_size)
39803e786bdSBiju Das 		strm->buffer_pos = 0;
39903e786bdSBiju Das 
40003e786bdSBiju Das 	current_period = strm->buffer_pos / runtime->period_size;
40103e786bdSBiju Das 	if (strm->period_counter != current_period) {
40203e786bdSBiju Das 		snd_pcm_period_elapsed(strm->substream);
40303e786bdSBiju Das 		strm->period_counter = current_period;
40403e786bdSBiju Das 	}
40503e786bdSBiju Das }
40603e786bdSBiju Das 
40703e786bdSBiju Das static int rz_ssi_pio_recv(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
40803e786bdSBiju Das {
40903e786bdSBiju Das 	struct snd_pcm_substream *substream = strm->substream;
41003e786bdSBiju Das 	struct snd_pcm_runtime *runtime;
4116570f991SLad Prabhakar 	bool done = false;
41203e786bdSBiju Das 	u16 *buf;
41303e786bdSBiju Das 	int fifo_samples;
41403e786bdSBiju Das 	int frames_left;
4156570f991SLad Prabhakar 	int samples;
41603e786bdSBiju Das 	int i;
41703e786bdSBiju Das 
41803e786bdSBiju Das 	if (!rz_ssi_stream_is_valid(ssi, strm))
41903e786bdSBiju Das 		return -EINVAL;
42003e786bdSBiju Das 
42103e786bdSBiju Das 	runtime = substream->runtime;
4226570f991SLad Prabhakar 
4236570f991SLad Prabhakar 	while (!done) {
42403e786bdSBiju Das 		/* frames left in this period */
4256570f991SLad Prabhakar 		frames_left = runtime->period_size -
4266570f991SLad Prabhakar 			      (strm->buffer_pos % runtime->period_size);
4276570f991SLad Prabhakar 		if (!frames_left)
42803e786bdSBiju Das 			frames_left = runtime->period_size;
42903e786bdSBiju Das 
43003e786bdSBiju Das 		/* Samples in RX FIFO */
43103e786bdSBiju Das 		fifo_samples = (rz_ssi_reg_readl(ssi, SSIFSR) >>
43203e786bdSBiju Das 				SSIFSR_RDC_SHIFT) & SSIFSR_RDC_MASK;
43303e786bdSBiju Das 
43403e786bdSBiju Das 		/* Only read full frames at a time */
4356570f991SLad Prabhakar 		samples = 0;
43603e786bdSBiju Das 		while (frames_left && (fifo_samples >= runtime->channels)) {
43703e786bdSBiju Das 			samples += runtime->channels;
43803e786bdSBiju Das 			fifo_samples -= runtime->channels;
43903e786bdSBiju Das 			frames_left--;
44003e786bdSBiju Das 		}
44103e786bdSBiju Das 
44203e786bdSBiju Das 		/* not enough samples yet */
4436570f991SLad Prabhakar 		if (!samples)
4446570f991SLad Prabhakar 			break;
44503e786bdSBiju Das 
44603e786bdSBiju Das 		/* calculate new buffer index */
44703e786bdSBiju Das 		buf = (u16 *)(runtime->dma_area);
44803e786bdSBiju Das 		buf += strm->buffer_pos * runtime->channels;
44903e786bdSBiju Das 
45003e786bdSBiju Das 		/* Note, only supports 16-bit samples */
45103e786bdSBiju Das 		for (i = 0; i < samples; i++)
45203e786bdSBiju Das 			*buf++ = (u16)(rz_ssi_reg_readl(ssi, SSIFRDR) >> 16);
45303e786bdSBiju Das 
45403e786bdSBiju Das 		rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0);
45503e786bdSBiju Das 		rz_ssi_pointer_update(strm, samples / runtime->channels);
45603e786bdSBiju Das 
4576570f991SLad Prabhakar 		/* check if there are no more samples in the RX FIFO */
4586570f991SLad Prabhakar 		if (!(!frames_left && fifo_samples >= runtime->channels))
4596570f991SLad Prabhakar 			done = true;
4606570f991SLad Prabhakar 	}
46103e786bdSBiju Das 
46203e786bdSBiju Das 	return 0;
46303e786bdSBiju Das }
46403e786bdSBiju Das 
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 
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 
56903e786bdSBiju Das 	return IRQ_HANDLED;
57003e786bdSBiju Das }
57103e786bdSBiju Das 
57226ac471cSBiju Das static int rz_ssi_dma_slave_config(struct rz_ssi_priv *ssi,
57326ac471cSBiju Das 				   struct dma_chan *dma_ch, bool is_play)
57426ac471cSBiju Das {
57526ac471cSBiju Das 	struct dma_slave_config cfg;
57626ac471cSBiju Das 
57726ac471cSBiju Das 	memset(&cfg, 0, sizeof(cfg));
57826ac471cSBiju Das 
57926ac471cSBiju Das 	cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
58026ac471cSBiju Das 	cfg.dst_addr = ssi->phys + SSIFTDR;
58126ac471cSBiju Das 	cfg.src_addr = ssi->phys + SSIFRDR;
58226ac471cSBiju Das 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
58326ac471cSBiju Das 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
58426ac471cSBiju Das 
58526ac471cSBiju Das 	return dmaengine_slave_config(dma_ch, &cfg);
58626ac471cSBiju Das }
58726ac471cSBiju Das 
58826ac471cSBiju Das static int rz_ssi_dma_transfer(struct rz_ssi_priv *ssi,
58926ac471cSBiju Das 			       struct rz_ssi_stream *strm)
59026ac471cSBiju Das {
59126ac471cSBiju Das 	struct snd_pcm_substream *substream = strm->substream;
59226ac471cSBiju Das 	struct dma_async_tx_descriptor *desc;
59326ac471cSBiju Das 	struct snd_pcm_runtime *runtime;
59426ac471cSBiju Das 	enum dma_transfer_direction dir;
59526ac471cSBiju Das 	u32 dma_paddr, dma_size;
59626ac471cSBiju Das 	int amount;
59726ac471cSBiju Das 
59826ac471cSBiju Das 	if (!rz_ssi_stream_is_valid(ssi, strm))
59926ac471cSBiju Das 		return -EINVAL;
60026ac471cSBiju Das 
60126ac471cSBiju Das 	runtime = substream->runtime;
60226ac471cSBiju Das 	if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
60326ac471cSBiju Das 		/*
60426ac471cSBiju Das 		 * Stream is ending, so do not queue up any more DMA
60526ac471cSBiju Das 		 * transfers otherwise we play partial sound clips
60626ac471cSBiju Das 		 * because we can't shut off the DMA quick enough.
60726ac471cSBiju Das 		 */
60826ac471cSBiju Das 		return 0;
60926ac471cSBiju Das 
61026ac471cSBiju Das 	dir = rz_ssi_stream_is_play(ssi, substream) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
61126ac471cSBiju Das 
61226ac471cSBiju Das 	/* Always transfer 1 period */
61326ac471cSBiju Das 	amount = runtime->period_size;
61426ac471cSBiju Das 
61526ac471cSBiju Das 	/* DMA physical address and size */
61626ac471cSBiju Das 	dma_paddr = runtime->dma_addr + frames_to_bytes(runtime,
61726ac471cSBiju Das 							strm->dma_buffer_pos);
61826ac471cSBiju Das 	dma_size = frames_to_bytes(runtime, amount);
61926ac471cSBiju Das 	desc = dmaengine_prep_slave_single(strm->dma_ch, dma_paddr, dma_size,
62026ac471cSBiju Das 					   dir,
62126ac471cSBiju Das 					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
62226ac471cSBiju Das 	if (!desc) {
62326ac471cSBiju Das 		dev_err(ssi->dev, "dmaengine_prep_slave_single() fail\n");
62426ac471cSBiju Das 		return -ENOMEM;
62526ac471cSBiju Das 	}
62626ac471cSBiju Das 
62726ac471cSBiju Das 	desc->callback = rz_ssi_dma_complete;
62826ac471cSBiju Das 	desc->callback_param = strm;
62926ac471cSBiju Das 
63026ac471cSBiju Das 	if (dmaengine_submit(desc) < 0) {
63126ac471cSBiju Das 		dev_err(ssi->dev, "dmaengine_submit() fail\n");
63226ac471cSBiju Das 		return -EIO;
63326ac471cSBiju Das 	}
63426ac471cSBiju Das 
63526ac471cSBiju Das 	/* Update DMA pointer */
63626ac471cSBiju Das 	strm->dma_buffer_pos += amount;
63726ac471cSBiju Das 	if (strm->dma_buffer_pos >= runtime->buffer_size)
63826ac471cSBiju Das 		strm->dma_buffer_pos = 0;
63926ac471cSBiju Das 
64026ac471cSBiju Das 	/* Start DMA */
64126ac471cSBiju Das 	dma_async_issue_pending(strm->dma_ch);
64226ac471cSBiju Das 
64326ac471cSBiju Das 	return 0;
64426ac471cSBiju Das }
64526ac471cSBiju Das 
64626ac471cSBiju Das static void rz_ssi_dma_complete(void *data)
64726ac471cSBiju Das {
64826ac471cSBiju Das 	struct rz_ssi_stream *strm = (struct rz_ssi_stream *)data;
64926ac471cSBiju Das 
65026ac471cSBiju Das 	if (!strm->running || !strm->substream || !strm->substream->runtime)
65126ac471cSBiju Das 		return;
65226ac471cSBiju Das 
65326ac471cSBiju Das 	/* Note that next DMA transaction has probably already started */
65426ac471cSBiju Das 	rz_ssi_pointer_update(strm, strm->substream->runtime->period_size);
65526ac471cSBiju Das 
65626ac471cSBiju Das 	/* Queue up another DMA transaction */
65726ac471cSBiju Das 	rz_ssi_dma_transfer(strm->priv, strm);
65826ac471cSBiju Das }
65926ac471cSBiju Das 
66026ac471cSBiju Das static void rz_ssi_release_dma_channels(struct rz_ssi_priv *ssi)
66126ac471cSBiju Das {
66226ac471cSBiju Das 	if (ssi->playback.dma_ch) {
66326ac471cSBiju Das 		dma_release_channel(ssi->playback.dma_ch);
66426ac471cSBiju Das 		ssi->playback.dma_ch = NULL;
66526ac471cSBiju Das 		if (ssi->dma_rt)
66626ac471cSBiju Das 			ssi->dma_rt = false;
66726ac471cSBiju Das 	}
66826ac471cSBiju Das 
66926ac471cSBiju Das 	if (ssi->capture.dma_ch) {
67026ac471cSBiju Das 		dma_release_channel(ssi->capture.dma_ch);
67126ac471cSBiju Das 		ssi->capture.dma_ch = NULL;
67226ac471cSBiju Das 	}
67326ac471cSBiju Das }
67426ac471cSBiju Das 
67526ac471cSBiju Das static int rz_ssi_dma_request(struct rz_ssi_priv *ssi, struct device *dev)
67626ac471cSBiju Das {
67726ac471cSBiju Das 	ssi->playback.dma_ch = dma_request_chan(dev, "tx");
6784b14f179SBiju Das 	if (IS_ERR(ssi->playback.dma_ch))
6794b14f179SBiju Das 		ssi->playback.dma_ch = NULL;
6804b14f179SBiju Das 
68126ac471cSBiju Das 	ssi->capture.dma_ch = dma_request_chan(dev, "rx");
6824b14f179SBiju Das 	if (IS_ERR(ssi->capture.dma_ch))
6834b14f179SBiju Das 		ssi->capture.dma_ch = NULL;
6844b14f179SBiju Das 
68526ac471cSBiju Das 	if (!ssi->playback.dma_ch && !ssi->capture.dma_ch) {
68626ac471cSBiju Das 		ssi->playback.dma_ch = dma_request_chan(dev, "rt");
6874b14f179SBiju Das 		if (IS_ERR(ssi->playback.dma_ch)) {
6884b14f179SBiju Das 			ssi->playback.dma_ch = NULL;
68926ac471cSBiju Das 			goto no_dma;
6904b14f179SBiju Das 		}
69126ac471cSBiju Das 
69226ac471cSBiju Das 		ssi->dma_rt = true;
69326ac471cSBiju Das 	}
69426ac471cSBiju Das 
6954b14f179SBiju Das 	if (!rz_ssi_is_dma_enabled(ssi))
6964b14f179SBiju Das 		goto no_dma;
6974b14f179SBiju Das 
69826ac471cSBiju Das 	if (ssi->playback.dma_ch &&
69926ac471cSBiju Das 	    (rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch, true) < 0))
70026ac471cSBiju Das 		goto no_dma;
70126ac471cSBiju Das 
70226ac471cSBiju Das 	if (ssi->capture.dma_ch &&
70326ac471cSBiju Das 	    (rz_ssi_dma_slave_config(ssi, ssi->capture.dma_ch, false) < 0))
70426ac471cSBiju Das 		goto no_dma;
70526ac471cSBiju Das 
70626ac471cSBiju Das 	return 0;
70726ac471cSBiju Das 
70826ac471cSBiju Das no_dma:
70926ac471cSBiju Das 	rz_ssi_release_dma_channels(ssi);
71026ac471cSBiju Das 
71126ac471cSBiju Das 	return -ENODEV;
71226ac471cSBiju Das }
71326ac471cSBiju Das 
71403e786bdSBiju Das static int rz_ssi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
71503e786bdSBiju Das 			      struct snd_soc_dai *dai)
71603e786bdSBiju Das {
71703e786bdSBiju Das 	struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
71803e786bdSBiju Das 	struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream);
71926ac471cSBiju Das 	int ret = 0, i, num_transfer = 1;
72003e786bdSBiju Das 
72103e786bdSBiju Das 	switch (cmd) {
72203e786bdSBiju Das 	case SNDRV_PCM_TRIGGER_START:
72303e786bdSBiju Das 		/* Soft Reset */
72403e786bdSBiju Das 		rz_ssi_reg_mask_setl(ssi, SSIFCR, 0, SSIFCR_SSIRST);
72503e786bdSBiju Das 		rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_SSIRST, 0);
72603e786bdSBiju Das 		udelay(5);
72703e786bdSBiju Das 
7284f78f3c9SLad Prabhakar 		rz_ssi_stream_init(strm, substream);
72903e786bdSBiju Das 
73026ac471cSBiju Das 		if (ssi->dma_rt) {
73126ac471cSBiju Das 			bool is_playback;
73226ac471cSBiju Das 
73326ac471cSBiju Das 			is_playback = rz_ssi_stream_is_play(ssi, substream);
73426ac471cSBiju Das 			ret = rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch,
73526ac471cSBiju Das 						      is_playback);
73626ac471cSBiju Das 			/* Fallback to pio */
73726ac471cSBiju Das 			if (ret < 0) {
73826ac471cSBiju Das 				ssi->playback.transfer = rz_ssi_pio_send;
73926ac471cSBiju Das 				ssi->capture.transfer = rz_ssi_pio_recv;
74026ac471cSBiju Das 				rz_ssi_release_dma_channels(ssi);
74126ac471cSBiju Das 			}
74226ac471cSBiju Das 		}
74326ac471cSBiju Das 
74426ac471cSBiju Das 		/* For DMA, queue up multiple DMA descriptors */
74526ac471cSBiju Das 		if (rz_ssi_is_dma_enabled(ssi))
74626ac471cSBiju Das 			num_transfer = 4;
74726ac471cSBiju Das 
74826ac471cSBiju Das 		for (i = 0; i < num_transfer; i++) {
74903e786bdSBiju Das 			ret = strm->transfer(ssi, strm);
75003e786bdSBiju Das 			if (ret)
75103e786bdSBiju Das 				goto done;
75226ac471cSBiju Das 		}
75303e786bdSBiju Das 
75403e786bdSBiju Das 		ret = rz_ssi_start(ssi, strm);
75503e786bdSBiju Das 		break;
75603e786bdSBiju Das 	case SNDRV_PCM_TRIGGER_STOP:
75703e786bdSBiju Das 		rz_ssi_stop(ssi, strm);
75803e786bdSBiju Das 		rz_ssi_stream_quit(ssi, strm);
75903e786bdSBiju Das 		break;
76003e786bdSBiju Das 	}
76103e786bdSBiju Das 
76203e786bdSBiju Das done:
76303e786bdSBiju Das 	return ret;
76403e786bdSBiju Das }
76503e786bdSBiju Das 
76603e786bdSBiju Das static int rz_ssi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
76703e786bdSBiju Das {
76803e786bdSBiju Das 	struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
76903e786bdSBiju Das 
77003e786bdSBiju Das 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
77103e786bdSBiju Das 	case SND_SOC_DAIFMT_CBC_CFC:
77203e786bdSBiju Das 		break;
77303e786bdSBiju Das 	default:
77403e786bdSBiju Das 		dev_err(ssi->dev, "Codec should be clk and frame consumer\n");
77503e786bdSBiju Das 		return -EINVAL;
77603e786bdSBiju Das 	}
77703e786bdSBiju Das 
77803e786bdSBiju Das 	/*
77903e786bdSBiju Das 	 * set clock polarity
78003e786bdSBiju Das 	 *
78103e786bdSBiju Das 	 * "normal" BCLK = Signal is available at rising edge of BCLK
78203e786bdSBiju Das 	 * "normal" FSYNC = (I2S) Left ch starts with falling FSYNC edge
78303e786bdSBiju Das 	 */
78403e786bdSBiju Das 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
78503e786bdSBiju Das 	case SND_SOC_DAIFMT_NB_NF:
78603e786bdSBiju Das 		ssi->bckp_rise = false;
78703e786bdSBiju Das 		ssi->lrckp_fsync_fall = false;
78803e786bdSBiju Das 		break;
78903e786bdSBiju Das 	case SND_SOC_DAIFMT_NB_IF:
79003e786bdSBiju Das 		ssi->bckp_rise = false;
79103e786bdSBiju Das 		ssi->lrckp_fsync_fall = true;
79203e786bdSBiju Das 		break;
79303e786bdSBiju Das 	case SND_SOC_DAIFMT_IB_NF:
79403e786bdSBiju Das 		ssi->bckp_rise = true;
79503e786bdSBiju Das 		ssi->lrckp_fsync_fall = false;
79603e786bdSBiju Das 		break;
79703e786bdSBiju Das 	case SND_SOC_DAIFMT_IB_IF:
79803e786bdSBiju Das 		ssi->bckp_rise = true;
79903e786bdSBiju Das 		ssi->lrckp_fsync_fall = true;
80003e786bdSBiju Das 		break;
80103e786bdSBiju Das 	default:
80203e786bdSBiju Das 		return -EINVAL;
80303e786bdSBiju Das 	}
80403e786bdSBiju Das 
80503e786bdSBiju Das 	/* only i2s support */
80603e786bdSBiju Das 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
80703e786bdSBiju Das 	case SND_SOC_DAIFMT_I2S:
80803e786bdSBiju Das 		break;
80903e786bdSBiju Das 	default:
81003e786bdSBiju Das 		dev_err(ssi->dev, "Only I2S mode is supported.\n");
81103e786bdSBiju Das 		return -EINVAL;
81203e786bdSBiju Das 	}
81303e786bdSBiju Das 
81403e786bdSBiju Das 	return 0;
81503e786bdSBiju Das }
81603e786bdSBiju Das 
81703e786bdSBiju Das static int rz_ssi_dai_hw_params(struct snd_pcm_substream *substream,
81803e786bdSBiju Das 				struct snd_pcm_hw_params *params,
81903e786bdSBiju Das 				struct snd_soc_dai *dai)
82003e786bdSBiju Das {
82103e786bdSBiju Das 	struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
82203e786bdSBiju Das 	unsigned int sample_bits = hw_param_interval(params,
82303e786bdSBiju Das 					SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
82403e786bdSBiju Das 	unsigned int channels = params_channels(params);
82503e786bdSBiju Das 
82603e786bdSBiju Das 	if (sample_bits != 16) {
82703e786bdSBiju Das 		dev_err(ssi->dev, "Unsupported sample width: %d\n",
82803e786bdSBiju Das 			sample_bits);
82903e786bdSBiju Das 		return -EINVAL;
83003e786bdSBiju Das 	}
83103e786bdSBiju Das 
83203e786bdSBiju Das 	if (channels != 2) {
83303e786bdSBiju Das 		dev_err(ssi->dev, "Number of channels not matched: %d\n",
83403e786bdSBiju Das 			channels);
83503e786bdSBiju Das 		return -EINVAL;
83603e786bdSBiju Das 	}
83703e786bdSBiju Das 
83803e786bdSBiju Das 	return rz_ssi_clk_setup(ssi, params_rate(params),
83903e786bdSBiju Das 				params_channels(params));
84003e786bdSBiju Das }
84103e786bdSBiju Das 
84203e786bdSBiju Das static const struct snd_soc_dai_ops rz_ssi_dai_ops = {
84303e786bdSBiju Das 	.trigger	= rz_ssi_dai_trigger,
84403e786bdSBiju Das 	.set_fmt	= rz_ssi_dai_set_fmt,
84503e786bdSBiju Das 	.hw_params	= rz_ssi_dai_hw_params,
84603e786bdSBiju Das };
84703e786bdSBiju Das 
84803e786bdSBiju Das static const struct snd_pcm_hardware rz_ssi_pcm_hardware = {
84903e786bdSBiju Das 	.info			= SNDRV_PCM_INFO_INTERLEAVED	|
85003e786bdSBiju Das 				  SNDRV_PCM_INFO_MMAP		|
85103e786bdSBiju Das 				  SNDRV_PCM_INFO_MMAP_VALID,
85203e786bdSBiju Das 	.buffer_bytes_max	= PREALLOC_BUFFER,
85303e786bdSBiju Das 	.period_bytes_min	= 32,
85403e786bdSBiju Das 	.period_bytes_max	= 8192,
85503e786bdSBiju Das 	.channels_min		= SSI_CHAN_MIN,
85603e786bdSBiju Das 	.channels_max		= SSI_CHAN_MAX,
85703e786bdSBiju Das 	.periods_min		= 1,
85803e786bdSBiju Das 	.periods_max		= 32,
85903e786bdSBiju Das 	.fifo_size		= 32 * 2,
86003e786bdSBiju Das };
86103e786bdSBiju Das 
86203e786bdSBiju Das static int rz_ssi_pcm_open(struct snd_soc_component *component,
86303e786bdSBiju Das 			   struct snd_pcm_substream *substream)
86403e786bdSBiju Das {
86503e786bdSBiju Das 	snd_soc_set_runtime_hwparams(substream, &rz_ssi_pcm_hardware);
86603e786bdSBiju Das 
86703e786bdSBiju Das 	return snd_pcm_hw_constraint_integer(substream->runtime,
86803e786bdSBiju Das 					    SNDRV_PCM_HW_PARAM_PERIODS);
86903e786bdSBiju Das }
87003e786bdSBiju Das 
87103e786bdSBiju Das static snd_pcm_uframes_t rz_ssi_pcm_pointer(struct snd_soc_component *component,
87203e786bdSBiju Das 					    struct snd_pcm_substream *substream)
87303e786bdSBiju Das {
87403e786bdSBiju Das 	struct snd_soc_dai *dai = rz_ssi_get_dai(substream);
87503e786bdSBiju Das 	struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
87603e786bdSBiju Das 	struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream);
87703e786bdSBiju Das 
87803e786bdSBiju Das 	return strm->buffer_pos;
87903e786bdSBiju Das }
88003e786bdSBiju Das 
88103e786bdSBiju Das static int rz_ssi_pcm_new(struct snd_soc_component *component,
88203e786bdSBiju Das 			  struct snd_soc_pcm_runtime *rtd)
88303e786bdSBiju Das {
88403e786bdSBiju Das 	snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
88503e786bdSBiju Das 				       rtd->card->snd_card->dev,
88603e786bdSBiju Das 				       PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
88703e786bdSBiju Das 	return 0;
88803e786bdSBiju Das }
88903e786bdSBiju Das 
89003e786bdSBiju Das static struct snd_soc_dai_driver rz_ssi_soc_dai[] = {
89103e786bdSBiju Das 	{
89203e786bdSBiju Das 		.name			= "rz-ssi-dai",
89303e786bdSBiju Das 		.playback = {
89403e786bdSBiju Das 			.rates		= SSI_RATES,
89503e786bdSBiju Das 			.formats	= SSI_FMTS,
89603e786bdSBiju Das 			.channels_min	= SSI_CHAN_MIN,
89703e786bdSBiju Das 			.channels_max	= SSI_CHAN_MAX,
89803e786bdSBiju Das 		},
89903e786bdSBiju Das 		.capture = {
90003e786bdSBiju Das 			.rates		= SSI_RATES,
90103e786bdSBiju Das 			.formats	= SSI_FMTS,
90203e786bdSBiju Das 			.channels_min	= SSI_CHAN_MIN,
90303e786bdSBiju Das 			.channels_max	= SSI_CHAN_MAX,
90403e786bdSBiju Das 		},
90503e786bdSBiju Das 		.ops = &rz_ssi_dai_ops,
90603e786bdSBiju Das 	},
90703e786bdSBiju Das };
90803e786bdSBiju Das 
90903e786bdSBiju Das static const struct snd_soc_component_driver rz_ssi_soc_component = {
91003e786bdSBiju Das 	.name		= "rz-ssi",
91103e786bdSBiju Das 	.open		= rz_ssi_pcm_open,
91203e786bdSBiju Das 	.pointer	= rz_ssi_pcm_pointer,
91303e786bdSBiju Das 	.pcm_construct	= rz_ssi_pcm_new,
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");
98003e786bdSBiju Das 	if (ssi->irq_int < 0)
98103e786bdSBiju Das 		return dev_err_probe(&pdev->dev, -ENODEV,
98203e786bdSBiju Das 				     "Unable to get SSI int_req IRQ\n");
98303e786bdSBiju Das 
98403e786bdSBiju Das 	ret = devm_request_irq(&pdev->dev, ssi->irq_int, &rz_ssi_interrupt,
98503e786bdSBiju Das 			       0, dev_name(&pdev->dev), ssi);
98603e786bdSBiju Das 	if (ret < 0)
98703e786bdSBiju Das 		return dev_err_probe(&pdev->dev, ret,
98803e786bdSBiju Das 				     "irq request error (int_req)\n");
98903e786bdSBiju Das 
99026ac471cSBiju Das 	if (!rz_ssi_is_dma_enabled(ssi)) {
99103e786bdSBiju Das 		/* Tx and Rx interrupts (pio only) */
99203e786bdSBiju Das 		ssi->irq_tx = platform_get_irq_byname(pdev, "dma_tx");
99303e786bdSBiju Das 		if (ssi->irq_tx < 0)
99403e786bdSBiju Das 			return dev_err_probe(&pdev->dev, -ENODEV,
99503e786bdSBiju Das 					     "Unable to get SSI dma_tx IRQ\n");
99603e786bdSBiju Das 
99726ac471cSBiju Das 		ret = devm_request_irq(&pdev->dev, ssi->irq_tx,
99826ac471cSBiju Das 				       &rz_ssi_interrupt, 0,
99903e786bdSBiju Das 				       dev_name(&pdev->dev), ssi);
100003e786bdSBiju Das 		if (ret < 0)
100103e786bdSBiju Das 			return dev_err_probe(&pdev->dev, ret,
100203e786bdSBiju Das 					     "irq request error (dma_tx)\n");
100303e786bdSBiju Das 
100403e786bdSBiju Das 		ssi->irq_rx = platform_get_irq_byname(pdev, "dma_rx");
100503e786bdSBiju Das 		if (ssi->irq_rx < 0)
100603e786bdSBiju Das 			return dev_err_probe(&pdev->dev, -ENODEV,
100703e786bdSBiju Das 					     "Unable to get SSI dma_rx IRQ\n");
100803e786bdSBiju Das 
100926ac471cSBiju Das 		ret = devm_request_irq(&pdev->dev, ssi->irq_rx,
101026ac471cSBiju Das 				       &rz_ssi_interrupt, 0,
101103e786bdSBiju Das 				       dev_name(&pdev->dev), ssi);
101203e786bdSBiju Das 		if (ret < 0)
101303e786bdSBiju Das 			return dev_err_probe(&pdev->dev, ret,
101403e786bdSBiju Das 					     "irq request error (dma_rx)\n");
101526ac471cSBiju Das 	}
101603e786bdSBiju Das 
101703e786bdSBiju Das 	ssi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
101803e786bdSBiju Das 	if (IS_ERR(ssi->rstc))
101903e786bdSBiju Das 		return PTR_ERR(ssi->rstc);
102003e786bdSBiju Das 
102103e786bdSBiju Das 	reset_control_deassert(ssi->rstc);
102203e786bdSBiju Das 	pm_runtime_enable(&pdev->dev);
1023f04b4fb4SHeiner Kallweit 	ret = pm_runtime_resume_and_get(&pdev->dev);
1024f04b4fb4SHeiner Kallweit 	if (ret < 0) {
1025f04b4fb4SHeiner Kallweit 		pm_runtime_disable(ssi->dev);
1026f04b4fb4SHeiner Kallweit 		reset_control_assert(ssi->rstc);
1027f04b4fb4SHeiner Kallweit 		return dev_err_probe(ssi->dev, ret, "pm_runtime_resume_and_get failed\n");
1028f04b4fb4SHeiner Kallweit 	}
102903e786bdSBiju Das 
103003e786bdSBiju Das 	ret = devm_snd_soc_register_component(&pdev->dev, &rz_ssi_soc_component,
103103e786bdSBiju Das 					      rz_ssi_soc_dai,
103203e786bdSBiju Das 					      ARRAY_SIZE(rz_ssi_soc_dai));
103303e786bdSBiju Das 	if (ret < 0) {
103426ac471cSBiju Das 		rz_ssi_release_dma_channels(ssi);
103526ac471cSBiju Das 
103603e786bdSBiju Das 		pm_runtime_put(ssi->dev);
103703e786bdSBiju Das 		pm_runtime_disable(ssi->dev);
103803e786bdSBiju Das 		reset_control_assert(ssi->rstc);
103903e786bdSBiju Das 		dev_err(&pdev->dev, "failed to register snd component\n");
104003e786bdSBiju Das 	}
104103e786bdSBiju Das 
104203e786bdSBiju Das 	return ret;
104303e786bdSBiju Das }
104403e786bdSBiju Das 
104503e786bdSBiju Das static int rz_ssi_remove(struct platform_device *pdev)
104603e786bdSBiju Das {
104703e786bdSBiju Das 	struct rz_ssi_priv *ssi = dev_get_drvdata(&pdev->dev);
104803e786bdSBiju Das 
104926ac471cSBiju Das 	rz_ssi_release_dma_channels(ssi);
105026ac471cSBiju Das 
105103e786bdSBiju Das 	pm_runtime_put(ssi->dev);
105203e786bdSBiju Das 	pm_runtime_disable(ssi->dev);
105303e786bdSBiju Das 	reset_control_assert(ssi->rstc);
105403e786bdSBiju Das 
105503e786bdSBiju Das 	return 0;
105603e786bdSBiju Das }
105703e786bdSBiju Das 
105803e786bdSBiju Das static const struct of_device_id rz_ssi_of_match[] = {
105903e786bdSBiju Das 	{ .compatible = "renesas,rz-ssi", },
106003e786bdSBiju Das 	{/* Sentinel */},
106103e786bdSBiju Das };
106203e786bdSBiju Das MODULE_DEVICE_TABLE(of, rz_ssi_of_match);
106303e786bdSBiju Das 
106403e786bdSBiju Das static struct platform_driver rz_ssi_driver = {
106503e786bdSBiju Das 	.driver	= {
106603e786bdSBiju Das 		.name	= "rz-ssi-pcm-audio",
106703e786bdSBiju Das 		.of_match_table = rz_ssi_of_match,
106803e786bdSBiju Das 	},
106903e786bdSBiju Das 	.probe		= rz_ssi_probe,
107003e786bdSBiju Das 	.remove		= rz_ssi_remove,
107103e786bdSBiju Das };
107203e786bdSBiju Das 
107303e786bdSBiju Das module_platform_driver(rz_ssi_driver);
107403e786bdSBiju Das 
107503e786bdSBiju Das MODULE_LICENSE("GPL v2");
107603e786bdSBiju Das MODULE_DESCRIPTION("Renesas RZ/G2L ASoC Serial Sound Interface Driver");
107703e786bdSBiju Das MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
1078