dma.c 18 KB

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