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
;
188 /* for suspend/resume */
209 struct fsi_priv fsia
;
210 struct fsi_priv fsib
;
211 struct fsi_core
*core
;
212 struct sh_fsi_platform_info
*info
;
215 /* for suspend/resume */
225 * basic read write function
228 static void __fsi_reg_write(u32 reg
, u32 data
)
230 /* valid data area is 24bit */
233 __raw_writel(data
, reg
);
236 static u32
__fsi_reg_read(u32 reg
)
238 return __raw_readl(reg
);
241 static void __fsi_reg_mask_set(u32 reg
, u32 mask
, u32 data
)
243 u32 val
= __fsi_reg_read(reg
);
248 __fsi_reg_write(reg
, val
);
251 #define fsi_reg_write(p, r, d)\
252 __fsi_reg_write((u32)(p->base + REG_##r), d)
254 #define fsi_reg_read(p, r)\
255 __fsi_reg_read((u32)(p->base + REG_##r))
257 #define fsi_reg_mask_set(p, r, m, d)\
258 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
260 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
261 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
262 static u32
_fsi_master_read(struct fsi_master
*master
, u32 reg
)
267 spin_lock_irqsave(&master
->lock
, flags
);
268 ret
= __fsi_reg_read((u32
)(master
->base
+ reg
));
269 spin_unlock_irqrestore(&master
->lock
, flags
);
274 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
275 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
276 static void _fsi_master_mask_set(struct fsi_master
*master
,
277 u32 reg
, u32 mask
, u32 data
)
281 spin_lock_irqsave(&master
->lock
, flags
);
282 __fsi_reg_mask_set((u32
)(master
->base
+ reg
), mask
, data
);
283 spin_unlock_irqrestore(&master
->lock
, flags
);
290 static struct fsi_master
*fsi_get_master(struct fsi_priv
*fsi
)
295 static int fsi_is_clk_master(struct fsi_priv
*fsi
)
297 return fsi
->clk_master
;
300 static int fsi_is_port_a(struct fsi_priv
*fsi
)
302 return fsi
->master
->base
== fsi
->base
;
305 static int fsi_is_spdif(struct fsi_priv
*fsi
)
310 static struct snd_soc_dai
*fsi_get_dai(struct snd_pcm_substream
*substream
)
312 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
317 static struct fsi_priv
*fsi_get_priv_frm_dai(struct snd_soc_dai
*dai
)
319 struct fsi_master
*master
= snd_soc_dai_get_drvdata(dai
);
322 return &master
->fsia
;
324 return &master
->fsib
;
327 static struct fsi_priv
*fsi_get_priv(struct snd_pcm_substream
*substream
)
329 return fsi_get_priv_frm_dai(fsi_get_dai(substream
));
332 static set_rate_func
fsi_get_info_set_rate(struct fsi_master
*master
)
337 return master
->info
->set_rate
;
340 static u32
fsi_get_info_flags(struct fsi_priv
*fsi
)
342 int is_porta
= fsi_is_port_a(fsi
);
343 struct fsi_master
*master
= fsi_get_master(fsi
);
348 return is_porta
? master
->info
->porta_flags
:
349 master
->info
->portb_flags
;
352 static inline int fsi_stream_is_play(int stream
)
354 return stream
== SNDRV_PCM_STREAM_PLAYBACK
;
357 static inline int fsi_is_play(struct snd_pcm_substream
*substream
)
359 return fsi_stream_is_play(substream
->stream
);
362 static inline struct fsi_stream
*fsi_get_stream(struct fsi_priv
*fsi
,
365 return is_play
? &fsi
->playback
: &fsi
->capture
;
368 static u32
fsi_get_port_shift(struct fsi_priv
*fsi
, int is_play
)
370 int is_porta
= fsi_is_port_a(fsi
);
374 shift
= is_play
? AO_SHIFT
: AI_SHIFT
;
376 shift
= is_play
? BO_SHIFT
: BI_SHIFT
;
381 static int fsi_frame2sample(struct fsi_priv
*fsi
, int frames
)
383 return frames
* fsi
->chan_num
;
386 static int fsi_sample2frame(struct fsi_priv
*fsi
, int samples
)
388 return samples
/ fsi
->chan_num
;
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
;
398 io
->substream
= substream
;
399 io
->buff_sample_capa
= fsi_frame2sample(fsi
, runtime
->buffer_size
);
400 io
->buff_sample_pos
= 0;
401 io
->period_samples
= fsi_frame2sample(fsi
, runtime
->period_size
);
403 io
->oerr_num
= -1; /* ignore 1st err */
404 io
->uerr_num
= -1; /* ignore 1st err */
407 static void fsi_stream_pop(struct fsi_priv
*fsi
, int is_play
)
409 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
410 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
413 if (io
->oerr_num
> 0)
414 dev_err(dai
->dev
, "over_run = %d\n", io
->oerr_num
);
416 if (io
->uerr_num
> 0)
417 dev_err(dai
->dev
, "under_run = %d\n", io
->uerr_num
);
419 io
->substream
= NULL
;
420 io
->buff_sample_capa
= 0;
421 io
->buff_sample_pos
= 0;
422 io
->period_samples
= 0;
428 static int fsi_get_current_fifo_samples(struct fsi_priv
*fsi
, int is_play
)
434 fsi_reg_read(fsi
, DOFF_ST
) :
435 fsi_reg_read(fsi
, DIFF_ST
);
437 frames
= 0x1ff & (status
>> 8);
439 return fsi_frame2sample(fsi
, frames
);
442 static void fsi_count_fifo_err(struct fsi_priv
*fsi
)
444 u32 ostatus
= fsi_reg_read(fsi
, DOFF_ST
);
445 u32 istatus
= fsi_reg_read(fsi
, DIFF_ST
);
447 if (ostatus
& ERR_OVER
)
448 fsi
->playback
.oerr_num
++;
450 if (ostatus
& ERR_UNDER
)
451 fsi
->playback
.uerr_num
++;
453 if (istatus
& ERR_OVER
)
454 fsi
->capture
.oerr_num
++;
456 if (istatus
& ERR_UNDER
)
457 fsi
->capture
.uerr_num
++;
459 fsi_reg_write(fsi
, DOFF_ST
, 0);
460 fsi_reg_write(fsi
, DIFF_ST
, 0);
467 static u8
*fsi_dma_get_area(struct fsi_priv
*fsi
, int stream
)
469 int is_play
= fsi_stream_is_play(stream
);
470 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
471 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
473 return runtime
->dma_area
+
474 samples_to_bytes(runtime
, io
->buff_sample_pos
);
477 static void fsi_dma_soft_push16(struct fsi_priv
*fsi
, int num
)
482 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
484 for (i
= 0; i
< num
; i
++)
485 fsi_reg_write(fsi
, DODT
, ((u32
)*(start
+ i
) << 8));
488 static void fsi_dma_soft_pop16(struct fsi_priv
*fsi
, int num
)
493 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
496 for (i
= 0; i
< num
; i
++)
497 *(start
+ i
) = (u16
)(fsi_reg_read(fsi
, DIDT
) >> 8);
500 static void fsi_dma_soft_push32(struct fsi_priv
*fsi
, int num
)
505 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
508 for (i
= 0; i
< num
; i
++)
509 fsi_reg_write(fsi
, DODT
, *(start
+ i
));
512 static void fsi_dma_soft_pop32(struct fsi_priv
*fsi
, int num
)
517 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
519 for (i
= 0; i
< num
; i
++)
520 *(start
+ i
) = fsi_reg_read(fsi
, DIDT
);
527 static void fsi_irq_enable(struct fsi_priv
*fsi
, int is_play
)
529 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
530 struct fsi_master
*master
= fsi_get_master(fsi
);
532 fsi_core_mask_set(master
, imsk
, data
, data
);
533 fsi_core_mask_set(master
, iemsk
, data
, data
);
536 static void fsi_irq_disable(struct fsi_priv
*fsi
, int is_play
)
538 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
539 struct fsi_master
*master
= fsi_get_master(fsi
);
541 fsi_core_mask_set(master
, imsk
, data
, 0);
542 fsi_core_mask_set(master
, iemsk
, data
, 0);
545 static u32
fsi_irq_get_status(struct fsi_master
*master
)
547 return fsi_core_read(master
, int_st
);
550 static void fsi_irq_clear_status(struct fsi_priv
*fsi
)
553 struct fsi_master
*master
= fsi_get_master(fsi
);
555 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 0));
556 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 1));
558 /* clear interrupt factor */
559 fsi_core_mask_set(master
, int_st
, data
, 0);
563 * SPDIF master clock function
565 * These functions are used later FSI2
567 static void fsi_spdif_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
569 struct fsi_master
*master
= fsi_get_master(fsi
);
572 if (master
->core
->ver
< 2) {
573 pr_err("fsi: register access err (%s)\n", __func__
);
578 val
= enable
? mask
: 0;
581 fsi_core_mask_set(master
, a_mclk
, mask
, val
) :
582 fsi_core_mask_set(master
, b_mclk
, mask
, val
);
588 static int fsi_set_master_clk(struct device
*dev
, struct fsi_priv
*fsi
,
589 long rate
, int enable
)
591 struct fsi_master
*master
= fsi_get_master(fsi
);
592 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
593 int fsi_ver
= master
->core
->ver
;
596 ret
= set_rate(dev
, fsi_is_port_a(fsi
), rate
, enable
);
597 if (ret
< 0) /* error */
606 switch (ret
& SH_FSI_ACKMD_MASK
) {
609 case SH_FSI_ACKMD_512
:
612 case SH_FSI_ACKMD_256
:
615 case SH_FSI_ACKMD_128
:
618 case SH_FSI_ACKMD_64
:
621 case SH_FSI_ACKMD_32
:
623 dev_err(dev
, "unsupported ACKMD\n");
629 switch (ret
& SH_FSI_BPFMD_MASK
) {
632 case SH_FSI_BPFMD_32
:
635 case SH_FSI_BPFMD_64
:
638 case SH_FSI_BPFMD_128
:
641 case SH_FSI_BPFMD_256
:
644 case SH_FSI_BPFMD_512
:
647 case SH_FSI_BPFMD_16
:
649 dev_err(dev
, "unsupported ACKMD\n");
655 fsi_reg_mask_set(fsi
, CKG1
, (ACKMD_MASK
| BPFMD_MASK
) , data
);
664 #define fsi_module_init(m, d) __fsi_module_clk_ctrl(m, d, 1)
665 #define fsi_module_kill(m, d) __fsi_module_clk_ctrl(m, d, 0)
666 static void __fsi_module_clk_ctrl(struct fsi_master
*master
,
670 pm_runtime_get_sync(dev
);
674 fsi_master_mask_set(master
, SOFT_RST
, FSISR
, FSISR
);
675 fsi_master_mask_set(master
, SOFT_RST
, PASR
| PBSR
, 0);
677 /* clear all registers */
678 fsi_master_mask_set(master
, SOFT_RST
, FSISR
, 0);
681 pm_runtime_put_sync(dev
);
684 #define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
685 #define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
686 static void __fsi_port_clk_ctrl(struct fsi_priv
*fsi
, int is_play
, int enable
)
688 struct fsi_master
*master
= fsi_get_master(fsi
);
689 u32 soft
= fsi_is_port_a(fsi
) ? PASR
: PBSR
;
690 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
691 int is_master
= fsi_is_clk_master(fsi
);
694 fsi_irq_enable(fsi
, is_play
);
696 fsi_irq_disable(fsi
, is_play
);
698 fsi_master_mask_set(master
, SOFT_RST
, soft
, (enable
) ? soft
: 0);
700 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
706 static void fsi_fifo_init(struct fsi_priv
*fsi
,
708 struct snd_soc_dai
*dai
)
710 struct fsi_master
*master
= fsi_get_master(fsi
);
711 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
715 /* get on-chip RAM capacity */
716 shift
= fsi_master_read(master
, FIFO_SZ
);
717 shift
>>= fsi_get_port_shift(fsi
, is_play
);
718 shift
&= FIFO_SZ_MASK
;
719 frame_capa
= 256 << shift
;
720 dev_dbg(dai
->dev
, "fifo = %d words\n", frame_capa
);
723 * The maximum number of sample data varies depending
724 * on the number of channels selected for the format.
726 * FIFOs are used in 4-channel units in 3-channel mode
727 * and in 8-channel units in 5- to 7-channel mode
728 * meaning that more FIFOs than the required size of DPRAM
731 * ex) if 256 words of DP-RAM is connected
732 * 1 channel: 256 (256 x 1 = 256)
733 * 2 channels: 128 (128 x 2 = 256)
734 * 3 channels: 64 ( 64 x 3 = 192)
735 * 4 channels: 64 ( 64 x 4 = 256)
736 * 5 channels: 32 ( 32 x 5 = 160)
737 * 6 channels: 32 ( 32 x 6 = 192)
738 * 7 channels: 32 ( 32 x 7 = 224)
739 * 8 channels: 32 ( 32 x 8 = 256)
741 for (i
= 1; i
< fsi
->chan_num
; i
<<= 1)
743 dev_dbg(dai
->dev
, "%d channel %d store\n",
744 fsi
->chan_num
, frame_capa
);
746 io
->fifo_sample_capa
= fsi_frame2sample(fsi
, frame_capa
);
749 * set interrupt generation factor
753 fsi_reg_write(fsi
, DOFF_CTL
, IRQ_HALF
);
754 fsi_reg_mask_set(fsi
, DOFF_CTL
, FIFO_CLR
, FIFO_CLR
);
756 fsi_reg_write(fsi
, DIFF_CTL
, IRQ_HALF
);
757 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
761 static int fsi_fifo_data_ctrl(struct fsi_priv
*fsi
, int stream
)
763 struct snd_pcm_runtime
*runtime
;
764 struct snd_pcm_substream
*substream
= NULL
;
765 int is_play
= fsi_stream_is_play(stream
);
766 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
772 void (*fn
)(struct fsi_priv
*fsi
, int size
);
776 !io
->substream
->runtime
)
780 substream
= io
->substream
;
781 runtime
= substream
->runtime
;
783 /* FSI FIFO has limit.
784 * So, this driver can not send periods data at a time
786 if (io
->buff_sample_pos
>=
787 io
->period_samples
* (io
->period_pos
+ 1)) {
790 io
->period_pos
= (io
->period_pos
+ 1) % runtime
->periods
;
792 if (0 == io
->period_pos
)
793 io
->buff_sample_pos
= 0;
796 /* get 1 sample data width */
797 sample_width
= samples_to_bytes(runtime
, 1);
799 /* get number of residue samples */
800 sample_residues
= io
->buff_sample_capa
- io
->buff_sample_pos
;
806 * samples_max : number of FSI fifo free samples space
807 * samples : number of ALSA residue samples
809 samples_max
= io
->fifo_sample_capa
;
810 samples_max
-= fsi_get_current_fifo_samples(fsi
, is_play
);
812 samples
= sample_residues
;
814 switch (sample_width
) {
816 fn
= fsi_dma_soft_push16
;
819 fn
= fsi_dma_soft_push32
;
828 * samples_max : number of ALSA free samples space
829 * samples : number of samples in FSI fifo
831 samples_max
= sample_residues
;
832 samples
= fsi_get_current_fifo_samples(fsi
, is_play
);
834 switch (sample_width
) {
836 fn
= fsi_dma_soft_pop16
;
839 fn
= fsi_dma_soft_pop32
;
846 samples
= min(samples
, samples_max
);
850 /* update buff_sample_pos */
851 io
->buff_sample_pos
+= samples
;
854 snd_pcm_period_elapsed(substream
);
859 static int fsi_data_pop(struct fsi_priv
*fsi
)
861 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
864 static int fsi_data_push(struct fsi_priv
*fsi
)
866 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
869 static irqreturn_t
fsi_interrupt(int irq
, void *data
)
871 struct fsi_master
*master
= data
;
872 u32 int_st
= fsi_irq_get_status(master
);
874 /* clear irq status */
875 fsi_master_mask_set(master
, SOFT_RST
, IR
, 0);
876 fsi_master_mask_set(master
, SOFT_RST
, IR
, IR
);
878 if (int_st
& AB_IO(1, AO_SHIFT
))
879 fsi_data_push(&master
->fsia
);
880 if (int_st
& AB_IO(1, BO_SHIFT
))
881 fsi_data_push(&master
->fsib
);
882 if (int_st
& AB_IO(1, AI_SHIFT
))
883 fsi_data_pop(&master
->fsia
);
884 if (int_st
& AB_IO(1, BI_SHIFT
))
885 fsi_data_pop(&master
->fsib
);
887 fsi_count_fifo_err(&master
->fsia
);
888 fsi_count_fifo_err(&master
->fsib
);
890 fsi_irq_clear_status(&master
->fsia
);
891 fsi_irq_clear_status(&master
->fsib
);
900 static int fsi_dai_startup(struct snd_pcm_substream
*substream
,
901 struct snd_soc_dai
*dai
)
903 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
904 u32 flags
= fsi_get_info_flags(fsi
);
906 int is_play
= fsi_is_play(substream
);
908 pm_runtime_get_sync(dai
->dev
);
911 if (fsi_is_clk_master(fsi
))
914 fsi_reg_mask_set(fsi
, CKG1
, (DIMD
| DOMD
), data
);
916 /* clock inversion (CKG2) */
918 if (SH_FSI_LRM_INV
& flags
)
920 if (SH_FSI_BRM_INV
& flags
)
922 if (SH_FSI_LRS_INV
& flags
)
924 if (SH_FSI_BRS_INV
& flags
)
927 fsi_reg_write(fsi
, CKG2
, data
);
930 fsi_reg_write(fsi
, DO_FMT
, fsi
->do_fmt
);
931 fsi_reg_write(fsi
, DI_FMT
, fsi
->di_fmt
);
934 if (fsi_is_spdif(fsi
)) {
935 fsi_spdif_clk_ctrl(fsi
, 1);
936 fsi_reg_mask_set(fsi
, OUT_SEL
, DMMD
, DMMD
);
940 fsi_irq_disable(fsi
, is_play
);
941 fsi_irq_clear_status(fsi
);
944 fsi_fifo_init(fsi
, is_play
, dai
);
949 static void fsi_dai_shutdown(struct snd_pcm_substream
*substream
,
950 struct snd_soc_dai
*dai
)
952 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
954 if (fsi_is_clk_master(fsi
))
955 fsi_set_master_clk(dai
->dev
, fsi
, fsi
->rate
, 0);
959 pm_runtime_put_sync(dai
->dev
);
962 static int fsi_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
963 struct snd_soc_dai
*dai
)
965 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
966 int is_play
= fsi_is_play(substream
);
970 case SNDRV_PCM_TRIGGER_START
:
971 fsi_stream_push(fsi
, is_play
, substream
);
972 ret
= is_play
? fsi_data_push(fsi
) : fsi_data_pop(fsi
);
973 fsi_port_start(fsi
, is_play
);
975 case SNDRV_PCM_TRIGGER_STOP
:
976 fsi_port_stop(fsi
, is_play
);
977 fsi_stream_pop(fsi
, is_play
);
984 static int fsi_set_fmt_dai(struct fsi_priv
*fsi
, unsigned int fmt
)
988 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
989 case SND_SOC_DAIFMT_I2S
:
993 case SND_SOC_DAIFMT_LEFT_J
:
1007 static int fsi_set_fmt_spdif(struct fsi_priv
*fsi
)
1009 struct fsi_master
*master
= fsi_get_master(fsi
);
1012 if (master
->core
->ver
< 2)
1015 data
= CR_BWS_16
| CR_DTMD_SPDIF_PCM
| CR_PCM
;
1025 static int fsi_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1027 struct fsi_priv
*fsi
= fsi_get_priv_frm_dai(dai
);
1028 struct fsi_master
*master
= fsi_get_master(fsi
);
1029 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
1030 u32 flags
= fsi_get_info_flags(fsi
);
1033 /* set master/slave audio interface */
1034 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1035 case SND_SOC_DAIFMT_CBM_CFM
:
1036 fsi
->clk_master
= 1;
1038 case SND_SOC_DAIFMT_CBS_CFS
:
1044 if (fsi_is_clk_master(fsi
) && !set_rate
) {
1045 dev_err(dai
->dev
, "platform doesn't have set_rate\n");
1050 switch (flags
& SH_FSI_FMT_MASK
) {
1051 case SH_FSI_FMT_DAI
:
1052 ret
= fsi_set_fmt_dai(fsi
, fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
1054 case SH_FSI_FMT_SPDIF
:
1055 ret
= fsi_set_fmt_spdif(fsi
);
1064 static int fsi_dai_hw_params(struct snd_pcm_substream
*substream
,
1065 struct snd_pcm_hw_params
*params
,
1066 struct snd_soc_dai
*dai
)
1068 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1069 long rate
= params_rate(params
);
1072 if (!fsi_is_clk_master(fsi
))
1075 ret
= fsi_set_master_clk(dai
->dev
, fsi
, rate
, 1);
1084 static struct snd_soc_dai_ops fsi_dai_ops
= {
1085 .startup
= fsi_dai_startup
,
1086 .shutdown
= fsi_dai_shutdown
,
1087 .trigger
= fsi_dai_trigger
,
1088 .set_fmt
= fsi_dai_set_fmt
,
1089 .hw_params
= fsi_dai_hw_params
,
1096 static struct snd_pcm_hardware fsi_pcm_hardware
= {
1097 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
1098 SNDRV_PCM_INFO_MMAP
|
1099 SNDRV_PCM_INFO_MMAP_VALID
|
1100 SNDRV_PCM_INFO_PAUSE
,
1101 .formats
= FSI_FMTS
,
1107 .buffer_bytes_max
= 64 * 1024,
1108 .period_bytes_min
= 32,
1109 .period_bytes_max
= 8192,
1115 static int fsi_pcm_open(struct snd_pcm_substream
*substream
)
1117 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1120 snd_soc_set_runtime_hwparams(substream
, &fsi_pcm_hardware
);
1122 ret
= snd_pcm_hw_constraint_integer(runtime
,
1123 SNDRV_PCM_HW_PARAM_PERIODS
);
1128 static int fsi_hw_params(struct snd_pcm_substream
*substream
,
1129 struct snd_pcm_hw_params
*hw_params
)
1131 return snd_pcm_lib_malloc_pages(substream
,
1132 params_buffer_bytes(hw_params
));
1135 static int fsi_hw_free(struct snd_pcm_substream
*substream
)
1137 return snd_pcm_lib_free_pages(substream
);
1140 static snd_pcm_uframes_t
fsi_pointer(struct snd_pcm_substream
*substream
)
1142 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1143 struct fsi_stream
*io
= fsi_get_stream(fsi
, fsi_is_play(substream
));
1144 int samples_pos
= io
->buff_sample_pos
- 1;
1146 if (samples_pos
< 0)
1149 return fsi_sample2frame(fsi
, samples_pos
);
1152 static struct snd_pcm_ops fsi_pcm_ops
= {
1153 .open
= fsi_pcm_open
,
1154 .ioctl
= snd_pcm_lib_ioctl
,
1155 .hw_params
= fsi_hw_params
,
1156 .hw_free
= fsi_hw_free
,
1157 .pointer
= fsi_pointer
,
1164 #define PREALLOC_BUFFER (32 * 1024)
1165 #define PREALLOC_BUFFER_MAX (32 * 1024)
1167 static void fsi_pcm_free(struct snd_pcm
*pcm
)
1169 snd_pcm_lib_preallocate_free_for_all(pcm
);
1172 static int fsi_pcm_new(struct snd_card
*card
,
1173 struct snd_soc_dai
*dai
,
1174 struct snd_pcm
*pcm
)
1177 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1178 * in MMAP mode (i.e. aplay -M)
1180 return snd_pcm_lib_preallocate_pages_for_all(
1182 SNDRV_DMA_TYPE_CONTINUOUS
,
1183 snd_dma_continuous_data(GFP_KERNEL
),
1184 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1191 static struct snd_soc_dai_driver fsi_soc_dai
[] = {
1196 .formats
= FSI_FMTS
,
1202 .formats
= FSI_FMTS
,
1206 .ops
= &fsi_dai_ops
,
1212 .formats
= FSI_FMTS
,
1218 .formats
= FSI_FMTS
,
1222 .ops
= &fsi_dai_ops
,
1226 static struct snd_soc_platform_driver fsi_soc_platform
= {
1227 .ops
= &fsi_pcm_ops
,
1228 .pcm_new
= fsi_pcm_new
,
1229 .pcm_free
= fsi_pcm_free
,
1236 static int fsi_probe(struct platform_device
*pdev
)
1238 struct fsi_master
*master
;
1239 const struct platform_device_id
*id_entry
;
1240 struct resource
*res
;
1244 id_entry
= pdev
->id_entry
;
1246 dev_err(&pdev
->dev
, "unknown fsi device\n");
1250 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1251 irq
= platform_get_irq(pdev
, 0);
1252 if (!res
|| (int)irq
<= 0) {
1253 dev_err(&pdev
->dev
, "Not enough FSI platform resources.\n");
1258 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
1260 dev_err(&pdev
->dev
, "Could not allocate master\n");
1265 master
->base
= ioremap_nocache(res
->start
, resource_size(res
));
1266 if (!master
->base
) {
1268 dev_err(&pdev
->dev
, "Unable to ioremap FSI registers.\n");
1272 /* master setting */
1274 master
->info
= pdev
->dev
.platform_data
;
1275 master
->core
= (struct fsi_core
*)id_entry
->driver_data
;
1276 spin_lock_init(&master
->lock
);
1279 master
->fsia
.base
= master
->base
;
1280 master
->fsia
.master
= master
;
1283 master
->fsib
.base
= master
->base
+ 0x40;
1284 master
->fsib
.master
= master
;
1286 pm_runtime_enable(&pdev
->dev
);
1287 dev_set_drvdata(&pdev
->dev
, master
);
1289 fsi_module_init(master
, &pdev
->dev
);
1291 ret
= request_irq(irq
, &fsi_interrupt
, IRQF_DISABLED
,
1292 id_entry
->name
, master
);
1294 dev_err(&pdev
->dev
, "irq request err\n");
1298 ret
= snd_soc_register_platform(&pdev
->dev
, &fsi_soc_platform
);
1300 dev_err(&pdev
->dev
, "cannot snd soc register\n");
1304 ret
= snd_soc_register_dais(&pdev
->dev
, fsi_soc_dai
,
1305 ARRAY_SIZE(fsi_soc_dai
));
1307 dev_err(&pdev
->dev
, "cannot snd dai register\n");
1314 snd_soc_unregister_platform(&pdev
->dev
);
1316 free_irq(irq
, master
);
1318 iounmap(master
->base
);
1319 pm_runtime_disable(&pdev
->dev
);
1327 static int fsi_remove(struct platform_device
*pdev
)
1329 struct fsi_master
*master
;
1331 master
= dev_get_drvdata(&pdev
->dev
);
1333 fsi_module_kill(master
, &pdev
->dev
);
1335 free_irq(master
->irq
, master
);
1336 pm_runtime_disable(&pdev
->dev
);
1338 snd_soc_unregister_dais(&pdev
->dev
, ARRAY_SIZE(fsi_soc_dai
));
1339 snd_soc_unregister_platform(&pdev
->dev
);
1341 iounmap(master
->base
);
1347 static void __fsi_suspend(struct fsi_priv
*fsi
,
1350 fsi
->saved_do_fmt
= fsi_reg_read(fsi
, DO_FMT
);
1351 fsi
->saved_di_fmt
= fsi_reg_read(fsi
, DI_FMT
);
1352 fsi
->saved_ckg1
= fsi_reg_read(fsi
, CKG1
);
1353 fsi
->saved_ckg2
= fsi_reg_read(fsi
, CKG2
);
1354 fsi
->saved_out_sel
= fsi_reg_read(fsi
, OUT_SEL
);
1356 if (fsi_is_clk_master(fsi
))
1357 fsi_set_master_clk(dev
, fsi
, fsi
->rate
, 0);
1360 static void __fsi_resume(struct fsi_priv
*fsi
,
1363 fsi_reg_write(fsi
, DO_FMT
, fsi
->saved_do_fmt
);
1364 fsi_reg_write(fsi
, DI_FMT
, fsi
->saved_di_fmt
);
1365 fsi_reg_write(fsi
, CKG1
, fsi
->saved_ckg1
);
1366 fsi_reg_write(fsi
, CKG2
, fsi
->saved_ckg2
);
1367 fsi_reg_write(fsi
, OUT_SEL
, fsi
->saved_out_sel
);
1369 if (fsi_is_clk_master(fsi
))
1370 fsi_set_master_clk(dev
, fsi
, fsi
->rate
, 1);
1373 static int fsi_suspend(struct device
*dev
)
1375 struct fsi_master
*master
= dev_get_drvdata(dev
);
1377 pm_runtime_get_sync(dev
);
1379 __fsi_suspend(&master
->fsia
, dev
);
1380 __fsi_suspend(&master
->fsib
, dev
);
1382 master
->saved_a_mclk
= fsi_core_read(master
, a_mclk
);
1383 master
->saved_b_mclk
= fsi_core_read(master
, b_mclk
);
1384 master
->saved_iemsk
= fsi_core_read(master
, iemsk
);
1385 master
->saved_imsk
= fsi_core_read(master
, imsk
);
1386 master
->saved_clk_rst
= fsi_master_read(master
, CLK_RST
);
1387 master
->saved_soft_rst
= fsi_master_read(master
, SOFT_RST
);
1389 fsi_module_kill(master
, dev
);
1391 pm_runtime_put_sync(dev
);
1396 static int fsi_resume(struct device
*dev
)
1398 struct fsi_master
*master
= dev_get_drvdata(dev
);
1400 pm_runtime_get_sync(dev
);
1402 fsi_module_init(master
, dev
);
1404 fsi_master_mask_set(master
, SOFT_RST
, 0xffff, master
->saved_soft_rst
);
1405 fsi_master_mask_set(master
, CLK_RST
, 0xffff, master
->saved_clk_rst
);
1406 fsi_core_mask_set(master
, a_mclk
, 0xffff, master
->saved_a_mclk
);
1407 fsi_core_mask_set(master
, b_mclk
, 0xffff, master
->saved_b_mclk
);
1408 fsi_core_mask_set(master
, iemsk
, 0xffff, master
->saved_iemsk
);
1409 fsi_core_mask_set(master
, imsk
, 0xffff, master
->saved_imsk
);
1411 __fsi_resume(&master
->fsia
, dev
);
1412 __fsi_resume(&master
->fsib
, dev
);
1414 pm_runtime_put_sync(dev
);
1419 static int fsi_runtime_nop(struct device
*dev
)
1421 /* Runtime PM callback shared between ->runtime_suspend()
1422 * and ->runtime_resume(). Simply returns success.
1424 * This driver re-initializes all registers after
1425 * pm_runtime_get_sync() anyway so there is no need
1426 * to save and restore registers here.
1431 static struct dev_pm_ops fsi_pm_ops
= {
1432 .suspend
= fsi_suspend
,
1433 .resume
= fsi_resume
,
1434 .runtime_suspend
= fsi_runtime_nop
,
1435 .runtime_resume
= fsi_runtime_nop
,
1438 static struct fsi_core fsi1_core
= {
1447 static struct fsi_core fsi2_core
= {
1451 .int_st
= CPU_INT_ST
,
1454 .a_mclk
= A_MST_CTLR
,
1455 .b_mclk
= B_MST_CTLR
,
1458 static struct platform_device_id fsi_id_table
[] = {
1459 { "sh_fsi", (kernel_ulong_t
)&fsi1_core
},
1460 { "sh_fsi2", (kernel_ulong_t
)&fsi2_core
},
1463 MODULE_DEVICE_TABLE(platform
, fsi_id_table
);
1465 static struct platform_driver fsi_driver
= {
1467 .name
= "fsi-pcm-audio",
1471 .remove
= fsi_remove
,
1472 .id_table
= fsi_id_table
,
1475 static int __init
fsi_mobile_init(void)
1477 return platform_driver_register(&fsi_driver
);
1480 static void __exit
fsi_mobile_exit(void)
1482 platform_driver_unregister(&fsi_driver
);
1485 module_init(fsi_mobile_init
);
1486 module_exit(fsi_mobile_exit
);
1488 MODULE_LICENSE("GPL");
1489 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1490 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1491 MODULE_ALIAS("platform:fsi-pcm-audio");