1 // SPDX-License-Identifier: GPL-2.0
3 // Renesas RZ/G2L ASoC Serial Sound Interface (SSIF-2) Driver
5 // Copyright (C) 2021 Renesas Electronics Corp.
6 // Copyright (C) 2019 Chris Brandt.
10 #include <linux/dmaengine.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/reset.h>
16 #include <sound/soc.h>
28 /* SSI REGISTER BITS */
29 #define SSICR_DWL(x) (((x) & 0x7) << 19)
30 #define SSICR_SWL(x) (((x) & 0x7) << 16)
32 #define SSICR_CKS BIT(30)
33 #define SSICR_TUIEN BIT(29)
34 #define SSICR_TOIEN BIT(28)
35 #define SSICR_RUIEN BIT(27)
36 #define SSICR_ROIEN BIT(26)
37 #define SSICR_MST BIT(14)
38 #define SSICR_BCKP BIT(13)
39 #define SSICR_LRCKP BIT(12)
40 #define SSICR_CKDV(x) (((x) & 0xf) << 4)
41 #define SSICR_TEN BIT(1)
42 #define SSICR_REN BIT(0)
44 #define SSISR_TUIRQ BIT(29)
45 #define SSISR_TOIRQ BIT(28)
46 #define SSISR_RUIRQ BIT(27)
47 #define SSISR_ROIRQ BIT(26)
48 #define SSISR_IIRQ BIT(25)
50 #define SSIFCR_AUCKE BIT(31)
51 #define SSIFCR_SSIRST BIT(16)
52 #define SSIFCR_TIE BIT(3)
53 #define SSIFCR_RIE BIT(2)
54 #define SSIFCR_TFRST BIT(1)
55 #define SSIFCR_RFRST BIT(0)
57 #define SSIFSR_TDC_MASK 0x3f
58 #define SSIFSR_TDC_SHIFT 24
59 #define SSIFSR_RDC_MASK 0x3f
60 #define SSIFSR_RDC_SHIFT 8
62 #define SSIFSR_TDE BIT(16)
63 #define SSIFSR_RDF BIT(0)
65 #define SSIOFR_LRCONT BIT(8)
67 #define SSISCR_TDES(x) (((x) & 0x1f) << 8)
68 #define SSISCR_RDFS(x) (((x) & 0x1f) << 0)
70 /* Pre allocated buffers sizes */
71 #define PREALLOC_BUFFER (SZ_32K)
72 #define PREALLOC_BUFFER_MAX (SZ_32K)
74 #define SSI_RATES SNDRV_PCM_RATE_8000_48000 /* 8k-44.1kHz */
75 #define SSI_FMTS SNDRV_PCM_FMTBIT_S16_LE
76 #define SSI_CHAN_MIN 2
77 #define SSI_CHAN_MAX 2
78 #define SSI_FIFO_DEPTH 32
82 struct rz_ssi_stream {
83 struct rz_ssi_priv *priv;
84 struct snd_pcm_substream *substream;
85 int fifo_sample_size; /* sample capacity of SSI FIFO */
86 int dma_buffer_pos; /* The address for the next DMA descriptor */
87 int period_counter; /* for keeping track of periods transferred */
89 int buffer_pos; /* current frame position in the buffer */
90 int running; /* 0=stopped, 1=running */
95 struct dma_chan *dma_ch;
97 int (*transfer)(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm);
102 struct platform_device *pdev;
103 struct reset_control *rstc;
116 * The SSI supports full-duplex transmission and reception.
117 * However, if an error occurs, channel reset (both transmission
118 * and reception reset) is required.
119 * So it is better to use as half-duplex (playing and recording
120 * should be done on separate channels).
122 struct rz_ssi_stream playback;
123 struct rz_ssi_stream capture;
126 unsigned long audio_mck;
127 unsigned long audio_clk_1;
128 unsigned long audio_clk_2;
130 bool lrckp_fsync_fall; /* LR clock polarity (SSICR.LRCKP) */
131 bool bckp_rise; /* Bit clock polarity (SSICR.BCKP) */
135 static void rz_ssi_dma_complete(void *data);
137 static void rz_ssi_reg_writel(struct rz_ssi_priv *priv, uint reg, u32 data)
139 writel(data, (priv->base + reg));
142 static u32 rz_ssi_reg_readl(struct rz_ssi_priv *priv, uint reg)
144 return readl(priv->base + reg);
147 static void rz_ssi_reg_mask_setl(struct rz_ssi_priv *priv, uint reg,
152 val = readl(priv->base + reg);
153 val = (val & ~bclr) | bset;
154 writel(val, (priv->base + reg));
157 static inline struct snd_soc_dai *
158 rz_ssi_get_dai(struct snd_pcm_substream *substream)
160 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
162 return asoc_rtd_to_cpu(rtd, 0);
165 static inline bool rz_ssi_stream_is_play(struct rz_ssi_priv *ssi,
166 struct snd_pcm_substream *substream)
168 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
171 static inline struct rz_ssi_stream *
172 rz_ssi_stream_get(struct rz_ssi_priv *ssi, struct snd_pcm_substream *substream)
174 struct rz_ssi_stream *stream = &ssi->playback;
176 if (substream->stream != SNDRV_PCM_STREAM_PLAYBACK)
177 stream = &ssi->capture;
182 static inline bool rz_ssi_is_dma_enabled(struct rz_ssi_priv *ssi)
184 return (ssi->playback.dma_ch && (ssi->dma_rt || ssi->capture.dma_ch));
187 static void rz_ssi_set_substream(struct rz_ssi_stream *strm,
188 struct snd_pcm_substream *substream)
190 struct rz_ssi_priv *ssi = strm->priv;
193 spin_lock_irqsave(&ssi->lock, flags);
194 strm->substream = substream;
195 spin_unlock_irqrestore(&ssi->lock, flags);
198 static bool rz_ssi_stream_is_valid(struct rz_ssi_priv *ssi,
199 struct rz_ssi_stream *strm)
204 spin_lock_irqsave(&ssi->lock, flags);
205 ret = strm->substream && strm->substream->runtime;
206 spin_unlock_irqrestore(&ssi->lock, flags);
211 static void rz_ssi_stream_init(struct rz_ssi_stream *strm,
212 struct snd_pcm_substream *substream)
214 struct snd_pcm_runtime *runtime = substream->runtime;
216 rz_ssi_set_substream(strm, substream);
217 strm->sample_width = samples_to_bytes(runtime, 1);
218 strm->dma_buffer_pos = 0;
219 strm->period_counter = 0;
220 strm->buffer_pos = 0;
227 strm->fifo_sample_size = SSI_FIFO_DEPTH;
230 static void rz_ssi_stream_quit(struct rz_ssi_priv *ssi,
231 struct rz_ssi_stream *strm)
233 struct snd_soc_dai *dai = rz_ssi_get_dai(strm->substream);
235 rz_ssi_set_substream(strm, NULL);
237 if (strm->oerr_num > 0)
238 dev_info(dai->dev, "overrun = %d\n", strm->oerr_num);
240 if (strm->uerr_num > 0)
241 dev_info(dai->dev, "underrun = %d\n", strm->uerr_num);
244 static int rz_ssi_clk_setup(struct rz_ssi_priv *ssi, unsigned int rate,
245 unsigned int channels)
247 static s8 ckdv[16] = { 1, 2, 4, 8, 16, 32, 64, 128,
248 6, 12, 24, 48, 96, -1, -1, -1 };
249 unsigned int channel_bits = 32; /* System Word Length */
250 unsigned long bclk_rate = rate * channels * channel_bits;
256 /* Clear AUCKE so we can set MST */
257 rz_ssi_reg_writel(ssi, SSIFCR, 0);
259 /* Continue to output LRCK pin even when idle */
260 rz_ssi_reg_writel(ssi, SSIOFR, SSIOFR_LRCONT);
261 if (ssi->audio_clk_1 && ssi->audio_clk_2) {
262 if (ssi->audio_clk_1 % bclk_rate)
263 ssi->audio_mck = ssi->audio_clk_2;
265 ssi->audio_mck = ssi->audio_clk_1;
270 if (ssi->audio_mck == ssi->audio_clk_1)
274 if (ssi->lrckp_fsync_fall)
275 ssicr |= SSICR_LRCKP;
277 /* Determine the clock divider */
279 div = ssi->audio_mck / bclk_rate;
280 /* try to find an match */
281 for (i = 0; i < ARRAY_SIZE(ckdv); i++) {
282 if (ckdv[i] == div) {
288 if (i == ARRAY_SIZE(ckdv)) {
289 dev_err(ssi->dev, "Rate not divisible by audio clock source\n");
294 * DWL: Data Word Length = 16 bits
295 * SWL: System Word Length = 32 bits
297 ssicr |= SSICR_CKDV(clk_ckdv);
298 ssicr |= SSICR_DWL(1) | SSICR_SWL(3);
299 rz_ssi_reg_writel(ssi, SSICR, ssicr);
300 rz_ssi_reg_writel(ssi, SSIFCR,
301 (SSIFCR_AUCKE | SSIFCR_TFRST | SSIFCR_RFRST));
306 static int rz_ssi_start(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
308 bool is_play = rz_ssi_stream_is_play(ssi, strm->substream);
311 ssicr = rz_ssi_reg_readl(ssi, SSICR);
312 ssifcr = rz_ssi_reg_readl(ssi, SSIFCR) & ~0xF;
314 /* FIFO interrupt thresholds */
315 if (rz_ssi_is_dma_enabled(ssi))
316 rz_ssi_reg_writel(ssi, SSISCR, 0);
318 rz_ssi_reg_writel(ssi, SSISCR,
319 SSISCR_TDES(strm->fifo_sample_size / 2 - 1) |
324 ssicr |= SSICR_TUIEN | SSICR_TOIEN;
325 ssifcr |= SSIFCR_TIE | SSIFCR_RFRST;
327 ssicr |= SSICR_RUIEN | SSICR_ROIEN;
328 ssifcr |= SSIFCR_RIE | SSIFCR_TFRST;
331 rz_ssi_reg_writel(ssi, SSICR, ssicr);
332 rz_ssi_reg_writel(ssi, SSIFCR, ssifcr);
334 /* Clear all error flags */
335 rz_ssi_reg_mask_setl(ssi, SSISR,
336 (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ |
340 ssicr |= is_play ? SSICR_TEN : SSICR_REN;
341 rz_ssi_reg_writel(ssi, SSICR, ssicr);
346 static int rz_ssi_stop(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
353 rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TEN | SSICR_REN, 0);
355 /* Cancel all remaining DMA transactions */
356 if (rz_ssi_is_dma_enabled(ssi))
357 dmaengine_terminate_async(strm->dma_ch);
360 rz_ssi_reg_mask_setl(ssi, SSICR, SSICR_TUIEN | SSICR_TOIEN |
361 SSICR_RUIEN | SSICR_ROIEN, 0);
362 rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_TIE | SSIFCR_RIE, 0);
364 /* Clear all error flags */
365 rz_ssi_reg_mask_setl(ssi, SSISR,
366 (SSISR_TOIRQ | SSISR_TUIRQ | SSISR_ROIRQ |
372 if (rz_ssi_reg_readl(ssi, SSISR) & SSISR_IIRQ)
378 dev_info(ssi->dev, "timeout waiting for SSI idle\n");
380 /* Hold FIFOs in reset */
381 rz_ssi_reg_mask_setl(ssi, SSIFCR, 0,
382 SSIFCR_TFRST | SSIFCR_RFRST);
387 static void rz_ssi_pointer_update(struct rz_ssi_stream *strm, int frames)
389 struct snd_pcm_substream *substream = strm->substream;
390 struct snd_pcm_runtime *runtime;
393 if (!strm->running || !substream || !substream->runtime)
396 runtime = substream->runtime;
397 strm->buffer_pos += frames;
398 WARN_ON(strm->buffer_pos > runtime->buffer_size);
401 if (strm->buffer_pos == runtime->buffer_size)
402 strm->buffer_pos = 0;
404 current_period = strm->buffer_pos / runtime->period_size;
405 if (strm->period_counter != current_period) {
406 snd_pcm_period_elapsed(strm->substream);
407 strm->period_counter = current_period;
411 static int rz_ssi_pio_recv(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
413 struct snd_pcm_substream *substream = strm->substream;
414 struct snd_pcm_runtime *runtime;
421 if (!rz_ssi_stream_is_valid(ssi, strm))
424 runtime = substream->runtime;
427 /* frames left in this period */
428 frames_left = runtime->period_size -
429 (strm->buffer_pos % runtime->period_size);
431 frames_left = runtime->period_size;
433 /* Samples in RX FIFO */
434 fifo_samples = (rz_ssi_reg_readl(ssi, SSIFSR) >>
435 SSIFSR_RDC_SHIFT) & SSIFSR_RDC_MASK;
437 /* Only read full frames at a time */
439 while (frames_left && (fifo_samples >= runtime->channels)) {
440 samples += runtime->channels;
441 fifo_samples -= runtime->channels;
445 /* not enough samples yet */
449 /* calculate new buffer index */
450 buf = (u16 *)runtime->dma_area;
451 buf += strm->buffer_pos * runtime->channels;
453 /* Note, only supports 16-bit samples */
454 for (i = 0; i < samples; i++)
455 *buf++ = (u16)(rz_ssi_reg_readl(ssi, SSIFRDR) >> 16);
457 rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0);
458 rz_ssi_pointer_update(strm, samples / runtime->channels);
459 } while (!frames_left && fifo_samples >= runtime->channels);
464 static int rz_ssi_pio_send(struct rz_ssi_priv *ssi, struct rz_ssi_stream *strm)
466 struct snd_pcm_substream *substream = strm->substream;
467 struct snd_pcm_runtime *runtime = substream->runtime;
475 if (!rz_ssi_stream_is_valid(ssi, strm))
478 /* frames left in this period */
479 frames_left = runtime->period_size - (strm->buffer_pos %
480 runtime->period_size);
481 if (frames_left == 0)
482 frames_left = runtime->period_size;
484 sample_space = strm->fifo_sample_size;
485 ssifsr = rz_ssi_reg_readl(ssi, SSIFSR);
486 sample_space -= (ssifsr >> SSIFSR_TDC_SHIFT) & SSIFSR_TDC_MASK;
488 /* Only add full frames at a time */
489 while (frames_left && (sample_space >= runtime->channels)) {
490 samples += runtime->channels;
491 sample_space -= runtime->channels;
495 /* no space to send anything right now */
499 /* calculate new buffer index */
500 buf = (u16 *)(runtime->dma_area);
501 buf += strm->buffer_pos * runtime->channels;
503 /* Note, only supports 16-bit samples */
504 for (i = 0; i < samples; i++)
505 rz_ssi_reg_writel(ssi, SSIFTDR, ((u32)(*buf++) << 16));
507 rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_TDE, 0);
508 rz_ssi_pointer_update(strm, samples / runtime->channels);
513 static irqreturn_t rz_ssi_interrupt(int irq, void *data)
515 struct rz_ssi_stream *strm = NULL;
516 struct rz_ssi_priv *ssi = data;
517 u32 ssisr = rz_ssi_reg_readl(ssi, SSISR);
519 if (ssi->playback.substream)
520 strm = &ssi->playback;
521 else if (ssi->capture.substream)
522 strm = &ssi->capture;
524 return IRQ_HANDLED; /* Left over TX/RX interrupt */
526 if (irq == ssi->irq_int) { /* error or idle */
527 if (ssisr & SSISR_TUIRQ)
529 if (ssisr & SSISR_TOIRQ)
531 if (ssisr & SSISR_RUIRQ)
533 if (ssisr & SSISR_ROIRQ)
536 if (ssisr & (SSISR_TUIRQ | SSISR_TOIRQ | SSISR_RUIRQ |
539 /* You must reset (stop/restart) after each interrupt */
540 rz_ssi_stop(ssi, strm);
542 /* Clear all flags */
543 rz_ssi_reg_mask_setl(ssi, SSISR, SSISR_TOIRQ |
544 SSISR_TUIRQ | SSISR_ROIRQ |
547 /* Add/remove more data */
548 strm->transfer(ssi, strm);
551 rz_ssi_start(ssi, strm);
559 if (irq == ssi->irq_tx)
560 strm->transfer(ssi, &ssi->playback);
563 if (irq == ssi->irq_rx) {
564 strm->transfer(ssi, &ssi->capture);
565 rz_ssi_reg_mask_setl(ssi, SSIFSR, SSIFSR_RDF, 0);
571 static int rz_ssi_dma_slave_config(struct rz_ssi_priv *ssi,
572 struct dma_chan *dma_ch, bool is_play)
574 struct dma_slave_config cfg;
576 memset(&cfg, 0, sizeof(cfg));
578 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
579 cfg.dst_addr = ssi->phys + SSIFTDR;
580 cfg.src_addr = ssi->phys + SSIFRDR;
581 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
582 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
584 return dmaengine_slave_config(dma_ch, &cfg);
587 static int rz_ssi_dma_transfer(struct rz_ssi_priv *ssi,
588 struct rz_ssi_stream *strm)
590 struct snd_pcm_substream *substream = strm->substream;
591 struct dma_async_tx_descriptor *desc;
592 struct snd_pcm_runtime *runtime;
593 enum dma_transfer_direction dir;
594 u32 dma_paddr, dma_size;
597 if (!rz_ssi_stream_is_valid(ssi, strm))
600 runtime = substream->runtime;
601 if (runtime->status->state == SNDRV_PCM_STATE_DRAINING)
603 * Stream is ending, so do not queue up any more DMA
604 * transfers otherwise we play partial sound clips
605 * because we can't shut off the DMA quick enough.
609 dir = rz_ssi_stream_is_play(ssi, substream) ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
611 /* Always transfer 1 period */
612 amount = runtime->period_size;
614 /* DMA physical address and size */
615 dma_paddr = runtime->dma_addr + frames_to_bytes(runtime,
616 strm->dma_buffer_pos);
617 dma_size = frames_to_bytes(runtime, amount);
618 desc = dmaengine_prep_slave_single(strm->dma_ch, dma_paddr, dma_size,
620 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
622 dev_err(ssi->dev, "dmaengine_prep_slave_single() fail\n");
626 desc->callback = rz_ssi_dma_complete;
627 desc->callback_param = strm;
629 if (dmaengine_submit(desc) < 0) {
630 dev_err(ssi->dev, "dmaengine_submit() fail\n");
634 /* Update DMA pointer */
635 strm->dma_buffer_pos += amount;
636 if (strm->dma_buffer_pos >= runtime->buffer_size)
637 strm->dma_buffer_pos = 0;
640 dma_async_issue_pending(strm->dma_ch);
645 static void rz_ssi_dma_complete(void *data)
647 struct rz_ssi_stream *strm = (struct rz_ssi_stream *)data;
649 if (!strm->running || !strm->substream || !strm->substream->runtime)
652 /* Note that next DMA transaction has probably already started */
653 rz_ssi_pointer_update(strm, strm->substream->runtime->period_size);
655 /* Queue up another DMA transaction */
656 rz_ssi_dma_transfer(strm->priv, strm);
659 static void rz_ssi_release_dma_channels(struct rz_ssi_priv *ssi)
661 if (ssi->playback.dma_ch) {
662 dma_release_channel(ssi->playback.dma_ch);
663 ssi->playback.dma_ch = NULL;
668 if (ssi->capture.dma_ch) {
669 dma_release_channel(ssi->capture.dma_ch);
670 ssi->capture.dma_ch = NULL;
674 static int rz_ssi_dma_request(struct rz_ssi_priv *ssi, struct device *dev)
676 ssi->playback.dma_ch = dma_request_chan(dev, "tx");
677 if (IS_ERR(ssi->playback.dma_ch))
678 ssi->playback.dma_ch = NULL;
680 ssi->capture.dma_ch = dma_request_chan(dev, "rx");
681 if (IS_ERR(ssi->capture.dma_ch))
682 ssi->capture.dma_ch = NULL;
684 if (!ssi->playback.dma_ch && !ssi->capture.dma_ch) {
685 ssi->playback.dma_ch = dma_request_chan(dev, "rt");
686 if (IS_ERR(ssi->playback.dma_ch)) {
687 ssi->playback.dma_ch = NULL;
694 if (!rz_ssi_is_dma_enabled(ssi))
697 if (ssi->playback.dma_ch &&
698 (rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch, true) < 0))
701 if (ssi->capture.dma_ch &&
702 (rz_ssi_dma_slave_config(ssi, ssi->capture.dma_ch, false) < 0))
708 rz_ssi_release_dma_channels(ssi);
713 static int rz_ssi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
714 struct snd_soc_dai *dai)
716 struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
717 struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream);
718 int ret = 0, i, num_transfer = 1;
721 case SNDRV_PCM_TRIGGER_START:
723 rz_ssi_reg_mask_setl(ssi, SSIFCR, 0, SSIFCR_SSIRST);
724 rz_ssi_reg_mask_setl(ssi, SSIFCR, SSIFCR_SSIRST, 0);
727 rz_ssi_stream_init(strm, substream);
732 is_playback = rz_ssi_stream_is_play(ssi, substream);
733 ret = rz_ssi_dma_slave_config(ssi, ssi->playback.dma_ch,
735 /* Fallback to pio */
737 ssi->playback.transfer = rz_ssi_pio_send;
738 ssi->capture.transfer = rz_ssi_pio_recv;
739 rz_ssi_release_dma_channels(ssi);
743 /* For DMA, queue up multiple DMA descriptors */
744 if (rz_ssi_is_dma_enabled(ssi))
747 for (i = 0; i < num_transfer; i++) {
748 ret = strm->transfer(ssi, strm);
753 ret = rz_ssi_start(ssi, strm);
755 case SNDRV_PCM_TRIGGER_STOP:
756 rz_ssi_stop(ssi, strm);
757 rz_ssi_stream_quit(ssi, strm);
765 static int rz_ssi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
767 struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
769 switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
770 case SND_SOC_DAIFMT_BP_FP:
773 dev_err(ssi->dev, "Codec should be clk and frame consumer\n");
780 * "normal" BCLK = Signal is available at rising edge of BCLK
781 * "normal" FSYNC = (I2S) Left ch starts with falling FSYNC edge
783 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
784 case SND_SOC_DAIFMT_NB_NF:
785 ssi->bckp_rise = false;
786 ssi->lrckp_fsync_fall = false;
788 case SND_SOC_DAIFMT_NB_IF:
789 ssi->bckp_rise = false;
790 ssi->lrckp_fsync_fall = true;
792 case SND_SOC_DAIFMT_IB_NF:
793 ssi->bckp_rise = true;
794 ssi->lrckp_fsync_fall = false;
796 case SND_SOC_DAIFMT_IB_IF:
797 ssi->bckp_rise = true;
798 ssi->lrckp_fsync_fall = true;
804 /* only i2s support */
805 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
806 case SND_SOC_DAIFMT_I2S:
809 dev_err(ssi->dev, "Only I2S mode is supported.\n");
816 static int rz_ssi_dai_hw_params(struct snd_pcm_substream *substream,
817 struct snd_pcm_hw_params *params,
818 struct snd_soc_dai *dai)
820 struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
821 unsigned int sample_bits = hw_param_interval(params,
822 SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
823 unsigned int channels = params_channels(params);
825 if (sample_bits != 16) {
826 dev_err(ssi->dev, "Unsupported sample width: %d\n",
832 dev_err(ssi->dev, "Number of channels not matched: %d\n",
837 return rz_ssi_clk_setup(ssi, params_rate(params),
838 params_channels(params));
841 static const struct snd_soc_dai_ops rz_ssi_dai_ops = {
842 .trigger = rz_ssi_dai_trigger,
843 .set_fmt = rz_ssi_dai_set_fmt,
844 .hw_params = rz_ssi_dai_hw_params,
847 static const struct snd_pcm_hardware rz_ssi_pcm_hardware = {
848 .info = SNDRV_PCM_INFO_INTERLEAVED |
849 SNDRV_PCM_INFO_MMAP |
850 SNDRV_PCM_INFO_MMAP_VALID,
851 .buffer_bytes_max = PREALLOC_BUFFER,
852 .period_bytes_min = 32,
853 .period_bytes_max = 8192,
854 .channels_min = SSI_CHAN_MIN,
855 .channels_max = SSI_CHAN_MAX,
861 static int rz_ssi_pcm_open(struct snd_soc_component *component,
862 struct snd_pcm_substream *substream)
864 snd_soc_set_runtime_hwparams(substream, &rz_ssi_pcm_hardware);
866 return snd_pcm_hw_constraint_integer(substream->runtime,
867 SNDRV_PCM_HW_PARAM_PERIODS);
870 static snd_pcm_uframes_t rz_ssi_pcm_pointer(struct snd_soc_component *component,
871 struct snd_pcm_substream *substream)
873 struct snd_soc_dai *dai = rz_ssi_get_dai(substream);
874 struct rz_ssi_priv *ssi = snd_soc_dai_get_drvdata(dai);
875 struct rz_ssi_stream *strm = rz_ssi_stream_get(ssi, substream);
877 return strm->buffer_pos;
880 static int rz_ssi_pcm_new(struct snd_soc_component *component,
881 struct snd_soc_pcm_runtime *rtd)
883 snd_pcm_set_managed_buffer_all(rtd->pcm, SNDRV_DMA_TYPE_DEV,
884 rtd->card->snd_card->dev,
885 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
889 static struct snd_soc_dai_driver rz_ssi_soc_dai[] = {
891 .name = "rz-ssi-dai",
895 .channels_min = SSI_CHAN_MIN,
896 .channels_max = SSI_CHAN_MAX,
901 .channels_min = SSI_CHAN_MIN,
902 .channels_max = SSI_CHAN_MAX,
904 .ops = &rz_ssi_dai_ops,
908 static const struct snd_soc_component_driver rz_ssi_soc_component = {
910 .open = rz_ssi_pcm_open,
911 .pointer = rz_ssi_pcm_pointer,
912 .pcm_construct = rz_ssi_pcm_new,
913 .legacy_dai_naming = 1,
916 static int rz_ssi_probe(struct platform_device *pdev)
918 struct rz_ssi_priv *ssi;
919 struct clk *audio_clk;
920 struct resource *res;
923 ssi = devm_kzalloc(&pdev->dev, sizeof(*ssi), GFP_KERNEL);
928 ssi->dev = &pdev->dev;
929 ssi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
930 if (IS_ERR(ssi->base))
931 return PTR_ERR(ssi->base);
933 ssi->phys = res->start;
934 ssi->clk = devm_clk_get(&pdev->dev, "ssi");
935 if (IS_ERR(ssi->clk))
936 return PTR_ERR(ssi->clk);
938 ssi->sfr_clk = devm_clk_get(&pdev->dev, "ssi_sfr");
939 if (IS_ERR(ssi->sfr_clk))
940 return PTR_ERR(ssi->sfr_clk);
942 audio_clk = devm_clk_get(&pdev->dev, "audio_clk1");
943 if (IS_ERR(audio_clk))
944 return dev_err_probe(&pdev->dev, PTR_ERR(audio_clk),
947 ssi->audio_clk_1 = clk_get_rate(audio_clk);
948 audio_clk = devm_clk_get(&pdev->dev, "audio_clk2");
949 if (IS_ERR(audio_clk))
950 return dev_err_probe(&pdev->dev, PTR_ERR(audio_clk),
953 ssi->audio_clk_2 = clk_get_rate(audio_clk);
954 if (!(ssi->audio_clk_1 || ssi->audio_clk_2))
955 return dev_err_probe(&pdev->dev, -EINVAL,
956 "no audio clk1 or audio clk2");
958 ssi->audio_mck = ssi->audio_clk_1 ? ssi->audio_clk_1 : ssi->audio_clk_2;
960 /* Detect DMA support */
961 ret = rz_ssi_dma_request(ssi, &pdev->dev);
963 dev_warn(&pdev->dev, "DMA not available, using PIO\n");
964 ssi->playback.transfer = rz_ssi_pio_send;
965 ssi->capture.transfer = rz_ssi_pio_recv;
967 dev_info(&pdev->dev, "DMA enabled");
968 ssi->playback.transfer = rz_ssi_dma_transfer;
969 ssi->capture.transfer = rz_ssi_dma_transfer;
972 ssi->playback.priv = ssi;
973 ssi->capture.priv = ssi;
975 spin_lock_init(&ssi->lock);
976 dev_set_drvdata(&pdev->dev, ssi);
978 /* Error Interrupt */
979 ssi->irq_int = platform_get_irq_byname(pdev, "int_req");
980 if (ssi->irq_int < 0) {
981 rz_ssi_release_dma_channels(ssi);
985 ret = devm_request_irq(&pdev->dev, ssi->irq_int, &rz_ssi_interrupt,
986 0, dev_name(&pdev->dev), ssi);
988 rz_ssi_release_dma_channels(ssi);
989 return dev_err_probe(&pdev->dev, ret,
990 "irq request error (int_req)\n");
993 if (!rz_ssi_is_dma_enabled(ssi)) {
994 /* Tx and Rx interrupts (pio only) */
995 ssi->irq_tx = platform_get_irq_byname(pdev, "dma_tx");
999 ret = devm_request_irq(&pdev->dev, ssi->irq_tx,
1000 &rz_ssi_interrupt, 0,
1001 dev_name(&pdev->dev), ssi);
1003 return dev_err_probe(&pdev->dev, ret,
1004 "irq request error (dma_tx)\n");
1006 ssi->irq_rx = platform_get_irq_byname(pdev, "dma_rx");
1007 if (ssi->irq_rx < 0)
1010 ret = devm_request_irq(&pdev->dev, ssi->irq_rx,
1011 &rz_ssi_interrupt, 0,
1012 dev_name(&pdev->dev), ssi);
1014 return dev_err_probe(&pdev->dev, ret,
1015 "irq request error (dma_rx)\n");
1018 ssi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
1019 if (IS_ERR(ssi->rstc)) {
1020 rz_ssi_release_dma_channels(ssi);
1021 return PTR_ERR(ssi->rstc);
1024 reset_control_deassert(ssi->rstc);
1025 pm_runtime_enable(&pdev->dev);
1026 ret = pm_runtime_resume_and_get(&pdev->dev);
1028 rz_ssi_release_dma_channels(ssi);
1029 pm_runtime_disable(ssi->dev);
1030 reset_control_assert(ssi->rstc);
1031 return dev_err_probe(ssi->dev, ret, "pm_runtime_resume_and_get failed\n");
1034 ret = devm_snd_soc_register_component(&pdev->dev, &rz_ssi_soc_component,
1036 ARRAY_SIZE(rz_ssi_soc_dai));
1038 rz_ssi_release_dma_channels(ssi);
1040 pm_runtime_put(ssi->dev);
1041 pm_runtime_disable(ssi->dev);
1042 reset_control_assert(ssi->rstc);
1043 dev_err(&pdev->dev, "failed to register snd component\n");
1049 static int rz_ssi_remove(struct platform_device *pdev)
1051 struct rz_ssi_priv *ssi = dev_get_drvdata(&pdev->dev);
1053 rz_ssi_release_dma_channels(ssi);
1055 pm_runtime_put(ssi->dev);
1056 pm_runtime_disable(ssi->dev);
1057 reset_control_assert(ssi->rstc);
1062 static const struct of_device_id rz_ssi_of_match[] = {
1063 { .compatible = "renesas,rz-ssi", },
1066 MODULE_DEVICE_TABLE(of, rz_ssi_of_match);
1068 static struct platform_driver rz_ssi_driver = {
1070 .name = "rz-ssi-pcm-audio",
1071 .of_match_table = rz_ssi_of_match,
1073 .probe = rz_ssi_probe,
1074 .remove = rz_ssi_remove,
1077 module_platform_driver(rz_ssi_driver);
1079 MODULE_LICENSE("GPL v2");
1080 MODULE_DESCRIPTION("Renesas RZ/G2L ASoC Serial Sound Interface Driver");
1081 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");