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;
84 struct usb_hcd *hcd = dev_get_drvdata(&dwc->xhci->dev);
92 mutex_lock(&rockchip->lock);
94 if (extcon_get_cable_state_(edev, EXTCON_USB) > 0) {
95 if (rockchip->connected)
99 * If dr_mode is host only, never to set
100 * the mode to the peripheral mode.
102 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_HOST))
106 * Assert otg reset can put the dwc in P2 state, it's
107 * necessary operation prior to phy power on. However,
108 * asserting the otg reset may affect dwc chip operation.
109 * The reset will clear all of the dwc controller registers.
110 * So we need to reinit the dwc controller after deassert
111 * the reset. We use pm runtime to initialize dwc controller.
112 * Also, there are no synchronization primitives, meaning
113 * the dwc3 core code could at least in theory access chip
114 * registers while the reset is asserted, with unknown impact.
116 reset_control_assert(rockchip->otg_rst);
117 usleep_range(1000, 1200);
118 reset_control_deassert(rockchip->otg_rst);
120 pm_runtime_get_sync(dwc->dev);
122 spin_lock_irqsave(&dwc->lock, flags);
123 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
124 spin_unlock_irqrestore(&dwc->lock, flags);
126 rockchip->connected = true;
127 dev_info(rockchip->dev, "USB peripheral connected\n");
128 } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
129 if (rockchip->connected)
133 * If dr_mode is device only, never to
134 * set the mode to the host mode.
136 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_PERIPHERAL))
140 * Assert otg reset can put the dwc in P2 state, it's
141 * necessary operation prior to phy power on. However,
142 * asserting the otg reset may affect dwc chip operation.
143 * The reset will clear all of the dwc controller registers.
144 * So we need to reinit the dwc controller after deassert
145 * the reset. We use pm runtime to initialize dwc controller.
146 * Also, there are no synchronization primitives, meaning
147 * the dwc3 core code could at least in theory access chip
148 * registers while the reset is asserted, with unknown impact.
150 reset_control_assert(rockchip->otg_rst);
151 usleep_range(1000, 1200);
152 reset_control_deassert(rockchip->otg_rst);
155 * Don't abort on errors. If powering on a phy fails,
156 * we still need to init dwc controller and add the
157 * HCDs to avoid a crash when unloading the driver.
159 ret = phy_power_on(dwc->usb2_generic_phy);
161 dev_err(dwc->dev, "Failed to power on usb2 phy\n");
163 ret = phy_power_on(dwc->usb3_generic_phy);
165 phy_power_off(dwc->usb2_generic_phy);
166 dev_err(dwc->dev, "Failed to power on usb3 phy\n");
169 pm_runtime_get_sync(dwc->dev);
171 spin_lock_irqsave(&dwc->lock, flags);
172 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
173 spin_unlock_irqrestore(&dwc->lock, flags);
176 * The following sleep helps to ensure that inserted USB3
177 * Ethernet devices are discovered if already inserted
180 usleep_range(10000, 11000);
182 if (hcd->state == HC_STATE_HALT) {
183 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
184 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
187 rockchip->connected = true;
188 dev_info(rockchip->dev, "USB HOST connected\n");
190 if (!rockchip->connected)
193 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
196 * xhci does not support runtime pm. If HCDs are not removed
197 * here and and re-added after a cable is inserted, USB3
198 * connections will not work.
199 * A clean(er) solution would be to implement runtime pm
200 * support in xhci. After that is available, this code should
202 * HCDs have to be removed here to prevent attempts by the
203 * xhci code to access xhci registers after the call to
204 * pm_runtime_put_sync_suspend(). On rk3399, this can result
205 * in a crash under certain circumstances (this was observed
206 * on 3399 chromebook if the system is running on battery).
208 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
209 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
210 hcd = dev_get_drvdata(&dwc->xhci->dev);
212 if (hcd->state != HC_STATE_HALT) {
213 usb_remove_hcd(hcd->shared_hcd);
217 phy_power_off(dwc->usb2_generic_phy);
218 phy_power_off(dwc->usb3_generic_phy);
221 pm_runtime_put_sync(dwc->dev);
223 rockchip->connected = false;
224 dev_info(rockchip->dev, "USB unconnected\n");
228 mutex_unlock(&rockchip->lock);
231 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
234 struct device *dev = rockchip->dev;
235 struct extcon_dev *edev;
237 if (device_property_read_bool(dev, "extcon")) {
238 edev = extcon_get_edev_by_phandle(dev, 0);
240 if (PTR_ERR(edev) != -EPROBE_DEFER)
241 dev_err(dev, "couldn't get extcon device\n");
242 return PTR_ERR(edev);
245 INIT_WORK(&rockchip->otg_work,
246 dwc3_rockchip_otg_extcon_evt_work);
248 rockchip->device_nb.notifier_call =
249 dwc3_rockchip_device_notifier;
250 ret = extcon_register_notifier(edev, EXTCON_USB,
251 &rockchip->device_nb);
253 dev_err(dev, "failed to register notifier for USB\n");
257 rockchip->host_nb.notifier_call =
258 dwc3_rockchip_host_notifier;
259 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
262 dev_err(dev, "failed to register notifier for USB HOST\n");
263 extcon_unregister_notifier(edev, EXTCON_USB,
264 &rockchip->device_nb);
268 rockchip->edev = edev;
274 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
279 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
280 &rockchip->device_nb);
281 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
283 cancel_work_sync(&rockchip->otg_work);
286 static int dwc3_rockchip_probe(struct platform_device *pdev)
288 struct dwc3_rockchip *rockchip;
289 struct device *dev = &pdev->dev;
290 struct device_node *np = dev->of_node, *child;
291 struct platform_device *child_pdev;
297 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
302 count = of_clk_get_parent_count(np);
306 rockchip->num_clocks = count;
308 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
309 sizeof(struct clk *), GFP_KERNEL);
313 platform_set_drvdata(pdev, rockchip);
315 mutex_init(&rockchip->lock);
319 mutex_lock(&rockchip->lock);
321 for (i = 0; i < rockchip->num_clocks; i++) {
324 clk = of_clk_get(np, i);
330 ret = clk_prepare_enable(clk);
336 rockchip->clks[i] = clk;
339 pm_runtime_set_active(dev);
340 pm_runtime_enable(dev);
341 ret = pm_runtime_get_sync(dev);
343 dev_err(dev, "get_sync failed with err %d\n", ret);
347 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
348 if (IS_ERR(rockchip->otg_rst)) {
349 dev_err(dev, "could not get reset controller\n");
350 ret = PTR_ERR(rockchip->otg_rst);
354 child = of_get_child_by_name(np, "dwc3");
356 dev_err(dev, "failed to find dwc3 core node\n");
361 /* Allocate and initialize the core */
362 ret = of_platform_populate(np, NULL, NULL, dev);
364 dev_err(dev, "failed to create dwc3 core\n");
368 child_pdev = of_find_device_by_node(child);
370 dev_err(dev, "failed to find dwc3 core device\n");
375 rockchip->dwc = platform_get_drvdata(child_pdev);
376 if (!rockchip->dwc) {
377 dev_err(dev, "failed to get drvdata dwc3\n");
382 ret = dwc3_rockchip_extcon_register(rockchip);
386 if (rockchip->edev) {
387 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
388 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
389 pm_runtime_allow(&child_pdev->dev);
391 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
392 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
393 struct usb_hcd *hcd =
394 dev_get_drvdata(&rockchip->dwc->xhci->dev);
396 dev_err(dev, "fail to get drvdata hcd\n");
400 if (hcd->state != HC_STATE_HALT) {
401 usb_remove_hcd(hcd->shared_hcd);
406 pm_runtime_put_sync(dev);
408 if ((extcon_get_cable_state_(rockchip->edev,
410 (extcon_get_cable_state_(rockchip->edev,
411 EXTCON_USB_HOST) > 0))
412 schedule_work(&rockchip->otg_work);
415 mutex_unlock(&rockchip->lock);
420 dwc3_rockchip_extcon_unregister(rockchip);
423 of_platform_depopulate(dev);
426 pm_runtime_put_sync(dev);
427 pm_runtime_disable(dev);
430 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
431 if (!pm_runtime_status_suspended(dev))
432 clk_disable(rockchip->clks[i]);
433 clk_unprepare(rockchip->clks[i]);
434 clk_put(rockchip->clks[i]);
437 mutex_unlock(&rockchip->lock);
442 static int dwc3_rockchip_remove(struct platform_device *pdev)
444 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
445 struct device *dev = &pdev->dev;
448 dwc3_rockchip_extcon_unregister(rockchip);
450 /* Restore hcd state before unregistering xhci */
451 if (rockchip->edev && !rockchip->connected) {
452 struct usb_hcd *hcd =
453 dev_get_drvdata(&rockchip->dwc->xhci->dev);
455 pm_runtime_get_sync(dev);
458 * The xhci code does not expect that HCDs have been removed.
459 * It will unconditionally call usb_remove_hcd() when the xhci
460 * driver is unloaded in of_platform_depopulate(). This results
461 * in a crash if the HCDs were already removed. To avoid this
462 * crash, add the HCDs here as dummy operation.
463 * This code should be removed after pm runtime support
464 * has been added to xhci.
466 if (hcd->state == HC_STATE_HALT) {
467 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
468 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
472 of_platform_depopulate(dev);
474 pm_runtime_put_sync(dev);
475 pm_runtime_disable(dev);
477 for (i = 0; i < rockchip->num_clocks; i++) {
478 if (!pm_runtime_status_suspended(dev))
479 clk_disable(rockchip->clks[i]);
480 clk_unprepare(rockchip->clks[i]);
481 clk_put(rockchip->clks[i]);
488 static int dwc3_rockchip_runtime_suspend(struct device *dev)
490 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
493 for (i = 0; i < rockchip->num_clocks; i++)
494 clk_disable(rockchip->clks[i]);
499 static int dwc3_rockchip_runtime_resume(struct device *dev)
501 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
504 for (i = 0; i < rockchip->num_clocks; i++)
505 clk_enable(rockchip->clks[i]);
510 static int dwc3_rockchip_suspend(struct device *dev)
512 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
514 rockchip->suspended = true;
515 cancel_work_sync(&rockchip->otg_work);
520 static int dwc3_rockchip_resume(struct device *dev)
522 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
524 rockchip->suspended = false;
529 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
530 SET_SYSTEM_SLEEP_PM_OPS(dwc3_rockchip_suspend, dwc3_rockchip_resume)
531 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
532 dwc3_rockchip_runtime_resume, NULL)
535 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
537 #define DEV_PM_OPS NULL
538 #endif /* CONFIG_PM */
540 static const struct of_device_id rockchip_dwc3_match[] = {
541 { .compatible = "rockchip,rk3399-dwc3" },
545 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
547 static struct platform_driver dwc3_rockchip_driver = {
548 .probe = dwc3_rockchip_probe,
549 .remove = dwc3_rockchip_remove,
551 .name = "rockchip-dwc3",
552 .of_match_table = rockchip_dwc3_match,
557 module_platform_driver(dwc3_rockchip_driver);
559 MODULE_ALIAS("platform:rockchip-dwc3");
560 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
561 MODULE_LICENSE("GPL v2");
562 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");