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 <linux/module.h>
20 #include <sound/soc.h>
21 #include <sound/sh_fsi.h>
23 /* PortA/PortB register */
24 #define REG_DO_FMT 0x0000
25 #define REG_DOFF_CTL 0x0004
26 #define REG_DOFF_ST 0x0008
27 #define REG_DI_FMT 0x000C
28 #define REG_DIFF_CTL 0x0010
29 #define REG_DIFF_ST 0x0014
30 #define REG_CKG1 0x0018
31 #define REG_CKG2 0x001C
32 #define REG_DIDT 0x0020
33 #define REG_DODT 0x0024
34 #define REG_MUTE_ST 0x0028
35 #define REG_OUT_DMAC 0x002C
36 #define REG_OUT_SEL 0x0030
37 #define REG_IN_DMAC 0x0038
40 #define MST_CLK_RST 0x0210
41 #define MST_SOFT_RST 0x0214
42 #define MST_FIFO_SZ 0x0218
44 /* core register (depend on FSI version) */
45 #define A_MST_CTLR 0x0180
46 #define B_MST_CTLR 0x01A0
47 #define CPU_INT_ST 0x01F4
48 #define CPU_IEMSK 0x01F8
49 #define CPU_IMSK 0x01FC
56 #define CR_BWS_24 (0x0 << 20) /* FSI2 */
57 #define CR_BWS_16 (0x1 << 20) /* FSI2 */
58 #define CR_BWS_20 (0x2 << 20) /* FSI2 */
60 #define CR_DTMD_PCM (0x0 << 8) /* FSI2 */
61 #define CR_DTMD_SPDIF_PCM (0x1 << 8) /* FSI2 */
62 #define CR_DTMD_SPDIF_STREAM (0x2 << 8) /* FSI2 */
64 #define CR_MONO (0x0 << 4)
65 #define CR_MONO_D (0x1 << 4)
66 #define CR_PCM (0x2 << 4)
67 #define CR_I2S (0x3 << 4)
68 #define CR_TDM (0x4 << 4)
69 #define CR_TDM_D (0x5 << 4)
73 #define IRQ_HALF 0x00100000
74 #define FIFO_CLR 0x00000001
77 #define ERR_OVER 0x00000010
78 #define ERR_UNDER 0x00000001
79 #define ST_ERR (ERR_OVER | ERR_UNDER)
82 #define ACKMD_MASK 0x00007000
83 #define BPFMD_MASK 0x00000700
88 #define BP (1 << 4) /* Fix the signal of Biphase output */
89 #define SE (1 << 0) /* Fix the master clock */
95 /* IO SHIFT / MACRO */
100 #define AB_IO(param, shift) (param << shift)
103 #define PBSR (1 << 12) /* Port B Software Reset */
104 #define PASR (1 << 8) /* Port A Software Reset */
105 #define IR (1 << 4) /* Interrupt Reset */
106 #define FSISR (1 << 0) /* Software Reset */
109 #define DMMD (1 << 4) /* SPDIF output timing 0: Biphase only */
110 /* 1: Biphase and serial */
113 #define FIFO_SZ_MASK 0x7
115 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
117 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
119 typedef int (*set_rate_func
)(struct device
*dev
, int is_porta
, int rate
, int enable
);
122 * FSI driver use below type name for variable
124 * xxx_num : number of data
125 * xxx_pos : position of data
126 * xxx_capa : capacity of data
130 * period/frame/sample image
134 * period pos period pos
136 * |<-------------------- period--------------------->|
137 * ==|============================================ ... =|==
139 * ||<----- frame ----->|<------ frame ----->| ... |
140 * |+--------------------+--------------------+- ... |
141 * ||[ sample ][ sample ]|[ sample ][ sample ]| ... |
142 * |+--------------------+--------------------+- ... |
143 * ==|============================================ ... =|==
163 struct snd_pcm_substream
*substream
;
165 int fifo_sample_capa
; /* sample capacity of FSI FIFO */
166 int buff_sample_capa
; /* sample capacity of ALSA buffer */
167 int buff_sample_pos
; /* sample position of ALSA buffer */
168 int period_samples
; /* sample number / 1 period */
169 int period_pos
; /* current period position */
177 struct fsi_master
*master
;
179 struct fsi_stream playback
;
180 struct fsi_stream capture
;
205 struct fsi_priv fsia
;
206 struct fsi_priv fsib
;
207 struct fsi_core
*core
;
208 struct sh_fsi_platform_info
*info
;
213 * basic read write function
216 static void __fsi_reg_write(u32 __iomem
*reg
, u32 data
)
218 /* valid data area is 24bit */
221 __raw_writel(data
, reg
);
224 static u32
__fsi_reg_read(u32 __iomem
*reg
)
226 return __raw_readl(reg
);
229 static void __fsi_reg_mask_set(u32 __iomem
*reg
, u32 mask
, u32 data
)
231 u32 val
= __fsi_reg_read(reg
);
236 __fsi_reg_write(reg
, val
);
239 #define fsi_reg_write(p, r, d)\
240 __fsi_reg_write((p->base + REG_##r), d)
242 #define fsi_reg_read(p, r)\
243 __fsi_reg_read((p->base + REG_##r))
245 #define fsi_reg_mask_set(p, r, m, d)\
246 __fsi_reg_mask_set((p->base + REG_##r), m, d)
248 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
249 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
250 static u32
_fsi_master_read(struct fsi_master
*master
, u32 reg
)
255 spin_lock_irqsave(&master
->lock
, flags
);
256 ret
= __fsi_reg_read(master
->base
+ reg
);
257 spin_unlock_irqrestore(&master
->lock
, flags
);
262 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
263 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
264 static void _fsi_master_mask_set(struct fsi_master
*master
,
265 u32 reg
, u32 mask
, u32 data
)
269 spin_lock_irqsave(&master
->lock
, flags
);
270 __fsi_reg_mask_set(master
->base
+ reg
, mask
, data
);
271 spin_unlock_irqrestore(&master
->lock
, flags
);
278 static struct fsi_master
*fsi_get_master(struct fsi_priv
*fsi
)
283 static int fsi_is_clk_master(struct fsi_priv
*fsi
)
285 return fsi
->clk_master
;
288 static int fsi_is_port_a(struct fsi_priv
*fsi
)
290 return fsi
->master
->base
== fsi
->base
;
293 static int fsi_is_spdif(struct fsi_priv
*fsi
)
298 static struct snd_soc_dai
*fsi_get_dai(struct snd_pcm_substream
*substream
)
300 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
305 static struct fsi_priv
*fsi_get_priv_frm_dai(struct snd_soc_dai
*dai
)
307 struct fsi_master
*master
= snd_soc_dai_get_drvdata(dai
);
310 return &master
->fsia
;
312 return &master
->fsib
;
315 static struct fsi_priv
*fsi_get_priv(struct snd_pcm_substream
*substream
)
317 return fsi_get_priv_frm_dai(fsi_get_dai(substream
));
320 static set_rate_func
fsi_get_info_set_rate(struct fsi_master
*master
)
325 return master
->info
->set_rate
;
328 static u32
fsi_get_info_flags(struct fsi_priv
*fsi
)
330 int is_porta
= fsi_is_port_a(fsi
);
331 struct fsi_master
*master
= fsi_get_master(fsi
);
336 return is_porta
? master
->info
->porta_flags
:
337 master
->info
->portb_flags
;
340 static inline int fsi_stream_is_play(int stream
)
342 return stream
== SNDRV_PCM_STREAM_PLAYBACK
;
345 static inline int fsi_is_play(struct snd_pcm_substream
*substream
)
347 return fsi_stream_is_play(substream
->stream
);
350 static inline struct fsi_stream
*fsi_get_stream(struct fsi_priv
*fsi
,
353 return is_play
? &fsi
->playback
: &fsi
->capture
;
356 static u32
fsi_get_port_shift(struct fsi_priv
*fsi
, int is_play
)
358 int is_porta
= fsi_is_port_a(fsi
);
362 shift
= is_play
? AO_SHIFT
: AI_SHIFT
;
364 shift
= is_play
? BO_SHIFT
: BI_SHIFT
;
369 static int fsi_frame2sample(struct fsi_priv
*fsi
, int frames
)
371 return frames
* fsi
->chan_num
;
374 static int fsi_sample2frame(struct fsi_priv
*fsi
, int samples
)
376 return samples
/ fsi
->chan_num
;
379 static int fsi_stream_is_working(struct fsi_priv
*fsi
,
382 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
383 struct fsi_master
*master
= fsi_get_master(fsi
);
387 spin_lock_irqsave(&master
->lock
, flags
);
388 ret
= !!io
->substream
;
389 spin_unlock_irqrestore(&master
->lock
, flags
);
394 static void fsi_stream_push(struct fsi_priv
*fsi
,
396 struct snd_pcm_substream
*substream
)
398 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
399 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
400 struct fsi_master
*master
= fsi_get_master(fsi
);
403 spin_lock_irqsave(&master
->lock
, flags
);
404 io
->substream
= substream
;
405 io
->buff_sample_capa
= fsi_frame2sample(fsi
, runtime
->buffer_size
);
406 io
->buff_sample_pos
= 0;
407 io
->period_samples
= fsi_frame2sample(fsi
, runtime
->period_size
);
409 io
->oerr_num
= -1; /* ignore 1st err */
410 io
->uerr_num
= -1; /* ignore 1st err */
411 spin_unlock_irqrestore(&master
->lock
, flags
);
414 static void fsi_stream_pop(struct fsi_priv
*fsi
, int is_play
)
416 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
417 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
418 struct fsi_master
*master
= fsi_get_master(fsi
);
421 spin_lock_irqsave(&master
->lock
, flags
);
423 if (io
->oerr_num
> 0)
424 dev_err(dai
->dev
, "over_run = %d\n", io
->oerr_num
);
426 if (io
->uerr_num
> 0)
427 dev_err(dai
->dev
, "under_run = %d\n", io
->uerr_num
);
429 io
->substream
= NULL
;
430 io
->buff_sample_capa
= 0;
431 io
->buff_sample_pos
= 0;
432 io
->period_samples
= 0;
436 spin_unlock_irqrestore(&master
->lock
, flags
);
439 static int fsi_get_current_fifo_samples(struct fsi_priv
*fsi
, int is_play
)
445 fsi_reg_read(fsi
, DOFF_ST
) :
446 fsi_reg_read(fsi
, DIFF_ST
);
448 frames
= 0x1ff & (status
>> 8);
450 return fsi_frame2sample(fsi
, frames
);
453 static void fsi_count_fifo_err(struct fsi_priv
*fsi
)
455 u32 ostatus
= fsi_reg_read(fsi
, DOFF_ST
);
456 u32 istatus
= fsi_reg_read(fsi
, DIFF_ST
);
458 if (ostatus
& ERR_OVER
)
459 fsi
->playback
.oerr_num
++;
461 if (ostatus
& ERR_UNDER
)
462 fsi
->playback
.uerr_num
++;
464 if (istatus
& ERR_OVER
)
465 fsi
->capture
.oerr_num
++;
467 if (istatus
& ERR_UNDER
)
468 fsi
->capture
.uerr_num
++;
470 fsi_reg_write(fsi
, DOFF_ST
, 0);
471 fsi_reg_write(fsi
, DIFF_ST
, 0);
478 static u8
*fsi_dma_get_area(struct fsi_priv
*fsi
, int stream
)
480 int is_play
= fsi_stream_is_play(stream
);
481 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
482 struct snd_pcm_runtime
*runtime
= io
->substream
->runtime
;
484 return runtime
->dma_area
+
485 samples_to_bytes(runtime
, io
->buff_sample_pos
);
488 static void fsi_dma_soft_push16(struct fsi_priv
*fsi
, int num
)
493 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
495 for (i
= 0; i
< num
; i
++)
496 fsi_reg_write(fsi
, DODT
, ((u32
)*(start
+ i
) << 8));
499 static void fsi_dma_soft_pop16(struct fsi_priv
*fsi
, int num
)
504 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
507 for (i
= 0; i
< num
; i
++)
508 *(start
+ i
) = (u16
)(fsi_reg_read(fsi
, DIDT
) >> 8);
511 static void fsi_dma_soft_push32(struct fsi_priv
*fsi
, int num
)
516 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
519 for (i
= 0; i
< num
; i
++)
520 fsi_reg_write(fsi
, DODT
, *(start
+ i
));
523 static void fsi_dma_soft_pop32(struct fsi_priv
*fsi
, int num
)
528 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
530 for (i
= 0; i
< num
; i
++)
531 *(start
+ i
) = fsi_reg_read(fsi
, DIDT
);
538 static void fsi_irq_enable(struct fsi_priv
*fsi
, int is_play
)
540 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
541 struct fsi_master
*master
= fsi_get_master(fsi
);
543 fsi_core_mask_set(master
, imsk
, data
, data
);
544 fsi_core_mask_set(master
, iemsk
, data
, data
);
547 static void fsi_irq_disable(struct fsi_priv
*fsi
, int is_play
)
549 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
550 struct fsi_master
*master
= fsi_get_master(fsi
);
552 fsi_core_mask_set(master
, imsk
, data
, 0);
553 fsi_core_mask_set(master
, iemsk
, data
, 0);
556 static u32
fsi_irq_get_status(struct fsi_master
*master
)
558 return fsi_core_read(master
, int_st
);
561 static void fsi_irq_clear_status(struct fsi_priv
*fsi
)
564 struct fsi_master
*master
= fsi_get_master(fsi
);
566 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 0));
567 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 1));
569 /* clear interrupt factor */
570 fsi_core_mask_set(master
, int_st
, data
, 0);
574 * SPDIF master clock function
576 * These functions are used later FSI2
578 static void fsi_spdif_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
580 struct fsi_master
*master
= fsi_get_master(fsi
);
583 if (master
->core
->ver
< 2) {
584 pr_err("fsi: register access err (%s)\n", __func__
);
589 val
= enable
? mask
: 0;
592 fsi_core_mask_set(master
, a_mclk
, mask
, val
) :
593 fsi_core_mask_set(master
, b_mclk
, mask
, val
);
599 static int fsi_set_master_clk(struct device
*dev
, struct fsi_priv
*fsi
,
600 long rate
, int enable
)
602 struct fsi_master
*master
= fsi_get_master(fsi
);
603 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
604 int fsi_ver
= master
->core
->ver
;
607 ret
= set_rate(dev
, fsi_is_port_a(fsi
), rate
, enable
);
608 if (ret
< 0) /* error */
617 switch (ret
& SH_FSI_ACKMD_MASK
) {
620 case SH_FSI_ACKMD_512
:
623 case SH_FSI_ACKMD_256
:
626 case SH_FSI_ACKMD_128
:
629 case SH_FSI_ACKMD_64
:
632 case SH_FSI_ACKMD_32
:
634 dev_err(dev
, "unsupported ACKMD\n");
640 switch (ret
& SH_FSI_BPFMD_MASK
) {
643 case SH_FSI_BPFMD_32
:
646 case SH_FSI_BPFMD_64
:
649 case SH_FSI_BPFMD_128
:
652 case SH_FSI_BPFMD_256
:
655 case SH_FSI_BPFMD_512
:
658 case SH_FSI_BPFMD_16
:
660 dev_err(dev
, "unsupported ACKMD\n");
666 fsi_reg_mask_set(fsi
, CKG1
, (ACKMD_MASK
| BPFMD_MASK
) , data
);
674 #define fsi_port_start(f, i) __fsi_port_clk_ctrl(f, i, 1)
675 #define fsi_port_stop(f, i) __fsi_port_clk_ctrl(f, i, 0)
676 static void __fsi_port_clk_ctrl(struct fsi_priv
*fsi
, int is_play
, int enable
)
678 struct fsi_master
*master
= fsi_get_master(fsi
);
679 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
682 fsi_irq_enable(fsi
, is_play
);
684 fsi_irq_disable(fsi
, is_play
);
686 if (fsi_is_clk_master(fsi
))
687 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
693 static void fsi_fifo_init(struct fsi_priv
*fsi
,
697 struct fsi_master
*master
= fsi_get_master(fsi
);
698 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
702 /* get on-chip RAM capacity */
703 shift
= fsi_master_read(master
, FIFO_SZ
);
704 shift
>>= fsi_get_port_shift(fsi
, is_play
);
705 shift
&= FIFO_SZ_MASK
;
706 frame_capa
= 256 << shift
;
707 dev_dbg(dev
, "fifo = %d words\n", frame_capa
);
710 * The maximum number of sample data varies depending
711 * on the number of channels selected for the format.
713 * FIFOs are used in 4-channel units in 3-channel mode
714 * and in 8-channel units in 5- to 7-channel mode
715 * meaning that more FIFOs than the required size of DPRAM
718 * ex) if 256 words of DP-RAM is connected
719 * 1 channel: 256 (256 x 1 = 256)
720 * 2 channels: 128 (128 x 2 = 256)
721 * 3 channels: 64 ( 64 x 3 = 192)
722 * 4 channels: 64 ( 64 x 4 = 256)
723 * 5 channels: 32 ( 32 x 5 = 160)
724 * 6 channels: 32 ( 32 x 6 = 192)
725 * 7 channels: 32 ( 32 x 7 = 224)
726 * 8 channels: 32 ( 32 x 8 = 256)
728 for (i
= 1; i
< fsi
->chan_num
; i
<<= 1)
730 dev_dbg(dev
, "%d channel %d store\n",
731 fsi
->chan_num
, frame_capa
);
733 io
->fifo_sample_capa
= fsi_frame2sample(fsi
, frame_capa
);
736 * set interrupt generation factor
740 fsi_reg_write(fsi
, DOFF_CTL
, IRQ_HALF
);
741 fsi_reg_mask_set(fsi
, DOFF_CTL
, FIFO_CLR
, FIFO_CLR
);
743 fsi_reg_write(fsi
, DIFF_CTL
, IRQ_HALF
);
744 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
748 static int fsi_fifo_data_ctrl(struct fsi_priv
*fsi
, int stream
)
750 struct snd_pcm_runtime
*runtime
;
751 struct snd_pcm_substream
*substream
= NULL
;
752 int is_play
= fsi_stream_is_play(stream
);
753 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
759 void (*fn
)(struct fsi_priv
*fsi
, int size
);
763 !io
->substream
->runtime
)
767 substream
= io
->substream
;
768 runtime
= substream
->runtime
;
770 /* FSI FIFO has limit.
771 * So, this driver can not send periods data at a time
773 if (io
->buff_sample_pos
>=
774 io
->period_samples
* (io
->period_pos
+ 1)) {
777 io
->period_pos
= (io
->period_pos
+ 1) % runtime
->periods
;
779 if (0 == io
->period_pos
)
780 io
->buff_sample_pos
= 0;
783 /* get 1 sample data width */
784 sample_width
= samples_to_bytes(runtime
, 1);
786 /* get number of residue samples */
787 sample_residues
= io
->buff_sample_capa
- io
->buff_sample_pos
;
793 * samples_max : number of FSI fifo free samples space
794 * samples : number of ALSA residue samples
796 samples_max
= io
->fifo_sample_capa
;
797 samples_max
-= fsi_get_current_fifo_samples(fsi
, is_play
);
799 samples
= sample_residues
;
801 switch (sample_width
) {
803 fn
= fsi_dma_soft_push16
;
806 fn
= fsi_dma_soft_push32
;
815 * samples_max : number of ALSA free samples space
816 * samples : number of samples in FSI fifo
818 samples_max
= sample_residues
;
819 samples
= fsi_get_current_fifo_samples(fsi
, is_play
);
821 switch (sample_width
) {
823 fn
= fsi_dma_soft_pop16
;
826 fn
= fsi_dma_soft_pop32
;
833 samples
= min(samples
, samples_max
);
837 /* update buff_sample_pos */
838 io
->buff_sample_pos
+= samples
;
841 snd_pcm_period_elapsed(substream
);
846 static int fsi_data_pop(struct fsi_priv
*fsi
)
848 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
851 static int fsi_data_push(struct fsi_priv
*fsi
)
853 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
856 static irqreturn_t
fsi_interrupt(int irq
, void *data
)
858 struct fsi_master
*master
= data
;
859 u32 int_st
= fsi_irq_get_status(master
);
861 /* clear irq status */
862 fsi_master_mask_set(master
, SOFT_RST
, IR
, 0);
863 fsi_master_mask_set(master
, SOFT_RST
, IR
, IR
);
865 if (int_st
& AB_IO(1, AO_SHIFT
))
866 fsi_data_push(&master
->fsia
);
867 if (int_st
& AB_IO(1, BO_SHIFT
))
868 fsi_data_push(&master
->fsib
);
869 if (int_st
& AB_IO(1, AI_SHIFT
))
870 fsi_data_pop(&master
->fsia
);
871 if (int_st
& AB_IO(1, BI_SHIFT
))
872 fsi_data_pop(&master
->fsib
);
874 fsi_count_fifo_err(&master
->fsia
);
875 fsi_count_fifo_err(&master
->fsib
);
877 fsi_irq_clear_status(&master
->fsia
);
878 fsi_irq_clear_status(&master
->fsib
);
887 static int fsi_hw_startup(struct fsi_priv
*fsi
,
891 struct fsi_master
*master
= fsi_get_master(fsi
);
892 int fsi_ver
= master
->core
->ver
;
893 u32 flags
= fsi_get_info_flags(fsi
);
897 if (fsi_is_clk_master(fsi
))
900 fsi_reg_mask_set(fsi
, CKG1
, (DIMD
| DOMD
), data
);
902 /* clock inversion (CKG2) */
904 if (SH_FSI_LRM_INV
& flags
)
906 if (SH_FSI_BRM_INV
& flags
)
908 if (SH_FSI_LRS_INV
& flags
)
910 if (SH_FSI_BRS_INV
& flags
)
913 fsi_reg_write(fsi
, CKG2
, data
);
916 fsi_reg_write(fsi
, DO_FMT
, fsi
->do_fmt
);
917 fsi_reg_write(fsi
, DI_FMT
, fsi
->di_fmt
);
920 if (fsi_is_spdif(fsi
)) {
921 fsi_spdif_clk_ctrl(fsi
, 1);
922 fsi_reg_mask_set(fsi
, OUT_SEL
, DMMD
, DMMD
);
928 * FSI driver assumed that data package is in-back.
929 * FSI2 chip can select it.
932 fsi_reg_write(fsi
, OUT_DMAC
, (1 << 4));
933 fsi_reg_write(fsi
, IN_DMAC
, (1 << 4));
937 fsi_irq_disable(fsi
, is_play
);
938 fsi_irq_clear_status(fsi
);
941 fsi_fifo_init(fsi
, is_play
, dev
);
946 static void fsi_hw_shutdown(struct fsi_priv
*fsi
,
950 if (fsi_is_clk_master(fsi
))
951 fsi_set_master_clk(dev
, fsi
, fsi
->rate
, 0);
954 static int fsi_dai_startup(struct snd_pcm_substream
*substream
,
955 struct snd_soc_dai
*dai
)
957 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
958 int is_play
= fsi_is_play(substream
);
960 return fsi_hw_startup(fsi
, is_play
, dai
->dev
);
963 static void fsi_dai_shutdown(struct snd_pcm_substream
*substream
,
964 struct snd_soc_dai
*dai
)
966 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
967 int is_play
= fsi_is_play(substream
);
969 fsi_hw_shutdown(fsi
, is_play
, dai
->dev
);
973 static int fsi_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
974 struct snd_soc_dai
*dai
)
976 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
977 int is_play
= fsi_is_play(substream
);
981 case SNDRV_PCM_TRIGGER_START
:
982 fsi_stream_push(fsi
, is_play
, substream
);
983 ret
= is_play
? fsi_data_push(fsi
) : fsi_data_pop(fsi
);
984 fsi_port_start(fsi
, is_play
);
986 case SNDRV_PCM_TRIGGER_STOP
:
987 fsi_port_stop(fsi
, is_play
);
988 fsi_stream_pop(fsi
, is_play
);
995 static int fsi_set_fmt_dai(struct fsi_priv
*fsi
, unsigned int fmt
)
999 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
1000 case SND_SOC_DAIFMT_I2S
:
1004 case SND_SOC_DAIFMT_LEFT_J
:
1018 static int fsi_set_fmt_spdif(struct fsi_priv
*fsi
)
1020 struct fsi_master
*master
= fsi_get_master(fsi
);
1023 if (master
->core
->ver
< 2)
1026 data
= CR_BWS_16
| CR_DTMD_SPDIF_PCM
| CR_PCM
;
1036 static int fsi_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
1038 struct fsi_priv
*fsi
= fsi_get_priv_frm_dai(dai
);
1039 struct fsi_master
*master
= fsi_get_master(fsi
);
1040 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
1041 u32 flags
= fsi_get_info_flags(fsi
);
1044 /* set master/slave audio interface */
1045 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
1046 case SND_SOC_DAIFMT_CBM_CFM
:
1047 fsi
->clk_master
= 1;
1049 case SND_SOC_DAIFMT_CBS_CFS
:
1055 if (fsi_is_clk_master(fsi
) && !set_rate
) {
1056 dev_err(dai
->dev
, "platform doesn't have set_rate\n");
1061 switch (flags
& SH_FSI_FMT_MASK
) {
1062 case SH_FSI_FMT_DAI
:
1063 ret
= fsi_set_fmt_dai(fsi
, fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
1065 case SH_FSI_FMT_SPDIF
:
1066 ret
= fsi_set_fmt_spdif(fsi
);
1075 static int fsi_dai_hw_params(struct snd_pcm_substream
*substream
,
1076 struct snd_pcm_hw_params
*params
,
1077 struct snd_soc_dai
*dai
)
1079 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1080 long rate
= params_rate(params
);
1083 if (!fsi_is_clk_master(fsi
))
1086 ret
= fsi_set_master_clk(dai
->dev
, fsi
, rate
, 1);
1095 static const struct snd_soc_dai_ops fsi_dai_ops
= {
1096 .startup
= fsi_dai_startup
,
1097 .shutdown
= fsi_dai_shutdown
,
1098 .trigger
= fsi_dai_trigger
,
1099 .set_fmt
= fsi_dai_set_fmt
,
1100 .hw_params
= fsi_dai_hw_params
,
1107 static struct snd_pcm_hardware fsi_pcm_hardware
= {
1108 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
1109 SNDRV_PCM_INFO_MMAP
|
1110 SNDRV_PCM_INFO_MMAP_VALID
|
1111 SNDRV_PCM_INFO_PAUSE
,
1112 .formats
= FSI_FMTS
,
1118 .buffer_bytes_max
= 64 * 1024,
1119 .period_bytes_min
= 32,
1120 .period_bytes_max
= 8192,
1126 static int fsi_pcm_open(struct snd_pcm_substream
*substream
)
1128 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1131 snd_soc_set_runtime_hwparams(substream
, &fsi_pcm_hardware
);
1133 ret
= snd_pcm_hw_constraint_integer(runtime
,
1134 SNDRV_PCM_HW_PARAM_PERIODS
);
1139 static int fsi_hw_params(struct snd_pcm_substream
*substream
,
1140 struct snd_pcm_hw_params
*hw_params
)
1142 return snd_pcm_lib_malloc_pages(substream
,
1143 params_buffer_bytes(hw_params
));
1146 static int fsi_hw_free(struct snd_pcm_substream
*substream
)
1148 return snd_pcm_lib_free_pages(substream
);
1151 static snd_pcm_uframes_t
fsi_pointer(struct snd_pcm_substream
*substream
)
1153 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1154 struct fsi_stream
*io
= fsi_get_stream(fsi
, fsi_is_play(substream
));
1156 return fsi_sample2frame(fsi
, io
->buff_sample_pos
);
1159 static struct snd_pcm_ops fsi_pcm_ops
= {
1160 .open
= fsi_pcm_open
,
1161 .ioctl
= snd_pcm_lib_ioctl
,
1162 .hw_params
= fsi_hw_params
,
1163 .hw_free
= fsi_hw_free
,
1164 .pointer
= fsi_pointer
,
1171 #define PREALLOC_BUFFER (32 * 1024)
1172 #define PREALLOC_BUFFER_MAX (32 * 1024)
1174 static void fsi_pcm_free(struct snd_pcm
*pcm
)
1176 snd_pcm_lib_preallocate_free_for_all(pcm
);
1179 static int fsi_pcm_new(struct snd_soc_pcm_runtime
*rtd
)
1181 struct snd_pcm
*pcm
= rtd
->pcm
;
1184 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1185 * in MMAP mode (i.e. aplay -M)
1187 return snd_pcm_lib_preallocate_pages_for_all(
1189 SNDRV_DMA_TYPE_CONTINUOUS
,
1190 snd_dma_continuous_data(GFP_KERNEL
),
1191 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1198 static struct snd_soc_dai_driver fsi_soc_dai
[] = {
1203 .formats
= FSI_FMTS
,
1209 .formats
= FSI_FMTS
,
1213 .ops
= &fsi_dai_ops
,
1219 .formats
= FSI_FMTS
,
1225 .formats
= FSI_FMTS
,
1229 .ops
= &fsi_dai_ops
,
1233 static struct snd_soc_platform_driver fsi_soc_platform
= {
1234 .ops
= &fsi_pcm_ops
,
1235 .pcm_new
= fsi_pcm_new
,
1236 .pcm_free
= fsi_pcm_free
,
1243 static int fsi_probe(struct platform_device
*pdev
)
1245 struct fsi_master
*master
;
1246 const struct platform_device_id
*id_entry
;
1247 struct resource
*res
;
1251 id_entry
= pdev
->id_entry
;
1253 dev_err(&pdev
->dev
, "unknown fsi device\n");
1257 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1258 irq
= platform_get_irq(pdev
, 0);
1259 if (!res
|| (int)irq
<= 0) {
1260 dev_err(&pdev
->dev
, "Not enough FSI platform resources.\n");
1265 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
1267 dev_err(&pdev
->dev
, "Could not allocate master\n");
1272 master
->base
= ioremap_nocache(res
->start
, resource_size(res
));
1273 if (!master
->base
) {
1275 dev_err(&pdev
->dev
, "Unable to ioremap FSI registers.\n");
1279 /* master setting */
1281 master
->info
= pdev
->dev
.platform_data
;
1282 master
->core
= (struct fsi_core
*)id_entry
->driver_data
;
1283 spin_lock_init(&master
->lock
);
1286 master
->fsia
.base
= master
->base
;
1287 master
->fsia
.master
= master
;
1290 master
->fsib
.base
= master
->base
+ 0x40;
1291 master
->fsib
.master
= master
;
1293 pm_runtime_enable(&pdev
->dev
);
1294 dev_set_drvdata(&pdev
->dev
, master
);
1296 ret
= request_irq(irq
, &fsi_interrupt
, 0,
1297 id_entry
->name
, master
);
1299 dev_err(&pdev
->dev
, "irq request err\n");
1303 ret
= snd_soc_register_platform(&pdev
->dev
, &fsi_soc_platform
);
1305 dev_err(&pdev
->dev
, "cannot snd soc register\n");
1309 ret
= snd_soc_register_dais(&pdev
->dev
, fsi_soc_dai
,
1310 ARRAY_SIZE(fsi_soc_dai
));
1312 dev_err(&pdev
->dev
, "cannot snd dai register\n");
1319 snd_soc_unregister_platform(&pdev
->dev
);
1321 free_irq(irq
, master
);
1323 iounmap(master
->base
);
1324 pm_runtime_disable(&pdev
->dev
);
1332 static int fsi_remove(struct platform_device
*pdev
)
1334 struct fsi_master
*master
;
1336 master
= dev_get_drvdata(&pdev
->dev
);
1338 free_irq(master
->irq
, master
);
1339 pm_runtime_disable(&pdev
->dev
);
1341 snd_soc_unregister_dais(&pdev
->dev
, ARRAY_SIZE(fsi_soc_dai
));
1342 snd_soc_unregister_platform(&pdev
->dev
);
1344 iounmap(master
->base
);
1350 static void __fsi_suspend(struct fsi_priv
*fsi
,
1354 if (!fsi_stream_is_working(fsi
, is_play
))
1357 fsi_port_stop(fsi
, is_play
);
1358 fsi_hw_shutdown(fsi
, is_play
, dev
);
1361 static void __fsi_resume(struct fsi_priv
*fsi
,
1365 if (!fsi_stream_is_working(fsi
, is_play
))
1368 fsi_hw_startup(fsi
, is_play
, dev
);
1370 if (fsi_is_clk_master(fsi
) && fsi
->rate
)
1371 fsi_set_master_clk(dev
, fsi
, fsi
->rate
, 1);
1373 fsi_port_start(fsi
, is_play
);
1377 static int fsi_suspend(struct device
*dev
)
1379 struct fsi_master
*master
= dev_get_drvdata(dev
);
1380 struct fsi_priv
*fsia
= &master
->fsia
;
1381 struct fsi_priv
*fsib
= &master
->fsib
;
1383 __fsi_suspend(fsia
, 1, dev
);
1384 __fsi_suspend(fsia
, 0, dev
);
1386 __fsi_suspend(fsib
, 1, dev
);
1387 __fsi_suspend(fsib
, 0, dev
);
1392 static int fsi_resume(struct device
*dev
)
1394 struct fsi_master
*master
= dev_get_drvdata(dev
);
1395 struct fsi_priv
*fsia
= &master
->fsia
;
1396 struct fsi_priv
*fsib
= &master
->fsib
;
1398 __fsi_resume(fsia
, 1, dev
);
1399 __fsi_resume(fsia
, 0, dev
);
1401 __fsi_resume(fsib
, 1, dev
);
1402 __fsi_resume(fsib
, 0, dev
);
1407 static int fsi_runtime_nop(struct device
*dev
)
1409 /* Runtime PM callback shared between ->runtime_suspend()
1410 * and ->runtime_resume(). Simply returns success.
1412 * This driver re-initializes all registers after
1413 * pm_runtime_get_sync() anyway so there is no need
1414 * to save and restore registers here.
1419 static struct dev_pm_ops fsi_pm_ops
= {
1420 .suspend
= fsi_suspend
,
1421 .resume
= fsi_resume
,
1422 .runtime_suspend
= fsi_runtime_nop
,
1423 .runtime_resume
= fsi_runtime_nop
,
1426 static struct fsi_core fsi1_core
= {
1435 static struct fsi_core fsi2_core
= {
1439 .int_st
= CPU_INT_ST
,
1442 .a_mclk
= A_MST_CTLR
,
1443 .b_mclk
= B_MST_CTLR
,
1446 static struct platform_device_id fsi_id_table
[] = {
1447 { "sh_fsi", (kernel_ulong_t
)&fsi1_core
},
1448 { "sh_fsi2", (kernel_ulong_t
)&fsi2_core
},
1451 MODULE_DEVICE_TABLE(platform
, fsi_id_table
);
1453 static struct platform_driver fsi_driver
= {
1455 .name
= "fsi-pcm-audio",
1459 .remove
= fsi_remove
,
1460 .id_table
= fsi_id_table
,
1463 module_platform_driver(fsi_driver
);
1465 MODULE_LICENSE("GPL");
1466 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1467 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1468 MODULE_ALIAS("platform:fsi-pcm-audio");