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_len : data length
122 * xxx_width : data width
123 * xxx_offset : data offset
124 * xxx_num : number of data
132 struct snd_pcm_substream
*substream
;
147 struct fsi_master
*master
;
149 struct fsi_stream playback
;
150 struct fsi_stream capture
;
157 /* for suspend/resume */
178 struct fsi_priv fsia
;
179 struct fsi_priv fsib
;
180 struct fsi_core
*core
;
181 struct sh_fsi_platform_info
*info
;
184 /* for suspend/resume */
194 * basic read write function
197 static void __fsi_reg_write(u32 reg
, u32 data
)
199 /* valid data area is 24bit */
202 __raw_writel(data
, reg
);
205 static u32
__fsi_reg_read(u32 reg
)
207 return __raw_readl(reg
);
210 static void __fsi_reg_mask_set(u32 reg
, u32 mask
, u32 data
)
212 u32 val
= __fsi_reg_read(reg
);
217 __fsi_reg_write(reg
, val
);
220 #define fsi_reg_write(p, r, d)\
221 __fsi_reg_write((u32)(p->base + REG_##r), d)
223 #define fsi_reg_read(p, r)\
224 __fsi_reg_read((u32)(p->base + REG_##r))
226 #define fsi_reg_mask_set(p, r, m, d)\
227 __fsi_reg_mask_set((u32)(p->base + REG_##r), m, d)
229 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
230 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
231 static u32
_fsi_master_read(struct fsi_master
*master
, u32 reg
)
236 spin_lock_irqsave(&master
->lock
, flags
);
237 ret
= __fsi_reg_read((u32
)(master
->base
+ reg
));
238 spin_unlock_irqrestore(&master
->lock
, flags
);
243 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
244 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
245 static void _fsi_master_mask_set(struct fsi_master
*master
,
246 u32 reg
, u32 mask
, u32 data
)
250 spin_lock_irqsave(&master
->lock
, flags
);
251 __fsi_reg_mask_set((u32
)(master
->base
+ reg
), mask
, data
);
252 spin_unlock_irqrestore(&master
->lock
, flags
);
259 static struct fsi_master
*fsi_get_master(struct fsi_priv
*fsi
)
264 static int fsi_is_clk_master(struct fsi_priv
*fsi
)
266 return fsi
->clk_master
;
269 static int fsi_is_port_a(struct fsi_priv
*fsi
)
271 return fsi
->master
->base
== fsi
->base
;
274 static struct snd_soc_dai
*fsi_get_dai(struct snd_pcm_substream
*substream
)
276 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
281 static struct fsi_priv
*fsi_get_priv_frm_dai(struct snd_soc_dai
*dai
)
283 struct fsi_master
*master
= snd_soc_dai_get_drvdata(dai
);
286 return &master
->fsia
;
288 return &master
->fsib
;
291 static struct fsi_priv
*fsi_get_priv(struct snd_pcm_substream
*substream
)
293 return fsi_get_priv_frm_dai(fsi_get_dai(substream
));
296 static set_rate_func
fsi_get_info_set_rate(struct fsi_master
*master
)
301 return master
->info
->set_rate
;
304 static u32
fsi_get_info_flags(struct fsi_priv
*fsi
)
306 int is_porta
= fsi_is_port_a(fsi
);
307 struct fsi_master
*master
= fsi_get_master(fsi
);
312 return is_porta
? master
->info
->porta_flags
:
313 master
->info
->portb_flags
;
316 static inline int fsi_stream_is_play(int stream
)
318 return stream
== SNDRV_PCM_STREAM_PLAYBACK
;
321 static inline int fsi_is_play(struct snd_pcm_substream
*substream
)
323 return fsi_stream_is_play(substream
->stream
);
326 static inline struct fsi_stream
*fsi_get_stream(struct fsi_priv
*fsi
,
329 return is_play
? &fsi
->playback
: &fsi
->capture
;
332 static u32
fsi_get_port_shift(struct fsi_priv
*fsi
, int is_play
)
334 int is_porta
= fsi_is_port_a(fsi
);
338 shift
= is_play
? AO_SHIFT
: AI_SHIFT
;
340 shift
= is_play
? BO_SHIFT
: BI_SHIFT
;
345 static void fsi_stream_push(struct fsi_priv
*fsi
,
347 struct snd_pcm_substream
*substream
,
351 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
353 io
->substream
= substream
;
354 io
->buff_len
= buffer_len
;
356 io
->period_len
= period_len
;
358 io
->oerr_num
= -1; /* ignore 1st err */
359 io
->uerr_num
= -1; /* ignore 1st err */
362 static void fsi_stream_pop(struct fsi_priv
*fsi
, int is_play
)
364 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
365 struct snd_soc_dai
*dai
= fsi_get_dai(io
->substream
);
368 if (io
->oerr_num
> 0)
369 dev_err(dai
->dev
, "over_run = %d\n", io
->oerr_num
);
371 if (io
->uerr_num
> 0)
372 dev_err(dai
->dev
, "under_run = %d\n", io
->uerr_num
);
374 io
->substream
= NULL
;
383 static int fsi_get_fifo_data_num(struct fsi_priv
*fsi
, int is_play
)
389 fsi_reg_read(fsi
, DOFF_ST
) :
390 fsi_reg_read(fsi
, DIFF_ST
);
392 data_num
= 0x1ff & (status
>> 8);
393 data_num
*= fsi
->chan_num
;
398 static int fsi_len2num(int len
, int width
)
403 #define fsi_num2offset(a, b) fsi_num2len(a, b)
404 static int fsi_num2len(int num
, int width
)
409 static int fsi_get_frame_width(struct fsi_priv
*fsi
, int is_play
)
411 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
412 struct snd_pcm_substream
*substream
= io
->substream
;
413 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
415 return frames_to_bytes(runtime
, 1) / fsi
->chan_num
;
418 static void fsi_count_fifo_err(struct fsi_priv
*fsi
)
420 u32 ostatus
= fsi_reg_read(fsi
, DOFF_ST
);
421 u32 istatus
= fsi_reg_read(fsi
, DIFF_ST
);
423 if (ostatus
& ERR_OVER
)
424 fsi
->playback
.oerr_num
++;
426 if (ostatus
& ERR_UNDER
)
427 fsi
->playback
.uerr_num
++;
429 if (istatus
& ERR_OVER
)
430 fsi
->capture
.oerr_num
++;
432 if (istatus
& ERR_UNDER
)
433 fsi
->capture
.uerr_num
++;
435 fsi_reg_write(fsi
, DOFF_ST
, 0);
436 fsi_reg_write(fsi
, DIFF_ST
, 0);
443 static u8
*fsi_dma_get_area(struct fsi_priv
*fsi
, int stream
)
445 int is_play
= fsi_stream_is_play(stream
);
446 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
448 return io
->substream
->runtime
->dma_area
+ io
->buff_offset
;
451 static void fsi_dma_soft_push16(struct fsi_priv
*fsi
, int num
)
456 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
458 for (i
= 0; i
< num
; i
++)
459 fsi_reg_write(fsi
, DODT
, ((u32
)*(start
+ i
) << 8));
462 static void fsi_dma_soft_pop16(struct fsi_priv
*fsi
, int num
)
467 start
= (u16
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
470 for (i
= 0; i
< num
; i
++)
471 *(start
+ i
) = (u16
)(fsi_reg_read(fsi
, DIDT
) >> 8);
474 static void fsi_dma_soft_push32(struct fsi_priv
*fsi
, int num
)
479 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
482 for (i
= 0; i
< num
; i
++)
483 fsi_reg_write(fsi
, DODT
, *(start
+ i
));
486 static void fsi_dma_soft_pop32(struct fsi_priv
*fsi
, int num
)
491 start
= (u32
*)fsi_dma_get_area(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
493 for (i
= 0; i
< num
; i
++)
494 *(start
+ i
) = fsi_reg_read(fsi
, DIDT
);
501 static void fsi_irq_enable(struct fsi_priv
*fsi
, int is_play
)
503 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
504 struct fsi_master
*master
= fsi_get_master(fsi
);
506 fsi_core_mask_set(master
, imsk
, data
, data
);
507 fsi_core_mask_set(master
, iemsk
, data
, data
);
510 static void fsi_irq_disable(struct fsi_priv
*fsi
, int is_play
)
512 u32 data
= AB_IO(1, fsi_get_port_shift(fsi
, is_play
));
513 struct fsi_master
*master
= fsi_get_master(fsi
);
515 fsi_core_mask_set(master
, imsk
, data
, 0);
516 fsi_core_mask_set(master
, iemsk
, data
, 0);
519 static u32
fsi_irq_get_status(struct fsi_master
*master
)
521 return fsi_core_read(master
, int_st
);
524 static void fsi_irq_clear_status(struct fsi_priv
*fsi
)
527 struct fsi_master
*master
= fsi_get_master(fsi
);
529 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 0));
530 data
|= AB_IO(1, fsi_get_port_shift(fsi
, 1));
532 /* clear interrupt factor */
533 fsi_core_mask_set(master
, int_st
, data
, 0);
537 * SPDIF master clock function
539 * These functions are used later FSI2
541 static void fsi_spdif_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
543 struct fsi_master
*master
= fsi_get_master(fsi
);
546 if (master
->core
->ver
< 2) {
547 pr_err("fsi: register access err (%s)\n", __func__
);
552 val
= enable
? mask
: 0;
555 fsi_core_mask_set(master
, a_mclk
, mask
, val
) :
556 fsi_core_mask_set(master
, b_mclk
, mask
, val
);
562 #define fsi_module_init(m, d) __fsi_module_clk_ctrl(m, d, 1)
563 #define fsi_module_kill(m, d) __fsi_module_clk_ctrl(m, d, 0)
564 static void __fsi_module_clk_ctrl(struct fsi_master
*master
,
568 pm_runtime_get_sync(dev
);
572 fsi_master_mask_set(master
, SOFT_RST
, FSISR
, FSISR
);
573 fsi_master_mask_set(master
, SOFT_RST
, PASR
| PBSR
, 0);
575 /* clear all registers */
576 fsi_master_mask_set(master
, SOFT_RST
, FSISR
, 0);
579 pm_runtime_put_sync(dev
);
582 #define fsi_port_start(f) __fsi_port_clk_ctrl(f, 1)
583 #define fsi_port_stop(f) __fsi_port_clk_ctrl(f, 0)
584 static void __fsi_port_clk_ctrl(struct fsi_priv
*fsi
, int enable
)
586 struct fsi_master
*master
= fsi_get_master(fsi
);
587 u32 soft
= fsi_is_port_a(fsi
) ? PASR
: PBSR
;
588 u32 clk
= fsi_is_port_a(fsi
) ? CRA
: CRB
;
589 int is_master
= fsi_is_clk_master(fsi
);
591 fsi_master_mask_set(master
, SOFT_RST
, soft
, (enable
) ? soft
: 0);
593 fsi_master_mask_set(master
, CLK_RST
, clk
, (enable
) ? clk
: 0);
599 static void fsi_fifo_init(struct fsi_priv
*fsi
,
601 struct snd_soc_dai
*dai
)
603 struct fsi_master
*master
= fsi_get_master(fsi
);
604 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
607 /* get on-chip RAM capacity */
608 shift
= fsi_master_read(master
, FIFO_SZ
);
609 shift
>>= fsi_get_port_shift(fsi
, is_play
);
610 shift
&= FIFO_SZ_MASK
;
611 io
->fifo_max_num
= 256 << shift
;
612 dev_dbg(dai
->dev
, "fifo = %d words\n", io
->fifo_max_num
);
615 * The maximum number of sample data varies depending
616 * on the number of channels selected for the format.
618 * FIFOs are used in 4-channel units in 3-channel mode
619 * and in 8-channel units in 5- to 7-channel mode
620 * meaning that more FIFOs than the required size of DPRAM
623 * ex) if 256 words of DP-RAM is connected
624 * 1 channel: 256 (256 x 1 = 256)
625 * 2 channels: 128 (128 x 2 = 256)
626 * 3 channels: 64 ( 64 x 3 = 192)
627 * 4 channels: 64 ( 64 x 4 = 256)
628 * 5 channels: 32 ( 32 x 5 = 160)
629 * 6 channels: 32 ( 32 x 6 = 192)
630 * 7 channels: 32 ( 32 x 7 = 224)
631 * 8 channels: 32 ( 32 x 8 = 256)
633 for (i
= 1; i
< fsi
->chan_num
; i
<<= 1)
634 io
->fifo_max_num
>>= 1;
635 dev_dbg(dai
->dev
, "%d channel %d store\n",
636 fsi
->chan_num
, io
->fifo_max_num
);
639 * set interrupt generation factor
643 fsi_reg_write(fsi
, DOFF_CTL
, IRQ_HALF
);
644 fsi_reg_mask_set(fsi
, DOFF_CTL
, FIFO_CLR
, FIFO_CLR
);
646 fsi_reg_write(fsi
, DIFF_CTL
, IRQ_HALF
);
647 fsi_reg_mask_set(fsi
, DIFF_CTL
, FIFO_CLR
, FIFO_CLR
);
651 static int fsi_fifo_data_ctrl(struct fsi_priv
*fsi
, int stream
)
653 struct snd_pcm_runtime
*runtime
;
654 struct snd_pcm_substream
*substream
= NULL
;
655 int is_play
= fsi_stream_is_play(stream
);
656 struct fsi_stream
*io
= fsi_get_stream(fsi
, is_play
);
657 int data_residue_num
;
662 void (*fn
)(struct fsi_priv
*fsi
, int size
);
666 !io
->substream
->runtime
)
670 substream
= io
->substream
;
671 runtime
= substream
->runtime
;
673 /* FSI FIFO has limit.
674 * So, this driver can not send periods data at a time
676 if (io
->buff_offset
>=
677 fsi_num2offset(io
->period_num
+ 1, io
->period_len
)) {
680 io
->period_num
= (io
->period_num
+ 1) % runtime
->periods
;
682 if (0 == io
->period_num
)
686 /* get 1 channel data width */
687 ch_width
= fsi_get_frame_width(fsi
, is_play
);
689 /* get residue data number of alsa */
690 data_residue_num
= fsi_len2num(io
->buff_len
- io
->buff_offset
,
697 * data_num_max : number of FSI fifo free space
698 * data_num : number of ALSA residue data
700 data_num_max
= io
->fifo_max_num
* fsi
->chan_num
;
701 data_num_max
-= fsi_get_fifo_data_num(fsi
, is_play
);
703 data_num
= data_residue_num
;
707 fn
= fsi_dma_soft_push16
;
710 fn
= fsi_dma_soft_push32
;
719 * data_num_max : number of ALSA free space
720 * data_num : number of data in FSI fifo
722 data_num_max
= data_residue_num
;
723 data_num
= fsi_get_fifo_data_num(fsi
, is_play
);
727 fn
= fsi_dma_soft_pop16
;
730 fn
= fsi_dma_soft_pop32
;
737 data_num
= min(data_num
, data_num_max
);
741 /* update buff_offset */
742 io
->buff_offset
+= fsi_num2offset(data_num
, ch_width
);
745 snd_pcm_period_elapsed(substream
);
750 static int fsi_data_pop(struct fsi_priv
*fsi
)
752 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_CAPTURE
);
755 static int fsi_data_push(struct fsi_priv
*fsi
)
757 return fsi_fifo_data_ctrl(fsi
, SNDRV_PCM_STREAM_PLAYBACK
);
760 static irqreturn_t
fsi_interrupt(int irq
, void *data
)
762 struct fsi_master
*master
= data
;
763 u32 int_st
= fsi_irq_get_status(master
);
765 /* clear irq status */
766 fsi_master_mask_set(master
, SOFT_RST
, IR
, 0);
767 fsi_master_mask_set(master
, SOFT_RST
, IR
, IR
);
769 if (int_st
& AB_IO(1, AO_SHIFT
))
770 fsi_data_push(&master
->fsia
);
771 if (int_st
& AB_IO(1, BO_SHIFT
))
772 fsi_data_push(&master
->fsib
);
773 if (int_st
& AB_IO(1, AI_SHIFT
))
774 fsi_data_pop(&master
->fsia
);
775 if (int_st
& AB_IO(1, BI_SHIFT
))
776 fsi_data_pop(&master
->fsib
);
778 fsi_count_fifo_err(&master
->fsia
);
779 fsi_count_fifo_err(&master
->fsib
);
781 fsi_irq_clear_status(&master
->fsia
);
782 fsi_irq_clear_status(&master
->fsib
);
791 static int fsi_dai_startup(struct snd_pcm_substream
*substream
,
792 struct snd_soc_dai
*dai
)
794 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
795 u32 flags
= fsi_get_info_flags(fsi
);
797 int is_play
= fsi_is_play(substream
);
799 pm_runtime_get_sync(dai
->dev
);
802 /* clock inversion (CKG2) */
804 if (SH_FSI_LRM_INV
& flags
)
806 if (SH_FSI_BRM_INV
& flags
)
808 if (SH_FSI_LRS_INV
& flags
)
810 if (SH_FSI_BRS_INV
& flags
)
813 fsi_reg_write(fsi
, CKG2
, data
);
816 fsi_irq_disable(fsi
, is_play
);
817 fsi_irq_clear_status(fsi
);
820 fsi_fifo_init(fsi
, is_play
, dai
);
825 static void fsi_dai_shutdown(struct snd_pcm_substream
*substream
,
826 struct snd_soc_dai
*dai
)
828 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
829 int is_play
= fsi_is_play(substream
);
830 struct fsi_master
*master
= fsi_get_master(fsi
);
831 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
833 fsi_irq_disable(fsi
, is_play
);
835 if (fsi_is_clk_master(fsi
))
836 set_rate(dai
->dev
, fsi_is_port_a(fsi
), fsi
->rate
, 0);
840 pm_runtime_put_sync(dai
->dev
);
843 static int fsi_dai_trigger(struct snd_pcm_substream
*substream
, int cmd
,
844 struct snd_soc_dai
*dai
)
846 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
847 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
848 int is_play
= fsi_is_play(substream
);
852 case SNDRV_PCM_TRIGGER_START
:
853 fsi_stream_push(fsi
, is_play
, substream
,
854 frames_to_bytes(runtime
, runtime
->buffer_size
),
855 frames_to_bytes(runtime
, runtime
->period_size
));
856 ret
= is_play
? fsi_data_push(fsi
) : fsi_data_pop(fsi
);
857 fsi_irq_enable(fsi
, is_play
);
860 case SNDRV_PCM_TRIGGER_STOP
:
862 fsi_irq_disable(fsi
, is_play
);
863 fsi_stream_pop(fsi
, is_play
);
870 static int fsi_set_fmt_dai(struct fsi_priv
*fsi
, unsigned int fmt
)
874 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
875 case SND_SOC_DAIFMT_I2S
:
879 case SND_SOC_DAIFMT_LEFT_J
:
887 fsi_reg_write(fsi
, DO_FMT
, data
);
888 fsi_reg_write(fsi
, DI_FMT
, data
);
893 static int fsi_set_fmt_spdif(struct fsi_priv
*fsi
)
895 struct fsi_master
*master
= fsi_get_master(fsi
);
898 if (master
->core
->ver
< 2)
901 data
= CR_BWS_16
| CR_DTMD_SPDIF_PCM
| CR_PCM
;
903 fsi_spdif_clk_ctrl(fsi
, 1);
904 fsi_reg_mask_set(fsi
, OUT_SEL
, DMMD
, DMMD
);
906 fsi_reg_write(fsi
, DO_FMT
, data
);
907 fsi_reg_write(fsi
, DI_FMT
, data
);
912 static int fsi_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
914 struct fsi_priv
*fsi
= fsi_get_priv_frm_dai(dai
);
915 struct fsi_master
*master
= fsi_get_master(fsi
);
916 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
917 u32 flags
= fsi_get_info_flags(fsi
);
921 pm_runtime_get_sync(dai
->dev
);
923 /* set master/slave audio interface */
924 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
925 case SND_SOC_DAIFMT_CBM_CFM
:
929 case SND_SOC_DAIFMT_CBS_CFS
:
936 if (fsi_is_clk_master(fsi
) && !set_rate
) {
937 dev_err(dai
->dev
, "platform doesn't have set_rate\n");
942 fsi_reg_mask_set(fsi
, CKG1
, (DIMD
| DOMD
), data
);
945 switch (flags
& SH_FSI_FMT_MASK
) {
947 ret
= fsi_set_fmt_dai(fsi
, fmt
& SND_SOC_DAIFMT_FORMAT_MASK
);
949 case SH_FSI_FMT_SPDIF
:
950 ret
= fsi_set_fmt_spdif(fsi
);
957 pm_runtime_put_sync(dai
->dev
);
962 static int fsi_dai_hw_params(struct snd_pcm_substream
*substream
,
963 struct snd_pcm_hw_params
*params
,
964 struct snd_soc_dai
*dai
)
966 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
967 struct fsi_master
*master
= fsi_get_master(fsi
);
968 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
969 int fsi_ver
= master
->core
->ver
;
970 long rate
= params_rate(params
);
973 if (!fsi_is_clk_master(fsi
))
976 ret
= set_rate(dai
->dev
, fsi_is_port_a(fsi
), rate
, 1);
977 if (ret
< 0) /* error */
984 switch (ret
& SH_FSI_ACKMD_MASK
) {
987 case SH_FSI_ACKMD_512
:
990 case SH_FSI_ACKMD_256
:
993 case SH_FSI_ACKMD_128
:
996 case SH_FSI_ACKMD_64
:
999 case SH_FSI_ACKMD_32
:
1001 dev_err(dai
->dev
, "unsupported ACKMD\n");
1003 data
|= (0x4 << 12);
1007 switch (ret
& SH_FSI_BPFMD_MASK
) {
1010 case SH_FSI_BPFMD_32
:
1013 case SH_FSI_BPFMD_64
:
1016 case SH_FSI_BPFMD_128
:
1019 case SH_FSI_BPFMD_256
:
1022 case SH_FSI_BPFMD_512
:
1025 case SH_FSI_BPFMD_16
:
1027 dev_err(dai
->dev
, "unsupported ACKMD\n");
1033 fsi_reg_mask_set(fsi
, CKG1
, (ACKMD_MASK
| BPFMD_MASK
) , data
);
1042 static struct snd_soc_dai_ops fsi_dai_ops
= {
1043 .startup
= fsi_dai_startup
,
1044 .shutdown
= fsi_dai_shutdown
,
1045 .trigger
= fsi_dai_trigger
,
1046 .set_fmt
= fsi_dai_set_fmt
,
1047 .hw_params
= fsi_dai_hw_params
,
1054 static struct snd_pcm_hardware fsi_pcm_hardware
= {
1055 .info
= SNDRV_PCM_INFO_INTERLEAVED
|
1056 SNDRV_PCM_INFO_MMAP
|
1057 SNDRV_PCM_INFO_MMAP_VALID
|
1058 SNDRV_PCM_INFO_PAUSE
,
1059 .formats
= FSI_FMTS
,
1065 .buffer_bytes_max
= 64 * 1024,
1066 .period_bytes_min
= 32,
1067 .period_bytes_max
= 8192,
1073 static int fsi_pcm_open(struct snd_pcm_substream
*substream
)
1075 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1078 snd_soc_set_runtime_hwparams(substream
, &fsi_pcm_hardware
);
1080 ret
= snd_pcm_hw_constraint_integer(runtime
,
1081 SNDRV_PCM_HW_PARAM_PERIODS
);
1086 static int fsi_hw_params(struct snd_pcm_substream
*substream
,
1087 struct snd_pcm_hw_params
*hw_params
)
1089 return snd_pcm_lib_malloc_pages(substream
,
1090 params_buffer_bytes(hw_params
));
1093 static int fsi_hw_free(struct snd_pcm_substream
*substream
)
1095 return snd_pcm_lib_free_pages(substream
);
1098 static snd_pcm_uframes_t
fsi_pointer(struct snd_pcm_substream
*substream
)
1100 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
1101 struct fsi_priv
*fsi
= fsi_get_priv(substream
);
1102 struct fsi_stream
*io
= fsi_get_stream(fsi
, fsi_is_play(substream
));
1105 location
= (io
->buff_offset
- 1);
1109 return bytes_to_frames(runtime
, location
);
1112 static struct snd_pcm_ops fsi_pcm_ops
= {
1113 .open
= fsi_pcm_open
,
1114 .ioctl
= snd_pcm_lib_ioctl
,
1115 .hw_params
= fsi_hw_params
,
1116 .hw_free
= fsi_hw_free
,
1117 .pointer
= fsi_pointer
,
1124 #define PREALLOC_BUFFER (32 * 1024)
1125 #define PREALLOC_BUFFER_MAX (32 * 1024)
1127 static void fsi_pcm_free(struct snd_pcm
*pcm
)
1129 snd_pcm_lib_preallocate_free_for_all(pcm
);
1132 static int fsi_pcm_new(struct snd_card
*card
,
1133 struct snd_soc_dai
*dai
,
1134 struct snd_pcm
*pcm
)
1137 * dont use SNDRV_DMA_TYPE_DEV, since it will oops the SH kernel
1138 * in MMAP mode (i.e. aplay -M)
1140 return snd_pcm_lib_preallocate_pages_for_all(
1142 SNDRV_DMA_TYPE_CONTINUOUS
,
1143 snd_dma_continuous_data(GFP_KERNEL
),
1144 PREALLOC_BUFFER
, PREALLOC_BUFFER_MAX
);
1151 static struct snd_soc_dai_driver fsi_soc_dai
[] = {
1156 .formats
= FSI_FMTS
,
1162 .formats
= FSI_FMTS
,
1166 .ops
= &fsi_dai_ops
,
1172 .formats
= FSI_FMTS
,
1178 .formats
= FSI_FMTS
,
1182 .ops
= &fsi_dai_ops
,
1186 static struct snd_soc_platform_driver fsi_soc_platform
= {
1187 .ops
= &fsi_pcm_ops
,
1188 .pcm_new
= fsi_pcm_new
,
1189 .pcm_free
= fsi_pcm_free
,
1196 static int fsi_probe(struct platform_device
*pdev
)
1198 struct fsi_master
*master
;
1199 const struct platform_device_id
*id_entry
;
1200 struct resource
*res
;
1204 id_entry
= pdev
->id_entry
;
1206 dev_err(&pdev
->dev
, "unknown fsi device\n");
1210 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1211 irq
= platform_get_irq(pdev
, 0);
1212 if (!res
|| (int)irq
<= 0) {
1213 dev_err(&pdev
->dev
, "Not enough FSI platform resources.\n");
1218 master
= kzalloc(sizeof(*master
), GFP_KERNEL
);
1220 dev_err(&pdev
->dev
, "Could not allocate master\n");
1225 master
->base
= ioremap_nocache(res
->start
, resource_size(res
));
1226 if (!master
->base
) {
1228 dev_err(&pdev
->dev
, "Unable to ioremap FSI registers.\n");
1232 /* master setting */
1234 master
->info
= pdev
->dev
.platform_data
;
1235 master
->core
= (struct fsi_core
*)id_entry
->driver_data
;
1236 spin_lock_init(&master
->lock
);
1239 master
->fsia
.base
= master
->base
;
1240 master
->fsia
.master
= master
;
1243 master
->fsib
.base
= master
->base
+ 0x40;
1244 master
->fsib
.master
= master
;
1246 pm_runtime_enable(&pdev
->dev
);
1247 dev_set_drvdata(&pdev
->dev
, master
);
1249 fsi_module_init(master
, &pdev
->dev
);
1251 ret
= request_irq(irq
, &fsi_interrupt
, IRQF_DISABLED
,
1252 id_entry
->name
, master
);
1254 dev_err(&pdev
->dev
, "irq request err\n");
1258 ret
= snd_soc_register_platform(&pdev
->dev
, &fsi_soc_platform
);
1260 dev_err(&pdev
->dev
, "cannot snd soc register\n");
1264 ret
= snd_soc_register_dais(&pdev
->dev
, fsi_soc_dai
,
1265 ARRAY_SIZE(fsi_soc_dai
));
1267 dev_err(&pdev
->dev
, "cannot snd dai register\n");
1274 snd_soc_unregister_platform(&pdev
->dev
);
1276 free_irq(irq
, master
);
1278 iounmap(master
->base
);
1279 pm_runtime_disable(&pdev
->dev
);
1287 static int fsi_remove(struct platform_device
*pdev
)
1289 struct fsi_master
*master
;
1291 master
= dev_get_drvdata(&pdev
->dev
);
1293 fsi_module_kill(master
, &pdev
->dev
);
1295 free_irq(master
->irq
, master
);
1296 pm_runtime_disable(&pdev
->dev
);
1298 snd_soc_unregister_dais(&pdev
->dev
, ARRAY_SIZE(fsi_soc_dai
));
1299 snd_soc_unregister_platform(&pdev
->dev
);
1301 iounmap(master
->base
);
1307 static void __fsi_suspend(struct fsi_priv
*fsi
,
1309 set_rate_func set_rate
)
1311 fsi
->saved_do_fmt
= fsi_reg_read(fsi
, DO_FMT
);
1312 fsi
->saved_di_fmt
= fsi_reg_read(fsi
, DI_FMT
);
1313 fsi
->saved_ckg1
= fsi_reg_read(fsi
, CKG1
);
1314 fsi
->saved_ckg2
= fsi_reg_read(fsi
, CKG2
);
1315 fsi
->saved_out_sel
= fsi_reg_read(fsi
, OUT_SEL
);
1317 if (fsi_is_clk_master(fsi
))
1318 set_rate(dev
, fsi_is_port_a(fsi
), fsi
->rate
, 0);
1321 static void __fsi_resume(struct fsi_priv
*fsi
,
1323 set_rate_func set_rate
)
1325 fsi_reg_write(fsi
, DO_FMT
, fsi
->saved_do_fmt
);
1326 fsi_reg_write(fsi
, DI_FMT
, fsi
->saved_di_fmt
);
1327 fsi_reg_write(fsi
, CKG1
, fsi
->saved_ckg1
);
1328 fsi_reg_write(fsi
, CKG2
, fsi
->saved_ckg2
);
1329 fsi_reg_write(fsi
, OUT_SEL
, fsi
->saved_out_sel
);
1331 if (fsi_is_clk_master(fsi
))
1332 set_rate(dev
, fsi_is_port_a(fsi
), fsi
->rate
, 1);
1335 static int fsi_suspend(struct device
*dev
)
1337 struct fsi_master
*master
= dev_get_drvdata(dev
);
1338 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
1340 pm_runtime_get_sync(dev
);
1342 __fsi_suspend(&master
->fsia
, dev
, set_rate
);
1343 __fsi_suspend(&master
->fsib
, dev
, set_rate
);
1345 master
->saved_a_mclk
= fsi_core_read(master
, a_mclk
);
1346 master
->saved_b_mclk
= fsi_core_read(master
, b_mclk
);
1347 master
->saved_iemsk
= fsi_core_read(master
, iemsk
);
1348 master
->saved_imsk
= fsi_core_read(master
, imsk
);
1349 master
->saved_clk_rst
= fsi_master_read(master
, CLK_RST
);
1350 master
->saved_soft_rst
= fsi_master_read(master
, SOFT_RST
);
1352 fsi_module_kill(master
, dev
);
1354 pm_runtime_put_sync(dev
);
1359 static int fsi_resume(struct device
*dev
)
1361 struct fsi_master
*master
= dev_get_drvdata(dev
);
1362 set_rate_func set_rate
= fsi_get_info_set_rate(master
);
1364 pm_runtime_get_sync(dev
);
1366 fsi_module_init(master
, dev
);
1368 fsi_master_mask_set(master
, SOFT_RST
, 0xffff, master
->saved_soft_rst
);
1369 fsi_master_mask_set(master
, CLK_RST
, 0xffff, master
->saved_clk_rst
);
1370 fsi_core_mask_set(master
, a_mclk
, 0xffff, master
->saved_a_mclk
);
1371 fsi_core_mask_set(master
, b_mclk
, 0xffff, master
->saved_b_mclk
);
1372 fsi_core_mask_set(master
, iemsk
, 0xffff, master
->saved_iemsk
);
1373 fsi_core_mask_set(master
, imsk
, 0xffff, master
->saved_imsk
);
1375 __fsi_resume(&master
->fsia
, dev
, set_rate
);
1376 __fsi_resume(&master
->fsib
, dev
, set_rate
);
1378 pm_runtime_put_sync(dev
);
1383 static int fsi_runtime_nop(struct device
*dev
)
1385 /* Runtime PM callback shared between ->runtime_suspend()
1386 * and ->runtime_resume(). Simply returns success.
1388 * This driver re-initializes all registers after
1389 * pm_runtime_get_sync() anyway so there is no need
1390 * to save and restore registers here.
1395 static struct dev_pm_ops fsi_pm_ops
= {
1396 .suspend
= fsi_suspend
,
1397 .resume
= fsi_resume
,
1398 .runtime_suspend
= fsi_runtime_nop
,
1399 .runtime_resume
= fsi_runtime_nop
,
1402 static struct fsi_core fsi1_core
= {
1411 static struct fsi_core fsi2_core
= {
1415 .int_st
= CPU_INT_ST
,
1418 .a_mclk
= A_MST_CTLR
,
1419 .b_mclk
= B_MST_CTLR
,
1422 static struct platform_device_id fsi_id_table
[] = {
1423 { "sh_fsi", (kernel_ulong_t
)&fsi1_core
},
1424 { "sh_fsi2", (kernel_ulong_t
)&fsi2_core
},
1427 MODULE_DEVICE_TABLE(platform
, fsi_id_table
);
1429 static struct platform_driver fsi_driver
= {
1431 .name
= "fsi-pcm-audio",
1435 .remove
= fsi_remove
,
1436 .id_table
= fsi_id_table
,
1439 static int __init
fsi_mobile_init(void)
1441 return platform_driver_register(&fsi_driver
);
1444 static void __exit
fsi_mobile_exit(void)
1446 platform_driver_unregister(&fsi_driver
);
1449 module_init(fsi_mobile_init
);
1450 module_exit(fsi_mobile_exit
);
1452 MODULE_LICENSE("GPL");
1453 MODULE_DESCRIPTION("SuperH onchip FSI audio driver");
1454 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
1455 MODULE_ALIAS("platform:fsi-pcm-audio");