2 * Fifo-attached Serial Interface (FSI) support for SH7724
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
8 * Copyright (c) 2007 Manuel Lauss <mano@roarinelk.homelinux.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #include <linux/delay.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/pm_runtime.h>
19 #include <linux/scatterlist.h>
20 #include <linux/sh_dma.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <sound/soc.h>
24 #include <sound/sh_fsi.h>
26 /* PortA/PortB register */
27 #define REG_DO_FMT 0x0000
28 #define REG_DOFF_CTL 0x0004
29 #define REG_DOFF_ST 0x0008
30 #define REG_DI_FMT 0x000C
31 #define REG_DIFF_CTL 0x0010
32 #define REG_DIFF_ST 0x0014
33 #define REG_CKG1 0x0018
34 #define REG_CKG2 0x001C
35 #define REG_DIDT 0x0020
36 #define REG_DODT 0x0024
37 #define REG_MUTE_ST 0x0028
38 #define REG_OUT_DMAC 0x002C
39 #define REG_OUT_SEL 0x0030
40 #define REG_IN_DMAC 0x0038
43 #define MST_CLK_RST 0x0210
44 #define MST_SOFT_RST 0x0214
45 #define MST_FIFO_SZ 0x0218
47 /* core register (depend on FSI version) */
48 #define A_MST_CTLR 0x0180
49 #define B_MST_CTLR 0x01A0
50 #define CPU_INT_ST 0x01F4
51 #define CPU_IEMSK 0x01F8
52 #define CPU_IMSK 0x01FC
59 #define CR_BWS_MASK (0x3 << 20) /* FSI2 */
60 #define CR_BWS_24 (0x0 << 20) /* FSI2 */
61 #define CR_BWS_16 (0x1 << 20) /* FSI2 */
62 #define CR_BWS_20 (0x2 << 20) /* FSI2 */
64 #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
65 #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
66 #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
68 #define CR_MONO (0x0 << 4)
69 #define CR_MONO_D (0x1 << 4)
70 #define CR_PCM (0x2 << 4)
71 #define CR_I2S (0x3 << 4)
72 #define CR_TDM (0x4 << 4)
73 #define CR_TDM_D (0x5 << 4)
77 #define VDMD_MASK (0x3 << 4)
78 #define VDMD_FRONT (0x0 << 4) /* Package in front */
79 #define VDMD_BACK (0x1 << 4) /* Package in back */
80 #define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
82 #define DMA_ON (0x1 << 0)
86 #define IRQ_HALF 0x00100000
87 #define FIFO_CLR 0x00000001
90 #define ERR_OVER 0x00000010
91 #define ERR_UNDER 0x00000001
92 #define ST_ERR (ERR_OVER | ERR_UNDER)
95 #define ACKMD_MASK 0x00007000
96 #define BPFMD_MASK 0x00000700
101 #define BP (1 << 4) /* Fix the signal of Biphase output */
102 #define SE (1 << 0) /* Fix the master clock */
108 /* IO SHIFT / MACRO */
113 #define AB_IO(param, shift) (param << shift)
116 #define PBSR (1 << 12) /* Port B Software Reset */
117 #define PASR (1 << 8) /* Port A Software Reset */
118 #define IR (1 << 4) /* Interrupt Reset */
119 #define FSISR (1 << 0) /* Software Reset */
122 #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
123 /* 1: Biphase and serial */
126 #define FIFO_SZ_MASK 0x7
128 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
130 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
132 typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
135 * FSI driver use below type name for variable
137 * xxx_num : number of data
138 * xxx_pos : position of data
139 * xxx_capa : capacity of data
143 * period/frame/sample image
147 * period pos period pos
149 * |<-------------------- period--------------------->|
150 * ==|============================================ ... =|==
152 * ||<----- frame ----->|<------ frame ----->| ... |
153 * |+--------------------+--------------------+- ... |
154 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
155 * |+--------------------+--------------------+- ... |
156 * ==|============================================ ... =|==
175 struct fsi_stream_handler;
179 * these are initialized by fsi_stream_init()
181 struct snd_pcm_substream *substream;
182 int fifo_sample_capa; /* sample capacity of FSI FIFO */
183 int buff_sample_capa; /* sample capacity of ALSA buffer */
184 int buff_sample_pos; /* sample position of ALSA buffer */
185 int period_samples; /* sample number / 1 period */
186 int period_pos; /* current period position */
187 int sample_width; /* sample width */
192 * thse are initialized by fsi_handler_init()
194 struct fsi_stream_handler *handler;
195 struct fsi_priv *priv;
198 * these are for DMAEngine
200 struct dma_chan *chan;
201 struct sh_dmae_slave slave; /* see fsi_handler_init() */
202 struct tasklet_struct tasklet;
208 struct fsi_master *master;
209 struct sh_fsi_port_info *info;
211 struct fsi_stream playback;
212 struct fsi_stream capture;
224 struct fsi_stream_handler {
225 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
226 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
227 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io);
228 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
229 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
230 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
233 #define fsi_stream_handler_call(io, func, args...) \
235 !((io)->handler->func) ? 0 : \
236 (io)->handler->func(args))
251 struct fsi_priv fsia;
252 struct fsi_priv fsib;
253 struct fsi_core *core;
257 static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
260 * basic read write function
263 static void __fsi_reg_write(u32 __iomem *reg, u32 data)
265 /* valid data area is 24bit */
268 __raw_writel(data, reg);
271 static u32 __fsi_reg_read(u32 __iomem *reg)
273 return __raw_readl(reg);
276 static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
278 u32 val = __fsi_reg_read(reg);
283 __fsi_reg_write(reg, val);
286 #define fsi_reg_write(p, r, d)\
287 __fsi_reg_write((p->base + REG_##r), d)
289 #define fsi_reg_read(p, r)\
290 __fsi_reg_read((p->base + REG_##r))
292 #define fsi_reg_mask_set(p, r, m, d)\
293 __fsi_reg_mask_set((p->base + REG_##r), m, d)
295 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
296 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
297 static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
302 spin_lock_irqsave(&master->lock, flags);
303 ret = __fsi_reg_read(master->base + reg);
304 spin_unlock_irqrestore(&master->lock, flags);
309 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
310 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
311 static void _fsi_master_mask_set(struct fsi_master *master,
312 u32 reg, u32 mask, u32 data)
316 spin_lock_irqsave(&master->lock, flags);
317 __fsi_reg_mask_set(master->base + reg, mask, data);
318 spin_unlock_irqrestore(&master->lock, flags);
325 static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
330 static int fsi_is_clk_master(struct fsi_priv *fsi)
332 return fsi->clk_master;
335 static int fsi_is_port_a(struct fsi_priv *fsi)
337 return fsi->master->base == fsi->base;
340 static int fsi_is_spdif(struct fsi_priv *fsi)
345 static int fsi_is_play(struct snd_pcm_substream *substream)
347 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
350 static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
352 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
359 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
362 return &master->fsia;
364 return &master->fsib;
367 static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
369 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
372 static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
377 return fsi->info->set_rate;
380 static u32 fsi_get_info_flags(struct fsi_priv *fsi)
385 return fsi->info->flags;
388 static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
390 int is_play = fsi_stream_is_play(fsi, io);
391 int is_porta = fsi_is_port_a(fsi);
395 shift = is_play ? AO_SHIFT : AI_SHIFT;
397 shift = is_play ? BO_SHIFT : BI_SHIFT;
402 static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
404 return frames * fsi->chan_num;
407 static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
409 return samples / fsi->chan_num;
412 static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
413 struct fsi_stream *io)
415 int is_play = fsi_stream_is_play(fsi, io);
420 fsi_reg_read(fsi, DOFF_ST) :
421 fsi_reg_read(fsi, DIFF_ST);
423 frames = 0x1ff & (status >> 8);
425 return fsi_frame2sample(fsi, frames);
428 static void fsi_count_fifo_err(struct fsi_priv *fsi)
430 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
431 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
433 if (ostatus & ERR_OVER)
434 fsi->playback.oerr_num++;
436 if (ostatus & ERR_UNDER)
437 fsi->playback.uerr_num++;
439 if (istatus & ERR_OVER)
440 fsi->capture.oerr_num++;
442 if (istatus & ERR_UNDER)
443 fsi->capture.uerr_num++;
445 fsi_reg_write(fsi, DOFF_ST, 0);
446 fsi_reg_write(fsi, DIFF_ST, 0);
450 * fsi_stream_xx() function
452 static inline int fsi_stream_is_play(struct fsi_priv *fsi,
453 struct fsi_stream *io)
455 return &fsi->playback == io;
458 static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
459 struct snd_pcm_substream *substream)
461 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
464 static int fsi_stream_is_working(struct fsi_priv *fsi,
465 struct fsi_stream *io)
467 struct fsi_master *master = fsi_get_master(fsi);
471 spin_lock_irqsave(&master->lock, flags);
472 ret = !!(io->substream && io->substream->runtime);
473 spin_unlock_irqrestore(&master->lock, flags);
478 static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
483 static void fsi_stream_init(struct fsi_priv *fsi,
484 struct fsi_stream *io,
485 struct snd_pcm_substream *substream)
487 struct snd_pcm_runtime *runtime = substream->runtime;
488 struct fsi_master *master = fsi_get_master(fsi);
491 spin_lock_irqsave(&master->lock, flags);
492 io->substream = substream;
493 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
494 io->buff_sample_pos = 0;
495 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
497 io->sample_width = samples_to_bytes(runtime, 1);
498 io->oerr_num = -1; /* ignore 1st err */
499 io->uerr_num = -1; /* ignore 1st err */
500 fsi_stream_handler_call(io, init, fsi, io);
501 spin_unlock_irqrestore(&master->lock, flags);
504 static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
506 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
507 struct fsi_master *master = fsi_get_master(fsi);
510 spin_lock_irqsave(&master->lock, flags);
512 if (io->oerr_num > 0)
513 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
515 if (io->uerr_num > 0)
516 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
518 fsi_stream_handler_call(io, quit, fsi, io);
519 io->substream = NULL;
520 io->buff_sample_capa = 0;
521 io->buff_sample_pos = 0;
522 io->period_samples = 0;
524 io->sample_width = 0;
527 spin_unlock_irqrestore(&master->lock, flags);
530 static int fsi_stream_transfer(struct fsi_stream *io)
532 struct fsi_priv *fsi = fsi_stream_to_priv(io);
536 return fsi_stream_handler_call(io, transfer, fsi, io);
539 #define fsi_stream_start(fsi, io)\
540 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
542 #define fsi_stream_stop(fsi, io)\
543 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
545 static int fsi_stream_probe(struct fsi_priv *fsi)
547 struct fsi_stream *io;
551 ret1 = fsi_stream_handler_call(io, probe, fsi, io);
554 ret2 = fsi_stream_handler_call(io, probe, fsi, io);
564 static int fsi_stream_remove(struct fsi_priv *fsi)
566 struct fsi_stream *io;
570 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
573 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
587 static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
589 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
590 struct fsi_master *master = fsi_get_master(fsi);
592 fsi_core_mask_set(master, imsk, data, data);
593 fsi_core_mask_set(master, iemsk, data, data);
596 static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
598 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
599 struct fsi_master *master = fsi_get_master(fsi);
601 fsi_core_mask_set(master, imsk, data, 0);
602 fsi_core_mask_set(master, iemsk, data, 0);
605 static u32 fsi_irq_get_status(struct fsi_master *master)
607 return fsi_core_read(master, int_st);
610 static void fsi_irq_clear_status(struct fsi_priv *fsi)
613 struct fsi_master *master = fsi_get_master(fsi);
615 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
616 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
618 /* clear interrupt factor */
619 fsi_core_mask_set(master, int_st, data, 0);
623 * SPDIF master clock function
625 * These functions are used later FSI2
627 static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
629 struct fsi_master *master = fsi_get_master(fsi);
632 if (master->core->ver < 2) {
633 pr_err("fsi: register access err (%s)\n", __func__);
638 val = enable ? mask : 0;
641 fsi_core_mask_set(master, a_mclk, mask, val) :
642 fsi_core_mask_set(master, b_mclk, mask, val);
648 static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
649 long rate, int enable)
651 struct fsi_master *master = fsi_get_master(fsi);
652 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
653 int fsi_ver = master->core->ver;
659 ret = set_rate(dev, rate, enable);
660 if (ret < 0) /* error */
669 switch (ret & SH_FSI_ACKMD_MASK) {
672 case SH_FSI_ACKMD_512:
675 case SH_FSI_ACKMD_256:
678 case SH_FSI_ACKMD_128:
681 case SH_FSI_ACKMD_64:
684 case SH_FSI_ACKMD_32:
686 dev_err(dev, "unsupported ACKMD\n");
692 switch (ret & SH_FSI_BPFMD_MASK) {
695 case SH_FSI_BPFMD_32:
698 case SH_FSI_BPFMD_64:
701 case SH_FSI_BPFMD_128:
704 case SH_FSI_BPFMD_256:
707 case SH_FSI_BPFMD_512:
710 case SH_FSI_BPFMD_16:
712 dev_err(dev, "unsupported ACKMD\n");
718 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
727 * pio data transfer handler
729 static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
731 u16 *buf = (u16 *)_buf;
734 for (i = 0; i < samples; i++)
735 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
738 static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
740 u16 *buf = (u16 *)_buf;
743 for (i = 0; i < samples; i++)
744 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
747 static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
749 u32 *buf = (u32 *)_buf;
752 for (i = 0; i < samples; i++)
753 fsi_reg_write(fsi, DODT, *(buf + i));
756 static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
758 u32 *buf = (u32 *)_buf;
761 for (i = 0; i < samples; i++)
762 *(buf + i) = fsi_reg_read(fsi, DIDT);
765 static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
767 struct snd_pcm_runtime *runtime = io->substream->runtime;
769 return runtime->dma_area +
770 samples_to_bytes(runtime, io->buff_sample_pos);
773 static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
774 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
775 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
778 struct snd_pcm_runtime *runtime;
779 struct snd_pcm_substream *substream;
783 if (!fsi_stream_is_working(fsi, io))
787 substream = io->substream;
788 runtime = substream->runtime;
790 /* FSI FIFO has limit.
791 * So, this driver can not send periods data at a time
793 if (io->buff_sample_pos >=
794 io->period_samples * (io->period_pos + 1)) {
797 io->period_pos = (io->period_pos + 1) % runtime->periods;
799 if (0 == io->period_pos)
800 io->buff_sample_pos = 0;
803 buf = fsi_pio_get_area(fsi, io);
805 switch (io->sample_width) {
807 run16(fsi, buf, samples);
810 run32(fsi, buf, samples);
816 /* update buff_sample_pos */
817 io->buff_sample_pos += samples;
820 snd_pcm_period_elapsed(substream);
825 static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
827 int sample_residues; /* samples in FSI fifo */
828 int sample_space; /* ALSA free samples space */
831 sample_residues = fsi_get_current_fifo_samples(fsi, io);
832 sample_space = io->buff_sample_capa - io->buff_sample_pos;
834 samples = min(sample_residues, sample_space);
836 return fsi_pio_transfer(fsi, io,
842 static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
844 int sample_residues; /* ALSA residue samples */
845 int sample_space; /* FSI fifo free samples space */
848 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
849 sample_space = io->fifo_sample_capa -
850 fsi_get_current_fifo_samples(fsi, io);
852 samples = min(sample_residues, sample_space);
854 return fsi_pio_transfer(fsi, io,
860 static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
863 struct fsi_master *master = fsi_get_master(fsi);
864 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
867 fsi_irq_enable(fsi, io);
869 fsi_irq_disable(fsi, io);
871 if (fsi_is_clk_master(fsi))
872 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
875 static struct fsi_stream_handler fsi_pio_push_handler = {
876 .transfer = fsi_pio_push,
877 .start_stop = fsi_pio_start_stop,
880 static struct fsi_stream_handler fsi_pio_pop_handler = {
881 .transfer = fsi_pio_pop,
882 .start_stop = fsi_pio_start_stop,
885 static irqreturn_t fsi_interrupt(int irq, void *data)
887 struct fsi_master *master = data;
888 u32 int_st = fsi_irq_get_status(master);
890 /* clear irq status */
891 fsi_master_mask_set(master, SOFT_RST, IR, 0);
892 fsi_master_mask_set(master, SOFT_RST, IR, IR);
894 if (int_st & AB_IO(1, AO_SHIFT))
895 fsi_stream_transfer(&master->fsia.playback);
896 if (int_st & AB_IO(1, BO_SHIFT))
897 fsi_stream_transfer(&master->fsib.playback);
898 if (int_st & AB_IO(1, AI_SHIFT))
899 fsi_stream_transfer(&master->fsia.capture);
900 if (int_st & AB_IO(1, BI_SHIFT))
901 fsi_stream_transfer(&master->fsib.capture);
903 fsi_count_fifo_err(&master->fsia);
904 fsi_count_fifo_err(&master->fsib);
906 fsi_irq_clear_status(&master->fsia);
907 fsi_irq_clear_status(&master->fsib);
913 * dma data transfer handler
915 static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
917 struct snd_pcm_runtime *runtime = io->substream->runtime;
918 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
919 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
920 DMA_TO_DEVICE : DMA_FROM_DEVICE;
922 io->dma = dma_map_single(dai->dev, runtime->dma_area,
923 snd_pcm_lib_buffer_bytes(io->substream), dir);
927 static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
929 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
930 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
931 DMA_TO_DEVICE : DMA_FROM_DEVICE;
933 dma_unmap_single(dai->dev, io->dma,
934 snd_pcm_lib_buffer_bytes(io->substream), dir);
938 static void fsi_dma_complete(void *data)
940 struct fsi_stream *io = (struct fsi_stream *)data;
941 struct fsi_priv *fsi = fsi_stream_to_priv(io);
942 struct snd_pcm_runtime *runtime = io->substream->runtime;
943 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
944 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
945 DMA_TO_DEVICE : DMA_FROM_DEVICE;
947 dma_sync_single_for_cpu(dai->dev, io->dma,
948 samples_to_bytes(runtime, io->period_samples), dir);
950 io->buff_sample_pos += io->period_samples;
953 if (io->period_pos >= runtime->periods) {
955 io->buff_sample_pos = 0;
958 fsi_count_fifo_err(fsi);
959 fsi_stream_transfer(io);
961 snd_pcm_period_elapsed(io->substream);
964 static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
966 struct snd_pcm_runtime *runtime = io->substream->runtime;
968 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
971 static void fsi_dma_do_tasklet(unsigned long data)
973 struct fsi_stream *io = (struct fsi_stream *)data;
974 struct fsi_priv *fsi = fsi_stream_to_priv(io);
975 struct dma_chan *chan;
976 struct snd_soc_dai *dai;
977 struct dma_async_tx_descriptor *desc;
978 struct scatterlist sg;
979 struct snd_pcm_runtime *runtime;
980 enum dma_data_direction dir;
982 int is_play = fsi_stream_is_play(fsi, io);
986 if (!fsi_stream_is_working(fsi, io))
989 dai = fsi_get_dai(io->substream);
991 runtime = io->substream->runtime;
992 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
993 len = samples_to_bytes(runtime, io->period_samples);
994 buf = fsi_dma_get_area(io);
996 dma_sync_single_for_device(dai->dev, io->dma, len, dir);
998 sg_init_table(&sg, 1);
999 sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
1000 len , offset_in_page(buf));
1001 sg_dma_address(&sg) = buf;
1002 sg_dma_len(&sg) = len;
1004 desc = dmaengine_prep_slave_sg(chan, &sg, 1, dir,
1005 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1007 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
1011 desc->callback = fsi_dma_complete;
1012 desc->callback_param = io;
1014 cookie = desc->tx_submit(desc);
1016 dev_err(dai->dev, "tx_submit() fail\n");
1020 dma_async_issue_pending(chan);
1025 * In DMAEngine case, codec and FSI cannot be started simultaneously
1026 * since FSI is using tasklet.
1027 * Therefore, in capture case, probably FSI FIFO will have got
1028 * overflow error in this point.
1029 * in that case, DMA cannot start transfer until error was cleared.
1032 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1033 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1034 fsi_reg_write(fsi, DIFF_ST, 0);
1039 static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1041 struct sh_dmae_slave *slave = param;
1043 chan->private = slave;
1048 static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1050 tasklet_schedule(&io->tasklet);
1055 static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1061 switch (io->sample_width * start) {
1064 dma = VDMD_STREAM | DMA_ON;
1068 dma = VDMD_BACK | DMA_ON;
1075 fsi_reg_mask_set(fsi, DO_FMT, CR_BWS_MASK, bws);
1076 fsi_reg_write(fsi, OUT_DMAC, dma);
1079 static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io)
1081 dma_cap_mask_t mask;
1084 dma_cap_set(DMA_SLAVE, mask);
1086 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
1090 tasklet_init(&io->tasklet, fsi_dma_do_tasklet, (unsigned long)io);
1095 static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1097 tasklet_kill(&io->tasklet);
1099 fsi_stream_stop(fsi, io);
1102 dma_release_channel(io->chan);
1108 static struct fsi_stream_handler fsi_dma_push_handler = {
1109 .init = fsi_dma_init,
1110 .quit = fsi_dma_quit,
1111 .probe = fsi_dma_probe,
1112 .transfer = fsi_dma_transfer,
1113 .remove = fsi_dma_remove,
1114 .start_stop = fsi_dma_push_start_stop,
1120 static void fsi_fifo_init(struct fsi_priv *fsi,
1121 struct fsi_stream *io,
1124 struct fsi_master *master = fsi_get_master(fsi);
1125 int is_play = fsi_stream_is_play(fsi, io);
1129 /* get on-chip RAM capacity */
1130 shift = fsi_master_read(master, FIFO_SZ);
1131 shift >>= fsi_get_port_shift(fsi, io);
1132 shift &= FIFO_SZ_MASK;
1133 frame_capa = 256 << shift;
1134 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1137 * The maximum number of sample data varies depending
1138 * on the number of channels selected for the format.
1140 * FIFOs are used in 4-channel units in 3-channel mode
1141 * and in 8-channel units in 5- to 7-channel mode
1142 * meaning that more FIFOs than the required size of DPRAM
1145 * ex) if 256 words of DP-RAM is connected
1146 * 1 channel: 256 (256 x 1 = 256)
1147 * 2 channels: 128 (128 x 2 = 256)
1148 * 3 channels: 64 ( 64 x 3 = 192)
1149 * 4 channels: 64 ( 64 x 4 = 256)
1150 * 5 channels: 32 ( 32 x 5 = 160)
1151 * 6 channels: 32 ( 32 x 6 = 192)
1152 * 7 channels: 32 ( 32 x 7 = 224)
1153 * 8 channels: 32 ( 32 x 8 = 256)
1155 for (i = 1; i < fsi->chan_num; i <<= 1)
1157 dev_dbg(dev, "%d channel %d store\n",
1158 fsi->chan_num, frame_capa);
1160 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1163 * set interrupt generation factor
1167 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1168 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1170 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1171 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1175 static int fsi_hw_startup(struct fsi_priv *fsi,
1176 struct fsi_stream *io,
1179 struct fsi_master *master = fsi_get_master(fsi);
1180 int fsi_ver = master->core->ver;
1181 u32 flags = fsi_get_info_flags(fsi);
1185 if (fsi_is_clk_master(fsi))
1188 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
1190 /* clock inversion (CKG2) */
1192 if (SH_FSI_LRM_INV & flags)
1194 if (SH_FSI_BRM_INV & flags)
1196 if (SH_FSI_LRS_INV & flags)
1198 if (SH_FSI_BRS_INV & flags)
1201 fsi_reg_write(fsi, CKG2, data);
1204 fsi_reg_write(fsi, DO_FMT, fsi->do_fmt);
1205 fsi_reg_write(fsi, DI_FMT, fsi->di_fmt);
1208 if (fsi_is_spdif(fsi)) {
1209 fsi_spdif_clk_ctrl(fsi, 1);
1210 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1216 * FSI driver assumed that data package is in-back.
1217 * FSI2 chip can select it.
1220 fsi_reg_write(fsi, OUT_DMAC, (1 << 4));
1221 fsi_reg_write(fsi, IN_DMAC, (1 << 4));
1225 fsi_irq_disable(fsi, io);
1226 fsi_irq_clear_status(fsi);
1229 fsi_fifo_init(fsi, io, dev);
1234 static void fsi_hw_shutdown(struct fsi_priv *fsi,
1237 if (fsi_is_clk_master(fsi))
1238 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
1241 static int fsi_dai_startup(struct snd_pcm_substream *substream,
1242 struct snd_soc_dai *dai)
1244 struct fsi_priv *fsi = fsi_get_priv(substream);
1246 return fsi_hw_startup(fsi, fsi_stream_get(fsi, substream), dai->dev);
1249 static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1250 struct snd_soc_dai *dai)
1252 struct fsi_priv *fsi = fsi_get_priv(substream);
1254 fsi_hw_shutdown(fsi, dai->dev);
1258 static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1259 struct snd_soc_dai *dai)
1261 struct fsi_priv *fsi = fsi_get_priv(substream);
1262 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1266 case SNDRV_PCM_TRIGGER_START:
1267 fsi_stream_init(fsi, io, substream);
1268 ret = fsi_stream_transfer(io);
1270 fsi_stream_start(fsi, io);
1272 case SNDRV_PCM_TRIGGER_STOP:
1273 fsi_stream_stop(fsi, io);
1274 fsi_stream_quit(fsi, io);
1281 static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1285 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1286 case SND_SOC_DAIFMT_I2S:
1290 case SND_SOC_DAIFMT_LEFT_J:
1304 static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1306 struct fsi_master *master = fsi_get_master(fsi);
1309 if (master->core->ver < 2)
1312 data = CR_BWS_16 | CR_DTMD_SPDIF_PCM | CR_PCM;
1322 static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1324 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
1325 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
1326 u32 flags = fsi_get_info_flags(fsi);
1329 /* set master/slave audio interface */
1330 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1331 case SND_SOC_DAIFMT_CBM_CFM:
1332 fsi->clk_master = 1;
1334 case SND_SOC_DAIFMT_CBS_CFS:
1340 if (fsi_is_clk_master(fsi) && !set_rate) {
1341 dev_err(dai->dev, "platform doesn't have set_rate\n");
1346 switch (flags & SH_FSI_FMT_MASK) {
1347 case SH_FSI_FMT_DAI:
1348 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1350 case SH_FSI_FMT_SPDIF:
1351 ret = fsi_set_fmt_spdif(fsi);
1360 static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1361 struct snd_pcm_hw_params *params,
1362 struct snd_soc_dai *dai)
1364 struct fsi_priv *fsi = fsi_get_priv(substream);
1365 long rate = params_rate(params);
1368 if (!fsi_is_clk_master(fsi))
1371 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1380 static const struct snd_soc_dai_ops fsi_dai_ops = {
1381 .startup = fsi_dai_startup,
1382 .shutdown = fsi_dai_shutdown,
1383 .trigger = fsi_dai_trigger,
1384 .set_fmt = fsi_dai_set_fmt,
1385 .hw_params = fsi_dai_hw_params,
1392 static struct snd_pcm_hardware fsi_pcm_hardware = {
1393 .info = SNDRV_PCM_INFO_INTERLEAVED |
1394 SNDRV_PCM_INFO_MMAP |
1395 SNDRV_PCM_INFO_MMAP_VALID |
1396 SNDRV_PCM_INFO_PAUSE,
1397 .formats = FSI_FMTS,
1403 .buffer_bytes_max = 64 * 1024,
1404 .period_bytes_min = 32,
1405 .period_bytes_max = 8192,
1411 static int fsi_pcm_open(struct snd_pcm_substream *substream)
1413 struct snd_pcm_runtime *runtime = substream->runtime;
1416 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1418 ret = snd_pcm_hw_constraint_integer(runtime,
1419 SNDRV_PCM_HW_PARAM_PERIODS);
1424 static int fsi_hw_params(struct snd_pcm_substream *substream,
1425 struct snd_pcm_hw_params *hw_params)
1427 return snd_pcm_lib_malloc_pages(substream,
1428 params_buffer_bytes(hw_params));
1431 static int fsi_hw_free(struct snd_pcm_substream *substream)
1433 return snd_pcm_lib_free_pages(substream);
1436 static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1438 struct fsi_priv *fsi = fsi_get_priv(substream);
1439 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1441 return fsi_sample2frame(fsi, io->buff_sample_pos);
1444 static struct snd_pcm_ops fsi_pcm_ops = {
1445 .open = fsi_pcm_open,
1446 .ioctl = snd_pcm_lib_ioctl,
1447 .hw_params = fsi_hw_params,
1448 .hw_free = fsi_hw_free,
1449 .pointer = fsi_pointer,
1456 #define PREALLOC_BUFFER (32 * 1024)
1457 #define PREALLOC_BUFFER_MAX (32 * 1024)
1459 static void fsi_pcm_free(struct snd_pcm *pcm)
1461 snd_pcm_lib_preallocate_free_for_all(pcm);
1464 static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
1466 struct snd_pcm *pcm = rtd->pcm;
1469 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1470 * in MMAP mode (i.e. aplay -M)
1472 return snd_pcm_lib_preallocate_pages_for_all(
1474 SNDRV_DMA_TYPE_CONTINUOUS,
1475 snd_dma_continuous_data(GFP_KERNEL),
1476 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1483 static struct snd_soc_dai_driver fsi_soc_dai[] = {
1488 .formats = FSI_FMTS,
1494 .formats = FSI_FMTS,
1498 .ops = &fsi_dai_ops,
1504 .formats = FSI_FMTS,
1510 .formats = FSI_FMTS,
1514 .ops = &fsi_dai_ops,
1518 static struct snd_soc_platform_driver fsi_soc_platform = {
1519 .ops = &fsi_pcm_ops,
1520 .pcm_new = fsi_pcm_new,
1521 .pcm_free = fsi_pcm_free,
1527 static void fsi_handler_init(struct fsi_priv *fsi)
1529 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1530 fsi->playback.priv = fsi;
1531 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1532 fsi->capture.priv = fsi;
1534 if (fsi->info->tx_id) {
1535 fsi->playback.slave.slave_id = fsi->info->tx_id;
1536 fsi->playback.handler = &fsi_dma_push_handler;
1540 static int fsi_probe(struct platform_device *pdev)
1542 struct fsi_master *master;
1543 const struct platform_device_id *id_entry;
1544 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
1545 struct resource *res;
1549 id_entry = pdev->id_entry;
1551 dev_err(&pdev->dev, "unknown fsi device\n");
1555 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1556 irq = platform_get_irq(pdev, 0);
1557 if (!res || (int)irq <= 0) {
1558 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1563 master = kzalloc(sizeof(*master), GFP_KERNEL);
1565 dev_err(&pdev->dev, "Could not allocate master\n");
1570 master->base = ioremap_nocache(res->start, resource_size(res));
1571 if (!master->base) {
1573 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1577 /* master setting */
1579 master->core = (struct fsi_core *)id_entry->driver_data;
1580 spin_lock_init(&master->lock);
1583 master->fsia.base = master->base;
1584 master->fsia.master = master;
1585 master->fsia.info = &info->port_a;
1586 fsi_handler_init(&master->fsia);
1587 ret = fsi_stream_probe(&master->fsia);
1589 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1594 master->fsib.base = master->base + 0x40;
1595 master->fsib.master = master;
1596 master->fsib.info = &info->port_b;
1597 fsi_handler_init(&master->fsib);
1598 ret = fsi_stream_probe(&master->fsib);
1600 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1604 pm_runtime_enable(&pdev->dev);
1605 dev_set_drvdata(&pdev->dev, master);
1607 ret = request_irq(irq, &fsi_interrupt, 0,
1608 id_entry->name, master);
1610 dev_err(&pdev->dev, "irq request err\n");
1614 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
1616 dev_err(&pdev->dev, "cannot snd soc register\n");
1620 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1621 ARRAY_SIZE(fsi_soc_dai));
1623 dev_err(&pdev->dev, "cannot snd dai register\n");
1630 snd_soc_unregister_platform(&pdev->dev);
1632 free_irq(irq, master);
1634 fsi_stream_remove(&master->fsib);
1636 fsi_stream_remove(&master->fsia);
1638 iounmap(master->base);
1639 pm_runtime_disable(&pdev->dev);
1647 static int fsi_remove(struct platform_device *pdev)
1649 struct fsi_master *master;
1651 master = dev_get_drvdata(&pdev->dev);
1653 free_irq(master->irq, master);
1654 pm_runtime_disable(&pdev->dev);
1656 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1657 snd_soc_unregister_platform(&pdev->dev);
1659 fsi_stream_remove(&master->fsia);
1660 fsi_stream_remove(&master->fsib);
1662 iounmap(master->base);
1668 static void __fsi_suspend(struct fsi_priv *fsi,
1669 struct fsi_stream *io,
1672 if (!fsi_stream_is_working(fsi, io))
1675 fsi_stream_stop(fsi, io);
1676 fsi_hw_shutdown(fsi, dev);
1679 static void __fsi_resume(struct fsi_priv *fsi,
1680 struct fsi_stream *io,
1683 if (!fsi_stream_is_working(fsi, io))
1686 fsi_hw_startup(fsi, io, dev);
1688 if (fsi_is_clk_master(fsi) && fsi->rate)
1689 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
1691 fsi_stream_start(fsi, io);
1694 static int fsi_suspend(struct device *dev)
1696 struct fsi_master *master = dev_get_drvdata(dev);
1697 struct fsi_priv *fsia = &master->fsia;
1698 struct fsi_priv *fsib = &master->fsib;
1700 __fsi_suspend(fsia, &fsia->playback, dev);
1701 __fsi_suspend(fsia, &fsia->capture, dev);
1703 __fsi_suspend(fsib, &fsib->playback, dev);
1704 __fsi_suspend(fsib, &fsib->capture, dev);
1709 static int fsi_resume(struct device *dev)
1711 struct fsi_master *master = dev_get_drvdata(dev);
1712 struct fsi_priv *fsia = &master->fsia;
1713 struct fsi_priv *fsib = &master->fsib;
1715 __fsi_resume(fsia, &fsia->playback, dev);
1716 __fsi_resume(fsia, &fsia->capture, dev);
1718 __fsi_resume(fsib, &fsib->playback, dev);
1719 __fsi_resume(fsib, &fsib->capture, dev);
1724 static struct dev_pm_ops fsi_pm_ops = {
1725 .suspend = fsi_suspend,
1726 .resume = fsi_resume,
1729 static struct fsi_core fsi1_core = {
1738 static struct fsi_core fsi2_core = {
1742 .int_st = CPU_INT_ST,
1745 .a_mclk = A_MST_CTLR,
1746 .b_mclk = B_MST_CTLR,
1749 static struct platform_device_id fsi_id_table[] = {
1750 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1751 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
1754 MODULE_DEVICE_TABLE(platform, fsi_id_table);
1756 static struct platform_driver fsi_driver = {
1758 .name = "fsi-pcm-audio",
1762 .remove = fsi_remove,
1763 .id_table = fsi_id_table,
1766 module_platform_driver(fsi_driver);
1768 MODULE_LICENSE("GPL");
1769 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1770 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1771 MODULE_ALIAS("platform:fsi-pcm-audio");