hidma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * Qualcomm Technologies HIDMA DMA engine interface
  3. *
  4. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. /*
  16. * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
  17. * Copyright (C) Semihalf 2009
  18. * Copyright (C) Ilya Yanok, Emcraft Systems 2010
  19. * Copyright (C) Alexander Popov, Promcontroller 2014
  20. *
  21. * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
  22. * (defines, structures and comments) was taken from MPC5121 DMA driver
  23. * written by Hongjun Chen <hong-jun.chen@freescale.com>.
  24. *
  25. * Approved as OSADL project by a majority of OSADL members and funded
  26. * by OSADL membership fees in 2009; for details see www.osadl.org.
  27. *
  28. * This program is free software; you can redistribute it and/or modify it
  29. * under the terms of the GNU General Public License as published by the Free
  30. * Software Foundation; either version 2 of the License, or (at your option)
  31. * any later version.
  32. *
  33. * This program is distributed in the hope that it will be useful, but WITHOUT
  34. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  35. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  36. * more details.
  37. *
  38. * The full GNU General Public License is included in this distribution in the
  39. * file called COPYING.
  40. */
  41. /* Linux Foundation elects GPLv2 license only. */
  42. #include <linux/dmaengine.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/list.h>
  45. #include <linux/module.h>
  46. #include <linux/platform_device.h>
  47. #include <linux/slab.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/of_dma.h>
  50. #include <linux/property.h>
  51. #include <linux/delay.h>
  52. #include <linux/acpi.h>
  53. #include <linux/irq.h>
  54. #include <linux/atomic.h>
  55. #include <linux/pm_runtime.h>
  56. #include "../dmaengine.h"
  57. #include "hidma.h"
  58. /*
  59. * Default idle time is 2 seconds. This parameter can
  60. * be overridden by changing the following
  61. * /sys/bus/platform/devices/QCOM8061:<xy>/power/autosuspend_delay_ms
  62. * during kernel boot.
  63. */
  64. #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
  65. #define HIDMA_ERR_INFO_SW 0xFF
  66. #define HIDMA_ERR_CODE_UNEXPECTED_TERMINATE 0x0
  67. #define HIDMA_NR_DEFAULT_DESC 10
  68. static inline struct hidma_dev *to_hidma_dev(struct dma_device *dmadev)
  69. {
  70. return container_of(dmadev, struct hidma_dev, ddev);
  71. }
  72. static inline
  73. struct hidma_dev *to_hidma_dev_from_lldev(struct hidma_lldev **_lldevp)
  74. {
  75. return container_of(_lldevp, struct hidma_dev, lldev);
  76. }
  77. static inline struct hidma_chan *to_hidma_chan(struct dma_chan *dmach)
  78. {
  79. return container_of(dmach, struct hidma_chan, chan);
  80. }
  81. static inline
  82. struct hidma_desc *to_hidma_desc(struct dma_async_tx_descriptor *t)
  83. {
  84. return container_of(t, struct hidma_desc, desc);
  85. }
  86. static void hidma_free(struct hidma_dev *dmadev)
  87. {
  88. INIT_LIST_HEAD(&dmadev->ddev.channels);
  89. }
  90. static unsigned int nr_desc_prm;
  91. module_param(nr_desc_prm, uint, 0644);
  92. MODULE_PARM_DESC(nr_desc_prm, "number of descriptors (default: 0)");
  93. /* process completed descriptors */
  94. static void hidma_process_completed(struct hidma_chan *mchan)
  95. {
  96. struct dma_device *ddev = mchan->chan.device;
  97. struct hidma_dev *mdma = to_hidma_dev(ddev);
  98. struct dma_async_tx_descriptor *desc;
  99. dma_cookie_t last_cookie;
  100. struct hidma_desc *mdesc;
  101. unsigned long irqflags;
  102. struct list_head list;
  103. INIT_LIST_HEAD(&list);
  104. /* Get all completed descriptors */
  105. spin_lock_irqsave(&mchan->lock, irqflags);
  106. list_splice_tail_init(&mchan->completed, &list);
  107. spin_unlock_irqrestore(&mchan->lock, irqflags);
  108. /* Execute callbacks and run dependencies */
  109. list_for_each_entry(mdesc, &list, node) {
  110. enum dma_status llstat;
  111. desc = &mdesc->desc;
  112. spin_lock_irqsave(&mchan->lock, irqflags);
  113. dma_cookie_complete(desc);
  114. spin_unlock_irqrestore(&mchan->lock, irqflags);
  115. llstat = hidma_ll_status(mdma->lldev, mdesc->tre_ch);
  116. if (desc->callback && (llstat == DMA_COMPLETE))
  117. desc->callback(desc->callback_param);
  118. last_cookie = desc->cookie;
  119. dma_run_dependencies(desc);
  120. }
  121. /* Free descriptors */
  122. spin_lock_irqsave(&mchan->lock, irqflags);
  123. list_splice_tail_init(&list, &mchan->free);
  124. spin_unlock_irqrestore(&mchan->lock, irqflags);
  125. }
  126. /*
  127. * Called once for each submitted descriptor.
  128. * PM is locked once for each descriptor that is currently
  129. * in execution.
  130. */
  131. static void hidma_callback(void *data)
  132. {
  133. struct hidma_desc *mdesc = data;
  134. struct hidma_chan *mchan = to_hidma_chan(mdesc->desc.chan);
  135. struct dma_device *ddev = mchan->chan.device;
  136. struct hidma_dev *dmadev = to_hidma_dev(ddev);
  137. unsigned long irqflags;
  138. bool queued = false;
  139. spin_lock_irqsave(&mchan->lock, irqflags);
  140. if (mdesc->node.next) {
  141. /* Delete from the active list, add to completed list */
  142. list_move_tail(&mdesc->node, &mchan->completed);
  143. queued = true;
  144. /* calculate the next running descriptor */
  145. mchan->running = list_first_entry(&mchan->active,
  146. struct hidma_desc, node);
  147. }
  148. spin_unlock_irqrestore(&mchan->lock, irqflags);
  149. hidma_process_completed(mchan);
  150. if (queued) {
  151. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  152. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  153. }
  154. }
  155. static int hidma_chan_init(struct hidma_dev *dmadev, u32 dma_sig)
  156. {
  157. struct hidma_chan *mchan;
  158. struct dma_device *ddev;
  159. mchan = devm_kzalloc(dmadev->ddev.dev, sizeof(*mchan), GFP_KERNEL);
  160. if (!mchan)
  161. return -ENOMEM;
  162. ddev = &dmadev->ddev;
  163. mchan->dma_sig = dma_sig;
  164. mchan->dmadev = dmadev;
  165. mchan->chan.device = ddev;
  166. dma_cookie_init(&mchan->chan);
  167. INIT_LIST_HEAD(&mchan->free);
  168. INIT_LIST_HEAD(&mchan->prepared);
  169. INIT_LIST_HEAD(&mchan->active);
  170. INIT_LIST_HEAD(&mchan->completed);
  171. spin_lock_init(&mchan->lock);
  172. list_add_tail(&mchan->chan.device_node, &ddev->channels);
  173. dmadev->ddev.chancnt++;
  174. return 0;
  175. }
  176. static void hidma_issue_task(unsigned long arg)
  177. {
  178. struct hidma_dev *dmadev = (struct hidma_dev *)arg;
  179. pm_runtime_get_sync(dmadev->ddev.dev);
  180. hidma_ll_start(dmadev->lldev);
  181. }
  182. static void hidma_issue_pending(struct dma_chan *dmach)
  183. {
  184. struct hidma_chan *mchan = to_hidma_chan(dmach);
  185. struct hidma_dev *dmadev = mchan->dmadev;
  186. unsigned long flags;
  187. int status;
  188. spin_lock_irqsave(&mchan->lock, flags);
  189. if (!mchan->running) {
  190. struct hidma_desc *desc = list_first_entry(&mchan->active,
  191. struct hidma_desc,
  192. node);
  193. mchan->running = desc;
  194. }
  195. spin_unlock_irqrestore(&mchan->lock, flags);
  196. /* PM will be released in hidma_callback function. */
  197. status = pm_runtime_get(dmadev->ddev.dev);
  198. if (status < 0)
  199. tasklet_schedule(&dmadev->task);
  200. else
  201. hidma_ll_start(dmadev->lldev);
  202. }
  203. static enum dma_status hidma_tx_status(struct dma_chan *dmach,
  204. dma_cookie_t cookie,
  205. struct dma_tx_state *txstate)
  206. {
  207. struct hidma_chan *mchan = to_hidma_chan(dmach);
  208. enum dma_status ret;
  209. ret = dma_cookie_status(dmach, cookie, txstate);
  210. if (ret == DMA_COMPLETE)
  211. return ret;
  212. if (mchan->paused && (ret == DMA_IN_PROGRESS)) {
  213. unsigned long flags;
  214. dma_cookie_t runcookie;
  215. spin_lock_irqsave(&mchan->lock, flags);
  216. if (mchan->running)
  217. runcookie = mchan->running->desc.cookie;
  218. else
  219. runcookie = -EINVAL;
  220. if (runcookie == cookie)
  221. ret = DMA_PAUSED;
  222. spin_unlock_irqrestore(&mchan->lock, flags);
  223. }
  224. return ret;
  225. }
  226. /*
  227. * Submit descriptor to hardware.
  228. * Lock the PM for each descriptor we are sending.
  229. */
  230. static dma_cookie_t hidma_tx_submit(struct dma_async_tx_descriptor *txd)
  231. {
  232. struct hidma_chan *mchan = to_hidma_chan(txd->chan);
  233. struct hidma_dev *dmadev = mchan->dmadev;
  234. struct hidma_desc *mdesc;
  235. unsigned long irqflags;
  236. dma_cookie_t cookie;
  237. pm_runtime_get_sync(dmadev->ddev.dev);
  238. if (!hidma_ll_isenabled(dmadev->lldev)) {
  239. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  240. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  241. return -ENODEV;
  242. }
  243. mdesc = container_of(txd, struct hidma_desc, desc);
  244. spin_lock_irqsave(&mchan->lock, irqflags);
  245. /* Move descriptor to active */
  246. list_move_tail(&mdesc->node, &mchan->active);
  247. /* Update cookie */
  248. cookie = dma_cookie_assign(txd);
  249. hidma_ll_queue_request(dmadev->lldev, mdesc->tre_ch);
  250. spin_unlock_irqrestore(&mchan->lock, irqflags);
  251. return cookie;
  252. }
  253. static int hidma_alloc_chan_resources(struct dma_chan *dmach)
  254. {
  255. struct hidma_chan *mchan = to_hidma_chan(dmach);
  256. struct hidma_dev *dmadev = mchan->dmadev;
  257. struct hidma_desc *mdesc, *tmp;
  258. unsigned long irqflags;
  259. LIST_HEAD(descs);
  260. unsigned int i;
  261. int rc = 0;
  262. if (mchan->allocated)
  263. return 0;
  264. /* Alloc descriptors for this channel */
  265. for (i = 0; i < dmadev->nr_descriptors; i++) {
  266. mdesc = kzalloc(sizeof(struct hidma_desc), GFP_NOWAIT);
  267. if (!mdesc) {
  268. rc = -ENOMEM;
  269. break;
  270. }
  271. dma_async_tx_descriptor_init(&mdesc->desc, dmach);
  272. mdesc->desc.tx_submit = hidma_tx_submit;
  273. rc = hidma_ll_request(dmadev->lldev, mchan->dma_sig,
  274. "DMA engine", hidma_callback, mdesc,
  275. &mdesc->tre_ch);
  276. if (rc) {
  277. dev_err(dmach->device->dev,
  278. "channel alloc failed at %u\n", i);
  279. kfree(mdesc);
  280. break;
  281. }
  282. list_add_tail(&mdesc->node, &descs);
  283. }
  284. if (rc) {
  285. /* return the allocated descriptors */
  286. list_for_each_entry_safe(mdesc, tmp, &descs, node) {
  287. hidma_ll_free(dmadev->lldev, mdesc->tre_ch);
  288. kfree(mdesc);
  289. }
  290. return rc;
  291. }
  292. spin_lock_irqsave(&mchan->lock, irqflags);
  293. list_splice_tail_init(&descs, &mchan->free);
  294. mchan->allocated = true;
  295. spin_unlock_irqrestore(&mchan->lock, irqflags);
  296. return 1;
  297. }
  298. static struct dma_async_tx_descriptor *
  299. hidma_prep_dma_memcpy(struct dma_chan *dmach, dma_addr_t dest, dma_addr_t src,
  300. size_t len, unsigned long flags)
  301. {
  302. struct hidma_chan *mchan = to_hidma_chan(dmach);
  303. struct hidma_desc *mdesc = NULL;
  304. struct hidma_dev *mdma = mchan->dmadev;
  305. unsigned long irqflags;
  306. /* Get free descriptor */
  307. spin_lock_irqsave(&mchan->lock, irqflags);
  308. if (!list_empty(&mchan->free)) {
  309. mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
  310. list_del(&mdesc->node);
  311. }
  312. spin_unlock_irqrestore(&mchan->lock, irqflags);
  313. if (!mdesc)
  314. return NULL;
  315. hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
  316. src, dest, len, flags);
  317. /* Place descriptor in prepared list */
  318. spin_lock_irqsave(&mchan->lock, irqflags);
  319. list_add_tail(&mdesc->node, &mchan->prepared);
  320. spin_unlock_irqrestore(&mchan->lock, irqflags);
  321. return &mdesc->desc;
  322. }
  323. static int hidma_terminate_channel(struct dma_chan *chan)
  324. {
  325. struct hidma_chan *mchan = to_hidma_chan(chan);
  326. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  327. struct hidma_desc *tmp, *mdesc;
  328. unsigned long irqflags;
  329. LIST_HEAD(list);
  330. int rc;
  331. pm_runtime_get_sync(dmadev->ddev.dev);
  332. /* give completed requests a chance to finish */
  333. hidma_process_completed(mchan);
  334. spin_lock_irqsave(&mchan->lock, irqflags);
  335. list_splice_init(&mchan->active, &list);
  336. list_splice_init(&mchan->prepared, &list);
  337. list_splice_init(&mchan->completed, &list);
  338. spin_unlock_irqrestore(&mchan->lock, irqflags);
  339. /* this suspends the existing transfer */
  340. rc = hidma_ll_pause(dmadev->lldev);
  341. if (rc) {
  342. dev_err(dmadev->ddev.dev, "channel did not pause\n");
  343. goto out;
  344. }
  345. /* return all user requests */
  346. list_for_each_entry_safe(mdesc, tmp, &list, node) {
  347. struct dma_async_tx_descriptor *txd = &mdesc->desc;
  348. dma_async_tx_callback callback = mdesc->desc.callback;
  349. void *param = mdesc->desc.callback_param;
  350. dma_descriptor_unmap(txd);
  351. if (callback)
  352. callback(param);
  353. dma_run_dependencies(txd);
  354. /* move myself to free_list */
  355. list_move(&mdesc->node, &mchan->free);
  356. }
  357. rc = hidma_ll_resume(dmadev->lldev);
  358. out:
  359. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  360. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  361. return rc;
  362. }
  363. static int hidma_terminate_all(struct dma_chan *chan)
  364. {
  365. struct hidma_chan *mchan = to_hidma_chan(chan);
  366. struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
  367. int rc;
  368. rc = hidma_terminate_channel(chan);
  369. if (rc)
  370. return rc;
  371. /* reinitialize the hardware */
  372. pm_runtime_get_sync(dmadev->ddev.dev);
  373. rc = hidma_ll_setup(dmadev->lldev);
  374. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  375. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  376. return rc;
  377. }
  378. static void hidma_free_chan_resources(struct dma_chan *dmach)
  379. {
  380. struct hidma_chan *mchan = to_hidma_chan(dmach);
  381. struct hidma_dev *mdma = mchan->dmadev;
  382. struct hidma_desc *mdesc, *tmp;
  383. unsigned long irqflags;
  384. LIST_HEAD(descs);
  385. /* terminate running transactions and free descriptors */
  386. hidma_terminate_channel(dmach);
  387. spin_lock_irqsave(&mchan->lock, irqflags);
  388. /* Move data */
  389. list_splice_tail_init(&mchan->free, &descs);
  390. /* Free descriptors */
  391. list_for_each_entry_safe(mdesc, tmp, &descs, node) {
  392. hidma_ll_free(mdma->lldev, mdesc->tre_ch);
  393. list_del(&mdesc->node);
  394. kfree(mdesc);
  395. }
  396. mchan->allocated = 0;
  397. spin_unlock_irqrestore(&mchan->lock, irqflags);
  398. }
  399. static int hidma_pause(struct dma_chan *chan)
  400. {
  401. struct hidma_chan *mchan;
  402. struct hidma_dev *dmadev;
  403. mchan = to_hidma_chan(chan);
  404. dmadev = to_hidma_dev(mchan->chan.device);
  405. if (!mchan->paused) {
  406. pm_runtime_get_sync(dmadev->ddev.dev);
  407. if (hidma_ll_pause(dmadev->lldev))
  408. dev_warn(dmadev->ddev.dev, "channel did not stop\n");
  409. mchan->paused = true;
  410. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  411. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  412. }
  413. return 0;
  414. }
  415. static int hidma_resume(struct dma_chan *chan)
  416. {
  417. struct hidma_chan *mchan;
  418. struct hidma_dev *dmadev;
  419. int rc = 0;
  420. mchan = to_hidma_chan(chan);
  421. dmadev = to_hidma_dev(mchan->chan.device);
  422. if (mchan->paused) {
  423. pm_runtime_get_sync(dmadev->ddev.dev);
  424. rc = hidma_ll_resume(dmadev->lldev);
  425. if (!rc)
  426. mchan->paused = false;
  427. else
  428. dev_err(dmadev->ddev.dev,
  429. "failed to resume the channel");
  430. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  431. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  432. }
  433. return rc;
  434. }
  435. static irqreturn_t hidma_chirq_handler(int chirq, void *arg)
  436. {
  437. struct hidma_lldev *lldev = arg;
  438. /*
  439. * All interrupts are request driven.
  440. * HW doesn't send an interrupt by itself.
  441. */
  442. return hidma_ll_inthandler(chirq, lldev);
  443. }
  444. static int hidma_probe(struct platform_device *pdev)
  445. {
  446. struct hidma_dev *dmadev;
  447. struct resource *trca_resource;
  448. struct resource *evca_resource;
  449. int chirq;
  450. void __iomem *evca;
  451. void __iomem *trca;
  452. int rc;
  453. pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
  454. pm_runtime_use_autosuspend(&pdev->dev);
  455. pm_runtime_set_active(&pdev->dev);
  456. pm_runtime_enable(&pdev->dev);
  457. trca_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  458. trca = devm_ioremap_resource(&pdev->dev, trca_resource);
  459. if (IS_ERR(trca)) {
  460. rc = -ENOMEM;
  461. goto bailout;
  462. }
  463. evca_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  464. evca = devm_ioremap_resource(&pdev->dev, evca_resource);
  465. if (IS_ERR(evca)) {
  466. rc = -ENOMEM;
  467. goto bailout;
  468. }
  469. /*
  470. * This driver only handles the channel IRQs.
  471. * Common IRQ is handled by the management driver.
  472. */
  473. chirq = platform_get_irq(pdev, 0);
  474. if (chirq < 0) {
  475. rc = -ENODEV;
  476. goto bailout;
  477. }
  478. dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
  479. if (!dmadev) {
  480. rc = -ENOMEM;
  481. goto bailout;
  482. }
  483. INIT_LIST_HEAD(&dmadev->ddev.channels);
  484. spin_lock_init(&dmadev->lock);
  485. dmadev->ddev.dev = &pdev->dev;
  486. pm_runtime_get_sync(dmadev->ddev.dev);
  487. dma_cap_set(DMA_MEMCPY, dmadev->ddev.cap_mask);
  488. if (WARN_ON(!pdev->dev.dma_mask)) {
  489. rc = -ENXIO;
  490. goto dmafree;
  491. }
  492. dmadev->dev_evca = evca;
  493. dmadev->evca_resource = evca_resource;
  494. dmadev->dev_trca = trca;
  495. dmadev->trca_resource = trca_resource;
  496. dmadev->ddev.device_prep_dma_memcpy = hidma_prep_dma_memcpy;
  497. dmadev->ddev.device_alloc_chan_resources = hidma_alloc_chan_resources;
  498. dmadev->ddev.device_free_chan_resources = hidma_free_chan_resources;
  499. dmadev->ddev.device_tx_status = hidma_tx_status;
  500. dmadev->ddev.device_issue_pending = hidma_issue_pending;
  501. dmadev->ddev.device_pause = hidma_pause;
  502. dmadev->ddev.device_resume = hidma_resume;
  503. dmadev->ddev.device_terminate_all = hidma_terminate_all;
  504. dmadev->ddev.copy_align = 8;
  505. device_property_read_u32(&pdev->dev, "desc-count",
  506. &dmadev->nr_descriptors);
  507. if (!dmadev->nr_descriptors && nr_desc_prm)
  508. dmadev->nr_descriptors = nr_desc_prm;
  509. if (!dmadev->nr_descriptors)
  510. dmadev->nr_descriptors = HIDMA_NR_DEFAULT_DESC;
  511. dmadev->chidx = readl(dmadev->dev_trca + 0x28);
  512. /* Set DMA mask to 64 bits. */
  513. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  514. if (rc) {
  515. dev_warn(&pdev->dev, "unable to set coherent mask to 64");
  516. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  517. if (rc)
  518. goto dmafree;
  519. }
  520. dmadev->lldev = hidma_ll_init(dmadev->ddev.dev,
  521. dmadev->nr_descriptors, dmadev->dev_trca,
  522. dmadev->dev_evca, dmadev->chidx);
  523. if (!dmadev->lldev) {
  524. rc = -EPROBE_DEFER;
  525. goto dmafree;
  526. }
  527. rc = devm_request_irq(&pdev->dev, chirq, hidma_chirq_handler, 0,
  528. "qcom-hidma", dmadev->lldev);
  529. if (rc)
  530. goto uninit;
  531. INIT_LIST_HEAD(&dmadev->ddev.channels);
  532. rc = hidma_chan_init(dmadev, 0);
  533. if (rc)
  534. goto uninit;
  535. rc = dma_async_device_register(&dmadev->ddev);
  536. if (rc)
  537. goto uninit;
  538. dmadev->irq = chirq;
  539. tasklet_init(&dmadev->task, hidma_issue_task, (unsigned long)dmadev);
  540. dev_info(&pdev->dev, "HI-DMA engine driver registration complete\n");
  541. platform_set_drvdata(pdev, dmadev);
  542. pm_runtime_mark_last_busy(dmadev->ddev.dev);
  543. pm_runtime_put_autosuspend(dmadev->ddev.dev);
  544. return 0;
  545. uninit:
  546. hidma_ll_uninit(dmadev->lldev);
  547. dmafree:
  548. if (dmadev)
  549. hidma_free(dmadev);
  550. bailout:
  551. pm_runtime_put_sync(&pdev->dev);
  552. pm_runtime_disable(&pdev->dev);
  553. return rc;
  554. }
  555. static int hidma_remove(struct platform_device *pdev)
  556. {
  557. struct hidma_dev *dmadev = platform_get_drvdata(pdev);
  558. pm_runtime_get_sync(dmadev->ddev.dev);
  559. dma_async_device_unregister(&dmadev->ddev);
  560. devm_free_irq(dmadev->ddev.dev, dmadev->irq, dmadev->lldev);
  561. hidma_ll_uninit(dmadev->lldev);
  562. hidma_free(dmadev);
  563. dev_info(&pdev->dev, "HI-DMA engine removed\n");
  564. pm_runtime_put_sync_suspend(&pdev->dev);
  565. pm_runtime_disable(&pdev->dev);
  566. return 0;
  567. }
  568. #if IS_ENABLED(CONFIG_ACPI)
  569. static const struct acpi_device_id hidma_acpi_ids[] = {
  570. {"QCOM8061"},
  571. {},
  572. };
  573. #endif
  574. static const struct of_device_id hidma_match[] = {
  575. {.compatible = "qcom,hidma-1.0",},
  576. {},
  577. };
  578. MODULE_DEVICE_TABLE(of, hidma_match);
  579. static struct platform_driver hidma_driver = {
  580. .probe = hidma_probe,
  581. .remove = hidma_remove,
  582. .driver = {
  583. .name = "hidma",
  584. .of_match_table = hidma_match,
  585. .acpi_match_table = ACPI_PTR(hidma_acpi_ids),
  586. },
  587. };
  588. module_platform_driver(hidma_driver);
  589. MODULE_LICENSE("GPL v2");