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 <linux/workqueue.h>
24 #include <sound/soc.h>
25 #include <sound/sh_fsi.h>
27 /* PortA/PortB register */
28 #define REG_DO_FMT 0x0000
29 #define REG_DOFF_CTL 0x0004
30 #define REG_DOFF_ST 0x0008
31 #define REG_DI_FMT 0x000C
32 #define REG_DIFF_CTL 0x0010
33 #define REG_DIFF_ST 0x0014
34 #define REG_CKG1 0x0018
35 #define REG_CKG2 0x001C
36 #define REG_DIDT 0x0020
37 #define REG_DODT 0x0024
38 #define REG_MUTE_ST 0x0028
39 #define REG_OUT_DMAC 0x002C
40 #define REG_OUT_SEL 0x0030
41 #define REG_IN_DMAC 0x0038
44 #define MST_CLK_RST 0x0210
45 #define MST_SOFT_RST 0x0214
46 #define MST_FIFO_SZ 0x0218
48 /* core register (depend on FSI version) */
49 #define A_MST_CTLR 0x0180
50 #define B_MST_CTLR 0x01A0
51 #define CPU_INT_ST 0x01F4
52 #define CPU_IEMSK 0x01F8
53 #define CPU_IMSK 0x01FC
60 #define CR_BWS_MASK (0x3 << 20) /* FSI2 */
61 #define CR_BWS_24 (0x0 << 20) /* FSI2 */
62 #define CR_BWS_16 (0x1 << 20) /* FSI2 */
63 #define CR_BWS_20 (0x2 << 20) /* FSI2 */
65 #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
66 #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
67 #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
69 #define CR_MONO (0x0 << 4)
70 #define CR_MONO_D (0x1 << 4)
71 #define CR_PCM (0x2 << 4)
72 #define CR_I2S (0x3 << 4)
73 #define CR_TDM (0x4 << 4)
74 #define CR_TDM_D (0x5 << 4)
78 #define VDMD_MASK (0x3 << 4)
79 #define VDMD_FRONT (0x0 << 4) /* Package in front */
80 #define VDMD_BACK (0x1 << 4) /* Package in back */
81 #define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
83 #define DMA_ON (0x1 << 0)
87 #define IRQ_HALF 0x00100000
88 #define FIFO_CLR 0x00000001
91 #define ERR_OVER 0x00000010
92 #define ERR_UNDER 0x00000001
93 #define ST_ERR (ERR_OVER | ERR_UNDER)
96 #define ACKMD_MASK 0x00007000
97 #define BPFMD_MASK 0x00000700
102 #define BP (1 << 4) /* Fix the signal of Biphase output */
103 #define SE (1 << 0) /* Fix the master clock */
109 /* IO SHIFT / MACRO */
114 #define AB_IO(param, shift) (param << shift)
117 #define PBSR (1 << 12) /* Port B Software Reset */
118 #define PASR (1 << 8) /* Port A Software Reset */
119 #define IR (1 << 4) /* Interrupt Reset */
120 #define FSISR (1 << 0) /* Software Reset */
123 #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
124 /* 1: Biphase and serial */
127 #define FIFO_SZ_MASK 0x7
129 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
131 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
133 typedef int (*set_rate_func)(struct device *dev, int rate, int enable);
140 * A : sample widtht 16bit setting
141 * B : sample widtht 24bit setting
144 #define SHIFT_16DATA 0
145 #define SHIFT_24DATA 4
147 #define PACKAGE_24BITBUS_BACK 0
148 #define PACKAGE_24BITBUS_FRONT 1
149 #define PACKAGE_16BITBUS_STREAM 2
151 #define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
152 #define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
155 * FSI driver use below type name for variable
157 * xxx_num : number of data
158 * xxx_pos : position of data
159 * xxx_capa : capacity of data
163 * period/frame/sample image
167 * period pos period pos
169 * |<-------------------- period--------------------->|
170 * ==|============================================ ... =|==
172 * ||<----- frame ----->|<------ frame ----->| ... |
173 * |+--------------------+--------------------+- ... |
174 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
175 * |+--------------------+--------------------+- ... |
176 * ==|============================================ ... =|==
195 struct fsi_stream_handler;
199 * these are initialized by fsi_stream_init()
201 struct snd_pcm_substream *substream;
202 int fifo_sample_capa; /* sample capacity of FSI FIFO */
203 int buff_sample_capa; /* sample capacity of ALSA buffer */
204 int buff_sample_pos; /* sample position of ALSA buffer */
205 int period_samples; /* sample number / 1 period */
206 int period_pos; /* current period position */
207 int sample_width; /* sample width */
217 * thse are initialized by fsi_handler_init()
219 struct fsi_stream_handler *handler;
220 struct fsi_priv *priv;
223 * these are for DMAEngine
225 struct dma_chan *chan;
226 struct sh_dmae_slave slave; /* see fsi_handler_init() */
227 struct work_struct work;
233 struct fsi_master *master;
234 struct sh_fsi_port_info *info;
236 struct fsi_stream playback;
237 struct fsi_stream capture;
248 struct fsi_stream_handler {
249 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
250 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
251 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev);
252 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
253 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
254 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
257 #define fsi_stream_handler_call(io, func, args...) \
259 !((io)->handler->func) ? 0 : \
260 (io)->handler->func(args))
275 struct fsi_priv fsia;
276 struct fsi_priv fsib;
277 struct fsi_core *core;
281 static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
284 * basic read write function
287 static void __fsi_reg_write(u32 __iomem *reg, u32 data)
289 /* valid data area is 24bit */
292 __raw_writel(data, reg);
295 static u32 __fsi_reg_read(u32 __iomem *reg)
297 return __raw_readl(reg);
300 static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
302 u32 val = __fsi_reg_read(reg);
307 __fsi_reg_write(reg, val);
310 #define fsi_reg_write(p, r, d)\
311 __fsi_reg_write((p->base + REG_##r), d)
313 #define fsi_reg_read(p, r)\
314 __fsi_reg_read((p->base + REG_##r))
316 #define fsi_reg_mask_set(p, r, m, d)\
317 __fsi_reg_mask_set((p->base + REG_##r), m, d)
319 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
320 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
321 static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
326 spin_lock_irqsave(&master->lock, flags);
327 ret = __fsi_reg_read(master->base + reg);
328 spin_unlock_irqrestore(&master->lock, flags);
333 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
334 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
335 static void _fsi_master_mask_set(struct fsi_master *master,
336 u32 reg, u32 mask, u32 data)
340 spin_lock_irqsave(&master->lock, flags);
341 __fsi_reg_mask_set(master->base + reg, mask, data);
342 spin_unlock_irqrestore(&master->lock, flags);
348 static int fsi_version(struct fsi_master *master)
350 return master->core->ver;
353 static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
358 static int fsi_is_clk_master(struct fsi_priv *fsi)
360 return fsi->clk_master;
363 static int fsi_is_port_a(struct fsi_priv *fsi)
365 return fsi->master->base == fsi->base;
368 static int fsi_is_spdif(struct fsi_priv *fsi)
373 static int fsi_is_play(struct snd_pcm_substream *substream)
375 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
378 static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
380 struct snd_soc_pcm_runtime *rtd = substream->private_data;
385 static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
387 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
390 return &master->fsia;
392 return &master->fsib;
395 static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
397 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
400 static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
405 return fsi->info->set_rate;
408 static u32 fsi_get_info_flags(struct fsi_priv *fsi)
413 return fsi->info->flags;
416 static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
418 int is_play = fsi_stream_is_play(fsi, io);
419 int is_porta = fsi_is_port_a(fsi);
423 shift = is_play ? AO_SHIFT : AI_SHIFT;
425 shift = is_play ? BO_SHIFT : BI_SHIFT;
430 static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
432 return frames * fsi->chan_num;
435 static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
437 return samples / fsi->chan_num;
440 static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
441 struct fsi_stream *io)
443 int is_play = fsi_stream_is_play(fsi, io);
448 fsi_reg_read(fsi, DOFF_ST) :
449 fsi_reg_read(fsi, DIFF_ST);
451 frames = 0x1ff & (status >> 8);
453 return fsi_frame2sample(fsi, frames);
456 static void fsi_count_fifo_err(struct fsi_priv *fsi)
458 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
459 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
461 if (ostatus & ERR_OVER)
462 fsi->playback.oerr_num++;
464 if (ostatus & ERR_UNDER)
465 fsi->playback.uerr_num++;
467 if (istatus & ERR_OVER)
468 fsi->capture.oerr_num++;
470 if (istatus & ERR_UNDER)
471 fsi->capture.uerr_num++;
473 fsi_reg_write(fsi, DOFF_ST, 0);
474 fsi_reg_write(fsi, DIFF_ST, 0);
478 * fsi_stream_xx() function
480 static inline int fsi_stream_is_play(struct fsi_priv *fsi,
481 struct fsi_stream *io)
483 return &fsi->playback == io;
486 static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
487 struct snd_pcm_substream *substream)
489 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
492 static int fsi_stream_is_working(struct fsi_priv *fsi,
493 struct fsi_stream *io)
495 struct fsi_master *master = fsi_get_master(fsi);
499 spin_lock_irqsave(&master->lock, flags);
500 ret = !!(io->substream && io->substream->runtime);
501 spin_unlock_irqrestore(&master->lock, flags);
506 static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
511 static void fsi_stream_init(struct fsi_priv *fsi,
512 struct fsi_stream *io,
513 struct snd_pcm_substream *substream)
515 struct snd_pcm_runtime *runtime = substream->runtime;
516 struct fsi_master *master = fsi_get_master(fsi);
519 spin_lock_irqsave(&master->lock, flags);
520 io->substream = substream;
521 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
522 io->buff_sample_pos = 0;
523 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
525 io->sample_width = samples_to_bytes(runtime, 1);
527 io->oerr_num = -1; /* ignore 1st err */
528 io->uerr_num = -1; /* ignore 1st err */
529 fsi_stream_handler_call(io, init, fsi, io);
530 spin_unlock_irqrestore(&master->lock, flags);
533 static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
535 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
536 struct fsi_master *master = fsi_get_master(fsi);
539 spin_lock_irqsave(&master->lock, flags);
541 if (io->oerr_num > 0)
542 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
544 if (io->uerr_num > 0)
545 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
547 fsi_stream_handler_call(io, quit, fsi, io);
548 io->substream = NULL;
549 io->buff_sample_capa = 0;
550 io->buff_sample_pos = 0;
551 io->period_samples = 0;
553 io->sample_width = 0;
557 spin_unlock_irqrestore(&master->lock, flags);
560 static int fsi_stream_transfer(struct fsi_stream *io)
562 struct fsi_priv *fsi = fsi_stream_to_priv(io);
566 return fsi_stream_handler_call(io, transfer, fsi, io);
569 #define fsi_stream_start(fsi, io)\
570 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
572 #define fsi_stream_stop(fsi, io)\
573 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
575 static int fsi_stream_probe(struct fsi_priv *fsi, struct device *dev)
577 struct fsi_stream *io;
581 ret1 = fsi_stream_handler_call(io, probe, fsi, io, dev);
584 ret2 = fsi_stream_handler_call(io, probe, fsi, io, dev);
594 static int fsi_stream_remove(struct fsi_priv *fsi)
596 struct fsi_stream *io;
600 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
603 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
614 * format/bus/dma setting
616 static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
617 u32 bus, struct device *dev)
619 struct fsi_master *master = fsi_get_master(fsi);
620 int is_play = fsi_stream_is_play(fsi, io);
623 if (fsi_version(master) >= 2) {
627 * FSI2 needs DMA/Bus setting
630 case PACKAGE_24BITBUS_FRONT:
633 dev_dbg(dev, "24bit bus / package in front\n");
635 case PACKAGE_16BITBUS_STREAM:
638 dev_dbg(dev, "16bit bus / stream mode\n");
640 case PACKAGE_24BITBUS_BACK:
644 dev_dbg(dev, "24bit bus / package in back\n");
649 fsi_reg_write(fsi, OUT_DMAC, dma);
651 fsi_reg_write(fsi, IN_DMAC, dma);
655 fsi_reg_write(fsi, DO_FMT, fmt);
657 fsi_reg_write(fsi, DI_FMT, fmt);
664 static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
666 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
667 struct fsi_master *master = fsi_get_master(fsi);
669 fsi_core_mask_set(master, imsk, data, data);
670 fsi_core_mask_set(master, iemsk, data, data);
673 static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
675 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
676 struct fsi_master *master = fsi_get_master(fsi);
678 fsi_core_mask_set(master, imsk, data, 0);
679 fsi_core_mask_set(master, iemsk, data, 0);
682 static u32 fsi_irq_get_status(struct fsi_master *master)
684 return fsi_core_read(master, int_st);
687 static void fsi_irq_clear_status(struct fsi_priv *fsi)
690 struct fsi_master *master = fsi_get_master(fsi);
692 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
693 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
695 /* clear interrupt factor */
696 fsi_core_mask_set(master, int_st, data, 0);
700 * SPDIF master clock function
702 * These functions are used later FSI2
704 static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
706 struct fsi_master *master = fsi_get_master(fsi);
710 val = enable ? mask : 0;
713 fsi_core_mask_set(master, a_mclk, mask, val) :
714 fsi_core_mask_set(master, b_mclk, mask, val);
720 static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
721 long rate, int enable)
723 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
729 ret = set_rate(dev, rate, enable);
730 if (ret < 0) /* error */
739 switch (ret & SH_FSI_ACKMD_MASK) {
742 case SH_FSI_ACKMD_512:
745 case SH_FSI_ACKMD_256:
748 case SH_FSI_ACKMD_128:
751 case SH_FSI_ACKMD_64:
754 case SH_FSI_ACKMD_32:
759 switch (ret & SH_FSI_BPFMD_MASK) {
762 case SH_FSI_BPFMD_32:
765 case SH_FSI_BPFMD_64:
768 case SH_FSI_BPFMD_128:
771 case SH_FSI_BPFMD_256:
774 case SH_FSI_BPFMD_512:
777 case SH_FSI_BPFMD_16:
782 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
791 * pio data transfer handler
793 static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
795 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
802 * fsi_pio_push_init()
804 u32 *buf = (u32 *)_buf;
806 for (i = 0; i < samples / 2; i++)
807 fsi_reg_write(fsi, DODT, buf[i]);
810 u16 *buf = (u16 *)_buf;
812 for (i = 0; i < samples; i++)
813 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
817 static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
819 u16 *buf = (u16 *)_buf;
822 for (i = 0; i < samples; i++)
823 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
826 static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
828 u32 *buf = (u32 *)_buf;
831 for (i = 0; i < samples; i++)
832 fsi_reg_write(fsi, DODT, *(buf + i));
835 static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
837 u32 *buf = (u32 *)_buf;
840 for (i = 0; i < samples; i++)
841 *(buf + i) = fsi_reg_read(fsi, DIDT);
844 static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
846 struct snd_pcm_runtime *runtime = io->substream->runtime;
848 return runtime->dma_area +
849 samples_to_bytes(runtime, io->buff_sample_pos);
852 static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
853 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
854 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
857 struct snd_pcm_runtime *runtime;
858 struct snd_pcm_substream *substream;
862 if (!fsi_stream_is_working(fsi, io))
866 substream = io->substream;
867 runtime = substream->runtime;
869 /* FSI FIFO has limit.
870 * So, this driver can not send periods data at a time
872 if (io->buff_sample_pos >=
873 io->period_samples * (io->period_pos + 1)) {
876 io->period_pos = (io->period_pos + 1) % runtime->periods;
878 if (0 == io->period_pos)
879 io->buff_sample_pos = 0;
882 buf = fsi_pio_get_area(fsi, io);
884 switch (io->sample_width) {
886 run16(fsi, buf, samples);
889 run32(fsi, buf, samples);
895 /* update buff_sample_pos */
896 io->buff_sample_pos += samples;
899 snd_pcm_period_elapsed(substream);
904 static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
906 int sample_residues; /* samples in FSI fifo */
907 int sample_space; /* ALSA free samples space */
910 sample_residues = fsi_get_current_fifo_samples(fsi, io);
911 sample_space = io->buff_sample_capa - io->buff_sample_pos;
913 samples = min(sample_residues, sample_space);
915 return fsi_pio_transfer(fsi, io,
921 static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
923 int sample_residues; /* ALSA residue samples */
924 int sample_space; /* FSI fifo free samples space */
927 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
928 sample_space = io->fifo_sample_capa -
929 fsi_get_current_fifo_samples(fsi, io);
931 samples = min(sample_residues, sample_space);
933 return fsi_pio_transfer(fsi, io,
939 static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
942 struct fsi_master *master = fsi_get_master(fsi);
943 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
946 fsi_irq_enable(fsi, io);
948 fsi_irq_disable(fsi, io);
950 if (fsi_is_clk_master(fsi))
951 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
954 static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
956 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
959 * we can use 16bit stream mode
960 * when "playback" and "16bit data"
961 * and platform allows "stream mode"
966 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
967 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
969 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
970 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
974 static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
977 * always 24bit bus, package back when "capture"
979 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
980 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
984 static struct fsi_stream_handler fsi_pio_push_handler = {
985 .init = fsi_pio_push_init,
986 .transfer = fsi_pio_push,
987 .start_stop = fsi_pio_start_stop,
990 static struct fsi_stream_handler fsi_pio_pop_handler = {
991 .init = fsi_pio_pop_init,
992 .transfer = fsi_pio_pop,
993 .start_stop = fsi_pio_start_stop,
996 static irqreturn_t fsi_interrupt(int irq, void *data)
998 struct fsi_master *master = data;
999 u32 int_st = fsi_irq_get_status(master);
1001 /* clear irq status */
1002 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1003 fsi_master_mask_set(master, SOFT_RST, IR, IR);
1005 if (int_st & AB_IO(1, AO_SHIFT))
1006 fsi_stream_transfer(&master->fsia.playback);
1007 if (int_st & AB_IO(1, BO_SHIFT))
1008 fsi_stream_transfer(&master->fsib.playback);
1009 if (int_st & AB_IO(1, AI_SHIFT))
1010 fsi_stream_transfer(&master->fsia.capture);
1011 if (int_st & AB_IO(1, BI_SHIFT))
1012 fsi_stream_transfer(&master->fsib.capture);
1014 fsi_count_fifo_err(&master->fsia);
1015 fsi_count_fifo_err(&master->fsib);
1017 fsi_irq_clear_status(&master->fsia);
1018 fsi_irq_clear_status(&master->fsib);
1024 * dma data transfer handler
1026 static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1028 struct snd_pcm_runtime *runtime = io->substream->runtime;
1029 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1030 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1031 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1034 * 24bit data : 24bit bus / package in back
1035 * 16bit data : 16bit bus / stream mode
1037 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1038 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1040 io->dma = dma_map_single(dai->dev, runtime->dma_area,
1041 snd_pcm_lib_buffer_bytes(io->substream), dir);
1045 static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
1047 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1048 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1049 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1051 dma_unmap_single(dai->dev, io->dma,
1052 snd_pcm_lib_buffer_bytes(io->substream), dir);
1056 static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
1058 struct snd_pcm_runtime *runtime = io->substream->runtime;
1060 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
1063 static void fsi_dma_complete(void *data)
1065 struct fsi_stream *io = (struct fsi_stream *)data;
1066 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1067 struct snd_pcm_runtime *runtime = io->substream->runtime;
1068 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1069 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1070 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1072 dma_sync_single_for_cpu(dai->dev, fsi_dma_get_area(io),
1073 samples_to_bytes(runtime, io->period_samples), dir);
1075 io->buff_sample_pos += io->period_samples;
1078 if (io->period_pos >= runtime->periods) {
1080 io->buff_sample_pos = 0;
1083 fsi_count_fifo_err(fsi);
1084 fsi_stream_transfer(io);
1086 snd_pcm_period_elapsed(io->substream);
1089 static void fsi_dma_do_work(struct work_struct *work)
1091 struct fsi_stream *io = container_of(work, struct fsi_stream, work);
1092 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1093 struct snd_soc_dai *dai;
1094 struct dma_async_tx_descriptor *desc;
1095 struct snd_pcm_runtime *runtime;
1096 enum dma_data_direction dir;
1097 int is_play = fsi_stream_is_play(fsi, io);
1101 if (!fsi_stream_is_working(fsi, io))
1104 dai = fsi_get_dai(io->substream);
1105 runtime = io->substream->runtime;
1106 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1107 len = samples_to_bytes(runtime, io->period_samples);
1108 buf = fsi_dma_get_area(io);
1110 dma_sync_single_for_device(dai->dev, buf, len, dir);
1112 desc = dmaengine_prep_slave_single(io->chan, buf, len, dir,
1113 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1115 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
1119 desc->callback = fsi_dma_complete;
1120 desc->callback_param = io;
1122 if (dmaengine_submit(desc) < 0) {
1123 dev_err(dai->dev, "tx_submit() fail\n");
1127 dma_async_issue_pending(io->chan);
1132 * In DMAEngine case, codec and FSI cannot be started simultaneously
1133 * since FSI is using the scheduler work queue.
1134 * Therefore, in capture case, probably FSI FIFO will have got
1135 * overflow error in this point.
1136 * in that case, DMA cannot start transfer until error was cleared.
1139 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1140 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1141 fsi_reg_write(fsi, DIFF_ST, 0);
1146 static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1148 struct sh_dmae_slave *slave = param;
1150 chan->private = slave;
1155 static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1157 schedule_work(&io->work);
1162 static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1165 struct fsi_master *master = fsi_get_master(fsi);
1166 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
1167 u32 enable = start ? DMA_ON : 0;
1169 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
1171 dmaengine_terminate_all(io->chan);
1173 if (fsi_is_clk_master(fsi))
1174 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
1177 static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev)
1179 dma_cap_mask_t mask;
1182 dma_cap_set(DMA_SLAVE, mask);
1184 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
1187 /* switch to PIO handler */
1188 if (fsi_stream_is_play(fsi, io))
1189 fsi->playback.handler = &fsi_pio_push_handler;
1191 fsi->capture.handler = &fsi_pio_pop_handler;
1193 dev_info(dev, "switch handler (dma => pio)\n");
1196 return fsi_stream_probe(fsi, dev);
1199 INIT_WORK(&io->work, fsi_dma_do_work);
1204 static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1206 cancel_work_sync(&io->work);
1208 fsi_stream_stop(fsi, io);
1211 dma_release_channel(io->chan);
1217 static struct fsi_stream_handler fsi_dma_push_handler = {
1218 .init = fsi_dma_init,
1219 .quit = fsi_dma_quit,
1220 .probe = fsi_dma_probe,
1221 .transfer = fsi_dma_transfer,
1222 .remove = fsi_dma_remove,
1223 .start_stop = fsi_dma_push_start_stop,
1229 static void fsi_fifo_init(struct fsi_priv *fsi,
1230 struct fsi_stream *io,
1233 struct fsi_master *master = fsi_get_master(fsi);
1234 int is_play = fsi_stream_is_play(fsi, io);
1238 /* get on-chip RAM capacity */
1239 shift = fsi_master_read(master, FIFO_SZ);
1240 shift >>= fsi_get_port_shift(fsi, io);
1241 shift &= FIFO_SZ_MASK;
1242 frame_capa = 256 << shift;
1243 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1246 * The maximum number of sample data varies depending
1247 * on the number of channels selected for the format.
1249 * FIFOs are used in 4-channel units in 3-channel mode
1250 * and in 8-channel units in 5- to 7-channel mode
1251 * meaning that more FIFOs than the required size of DPRAM
1254 * ex) if 256 words of DP-RAM is connected
1255 * 1 channel: 256 (256 x 1 = 256)
1256 * 2 channels: 128 (128 x 2 = 256)
1257 * 3 channels: 64 ( 64 x 3 = 192)
1258 * 4 channels: 64 ( 64 x 4 = 256)
1259 * 5 channels: 32 ( 32 x 5 = 160)
1260 * 6 channels: 32 ( 32 x 6 = 192)
1261 * 7 channels: 32 ( 32 x 7 = 224)
1262 * 8 channels: 32 ( 32 x 8 = 256)
1264 for (i = 1; i < fsi->chan_num; i <<= 1)
1266 dev_dbg(dev, "%d channel %d store\n",
1267 fsi->chan_num, frame_capa);
1269 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1272 * set interrupt generation factor
1276 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1277 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1279 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1280 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1284 static int fsi_hw_startup(struct fsi_priv *fsi,
1285 struct fsi_stream *io,
1288 u32 flags = fsi_get_info_flags(fsi);
1292 if (fsi_is_clk_master(fsi))
1295 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
1297 /* clock inversion (CKG2) */
1299 if (SH_FSI_LRM_INV & flags)
1301 if (SH_FSI_BRM_INV & flags)
1303 if (SH_FSI_LRS_INV & flags)
1305 if (SH_FSI_BRS_INV & flags)
1308 fsi_reg_write(fsi, CKG2, data);
1311 if (fsi_is_spdif(fsi)) {
1312 fsi_spdif_clk_ctrl(fsi, 1);
1313 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1320 switch (io->sample_width) {
1322 data = BUSOP_GET(16, io->bus_option);
1325 data = BUSOP_GET(24, io->bus_option);
1328 fsi_format_bus_setup(fsi, io, data, dev);
1331 fsi_irq_disable(fsi, io);
1332 fsi_irq_clear_status(fsi);
1335 fsi_fifo_init(fsi, io, dev);
1340 static void fsi_hw_shutdown(struct fsi_priv *fsi,
1343 if (fsi_is_clk_master(fsi))
1344 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
1347 static int fsi_dai_startup(struct snd_pcm_substream *substream,
1348 struct snd_soc_dai *dai)
1350 struct fsi_priv *fsi = fsi_get_priv(substream);
1357 static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1358 struct snd_soc_dai *dai)
1360 struct fsi_priv *fsi = fsi_get_priv(substream);
1365 static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1366 struct snd_soc_dai *dai)
1368 struct fsi_priv *fsi = fsi_get_priv(substream);
1369 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1373 case SNDRV_PCM_TRIGGER_START:
1374 fsi_stream_init(fsi, io, substream);
1375 fsi_hw_startup(fsi, io, dai->dev);
1376 ret = fsi_stream_transfer(io);
1378 fsi_stream_start(fsi, io);
1380 case SNDRV_PCM_TRIGGER_STOP:
1381 fsi_hw_shutdown(fsi, dai->dev);
1382 fsi_stream_stop(fsi, io);
1383 fsi_stream_quit(fsi, io);
1390 static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1392 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1393 case SND_SOC_DAIFMT_I2S:
1397 case SND_SOC_DAIFMT_LEFT_J:
1408 static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1410 struct fsi_master *master = fsi_get_master(fsi);
1412 if (fsi_version(master) < 2)
1415 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
1422 static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1424 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
1425 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
1426 u32 flags = fsi_get_info_flags(fsi);
1429 /* set master/slave audio interface */
1430 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1431 case SND_SOC_DAIFMT_CBM_CFM:
1432 fsi->clk_master = 1;
1434 case SND_SOC_DAIFMT_CBS_CFS:
1440 if (fsi_is_clk_master(fsi) && !set_rate) {
1441 dev_err(dai->dev, "platform doesn't have set_rate\n");
1446 switch (flags & SH_FSI_FMT_MASK) {
1447 case SH_FSI_FMT_DAI:
1448 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1450 case SH_FSI_FMT_SPDIF:
1451 ret = fsi_set_fmt_spdif(fsi);
1460 static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1461 struct snd_pcm_hw_params *params,
1462 struct snd_soc_dai *dai)
1464 struct fsi_priv *fsi = fsi_get_priv(substream);
1465 long rate = params_rate(params);
1468 if (!fsi_is_clk_master(fsi))
1471 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1480 static const struct snd_soc_dai_ops fsi_dai_ops = {
1481 .startup = fsi_dai_startup,
1482 .shutdown = fsi_dai_shutdown,
1483 .trigger = fsi_dai_trigger,
1484 .set_fmt = fsi_dai_set_fmt,
1485 .hw_params = fsi_dai_hw_params,
1492 static struct snd_pcm_hardware fsi_pcm_hardware = {
1493 .info = SNDRV_PCM_INFO_INTERLEAVED |
1494 SNDRV_PCM_INFO_MMAP |
1495 SNDRV_PCM_INFO_MMAP_VALID |
1496 SNDRV_PCM_INFO_PAUSE,
1497 .formats = FSI_FMTS,
1503 .buffer_bytes_max = 64 * 1024,
1504 .period_bytes_min = 32,
1505 .period_bytes_max = 8192,
1511 static int fsi_pcm_open(struct snd_pcm_substream *substream)
1513 struct snd_pcm_runtime *runtime = substream->runtime;
1516 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1518 ret = snd_pcm_hw_constraint_integer(runtime,
1519 SNDRV_PCM_HW_PARAM_PERIODS);
1524 static int fsi_hw_params(struct snd_pcm_substream *substream,
1525 struct snd_pcm_hw_params *hw_params)
1527 return snd_pcm_lib_malloc_pages(substream,
1528 params_buffer_bytes(hw_params));
1531 static int fsi_hw_free(struct snd_pcm_substream *substream)
1533 return snd_pcm_lib_free_pages(substream);
1536 static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1538 struct fsi_priv *fsi = fsi_get_priv(substream);
1539 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1541 return fsi_sample2frame(fsi, io->buff_sample_pos);
1544 static struct snd_pcm_ops fsi_pcm_ops = {
1545 .open = fsi_pcm_open,
1546 .ioctl = snd_pcm_lib_ioctl,
1547 .hw_params = fsi_hw_params,
1548 .hw_free = fsi_hw_free,
1549 .pointer = fsi_pointer,
1556 #define PREALLOC_BUFFER (32 * 1024)
1557 #define PREALLOC_BUFFER_MAX (32 * 1024)
1559 static void fsi_pcm_free(struct snd_pcm *pcm)
1561 snd_pcm_lib_preallocate_free_for_all(pcm);
1564 static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
1566 struct snd_pcm *pcm = rtd->pcm;
1569 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1570 * in MMAP mode (i.e. aplay -M)
1572 return snd_pcm_lib_preallocate_pages_for_all(
1574 SNDRV_DMA_TYPE_CONTINUOUS,
1575 snd_dma_continuous_data(GFP_KERNEL),
1576 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1583 static struct snd_soc_dai_driver fsi_soc_dai[] = {
1588 .formats = FSI_FMTS,
1594 .formats = FSI_FMTS,
1598 .ops = &fsi_dai_ops,
1604 .formats = FSI_FMTS,
1610 .formats = FSI_FMTS,
1614 .ops = &fsi_dai_ops,
1618 static struct snd_soc_platform_driver fsi_soc_platform = {
1619 .ops = &fsi_pcm_ops,
1620 .pcm_new = fsi_pcm_new,
1621 .pcm_free = fsi_pcm_free,
1627 static void fsi_handler_init(struct fsi_priv *fsi)
1629 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1630 fsi->playback.priv = fsi;
1631 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1632 fsi->capture.priv = fsi;
1634 if (fsi->info->tx_id) {
1635 fsi->playback.slave.shdma_slave.slave_id = fsi->info->tx_id;
1636 fsi->playback.handler = &fsi_dma_push_handler;
1640 static int fsi_probe(struct platform_device *pdev)
1642 struct fsi_master *master;
1643 const struct platform_device_id *id_entry;
1644 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
1645 struct resource *res;
1649 id_entry = pdev->id_entry;
1651 dev_err(&pdev->dev, "unknown fsi device\n");
1655 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1656 irq = platform_get_irq(pdev, 0);
1657 if (!res || (int)irq <= 0) {
1658 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1662 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
1664 dev_err(&pdev->dev, "Could not allocate master\n");
1668 master->base = devm_ioremap_nocache(&pdev->dev,
1669 res->start, resource_size(res));
1670 if (!master->base) {
1671 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1675 /* master setting */
1677 master->core = (struct fsi_core *)id_entry->driver_data;
1678 spin_lock_init(&master->lock);
1681 master->fsia.base = master->base;
1682 master->fsia.master = master;
1683 master->fsia.info = &info->port_a;
1684 fsi_handler_init(&master->fsia);
1685 ret = fsi_stream_probe(&master->fsia, &pdev->dev);
1687 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1692 master->fsib.base = master->base + 0x40;
1693 master->fsib.master = master;
1694 master->fsib.info = &info->port_b;
1695 fsi_handler_init(&master->fsib);
1696 ret = fsi_stream_probe(&master->fsib, &pdev->dev);
1698 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1702 pm_runtime_enable(&pdev->dev);
1703 dev_set_drvdata(&pdev->dev, master);
1705 ret = request_irq(irq, &fsi_interrupt, 0,
1706 id_entry->name, master);
1708 dev_err(&pdev->dev, "irq request err\n");
1712 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
1714 dev_err(&pdev->dev, "cannot snd soc register\n");
1718 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1719 ARRAY_SIZE(fsi_soc_dai));
1721 dev_err(&pdev->dev, "cannot snd dai register\n");
1728 snd_soc_unregister_platform(&pdev->dev);
1730 free_irq(irq, master);
1732 pm_runtime_disable(&pdev->dev);
1733 fsi_stream_remove(&master->fsib);
1735 fsi_stream_remove(&master->fsia);
1740 static int fsi_remove(struct platform_device *pdev)
1742 struct fsi_master *master;
1744 master = dev_get_drvdata(&pdev->dev);
1746 free_irq(master->irq, master);
1747 pm_runtime_disable(&pdev->dev);
1749 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1750 snd_soc_unregister_platform(&pdev->dev);
1752 fsi_stream_remove(&master->fsia);
1753 fsi_stream_remove(&master->fsib);
1758 static void __fsi_suspend(struct fsi_priv *fsi,
1759 struct fsi_stream *io,
1762 if (!fsi_stream_is_working(fsi, io))
1765 fsi_stream_stop(fsi, io);
1766 fsi_hw_shutdown(fsi, dev);
1769 static void __fsi_resume(struct fsi_priv *fsi,
1770 struct fsi_stream *io,
1773 if (!fsi_stream_is_working(fsi, io))
1776 fsi_hw_startup(fsi, io, dev);
1778 if (fsi_is_clk_master(fsi) && fsi->rate)
1779 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
1781 fsi_stream_start(fsi, io);
1784 static int fsi_suspend(struct device *dev)
1786 struct fsi_master *master = dev_get_drvdata(dev);
1787 struct fsi_priv *fsia = &master->fsia;
1788 struct fsi_priv *fsib = &master->fsib;
1790 __fsi_suspend(fsia, &fsia->playback, dev);
1791 __fsi_suspend(fsia, &fsia->capture, dev);
1793 __fsi_suspend(fsib, &fsib->playback, dev);
1794 __fsi_suspend(fsib, &fsib->capture, dev);
1799 static int fsi_resume(struct device *dev)
1801 struct fsi_master *master = dev_get_drvdata(dev);
1802 struct fsi_priv *fsia = &master->fsia;
1803 struct fsi_priv *fsib = &master->fsib;
1805 __fsi_resume(fsia, &fsia->playback, dev);
1806 __fsi_resume(fsia, &fsia->capture, dev);
1808 __fsi_resume(fsib, &fsib->playback, dev);
1809 __fsi_resume(fsib, &fsib->capture, dev);
1814 static struct dev_pm_ops fsi_pm_ops = {
1815 .suspend = fsi_suspend,
1816 .resume = fsi_resume,
1819 static struct fsi_core fsi1_core = {
1828 static struct fsi_core fsi2_core = {
1832 .int_st = CPU_INT_ST,
1835 .a_mclk = A_MST_CTLR,
1836 .b_mclk = B_MST_CTLR,
1839 static struct platform_device_id fsi_id_table[] = {
1840 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1841 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
1844 MODULE_DEVICE_TABLE(platform, fsi_id_table);
1846 static struct platform_driver fsi_driver = {
1848 .name = "fsi-pcm-audio",
1852 .remove = fsi_remove,
1853 .id_table = fsi_id_table,
1856 module_platform_driver(fsi_driver);
1858 MODULE_LICENSE("GPL");
1859 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1860 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1861 MODULE_ALIAS("platform:fsi-pcm-audio");