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>
20 #include <linux/of_device.h>
21 #include <linux/scatterlist.h>
22 #include <linux/sh_dma.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/workqueue.h>
26 #include <sound/soc.h>
27 #include <sound/pcm_params.h>
28 #include <sound/sh_fsi.h>
30 /* PortA/PortB register */
31 #define REG_DO_FMT 0x0000
32 #define REG_DOFF_CTL 0x0004
33 #define REG_DOFF_ST 0x0008
34 #define REG_DI_FMT 0x000C
35 #define REG_DIFF_CTL 0x0010
36 #define REG_DIFF_ST 0x0014
37 #define REG_CKG1 0x0018
38 #define REG_CKG2 0x001C
39 #define REG_DIDT 0x0020
40 #define REG_DODT 0x0024
41 #define REG_MUTE_ST 0x0028
42 #define REG_OUT_DMAC 0x002C
43 #define REG_OUT_SEL 0x0030
44 #define REG_IN_DMAC 0x0038
47 #define MST_CLK_RST 0x0210
48 #define MST_SOFT_RST 0x0214
49 #define MST_FIFO_SZ 0x0218
51 /* core register (depend on FSI version) */
52 #define A_MST_CTLR 0x0180
53 #define B_MST_CTLR 0x01A0
54 #define CPU_INT_ST 0x01F4
55 #define CPU_IEMSK 0x01F8
56 #define CPU_IMSK 0x01FC
63 #define CR_BWS_MASK (0x3 << 20) /* FSI2 */
64 #define CR_BWS_24 (0x0 << 20) /* FSI2 */
65 #define CR_BWS_16 (0x1 << 20) /* FSI2 */
66 #define CR_BWS_20 (0x2 << 20) /* FSI2 */
68 #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
69 #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
70 #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
72 #define CR_MONO (0x0 << 4)
73 #define CR_MONO_D (0x1 << 4)
74 #define CR_PCM (0x2 << 4)
75 #define CR_I2S (0x3 << 4)
76 #define CR_TDM (0x4 << 4)
77 #define CR_TDM_D (0x5 << 4)
81 #define VDMD_MASK (0x3 << 4)
82 #define VDMD_FRONT (0x0 << 4) /* Package in front */
83 #define VDMD_BACK (0x1 << 4) /* Package in back */
84 #define VDMD_STREAM (0x2 << 4) /* Stream mode(16bit * 2) */
86 #define DMA_ON (0x1 << 0)
90 #define IRQ_HALF 0x00100000
91 #define FIFO_CLR 0x00000001
94 #define ERR_OVER 0x00000010
95 #define ERR_UNDER 0x00000001
96 #define ST_ERR (ERR_OVER | ERR_UNDER)
99 #define ACKMD_MASK 0x00007000
100 #define BPFMD_MASK 0x00000700
101 #define DIMD (1 << 4)
102 #define DOMD (1 << 0)
105 #define BP (1 << 4) /* Fix the signal of Biphase output */
106 #define SE (1 << 0) /* Fix the master clock */
112 /* IO SHIFT / MACRO */
117 #define AB_IO(param, shift) (param << shift)
120 #define PBSR (1 << 12) /* Port B Software Reset */
121 #define PASR (1 << 8) /* Port A Software Reset */
122 #define IR (1 << 4) /* Interrupt Reset */
123 #define FSISR (1 << 0) /* Software Reset */
126 #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
127 /* 1: Biphase and serial */
130 #define FIFO_SZ_MASK 0x7
132 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
134 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
141 * A : sample widtht 16bit setting
142 * B : sample widtht 24bit setting
145 #define SHIFT_16DATA 0
146 #define SHIFT_24DATA 4
148 #define PACKAGE_24BITBUS_BACK 0
149 #define PACKAGE_24BITBUS_FRONT 1
150 #define PACKAGE_16BITBUS_STREAM 2
152 #define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
153 #define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
156 * FSI driver use below type name for variable
158 * xxx_num : number of data
159 * xxx_pos : position of data
160 * xxx_capa : capacity of data
164 * period/frame/sample image
168 * period pos period pos
170 * |<-------------------- period--------------------->|
171 * ==|============================================ ... =|==
173 * ||<----- frame ----->|<------ frame ----->| ... |
174 * |+--------------------+--------------------+- ... |
175 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
176 * |+--------------------+--------------------+- ... |
177 * ==|============================================ ... =|==
195 * FSIxCLK [CPG] (ick) -------> |
196 * |-> FSI_DIV (div)-> FSI2
197 * FSIxCK [external] (xck) ---> |
204 struct fsi_stream_handler
;
208 * these are initialized by fsi_stream_init()
210 struct snd_pcm_substream
*substream
;
211 int fifo_sample_capa
; /* sample capacity of FSI FIFO */
212 int buff_sample_capa
; /* sample capacity of ALSA buffer */
213 int buff_sample_pos
; /* sample position of ALSA buffer */
214 int period_samples
; /* sample number / 1 period */
215 int period_pos
; /* current period position */
216 int sample_width
; /* sample width */
226 * thse are initialized by fsi_handler_init()
228 struct fsi_stream_handler
*handler
;
229 struct fsi_priv
*priv
;
232 * these are for DMAEngine
234 struct dma_chan
*chan
;
235 struct sh_dmae_slave slave
; /* see fsi_handler_init() */
236 struct work_struct work
;
241 /* see [FSI clock] */
246 int (*set_rate
)(struct device
*dev
,
247 struct fsi_priv
*fsi
);
255 struct fsi_master
*master
;
257 struct fsi_stream playback
;
258 struct fsi_stream capture
;
260 struct fsi_clk clock
;
273 struct fsi_stream_handler
{
274 int (*init
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
275 int (*quit
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
276 int (*probe
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
, struct device
*dev
);
277 int (*transfer
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
278 int (*remove
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
279 void (*start_stop
)(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
282 #define fsi_stream_handler_call(io, func, args...) \
284 !((io)->handler->func) ? 0 : \
285 (io)->handler->func(args))
300 struct fsi_priv fsia
;
301 struct fsi_priv fsib
;
302 const struct fsi_core
*core
;
306 static int fsi_stream_is_play(struct fsi_priv
*fsi
, struct fsi_stream
*io
);
309 * basic read write function
312 static void __fsi_reg_write(u32 __iomem
*reg
, u32 data
)
314 /* valid data area is 24bit */
317 __raw_writel(data
, reg
);
320 static u32
__fsi_reg_read(u32 __iomem
*reg
)
322 return __raw_readl(reg
);
325 static void __fsi_reg_mask_set(u32 __iomem
*reg
, u32 mask
, u32 data
)
327 u32 val
= __fsi_reg_read(reg
);
332 __fsi_reg_write(reg
, val
);
335 #define fsi_reg_write(p, r, d)\
336 __fsi_reg_write((p->base + REG_##r), d)
338 #define fsi_reg_read(p, r)\
339 __fsi_reg_read((p->base + REG_##r))
341 #define fsi_reg_mask_set(p, r, m, d)\
342 __fsi_reg_mask_set((p->base + REG_##r), m, d)
344 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
345 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
346 static u32
_fsi_master_read(struct fsi_master
*master
, u32 reg
)
351 spin_lock_irqsave(&master
->lock
, flags
);
352 ret
= __fsi_reg_read(master
->base
+ reg
);
353 spin_unlock_irqrestore(&master
->lock
, flags
);
358 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
359 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
360 static void _fsi_master_mask_set(struct fsi_master
*master
,
361 u32 reg
, u32 mask
, u32 data
)
365 spin_lock_irqsave(&master
->lock
, flags
);
366 __fsi_reg_mask_set(master
->base
+ reg
, mask
, data
);
367 spin_unlock_irqrestore(&master
->lock
, flags
);
373 static int fsi_version(struct fsi_master
*master
)
375 return master
->core
->ver
;
378 static struct fsi_master
*fsi_get_master(struct fsi_priv
*fsi
)
383 static int fsi_is_clk_master(struct fsi_priv
*fsi
)
385 return fsi
->clk_master
;
388 static int fsi_is_port_a(struct fsi_priv
*fsi
)
390 return fsi
->master
->base
== fsi
->base
;
393 static int fsi_is_spdif(struct fsi_priv
*fsi
)
398 static int fsi_is_enable_stream(struct fsi_priv
*fsi
)
400 return fsi
->enable_stream
;
403 static int fsi_is_play(struct snd_pcm_substream
*substream
)
405 return substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
408 static struct snd_soc_dai
*fsi_get_dai(struct snd_pcm_substream
*substream
)
410 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
415 static struct fsi_priv
*fsi_get_priv_frm_dai(struct snd_soc_dai
*dai
)
417 struct fsi_master
*master
= snd_soc_dai_get_drvdata(dai
);
420 return &master
->fsia
;
422 return &master
->fsib
;
425 static struct fsi_priv
*fsi_get_priv(struct snd_pcm_substream
*substream
)
427 return fsi_get_priv_frm_dai(fsi_get_dai(substream
));
430 static u32
fsi_get_port_shift(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
432 int is_play
= fsi_stream_is_play(fsi
, io
);
433 int is_porta
= fsi_is_port_a(fsi
);
437 shift
= is_play
? AO_SHIFT
: AI_SHIFT
;
439 shift
= is_play
? BO_SHIFT
: BI_SHIFT
;
444 static int fsi_frame2sample(struct fsi_priv
*fsi
, int frames
)
446 return frames
* fsi
->chan_num
;
449 static int fsi_sample2frame(struct fsi_priv
*fsi
, int samples
)
451 return samples
/ fsi
->chan_num
;
454 static int fsi_get_current_fifo_samples(struct fsi_priv
*fsi
,
455 struct fsi_stream
*io
)
457 int is_play
= fsi_stream_is_play(fsi
, io
);
462 fsi_reg_read(fsi
, DOFF_ST
) :
463 fsi_reg_read(fsi
, DIFF_ST
);
465 frames
= 0x1ff & (status
>> 8);
467 return fsi_frame2sample(fsi
, frames
);
470 static void fsi_count_fifo_err(struct fsi_priv
*fsi
)
472 u32 ostatus
= fsi_reg_read(fsi
, DOFF_ST
);
473 u32 istatus
= fsi_reg_read(fsi
, DIFF_ST
);
475 if (ostatus
& ERR_OVER
)
476 fsi
->playback
.oerr_num
++;
478 if (ostatus
& ERR_UNDER
)
479 fsi
->playback
.uerr_num
++;
481 if (istatus
& ERR_OVER
)
482 fsi
->capture
.oerr_num
++;
484 if (istatus
& ERR_UNDER
)
485 fsi
->capture
.uerr_num
++;
487 fsi_reg_write(fsi
, DOFF_ST
, 0);
488 fsi_reg_write(fsi
, DIFF_ST
, 0);
492 * fsi_stream_xx() function
494 static inline int fsi_stream_is_play(struct fsi_priv
*fsi
,
495 struct fsi_stream
*io
)
497 return &fsi
->playback
== io
;
500 static inline struct fsi_stream
*fsi_stream_get(struct fsi_priv
*fsi
,
501 struct snd_pcm_substream
*substream
)
503 return fsi_is_play(substream
) ? &fsi
->playback
: &fsi
->capture
;
506 static int fsi_stream_is_working(struct fsi_priv
*fsi
,
507 struct fsi_stream
*io
)
509 struct fsi_master
*master
= fsi_get_master(fsi
);
513 spin_lock_irqsave(&master
->lock
, flags
);
514 ret
= !!(io
->substream
&& io
->substream
->runtime
);
515 spin_unlock_irqrestore(&master
->lock
, flags
);
520 static struct fsi_priv
*fsi_stream_to_priv(struct fsi_stream
*io
)
525 static void fsi_stream_init(struct fsi_priv
*fsi
,
526 struct fsi_stream
*io
,
527 struct snd_pcm_substream
*substream
)
529 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
530 struct fsi_master
*master
= fsi_get_master(fsi
);
533 spin_lock_irqsave(&master
->lock
, flags
);
534 io
->substream
= substream
;
535 io
->buff_sample_capa
= fsi_frame2sample(fsi
, runtime
->buffer_size
);
536 io
->buff_sample_pos
= 0;
537 io
->period_samples
= fsi_frame2sample(fsi
, runtime
->period_size
);
539 io
->sample_width
= samples_to_bytes(runtime
, 1);
541 io
->oerr_num
= -1; /* ignore 1st err */
542 io
->uerr_num
= -1; /* ignore 1st err */
543 fsi_stream_handler_call(io
, init
, fsi
, io
);
544 spin_unlock_irqrestore(&master
->lock
, flags
);
547 static void fsi_stream_quit(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
549 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
550 struct fsi_master
*master
= fsi_get_master(fsi
);
553 spin_lock_irqsave(&master
->lock
, flags
);
555 if (io
->oerr_num
> 0)
556 dev_err(dai
->dev
, "over_run = %d\n", io
->oerr_num
);
558 if (io
->uerr_num
> 0)
559 dev_err(dai
->dev
, "under_run = %d\n", io
->uerr_num
);
561 fsi_stream_handler_call(io
, quit
, fsi
, io
);
562 io
->substream
= NULL
;
563 io
->buff_sample_capa
= 0;
564 io
->buff_sample_pos
= 0;
565 io
->period_samples
= 0;
567 io
->sample_width
= 0;
571 spin_unlock_irqrestore(&master
->lock
, flags
);
574 static int fsi_stream_transfer(struct fsi_stream
*io
)
576 struct fsi_priv
*fsi
= fsi_stream_to_priv(io
);
580 return fsi_stream_handler_call(io
, transfer
, fsi
, io
);
583 #define fsi_stream_start(fsi, io)\
584 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
586 #define fsi_stream_stop(fsi, io)\
587 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
589 static int fsi_stream_probe(struct fsi_priv
*fsi
, struct device
*dev
)
591 struct fsi_stream
*io
;
595 ret1
= fsi_stream_handler_call(io
, probe
, fsi
, io
, dev
);
598 ret2
= fsi_stream_handler_call(io
, probe
, fsi
, io
, dev
);
608 static int fsi_stream_remove(struct fsi_priv
*fsi
)
610 struct fsi_stream
*io
;
614 ret1
= fsi_stream_handler_call(io
, remove
, fsi
, io
);
617 ret2
= fsi_stream_handler_call(io
, remove
, fsi
, io
);
628 * format/bus/dma setting
630 static void fsi_format_bus_setup(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
631 u32 bus
, struct device
*dev
)
633 struct fsi_master
*master
= fsi_get_master(fsi
);
634 int is_play
= fsi_stream_is_play(fsi
, io
);
637 if (fsi_version(master
) >= 2) {
641 * FSI2 needs DMA/Bus setting
644 case PACKAGE_24BITBUS_FRONT
:
647 dev_dbg(dev
, "24bit bus / package in front\n");
649 case PACKAGE_16BITBUS_STREAM
:
652 dev_dbg(dev
, "16bit bus / stream mode\n");
654 case PACKAGE_24BITBUS_BACK
:
658 dev_dbg(dev
, "24bit bus / package in back\n");
663 fsi_reg_write(fsi
, OUT_DMAC
, dma
);
665 fsi_reg_write(fsi
, IN_DMAC
, dma
);
669 fsi_reg_write(fsi
, DO_FMT
, fmt
);
671 fsi_reg_write(fsi
, DI_FMT
, fmt
);
678 static void fsi_irq_enable(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
680 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, io
));
681 struct fsi_master
*master
= fsi_get_master(fsi
);
683 fsi_core_mask_set(master
, imsk
, data
, data
);
684 fsi_core_mask_set(master
, iemsk
, data
, data
);
687 static void fsi_irq_disable(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
689 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, io
));
690 struct fsi_master
*master
= fsi_get_master(fsi
);
692 fsi_core_mask_set(master
, imsk
, data
, 0);
693 fsi_core_mask_set(master
, iemsk
, data
, 0);
696 static u32
fsi_irq_get_status(struct fsi_master
*master
)
698 return fsi_core_read(master
, int_st
);
701 static void fsi_irq_clear_status(struct fsi_priv
*fsi
)
704 struct fsi_master
*master
= fsi_get_master(fsi
);
706 data
|= AB_IO(1, fsi_get_port_shift(fsi
, &fsi
->playback
));
707 data
|= AB_IO(1, fsi_get_port_shift(fsi
, &fsi
->capture
));
709 /* clear interrupt factor */
710 fsi_core_mask_set(master
, int_st
, data
, 0);
714 * SPDIF master clock function
716 * These functions are used later FSI2
718 static void fsi_spdif_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
720 struct fsi_master
*master
= fsi_get_master(fsi
);
724 val
= enable
? mask
: 0;
727 fsi_core_mask_set(master
, a_mclk
, mask
, val
) :
728 fsi_core_mask_set(master
, b_mclk
, mask
, val
);
734 static int fsi_clk_init(struct device
*dev
,
735 struct fsi_priv
*fsi
,
739 int (*set_rate
)(struct device
*dev
,
740 struct fsi_priv
*fsi
))
742 struct fsi_clk
*clock
= &fsi
->clock
;
743 int is_porta
= fsi_is_port_a(fsi
);
750 clock
->set_rate
= set_rate
;
752 clock
->own
= devm_clk_get(dev
, NULL
);
753 if (IS_ERR(clock
->own
))
758 clock
->xck
= devm_clk_get(dev
, is_porta
? "xcka" : "xckb");
759 if (IS_ERR(clock
->xck
)) {
760 dev_err(dev
, "can't get xck clock\n");
763 if (clock
->xck
== clock
->own
) {
764 dev_err(dev
, "cpu doesn't support xck clock\n");
769 /* FSIACLK/FSIBCLK */
771 clock
->ick
= devm_clk_get(dev
, is_porta
? "icka" : "ickb");
772 if (IS_ERR(clock
->ick
)) {
773 dev_err(dev
, "can't get ick clock\n");
776 if (clock
->ick
== clock
->own
) {
777 dev_err(dev
, "cpu doesn't support ick clock\n");
784 clock
->div
= devm_clk_get(dev
, is_porta
? "diva" : "divb");
785 if (IS_ERR(clock
->div
)) {
786 dev_err(dev
, "can't get div clock\n");
789 if (clock
->div
== clock
->own
) {
790 dev_err(dev
, "cpu doens't support div clock\n");
798 #define fsi_clk_invalid(fsi) fsi_clk_valid(fsi, 0)
799 static void fsi_clk_valid(struct fsi_priv
*fsi
, unsigned long rate
)
801 fsi
->clock
.rate
= rate
;
804 static int fsi_clk_is_valid(struct fsi_priv
*fsi
)
806 return fsi
->clock
.set_rate
&&
810 static int fsi_clk_enable(struct device
*dev
,
811 struct fsi_priv
*fsi
)
813 struct fsi_clk
*clock
= &fsi
->clock
;
816 if (!fsi_clk_is_valid(fsi
))
819 if (0 == clock
->count
) {
820 ret
= clock
->set_rate(dev
, fsi
);
822 fsi_clk_invalid(fsi
);
827 clk_enable(clock
->xck
);
829 clk_enable(clock
->ick
);
831 clk_enable(clock
->div
);
839 static int fsi_clk_disable(struct device
*dev
,
840 struct fsi_priv
*fsi
)
842 struct fsi_clk
*clock
= &fsi
->clock
;
844 if (!fsi_clk_is_valid(fsi
))
847 if (1 == clock
->count
--) {
849 clk_disable(clock
->xck
);
851 clk_disable(clock
->ick
);
853 clk_disable(clock
->div
);
859 static int fsi_clk_set_ackbpf(struct device
*dev
,
860 struct fsi_priv
*fsi
,
861 int ackmd
, int bpfmd
)
865 /* check ackmd/bpfmd relationship */
867 dev_err(dev
, "unsupported rate (%d/%d)\n", ackmd
, bpfmd
);
889 dev_err(dev
, "unsupported ackmd (%d)\n", ackmd
);
914 dev_err(dev
, "unsupported bpfmd (%d)\n", bpfmd
);
918 dev_dbg(dev
, "ACKMD/BPFMD = %d/%d\n", ackmd
, bpfmd
);
920 fsi_reg_mask_set(fsi
, CKG1
, (ACKMD_MASK
| BPFMD_MASK
) , data
);
926 static int fsi_clk_set_rate_external(struct device
*dev
,
927 struct fsi_priv
*fsi
)
929 struct clk
*xck
= fsi
->clock
.xck
;
930 struct clk
*ick
= fsi
->clock
.ick
;
931 unsigned long rate
= fsi
->clock
.rate
;
936 /* check clock rate */
937 xrate
= clk_get_rate(xck
);
939 dev_err(dev
, "unsupported clock rate\n");
943 clk_set_parent(ick
, xck
);
944 clk_set_rate(ick
, xrate
);
946 bpfmd
= fsi
->chan_num
* 32;
947 ackmd
= xrate
/ rate
;
949 dev_dbg(dev
, "external/rate = %ld/%ld\n", xrate
, rate
);
951 ret
= fsi_clk_set_ackbpf(dev
, fsi
, ackmd
, bpfmd
);
953 dev_err(dev
, "%s failed", __func__
);
958 static int fsi_clk_set_rate_cpg(struct device
*dev
,
959 struct fsi_priv
*fsi
)
961 struct clk
*ick
= fsi
->clock
.ick
;
962 struct clk
*div
= fsi
->clock
.div
;
963 unsigned long rate
= fsi
->clock
.rate
;
964 unsigned long target
= 0; /* 12288000 or 11289600 */
965 unsigned long actual
, cout
;
966 unsigned long diff
, min
;
967 unsigned long best_cout
, best_act
;
972 if (!(12288000 % rate
))
974 if (!(11289600 % rate
))
977 dev_err(dev
, "unsupported rate\n");
981 bpfmd
= fsi
->chan_num
* 32;
982 ackmd
= target
/ rate
;
983 ret
= fsi_clk_set_ackbpf(dev
, fsi
, ackmd
, bpfmd
);
985 dev_err(dev
, "%s failed", __func__
);
992 * [CPG] = cout => [FSI_DIV] = audio => [FSI] => [codec]
994 * But, it needs to find best match of CPG and FSI_DIV
995 * combination, since it is difficult to generate correct
996 * frequency of audio clock from ick clock only.
997 * Because ick is created from its parent clock.
999 * target = rate x [512/256/128/64]fs
1000 * cout = round(target x adjustment)
1001 * actual = cout / adjustment (by FSI-DIV) ~= target
1007 for (adj
= 1; adj
< 0xffff; adj
++) {
1009 cout
= target
* adj
;
1010 if (cout
> 100000000) /* max clock = 100MHz */
1013 /* cout/actual audio clock */
1014 cout
= clk_round_rate(ick
, cout
);
1015 actual
= cout
/ adj
;
1017 /* find best frequency */
1018 diff
= abs(actual
- target
);
1026 ret
= clk_set_rate(ick
, best_cout
);
1028 dev_err(dev
, "ick clock failed\n");
1032 ret
= clk_set_rate(div
, clk_round_rate(div
, best_act
));
1034 dev_err(dev
, "div clock failed\n");
1038 dev_dbg(dev
, "ick/div = %ld/%ld\n",
1039 clk_get_rate(ick
), clk_get_rate(div
));
1045 * pio data transfer handler
1047 static void fsi_pio_push16(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1051 if (fsi_is_enable_stream(fsi
)) {
1055 * fsi_pio_push_init()
1057 u32
*buf
= (u32
*)_buf
;
1059 for (i
= 0; i
< samples
/ 2; i
++)
1060 fsi_reg_write(fsi
, DODT
, buf
[i
]);
1063 u16
*buf
= (u16
*)_buf
;
1065 for (i
= 0; i
< samples
; i
++)
1066 fsi_reg_write(fsi
, DODT
, ((u32
)*(buf
+ i
) << 8));
1070 static void fsi_pio_pop16(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1072 u16
*buf
= (u16
*)_buf
;
1075 for (i
= 0; i
< samples
; i
++)
1076 *(buf
+ i
) = (u16
)(fsi_reg_read(fsi
, DIDT
) >> 8);
1079 static void fsi_pio_push32(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1081 u32
*buf
= (u32
*)_buf
;
1084 for (i
= 0; i
< samples
; i
++)
1085 fsi_reg_write(fsi
, DODT
, *(buf
+ i
));
1088 static void fsi_pio_pop32(struct fsi_priv
*fsi
, u8
*_buf
, int samples
)
1090 u32
*buf
= (u32
*)_buf
;
1093 for (i
= 0; i
< samples
; i
++)
1094 *(buf
+ i
) = fsi_reg_read(fsi
, DIDT
);
1097 static u8
*fsi_pio_get_area(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1099 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1101 return runtime
->dma_area
+
1102 samples_to_bytes(runtime
, io
->buff_sample_pos
);
1105 static int fsi_pio_transfer(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
1106 void (*run16
)(struct fsi_priv
*fsi
, u8
*buf
, int samples
),
1107 void (*run32
)(struct fsi_priv
*fsi
, u8
*buf
, int samples
),
1110 struct snd_pcm_runtime
*runtime
;
1111 struct snd_pcm_substream
*substream
;
1115 if (!fsi_stream_is_working(fsi
, io
))
1119 substream
= io
->substream
;
1120 runtime
= substream
->runtime
;
1122 /* FSI FIFO has limit.
1123 * So, this driver can not send periods data at a time
1125 if (io
->buff_sample_pos
>=
1126 io
->period_samples
* (io
->period_pos
+ 1)) {
1129 io
->period_pos
= (io
->period_pos
+ 1) % runtime
->periods
;
1131 if (0 == io
->period_pos
)
1132 io
->buff_sample_pos
= 0;
1135 buf
= fsi_pio_get_area(fsi
, io
);
1137 switch (io
->sample_width
) {
1139 run16(fsi
, buf
, samples
);
1142 run32(fsi
, buf
, samples
);
1148 /* update buff_sample_pos */
1149 io
->buff_sample_pos
+= samples
;
1152 snd_pcm_period_elapsed(substream
);
1157 static int fsi_pio_pop(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1159 int sample_residues
; /* samples in FSI fifo */
1160 int sample_space
; /* ALSA free samples space */
1163 sample_residues
= fsi_get_current_fifo_samples(fsi
, io
);
1164 sample_space
= io
->buff_sample_capa
- io
->buff_sample_pos
;
1166 samples
= min(sample_residues
, sample_space
);
1168 return fsi_pio_transfer(fsi
, io
,
1174 static int fsi_pio_push(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1176 int sample_residues
; /* ALSA residue samples */
1177 int sample_space
; /* FSI fifo free samples space */
1180 sample_residues
= io
->buff_sample_capa
- io
->buff_sample_pos
;
1181 sample_space
= io
->fifo_sample_capa
-
1182 fsi_get_current_fifo_samples(fsi
, io
);
1184 samples
= min(sample_residues
, sample_space
);
1186 return fsi_pio_transfer(fsi
, io
,
1192 static void fsi_pio_start_stop(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
1195 struct fsi_master
*master
= fsi_get_master(fsi
);
1196 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
1199 fsi_irq_enable(fsi
, io
);
1201 fsi_irq_disable(fsi
, io
);
1203 if (fsi_is_clk_master(fsi
))
1204 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
1207 static int fsi_pio_push_init(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1210 * we can use 16bit stream mode
1211 * when "playback" and "16bit data"
1212 * and platform allows "stream mode"
1216 if (fsi_is_enable_stream(fsi
))
1217 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1218 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM
);
1220 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1221 BUSOP_SET(16, PACKAGE_24BITBUS_BACK
);
1225 static int fsi_pio_pop_init(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1228 * always 24bit bus, package back when "capture"
1230 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1231 BUSOP_SET(16, PACKAGE_24BITBUS_BACK
);
1235 static struct fsi_stream_handler fsi_pio_push_handler
= {
1236 .init
= fsi_pio_push_init
,
1237 .transfer
= fsi_pio_push
,
1238 .start_stop
= fsi_pio_start_stop
,
1241 static struct fsi_stream_handler fsi_pio_pop_handler
= {
1242 .init
= fsi_pio_pop_init
,
1243 .transfer
= fsi_pio_pop
,
1244 .start_stop
= fsi_pio_start_stop
,
1247 static irqreturn_t
fsi_interrupt(int irq
, void *data
)
1249 struct fsi_master
*master
= data
;
1250 u32 int_st
= fsi_irq_get_status(master
);
1252 /* clear irq status */
1253 fsi_master_mask_set(master
, SOFT_RST
, IR
, 0);
1254 fsi_master_mask_set(master
, SOFT_RST
, IR
, IR
);
1256 if (int_st
& AB_IO(1, AO_SHIFT
))
1257 fsi_stream_transfer(&master
->fsia
.playback
);
1258 if (int_st
& AB_IO(1, BO_SHIFT
))
1259 fsi_stream_transfer(&master
->fsib
.playback
);
1260 if (int_st
& AB_IO(1, AI_SHIFT
))
1261 fsi_stream_transfer(&master
->fsia
.capture
);
1262 if (int_st
& AB_IO(1, BI_SHIFT
))
1263 fsi_stream_transfer(&master
->fsib
.capture
);
1265 fsi_count_fifo_err(&master
->fsia
);
1266 fsi_count_fifo_err(&master
->fsib
);
1268 fsi_irq_clear_status(&master
->fsia
);
1269 fsi_irq_clear_status(&master
->fsib
);
1275 * dma data transfer handler
1277 static int fsi_dma_init(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1279 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1280 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
1281 enum dma_data_direction dir
= fsi_stream_is_play(fsi
, io
) ?
1282 DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1285 * 24bit data : 24bit bus / package in back
1286 * 16bit data : 16bit bus / stream mode
1288 io
->bus_option
= BUSOP_SET(24, PACKAGE_24BITBUS_BACK
) |
1289 BUSOP_SET(16, PACKAGE_16BITBUS_STREAM
);
1291 io
->dma
= dma_map_single(dai
->dev
, runtime
->dma_area
,
1292 snd_pcm_lib_buffer_bytes(io
->substream
), dir
);
1296 static int fsi_dma_quit(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1298 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
1299 enum dma_data_direction dir
= fsi_stream_is_play(fsi
, io
) ?
1300 DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1302 dma_unmap_single(dai
->dev
, io
->dma
,
1303 snd_pcm_lib_buffer_bytes(io
->substream
), dir
);
1307 static dma_addr_t
fsi_dma_get_area(struct fsi_stream
*io
)
1309 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1311 return io
->dma
+ samples_to_bytes(runtime
, io
->buff_sample_pos
);
1314 static void fsi_dma_complete(void *data
)
1316 struct fsi_stream
*io
= (struct fsi_stream
*)data
;
1317 struct fsi_priv
*fsi
= fsi_stream_to_priv(io
);
1318 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
1319 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
1320 enum dma_data_direction dir
= fsi_stream_is_play(fsi
, io
) ?
1321 DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1323 dma_sync_single_for_cpu(dai
->dev
, fsi_dma_get_area(io
),
1324 samples_to_bytes(runtime
, io
->period_samples
), dir
);
1326 io
->buff_sample_pos
+= io
->period_samples
;
1329 if (io
->period_pos
>= runtime
->periods
) {
1331 io
->buff_sample_pos
= 0;
1334 fsi_count_fifo_err(fsi
);
1335 fsi_stream_transfer(io
);
1337 snd_pcm_period_elapsed(io
->substream
);
1340 static void fsi_dma_do_work(struct work_struct
*work
)
1342 struct fsi_stream
*io
= container_of(work
, struct fsi_stream
, work
);
1343 struct fsi_priv
*fsi
= fsi_stream_to_priv(io
);
1344 struct snd_soc_dai
*dai
;
1345 struct dma_async_tx_descriptor
*desc
;
1346 struct snd_pcm_runtime
*runtime
;
1347 enum dma_data_direction dir
;
1348 int is_play
= fsi_stream_is_play(fsi
, io
);
1352 if (!fsi_stream_is_working(fsi
, io
))
1355 dai
= fsi_get_dai(io
->substream
);
1356 runtime
= io
->substream
->runtime
;
1357 dir
= is_play
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
;
1358 len
= samples_to_bytes(runtime
, io
->period_samples
);
1359 buf
= fsi_dma_get_area(io
);
1361 dma_sync_single_for_device(dai
->dev
, buf
, len
, dir
);
1363 desc
= dmaengine_prep_slave_single(io
->chan
, buf
, len
, dir
,
1364 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1366 dev_err(dai
->dev
, "dmaengine_prep_slave_sg() fail\n");
1370 desc
->callback
= fsi_dma_complete
;
1371 desc
->callback_param
= io
;
1373 if (dmaengine_submit(desc
) < 0) {
1374 dev_err(dai
->dev
, "tx_submit() fail\n");
1378 dma_async_issue_pending(io
->chan
);
1383 * In DMAEngine case, codec and FSI cannot be started simultaneously
1384 * since FSI is using the scheduler work queue.
1385 * Therefore, in capture case, probably FSI FIFO will have got
1386 * overflow error in this point.
1387 * in that case, DMA cannot start transfer until error was cleared.
1390 if (ERR_OVER
& fsi_reg_read(fsi
, DIFF_ST
)) {
1391 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
1392 fsi_reg_write(fsi
, DIFF_ST
, 0);
1397 static bool fsi_dma_filter(struct dma_chan
*chan
, void *param
)
1399 struct sh_dmae_slave
*slave
= param
;
1401 chan
->private = slave
;
1406 static int fsi_dma_transfer(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1408 schedule_work(&io
->work
);
1413 static void fsi_dma_push_start_stop(struct fsi_priv
*fsi
, struct fsi_stream
*io
,
1416 struct fsi_master
*master
= fsi_get_master(fsi
);
1417 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
1418 u32 enable
= start
? DMA_ON
: 0;
1420 fsi_reg_mask_set(fsi
, OUT_DMAC
, DMA_ON
, enable
);
1422 dmaengine_terminate_all(io
->chan
);
1424 if (fsi_is_clk_master(fsi
))
1425 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
1428 static int fsi_dma_probe(struct fsi_priv
*fsi
, struct fsi_stream
*io
, struct device
*dev
)
1430 dma_cap_mask_t mask
;
1433 dma_cap_set(DMA_SLAVE
, mask
);
1435 io
->chan
= dma_request_channel(mask
, fsi_dma_filter
, &io
->slave
);
1438 /* switch to PIO handler */
1439 if (fsi_stream_is_play(fsi
, io
))
1440 fsi
->playback
.handler
= &fsi_pio_push_handler
;
1442 fsi
->capture
.handler
= &fsi_pio_pop_handler
;
1444 dev_info(dev
, "switch handler (dma => pio)\n");
1447 return fsi_stream_probe(fsi
, dev
);
1450 INIT_WORK(&io
->work
, fsi_dma_do_work
);
1455 static int fsi_dma_remove(struct fsi_priv
*fsi
, struct fsi_stream
*io
)
1457 cancel_work_sync(&io
->work
);
1459 fsi_stream_stop(fsi
, io
);
1462 dma_release_channel(io
->chan
);
1468 static struct fsi_stream_handler fsi_dma_push_handler
= {
1469 .init
= fsi_dma_init
,
1470 .quit
= fsi_dma_quit
,
1471 .probe
= fsi_dma_probe
,
1472 .transfer
= fsi_dma_transfer
,
1473 .remove
= fsi_dma_remove
,
1474 .start_stop
= fsi_dma_push_start_stop
,
1480 static void fsi_fifo_init(struct fsi_priv
*fsi
,
1481 struct fsi_stream
*io
,
1484 struct fsi_master
*master
= fsi_get_master(fsi
);
1485 int is_play
= fsi_stream_is_play(fsi
, io
);
1489 /* get on-chip RAM capacity */
1490 shift
= fsi_master_read(master
, FIFO_SZ
);
1491 shift
>>= fsi_get_port_shift(fsi
, io
);
1492 shift
&= FIFO_SZ_MASK
;
1493 frame_capa
= 256 << shift
;
1494 dev_dbg(dev
, "fifo = %d words\n", frame_capa
);
1497 * The maximum number of sample data varies depending
1498 * on the number of channels selected for the format.
1500 * FIFOs are used in 4-channel units in 3-channel mode
1501 * and in 8-channel units in 5- to 7-channel mode
1502 * meaning that more FIFOs than the required size of DPRAM
1505 * ex) if 256 words of DP-RAM is connected
1506 * 1 channel: 256 (256 x 1 = 256)
1507 * 2 channels: 128 (128 x 2 = 256)
1508 * 3 channels: 64 ( 64 x 3 = 192)
1509 * 4 channels: 64 ( 64 x 4 = 256)
1510 * 5 channels: 32 ( 32 x 5 = 160)
1511 * 6 channels: 32 ( 32 x 6 = 192)
1512 * 7 channels: 32 ( 32 x 7 = 224)
1513 * 8 channels: 32 ( 32 x 8 = 256)
1515 for (i
= 1; i
< fsi
->chan_num
; i
<<= 1)
1517 dev_dbg(dev
, "%d channel %d store\n",
1518 fsi
->chan_num
, frame_capa
);
1520 io
->fifo_sample_capa
= fsi_frame2sample(fsi
, frame_capa
);
1523 * set interrupt generation factor
1527 fsi_reg_write(fsi
, DOFF_CTL
, IRQ_HALF
);
1528 fsi_reg_mask_set(fsi
, DOFF_CTL
, FIFO_CLR
, FIFO_CLR
);
1530 fsi_reg_write(fsi
, DIFF_CTL
, IRQ_HALF
);
1531 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
1535 static int fsi_hw_startup(struct fsi_priv
*fsi
,
1536 struct fsi_stream
*io
,
1542 if (fsi_is_clk_master(fsi
))
1545 fsi_reg_mask_set(fsi
, CKG1
, (DIMD
| DOMD
), data
);
1547 /* clock inversion (CKG2) */
1549 if (fsi
->bit_clk_inv
)
1551 if (fsi
->lr_clk_inv
)
1553 if (fsi_is_clk_master(fsi
))
1555 fsi_reg_write(fsi
, CKG2
, data
);
1558 if (fsi_is_spdif(fsi
)) {
1559 fsi_spdif_clk_ctrl(fsi
, 1);
1560 fsi_reg_mask_set(fsi
, OUT_SEL
, DMMD
, DMMD
);
1567 switch (io
->sample_width
) {
1569 data
= BUSOP_GET(16, io
->bus_option
);
1572 data
= BUSOP_GET(24, io
->bus_option
);
1575 fsi_format_bus_setup(fsi
, io
, data
, dev
);
1578 fsi_irq_disable(fsi
, io
);
1579 fsi_irq_clear_status(fsi
);
1582 fsi_fifo_init(fsi
, io
, dev
);
1584 /* start master clock */
1585 if (fsi_is_clk_master(fsi
))
1586 return fsi_clk_enable(dev
, fsi
);
1591 static int fsi_hw_shutdown(struct fsi_priv
*fsi
,
1594 /* stop master clock */
1595 if (fsi_is_clk_master(fsi
))
1596 return fsi_clk_disable(dev
, fsi
);
1601 static int fsi_dai_startup(struct snd_pcm_substream
*substream
,
1602 struct snd_soc_dai
*dai
)
1604 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1606 fsi_clk_invalid(fsi
);
1611 static void fsi_dai_shutdown(struct snd_pcm_substream
*substream
,
1612 struct snd_soc_dai
*dai
)
1614 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1616 fsi_clk_invalid(fsi
);
1619 static int fsi_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
1620 struct snd_soc_dai
*dai
)
1622 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1623 struct fsi_stream
*io
= fsi_stream_get(fsi
, substream
);
1627 case SNDRV_PCM_TRIGGER_START
:
1628 fsi_stream_init(fsi
, io
, substream
);
1630 ret
= fsi_hw_startup(fsi
, io
, dai
->dev
);
1632 ret
= fsi_stream_transfer(io
);
1634 fsi_stream_start(fsi
, io
);
1636 case SNDRV_PCM_TRIGGER_STOP
:
1638 ret
= fsi_hw_shutdown(fsi
, dai
->dev
);
1639 fsi_stream_stop(fsi
, io
);
1640 fsi_stream_quit(fsi
, io
);
1647 static int fsi_set_fmt_dai(struct fsi_priv
*fsi
, unsigned int fmt
)
1649 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1650 case SND_SOC_DAIFMT_I2S
:
1654 case SND_SOC_DAIFMT_LEFT_J
:
1665 static int fsi_set_fmt_spdif(struct fsi_priv
*fsi
)
1667 struct fsi_master
*master
= fsi_get_master(fsi
);
1669 if (fsi_version(master
) < 2)
1672 fsi
->fmt
= CR_DTMD_SPDIF_PCM
| CR_PCM
;
1678 static int fsi_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1680 struct fsi_priv
*fsi
= fsi_get_priv_frm_dai(dai
);
1683 /* set master/slave audio interface */
1684 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1685 case SND_SOC_DAIFMT_CBM_CFM
:
1686 fsi
->clk_master
= 1;
1688 case SND_SOC_DAIFMT_CBS_CFS
:
1694 /* set clock inversion */
1695 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
1696 case SND_SOC_DAIFMT_NB_IF
:
1697 fsi
->bit_clk_inv
= 0;
1698 fsi
->lr_clk_inv
= 1;
1700 case SND_SOC_DAIFMT_IB_NF
:
1701 fsi
->bit_clk_inv
= 1;
1702 fsi
->lr_clk_inv
= 0;
1704 case SND_SOC_DAIFMT_IB_IF
:
1705 fsi
->bit_clk_inv
= 1;
1706 fsi
->lr_clk_inv
= 1;
1708 case SND_SOC_DAIFMT_NB_NF
:
1710 fsi
->bit_clk_inv
= 0;
1711 fsi
->lr_clk_inv
= 0;
1715 if (fsi_is_clk_master(fsi
)) {
1717 fsi_clk_init(dai
->dev
, fsi
, 0, 1, 1,
1718 fsi_clk_set_rate_cpg
);
1720 fsi_clk_init(dai
->dev
, fsi
, 1, 1, 0,
1721 fsi_clk_set_rate_external
);
1725 if (fsi_is_spdif(fsi
))
1726 ret
= fsi_set_fmt_spdif(fsi
);
1728 ret
= fsi_set_fmt_dai(fsi
, fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
1733 static int fsi_dai_hw_params(struct snd_pcm_substream
*substream
,
1734 struct snd_pcm_hw_params
*params
,
1735 struct snd_soc_dai
*dai
)
1737 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1739 if (fsi_is_clk_master(fsi
))
1740 fsi_clk_valid(fsi
, params_rate(params
));
1745 static const struct snd_soc_dai_ops fsi_dai_ops
= {
1746 .startup
= fsi_dai_startup
,
1747 .shutdown
= fsi_dai_shutdown
,
1748 .trigger
= fsi_dai_trigger
,
1749 .set_fmt
= fsi_dai_set_fmt
,
1750 .hw_params
= fsi_dai_hw_params
,
1757 static struct snd_pcm_hardware fsi_pcm_hardware
= {
1758 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
1759 SNDRV_PCM_INFO_MMAP
|
1760 SNDRV_PCM_INFO_MMAP_VALID
|
1761 SNDRV_PCM_INFO_PAUSE
,
1762 .formats
= FSI_FMTS
,
1768 .buffer_bytes_max
= 64 * 1024,
1769 .period_bytes_min
= 32,
1770 .period_bytes_max
= 8192,
1776 static int fsi_pcm_open(struct snd_pcm_substream
*substream
)
1778 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1781 snd_soc_set_runtime_hwparams(substream
, &fsi_pcm_hardware
);
1783 ret
= snd_pcm_hw_constraint_integer(runtime
,
1784 SNDRV_PCM_HW_PARAM_PERIODS
);
1789 static int fsi_hw_params(struct snd_pcm_substream
*substream
,
1790 struct snd_pcm_hw_params
*hw_params
)
1792 return snd_pcm_lib_malloc_pages(substream
,
1793 params_buffer_bytes(hw_params
));
1796 static int fsi_hw_free(struct snd_pcm_substream
*substream
)
1798 return snd_pcm_lib_free_pages(substream
);
1801 static snd_pcm_uframes_t
fsi_pointer(struct snd_pcm_substream
*substream
)
1803 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1804 struct fsi_stream
*io
= fsi_stream_get(fsi
, substream
);
1806 return fsi_sample2frame(fsi
, io
->buff_sample_pos
);
1809 static struct snd_pcm_ops fsi_pcm_ops
= {
1810 .open
= fsi_pcm_open
,
1811 .ioctl
= snd_pcm_lib_ioctl
,
1812 .hw_params
= fsi_hw_params
,
1813 .hw_free
= fsi_hw_free
,
1814 .pointer
= fsi_pointer
,
1821 #define PREALLOC_BUFFER (32 * 1024)
1822 #define PREALLOC_BUFFER_MAX (32 * 1024)
1824 static void fsi_pcm_free(struct snd_pcm
*pcm
)
1826 snd_pcm_lib_preallocate_free_for_all(pcm
);
1829 static int fsi_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
1831 struct snd_pcm
*pcm
= rtd
->pcm
;
1834 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1835 * in MMAP mode (i.e. aplay -M)
1837 return snd_pcm_lib_preallocate_pages_for_all(
1839 SNDRV_DMA_TYPE_CONTINUOUS
,
1840 snd_dma_continuous_data(GFP_KERNEL
),
1841 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1848 static struct snd_soc_dai_driver fsi_soc_dai
[] = {
1853 .formats
= FSI_FMTS
,
1859 .formats
= FSI_FMTS
,
1863 .ops
= &fsi_dai_ops
,
1869 .formats
= FSI_FMTS
,
1875 .formats
= FSI_FMTS
,
1879 .ops
= &fsi_dai_ops
,
1883 static struct snd_soc_platform_driver fsi_soc_platform
= {
1884 .ops
= &fsi_pcm_ops
,
1885 .pcm_new
= fsi_pcm_new
,
1886 .pcm_free
= fsi_pcm_free
,
1892 static void fsi_of_parse(char *name
,
1893 struct device_node
*np
,
1894 struct sh_fsi_port_info
*info
,
1899 unsigned long flags
= 0;
1903 } of_parse_property
[] = {
1904 { "spdif-connection", SH_FSI_FMT_SPDIF
},
1905 { "stream-mode-support", SH_FSI_ENABLE_STREAM_MODE
},
1906 { "use-internal-clock", SH_FSI_CLK_CPG
},
1909 for (i
= 0; i
< ARRAY_SIZE(of_parse_property
); i
++) {
1910 sprintf(prop
, "%s,%s", name
, of_parse_property
[i
].name
);
1911 if (of_get_property(np
, prop
, NULL
))
1912 flags
|= of_parse_property
[i
].val
;
1914 info
->flags
= flags
;
1916 dev_dbg(dev
, "%s flags : %lx\n", name
, info
->flags
);
1919 static void fsi_port_info_init(struct fsi_priv
*fsi
,
1920 struct sh_fsi_port_info
*info
)
1922 if (info
->flags
& SH_FSI_FMT_SPDIF
)
1925 if (info
->flags
& SH_FSI_CLK_CPG
)
1928 if (info
->flags
& SH_FSI_ENABLE_STREAM_MODE
)
1929 fsi
->enable_stream
= 1;
1932 static void fsi_handler_init(struct fsi_priv
*fsi
,
1933 struct sh_fsi_port_info
*info
)
1935 fsi
->playback
.handler
= &fsi_pio_push_handler
; /* default PIO */
1936 fsi
->playback
.priv
= fsi
;
1937 fsi
->capture
.handler
= &fsi_pio_pop_handler
; /* default PIO */
1938 fsi
->capture
.priv
= fsi
;
1941 fsi
->playback
.slave
.shdma_slave
.slave_id
= info
->tx_id
;
1942 fsi
->playback
.handler
= &fsi_dma_push_handler
;
1946 static struct of_device_id fsi_of_match
[];
1947 static int fsi_probe(struct platform_device
*pdev
)
1949 struct fsi_master
*master
;
1950 struct device_node
*np
= pdev
->dev
.of_node
;
1951 struct sh_fsi_platform_info info
;
1952 const struct fsi_core
*core
;
1953 struct fsi_priv
*fsi
;
1954 struct resource
*res
;
1958 memset(&info
, 0, sizeof(info
));
1962 const struct of_device_id
*of_id
;
1964 of_id
= of_match_device(fsi_of_match
, &pdev
->dev
);
1967 fsi_of_parse("fsia", np
, &info
.port_a
, &pdev
->dev
);
1968 fsi_of_parse("fsib", np
, &info
.port_b
, &pdev
->dev
);
1971 const struct platform_device_id
*id_entry
= pdev
->id_entry
;
1973 core
= (struct fsi_core
*)id_entry
->driver_data
;
1975 if (pdev
->dev
.platform_data
)
1976 memcpy(&info
, pdev
->dev
.platform_data
, sizeof(info
));
1980 dev_err(&pdev
->dev
, "unknown fsi device\n");
1984 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1985 irq
= platform_get_irq(pdev
, 0);
1986 if (!res
|| (int)irq
<= 0) {
1987 dev_err(&pdev
->dev
, "Not enough FSI platform resources.\n");
1991 master
= devm_kzalloc(&pdev
->dev
, sizeof(*master
), GFP_KERNEL
);
1993 dev_err(&pdev
->dev
, "Could not allocate master\n");
1997 master
->base
= devm_ioremap_nocache(&pdev
->dev
,
1998 res
->start
, resource_size(res
));
1999 if (!master
->base
) {
2000 dev_err(&pdev
->dev
, "Unable to ioremap FSI registers.\n");
2004 /* master setting */
2006 master
->core
= core
;
2007 spin_lock_init(&master
->lock
);
2010 fsi
= &master
->fsia
;
2011 fsi
->base
= master
->base
;
2012 fsi
->master
= master
;
2013 fsi_port_info_init(fsi
, &info
.port_a
);
2014 fsi_handler_init(fsi
, &info
.port_a
);
2015 ret
= fsi_stream_probe(fsi
, &pdev
->dev
);
2017 dev_err(&pdev
->dev
, "FSIA stream probe failed\n");
2022 fsi
= &master
->fsib
;
2023 fsi
->base
= master
->base
+ 0x40;
2024 fsi
->master
= master
;
2025 fsi_port_info_init(fsi
, &info
.port_b
);
2026 fsi_handler_init(fsi
, &info
.port_b
);
2027 ret
= fsi_stream_probe(fsi
, &pdev
->dev
);
2029 dev_err(&pdev
->dev
, "FSIB stream probe failed\n");
2033 pm_runtime_enable(&pdev
->dev
);
2034 dev_set_drvdata(&pdev
->dev
, master
);
2036 ret
= devm_request_irq(&pdev
->dev
, irq
, &fsi_interrupt
, 0,
2037 dev_name(&pdev
->dev
), master
);
2039 dev_err(&pdev
->dev
, "irq request err\n");
2043 ret
= snd_soc_register_platform(&pdev
->dev
, &fsi_soc_platform
);
2045 dev_err(&pdev
->dev
, "cannot snd soc register\n");
2049 ret
= snd_soc_register_dais(&pdev
->dev
, fsi_soc_dai
,
2050 ARRAY_SIZE(fsi_soc_dai
));
2052 dev_err(&pdev
->dev
, "cannot snd dai register\n");
2059 snd_soc_unregister_platform(&pdev
->dev
);
2061 pm_runtime_disable(&pdev
->dev
);
2062 fsi_stream_remove(&master
->fsib
);
2064 fsi_stream_remove(&master
->fsia
);
2069 static int fsi_remove(struct platform_device
*pdev
)
2071 struct fsi_master
*master
;
2073 master
= dev_get_drvdata(&pdev
->dev
);
2075 pm_runtime_disable(&pdev
->dev
);
2077 snd_soc_unregister_dais(&pdev
->dev
, ARRAY_SIZE(fsi_soc_dai
));
2078 snd_soc_unregister_platform(&pdev
->dev
);
2080 fsi_stream_remove(&master
->fsia
);
2081 fsi_stream_remove(&master
->fsib
);
2086 static void __fsi_suspend(struct fsi_priv
*fsi
,
2087 struct fsi_stream
*io
,
2090 if (!fsi_stream_is_working(fsi
, io
))
2093 fsi_stream_stop(fsi
, io
);
2094 fsi_hw_shutdown(fsi
, dev
);
2097 static void __fsi_resume(struct fsi_priv
*fsi
,
2098 struct fsi_stream
*io
,
2101 if (!fsi_stream_is_working(fsi
, io
))
2104 fsi_hw_startup(fsi
, io
, dev
);
2105 fsi_stream_start(fsi
, io
);
2108 static int fsi_suspend(struct device
*dev
)
2110 struct fsi_master
*master
= dev_get_drvdata(dev
);
2111 struct fsi_priv
*fsia
= &master
->fsia
;
2112 struct fsi_priv
*fsib
= &master
->fsib
;
2114 __fsi_suspend(fsia
, &fsia
->playback
, dev
);
2115 __fsi_suspend(fsia
, &fsia
->capture
, dev
);
2117 __fsi_suspend(fsib
, &fsib
->playback
, dev
);
2118 __fsi_suspend(fsib
, &fsib
->capture
, dev
);
2123 static int fsi_resume(struct device
*dev
)
2125 struct fsi_master
*master
= dev_get_drvdata(dev
);
2126 struct fsi_priv
*fsia
= &master
->fsia
;
2127 struct fsi_priv
*fsib
= &master
->fsib
;
2129 __fsi_resume(fsia
, &fsia
->playback
, dev
);
2130 __fsi_resume(fsia
, &fsia
->capture
, dev
);
2132 __fsi_resume(fsib
, &fsib
->playback
, dev
);
2133 __fsi_resume(fsib
, &fsib
->capture
, dev
);
2138 static struct dev_pm_ops fsi_pm_ops
= {
2139 .suspend
= fsi_suspend
,
2140 .resume
= fsi_resume
,
2143 static struct fsi_core fsi1_core
= {
2152 static struct fsi_core fsi2_core
= {
2156 .int_st
= CPU_INT_ST
,
2159 .a_mclk
= A_MST_CTLR
,
2160 .b_mclk
= B_MST_CTLR
,
2163 static struct of_device_id fsi_of_match
[] = {
2164 { .compatible
= "renesas,sh_fsi", .data
= &fsi1_core
},
2165 { .compatible
= "renesas,sh_fsi2", .data
= &fsi2_core
},
2168 MODULE_DEVICE_TABLE(of
, fsi_of_match
);
2170 static struct platform_device_id fsi_id_table
[] = {
2171 { "sh_fsi", (kernel_ulong_t
)&fsi1_core
},
2172 { "sh_fsi2", (kernel_ulong_t
)&fsi2_core
},
2175 MODULE_DEVICE_TABLE(platform
, fsi_id_table
);
2177 static struct platform_driver fsi_driver
= {
2179 .name
= "fsi-pcm-audio",
2181 .of_match_table
= fsi_of_match
,
2184 .remove
= fsi_remove
,
2185 .id_table
= fsi_id_table
,
2188 module_platform_driver(fsi_driver
);
2190 MODULE_LICENSE("GPL");
2191 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
2192 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
2193 MODULE_ALIAS("platform:fsi-pcm-audio");