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);
139 * A : sample widtht 16bit setting
140 * B : sample widtht 24bit setting
143 #define SHIFT_16DATA 0
144 #define SHIFT_24DATA 4
146 #define PACKAGE_24BITBUS_BACK 0
147 #define PACKAGE_24BITBUS_FRONT 1
148 #define PACKAGE_16BITBUS_STREAM 2
150 #define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
151 #define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
154 * FSI driver use below type name for variable
156 * xxx_num : number of data
157 * xxx_pos : position of data
158 * xxx_capa : capacity of data
162 * period/frame/sample image
166 * period pos period pos
168 * |<-------------------- period--------------------->|
169 * ==|============================================ ... =|==
171 * ||<----- frame ----->|<------ frame ----->| ... |
172 * |+--------------------+--------------------+- ... |
173 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
174 * |+--------------------+--------------------+- ... |
175 * ==|============================================ ... =|==
194 struct fsi_stream_handler;
198 * these are initialized by fsi_stream_init()
200 struct snd_pcm_substream *substream;
201 int fifo_sample_capa; /* sample capacity of FSI FIFO */
202 int buff_sample_capa; /* sample capacity of ALSA buffer */
203 int buff_sample_pos; /* sample position of ALSA buffer */
204 int period_samples; /* sample number / 1 period */
205 int period_pos; /* current period position */
206 int sample_width; /* sample width */
216 * thse are initialized by fsi_handler_init()
218 struct fsi_stream_handler *handler;
219 struct fsi_priv *priv;
222 * these are for DMAEngine
224 struct dma_chan *chan;
225 struct sh_dmae_slave slave; /* see fsi_handler_init() */
226 struct tasklet_struct tasklet;
232 struct fsi_master *master;
233 struct sh_fsi_port_info *info;
235 struct fsi_stream playback;
236 struct fsi_stream capture;
247 struct fsi_stream_handler {
248 int (*init)(struct fsi_priv *fsi, struct fsi_stream *io);
249 int (*quit)(struct fsi_priv *fsi, struct fsi_stream *io);
250 int (*probe)(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev);
251 int (*transfer)(struct fsi_priv *fsi, struct fsi_stream *io);
252 int (*remove)(struct fsi_priv *fsi, struct fsi_stream *io);
253 void (*start_stop)(struct fsi_priv *fsi, struct fsi_stream *io,
256 #define fsi_stream_handler_call(io, func, args...) \
258 !((io)->handler->func) ? 0 : \
259 (io)->handler->func(args))
274 struct fsi_priv fsia;
275 struct fsi_priv fsib;
276 struct fsi_core *core;
280 static int fsi_stream_is_play(struct fsi_priv *fsi, struct fsi_stream *io);
283 * basic read write function
286 static void __fsi_reg_write(u32 __iomem *reg, u32 data)
288 /* valid data area is 24bit */
291 __raw_writel(data, reg);
294 static u32 __fsi_reg_read(u32 __iomem *reg)
296 return __raw_readl(reg);
299 static void __fsi_reg_mask_set(u32 __iomem *reg, u32 mask, u32 data)
301 u32 val = __fsi_reg_read(reg);
306 __fsi_reg_write(reg, val);
309 #define fsi_reg_write(p, r, d)\
310 __fsi_reg_write((p->base + REG_##r), d)
312 #define fsi_reg_read(p, r)\
313 __fsi_reg_read((p->base + REG_##r))
315 #define fsi_reg_mask_set(p, r, m, d)\
316 __fsi_reg_mask_set((p->base + REG_##r), m, d)
318 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
319 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
320 static u32 _fsi_master_read(struct fsi_master *master, u32 reg)
325 spin_lock_irqsave(&master->lock, flags);
326 ret = __fsi_reg_read(master->base + reg);
327 spin_unlock_irqrestore(&master->lock, flags);
332 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
333 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
334 static void _fsi_master_mask_set(struct fsi_master *master,
335 u32 reg, u32 mask, u32 data)
339 spin_lock_irqsave(&master->lock, flags);
340 __fsi_reg_mask_set(master->base + reg, mask, data);
341 spin_unlock_irqrestore(&master->lock, flags);
347 static int fsi_version(struct fsi_master *master)
349 return master->core->ver;
352 static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
357 static int fsi_is_clk_master(struct fsi_priv *fsi)
359 return fsi->clk_master;
362 static int fsi_is_port_a(struct fsi_priv *fsi)
364 return fsi->master->base == fsi->base;
367 static int fsi_is_spdif(struct fsi_priv *fsi)
372 static int fsi_is_play(struct snd_pcm_substream *substream)
374 return substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
377 static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
379 struct snd_soc_pcm_runtime *rtd = substream->private_data;
384 static struct fsi_priv *fsi_get_priv_frm_dai(struct snd_soc_dai *dai)
386 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
389 return &master->fsia;
391 return &master->fsib;
394 static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
396 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
399 static set_rate_func fsi_get_info_set_rate(struct fsi_priv *fsi)
404 return fsi->info->set_rate;
407 static u32 fsi_get_info_flags(struct fsi_priv *fsi)
412 return fsi->info->flags;
415 static u32 fsi_get_port_shift(struct fsi_priv *fsi, struct fsi_stream *io)
417 int is_play = fsi_stream_is_play(fsi, io);
418 int is_porta = fsi_is_port_a(fsi);
422 shift = is_play ? AO_SHIFT : AI_SHIFT;
424 shift = is_play ? BO_SHIFT : BI_SHIFT;
429 static int fsi_frame2sample(struct fsi_priv *fsi, int frames)
431 return frames * fsi->chan_num;
434 static int fsi_sample2frame(struct fsi_priv *fsi, int samples)
436 return samples / fsi->chan_num;
439 static int fsi_get_current_fifo_samples(struct fsi_priv *fsi,
440 struct fsi_stream *io)
442 int is_play = fsi_stream_is_play(fsi, io);
447 fsi_reg_read(fsi, DOFF_ST) :
448 fsi_reg_read(fsi, DIFF_ST);
450 frames = 0x1ff & (status >> 8);
452 return fsi_frame2sample(fsi, frames);
455 static void fsi_count_fifo_err(struct fsi_priv *fsi)
457 u32 ostatus = fsi_reg_read(fsi, DOFF_ST);
458 u32 istatus = fsi_reg_read(fsi, DIFF_ST);
460 if (ostatus & ERR_OVER)
461 fsi->playback.oerr_num++;
463 if (ostatus & ERR_UNDER)
464 fsi->playback.uerr_num++;
466 if (istatus & ERR_OVER)
467 fsi->capture.oerr_num++;
469 if (istatus & ERR_UNDER)
470 fsi->capture.uerr_num++;
472 fsi_reg_write(fsi, DOFF_ST, 0);
473 fsi_reg_write(fsi, DIFF_ST, 0);
477 * fsi_stream_xx() function
479 static inline int fsi_stream_is_play(struct fsi_priv *fsi,
480 struct fsi_stream *io)
482 return &fsi->playback == io;
485 static inline struct fsi_stream *fsi_stream_get(struct fsi_priv *fsi,
486 struct snd_pcm_substream *substream)
488 return fsi_is_play(substream) ? &fsi->playback : &fsi->capture;
491 static int fsi_stream_is_working(struct fsi_priv *fsi,
492 struct fsi_stream *io)
494 struct fsi_master *master = fsi_get_master(fsi);
498 spin_lock_irqsave(&master->lock, flags);
499 ret = !!(io->substream && io->substream->runtime);
500 spin_unlock_irqrestore(&master->lock, flags);
505 static struct fsi_priv *fsi_stream_to_priv(struct fsi_stream *io)
510 static void fsi_stream_init(struct fsi_priv *fsi,
511 struct fsi_stream *io,
512 struct snd_pcm_substream *substream)
514 struct snd_pcm_runtime *runtime = substream->runtime;
515 struct fsi_master *master = fsi_get_master(fsi);
518 spin_lock_irqsave(&master->lock, flags);
519 io->substream = substream;
520 io->buff_sample_capa = fsi_frame2sample(fsi, runtime->buffer_size);
521 io->buff_sample_pos = 0;
522 io->period_samples = fsi_frame2sample(fsi, runtime->period_size);
524 io->sample_width = samples_to_bytes(runtime, 1);
526 io->oerr_num = -1; /* ignore 1st err */
527 io->uerr_num = -1; /* ignore 1st err */
528 fsi_stream_handler_call(io, init, fsi, io);
529 spin_unlock_irqrestore(&master->lock, flags);
532 static void fsi_stream_quit(struct fsi_priv *fsi, struct fsi_stream *io)
534 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
535 struct fsi_master *master = fsi_get_master(fsi);
538 spin_lock_irqsave(&master->lock, flags);
540 if (io->oerr_num > 0)
541 dev_err(dai->dev, "over_run = %d\n", io->oerr_num);
543 if (io->uerr_num > 0)
544 dev_err(dai->dev, "under_run = %d\n", io->uerr_num);
546 fsi_stream_handler_call(io, quit, fsi, io);
547 io->substream = NULL;
548 io->buff_sample_capa = 0;
549 io->buff_sample_pos = 0;
550 io->period_samples = 0;
552 io->sample_width = 0;
556 spin_unlock_irqrestore(&master->lock, flags);
559 static int fsi_stream_transfer(struct fsi_stream *io)
561 struct fsi_priv *fsi = fsi_stream_to_priv(io);
565 return fsi_stream_handler_call(io, transfer, fsi, io);
568 #define fsi_stream_start(fsi, io)\
569 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
571 #define fsi_stream_stop(fsi, io)\
572 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
574 static int fsi_stream_probe(struct fsi_priv *fsi, struct device *dev)
576 struct fsi_stream *io;
580 ret1 = fsi_stream_handler_call(io, probe, fsi, io, dev);
583 ret2 = fsi_stream_handler_call(io, probe, fsi, io, dev);
593 static int fsi_stream_remove(struct fsi_priv *fsi)
595 struct fsi_stream *io;
599 ret1 = fsi_stream_handler_call(io, remove, fsi, io);
602 ret2 = fsi_stream_handler_call(io, remove, fsi, io);
613 * format/bus/dma setting
615 static void fsi_format_bus_setup(struct fsi_priv *fsi, struct fsi_stream *io,
616 u32 bus, struct device *dev)
618 struct fsi_master *master = fsi_get_master(fsi);
619 int is_play = fsi_stream_is_play(fsi, io);
622 if (fsi_version(master) >= 2) {
626 * FSI2 needs DMA/Bus setting
629 case PACKAGE_24BITBUS_FRONT:
632 dev_dbg(dev, "24bit bus / package in front\n");
634 case PACKAGE_16BITBUS_STREAM:
637 dev_dbg(dev, "16bit bus / stream mode\n");
639 case PACKAGE_24BITBUS_BACK:
643 dev_dbg(dev, "24bit bus / package in back\n");
648 fsi_reg_write(fsi, OUT_DMAC, dma);
650 fsi_reg_write(fsi, IN_DMAC, dma);
654 fsi_reg_write(fsi, DO_FMT, fmt);
656 fsi_reg_write(fsi, DI_FMT, fmt);
663 static void fsi_irq_enable(struct fsi_priv *fsi, struct fsi_stream *io)
665 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
666 struct fsi_master *master = fsi_get_master(fsi);
668 fsi_core_mask_set(master, imsk, data, data);
669 fsi_core_mask_set(master, iemsk, data, data);
672 static void fsi_irq_disable(struct fsi_priv *fsi, struct fsi_stream *io)
674 u32 data = AB_IO(1, fsi_get_port_shift(fsi, io));
675 struct fsi_master *master = fsi_get_master(fsi);
677 fsi_core_mask_set(master, imsk, data, 0);
678 fsi_core_mask_set(master, iemsk, data, 0);
681 static u32 fsi_irq_get_status(struct fsi_master *master)
683 return fsi_core_read(master, int_st);
686 static void fsi_irq_clear_status(struct fsi_priv *fsi)
689 struct fsi_master *master = fsi_get_master(fsi);
691 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->playback));
692 data |= AB_IO(1, fsi_get_port_shift(fsi, &fsi->capture));
694 /* clear interrupt factor */
695 fsi_core_mask_set(master, int_st, data, 0);
699 * SPDIF master clock function
701 * These functions are used later FSI2
703 static void fsi_spdif_clk_ctrl(struct fsi_priv *fsi, int enable)
705 struct fsi_master *master = fsi_get_master(fsi);
709 val = enable ? mask : 0;
712 fsi_core_mask_set(master, a_mclk, mask, val) :
713 fsi_core_mask_set(master, b_mclk, mask, val);
719 static int fsi_set_master_clk(struct device *dev, struct fsi_priv *fsi,
720 long rate, int enable)
722 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
728 ret = set_rate(dev, rate, enable);
729 if (ret < 0) /* error */
738 switch (ret & SH_FSI_ACKMD_MASK) {
741 case SH_FSI_ACKMD_512:
744 case SH_FSI_ACKMD_256:
747 case SH_FSI_ACKMD_128:
750 case SH_FSI_ACKMD_64:
753 case SH_FSI_ACKMD_32:
758 switch (ret & SH_FSI_BPFMD_MASK) {
761 case SH_FSI_BPFMD_32:
764 case SH_FSI_BPFMD_64:
767 case SH_FSI_BPFMD_128:
770 case SH_FSI_BPFMD_256:
773 case SH_FSI_BPFMD_512:
776 case SH_FSI_BPFMD_16:
781 fsi_reg_mask_set(fsi, CKG1, (ACKMD_MASK | BPFMD_MASK) , data);
790 * pio data transfer handler
792 static void fsi_pio_push16(struct fsi_priv *fsi, u8 *_buf, int samples)
794 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
801 * fsi_pio_push_init()
803 u32 *buf = (u32 *)_buf;
805 for (i = 0; i < samples / 2; i++)
806 fsi_reg_write(fsi, DODT, buf[i]);
809 u16 *buf = (u16 *)_buf;
811 for (i = 0; i < samples; i++)
812 fsi_reg_write(fsi, DODT, ((u32)*(buf + i) << 8));
816 static void fsi_pio_pop16(struct fsi_priv *fsi, u8 *_buf, int samples)
818 u16 *buf = (u16 *)_buf;
821 for (i = 0; i < samples; i++)
822 *(buf + i) = (u16)(fsi_reg_read(fsi, DIDT) >> 8);
825 static void fsi_pio_push32(struct fsi_priv *fsi, u8 *_buf, int samples)
827 u32 *buf = (u32 *)_buf;
830 for (i = 0; i < samples; i++)
831 fsi_reg_write(fsi, DODT, *(buf + i));
834 static void fsi_pio_pop32(struct fsi_priv *fsi, u8 *_buf, int samples)
836 u32 *buf = (u32 *)_buf;
839 for (i = 0; i < samples; i++)
840 *(buf + i) = fsi_reg_read(fsi, DIDT);
843 static u8 *fsi_pio_get_area(struct fsi_priv *fsi, struct fsi_stream *io)
845 struct snd_pcm_runtime *runtime = io->substream->runtime;
847 return runtime->dma_area +
848 samples_to_bytes(runtime, io->buff_sample_pos);
851 static int fsi_pio_transfer(struct fsi_priv *fsi, struct fsi_stream *io,
852 void (*run16)(struct fsi_priv *fsi, u8 *buf, int samples),
853 void (*run32)(struct fsi_priv *fsi, u8 *buf, int samples),
856 struct snd_pcm_runtime *runtime;
857 struct snd_pcm_substream *substream;
861 if (!fsi_stream_is_working(fsi, io))
865 substream = io->substream;
866 runtime = substream->runtime;
868 /* FSI FIFO has limit.
869 * So, this driver can not send periods data at a time
871 if (io->buff_sample_pos >=
872 io->period_samples * (io->period_pos + 1)) {
875 io->period_pos = (io->period_pos + 1) % runtime->periods;
877 if (0 == io->period_pos)
878 io->buff_sample_pos = 0;
881 buf = fsi_pio_get_area(fsi, io);
883 switch (io->sample_width) {
885 run16(fsi, buf, samples);
888 run32(fsi, buf, samples);
894 /* update buff_sample_pos */
895 io->buff_sample_pos += samples;
898 snd_pcm_period_elapsed(substream);
903 static int fsi_pio_pop(struct fsi_priv *fsi, struct fsi_stream *io)
905 int sample_residues; /* samples in FSI fifo */
906 int sample_space; /* ALSA free samples space */
909 sample_residues = fsi_get_current_fifo_samples(fsi, io);
910 sample_space = io->buff_sample_capa - io->buff_sample_pos;
912 samples = min(sample_residues, sample_space);
914 return fsi_pio_transfer(fsi, io,
920 static int fsi_pio_push(struct fsi_priv *fsi, struct fsi_stream *io)
922 int sample_residues; /* ALSA residue samples */
923 int sample_space; /* FSI fifo free samples space */
926 sample_residues = io->buff_sample_capa - io->buff_sample_pos;
927 sample_space = io->fifo_sample_capa -
928 fsi_get_current_fifo_samples(fsi, io);
930 samples = min(sample_residues, sample_space);
932 return fsi_pio_transfer(fsi, io,
938 static void fsi_pio_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
941 struct fsi_master *master = fsi_get_master(fsi);
942 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
945 fsi_irq_enable(fsi, io);
947 fsi_irq_disable(fsi, io);
949 if (fsi_is_clk_master(fsi))
950 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
953 static int fsi_pio_push_init(struct fsi_priv *fsi, struct fsi_stream *io)
955 u32 enable_stream = fsi_get_info_flags(fsi) & SH_FSI_ENABLE_STREAM_MODE;
958 * we can use 16bit stream mode
959 * when "playback" and "16bit data"
960 * and platform allows "stream mode"
965 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
966 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
968 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
969 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
973 static int fsi_pio_pop_init(struct fsi_priv *fsi, struct fsi_stream *io)
976 * always 24bit bus, package back when "capture"
978 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
979 BUSOP_SET(16, PACKAGE_24BITBUS_BACK);
983 static struct fsi_stream_handler fsi_pio_push_handler = {
984 .init = fsi_pio_push_init,
985 .transfer = fsi_pio_push,
986 .start_stop = fsi_pio_start_stop,
989 static struct fsi_stream_handler fsi_pio_pop_handler = {
990 .init = fsi_pio_pop_init,
991 .transfer = fsi_pio_pop,
992 .start_stop = fsi_pio_start_stop,
995 static irqreturn_t fsi_interrupt(int irq, void *data)
997 struct fsi_master *master = data;
998 u32 int_st = fsi_irq_get_status(master);
1000 /* clear irq status */
1001 fsi_master_mask_set(master, SOFT_RST, IR, 0);
1002 fsi_master_mask_set(master, SOFT_RST, IR, IR);
1004 if (int_st & AB_IO(1, AO_SHIFT))
1005 fsi_stream_transfer(&master->fsia.playback);
1006 if (int_st & AB_IO(1, BO_SHIFT))
1007 fsi_stream_transfer(&master->fsib.playback);
1008 if (int_st & AB_IO(1, AI_SHIFT))
1009 fsi_stream_transfer(&master->fsia.capture);
1010 if (int_st & AB_IO(1, BI_SHIFT))
1011 fsi_stream_transfer(&master->fsib.capture);
1013 fsi_count_fifo_err(&master->fsia);
1014 fsi_count_fifo_err(&master->fsib);
1016 fsi_irq_clear_status(&master->fsia);
1017 fsi_irq_clear_status(&master->fsib);
1023 * dma data transfer handler
1025 static int fsi_dma_init(struct fsi_priv *fsi, struct fsi_stream *io)
1027 struct snd_pcm_runtime *runtime = io->substream->runtime;
1028 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1029 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1030 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1033 * 24bit data : 24bit bus / package in back
1034 * 16bit data : 16bit bus / stream mode
1036 io->bus_option = BUSOP_SET(24, PACKAGE_24BITBUS_BACK) |
1037 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM);
1039 io->dma = dma_map_single(dai->dev, runtime->dma_area,
1040 snd_pcm_lib_buffer_bytes(io->substream), dir);
1044 static int fsi_dma_quit(struct fsi_priv *fsi, struct fsi_stream *io)
1046 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1047 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1048 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1050 dma_unmap_single(dai->dev, io->dma,
1051 snd_pcm_lib_buffer_bytes(io->substream), dir);
1055 static dma_addr_t fsi_dma_get_area(struct fsi_stream *io)
1057 struct snd_pcm_runtime *runtime = io->substream->runtime;
1059 return io->dma + samples_to_bytes(runtime, io->buff_sample_pos);
1062 static void fsi_dma_complete(void *data)
1064 struct fsi_stream *io = (struct fsi_stream *)data;
1065 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1066 struct snd_pcm_runtime *runtime = io->substream->runtime;
1067 struct snd_soc_dai *dai = fsi_get_dai(io->substream);
1068 enum dma_data_direction dir = fsi_stream_is_play(fsi, io) ?
1069 DMA_TO_DEVICE : DMA_FROM_DEVICE;
1071 dma_sync_single_for_cpu(dai->dev, fsi_dma_get_area(io),
1072 samples_to_bytes(runtime, io->period_samples), dir);
1074 io->buff_sample_pos += io->period_samples;
1077 if (io->period_pos >= runtime->periods) {
1079 io->buff_sample_pos = 0;
1082 fsi_count_fifo_err(fsi);
1083 fsi_stream_transfer(io);
1085 snd_pcm_period_elapsed(io->substream);
1088 static void fsi_dma_do_tasklet(unsigned long data)
1090 struct fsi_stream *io = (struct fsi_stream *)data;
1091 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1092 struct snd_soc_dai *dai;
1093 struct dma_async_tx_descriptor *desc;
1094 struct snd_pcm_runtime *runtime;
1095 enum dma_data_direction dir;
1096 int is_play = fsi_stream_is_play(fsi, io);
1100 if (!fsi_stream_is_working(fsi, io))
1103 dai = fsi_get_dai(io->substream);
1104 runtime = io->substream->runtime;
1105 dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
1106 len = samples_to_bytes(runtime, io->period_samples);
1107 buf = fsi_dma_get_area(io);
1109 dma_sync_single_for_device(dai->dev, buf, len, dir);
1111 desc = dmaengine_prep_slave_single(io->chan, buf, len, dir,
1112 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1114 dev_err(dai->dev, "dmaengine_prep_slave_sg() fail\n");
1118 desc->callback = fsi_dma_complete;
1119 desc->callback_param = io;
1121 if (dmaengine_submit(desc) < 0) {
1122 dev_err(dai->dev, "tx_submit() fail\n");
1126 dma_async_issue_pending(io->chan);
1131 * In DMAEngine case, codec and FSI cannot be started simultaneously
1132 * since FSI is using tasklet.
1133 * Therefore, in capture case, probably FSI FIFO will have got
1134 * overflow error in this point.
1135 * in that case, DMA cannot start transfer until error was cleared.
1138 if (ERR_OVER & fsi_reg_read(fsi, DIFF_ST)) {
1139 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1140 fsi_reg_write(fsi, DIFF_ST, 0);
1145 static bool fsi_dma_filter(struct dma_chan *chan, void *param)
1147 struct sh_dmae_slave *slave = param;
1149 chan->private = slave;
1154 static int fsi_dma_transfer(struct fsi_priv *fsi, struct fsi_stream *io)
1156 tasklet_schedule(&io->tasklet);
1161 static void fsi_dma_push_start_stop(struct fsi_priv *fsi, struct fsi_stream *io,
1164 struct fsi_master *master = fsi_get_master(fsi);
1165 u32 clk = fsi_is_port_a(fsi) ? CRA : CRB;
1166 u32 enable = start ? DMA_ON : 0;
1168 fsi_reg_mask_set(fsi, OUT_DMAC, DMA_ON, enable);
1170 dmaengine_terminate_all(io->chan);
1172 if (fsi_is_clk_master(fsi))
1173 fsi_master_mask_set(master, CLK_RST, clk, (enable) ? clk : 0);
1176 static int fsi_dma_probe(struct fsi_priv *fsi, struct fsi_stream *io, struct device *dev)
1178 dma_cap_mask_t mask;
1181 dma_cap_set(DMA_SLAVE, mask);
1183 io->chan = dma_request_channel(mask, fsi_dma_filter, &io->slave);
1186 /* switch to PIO handler */
1187 if (fsi_stream_is_play(fsi, io))
1188 fsi->playback.handler = &fsi_pio_push_handler;
1190 fsi->capture.handler = &fsi_pio_pop_handler;
1192 dev_info(dev, "switch handler (dma => pio)\n");
1195 return fsi_stream_probe(fsi, dev);
1198 tasklet_init(&io->tasklet, fsi_dma_do_tasklet, (unsigned long)io);
1203 static int fsi_dma_remove(struct fsi_priv *fsi, struct fsi_stream *io)
1205 tasklet_kill(&io->tasklet);
1207 fsi_stream_stop(fsi, io);
1210 dma_release_channel(io->chan);
1216 static struct fsi_stream_handler fsi_dma_push_handler = {
1217 .init = fsi_dma_init,
1218 .quit = fsi_dma_quit,
1219 .probe = fsi_dma_probe,
1220 .transfer = fsi_dma_transfer,
1221 .remove = fsi_dma_remove,
1222 .start_stop = fsi_dma_push_start_stop,
1228 static void fsi_fifo_init(struct fsi_priv *fsi,
1229 struct fsi_stream *io,
1232 struct fsi_master *master = fsi_get_master(fsi);
1233 int is_play = fsi_stream_is_play(fsi, io);
1237 /* get on-chip RAM capacity */
1238 shift = fsi_master_read(master, FIFO_SZ);
1239 shift >>= fsi_get_port_shift(fsi, io);
1240 shift &= FIFO_SZ_MASK;
1241 frame_capa = 256 << shift;
1242 dev_dbg(dev, "fifo = %d words\n", frame_capa);
1245 * The maximum number of sample data varies depending
1246 * on the number of channels selected for the format.
1248 * FIFOs are used in 4-channel units in 3-channel mode
1249 * and in 8-channel units in 5- to 7-channel mode
1250 * meaning that more FIFOs than the required size of DPRAM
1253 * ex) if 256 words of DP-RAM is connected
1254 * 1 channel: 256 (256 x 1 = 256)
1255 * 2 channels: 128 (128 x 2 = 256)
1256 * 3 channels: 64 ( 64 x 3 = 192)
1257 * 4 channels: 64 ( 64 x 4 = 256)
1258 * 5 channels: 32 ( 32 x 5 = 160)
1259 * 6 channels: 32 ( 32 x 6 = 192)
1260 * 7 channels: 32 ( 32 x 7 = 224)
1261 * 8 channels: 32 ( 32 x 8 = 256)
1263 for (i = 1; i < fsi->chan_num; i <<= 1)
1265 dev_dbg(dev, "%d channel %d store\n",
1266 fsi->chan_num, frame_capa);
1268 io->fifo_sample_capa = fsi_frame2sample(fsi, frame_capa);
1271 * set interrupt generation factor
1275 fsi_reg_write(fsi, DOFF_CTL, IRQ_HALF);
1276 fsi_reg_mask_set(fsi, DOFF_CTL, FIFO_CLR, FIFO_CLR);
1278 fsi_reg_write(fsi, DIFF_CTL, IRQ_HALF);
1279 fsi_reg_mask_set(fsi, DIFF_CTL, FIFO_CLR, FIFO_CLR);
1283 static int fsi_hw_startup(struct fsi_priv *fsi,
1284 struct fsi_stream *io,
1287 u32 flags = fsi_get_info_flags(fsi);
1291 if (fsi_is_clk_master(fsi))
1294 fsi_reg_mask_set(fsi, CKG1, (DIMD | DOMD), data);
1296 /* clock inversion (CKG2) */
1298 if (SH_FSI_LRM_INV & flags)
1300 if (SH_FSI_BRM_INV & flags)
1302 if (SH_FSI_LRS_INV & flags)
1304 if (SH_FSI_BRS_INV & flags)
1307 fsi_reg_write(fsi, CKG2, data);
1310 if (fsi_is_spdif(fsi)) {
1311 fsi_spdif_clk_ctrl(fsi, 1);
1312 fsi_reg_mask_set(fsi, OUT_SEL, DMMD, DMMD);
1319 switch (io->sample_width) {
1321 data = BUSOP_GET(16, io->bus_option);
1324 data = BUSOP_GET(24, io->bus_option);
1327 fsi_format_bus_setup(fsi, io, data, dev);
1330 fsi_irq_disable(fsi, io);
1331 fsi_irq_clear_status(fsi);
1334 fsi_fifo_init(fsi, io, dev);
1339 static void fsi_hw_shutdown(struct fsi_priv *fsi,
1342 if (fsi_is_clk_master(fsi))
1343 fsi_set_master_clk(dev, fsi, fsi->rate, 0);
1346 static int fsi_dai_startup(struct snd_pcm_substream *substream,
1347 struct snd_soc_dai *dai)
1349 struct fsi_priv *fsi = fsi_get_priv(substream);
1356 static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
1357 struct snd_soc_dai *dai)
1359 struct fsi_priv *fsi = fsi_get_priv(substream);
1364 static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
1365 struct snd_soc_dai *dai)
1367 struct fsi_priv *fsi = fsi_get_priv(substream);
1368 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1372 case SNDRV_PCM_TRIGGER_START:
1373 fsi_stream_init(fsi, io, substream);
1374 fsi_hw_startup(fsi, io, dai->dev);
1375 ret = fsi_stream_transfer(io);
1377 fsi_stream_start(fsi, io);
1379 case SNDRV_PCM_TRIGGER_STOP:
1380 fsi_hw_shutdown(fsi, dai->dev);
1381 fsi_stream_stop(fsi, io);
1382 fsi_stream_quit(fsi, io);
1389 static int fsi_set_fmt_dai(struct fsi_priv *fsi, unsigned int fmt)
1391 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1392 case SND_SOC_DAIFMT_I2S:
1396 case SND_SOC_DAIFMT_LEFT_J:
1407 static int fsi_set_fmt_spdif(struct fsi_priv *fsi)
1409 struct fsi_master *master = fsi_get_master(fsi);
1411 if (fsi_version(master) < 2)
1414 fsi->fmt = CR_DTMD_SPDIF_PCM | CR_PCM;
1421 static int fsi_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1423 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
1424 set_rate_func set_rate = fsi_get_info_set_rate(fsi);
1425 u32 flags = fsi_get_info_flags(fsi);
1428 /* set master/slave audio interface */
1429 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1430 case SND_SOC_DAIFMT_CBM_CFM:
1431 fsi->clk_master = 1;
1433 case SND_SOC_DAIFMT_CBS_CFS:
1439 if (fsi_is_clk_master(fsi) && !set_rate) {
1440 dev_err(dai->dev, "platform doesn't have set_rate\n");
1445 switch (flags & SH_FSI_FMT_MASK) {
1446 case SH_FSI_FMT_DAI:
1447 ret = fsi_set_fmt_dai(fsi, fmt & SND_SOC_DAIFMT_FORMAT_MASK);
1449 case SH_FSI_FMT_SPDIF:
1450 ret = fsi_set_fmt_spdif(fsi);
1459 static int fsi_dai_hw_params(struct snd_pcm_substream *substream,
1460 struct snd_pcm_hw_params *params,
1461 struct snd_soc_dai *dai)
1463 struct fsi_priv *fsi = fsi_get_priv(substream);
1464 long rate = params_rate(params);
1467 if (!fsi_is_clk_master(fsi))
1470 ret = fsi_set_master_clk(dai->dev, fsi, rate, 1);
1479 static const struct snd_soc_dai_ops fsi_dai_ops = {
1480 .startup = fsi_dai_startup,
1481 .shutdown = fsi_dai_shutdown,
1482 .trigger = fsi_dai_trigger,
1483 .set_fmt = fsi_dai_set_fmt,
1484 .hw_params = fsi_dai_hw_params,
1491 static struct snd_pcm_hardware fsi_pcm_hardware = {
1492 .info = SNDRV_PCM_INFO_INTERLEAVED |
1493 SNDRV_PCM_INFO_MMAP |
1494 SNDRV_PCM_INFO_MMAP_VALID |
1495 SNDRV_PCM_INFO_PAUSE,
1496 .formats = FSI_FMTS,
1502 .buffer_bytes_max = 64 * 1024,
1503 .period_bytes_min = 32,
1504 .period_bytes_max = 8192,
1510 static int fsi_pcm_open(struct snd_pcm_substream *substream)
1512 struct snd_pcm_runtime *runtime = substream->runtime;
1515 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
1517 ret = snd_pcm_hw_constraint_integer(runtime,
1518 SNDRV_PCM_HW_PARAM_PERIODS);
1523 static int fsi_hw_params(struct snd_pcm_substream *substream,
1524 struct snd_pcm_hw_params *hw_params)
1526 return snd_pcm_lib_malloc_pages(substream,
1527 params_buffer_bytes(hw_params));
1530 static int fsi_hw_free(struct snd_pcm_substream *substream)
1532 return snd_pcm_lib_free_pages(substream);
1535 static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
1537 struct fsi_priv *fsi = fsi_get_priv(substream);
1538 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1540 return fsi_sample2frame(fsi, io->buff_sample_pos);
1543 static struct snd_pcm_ops fsi_pcm_ops = {
1544 .open = fsi_pcm_open,
1545 .ioctl = snd_pcm_lib_ioctl,
1546 .hw_params = fsi_hw_params,
1547 .hw_free = fsi_hw_free,
1548 .pointer = fsi_pointer,
1555 #define PREALLOC_BUFFER (32 * 1024)
1556 #define PREALLOC_BUFFER_MAX (32 * 1024)
1558 static void fsi_pcm_free(struct snd_pcm *pcm)
1560 snd_pcm_lib_preallocate_free_for_all(pcm);
1563 static int fsi_pcm_new(struct snd_soc_pcm_runtime *rtd)
1565 struct snd_pcm *pcm = rtd->pcm;
1568 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1569 * in MMAP mode (i.e. aplay -M)
1571 return snd_pcm_lib_preallocate_pages_for_all(
1573 SNDRV_DMA_TYPE_CONTINUOUS,
1574 snd_dma_continuous_data(GFP_KERNEL),
1575 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
1582 static struct snd_soc_dai_driver fsi_soc_dai[] = {
1587 .formats = FSI_FMTS,
1593 .formats = FSI_FMTS,
1597 .ops = &fsi_dai_ops,
1603 .formats = FSI_FMTS,
1609 .formats = FSI_FMTS,
1613 .ops = &fsi_dai_ops,
1617 static struct snd_soc_platform_driver fsi_soc_platform = {
1618 .ops = &fsi_pcm_ops,
1619 .pcm_new = fsi_pcm_new,
1620 .pcm_free = fsi_pcm_free,
1626 static void fsi_handler_init(struct fsi_priv *fsi)
1628 fsi->playback.handler = &fsi_pio_push_handler; /* default PIO */
1629 fsi->playback.priv = fsi;
1630 fsi->capture.handler = &fsi_pio_pop_handler; /* default PIO */
1631 fsi->capture.priv = fsi;
1633 if (fsi->info->tx_id) {
1634 fsi->playback.slave.shdma_slave.slave_id = fsi->info->tx_id;
1635 fsi->playback.handler = &fsi_dma_push_handler;
1639 static int fsi_probe(struct platform_device *pdev)
1641 struct fsi_master *master;
1642 const struct platform_device_id *id_entry;
1643 struct sh_fsi_platform_info *info = pdev->dev.platform_data;
1644 struct resource *res;
1648 id_entry = pdev->id_entry;
1650 dev_err(&pdev->dev, "unknown fsi device\n");
1654 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1655 irq = platform_get_irq(pdev, 0);
1656 if (!res || (int)irq <= 0) {
1657 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
1661 master = devm_kzalloc(&pdev->dev, sizeof(*master), GFP_KERNEL);
1663 dev_err(&pdev->dev, "Could not allocate master\n");
1667 master->base = devm_ioremap_nocache(&pdev->dev,
1668 res->start, resource_size(res));
1669 if (!master->base) {
1670 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
1674 /* master setting */
1676 master->core = (struct fsi_core *)id_entry->driver_data;
1677 spin_lock_init(&master->lock);
1680 master->fsia.base = master->base;
1681 master->fsia.master = master;
1682 master->fsia.info = &info->port_a;
1683 fsi_handler_init(&master->fsia);
1684 ret = fsi_stream_probe(&master->fsia, &pdev->dev);
1686 dev_err(&pdev->dev, "FSIA stream probe failed\n");
1691 master->fsib.base = master->base + 0x40;
1692 master->fsib.master = master;
1693 master->fsib.info = &info->port_b;
1694 fsi_handler_init(&master->fsib);
1695 ret = fsi_stream_probe(&master->fsib, &pdev->dev);
1697 dev_err(&pdev->dev, "FSIB stream probe failed\n");
1701 pm_runtime_enable(&pdev->dev);
1702 dev_set_drvdata(&pdev->dev, master);
1704 ret = request_irq(irq, &fsi_interrupt, 0,
1705 id_entry->name, master);
1707 dev_err(&pdev->dev, "irq request err\n");
1711 ret = snd_soc_register_platform(&pdev->dev, &fsi_soc_platform);
1713 dev_err(&pdev->dev, "cannot snd soc register\n");
1717 ret = snd_soc_register_dais(&pdev->dev, fsi_soc_dai,
1718 ARRAY_SIZE(fsi_soc_dai));
1720 dev_err(&pdev->dev, "cannot snd dai register\n");
1727 snd_soc_unregister_platform(&pdev->dev);
1729 free_irq(irq, master);
1731 pm_runtime_disable(&pdev->dev);
1732 fsi_stream_remove(&master->fsib);
1734 fsi_stream_remove(&master->fsia);
1739 static int fsi_remove(struct platform_device *pdev)
1741 struct fsi_master *master;
1743 master = dev_get_drvdata(&pdev->dev);
1745 free_irq(master->irq, master);
1746 pm_runtime_disable(&pdev->dev);
1748 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(fsi_soc_dai));
1749 snd_soc_unregister_platform(&pdev->dev);
1751 fsi_stream_remove(&master->fsia);
1752 fsi_stream_remove(&master->fsib);
1757 static void __fsi_suspend(struct fsi_priv *fsi,
1758 struct fsi_stream *io,
1761 if (!fsi_stream_is_working(fsi, io))
1764 fsi_stream_stop(fsi, io);
1765 fsi_hw_shutdown(fsi, dev);
1768 static void __fsi_resume(struct fsi_priv *fsi,
1769 struct fsi_stream *io,
1772 if (!fsi_stream_is_working(fsi, io))
1775 fsi_hw_startup(fsi, io, dev);
1777 if (fsi_is_clk_master(fsi) && fsi->rate)
1778 fsi_set_master_clk(dev, fsi, fsi->rate, 1);
1780 fsi_stream_start(fsi, io);
1783 static int fsi_suspend(struct device *dev)
1785 struct fsi_master *master = dev_get_drvdata(dev);
1786 struct fsi_priv *fsia = &master->fsia;
1787 struct fsi_priv *fsib = &master->fsib;
1789 __fsi_suspend(fsia, &fsia->playback, dev);
1790 __fsi_suspend(fsia, &fsia->capture, dev);
1792 __fsi_suspend(fsib, &fsib->playback, dev);
1793 __fsi_suspend(fsib, &fsib->capture, dev);
1798 static int fsi_resume(struct device *dev)
1800 struct fsi_master *master = dev_get_drvdata(dev);
1801 struct fsi_priv *fsia = &master->fsia;
1802 struct fsi_priv *fsib = &master->fsib;
1804 __fsi_resume(fsia, &fsia->playback, dev);
1805 __fsi_resume(fsia, &fsia->capture, dev);
1807 __fsi_resume(fsib, &fsib->playback, dev);
1808 __fsi_resume(fsib, &fsib->capture, dev);
1813 static struct dev_pm_ops fsi_pm_ops = {
1814 .suspend = fsi_suspend,
1815 .resume = fsi_resume,
1818 static struct fsi_core fsi1_core = {
1827 static struct fsi_core fsi2_core = {
1831 .int_st = CPU_INT_ST,
1834 .a_mclk = A_MST_CTLR,
1835 .b_mclk = B_MST_CTLR,
1838 static struct platform_device_id fsi_id_table[] = {
1839 { "sh_fsi", (kernel_ulong_t)&fsi1_core },
1840 { "sh_fsi2", (kernel_ulong_t)&fsi2_core },
1843 MODULE_DEVICE_TABLE(platform, fsi_id_table);
1845 static struct platform_driver fsi_driver = {
1847 .name = "fsi-pcm-audio",
1851 .remove = fsi_remove,
1852 .id_table = fsi_id_table,
1855 module_platform_driver(fsi_driver);
1857 MODULE_LICENSE("GPL");
1858 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1859 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1860 MODULE_ALIAS("platform:fsi-pcm-audio");