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/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/platform_device.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/clk.h>
24 #include <linux/clk-provider.h>
26 #include <linux/of_platform.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/extcon.h>
29 #include <linux/reset.h>
30 #include <linux/usb.h>
31 #include <linux/usb/hcd.h>
36 #define DWC3_ROCKCHIP_AUTOSUSPEND_DELAY 500 /* ms */
38 struct dwc3_rockchip {
44 struct reset_control *otg_rst;
45 struct extcon_dev *edev;
46 struct notifier_block device_nb;
47 struct notifier_block host_nb;
48 struct work_struct otg_work;
51 static int dwc3_rockchip_device_notifier(struct notifier_block *nb,
52 unsigned long event, void *ptr)
54 struct dwc3_rockchip *rockchip =
55 container_of(nb, struct dwc3_rockchip, device_nb);
57 schedule_work(&rockchip->otg_work);
62 static int dwc3_rockchip_host_notifier(struct notifier_block *nb,
63 unsigned long event, void *ptr)
65 struct dwc3_rockchip *rockchip =
66 container_of(nb, struct dwc3_rockchip, host_nb);
68 schedule_work(&rockchip->otg_work);
73 static void dwc3_rockchip_otg_extcon_evt_work(struct work_struct *work)
75 struct dwc3_rockchip *rockchip =
76 container_of(work, struct dwc3_rockchip, otg_work);
77 struct dwc3 *dwc = rockchip->dwc;
78 struct extcon_dev *edev = rockchip->edev;
79 struct usb_hcd *hcd = dev_get_drvdata(&dwc->xhci->dev);
87 if (extcon_get_cable_state_(edev, EXTCON_USB) > 0) {
88 if (rockchip->connected)
92 * If dr_mode is host only, never to set
93 * the mode to the peripheral mode.
95 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_HOST))
99 * Assert otg reset can put the dwc in P2 state, it's
100 * necessary operation prior to phy power on. However,
101 * asserting the otg reset may affect dwc chip operation.
102 * The reset will clear all of the dwc controller registers.
103 * So we need to reinit the dwc controller after deassert
104 * the reset. We use pm runtime to initialize dwc controller.
105 * Also, there are no synchronization primitives, meaning
106 * the dwc3 core code could at least in theory access chip
107 * registers while the reset is asserted, with unknown impact.
109 reset_control_assert(rockchip->otg_rst);
110 usleep_range(1000, 1200);
111 reset_control_deassert(rockchip->otg_rst);
113 pm_runtime_get_sync(dwc->dev);
115 spin_lock_irqsave(&dwc->lock, flags);
116 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
117 spin_unlock_irqrestore(&dwc->lock, flags);
119 rockchip->connected = true;
120 dev_info(rockchip->dev, "USB peripheral connected\n");
121 } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
122 if (rockchip->connected)
126 * If dr_mode is device only, never to
127 * set the mode to the host mode.
129 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_PERIPHERAL))
133 * Assert otg reset can put the dwc in P2 state, it's
134 * necessary operation prior to phy power on. However,
135 * asserting the otg reset may affect dwc chip operation.
136 * The reset will clear all of the dwc controller registers.
137 * So we need to reinit the dwc controller after deassert
138 * the reset. We use pm runtime to initialize dwc controller.
139 * Also, there are no synchronization primitives, meaning
140 * the dwc3 core code could at least in theory access chip
141 * registers while the reset is asserted, with unknown impact.
143 reset_control_assert(rockchip->otg_rst);
144 usleep_range(1000, 1200);
145 reset_control_deassert(rockchip->otg_rst);
148 * Don't abort on errors. If powering on a phy fails,
149 * we still need to init dwc controller and add the
150 * HCDs to avoid a crash when unloading the driver.
152 ret = phy_power_on(dwc->usb2_generic_phy);
154 dev_err(dwc->dev, "Failed to power on usb2 phy\n");
156 ret = phy_power_on(dwc->usb3_generic_phy);
158 phy_power_off(dwc->usb2_generic_phy);
159 dev_err(dwc->dev, "Failed to power on usb3 phy\n");
162 pm_runtime_get_sync(dwc->dev);
164 spin_lock_irqsave(&dwc->lock, flags);
165 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
166 spin_unlock_irqrestore(&dwc->lock, flags);
169 * The following sleep helps to ensure that inserted USB3
170 * Ethernet devices are discovered if already inserted
173 usleep_range(10000, 11000);
175 if (hcd->state == HC_STATE_HALT) {
176 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
177 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
180 rockchip->connected = true;
181 dev_info(rockchip->dev, "USB HOST connected\n");
183 if (!rockchip->connected)
186 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
188 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
189 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
190 hcd = dev_get_drvdata(&dwc->xhci->dev);
192 if (hcd->state != HC_STATE_HALT) {
193 usb_remove_hcd(hcd->shared_hcd);
197 phy_power_off(dwc->usb2_generic_phy);
198 phy_power_off(dwc->usb3_generic_phy);
201 pm_runtime_put_sync(dwc->dev);
203 rockchip->connected = false;
204 dev_info(rockchip->dev, "USB unconnected\n");
208 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
211 struct device *dev = rockchip->dev;
212 struct extcon_dev *edev;
214 if (device_property_read_bool(dev, "extcon")) {
215 edev = extcon_get_edev_by_phandle(dev, 0);
217 if (PTR_ERR(edev) != -EPROBE_DEFER)
218 dev_err(dev, "couldn't get extcon device\n");
219 return PTR_ERR(edev);
222 INIT_WORK(&rockchip->otg_work,
223 dwc3_rockchip_otg_extcon_evt_work);
225 rockchip->device_nb.notifier_call =
226 dwc3_rockchip_device_notifier;
227 ret = extcon_register_notifier(edev, EXTCON_USB,
228 &rockchip->device_nb);
230 dev_err(dev, "failed to register notifier for USB\n");
234 rockchip->host_nb.notifier_call =
235 dwc3_rockchip_host_notifier;
236 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
239 dev_err(dev, "failed to register notifier for USB HOST\n");
240 extcon_unregister_notifier(edev, EXTCON_USB,
241 &rockchip->device_nb);
245 rockchip->edev = edev;
251 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
256 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
257 &rockchip->device_nb);
258 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
262 static int dwc3_rockchip_probe(struct platform_device *pdev)
264 struct dwc3_rockchip *rockchip;
265 struct device *dev = &pdev->dev;
266 struct device_node *np = dev->of_node, *child;
267 struct platform_device *child_pdev;
273 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
278 count = of_clk_get_parent_count(np);
282 rockchip->num_clocks = count;
284 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
285 sizeof(struct clk *), GFP_KERNEL);
289 platform_set_drvdata(pdev, rockchip);
293 for (i = 0; i < rockchip->num_clocks; i++) {
296 clk = of_clk_get(np, i);
302 ret = clk_prepare_enable(clk);
308 rockchip->clks[i] = clk;
311 pm_runtime_set_active(dev);
312 pm_runtime_enable(dev);
313 ret = pm_runtime_get_sync(dev);
315 dev_err(dev, "get_sync failed with err %d\n", ret);
319 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
320 if (IS_ERR(rockchip->otg_rst)) {
321 dev_err(dev, "could not get reset controller\n");
322 ret = PTR_ERR(rockchip->otg_rst);
326 child = of_get_child_by_name(np, "dwc3");
328 dev_err(dev, "failed to find dwc3 core node\n");
333 /* Allocate and initialize the core */
334 ret = of_platform_populate(np, NULL, NULL, dev);
336 dev_err(dev, "failed to create dwc3 core\n");
340 child_pdev = of_find_device_by_node(child);
342 dev_err(dev, "failed to find dwc3 core device\n");
347 rockchip->dwc = platform_get_drvdata(child_pdev);
348 if (!rockchip->dwc) {
349 dev_err(dev, "failed to get drvdata dwc3\n");
354 ret = dwc3_rockchip_extcon_register(rockchip);
358 if (rockchip->edev) {
359 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
360 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
361 pm_runtime_allow(&child_pdev->dev);
363 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
364 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
365 struct usb_hcd *hcd =
366 dev_get_drvdata(&rockchip->dwc->xhci->dev);
368 dev_err(dev, "fail to get drvdata hcd\n");
372 if (hcd->state != HC_STATE_HALT) {
373 usb_remove_hcd(hcd->shared_hcd);
378 pm_runtime_put_sync(dev);
380 if ((extcon_get_cable_state_(rockchip->edev,
382 (extcon_get_cable_state_(rockchip->edev,
383 EXTCON_USB_HOST) > 0))
384 schedule_work(&rockchip->otg_work);
390 dwc3_rockchip_extcon_unregister(rockchip);
393 of_platform_depopulate(dev);
396 pm_runtime_put_sync(dev);
397 pm_runtime_disable(dev);
400 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
401 if (!pm_runtime_status_suspended(dev))
402 clk_disable(rockchip->clks[i]);
403 clk_unprepare(rockchip->clks[i]);
404 clk_put(rockchip->clks[i]);
410 static int dwc3_rockchip_remove(struct platform_device *pdev)
412 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
413 struct device *dev = &pdev->dev;
416 dwc3_rockchip_extcon_unregister(rockchip);
418 of_platform_depopulate(dev);
421 pm_runtime_put_sync(dev);
423 pm_runtime_disable(dev);
424 pm_runtime_set_suspended(dev);
426 for (i = 0; i < rockchip->num_clocks; i++) {
427 if (!pm_runtime_status_suspended(dev))
428 clk_disable(rockchip->clks[i]);
429 clk_unprepare(rockchip->clks[i]);
430 clk_put(rockchip->clks[i]);
437 static int dwc3_rockchip_runtime_suspend(struct device *dev)
439 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
442 for (i = 0; i < rockchip->num_clocks; i++)
443 clk_disable(rockchip->clks[i]);
448 static int dwc3_rockchip_runtime_resume(struct device *dev)
450 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
453 for (i = 0; i < rockchip->num_clocks; i++)
454 clk_enable(rockchip->clks[i]);
459 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
460 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
461 dwc3_rockchip_runtime_resume, NULL)
464 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
466 #define DEV_PM_OPS NULL
467 #endif /* CONFIG_PM */
469 static const struct of_device_id rockchip_dwc3_match[] = {
470 { .compatible = "rockchip,rk3399-dwc3" },
474 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
476 static struct platform_driver dwc3_rockchip_driver = {
477 .probe = dwc3_rockchip_probe,
478 .remove = dwc3_rockchip_remove,
480 .name = "rockchip-dwc3",
481 .of_match_table = rockchip_dwc3_match,
486 module_platform_driver(dwc3_rockchip_driver);
488 MODULE_ALIAS("platform:rockchip-dwc3");
489 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
490 MODULE_LICENSE("GPL v2");
491 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");