mtk-uart-apdma.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MediaTek UART APDMA driver.
  4. *
  5. * Copyright (c) 2019 MediaTek Inc.
  6. * Author: Long Cheng <long.cheng@mediatek.com>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/dmaengine.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/of_dma.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. #include "../virt-dma.h"
  24. /* The default number of virtual channel */
  25. #define MTK_UART_APDMA_NR_VCHANS 8
  26. #define VFF_EN_B BIT(0)
  27. #define VFF_STOP_B BIT(0)
  28. #define VFF_FLUSH_B BIT(0)
  29. #define VFF_4G_EN_B BIT(0)
  30. /* rx valid size >= vff thre */
  31. #define VFF_RX_INT_EN_B (BIT(0) | BIT(1))
  32. /* tx left size >= vff thre */
  33. #define VFF_TX_INT_EN_B BIT(0)
  34. #define VFF_WARM_RST_B BIT(0)
  35. #define VFF_RX_INT_CLR_B (BIT(0) | BIT(1))
  36. #define VFF_TX_INT_CLR_B 0
  37. #define VFF_STOP_CLR_B 0
  38. #define VFF_EN_CLR_B 0
  39. #define VFF_INT_EN_CLR_B 0
  40. #define VFF_4G_SUPPORT_CLR_B 0
  41. /*
  42. * interrupt trigger level for tx
  43. * if threshold is n, no polling is required to start tx.
  44. * otherwise need polling VFF_FLUSH.
  45. */
  46. #define VFF_TX_THRE(n) (n)
  47. /* interrupt trigger level for rx */
  48. #define VFF_RX_THRE(n) ((n) * 3 / 4)
  49. #define VFF_RING_SIZE 0xffff
  50. /* invert this bit when wrap ring head again */
  51. #define VFF_RING_WRAP 0x10000
  52. #define VFF_INT_FLAG 0x00
  53. #define VFF_INT_EN 0x04
  54. #define VFF_EN 0x08
  55. #define VFF_RST 0x0c
  56. #define VFF_STOP 0x10
  57. #define VFF_FLUSH 0x14
  58. #define VFF_ADDR 0x1c
  59. #define VFF_LEN 0x24
  60. #define VFF_THRE 0x28
  61. #define VFF_WPT 0x2c
  62. #define VFF_RPT 0x30
  63. /* TX: the buffer size HW can read. RX: the buffer size SW can read. */
  64. #define VFF_VALID_SIZE 0x3c
  65. /* TX: the buffer size SW can write. RX: the buffer size HW can write. */
  66. #define VFF_LEFT_SIZE 0x40
  67. #define VFF_DEBUG_STATUS 0x50
  68. #define VFF_4G_SUPPORT 0x54
  69. struct mtk_uart_apdmadev {
  70. struct dma_device ddev;
  71. struct clk *clk;
  72. bool support_33bits;
  73. unsigned int dma_requests;
  74. };
  75. struct mtk_uart_apdma_desc {
  76. struct virt_dma_desc vd;
  77. dma_addr_t addr;
  78. unsigned int avail_len;
  79. };
  80. struct mtk_chan {
  81. struct virt_dma_chan vc;
  82. struct dma_slave_config cfg;
  83. struct mtk_uart_apdma_desc *desc;
  84. enum dma_transfer_direction dir;
  85. void __iomem *base;
  86. unsigned int irq;
  87. unsigned int rx_status;
  88. };
  89. static inline struct mtk_uart_apdmadev *
  90. to_mtk_uart_apdma_dev(struct dma_device *d)
  91. {
  92. return container_of(d, struct mtk_uart_apdmadev, ddev);
  93. }
  94. static inline struct mtk_chan *to_mtk_uart_apdma_chan(struct dma_chan *c)
  95. {
  96. return container_of(c, struct mtk_chan, vc.chan);
  97. }
  98. static inline struct mtk_uart_apdma_desc *to_mtk_uart_apdma_desc
  99. (struct dma_async_tx_descriptor *t)
  100. {
  101. return container_of(t, struct mtk_uart_apdma_desc, vd.tx);
  102. }
  103. static void mtk_uart_apdma_write(struct mtk_chan *c,
  104. unsigned int reg, unsigned int val)
  105. {
  106. writel(val, c->base + reg);
  107. }
  108. static unsigned int mtk_uart_apdma_read(struct mtk_chan *c, unsigned int reg)
  109. {
  110. return readl(c->base + reg);
  111. }
  112. static void mtk_uart_apdma_desc_free(struct virt_dma_desc *vd)
  113. {
  114. kfree(container_of(vd, struct mtk_uart_apdma_desc, vd));
  115. }
  116. static void mtk_uart_apdma_start_tx(struct mtk_chan *c)
  117. {
  118. struct mtk_uart_apdmadev *mtkd =
  119. to_mtk_uart_apdma_dev(c->vc.chan.device);
  120. struct mtk_uart_apdma_desc *d = c->desc;
  121. unsigned int wpt, vff_sz;
  122. vff_sz = c->cfg.dst_port_window_size;
  123. if (!mtk_uart_apdma_read(c, VFF_LEN)) {
  124. mtk_uart_apdma_write(c, VFF_ADDR, d->addr);
  125. mtk_uart_apdma_write(c, VFF_LEN, vff_sz);
  126. mtk_uart_apdma_write(c, VFF_THRE, VFF_TX_THRE(vff_sz));
  127. mtk_uart_apdma_write(c, VFF_WPT, 0);
  128. mtk_uart_apdma_write(c, VFF_INT_FLAG, VFF_TX_INT_CLR_B);
  129. if (mtkd->support_33bits)
  130. mtk_uart_apdma_write(c, VFF_4G_SUPPORT, VFF_4G_EN_B);
  131. }
  132. mtk_uart_apdma_write(c, VFF_EN, VFF_EN_B);
  133. if (mtk_uart_apdma_read(c, VFF_EN) != VFF_EN_B)
  134. dev_err(c->vc.chan.device->dev, "Enable TX fail\n");
  135. if (!mtk_uart_apdma_read(c, VFF_LEFT_SIZE)) {
  136. mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
  137. return;
  138. }
  139. wpt = mtk_uart_apdma_read(c, VFF_WPT);
  140. wpt += c->desc->avail_len;
  141. if ((wpt & VFF_RING_SIZE) == vff_sz)
  142. wpt = (wpt & VFF_RING_WRAP) ^ VFF_RING_WRAP;
  143. /* Let DMA start moving data */
  144. mtk_uart_apdma_write(c, VFF_WPT, wpt);
  145. /* HW auto set to 0 when left size >= threshold */
  146. mtk_uart_apdma_write(c, VFF_INT_EN, VFF_TX_INT_EN_B);
  147. if (!mtk_uart_apdma_read(c, VFF_FLUSH))
  148. mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
  149. }
  150. static void mtk_uart_apdma_start_rx(struct mtk_chan *c)
  151. {
  152. struct mtk_uart_apdmadev *mtkd =
  153. to_mtk_uart_apdma_dev(c->vc.chan.device);
  154. struct mtk_uart_apdma_desc *d = c->desc;
  155. unsigned int vff_sz;
  156. vff_sz = c->cfg.src_port_window_size;
  157. if (!mtk_uart_apdma_read(c, VFF_LEN)) {
  158. mtk_uart_apdma_write(c, VFF_ADDR, d->addr);
  159. mtk_uart_apdma_write(c, VFF_LEN, vff_sz);
  160. mtk_uart_apdma_write(c, VFF_THRE, VFF_RX_THRE(vff_sz));
  161. mtk_uart_apdma_write(c, VFF_RPT, 0);
  162. mtk_uart_apdma_write(c, VFF_INT_FLAG, VFF_RX_INT_CLR_B);
  163. if (mtkd->support_33bits)
  164. mtk_uart_apdma_write(c, VFF_4G_SUPPORT, VFF_4G_EN_B);
  165. }
  166. mtk_uart_apdma_write(c, VFF_INT_EN, VFF_RX_INT_EN_B);
  167. mtk_uart_apdma_write(c, VFF_EN, VFF_EN_B);
  168. if (mtk_uart_apdma_read(c, VFF_EN) != VFF_EN_B)
  169. dev_err(c->vc.chan.device->dev, "Enable RX fail\n");
  170. }
  171. static void mtk_uart_apdma_tx_handler(struct mtk_chan *c)
  172. {
  173. mtk_uart_apdma_write(c, VFF_INT_FLAG, VFF_TX_INT_CLR_B);
  174. mtk_uart_apdma_write(c, VFF_INT_EN, VFF_INT_EN_CLR_B);
  175. mtk_uart_apdma_write(c, VFF_EN, VFF_EN_CLR_B);
  176. }
  177. static void mtk_uart_apdma_rx_handler(struct mtk_chan *c)
  178. {
  179. struct mtk_uart_apdma_desc *d = c->desc;
  180. unsigned int len, wg, rg;
  181. int cnt;
  182. mtk_uart_apdma_write(c, VFF_INT_FLAG, VFF_RX_INT_CLR_B);
  183. if (!mtk_uart_apdma_read(c, VFF_VALID_SIZE))
  184. return;
  185. mtk_uart_apdma_write(c, VFF_EN, VFF_EN_CLR_B);
  186. mtk_uart_apdma_write(c, VFF_INT_EN, VFF_INT_EN_CLR_B);
  187. len = c->cfg.src_port_window_size;
  188. rg = mtk_uart_apdma_read(c, VFF_RPT);
  189. wg = mtk_uart_apdma_read(c, VFF_WPT);
  190. cnt = (wg & VFF_RING_SIZE) - (rg & VFF_RING_SIZE);
  191. /*
  192. * The buffer is ring buffer. If wrap bit different,
  193. * represents the start of the next cycle for WPT
  194. */
  195. if ((rg ^ wg) & VFF_RING_WRAP)
  196. cnt += len;
  197. c->rx_status = d->avail_len - cnt;
  198. mtk_uart_apdma_write(c, VFF_RPT, wg);
  199. }
  200. static void mtk_uart_apdma_chan_complete_handler(struct mtk_chan *c)
  201. {
  202. struct mtk_uart_apdma_desc *d = c->desc;
  203. if (d) {
  204. list_del(&d->vd.node);
  205. vchan_cookie_complete(&d->vd);
  206. c->desc = NULL;
  207. }
  208. }
  209. static irqreturn_t mtk_uart_apdma_irq_handler(int irq, void *dev_id)
  210. {
  211. struct dma_chan *chan = (struct dma_chan *)dev_id;
  212. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  213. unsigned long flags;
  214. spin_lock_irqsave(&c->vc.lock, flags);
  215. if (c->dir == DMA_DEV_TO_MEM)
  216. mtk_uart_apdma_rx_handler(c);
  217. else if (c->dir == DMA_MEM_TO_DEV)
  218. mtk_uart_apdma_tx_handler(c);
  219. mtk_uart_apdma_chan_complete_handler(c);
  220. spin_unlock_irqrestore(&c->vc.lock, flags);
  221. return IRQ_HANDLED;
  222. }
  223. static int mtk_uart_apdma_alloc_chan_resources(struct dma_chan *chan)
  224. {
  225. struct mtk_uart_apdmadev *mtkd = to_mtk_uart_apdma_dev(chan->device);
  226. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  227. unsigned int status;
  228. int ret;
  229. ret = pm_runtime_resume_and_get(mtkd->ddev.dev);
  230. if (ret < 0) {
  231. pm_runtime_put_noidle(chan->device->dev);
  232. return ret;
  233. }
  234. mtk_uart_apdma_write(c, VFF_ADDR, 0);
  235. mtk_uart_apdma_write(c, VFF_THRE, 0);
  236. mtk_uart_apdma_write(c, VFF_LEN, 0);
  237. mtk_uart_apdma_write(c, VFF_RST, VFF_WARM_RST_B);
  238. ret = readx_poll_timeout(readl, c->base + VFF_EN,
  239. status, !status, 10, 100);
  240. if (ret)
  241. goto err_pm;
  242. ret = request_irq(c->irq, mtk_uart_apdma_irq_handler,
  243. IRQF_TRIGGER_NONE, KBUILD_MODNAME, chan);
  244. if (ret < 0) {
  245. dev_err(chan->device->dev, "Can't request dma IRQ\n");
  246. ret = -EINVAL;
  247. goto err_pm;
  248. }
  249. if (mtkd->support_33bits)
  250. mtk_uart_apdma_write(c, VFF_4G_SUPPORT, VFF_4G_SUPPORT_CLR_B);
  251. err_pm:
  252. pm_runtime_put_noidle(mtkd->ddev.dev);
  253. return ret;
  254. }
  255. static void mtk_uart_apdma_free_chan_resources(struct dma_chan *chan)
  256. {
  257. struct mtk_uart_apdmadev *mtkd = to_mtk_uart_apdma_dev(chan->device);
  258. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  259. free_irq(c->irq, chan);
  260. tasklet_kill(&c->vc.task);
  261. vchan_free_chan_resources(&c->vc);
  262. pm_runtime_put_sync(mtkd->ddev.dev);
  263. }
  264. static enum dma_status mtk_uart_apdma_tx_status(struct dma_chan *chan,
  265. dma_cookie_t cookie,
  266. struct dma_tx_state *txstate)
  267. {
  268. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  269. enum dma_status ret;
  270. ret = dma_cookie_status(chan, cookie, txstate);
  271. if (!txstate)
  272. return ret;
  273. dma_set_residue(txstate, c->rx_status);
  274. return ret;
  275. }
  276. /*
  277. * dmaengine_prep_slave_single will call the function. and sglen is 1.
  278. * 8250 uart using one ring buffer, and deal with one sg.
  279. */
  280. static struct dma_async_tx_descriptor *mtk_uart_apdma_prep_slave_sg
  281. (struct dma_chan *chan, struct scatterlist *sgl,
  282. unsigned int sglen, enum dma_transfer_direction dir,
  283. unsigned long tx_flags, void *context)
  284. {
  285. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  286. struct mtk_uart_apdma_desc *d;
  287. if (!is_slave_direction(dir) || sglen != 1)
  288. return NULL;
  289. /* Now allocate and setup the descriptor */
  290. d = kzalloc(sizeof(*d), GFP_NOWAIT);
  291. if (!d)
  292. return NULL;
  293. d->avail_len = sg_dma_len(sgl);
  294. d->addr = sg_dma_address(sgl);
  295. c->dir = dir;
  296. return vchan_tx_prep(&c->vc, &d->vd, tx_flags);
  297. }
  298. static void mtk_uart_apdma_issue_pending(struct dma_chan *chan)
  299. {
  300. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  301. struct virt_dma_desc *vd;
  302. unsigned long flags;
  303. spin_lock_irqsave(&c->vc.lock, flags);
  304. if (vchan_issue_pending(&c->vc) && !c->desc) {
  305. vd = vchan_next_desc(&c->vc);
  306. c->desc = to_mtk_uart_apdma_desc(&vd->tx);
  307. if (c->dir == DMA_DEV_TO_MEM)
  308. mtk_uart_apdma_start_rx(c);
  309. else if (c->dir == DMA_MEM_TO_DEV)
  310. mtk_uart_apdma_start_tx(c);
  311. }
  312. spin_unlock_irqrestore(&c->vc.lock, flags);
  313. }
  314. static int mtk_uart_apdma_slave_config(struct dma_chan *chan,
  315. struct dma_slave_config *config)
  316. {
  317. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  318. memcpy(&c->cfg, config, sizeof(*config));
  319. return 0;
  320. }
  321. static int mtk_uart_apdma_terminate_all(struct dma_chan *chan)
  322. {
  323. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  324. unsigned long flags;
  325. unsigned int status;
  326. LIST_HEAD(head);
  327. int ret;
  328. mtk_uart_apdma_write(c, VFF_FLUSH, VFF_FLUSH_B);
  329. ret = readx_poll_timeout(readl, c->base + VFF_FLUSH,
  330. status, status != VFF_FLUSH_B, 10, 100);
  331. if (ret)
  332. dev_err(c->vc.chan.device->dev, "flush: fail, status=0x%x\n",
  333. mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
  334. /*
  335. * Stop need 3 steps.
  336. * 1. set stop to 1
  337. * 2. wait en to 0
  338. * 3. set stop as 0
  339. */
  340. mtk_uart_apdma_write(c, VFF_STOP, VFF_STOP_B);
  341. ret = readx_poll_timeout(readl, c->base + VFF_EN,
  342. status, !status, 10, 100);
  343. if (ret)
  344. dev_err(c->vc.chan.device->dev, "stop: fail, status=0x%x\n",
  345. mtk_uart_apdma_read(c, VFF_DEBUG_STATUS));
  346. mtk_uart_apdma_write(c, VFF_STOP, VFF_STOP_CLR_B);
  347. mtk_uart_apdma_write(c, VFF_INT_EN, VFF_INT_EN_CLR_B);
  348. if (c->dir == DMA_DEV_TO_MEM)
  349. mtk_uart_apdma_write(c, VFF_INT_FLAG, VFF_RX_INT_CLR_B);
  350. else if (c->dir == DMA_MEM_TO_DEV)
  351. mtk_uart_apdma_write(c, VFF_INT_FLAG, VFF_TX_INT_CLR_B);
  352. synchronize_irq(c->irq);
  353. spin_lock_irqsave(&c->vc.lock, flags);
  354. vchan_get_all_descriptors(&c->vc, &head);
  355. spin_unlock_irqrestore(&c->vc.lock, flags);
  356. vchan_dma_desc_free_list(&c->vc, &head);
  357. return 0;
  358. }
  359. static int mtk_uart_apdma_device_pause(struct dma_chan *chan)
  360. {
  361. struct mtk_chan *c = to_mtk_uart_apdma_chan(chan);
  362. unsigned long flags;
  363. spin_lock_irqsave(&c->vc.lock, flags);
  364. mtk_uart_apdma_write(c, VFF_EN, VFF_EN_CLR_B);
  365. mtk_uart_apdma_write(c, VFF_INT_EN, VFF_INT_EN_CLR_B);
  366. spin_unlock_irqrestore(&c->vc.lock, flags);
  367. synchronize_irq(c->irq);
  368. return 0;
  369. }
  370. static void mtk_uart_apdma_free(struct mtk_uart_apdmadev *mtkd)
  371. {
  372. while (!list_empty(&mtkd->ddev.channels)) {
  373. struct mtk_chan *c = list_first_entry(&mtkd->ddev.channels,
  374. struct mtk_chan, vc.chan.device_node);
  375. list_del(&c->vc.chan.device_node);
  376. tasklet_kill(&c->vc.task);
  377. }
  378. }
  379. static const struct of_device_id mtk_uart_apdma_match[] = {
  380. { .compatible = "mediatek,mt6577-uart-dma", },
  381. { /* sentinel */ },
  382. };
  383. MODULE_DEVICE_TABLE(of, mtk_uart_apdma_match);
  384. static int mtk_uart_apdma_probe(struct platform_device *pdev)
  385. {
  386. struct device_node *np = pdev->dev.of_node;
  387. struct mtk_uart_apdmadev *mtkd;
  388. int bit_mask = 32, rc;
  389. struct mtk_chan *c;
  390. unsigned int i;
  391. mtkd = devm_kzalloc(&pdev->dev, sizeof(*mtkd), GFP_KERNEL);
  392. if (!mtkd)
  393. return -ENOMEM;
  394. mtkd->clk = devm_clk_get(&pdev->dev, NULL);
  395. if (IS_ERR(mtkd->clk)) {
  396. dev_err(&pdev->dev, "No clock specified\n");
  397. rc = PTR_ERR(mtkd->clk);
  398. return rc;
  399. }
  400. if (of_property_read_bool(np, "mediatek,dma-33bits"))
  401. mtkd->support_33bits = true;
  402. if (mtkd->support_33bits)
  403. bit_mask = 33;
  404. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(bit_mask));
  405. if (rc)
  406. return rc;
  407. dma_cap_set(DMA_SLAVE, mtkd->ddev.cap_mask);
  408. mtkd->ddev.device_alloc_chan_resources =
  409. mtk_uart_apdma_alloc_chan_resources;
  410. mtkd->ddev.device_free_chan_resources =
  411. mtk_uart_apdma_free_chan_resources;
  412. mtkd->ddev.device_tx_status = mtk_uart_apdma_tx_status;
  413. mtkd->ddev.device_issue_pending = mtk_uart_apdma_issue_pending;
  414. mtkd->ddev.device_prep_slave_sg = mtk_uart_apdma_prep_slave_sg;
  415. mtkd->ddev.device_config = mtk_uart_apdma_slave_config;
  416. mtkd->ddev.device_pause = mtk_uart_apdma_device_pause;
  417. mtkd->ddev.device_terminate_all = mtk_uart_apdma_terminate_all;
  418. mtkd->ddev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
  419. mtkd->ddev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE);
  420. mtkd->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
  421. mtkd->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
  422. mtkd->ddev.dev = &pdev->dev;
  423. INIT_LIST_HEAD(&mtkd->ddev.channels);
  424. mtkd->dma_requests = MTK_UART_APDMA_NR_VCHANS;
  425. if (of_property_read_u32(np, "dma-requests", &mtkd->dma_requests)) {
  426. dev_info(&pdev->dev,
  427. "Using %u as missing dma-requests property\n",
  428. MTK_UART_APDMA_NR_VCHANS);
  429. }
  430. for (i = 0; i < mtkd->dma_requests; i++) {
  431. c = devm_kzalloc(mtkd->ddev.dev, sizeof(*c), GFP_KERNEL);
  432. if (!c) {
  433. rc = -ENODEV;
  434. goto err_no_dma;
  435. }
  436. c->base = devm_platform_ioremap_resource(pdev, i);
  437. if (IS_ERR(c->base)) {
  438. rc = PTR_ERR(c->base);
  439. goto err_no_dma;
  440. }
  441. c->vc.desc_free = mtk_uart_apdma_desc_free;
  442. vchan_init(&c->vc, &mtkd->ddev);
  443. rc = platform_get_irq(pdev, i);
  444. if (rc < 0)
  445. goto err_no_dma;
  446. c->irq = rc;
  447. }
  448. pm_runtime_enable(&pdev->dev);
  449. rc = dma_async_device_register(&mtkd->ddev);
  450. if (rc)
  451. goto rpm_disable;
  452. platform_set_drvdata(pdev, mtkd);
  453. /* Device-tree DMA controller registration */
  454. rc = of_dma_controller_register(np, of_dma_xlate_by_chan_id, mtkd);
  455. if (rc)
  456. goto dma_remove;
  457. return rc;
  458. dma_remove:
  459. dma_async_device_unregister(&mtkd->ddev);
  460. rpm_disable:
  461. pm_runtime_disable(&pdev->dev);
  462. err_no_dma:
  463. mtk_uart_apdma_free(mtkd);
  464. return rc;
  465. }
  466. static void mtk_uart_apdma_remove(struct platform_device *pdev)
  467. {
  468. struct mtk_uart_apdmadev *mtkd = platform_get_drvdata(pdev);
  469. of_dma_controller_free(pdev->dev.of_node);
  470. mtk_uart_apdma_free(mtkd);
  471. dma_async_device_unregister(&mtkd->ddev);
  472. pm_runtime_disable(&pdev->dev);
  473. }
  474. #ifdef CONFIG_PM_SLEEP
  475. static int mtk_uart_apdma_suspend(struct device *dev)
  476. {
  477. struct mtk_uart_apdmadev *mtkd = dev_get_drvdata(dev);
  478. if (!pm_runtime_suspended(dev))
  479. clk_disable_unprepare(mtkd->clk);
  480. return 0;
  481. }
  482. static int mtk_uart_apdma_resume(struct device *dev)
  483. {
  484. int ret;
  485. struct mtk_uart_apdmadev *mtkd = dev_get_drvdata(dev);
  486. if (!pm_runtime_suspended(dev)) {
  487. ret = clk_prepare_enable(mtkd->clk);
  488. if (ret)
  489. return ret;
  490. }
  491. return 0;
  492. }
  493. #endif /* CONFIG_PM_SLEEP */
  494. #ifdef CONFIG_PM
  495. static int mtk_uart_apdma_runtime_suspend(struct device *dev)
  496. {
  497. struct mtk_uart_apdmadev *mtkd = dev_get_drvdata(dev);
  498. clk_disable_unprepare(mtkd->clk);
  499. return 0;
  500. }
  501. static int mtk_uart_apdma_runtime_resume(struct device *dev)
  502. {
  503. struct mtk_uart_apdmadev *mtkd = dev_get_drvdata(dev);
  504. return clk_prepare_enable(mtkd->clk);
  505. }
  506. #endif /* CONFIG_PM */
  507. static const struct dev_pm_ops mtk_uart_apdma_pm_ops = {
  508. SET_SYSTEM_SLEEP_PM_OPS(mtk_uart_apdma_suspend, mtk_uart_apdma_resume)
  509. SET_RUNTIME_PM_OPS(mtk_uart_apdma_runtime_suspend,
  510. mtk_uart_apdma_runtime_resume, NULL)
  511. };
  512. static struct platform_driver mtk_uart_apdma_driver = {
  513. .probe = mtk_uart_apdma_probe,
  514. .remove_new = mtk_uart_apdma_remove,
  515. .driver = {
  516. .name = KBUILD_MODNAME,
  517. .pm = &mtk_uart_apdma_pm_ops,
  518. .of_match_table = of_match_ptr(mtk_uart_apdma_match),
  519. },
  520. };
  521. module_platform_driver(mtk_uart_apdma_driver);
  522. MODULE_DESCRIPTION("MediaTek UART APDMA Controller Driver");
  523. MODULE_AUTHOR("Long Cheng <long.cheng@mediatek.com>");
  524. MODULE_LICENSE("GPL v2");