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(rockchip->dev);
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 (dwc->dr_mode == USB_DR_MODE_PERIPHERAL) {
137 dev_warn(rockchip->dev, "USB HOST not support!\n");
142 * Assert otg reset can put the dwc in P2 state, it's
143 * necessary operation prior to phy power on. However,
144 * asserting the otg reset may affect dwc chip operation.
145 * The reset will clear all of the dwc controller registers.
146 * So we need to reinit the dwc controller after deassert
147 * the reset. We use pm runtime to initialize dwc controller.
148 * Also, there are no synchronization primitives, meaning
149 * the dwc3 core code could at least in theory access chip
150 * registers while the reset is asserted, with unknown impact.
152 reset_control_assert(rockchip->otg_rst);
153 usleep_range(1000, 1200);
154 reset_control_deassert(rockchip->otg_rst);
157 * In usb3 phy init, it will access usb3 module, so we need
158 * to resume rockchip dev before phy init to make sure usb3
161 pm_runtime_get_sync(rockchip->dev);
164 * Don't abort on errors. If powering on a phy fails,
165 * we still need to init dwc controller and add the
166 * HCDs to avoid a crash when unloading the driver.
168 ret = phy_power_on(dwc->usb2_generic_phy);
170 dev_err(dwc->dev, "Failed to power on usb2 phy\n");
172 ret = phy_power_on(dwc->usb3_generic_phy);
174 phy_power_off(dwc->usb2_generic_phy);
175 dev_err(dwc->dev, "Failed to power on usb3 phy\n");
178 pm_runtime_get_sync(dwc->dev);
180 spin_lock_irqsave(&dwc->lock, flags);
181 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
182 spin_unlock_irqrestore(&dwc->lock, flags);
185 * The following sleep helps to ensure that inserted USB3
186 * Ethernet devices are discovered if already inserted
189 usleep_range(10000, 11000);
191 hcd = dev_get_drvdata(&dwc->xhci->dev);
193 if (hcd->state == HC_STATE_HALT) {
194 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
195 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
198 rockchip->connected = true;
199 dev_info(rockchip->dev, "USB HOST connected\n");
201 if (!rockchip->connected)
204 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
207 * xhci does not support runtime pm. If HCDs are not removed
208 * here and and re-added after a cable is inserted, USB3
209 * connections will not work.
210 * A clean(er) solution would be to implement runtime pm
211 * support in xhci. After that is available, this code should
213 * HCDs have to be removed here to prevent attempts by the
214 * xhci code to access xhci registers after the call to
215 * pm_runtime_put_sync_suspend(). On rk3399, this can result
216 * in a crash under certain circumstances (this was observed
217 * on 3399 chromebook if the system is running on battery).
219 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
220 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
221 hcd = dev_get_drvdata(&dwc->xhci->dev);
223 if (hcd->state != HC_STATE_HALT) {
224 usb_remove_hcd(hcd->shared_hcd);
228 phy_power_off(dwc->usb2_generic_phy);
229 phy_power_off(dwc->usb3_generic_phy);
232 pm_runtime_put_sync(rockchip->dev);
233 pm_runtime_put_sync_suspend(dwc->dev);
235 rockchip->connected = false;
236 dev_info(rockchip->dev, "USB unconnected\n");
240 mutex_unlock(&rockchip->lock);
243 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
246 struct device *dev = rockchip->dev;
247 struct extcon_dev *edev;
249 if (device_property_read_bool(dev, "extcon")) {
250 edev = extcon_get_edev_by_phandle(dev, 0);
252 if (PTR_ERR(edev) != -EPROBE_DEFER)
253 dev_err(dev, "couldn't get extcon device\n");
254 return PTR_ERR(edev);
257 INIT_WORK(&rockchip->otg_work,
258 dwc3_rockchip_otg_extcon_evt_work);
260 rockchip->device_nb.notifier_call =
261 dwc3_rockchip_device_notifier;
262 ret = extcon_register_notifier(edev, EXTCON_USB,
263 &rockchip->device_nb);
265 dev_err(dev, "failed to register notifier for USB\n");
269 rockchip->host_nb.notifier_call =
270 dwc3_rockchip_host_notifier;
271 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
274 dev_err(dev, "failed to register notifier for USB HOST\n");
275 extcon_unregister_notifier(edev, EXTCON_USB,
276 &rockchip->device_nb);
280 rockchip->edev = edev;
286 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
291 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
292 &rockchip->device_nb);
293 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
295 cancel_work_sync(&rockchip->otg_work);
298 static int dwc3_rockchip_probe(struct platform_device *pdev)
300 struct dwc3_rockchip *rockchip;
301 struct device *dev = &pdev->dev;
302 struct device_node *np = dev->of_node, *child;
303 struct platform_device *child_pdev;
309 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
314 count = of_clk_get_parent_count(np);
318 rockchip->num_clocks = count;
320 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
321 sizeof(struct clk *), GFP_KERNEL);
325 platform_set_drvdata(pdev, rockchip);
327 mutex_init(&rockchip->lock);
331 mutex_lock(&rockchip->lock);
333 for (i = 0; i < rockchip->num_clocks; i++) {
336 clk = of_clk_get(np, i);
342 ret = clk_prepare_enable(clk);
348 rockchip->clks[i] = clk;
351 pm_runtime_set_active(dev);
352 pm_runtime_enable(dev);
353 ret = pm_runtime_get_sync(dev);
355 dev_err(dev, "get_sync failed with err %d\n", ret);
359 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
360 if (IS_ERR(rockchip->otg_rst)) {
361 dev_err(dev, "could not get reset controller\n");
362 ret = PTR_ERR(rockchip->otg_rst);
366 child = of_get_child_by_name(np, "dwc3");
368 dev_err(dev, "failed to find dwc3 core node\n");
373 /* Allocate and initialize the core */
374 ret = of_platform_populate(np, NULL, NULL, dev);
376 dev_err(dev, "failed to create dwc3 core\n");
380 child_pdev = of_find_device_by_node(child);
382 dev_err(dev, "failed to find dwc3 core device\n");
387 rockchip->dwc = platform_get_drvdata(child_pdev);
388 if (!rockchip->dwc) {
389 dev_err(dev, "failed to get drvdata dwc3\n");
394 ret = dwc3_rockchip_extcon_register(rockchip);
398 if (rockchip->edev) {
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_set_autosuspend_delay(&child_pdev->dev,
415 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
416 pm_runtime_allow(&child_pdev->dev);
417 pm_runtime_suspend(&child_pdev->dev);
418 pm_runtime_put_sync(dev);
420 if ((extcon_get_cable_state_(rockchip->edev,
422 (extcon_get_cable_state_(rockchip->edev,
423 EXTCON_USB_HOST) > 0))
424 schedule_work(&rockchip->otg_work);
427 mutex_unlock(&rockchip->lock);
432 dwc3_rockchip_extcon_unregister(rockchip);
435 of_platform_depopulate(dev);
438 pm_runtime_put_sync(dev);
439 pm_runtime_disable(dev);
442 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
443 if (!pm_runtime_status_suspended(dev))
444 clk_disable(rockchip->clks[i]);
445 clk_unprepare(rockchip->clks[i]);
446 clk_put(rockchip->clks[i]);
449 mutex_unlock(&rockchip->lock);
454 static int dwc3_rockchip_remove(struct platform_device *pdev)
456 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
457 struct device *dev = &pdev->dev;
460 dwc3_rockchip_extcon_unregister(rockchip);
462 /* Restore hcd state before unregistering xhci */
463 if (rockchip->edev && !rockchip->connected) {
464 struct usb_hcd *hcd =
465 dev_get_drvdata(&rockchip->dwc->xhci->dev);
467 pm_runtime_get_sync(dev);
470 * The xhci code does not expect that HCDs have been removed.
471 * It will unconditionally call usb_remove_hcd() when the xhci
472 * driver is unloaded in of_platform_depopulate(). This results
473 * in a crash if the HCDs were already removed. To avoid this
474 * crash, add the HCDs here as dummy operation.
475 * This code should be removed after pm runtime support
476 * has been added to xhci.
478 if (hcd->state == HC_STATE_HALT) {
479 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
480 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
484 of_platform_depopulate(dev);
486 pm_runtime_put_sync(dev);
487 pm_runtime_disable(dev);
489 for (i = 0; i < rockchip->num_clocks; i++) {
490 if (!pm_runtime_status_suspended(dev))
491 clk_disable(rockchip->clks[i]);
492 clk_unprepare(rockchip->clks[i]);
493 clk_put(rockchip->clks[i]);
500 static int dwc3_rockchip_runtime_suspend(struct device *dev)
502 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
505 for (i = 0; i < rockchip->num_clocks; i++)
506 clk_disable(rockchip->clks[i]);
508 device_init_wakeup(dev, false);
513 static int dwc3_rockchip_runtime_resume(struct device *dev)
515 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
518 for (i = 0; i < rockchip->num_clocks; i++)
519 clk_enable(rockchip->clks[i]);
521 device_init_wakeup(dev, true);
526 static int dwc3_rockchip_suspend(struct device *dev)
528 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
530 rockchip->suspended = true;
531 cancel_work_sync(&rockchip->otg_work);
536 static int dwc3_rockchip_resume(struct device *dev)
538 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
540 rockchip->suspended = false;
545 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
546 SET_SYSTEM_SLEEP_PM_OPS(dwc3_rockchip_suspend, dwc3_rockchip_resume)
547 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
548 dwc3_rockchip_runtime_resume, NULL)
551 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
553 #define DEV_PM_OPS NULL
554 #endif /* CONFIG_PM */
556 static const struct of_device_id rockchip_dwc3_match[] = {
557 { .compatible = "rockchip,rk3399-dwc3" },
561 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
563 static struct platform_driver dwc3_rockchip_driver = {
564 .probe = dwc3_rockchip_probe,
565 .remove = dwc3_rockchip_remove,
567 .name = "rockchip-dwc3",
568 .of_match_table = rockchip_dwc3_match,
573 module_platform_driver(dwc3_rockchip_driver);
575 MODULE_ALIAS("platform:rockchip-dwc3");
576 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
577 MODULE_LICENSE("GPL v2");
578 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");