MINI2440: Add touchscreen support
[linux-2.6/mini2440.git] / sound / soc / sh / fsi.c
blob44123248b6301a5547871b1ded1f64acaddfec12
1 /*
2 * Fifo-attached Serial Interface (FSI) support for SH7724
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
7 * Based on ssi.c
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>
21 #include <linux/io.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>
29 #include <asm/dma.h>
30 #include <asm/dma-sh.h>
32 #define DO_FMT 0x0000
33 #define DOFF_CTL 0x0004
34 #define DOFF_ST 0x0008
35 #define DI_FMT 0x000C
36 #define DIFF_CTL 0x0010
37 #define DIFF_ST 0x0014
38 #define CKG1 0x0018
39 #define CKG2 0x001C
40 #define DIDT 0x0020
41 #define DODT 0x0024
42 #define MUTE_ST 0x0028
43 #define REG_END MUTE_ST
45 #define INT_ST 0x0200
46 #define IEMSK 0x0204
47 #define IMSK 0x0208
48 #define MUTE 0x020C
49 #define CLK_RST 0x0210
50 #define SOFT_RST 0x0214
51 #define MREG_START INT_ST
52 #define MREG_END SOFT_RST
54 /* DO_FMT */
55 /* DI_FMT */
56 #define CR_FMT(param) ((param) << 4)
57 # define CR_MONO 0x0
58 # define CR_MONO_D 0x1
59 # define CR_PCM 0x2
60 # define CR_I2S 0x3
61 # define CR_TDM 0x4
62 # define CR_TDM_D 0x5
64 /* DOFF_CTL */
65 /* DIFF_CTL */
66 #define IRQ_HALF 0x00100000
67 #define FIFO_CLR 0x00000001
69 /* DOFF_ST */
70 #define ERR_OVER 0x00000010
71 #define ERR_UNDER 0x00000001
73 /* CLK_RST */
74 #define B_CLK 0x00000010
75 #define A_CLK 0x00000001
77 /* INT_ST */
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 /************************************************************************
90 struct
93 ************************************************************************/
94 struct fsi_priv {
95 void __iomem *base;
96 struct snd_pcm_substream *substream;
98 int fifo_max;
99 int chan;
100 int dma_chan;
102 int byte_offset;
103 int period_len;
104 int buffer_len;
105 int periods;
108 struct fsi_master {
109 void __iomem *base;
110 int irq;
111 struct clk *clk;
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 */
129 data &= 0x00ffffff;
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);
143 val &= ~mask;
144 val |= data & mask;
146 return __fsi_reg_write(reg, val);
149 static int fsi_reg_write(struct fsi_priv *fsi, u32 reg, u32 data)
151 if (reg > REG_END)
152 return -1;
154 return __fsi_reg_write((u32)(fsi->base + reg), data);
157 static u32 fsi_reg_read(struct fsi_priv *fsi, u32 reg)
159 if (reg > REG_END)
160 return 0;
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)
167 if (reg > REG_END)
168 return -1;
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) ||
176 (reg > MREG_END))
177 return -1;
179 return __fsi_reg_write((u32)(master->base + reg), data);
182 static u32 fsi_master_read(u32 reg)
184 if ((reg < MREG_START) ||
185 (reg > MREG_END))
186 return 0;
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) ||
194 (reg > MREG_END))
195 return -1;
197 return __fsi_reg_mask_set((u32)(master->base + reg), mask, data);
200 /************************************************************************
203 basic function
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)
213 return NULL;
215 rtd = substream->private_data;
216 switch (rtd->dai->cpu_dai->id) {
217 case 0:
218 fsi = &master->fsia;
219 break;
220 case 1:
221 fsi = &master->fsib;
222 break;
225 return fsi;
228 static int fsi_is_port_a(struct fsi_priv *fsi)
230 /* return
231 * 1 : port a
232 * 0 : port b
235 if (fsi == &master->fsia)
236 return 1;
238 return 0;
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)
251 u32 mode;
252 u32 flags = fsi_get_info_flags(fsi);
254 mode = is_play ? SH_FSI_OUT_SLAVE_MODE : SH_FSI_IN_SLAVE_MODE;
256 /* return
257 * 1 : master mode
258 * 0 : 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);
267 u32 data;
269 if (is_porta)
270 data = is_play ? (1 << 0) : (1 << 4);
271 else
272 data = is_play ? (1 << 8) : (1 << 12);
274 return data;
277 static void fsi_stream_push(struct fsi_priv *fsi,
278 struct snd_pcm_substream *substream,
279 u32 buffer_len,
280 u32 period_len)
282 fsi->substream = substream;
283 fsi->buffer_len = buffer_len;
284 fsi->period_len = period_len;
285 fsi->byte_offset = 0;
286 fsi->periods = 0;
289 static void fsi_stream_pop(struct fsi_priv *fsi)
291 fsi->substream = NULL;
292 fsi->buffer_len = 0;
293 fsi->period_len = 0;
294 fsi->byte_offset = 0;
295 fsi->periods = 0;
298 static int fsi_get_fifo_residue(struct fsi_priv *fsi, int is_play)
300 u32 status;
301 u32 reg = is_play ? DOFF_ST : DIFF_ST;
302 int residue;
304 status = fsi_reg_read(fsi, reg);
305 residue = 0x1ff & (status >> 8);
306 residue *= fsi->chan;
308 return residue;
311 static int fsi_get_residue(struct fsi_priv *fsi, int is_play)
313 int residue;
314 int width;
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;
322 if (2 == width)
323 residue = fsi_get_fifo_residue(fsi, is_play);
324 else
325 residue = get_dma_residue(fsi->dma_chan);
327 return residue;
330 /************************************************************************
333 basic dma function
336 ************************************************************************/
337 #define PORTA_DMA 0
338 #define PORTB_DMA 1
340 static int fsi_get_dma_chan(void)
342 if (0 != request_dma(PORTA_DMA, "fsia"))
343 return -EIO;
345 if (0 != request_dma(PORTB_DMA, "fsib")) {
346 free_dma(PORTA_DMA);
347 return -EIO;
350 master->fsia.dma_chan = PORTA_DMA;
351 master->fsib.dma_chan = PORTB_DMA;
353 return 0;
356 static void fsi_free_dma_chan(void)
358 dma_wait_for_completion(PORTA_DMA);
359 dma_wait_for_completion(PORTB_DMA);
360 free_dma(PORTA_DMA);
361 free_dma(PORTB_DMA);
363 master->fsia.dma_chan = -1;
364 master->fsib.dma_chan = -1;
367 /************************************************************************
370 ctrl function
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);
394 if (enable)
395 fsi_master_mask_set(CLK_RST, val, val);
396 else
397 fsi_master_mask_set(CLK_RST, val, 0);
400 static void fsi_irq_init(struct fsi_priv *fsi, int is_play)
402 u32 data;
403 u32 ctrl;
405 data = fsi_port_ab_io_bit(fsi, is_play);
406 ctrl = is_play ? DOFF_CTL : DIFF_CTL;
408 /* set IMSK */
409 fsi_irq_disable(fsi, is_play);
411 /* set interrupt generation factor */
412 fsi_reg_write(fsi, ctrl, IRQ_HALF);
414 /* clear FIFO */
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);
425 /* port AB reset */
426 status &= 0x000000ff;
427 fsi_master_write(SOFT_RST, status);
428 mdelay(10);
430 /* soft reset */
431 status &= 0x000000f0;
432 fsi_master_write(SOFT_RST, status);
433 status |= 0x00000001;
434 fsi_master_write(SOFT_RST, status);
435 mdelay(10);
438 static void fsi_16data_push(struct fsi_priv *fsi,
439 struct snd_pcm_runtime *runtime,
440 int send)
442 u16 *dma_start;
443 u32 snd;
444 int i;
446 /* get dma start position for FSI */
447 dma_start = (u16 *)runtime->dma_area;
448 dma_start += fsi->byte_offset / 2;
451 * soft dma
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,
462 int send)
464 u32 *dma_start;
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;
481 int send;
482 int fifo_free;
483 int width;
485 if (!fsi ||
486 !fsi->substream ||
487 !fsi->substream->runtime)
488 return -EINVAL;
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);
514 /* size check */
515 if (fifo_free < send)
516 send = fifo_free;
518 if (2 == width)
519 fsi_16data_push(fsi, runtime, send);
520 else if (4 == width)
521 fsi_32data_push(fsi, runtime, send);
522 else
523 return -EINVAL;
525 fsi->byte_offset += send * width;
527 fsi_irq_enable(fsi, 1);
529 if (substream)
530 snd_pcm_period_elapsed(substream);
532 return 0;
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);
551 return IRQ_HANDLED;
554 /************************************************************************
557 dai ops
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);
565 const char *msg;
566 u32 flags = fsi_get_info_flags(fsi);
567 u32 fmt;
568 u32 reg;
569 u32 data;
570 int is_play = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
571 int is_master;
572 int ret = 0;
574 clk_enable(master->clk);
576 /* CKG1 */
577 data = is_play ? (1 << 0) : (1 << 4);
578 is_master = fsi_is_master_mode(fsi, is_play);
579 if (is_master)
580 fsi_reg_mask_set(fsi, CKG1, data, data);
581 else
582 fsi_reg_mask_set(fsi, CKG1, data, 0);
584 /* clock inversion (CKG2) */
585 data = 0;
586 switch (SH_FSI_INVERSION_MASK & flags) {
587 case SH_FSI_LRM_INV:
588 data = 1 << 12;
589 break;
590 case SH_FSI_BRM_INV:
591 data = 1 << 8;
592 break;
593 case SH_FSI_LRS_INV:
594 data = 1 << 4;
595 break;
596 case SH_FSI_BRS_INV:
597 data = 1 << 0;
598 break;
600 fsi_reg_write(fsi, CKG2, data);
602 /* do fmt, di fmt */
603 data = 0;
604 reg = is_play ? DO_FMT : DI_FMT;
605 fmt = is_play ? SH_FSI_GET_OFMT(flags) : SH_FSI_GET_IFMT(flags);
606 switch (fmt) {
607 case SH_FSI_FMT_MONO:
608 msg = "MONO";
609 data = CR_FMT(CR_MONO);
610 fsi->chan = 1;
611 break;
612 case SH_FSI_FMT_MONO_DELAY:
613 msg = "MONO Delay";
614 data = CR_FMT(CR_MONO_D);
615 fsi->chan = 1;
616 break;
617 case SH_FSI_FMT_PCM:
618 msg = "PCM";
619 data = CR_FMT(CR_PCM);
620 fsi->chan = 2;
621 break;
622 case SH_FSI_FMT_I2S:
623 msg = "I2S";
624 data = CR_FMT(CR_I2S);
625 fsi->chan = 2;
626 break;
627 case SH_FSI_FMT_TDM:
628 msg = "TDM";
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);
632 break;
633 case SH_FSI_FMT_TDM_DELAY:
634 msg = "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);
638 break;
639 default:
640 dev_err(dai->dev, "unknown format.\n");
641 return -EINVAL;
644 switch (fsi->chan) {
645 case 1:
646 fsi->fifo_max = 256;
647 break;
648 case 2:
649 fsi->fifo_max = 128;
650 break;
651 case 3:
652 case 4:
653 fsi->fifo_max = 64;
654 break;
655 case 5:
656 case 6:
657 case 7:
658 case 8:
659 fsi->fifo_max = 32;
660 break;
661 default:
662 dev_err(dai->dev, "channel size error.\n");
663 return -EINVAL;
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
673 if (is_master)
674 fsi_clk_ctrl(fsi, 1);
676 /* irq setting */
677 fsi_irq_init(fsi, is_play);
679 return ret;
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;
700 int ret = 0;
702 /* capture not supported */
703 if (!is_play)
704 return -ENODEV;
706 switch (cmd) {
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);
712 break;
713 case SNDRV_PCM_TRIGGER_STOP:
714 fsi_irq_disable(fsi, is_play);
715 fsi_stream_pop(fsi);
716 break;
719 return ret;
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 /************************************************************************
731 pcm ops
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,
740 .formats = FSI_FMTS,
741 .rates = FSI_RATES,
742 .rate_min = 8000,
743 .rate_max = 192000,
744 .channels_min = 1,
745 .channels_max = 2,
746 .buffer_bytes_max = 64 * 1024,
747 .period_bytes_min = 32,
748 .period_bytes_max = 8192,
749 .periods_min = 1,
750 .periods_max = 32,
751 .fifo_size = 256,
754 static int fsi_pcm_open(struct snd_pcm_substream *substream)
756 struct snd_pcm_runtime *runtime = substream->runtime;
757 int ret = 0;
759 snd_soc_set_runtime_hwparams(substream, &fsi_pcm_hardware);
761 ret = snd_pcm_hw_constraint_integer(runtime,
762 SNDRV_PCM_HW_PARAM_PERIODS);
764 return ret;
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;
784 long location;
786 location = (fsi->byte_offset - 1) - fsi_get_residue(fsi, is_play);
787 if (location < 0)
788 location = 0;
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 /************************************************************************
804 snd_soc_platform
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,
818 struct snd_pcm *pcm)
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(
825 pcm,
826 SNDRV_DMA_TYPE_CONTINUOUS,
827 snd_dma_continuous_data(GFP_KERNEL),
828 PREALLOC_BUFFER, PREALLOC_BUFFER_MAX);
831 /************************************************************************
834 alsa struct
837 ************************************************************************/
838 struct snd_soc_dai fsi_soc_dai[] = {
840 .name = "FSIA",
841 .id = 0,
842 .playback = {
843 .rates = FSI_RATES,
844 .formats = FSI_FMTS,
845 .channels_min = 1,
846 .channels_max = 8,
848 /* capture not supported */
849 .ops = &fsi_dai_ops,
852 .name = "FSIB",
853 .id = 1,
854 .playback = {
855 .rates = FSI_RATES,
856 .formats = FSI_FMTS,
857 .channels_min = 1,
858 .channels_max = 8,
860 /* capture not supported */
861 .ops = &fsi_dai_ops,
864 EXPORT_SYMBOL_GPL(fsi_soc_dai);
866 struct snd_soc_platform fsi_soc_platform = {
867 .name = "fsi-pcm",
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 /************************************************************************
877 platform function
880 ************************************************************************/
881 static int fsi_probe(struct platform_device *pdev)
883 struct resource *res;
884 char clk_name[8];
885 unsigned int irq;
886 int ret;
888 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
889 irq = platform_get_irq(pdev, 0);
890 if (!res || !irq) {
891 dev_err(&pdev->dev, "Not enough FSI platform resources.\n");
892 ret = -ENODEV;
893 goto exit;
896 master = kzalloc(sizeof(*master), GFP_KERNEL);
897 if (!master) {
898 dev_err(&pdev->dev, "Could not allocate master\n");
899 ret = -ENOMEM;
900 goto exit;
903 master->base = ioremap_nocache(res->start, resource_size(res));
904 if (!master->base) {
905 ret = -ENXIO;
906 dev_err(&pdev->dev, "Unable to ioremap FSI registers.\n");
907 goto exit_kfree;
910 master->irq = irq;
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();
919 if (ret < 0) {
920 dev_err(&pdev->dev, "cannot get dma api\n");
921 goto exit_iounmap;
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);
929 ret = -EIO;
930 goto exit_free_dma;
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);
939 if (ret) {
940 dev_err(&pdev->dev, "irq request err\n");
941 goto exit_free_dma;
944 ret = snd_soc_register_platform(&fsi_soc_platform);
945 if (ret < 0) {
946 dev_err(&pdev->dev, "cannot snd soc register\n");
947 goto exit_free_irq;
950 return snd_soc_register_dais(fsi_soc_dai, ARRAY_SIZE(fsi_soc_dai));
952 exit_free_irq:
953 free_irq(irq, master);
954 exit_free_dma:
955 fsi_free_dma_chan();
956 exit_iounmap:
957 iounmap(master->base);
958 exit_kfree:
959 kfree(master);
960 master = NULL;
961 exit:
962 return ret;
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);
972 fsi_free_dma_chan();
974 free_irq(master->irq, master);
976 iounmap(master->base);
977 kfree(master);
978 master = NULL;
979 return 0;
982 static struct platform_driver fsi_driver = {
983 .driver = {
984 .name = "sh_fsi",
986 .probe = fsi_probe,
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>");