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/pm_runtime.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>
31 #define DOFF_CTL 0x0004
32 #define DOFF_ST 0x0008
34 #define DIFF_CTL 0x0010
35 #define DIFF_ST 0x0014
40 #define MUTE_ST 0x0028
41 #define REG_END MUTE_ST
47 #define CLK_RST 0x0210
48 #define SOFT_RST 0x0214
49 #define MREG_START INT_ST
50 #define MREG_END SOFT_RST
54 #define CR_FMT(param) ((param) << 4)
56 # define CR_MONO_D 0x1
64 #define IRQ_HALF 0x00100000
65 #define FIFO_CLR 0x00000001
68 #define ERR_OVER 0x00000010
69 #define ERR_UNDER 0x00000001
70 #define ST_ERR (ERR_OVER | ERR_UNDER)
73 #define B_CLK 0x00000010
74 #define A_CLK 0x00000001
77 #define INT_B_IN (1 << 12)
78 #define INT_B_OUT (1 << 8)
79 #define INT_A_IN (1 << 4)
80 #define INT_A_OUT (1 << 0)
83 #define PBSR (1 << 12) /* Port B Software Reset */
84 #define PASR (1 << 8) /* Port A Software Reset */
85 #define IR (1 << 4) /* Interrupt Reset */
86 #define FSISR (1 << 0) /* Software Reset */
88 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
90 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
92 /************************************************************************
98 ************************************************************************/
101 struct snd_pcm_substream *substream;
102 struct fsi_master *master;
116 struct fsi_priv fsia;
117 struct fsi_priv fsib;
118 struct sh_fsi_platform_info *info;
122 /************************************************************************
125 basic read write function
128 ************************************************************************/
129 static void __fsi_reg_write(u32 reg, u32 data)
131 /* valid data area is 24bit */
134 __raw_writel(data, reg);
137 static u32 __fsi_reg_read(u32 reg)
139 return __raw_readl(reg);
142 static void __fsi_reg_mask_set(u32 reg, u32 mask, u32 data)
144 u32 val = __fsi_reg_read(reg);
149 __fsi_reg_write(reg, val);
152 static void fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
157 __fsi_reg_write((u32)(fsi->base + reg), data);
160 static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
165 return __fsi_reg_read((u32)(fsi->base + reg));
168 static void fsi_reg_mask_set(struct fsi_priv *fsi, u32 reg, u32 mask, u32 data)
173 __fsi_reg_mask_set((u32)(fsi->base + reg), mask, data);
176 static void fsi_master_write(struct fsi_master *master, u32 reg, u32 data)
180 if ((reg < MREG_START) ||
184 spin_lock_irqsave(&master->lock, flags);
185 __fsi_reg_write((u32)(master->base + reg), data);
186 spin_unlock_irqrestore(&master->lock, flags);
189 static u32 fsi_master_read(struct fsi_master *master, u32 reg)
194 if ((reg < MREG_START) ||
198 spin_lock_irqsave(&master->lock, flags);
199 ret = __fsi_reg_read((u32)(master->base + reg));
200 spin_unlock_irqrestore(&master->lock, flags);
205 static void fsi_master_mask_set(struct fsi_master *master,
206 u32 reg, u32 mask, u32 data)
210 if ((reg < MREG_START) ||
214 spin_lock_irqsave(&master->lock, flags);
215 __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
216 spin_unlock_irqrestore(&master->lock, flags);
219 /************************************************************************
225 ************************************************************************/
226 static struct fsi_master *fsi_get_master(struct fsi_priv *fsi)
231 static int fsi_is_port_a(struct fsi_priv *fsi)
233 return fsi->master->base == fsi->base;
236 static struct snd_soc_dai *fsi_get_dai(struct snd_pcm_substream *substream)
238 struct snd_soc_pcm_runtime *rtd = substream->private_data;
239 struct snd_soc_dai_link *machine = rtd->dai;
241 return machine->cpu_dai;
244 static struct fsi_priv *fsi_get_priv(struct snd_pcm_substream *substream)
246 struct snd_soc_dai *dai = fsi_get_dai(substream);
248 return dai->private_data;
251 static u32 fsi_get_info_flags(struct fsi_priv *fsi)
253 int is_porta = fsi_is_port_a(fsi);
254 struct fsi_master *master = fsi_get_master(fsi);
256 return is_porta ? master->info->porta_flags :
257 master->info->portb_flags;
260 static int fsi_is_master_mode(struct fsi_priv *fsi, int is_play)
263 u32 flags = fsi_get_info_flags(fsi);
265 mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
272 return (mode & flags) != mode;
275 static u32 fsi_port_ab_io_bit(struct fsi_priv *fsi, int is_play)
277 int is_porta = fsi_is_port_a(fsi);
281 data = is_play ? (1 << 0) : (1 << 4);
283 data = is_play ? (1 << 8) : (1 << 12);
288 static void fsi_stream_push(struct fsi_priv *fsi,
289 struct snd_pcm_substream *substream,
293 fsi->substream = substream;
294 fsi->buffer_len = buffer_len;
295 fsi->period_len = period_len;
296 fsi->byte_offset = 0;
300 static void fsi_stream_pop(struct fsi_priv *fsi)
302 fsi->substream = NULL;
305 fsi->byte_offset = 0;
309 static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
312 u32 reg = is_play ? DOFF_ST : DIFF_ST;
315 status = fsi_reg_read(fsi, reg);
316 residue = 0x1ff & (status >> 8);
317 residue *= fsi->chan;
322 /************************************************************************
328 ************************************************************************/
329 static void fsi_irq_enable(struct fsi_priv *fsi, int is_play)
331 u32 data = fsi_port_ab_io_bit(fsi, is_play);
332 struct fsi_master *master = fsi_get_master(fsi);
334 fsi_master_mask_set(master, IMSK, data, data);
335 fsi_master_mask_set(master, IEMSK, data, data);
338 static void fsi_irq_disable(struct fsi_priv *fsi, int is_play)
340 u32 data = fsi_port_ab_io_bit(fsi, is_play);
341 struct fsi_master *master = fsi_get_master(fsi);
343 fsi_master_mask_set(master, IMSK, data, 0);
344 fsi_master_mask_set(master, IEMSK, data, 0);
347 static void fsi_clk_ctrl(struct fsi_priv *fsi, int enable)
349 u32 val = fsi_is_port_a(fsi) ? (1 << 0) : (1 << 4);
350 struct fsi_master *master = fsi_get_master(fsi);
353 fsi_master_mask_set(master, CLK_RST, val, val);
355 fsi_master_mask_set(master, CLK_RST, val, 0);
358 static void fsi_irq_init(struct fsi_priv *fsi, int is_play)
363 data = fsi_port_ab_io_bit(fsi, is_play);
364 ctrl = is_play ? DOFF_CTL : DIFF_CTL;
367 fsi_irq_disable(fsi, is_play);
369 /* set interrupt generation factor */
370 fsi_reg_write(fsi, ctrl, IRQ_HALF);
373 fsi_reg_mask_set(fsi, ctrl, FIFO_CLR, FIFO_CLR);
375 /* clear interrupt factor */
376 fsi_master_mask_set(fsi_get_master(fsi), INT_ST, data, 0);
379 static void fsi_soft_all_reset(struct fsi_master *master)
382 fsi_master_mask_set(master, SOFT_RST, PASR | PBSR, 0);
386 fsi_master_mask_set(master, SOFT_RST, FSISR, 0);
387 fsi_master_mask_set(master, SOFT_RST, FSISR, FSISR);
391 /* playback interrupt */
392 static int fsi_data_push(struct fsi_priv *fsi, int startup)
394 struct snd_pcm_runtime *runtime;
395 struct snd_pcm_substream *substream = NULL;
405 !fsi->substream->runtime)
409 substream = fsi->substream;
410 runtime = substream->runtime;
412 /* FSI FIFO has limit.
413 * So, this driver can not send periods data at a time
415 if (fsi->byte_offset >=
416 fsi->period_len * (fsi->periods + 1)) {
419 fsi->periods = (fsi->periods + 1) % runtime->periods;
421 if (0 == fsi->periods)
422 fsi->byte_offset = 0;
425 /* get 1 channel data width */
426 width = frames_to_bytes(runtime, 1) / fsi->chan;
428 /* get send size for alsa */
429 send = (fsi->buffer_len - fsi->byte_offset) / width;
431 /* get FIFO free size */
432 fifo_free = (fsi->fifo_max * fsi->chan) - fsi_get_fifo_residue(fsi, 1);
435 if (fifo_free < send)
438 start = runtime->dma_area;
439 start += fsi->byte_offset;
443 for (i = 0; i < send; i++)
444 fsi_reg_write(fsi, DODT,
445 ((u32)*((u16 *)start + i) << 8));
448 for (i = 0; i < send; i++)
449 fsi_reg_write(fsi, DODT, *((u32 *)start + i));
455 fsi->byte_offset += send * width;
457 status = fsi_reg_read(fsi, DOFF_ST);
459 struct snd_soc_dai *dai = fsi_get_dai(substream);
461 if (status & ERR_OVER)
462 dev_err(dai->dev, "over run\n");
463 if (status & ERR_UNDER)
464 dev_err(dai->dev, "under run\n");
466 fsi_reg_write(fsi, DOFF_ST, 0);
468 fsi_irq_enable(fsi, 1);
471 snd_pcm_period_elapsed(substream);
476 static int fsi_data_pop(struct fsi_priv *fsi, int startup)
478 struct snd_pcm_runtime *runtime;
479 struct snd_pcm_substream *substream = NULL;
489 !fsi->substream->runtime)
493 substream = fsi->substream;
494 runtime = substream->runtime;
496 /* FSI FIFO has limit.
497 * So, this driver can not send periods data at a time
499 if (fsi->byte_offset >=
500 fsi->period_len * (fsi->periods + 1)) {
503 fsi->periods = (fsi->periods + 1) % runtime->periods;
505 if (0 == fsi->periods)
506 fsi->byte_offset = 0;
509 /* get 1 channel data width */
510 width = frames_to_bytes(runtime, 1) / fsi->chan;
512 /* get free space for alsa */
513 free = (fsi->buffer_len - fsi->byte_offset) / width;
516 fifo_fill = fsi_get_fifo_residue(fsi, 0);
518 if (free < fifo_fill)
521 start = runtime->dma_area;
522 start += fsi->byte_offset;
526 for (i = 0; i < fifo_fill; i++)
527 *((u16 *)start + i) =
528 (u16)(fsi_reg_read(fsi, DIDT) >> 8);
531 for (i = 0; i < fifo_fill; i++)
532 *((u32 *)start + i) = fsi_reg_read(fsi, DIDT);
538 fsi->byte_offset += fifo_fill * width;
540 status = fsi_reg_read(fsi, DIFF_ST);
542 struct snd_soc_dai *dai = fsi_get_dai(substream);
544 if (status & ERR_OVER)
545 dev_err(dai->dev, "over run\n");
546 if (status & ERR_UNDER)
547 dev_err(dai->dev, "under run\n");
549 fsi_reg_write(fsi, DIFF_ST, 0);
551 fsi_irq_enable(fsi, 0);
554 snd_pcm_period_elapsed(substream);
559 static irqreturn_t fsi_interrupt(int irq, void *data)
561 struct fsi_master *master = data;
562 u32 int_st = fsi_master_read(master, INT_ST);
564 /* clear irq status */
565 fsi_master_mask_set(master, SOFT_RST, IR, 0);
566 fsi_master_mask_set(master, SOFT_RST, IR, IR);
568 if (int_st & INT_A_OUT)
569 fsi_data_push(&master->fsia, 0);
570 if (int_st & INT_B_OUT)
571 fsi_data_push(&master->fsib, 0);
572 if (int_st & INT_A_IN)
573 fsi_data_pop(&master->fsia, 0);
574 if (int_st & INT_B_IN)
575 fsi_data_pop(&master->fsib, 0);
577 fsi_master_write(master, INT_ST, 0x0000000);
582 /************************************************************************
588 ************************************************************************/
589 static int fsi_dai_startup(struct snd_pcm_substream *substream,
590 struct snd_soc_dai *dai)
592 struct fsi_priv *fsi = fsi_get_priv(substream);
594 u32 flags = fsi_get_info_flags(fsi);
598 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
602 pm_runtime_get_sync(dai->dev);
605 data = is_play ? (1 << 0) : (1 << 4);
606 is_master = fsi_is_master_mode(fsi, is_play);
608 fsi_reg_mask_set(fsi, CKG1, data, data);
610 fsi_reg_mask_set(fsi, CKG1, data, 0);
612 /* clock inversion (CKG2) */
614 switch (SH_FSI_INVERSION_MASK & flags) {
628 fsi_reg_write(fsi, CKG2, data);
632 reg = is_play ? DO_FMT : DI_FMT;
633 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
635 case SH_FSI_FMT_MONO:
637 data = CR_FMT(CR_MONO);
640 case SH_FSI_FMT_MONO_DELAY:
642 data = CR_FMT(CR_MONO_D);
647 data = CR_FMT(CR_PCM);
652 data = CR_FMT(CR_I2S);
657 data = CR_FMT(CR_TDM) | (fsi->chan - 1);
658 fsi->chan = is_play ?
659 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
661 case SH_FSI_FMT_TDM_DELAY:
663 data = CR_FMT(CR_TDM_D) | (fsi->chan - 1);
664 fsi->chan = is_play ?
665 SH_FSI_GET_CH_O(flags) : SH_FSI_GET_CH_I(flags);
668 dev_err(dai->dev, "unknown format.\n");
690 dev_err(dai->dev, "channel size error.\n");
694 fsi_reg_write(fsi, reg, data);
697 * clear clk reset if master mode
700 fsi_clk_ctrl(fsi, 1);
703 fsi_irq_init(fsi, is_play);
708 static void fsi_dai_shutdown(struct snd_pcm_substream *substream,
709 struct snd_soc_dai *dai)
711 struct fsi_priv *fsi = fsi_get_priv(substream);
712 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
714 fsi_irq_disable(fsi, is_play);
715 fsi_clk_ctrl(fsi, 0);
717 pm_runtime_put_sync(dai->dev);
720 static int fsi_dai_trigger(struct snd_pcm_substream *substream, int cmd,
721 struct snd_soc_dai *dai)
723 struct fsi_priv *fsi = fsi_get_priv(substream);
724 struct snd_pcm_runtime *runtime = substream->runtime;
725 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
729 case SNDRV_PCM_TRIGGER_START:
730 fsi_stream_push(fsi, substream,
731 frames_to_bytes(runtime, runtime->buffer_size),
732 frames_to_bytes(runtime, runtime->period_size));
733 ret = is_play ? fsi_data_push(fsi, 1) : fsi_data_pop(fsi, 1);
735 case SNDRV_PCM_TRIGGER_STOP:
736 fsi_irq_disable(fsi, is_play);
744 static struct snd_soc_dai_ops fsi_dai_ops = {
745 .startup = fsi_dai_startup,
746 .shutdown = fsi_dai_shutdown,
747 .trigger = fsi_dai_trigger,
750 /************************************************************************
756 ************************************************************************/
757 static struct snd_pcm_hardware fsi_pcm_hardware = {
758 .info = SNDRV_PCM_INFO_INTERLEAVED |
759 SNDRV_PCM_INFO_MMAP |
760 SNDRV_PCM_INFO_MMAP_VALID |
761 SNDRV_PCM_INFO_PAUSE,
768 .buffer_bytes_max = 64 * 1024,
769 .period_bytes_min = 32,
770 .period_bytes_max = 8192,
776 static int fsi_pcm_open(struct snd_pcm_substream *substream)
778 struct snd_pcm_runtime *runtime = substream->runtime;
781 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
783 ret = snd_pcm_hw_constraint_integer(runtime,
784 SNDRV_PCM_HW_PARAM_PERIODS);
789 static int fsi_hw_params(struct snd_pcm_substream *substream,
790 struct snd_pcm_hw_params *hw_params)
792 return snd_pcm_lib_malloc_pages(substream,
793 params_buffer_bytes(hw_params));
796 static int fsi_hw_free(struct snd_pcm_substream *substream)
798 return snd_pcm_lib_free_pages(substream);
801 static snd_pcm_uframes_t fsi_pointer(struct snd_pcm_substream *substream)
803 struct snd_pcm_runtime *runtime = substream->runtime;
804 struct fsi_priv *fsi = fsi_get_priv(substream);
807 location = (fsi->byte_offset - 1);
811 return bytes_to_frames(runtime, location);
814 static struct snd_pcm_ops fsi_pcm_ops = {
815 .open = fsi_pcm_open,
816 .ioctl = snd_pcm_lib_ioctl,
817 .hw_params = fsi_hw_params,
818 .hw_free = fsi_hw_free,
819 .pointer = fsi_pointer,
822 /************************************************************************
828 ************************************************************************/
829 #define PREALLOC_BUFFER (32 * 1024)
830 #define PREALLOC_BUFFER_MAX (32 * 1024)
832 static void fsi_pcm_free(struct snd_pcm *pcm)
834 snd_pcm_lib_preallocate_free_for_all(pcm);
837 static int fsi_pcm_new(struct snd_card *card,
838 struct snd_soc_dai *dai,
842 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
843 * in MMAP mode (i.e. aplay -M)
845 return snd_pcm_lib_preallocate_pages_for_all(
847 SNDRV_DMA_TYPE_CONTINUOUS,
848 snd_dma_continuous_data(GFP_KERNEL),
849 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
852 /************************************************************************
858 ************************************************************************/
859 struct snd_soc_dai fsi_soc_dai[] = {
895 EXPORT_SYMBOL_GPL(fsi_soc_dai);
897 struct snd_soc_platform fsi_soc_platform = {
899 .pcm_ops = &fsi_pcm_ops,
900 .pcm_new = fsi_pcm_new,
901 .pcm_free = fsi_pcm_free,
903 EXPORT_SYMBOL_GPL(fsi_soc_platform);
905 /************************************************************************
911 ************************************************************************/
912 static int fsi_probe(struct platform_device *pdev)
914 struct fsi_master *master;
915 struct resource *res;
920 dev_err(&pdev->dev, "current fsi support id 0 only now\n");
924 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
925 irq = platform_get_irq(pdev, 0);
926 if (!res || (int)irq <= 0) {
927 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
932 master = kzalloc(sizeof(*master), GFP_KERNEL);
934 dev_err(&pdev->dev, "Could not allocate master\n");
939 master->base = ioremap_nocache(res->start, resource_size(res));
942 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
947 master->info = pdev->dev.platform_data;
948 master->fsia.base = master->base;
949 master->fsia.master = master;
950 master->fsib.base = master->base + 0x40;
951 master->fsib.master = master;
952 spin_lock_init(&master->lock);
954 pm_runtime_enable(&pdev->dev);
955 pm_runtime_resume(&pdev->dev);
957 fsi_soc_dai[0].dev = &pdev->dev;
958 fsi_soc_dai[0].private_data = &master->fsia;
959 fsi_soc_dai[1].dev = &pdev->dev;
960 fsi_soc_dai[1].private_data = &master->fsib;
962 fsi_soft_all_reset(master);
964 ret = request_irq(irq, &fsi_interrupt, IRQF_DISABLED, "fsi", master);
966 dev_err(&pdev->dev, "irq request err\n");
970 ret = snd_soc_register_platform(&fsi_soc_platform);
972 dev_err(&pdev->dev, "cannot snd soc register\n");
976 return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
979 free_irq(irq, master);
981 iounmap(master->base);
982 pm_runtime_disable(&pdev->dev);
990 static int fsi_remove(struct platform_device *pdev)
992 struct fsi_master *master;
994 master = fsi_get_master(fsi_soc_dai[0].private_data);
996 snd_soc_unregister_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
997 snd_soc_unregister_platform(&fsi_soc_platform);
999 pm_runtime_disable(&pdev->dev);
1001 free_irq(master->irq, master);
1003 iounmap(master->base);
1006 fsi_soc_dai[0].dev = NULL;
1007 fsi_soc_dai[0].private_data = NULL;
1008 fsi_soc_dai[1].dev = NULL;
1009 fsi_soc_dai[1].private_data = NULL;
1014 static int fsi_runtime_nop(struct device *dev)
1016 /* Runtime PM callback shared between ->runtime_suspend()
1017 * and ->runtime_resume(). Simply returns success.
1019 * This driver re-initializes all registers after
1020 * pm_runtime_get_sync() anyway so there is no need
1021 * to save and restore registers here.
1026 static struct dev_pm_ops fsi_pm_ops = {
1027 .runtime_suspend = fsi_runtime_nop,
1028 .runtime_resume = fsi_runtime_nop,
1031 static struct platform_driver fsi_driver = {
1037 .remove = fsi_remove,
1040 static int __init fsi_mobile_init(void)
1042 return platform_driver_register(&fsi_driver);
1045 static void __exit fsi_mobile_exit(void)
1047 platform_driver_unregister(&fsi_driver);
1049 module_init(fsi_mobile_init);
1050 module_exit(fsi_mobile_exit);
1052 MODULE_LICENSE("GPL");
1053 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1054 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");