dma.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. * Audio DMAC peri peri register
  16. */
  17. #define PDMASAR 0x00
  18. #define PDMADAR 0x04
  19. #define PDMACHCR 0x0c
  20. /* PDMACHCR */
  21. #define PDMACHCR_DE (1 << 0)
  22. struct rsnd_dma_ctrl {
  23. void __iomem *base;
  24. int dmapp_num;
  25. };
  26. struct rsnd_dma_ops {
  27. char *name;
  28. void (*start)(struct rsnd_dai_stream *io, struct rsnd_dma *dma);
  29. void (*stop)(struct rsnd_dai_stream *io, struct rsnd_dma *dma);
  30. int (*init)(struct rsnd_dai_stream *io, struct rsnd_dma *dma, int id,
  31. struct rsnd_mod *mod_from, struct rsnd_mod *mod_to);
  32. void (*quit)(struct rsnd_dai_stream *io, struct rsnd_dma *dma);
  33. };
  34. #define rsnd_priv_to_dmac(p) ((struct rsnd_dma_ctrl *)(p)->dma)
  35. /*
  36. * Audio DMAC
  37. */
  38. static void __rsnd_dmaen_complete(struct rsnd_mod *mod,
  39. struct rsnd_dai_stream *io)
  40. {
  41. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  42. bool elapsed = false;
  43. unsigned long flags;
  44. /*
  45. * Renesas sound Gen1 needs 1 DMAC,
  46. * Gen2 needs 2 DMAC.
  47. * In Gen2 case, it are Audio-DMAC, and Audio-DMAC-peri-peri.
  48. * But, Audio-DMAC-peri-peri doesn't have interrupt,
  49. * and this driver is assuming that here.
  50. *
  51. * If Audio-DMAC-peri-peri has interrpt,
  52. * rsnd_dai_pointer_update() will be called twice,
  53. * ant it will breaks io->byte_pos
  54. */
  55. spin_lock_irqsave(&priv->lock, flags);
  56. if (rsnd_io_is_working(io))
  57. elapsed = rsnd_dai_pointer_update(io, io->byte_per_period);
  58. spin_unlock_irqrestore(&priv->lock, flags);
  59. if (elapsed)
  60. rsnd_dai_period_elapsed(io);
  61. }
  62. static void rsnd_dmaen_complete(void *data)
  63. {
  64. struct rsnd_mod *mod = data;
  65. rsnd_mod_interrupt(mod, __rsnd_dmaen_complete);
  66. }
  67. static void rsnd_dmaen_stop(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  68. {
  69. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  70. dmaengine_terminate_all(dmaen->chan);
  71. }
  72. static void rsnd_dmaen_start(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  73. {
  74. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  75. struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
  76. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  77. struct snd_pcm_substream *substream = io->substream;
  78. struct device *dev = rsnd_priv_to_dev(priv);
  79. struct dma_async_tx_descriptor *desc;
  80. int is_play = rsnd_io_is_play(io);
  81. desc = dmaengine_prep_dma_cyclic(dmaen->chan,
  82. substream->runtime->dma_addr,
  83. snd_pcm_lib_buffer_bytes(substream),
  84. snd_pcm_lib_period_bytes(substream),
  85. is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
  86. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  87. if (!desc) {
  88. dev_err(dev, "dmaengine_prep_slave_sg() fail\n");
  89. return;
  90. }
  91. desc->callback = rsnd_dmaen_complete;
  92. desc->callback_param = mod;
  93. if (dmaengine_submit(desc) < 0) {
  94. dev_err(dev, "dmaengine_submit() fail\n");
  95. return;
  96. }
  97. dma_async_issue_pending(dmaen->chan);
  98. }
  99. struct dma_chan *rsnd_dma_request_channel(struct device_node *of_node,
  100. struct rsnd_mod *mod, char *name)
  101. {
  102. struct dma_chan *chan;
  103. struct device_node *np;
  104. int i = 0;
  105. for_each_child_of_node(of_node, np) {
  106. if (i == rsnd_mod_id(mod))
  107. break;
  108. i++;
  109. }
  110. chan = of_dma_request_slave_channel(np, name);
  111. of_node_put(np);
  112. of_node_put(of_node);
  113. return chan;
  114. }
  115. static struct dma_chan *rsnd_dmaen_request_channel(struct rsnd_dai_stream *io,
  116. struct rsnd_mod *mod_from,
  117. struct rsnd_mod *mod_to)
  118. {
  119. if ((!mod_from && !mod_to) ||
  120. (mod_from && mod_to))
  121. return NULL;
  122. if (mod_from)
  123. return rsnd_mod_dma_req(io, mod_from);
  124. else
  125. return rsnd_mod_dma_req(io, mod_to);
  126. }
  127. static int rsnd_dmaen_init(struct rsnd_dai_stream *io,
  128. struct rsnd_dma *dma, int id,
  129. struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
  130. {
  131. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  132. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  133. struct device *dev = rsnd_priv_to_dev(priv);
  134. struct dma_slave_config cfg = {};
  135. int is_play = rsnd_io_is_play(io);
  136. int ret;
  137. if (dmaen->chan) {
  138. dev_err(dev, "it already has dma channel\n");
  139. return -EIO;
  140. }
  141. if (dev->of_node) {
  142. dmaen->chan = rsnd_dmaen_request_channel(io, mod_from, mod_to);
  143. } else {
  144. dma_cap_mask_t mask;
  145. dma_cap_zero(mask);
  146. dma_cap_set(DMA_SLAVE, mask);
  147. dmaen->chan = dma_request_channel(mask, shdma_chan_filter,
  148. (void *)(uintptr_t)id);
  149. }
  150. if (IS_ERR_OR_NULL(dmaen->chan)) {
  151. dmaen->chan = NULL;
  152. dev_err(dev, "can't get dma channel\n");
  153. goto rsnd_dma_channel_err;
  154. }
  155. cfg.direction = is_play ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM;
  156. cfg.src_addr = dma->src_addr;
  157. cfg.dst_addr = dma->dst_addr;
  158. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  159. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  160. dev_dbg(dev, "%s %pad -> %pad\n",
  161. dma->ops->name,
  162. &cfg.src_addr, &cfg.dst_addr);
  163. ret = dmaengine_slave_config(dmaen->chan, &cfg);
  164. if (ret < 0)
  165. goto rsnd_dma_init_err;
  166. return 0;
  167. rsnd_dma_init_err:
  168. rsnd_dma_quit(io, dma);
  169. rsnd_dma_channel_err:
  170. /*
  171. * DMA failed. try to PIO mode
  172. * see
  173. * rsnd_ssi_fallback()
  174. * rsnd_rdai_continuance_probe()
  175. */
  176. return -EAGAIN;
  177. }
  178. static void rsnd_dmaen_quit(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  179. {
  180. struct rsnd_dmaen *dmaen = rsnd_dma_to_dmaen(dma);
  181. if (dmaen->chan)
  182. dma_release_channel(dmaen->chan);
  183. dmaen->chan = NULL;
  184. }
  185. static struct rsnd_dma_ops rsnd_dmaen_ops = {
  186. .name = "audmac",
  187. .start = rsnd_dmaen_start,
  188. .stop = rsnd_dmaen_stop,
  189. .init = rsnd_dmaen_init,
  190. .quit = rsnd_dmaen_quit,
  191. };
  192. /*
  193. * Audio DMAC peri peri
  194. */
  195. static const u8 gen2_id_table_ssiu[] = {
  196. 0x00, /* SSI00 */
  197. 0x04, /* SSI10 */
  198. 0x08, /* SSI20 */
  199. 0x0c, /* SSI3 */
  200. 0x0d, /* SSI4 */
  201. 0x0e, /* SSI5 */
  202. 0x0f, /* SSI6 */
  203. 0x10, /* SSI7 */
  204. 0x11, /* SSI8 */
  205. 0x12, /* SSI90 */
  206. };
  207. static const u8 gen2_id_table_scu[] = {
  208. 0x2d, /* SCU_SRCI0 */
  209. 0x2e, /* SCU_SRCI1 */
  210. 0x2f, /* SCU_SRCI2 */
  211. 0x30, /* SCU_SRCI3 */
  212. 0x31, /* SCU_SRCI4 */
  213. 0x32, /* SCU_SRCI5 */
  214. 0x33, /* SCU_SRCI6 */
  215. 0x34, /* SCU_SRCI7 */
  216. 0x35, /* SCU_SRCI8 */
  217. 0x36, /* SCU_SRCI9 */
  218. };
  219. static const u8 gen2_id_table_cmd[] = {
  220. 0x37, /* SCU_CMD0 */
  221. 0x38, /* SCU_CMD1 */
  222. };
  223. static u32 rsnd_dmapp_get_id(struct rsnd_dai_stream *io,
  224. struct rsnd_mod *mod)
  225. {
  226. struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
  227. struct rsnd_mod *src = rsnd_io_to_mod_src(io);
  228. struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
  229. const u8 *entry = NULL;
  230. int id = rsnd_mod_id(mod);
  231. int size = 0;
  232. if (mod == ssi) {
  233. entry = gen2_id_table_ssiu;
  234. size = ARRAY_SIZE(gen2_id_table_ssiu);
  235. } else if (mod == src) {
  236. entry = gen2_id_table_scu;
  237. size = ARRAY_SIZE(gen2_id_table_scu);
  238. } else if (mod == dvc) {
  239. entry = gen2_id_table_cmd;
  240. size = ARRAY_SIZE(gen2_id_table_cmd);
  241. }
  242. if (!entry)
  243. return 0xFF;
  244. if (size <= id)
  245. return 0xFF;
  246. return entry[id];
  247. }
  248. static u32 rsnd_dmapp_get_chcr(struct rsnd_dai_stream *io,
  249. struct rsnd_mod *mod_from,
  250. struct rsnd_mod *mod_to)
  251. {
  252. return (rsnd_dmapp_get_id(io, mod_from) << 24) +
  253. (rsnd_dmapp_get_id(io, mod_to) << 16);
  254. }
  255. #define rsnd_dmapp_addr(dmac, dma, reg) \
  256. (dmac->base + 0x20 + reg + \
  257. (0x10 * rsnd_dma_to_dmapp(dma)->dmapp_id))
  258. static void rsnd_dmapp_write(struct rsnd_dma *dma, u32 data, u32 reg)
  259. {
  260. struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
  261. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  262. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  263. struct device *dev = rsnd_priv_to_dev(priv);
  264. dev_dbg(dev, "w %p : %08x\n", rsnd_dmapp_addr(dmac, dma, reg), data);
  265. iowrite32(data, rsnd_dmapp_addr(dmac, dma, reg));
  266. }
  267. static u32 rsnd_dmapp_read(struct rsnd_dma *dma, u32 reg)
  268. {
  269. struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
  270. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  271. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  272. return ioread32(rsnd_dmapp_addr(dmac, dma, reg));
  273. }
  274. static void rsnd_dmapp_stop(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  275. {
  276. int i;
  277. rsnd_dmapp_write(dma, 0, PDMACHCR);
  278. for (i = 0; i < 1024; i++) {
  279. if (0 == rsnd_dmapp_read(dma, PDMACHCR))
  280. return;
  281. udelay(1);
  282. }
  283. }
  284. static void rsnd_dmapp_start(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  285. {
  286. struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
  287. rsnd_dmapp_write(dma, dma->src_addr, PDMASAR);
  288. rsnd_dmapp_write(dma, dma->dst_addr, PDMADAR);
  289. rsnd_dmapp_write(dma, dmapp->chcr, PDMACHCR);
  290. }
  291. static int rsnd_dmapp_init(struct rsnd_dai_stream *io,
  292. struct rsnd_dma *dma, int id,
  293. struct rsnd_mod *mod_from, struct rsnd_mod *mod_to)
  294. {
  295. struct rsnd_dmapp *dmapp = rsnd_dma_to_dmapp(dma);
  296. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  297. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  298. struct device *dev = rsnd_priv_to_dev(priv);
  299. dmapp->dmapp_id = dmac->dmapp_num;
  300. dmapp->chcr = rsnd_dmapp_get_chcr(io, mod_from, mod_to) | PDMACHCR_DE;
  301. dmac->dmapp_num++;
  302. rsnd_dmapp_stop(io, dma);
  303. dev_dbg(dev, "id/src/dst/chcr = %d/%pad/%pad/%08x\n",
  304. dmapp->dmapp_id, &dma->src_addr, &dma->dst_addr, dmapp->chcr);
  305. return 0;
  306. }
  307. static struct rsnd_dma_ops rsnd_dmapp_ops = {
  308. .name = "audmac-pp",
  309. .start = rsnd_dmapp_start,
  310. .stop = rsnd_dmapp_stop,
  311. .init = rsnd_dmapp_init,
  312. .quit = rsnd_dmapp_stop,
  313. };
  314. /*
  315. * Common DMAC Interface
  316. */
  317. /*
  318. * DMA read/write register offset
  319. *
  320. * RSND_xxx_I_N for Audio DMAC input
  321. * RSND_xxx_O_N for Audio DMAC output
  322. * RSND_xxx_I_P for Audio DMAC peri peri input
  323. * RSND_xxx_O_P for Audio DMAC peri peri output
  324. *
  325. * ex) R-Car H2 case
  326. * mod / DMAC in / DMAC out / DMAC PP in / DMAC pp out
  327. * SSI : 0xec541000 / 0xec241008 / 0xec24100c
  328. * SSIU: 0xec541000 / 0xec100000 / 0xec100000 / 0xec400000 / 0xec400000
  329. * SCU : 0xec500000 / 0xec000000 / 0xec004000 / 0xec300000 / 0xec304000
  330. * CMD : 0xec500000 / / 0xec008000 0xec308000
  331. */
  332. #define RDMA_SSI_I_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0x8)
  333. #define RDMA_SSI_O_N(addr, i) (addr ##_reg - 0x00300000 + (0x40 * i) + 0xc)
  334. #define RDMA_SSIU_I_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
  335. #define RDMA_SSIU_O_N(addr, i) (addr ##_reg - 0x00441000 + (0x1000 * i))
  336. #define RDMA_SSIU_I_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
  337. #define RDMA_SSIU_O_P(addr, i) (addr ##_reg - 0x00141000 + (0x1000 * i))
  338. #define RDMA_SRC_I_N(addr, i) (addr ##_reg - 0x00500000 + (0x400 * i))
  339. #define RDMA_SRC_O_N(addr, i) (addr ##_reg - 0x004fc000 + (0x400 * i))
  340. #define RDMA_SRC_I_P(addr, i) (addr ##_reg - 0x00200000 + (0x400 * i))
  341. #define RDMA_SRC_O_P(addr, i) (addr ##_reg - 0x001fc000 + (0x400 * i))
  342. #define RDMA_CMD_O_N(addr, i) (addr ##_reg - 0x004f8000 + (0x400 * i))
  343. #define RDMA_CMD_O_P(addr, i) (addr ##_reg - 0x001f8000 + (0x400 * i))
  344. static dma_addr_t
  345. rsnd_gen2_dma_addr(struct rsnd_dai_stream *io,
  346. struct rsnd_mod *mod,
  347. int is_play, int is_from)
  348. {
  349. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  350. struct device *dev = rsnd_priv_to_dev(priv);
  351. phys_addr_t ssi_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SSI);
  352. phys_addr_t src_reg = rsnd_gen_get_phy_addr(priv, RSND_GEN2_SCU);
  353. int is_ssi = !!(rsnd_io_to_mod_ssi(io) == mod);
  354. int use_src = !!rsnd_io_to_mod_src(io);
  355. int use_cmd = !!rsnd_io_to_mod_dvc(io) ||
  356. !!rsnd_io_to_mod_mix(io) ||
  357. !!rsnd_io_to_mod_ctu(io);
  358. int id = rsnd_mod_id(mod);
  359. struct dma_addr {
  360. dma_addr_t out_addr;
  361. dma_addr_t in_addr;
  362. } dma_addrs[3][2][3] = {
  363. /* SRC */
  364. {{{ 0, 0 },
  365. /* Capture */
  366. { RDMA_SRC_O_N(src, id), RDMA_SRC_I_P(src, id) },
  367. { RDMA_CMD_O_N(src, id), RDMA_SRC_I_P(src, id) } },
  368. /* Playback */
  369. {{ 0, 0, },
  370. { RDMA_SRC_O_P(src, id), RDMA_SRC_I_N(src, id) },
  371. { RDMA_CMD_O_P(src, id), RDMA_SRC_I_N(src, id) } }
  372. },
  373. /* SSI */
  374. /* Capture */
  375. {{{ RDMA_SSI_O_N(ssi, id), 0 },
  376. { RDMA_SSIU_O_P(ssi, id), 0 },
  377. { RDMA_SSIU_O_P(ssi, id), 0 } },
  378. /* Playback */
  379. {{ 0, RDMA_SSI_I_N(ssi, id) },
  380. { 0, RDMA_SSIU_I_P(ssi, id) },
  381. { 0, RDMA_SSIU_I_P(ssi, id) } }
  382. },
  383. /* SSIU */
  384. /* Capture */
  385. {{{ RDMA_SSIU_O_N(ssi, id), 0 },
  386. { RDMA_SSIU_O_P(ssi, id), 0 },
  387. { RDMA_SSIU_O_P(ssi, id), 0 } },
  388. /* Playback */
  389. {{ 0, RDMA_SSIU_I_N(ssi, id) },
  390. { 0, RDMA_SSIU_I_P(ssi, id) },
  391. { 0, RDMA_SSIU_I_P(ssi, id) } } },
  392. };
  393. /* it shouldn't happen */
  394. if (use_cmd && !use_src)
  395. dev_err(dev, "DVC is selected without SRC\n");
  396. /* use SSIU or SSI ? */
  397. if (is_ssi && rsnd_ssi_use_busif(io, mod))
  398. is_ssi++;
  399. return (is_from) ?
  400. dma_addrs[is_ssi][is_play][use_src + use_cmd].out_addr :
  401. dma_addrs[is_ssi][is_play][use_src + use_cmd].in_addr;
  402. }
  403. static dma_addr_t rsnd_dma_addr(struct rsnd_dai_stream *io,
  404. struct rsnd_mod *mod,
  405. int is_play, int is_from)
  406. {
  407. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  408. /*
  409. * gen1 uses default DMA addr
  410. */
  411. if (rsnd_is_gen1(priv))
  412. return 0;
  413. if (!mod)
  414. return 0;
  415. return rsnd_gen2_dma_addr(io, mod, is_play, is_from);
  416. }
  417. #define MOD_MAX (RSND_MOD_MAX + 1) /* +Memory */
  418. static void rsnd_dma_of_path(struct rsnd_dma *dma,
  419. struct rsnd_dai_stream *io,
  420. int is_play,
  421. struct rsnd_mod **mod_from,
  422. struct rsnd_mod **mod_to)
  423. {
  424. struct rsnd_mod *this = rsnd_dma_to_mod(dma);
  425. struct rsnd_mod *ssi = rsnd_io_to_mod_ssi(io);
  426. struct rsnd_mod *src = rsnd_io_to_mod_src(io);
  427. struct rsnd_mod *ctu = rsnd_io_to_mod_ctu(io);
  428. struct rsnd_mod *mix = rsnd_io_to_mod_mix(io);
  429. struct rsnd_mod *dvc = rsnd_io_to_mod_dvc(io);
  430. struct rsnd_mod *mod[MOD_MAX];
  431. struct rsnd_mod *mod_start, *mod_end;
  432. struct rsnd_priv *priv = rsnd_mod_to_priv(this);
  433. struct device *dev = rsnd_priv_to_dev(priv);
  434. int nr, i;
  435. if (!ssi)
  436. return;
  437. nr = 0;
  438. for (i = 0; i < MOD_MAX; i++) {
  439. mod[i] = NULL;
  440. nr += !!rsnd_io_to_mod(io, i);
  441. }
  442. /*
  443. * [S] -*-> [E]
  444. * [S] -*-> SRC -o-> [E]
  445. * [S] -*-> SRC -> DVC -o-> [E]
  446. * [S] -*-> SRC -> CTU -> MIX -> DVC -o-> [E]
  447. *
  448. * playback [S] = mem
  449. * [E] = SSI
  450. *
  451. * capture [S] = SSI
  452. * [E] = mem
  453. *
  454. * -*-> Audio DMAC
  455. * -o-> Audio DMAC peri peri
  456. */
  457. mod_start = (is_play) ? NULL : ssi;
  458. mod_end = (is_play) ? ssi : NULL;
  459. mod[0] = mod_start;
  460. for (i = 1; i < nr; i++) {
  461. if (src) {
  462. mod[i] = src;
  463. src = NULL;
  464. } else if (ctu) {
  465. mod[i] = ctu;
  466. ctu = NULL;
  467. } else if (mix) {
  468. mod[i] = mix;
  469. mix = NULL;
  470. } else if (dvc) {
  471. mod[i] = dvc;
  472. dvc = NULL;
  473. }
  474. }
  475. mod[i] = mod_end;
  476. /*
  477. * | SSI | SRC |
  478. * -------------+-----+-----+
  479. * is_play | o | * |
  480. * !is_play | * | o |
  481. */
  482. if ((this == ssi) == (is_play)) {
  483. *mod_from = mod[nr - 1];
  484. *mod_to = mod[nr];
  485. } else {
  486. *mod_from = mod[0];
  487. *mod_to = mod[1];
  488. }
  489. dev_dbg(dev, "module connection (this is %s[%d])\n",
  490. rsnd_mod_name(this), rsnd_mod_id(this));
  491. for (i = 0; i <= nr; i++) {
  492. dev_dbg(dev, " %s[%d]%s\n",
  493. rsnd_mod_name(mod[i]), rsnd_mod_id(mod[i]),
  494. (mod[i] == *mod_from) ? " from" :
  495. (mod[i] == *mod_to) ? " to" : "");
  496. }
  497. }
  498. void rsnd_dma_stop(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  499. {
  500. dma->ops->stop(io, dma);
  501. }
  502. void rsnd_dma_start(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  503. {
  504. dma->ops->start(io, dma);
  505. }
  506. void rsnd_dma_quit(struct rsnd_dai_stream *io, struct rsnd_dma *dma)
  507. {
  508. struct rsnd_mod *mod = rsnd_dma_to_mod(dma);
  509. struct rsnd_priv *priv = rsnd_mod_to_priv(mod);
  510. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  511. if (!dmac)
  512. return;
  513. dma->ops->quit(io, dma);
  514. }
  515. int rsnd_dma_init(struct rsnd_dai_stream *io, struct rsnd_dma *dma, int id)
  516. {
  517. struct rsnd_mod *mod_from = NULL;
  518. struct rsnd_mod *mod_to = NULL;
  519. struct rsnd_priv *priv = rsnd_io_to_priv(io);
  520. struct rsnd_dma_ctrl *dmac = rsnd_priv_to_dmac(priv);
  521. struct device *dev = rsnd_priv_to_dev(priv);
  522. int is_play = rsnd_io_is_play(io);
  523. /*
  524. * DMA failed. try to PIO mode
  525. * see
  526. * rsnd_ssi_fallback()
  527. * rsnd_rdai_continuance_probe()
  528. */
  529. if (!dmac)
  530. return -EAGAIN;
  531. rsnd_dma_of_path(dma, io, is_play, &mod_from, &mod_to);
  532. dma->src_addr = rsnd_dma_addr(io, mod_from, is_play, 1);
  533. dma->dst_addr = rsnd_dma_addr(io, mod_to, is_play, 0);
  534. /* for Gen2 */
  535. if (mod_from && mod_to)
  536. dma->ops = &rsnd_dmapp_ops;
  537. else
  538. dma->ops = &rsnd_dmaen_ops;
  539. /* for Gen1, overwrite */
  540. if (rsnd_is_gen1(priv))
  541. dma->ops = &rsnd_dmaen_ops;
  542. dev_dbg(dev, "%s %s[%d] -> %s[%d]\n",
  543. dma->ops->name,
  544. rsnd_mod_name(mod_from), rsnd_mod_id(mod_from),
  545. rsnd_mod_name(mod_to), rsnd_mod_id(mod_to));
  546. return dma->ops->init(io, dma, id, mod_from, mod_to);
  547. }
  548. int rsnd_dma_probe(struct platform_device *pdev,
  549. const struct rsnd_of_data *of_data,
  550. struct rsnd_priv *priv)
  551. {
  552. struct device *dev = rsnd_priv_to_dev(priv);
  553. struct rsnd_dma_ctrl *dmac;
  554. struct resource *res;
  555. /*
  556. * for Gen1
  557. */
  558. if (rsnd_is_gen1(priv))
  559. return 0;
  560. /*
  561. * for Gen2
  562. */
  563. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "audmapp");
  564. dmac = devm_kzalloc(dev, sizeof(*dmac), GFP_KERNEL);
  565. if (!dmac || !res) {
  566. dev_err(dev, "dma allocate failed\n");
  567. return 0; /* it will be PIO mode */
  568. }
  569. dmac->dmapp_num = 0;
  570. dmac->base = devm_ioremap_resource(dev, res);
  571. if (IS_ERR(dmac->base))
  572. return PTR_ERR(dmac->base);
  573. priv->dma = dmac;
  574. return 0;
  575. }