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/init.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/delay.h>
19 #include <linux/list.h>
20 #include <linux/clk.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/initval.h>
25 #include <sound/soc.h>
26 #include <sound/pcm_params.h>
27 #include <sound/sh_fsi.h>
28 #include <asm/atomic.h>
30 #include <asm/dma-sh.h>
33 #define DOFF_CTL 0x0004
34 #define DOFF_ST 0x0008
36 #define DIFF_CTL 0x0010
37 #define DIFF_ST 0x0014
42 #define MUTE_ST 0x0028
43 #define REG_END MUTE_ST
49 #define CLK_RST 0x0210
50 #define SOFT_RST 0x0214
51 #define MREG_START INT_ST
52 #define MREG_END SOFT_RST
56 #define CR_FMT(param) ((param) << 4)
58 # define CR_MONO_D 0x1
66 #define IRQ_HALF 0x00100000
67 #define FIFO_CLR 0x00000001
70 #define ERR_OVER 0x00000010
71 #define ERR_UNDER 0x00000001
74 #define B_CLK 0x00000010
75 #define A_CLK 0x00000001
78 #define INT_B_IN (1 << 12)
79 #define INT_B_OUT (1 << 8)
80 #define INT_A_IN (1 << 4)
81 #define INT_A_OUT (1 << 0)
83 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
85 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
87 /************************************************************************
93 ************************************************************************/
96 struct snd_pcm_substream
*substream
;
112 struct fsi_priv fsia
;
113 struct fsi_priv fsib
;
114 struct sh_fsi_platform_info
*info
;
117 static struct fsi_master
*master
;
119 /************************************************************************
122 basic read write function
125 ************************************************************************/
126 static int __fsi_reg_write(u32 reg
, u32 data
)
128 /* valid data area is 24bit */
131 return ctrl_outl(data
, reg
);
134 static u32
__fsi_reg_read(u32 reg
)
136 return ctrl_inl(reg
);
139 static int __fsi_reg_mask_set(u32 reg
, u32 mask
, u32 data
)
141 u32 val
= __fsi_reg_read(reg
);
146 return __fsi_reg_write(reg
, val
);
149 static int fsi_reg_write(struct fsi_priv
*fsi
, u32 reg
, u32 data
)
154 return __fsi_reg_write((u32
)(fsi
->base
+ reg
), data
);
157 static u32
fsi_reg_read(struct fsi_priv
*fsi
, u32 reg
)
162 return __fsi_reg_read((u32
)(fsi
->base
+ reg
));
165 static int fsi_reg_mask_set(struct fsi_priv
*fsi
, u32 reg
, u32 mask
, u32 data
)
170 return __fsi_reg_mask_set((u32
)(fsi
->base
+ reg
), mask
, data
);
173 static int fsi_master_write(u32 reg
, u32 data
)
175 if ((reg
< MREG_START
) ||
179 return __fsi_reg_write((u32
)(master
->base
+ reg
), data
);
182 static u32
fsi_master_read(u32 reg
)
184 if ((reg
< MREG_START
) ||
188 return __fsi_reg_read((u32
)(master
->base
+ reg
));
191 static int fsi_master_mask_set(u32 reg
, u32 mask
, u32 data
)
193 if ((reg
< MREG_START
) ||
197 return __fsi_reg_mask_set((u32
)(master
->base
+ reg
), mask
, data
);
200 /************************************************************************
206 ************************************************************************/
207 static struct fsi_priv
*fsi_get(struct snd_pcm_substream
*substream
)
209 struct snd_soc_pcm_runtime
*rtd
;
210 struct fsi_priv
*fsi
= NULL
;
212 if (!substream
|| !master
)
215 rtd
= substream
->private_data
;
216 switch (rtd
->dai
->cpu_dai
->id
) {
228 static int fsi_is_port_a(struct fsi_priv
*fsi
)
235 if (fsi
== &master
->fsia
)
241 static u32
fsi_get_info_flags(struct fsi_priv
*fsi
)
243 int is_porta
= fsi_is_port_a(fsi
);
245 return is_porta
? master
->info
->porta_flags
:
246 master
->info
->portb_flags
;
249 static int fsi_is_master_mode(struct fsi_priv
*fsi
, int is_play
)
252 u32 flags
= fsi_get_info_flags(fsi
);
254 mode
= is_play
? SH_FSI_OUT_SLAVE_MODE
: SH_FSI_IN_SLAVE_MODE
;
261 return (mode
& flags
) != mode
;
264 static u32
fsi_port_ab_io_bit(struct fsi_priv
*fsi
, int is_play
)
266 int is_porta
= fsi_is_port_a(fsi
);
270 data
= is_play
? (1 << 0) : (1 << 4);
272 data
= is_play
? (1 << 8) : (1 << 12);
277 static void fsi_stream_push(struct fsi_priv
*fsi
,
278 struct snd_pcm_substream
*substream
,
282 fsi
->substream
= substream
;
283 fsi
->buffer_len
= buffer_len
;
284 fsi
->period_len
= period_len
;
285 fsi
->byte_offset
= 0;
289 static void fsi_stream_pop(struct fsi_priv
*fsi
)
291 fsi
->substream
= NULL
;
294 fsi
->byte_offset
= 0;
298 static int fsi_get_fifo_residue(struct fsi_priv
*fsi
, int is_play
)
301 u32 reg
= is_play
? DOFF_ST
: DIFF_ST
;
304 status
= fsi_reg_read(fsi
, reg
);
305 residue
= 0x1ff & (status
>> 8);
306 residue
*= fsi
->chan
;
311 static int fsi_get_residue(struct fsi_priv
*fsi
, int is_play
)
315 struct snd_pcm_runtime
*runtime
;
317 runtime
= fsi
->substream
->runtime
;
319 /* get 1 channel data width */
320 width
= frames_to_bytes(runtime
, 1) / fsi
->chan
;
323 residue
= fsi_get_fifo_residue(fsi
, is_play
);
325 residue
= get_dma_residue(fsi
->dma_chan
);
330 /************************************************************************
336 ************************************************************************/
340 static int fsi_get_dma_chan(void)
342 if (0 != request_dma(PORTA_DMA
, "fsia"))
345 if (0 != request_dma(PORTB_DMA
, "fsib")) {
350 master
->fsia
.dma_chan
= PORTA_DMA
;
351 master
->fsib
.dma_chan
= PORTB_DMA
;
356 static void fsi_free_dma_chan(void)
358 dma_wait_for_completion(PORTA_DMA
);
359 dma_wait_for_completion(PORTB_DMA
);
363 master
->fsia
.dma_chan
= -1;
364 master
->fsib
.dma_chan
= -1;
367 /************************************************************************
373 ************************************************************************/
374 static void fsi_irq_enable(struct fsi_priv
*fsi
, int is_play
)
376 u32 data
= fsi_port_ab_io_bit(fsi
, is_play
);
378 fsi_master_mask_set(IMSK
, data
, data
);
379 fsi_master_mask_set(IEMSK
, data
, data
);
382 static void fsi_irq_disable(struct fsi_priv
*fsi
, int is_play
)
384 u32 data
= fsi_port_ab_io_bit(fsi
, is_play
);
386 fsi_master_mask_set(IMSK
, data
, 0);
387 fsi_master_mask_set(IEMSK
, data
, 0);
390 static void fsi_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
392 u32 val
= fsi_is_port_a(fsi
) ? (1 << 0) : (1 << 4);
395 fsi_master_mask_set(CLK_RST
, val
, val
);
397 fsi_master_mask_set(CLK_RST
, val
, 0);
400 static void fsi_irq_init(struct fsi_priv
*fsi
, int is_play
)
405 data
= fsi_port_ab_io_bit(fsi
, is_play
);
406 ctrl
= is_play
? DOFF_CTL
: DIFF_CTL
;
409 fsi_irq_disable(fsi
, is_play
);
411 /* set interrupt generation factor */
412 fsi_reg_write(fsi
, ctrl
, IRQ_HALF
);
415 fsi_reg_mask_set(fsi
, ctrl
, FIFO_CLR
, FIFO_CLR
);
417 /* clear interrupt factor */
418 fsi_master_mask_set(INT_ST
, data
, 0);
421 static void fsi_soft_all_reset(void)
423 u32 status
= fsi_master_read(SOFT_RST
);
426 status
&= 0x000000ff;
427 fsi_master_write(SOFT_RST
, status
);
431 status
&= 0x000000f0;
432 fsi_master_write(SOFT_RST
, status
);
433 status
|= 0x00000001;
434 fsi_master_write(SOFT_RST
, status
);
438 static void fsi_16data_push(struct fsi_priv
*fsi
,
439 struct snd_pcm_runtime
*runtime
,
446 /* get dma start position for FSI */
447 dma_start
= (u16
*)runtime
->dma_area
;
448 dma_start
+= fsi
->byte_offset
/ 2;
452 * FSI can not use DMA when 16bpp
454 for (i
= 0; i
< send
; i
++) {
455 snd
= (u32
)dma_start
[i
];
456 fsi_reg_write(fsi
, DODT
, snd
<< 8);
460 static void fsi_32data_push(struct fsi_priv
*fsi
,
461 struct snd_pcm_runtime
*runtime
,
466 /* get dma start position for FSI */
467 dma_start
= (u32
*)runtime
->dma_area
;
468 dma_start
+= fsi
->byte_offset
/ 4;
470 dma_wait_for_completion(fsi
->dma_chan
);
471 dma_configure_channel(fsi
->dma_chan
, (SM_INC
|0x400|TS_32
|TM_BUR
));
472 dma_write(fsi
->dma_chan
, (u32
)dma_start
,
473 (u32
)(fsi
->base
+ DODT
), send
* 4);
476 /* playback interrupt */
477 static int fsi_data_push(struct fsi_priv
*fsi
)
479 struct snd_pcm_runtime
*runtime
;
480 struct snd_pcm_substream
*substream
= NULL
;
487 !fsi
->substream
->runtime
)
490 runtime
= fsi
->substream
->runtime
;
492 /* FSI FIFO has limit.
493 * So, this driver can not send periods data at a time
495 if (fsi
->byte_offset
>=
496 fsi
->period_len
* (fsi
->periods
+ 1)) {
498 substream
= fsi
->substream
;
499 fsi
->periods
= (fsi
->periods
+ 1) % runtime
->periods
;
501 if (0 == fsi
->periods
)
502 fsi
->byte_offset
= 0;
505 /* get 1 channel data width */
506 width
= frames_to_bytes(runtime
, 1) / fsi
->chan
;
508 /* get send size for alsa */
509 send
= (fsi
->buffer_len
- fsi
->byte_offset
) / width
;
511 /* get FIFO free size */
512 fifo_free
= (fsi
->fifo_max
* fsi
->chan
) - fsi_get_fifo_residue(fsi
, 1);
515 if (fifo_free
< send
)
519 fsi_16data_push(fsi
, runtime
, send
);
521 fsi_32data_push(fsi
, runtime
, send
);
525 fsi
->byte_offset
+= send
* width
;
527 fsi_irq_enable(fsi
, 1);
530 snd_pcm_period_elapsed(substream
);
535 static irqreturn_t
fsi_interrupt(int irq
, void *data
)
537 u32 status
= fsi_master_read(SOFT_RST
) & ~0x00000010;
538 u32 int_st
= fsi_master_read(INT_ST
);
540 /* clear irq status */
541 fsi_master_write(SOFT_RST
, status
);
542 fsi_master_write(SOFT_RST
, status
| 0x00000010);
544 if (int_st
& INT_A_OUT
)
545 fsi_data_push(&master
->fsia
);
546 if (int_st
& INT_B_OUT
)
547 fsi_data_push(&master
->fsib
);
549 fsi_master_write(INT_ST
, 0x0000000);
554 /************************************************************************
560 ************************************************************************/
561 static int fsi_dai_startup(struct snd_pcm_substream
*substream
,
562 struct snd_soc_dai
*dai
)
564 struct fsi_priv
*fsi
= fsi_get(substream
);
566 u32 flags
= fsi_get_info_flags(fsi
);
570 int is_play
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
574 clk_enable(master
->clk
);
577 data
= is_play
? (1 << 0) : (1 << 4);
578 is_master
= fsi_is_master_mode(fsi
, is_play
);
580 fsi_reg_mask_set(fsi
, CKG1
, data
, data
);
582 fsi_reg_mask_set(fsi
, CKG1
, data
, 0);
584 /* clock inversion (CKG2) */
586 switch (SH_FSI_INVERSION_MASK
& flags
) {
600 fsi_reg_write(fsi
, CKG2
, data
);
604 reg
= is_play
? DO_FMT
: DI_FMT
;
605 fmt
= is_play
? SH_FSI_GET_OFMT(flags
) : SH_FSI_GET_IFMT(flags
);
607 case SH_FSI_FMT_MONO
:
609 data
= CR_FMT(CR_MONO
);
612 case SH_FSI_FMT_MONO_DELAY
:
614 data
= CR_FMT(CR_MONO_D
);
619 data
= CR_FMT(CR_PCM
);
624 data
= CR_FMT(CR_I2S
);
629 data
= CR_FMT(CR_TDM
) | (fsi
->chan
- 1);
630 fsi
->chan
= is_play
?
631 SH_FSI_GET_CH_O(flags
) : SH_FSI_GET_CH_I(flags
);
633 case SH_FSI_FMT_TDM_DELAY
:
635 data
= CR_FMT(CR_TDM_D
) | (fsi
->chan
- 1);
636 fsi
->chan
= is_play
?
637 SH_FSI_GET_CH_O(flags
) : SH_FSI_GET_CH_I(flags
);
640 dev_err(dai
->dev
, "unknown format.\n");
662 dev_err(dai
->dev
, "channel size error.\n");
666 fsi_reg_write(fsi
, reg
, data
);
667 dev_dbg(dai
->dev
, "use %s format (%d channel) use %d DMAC\n",
668 msg
, fsi
->chan
, fsi
->dma_chan
);
671 * clear clk reset if master mode
674 fsi_clk_ctrl(fsi
, 1);
677 fsi_irq_init(fsi
, is_play
);
682 static void fsi_dai_shutdown(struct snd_pcm_substream
*substream
,
683 struct snd_soc_dai
*dai
)
685 struct fsi_priv
*fsi
= fsi_get(substream
);
686 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
688 fsi_irq_disable(fsi
, is_play
);
689 fsi_clk_ctrl(fsi
, 0);
691 clk_disable(master
->clk
);
694 static int fsi_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
695 struct snd_soc_dai
*dai
)
697 struct fsi_priv
*fsi
= fsi_get(substream
);
698 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
699 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
702 /* capture not supported */
707 case SNDRV_PCM_TRIGGER_START
:
708 fsi_stream_push(fsi
, substream
,
709 frames_to_bytes(runtime
, runtime
->buffer_size
),
710 frames_to_bytes(runtime
, runtime
->period_size
));
711 ret
= fsi_data_push(fsi
);
713 case SNDRV_PCM_TRIGGER_STOP
:
714 fsi_irq_disable(fsi
, is_play
);
722 static struct snd_soc_dai_ops fsi_dai_ops
= {
723 .startup
= fsi_dai_startup
,
724 .shutdown
= fsi_dai_shutdown
,
725 .trigger
= fsi_dai_trigger
,
728 /************************************************************************
734 ************************************************************************/
735 static struct snd_pcm_hardware fsi_pcm_hardware
= {
736 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
737 SNDRV_PCM_INFO_MMAP
|
738 SNDRV_PCM_INFO_MMAP_VALID
|
739 SNDRV_PCM_INFO_PAUSE
,
746 .buffer_bytes_max
= 64 * 1024,
747 .period_bytes_min
= 32,
748 .period_bytes_max
= 8192,
754 static int fsi_pcm_open(struct snd_pcm_substream
*substream
)
756 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
759 snd_soc_set_runtime_hwparams(substream
, &fsi_pcm_hardware
);
761 ret
= snd_pcm_hw_constraint_integer(runtime
,
762 SNDRV_PCM_HW_PARAM_PERIODS
);
767 static int fsi_hw_params(struct snd_pcm_substream
*substream
,
768 struct snd_pcm_hw_params
*hw_params
)
770 return snd_pcm_lib_malloc_pages(substream
,
771 params_buffer_bytes(hw_params
));
774 static int fsi_hw_free(struct snd_pcm_substream
*substream
)
776 return snd_pcm_lib_free_pages(substream
);
779 static snd_pcm_uframes_t
fsi_pointer(struct snd_pcm_substream
*substream
)
781 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
782 struct fsi_priv
*fsi
= fsi_get(substream
);
783 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
786 location
= (fsi
->byte_offset
- 1) - fsi_get_residue(fsi
, is_play
);
790 return bytes_to_frames(runtime
, location
);
793 static struct snd_pcm_ops fsi_pcm_ops
= {
794 .open
= fsi_pcm_open
,
795 .ioctl
= snd_pcm_lib_ioctl
,
796 .hw_params
= fsi_hw_params
,
797 .hw_free
= fsi_hw_free
,
798 .pointer
= fsi_pointer
,
801 /************************************************************************
807 ************************************************************************/
808 #define PREALLOC_BUFFER (32 * 1024)
809 #define PREALLOC_BUFFER_MAX (32 * 1024)
811 static void fsi_pcm_free(struct snd_pcm
*pcm
)
813 snd_pcm_lib_preallocate_free_for_all(pcm
);
816 static int fsi_pcm_new(struct snd_card
*card
,
817 struct snd_soc_dai
*dai
,
821 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
822 * in MMAP mode (i.e. aplay -M)
824 return snd_pcm_lib_preallocate_pages_for_all(
826 SNDRV_DMA_TYPE_CONTINUOUS
,
827 snd_dma_continuous_data(GFP_KERNEL
),
828 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
831 /************************************************************************
837 ************************************************************************/
838 struct snd_soc_dai fsi_soc_dai
[] = {
848 /* capture not supported */
860 /* capture not supported */
864 EXPORT_SYMBOL_GPL(fsi_soc_dai
);
866 struct snd_soc_platform fsi_soc_platform
= {
868 .pcm_ops
= &fsi_pcm_ops
,
869 .pcm_new
= fsi_pcm_new
,
870 .pcm_free
= fsi_pcm_free
,
872 EXPORT_SYMBOL_GPL(fsi_soc_platform
);
874 /************************************************************************
880 ************************************************************************/
881 static int fsi_probe(struct platform_device
*pdev
)
883 struct resource
*res
;
888 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
889 irq
= platform_get_irq(pdev
, 0);
891 dev_err(&pdev
->dev
, "Not enough FSI platform resources.\n");
896 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
898 dev_err(&pdev
->dev
, "Could not allocate master\n");
903 master
->base
= ioremap_nocache(res
->start
, resource_size(res
));
906 dev_err(&pdev
->dev
, "Unable to ioremap FSI registers.\n");
911 master
->info
= pdev
->dev
.platform_data
;
912 master
->fsia
.base
= master
->base
;
913 master
->fsib
.base
= master
->base
+ 0x40;
915 master
->fsia
.dma_chan
= -1;
916 master
->fsib
.dma_chan
= -1;
918 ret
= fsi_get_dma_chan();
920 dev_err(&pdev
->dev
, "cannot get dma api\n");
924 /* FSI is based on SPU mstp */
925 snprintf(clk_name
, sizeof(clk_name
), "spu%d", pdev
->id
);
926 master
->clk
= clk_get(NULL
, clk_name
);
927 if (IS_ERR(master
->clk
)) {
928 dev_err(&pdev
->dev
, "cannot get %s mstp\n", clk_name
);
933 fsi_soc_dai
[0].dev
= &pdev
->dev
;
934 fsi_soc_dai
[1].dev
= &pdev
->dev
;
936 fsi_soft_all_reset();
938 ret
= request_irq(irq
, &fsi_interrupt
, IRQF_DISABLED
, "fsi", master
);
940 dev_err(&pdev
->dev
, "irq request err\n");
944 ret
= snd_soc_register_platform(&fsi_soc_platform
);
946 dev_err(&pdev
->dev
, "cannot snd soc register\n");
950 return snd_soc_register_dais(fsi_soc_dai
, ARRAY_SIZE(fsi_soc_dai
));
953 free_irq(irq
, master
);
957 iounmap(master
->base
);
965 static int fsi_remove(struct platform_device
*pdev
)
967 snd_soc_unregister_dais(fsi_soc_dai
, ARRAY_SIZE(fsi_soc_dai
));
968 snd_soc_unregister_platform(&fsi_soc_platform
);
970 clk_put(master
->clk
);
974 free_irq(master
->irq
, master
);
976 iounmap(master
->base
);
982 static struct platform_driver fsi_driver
= {
987 .remove
= fsi_remove
,
990 static int __init
fsi_mobile_init(void)
992 return platform_driver_register(&fsi_driver
);
995 static void __exit
fsi_mobile_exit(void)
997 platform_driver_unregister(&fsi_driver
);
999 module_init(fsi_mobile_init
);
1000 module_exit(fsi_mobile_exit
);
1002 MODULE_LICENSE("GPL");
1003 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1004 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");