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/pm_runtime.h>
18 #include <linux/slab.h>
19 #include <sound/soc.h>
20 #include <sound/sh_fsi.h>
22 /* PortA/PortB register */
23 #define REG_DO_FMT 0x0000
24 #define REG_DOFF_CTL 0x0004
25 #define REG_DOFF_ST 0x0008
26 #define REG_DI_FMT 0x000C
27 #define REG_DIFF_CTL 0x0010
28 #define REG_DIFF_ST 0x0014
29 #define REG_CKG1 0x0018
30 #define REG_CKG2 0x001C
31 #define REG_DIDT 0x0020
32 #define REG_DODT 0x0024
33 #define REG_MUTE_ST 0x0028
34 #define REG_OUT_SEL 0x0030
37 #define MST_CLK_RST 0x0210
38 #define MST_SOFT_RST 0x0214
39 #define MST_FIFO_SZ 0x0218
41 /* core register (depend on FSI version) */
42 #define A_MST_CTLR 0x0180
43 #define B_MST_CTLR 0x01A0
44 #define CPU_INT_ST 0x01F4
45 #define CPU_IEMSK 0x01F8
46 #define CPU_IMSK 0x01FC
53 #define CR_BWS_24 (0x0 << 20) /* FSI2 */
54 #define CR_BWS_16 (0x1 << 20) /* FSI2 */
55 #define CR_BWS_20 (0x2 << 20) /* FSI2 */
57 #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
58 #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
59 #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
61 #define CR_MONO (0x0 << 4)
62 #define CR_MONO_D (0x1 << 4)
63 #define CR_PCM (0x2 << 4)
64 #define CR_I2S (0x3 << 4)
65 #define CR_TDM (0x4 << 4)
66 #define CR_TDM_D (0x5 << 4)
70 #define IRQ_HALF 0x00100000
71 #define FIFO_CLR 0x00000001
74 #define ERR_OVER 0x00000010
75 #define ERR_UNDER 0x00000001
76 #define ST_ERR (ERR_OVER | ERR_UNDER)
79 #define ACKMD_MASK 0x00007000
80 #define BPFMD_MASK 0x00000700
85 #define BP (1 << 4) /* Fix the signal of Biphase output */
86 #define SE (1 << 0) /* Fix the master clock */
92 /* IO SHIFT / MACRO */
97 #define AB_IO(param, shift) (param << shift)
100 #define PBSR (1 << 12) /* Port B Software Reset */
101 #define PASR (1 << 8) /* Port A Software Reset */
102 #define IR (1 << 4) /* Interrupt Reset */
103 #define FSISR (1 << 0) /* Software Reset */
106 #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
107 /* 1: Biphase and serial */
110 #define FIFO_SZ_MASK 0x7
112 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
114 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
116 typedef int (*set_rate_func
)(struct device
*dev
, int is_porta
, int rate
, int enable
);
119 * FSI driver use below type name for variable
121 * xxx_num : number of data
122 * xxx_pos : position of data
123 * xxx_capa : capacity of data
127 * period/frame/sample image
131 * period pos period pos
133 * |<-------------------- period--------------------->|
134 * ==|============================================ ... =|==
136 * ||<----- frame ----->|<------ frame ----->| ... |
137 * |+--------------------+--------------------+- ... |
138 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
139 * |+--------------------+--------------------+- ... |
140 * ==|============================================ ... =|==
160 struct snd_pcm_substream
*substream
;
162 int fifo_sample_capa
; /* sample capacity of FSI FIFO */
163 int buff_sample_capa
; /* sample capacity of ALSA buffer */
164 int buff_sample_pos
; /* sample position of ALSA buffer */
165 int period_samples
; /* sample number / 1 period */
166 int period_pos
; /* current period position */
174 struct fsi_master
*master
;
176 struct fsi_stream playback
;
177 struct fsi_stream capture
;
202 struct fsi_priv fsia
;
203 struct fsi_priv fsib
;
204 struct fsi_core
*core
;
205 struct sh_fsi_platform_info
*info
;
210 * basic read write function
213 static void __fsi_reg_write(u32 reg
, u32 data
)
215 /* valid data area is 24bit */
218 __raw_writel(data
, reg
);
221 static u32
__fsi_reg_read(u32 reg
)
223 return __raw_readl(reg
);
226 static void __fsi_reg_mask_set(u32 reg
, u32 mask
, u32 data
)
228 u32 val
= __fsi_reg_read(reg
);
233 __fsi_reg_write(reg
, val
);
236 #define fsi_reg_write(p, r, d)\
237 __fsi_reg_write((u32)(p->base + REG_##r), d)
239 #define fsi_reg_read(p, r)\
240 __fsi_reg_read((u32)(p->base + REG_##r))
242 #define fsi_reg_mask_set(p, r, m, d)\
243 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
245 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
246 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
247 static u32
_fsi_master_read(struct fsi_master
*master
, u32 reg
)
252 spin_lock_irqsave(&master
->lock
, flags
);
253 ret
= __fsi_reg_read((u32
)(master
->base
+ reg
));
254 spin_unlock_irqrestore(&master
->lock
, flags
);
259 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
260 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
261 static void _fsi_master_mask_set(struct fsi_master
*master
,
262 u32 reg
, u32 mask
, u32 data
)
266 spin_lock_irqsave(&master
->lock
, flags
);
267 __fsi_reg_mask_set((u32
)(master
->base
+ reg
), mask
, data
);
268 spin_unlock_irqrestore(&master
->lock
, flags
);
275 static struct fsi_master
*fsi_get_master(struct fsi_priv
*fsi
)
280 static int fsi_is_clk_master(struct fsi_priv
*fsi
)
282 return fsi
->clk_master
;
285 static int fsi_is_port_a(struct fsi_priv
*fsi
)
287 return fsi
->master
->base
== fsi
->base
;
290 static int fsi_is_spdif(struct fsi_priv
*fsi
)
295 static struct snd_soc_dai
*fsi_get_dai(struct snd_pcm_substream
*substream
)
297 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
302 static struct fsi_priv
*fsi_get_priv_frm_dai(struct snd_soc_dai
*dai
)
304 struct fsi_master
*master
= snd_soc_dai_get_drvdata(dai
);
307 return &master
->fsia
;
309 return &master
->fsib
;
312 static struct fsi_priv
*fsi_get_priv(struct snd_pcm_substream
*substream
)
314 return fsi_get_priv_frm_dai(fsi_get_dai(substream
));
317 static set_rate_func
fsi_get_info_set_rate(struct fsi_master
*master
)
322 return master
->info
->set_rate
;
325 static u32
fsi_get_info_flags(struct fsi_priv
*fsi
)
327 int is_porta
= fsi_is_port_a(fsi
);
328 struct fsi_master
*master
= fsi_get_master(fsi
);
333 return is_porta
? master
->info
->porta_flags
:
334 master
->info
->portb_flags
;
337 static inline int fsi_stream_is_play(int stream
)
339 return stream
== SNDRV_PCM_STREAM_PLAYBACK
;
342 static inline int fsi_is_play(struct snd_pcm_substream
*substream
)
344 return fsi_stream_is_play(substream
->stream
);
347 static inline struct fsi_stream
*fsi_get_stream(struct fsi_priv
*fsi
,
350 return is_play
? &fsi
->playback
: &fsi
->capture
;
353 static u32
fsi_get_port_shift(struct fsi_priv
*fsi
, int is_play
)
355 int is_porta
= fsi_is_port_a(fsi
);
359 shift
= is_play
? AO_SHIFT
: AI_SHIFT
;
361 shift
= is_play
? BO_SHIFT
: BI_SHIFT
;
366 static int fsi_frame2sample(struct fsi_priv
*fsi
, int frames
)
368 return frames
* fsi
->chan_num
;
371 static int fsi_sample2frame(struct fsi_priv
*fsi
, int samples
)
373 return samples
/ fsi
->chan_num
;
376 static int fsi_stream_is_working(struct fsi_priv
*fsi
,
379 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
380 struct fsi_master
*master
= fsi_get_master(fsi
);
384 spin_lock_irqsave(&master
->lock
, flags
);
385 ret
= !!io
->substream
;
386 spin_unlock_irqrestore(&master
->lock
, flags
);
391 static void fsi_stream_push(struct fsi_priv
*fsi
,
393 struct snd_pcm_substream
*substream
)
395 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
396 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
397 struct fsi_master
*master
= fsi_get_master(fsi
);
400 spin_lock_irqsave(&master
->lock
, flags
);
401 io
->substream
= substream
;
402 io
->buff_sample_capa
= fsi_frame2sample(fsi
, runtime
->buffer_size
);
403 io
->buff_sample_pos
= 0;
404 io
->period_samples
= fsi_frame2sample(fsi
, runtime
->period_size
);
406 io
->oerr_num
= -1; /* ignore 1st err */
407 io
->uerr_num
= -1; /* ignore 1st err */
408 spin_unlock_irqrestore(&master
->lock
, flags
);
411 static void fsi_stream_pop(struct fsi_priv
*fsi
, int is_play
)
413 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
414 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
415 struct fsi_master
*master
= fsi_get_master(fsi
);
418 spin_lock_irqsave(&master
->lock
, flags
);
420 if (io
->oerr_num
> 0)
421 dev_err(dai
->dev
, "over_run = %d\n", io
->oerr_num
);
423 if (io
->uerr_num
> 0)
424 dev_err(dai
->dev
, "under_run = %d\n", io
->uerr_num
);
426 io
->substream
= NULL
;
427 io
->buff_sample_capa
= 0;
428 io
->buff_sample_pos
= 0;
429 io
->period_samples
= 0;
433 spin_unlock_irqrestore(&master
->lock
, flags
);
436 static int fsi_get_current_fifo_samples(struct fsi_priv
*fsi
, int is_play
)
442 fsi_reg_read(fsi
, DOFF_ST
) :
443 fsi_reg_read(fsi
, DIFF_ST
);
445 frames
= 0x1ff & (status
>> 8);
447 return fsi_frame2sample(fsi
, frames
);
450 static void fsi_count_fifo_err(struct fsi_priv
*fsi
)
452 u32 ostatus
= fsi_reg_read(fsi
, DOFF_ST
);
453 u32 istatus
= fsi_reg_read(fsi
, DIFF_ST
);
455 if (ostatus
& ERR_OVER
)
456 fsi
->playback
.oerr_num
++;
458 if (ostatus
& ERR_UNDER
)
459 fsi
->playback
.uerr_num
++;
461 if (istatus
& ERR_OVER
)
462 fsi
->capture
.oerr_num
++;
464 if (istatus
& ERR_UNDER
)
465 fsi
->capture
.uerr_num
++;
467 fsi_reg_write(fsi
, DOFF_ST
, 0);
468 fsi_reg_write(fsi
, DIFF_ST
, 0);
475 static u8
*fsi_dma_get_area(struct fsi_priv
*fsi
, int stream
)
477 int is_play
= fsi_stream_is_play(stream
);
478 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
479 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
481 return runtime
->dma_area
+
482 samples_to_bytes(runtime
, io
->buff_sample_pos
);
485 static void fsi_dma_soft_push16(struct fsi_priv
*fsi
, int num
)
490 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
492 for (i
= 0; i
< num
; i
++)
493 fsi_reg_write(fsi
, DODT
, ((u32
)*(start
+ i
) << 8));
496 static void fsi_dma_soft_pop16(struct fsi_priv
*fsi
, int num
)
501 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
504 for (i
= 0; i
< num
; i
++)
505 *(start
+ i
) = (u16
)(fsi_reg_read(fsi
, DIDT
) >> 8);
508 static void fsi_dma_soft_push32(struct fsi_priv
*fsi
, int num
)
513 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
516 for (i
= 0; i
< num
; i
++)
517 fsi_reg_write(fsi
, DODT
, *(start
+ i
));
520 static void fsi_dma_soft_pop32(struct fsi_priv
*fsi
, int num
)
525 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
527 for (i
= 0; i
< num
; i
++)
528 *(start
+ i
) = fsi_reg_read(fsi
, DIDT
);
535 static void fsi_irq_enable(struct fsi_priv
*fsi
, int is_play
)
537 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
538 struct fsi_master
*master
= fsi_get_master(fsi
);
540 fsi_core_mask_set(master
, imsk
, data
, data
);
541 fsi_core_mask_set(master
, iemsk
, data
, data
);
544 static void fsi_irq_disable(struct fsi_priv
*fsi
, int is_play
)
546 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
547 struct fsi_master
*master
= fsi_get_master(fsi
);
549 fsi_core_mask_set(master
, imsk
, data
, 0);
550 fsi_core_mask_set(master
, iemsk
, data
, 0);
553 static u32
fsi_irq_get_status(struct fsi_master
*master
)
555 return fsi_core_read(master
, int_st
);
558 static void fsi_irq_clear_status(struct fsi_priv
*fsi
)
561 struct fsi_master
*master
= fsi_get_master(fsi
);
563 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 0));
564 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 1));
566 /* clear interrupt factor */
567 fsi_core_mask_set(master
, int_st
, data
, 0);
571 * SPDIF master clock function
573 * These functions are used later FSI2
575 static void fsi_spdif_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
577 struct fsi_master
*master
= fsi_get_master(fsi
);
580 if (master
->core
->ver
< 2) {
581 pr_err("fsi: register access err (%s)\n", __func__
);
586 val
= enable
? mask
: 0;
589 fsi_core_mask_set(master
, a_mclk
, mask
, val
) :
590 fsi_core_mask_set(master
, b_mclk
, mask
, val
);
596 static int fsi_set_master_clk(struct device
*dev
, struct fsi_priv
*fsi
,
597 long rate
, int enable
)
599 struct fsi_master
*master
= fsi_get_master(fsi
);
600 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
601 int fsi_ver
= master
->core
->ver
;
604 ret
= set_rate(dev
, fsi_is_port_a(fsi
), rate
, enable
);
605 if (ret
< 0) /* error */
614 switch (ret
& SH_FSI_ACKMD_MASK
) {
617 case SH_FSI_ACKMD_512
:
620 case SH_FSI_ACKMD_256
:
623 case SH_FSI_ACKMD_128
:
626 case SH_FSI_ACKMD_64
:
629 case SH_FSI_ACKMD_32
:
631 dev_err(dev
, "unsupported ACKMD\n");
637 switch (ret
& SH_FSI_BPFMD_MASK
) {
640 case SH_FSI_BPFMD_32
:
643 case SH_FSI_BPFMD_64
:
646 case SH_FSI_BPFMD_128
:
649 case SH_FSI_BPFMD_256
:
652 case SH_FSI_BPFMD_512
:
655 case SH_FSI_BPFMD_16
:
657 dev_err(dev
, "unsupported ACKMD\n");
663 fsi_reg_mask_set(fsi
, CKG1
, (ACKMD_MASK
| BPFMD_MASK
) , data
);
671 #define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
672 #define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
673 static void __fsi_port_clk_ctrl(struct fsi_priv
*fsi
, int is_play
, int enable
)
675 struct fsi_master
*master
= fsi_get_master(fsi
);
676 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
679 fsi_irq_enable(fsi
, is_play
);
681 fsi_irq_disable(fsi
, is_play
);
683 if (fsi_is_clk_master(fsi
))
684 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
690 static void fsi_fifo_init(struct fsi_priv
*fsi
,
694 struct fsi_master
*master
= fsi_get_master(fsi
);
695 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
699 /* get on-chip RAM capacity */
700 shift
= fsi_master_read(master
, FIFO_SZ
);
701 shift
>>= fsi_get_port_shift(fsi
, is_play
);
702 shift
&= FIFO_SZ_MASK
;
703 frame_capa
= 256 << shift
;
704 dev_dbg(dev
, "fifo = %d words\n", frame_capa
);
707 * The maximum number of sample data varies depending
708 * on the number of channels selected for the format.
710 * FIFOs are used in 4-channel units in 3-channel mode
711 * and in 8-channel units in 5- to 7-channel mode
712 * meaning that more FIFOs than the required size of DPRAM
715 * ex) if 256 words of DP-RAM is connected
716 * 1 channel: 256 (256 x 1 = 256)
717 * 2 channels: 128 (128 x 2 = 256)
718 * 3 channels: 64 ( 64 x 3 = 192)
719 * 4 channels: 64 ( 64 x 4 = 256)
720 * 5 channels: 32 ( 32 x 5 = 160)
721 * 6 channels: 32 ( 32 x 6 = 192)
722 * 7 channels: 32 ( 32 x 7 = 224)
723 * 8 channels: 32 ( 32 x 8 = 256)
725 for (i
= 1; i
< fsi
->chan_num
; i
<<= 1)
727 dev_dbg(dev
, "%d channel %d store\n",
728 fsi
->chan_num
, frame_capa
);
730 io
->fifo_sample_capa
= fsi_frame2sample(fsi
, frame_capa
);
733 * set interrupt generation factor
737 fsi_reg_write(fsi
, DOFF_CTL
, IRQ_HALF
);
738 fsi_reg_mask_set(fsi
, DOFF_CTL
, FIFO_CLR
, FIFO_CLR
);
740 fsi_reg_write(fsi
, DIFF_CTL
, IRQ_HALF
);
741 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
745 static int fsi_fifo_data_ctrl(struct fsi_priv
*fsi
, int stream
)
747 struct snd_pcm_runtime
*runtime
;
748 struct snd_pcm_substream
*substream
= NULL
;
749 int is_play
= fsi_stream_is_play(stream
);
750 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
756 void (*fn
)(struct fsi_priv
*fsi
, int size
);
760 !io
->substream
->runtime
)
764 substream
= io
->substream
;
765 runtime
= substream
->runtime
;
767 /* FSI FIFO has limit.
768 * So, this driver can not send periods data at a time
770 if (io
->buff_sample_pos
>=
771 io
->period_samples
* (io
->period_pos
+ 1)) {
774 io
->period_pos
= (io
->period_pos
+ 1) % runtime
->periods
;
776 if (0 == io
->period_pos
)
777 io
->buff_sample_pos
= 0;
780 /* get 1 sample data width */
781 sample_width
= samples_to_bytes(runtime
, 1);
783 /* get number of residue samples */
784 sample_residues
= io
->buff_sample_capa
- io
->buff_sample_pos
;
790 * samples_max : number of FSI fifo free samples space
791 * samples : number of ALSA residue samples
793 samples_max
= io
->fifo_sample_capa
;
794 samples_max
-= fsi_get_current_fifo_samples(fsi
, is_play
);
796 samples
= sample_residues
;
798 switch (sample_width
) {
800 fn
= fsi_dma_soft_push16
;
803 fn
= fsi_dma_soft_push32
;
812 * samples_max : number of ALSA free samples space
813 * samples : number of samples in FSI fifo
815 samples_max
= sample_residues
;
816 samples
= fsi_get_current_fifo_samples(fsi
, is_play
);
818 switch (sample_width
) {
820 fn
= fsi_dma_soft_pop16
;
823 fn
= fsi_dma_soft_pop32
;
830 samples
= min(samples
, samples_max
);
834 /* update buff_sample_pos */
835 io
->buff_sample_pos
+= samples
;
838 snd_pcm_period_elapsed(substream
);
843 static int fsi_data_pop(struct fsi_priv
*fsi
)
845 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
848 static int fsi_data_push(struct fsi_priv
*fsi
)
850 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
853 static irqreturn_t
fsi_interrupt(int irq
, void *data
)
855 struct fsi_master
*master
= data
;
856 u32 int_st
= fsi_irq_get_status(master
);
858 /* clear irq status */
859 fsi_master_mask_set(master
, SOFT_RST
, IR
, 0);
860 fsi_master_mask_set(master
, SOFT_RST
, IR
, IR
);
862 if (int_st
& AB_IO(1, AO_SHIFT
))
863 fsi_data_push(&master
->fsia
);
864 if (int_st
& AB_IO(1, BO_SHIFT
))
865 fsi_data_push(&master
->fsib
);
866 if (int_st
& AB_IO(1, AI_SHIFT
))
867 fsi_data_pop(&master
->fsia
);
868 if (int_st
& AB_IO(1, BI_SHIFT
))
869 fsi_data_pop(&master
->fsib
);
871 fsi_count_fifo_err(&master
->fsia
);
872 fsi_count_fifo_err(&master
->fsib
);
874 fsi_irq_clear_status(&master
->fsia
);
875 fsi_irq_clear_status(&master
->fsib
);
884 static int fsi_hw_startup(struct fsi_priv
*fsi
,
888 u32 flags
= fsi_get_info_flags(fsi
);
891 pm_runtime_get_sync(dev
);
894 if (fsi_is_clk_master(fsi
))
897 fsi_reg_mask_set(fsi
, CKG1
, (DIMD
| DOMD
), data
);
899 /* clock inversion (CKG2) */
901 if (SH_FSI_LRM_INV
& flags
)
903 if (SH_FSI_BRM_INV
& flags
)
905 if (SH_FSI_LRS_INV
& flags
)
907 if (SH_FSI_BRS_INV
& flags
)
910 fsi_reg_write(fsi
, CKG2
, data
);
913 fsi_reg_write(fsi
, DO_FMT
, fsi
->do_fmt
);
914 fsi_reg_write(fsi
, DI_FMT
, fsi
->di_fmt
);
917 if (fsi_is_spdif(fsi
)) {
918 fsi_spdif_clk_ctrl(fsi
, 1);
919 fsi_reg_mask_set(fsi
, OUT_SEL
, DMMD
, DMMD
);
923 fsi_irq_disable(fsi
, is_play
);
924 fsi_irq_clear_status(fsi
);
927 fsi_fifo_init(fsi
, is_play
, dev
);
932 static void fsi_hw_shutdown(struct fsi_priv
*fsi
,
936 if (fsi_is_clk_master(fsi
))
937 fsi_set_master_clk(dev
, fsi
, fsi
->rate
, 0);
939 pm_runtime_put_sync(dev
);
942 static int fsi_dai_startup(struct snd_pcm_substream
*substream
,
943 struct snd_soc_dai
*dai
)
945 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
946 int is_play
= fsi_is_play(substream
);
948 return fsi_hw_startup(fsi
, is_play
, dai
->dev
);
951 static void fsi_dai_shutdown(struct snd_pcm_substream
*substream
,
952 struct snd_soc_dai
*dai
)
954 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
955 int is_play
= fsi_is_play(substream
);
957 fsi_hw_shutdown(fsi
, is_play
, dai
->dev
);
961 static int fsi_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
962 struct snd_soc_dai
*dai
)
964 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
965 int is_play
= fsi_is_play(substream
);
969 case SNDRV_PCM_TRIGGER_START
:
970 fsi_stream_push(fsi
, is_play
, substream
);
971 ret
= is_play
? fsi_data_push(fsi
) : fsi_data_pop(fsi
);
972 fsi_port_start(fsi
, is_play
);
974 case SNDRV_PCM_TRIGGER_STOP
:
975 fsi_port_stop(fsi
, is_play
);
976 fsi_stream_pop(fsi
, is_play
);
983 static int fsi_set_fmt_dai(struct fsi_priv
*fsi
, unsigned int fmt
)
987 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
988 case SND_SOC_DAIFMT_I2S
:
992 case SND_SOC_DAIFMT_LEFT_J
:
1006 static int fsi_set_fmt_spdif(struct fsi_priv
*fsi
)
1008 struct fsi_master
*master
= fsi_get_master(fsi
);
1011 if (master
->core
->ver
< 2)
1014 data
= CR_BWS_16
| CR_DTMD_SPDIF_PCM
| CR_PCM
;
1024 static int fsi_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1026 struct fsi_priv
*fsi
= fsi_get_priv_frm_dai(dai
);
1027 struct fsi_master
*master
= fsi_get_master(fsi
);
1028 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
1029 u32 flags
= fsi_get_info_flags(fsi
);
1032 /* set master/slave audio interface */
1033 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1034 case SND_SOC_DAIFMT_CBM_CFM
:
1035 fsi
->clk_master
= 1;
1037 case SND_SOC_DAIFMT_CBS_CFS
:
1043 if (fsi_is_clk_master(fsi
) && !set_rate
) {
1044 dev_err(dai
->dev
, "platform doesn't have set_rate\n");
1049 switch (flags
& SH_FSI_FMT_MASK
) {
1050 case SH_FSI_FMT_DAI
:
1051 ret
= fsi_set_fmt_dai(fsi
, fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
1053 case SH_FSI_FMT_SPDIF
:
1054 ret
= fsi_set_fmt_spdif(fsi
);
1063 static int fsi_dai_hw_params(struct snd_pcm_substream
*substream
,
1064 struct snd_pcm_hw_params
*params
,
1065 struct snd_soc_dai
*dai
)
1067 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1068 long rate
= params_rate(params
);
1071 if (!fsi_is_clk_master(fsi
))
1074 ret
= fsi_set_master_clk(dai
->dev
, fsi
, rate
, 1);
1083 static struct snd_soc_dai_ops fsi_dai_ops
= {
1084 .startup
= fsi_dai_startup
,
1085 .shutdown
= fsi_dai_shutdown
,
1086 .trigger
= fsi_dai_trigger
,
1087 .set_fmt
= fsi_dai_set_fmt
,
1088 .hw_params
= fsi_dai_hw_params
,
1095 static struct snd_pcm_hardware fsi_pcm_hardware
= {
1096 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
1097 SNDRV_PCM_INFO_MMAP
|
1098 SNDRV_PCM_INFO_MMAP_VALID
|
1099 SNDRV_PCM_INFO_PAUSE
,
1100 .formats
= FSI_FMTS
,
1106 .buffer_bytes_max
= 64 * 1024,
1107 .period_bytes_min
= 32,
1108 .period_bytes_max
= 8192,
1114 static int fsi_pcm_open(struct snd_pcm_substream
*substream
)
1116 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1119 snd_soc_set_runtime_hwparams(substream
, &fsi_pcm_hardware
);
1121 ret
= snd_pcm_hw_constraint_integer(runtime
,
1122 SNDRV_PCM_HW_PARAM_PERIODS
);
1127 static int fsi_hw_params(struct snd_pcm_substream
*substream
,
1128 struct snd_pcm_hw_params
*hw_params
)
1130 return snd_pcm_lib_malloc_pages(substream
,
1131 params_buffer_bytes(hw_params
));
1134 static int fsi_hw_free(struct snd_pcm_substream
*substream
)
1136 return snd_pcm_lib_free_pages(substream
);
1139 static snd_pcm_uframes_t
fsi_pointer(struct snd_pcm_substream
*substream
)
1141 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1142 struct fsi_stream
*io
= fsi_get_stream(fsi
, fsi_is_play(substream
));
1143 int samples_pos
= io
->buff_sample_pos
- 1;
1145 if (samples_pos
< 0)
1148 return fsi_sample2frame(fsi
, samples_pos
);
1151 static struct snd_pcm_ops fsi_pcm_ops
= {
1152 .open
= fsi_pcm_open
,
1153 .ioctl
= snd_pcm_lib_ioctl
,
1154 .hw_params
= fsi_hw_params
,
1155 .hw_free
= fsi_hw_free
,
1156 .pointer
= fsi_pointer
,
1163 #define PREALLOC_BUFFER (32 * 1024)
1164 #define PREALLOC_BUFFER_MAX (32 * 1024)
1166 static void fsi_pcm_free(struct snd_pcm
*pcm
)
1168 snd_pcm_lib_preallocate_free_for_all(pcm
);
1171 static int fsi_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
1173 struct snd_pcm
*pcm
= rtd
->pcm
;
1176 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1177 * in MMAP mode (i.e. aplay -M)
1179 return snd_pcm_lib_preallocate_pages_for_all(
1181 SNDRV_DMA_TYPE_CONTINUOUS
,
1182 snd_dma_continuous_data(GFP_KERNEL
),
1183 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1190 static struct snd_soc_dai_driver fsi_soc_dai
[] = {
1195 .formats
= FSI_FMTS
,
1201 .formats
= FSI_FMTS
,
1205 .ops
= &fsi_dai_ops
,
1211 .formats
= FSI_FMTS
,
1217 .formats
= FSI_FMTS
,
1221 .ops
= &fsi_dai_ops
,
1225 static struct snd_soc_platform_driver fsi_soc_platform
= {
1226 .ops
= &fsi_pcm_ops
,
1227 .pcm_new
= fsi_pcm_new
,
1228 .pcm_free
= fsi_pcm_free
,
1235 static int fsi_probe(struct platform_device
*pdev
)
1237 struct fsi_master
*master
;
1238 const struct platform_device_id
*id_entry
;
1239 struct resource
*res
;
1243 id_entry
= pdev
->id_entry
;
1245 dev_err(&pdev
->dev
, "unknown fsi device\n");
1249 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1250 irq
= platform_get_irq(pdev
, 0);
1251 if (!res
|| (int)irq
<= 0) {
1252 dev_err(&pdev
->dev
, "Not enough FSI platform resources.\n");
1257 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
1259 dev_err(&pdev
->dev
, "Could not allocate master\n");
1264 master
->base
= ioremap_nocache(res
->start
, resource_size(res
));
1265 if (!master
->base
) {
1267 dev_err(&pdev
->dev
, "Unable to ioremap FSI registers.\n");
1271 /* master setting */
1273 master
->info
= pdev
->dev
.platform_data
;
1274 master
->core
= (struct fsi_core
*)id_entry
->driver_data
;
1275 spin_lock_init(&master
->lock
);
1278 master
->fsia
.base
= master
->base
;
1279 master
->fsia
.master
= master
;
1282 master
->fsib
.base
= master
->base
+ 0x40;
1283 master
->fsib
.master
= master
;
1285 pm_runtime_enable(&pdev
->dev
);
1286 dev_set_drvdata(&pdev
->dev
, master
);
1288 ret
= request_irq(irq
, &fsi_interrupt
, IRQF_DISABLED
,
1289 id_entry
->name
, master
);
1291 dev_err(&pdev
->dev
, "irq request err\n");
1295 ret
= snd_soc_register_platform(&pdev
->dev
, &fsi_soc_platform
);
1297 dev_err(&pdev
->dev
, "cannot snd soc register\n");
1301 ret
= snd_soc_register_dais(&pdev
->dev
, fsi_soc_dai
,
1302 ARRAY_SIZE(fsi_soc_dai
));
1304 dev_err(&pdev
->dev
, "cannot snd dai register\n");
1311 snd_soc_unregister_platform(&pdev
->dev
);
1313 free_irq(irq
, master
);
1315 iounmap(master
->base
);
1316 pm_runtime_disable(&pdev
->dev
);
1324 static int fsi_remove(struct platform_device
*pdev
)
1326 struct fsi_master
*master
;
1328 master
= dev_get_drvdata(&pdev
->dev
);
1330 free_irq(master
->irq
, master
);
1331 pm_runtime_disable(&pdev
->dev
);
1333 snd_soc_unregister_dais(&pdev
->dev
, ARRAY_SIZE(fsi_soc_dai
));
1334 snd_soc_unregister_platform(&pdev
->dev
);
1336 iounmap(master
->base
);
1342 static void __fsi_suspend(struct fsi_priv
*fsi
,
1346 if (!fsi_stream_is_working(fsi
, is_play
))
1349 fsi_port_stop(fsi
, is_play
);
1350 fsi_hw_shutdown(fsi
, is_play
, dev
);
1353 static void __fsi_resume(struct fsi_priv
*fsi
,
1357 if (!fsi_stream_is_working(fsi
, is_play
))
1360 fsi_hw_startup(fsi
, is_play
, dev
);
1362 if (fsi_is_clk_master(fsi
) && fsi
->rate
)
1363 fsi_set_master_clk(dev
, fsi
, fsi
->rate
, 1);
1365 fsi_port_start(fsi
, is_play
);
1369 static int fsi_suspend(struct device
*dev
)
1371 struct fsi_master
*master
= dev_get_drvdata(dev
);
1372 struct fsi_priv
*fsia
= &master
->fsia
;
1373 struct fsi_priv
*fsib
= &master
->fsib
;
1375 __fsi_suspend(fsia
, 1, dev
);
1376 __fsi_suspend(fsia
, 0, dev
);
1378 __fsi_suspend(fsib
, 1, dev
);
1379 __fsi_suspend(fsib
, 0, dev
);
1384 static int fsi_resume(struct device
*dev
)
1386 struct fsi_master
*master
= dev_get_drvdata(dev
);
1387 struct fsi_priv
*fsia
= &master
->fsia
;
1388 struct fsi_priv
*fsib
= &master
->fsib
;
1390 __fsi_resume(fsia
, 1, dev
);
1391 __fsi_resume(fsia
, 0, dev
);
1393 __fsi_resume(fsib
, 1, dev
);
1394 __fsi_resume(fsib
, 0, dev
);
1399 static int fsi_runtime_nop(struct device
*dev
)
1401 /* Runtime PM callback shared between ->runtime_suspend()
1402 * and ->runtime_resume(). Simply returns success.
1404 * This driver re-initializes all registers after
1405 * pm_runtime_get_sync() anyway so there is no need
1406 * to save and restore registers here.
1411 static struct dev_pm_ops fsi_pm_ops
= {
1412 .suspend
= fsi_suspend
,
1413 .resume
= fsi_resume
,
1414 .runtime_suspend
= fsi_runtime_nop
,
1415 .runtime_resume
= fsi_runtime_nop
,
1418 static struct fsi_core fsi1_core
= {
1427 static struct fsi_core fsi2_core
= {
1431 .int_st
= CPU_INT_ST
,
1434 .a_mclk
= A_MST_CTLR
,
1435 .b_mclk
= B_MST_CTLR
,
1438 static struct platform_device_id fsi_id_table
[] = {
1439 { "sh_fsi", (kernel_ulong_t
)&fsi1_core
},
1440 { "sh_fsi2", (kernel_ulong_t
)&fsi2_core
},
1443 MODULE_DEVICE_TABLE(platform
, fsi_id_table
);
1445 static struct platform_driver fsi_driver
= {
1447 .name
= "fsi-pcm-audio",
1451 .remove
= fsi_remove
,
1452 .id_table
= fsi_id_table
,
1455 static int __init
fsi_mobile_init(void)
1457 return platform_driver_register(&fsi_driver
);
1460 static void __exit
fsi_mobile_exit(void)
1462 platform_driver_unregister(&fsi_driver
);
1465 module_init(fsi_mobile_init
);
1466 module_exit(fsi_mobile_exit
);
1468 MODULE_LICENSE("GPL");
1469 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1470 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1471 MODULE_ALIAS("platform:fsi-pcm-audio");