rcar-audmapp.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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/platform_data/dma-rcar-audmapp.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/shdma-base.h>
  28. /*
  29. * DMA register
  30. */
  31. #define PDMASAR 0x00
  32. #define PDMADAR 0x04
  33. #define PDMACHCR 0x0c
  34. /* PDMACHCR */
  35. #define PDMACHCR_DE (1 << 0)
  36. #define AUDMAPP_MAX_CHANNELS 29
  37. /* Default MEMCPY transfer size = 2^2 = 4 bytes */
  38. #define LOG2_DEFAULT_XFER_SIZE 2
  39. #define AUDMAPP_SLAVE_NUMBER 256
  40. #define AUDMAPP_LEN_MAX (16 * 1024 * 1024)
  41. struct audmapp_chan {
  42. struct shdma_chan shdma_chan;
  43. struct audmapp_slave_config *config;
  44. void __iomem *base;
  45. };
  46. struct audmapp_device {
  47. struct shdma_dev shdma_dev;
  48. struct audmapp_pdata *pdata;
  49. struct device *dev;
  50. void __iomem *chan_reg;
  51. };
  52. #define to_chan(chan) container_of(chan, struct audmapp_chan, shdma_chan)
  53. #define to_dev(chan) container_of(chan->shdma_chan.dma_chan.device, \
  54. struct audmapp_device, shdma_dev.dma_dev)
  55. static void audmapp_write(struct audmapp_chan *auchan, u32 data, u32 reg)
  56. {
  57. struct audmapp_device *audev = to_dev(auchan);
  58. struct device *dev = audev->dev;
  59. dev_dbg(dev, "w %p : %08x\n", auchan->base + reg, data);
  60. iowrite32(data, auchan->base + reg);
  61. }
  62. static u32 audmapp_read(struct audmapp_chan *auchan, u32 reg)
  63. {
  64. return ioread32(auchan->base + reg);
  65. }
  66. static void audmapp_halt(struct shdma_chan *schan)
  67. {
  68. struct audmapp_chan *auchan = to_chan(schan);
  69. int i;
  70. audmapp_write(auchan, 0, PDMACHCR);
  71. for (i = 0; i < 1024; i++) {
  72. if (0 == audmapp_read(auchan, PDMACHCR))
  73. return;
  74. udelay(1);
  75. }
  76. }
  77. static void audmapp_start_xfer(struct shdma_chan *schan,
  78. struct shdma_desc *sdecs)
  79. {
  80. struct audmapp_chan *auchan = to_chan(schan);
  81. struct audmapp_device *audev = to_dev(auchan);
  82. struct audmapp_slave_config *cfg = auchan->config;
  83. struct device *dev = audev->dev;
  84. u32 chcr = cfg->chcr | PDMACHCR_DE;
  85. dev_dbg(dev, "src/dst/chcr = %pad/%pad/%x\n",
  86. &cfg->src, &cfg->dst, cfg->chcr);
  87. audmapp_write(auchan, cfg->src, PDMASAR);
  88. audmapp_write(auchan, cfg->dst, PDMADAR);
  89. audmapp_write(auchan, chcr, PDMACHCR);
  90. }
  91. static struct audmapp_slave_config *
  92. audmapp_find_slave(struct audmapp_chan *auchan, int slave_id)
  93. {
  94. struct audmapp_device *audev = to_dev(auchan);
  95. struct audmapp_pdata *pdata = audev->pdata;
  96. struct audmapp_slave_config *cfg;
  97. int i;
  98. if (slave_id >= AUDMAPP_SLAVE_NUMBER)
  99. return NULL;
  100. for (i = 0, cfg = pdata->slave; i < pdata->slave_num; i++, cfg++)
  101. if (cfg->slave_id == slave_id)
  102. return cfg;
  103. return NULL;
  104. }
  105. static int audmapp_set_slave(struct shdma_chan *schan, int slave_id,
  106. dma_addr_t slave_addr, bool try)
  107. {
  108. struct audmapp_chan *auchan = to_chan(schan);
  109. struct audmapp_slave_config *cfg =
  110. audmapp_find_slave(auchan, slave_id);
  111. if (!cfg)
  112. return -ENODEV;
  113. if (try)
  114. return 0;
  115. auchan->config = cfg;
  116. return 0;
  117. }
  118. static int audmapp_desc_setup(struct shdma_chan *schan,
  119. struct shdma_desc *sdecs,
  120. dma_addr_t src, dma_addr_t dst, size_t *len)
  121. {
  122. struct audmapp_chan *auchan = to_chan(schan);
  123. struct audmapp_slave_config *cfg = auchan->config;
  124. if (!cfg)
  125. return -ENODEV;
  126. if (*len > (size_t)AUDMAPP_LEN_MAX)
  127. *len = (size_t)AUDMAPP_LEN_MAX;
  128. return 0;
  129. }
  130. static void audmapp_setup_xfer(struct shdma_chan *schan,
  131. int slave_id)
  132. {
  133. }
  134. static dma_addr_t audmapp_slave_addr(struct shdma_chan *schan)
  135. {
  136. return 0; /* always fixed address */
  137. }
  138. static bool audmapp_channel_busy(struct shdma_chan *schan)
  139. {
  140. struct audmapp_chan *auchan = to_chan(schan);
  141. u32 chcr = audmapp_read(auchan, PDMACHCR);
  142. return chcr & ~PDMACHCR_DE;
  143. }
  144. static bool audmapp_desc_completed(struct shdma_chan *schan,
  145. struct shdma_desc *sdesc)
  146. {
  147. return true;
  148. }
  149. static struct shdma_desc *audmapp_embedded_desc(void *buf, int i)
  150. {
  151. return &((struct shdma_desc *)buf)[i];
  152. }
  153. static const struct shdma_ops audmapp_shdma_ops = {
  154. .halt_channel = audmapp_halt,
  155. .desc_setup = audmapp_desc_setup,
  156. .set_slave = audmapp_set_slave,
  157. .start_xfer = audmapp_start_xfer,
  158. .embedded_desc = audmapp_embedded_desc,
  159. .setup_xfer = audmapp_setup_xfer,
  160. .slave_addr = audmapp_slave_addr,
  161. .channel_busy = audmapp_channel_busy,
  162. .desc_completed = audmapp_desc_completed,
  163. };
  164. static int audmapp_chan_probe(struct platform_device *pdev,
  165. struct audmapp_device *audev, int id)
  166. {
  167. struct shdma_dev *sdev = &audev->shdma_dev;
  168. struct audmapp_chan *auchan;
  169. struct shdma_chan *schan;
  170. struct device *dev = audev->dev;
  171. auchan = devm_kzalloc(dev, sizeof(*auchan), GFP_KERNEL);
  172. if (!auchan)
  173. return -ENOMEM;
  174. schan = &auchan->shdma_chan;
  175. schan->max_xfer_len = AUDMAPP_LEN_MAX;
  176. shdma_chan_probe(sdev, schan, id);
  177. auchan->base = audev->chan_reg + 0x20 + (0x10 * id);
  178. dev_dbg(dev, "%02d : %p / %p", id, auchan->base, audev->chan_reg);
  179. return 0;
  180. }
  181. static void audmapp_chan_remove(struct audmapp_device *audev)
  182. {
  183. struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
  184. struct shdma_chan *schan;
  185. int i;
  186. shdma_for_each_chan(schan, &audev->shdma_dev, i) {
  187. BUG_ON(!schan);
  188. shdma_chan_remove(schan);
  189. }
  190. dma_dev->chancnt = 0;
  191. }
  192. static int audmapp_probe(struct platform_device *pdev)
  193. {
  194. struct audmapp_pdata *pdata = pdev->dev.platform_data;
  195. struct audmapp_device *audev;
  196. struct shdma_dev *sdev;
  197. struct dma_device *dma_dev;
  198. struct resource *res;
  199. int err, i;
  200. if (!pdata)
  201. return -ENODEV;
  202. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  203. audev = devm_kzalloc(&pdev->dev, sizeof(*audev), GFP_KERNEL);
  204. if (!audev)
  205. return -ENOMEM;
  206. audev->dev = &pdev->dev;
  207. audev->pdata = pdata;
  208. audev->chan_reg = devm_ioremap_resource(&pdev->dev, res);
  209. if (IS_ERR(audev->chan_reg))
  210. return PTR_ERR(audev->chan_reg);
  211. sdev = &audev->shdma_dev;
  212. sdev->ops = &audmapp_shdma_ops;
  213. sdev->desc_size = sizeof(struct shdma_desc);
  214. dma_dev = &sdev->dma_dev;
  215. dma_dev->copy_align = LOG2_DEFAULT_XFER_SIZE;
  216. dma_cap_set(DMA_SLAVE, dma_dev->cap_mask);
  217. err = shdma_init(&pdev->dev, sdev, AUDMAPP_MAX_CHANNELS);
  218. if (err < 0)
  219. return err;
  220. platform_set_drvdata(pdev, audev);
  221. /* Create DMA Channel */
  222. for (i = 0; i < AUDMAPP_MAX_CHANNELS; i++) {
  223. err = audmapp_chan_probe(pdev, audev, i);
  224. if (err)
  225. goto chan_probe_err;
  226. }
  227. err = dma_async_device_register(dma_dev);
  228. if (err < 0)
  229. goto chan_probe_err;
  230. return err;
  231. chan_probe_err:
  232. audmapp_chan_remove(audev);
  233. shdma_cleanup(sdev);
  234. return err;
  235. }
  236. static int audmapp_remove(struct platform_device *pdev)
  237. {
  238. struct audmapp_device *audev = platform_get_drvdata(pdev);
  239. struct dma_device *dma_dev = &audev->shdma_dev.dma_dev;
  240. dma_async_device_unregister(dma_dev);
  241. audmapp_chan_remove(audev);
  242. shdma_cleanup(&audev->shdma_dev);
  243. return 0;
  244. }
  245. static struct platform_driver audmapp_driver = {
  246. .probe = audmapp_probe,
  247. .remove = audmapp_remove,
  248. .driver = {
  249. .owner = THIS_MODULE,
  250. .name = "rcar-audmapp-engine",
  251. },
  252. };
  253. module_platform_driver(audmapp_driver);
  254. MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
  255. MODULE_DESCRIPTION("Renesas R-Car Audio DMAC peri-peri driver");
  256. MODULE_LICENSE("GPL");