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;
228 usb_remove_hcd(hcd->shared_hcd);
232 phy_power_off(dwc->usb2_generic_phy);
233 phy_power_off(dwc->usb3_generic_phy);
236 pm_runtime_put_sync(rockchip->dev);
237 pm_runtime_put_sync_suspend(dwc->dev);
239 rockchip->connected = false;
240 dev_info(rockchip->dev, "USB unconnected\n");
244 mutex_unlock(&rockchip->lock);
247 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
250 struct device *dev = rockchip->dev;
251 struct extcon_dev *edev;
253 if (device_property_read_bool(dev, "extcon")) {
254 edev = extcon_get_edev_by_phandle(dev, 0);
256 if (PTR_ERR(edev) != -EPROBE_DEFER)
257 dev_err(dev, "couldn't get extcon device\n");
258 return PTR_ERR(edev);
261 INIT_WORK(&rockchip->otg_work,
262 dwc3_rockchip_otg_extcon_evt_work);
264 rockchip->device_nb.notifier_call =
265 dwc3_rockchip_device_notifier;
266 ret = extcon_register_notifier(edev, EXTCON_USB,
267 &rockchip->device_nb);
269 dev_err(dev, "failed to register notifier for USB\n");
273 rockchip->host_nb.notifier_call =
274 dwc3_rockchip_host_notifier;
275 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
278 dev_err(dev, "failed to register notifier for USB HOST\n");
279 extcon_unregister_notifier(edev, EXTCON_USB,
280 &rockchip->device_nb);
284 rockchip->edev = edev;
290 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
295 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
296 &rockchip->device_nb);
297 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
299 cancel_work_sync(&rockchip->otg_work);
302 static int dwc3_rockchip_probe(struct platform_device *pdev)
304 struct dwc3_rockchip *rockchip;
305 struct device *dev = &pdev->dev;
306 struct device_node *np = dev->of_node, *child;
307 struct platform_device *child_pdev;
313 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
318 count = of_clk_get_parent_count(np);
322 rockchip->num_clocks = count;
324 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
325 sizeof(struct clk *), GFP_KERNEL);
329 platform_set_drvdata(pdev, rockchip);
331 mutex_init(&rockchip->lock);
335 mutex_lock(&rockchip->lock);
337 for (i = 0; i < rockchip->num_clocks; i++) {
340 clk = of_clk_get(np, i);
346 ret = clk_prepare_enable(clk);
352 rockchip->clks[i] = clk;
355 pm_runtime_set_active(dev);
356 pm_runtime_enable(dev);
357 ret = pm_runtime_get_sync(dev);
359 dev_err(dev, "get_sync failed with err %d\n", ret);
363 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
364 if (IS_ERR(rockchip->otg_rst)) {
365 dev_err(dev, "could not get reset controller\n");
366 ret = PTR_ERR(rockchip->otg_rst);
370 child = of_get_child_by_name(np, "dwc3");
372 dev_err(dev, "failed to find dwc3 core node\n");
377 /* Allocate and initialize the core */
378 ret = of_platform_populate(np, NULL, NULL, dev);
380 dev_err(dev, "failed to create dwc3 core\n");
384 child_pdev = of_find_device_by_node(child);
386 dev_err(dev, "failed to find dwc3 core device\n");
391 rockchip->dwc = platform_get_drvdata(child_pdev);
392 if (!rockchip->dwc) {
393 dev_err(dev, "failed to get drvdata dwc3\n");
398 ret = dwc3_rockchip_extcon_register(rockchip);
402 if (rockchip->edev) {
403 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
404 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
405 struct usb_hcd *hcd =
406 dev_get_drvdata(&rockchip->dwc->xhci->dev);
408 dev_err(dev, "fail to get drvdata hcd\n");
412 if (hcd->state != HC_STATE_HALT) {
413 usb_remove_hcd(hcd->shared_hcd);
418 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
419 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
420 pm_runtime_allow(&child_pdev->dev);
421 pm_runtime_suspend(&child_pdev->dev);
422 pm_runtime_put_sync(dev);
424 if ((extcon_get_cable_state_(rockchip->edev,
426 (extcon_get_cable_state_(rockchip->edev,
427 EXTCON_USB_HOST) > 0))
428 schedule_work(&rockchip->otg_work);
431 mutex_unlock(&rockchip->lock);
436 dwc3_rockchip_extcon_unregister(rockchip);
439 of_platform_depopulate(dev);
442 pm_runtime_put_sync(dev);
443 pm_runtime_disable(dev);
446 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
447 if (!pm_runtime_status_suspended(dev))
448 clk_disable(rockchip->clks[i]);
449 clk_unprepare(rockchip->clks[i]);
450 clk_put(rockchip->clks[i]);
453 mutex_unlock(&rockchip->lock);
458 static int dwc3_rockchip_remove(struct platform_device *pdev)
460 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
461 struct device *dev = &pdev->dev;
464 dwc3_rockchip_extcon_unregister(rockchip);
466 /* Restore hcd state before unregistering xhci */
467 if (rockchip->edev && !rockchip->connected) {
468 struct usb_hcd *hcd =
469 dev_get_drvdata(&rockchip->dwc->xhci->dev);
471 pm_runtime_get_sync(dev);
474 * The xhci code does not expect that HCDs have been removed.
475 * It will unconditionally call usb_remove_hcd() when the xhci
476 * driver is unloaded in of_platform_depopulate(). This results
477 * in a crash if the HCDs were already removed. To avoid this
478 * crash, add the HCDs here as dummy operation.
479 * This code should be removed after pm runtime support
480 * has been added to xhci.
482 if (hcd->state == HC_STATE_HALT) {
483 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
484 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
488 of_platform_depopulate(dev);
490 pm_runtime_put_sync(dev);
491 pm_runtime_disable(dev);
493 for (i = 0; i < rockchip->num_clocks; i++) {
494 if (!pm_runtime_status_suspended(dev))
495 clk_disable(rockchip->clks[i]);
496 clk_unprepare(rockchip->clks[i]);
497 clk_put(rockchip->clks[i]);
504 static int dwc3_rockchip_runtime_suspend(struct device *dev)
506 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
509 for (i = 0; i < rockchip->num_clocks; i++)
510 clk_disable(rockchip->clks[i]);
512 device_init_wakeup(dev, false);
517 static int dwc3_rockchip_runtime_resume(struct device *dev)
519 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
522 for (i = 0; i < rockchip->num_clocks; i++)
523 clk_enable(rockchip->clks[i]);
525 device_init_wakeup(dev, true);
530 static int dwc3_rockchip_suspend(struct device *dev)
532 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
534 rockchip->suspended = true;
535 cancel_work_sync(&rockchip->otg_work);
540 static int dwc3_rockchip_resume(struct device *dev)
542 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
544 rockchip->suspended = false;
549 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
550 SET_SYSTEM_SLEEP_PM_OPS(dwc3_rockchip_suspend, dwc3_rockchip_resume)
551 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
552 dwc3_rockchip_runtime_resume, NULL)
555 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
557 #define DEV_PM_OPS NULL
558 #endif /* CONFIG_PM */
560 static const struct of_device_id rockchip_dwc3_match[] = {
561 { .compatible = "rockchip,rk3399-dwc3" },
565 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
567 static struct platform_driver dwc3_rockchip_driver = {
568 .probe = dwc3_rockchip_probe,
569 .remove = dwc3_rockchip_remove,
571 .name = "rockchip-dwc3",
572 .of_match_table = rockchip_dwc3_match,
577 module_platform_driver(dwc3_rockchip_driver);
579 MODULE_ALIAS("platform:rockchip-dwc3");
580 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
581 MODULE_LICENSE("GPL v2");
582 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");