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;
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))
98 pm_runtime_get_sync(dwc->dev);
100 spin_lock_irqsave(&dwc->lock, flags);
101 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
102 spin_unlock_irqrestore(&dwc->lock, flags);
104 rockchip->connected = true;
105 dev_info(rockchip->dev, "USB peripheral connected\n");
106 } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
107 if (rockchip->connected)
111 * If dr_mode is device only, never to
112 * set the mode to the host mode.
114 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_PERIPHERAL))
117 reset_control_assert(rockchip->otg_rst);
119 ret = phy_power_on(dwc->usb2_generic_phy);
121 reset_control_deassert(rockchip->otg_rst);
125 ret = phy_power_on(dwc->usb3_generic_phy);
127 phy_power_off(dwc->usb2_generic_phy);
128 reset_control_deassert(rockchip->otg_rst);
132 reset_control_deassert(rockchip->otg_rst);
134 pm_runtime_get_sync(dwc->dev);
136 hcd = dev_get_drvdata(&dwc->xhci->dev);
138 spin_lock_irqsave(&dwc->lock, flags);
139 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
140 spin_unlock_irqrestore(&dwc->lock, flags);
142 if (hcd->state == HC_STATE_HALT) {
143 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
144 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
147 rockchip->connected = true;
148 dev_info(rockchip->dev, "USB HOST connected\n");
150 if (!rockchip->connected)
153 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
155 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
156 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
157 hcd = dev_get_drvdata(&dwc->xhci->dev);
159 if (hcd->state != HC_STATE_HALT) {
160 usb_remove_hcd(hcd->shared_hcd);
164 phy_power_off(dwc->usb2_generic_phy);
165 phy_power_off(dwc->usb3_generic_phy);
169 pm_runtime_put_sync(dwc->dev);
171 rockchip->connected = false;
172 dev_info(rockchip->dev, "USB unconnected\n");
176 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
179 struct device *dev = rockchip->dev;
180 struct extcon_dev *edev;
182 if (device_property_read_bool(dev, "extcon")) {
183 edev = extcon_get_edev_by_phandle(dev, 0);
185 if (PTR_ERR(edev) != -EPROBE_DEFER)
186 dev_err(dev, "couldn't get extcon device\n");
187 return PTR_ERR(edev);
190 INIT_WORK(&rockchip->otg_work,
191 dwc3_rockchip_otg_extcon_evt_work);
193 rockchip->device_nb.notifier_call =
194 dwc3_rockchip_device_notifier;
195 ret = extcon_register_notifier(edev, EXTCON_USB,
196 &rockchip->device_nb);
198 dev_err(dev, "failed to register notifier for USB\n");
202 rockchip->host_nb.notifier_call =
203 dwc3_rockchip_host_notifier;
204 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
207 dev_err(dev, "failed to register notifier for USB HOST\n");
208 extcon_unregister_notifier(edev, EXTCON_USB,
209 &rockchip->device_nb);
213 rockchip->edev = edev;
219 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
224 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
225 &rockchip->device_nb);
226 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
230 static int dwc3_rockchip_probe(struct platform_device *pdev)
232 struct dwc3_rockchip *rockchip;
233 struct device *dev = &pdev->dev;
234 struct device_node *np = dev->of_node, *child;
235 struct platform_device *child_pdev;
241 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
246 count = of_clk_get_parent_count(np);
250 rockchip->num_clocks = count;
252 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
253 sizeof(struct clk *), GFP_KERNEL);
257 platform_set_drvdata(pdev, rockchip);
261 for (i = 0; i < rockchip->num_clocks; i++) {
264 clk = of_clk_get(np, i);
270 ret = clk_prepare_enable(clk);
276 rockchip->clks[i] = clk;
279 pm_runtime_set_active(dev);
280 pm_runtime_enable(dev);
281 ret = pm_runtime_get_sync(dev);
283 dev_err(dev, "get_sync failed with err %d\n", ret);
287 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
288 if (IS_ERR(rockchip->otg_rst)) {
289 dev_err(dev, "could not get reset controller\n");
290 ret = PTR_ERR(rockchip->otg_rst);
294 child = of_get_child_by_name(np, "dwc3");
296 dev_err(dev, "failed to find dwc3 core node\n");
301 /* Allocate and initialize the core */
302 ret = of_platform_populate(np, NULL, NULL, dev);
304 dev_err(dev, "failed to create dwc3 core\n");
308 child_pdev = of_find_device_by_node(child);
310 dev_err(dev, "failed to find dwc3 core device\n");
315 rockchip->dwc = platform_get_drvdata(child_pdev);
316 if (!rockchip->dwc) {
317 dev_err(dev, "failed to get drvdata dwc3\n");
322 ret = dwc3_rockchip_extcon_register(rockchip);
326 if (rockchip->edev) {
327 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
328 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
329 pm_runtime_allow(&child_pdev->dev);
331 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
332 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
333 struct usb_hcd *hcd =
334 dev_get_drvdata(&rockchip->dwc->xhci->dev);
336 dev_err(dev, "fail to get drvdata hcd\n");
340 if (hcd->state != HC_STATE_HALT) {
341 usb_remove_hcd(hcd->shared_hcd);
346 pm_runtime_put_sync(dev);
348 if ((extcon_get_cable_state_(rockchip->edev,
350 (extcon_get_cable_state_(rockchip->edev,
351 EXTCON_USB_HOST) > 0))
352 schedule_work(&rockchip->otg_work);
358 of_platform_depopulate(dev);
361 pm_runtime_put_sync(dev);
362 pm_runtime_disable(dev);
365 for (i = 0; i < rockchip->num_clocks && rockchip->clks[i]; i++) {
366 if (!pm_runtime_status_suspended(dev))
367 clk_disable(rockchip->clks[i]);
368 clk_unprepare(rockchip->clks[i]);
369 clk_put(rockchip->clks[i]);
375 static int dwc3_rockchip_remove(struct platform_device *pdev)
377 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
378 struct device *dev = &pdev->dev;
381 dwc3_rockchip_extcon_unregister(rockchip);
383 of_platform_depopulate(dev);
386 pm_runtime_put_sync(dev);
388 pm_runtime_disable(dev);
389 pm_runtime_set_suspended(dev);
391 for (i = 0; i < rockchip->num_clocks; i++) {
392 if (!pm_runtime_status_suspended(dev))
393 clk_disable(rockchip->clks[i]);
394 clk_unprepare(rockchip->clks[i]);
395 clk_put(rockchip->clks[i]);
402 static int dwc3_rockchip_runtime_suspend(struct device *dev)
404 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
407 for (i = 0; i < rockchip->num_clocks; i++)
408 clk_disable(rockchip->clks[i]);
413 static int dwc3_rockchip_runtime_resume(struct device *dev)
415 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
418 for (i = 0; i < rockchip->num_clocks; i++)
419 clk_enable(rockchip->clks[i]);
424 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
425 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
426 dwc3_rockchip_runtime_resume, NULL)
429 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
431 #define DEV_PM_OPS NULL
432 #endif /* CONFIG_PM */
434 static const struct of_device_id rockchip_dwc3_match[] = {
435 { .compatible = "rockchip,rk3399-dwc3" },
439 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
441 static struct platform_driver dwc3_rockchip_driver = {
442 .probe = dwc3_rockchip_probe,
443 .remove = dwc3_rockchip_remove,
445 .name = "rockchip-dwc3",
446 .of_match_table = rockchip_dwc3_match,
451 module_platform_driver(dwc3_rockchip_driver);
453 MODULE_ALIAS("platform:rockchip-dwc3");
454 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
455 MODULE_LICENSE("GPL v2");
456 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");