2 * Renesas R-Car Audio DMAC support
4 * Copyright (C) 2015 Renesas Electronics Corp.
5 * Copyright (c) 2015 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
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.
11 #include <linux/delay.h>
12 #include <linux/of_dma.h>
16 * Audio DMAC peri peri register
23 #define PDMACHCR_DE (1 << 0)
25 struct rsnd_dma_ctrl
{
30 #define rsnd_priv_to_dmac(p) ((struct rsnd_dma_ctrl *)(p)->dma)
35 static void __rsnd_dmaen_complete(struct rsnd_mod
*mod
,
36 struct rsnd_dai_stream
*io
)
38 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
43 * Renesas sound Gen1 needs 1 DMAC,
45 * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
46 * But, Audio-DMAC-peri-peri doesn't have interrupt,
47 * and this driver is assuming that here.
49 * If Audio-DMAC-peri-peri has interrpt,
50 * rsnd_dai_pointer_update() will be called twice,
51 * ant it will breaks io->byte_pos
53 spin_lock_irqsave(&priv
->lock
, flags
);
55 if (rsnd_io_is_working(io
))
56 elapsed
= rsnd_dai_pointer_update(io
, io
->byte_per_period
);
58 spin_unlock_irqrestore(&priv
->lock
, flags
);
61 rsnd_dai_period_elapsed(io
);
64 static void rsnd_dmaen_complete(void *data
)
66 struct rsnd_mod
*mod
= data
;
68 rsnd_mod_interrupt(mod
, __rsnd_dmaen_complete
);
71 static void rsnd_dmaen_stop(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
73 struct rsnd_dmaen
*dmaen
= rsnd_dma_to_dmaen(dma
);
75 dmaengine_terminate_all(dmaen
->chan
);
78 static void rsnd_dmaen_start(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
80 struct rsnd_dmaen
*dmaen
= rsnd_dma_to_dmaen(dma
);
81 struct rsnd_mod
*mod
= rsnd_dma_to_mod(dma
);
82 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
83 struct snd_pcm_substream
*substream
= io
->substream
;
84 struct device
*dev
= rsnd_priv_to_dev(priv
);
85 struct dma_async_tx_descriptor
*desc
;
86 int is_play
= rsnd_io_is_play(io
);
88 desc
= dmaengine_prep_dma_cyclic(dmaen
->chan
,
89 substream
->runtime
->dma_addr
,
90 snd_pcm_lib_buffer_bytes(substream
),
91 snd_pcm_lib_period_bytes(substream
),
92 is_play
? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
,
93 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
96 dev_err(dev
, "dmaengine_prep_slave_sg() fail\n");
100 desc
->callback
= rsnd_dmaen_complete
;
101 desc
->callback_param
= mod
;
103 if (dmaengine_submit(desc
) < 0) {
104 dev_err(dev
, "dmaengine_submit() fail\n");
108 dma_async_issue_pending(dmaen
->chan
);
111 struct dma_chan
*rsnd_dma_request_channel(struct device_node
*of_node
,
112 struct rsnd_mod
*mod
, char *name
)
114 struct dma_chan
*chan
;
115 struct device_node
*np
;
118 for_each_child_of_node(of_node
, np
) {
119 if (i
== rsnd_mod_id(mod
))
124 chan
= of_dma_request_slave_channel(np
, name
);
127 of_node_put(of_node
);
132 static struct dma_chan
*rsnd_dmaen_request_channel(struct rsnd_dai_stream
*io
,
133 struct rsnd_mod
*mod_from
,
134 struct rsnd_mod
*mod_to
)
136 if ((!mod_from
&& !mod_to
) ||
137 (mod_from
&& mod_to
))
141 return rsnd_mod_dma_req(io
, mod_from
);
143 return rsnd_mod_dma_req(io
, mod_to
);
146 static int rsnd_dmaen_init(struct rsnd_dai_stream
*io
,
147 struct rsnd_dma
*dma
, int id
,
148 struct rsnd_mod
*mod_from
, struct rsnd_mod
*mod_to
)
150 struct rsnd_dmaen
*dmaen
= rsnd_dma_to_dmaen(dma
);
151 struct rsnd_priv
*priv
= rsnd_io_to_priv(io
);
152 struct device
*dev
= rsnd_priv_to_dev(priv
);
153 struct dma_slave_config cfg
= {};
154 int is_play
= rsnd_io_is_play(io
);
158 dev_err(dev
, "it already has dma channel\n");
163 dmaen
->chan
= rsnd_dmaen_request_channel(io
, mod_from
, mod_to
);
168 dma_cap_set(DMA_SLAVE
, mask
);
170 dmaen
->chan
= dma_request_channel(mask
, shdma_chan_filter
,
173 if (IS_ERR_OR_NULL(dmaen
->chan
)) {
175 dev_err(dev
, "can't get dma channel\n");
176 goto rsnd_dma_channel_err
;
179 cfg
.direction
= is_play
? DMA_MEM_TO_DEV
: DMA_DEV_TO_MEM
;
180 cfg
.src_addr
= dma
->src_addr
;
181 cfg
.dst_addr
= dma
->dst_addr
;
182 cfg
.src_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
183 cfg
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_4_BYTES
;
185 dev_dbg(dev
, "dma : %pad -> %pad\n",
186 &cfg
.src_addr
, &cfg
.dst_addr
);
188 ret
= dmaengine_slave_config(dmaen
->chan
, &cfg
);
190 goto rsnd_dma_init_err
;
195 rsnd_dma_quit(io
, dma
);
196 rsnd_dma_channel_err
:
199 * DMA failed. try to PIO mode
201 * rsnd_ssi_fallback()
202 * rsnd_rdai_continuance_probe()
207 static void rsnd_dmaen_quit(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
209 struct rsnd_dmaen
*dmaen
= rsnd_dma_to_dmaen(dma
);
212 dma_release_channel(dmaen
->chan
);
217 static struct rsnd_dma_ops rsnd_dmaen_ops
= {
218 .start
= rsnd_dmaen_start
,
219 .stop
= rsnd_dmaen_stop
,
220 .init
= rsnd_dmaen_init
,
221 .quit
= rsnd_dmaen_quit
,
225 * Audio DMAC peri peri
227 static const u8 gen2_id_table_ssiu
[] = {
239 static const u8 gen2_id_table_scu
[] = {
240 0x2d, /* SCU_SRCI0 */
241 0x2e, /* SCU_SRCI1 */
242 0x2f, /* SCU_SRCI2 */
243 0x30, /* SCU_SRCI3 */
244 0x31, /* SCU_SRCI4 */
245 0x32, /* SCU_SRCI5 */
246 0x33, /* SCU_SRCI6 */
247 0x34, /* SCU_SRCI7 */
248 0x35, /* SCU_SRCI8 */
249 0x36, /* SCU_SRCI9 */
251 static const u8 gen2_id_table_cmd
[] = {
256 static u32
rsnd_dmapp_get_id(struct rsnd_dai_stream
*io
,
257 struct rsnd_mod
*mod
)
259 struct rsnd_mod
*ssi
= rsnd_io_to_mod_ssi(io
);
260 struct rsnd_mod
*src
= rsnd_io_to_mod_src(io
);
261 struct rsnd_mod
*dvc
= rsnd_io_to_mod_dvc(io
);
262 const u8
*entry
= NULL
;
263 int id
= rsnd_mod_id(mod
);
267 entry
= gen2_id_table_ssiu
;
268 size
= ARRAY_SIZE(gen2_id_table_ssiu
);
269 } else if (mod
== src
) {
270 entry
= gen2_id_table_scu
;
271 size
= ARRAY_SIZE(gen2_id_table_scu
);
272 } else if (mod
== dvc
) {
273 entry
= gen2_id_table_cmd
;
274 size
= ARRAY_SIZE(gen2_id_table_cmd
);
286 static u32
rsnd_dmapp_get_chcr(struct rsnd_dai_stream
*io
,
287 struct rsnd_mod
*mod_from
,
288 struct rsnd_mod
*mod_to
)
290 return (rsnd_dmapp_get_id(io
, mod_from
) << 24) +
291 (rsnd_dmapp_get_id(io
, mod_to
) << 16);
294 #define rsnd_dmapp_addr(dmac, dma, reg) \
295 (dmac->base + 0x20 + reg + \
296 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
297 static void rsnd_dmapp_write(struct rsnd_dma
*dma
, u32 data
, u32 reg
)
299 struct rsnd_mod
*mod
= rsnd_dma_to_mod(dma
);
300 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
301 struct rsnd_dma_ctrl
*dmac
= rsnd_priv_to_dmac(priv
);
302 struct device
*dev
= rsnd_priv_to_dev(priv
);
304 dev_dbg(dev
, "w %p : %08x\n", rsnd_dmapp_addr(dmac
, dma
, reg
), data
);
306 iowrite32(data
, rsnd_dmapp_addr(dmac
, dma
, reg
));
309 static u32
rsnd_dmapp_read(struct rsnd_dma
*dma
, u32 reg
)
311 struct rsnd_mod
*mod
= rsnd_dma_to_mod(dma
);
312 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
313 struct rsnd_dma_ctrl
*dmac
= rsnd_priv_to_dmac(priv
);
315 return ioread32(rsnd_dmapp_addr(dmac
, dma
, reg
));
318 static void rsnd_dmapp_stop(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
322 rsnd_dmapp_write(dma
, 0, PDMACHCR
);
324 for (i
= 0; i
< 1024; i
++) {
325 if (0 == rsnd_dmapp_read(dma
, PDMACHCR
))
331 static void rsnd_dmapp_start(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
333 struct rsnd_dmapp
*dmapp
= rsnd_dma_to_dmapp(dma
);
335 rsnd_dmapp_write(dma
, dma
->src_addr
, PDMASAR
);
336 rsnd_dmapp_write(dma
, dma
->dst_addr
, PDMADAR
);
337 rsnd_dmapp_write(dma
, dmapp
->chcr
, PDMACHCR
);
340 static int rsnd_dmapp_init(struct rsnd_dai_stream
*io
,
341 struct rsnd_dma
*dma
, int id
,
342 struct rsnd_mod
*mod_from
, struct rsnd_mod
*mod_to
)
344 struct rsnd_dmapp
*dmapp
= rsnd_dma_to_dmapp(dma
);
345 struct rsnd_priv
*priv
= rsnd_io_to_priv(io
);
346 struct rsnd_dma_ctrl
*dmac
= rsnd_priv_to_dmac(priv
);
347 struct device
*dev
= rsnd_priv_to_dev(priv
);
349 dmapp
->dmapp_id
= dmac
->dmapp_num
;
350 dmapp
->chcr
= rsnd_dmapp_get_chcr(io
, mod_from
, mod_to
) | PDMACHCR_DE
;
354 rsnd_dmapp_stop(io
, dma
);
356 dev_dbg(dev
, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
357 dmapp
->dmapp_id
, &dma
->src_addr
, &dma
->dst_addr
, dmapp
->chcr
);
362 static struct rsnd_dma_ops rsnd_dmapp_ops
= {
363 .start
= rsnd_dmapp_start
,
364 .stop
= rsnd_dmapp_stop
,
365 .init
= rsnd_dmapp_init
,
366 .quit
= rsnd_dmapp_stop
,
370 * Common DMAC Interface
374 * DMA read/write register offset
376 * RSND_xxx_I_N for Audio DMAC input
377 * RSND_xxx_O_N for Audio DMAC output
378 * RSND_xxx_I_P for Audio DMAC peri peri input
379 * RSND_xxx_O_P for Audio DMAC peri peri output
382 * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out
383 * SSI : 0xec541000 / 0xec241008 / 0xec24100c
384 * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
385 * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
386 * CMD : 0xec500000 / / 0xec008000 0xec308000
388 #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
389 #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
391 #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
392 #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
394 #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
395 #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
397 #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i))
398 #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i))
400 #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i))
401 #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i))
403 #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i))
404 #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i))
407 rsnd_gen2_dma_addr(struct rsnd_dai_stream
*io
,
408 struct rsnd_mod
*mod
,
409 int is_play
, int is_from
)
411 struct rsnd_priv
*priv
= rsnd_io_to_priv(io
);
412 struct device
*dev
= rsnd_priv_to_dev(priv
);
413 phys_addr_t ssi_reg
= rsnd_gen_get_phy_addr(priv
, RSND_GEN2_SSI
);
414 phys_addr_t src_reg
= rsnd_gen_get_phy_addr(priv
, RSND_GEN2_SCU
);
415 int is_ssi
= !!(rsnd_io_to_mod_ssi(io
) == mod
);
416 int use_src
= !!rsnd_io_to_mod_src(io
);
417 int use_dvc
= !!rsnd_io_to_mod_dvc(io
);
418 int id
= rsnd_mod_id(mod
);
422 } dma_addrs
[3][2][3] = {
426 { RDMA_SRC_O_N(src
, id
), RDMA_SRC_I_P(src
, id
) },
427 { RDMA_CMD_O_N(src
, id
), RDMA_SRC_I_P(src
, id
) } },
430 { RDMA_SRC_O_P(src
, id
), RDMA_SRC_I_N(src
, id
) },
431 { RDMA_CMD_O_P(src
, id
), RDMA_SRC_I_N(src
, id
) } }
435 {{{ RDMA_SSI_O_N(ssi
, id
), 0 },
436 { RDMA_SSIU_O_P(ssi
, id
), 0 },
437 { RDMA_SSIU_O_P(ssi
, id
), 0 } },
439 {{ 0, RDMA_SSI_I_N(ssi
, id
) },
440 { 0, RDMA_SSIU_I_P(ssi
, id
) },
441 { 0, RDMA_SSIU_I_P(ssi
, id
) } }
445 {{{ RDMA_SSIU_O_N(ssi
, id
), 0 },
446 { RDMA_SSIU_O_P(ssi
, id
), 0 },
447 { RDMA_SSIU_O_P(ssi
, id
), 0 } },
449 {{ 0, RDMA_SSIU_I_N(ssi
, id
) },
450 { 0, RDMA_SSIU_I_P(ssi
, id
) },
451 { 0, RDMA_SSIU_I_P(ssi
, id
) } } },
454 /* it shouldn't happen */
455 if (use_dvc
&& !use_src
)
456 dev_err(dev
, "DVC is selected without SRC\n");
458 /* use SSIU or SSI ? */
459 if (is_ssi
&& rsnd_ssi_use_busif(io
, mod
))
463 dma_addrs
[is_ssi
][is_play
][use_src
+ use_dvc
].out_addr
:
464 dma_addrs
[is_ssi
][is_play
][use_src
+ use_dvc
].in_addr
;
467 static dma_addr_t
rsnd_dma_addr(struct rsnd_dai_stream
*io
,
468 struct rsnd_mod
*mod
,
469 int is_play
, int is_from
)
471 struct rsnd_priv
*priv
= rsnd_io_to_priv(io
);
474 * gen1 uses default DMA addr
476 if (rsnd_is_gen1(priv
))
482 return rsnd_gen2_dma_addr(io
, mod
, is_play
, is_from
);
485 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
486 static void rsnd_dma_of_path(struct rsnd_dma
*dma
,
487 struct rsnd_dai_stream
*io
,
489 struct rsnd_mod
**mod_from
,
490 struct rsnd_mod
**mod_to
)
492 struct rsnd_mod
*this = rsnd_dma_to_mod(dma
);
493 struct rsnd_mod
*ssi
= rsnd_io_to_mod_ssi(io
);
494 struct rsnd_mod
*src
= rsnd_io_to_mod_src(io
);
495 struct rsnd_mod
*dvc
= rsnd_io_to_mod_dvc(io
);
496 struct rsnd_mod
*mod
[MOD_MAX
];
500 for (i
= 0; i
< MOD_MAX
; i
++)
510 * mem -> SRC -> DVC -> SSI
512 mod
[0] = NULL
; /* for "mem" */
514 for (i
= 1; i
< MOD_MAX
; i
++) {
521 if ((!is_play
) && (this == src
))
524 mod
[i
] = (is_play
) ? src
: dvc
;
526 mod
[i
] = (is_play
) ? dvc
: src
;
539 *mod_from
= mod
[index
- 1];
540 *mod_to
= mod
[index
];
542 *mod_from
= mod
[index
];
543 *mod_to
= mod
[index
- 1];
547 void rsnd_dma_stop(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
549 dma
->ops
->stop(io
, dma
);
552 void rsnd_dma_start(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
554 dma
->ops
->start(io
, dma
);
557 void rsnd_dma_quit(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
)
559 struct rsnd_mod
*mod
= rsnd_dma_to_mod(dma
);
560 struct rsnd_priv
*priv
= rsnd_mod_to_priv(mod
);
561 struct rsnd_dma_ctrl
*dmac
= rsnd_priv_to_dmac(priv
);
566 dma
->ops
->quit(io
, dma
);
569 int rsnd_dma_init(struct rsnd_dai_stream
*io
, struct rsnd_dma
*dma
, int id
)
571 struct rsnd_mod
*mod_from
;
572 struct rsnd_mod
*mod_to
;
573 struct rsnd_priv
*priv
= rsnd_io_to_priv(io
);
574 struct rsnd_dma_ctrl
*dmac
= rsnd_priv_to_dmac(priv
);
575 int is_play
= rsnd_io_is_play(io
);
578 * DMA failed. try to PIO mode
580 * rsnd_ssi_fallback()
581 * rsnd_rdai_continuance_probe()
586 rsnd_dma_of_path(dma
, io
, is_play
, &mod_from
, &mod_to
);
588 dma
->src_addr
= rsnd_dma_addr(io
, mod_from
, is_play
, 1);
589 dma
->dst_addr
= rsnd_dma_addr(io
, mod_to
, is_play
, 0);
592 if (mod_from
&& mod_to
)
593 dma
->ops
= &rsnd_dmapp_ops
;
595 dma
->ops
= &rsnd_dmaen_ops
;
597 /* for Gen1, overwrite */
598 if (rsnd_is_gen1(priv
))
599 dma
->ops
= &rsnd_dmaen_ops
;
601 return dma
->ops
->init(io
, dma
, id
, mod_from
, mod_to
);
604 int rsnd_dma_probe(struct platform_device
*pdev
,
605 const struct rsnd_of_data
*of_data
,
606 struct rsnd_priv
*priv
)
608 struct device
*dev
= rsnd_priv_to_dev(priv
);
609 struct rsnd_dma_ctrl
*dmac
;
610 struct resource
*res
;
615 if (rsnd_is_gen1(priv
))
621 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "audmapp");
622 dmac
= devm_kzalloc(dev
, sizeof(*dmac
), GFP_KERNEL
);
624 dev_err(dev
, "dma allocate failed\n");
625 return 0; /* it will be PIO mode */
629 dmac
->base
= devm_ioremap_resource(dev
, res
);
630 if (IS_ERR(dmac
->base
))
631 return PTR_ERR(dmac
->base
);