21 #include <linux/slab.h>
22 #include <linux/module.h>
28 #define REG_DO_FMT 0x0000
29 #define REG_DOFF_CTL 0x0004
30 #define REG_DOFF_ST 0x0008
31 #define REG_DI_FMT 0x000C
32 #define REG_DIFF_CTL 0x0010
33 #define REG_DIFF_ST 0x0014
34 #define REG_CKG1 0x0018
35 #define REG_CKG2 0x001C
36 #define REG_DIDT 0x0020
37 #define REG_DODT 0x0024
38 #define REG_MUTE_ST 0x0028
39 #define REG_OUT_DMAC 0x002C
40 #define REG_OUT_SEL 0x0030
41 #define REG_IN_DMAC 0x0038
44 #define MST_CLK_RST 0x0210
45 #define MST_SOFT_RST 0x0214
46 #define MST_FIFO_SZ 0x0218
49 #define A_MST_CTLR 0x0180
50 #define B_MST_CTLR 0x01A0
51 #define CPU_INT_ST 0x01F4
52 #define CPU_IEMSK 0x01F8
53 #define CPU_IMSK 0x01FC
60 #define CR_BWS_MASK (0x3 << 20)
61 #define CR_BWS_24 (0x0 << 20)
62 #define CR_BWS_16 (0x1 << 20)
63 #define CR_BWS_20 (0x2 << 20)
65 #define CR_DTMD_PCM (0x0 << 8)
66 #define CR_DTMD_SPDIF_PCM (0x1 << 8)
67 #define CR_DTMD_SPDIF_STREAM (0x2 << 8)
69 #define CR_MONO (0x0 << 4)
70 #define CR_MONO_D (0x1 << 4)
71 #define CR_PCM (0x2 << 4)
72 #define CR_I2S (0x3 << 4)
73 #define CR_TDM (0x4 << 4)
74 #define CR_TDM_D (0x5 << 4)
78 #define VDMD_MASK (0x3 << 4)
79 #define VDMD_FRONT (0x0 << 4)
80 #define VDMD_BACK (0x1 << 4)
81 #define VDMD_STREAM (0x2 << 4)
83 #define DMA_ON (0x1 << 0)
87 #define IRQ_HALF 0x00100000
88 #define FIFO_CLR 0x00000001
91 #define ERR_OVER 0x00000010
92 #define ERR_UNDER 0x00000001
93 #define ST_ERR (ERR_OVER | ERR_UNDER)
96 #define ACKMD_MASK 0x00007000
97 #define BPFMD_MASK 0x00000700
114 #define AB_IO(param, shift) (param << shift)
117 #define PBSR (1 << 12)
118 #define PASR (1 << 8)
120 #define FSISR (1 << 0)
123 #define DMMD (1 << 4)
127 #define FIFO_SZ_MASK 0x7
129 #define FSI_RATES SNDRV_PCM_RATE_8000_96000
131 #define FSI_FMTS (SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S16_LE)
144 #define SHIFT_16DATA 0
145 #define SHIFT_24DATA 4
147 #define PACKAGE_24BITBUS_BACK 0
148 #define PACKAGE_24BITBUS_FRONT 1
149 #define PACKAGE_16BITBUS_STREAM 2
151 #define BUSOP_SET(s, a) ((a) << SHIFT_ ## s ## DATA)
152 #define BUSOP_GET(s, a) (((a) >> SHIFT_ ## s ## DATA) & 0xF)
257 #define fsi_stream_handler_call(io, func, args...) \
259 !((io)->handler->func) ? 0 : \
260 (io)->handler->func(args))
302 u32 val = __fsi_reg_read(reg);
307 __fsi_reg_write(reg, val);
310 #define fsi_reg_write(p, r, d)\
311 __fsi_reg_write((p->base + REG_##r), d)
313 #define fsi_reg_read(p, r)\
314 __fsi_reg_read((p->base + REG_##r))
316 #define fsi_reg_mask_set(p, r, m, d)\
317 __fsi_reg_mask_set((p->base + REG_##r), m, d)
319 #define fsi_master_read(p, r) _fsi_master_read(p, MST_##r)
320 #define fsi_core_read(p, r) _fsi_master_read(p, p->core->r)
327 ret = __fsi_reg_read(master->
base + reg);
328 spin_unlock_irqrestore(&master->
lock, flags);
333 #define fsi_master_mask_set(p, r, m, d) _fsi_master_mask_set(p, MST_##r, m, d)
334 #define fsi_core_mask_set(p, r, m, d) _fsi_master_mask_set(p, p->core->r, m, d)
335 static void _fsi_master_mask_set(
struct fsi_master *master,
341 __fsi_reg_mask_set(master->
base + reg, mask, data);
342 spin_unlock_irqrestore(&master->
lock, flags);
348 static int fsi_version(
struct fsi_master *master)
350 return master->
core->ver;
358 static int fsi_is_clk_master(
struct fsi_priv *fsi)
363 static int fsi_is_port_a(
struct fsi_priv *fsi)
368 static int fsi_is_spdif(
struct fsi_priv *fsi)
387 struct fsi_master *master = snd_soc_dai_get_drvdata(dai);
390 return &master->
fsia;
392 return &master->
fsib;
397 return fsi_get_priv_frm_dai(fsi_get_dai(substream));
405 return fsi->
info->set_rate;
408 static u32 fsi_get_info_flags(
struct fsi_priv *fsi)
413 return fsi->
info->flags;
418 int is_play = fsi_stream_is_play(fsi, io);
419 int is_porta = fsi_is_port_a(fsi);
430 static int fsi_frame2sample(
struct fsi_priv *fsi,
int frames)
435 static int fsi_sample2frame(
struct fsi_priv *fsi,
int samples)
440 static int fsi_get_current_fifo_samples(
struct fsi_priv *fsi,
443 int is_play = fsi_stream_is_play(fsi, io);
449 fsi_reg_read(fsi, DIFF_ST);
451 frames = 0x1ff & (status >> 8);
453 return fsi_frame2sample(fsi, frames);
456 static void fsi_count_fifo_err(
struct fsi_priv *fsi)
467 if (istatus & ERR_OVER)
470 if (istatus & ERR_UNDER)
480 static inline int fsi_stream_is_play(
struct fsi_priv *fsi,
492 static int fsi_stream_is_working(
struct fsi_priv *fsi,
495 struct fsi_master *master = fsi_get_master(fsi);
501 spin_unlock_irqrestore(&master->
lock, flags);
511 static void fsi_stream_init(
struct fsi_priv *fsi,
516 struct fsi_master *master = fsi_get_master(fsi);
530 spin_unlock_irqrestore(&master->
lock, flags);
536 struct fsi_master *master = fsi_get_master(fsi);
557 spin_unlock_irqrestore(&master->
lock, flags);
560 static int fsi_stream_transfer(
struct fsi_stream *io)
562 struct fsi_priv *fsi = fsi_stream_to_priv(io);
569 #define fsi_stream_start(fsi, io)\
570 fsi_stream_handler_call(io, start_stop, fsi, io, 1)
572 #define fsi_stream_stop(fsi, io)\
573 fsi_stream_handler_call(io, start_stop, fsi, io, 0)
575 static int fsi_stream_probe(
struct fsi_priv *fsi,
struct device *dev)
594 static int fsi_stream_remove(
struct fsi_priv *fsi)
619 struct fsi_master *master = fsi_get_master(fsi);
620 int is_play = fsi_stream_is_play(fsi, io);
623 if (fsi_version(master) >= 2) {
633 dev_dbg(dev,
"24bit bus / package in front\n");
638 dev_dbg(dev,
"16bit bus / stream mode\n");
644 dev_dbg(dev,
"24bit bus / package in back\n");
667 struct fsi_master *master = fsi_get_master(fsi);
675 u32 data =
AB_IO(1, fsi_get_port_shift(fsi, io));
676 struct fsi_master *master = fsi_get_master(fsi);
687 static void fsi_irq_clear_status(
struct fsi_priv *fsi)
690 struct fsi_master *master = fsi_get_master(fsi);
693 data |=
AB_IO(1, fsi_get_port_shift(fsi, &fsi->
capture));
706 struct fsi_master *master = fsi_get_master(fsi);
710 val = enable ? mask : 0;
720 static int fsi_set_master_clk(
struct device *dev,
struct fsi_priv *fsi,
721 long rate,
int enable)
729 ret = set_rate(dev, rate, enable);
793 static void fsi_pio_push16(
struct fsi_priv *fsi,
u8 *_buf,
int samples)
806 for (i = 0; i < samples / 2; i++)
812 for (i = 0; i < samples; i++)
817 static void fsi_pio_pop16(
struct fsi_priv *fsi,
u8 *_buf,
int samples)
822 for (i = 0; i < samples; i++)
826 static void fsi_pio_push32(
struct fsi_priv *fsi,
u8 *_buf,
int samples)
831 for (i = 0; i < samples; i++)
835 static void fsi_pio_pop32(
struct fsi_priv *fsi,
u8 *_buf,
int samples)
840 for (i = 0; i < samples; i++)
853 void (*run16)(
struct fsi_priv *fsi,
u8 *buf,
int samples),
854 void (*run32)(
struct fsi_priv *fsi,
u8 *buf,
int samples),
862 if (!fsi_stream_is_working(fsi, io))
882 buf = fsi_pio_get_area(fsi, io);
886 run16(fsi, buf, samples);
889 run32(fsi, buf, samples);
910 sample_residues = fsi_get_current_fifo_samples(fsi, io);
913 samples =
min(sample_residues, sample_space);
915 return fsi_pio_transfer(fsi, io,
929 fsi_get_current_fifo_samples(fsi, io);
931 samples =
min(sample_residues, sample_space);
933 return fsi_pio_transfer(fsi, io,
942 struct fsi_master *master = fsi_get_master(fsi);
946 fsi_irq_enable(fsi, io);
948 fsi_irq_disable(fsi, io);
950 if (fsi_is_clk_master(fsi))
985 .init = fsi_pio_push_init,
986 .transfer = fsi_pio_push,
987 .start_stop = fsi_pio_start_stop,
991 .init = fsi_pio_pop_init,
992 .transfer = fsi_pio_pop,
993 .start_stop = fsi_pio_start_stop,
996 static irqreturn_t fsi_interrupt(
int irq,
void *data)
999 u32 int_st = fsi_irq_get_status(master);
1006 fsi_stream_transfer(&master->
fsia.playback);
1008 fsi_stream_transfer(&master->
fsib.playback);
1010 fsi_stream_transfer(&master->
fsia.capture);
1012 fsi_stream_transfer(&master->
fsib.capture);
1014 fsi_count_fifo_err(&master->
fsia);
1015 fsi_count_fifo_err(&master->
fsib);
1017 fsi_irq_clear_status(&master->
fsia);
1018 fsi_irq_clear_status(&master->
fsib);
1041 snd_pcm_lib_buffer_bytes(io->
substream), dir);
1052 snd_pcm_lib_buffer_bytes(io->
substream), dir);
1063 static void fsi_dma_complete(
void *data)
1066 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1083 fsi_count_fifo_err(fsi);
1084 fsi_stream_transfer(io);
1092 struct fsi_priv *fsi = fsi_stream_to_priv(io);
1097 int is_play = fsi_stream_is_play(fsi, io);
1101 if (!fsi_stream_is_working(fsi, io))
1108 buf = fsi_dma_get_area(io);
1112 desc = dmaengine_prep_slave_single(io->
chan, buf, len, dir,
1115 dev_err(dai->
dev,
"dmaengine_prep_slave_sg() fail\n");
1122 if (dmaengine_submit(desc) < 0) {
1127 dma_async_issue_pending(io->
chan);
1165 struct fsi_master *master = fsi_get_master(fsi);
1166 u32 clk = fsi_is_port_a(fsi) ?
CRA :
CRB;
1171 dmaengine_terminate_all(io->
chan);
1173 if (fsi_is_clk_master(fsi))
1188 if (fsi_stream_is_play(fsi, io))
1189 fsi->
playback.handler = &fsi_pio_push_handler;
1191 fsi->
capture.handler = &fsi_pio_pop_handler;
1193 dev_info(dev,
"switch handler (dma => pio)\n");
1196 return fsi_stream_probe(fsi, dev);
1218 .init = fsi_dma_init,
1219 .quit = fsi_dma_quit,
1220 .probe = fsi_dma_probe,
1221 .transfer = fsi_dma_transfer,
1222 .remove = fsi_dma_remove,
1223 .start_stop = fsi_dma_push_start_stop,
1229 static void fsi_fifo_init(
struct fsi_priv *fsi,
1233 struct fsi_master *master = fsi_get_master(fsi);
1234 int is_play = fsi_stream_is_play(fsi, io);
1240 shift >>= fsi_get_port_shift(fsi, io);
1242 frame_capa = 256 << shift;
1243 dev_dbg(dev,
"fifo = %d words\n", frame_capa);
1264 for (i = 1; i < fsi->
chan_num; i <<= 1)
1266 dev_dbg(dev,
"%d channel %d store\n",
1284 static int fsi_hw_startup(
struct fsi_priv *fsi,
1288 u32 flags = fsi_get_info_flags(fsi);
1292 if (fsi_is_clk_master(fsi))
1311 if (fsi_is_spdif(fsi)) {
1312 fsi_spdif_clk_ctrl(fsi, 1);
1328 fsi_format_bus_setup(fsi, io, data, dev);
1331 fsi_irq_disable(fsi, io);
1332 fsi_irq_clear_status(fsi);
1335 fsi_fifo_init(fsi, io, dev);
1340 static void fsi_hw_shutdown(
struct fsi_priv *fsi,
1343 if (fsi_is_clk_master(fsi))
1344 fsi_set_master_clk(dev, fsi, fsi->
rate, 0);
1350 struct fsi_priv *fsi = fsi_get_priv(substream);
1360 struct fsi_priv *fsi = fsi_get_priv(substream);
1368 struct fsi_priv *fsi = fsi_get_priv(substream);
1369 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1374 fsi_stream_init(fsi, io, substream);
1375 fsi_hw_startup(fsi, io, dai->
dev);
1376 ret = fsi_stream_transfer(io);
1381 fsi_hw_shutdown(fsi, dai->
dev);
1383 fsi_stream_quit(fsi, io);
1390 static int fsi_set_fmt_dai(
struct fsi_priv *fsi,
unsigned int fmt)
1408 static int fsi_set_fmt_spdif(
struct fsi_priv *fsi)
1410 struct fsi_master *master = fsi_get_master(fsi);
1412 if (fsi_version(master) < 2)
1422 static int fsi_dai_set_fmt(
struct snd_soc_dai *dai,
unsigned int fmt)
1424 struct fsi_priv *fsi = fsi_get_priv_frm_dai(dai);
1426 u32 flags = fsi_get_info_flags(fsi);
1440 if (fsi_is_clk_master(fsi) && !set_rate) {
1441 dev_err(dai->
dev,
"platform doesn't have set_rate\n");
1451 ret = fsi_set_fmt_spdif(fsi);
1464 struct fsi_priv *fsi = fsi_get_priv(substream);
1468 if (!fsi_is_clk_master(fsi))
1471 ret = fsi_set_master_clk(dai->
dev, fsi, rate, 1);
1481 .startup = fsi_dai_startup,
1482 .shutdown = fsi_dai_shutdown,
1483 .trigger = fsi_dai_trigger,
1484 .set_fmt = fsi_dai_set_fmt,
1485 .hw_params = fsi_dai_hw_params,
1503 .buffer_bytes_max = 64 * 1024,
1504 .period_bytes_min = 32,
1505 .period_bytes_max = 8192,
1538 struct fsi_priv *fsi = fsi_get_priv(substream);
1539 struct fsi_stream *io = fsi_stream_get(fsi, substream);
1545 .open = fsi_pcm_open,
1547 .hw_params = fsi_hw_params,
1548 .hw_free = fsi_hw_free,
1549 .pointer = fsi_pointer,
1556 #define PREALLOC_BUFFER (32 * 1024)
1557 #define PREALLOC_BUFFER_MAX (32 * 1024)
1559 static void fsi_pcm_free(
struct snd_pcm *pcm)
1598 .ops = &fsi_dai_ops,
1614 .ops = &fsi_dai_ops,
1619 .ops = &fsi_pcm_ops,
1620 .pcm_new = fsi_pcm_new,
1621 .pcm_free = fsi_pcm_free,
1627 static void fsi_handler_init(
struct fsi_priv *fsi)
1629 fsi->
playback.handler = &fsi_pio_push_handler;
1631 fsi->
capture.handler = &fsi_pio_pop_handler;
1634 if (fsi->
info->tx_id) {
1635 fsi->
playback.slave.shdma_slave.slave_id = fsi->
info->tx_id;
1636 fsi->
playback.handler = &fsi_dma_push_handler;
1657 if (!res || (
int)irq <= 0) {
1658 dev_err(&pdev->
dev,
"Not enough FSI platform resources.\n");
1664 dev_err(&pdev->
dev,
"Could not allocate master\n");
1669 res->
start, resource_size(res));
1670 if (!master->
base) {
1671 dev_err(&pdev->
dev,
"Unable to ioremap FSI registers.\n");
1677 master->
core = (
struct fsi_core *)id_entry->driver_data;
1682 master->
fsia.master = master;
1684 fsi_handler_init(&master->
fsia);
1685 ret = fsi_stream_probe(&master->
fsia, &pdev->
dev);
1687 dev_err(&pdev->
dev,
"FSIA stream probe failed\n");
1692 master->
fsib.base = master->
base + 0x40;
1693 master->
fsib.master = master;
1695 fsi_handler_init(&master->
fsib);
1696 ret = fsi_stream_probe(&master->
fsib, &pdev->
dev);
1698 dev_err(&pdev->
dev,
"FSIB stream probe failed\n");
1706 id_entry->
name, master);
1714 dev_err(&pdev->
dev,
"cannot snd soc register\n");
1721 dev_err(&pdev->
dev,
"cannot snd dai register\n");
1732 pm_runtime_disable(&pdev->
dev);
1733 fsi_stream_remove(&master->
fsib);
1735 fsi_stream_remove(&master->
fsia);
1747 pm_runtime_disable(&pdev->
dev);
1752 fsi_stream_remove(&master->
fsia);
1753 fsi_stream_remove(&master->
fsib);
1758 static void __fsi_suspend(
struct fsi_priv *fsi,
1762 if (!fsi_stream_is_working(fsi, io))
1766 fsi_hw_shutdown(fsi, dev);
1769 static void __fsi_resume(
struct fsi_priv *fsi,
1773 if (!fsi_stream_is_working(fsi, io))
1776 fsi_hw_startup(fsi, io, dev);
1778 if (fsi_is_clk_master(fsi) && fsi->
rate)
1779 fsi_set_master_clk(dev, fsi, fsi->
rate, 1);
1784 static int fsi_suspend(
struct device *dev)
1790 __fsi_suspend(fsia, &fsia->
playback, dev);
1791 __fsi_suspend(fsia, &fsia->
capture, dev);
1793 __fsi_suspend(fsib, &fsib->
playback, dev);
1794 __fsi_suspend(fsib, &fsib->
capture, dev);
1799 static int fsi_resume(
struct device *dev)
1805 __fsi_resume(fsia, &fsia->
playback, dev);
1806 __fsi_resume(fsia, &fsia->
capture, dev);
1808 __fsi_resume(fsib, &fsib->
playback, dev);
1809 __fsi_resume(fsib, &fsib->
capture, dev);
1815 .suspend = fsi_suspend,
1816 .resume = fsi_resume,
1819 static struct fsi_core fsi1_core = {
1828 static struct fsi_core fsi2_core = {
1848 .name =
"fsi-pcm-audio",
1852 .remove = fsi_remove,
1853 .id_table = fsi_id_table,