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>
37 #define DWC3_ROCKCHIP_AUTOSUSPEND_DELAY 500 /* ms */
39 struct dwc3_rockchip {
46 struct reset_control *otg_rst;
47 struct extcon_dev *edev;
48 struct notifier_block device_nb;
49 struct notifier_block host_nb;
50 struct work_struct otg_work;
54 static int dwc3_rockchip_device_notifier(struct notifier_block *nb,
55 unsigned long event, void *ptr)
57 struct dwc3_rockchip *rockchip =
58 container_of(nb, struct dwc3_rockchip, device_nb);
60 if (!rockchip->suspended)
61 schedule_work(&rockchip->otg_work);
66 static int dwc3_rockchip_host_notifier(struct notifier_block *nb,
67 unsigned long event, void *ptr)
69 struct dwc3_rockchip *rockchip =
70 container_of(nb, struct dwc3_rockchip, host_nb);
72 if (!rockchip->suspended)
73 schedule_work(&rockchip->otg_work);
78 static void dwc3_rockchip_otg_extcon_evt_work(struct work_struct *work)
80 struct dwc3_rockchip *rockchip =
81 container_of(work, struct dwc3_rockchip, otg_work);
82 struct dwc3 *dwc = rockchip->dwc;
83 struct extcon_dev *edev = rockchip->edev;
89 mutex_lock(&rockchip->lock);
91 if (extcon_get_cable_state_(edev, EXTCON_USB) > 0) {
92 if (rockchip->connected)
96 * If dr_mode is host only, never to set
97 * the mode to the peripheral mode.
99 if (dwc->dr_mode == USB_DR_MODE_HOST) {
100 dev_warn(rockchip->dev, "USB peripheral not support!\n");
105 * Assert otg reset can put the dwc in P2 state, it's
106 * necessary operation prior to phy power on. However,
107 * asserting the otg reset may affect dwc chip operation.
108 * The reset will clear all of the dwc controller registers.
109 * So we need to reinit the dwc controller after deassert
110 * the reset. We use pm runtime to initialize dwc controller.
111 * Also, there are no synchronization primitives, meaning
112 * the dwc3 core code could at least in theory access chip
113 * registers while the reset is asserted, with unknown impact.
115 reset_control_assert(rockchip->otg_rst);
116 usleep_range(1000, 1200);
117 reset_control_deassert(rockchip->otg_rst);
119 pm_runtime_get_sync(dwc->dev);
121 spin_lock_irqsave(&dwc->lock, flags);
122 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
123 spin_unlock_irqrestore(&dwc->lock, flags);
125 rockchip->connected = true;
126 dev_info(rockchip->dev, "USB peripheral connected\n");
127 } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
128 if (rockchip->connected)
132 * If dr_mode is device only, never to
133 * set the mode to the host mode.
135 if (dwc->dr_mode == USB_DR_MODE_PERIPHERAL) {
136 dev_warn(rockchip->dev, "USB HOST not support!\n");
141 * Assert otg reset can put the dwc in P2 state, it's
142 * necessary operation prior to phy power on. However,
143 * asserting the otg reset may affect dwc chip operation.
144 * The reset will clear all of the dwc controller registers.
145 * So we need to reinit the dwc controller after deassert
146 * the reset. We use pm runtime to initialize dwc controller.
147 * Also, there are no synchronization primitives, meaning
148 * the dwc3 core code could at least in theory access chip
149 * registers while the reset is asserted, with unknown impact.
151 reset_control_assert(rockchip->otg_rst);
152 usleep_range(1000, 1200);
153 reset_control_deassert(rockchip->otg_rst);
156 * Don't abort on errors. If powering on a phy fails,
157 * we still need to init dwc controller and add the
158 * HCDs to avoid a crash when unloading the driver.
160 ret = phy_power_on(dwc->usb2_generic_phy);
162 dev_err(dwc->dev, "Failed to power on usb2 phy\n");
164 ret = phy_power_on(dwc->usb3_generic_phy);
166 phy_power_off(dwc->usb2_generic_phy);
167 dev_err(dwc->dev, "Failed to power on usb3 phy\n");
170 pm_runtime_get_sync(dwc->dev);
172 spin_lock_irqsave(&dwc->lock, flags);
173 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
174 spin_unlock_irqrestore(&dwc->lock, flags);
177 * The following sleep helps to ensure that inserted USB3
178 * Ethernet devices are discovered if already inserted
181 usleep_range(10000, 11000);
183 hcd = dev_get_drvdata(&dwc->xhci->dev);
185 if (hcd->state == HC_STATE_HALT) {
186 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
187 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
190 rockchip->connected = true;
191 dev_info(rockchip->dev, "USB HOST connected\n");
193 if (!rockchip->connected)
196 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
199 * xhci does not support runtime pm. If HCDs are not removed
200 * here and and re-added after a cable is inserted, USB3
201 * connections will not work.
202 * A clean(er) solution would be to implement runtime pm
203 * support in xhci. After that is available, this code should
205 * HCDs have to be removed here to prevent attempts by the
206 * xhci code to access xhci registers after the call to
207 * pm_runtime_put_sync_suspend(). On rk3399, this can result
208 * in a crash under certain circumstances (this was observed
209 * on 3399 chromebook if the system is running on battery).
211 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
212 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
213 hcd = dev_get_drvdata(&dwc->xhci->dev);
215 if (hcd->state == HC_STATE_SUSPENDED) {
216 dev_dbg(rockchip->dev, "USB suspended\n");
220 if (hcd->state != HC_STATE_HALT) {
221 usb_remove_hcd(hcd->shared_hcd);
225 phy_power_off(dwc->usb2_generic_phy);
226 phy_power_off(dwc->usb3_generic_phy);
229 pm_runtime_put_sync(dwc->dev);
231 rockchip->connected = false;
232 dev_info(rockchip->dev, "USB unconnected\n");
236 mutex_unlock(&rockchip->lock);
239 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
242 struct device *dev = rockchip->dev;
243 struct extcon_dev *edev;
245 if (device_property_read_bool(dev, "extcon")) {
246 edev = extcon_get_edev_by_phandle(dev, 0);
248 if (PTR_ERR(edev) != -EPROBE_DEFER)
249 dev_err(dev, "couldn't get extcon device\n");
250 return PTR_ERR(edev);
253 INIT_WORK(&rockchip->otg_work,
254 dwc3_rockchip_otg_extcon_evt_work);
256 rockchip->device_nb.notifier_call =
257 dwc3_rockchip_device_notifier;
258 ret = extcon_register_notifier(edev, EXTCON_USB,
259 &rockchip->device_nb);
261 dev_err(dev, "failed to register notifier for USB\n");
265 rockchip->host_nb.notifier_call =
266 dwc3_rockchip_host_notifier;
267 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
270 dev_err(dev, "failed to register notifier for USB HOST\n");
271 extcon_unregister_notifier(edev, EXTCON_USB,
272 &rockchip->device_nb);
276 rockchip->edev = edev;
282 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
287 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
288 &rockchip->device_nb);
289 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
291 cancel_work_sync(&rockchip->otg_work);
294 static int dwc3_rockchip_probe(struct platform_device *pdev)
296 struct dwc3_rockchip *rockchip;
297 struct device *dev = &pdev->dev;
298 struct device_node *np = dev->of_node, *child;
299 struct platform_device *child_pdev;
305 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
310 count = of_clk_get_parent_count(np);
314 rockchip->num_clocks = count;
316 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
317 sizeof(struct clk *), GFP_KERNEL);
321 platform_set_drvdata(pdev, rockchip);
323 mutex_init(&rockchip->lock);
327 mutex_lock(&rockchip->lock);
329 for (i = 0; i < rockchip->num_clocks; i++) {
332 clk = of_clk_get(np, i);
338 ret = clk_prepare_enable(clk);
344 rockchip->clks[i] = clk;
347 pm_runtime_set_active(dev);
348 pm_runtime_enable(dev);
349 ret = pm_runtime_get_sync(dev);
351 dev_err(dev, "get_sync failed with err %d\n", ret);
355 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
356 if (IS_ERR(rockchip->otg_rst)) {
357 dev_err(dev, "could not get reset controller\n");
358 ret = PTR_ERR(rockchip->otg_rst);
362 child = of_get_child_by_name(np, "dwc3");
364 dev_err(dev, "failed to find dwc3 core node\n");
369 /* Allocate and initialize the core */
370 ret = of_platform_populate(np, NULL, NULL, dev);
372 dev_err(dev, "failed to create dwc3 core\n");
376 child_pdev = of_find_device_by_node(child);
378 dev_err(dev, "failed to find dwc3 core device\n");
383 rockchip->dwc = platform_get_drvdata(child_pdev);
384 if (!rockchip->dwc) {
385 dev_err(dev, "failed to get drvdata dwc3\n");
390 ret = dwc3_rockchip_extcon_register(rockchip);
394 if (rockchip->edev) {
395 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
396 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
397 pm_runtime_allow(&child_pdev->dev);
399 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
400 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
401 struct usb_hcd *hcd =
402 dev_get_drvdata(&rockchip->dwc->xhci->dev);
404 dev_err(dev, "fail to get drvdata hcd\n");
408 if (hcd->state != HC_STATE_HALT) {
409 usb_remove_hcd(hcd->shared_hcd);
414 pm_runtime_put_sync(dev);
416 if ((extcon_get_cable_state_(rockchip->edev,
418 (extcon_get_cable_state_(rockchip->edev,
419 EXTCON_USB_HOST) > 0))
420 schedule_work(&rockchip->otg_work);
423 mutex_unlock(&rockchip->lock);
428 dwc3_rockchip_extcon_unregister(rockchip);
431 of_platform_depopulate(dev);
434 pm_runtime_put_sync(dev);
435 pm_runtime_disable(dev);
438 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
439 if (!pm_runtime_status_suspended(dev))
440 clk_disable(rockchip->clks[i]);
441 clk_unprepare(rockchip->clks[i]);
442 clk_put(rockchip->clks[i]);
445 mutex_unlock(&rockchip->lock);
450 static int dwc3_rockchip_remove(struct platform_device *pdev)
452 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
453 struct device *dev = &pdev->dev;
456 dwc3_rockchip_extcon_unregister(rockchip);
458 /* Restore hcd state before unregistering xhci */
459 if (rockchip->edev && !rockchip->connected) {
460 struct usb_hcd *hcd =
461 dev_get_drvdata(&rockchip->dwc->xhci->dev);
463 pm_runtime_get_sync(dev);
466 * The xhci code does not expect that HCDs have been removed.
467 * It will unconditionally call usb_remove_hcd() when the xhci
468 * driver is unloaded in of_platform_depopulate(). This results
469 * in a crash if the HCDs were already removed. To avoid this
470 * crash, add the HCDs here as dummy operation.
471 * This code should be removed after pm runtime support
472 * has been added to xhci.
474 if (hcd->state == HC_STATE_HALT) {
475 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
476 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
480 of_platform_depopulate(dev);
482 pm_runtime_put_sync(dev);
483 pm_runtime_disable(dev);
485 for (i = 0; i < rockchip->num_clocks; i++) {
486 if (!pm_runtime_status_suspended(dev))
487 clk_disable(rockchip->clks[i]);
488 clk_unprepare(rockchip->clks[i]);
489 clk_put(rockchip->clks[i]);
496 static int dwc3_rockchip_runtime_suspend(struct device *dev)
498 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
501 for (i = 0; i < rockchip->num_clocks; i++)
502 clk_disable(rockchip->clks[i]);
507 static int dwc3_rockchip_runtime_resume(struct device *dev)
509 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
512 for (i = 0; i < rockchip->num_clocks; i++)
513 clk_enable(rockchip->clks[i]);
518 static int dwc3_rockchip_suspend(struct device *dev)
520 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
522 rockchip->suspended = true;
523 cancel_work_sync(&rockchip->otg_work);
528 static int dwc3_rockchip_resume(struct device *dev)
530 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
532 rockchip->suspended = false;
537 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
538 SET_SYSTEM_SLEEP_PM_OPS(dwc3_rockchip_suspend, dwc3_rockchip_resume)
539 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
540 dwc3_rockchip_runtime_resume, NULL)
543 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
545 #define DEV_PM_OPS NULL
546 #endif /* CONFIG_PM */
548 static const struct of_device_id rockchip_dwc3_match[] = {
549 { .compatible = "rockchip,rk3399-dwc3" },
553 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
555 static struct platform_driver dwc3_rockchip_driver = {
556 .probe = dwc3_rockchip_probe,
557 .remove = dwc3_rockchip_remove,
559 .name = "rockchip-dwc3",
560 .of_match_table = rockchip_dwc3_match,
565 module_platform_driver(dwc3_rockchip_driver);
567 MODULE_ALIAS("platform:rockchip-dwc3");
568 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
569 MODULE_LICENSE("GPL v2");
570 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");