xref: /openbmc/linux/drivers/dma/mmp_tdma.c (revision abdd4a70)
1c6da0ba8SZhangfei Gao /*
2c6da0ba8SZhangfei Gao  * Driver For Marvell Two-channel DMA Engine
3c6da0ba8SZhangfei Gao  *
4c6da0ba8SZhangfei Gao  * Copyright: Marvell International Ltd.
5c6da0ba8SZhangfei Gao  *
6c6da0ba8SZhangfei Gao  * The code contained herein is licensed under the GNU General Public
7c6da0ba8SZhangfei Gao  * License. You may obtain a copy of the GNU General Public License
8c6da0ba8SZhangfei Gao  * Version 2 or later at the following locations:
9c6da0ba8SZhangfei Gao  *
10c6da0ba8SZhangfei Gao  */
11c6da0ba8SZhangfei Gao 
127331205aSThierry Reding #include <linux/err.h>
13c6da0ba8SZhangfei Gao #include <linux/module.h>
14c6da0ba8SZhangfei Gao #include <linux/init.h>
15c6da0ba8SZhangfei Gao #include <linux/types.h>
16c6da0ba8SZhangfei Gao #include <linux/interrupt.h>
17c6da0ba8SZhangfei Gao #include <linux/dma-mapping.h>
18c6da0ba8SZhangfei Gao #include <linux/slab.h>
19c6da0ba8SZhangfei Gao #include <linux/dmaengine.h>
20c6da0ba8SZhangfei Gao #include <linux/platform_device.h>
21c6da0ba8SZhangfei Gao #include <linux/device.h>
22293b2da1SArnd Bergmann #include <linux/platform_data/dma-mmp_tdma.h>
23f1a77570SZhangfei Gao #include <linux/of_device.h>
247dedc002SNenghua Cao #include <linux/of_dma.h>
25c6da0ba8SZhangfei Gao 
26c6da0ba8SZhangfei Gao #include "dmaengine.h"
27c6da0ba8SZhangfei Gao 
28c6da0ba8SZhangfei Gao /*
29c6da0ba8SZhangfei Gao  * Two-Channel DMA registers
30c6da0ba8SZhangfei Gao  */
31c6da0ba8SZhangfei Gao #define TDBCR		0x00	/* Byte Count */
32c6da0ba8SZhangfei Gao #define TDSAR		0x10	/* Src Addr */
33c6da0ba8SZhangfei Gao #define TDDAR		0x20	/* Dst Addr */
34c6da0ba8SZhangfei Gao #define TDNDPR		0x30	/* Next Desc */
35c6da0ba8SZhangfei Gao #define TDCR		0x40	/* Control */
36c6da0ba8SZhangfei Gao #define TDCP		0x60	/* Priority*/
37c6da0ba8SZhangfei Gao #define TDCDPR		0x70	/* Current Desc */
38c6da0ba8SZhangfei Gao #define TDIMR		0x80	/* Int Mask */
39c6da0ba8SZhangfei Gao #define TDISR		0xa0	/* Int Status */
40c6da0ba8SZhangfei Gao 
41c6da0ba8SZhangfei Gao /* Two-Channel DMA Control Register */
42c6da0ba8SZhangfei Gao #define TDCR_SSZ_8_BITS		(0x0 << 22)	/* Sample Size */
43c6da0ba8SZhangfei Gao #define TDCR_SSZ_12_BITS	(0x1 << 22)
44c6da0ba8SZhangfei Gao #define TDCR_SSZ_16_BITS	(0x2 << 22)
45c6da0ba8SZhangfei Gao #define TDCR_SSZ_20_BITS	(0x3 << 22)
46c6da0ba8SZhangfei Gao #define TDCR_SSZ_24_BITS	(0x4 << 22)
47c6da0ba8SZhangfei Gao #define TDCR_SSZ_32_BITS	(0x5 << 22)
48c6da0ba8SZhangfei Gao #define TDCR_SSZ_SHIFT		(0x1 << 22)
49c6da0ba8SZhangfei Gao #define TDCR_SSZ_MASK		(0x7 << 22)
50c6da0ba8SZhangfei Gao #define TDCR_SSPMOD		(0x1 << 21)	/* SSP MOD */
51c6da0ba8SZhangfei Gao #define TDCR_ABR		(0x1 << 20)	/* Channel Abort */
52c6da0ba8SZhangfei Gao #define TDCR_CDE		(0x1 << 17)	/* Close Desc Enable */
53c6da0ba8SZhangfei Gao #define TDCR_PACKMOD		(0x1 << 16)	/* Pack Mode (ADMA Only) */
54c6da0ba8SZhangfei Gao #define TDCR_CHANACT		(0x1 << 14)	/* Channel Active */
55c6da0ba8SZhangfei Gao #define TDCR_FETCHND		(0x1 << 13)	/* Fetch Next Desc */
56c6da0ba8SZhangfei Gao #define TDCR_CHANEN		(0x1 << 12)	/* Channel Enable */
57c6da0ba8SZhangfei Gao #define TDCR_INTMODE		(0x1 << 10)	/* Interrupt Mode */
58c6da0ba8SZhangfei Gao #define TDCR_CHAINMOD		(0x1 << 9)	/* Chain Mode */
59c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_MSK	(0x7 << 6)	/* Burst Size */
60c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_4B		(0x0 << 6)
61c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_8B		(0x1 << 6)
62c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_16B	(0x3 << 6)
63c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_32B	(0x6 << 6)
64c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_64B	(0x7 << 6)
6520a90b0eSQiao Zhou #define TDCR_BURSTSZ_SQU_1B		(0x5 << 6)
6620a90b0eSQiao Zhou #define TDCR_BURSTSZ_SQU_2B		(0x6 << 6)
6720a90b0eSQiao Zhou #define TDCR_BURSTSZ_SQU_4B		(0x0 << 6)
6820a90b0eSQiao Zhou #define TDCR_BURSTSZ_SQU_8B		(0x1 << 6)
6920a90b0eSQiao Zhou #define TDCR_BURSTSZ_SQU_16B	(0x3 << 6)
70c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_SQU_32B	(0x7 << 6)
71c6da0ba8SZhangfei Gao #define TDCR_BURSTSZ_128B	(0x5 << 6)
72c6da0ba8SZhangfei Gao #define TDCR_DSTDIR_MSK		(0x3 << 4)	/* Dst Direction */
73c6da0ba8SZhangfei Gao #define TDCR_DSTDIR_ADDR_HOLD	(0x2 << 4)	/* Dst Addr Hold */
74c6da0ba8SZhangfei Gao #define TDCR_DSTDIR_ADDR_INC	(0x0 << 4)	/* Dst Addr Increment */
75c6da0ba8SZhangfei Gao #define TDCR_SRCDIR_MSK		(0x3 << 2)	/* Src Direction */
76c6da0ba8SZhangfei Gao #define TDCR_SRCDIR_ADDR_HOLD	(0x2 << 2)	/* Src Addr Hold */
77c6da0ba8SZhangfei Gao #define TDCR_SRCDIR_ADDR_INC	(0x0 << 2)	/* Src Addr Increment */
78c6da0ba8SZhangfei Gao #define TDCR_DSTDESCCONT	(0x1 << 1)
79c6da0ba8SZhangfei Gao #define TDCR_SRCDESTCONT	(0x1 << 0)
80c6da0ba8SZhangfei Gao 
81c6da0ba8SZhangfei Gao /* Two-Channel DMA Int Mask Register */
82c6da0ba8SZhangfei Gao #define TDIMR_COMP		(0x1 << 0)
83c6da0ba8SZhangfei Gao 
84c6da0ba8SZhangfei Gao /* Two-Channel DMA Int Status Register */
85c6da0ba8SZhangfei Gao #define TDISR_COMP		(0x1 << 0)
86c6da0ba8SZhangfei Gao 
87c6da0ba8SZhangfei Gao /*
88c6da0ba8SZhangfei Gao  * Two-Channel DMA Descriptor Struct
89c6da0ba8SZhangfei Gao  * NOTE: desc's buf must be aligned to 16 bytes.
90c6da0ba8SZhangfei Gao  */
91c6da0ba8SZhangfei Gao struct mmp_tdma_desc {
92c6da0ba8SZhangfei Gao 	u32 byte_cnt;
93c6da0ba8SZhangfei Gao 	u32 src_addr;
94c6da0ba8SZhangfei Gao 	u32 dst_addr;
95c6da0ba8SZhangfei Gao 	u32 nxt_desc;
96c6da0ba8SZhangfei Gao };
97c6da0ba8SZhangfei Gao 
98c6da0ba8SZhangfei Gao enum mmp_tdma_type {
99c6da0ba8SZhangfei Gao 	MMP_AUD_TDMA = 0,
100c6da0ba8SZhangfei Gao 	PXA910_SQU,
101c6da0ba8SZhangfei Gao };
102c6da0ba8SZhangfei Gao 
103c6da0ba8SZhangfei Gao #define TDMA_ALIGNMENT		3
104c6da0ba8SZhangfei Gao #define TDMA_MAX_XFER_BYTES    SZ_64K
105c6da0ba8SZhangfei Gao 
106c6da0ba8SZhangfei Gao struct mmp_tdma_chan {
107c6da0ba8SZhangfei Gao 	struct device			*dev;
108c6da0ba8SZhangfei Gao 	struct dma_chan			chan;
109c6da0ba8SZhangfei Gao 	struct dma_async_tx_descriptor	desc;
110c6da0ba8SZhangfei Gao 	struct tasklet_struct		tasklet;
111c6da0ba8SZhangfei Gao 
112c6da0ba8SZhangfei Gao 	struct mmp_tdma_desc		*desc_arr;
1131eed601aSQiao Zhou 	dma_addr_t			desc_arr_phys;
114c6da0ba8SZhangfei Gao 	int				desc_num;
115c6da0ba8SZhangfei Gao 	enum dma_transfer_direction	dir;
116c6da0ba8SZhangfei Gao 	dma_addr_t			dev_addr;
117c6da0ba8SZhangfei Gao 	u32				burst_sz;
118c6da0ba8SZhangfei Gao 	enum dma_slave_buswidth		buswidth;
119c6da0ba8SZhangfei Gao 	enum dma_status			status;
120c6da0ba8SZhangfei Gao 
121c6da0ba8SZhangfei Gao 	int				idx;
122c6da0ba8SZhangfei Gao 	enum mmp_tdma_type		type;
123c6da0ba8SZhangfei Gao 	int				irq;
1249d0f1fa6SVinod Koul 	void __iomem			*reg_base;
125c6da0ba8SZhangfei Gao 
126c6da0ba8SZhangfei Gao 	size_t				buf_len;
127c6da0ba8SZhangfei Gao 	size_t				period_len;
128c6da0ba8SZhangfei Gao 	size_t				pos;
1293b0f4a54SNenghua Cao 
1303b0f4a54SNenghua Cao 	struct gen_pool			*pool;
131c6da0ba8SZhangfei Gao };
132c6da0ba8SZhangfei Gao 
133c6da0ba8SZhangfei Gao #define TDMA_CHANNEL_NUM 2
134c6da0ba8SZhangfei Gao struct mmp_tdma_device {
135c6da0ba8SZhangfei Gao 	struct device			*dev;
136c6da0ba8SZhangfei Gao 	void __iomem			*base;
137c6da0ba8SZhangfei Gao 	struct dma_device		device;
138c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan		*tdmac[TDMA_CHANNEL_NUM];
139c6da0ba8SZhangfei Gao };
140c6da0ba8SZhangfei Gao 
141c6da0ba8SZhangfei Gao #define to_mmp_tdma_chan(dchan) container_of(dchan, struct mmp_tdma_chan, chan)
142c6da0ba8SZhangfei Gao 
143c6da0ba8SZhangfei Gao static void mmp_tdma_chan_set_desc(struct mmp_tdma_chan *tdmac, dma_addr_t phys)
144c6da0ba8SZhangfei Gao {
145c6da0ba8SZhangfei Gao 	writel(phys, tdmac->reg_base + TDNDPR);
146c6da0ba8SZhangfei Gao 	writel(readl(tdmac->reg_base + TDCR) | TDCR_FETCHND,
147c6da0ba8SZhangfei Gao 					tdmac->reg_base + TDCR);
148c6da0ba8SZhangfei Gao }
149c6da0ba8SZhangfei Gao 
150e6222263SQiao Zhou static void mmp_tdma_enable_irq(struct mmp_tdma_chan *tdmac, bool enable)
151e6222263SQiao Zhou {
152e6222263SQiao Zhou 	if (enable)
153e6222263SQiao Zhou 		writel(TDIMR_COMP, tdmac->reg_base + TDIMR);
154e6222263SQiao Zhou 	else
155e6222263SQiao Zhou 		writel(0, tdmac->reg_base + TDIMR);
156e6222263SQiao Zhou }
157e6222263SQiao Zhou 
158c6da0ba8SZhangfei Gao static void mmp_tdma_enable_chan(struct mmp_tdma_chan *tdmac)
159c6da0ba8SZhangfei Gao {
160c6da0ba8SZhangfei Gao 	/* enable dma chan */
161c6da0ba8SZhangfei Gao 	writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
162c6da0ba8SZhangfei Gao 					tdmac->reg_base + TDCR);
163c6da0ba8SZhangfei Gao 	tdmac->status = DMA_IN_PROGRESS;
164c6da0ba8SZhangfei Gao }
165c6da0ba8SZhangfei Gao 
166f43a6fd4SMaxime Ripard static int mmp_tdma_disable_chan(struct dma_chan *chan)
167c6da0ba8SZhangfei Gao {
168f43a6fd4SMaxime Ripard 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
1691eed601aSQiao Zhou 	u32 tdcr;
170f43a6fd4SMaxime Ripard 
1711eed601aSQiao Zhou 	tdcr = readl(tdmac->reg_base + TDCR);
1721eed601aSQiao Zhou 	tdcr |= TDCR_ABR;
1731eed601aSQiao Zhou 	tdcr &= ~TDCR_CHANEN;
1741eed601aSQiao Zhou 	writel(tdcr, tdmac->reg_base + TDCR);
1758e3c518fSQiao Zhou 
176f64eabd0SVinod Koul 	tdmac->status = DMA_COMPLETE;
177f43a6fd4SMaxime Ripard 
178f43a6fd4SMaxime Ripard 	return 0;
179c6da0ba8SZhangfei Gao }
180c6da0ba8SZhangfei Gao 
181f43a6fd4SMaxime Ripard static int mmp_tdma_resume_chan(struct dma_chan *chan)
182c6da0ba8SZhangfei Gao {
183f43a6fd4SMaxime Ripard 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
184f43a6fd4SMaxime Ripard 
185c6da0ba8SZhangfei Gao 	writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN,
186c6da0ba8SZhangfei Gao 					tdmac->reg_base + TDCR);
187c6da0ba8SZhangfei Gao 	tdmac->status = DMA_IN_PROGRESS;
188f43a6fd4SMaxime Ripard 
189f43a6fd4SMaxime Ripard 	return 0;
190c6da0ba8SZhangfei Gao }
191c6da0ba8SZhangfei Gao 
192f43a6fd4SMaxime Ripard static int mmp_tdma_pause_chan(struct dma_chan *chan)
193c6da0ba8SZhangfei Gao {
194f43a6fd4SMaxime Ripard 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
195f43a6fd4SMaxime Ripard 
196c6da0ba8SZhangfei Gao 	writel(readl(tdmac->reg_base + TDCR) & ~TDCR_CHANEN,
197c6da0ba8SZhangfei Gao 					tdmac->reg_base + TDCR);
198c6da0ba8SZhangfei Gao 	tdmac->status = DMA_PAUSED;
199f43a6fd4SMaxime Ripard 
200f43a6fd4SMaxime Ripard 	return 0;
201c6da0ba8SZhangfei Gao }
202c6da0ba8SZhangfei Gao 
203f43a6fd4SMaxime Ripard static int mmp_tdma_config_chan(struct dma_chan *chan)
204c6da0ba8SZhangfei Gao {
205f43a6fd4SMaxime Ripard 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
206a9ebbcd9SVinod Koul 	unsigned int tdcr = 0;
207c6da0ba8SZhangfei Gao 
208f43a6fd4SMaxime Ripard 	mmp_tdma_disable_chan(chan);
209c6da0ba8SZhangfei Gao 
210c6da0ba8SZhangfei Gao 	if (tdmac->dir == DMA_MEM_TO_DEV)
211c6da0ba8SZhangfei Gao 		tdcr = TDCR_DSTDIR_ADDR_HOLD | TDCR_SRCDIR_ADDR_INC;
212c6da0ba8SZhangfei Gao 	else if (tdmac->dir == DMA_DEV_TO_MEM)
213c6da0ba8SZhangfei Gao 		tdcr = TDCR_SRCDIR_ADDR_HOLD | TDCR_DSTDIR_ADDR_INC;
214c6da0ba8SZhangfei Gao 
215c6da0ba8SZhangfei Gao 	if (tdmac->type == MMP_AUD_TDMA) {
216c6da0ba8SZhangfei Gao 		tdcr |= TDCR_PACKMOD;
217c6da0ba8SZhangfei Gao 
218c6da0ba8SZhangfei Gao 		switch (tdmac->burst_sz) {
219c6da0ba8SZhangfei Gao 		case 4:
220c6da0ba8SZhangfei Gao 			tdcr |= TDCR_BURSTSZ_4B;
221c6da0ba8SZhangfei Gao 			break;
222c6da0ba8SZhangfei Gao 		case 8:
223c6da0ba8SZhangfei Gao 			tdcr |= TDCR_BURSTSZ_8B;
224c6da0ba8SZhangfei Gao 			break;
225c6da0ba8SZhangfei Gao 		case 16:
226c6da0ba8SZhangfei Gao 			tdcr |= TDCR_BURSTSZ_16B;
227c6da0ba8SZhangfei Gao 			break;
228c6da0ba8SZhangfei Gao 		case 32:
229c6da0ba8SZhangfei Gao 			tdcr |= TDCR_BURSTSZ_32B;
230c6da0ba8SZhangfei Gao 			break;
231c6da0ba8SZhangfei Gao 		case 64:
232c6da0ba8SZhangfei Gao 			tdcr |= TDCR_BURSTSZ_64B;
233c6da0ba8SZhangfei Gao 			break;
234c6da0ba8SZhangfei Gao 		case 128:
235c6da0ba8SZhangfei Gao 			tdcr |= TDCR_BURSTSZ_128B;
236c6da0ba8SZhangfei Gao 			break;
237c6da0ba8SZhangfei Gao 		default:
238c6da0ba8SZhangfei Gao 			dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
239c6da0ba8SZhangfei Gao 			return -EINVAL;
240c6da0ba8SZhangfei Gao 		}
241c6da0ba8SZhangfei Gao 
242c6da0ba8SZhangfei Gao 		switch (tdmac->buswidth) {
243c6da0ba8SZhangfei Gao 		case DMA_SLAVE_BUSWIDTH_1_BYTE:
244c6da0ba8SZhangfei Gao 			tdcr |= TDCR_SSZ_8_BITS;
245c6da0ba8SZhangfei Gao 			break;
246c6da0ba8SZhangfei Gao 		case DMA_SLAVE_BUSWIDTH_2_BYTES:
247c6da0ba8SZhangfei Gao 			tdcr |= TDCR_SSZ_16_BITS;
248c6da0ba8SZhangfei Gao 			break;
249c6da0ba8SZhangfei Gao 		case DMA_SLAVE_BUSWIDTH_4_BYTES:
250c6da0ba8SZhangfei Gao 			tdcr |= TDCR_SSZ_32_BITS;
251c6da0ba8SZhangfei Gao 			break;
252c6da0ba8SZhangfei Gao 		default:
253c6da0ba8SZhangfei Gao 			dev_err(tdmac->dev, "mmp_tdma: unknown bus size.\n");
254c6da0ba8SZhangfei Gao 			return -EINVAL;
255c6da0ba8SZhangfei Gao 		}
256c6da0ba8SZhangfei Gao 	} else if (tdmac->type == PXA910_SQU) {
257c6da0ba8SZhangfei Gao 		tdcr |= TDCR_SSPMOD;
25820a90b0eSQiao Zhou 
25920a90b0eSQiao Zhou 		switch (tdmac->burst_sz) {
26020a90b0eSQiao Zhou 		case 1:
26120a90b0eSQiao Zhou 			tdcr |= TDCR_BURSTSZ_SQU_1B;
26220a90b0eSQiao Zhou 			break;
26320a90b0eSQiao Zhou 		case 2:
26420a90b0eSQiao Zhou 			tdcr |= TDCR_BURSTSZ_SQU_2B;
26520a90b0eSQiao Zhou 			break;
26620a90b0eSQiao Zhou 		case 4:
26720a90b0eSQiao Zhou 			tdcr |= TDCR_BURSTSZ_SQU_4B;
26820a90b0eSQiao Zhou 			break;
26920a90b0eSQiao Zhou 		case 8:
27020a90b0eSQiao Zhou 			tdcr |= TDCR_BURSTSZ_SQU_8B;
27120a90b0eSQiao Zhou 			break;
27220a90b0eSQiao Zhou 		case 16:
27320a90b0eSQiao Zhou 			tdcr |= TDCR_BURSTSZ_SQU_16B;
27420a90b0eSQiao Zhou 			break;
27520a90b0eSQiao Zhou 		case 32:
27620a90b0eSQiao Zhou 			tdcr |= TDCR_BURSTSZ_SQU_32B;
27720a90b0eSQiao Zhou 			break;
27820a90b0eSQiao Zhou 		default:
27920a90b0eSQiao Zhou 			dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n");
28020a90b0eSQiao Zhou 			return -EINVAL;
28120a90b0eSQiao Zhou 		}
282c6da0ba8SZhangfei Gao 	}
283c6da0ba8SZhangfei Gao 
284c6da0ba8SZhangfei Gao 	writel(tdcr, tdmac->reg_base + TDCR);
285c6da0ba8SZhangfei Gao 	return 0;
286c6da0ba8SZhangfei Gao }
287c6da0ba8SZhangfei Gao 
288c6da0ba8SZhangfei Gao static int mmp_tdma_clear_chan_irq(struct mmp_tdma_chan *tdmac)
289c6da0ba8SZhangfei Gao {
290c6da0ba8SZhangfei Gao 	u32 reg = readl(tdmac->reg_base + TDISR);
291c6da0ba8SZhangfei Gao 
292c6da0ba8SZhangfei Gao 	if (reg & TDISR_COMP) {
293c6da0ba8SZhangfei Gao 		/* clear irq */
294c6da0ba8SZhangfei Gao 		reg &= ~TDISR_COMP;
295c6da0ba8SZhangfei Gao 		writel(reg, tdmac->reg_base + TDISR);
296c6da0ba8SZhangfei Gao 
297c6da0ba8SZhangfei Gao 		return 0;
298c6da0ba8SZhangfei Gao 	}
299c6da0ba8SZhangfei Gao 	return -EAGAIN;
300c6da0ba8SZhangfei Gao }
301c6da0ba8SZhangfei Gao 
3021eed601aSQiao Zhou static size_t mmp_tdma_get_pos(struct mmp_tdma_chan *tdmac)
3031eed601aSQiao Zhou {
3041eed601aSQiao Zhou 	size_t reg;
3051eed601aSQiao Zhou 
3061eed601aSQiao Zhou 	if (tdmac->idx == 0) {
3071eed601aSQiao Zhou 		reg = __raw_readl(tdmac->reg_base + TDSAR);
3081eed601aSQiao Zhou 		reg -= tdmac->desc_arr[0].src_addr;
3091eed601aSQiao Zhou 	} else if (tdmac->idx == 1) {
3101eed601aSQiao Zhou 		reg = __raw_readl(tdmac->reg_base + TDDAR);
3111eed601aSQiao Zhou 		reg -= tdmac->desc_arr[0].dst_addr;
3121eed601aSQiao Zhou 	} else
3131eed601aSQiao Zhou 		return -EINVAL;
3141eed601aSQiao Zhou 
3151eed601aSQiao Zhou 	return reg;
3161eed601aSQiao Zhou }
3171eed601aSQiao Zhou 
318c6da0ba8SZhangfei Gao static irqreturn_t mmp_tdma_chan_handler(int irq, void *dev_id)
319c6da0ba8SZhangfei Gao {
320c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = dev_id;
321c6da0ba8SZhangfei Gao 
322c6da0ba8SZhangfei Gao 	if (mmp_tdma_clear_chan_irq(tdmac) == 0) {
323c6da0ba8SZhangfei Gao 		tasklet_schedule(&tdmac->tasklet);
324c6da0ba8SZhangfei Gao 		return IRQ_HANDLED;
325c6da0ba8SZhangfei Gao 	} else
326c6da0ba8SZhangfei Gao 		return IRQ_NONE;
327c6da0ba8SZhangfei Gao }
328c6da0ba8SZhangfei Gao 
329c6da0ba8SZhangfei Gao static irqreturn_t mmp_tdma_int_handler(int irq, void *dev_id)
330c6da0ba8SZhangfei Gao {
331c6da0ba8SZhangfei Gao 	struct mmp_tdma_device *tdev = dev_id;
332c6da0ba8SZhangfei Gao 	int i, ret;
333c6da0ba8SZhangfei Gao 	int irq_num = 0;
334c6da0ba8SZhangfei Gao 
335c6da0ba8SZhangfei Gao 	for (i = 0; i < TDMA_CHANNEL_NUM; i++) {
336c6da0ba8SZhangfei Gao 		struct mmp_tdma_chan *tdmac = tdev->tdmac[i];
337c6da0ba8SZhangfei Gao 
338c6da0ba8SZhangfei Gao 		ret = mmp_tdma_chan_handler(irq, tdmac);
339c6da0ba8SZhangfei Gao 		if (ret == IRQ_HANDLED)
340c6da0ba8SZhangfei Gao 			irq_num++;
341c6da0ba8SZhangfei Gao 	}
342c6da0ba8SZhangfei Gao 
343c6da0ba8SZhangfei Gao 	if (irq_num)
344c6da0ba8SZhangfei Gao 		return IRQ_HANDLED;
345c6da0ba8SZhangfei Gao 	else
346c6da0ba8SZhangfei Gao 		return IRQ_NONE;
347c6da0ba8SZhangfei Gao }
348c6da0ba8SZhangfei Gao 
349c6da0ba8SZhangfei Gao static void dma_do_tasklet(unsigned long data)
350c6da0ba8SZhangfei Gao {
351c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = (struct mmp_tdma_chan *)data;
352c6da0ba8SZhangfei Gao 
353c6da0ba8SZhangfei Gao 	if (tdmac->desc.callback)
354c6da0ba8SZhangfei Gao 		tdmac->desc.callback(tdmac->desc.callback_param);
355c6da0ba8SZhangfei Gao 
356c6da0ba8SZhangfei Gao }
357c6da0ba8SZhangfei Gao 
358c6da0ba8SZhangfei Gao static void mmp_tdma_free_descriptor(struct mmp_tdma_chan *tdmac)
359c6da0ba8SZhangfei Gao {
360c6da0ba8SZhangfei Gao 	struct gen_pool *gpool;
361c6da0ba8SZhangfei Gao 	int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
362c6da0ba8SZhangfei Gao 
3633b0f4a54SNenghua Cao 	gpool = tdmac->pool;
3641eed601aSQiao Zhou 	if (gpool && tdmac->desc_arr)
365c6da0ba8SZhangfei Gao 		gen_pool_free(gpool, (unsigned long)tdmac->desc_arr,
366c6da0ba8SZhangfei Gao 				size);
367c6da0ba8SZhangfei Gao 	tdmac->desc_arr = NULL;
368c6da0ba8SZhangfei Gao 
369c6da0ba8SZhangfei Gao 	return;
370c6da0ba8SZhangfei Gao }
371c6da0ba8SZhangfei Gao 
372c6da0ba8SZhangfei Gao static dma_cookie_t mmp_tdma_tx_submit(struct dma_async_tx_descriptor *tx)
373c6da0ba8SZhangfei Gao {
374c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(tx->chan);
375c6da0ba8SZhangfei Gao 
376c6da0ba8SZhangfei Gao 	mmp_tdma_chan_set_desc(tdmac, tdmac->desc_arr_phys);
377c6da0ba8SZhangfei Gao 
378c6da0ba8SZhangfei Gao 	return 0;
379c6da0ba8SZhangfei Gao }
380c6da0ba8SZhangfei Gao 
381c6da0ba8SZhangfei Gao static int mmp_tdma_alloc_chan_resources(struct dma_chan *chan)
382c6da0ba8SZhangfei Gao {
383c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
384c6da0ba8SZhangfei Gao 	int ret;
385c6da0ba8SZhangfei Gao 
386c6da0ba8SZhangfei Gao 	dma_async_tx_descriptor_init(&tdmac->desc, chan);
387c6da0ba8SZhangfei Gao 	tdmac->desc.tx_submit = mmp_tdma_tx_submit;
388c6da0ba8SZhangfei Gao 
389c6da0ba8SZhangfei Gao 	if (tdmac->irq) {
390c6da0ba8SZhangfei Gao 		ret = devm_request_irq(tdmac->dev, tdmac->irq,
391174b537aSMichael Opdenacker 			mmp_tdma_chan_handler, 0, "tdma", tdmac);
392c6da0ba8SZhangfei Gao 		if (ret)
393c6da0ba8SZhangfei Gao 			return ret;
394c6da0ba8SZhangfei Gao 	}
395c6da0ba8SZhangfei Gao 	return 1;
396c6da0ba8SZhangfei Gao }
397c6da0ba8SZhangfei Gao 
398c6da0ba8SZhangfei Gao static void mmp_tdma_free_chan_resources(struct dma_chan *chan)
399c6da0ba8SZhangfei Gao {
400c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
401c6da0ba8SZhangfei Gao 
402c6da0ba8SZhangfei Gao 	if (tdmac->irq)
403c6da0ba8SZhangfei Gao 		devm_free_irq(tdmac->dev, tdmac->irq, tdmac);
404c6da0ba8SZhangfei Gao 	mmp_tdma_free_descriptor(tdmac);
405c6da0ba8SZhangfei Gao 	return;
406c6da0ba8SZhangfei Gao }
407c6da0ba8SZhangfei Gao 
408c6da0ba8SZhangfei Gao struct mmp_tdma_desc *mmp_tdma_alloc_descriptor(struct mmp_tdma_chan *tdmac)
409c6da0ba8SZhangfei Gao {
410c6da0ba8SZhangfei Gao 	struct gen_pool *gpool;
411c6da0ba8SZhangfei Gao 	int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc);
412c6da0ba8SZhangfei Gao 
4133b0f4a54SNenghua Cao 	gpool = tdmac->pool;
414c6da0ba8SZhangfei Gao 	if (!gpool)
415c6da0ba8SZhangfei Gao 		return NULL;
416c6da0ba8SZhangfei Gao 
417a6dd30e2SNicolin Chen 	tdmac->desc_arr = gen_pool_dma_alloc(gpool, size, &tdmac->desc_arr_phys);
418c6da0ba8SZhangfei Gao 
419c6da0ba8SZhangfei Gao 	return tdmac->desc_arr;
420c6da0ba8SZhangfei Gao }
421c6da0ba8SZhangfei Gao 
422c6da0ba8SZhangfei Gao static struct dma_async_tx_descriptor *mmp_tdma_prep_dma_cyclic(
423c6da0ba8SZhangfei Gao 		struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len,
424c6da0ba8SZhangfei Gao 		size_t period_len, enum dma_transfer_direction direction,
42531c1e5a1SLaurent Pinchart 		unsigned long flags)
426c6da0ba8SZhangfei Gao {
427c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
428c6da0ba8SZhangfei Gao 	struct mmp_tdma_desc *desc;
429c6da0ba8SZhangfei Gao 	int num_periods = buf_len / period_len;
430c6da0ba8SZhangfei Gao 	int i = 0, buf = 0;
431c6da0ba8SZhangfei Gao 
432f64eabd0SVinod Koul 	if (tdmac->status != DMA_COMPLETE)
433c6da0ba8SZhangfei Gao 		return NULL;
434c6da0ba8SZhangfei Gao 
435c6da0ba8SZhangfei Gao 	if (period_len > TDMA_MAX_XFER_BYTES) {
436c6da0ba8SZhangfei Gao 		dev_err(tdmac->dev,
437c6da0ba8SZhangfei Gao 				"maximum period size exceeded: %d > %d\n",
438c6da0ba8SZhangfei Gao 				period_len, TDMA_MAX_XFER_BYTES);
439c6da0ba8SZhangfei Gao 		goto err_out;
440c6da0ba8SZhangfei Gao 	}
441c6da0ba8SZhangfei Gao 
442c6da0ba8SZhangfei Gao 	tdmac->status = DMA_IN_PROGRESS;
443c6da0ba8SZhangfei Gao 	tdmac->desc_num = num_periods;
444c6da0ba8SZhangfei Gao 	desc = mmp_tdma_alloc_descriptor(tdmac);
445c6da0ba8SZhangfei Gao 	if (!desc)
446c6da0ba8SZhangfei Gao 		goto err_out;
447c6da0ba8SZhangfei Gao 
448c6da0ba8SZhangfei Gao 	while (buf < buf_len) {
449c6da0ba8SZhangfei Gao 		desc = &tdmac->desc_arr[i];
450c6da0ba8SZhangfei Gao 
451c6da0ba8SZhangfei Gao 		if (i + 1 == num_periods)
452c6da0ba8SZhangfei Gao 			desc->nxt_desc = tdmac->desc_arr_phys;
453c6da0ba8SZhangfei Gao 		else
454c6da0ba8SZhangfei Gao 			desc->nxt_desc = tdmac->desc_arr_phys +
455c6da0ba8SZhangfei Gao 				sizeof(*desc) * (i + 1);
456c6da0ba8SZhangfei Gao 
457c6da0ba8SZhangfei Gao 		if (direction == DMA_MEM_TO_DEV) {
458c6da0ba8SZhangfei Gao 			desc->src_addr = dma_addr;
459c6da0ba8SZhangfei Gao 			desc->dst_addr = tdmac->dev_addr;
460c6da0ba8SZhangfei Gao 		} else {
461c6da0ba8SZhangfei Gao 			desc->src_addr = tdmac->dev_addr;
462c6da0ba8SZhangfei Gao 			desc->dst_addr = dma_addr;
463c6da0ba8SZhangfei Gao 		}
464c6da0ba8SZhangfei Gao 		desc->byte_cnt = period_len;
465c6da0ba8SZhangfei Gao 		dma_addr += period_len;
466c6da0ba8SZhangfei Gao 		buf += period_len;
467c6da0ba8SZhangfei Gao 		i++;
468c6da0ba8SZhangfei Gao 	}
469c6da0ba8SZhangfei Gao 
470e6222263SQiao Zhou 	/* enable interrupt */
471e6222263SQiao Zhou 	if (flags & DMA_PREP_INTERRUPT)
472e6222263SQiao Zhou 		mmp_tdma_enable_irq(tdmac, true);
473e6222263SQiao Zhou 
474c6da0ba8SZhangfei Gao 	tdmac->buf_len = buf_len;
475c6da0ba8SZhangfei Gao 	tdmac->period_len = period_len;
476c6da0ba8SZhangfei Gao 	tdmac->pos = 0;
477c6da0ba8SZhangfei Gao 
478c6da0ba8SZhangfei Gao 	return &tdmac->desc;
479c6da0ba8SZhangfei Gao 
480c6da0ba8SZhangfei Gao err_out:
481c6da0ba8SZhangfei Gao 	tdmac->status = DMA_ERROR;
482c6da0ba8SZhangfei Gao 	return NULL;
483c6da0ba8SZhangfei Gao }
484c6da0ba8SZhangfei Gao 
485f43a6fd4SMaxime Ripard static int mmp_tdma_terminate_all(struct dma_chan *chan)
486c6da0ba8SZhangfei Gao {
487c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
488c6da0ba8SZhangfei Gao 
489f43a6fd4SMaxime Ripard 	mmp_tdma_disable_chan(chan);
490e6222263SQiao Zhou 	/* disable interrupt */
491e6222263SQiao Zhou 	mmp_tdma_enable_irq(tdmac, false);
4923c20ba5fSArnd Bergmann 
4933c20ba5fSArnd Bergmann 	return 0;
494f43a6fd4SMaxime Ripard }
495f43a6fd4SMaxime Ripard 
496f43a6fd4SMaxime Ripard static int mmp_tdma_config(struct dma_chan *chan,
497f43a6fd4SMaxime Ripard 			   struct dma_slave_config *dmaengine_cfg)
498f43a6fd4SMaxime Ripard {
499f43a6fd4SMaxime Ripard 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
500f43a6fd4SMaxime Ripard 
501c6da0ba8SZhangfei Gao 	if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) {
502c6da0ba8SZhangfei Gao 		tdmac->dev_addr = dmaengine_cfg->src_addr;
503c6da0ba8SZhangfei Gao 		tdmac->burst_sz = dmaengine_cfg->src_maxburst;
504c6da0ba8SZhangfei Gao 		tdmac->buswidth = dmaengine_cfg->src_addr_width;
505c6da0ba8SZhangfei Gao 	} else {
506c6da0ba8SZhangfei Gao 		tdmac->dev_addr = dmaengine_cfg->dst_addr;
507c6da0ba8SZhangfei Gao 		tdmac->burst_sz = dmaengine_cfg->dst_maxburst;
508c6da0ba8SZhangfei Gao 		tdmac->buswidth = dmaengine_cfg->dst_addr_width;
509c6da0ba8SZhangfei Gao 	}
510c6da0ba8SZhangfei Gao 	tdmac->dir = dmaengine_cfg->direction;
511c6da0ba8SZhangfei Gao 
512f43a6fd4SMaxime Ripard 	return mmp_tdma_config_chan(chan);
513c6da0ba8SZhangfei Gao }
514c6da0ba8SZhangfei Gao 
515c6da0ba8SZhangfei Gao static enum dma_status mmp_tdma_tx_status(struct dma_chan *chan,
516c6da0ba8SZhangfei Gao 			dma_cookie_t cookie, struct dma_tx_state *txstate)
517c6da0ba8SZhangfei Gao {
518c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
519c6da0ba8SZhangfei Gao 
5201eed601aSQiao Zhou 	tdmac->pos = mmp_tdma_get_pos(tdmac);
521c14d2bc4SAndy Shevchenko 	dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie,
522c14d2bc4SAndy Shevchenko 			 tdmac->buf_len - tdmac->pos);
523c6da0ba8SZhangfei Gao 
524c6da0ba8SZhangfei Gao 	return tdmac->status;
525c6da0ba8SZhangfei Gao }
526c6da0ba8SZhangfei Gao 
527c6da0ba8SZhangfei Gao static void mmp_tdma_issue_pending(struct dma_chan *chan)
528c6da0ba8SZhangfei Gao {
529c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
530c6da0ba8SZhangfei Gao 
531c6da0ba8SZhangfei Gao 	mmp_tdma_enable_chan(tdmac);
532c6da0ba8SZhangfei Gao }
533c6da0ba8SZhangfei Gao 
5344bf27b8bSGreg Kroah-Hartman static int mmp_tdma_remove(struct platform_device *pdev)
535c6da0ba8SZhangfei Gao {
536c6da0ba8SZhangfei Gao 	struct mmp_tdma_device *tdev = platform_get_drvdata(pdev);
537c6da0ba8SZhangfei Gao 
538c6da0ba8SZhangfei Gao 	dma_async_device_unregister(&tdev->device);
539c6da0ba8SZhangfei Gao 	return 0;
540c6da0ba8SZhangfei Gao }
541c6da0ba8SZhangfei Gao 
542463a1f8bSBill Pemberton static int mmp_tdma_chan_init(struct mmp_tdma_device *tdev,
5433b0f4a54SNenghua Cao 					int idx, int irq,
5443b0f4a54SNenghua Cao 					int type, struct gen_pool *pool)
545c6da0ba8SZhangfei Gao {
546c6da0ba8SZhangfei Gao 	struct mmp_tdma_chan *tdmac;
547c6da0ba8SZhangfei Gao 
548c6da0ba8SZhangfei Gao 	if (idx >= TDMA_CHANNEL_NUM) {
549c6da0ba8SZhangfei Gao 		dev_err(tdev->dev, "too many channels for device!\n");
550c6da0ba8SZhangfei Gao 		return -EINVAL;
551c6da0ba8SZhangfei Gao 	}
552c6da0ba8SZhangfei Gao 
553c6da0ba8SZhangfei Gao 	/* alloc channel */
554c6da0ba8SZhangfei Gao 	tdmac = devm_kzalloc(tdev->dev, sizeof(*tdmac), GFP_KERNEL);
555c6da0ba8SZhangfei Gao 	if (!tdmac) {
556c6da0ba8SZhangfei Gao 		dev_err(tdev->dev, "no free memory for DMA channels!\n");
557c6da0ba8SZhangfei Gao 		return -ENOMEM;
558c6da0ba8SZhangfei Gao 	}
559c6da0ba8SZhangfei Gao 	if (irq)
560f1a77570SZhangfei Gao 		tdmac->irq = irq;
561c6da0ba8SZhangfei Gao 	tdmac->dev	   = tdev->dev;
562c6da0ba8SZhangfei Gao 	tdmac->chan.device = &tdev->device;
563c6da0ba8SZhangfei Gao 	tdmac->idx	   = idx;
564c6da0ba8SZhangfei Gao 	tdmac->type	   = type;
5659d0f1fa6SVinod Koul 	tdmac->reg_base	   = tdev->base + idx * 4;
5663b0f4a54SNenghua Cao 	tdmac->pool	   = pool;
567f64eabd0SVinod Koul 	tdmac->status = DMA_COMPLETE;
568c6da0ba8SZhangfei Gao 	tdev->tdmac[tdmac->idx] = tdmac;
569c6da0ba8SZhangfei Gao 	tasklet_init(&tdmac->tasklet, dma_do_tasklet, (unsigned long)tdmac);
570c6da0ba8SZhangfei Gao 
571c6da0ba8SZhangfei Gao 	/* add the channel to tdma_chan list */
572c6da0ba8SZhangfei Gao 	list_add_tail(&tdmac->chan.device_node,
573c6da0ba8SZhangfei Gao 			&tdev->device.channels);
574c6da0ba8SZhangfei Gao 	return 0;
575c6da0ba8SZhangfei Gao }
576c6da0ba8SZhangfei Gao 
5777dedc002SNenghua Cao struct mmp_tdma_filter_param {
5787dedc002SNenghua Cao 	struct device_node *of_node;
5797dedc002SNenghua Cao 	unsigned int chan_id;
5807dedc002SNenghua Cao };
5817dedc002SNenghua Cao 
5827dedc002SNenghua Cao static bool mmp_tdma_filter_fn(struct dma_chan *chan, void *fn_param)
5837dedc002SNenghua Cao {
5847dedc002SNenghua Cao 	struct mmp_tdma_filter_param *param = fn_param;
5857dedc002SNenghua Cao 	struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan);
5867dedc002SNenghua Cao 	struct dma_device *pdma_device = tdmac->chan.device;
5877dedc002SNenghua Cao 
5887dedc002SNenghua Cao 	if (pdma_device->dev->of_node != param->of_node)
5897dedc002SNenghua Cao 		return false;
5907dedc002SNenghua Cao 
5917dedc002SNenghua Cao 	if (chan->chan_id != param->chan_id)
5927dedc002SNenghua Cao 		return false;
5937dedc002SNenghua Cao 
5947dedc002SNenghua Cao 	return true;
5957dedc002SNenghua Cao }
5967dedc002SNenghua Cao 
5977dedc002SNenghua Cao struct dma_chan *mmp_tdma_xlate(struct of_phandle_args *dma_spec,
5987dedc002SNenghua Cao 			       struct of_dma *ofdma)
5997dedc002SNenghua Cao {
6007dedc002SNenghua Cao 	struct mmp_tdma_device *tdev = ofdma->of_dma_data;
6017dedc002SNenghua Cao 	dma_cap_mask_t mask = tdev->device.cap_mask;
6027dedc002SNenghua Cao 	struct mmp_tdma_filter_param param;
6037dedc002SNenghua Cao 
6047dedc002SNenghua Cao 	if (dma_spec->args_count != 1)
6057dedc002SNenghua Cao 		return NULL;
6067dedc002SNenghua Cao 
6077dedc002SNenghua Cao 	param.of_node = ofdma->of_node;
6087dedc002SNenghua Cao 	param.chan_id = dma_spec->args[0];
6097dedc002SNenghua Cao 
6107dedc002SNenghua Cao 	if (param.chan_id >= TDMA_CHANNEL_NUM)
6117dedc002SNenghua Cao 		return NULL;
6127dedc002SNenghua Cao 
6137dedc002SNenghua Cao 	return dma_request_channel(mask, mmp_tdma_filter_fn, &param);
6147dedc002SNenghua Cao }
6157dedc002SNenghua Cao 
61657c03422SFabian Frederick static const struct of_device_id mmp_tdma_dt_ids[] = {
617f1a77570SZhangfei Gao 	{ .compatible = "marvell,adma-1.0", .data = (void *)MMP_AUD_TDMA},
618f1a77570SZhangfei Gao 	{ .compatible = "marvell,pxa910-squ", .data = (void *)PXA910_SQU},
619f1a77570SZhangfei Gao 	{}
620f1a77570SZhangfei Gao };
621f1a77570SZhangfei Gao MODULE_DEVICE_TABLE(of, mmp_tdma_dt_ids);
622f1a77570SZhangfei Gao 
623463a1f8bSBill Pemberton static int mmp_tdma_probe(struct platform_device *pdev)
624c6da0ba8SZhangfei Gao {
625f1a77570SZhangfei Gao 	enum mmp_tdma_type type;
626f1a77570SZhangfei Gao 	const struct of_device_id *of_id;
627c6da0ba8SZhangfei Gao 	struct mmp_tdma_device *tdev;
628c6da0ba8SZhangfei Gao 	struct resource *iores;
629c6da0ba8SZhangfei Gao 	int i, ret;
630f1a77570SZhangfei Gao 	int irq = 0, irq_num = 0;
631c6da0ba8SZhangfei Gao 	int chan_num = TDMA_CHANNEL_NUM;
6321eed601aSQiao Zhou 	struct gen_pool *pool = NULL;
633c6da0ba8SZhangfei Gao 
634f1a77570SZhangfei Gao 	of_id = of_match_device(mmp_tdma_dt_ids, &pdev->dev);
635f1a77570SZhangfei Gao 	if (of_id)
636f1a77570SZhangfei Gao 		type = (enum mmp_tdma_type) of_id->data;
637f1a77570SZhangfei Gao 	else
638f1a77570SZhangfei Gao 		type = platform_get_device_id(pdev)->driver_data;
639f1a77570SZhangfei Gao 
640c6da0ba8SZhangfei Gao 	/* always have couple channels */
641c6da0ba8SZhangfei Gao 	tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
642c6da0ba8SZhangfei Gao 	if (!tdev)
643c6da0ba8SZhangfei Gao 		return -ENOMEM;
644c6da0ba8SZhangfei Gao 
645c6da0ba8SZhangfei Gao 	tdev->dev = &pdev->dev;
646c6da0ba8SZhangfei Gao 
647f1a77570SZhangfei Gao 	for (i = 0; i < chan_num; i++) {
648f1a77570SZhangfei Gao 		if (platform_get_irq(pdev, i) > 0)
649f1a77570SZhangfei Gao 			irq_num++;
650f1a77570SZhangfei Gao 	}
651c6da0ba8SZhangfei Gao 
652c6da0ba8SZhangfei Gao 	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
6537331205aSThierry Reding 	tdev->base = devm_ioremap_resource(&pdev->dev, iores);
6547331205aSThierry Reding 	if (IS_ERR(tdev->base))
6557331205aSThierry Reding 		return PTR_ERR(tdev->base);
656c6da0ba8SZhangfei Gao 
657f1a77570SZhangfei Gao 	INIT_LIST_HEAD(&tdev->device.channels);
658f1a77570SZhangfei Gao 
6593b0f4a54SNenghua Cao 	if (pdev->dev.of_node)
660abdd4a70SVladimir Zapolskiy 		pool = of_gen_pool_get(pdev->dev.of_node, "asram", 0);
6613b0f4a54SNenghua Cao 	else
6623b0f4a54SNenghua Cao 		pool = sram_get_gpool("asram");
6633b0f4a54SNenghua Cao 	if (!pool) {
6643b0f4a54SNenghua Cao 		dev_err(&pdev->dev, "asram pool not available\n");
6653b0f4a54SNenghua Cao 		return -ENOMEM;
6663b0f4a54SNenghua Cao 	}
6673b0f4a54SNenghua Cao 
668f1a77570SZhangfei Gao 	if (irq_num != chan_num) {
669f1a77570SZhangfei Gao 		irq = platform_get_irq(pdev, 0);
670f1a77570SZhangfei Gao 		ret = devm_request_irq(&pdev->dev, irq,
671174b537aSMichael Opdenacker 			mmp_tdma_int_handler, 0, "tdma", tdev);
672c6da0ba8SZhangfei Gao 		if (ret)
673c6da0ba8SZhangfei Gao 			return ret;
674c6da0ba8SZhangfei Gao 	}
675c6da0ba8SZhangfei Gao 
676f1a77570SZhangfei Gao 	/* initialize channel parameters */
677f1a77570SZhangfei Gao 	for (i = 0; i < chan_num; i++) {
678f1a77570SZhangfei Gao 		irq = (irq_num != chan_num) ? 0 : platform_get_irq(pdev, i);
6793b0f4a54SNenghua Cao 		ret = mmp_tdma_chan_init(tdev, i, irq, type, pool);
680f1a77570SZhangfei Gao 		if (ret)
681f1a77570SZhangfei Gao 			return ret;
682f1a77570SZhangfei Gao 	}
683f1a77570SZhangfei Gao 
684c6da0ba8SZhangfei Gao 	dma_cap_set(DMA_SLAVE, tdev->device.cap_mask);
685c6da0ba8SZhangfei Gao 	dma_cap_set(DMA_CYCLIC, tdev->device.cap_mask);
686c6da0ba8SZhangfei Gao 	tdev->device.dev = &pdev->dev;
687c6da0ba8SZhangfei Gao 	tdev->device.device_alloc_chan_resources =
688c6da0ba8SZhangfei Gao 					mmp_tdma_alloc_chan_resources;
689c6da0ba8SZhangfei Gao 	tdev->device.device_free_chan_resources =
690c6da0ba8SZhangfei Gao 					mmp_tdma_free_chan_resources;
691c6da0ba8SZhangfei Gao 	tdev->device.device_prep_dma_cyclic = mmp_tdma_prep_dma_cyclic;
692c6da0ba8SZhangfei Gao 	tdev->device.device_tx_status = mmp_tdma_tx_status;
693c6da0ba8SZhangfei Gao 	tdev->device.device_issue_pending = mmp_tdma_issue_pending;
694f43a6fd4SMaxime Ripard 	tdev->device.device_config = mmp_tdma_config;
695f43a6fd4SMaxime Ripard 	tdev->device.device_pause = mmp_tdma_pause_chan;
696f43a6fd4SMaxime Ripard 	tdev->device.device_resume = mmp_tdma_resume_chan;
697f43a6fd4SMaxime Ripard 	tdev->device.device_terminate_all = mmp_tdma_terminate_all;
698c6da0ba8SZhangfei Gao 	tdev->device.copy_align = TDMA_ALIGNMENT;
699c6da0ba8SZhangfei Gao 
700c6da0ba8SZhangfei Gao 	dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
701c6da0ba8SZhangfei Gao 	platform_set_drvdata(pdev, tdev);
702c6da0ba8SZhangfei Gao 
703c6da0ba8SZhangfei Gao 	ret = dma_async_device_register(&tdev->device);
704c6da0ba8SZhangfei Gao 	if (ret) {
705c6da0ba8SZhangfei Gao 		dev_err(tdev->device.dev, "unable to register\n");
706c6da0ba8SZhangfei Gao 		return ret;
707c6da0ba8SZhangfei Gao 	}
708c6da0ba8SZhangfei Gao 
7097dedc002SNenghua Cao 	if (pdev->dev.of_node) {
7107dedc002SNenghua Cao 		ret = of_dma_controller_register(pdev->dev.of_node,
7117dedc002SNenghua Cao 							mmp_tdma_xlate, tdev);
7127dedc002SNenghua Cao 		if (ret) {
7137dedc002SNenghua Cao 			dev_err(tdev->device.dev,
7147dedc002SNenghua Cao 				"failed to register controller\n");
7157dedc002SNenghua Cao 			dma_async_device_unregister(&tdev->device);
7167dedc002SNenghua Cao 		}
7177dedc002SNenghua Cao 	}
7187dedc002SNenghua Cao 
719c6da0ba8SZhangfei Gao 	dev_info(tdev->device.dev, "initialized\n");
720c6da0ba8SZhangfei Gao 	return 0;
721c6da0ba8SZhangfei Gao }
722c6da0ba8SZhangfei Gao 
723c6da0ba8SZhangfei Gao static const struct platform_device_id mmp_tdma_id_table[] = {
724c6da0ba8SZhangfei Gao 	{ "mmp-adma",	MMP_AUD_TDMA },
725c6da0ba8SZhangfei Gao 	{ "pxa910-squ",	PXA910_SQU },
726c6da0ba8SZhangfei Gao 	{ },
727c6da0ba8SZhangfei Gao };
728c6da0ba8SZhangfei Gao 
729c6da0ba8SZhangfei Gao static struct platform_driver mmp_tdma_driver = {
730c6da0ba8SZhangfei Gao 	.driver		= {
731c6da0ba8SZhangfei Gao 		.name	= "mmp-tdma",
732f1a77570SZhangfei Gao 		.of_match_table = mmp_tdma_dt_ids,
733c6da0ba8SZhangfei Gao 	},
734c6da0ba8SZhangfei Gao 	.id_table	= mmp_tdma_id_table,
735c6da0ba8SZhangfei Gao 	.probe		= mmp_tdma_probe,
736a7d6e3ecSBill Pemberton 	.remove		= mmp_tdma_remove,
737c6da0ba8SZhangfei Gao };
738c6da0ba8SZhangfei Gao 
739c6da0ba8SZhangfei Gao module_platform_driver(mmp_tdma_driver);
740c6da0ba8SZhangfei Gao 
741c6da0ba8SZhangfei Gao MODULE_LICENSE("GPL");
742c6da0ba8SZhangfei Gao MODULE_DESCRIPTION("MMP Two-Channel DMA Driver");
743c6da0ba8SZhangfei Gao MODULE_ALIAS("platform:mmp-tdma");
744c6da0ba8SZhangfei Gao MODULE_AUTHOR("Leo Yan <leoy@marvell.com>");
745c6da0ba8SZhangfei Gao MODULE_AUTHOR("Zhangfei Gao <zhangfei.gao@marvell.com>");
746