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 {
43 struct reset_control *otg_rst;
44 struct extcon_dev *edev;
45 struct notifier_block device_nb;
46 struct notifier_block host_nb;
47 struct work_struct otg_work;
50 static int dwc3_rockchip_device_notifier(struct notifier_block *nb,
51 unsigned long event, void *ptr)
53 struct dwc3_rockchip *rockchip =
54 container_of(nb, struct dwc3_rockchip, device_nb);
56 schedule_work(&rockchip->otg_work);
61 static int dwc3_rockchip_host_notifier(struct notifier_block *nb,
62 unsigned long event, void *ptr)
64 struct dwc3_rockchip *rockchip =
65 container_of(nb, struct dwc3_rockchip, host_nb);
67 schedule_work(&rockchip->otg_work);
72 static void dwc3_rockchip_otg_extcon_evt_work(struct work_struct *work)
74 struct dwc3_rockchip *rockchip =
75 container_of(work, struct dwc3_rockchip, otg_work);
76 struct dwc3 *dwc = rockchip->dwc;
77 struct extcon_dev *edev = rockchip->edev;
83 if (extcon_get_cable_state_(edev, EXTCON_USB) > 0) {
88 * If dr_mode is host only, never to set
89 * the mode to the peripheral mode.
91 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_HOST))
94 pm_runtime_get_sync(dwc->dev);
96 spin_lock_irqsave(&dwc->lock, flags);
97 dwc->connected = true;
98 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
99 spin_unlock_irqrestore(&dwc->lock, flags);
101 dev_info(rockchip->dev, "USB peripheral connected\n");
102 } else if (extcon_get_cable_state_(edev, EXTCON_USB_HOST) > 0) {
107 * If dr_mode is device only, never to
108 * set the mode to the host mode.
110 if (WARN_ON(dwc->dr_mode == USB_DR_MODE_PERIPHERAL))
113 reset_control_assert(rockchip->otg_rst);
115 ret = phy_power_on(dwc->usb2_generic_phy);
119 ret = phy_power_on(dwc->usb3_generic_phy);
123 reset_control_deassert(rockchip->otg_rst);
125 pm_runtime_get_sync(dwc->dev);
127 hcd = dev_get_drvdata(&dwc->xhci->dev);
129 spin_lock_irqsave(&dwc->lock, flags);
130 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
131 dwc->connected = true;
132 spin_unlock_irqrestore(&dwc->lock, flags);
134 if (hcd->state == HC_STATE_HALT) {
135 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
136 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
139 dev_info(rockchip->dev, "USB HOST connected\n");
144 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
146 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
147 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
148 hcd = dev_get_drvdata(&dwc->xhci->dev);
150 if (hcd->state != HC_STATE_HALT) {
151 usb_remove_hcd(hcd->shared_hcd);
155 phy_power_off(dwc->usb2_generic_phy);
156 phy_power_off(dwc->usb3_generic_phy);
160 spin_lock_irqsave(&dwc->lock, flags);
161 dwc->connected = false;
162 spin_unlock_irqrestore(&dwc->lock, flags);
164 pm_runtime_put_sync(dwc->dev);
166 dev_info(rockchip->dev, "USB unconnected\n");
170 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
173 struct device *dev = rockchip->dev;
174 struct extcon_dev *edev;
176 if (device_property_read_bool(dev, "extcon")) {
177 edev = extcon_get_edev_by_phandle(dev, 0);
179 if (PTR_ERR(edev) != -EPROBE_DEFER)
180 dev_err(dev, "couldn't get extcon device\n");
181 return PTR_ERR(edev);
184 INIT_WORK(&rockchip->otg_work,
185 dwc3_rockchip_otg_extcon_evt_work);
187 rockchip->device_nb.notifier_call =
188 dwc3_rockchip_device_notifier;
189 ret = extcon_register_notifier(edev, EXTCON_USB,
190 &rockchip->device_nb);
192 dev_err(dev, "failed to register notifier for USB\n");
196 rockchip->host_nb.notifier_call =
197 dwc3_rockchip_host_notifier;
198 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
201 dev_err(dev, "failed to register notifier for USB HOST\n");
202 extcon_unregister_notifier(edev, EXTCON_USB,
203 &rockchip->device_nb);
207 rockchip->edev = edev;
213 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
218 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
219 &rockchip->device_nb);
220 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
224 static int dwc3_rockchip_probe(struct platform_device *pdev)
226 struct dwc3_rockchip *rockchip;
227 struct device *dev = &pdev->dev;
228 struct device_node *np = dev->of_node, *child;
229 struct platform_device *child_pdev;
235 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
240 count = of_clk_get_parent_count(np);
244 rockchip->num_clocks = count;
246 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
247 sizeof(struct clk *), GFP_KERNEL);
251 platform_set_drvdata(pdev, rockchip);
254 rockchip->edev = NULL;
256 for (i = 0; i < rockchip->num_clocks; i++) {
259 clk = of_clk_get(np, i);
262 clk_put(rockchip->clks[i]);
266 ret = clk_prepare_enable(clk);
269 clk_disable_unprepare(rockchip->clks[i]);
270 clk_put(rockchip->clks[i]);
277 rockchip->clks[i] = clk;
280 pm_runtime_set_active(dev);
281 pm_runtime_enable(dev);
282 ret = pm_runtime_get_sync(dev);
284 dev_err(dev, "get_sync failed with err %d\n", ret);
288 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
289 if (IS_ERR(rockchip->otg_rst)) {
290 dev_err(dev, "could not get reset controller\n");
291 ret = PTR_ERR(rockchip->otg_rst);
295 ret = dwc3_rockchip_extcon_register(rockchip);
299 child = of_get_child_by_name(np, "dwc3");
301 dev_err(dev, "failed to find dwc3 core node\n");
306 /* Allocate and initialize the core */
307 ret = of_platform_populate(np, NULL, NULL, dev);
309 dev_err(dev, "failed to create dwc3 core\n");
313 child_pdev = of_find_device_by_node(child);
315 dev_err(dev, "failed to find dwc3 core device\n");
320 rockchip->dwc = platform_get_drvdata(child_pdev);
322 if (rockchip->edev) {
323 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
324 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
325 pm_runtime_allow(&child_pdev->dev);
327 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
328 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
329 struct usb_hcd *hcd =
330 dev_get_drvdata(&rockchip->dwc->xhci->dev);
332 if (hcd->state != HC_STATE_HALT) {
333 usb_remove_hcd(hcd->shared_hcd);
338 pm_runtime_put_sync(dev);
344 of_platform_depopulate(dev);
347 dwc3_rockchip_extcon_unregister(rockchip);
350 pm_runtime_put_sync(dev);
351 pm_runtime_disable(dev);
353 for (i = 0; i < rockchip->num_clocks; i++) {
354 clk_unprepare(rockchip->clks[i]);
355 clk_put(rockchip->clks[i]);
361 static int dwc3_rockchip_remove(struct platform_device *pdev)
363 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
364 struct device *dev = &pdev->dev;
367 dwc3_rockchip_extcon_unregister(rockchip);
369 of_platform_depopulate(dev);
371 pm_runtime_put_sync(dev);
372 pm_runtime_disable(dev);
374 for (i = 0; i < rockchip->num_clocks; i++) {
375 clk_unprepare(rockchip->clks[i]);
376 clk_put(rockchip->clks[i]);
383 static int dwc3_rockchip_runtime_suspend(struct device *dev)
385 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
388 for (i = 0; i < rockchip->num_clocks; i++)
389 clk_disable(rockchip->clks[i]);
394 static int dwc3_rockchip_runtime_resume(struct device *dev)
396 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
399 for (i = 0; i < rockchip->num_clocks; i++)
400 clk_enable(rockchip->clks[i]);
405 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
406 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
407 dwc3_rockchip_runtime_resume, NULL)
410 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
412 #define DEV_PM_OPS NULL
413 #endif /* CONFIG_PM */
415 static const struct of_device_id rockchip_dwc3_match[] = {
416 { .compatible = "rockchip,rk3399-dwc3" },
420 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
422 static struct platform_driver dwc3_rockchip_driver = {
423 .probe = dwc3_rockchip_probe,
424 .remove = dwc3_rockchip_remove,
426 .name = "rockchip-dwc3",
427 .of_match_table = rockchip_dwc3_match,
432 module_platform_driver(dwc3_rockchip_driver);
434 MODULE_ALIAS("platform:rockchip-dwc3");
435 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
436 MODULE_LICENSE("GPL v2");
437 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");