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/pcm_params.h>
26 #include <sound/sh_fsi.h>
28 /* PortA/PortB register */
29 #define REG_DO_FMT 0x0000
30 #define REG_DOFF_CTL 0x0004
31 #define REG_DOFF_ST 0x0008
32 #define REG_DI_FMT 0x000C
33 #define REG_DIFF_CTL 0x0010
34 #define REG_DIFF_ST 0x0014
35 #define REG_CKG1 0x0018
36 #define REG_CKG2 0x001C
37 #define REG_DIDT 0x0020
38 #define REG_DODT 0x0024
39 #define REG_MUTE_ST 0x0028
40 #define REG_OUT_DMAC 0x002C
41 #define REG_OUT_SEL 0x0030
42 #define REG_IN_DMAC 0x0038
45 #define MST_CLK_RST 0x0210
46 #define MST_SOFT_RST 0x0214
47 #define MST_FIFO_SZ 0x0218
49 /* core register (depend on FSI version) */
50 #define A_MST_CTLR 0x0180
51 #define B_MST_CTLR 0x01A0
52 #define CPU_INT_ST 0x01F4
53 #define CPU_IEMSK 0x01F8
54 #define CPU_IMSK 0x01FC
61 #define CR_BWS_MASK (0x3 << 20) /* FSI2 */
62 #define CR_BWS_24 (0x0 << 20) /* FSI2 */
63 #define CR_BWS_16 (0x1 << 20) /* FSI2 */
64 #define CR_BWS_20 (0x2 << 20) /* FSI2 */
66 #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
67 #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
68 #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
70 #define CR_MONO (0x0 << 4)
71 #define CR_MONO_D (0x1 << 4)
72 #define CR_PCM (0x2 << 4)
73 #define CR_I2S (0x3 << 4)
74 #define CR_TDM (0x4 << 4)
75 #define CR_TDM_D (0x5 << 4)
79 #define VDMD_MASK (0x3 << 4)
80 #define VDMD_FRONT (0x0 << 4) /* Package in front */
81 #define VDMD_BACK (0x1 << 4) /* Package in back */
82 #define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
84 #define DMA_ON (0x1 << 0)
88 #define IRQ_HALF 0x00100000
89 #define FIFO_CLR 0x00000001
92 #define ERR_OVER 0x00000010
93 #define ERR_UNDER 0x00000001
94 #define ST_ERR (ERR_OVER | ERR_UNDER)
97 #define ACKMD_MASK 0x00007000
98 #define BPFMD_MASK 0x00000700
100 #define DOMD (1 << 0)
103 #define BP (1 << 4) /* Fix the signal of Biphase output */
104 #define SE (1 << 0) /* Fix the master clock */
110 /* IO SHIFT / MACRO */
115 #define AB_IO(param, shift) (param << shift)
118 #define PBSR (1 << 12) /* Port B Software Reset */
119 #define PASR (1 << 8) /* Port A Software Reset */
120 #define IR (1 << 4) /* Interrupt Reset */
121 #define FSISR (1 << 0) /* Software Reset */
124 #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
125 /* 1: Biphase and serial */
128 #define FIFO_SZ_MASK 0x7
130 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
132 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
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 * ==|============================================ ... =|==
193 * FSIxCLK [CPG] (ick) -------> |
194 * |-> FSI_DIV (div)-> FSI2
195 * FSIxCK [external] (xck) ---> |
202 struct fsi_stream_handler
;
206 * these are initialized by fsi_stream_init()
208 struct snd_pcm_substream
*substream
;
209 int fifo_sample_capa
; /* sample capacity of FSI FIFO */
210 int buff_sample_capa
; /* sample capacity of ALSA buffer */
211 int buff_sample_pos
; /* sample position of ALSA buffer */
212 int period_samples
; /* sample number / 1 period */
213 int period_pos
; /* current period position */
214 int sample_width
; /* sample width */
224 * thse are initialized by fsi_handler_init()
226 struct fsi_stream_handler
*handler
;
227 struct fsi_priv
*priv
;
230 * these are for DMAEngine
232 struct dma_chan
*chan
;
233 struct sh_dmae_slave slave
; /* see fsi_handler_init() */
234 struct work_struct work
;
239 /* see [FSI clock] */
244 int (*set_rate
)(struct device
*dev
,
245 struct fsi_priv
*fsi
);
253 struct fsi_master
*master
;
255 struct fsi_stream playback
;
256 struct fsi_stream capture
;
258 struct fsi_clk clock
;
271 struct fsi_stream_handler
{
272 int (*init
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
273 int (*quit
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
274 int (*probe
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
, struct device
*dev
);
275 int (*transfer
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
276 int (*remove
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
277 void (*start_stop
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
280 #define fsi_stream_handler_call(io, func, args...) \
282 !((io)->handler->func) ? 0 : \
283 (io)->handler->func(args))
298 struct fsi_priv fsia
;
299 struct fsi_priv fsib
;
300 struct fsi_core
*core
;
304 static int fsi_stream_is_play(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
307 * basic read write function
310 static void __fsi_reg_write(u32 __iomem
*reg
, u32 data
)
312 /* valid data area is 24bit */
315 __raw_writel(data
, reg
);
318 static u32
__fsi_reg_read(u32 __iomem
*reg
)
320 return __raw_readl(reg
);
323 static void __fsi_reg_mask_set(u32 __iomem
*reg
, u32 mask
, u32 data
)
325 u32 val
= __fsi_reg_read(reg
);
330 __fsi_reg_write(reg
, val
);
333 #define fsi_reg_write(p, r, d)\
334 __fsi_reg_write((p->base + REG_##r), d)
336 #define fsi_reg_read(p, r)\
337 __fsi_reg_read((p->base + REG_##r))
339 #define fsi_reg_mask_set(p, r, m, d)\
340 __fsi_reg_mask_set((p->base + REG_##r), m, d)
342 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
343 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
344 static u32
_fsi_master_read(struct fsi_master
*master
, u32 reg
)
349 spin_lock_irqsave(&master
->lock
, flags
);
350 ret
= __fsi_reg_read(master
->base
+ reg
);
351 spin_unlock_irqrestore(&master
->lock
, flags
);
356 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
357 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
358 static void _fsi_master_mask_set(struct fsi_master
*master
,
359 u32 reg
, u32 mask
, u32 data
)
363 spin_lock_irqsave(&master
->lock
, flags
);
364 __fsi_reg_mask_set(master
->base
+ reg
, mask
, data
);
365 spin_unlock_irqrestore(&master
->lock
, flags
);
371 static int fsi_version(struct fsi_master
*master
)
373 return master
->core
->ver
;
376 static struct fsi_master
*fsi_get_master(struct fsi_priv
*fsi
)
381 static int fsi_is_clk_master(struct fsi_priv
*fsi
)
383 return fsi
->clk_master
;
386 static int fsi_is_port_a(struct fsi_priv
*fsi
)
388 return fsi
->master
->base
== fsi
->base
;
391 static int fsi_is_spdif(struct fsi_priv
*fsi
)
396 static int fsi_is_enable_stream(struct fsi_priv
*fsi
)
398 return fsi
->enable_stream
;
401 static int fsi_is_play(struct snd_pcm_substream
*substream
)
403 return substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
406 static struct snd_soc_dai
*fsi_get_dai(struct snd_pcm_substream
*substream
)
408 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
413 static struct fsi_priv
*fsi_get_priv_frm_dai(struct snd_soc_dai
*dai
)
415 struct fsi_master
*master
= snd_soc_dai_get_drvdata(dai
);
418 return &master
->fsia
;
420 return &master
->fsib
;
423 static struct fsi_priv
*fsi_get_priv(struct snd_pcm_substream
*substream
)
425 return fsi_get_priv_frm_dai(fsi_get_dai(substream
));
428 static u32
fsi_get_port_shift(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
430 int is_play
= fsi_stream_is_play(fsi
, io
);
431 int is_porta
= fsi_is_port_a(fsi
);
435 shift
= is_play
? AO_SHIFT
: AI_SHIFT
;
437 shift
= is_play
? BO_SHIFT
: BI_SHIFT
;
442 static int fsi_frame2sample(struct fsi_priv
*fsi
, int frames
)
444 return frames
* fsi
->chan_num
;
447 static int fsi_sample2frame(struct fsi_priv
*fsi
, int samples
)
449 return samples
/ fsi
->chan_num
;
452 static int fsi_get_current_fifo_samples(struct fsi_priv
*fsi
,
453 struct fsi_stream
*io
)
455 int is_play
= fsi_stream_is_play(fsi
, io
);
460 fsi_reg_read(fsi
, DOFF_ST
) :
461 fsi_reg_read(fsi
, DIFF_ST
);
463 frames
= 0x1ff & (status
>> 8);
465 return fsi_frame2sample(fsi
, frames
);
468 static void fsi_count_fifo_err(struct fsi_priv
*fsi
)
470 u32 ostatus
= fsi_reg_read(fsi
, DOFF_ST
);
471 u32 istatus
= fsi_reg_read(fsi
, DIFF_ST
);
473 if (ostatus
& ERR_OVER
)
474 fsi
->playback
.oerr_num
++;
476 if (ostatus
& ERR_UNDER
)
477 fsi
->playback
.uerr_num
++;
479 if (istatus
& ERR_OVER
)
480 fsi
->capture
.oerr_num
++;
482 if (istatus
& ERR_UNDER
)
483 fsi
->capture
.uerr_num
++;
485 fsi_reg_write(fsi
, DOFF_ST
, 0);
486 fsi_reg_write(fsi
, DIFF_ST
, 0);
490 * fsi_stream_xx() function
492 static inline int fsi_stream_is_play(struct fsi_priv
*fsi
,
493 struct fsi_stream
*io
)
495 return &fsi
->playback
== io
;
498 static inline struct fsi_stream
*fsi_stream_get(struct fsi_priv
*fsi
,
499 struct snd_pcm_substream
*substream
)
501 return fsi_is_play(substream
) ? &fsi
->playback
: &fsi
->capture
;
504 static int fsi_stream_is_working(struct fsi_priv
*fsi
,
505 struct fsi_stream
*io
)
507 struct fsi_master
*master
= fsi_get_master(fsi
);
511 spin_lock_irqsave(&master
->lock
, flags
);
512 ret
= !!(io
->substream
&& io
->substream
->runtime
);
513 spin_unlock_irqrestore(&master
->lock
, flags
);
518 static struct fsi_priv
*fsi_stream_to_priv(struct fsi_stream
*io
)
523 static void fsi_stream_init(struct fsi_priv
*fsi
,
524 struct fsi_stream
*io
,
525 struct snd_pcm_substream
*substream
)
527 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
528 struct fsi_master
*master
= fsi_get_master(fsi
);
531 spin_lock_irqsave(&master
->lock
, flags
);
532 io
->substream
= substream
;
533 io
->buff_sample_capa
= fsi_frame2sample(fsi
, runtime
->buffer_size
);
534 io
->buff_sample_pos
= 0;
535 io
->period_samples
= fsi_frame2sample(fsi
, runtime
->period_size
);
537 io
->sample_width
= samples_to_bytes(runtime
, 1);
539 io
->oerr_num
= -1; /* ignore 1st err */
540 io
->uerr_num
= -1; /* ignore 1st err */
541 fsi_stream_handler_call(io
, init
, fsi
, io
);
542 spin_unlock_irqrestore(&master
->lock
, flags
);
545 static void fsi_stream_quit(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
547 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
548 struct fsi_master
*master
= fsi_get_master(fsi
);
551 spin_lock_irqsave(&master
->lock
, flags
);
553 if (io
->oerr_num
> 0)
554 dev_err(dai
->dev
, "over_run = %d\n", io
->oerr_num
);
556 if (io
->uerr_num
> 0)
557 dev_err(dai
->dev
, "under_run = %d\n", io
->uerr_num
);
559 fsi_stream_handler_call(io
, quit
, fsi
, io
);
560 io
->substream
= NULL
;
561 io
->buff_sample_capa
= 0;
562 io
->buff_sample_pos
= 0;
563 io
->period_samples
= 0;
565 io
->sample_width
= 0;
569 spin_unlock_irqrestore(&master
->lock
, flags
);
572 static int fsi_stream_transfer(struct fsi_stream
*io
)
574 struct fsi_priv
*fsi
= fsi_stream_to_priv(io
);
578 return fsi_stream_handler_call(io
, transfer
, fsi
, io
);
581 #define fsi_stream_start(fsi, io)\
582 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
584 #define fsi_stream_stop(fsi, io)\
585 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
587 static int fsi_stream_probe(struct fsi_priv
*fsi
, struct device
*dev
)
589 struct fsi_stream
*io
;
593 ret1
= fsi_stream_handler_call(io
, probe
, fsi
, io
, dev
);
596 ret2
= fsi_stream_handler_call(io
, probe
, fsi
, io
, dev
);
606 static int fsi_stream_remove(struct fsi_priv
*fsi
)
608 struct fsi_stream
*io
;
612 ret1
= fsi_stream_handler_call(io
, remove
, fsi
, io
);
615 ret2
= fsi_stream_handler_call(io
, remove
, fsi
, io
);
626 * format/bus/dma setting
628 static void fsi_format_bus_setup(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
629 u32 bus
, struct device
*dev
)
631 struct fsi_master
*master
= fsi_get_master(fsi
);
632 int is_play
= fsi_stream_is_play(fsi
, io
);
635 if (fsi_version(master
) >= 2) {
639 * FSI2 needs DMA/Bus setting
642 case PACKAGE_24BITBUS_FRONT
:
645 dev_dbg(dev
, "24bit bus / package in front\n");
647 case PACKAGE_16BITBUS_STREAM
:
650 dev_dbg(dev
, "16bit bus / stream mode\n");
652 case PACKAGE_24BITBUS_BACK
:
656 dev_dbg(dev
, "24bit bus / package in back\n");
661 fsi_reg_write(fsi
, OUT_DMAC
, dma
);
663 fsi_reg_write(fsi
, IN_DMAC
, dma
);
667 fsi_reg_write(fsi
, DO_FMT
, fmt
);
669 fsi_reg_write(fsi
, DI_FMT
, fmt
);
676 static void fsi_irq_enable(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
678 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, io
));
679 struct fsi_master
*master
= fsi_get_master(fsi
);
681 fsi_core_mask_set(master
, imsk
, data
, data
);
682 fsi_core_mask_set(master
, iemsk
, data
, data
);
685 static void fsi_irq_disable(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
687 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, io
));
688 struct fsi_master
*master
= fsi_get_master(fsi
);
690 fsi_core_mask_set(master
, imsk
, data
, 0);
691 fsi_core_mask_set(master
, iemsk
, data
, 0);
694 static u32
fsi_irq_get_status(struct fsi_master
*master
)
696 return fsi_core_read(master
, int_st
);
699 static void fsi_irq_clear_status(struct fsi_priv
*fsi
)
702 struct fsi_master
*master
= fsi_get_master(fsi
);
704 data
|= AB_IO(1, fsi_get_port_shift(fsi
, &fsi
->playback
));
705 data
|= AB_IO(1, fsi_get_port_shift(fsi
, &fsi
->capture
));
707 /* clear interrupt factor */
708 fsi_core_mask_set(master
, int_st
, data
, 0);
712 * SPDIF master clock function
714 * These functions are used later FSI2
716 static void fsi_spdif_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
718 struct fsi_master
*master
= fsi_get_master(fsi
);
722 val
= enable
? mask
: 0;
725 fsi_core_mask_set(master
, a_mclk
, mask
, val
) :
726 fsi_core_mask_set(master
, b_mclk
, mask
, val
);
732 static int fsi_clk_init(struct device
*dev
,
733 struct fsi_priv
*fsi
,
737 int (*set_rate
)(struct device
*dev
,
738 struct fsi_priv
*fsi
))
740 struct fsi_clk
*clock
= &fsi
->clock
;
741 int is_porta
= fsi_is_port_a(fsi
);
748 clock
->set_rate
= set_rate
;
750 clock
->own
= devm_clk_get(dev
, NULL
);
751 if (IS_ERR(clock
->own
))
756 clock
->xck
= devm_clk_get(dev
, is_porta
? "xcka" : "xckb");
757 if (IS_ERR(clock
->xck
)) {
758 dev_err(dev
, "can't get xck clock\n");
761 if (clock
->xck
== clock
->own
) {
762 dev_err(dev
, "cpu doesn't support xck clock\n");
767 /* FSIACLK/FSIBCLK */
769 clock
->ick
= devm_clk_get(dev
, is_porta
? "icka" : "ickb");
770 if (IS_ERR(clock
->ick
)) {
771 dev_err(dev
, "can't get ick clock\n");
774 if (clock
->ick
== clock
->own
) {
775 dev_err(dev
, "cpu doesn't support ick clock\n");
782 clock
->div
= devm_clk_get(dev
, is_porta
? "diva" : "divb");
783 if (IS_ERR(clock
->div
)) {
784 dev_err(dev
, "can't get div clock\n");
787 if (clock
->div
== clock
->own
) {
788 dev_err(dev
, "cpu doens't support div clock\n");
796 #define fsi_clk_invalid(fsi) fsi_clk_valid(fsi, 0)
797 static void fsi_clk_valid(struct fsi_priv
*fsi
, unsigned long rate
)
799 fsi
->clock
.rate
= rate
;
802 static int fsi_clk_is_valid(struct fsi_priv
*fsi
)
804 return fsi
->clock
.set_rate
&&
808 static int fsi_clk_enable(struct device
*dev
,
809 struct fsi_priv
*fsi
)
811 struct fsi_clk
*clock
= &fsi
->clock
;
814 if (!fsi_clk_is_valid(fsi
))
817 if (0 == clock
->count
) {
818 ret
= clock
->set_rate(dev
, fsi
);
820 fsi_clk_invalid(fsi
);
825 clk_enable(clock
->xck
);
827 clk_enable(clock
->ick
);
829 clk_enable(clock
->div
);
837 static int fsi_clk_disable(struct device
*dev
,
838 struct fsi_priv
*fsi
)
840 struct fsi_clk
*clock
= &fsi
->clock
;
842 if (!fsi_clk_is_valid(fsi
))
845 if (1 == clock
->count
--) {
847 clk_disable(clock
->xck
);
849 clk_disable(clock
->ick
);
851 clk_disable(clock
->div
);
857 static int fsi_clk_set_ackbpf(struct device
*dev
,
858 struct fsi_priv
*fsi
,
859 int ackmd
, int bpfmd
)
863 /* check ackmd/bpfmd relationship */
865 dev_err(dev
, "unsupported rate (%d/%d)\n", ackmd
, bpfmd
);
887 dev_err(dev
, "unsupported ackmd (%d)\n", ackmd
);
912 dev_err(dev
, "unsupported bpfmd (%d)\n", bpfmd
);
916 dev_dbg(dev
, "ACKMD/BPFMD = %d/%d\n", ackmd
, bpfmd
);
918 fsi_reg_mask_set(fsi
, CKG1
, (ACKMD_MASK
| BPFMD_MASK
) , data
);
924 static int fsi_clk_set_rate_external(struct device
*dev
,
925 struct fsi_priv
*fsi
)
927 struct clk
*xck
= fsi
->clock
.xck
;
928 struct clk
*ick
= fsi
->clock
.ick
;
929 unsigned long rate
= fsi
->clock
.rate
;
934 /* check clock rate */
935 xrate
= clk_get_rate(xck
);
937 dev_err(dev
, "unsupported clock rate\n");
941 clk_set_parent(ick
, xck
);
942 clk_set_rate(ick
, xrate
);
944 bpfmd
= fsi
->chan_num
* 32;
945 ackmd
= xrate
/ rate
;
947 dev_dbg(dev
, "external/rate = %ld/%ld\n", xrate
, rate
);
949 ret
= fsi_clk_set_ackbpf(dev
, fsi
, ackmd
, bpfmd
);
951 dev_err(dev
, "%s failed", __func__
);
956 static int fsi_clk_set_rate_cpg(struct device
*dev
,
957 struct fsi_priv
*fsi
)
959 struct clk
*ick
= fsi
->clock
.ick
;
960 struct clk
*div
= fsi
->clock
.div
;
961 unsigned long rate
= fsi
->clock
.rate
;
962 unsigned long target
= 0; /* 12288000 or 11289600 */
963 unsigned long actual
, cout
;
964 unsigned long diff
, min
;
965 unsigned long best_cout
, best_act
;
970 if (!(12288000 % rate
))
972 if (!(11289600 % rate
))
975 dev_err(dev
, "unsupported rate\n");
979 bpfmd
= fsi
->chan_num
* 32;
980 ackmd
= target
/ rate
;
981 ret
= fsi_clk_set_ackbpf(dev
, fsi
, ackmd
, bpfmd
);
983 dev_err(dev
, "%s failed", __func__
);
990 * [CPG] = cout => [FSI_DIV] = audio => [FSI] => [codec]
992 * But, it needs to find best match of CPG and FSI_DIV
993 * combination, since it is difficult to generate correct
994 * frequency of audio clock from ick clock only.
995 * Because ick is created from its parent clock.
997 * target = rate x [512/256/128/64]fs
998 * cout = round(target x adjustment)
999 * actual = cout / adjustment (by FSI-DIV) ~= target
1005 for (adj
= 1; adj
< 0xffff; adj
++) {
1007 cout
= target
* adj
;
1008 if (cout
> 100000000) /* max clock = 100MHz */
1011 /* cout/actual audio clock */
1012 cout
= clk_round_rate(ick
, cout
);
1013 actual
= cout
/ adj
;
1015 /* find best frequency */
1016 diff
= abs(actual
- target
);
1024 ret
= clk_set_rate(ick
, best_cout
);
1026 dev_err(dev
, "ick clock failed\n");
1030 ret
= clk_set_rate(div
, clk_round_rate(div
, best_act
));
1032 dev_err(dev
, "div clock failed\n");
1036 dev_dbg(dev
, "ick/div = %ld/%ld\n",
1037 clk_get_rate(ick
), clk_get_rate(div
));
1043 * pio data transfer handler
1045 static void fsi_pio_push16(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1049 if (fsi_is_enable_stream(fsi
)) {
1053 * fsi_pio_push_init()
1055 u32
*buf
= (u32
*)_buf
;
1057 for (i
= 0; i
< samples
/ 2; i
++)
1058 fsi_reg_write(fsi
, DODT
, buf
[i
]);
1061 u16
*buf
= (u16
*)_buf
;
1063 for (i
= 0; i
< samples
; i
++)
1064 fsi_reg_write(fsi
, DODT
, ((u32
)*(buf
+ i
) << 8));
1068 static void fsi_pio_pop16(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1070 u16
*buf
= (u16
*)_buf
;
1073 for (i
= 0; i
< samples
; i
++)
1074 *(buf
+ i
) = (u16
)(fsi_reg_read(fsi
, DIDT
) >> 8);
1077 static void fsi_pio_push32(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1079 u32
*buf
= (u32
*)_buf
;
1082 for (i
= 0; i
< samples
; i
++)
1083 fsi_reg_write(fsi
, DODT
, *(buf
+ i
));
1086 static void fsi_pio_pop32(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1088 u32
*buf
= (u32
*)_buf
;
1091 for (i
= 0; i
< samples
; i
++)
1092 *(buf
+ i
) = fsi_reg_read(fsi
, DIDT
);
1095 static u8
*fsi_pio_get_area(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1097 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1099 return runtime
->dma_area
+
1100 samples_to_bytes(runtime
, io
->buff_sample_pos
);
1103 static int fsi_pio_transfer(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
1104 void (*run16
)(struct fsi_priv
*fsi
, u8
*buf
, int samples
),
1105 void (*run32
)(struct fsi_priv
*fsi
, u8
*buf
, int samples
),
1108 struct snd_pcm_runtime
*runtime
;
1109 struct snd_pcm_substream
*substream
;
1113 if (!fsi_stream_is_working(fsi
, io
))
1117 substream
= io
->substream
;
1118 runtime
= substream
->runtime
;
1120 /* FSI FIFO has limit.
1121 * So, this driver can not send periods data at a time
1123 if (io
->buff_sample_pos
>=
1124 io
->period_samples
* (io
->period_pos
+ 1)) {
1127 io
->period_pos
= (io
->period_pos
+ 1) % runtime
->periods
;
1129 if (0 == io
->period_pos
)
1130 io
->buff_sample_pos
= 0;
1133 buf
= fsi_pio_get_area(fsi
, io
);
1135 switch (io
->sample_width
) {
1137 run16(fsi
, buf
, samples
);
1140 run32(fsi
, buf
, samples
);
1146 /* update buff_sample_pos */
1147 io
->buff_sample_pos
+= samples
;
1150 snd_pcm_period_elapsed(substream
);
1155 static int fsi_pio_pop(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1157 int sample_residues
; /* samples in FSI fifo */
1158 int sample_space
; /* ALSA free samples space */
1161 sample_residues
= fsi_get_current_fifo_samples(fsi
, io
);
1162 sample_space
= io
->buff_sample_capa
- io
->buff_sample_pos
;
1164 samples
= min(sample_residues
, sample_space
);
1166 return fsi_pio_transfer(fsi
, io
,
1172 static int fsi_pio_push(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1174 int sample_residues
; /* ALSA residue samples */
1175 int sample_space
; /* FSI fifo free samples space */
1178 sample_residues
= io
->buff_sample_capa
- io
->buff_sample_pos
;
1179 sample_space
= io
->fifo_sample_capa
-
1180 fsi_get_current_fifo_samples(fsi
, io
);
1182 samples
= min(sample_residues
, sample_space
);
1184 return fsi_pio_transfer(fsi
, io
,
1190 static void fsi_pio_start_stop(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
1193 struct fsi_master
*master
= fsi_get_master(fsi
);
1194 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
1197 fsi_irq_enable(fsi
, io
);
1199 fsi_irq_disable(fsi
, io
);
1201 if (fsi_is_clk_master(fsi
))
1202 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
1205 static int fsi_pio_push_init(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1208 * we can use 16bit stream mode
1209 * when "playback" and "16bit data"
1210 * and platform allows "stream mode"
1214 if (fsi_is_enable_stream(fsi
))
1215 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1216 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM
);
1218 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1219 BUSOP_SET(16, PACKAGE_24BITBUS_BACK
);
1223 static int fsi_pio_pop_init(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1226 * always 24bit bus, package back when "capture"
1228 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1229 BUSOP_SET(16, PACKAGE_24BITBUS_BACK
);
1233 static struct fsi_stream_handler fsi_pio_push_handler
= {
1234 .init
= fsi_pio_push_init
,
1235 .transfer
= fsi_pio_push
,
1236 .start_stop
= fsi_pio_start_stop
,
1239 static struct fsi_stream_handler fsi_pio_pop_handler
= {
1240 .init
= fsi_pio_pop_init
,
1241 .transfer
= fsi_pio_pop
,
1242 .start_stop
= fsi_pio_start_stop
,
1245 static irqreturn_t
fsi_interrupt(int irq
, void *data
)
1247 struct fsi_master
*master
= data
;
1248 u32 int_st
= fsi_irq_get_status(master
);
1250 /* clear irq status */
1251 fsi_master_mask_set(master
, SOFT_RST
, IR
, 0);
1252 fsi_master_mask_set(master
, SOFT_RST
, IR
, IR
);
1254 if (int_st
& AB_IO(1, AO_SHIFT
))
1255 fsi_stream_transfer(&master
->fsia
.playback
);
1256 if (int_st
& AB_IO(1, BO_SHIFT
))
1257 fsi_stream_transfer(&master
->fsib
.playback
);
1258 if (int_st
& AB_IO(1, AI_SHIFT
))
1259 fsi_stream_transfer(&master
->fsia
.capture
);
1260 if (int_st
& AB_IO(1, BI_SHIFT
))
1261 fsi_stream_transfer(&master
->fsib
.capture
);
1263 fsi_count_fifo_err(&master
->fsia
);
1264 fsi_count_fifo_err(&master
->fsib
);
1266 fsi_irq_clear_status(&master
->fsia
);
1267 fsi_irq_clear_status(&master
->fsib
);
1273 * dma data transfer handler
1275 static int fsi_dma_init(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1277 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1278 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
1279 enum dma_data_direction dir
= fsi_stream_is_play(fsi
, io
) ?
1280 DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1283 * 24bit data : 24bit bus / package in back
1284 * 16bit data : 16bit bus / stream mode
1286 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1287 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM
);
1289 io
->dma
= dma_map_single(dai
->dev
, runtime
->dma_area
,
1290 snd_pcm_lib_buffer_bytes(io
->substream
), dir
);
1294 static int fsi_dma_quit(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1296 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
1297 enum dma_data_direction dir
= fsi_stream_is_play(fsi
, io
) ?
1298 DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1300 dma_unmap_single(dai
->dev
, io
->dma
,
1301 snd_pcm_lib_buffer_bytes(io
->substream
), dir
);
1305 static dma_addr_t
fsi_dma_get_area(struct fsi_stream
*io
)
1307 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1309 return io
->dma
+ samples_to_bytes(runtime
, io
->buff_sample_pos
);
1312 static void fsi_dma_complete(void *data
)
1314 struct fsi_stream
*io
= (struct fsi_stream
*)data
;
1315 struct fsi_priv
*fsi
= fsi_stream_to_priv(io
);
1316 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1317 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
1318 enum dma_data_direction dir
= fsi_stream_is_play(fsi
, io
) ?
1319 DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1321 dma_sync_single_for_cpu(dai
->dev
, fsi_dma_get_area(io
),
1322 samples_to_bytes(runtime
, io
->period_samples
), dir
);
1324 io
->buff_sample_pos
+= io
->period_samples
;
1327 if (io
->period_pos
>= runtime
->periods
) {
1329 io
->buff_sample_pos
= 0;
1332 fsi_count_fifo_err(fsi
);
1333 fsi_stream_transfer(io
);
1335 snd_pcm_period_elapsed(io
->substream
);
1338 static void fsi_dma_do_work(struct work_struct
*work
)
1340 struct fsi_stream
*io
= container_of(work
, struct fsi_stream
, work
);
1341 struct fsi_priv
*fsi
= fsi_stream_to_priv(io
);
1342 struct snd_soc_dai
*dai
;
1343 struct dma_async_tx_descriptor
*desc
;
1344 struct snd_pcm_runtime
*runtime
;
1345 enum dma_data_direction dir
;
1346 int is_play
= fsi_stream_is_play(fsi
, io
);
1350 if (!fsi_stream_is_working(fsi
, io
))
1353 dai
= fsi_get_dai(io
->substream
);
1354 runtime
= io
->substream
->runtime
;
1355 dir
= is_play
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1356 len
= samples_to_bytes(runtime
, io
->period_samples
);
1357 buf
= fsi_dma_get_area(io
);
1359 dma_sync_single_for_device(dai
->dev
, buf
, len
, dir
);
1361 desc
= dmaengine_prep_slave_single(io
->chan
, buf
, len
, dir
,
1362 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1364 dev_err(dai
->dev
, "dmaengine_prep_slave_sg() fail\n");
1368 desc
->callback
= fsi_dma_complete
;
1369 desc
->callback_param
= io
;
1371 if (dmaengine_submit(desc
) < 0) {
1372 dev_err(dai
->dev
, "tx_submit() fail\n");
1376 dma_async_issue_pending(io
->chan
);
1381 * In DMAEngine case, codec and FSI cannot be started simultaneously
1382 * since FSI is using the scheduler work queue.
1383 * Therefore, in capture case, probably FSI FIFO will have got
1384 * overflow error in this point.
1385 * in that case, DMA cannot start transfer until error was cleared.
1388 if (ERR_OVER
& fsi_reg_read(fsi
, DIFF_ST
)) {
1389 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
1390 fsi_reg_write(fsi
, DIFF_ST
, 0);
1395 static bool fsi_dma_filter(struct dma_chan
*chan
, void *param
)
1397 struct sh_dmae_slave
*slave
= param
;
1399 chan
->private = slave
;
1404 static int fsi_dma_transfer(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1406 schedule_work(&io
->work
);
1411 static void fsi_dma_push_start_stop(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
1414 struct fsi_master
*master
= fsi_get_master(fsi
);
1415 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
1416 u32 enable
= start
? DMA_ON
: 0;
1418 fsi_reg_mask_set(fsi
, OUT_DMAC
, DMA_ON
, enable
);
1420 dmaengine_terminate_all(io
->chan
);
1422 if (fsi_is_clk_master(fsi
))
1423 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
1426 static int fsi_dma_probe(struct fsi_priv
*fsi
, struct fsi_stream
*io
, struct device
*dev
)
1428 dma_cap_mask_t mask
;
1431 dma_cap_set(DMA_SLAVE
, mask
);
1433 io
->chan
= dma_request_channel(mask
, fsi_dma_filter
, &io
->slave
);
1436 /* switch to PIO handler */
1437 if (fsi_stream_is_play(fsi
, io
))
1438 fsi
->playback
.handler
= &fsi_pio_push_handler
;
1440 fsi
->capture
.handler
= &fsi_pio_pop_handler
;
1442 dev_info(dev
, "switch handler (dma => pio)\n");
1445 return fsi_stream_probe(fsi
, dev
);
1448 INIT_WORK(&io
->work
, fsi_dma_do_work
);
1453 static int fsi_dma_remove(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1455 cancel_work_sync(&io
->work
);
1457 fsi_stream_stop(fsi
, io
);
1460 dma_release_channel(io
->chan
);
1466 static struct fsi_stream_handler fsi_dma_push_handler
= {
1467 .init
= fsi_dma_init
,
1468 .quit
= fsi_dma_quit
,
1469 .probe
= fsi_dma_probe
,
1470 .transfer
= fsi_dma_transfer
,
1471 .remove
= fsi_dma_remove
,
1472 .start_stop
= fsi_dma_push_start_stop
,
1478 static void fsi_fifo_init(struct fsi_priv
*fsi
,
1479 struct fsi_stream
*io
,
1482 struct fsi_master
*master
= fsi_get_master(fsi
);
1483 int is_play
= fsi_stream_is_play(fsi
, io
);
1487 /* get on-chip RAM capacity */
1488 shift
= fsi_master_read(master
, FIFO_SZ
);
1489 shift
>>= fsi_get_port_shift(fsi
, io
);
1490 shift
&= FIFO_SZ_MASK
;
1491 frame_capa
= 256 << shift
;
1492 dev_dbg(dev
, "fifo = %d words\n", frame_capa
);
1495 * The maximum number of sample data varies depending
1496 * on the number of channels selected for the format.
1498 * FIFOs are used in 4-channel units in 3-channel mode
1499 * and in 8-channel units in 5- to 7-channel mode
1500 * meaning that more FIFOs than the required size of DPRAM
1503 * ex) if 256 words of DP-RAM is connected
1504 * 1 channel: 256 (256 x 1 = 256)
1505 * 2 channels: 128 (128 x 2 = 256)
1506 * 3 channels: 64 ( 64 x 3 = 192)
1507 * 4 channels: 64 ( 64 x 4 = 256)
1508 * 5 channels: 32 ( 32 x 5 = 160)
1509 * 6 channels: 32 ( 32 x 6 = 192)
1510 * 7 channels: 32 ( 32 x 7 = 224)
1511 * 8 channels: 32 ( 32 x 8 = 256)
1513 for (i
= 1; i
< fsi
->chan_num
; i
<<= 1)
1515 dev_dbg(dev
, "%d channel %d store\n",
1516 fsi
->chan_num
, frame_capa
);
1518 io
->fifo_sample_capa
= fsi_frame2sample(fsi
, frame_capa
);
1521 * set interrupt generation factor
1525 fsi_reg_write(fsi
, DOFF_CTL
, IRQ_HALF
);
1526 fsi_reg_mask_set(fsi
, DOFF_CTL
, FIFO_CLR
, FIFO_CLR
);
1528 fsi_reg_write(fsi
, DIFF_CTL
, IRQ_HALF
);
1529 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
1533 static int fsi_hw_startup(struct fsi_priv
*fsi
,
1534 struct fsi_stream
*io
,
1540 if (fsi_is_clk_master(fsi
))
1543 fsi_reg_mask_set(fsi
, CKG1
, (DIMD
| DOMD
), data
);
1545 /* clock inversion (CKG2) */
1547 if (fsi
->bit_clk_inv
)
1549 if (fsi
->lr_clk_inv
)
1551 if (fsi_is_clk_master(fsi
))
1553 fsi_reg_write(fsi
, CKG2
, data
);
1556 if (fsi_is_spdif(fsi
)) {
1557 fsi_spdif_clk_ctrl(fsi
, 1);
1558 fsi_reg_mask_set(fsi
, OUT_SEL
, DMMD
, DMMD
);
1565 switch (io
->sample_width
) {
1567 data
= BUSOP_GET(16, io
->bus_option
);
1570 data
= BUSOP_GET(24, io
->bus_option
);
1573 fsi_format_bus_setup(fsi
, io
, data
, dev
);
1576 fsi_irq_disable(fsi
, io
);
1577 fsi_irq_clear_status(fsi
);
1580 fsi_fifo_init(fsi
, io
, dev
);
1582 /* start master clock */
1583 if (fsi_is_clk_master(fsi
))
1584 return fsi_clk_enable(dev
, fsi
);
1589 static int fsi_hw_shutdown(struct fsi_priv
*fsi
,
1592 /* stop master clock */
1593 if (fsi_is_clk_master(fsi
))
1594 return fsi_clk_disable(dev
, fsi
);
1599 static int fsi_dai_startup(struct snd_pcm_substream
*substream
,
1600 struct snd_soc_dai
*dai
)
1602 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1604 fsi_clk_invalid(fsi
);
1609 static void fsi_dai_shutdown(struct snd_pcm_substream
*substream
,
1610 struct snd_soc_dai
*dai
)
1612 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1614 fsi_clk_invalid(fsi
);
1617 static int fsi_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
1618 struct snd_soc_dai
*dai
)
1620 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1621 struct fsi_stream
*io
= fsi_stream_get(fsi
, substream
);
1625 case SNDRV_PCM_TRIGGER_START
:
1626 fsi_stream_init(fsi
, io
, substream
);
1628 ret
= fsi_hw_startup(fsi
, io
, dai
->dev
);
1630 ret
= fsi_stream_transfer(io
);
1632 fsi_stream_start(fsi
, io
);
1634 case SNDRV_PCM_TRIGGER_STOP
:
1636 ret
= fsi_hw_shutdown(fsi
, dai
->dev
);
1637 fsi_stream_stop(fsi
, io
);
1638 fsi_stream_quit(fsi
, io
);
1645 static int fsi_set_fmt_dai(struct fsi_priv
*fsi
, unsigned int fmt
)
1647 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1648 case SND_SOC_DAIFMT_I2S
:
1652 case SND_SOC_DAIFMT_LEFT_J
:
1663 static int fsi_set_fmt_spdif(struct fsi_priv
*fsi
)
1665 struct fsi_master
*master
= fsi_get_master(fsi
);
1667 if (fsi_version(master
) < 2)
1670 fsi
->fmt
= CR_DTMD_SPDIF_PCM
| CR_PCM
;
1676 static int fsi_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1678 struct fsi_priv
*fsi
= fsi_get_priv_frm_dai(dai
);
1681 /* set master/slave audio interface */
1682 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1683 case SND_SOC_DAIFMT_CBM_CFM
:
1684 fsi
->clk_master
= 1;
1686 case SND_SOC_DAIFMT_CBS_CFS
:
1692 /* set clock inversion */
1693 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
1694 case SND_SOC_DAIFMT_NB_IF
:
1695 fsi
->bit_clk_inv
= 0;
1696 fsi
->lr_clk_inv
= 1;
1698 case SND_SOC_DAIFMT_IB_NF
:
1699 fsi
->bit_clk_inv
= 1;
1700 fsi
->lr_clk_inv
= 0;
1702 case SND_SOC_DAIFMT_IB_IF
:
1703 fsi
->bit_clk_inv
= 1;
1704 fsi
->lr_clk_inv
= 1;
1706 case SND_SOC_DAIFMT_NB_NF
:
1708 fsi
->bit_clk_inv
= 0;
1709 fsi
->lr_clk_inv
= 0;
1713 if (fsi_is_clk_master(fsi
)) {
1715 fsi_clk_init(dai
->dev
, fsi
, 0, 1, 1,
1716 fsi_clk_set_rate_cpg
);
1718 fsi_clk_init(dai
->dev
, fsi
, 1, 1, 0,
1719 fsi_clk_set_rate_external
);
1723 if (fsi_is_spdif(fsi
))
1724 ret
= fsi_set_fmt_spdif(fsi
);
1726 ret
= fsi_set_fmt_dai(fsi
, fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
1731 static int fsi_dai_hw_params(struct snd_pcm_substream
*substream
,
1732 struct snd_pcm_hw_params
*params
,
1733 struct snd_soc_dai
*dai
)
1735 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1737 if (fsi_is_clk_master(fsi
))
1738 fsi_clk_valid(fsi
, params_rate(params
));
1743 static const struct snd_soc_dai_ops fsi_dai_ops
= {
1744 .startup
= fsi_dai_startup
,
1745 .shutdown
= fsi_dai_shutdown
,
1746 .trigger
= fsi_dai_trigger
,
1747 .set_fmt
= fsi_dai_set_fmt
,
1748 .hw_params
= fsi_dai_hw_params
,
1755 static struct snd_pcm_hardware fsi_pcm_hardware
= {
1756 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
1757 SNDRV_PCM_INFO_MMAP
|
1758 SNDRV_PCM_INFO_MMAP_VALID
|
1759 SNDRV_PCM_INFO_PAUSE
,
1760 .formats
= FSI_FMTS
,
1766 .buffer_bytes_max
= 64 * 1024,
1767 .period_bytes_min
= 32,
1768 .period_bytes_max
= 8192,
1774 static int fsi_pcm_open(struct snd_pcm_substream
*substream
)
1776 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1779 snd_soc_set_runtime_hwparams(substream
, &fsi_pcm_hardware
);
1781 ret
= snd_pcm_hw_constraint_integer(runtime
,
1782 SNDRV_PCM_HW_PARAM_PERIODS
);
1787 static int fsi_hw_params(struct snd_pcm_substream
*substream
,
1788 struct snd_pcm_hw_params
*hw_params
)
1790 return snd_pcm_lib_malloc_pages(substream
,
1791 params_buffer_bytes(hw_params
));
1794 static int fsi_hw_free(struct snd_pcm_substream
*substream
)
1796 return snd_pcm_lib_free_pages(substream
);
1799 static snd_pcm_uframes_t
fsi_pointer(struct snd_pcm_substream
*substream
)
1801 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1802 struct fsi_stream
*io
= fsi_stream_get(fsi
, substream
);
1804 return fsi_sample2frame(fsi
, io
->buff_sample_pos
);
1807 static struct snd_pcm_ops fsi_pcm_ops
= {
1808 .open
= fsi_pcm_open
,
1809 .ioctl
= snd_pcm_lib_ioctl
,
1810 .hw_params
= fsi_hw_params
,
1811 .hw_free
= fsi_hw_free
,
1812 .pointer
= fsi_pointer
,
1819 #define PREALLOC_BUFFER (32 * 1024)
1820 #define PREALLOC_BUFFER_MAX (32 * 1024)
1822 static void fsi_pcm_free(struct snd_pcm
*pcm
)
1824 snd_pcm_lib_preallocate_free_for_all(pcm
);
1827 static int fsi_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
1829 struct snd_pcm
*pcm
= rtd
->pcm
;
1832 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1833 * in MMAP mode (i.e. aplay -M)
1835 return snd_pcm_lib_preallocate_pages_for_all(
1837 SNDRV_DMA_TYPE_CONTINUOUS
,
1838 snd_dma_continuous_data(GFP_KERNEL
),
1839 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1846 static struct snd_soc_dai_driver fsi_soc_dai
[] = {
1851 .formats
= FSI_FMTS
,
1857 .formats
= FSI_FMTS
,
1861 .ops
= &fsi_dai_ops
,
1867 .formats
= FSI_FMTS
,
1873 .formats
= FSI_FMTS
,
1877 .ops
= &fsi_dai_ops
,
1881 static struct snd_soc_platform_driver fsi_soc_platform
= {
1882 .ops
= &fsi_pcm_ops
,
1883 .pcm_new
= fsi_pcm_new
,
1884 .pcm_free
= fsi_pcm_free
,
1890 static void fsi_port_info_init(struct fsi_priv
*fsi
,
1891 struct sh_fsi_port_info
*info
)
1893 if (info
->flags
& SH_FSI_FMT_SPDIF
)
1896 if (info
->flags
& SH_FSI_CLK_CPG
)
1899 if (info
->flags
& SH_FSI_ENABLE_STREAM_MODE
)
1900 fsi
->enable_stream
= 1;
1903 static void fsi_handler_init(struct fsi_priv
*fsi
,
1904 struct sh_fsi_port_info
*info
)
1906 fsi
->playback
.handler
= &fsi_pio_push_handler
; /* default PIO */
1907 fsi
->playback
.priv
= fsi
;
1908 fsi
->capture
.handler
= &fsi_pio_pop_handler
; /* default PIO */
1909 fsi
->capture
.priv
= fsi
;
1912 fsi
->playback
.slave
.shdma_slave
.slave_id
= info
->tx_id
;
1913 fsi
->playback
.handler
= &fsi_dma_push_handler
;
1917 static int fsi_probe(struct platform_device
*pdev
)
1919 struct fsi_master
*master
;
1920 const struct platform_device_id
*id_entry
;
1921 struct sh_fsi_platform_info
*info
= pdev
->dev
.platform_data
;
1922 struct sh_fsi_port_info nul_info
, *pinfo
;
1923 struct fsi_priv
*fsi
;
1924 struct resource
*res
;
1932 id_entry
= pdev
->id_entry
;
1934 dev_err(&pdev
->dev
, "unknown fsi device\n");
1938 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1939 irq
= platform_get_irq(pdev
, 0);
1940 if (!res
|| (int)irq
<= 0) {
1941 dev_err(&pdev
->dev
, "Not enough FSI platform resources.\n");
1945 master
= devm_kzalloc(&pdev
->dev
, sizeof(*master
), GFP_KERNEL
);
1947 dev_err(&pdev
->dev
, "Could not allocate master\n");
1951 master
->base
= devm_ioremap_nocache(&pdev
->dev
,
1952 res
->start
, resource_size(res
));
1953 if (!master
->base
) {
1954 dev_err(&pdev
->dev
, "Unable to ioremap FSI registers.\n");
1958 /* master setting */
1960 master
->core
= (struct fsi_core
*)id_entry
->driver_data
;
1961 spin_lock_init(&master
->lock
);
1964 pinfo
= (info
) ? &info
->port_a
: &nul_info
;
1965 fsi
= &master
->fsia
;
1966 fsi
->base
= master
->base
;
1967 fsi
->master
= master
;
1968 fsi_port_info_init(fsi
, pinfo
);
1969 fsi_handler_init(fsi
, pinfo
);
1970 ret
= fsi_stream_probe(fsi
, &pdev
->dev
);
1972 dev_err(&pdev
->dev
, "FSIA stream probe failed\n");
1977 pinfo
= (info
) ? &info
->port_b
: &nul_info
;
1978 fsi
= &master
->fsib
;
1979 fsi
->base
= master
->base
+ 0x40;
1980 fsi
->master
= master
;
1981 fsi_port_info_init(fsi
, pinfo
);
1982 fsi_handler_init(fsi
, pinfo
);
1983 ret
= fsi_stream_probe(fsi
, &pdev
->dev
);
1985 dev_err(&pdev
->dev
, "FSIB stream probe failed\n");
1989 pm_runtime_enable(&pdev
->dev
);
1990 dev_set_drvdata(&pdev
->dev
, master
);
1992 ret
= devm_request_irq(&pdev
->dev
, irq
, &fsi_interrupt
, 0,
1993 id_entry
->name
, master
);
1995 dev_err(&pdev
->dev
, "irq request err\n");
1999 ret
= snd_soc_register_platform(&pdev
->dev
, &fsi_soc_platform
);
2001 dev_err(&pdev
->dev
, "cannot snd soc register\n");
2005 ret
= snd_soc_register_dais(&pdev
->dev
, fsi_soc_dai
,
2006 ARRAY_SIZE(fsi_soc_dai
));
2008 dev_err(&pdev
->dev
, "cannot snd dai register\n");
2015 snd_soc_unregister_platform(&pdev
->dev
);
2017 pm_runtime_disable(&pdev
->dev
);
2018 fsi_stream_remove(&master
->fsib
);
2020 fsi_stream_remove(&master
->fsia
);
2025 static int fsi_remove(struct platform_device
*pdev
)
2027 struct fsi_master
*master
;
2029 master
= dev_get_drvdata(&pdev
->dev
);
2031 pm_runtime_disable(&pdev
->dev
);
2033 snd_soc_unregister_dais(&pdev
->dev
, ARRAY_SIZE(fsi_soc_dai
));
2034 snd_soc_unregister_platform(&pdev
->dev
);
2036 fsi_stream_remove(&master
->fsia
);
2037 fsi_stream_remove(&master
->fsib
);
2042 static void __fsi_suspend(struct fsi_priv
*fsi
,
2043 struct fsi_stream
*io
,
2046 if (!fsi_stream_is_working(fsi
, io
))
2049 fsi_stream_stop(fsi
, io
);
2050 fsi_hw_shutdown(fsi
, dev
);
2053 static void __fsi_resume(struct fsi_priv
*fsi
,
2054 struct fsi_stream
*io
,
2057 if (!fsi_stream_is_working(fsi
, io
))
2060 fsi_hw_startup(fsi
, io
, dev
);
2061 fsi_stream_start(fsi
, io
);
2064 static int fsi_suspend(struct device
*dev
)
2066 struct fsi_master
*master
= dev_get_drvdata(dev
);
2067 struct fsi_priv
*fsia
= &master
->fsia
;
2068 struct fsi_priv
*fsib
= &master
->fsib
;
2070 __fsi_suspend(fsia
, &fsia
->playback
, dev
);
2071 __fsi_suspend(fsia
, &fsia
->capture
, dev
);
2073 __fsi_suspend(fsib
, &fsib
->playback
, dev
);
2074 __fsi_suspend(fsib
, &fsib
->capture
, dev
);
2079 static int fsi_resume(struct device
*dev
)
2081 struct fsi_master
*master
= dev_get_drvdata(dev
);
2082 struct fsi_priv
*fsia
= &master
->fsia
;
2083 struct fsi_priv
*fsib
= &master
->fsib
;
2085 __fsi_resume(fsia
, &fsia
->playback
, dev
);
2086 __fsi_resume(fsia
, &fsia
->capture
, dev
);
2088 __fsi_resume(fsib
, &fsib
->playback
, dev
);
2089 __fsi_resume(fsib
, &fsib
->capture
, dev
);
2094 static struct dev_pm_ops fsi_pm_ops
= {
2095 .suspend
= fsi_suspend
,
2096 .resume
= fsi_resume
,
2099 static struct fsi_core fsi1_core
= {
2108 static struct fsi_core fsi2_core
= {
2112 .int_st
= CPU_INT_ST
,
2115 .a_mclk
= A_MST_CTLR
,
2116 .b_mclk
= B_MST_CTLR
,
2119 static struct platform_device_id fsi_id_table
[] = {
2120 { "sh_fsi", (kernel_ulong_t
)&fsi1_core
},
2121 { "sh_fsi2", (kernel_ulong_t
)&fsi2_core
},
2124 MODULE_DEVICE_TABLE(platform
, fsi_id_table
);
2126 static struct platform_driver fsi_driver
= {
2128 .name
= "fsi-pcm-audio",
2132 .remove
= fsi_remove
,
2133 .id_table
= fsi_id_table
,
2136 module_platform_driver(fsi_driver
);
2138 MODULE_LICENSE("GPL");
2139 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
2140 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
2141 MODULE_ALIAS("platform:fsi-pcm-audio");