d8a8d254d8fe8f2ddcfbc5e025dabbb2cce1ff6d
[linux-2.6-microblaze.git] / drivers / usb / host / xhci-mtk.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * MediaTek xHCI Host Controller Driver
4  *
5  * Copyright (c) 2015 MediaTek Inc.
6  * Author:
7  *  Chunfeng Yun <chunfeng.yun@mediatek.com>
8  */
9
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/iopoll.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/regmap.h>
20 #include <linux/regulator/consumer.h>
21
22 #include "xhci.h"
23 #include "xhci-mtk.h"
24
25 /* ip_pw_ctrl0 register */
26 #define CTRL0_IP_SW_RST BIT(0)
27
28 /* ip_pw_ctrl1 register */
29 #define CTRL1_IP_HOST_PDN       BIT(0)
30
31 /* ip_pw_ctrl2 register */
32 #define CTRL2_IP_DEV_PDN        BIT(0)
33
34 /* ip_pw_sts1 register */
35 #define STS1_IP_SLEEP_STS       BIT(30)
36 #define STS1_U3_MAC_RST BIT(16)
37 #define STS1_XHCI_RST           BIT(11)
38 #define STS1_SYS125_RST BIT(10)
39 #define STS1_REF_RST            BIT(8)
40 #define STS1_SYSPLL_STABLE      BIT(0)
41
42 /* ip_xhci_cap register */
43 #define CAP_U3_PORT_NUM(p)      ((p) & 0xff)
44 #define CAP_U2_PORT_NUM(p)      (((p) >> 8) & 0xff)
45
46 /* u3_ctrl_p register */
47 #define CTRL_U3_PORT_HOST_SEL   BIT(2)
48 #define CTRL_U3_PORT_PDN        BIT(1)
49 #define CTRL_U3_PORT_DIS        BIT(0)
50
51 /* u2_ctrl_p register */
52 #define CTRL_U2_PORT_HOST_SEL   BIT(2)
53 #define CTRL_U2_PORT_PDN        BIT(1)
54 #define CTRL_U2_PORT_DIS        BIT(0)
55
56 /* u2_phy_pll register */
57 #define CTRL_U2_FORCE_PLL_STB   BIT(28)
58
59 /* usb remote wakeup registers in syscon */
60
61 /* mt8173 etc */
62 #define PERI_WK_CTRL1   0x4
63 #define WC1_IS_C(x)     (((x) & 0xf) << 26)  /* cycle debounce */
64 #define WC1_IS_EN       BIT(25)
65 #define WC1_IS_P        BIT(6)  /* polarity for ip sleep */
66
67 /* mt8183 */
68 #define PERI_WK_CTRL0   0x0
69 #define WC0_IS_C(x)     ((u32)(((x) & 0xf) << 28))  /* cycle debounce */
70 #define WC0_IS_P        BIT(12) /* polarity */
71 #define WC0_IS_EN       BIT(6)
72
73 /* mt8192 */
74 #define WC0_SSUSB0_CDEN         BIT(6)
75 #define WC0_IS_SPM_EN           BIT(1)
76
77 /* mt2712 etc */
78 #define PERI_SSUSB_SPM_CTRL     0x0
79 #define SSC_IP_SLEEP_EN BIT(4)
80 #define SSC_SPM_INT_EN          BIT(1)
81
82 enum ssusb_uwk_vers {
83         SSUSB_UWK_V1 = 1,
84         SSUSB_UWK_V2,
85         SSUSB_UWK_V1_1 = 101,   /* specific revision 1.01 */
86         SSUSB_UWK_V1_2,         /* specific revision 1.2 */
87 };
88
89 static int xhci_mtk_host_enable(struct xhci_hcd_mtk *mtk)
90 {
91         struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
92         u32 value, check_val;
93         int u3_ports_disabled = 0;
94         int ret;
95         int i;
96
97         if (!mtk->has_ippc)
98                 return 0;
99
100         /* power on host ip */
101         value = readl(&ippc->ip_pw_ctr1);
102         value &= ~CTRL1_IP_HOST_PDN;
103         writel(value, &ippc->ip_pw_ctr1);
104
105         /* power on and enable u3 ports except skipped ones */
106         for (i = 0; i < mtk->num_u3_ports; i++) {
107                 if ((0x1 << i) & mtk->u3p_dis_msk) {
108                         u3_ports_disabled++;
109                         continue;
110                 }
111
112                 value = readl(&ippc->u3_ctrl_p[i]);
113                 value &= ~(CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS);
114                 value |= CTRL_U3_PORT_HOST_SEL;
115                 writel(value, &ippc->u3_ctrl_p[i]);
116         }
117
118         /* power on and enable all u2 ports */
119         for (i = 0; i < mtk->num_u2_ports; i++) {
120                 value = readl(&ippc->u2_ctrl_p[i]);
121                 value &= ~(CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS);
122                 value |= CTRL_U2_PORT_HOST_SEL;
123                 writel(value, &ippc->u2_ctrl_p[i]);
124         }
125
126         /*
127          * wait for clocks to be stable, and clock domains reset to
128          * be inactive after power on and enable ports
129          */
130         check_val = STS1_SYSPLL_STABLE | STS1_REF_RST |
131                         STS1_SYS125_RST | STS1_XHCI_RST;
132
133         if (mtk->num_u3_ports > u3_ports_disabled)
134                 check_val |= STS1_U3_MAC_RST;
135
136         ret = readl_poll_timeout(&ippc->ip_pw_sts1, value,
137                           (check_val == (value & check_val)), 100, 20000);
138         if (ret) {
139                 dev_err(mtk->dev, "clocks are not stable (0x%x)\n", value);
140                 return ret;
141         }
142
143         return 0;
144 }
145
146 static int xhci_mtk_host_disable(struct xhci_hcd_mtk *mtk)
147 {
148         struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
149         u32 value;
150         int ret;
151         int i;
152
153         if (!mtk->has_ippc)
154                 return 0;
155
156         /* power down u3 ports except skipped ones */
157         for (i = 0; i < mtk->num_u3_ports; i++) {
158                 if ((0x1 << i) & mtk->u3p_dis_msk)
159                         continue;
160
161                 value = readl(&ippc->u3_ctrl_p[i]);
162                 value |= CTRL_U3_PORT_PDN;
163                 writel(value, &ippc->u3_ctrl_p[i]);
164         }
165
166         /* power down all u2 ports */
167         for (i = 0; i < mtk->num_u2_ports; i++) {
168                 value = readl(&ippc->u2_ctrl_p[i]);
169                 value |= CTRL_U2_PORT_PDN;
170                 writel(value, &ippc->u2_ctrl_p[i]);
171         }
172
173         /* power down host ip */
174         value = readl(&ippc->ip_pw_ctr1);
175         value |= CTRL1_IP_HOST_PDN;
176         writel(value, &ippc->ip_pw_ctr1);
177
178         /* wait for host ip to sleep */
179         ret = readl_poll_timeout(&ippc->ip_pw_sts1, value,
180                           (value & STS1_IP_SLEEP_STS), 100, 100000);
181         if (ret) {
182                 dev_err(mtk->dev, "ip sleep failed!!!\n");
183                 return ret;
184         }
185         return 0;
186 }
187
188 static int xhci_mtk_ssusb_config(struct xhci_hcd_mtk *mtk)
189 {
190         struct mu3c_ippc_regs __iomem *ippc = mtk->ippc_regs;
191         u32 value;
192
193         if (!mtk->has_ippc)
194                 return 0;
195
196         /* reset whole ip */
197         value = readl(&ippc->ip_pw_ctr0);
198         value |= CTRL0_IP_SW_RST;
199         writel(value, &ippc->ip_pw_ctr0);
200         udelay(1);
201         value = readl(&ippc->ip_pw_ctr0);
202         value &= ~CTRL0_IP_SW_RST;
203         writel(value, &ippc->ip_pw_ctr0);
204
205         /*
206          * device ip is default power-on in fact
207          * power down device ip, otherwise ip-sleep will fail
208          */
209         value = readl(&ippc->ip_pw_ctr2);
210         value |= CTRL2_IP_DEV_PDN;
211         writel(value, &ippc->ip_pw_ctr2);
212
213         value = readl(&ippc->ip_xhci_cap);
214         mtk->num_u3_ports = CAP_U3_PORT_NUM(value);
215         mtk->num_u2_ports = CAP_U2_PORT_NUM(value);
216         dev_dbg(mtk->dev, "%s u2p:%d, u3p:%d\n", __func__,
217                         mtk->num_u2_ports, mtk->num_u3_ports);
218
219         return xhci_mtk_host_enable(mtk);
220 }
221
222 static int xhci_mtk_clks_get(struct xhci_hcd_mtk *mtk)
223 {
224         struct device *dev = mtk->dev;
225
226         mtk->sys_clk = devm_clk_get(dev, "sys_ck");
227         if (IS_ERR(mtk->sys_clk)) {
228                 dev_err(dev, "fail to get sys_ck\n");
229                 return PTR_ERR(mtk->sys_clk);
230         }
231
232         mtk->xhci_clk = devm_clk_get_optional(dev, "xhci_ck");
233         if (IS_ERR(mtk->xhci_clk))
234                 return PTR_ERR(mtk->xhci_clk);
235
236         mtk->ref_clk = devm_clk_get_optional(dev, "ref_ck");
237         if (IS_ERR(mtk->ref_clk))
238                 return PTR_ERR(mtk->ref_clk);
239
240         mtk->mcu_clk = devm_clk_get_optional(dev, "mcu_ck");
241         if (IS_ERR(mtk->mcu_clk))
242                 return PTR_ERR(mtk->mcu_clk);
243
244         mtk->dma_clk = devm_clk_get_optional(dev, "dma_ck");
245         return PTR_ERR_OR_ZERO(mtk->dma_clk);
246 }
247
248 static int xhci_mtk_clks_enable(struct xhci_hcd_mtk *mtk)
249 {
250         int ret;
251
252         ret = clk_prepare_enable(mtk->ref_clk);
253         if (ret) {
254                 dev_err(mtk->dev, "failed to enable ref_clk\n");
255                 goto ref_clk_err;
256         }
257
258         ret = clk_prepare_enable(mtk->sys_clk);
259         if (ret) {
260                 dev_err(mtk->dev, "failed to enable sys_clk\n");
261                 goto sys_clk_err;
262         }
263
264         ret = clk_prepare_enable(mtk->xhci_clk);
265         if (ret) {
266                 dev_err(mtk->dev, "failed to enable xhci_clk\n");
267                 goto xhci_clk_err;
268         }
269
270         ret = clk_prepare_enable(mtk->mcu_clk);
271         if (ret) {
272                 dev_err(mtk->dev, "failed to enable mcu_clk\n");
273                 goto mcu_clk_err;
274         }
275
276         ret = clk_prepare_enable(mtk->dma_clk);
277         if (ret) {
278                 dev_err(mtk->dev, "failed to enable dma_clk\n");
279                 goto dma_clk_err;
280         }
281
282         return 0;
283
284 dma_clk_err:
285         clk_disable_unprepare(mtk->mcu_clk);
286 mcu_clk_err:
287         clk_disable_unprepare(mtk->xhci_clk);
288 xhci_clk_err:
289         clk_disable_unprepare(mtk->sys_clk);
290 sys_clk_err:
291         clk_disable_unprepare(mtk->ref_clk);
292 ref_clk_err:
293         return ret;
294 }
295
296 static void xhci_mtk_clks_disable(struct xhci_hcd_mtk *mtk)
297 {
298         clk_disable_unprepare(mtk->dma_clk);
299         clk_disable_unprepare(mtk->mcu_clk);
300         clk_disable_unprepare(mtk->xhci_clk);
301         clk_disable_unprepare(mtk->sys_clk);
302         clk_disable_unprepare(mtk->ref_clk);
303 }
304
305 /* only clocks can be turn off for ip-sleep wakeup mode */
306 static void usb_wakeup_ip_sleep_set(struct xhci_hcd_mtk *mtk, bool enable)
307 {
308         u32 reg, msk, val;
309
310         switch (mtk->uwk_vers) {
311         case SSUSB_UWK_V1:
312                 reg = mtk->uwk_reg_base + PERI_WK_CTRL1;
313                 msk = WC1_IS_EN | WC1_IS_C(0xf) | WC1_IS_P;
314                 val = enable ? (WC1_IS_EN | WC1_IS_C(0x8)) : 0;
315                 break;
316         case SSUSB_UWK_V1_1:
317                 reg = mtk->uwk_reg_base + PERI_WK_CTRL0;
318                 msk = WC0_IS_EN | WC0_IS_C(0xf) | WC0_IS_P;
319                 val = enable ? (WC0_IS_EN | WC0_IS_C(0x8)) : 0;
320                 break;
321         case SSUSB_UWK_V1_2:
322                 reg = mtk->uwk_reg_base + PERI_WK_CTRL0;
323                 msk = WC0_SSUSB0_CDEN | WC0_IS_SPM_EN;
324                 val = enable ? msk : 0;
325                 break;
326         case SSUSB_UWK_V2:
327                 reg = mtk->uwk_reg_base + PERI_SSUSB_SPM_CTRL;
328                 msk = SSC_IP_SLEEP_EN | SSC_SPM_INT_EN;
329                 val = enable ? msk : 0;
330                 break;
331         default:
332                 return;
333         }
334         regmap_update_bits(mtk->uwk, reg, msk, val);
335 }
336
337 static int usb_wakeup_of_property_parse(struct xhci_hcd_mtk *mtk,
338                                 struct device_node *dn)
339 {
340         struct of_phandle_args args;
341         int ret;
342
343         /* Wakeup function is optional */
344         mtk->uwk_en = of_property_read_bool(dn, "wakeup-source");
345         if (!mtk->uwk_en)
346                 return 0;
347
348         ret = of_parse_phandle_with_fixed_args(dn,
349                                 "mediatek,syscon-wakeup", 2, 0, &args);
350         if (ret)
351                 return ret;
352
353         mtk->uwk_reg_base = args.args[0];
354         mtk->uwk_vers = args.args[1];
355         mtk->uwk = syscon_node_to_regmap(args.np);
356         of_node_put(args.np);
357         dev_info(mtk->dev, "uwk - reg:0x%x, version:%d\n",
358                         mtk->uwk_reg_base, mtk->uwk_vers);
359
360         return PTR_ERR_OR_ZERO(mtk->uwk);
361
362 }
363
364 static void usb_wakeup_set(struct xhci_hcd_mtk *mtk, bool enable)
365 {
366         if (mtk->uwk_en)
367                 usb_wakeup_ip_sleep_set(mtk, enable);
368 }
369
370 static int xhci_mtk_ldos_enable(struct xhci_hcd_mtk *mtk)
371 {
372         int ret;
373
374         ret = regulator_enable(mtk->vbus);
375         if (ret) {
376                 dev_err(mtk->dev, "failed to enable vbus\n");
377                 return ret;
378         }
379
380         ret = regulator_enable(mtk->vusb33);
381         if (ret) {
382                 dev_err(mtk->dev, "failed to enable vusb33\n");
383                 regulator_disable(mtk->vbus);
384                 return ret;
385         }
386         return 0;
387 }
388
389 static void xhci_mtk_ldos_disable(struct xhci_hcd_mtk *mtk)
390 {
391         regulator_disable(mtk->vbus);
392         regulator_disable(mtk->vusb33);
393 }
394
395 static void xhci_mtk_quirks(struct device *dev, struct xhci_hcd *xhci)
396 {
397         struct usb_hcd *hcd = xhci_to_hcd(xhci);
398         struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
399
400         /*
401          * As of now platform drivers don't provide MSI support so we ensure
402          * here that the generic code does not try to make a pci_dev from our
403          * dev struct in order to setup MSI
404          */
405         xhci->quirks |= XHCI_PLAT;
406         xhci->quirks |= XHCI_MTK_HOST;
407         /*
408          * MTK host controller gives a spurious successful event after a
409          * short transfer. Ignore it.
410          */
411         xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
412         if (mtk->lpm_support)
413                 xhci->quirks |= XHCI_LPM_SUPPORT;
414         if (mtk->u2_lpm_disable)
415                 xhci->quirks |= XHCI_HW_LPM_DISABLE;
416
417         /*
418          * MTK xHCI 0.96: PSA is 1 by default even if doesn't support stream,
419          * and it's 3 when support it.
420          */
421         if (xhci->hci_version < 0x100 && HCC_MAX_PSA(xhci->hcc_params) == 4)
422                 xhci->quirks |= XHCI_BROKEN_STREAMS;
423 }
424
425 /* called during probe() after chip reset completes */
426 static int xhci_mtk_setup(struct usb_hcd *hcd)
427 {
428         struct xhci_hcd_mtk *mtk = hcd_to_mtk(hcd);
429         int ret;
430
431         if (usb_hcd_is_primary_hcd(hcd)) {
432                 ret = xhci_mtk_ssusb_config(mtk);
433                 if (ret)
434                         return ret;
435         }
436
437         ret = xhci_gen_setup(hcd, xhci_mtk_quirks);
438         if (ret)
439                 return ret;
440
441         if (usb_hcd_is_primary_hcd(hcd)) {
442                 ret = xhci_mtk_sch_init(mtk);
443                 if (ret)
444                         return ret;
445         }
446
447         return ret;
448 }
449
450 static const struct xhci_driver_overrides xhci_mtk_overrides __initconst = {
451         .reset = xhci_mtk_setup,
452         .add_endpoint = xhci_mtk_add_ep,
453         .drop_endpoint = xhci_mtk_drop_ep,
454         .check_bandwidth = xhci_mtk_check_bandwidth,
455         .reset_bandwidth = xhci_mtk_reset_bandwidth,
456 };
457
458 static struct hc_driver __read_mostly xhci_mtk_hc_driver;
459
460 static int xhci_mtk_probe(struct platform_device *pdev)
461 {
462         struct device *dev = &pdev->dev;
463         struct device_node *node = dev->of_node;
464         struct xhci_hcd_mtk *mtk;
465         const struct hc_driver *driver;
466         struct xhci_hcd *xhci;
467         struct resource *res;
468         struct usb_hcd *hcd;
469         int ret = -ENODEV;
470         int irq;
471
472         if (usb_disabled())
473                 return -ENODEV;
474
475         driver = &xhci_mtk_hc_driver;
476         mtk = devm_kzalloc(dev, sizeof(*mtk), GFP_KERNEL);
477         if (!mtk)
478                 return -ENOMEM;
479
480         mtk->dev = dev;
481         mtk->vbus = devm_regulator_get(dev, "vbus");
482         if (IS_ERR(mtk->vbus)) {
483                 dev_err(dev, "fail to get vbus\n");
484                 return PTR_ERR(mtk->vbus);
485         }
486
487         mtk->vusb33 = devm_regulator_get(dev, "vusb33");
488         if (IS_ERR(mtk->vusb33)) {
489                 dev_err(dev, "fail to get vusb33\n");
490                 return PTR_ERR(mtk->vusb33);
491         }
492
493         ret = xhci_mtk_clks_get(mtk);
494         if (ret)
495                 return ret;
496
497         mtk->lpm_support = of_property_read_bool(node, "usb3-lpm-capable");
498         mtk->u2_lpm_disable = of_property_read_bool(node, "usb2-lpm-disable");
499         /* optional property, ignore the error if it does not exist */
500         of_property_read_u32(node, "mediatek,u3p-dis-msk",
501                              &mtk->u3p_dis_msk);
502
503         ret = usb_wakeup_of_property_parse(mtk, node);
504         if (ret) {
505                 dev_err(dev, "failed to parse uwk property\n");
506                 return ret;
507         }
508
509         pm_runtime_enable(dev);
510         pm_runtime_get_sync(dev);
511         device_enable_async_suspend(dev);
512
513         ret = xhci_mtk_ldos_enable(mtk);
514         if (ret)
515                 goto disable_pm;
516
517         ret = xhci_mtk_clks_enable(mtk);
518         if (ret)
519                 goto disable_ldos;
520
521         irq = platform_get_irq(pdev, 0);
522         if (irq < 0) {
523                 ret = irq;
524                 goto disable_clk;
525         }
526
527         hcd = usb_create_hcd(driver, dev, dev_name(dev));
528         if (!hcd) {
529                 ret = -ENOMEM;
530                 goto disable_clk;
531         }
532
533         /*
534          * USB 2.0 roothub is stored in the platform_device.
535          * Swap it with mtk HCD.
536          */
537         mtk->hcd = platform_get_drvdata(pdev);
538         platform_set_drvdata(pdev, mtk);
539
540         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mac");
541         hcd->regs = devm_ioremap_resource(dev, res);
542         if (IS_ERR(hcd->regs)) {
543                 ret = PTR_ERR(hcd->regs);
544                 goto put_usb2_hcd;
545         }
546         hcd->rsrc_start = res->start;
547         hcd->rsrc_len = resource_size(res);
548
549         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ippc");
550         if (res) {      /* ippc register is optional */
551                 mtk->ippc_regs = devm_ioremap_resource(dev, res);
552                 if (IS_ERR(mtk->ippc_regs)) {
553                         ret = PTR_ERR(mtk->ippc_regs);
554                         goto put_usb2_hcd;
555                 }
556                 mtk->has_ippc = true;
557         } else {
558                 mtk->has_ippc = false;
559         }
560
561         device_init_wakeup(dev, true);
562
563         xhci = hcd_to_xhci(hcd);
564         xhci->main_hcd = hcd;
565
566         /*
567          * imod_interval is the interrupt moderation value in nanoseconds.
568          * The increment interval is 8 times as much as that defined in
569          * the xHCI spec on MTK's controller.
570          */
571         xhci->imod_interval = 5000;
572         device_property_read_u32(dev, "imod-interval-ns", &xhci->imod_interval);
573
574         xhci->shared_hcd = usb_create_shared_hcd(driver, dev,
575                         dev_name(dev), hcd);
576         if (!xhci->shared_hcd) {
577                 ret = -ENOMEM;
578                 goto disable_device_wakeup;
579         }
580
581         ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
582         if (ret)
583                 goto put_usb3_hcd;
584
585         if (HCC_MAX_PSA(xhci->hcc_params) >= 4 &&
586             !(xhci->quirks & XHCI_BROKEN_STREAMS))
587                 xhci->shared_hcd->can_do_streams = 1;
588
589         ret = usb_add_hcd(xhci->shared_hcd, irq, IRQF_SHARED);
590         if (ret)
591                 goto dealloc_usb2_hcd;
592
593         return 0;
594
595 dealloc_usb2_hcd:
596         usb_remove_hcd(hcd);
597
598 put_usb3_hcd:
599         xhci_mtk_sch_exit(mtk);
600         usb_put_hcd(xhci->shared_hcd);
601
602 disable_device_wakeup:
603         device_init_wakeup(dev, false);
604
605 put_usb2_hcd:
606         usb_put_hcd(hcd);
607
608 disable_clk:
609         xhci_mtk_clks_disable(mtk);
610
611 disable_ldos:
612         xhci_mtk_ldos_disable(mtk);
613
614 disable_pm:
615         pm_runtime_put_sync(dev);
616         pm_runtime_disable(dev);
617         return ret;
618 }
619
620 static int xhci_mtk_remove(struct platform_device *dev)
621 {
622         struct xhci_hcd_mtk *mtk = platform_get_drvdata(dev);
623         struct usb_hcd  *hcd = mtk->hcd;
624         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
625         struct usb_hcd  *shared_hcd = xhci->shared_hcd;
626
627         pm_runtime_put_noidle(&dev->dev);
628         pm_runtime_disable(&dev->dev);
629
630         usb_remove_hcd(shared_hcd);
631         xhci->shared_hcd = NULL;
632         device_init_wakeup(&dev->dev, false);
633
634         usb_remove_hcd(hcd);
635         usb_put_hcd(shared_hcd);
636         usb_put_hcd(hcd);
637         xhci_mtk_sch_exit(mtk);
638         xhci_mtk_clks_disable(mtk);
639         xhci_mtk_ldos_disable(mtk);
640
641         return 0;
642 }
643
644 static int __maybe_unused xhci_mtk_suspend(struct device *dev)
645 {
646         struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
647         struct usb_hcd *hcd = mtk->hcd;
648         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
649         int ret;
650
651         xhci_dbg(xhci, "%s: stop port polling\n", __func__);
652         clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
653         del_timer_sync(&hcd->rh_timer);
654         clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
655         del_timer_sync(&xhci->shared_hcd->rh_timer);
656
657         ret = xhci_mtk_host_disable(mtk);
658         if (ret)
659                 goto restart_poll_rh;
660
661         xhci_mtk_clks_disable(mtk);
662         usb_wakeup_set(mtk, true);
663         return 0;
664
665 restart_poll_rh:
666         xhci_dbg(xhci, "%s: restart port polling\n", __func__);
667         set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
668         usb_hcd_poll_rh_status(xhci->shared_hcd);
669         set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
670         usb_hcd_poll_rh_status(hcd);
671         return ret;
672 }
673
674 static int __maybe_unused xhci_mtk_resume(struct device *dev)
675 {
676         struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
677         struct usb_hcd *hcd = mtk->hcd;
678         struct xhci_hcd *xhci = hcd_to_xhci(hcd);
679         int ret;
680
681         usb_wakeup_set(mtk, false);
682         ret = xhci_mtk_clks_enable(mtk);
683         if (ret)
684                 goto enable_wakeup;
685
686         ret = xhci_mtk_host_enable(mtk);
687         if (ret)
688                 goto disable_clks;
689
690         xhci_dbg(xhci, "%s: restart port polling\n", __func__);
691         set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
692         usb_hcd_poll_rh_status(xhci->shared_hcd);
693         set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
694         usb_hcd_poll_rh_status(hcd);
695         return 0;
696
697 disable_clks:
698         xhci_mtk_clks_disable(mtk);
699 enable_wakeup:
700         usb_wakeup_set(mtk, true);
701         return ret;
702 }
703
704 static const struct dev_pm_ops xhci_mtk_pm_ops = {
705         SET_SYSTEM_SLEEP_PM_OPS(xhci_mtk_suspend, xhci_mtk_resume)
706 };
707 #define DEV_PM_OPS IS_ENABLED(CONFIG_PM) ? &xhci_mtk_pm_ops : NULL
708
709 static const struct of_device_id mtk_xhci_of_match[] = {
710         { .compatible = "mediatek,mt8173-xhci"},
711         { .compatible = "mediatek,mtk-xhci"},
712         { },
713 };
714 MODULE_DEVICE_TABLE(of, mtk_xhci_of_match);
715
716 static struct platform_driver mtk_xhci_driver = {
717         .probe  = xhci_mtk_probe,
718         .remove = xhci_mtk_remove,
719         .driver = {
720                 .name = "xhci-mtk",
721                 .pm = DEV_PM_OPS,
722                 .of_match_table = mtk_xhci_of_match,
723         },
724 };
725
726 static int __init xhci_mtk_init(void)
727 {
728         xhci_init_driver(&xhci_mtk_hc_driver, &xhci_mtk_overrides);
729         return platform_driver_register(&mtk_xhci_driver);
730 }
731 module_init(xhci_mtk_init);
732
733 static void __exit xhci_mtk_exit(void)
734 {
735         platform_driver_unregister(&mtk_xhci_driver);
736 }
737 module_exit(xhci_mtk_exit);
738
739 MODULE_AUTHOR("Chunfeng Yun <chunfeng.yun@mediatek.com>");
740 MODULE_DESCRIPTION("MediaTek xHCI Host Controller Driver");
741 MODULE_LICENSE("GPL v2");