rcar-audmapp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * This is for Renesas R-Car Audio-DMAC-peri-peri.
  3. *
  4. * Copyright (C) 2014 Renesas Electronics Corporation
  5. * Copyright (C) 2014 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  6. *
  7. * based on the drivers/dma/sh/shdma.c
  8. *
  9. * Copyright (C) 2011-2012 Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  10. * Copyright (C) 2009 Nobuhiro Iwamatsu <iwamatsu.nobuhiro@renesas.com>
  11. * Copyright (C) 2009 Renesas Solutions, Inc. All rights reserved.
  12. * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
  13. *
  14. * This is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/dmaengine.h>
  25. #include <linux/of_dma.h>
  26. #include <linux/platform_data/dma-rcar-audmapp.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/shdma-base.h>
  29. /*
  30. * DMA register
  31. */
  32. #define PDMASAR 0x00
  33. #define PDMADAR 0x04
  34. #define PDMACHCR 0x0c
  35. /* PDMACHCR */
  36. #define PDMACHCR_DE (1 << 0)
  37. #define AUDMAPP_MAX_CHANNELS 29
  38. /* Default MEMCPY transfer size = 2^2 = 4 bytes */
  39. #define LOG2_DEFAULT_XFER_SIZE 2
  40. #define AUDMAPP_SLAVE_NUMBER 256
  41. #define AUDMAPP_LEN_MAX (16 * 1024 * 1024)
  42. struct audmapp_chan {
  43. struct shdma_chan shdma_chan;
  44. void __iomem *base;
  45. dma_addr_t slave_addr;
  46. u32 chcr;
  47. };
  48. struct audmapp_device {
  49. struct shdma_dev shdma_dev;
  50. struct audmapp_pdata *pdata;
  51. struct device *dev;
  52. void __iomem *chan_reg;
  53. };
  54. struct audmapp_desc {
  55. struct shdma_desc shdma_desc;
  56. dma_addr_t src;
  57. dma_addr_t dst;
  58. };
  59. #define to_shdma_chan(c) container_of(c, struct shdma_chan, dma_chan)
  60. #define to_chan(chan) container_of(chan, struct audmapp_chan, shdma_chan)
  61. #define to_desc(sdesc) container_of(sdesc, struct audmapp_desc, shdma_desc)
  62. #define to_dev(chan) container_of(chan->shdma_chan.dma_chan.device, \
  63. struct audmapp_device, shdma_dev.dma_dev)
  64. static void audmapp_write(struct audmapp_chan *auchan, u32 data, u32 reg)
  65. {
  66. struct audmapp_device *audev = to_dev(auchan);
  67. struct device *dev = audev->dev;
  68. dev_dbg(dev, "w %p : %08x\n", auchan->base + reg, data);
  69. iowrite32(data, auchan->base + reg);
  70. }
  71. static u32 audmapp_read(struct audmapp_chan *auchan, u32 reg)
  72. {
  73. return ioread32(auchan->base + reg);
  74. }
  75. static void audmapp_halt(struct shdma_chan *schan)
  76. {
  77. struct audmapp_chan *auchan = to_chan(schan);
  78. int i;
  79. audmapp_write(auchan, 0, PDMACHCR);
  80. for (i = 0; i < 1024; i++) {
  81. if (0 == audmapp_read(auchan, PDMACHCR))
  82. return;
  83. udelay(1);
  84. }
  85. }
  86. static void audmapp_start_xfer(struct shdma_chan *schan,
  87. struct shdma_desc *sdesc)
  88. {
  89. struct audmapp_chan *auchan = to_chan(schan);
  90. struct audmapp_device *audev = to_dev(auchan);
  91. struct audmapp_desc *desc = to_desc(sdesc);
  92. struct device *dev = audev->dev;
  93. u32 chcr = auchan->chcr | PDMACHCR_DE;
  94. dev_dbg(dev, "src/dst/chcr = %pad/%pad/%08x\n",
  95. &desc->src, &desc->dst, chcr);
  96. audmapp_write(auchan, desc->src, PDMASAR);
  97. audmapp_write(auchan, desc->dst, PDMADAR);
  98. audmapp_write(auchan, chcr, PDMACHCR);
  99. }
  100. static int audmapp_get_config(struct audmapp_chan *auchan, int slave_id,
  101. u32 *chcr, dma_addr_t *dst)
  102. {
  103. struct audmapp_device *audev = to_dev(auchan);
  104. struct audmapp_pdata *pdata = audev->pdata;
  105. struct audmapp_slave_config *cfg;
  106. int i;
  107. *chcr = 0;
  108. *dst = 0;
  109. if (!pdata) { /* DT */
  110. *chcr = ((u32)slave_id) << 16;
  111. auchan->shdma_chan.slave_id = (slave_id) >> 8;
  112. return 0;
  113. }
  114. /* non-DT */
  115. if (slave_id >= AUDMAPP_SLAVE_NUMBER)
  116. return -ENXIO;
  117. for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
  118. if (cfg->slave_id == slave_id) {
  119. *chcr = cfg->chcr;
  120. *dst = cfg->dst;
  121. return 0;
  122. }
  123. return -ENXIO;
  124. }
  125. static int audmapp_set_slave(struct shdma_chan *schan, int slave_id,
  126. dma_addr_t slave_addr, bool try)
  127. {
  128. struct audmapp_chan *auchan = to_chan(schan);
  129. u32 chcr;
  130. dma_addr_t dst;
  131. int ret;
  132. ret = audmapp_get_config(auchan, slave_id, &chcr, &dst);
  133. if (ret < 0)
  134. return ret;
  135. if (try)
  136. return 0;
  137. auchan->chcr = chcr;
  138. auchan->slave_addr = slave_addr ? : dst;
  139. return 0;
  140. }
  141. static int audmapp_desc_setup(struct shdma_chan *schan,
  142. struct shdma_desc *sdesc,
  143. dma_addr_t src, dma_addr_t dst, size_t *len)
  144. {
  145. struct audmapp_desc *desc = to_desc(sdesc);
  146. if (*len > (size_t)AUDMAPP_LEN_MAX)
  147. *len = (size_t)AUDMAPP_LEN_MAX;
  148. desc->src = src;
  149. desc->dst = dst;
  150. return 0;
  151. }
  152. static void audmapp_setup_xfer(struct shdma_chan *schan,
  153. int slave_id)
  154. {
  155. }
  156. static dma_addr_t audmapp_slave_addr(struct shdma_chan *schan)
  157. {
  158. struct audmapp_chan *auchan = to_chan(schan);
  159. return auchan->slave_addr;
  160. }
  161. static bool audmapp_channel_busy(struct shdma_chan *schan)
  162. {
  163. struct audmapp_chan *auchan = to_chan(schan);
  164. u32 chcr = audmapp_read(auchan, PDMACHCR);
  165. return chcr & ~PDMACHCR_DE;
  166. }
  167. static bool audmapp_desc_completed(struct shdma_chan *schan,
  168. struct shdma_desc *sdesc)
  169. {
  170. return true;
  171. }
  172. static struct shdma_desc *audmapp_embedded_desc(void *buf, int i)
  173. {
  174. return &((struct audmapp_desc *)buf)[i].shdma_desc;
  175. }
  176. static const struct shdma_ops audmapp_shdma_ops = {
  177. .halt_channel = audmapp_halt,
  178. .desc_setup = audmapp_desc_setup,
  179. .set_slave = audmapp_set_slave,
  180. .start_xfer = audmapp_start_xfer,
  181. .embedded_desc = audmapp_embedded_desc,
  182. .setup_xfer = audmapp_setup_xfer,
  183. .slave_addr = audmapp_slave_addr,
  184. .channel_busy = audmapp_channel_busy,
  185. .desc_completed = audmapp_desc_completed,
  186. };
  187. static int audmapp_chan_probe(struct platform_device *pdev,
  188. struct audmapp_device *audev, int id)
  189. {
  190. struct shdma_dev *sdev = &audev->shdma_dev;
  191. struct audmapp_chan *auchan;
  192. struct shdma_chan *schan;
  193. struct device *dev = audev->dev;
  194. auchan = devm_kzalloc(dev, sizeof(*auchan), GFP_KERNEL);
  195. if (!auchan)
  196. return -ENOMEM;
  197. schan = &auchan->shdma_chan;
  198. schan->max_xfer_len = AUDMAPP_LEN_MAX;
  199. shdma_chan_probe(sdev, schan, id);
  200. auchan->base = audev->chan_reg + 0x20 + (0x10 * id);
  201. dev_dbg(dev, "%02d : %p / %p", id, auchan->base, audev->chan_reg);
  202. return 0;
  203. }
  204. static void audmapp_chan_remove(struct audmapp_device *audev)
  205. {
  206. struct shdma_chan *schan;
  207. int i;
  208. shdma_for_each_chan(schan, &audev->shdma_dev, i) {
  209. BUG_ON(!schan);
  210. shdma_chan_remove(schan);
  211. }
  212. }
  213. static struct dma_chan *audmapp_of_xlate(struct of_phandle_args *dma_spec,
  214. struct of_dma *ofdma)
  215. {
  216. dma_cap_mask_t mask;
  217. struct dma_chan *chan;
  218. u32 chcr = dma_spec->args[0];
  219. if (dma_spec->args_count != 1)
  220. return NULL;
  221. dma_cap_zero(mask);
  222. dma_cap_set(DMA_SLAVE, mask);
  223. chan = dma_request_channel(mask, shdma_chan_filter, NULL);
  224. if (chan)
  225. to_shdma_chan(chan)->hw_req = chcr;
  226. return chan;
  227. }
  228. static int audmapp_probe(struct platform_device *pdev)
  229. {
  230. struct audmapp_pdata *pdata = pdev->dev.platform_data;
  231. struct device_node *np = pdev->dev.of_node;
  232. struct audmapp_device *audev;
  233. struct shdma_dev *sdev;
  234. struct dma_device *dma_dev;
  235. struct resource *res;
  236. int err, i;
  237. if (np)
  238. of_dma_controller_register(np, audmapp_of_xlate, pdev);
  239. else if (!pdata)
  240. return -ENODEV;
  241. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  242. audev = devm_kzalloc(&pdev->dev, sizeof(*audev), GFP_KERNEL);
  243. if (!audev)
  244. return -ENOMEM;
  245. audev->dev = &pdev->dev;
  246. audev->pdata = pdata;
  247. audev->chan_reg = devm_ioremap_resource(&pdev->dev, res);
  248. if (IS_ERR(audev->chan_reg))
  249. return PTR_ERR(audev->chan_reg);
  250. sdev = &audev->shdma_dev;
  251. sdev->ops = &audmapp_shdma_ops;
  252. sdev->desc_size = sizeof(struct audmapp_desc);
  253. dma_dev = &sdev->dma_dev;
  254. dma_dev->copy_align = LOG2_DEFAULT_XFER_SIZE;
  255. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  256. err = shdma_init(&pdev->dev, sdev, AUDMAPP_MAX_CHANNELS);
  257. if (err < 0)
  258. return err;
  259. platform_set_drvdata(pdev, audev);
  260. /* Create DMA Channel */
  261. for (i = 0; i < AUDMAPP_MAX_CHANNELS; i++) {
  262. err = audmapp_chan_probe(pdev, audev, i);
  263. if (err)
  264. goto chan_probe_err;
  265. }
  266. err = dma_async_device_register(dma_dev);
  267. if (err < 0)
  268. goto chan_probe_err;
  269. return err;
  270. chan_probe_err:
  271. audmapp_chan_remove(audev);
  272. shdma_cleanup(sdev);
  273. return err;
  274. }
  275. static int audmapp_remove(struct platform_device *pdev)
  276. {
  277. struct audmapp_device *audev = platform_get_drvdata(pdev);
  278. struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
  279. dma_async_device_unregister(dma_dev);
  280. audmapp_chan_remove(audev);
  281. shdma_cleanup(&audev->shdma_dev);
  282. return 0;
  283. }
  284. static const struct of_device_id audmapp_of_match[] = {
  285. { .compatible = "renesas,rcar-audmapp", },
  286. {},
  287. };
  288. static struct platform_driver audmapp_driver = {
  289. .probe = audmapp_probe,
  290. .remove = audmapp_remove,
  291. .driver = {
  292. .name = "rcar-audmapp-engine",
  293. .of_match_table = audmapp_of_match,
  294. },
  295. };
  296. module_platform_driver(audmapp_driver);
  297. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  298. MODULE_DESCRIPTION("Renesas R-Car Audio DMAC peri-peri driver");
  299. MODULE_LICENSE("GPL");