xref: /openbmc/linux/sound/soc/sh/rcar/dma.c (revision 4f6cce39)
1 /*
2  * Renesas R-Car Audio DMAC support
3  *
4  * Copyright (C) 2015 Renesas Electronics Corp.
5  * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/delay.h>
12 #include <linux/of_dma.h>
13 #include "rsnd.h"
14 
15 /*
16  * Audio DMAC peri peri register
17  */
18 #define PDMASAR		0x00
19 #define PDMADAR		0x04
20 #define PDMACHCR	0x0c
21 
22 /* PDMACHCR */
23 #define PDMACHCR_DE		(1 << 0)
24 
25 
26 struct rsnd_dmaen {
27 	struct dma_chan		*chan;
28 	dma_addr_t		dma_buf;
29 	unsigned int		dma_len;
30 	unsigned int		dma_period;
31 	unsigned int		dma_cnt;
32 };
33 
34 struct rsnd_dmapp {
35 	int			dmapp_id;
36 	u32			chcr;
37 };
38 
39 struct rsnd_dma {
40 	struct rsnd_mod		mod;
41 	struct rsnd_mod		*mod_from;
42 	struct rsnd_mod		*mod_to;
43 	dma_addr_t		src_addr;
44 	dma_addr_t		dst_addr;
45 	union {
46 		struct rsnd_dmaen en;
47 		struct rsnd_dmapp pp;
48 	} dma;
49 };
50 
51 struct rsnd_dma_ctrl {
52 	void __iomem *base;
53 	int dmaen_num;
54 	int dmapp_num;
55 };
56 
57 #define rsnd_priv_to_dmac(p)	((struct rsnd_dma_ctrl *)(p)->dma)
58 #define rsnd_mod_to_dma(_mod) container_of((_mod), struct rsnd_dma, mod)
59 #define rsnd_dma_to_dmaen(dma)	(&(dma)->dma.en)
60 #define rsnd_dma_to_dmapp(dma)	(&(dma)->dma.pp)
61 
62 /*
63  *		Audio DMAC
64  */
65 #define rsnd_dmaen_sync(dmaen, io, i)	__rsnd_dmaen_sync(dmaen, io, i, 1)
66 #define rsnd_dmaen_unsync(dmaen, io, i)	__rsnd_dmaen_sync(dmaen, io, i, 0)
67 static void __rsnd_dmaen_sync(struct rsnd_dmaen *dmaen, struct rsnd_dai_stream *io,
68 			      int i, int sync)
69 {
70 	struct device *dev = dmaen->chan->device->dev;
71 	enum dma_data_direction dir;
72 	int is_play = rsnd_io_is_play(io);
73 	dma_addr_t buf;
74 	int len, max;
75 	size_t period;
76 
77 	len	= dmaen->dma_len;
78 	period	= dmaen->dma_period;
79 	max	= len / period;
80 	i	= i % max;
81 	buf	= dmaen->dma_buf + (period * i);
82 
83 	dir = is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
84 
85 	if (sync)
86 		dma_sync_single_for_device(dev, buf, period, dir);
87 	else
88 		dma_sync_single_for_cpu(dev, buf, period, dir);
89 }
90 
91 static void __rsnd_dmaen_complete(struct rsnd_mod *mod,
92 				  struct rsnd_dai_stream *io)
93 {
94 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
95 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
96 	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
97 	bool elapsed = false;
98 	unsigned long flags;
99 
100 	/*
101 	 * Renesas sound Gen1 needs 1 DMAC,
102 	 * Gen2 needs 2 DMAC.
103 	 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
104 	 * But, Audio-DMAC-peri-peri doesn't have interrupt,
105 	 * and this driver is assuming that here.
106 	 *
107 	 * If Audio-DMAC-peri-peri has interrpt,
108 	 * rsnd_dai_pointer_update() will be called twice,
109 	 * ant it will breaks io->byte_pos
110 	 */
111 	spin_lock_irqsave(&priv->lock, flags);
112 
113 	if (rsnd_io_is_working(io)) {
114 		rsnd_dmaen_unsync(dmaen, io, dmaen->dma_cnt);
115 
116 		/*
117 		 * Next period is already started.
118 		 * Let's sync Next Next period
119 		 * see
120 		 *	rsnd_dmaen_start()
121 		 */
122 		rsnd_dmaen_sync(dmaen, io, dmaen->dma_cnt + 2);
123 
124 		elapsed = rsnd_dai_pointer_update(io, io->byte_per_period);
125 
126 		dmaen->dma_cnt++;
127 	}
128 
129 	spin_unlock_irqrestore(&priv->lock, flags);
130 
131 	if (elapsed)
132 		rsnd_dai_period_elapsed(io);
133 }
134 
135 static void rsnd_dmaen_complete(void *data)
136 {
137 	struct rsnd_mod *mod = data;
138 
139 	rsnd_mod_interrupt(mod, __rsnd_dmaen_complete);
140 }
141 
142 static struct dma_chan *rsnd_dmaen_request_channel(struct rsnd_dai_stream *io,
143 						   struct rsnd_mod *mod_from,
144 						   struct rsnd_mod *mod_to)
145 {
146 	if ((!mod_from && !mod_to) ||
147 	    (mod_from && mod_to))
148 		return NULL;
149 
150 	if (mod_from)
151 		return rsnd_mod_dma_req(io, mod_from);
152 	else
153 		return rsnd_mod_dma_req(io, mod_to);
154 }
155 
156 static int rsnd_dmaen_stop(struct rsnd_mod *mod,
157 			   struct rsnd_dai_stream *io,
158 			   struct rsnd_priv *priv)
159 {
160 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
161 	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
162 
163 	if (dmaen->chan) {
164 		int is_play = rsnd_io_is_play(io);
165 
166 		dmaengine_terminate_all(dmaen->chan);
167 		dma_unmap_single(dmaen->chan->device->dev,
168 				 dmaen->dma_buf, dmaen->dma_len,
169 				 is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
170 	}
171 
172 	return 0;
173 }
174 
175 static int rsnd_dmaen_nolock_stop(struct rsnd_mod *mod,
176 				   struct rsnd_dai_stream *io,
177 				   struct rsnd_priv *priv)
178 {
179 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
180 	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
181 
182 	/*
183 	 * DMAEngine release uses mutex lock.
184 	 * Thus, it shouldn't be called under spinlock.
185 	 * Let's call it under nolock_start
186 	 */
187 	if (dmaen->chan)
188 		dma_release_channel(dmaen->chan);
189 
190 	dmaen->chan = NULL;
191 
192 	return 0;
193 }
194 
195 static int rsnd_dmaen_nolock_start(struct rsnd_mod *mod,
196 			    struct rsnd_dai_stream *io,
197 			    struct rsnd_priv *priv)
198 {
199 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
200 	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
201 	struct device *dev = rsnd_priv_to_dev(priv);
202 
203 	if (dmaen->chan) {
204 		dev_err(dev, "it already has dma channel\n");
205 		return -EIO;
206 	}
207 
208 	/*
209 	 * DMAEngine request uses mutex lock.
210 	 * Thus, it shouldn't be called under spinlock.
211 	 * Let's call it under nolock_start
212 	 */
213 	dmaen->chan = rsnd_dmaen_request_channel(io,
214 						 dma->mod_from,
215 						 dma->mod_to);
216 	if (IS_ERR_OR_NULL(dmaen->chan)) {
217 		int ret = PTR_ERR(dmaen->chan);
218 
219 		dmaen->chan = NULL;
220 		dev_err(dev, "can't get dma channel\n");
221 		return ret;
222 	}
223 
224 	return 0;
225 }
226 
227 static int rsnd_dmaen_start(struct rsnd_mod *mod,
228 			    struct rsnd_dai_stream *io,
229 			    struct rsnd_priv *priv)
230 {
231 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
232 	struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
233 	struct snd_pcm_substream *substream = io->substream;
234 	struct device *dev = rsnd_priv_to_dev(priv);
235 	struct dma_async_tx_descriptor *desc;
236 	struct dma_slave_config cfg = {};
237 	dma_addr_t buf;
238 	size_t len;
239 	size_t period;
240 	int is_play = rsnd_io_is_play(io);
241 	int i;
242 	int ret;
243 
244 	cfg.direction	= is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
245 	cfg.src_addr	= dma->src_addr;
246 	cfg.dst_addr	= dma->dst_addr;
247 	cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
248 	cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
249 
250 	dev_dbg(dev, "%s[%d] %pad -> %pad\n",
251 		rsnd_mod_name(mod), rsnd_mod_id(mod),
252 		&cfg.src_addr, &cfg.dst_addr);
253 
254 	ret = dmaengine_slave_config(dmaen->chan, &cfg);
255 	if (ret < 0)
256 		return ret;
257 
258 	len	= snd_pcm_lib_buffer_bytes(substream);
259 	period	= snd_pcm_lib_period_bytes(substream);
260 	buf	= dma_map_single(dmaen->chan->device->dev,
261 				 substream->runtime->dma_area,
262 				 len,
263 				 is_play ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
264 	if (dma_mapping_error(dmaen->chan->device->dev, buf)) {
265 		dev_err(dev, "dma map failed\n");
266 		return -EIO;
267 	}
268 
269 	desc = dmaengine_prep_dma_cyclic(dmaen->chan,
270 					 buf, len, period,
271 					 is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
272 					 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
273 
274 	if (!desc) {
275 		dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
276 		return -EIO;
277 	}
278 
279 	desc->callback		= rsnd_dmaen_complete;
280 	desc->callback_param	= rsnd_mod_get(dma);
281 
282 	dmaen->dma_buf		= buf;
283 	dmaen->dma_len		= len;
284 	dmaen->dma_period	= period;
285 	dmaen->dma_cnt		= 0;
286 
287 	/*
288 	 * synchronize this and next period
289 	 * see
290 	 *	__rsnd_dmaen_complete()
291 	 */
292 	for (i = 0; i < 2; i++)
293 		rsnd_dmaen_sync(dmaen, io, i);
294 
295 	if (dmaengine_submit(desc) < 0) {
296 		dev_err(dev, "dmaengine_submit() fail\n");
297 		return -EIO;
298 	}
299 
300 	dma_async_issue_pending(dmaen->chan);
301 
302 	return 0;
303 }
304 
305 struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node,
306 					  struct rsnd_mod *mod, char *name)
307 {
308 	struct dma_chan *chan = NULL;
309 	struct device_node *np;
310 	int i = 0;
311 
312 	for_each_child_of_node(of_node, np) {
313 		if (i == rsnd_mod_id(mod) && (!chan))
314 			chan = of_dma_request_slave_channel(np, name);
315 		i++;
316 	}
317 
318 	/* It should call of_node_put(), since, it is rsnd_xxx_of_node() */
319 	of_node_put(of_node);
320 
321 	return chan;
322 }
323 
324 static int rsnd_dmaen_attach(struct rsnd_dai_stream *io,
325 			   struct rsnd_dma *dma,
326 			   struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
327 {
328 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
329 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
330 	struct dma_chan *chan;
331 
332 	/* try to get DMAEngine channel */
333 	chan = rsnd_dmaen_request_channel(io, mod_from, mod_to);
334 	if (IS_ERR_OR_NULL(chan)) {
335 		/*
336 		 * DMA failed. try to PIO mode
337 		 * see
338 		 *	rsnd_ssi_fallback()
339 		 *	rsnd_rdai_continuance_probe()
340 		 */
341 		return -EAGAIN;
342 	}
343 
344 	dma_release_channel(chan);
345 
346 	dmac->dmaen_num++;
347 
348 	return 0;
349 }
350 
351 static struct rsnd_mod_ops rsnd_dmaen_ops = {
352 	.name	= "audmac",
353 	.nolock_start = rsnd_dmaen_nolock_start,
354 	.nolock_stop  = rsnd_dmaen_nolock_stop,
355 	.start	= rsnd_dmaen_start,
356 	.stop	= rsnd_dmaen_stop,
357 };
358 
359 /*
360  *		Audio DMAC peri peri
361  */
362 static const u8 gen2_id_table_ssiu[] = {
363 	0x00, /* SSI00 */
364 	0x04, /* SSI10 */
365 	0x08, /* SSI20 */
366 	0x0c, /* SSI3  */
367 	0x0d, /* SSI4  */
368 	0x0e, /* SSI5  */
369 	0x0f, /* SSI6  */
370 	0x10, /* SSI7  */
371 	0x11, /* SSI8  */
372 	0x12, /* SSI90 */
373 };
374 static const u8 gen2_id_table_scu[] = {
375 	0x2d, /* SCU_SRCI0 */
376 	0x2e, /* SCU_SRCI1 */
377 	0x2f, /* SCU_SRCI2 */
378 	0x30, /* SCU_SRCI3 */
379 	0x31, /* SCU_SRCI4 */
380 	0x32, /* SCU_SRCI5 */
381 	0x33, /* SCU_SRCI6 */
382 	0x34, /* SCU_SRCI7 */
383 	0x35, /* SCU_SRCI8 */
384 	0x36, /* SCU_SRCI9 */
385 };
386 static const u8 gen2_id_table_cmd[] = {
387 	0x37, /* SCU_CMD0 */
388 	0x38, /* SCU_CMD1 */
389 };
390 
391 static u32 rsnd_dmapp_get_id(struct rsnd_dai_stream *io,
392 			     struct rsnd_mod *mod)
393 {
394 	struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
395 	struct rsnd_mod *src = rsnd_io_to_mod_src(io);
396 	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
397 	const u8 *entry = NULL;
398 	int id = rsnd_mod_id(mod);
399 	int size = 0;
400 
401 	if (mod == ssi) {
402 		entry = gen2_id_table_ssiu;
403 		size = ARRAY_SIZE(gen2_id_table_ssiu);
404 	} else if (mod == src) {
405 		entry = gen2_id_table_scu;
406 		size = ARRAY_SIZE(gen2_id_table_scu);
407 	} else if (mod == dvc) {
408 		entry = gen2_id_table_cmd;
409 		size = ARRAY_SIZE(gen2_id_table_cmd);
410 	}
411 
412 	if ((!entry) || (size <= id)) {
413 		struct device *dev = rsnd_priv_to_dev(rsnd_io_to_priv(io));
414 
415 		dev_err(dev, "unknown connection (%s[%d])\n",
416 			rsnd_mod_name(mod), rsnd_mod_id(mod));
417 
418 		/* use non-prohibited SRS number as error */
419 		return 0x00; /* SSI00 */
420 	}
421 
422 	return entry[id];
423 }
424 
425 static u32 rsnd_dmapp_get_chcr(struct rsnd_dai_stream *io,
426 			       struct rsnd_mod *mod_from,
427 			       struct rsnd_mod *mod_to)
428 {
429 	return	(rsnd_dmapp_get_id(io, mod_from) << 24) +
430 		(rsnd_dmapp_get_id(io, mod_to) << 16);
431 }
432 
433 #define rsnd_dmapp_addr(dmac, dma, reg) \
434 	(dmac->base + 0x20 + reg + \
435 	 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
436 static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
437 {
438 	struct rsnd_mod *mod = rsnd_mod_get(dma);
439 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
440 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
441 	struct device *dev = rsnd_priv_to_dev(priv);
442 
443 	dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
444 
445 	iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
446 }
447 
448 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
449 {
450 	struct rsnd_mod *mod = rsnd_mod_get(dma);
451 	struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
452 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
453 
454 	return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
455 }
456 
457 static int rsnd_dmapp_stop(struct rsnd_mod *mod,
458 			   struct rsnd_dai_stream *io,
459 			   struct rsnd_priv *priv)
460 {
461 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
462 	int i;
463 
464 	rsnd_dmapp_write(dma, 0, PDMACHCR);
465 
466 	for (i = 0; i < 1024; i++) {
467 		if (0 == rsnd_dmapp_read(dma, PDMACHCR))
468 			return 0;
469 		udelay(1);
470 	}
471 
472 	return -EIO;
473 }
474 
475 static int rsnd_dmapp_start(struct rsnd_mod *mod,
476 			    struct rsnd_dai_stream *io,
477 			    struct rsnd_priv *priv)
478 {
479 	struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
480 	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
481 
482 	rsnd_dmapp_write(dma, dma->src_addr,	PDMASAR);
483 	rsnd_dmapp_write(dma, dma->dst_addr,	PDMADAR);
484 	rsnd_dmapp_write(dma, dmapp->chcr,	PDMACHCR);
485 
486 	return 0;
487 }
488 
489 static int rsnd_dmapp_attach(struct rsnd_dai_stream *io,
490 			     struct rsnd_dma *dma,
491 			     struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
492 {
493 	struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
494 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
495 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
496 	struct device *dev = rsnd_priv_to_dev(priv);
497 
498 	dmapp->dmapp_id = dmac->dmapp_num;
499 	dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;
500 
501 	dmac->dmapp_num++;
502 
503 	dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
504 		dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
505 
506 	return 0;
507 }
508 
509 static struct rsnd_mod_ops rsnd_dmapp_ops = {
510 	.name	= "audmac-pp",
511 	.start	= rsnd_dmapp_start,
512 	.stop	= rsnd_dmapp_stop,
513 	.quit	= rsnd_dmapp_stop,
514 };
515 
516 /*
517  *		Common DMAC Interface
518  */
519 
520 /*
521  *	DMA read/write register offset
522  *
523  *	RSND_xxx_I_N	for Audio DMAC input
524  *	RSND_xxx_O_N	for Audio DMAC output
525  *	RSND_xxx_I_P	for Audio DMAC peri peri input
526  *	RSND_xxx_O_P	for Audio DMAC peri peri output
527  *
528  *	ex) R-Car H2 case
529  *	      mod        / DMAC in    / DMAC out   / DMAC PP in / DMAC pp out
530  *	SSI : 0xec541000 / 0xec241008 / 0xec24100c
531  *	SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
532  *	SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
533  *	CMD : 0xec500000 /            / 0xec008000                0xec308000
534  */
535 #define RDMA_SSI_I_N(addr, i)	(addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
536 #define RDMA_SSI_O_N(addr, i)	(addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
537 
538 #define RDMA_SSIU_I_N(addr, i)	(addr ##_reg - 0x00441000 + (0x1000 * i))
539 #define RDMA_SSIU_O_N(addr, i)	(addr ##_reg - 0x00441000 + (0x1000 * i))
540 
541 #define RDMA_SSIU_I_P(addr, i)	(addr ##_reg - 0x00141000 + (0x1000 * i))
542 #define RDMA_SSIU_O_P(addr, i)	(addr ##_reg - 0x00141000 + (0x1000 * i))
543 
544 #define RDMA_SRC_I_N(addr, i)	(addr ##_reg - 0x00500000 + (0x400 * i))
545 #define RDMA_SRC_O_N(addr, i)	(addr ##_reg - 0x004fc000 + (0x400 * i))
546 
547 #define RDMA_SRC_I_P(addr, i)	(addr ##_reg - 0x00200000 + (0x400 * i))
548 #define RDMA_SRC_O_P(addr, i)	(addr ##_reg - 0x001fc000 + (0x400 * i))
549 
550 #define RDMA_CMD_O_N(addr, i)	(addr ##_reg - 0x004f8000 + (0x400 * i))
551 #define RDMA_CMD_O_P(addr, i)	(addr ##_reg - 0x001f8000 + (0x400 * i))
552 
553 static dma_addr_t
554 rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,
555 		   struct rsnd_mod *mod,
556 		   int is_play, int is_from)
557 {
558 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
559 	struct device *dev = rsnd_priv_to_dev(priv);
560 	phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
561 	phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
562 	int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod);
563 	int use_src = !!rsnd_io_to_mod_src(io);
564 	int use_cmd = !!rsnd_io_to_mod_dvc(io) ||
565 		      !!rsnd_io_to_mod_mix(io) ||
566 		      !!rsnd_io_to_mod_ctu(io);
567 	int id = rsnd_mod_id(mod);
568 	struct dma_addr {
569 		dma_addr_t out_addr;
570 		dma_addr_t in_addr;
571 	} dma_addrs[3][2][3] = {
572 		/* SRC */
573 		{{{ 0,				0 },
574 		  /* Capture */
575 		  { RDMA_SRC_O_N(src, id),	RDMA_SRC_I_P(src, id) },
576 		  { RDMA_CMD_O_N(src, id),	RDMA_SRC_I_P(src, id) } },
577 		 /* Playback */
578 		 {{ 0,				0, },
579 		  { RDMA_SRC_O_P(src, id),	RDMA_SRC_I_N(src, id) },
580 		  { RDMA_CMD_O_P(src, id),	RDMA_SRC_I_N(src, id) } }
581 		},
582 		/* SSI */
583 		/* Capture */
584 		{{{ RDMA_SSI_O_N(ssi, id),	0 },
585 		  { RDMA_SSIU_O_P(ssi, id),	0 },
586 		  { RDMA_SSIU_O_P(ssi, id),	0 } },
587 		 /* Playback */
588 		 {{ 0,				RDMA_SSI_I_N(ssi, id) },
589 		  { 0,				RDMA_SSIU_I_P(ssi, id) },
590 		  { 0,				RDMA_SSIU_I_P(ssi, id) } }
591 		},
592 		/* SSIU */
593 		/* Capture */
594 		{{{ RDMA_SSIU_O_N(ssi, id),	0 },
595 		  { RDMA_SSIU_O_P(ssi, id),	0 },
596 		  { RDMA_SSIU_O_P(ssi, id),	0 } },
597 		 /* Playback */
598 		 {{ 0,				RDMA_SSIU_I_N(ssi, id) },
599 		  { 0,				RDMA_SSIU_I_P(ssi, id) },
600 		  { 0,				RDMA_SSIU_I_P(ssi, id) } } },
601 	};
602 
603 	/* it shouldn't happen */
604 	if (use_cmd && !use_src)
605 		dev_err(dev, "DVC is selected without SRC\n");
606 
607 	/* use SSIU or SSI ? */
608 	if (is_ssi && rsnd_ssi_use_busif(io))
609 		is_ssi++;
610 
611 	return (is_from) ?
612 		dma_addrs[is_ssi][is_play][use_src + use_cmd].out_addr :
613 		dma_addrs[is_ssi][is_play][use_src + use_cmd].in_addr;
614 }
615 
616 static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,
617 				struct rsnd_mod *mod,
618 				int is_play, int is_from)
619 {
620 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
621 
622 	/*
623 	 * gen1 uses default DMA addr
624 	 */
625 	if (rsnd_is_gen1(priv))
626 		return 0;
627 
628 	if (!mod)
629 		return 0;
630 
631 	return rsnd_gen2_dma_addr(io, mod, is_play, is_from);
632 }
633 
634 #define MOD_MAX (RSND_MOD_MAX + 1) /* +Memory */
635 static void rsnd_dma_of_path(struct rsnd_mod *this,
636 			     struct rsnd_dai_stream *io,
637 			     int is_play,
638 			     struct rsnd_mod **mod_from,
639 			     struct rsnd_mod **mod_to)
640 {
641 	struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
642 	struct rsnd_mod *src = rsnd_io_to_mod_src(io);
643 	struct rsnd_mod *ctu = rsnd_io_to_mod_ctu(io);
644 	struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
645 	struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
646 	struct rsnd_mod *mod[MOD_MAX];
647 	struct rsnd_mod *mod_start, *mod_end;
648 	struct rsnd_priv *priv = rsnd_mod_to_priv(this);
649 	struct device *dev = rsnd_priv_to_dev(priv);
650 	int nr, i, idx;
651 
652 	if (!ssi)
653 		return;
654 
655 	nr = 0;
656 	for (i = 0; i < MOD_MAX; i++) {
657 		mod[i] = NULL;
658 		nr += !!rsnd_io_to_mod(io, i);
659 	}
660 
661 	/*
662 	 * [S] -*-> [E]
663 	 * [S] -*-> SRC -o-> [E]
664 	 * [S] -*-> SRC -> DVC -o-> [E]
665 	 * [S] -*-> SRC -> CTU -> MIX -> DVC -o-> [E]
666 	 *
667 	 * playback	[S] = mem
668 	 *		[E] = SSI
669 	 *
670 	 * capture	[S] = SSI
671 	 *		[E] = mem
672 	 *
673 	 * -*->		Audio DMAC
674 	 * -o->		Audio DMAC peri peri
675 	 */
676 	mod_start	= (is_play) ? NULL : ssi;
677 	mod_end		= (is_play) ? ssi  : NULL;
678 
679 	idx = 0;
680 	mod[idx++] = mod_start;
681 	for (i = 1; i < nr; i++) {
682 		if (src) {
683 			mod[idx++] = src;
684 			src = NULL;
685 		} else if (ctu) {
686 			mod[idx++] = ctu;
687 			ctu = NULL;
688 		} else if (mix) {
689 			mod[idx++] = mix;
690 			mix = NULL;
691 		} else if (dvc) {
692 			mod[idx++] = dvc;
693 			dvc = NULL;
694 		}
695 	}
696 	mod[idx] = mod_end;
697 
698 	/*
699 	 *		| SSI | SRC |
700 	 * -------------+-----+-----+
701 	 *  is_play	|  o  |  *  |
702 	 * !is_play	|  *  |  o  |
703 	 */
704 	if ((this == ssi) == (is_play)) {
705 		*mod_from	= mod[idx - 1];
706 		*mod_to		= mod[idx];
707 	} else {
708 		*mod_from	= mod[0];
709 		*mod_to		= mod[1];
710 	}
711 
712 	dev_dbg(dev, "module connection (this is %s[%d])\n",
713 		rsnd_mod_name(this), rsnd_mod_id(this));
714 	for (i = 0; i <= idx; i++) {
715 		dev_dbg(dev, "  %s[%d]%s\n",
716 		       rsnd_mod_name(mod[i]), rsnd_mod_id(mod[i]),
717 		       (mod[i] == *mod_from) ? " from" :
718 		       (mod[i] == *mod_to)   ? " to" : "");
719 	}
720 }
721 
722 int rsnd_dma_attach(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
723 		    struct rsnd_mod **dma_mod)
724 {
725 	struct rsnd_mod *mod_from = NULL;
726 	struct rsnd_mod *mod_to = NULL;
727 	struct rsnd_priv *priv = rsnd_io_to_priv(io);
728 	struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
729 	struct device *dev = rsnd_priv_to_dev(priv);
730 	struct rsnd_mod_ops *ops;
731 	enum rsnd_mod_type type;
732 	int (*attach)(struct rsnd_dai_stream *io, struct rsnd_dma *dma,
733 		      struct rsnd_mod *mod_from, struct rsnd_mod *mod_to);
734 	int is_play = rsnd_io_is_play(io);
735 	int ret, dma_id;
736 
737 	/*
738 	 * DMA failed. try to PIO mode
739 	 * see
740 	 *	rsnd_ssi_fallback()
741 	 *	rsnd_rdai_continuance_probe()
742 	 */
743 	if (!dmac)
744 		return -EAGAIN;
745 
746 	rsnd_dma_of_path(mod, io, is_play, &mod_from, &mod_to);
747 
748 	/* for Gen2 */
749 	if (mod_from && mod_to) {
750 		ops	= &rsnd_dmapp_ops;
751 		attach	= rsnd_dmapp_attach;
752 		dma_id	= dmac->dmapp_num;
753 		type	= RSND_MOD_AUDMAPP;
754 	} else {
755 		ops	= &rsnd_dmaen_ops;
756 		attach	= rsnd_dmaen_attach;
757 		dma_id	= dmac->dmaen_num;
758 		type	= RSND_MOD_AUDMA;
759 	}
760 
761 	/* for Gen1, overwrite */
762 	if (rsnd_is_gen1(priv)) {
763 		ops	= &rsnd_dmaen_ops;
764 		attach	= rsnd_dmaen_attach;
765 		dma_id	= dmac->dmaen_num;
766 		type	= RSND_MOD_AUDMA;
767 	}
768 
769 	if (!(*dma_mod)) {
770 		struct rsnd_dma *dma;
771 
772 		dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
773 		if (!dma)
774 			return -ENOMEM;
775 
776 		*dma_mod = rsnd_mod_get(dma);
777 
778 		ret = rsnd_mod_init(priv, *dma_mod, ops, NULL,
779 				    rsnd_mod_get_status, type, dma_id);
780 		if (ret < 0)
781 			return ret;
782 
783 		dev_dbg(dev, "%s[%d] %s[%d] -> %s[%d]\n",
784 			rsnd_mod_name(*dma_mod), rsnd_mod_id(*dma_mod),
785 			rsnd_mod_name(mod_from), rsnd_mod_id(mod_from),
786 			rsnd_mod_name(mod_to),   rsnd_mod_id(mod_to));
787 
788 		ret = attach(io, dma, mod_from, mod_to);
789 		if (ret < 0)
790 			return ret;
791 
792 		dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);
793 		dma->dst_addr = rsnd_dma_addr(io, mod_to,   is_play, 0);
794 		dma->mod_from = mod_from;
795 		dma->mod_to   = mod_to;
796 	}
797 
798 	ret = rsnd_dai_connect(*dma_mod, io, type);
799 	if (ret < 0)
800 		return ret;
801 
802 	return 0;
803 }
804 
805 int rsnd_dma_probe(struct rsnd_priv *priv)
806 {
807 	struct platform_device *pdev = rsnd_priv_to_pdev(priv);
808 	struct device *dev = rsnd_priv_to_dev(priv);
809 	struct rsnd_dma_ctrl *dmac;
810 	struct resource *res;
811 
812 	/*
813 	 * for Gen1
814 	 */
815 	if (rsnd_is_gen1(priv))
816 		return 0;
817 
818 	/*
819 	 * for Gen2
820 	 */
821 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
822 	dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
823 	if (!dmac || !res) {
824 		dev_err(dev, "dma allocate failed\n");
825 		return 0; /* it will be PIO mode */
826 	}
827 
828 	dmac->dmapp_num = 0;
829 	dmac->base = devm_ioremap_resource(dev, res);
830 	if (IS_ERR(dmac->base))
831 		return PTR_ERR(dmac->base);
832 
833 	priv->dma = dmac;
834 
835 	return 0;
836 }
837