]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/soc/sh/rcar/dma.c
Merge remote-tracking branches 'asoc/topic/adsp', 'asoc/topic/ak4613', 'asoc/topic...
[mirror_ubuntu-bionic-kernel.git] / sound / soc / sh / rcar / dma.c
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 void rsnd_dmapp_bset(struct rsnd_dma *dma, u32 data, u32 mask, u32 reg)
458 {
459 struct rsnd_mod *mod = rsnd_mod_get(dma);
460 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
461 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
462 void __iomem *addr = rsnd_dmapp_addr(dmac, dma, reg);
463 u32 val = ioread32(addr);
464
465 val &= ~mask;
466 val |= (data & mask);
467
468 iowrite32(val, addr);
469 }
470
471 static int rsnd_dmapp_stop(struct rsnd_mod *mod,
472 struct rsnd_dai_stream *io,
473 struct rsnd_priv *priv)
474 {
475 struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
476 int i;
477
478 rsnd_dmapp_bset(dma, 0, PDMACHCR_DE, PDMACHCR);
479
480 for (i = 0; i < 1024; i++) {
481 if (0 == (rsnd_dmapp_read(dma, PDMACHCR) & PDMACHCR_DE))
482 return 0;
483 udelay(1);
484 }
485
486 return -EIO;
487 }
488
489 static int rsnd_dmapp_start(struct rsnd_mod *mod,
490 struct rsnd_dai_stream *io,
491 struct rsnd_priv *priv)
492 {
493 struct rsnd_dma *dma = rsnd_mod_to_dma(mod);
494 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
495
496 rsnd_dmapp_write(dma, dma->src_addr, PDMASAR);
497 rsnd_dmapp_write(dma, dma->dst_addr, PDMADAR);
498 rsnd_dmapp_write(dma, dmapp->chcr, PDMACHCR);
499
500 return 0;
501 }
502
503 static int rsnd_dmapp_attach(struct rsnd_dai_stream *io,
504 struct rsnd_dma *dma,
505 struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
506 {
507 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
508 struct rsnd_priv *priv = rsnd_io_to_priv(io);
509 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
510 struct device *dev = rsnd_priv_to_dev(priv);
511
512 dmapp->dmapp_id = dmac->dmapp_num;
513 dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;
514
515 dmac->dmapp_num++;
516
517 dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
518 dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
519
520 return 0;
521 }
522
523 static struct rsnd_mod_ops rsnd_dmapp_ops = {
524 .name = "audmac-pp",
525 .start = rsnd_dmapp_start,
526 .stop = rsnd_dmapp_stop,
527 .quit = rsnd_dmapp_stop,
528 };
529
530 /*
531 * Common DMAC Interface
532 */
533
534 /*
535 * DMA read/write register offset
536 *
537 * RSND_xxx_I_N for Audio DMAC input
538 * RSND_xxx_O_N for Audio DMAC output
539 * RSND_xxx_I_P for Audio DMAC peri peri input
540 * RSND_xxx_O_P for Audio DMAC peri peri output
541 *
542 * ex) R-Car H2 case
543 * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out
544 * SSI : 0xec541000 / 0xec241008 / 0xec24100c
545 * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
546 * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
547 * CMD : 0xec500000 / / 0xec008000 0xec308000
548 */
549 #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
550 #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
551
552 #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
553 #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
554
555 #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
556 #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
557
558 #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i))
559 #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i))
560
561 #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i))
562 #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i))
563
564 #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i))
565 #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i))
566
567 static dma_addr_t
568 rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,
569 struct rsnd_mod *mod,
570 int is_play, int is_from)
571 {
572 struct rsnd_priv *priv = rsnd_io_to_priv(io);
573 struct device *dev = rsnd_priv_to_dev(priv);
574 phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
575 phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
576 int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod);
577 int use_src = !!rsnd_io_to_mod_src(io);
578 int use_cmd = !!rsnd_io_to_mod_dvc(io) ||
579 !!rsnd_io_to_mod_mix(io) ||
580 !!rsnd_io_to_mod_ctu(io);
581 int id = rsnd_mod_id(mod);
582 struct dma_addr {
583 dma_addr_t out_addr;
584 dma_addr_t in_addr;
585 } dma_addrs[3][2][3] = {
586 /* SRC */
587 {{{ 0, 0 },
588 /* Capture */
589 { RDMA_SRC_O_N(src, id), RDMA_SRC_I_P(src, id) },
590 { RDMA_CMD_O_N(src, id), RDMA_SRC_I_P(src, id) } },
591 /* Playback */
592 {{ 0, 0, },
593 { RDMA_SRC_O_P(src, id), RDMA_SRC_I_N(src, id) },
594 { RDMA_CMD_O_P(src, id), RDMA_SRC_I_N(src, id) } }
595 },
596 /* SSI */
597 /* Capture */
598 {{{ RDMA_SSI_O_N(ssi, id), 0 },
599 { RDMA_SSIU_O_P(ssi, id), 0 },
600 { RDMA_SSIU_O_P(ssi, id), 0 } },
601 /* Playback */
602 {{ 0, RDMA_SSI_I_N(ssi, id) },
603 { 0, RDMA_SSIU_I_P(ssi, id) },
604 { 0, RDMA_SSIU_I_P(ssi, id) } }
605 },
606 /* SSIU */
607 /* Capture */
608 {{{ RDMA_SSIU_O_N(ssi, id), 0 },
609 { RDMA_SSIU_O_P(ssi, id), 0 },
610 { RDMA_SSIU_O_P(ssi, id), 0 } },
611 /* Playback */
612 {{ 0, RDMA_SSIU_I_N(ssi, id) },
613 { 0, RDMA_SSIU_I_P(ssi, id) },
614 { 0, RDMA_SSIU_I_P(ssi, id) } } },
615 };
616
617 /* it shouldn't happen */
618 if (use_cmd && !use_src)
619 dev_err(dev, "DVC is selected without SRC\n");
620
621 /* use SSIU or SSI ? */
622 if (is_ssi && rsnd_ssi_use_busif(io))
623 is_ssi++;
624
625 return (is_from) ?
626 dma_addrs[is_ssi][is_play][use_src + use_cmd].out_addr :
627 dma_addrs[is_ssi][is_play][use_src + use_cmd].in_addr;
628 }
629
630 static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,
631 struct rsnd_mod *mod,
632 int is_play, int is_from)
633 {
634 struct rsnd_priv *priv = rsnd_io_to_priv(io);
635
636 /*
637 * gen1 uses default DMA addr
638 */
639 if (rsnd_is_gen1(priv))
640 return 0;
641
642 if (!mod)
643 return 0;
644
645 return rsnd_gen2_dma_addr(io, mod, is_play, is_from);
646 }
647
648 #define MOD_MAX (RSND_MOD_MAX + 1) /* +Memory */
649 static void rsnd_dma_of_path(struct rsnd_mod *this,
650 struct rsnd_dai_stream *io,
651 int is_play,
652 struct rsnd_mod **mod_from,
653 struct rsnd_mod **mod_to)
654 {
655 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
656 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
657 struct rsnd_mod *ctu = rsnd_io_to_mod_ctu(io);
658 struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
659 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
660 struct rsnd_mod *mod[MOD_MAX];
661 struct rsnd_mod *mod_start, *mod_end;
662 struct rsnd_priv *priv = rsnd_mod_to_priv(this);
663 struct device *dev = rsnd_priv_to_dev(priv);
664 int nr, i, idx;
665
666 if (!ssi)
667 return;
668
669 nr = 0;
670 for (i = 0; i < MOD_MAX; i++) {
671 mod[i] = NULL;
672 nr += !!rsnd_io_to_mod(io, i);
673 }
674
675 /*
676 * [S] -*-> [E]
677 * [S] -*-> SRC -o-> [E]
678 * [S] -*-> SRC -> DVC -o-> [E]
679 * [S] -*-> SRC -> CTU -> MIX -> DVC -o-> [E]
680 *
681 * playback [S] = mem
682 * [E] = SSI
683 *
684 * capture [S] = SSI
685 * [E] = mem
686 *
687 * -*-> Audio DMAC
688 * -o-> Audio DMAC peri peri
689 */
690 mod_start = (is_play) ? NULL : ssi;
691 mod_end = (is_play) ? ssi : NULL;
692
693 idx = 0;
694 mod[idx++] = mod_start;
695 for (i = 1; i < nr; i++) {
696 if (src) {
697 mod[idx++] = src;
698 src = NULL;
699 } else if (ctu) {
700 mod[idx++] = ctu;
701 ctu = NULL;
702 } else if (mix) {
703 mod[idx++] = mix;
704 mix = NULL;
705 } else if (dvc) {
706 mod[idx++] = dvc;
707 dvc = NULL;
708 }
709 }
710 mod[idx] = mod_end;
711
712 /*
713 * | SSI | SRC |
714 * -------------+-----+-----+
715 * is_play | o | * |
716 * !is_play | * | o |
717 */
718 if ((this == ssi) == (is_play)) {
719 *mod_from = mod[idx - 1];
720 *mod_to = mod[idx];
721 } else {
722 *mod_from = mod[0];
723 *mod_to = mod[1];
724 }
725
726 dev_dbg(dev, "module connection (this is %s[%d])\n",
727 rsnd_mod_name(this), rsnd_mod_id(this));
728 for (i = 0; i <= idx; i++) {
729 dev_dbg(dev, " %s[%d]%s\n",
730 rsnd_mod_name(mod[i]), rsnd_mod_id(mod[i]),
731 (mod[i] == *mod_from) ? " from" :
732 (mod[i] == *mod_to) ? " to" : "");
733 }
734 }
735
736 int rsnd_dma_attach(struct rsnd_dai_stream *io, struct rsnd_mod *mod,
737 struct rsnd_mod **dma_mod)
738 {
739 struct rsnd_mod *mod_from = NULL;
740 struct rsnd_mod *mod_to = NULL;
741 struct rsnd_priv *priv = rsnd_io_to_priv(io);
742 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
743 struct device *dev = rsnd_priv_to_dev(priv);
744 struct rsnd_mod_ops *ops;
745 enum rsnd_mod_type type;
746 int (*attach)(struct rsnd_dai_stream *io, struct rsnd_dma *dma,
747 struct rsnd_mod *mod_from, struct rsnd_mod *mod_to);
748 int is_play = rsnd_io_is_play(io);
749 int ret, dma_id;
750
751 /*
752 * DMA failed. try to PIO mode
753 * see
754 * rsnd_ssi_fallback()
755 * rsnd_rdai_continuance_probe()
756 */
757 if (!dmac)
758 return -EAGAIN;
759
760 rsnd_dma_of_path(mod, io, is_play, &mod_from, &mod_to);
761
762 /* for Gen2 */
763 if (mod_from && mod_to) {
764 ops = &rsnd_dmapp_ops;
765 attach = rsnd_dmapp_attach;
766 dma_id = dmac->dmapp_num;
767 type = RSND_MOD_AUDMAPP;
768 } else {
769 ops = &rsnd_dmaen_ops;
770 attach = rsnd_dmaen_attach;
771 dma_id = dmac->dmaen_num;
772 type = RSND_MOD_AUDMA;
773 }
774
775 /* for Gen1, overwrite */
776 if (rsnd_is_gen1(priv)) {
777 ops = &rsnd_dmaen_ops;
778 attach = rsnd_dmaen_attach;
779 dma_id = dmac->dmaen_num;
780 type = RSND_MOD_AUDMA;
781 }
782
783 if (!(*dma_mod)) {
784 struct rsnd_dma *dma;
785
786 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
787 if (!dma)
788 return -ENOMEM;
789
790 *dma_mod = rsnd_mod_get(dma);
791
792 ret = rsnd_mod_init(priv, *dma_mod, ops, NULL,
793 rsnd_mod_get_status, type, dma_id);
794 if (ret < 0)
795 return ret;
796
797 dev_dbg(dev, "%s[%d] %s[%d] -> %s[%d]\n",
798 rsnd_mod_name(*dma_mod), rsnd_mod_id(*dma_mod),
799 rsnd_mod_name(mod_from), rsnd_mod_id(mod_from),
800 rsnd_mod_name(mod_to), rsnd_mod_id(mod_to));
801
802 ret = attach(io, dma, mod_from, mod_to);
803 if (ret < 0)
804 return ret;
805
806 dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);
807 dma->dst_addr = rsnd_dma_addr(io, mod_to, is_play, 0);
808 dma->mod_from = mod_from;
809 dma->mod_to = mod_to;
810 }
811
812 ret = rsnd_dai_connect(*dma_mod, io, type);
813 if (ret < 0)
814 return ret;
815
816 return 0;
817 }
818
819 int rsnd_dma_probe(struct rsnd_priv *priv)
820 {
821 struct platform_device *pdev = rsnd_priv_to_pdev(priv);
822 struct device *dev = rsnd_priv_to_dev(priv);
823 struct rsnd_dma_ctrl *dmac;
824 struct resource *res;
825
826 /*
827 * for Gen1
828 */
829 if (rsnd_is_gen1(priv))
830 return 0;
831
832 /*
833 * for Gen2
834 */
835 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
836 dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
837 if (!dmac || !res) {
838 dev_err(dev, "dma allocate failed\n");
839 return 0; /* it will be PIO mode */
840 }
841
842 dmac->dmapp_num = 0;
843 dmac->base = devm_ioremap_resource(dev, res);
844 if (IS_ERR(dmac->base))
845 return PTR_ERR(dmac->base);
846
847 priv->dma = dmac;
848
849 return 0;
850 }