2 * dwc3-rockchip.c - Rockchip Specific Glue layer
4 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
6 * Authors: William Wu <william.wu@rock-chips.com>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 of
10 * the License as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/kernel.h>
21 #include <linux/slab.h>
22 #include <linux/platform_device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/clk.h>
25 #include <linux/clk-provider.h>
27 #include <linux/of_platform.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/extcon.h>
30 #include <linux/reset.h>
31 #include <linux/usb.h>
32 #include <linux/usb/hcd.h>
36 #include "../host/xhci.h"
38 #define DWC3_ROCKCHIP_AUTOSUSPEND_DELAY 500 /* ms */
40 struct dwc3_rockchip {
47 struct reset_control *otg_rst;
48 struct extcon_dev *edev;
49 struct notifier_block device_nb;
50 struct notifier_block host_nb;
51 struct work_struct otg_work;
55 static int dwc3_rockchip_device_notifier(struct notifier_block *nb,
56 unsigned long event, void *ptr)
58 struct dwc3_rockchip *rockchip =
59 container_of(nb, struct dwc3_rockchip, device_nb);
61 if (!rockchip->suspended)
62 schedule_work(&rockchip->otg_work);
67 static int dwc3_rockchip_host_notifier(struct notifier_block *nb,
68 unsigned long event, void *ptr)
70 struct dwc3_rockchip *rockchip =
71 container_of(nb, struct dwc3_rockchip, host_nb);
73 if (!rockchip->suspended)
74 schedule_work(&rockchip->otg_work);
79 static void dwc3_rockchip_otg_extcon_evt_work(struct work_struct *work)
81 struct dwc3_rockchip *rockchip =
82 container_of(work, struct dwc3_rockchip, otg_work);
83 struct dwc3 *dwc = rockchip->dwc;
84 struct extcon_dev *edev = rockchip->edev;
86 struct xhci_hcd *xhci;
91 mutex_lock(&rockchip->lock);
93 if (extcon_get_cable_state_(edev, EXTCON_USB) > 0) {
94 if (rockchip->connected)
98 * If dr_mode is host only, never to set
99 * the mode to the peripheral mode.
101 if (dwc->dr_mode == USB_DR_MODE_HOST) {
102 dev_warn(rockchip->dev, "USB peripheral not support!\n");
107 * Assert otg reset can put the dwc in P2 state, it's
108 * necessary operation prior to phy power on. However,
109 * asserting the otg reset may affect dwc chip operation.
110 * The reset will clear all of the dwc controller registers.
111 * So we need to reinit the dwc controller after deassert
112 * the reset. We use pm runtime to initialize dwc controller.
113 * Also, there are no synchronization primitives, meaning
114 * the dwc3 core code could at least in theory access chip
115 * registers while the reset is asserted, with unknown impact.
117 reset_control_assert(rockchip->otg_rst);
118 usleep_range(1000, 1200);
119 reset_control_deassert(rockchip->otg_rst);
121 pm_runtime_get_sync(rockchip->dev);
122 pm_runtime_get_sync(dwc->dev);
124 spin_lock_irqsave(&dwc->lock, flags);
125 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
126 spin_unlock_irqrestore(&dwc->lock, flags);
128 rockchip->connected = true;
129 dev_info(rockchip->dev, "USB peripheral connected\n");
130 } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
131 if (rockchip->connected)
135 * If dr_mode is device only, never to
136 * set the mode to the host mode.
138 if (dwc->dr_mode == USB_DR_MODE_PERIPHERAL) {
139 dev_warn(rockchip->dev, "USB HOST not support!\n");
144 * Assert otg reset can put the dwc in P2 state, it's
145 * necessary operation prior to phy power on. However,
146 * asserting the otg reset may affect dwc chip operation.
147 * The reset will clear all of the dwc controller registers.
148 * So we need to reinit the dwc controller after deassert
149 * the reset. We use pm runtime to initialize dwc controller.
150 * Also, there are no synchronization primitives, meaning
151 * the dwc3 core code could at least in theory access chip
152 * registers while the reset is asserted, with unknown impact.
154 reset_control_assert(rockchip->otg_rst);
155 usleep_range(1000, 1200);
156 reset_control_deassert(rockchip->otg_rst);
159 * In usb3 phy init, it will access usb3 module, so we need
160 * to resume rockchip dev before phy init to make sure usb3
163 pm_runtime_get_sync(rockchip->dev);
166 * Don't abort on errors. If powering on a phy fails,
167 * we still need to init dwc controller and add the
168 * HCDs to avoid a crash when unloading the driver.
170 ret = phy_power_on(dwc->usb2_generic_phy);
172 dev_err(dwc->dev, "Failed to power on usb2 phy\n");
174 ret = phy_power_on(dwc->usb3_generic_phy);
176 phy_power_off(dwc->usb2_generic_phy);
177 dev_err(dwc->dev, "Failed to power on usb3 phy\n");
180 pm_runtime_get_sync(dwc->dev);
182 spin_lock_irqsave(&dwc->lock, flags);
183 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
184 spin_unlock_irqrestore(&dwc->lock, flags);
187 * The following sleep helps to ensure that inserted USB3
188 * Ethernet devices are discovered if already inserted
191 usleep_range(10000, 11000);
193 hcd = dev_get_drvdata(&dwc->xhci->dev);
195 if (hcd->state == HC_STATE_HALT) {
196 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
197 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
200 rockchip->connected = true;
201 dev_info(rockchip->dev, "USB HOST connected\n");
203 if (!rockchip->connected)
206 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
209 * xhci does not support runtime pm. If HCDs are not removed
210 * here and and re-added after a cable is inserted, USB3
211 * connections will not work.
212 * A clean(er) solution would be to implement runtime pm
213 * support in xhci. After that is available, this code should
215 * HCDs have to be removed here to prevent attempts by the
216 * xhci code to access xhci registers after the call to
217 * pm_runtime_put_sync_suspend(). On rk3399, this can result
218 * in a crash under certain circumstances (this was observed
219 * on 3399 chromebook if the system is running on battery).
221 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
222 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
223 hcd = dev_get_drvdata(&dwc->xhci->dev);
224 xhci = hcd_to_xhci(hcd);
226 if (hcd->state != HC_STATE_HALT) {
227 xhci->xhc_state |= XHCI_STATE_REMOVING;
231 * Wait until XHCI controller resume from
232 * PM suspend, them we can remove hcd safely.
234 while (dwc->xhci->dev.power.is_suspended) {
236 dev_err(rockchip->dev,
237 "wait for XHCI resume 10s timeout!\n");
243 usb_remove_hcd(hcd->shared_hcd);
247 phy_power_off(dwc->usb2_generic_phy);
248 phy_power_off(dwc->usb3_generic_phy);
251 pm_runtime_put_sync(rockchip->dev);
252 pm_runtime_put_sync_suspend(dwc->dev);
254 rockchip->connected = false;
255 dev_info(rockchip->dev, "USB unconnected\n");
259 mutex_unlock(&rockchip->lock);
262 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
265 struct device *dev = rockchip->dev;
266 struct extcon_dev *edev;
268 if (device_property_read_bool(dev, "extcon")) {
269 edev = extcon_get_edev_by_phandle(dev, 0);
271 if (PTR_ERR(edev) != -EPROBE_DEFER)
272 dev_err(dev, "couldn't get extcon device\n");
273 return PTR_ERR(edev);
276 INIT_WORK(&rockchip->otg_work,
277 dwc3_rockchip_otg_extcon_evt_work);
279 rockchip->device_nb.notifier_call =
280 dwc3_rockchip_device_notifier;
281 ret = extcon_register_notifier(edev, EXTCON_USB,
282 &rockchip->device_nb);
284 dev_err(dev, "failed to register notifier for USB\n");
288 rockchip->host_nb.notifier_call =
289 dwc3_rockchip_host_notifier;
290 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
293 dev_err(dev, "failed to register notifier for USB HOST\n");
294 extcon_unregister_notifier(edev, EXTCON_USB,
295 &rockchip->device_nb);
299 rockchip->edev = edev;
305 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
310 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
311 &rockchip->device_nb);
312 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
314 cancel_work_sync(&rockchip->otg_work);
317 static int dwc3_rockchip_probe(struct platform_device *pdev)
319 struct dwc3_rockchip *rockchip;
320 struct device *dev = &pdev->dev;
321 struct device_node *np = dev->of_node, *child;
322 struct platform_device *child_pdev;
328 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
333 count = of_clk_get_parent_count(np);
337 rockchip->num_clocks = count;
339 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
340 sizeof(struct clk *), GFP_KERNEL);
344 platform_set_drvdata(pdev, rockchip);
346 mutex_init(&rockchip->lock);
350 mutex_lock(&rockchip->lock);
352 for (i = 0; i < rockchip->num_clocks; i++) {
355 clk = of_clk_get(np, i);
361 ret = clk_prepare_enable(clk);
367 rockchip->clks[i] = clk;
370 pm_runtime_set_active(dev);
371 pm_runtime_enable(dev);
372 ret = pm_runtime_get_sync(dev);
374 dev_err(dev, "get_sync failed with err %d\n", ret);
378 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
379 if (IS_ERR(rockchip->otg_rst)) {
380 dev_err(dev, "could not get reset controller\n");
381 ret = PTR_ERR(rockchip->otg_rst);
385 child = of_get_child_by_name(np, "dwc3");
387 dev_err(dev, "failed to find dwc3 core node\n");
392 /* Allocate and initialize the core */
393 ret = of_platform_populate(np, NULL, NULL, dev);
395 dev_err(dev, "failed to create dwc3 core\n");
399 child_pdev = of_find_device_by_node(child);
401 dev_err(dev, "failed to find dwc3 core device\n");
406 rockchip->dwc = platform_get_drvdata(child_pdev);
407 if (!rockchip->dwc) {
408 dev_err(dev, "failed to get drvdata dwc3\n");
413 ret = dwc3_rockchip_extcon_register(rockchip);
417 if (rockchip->edev) {
418 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
419 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
420 struct usb_hcd *hcd =
421 dev_get_drvdata(&rockchip->dwc->xhci->dev);
423 dev_err(dev, "fail to get drvdata hcd\n");
427 if (hcd->state != HC_STATE_HALT) {
428 usb_remove_hcd(hcd->shared_hcd);
433 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
434 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
435 pm_runtime_allow(&child_pdev->dev);
436 pm_runtime_suspend(&child_pdev->dev);
437 pm_runtime_put_sync(dev);
439 if ((extcon_get_cable_state_(rockchip->edev,
441 (extcon_get_cable_state_(rockchip->edev,
442 EXTCON_USB_HOST) > 0))
443 schedule_work(&rockchip->otg_work);
446 mutex_unlock(&rockchip->lock);
451 dwc3_rockchip_extcon_unregister(rockchip);
454 of_platform_depopulate(dev);
457 pm_runtime_put_sync(dev);
458 pm_runtime_disable(dev);
461 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
462 if (!pm_runtime_status_suspended(dev))
463 clk_disable(rockchip->clks[i]);
464 clk_unprepare(rockchip->clks[i]);
465 clk_put(rockchip->clks[i]);
468 mutex_unlock(&rockchip->lock);
473 static int dwc3_rockchip_remove(struct platform_device *pdev)
475 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
476 struct device *dev = &pdev->dev;
479 dwc3_rockchip_extcon_unregister(rockchip);
481 /* Restore hcd state before unregistering xhci */
482 if (rockchip->edev && !rockchip->connected) {
483 struct usb_hcd *hcd =
484 dev_get_drvdata(&rockchip->dwc->xhci->dev);
486 pm_runtime_get_sync(dev);
489 * The xhci code does not expect that HCDs have been removed.
490 * It will unconditionally call usb_remove_hcd() when the xhci
491 * driver is unloaded in of_platform_depopulate(). This results
492 * in a crash if the HCDs were already removed. To avoid this
493 * crash, add the HCDs here as dummy operation.
494 * This code should be removed after pm runtime support
495 * has been added to xhci.
497 if (hcd->state == HC_STATE_HALT) {
498 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
499 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
503 of_platform_depopulate(dev);
505 pm_runtime_put_sync(dev);
506 pm_runtime_disable(dev);
508 for (i = 0; i < rockchip->num_clocks; i++) {
509 if (!pm_runtime_status_suspended(dev))
510 clk_disable(rockchip->clks[i]);
511 clk_unprepare(rockchip->clks[i]);
512 clk_put(rockchip->clks[i]);
519 static int dwc3_rockchip_runtime_suspend(struct device *dev)
521 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
524 for (i = 0; i < rockchip->num_clocks; i++)
525 clk_disable(rockchip->clks[i]);
527 device_init_wakeup(dev, false);
532 static int dwc3_rockchip_runtime_resume(struct device *dev)
534 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
537 for (i = 0; i < rockchip->num_clocks; i++)
538 clk_enable(rockchip->clks[i]);
540 device_init_wakeup(dev, true);
545 static int dwc3_rockchip_suspend(struct device *dev)
547 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
549 rockchip->suspended = true;
550 cancel_work_sync(&rockchip->otg_work);
555 static int dwc3_rockchip_resume(struct device *dev)
557 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
559 rockchip->suspended = false;
562 schedule_work(&rockchip->otg_work);
567 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
568 SET_SYSTEM_SLEEP_PM_OPS(dwc3_rockchip_suspend, dwc3_rockchip_resume)
569 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
570 dwc3_rockchip_runtime_resume, NULL)
573 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
575 #define DEV_PM_OPS NULL
576 #endif /* CONFIG_PM */
578 static const struct of_device_id rockchip_dwc3_match[] = {
579 { .compatible = "rockchip,rk3399-dwc3" },
583 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
585 static struct platform_driver dwc3_rockchip_driver = {
586 .probe = dwc3_rockchip_probe,
587 .remove = dwc3_rockchip_remove,
589 .name = "rockchip-dwc3",
590 .of_match_table = rockchip_dwc3_match,
595 module_platform_driver(dwc3_rockchip_driver);
597 MODULE_ALIAS("platform:rockchip-dwc3");
598 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
599 MODULE_LICENSE("GPL v2");
600 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");