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_mod_is_working(mod))
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)) {
174 dev_err(dev, "can't get dma channel\n");
175 goto rsnd_dma_channel_err;
178 cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
179 cfg.src_addr = dma->src_addr;
180 cfg.dst_addr = dma->dst_addr;
181 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
182 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
184 dev_dbg(dev, "dma : %pad -> %pad\n",
185 &cfg.src_addr, &cfg.dst_addr);
187 ret = dmaengine_slave_config(dmaen->chan, &cfg);
189 goto rsnd_dma_init_err;
194 rsnd_dma_quit(io, dma);
195 rsnd_dma_channel_err:
198 * DMA failed. try to PIO mode
200 * rsnd_ssi_fallback()
201 * rsnd_rdai_continuance_probe()
206 static void rsnd_dmaen_quit(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
208 struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
211 dma_release_channel(dmaen->chan);
216 static struct rsnd_dma_ops rsnd_dmaen_ops = {
217 .start = rsnd_dmaen_start,
218 .stop = rsnd_dmaen_stop,
219 .init = rsnd_dmaen_init,
220 .quit = rsnd_dmaen_quit,
224 * Audio DMAC peri peri
226 static const u8 gen2_id_table_ssiu[] = {
238 static const u8 gen2_id_table_scu[] = {
239 0x2d, /* SCU_SRCI0 */
240 0x2e, /* SCU_SRCI1 */
241 0x2f, /* SCU_SRCI2 */
242 0x30, /* SCU_SRCI3 */
243 0x31, /* SCU_SRCI4 */
244 0x32, /* SCU_SRCI5 */
245 0x33, /* SCU_SRCI6 */
246 0x34, /* SCU_SRCI7 */
247 0x35, /* SCU_SRCI8 */
248 0x36, /* SCU_SRCI9 */
250 static const u8 gen2_id_table_cmd[] = {
255 static u32 rsnd_dmapp_get_id(struct rsnd_dai_stream *io,
256 struct rsnd_mod *mod)
258 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
259 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
260 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
261 const u8 *entry = NULL;
262 int id = rsnd_mod_id(mod);
266 entry = gen2_id_table_ssiu;
267 size = ARRAY_SIZE(gen2_id_table_ssiu);
268 } else if (mod == src) {
269 entry = gen2_id_table_scu;
270 size = ARRAY_SIZE(gen2_id_table_scu);
271 } else if (mod == dvc) {
272 entry = gen2_id_table_cmd;
273 size = ARRAY_SIZE(gen2_id_table_cmd);
285 static u32 rsnd_dmapp_get_chcr(struct rsnd_dai_stream *io,
286 struct rsnd_mod *mod_from,
287 struct rsnd_mod *mod_to)
289 return (rsnd_dmapp_get_id(io, mod_from) << 24) +
290 (rsnd_dmapp_get_id(io, mod_to) << 16);
293 #define rsnd_dmapp_addr(dmac, dma, reg) \
294 (dmac->base + 0x20 + reg + \
295 (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
296 static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
298 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
299 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
300 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
301 struct device *dev = rsnd_priv_to_dev(priv);
303 dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
305 iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
308 static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
310 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
311 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
312 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
314 return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
317 static void rsnd_dmapp_stop(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
321 rsnd_dmapp_write(dma, 0, PDMACHCR);
323 for (i = 0; i < 1024; i++) {
324 if (0 == rsnd_dmapp_read(dma, PDMACHCR))
330 static void rsnd_dmapp_start(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
332 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
334 rsnd_dmapp_write(dma, dma->src_addr, PDMASAR);
335 rsnd_dmapp_write(dma, dma->dst_addr, PDMADAR);
336 rsnd_dmapp_write(dma, dmapp->chcr, PDMACHCR);
339 static int rsnd_dmapp_init(struct rsnd_dai_stream *io,
340 struct rsnd_dma *dma, int id,
341 struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
343 struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
344 struct rsnd_priv *priv = rsnd_io_to_priv(io);
345 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
346 struct device *dev = rsnd_priv_to_dev(priv);
348 dmapp->dmapp_id = dmac->dmapp_num;
349 dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;
353 rsnd_dmapp_stop(io, dma);
355 dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
356 dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
361 static struct rsnd_dma_ops rsnd_dmapp_ops = {
362 .start = rsnd_dmapp_start,
363 .stop = rsnd_dmapp_stop,
364 .init = rsnd_dmapp_init,
365 .quit = rsnd_dmapp_stop,
369 * Common DMAC Interface
373 * DMA read/write register offset
375 * RSND_xxx_I_N for Audio DMAC input
376 * RSND_xxx_O_N for Audio DMAC output
377 * RSND_xxx_I_P for Audio DMAC peri peri input
378 * RSND_xxx_O_P for Audio DMAC peri peri output
381 * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out
382 * SSI : 0xec541000 / 0xec241008 / 0xec24100c
383 * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
384 * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
385 * CMD : 0xec500000 / / 0xec008000 0xec308000
387 #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
388 #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
390 #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
391 #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
393 #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
394 #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
396 #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i))
397 #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i))
399 #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i))
400 #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i))
402 #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i))
403 #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i))
406 rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,
407 struct rsnd_mod *mod,
408 int is_play, int is_from)
410 struct rsnd_priv *priv = rsnd_io_to_priv(io);
411 struct device *dev = rsnd_priv_to_dev(priv);
412 phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
413 phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
414 int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod);
415 int use_src = !!rsnd_io_to_mod_src(io);
416 int use_dvc = !!rsnd_io_to_mod_dvc(io);
417 int id = rsnd_mod_id(mod);
421 } dma_addrs[3][2][3] = {
425 { RDMA_SRC_O_N(src, id), RDMA_SRC_I_P(src, id) },
426 { RDMA_CMD_O_N(src, id), RDMA_SRC_I_P(src, id) } },
429 { RDMA_SRC_O_P(src, id), RDMA_SRC_I_N(src, id) },
430 { RDMA_CMD_O_P(src, id), RDMA_SRC_I_N(src, id) } }
434 {{{ RDMA_SSI_O_N(ssi, id), 0 },
435 { RDMA_SSIU_O_P(ssi, id), 0 },
436 { RDMA_SSIU_O_P(ssi, id), 0 } },
438 {{ 0, RDMA_SSI_I_N(ssi, id) },
439 { 0, RDMA_SSIU_I_P(ssi, id) },
440 { 0, RDMA_SSIU_I_P(ssi, id) } }
444 {{{ RDMA_SSIU_O_N(ssi, id), 0 },
445 { RDMA_SSIU_O_P(ssi, id), 0 },
446 { RDMA_SSIU_O_P(ssi, id), 0 } },
448 {{ 0, RDMA_SSIU_I_N(ssi, id) },
449 { 0, RDMA_SSIU_I_P(ssi, id) },
450 { 0, RDMA_SSIU_I_P(ssi, id) } } },
453 /* it shouldn't happen */
454 if (use_dvc && !use_src)
455 dev_err(dev, "DVC is selected without SRC\n");
457 /* use SSIU or SSI ? */
458 if (is_ssi && rsnd_ssi_use_busif(mod))
462 dma_addrs[is_ssi][is_play][use_src + use_dvc].out_addr :
463 dma_addrs[is_ssi][is_play][use_src + use_dvc].in_addr;
466 static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,
467 struct rsnd_mod *mod,
468 int is_play, int is_from)
470 struct rsnd_priv *priv = rsnd_io_to_priv(io);
473 * gen1 uses default DMA addr
475 if (rsnd_is_gen1(priv))
481 return rsnd_gen2_dma_addr(io, mod, is_play, is_from);
484 #define MOD_MAX 4 /* MEM/SSI/SRC/DVC */
485 static void rsnd_dma_of_path(struct rsnd_dma *dma,
486 struct rsnd_dai_stream *io,
488 struct rsnd_mod **mod_from,
489 struct rsnd_mod **mod_to)
491 struct rsnd_mod *this = rsnd_dma_to_mod(dma);
492 struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
493 struct rsnd_mod *src = rsnd_io_to_mod_src(io);
494 struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
495 struct rsnd_mod *mod[MOD_MAX];
499 for (i = 0; i < MOD_MAX; i++)
509 * mem -> SRC -> DVC -> SSI
511 mod[0] = NULL; /* for "mem" */
513 for (i = 1; i < MOD_MAX; i++) {
520 if ((!is_play) && (this == src))
523 mod[i] = (is_play) ? src : dvc;
525 mod[i] = (is_play) ? dvc : src;
538 *mod_from = mod[index - 1];
539 *mod_to = mod[index];
541 *mod_from = mod[index];
542 *mod_to = mod[index - 1];
546 void rsnd_dma_stop(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
548 dma->ops->stop(io, dma);
551 void rsnd_dma_start(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
553 dma->ops->start(io, dma);
556 void rsnd_dma_quit(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
558 struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
559 struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
560 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
565 dma->ops->quit(io, dma);
568 int rsnd_dma_init(struct rsnd_dai_stream *io, struct rsnd_dma *dma, int id)
570 struct rsnd_mod *mod_from;
571 struct rsnd_mod *mod_to;
572 struct rsnd_priv *priv = rsnd_io_to_priv(io);
573 struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
574 int is_play = rsnd_io_is_play(io);
577 * DMA failed. try to PIO mode
579 * rsnd_ssi_fallback()
580 * rsnd_rdai_continuance_probe()
585 rsnd_dma_of_path(dma, io, is_play, &mod_from, &mod_to);
587 dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);
588 dma->dst_addr = rsnd_dma_addr(io, mod_to, is_play, 0);
591 if (mod_from && mod_to)
592 dma->ops = &rsnd_dmapp_ops;
594 dma->ops = &rsnd_dmaen_ops;
596 /* for Gen1, overwrite */
597 if (rsnd_is_gen1(priv))
598 dma->ops = &rsnd_dmaen_ops;
600 return dma->ops->init(io, dma, id, mod_from, mod_to);
603 int rsnd_dma_probe(struct platform_device *pdev,
604 const struct rsnd_of_data *of_data,
605 struct rsnd_priv *priv)
607 struct device *dev = rsnd_priv_to_dev(priv);
608 struct rsnd_dma_ctrl *dmac;
609 struct resource *res;
614 if (rsnd_is_gen1(priv))
620 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
621 dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
623 dev_err(dev, "dma allocate failed\n");
624 return 0; /* it will be PIO mode */
628 dmac->base = devm_ioremap_resource(dev, res);
629 if (IS_ERR(dmac->base))
630 return PTR_ERR(dmac->base);