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