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);
123 ret = phy_power_on(dwc->usb3_generic_phy);
127 reset_control_deassert(rockchip->otg_rst);
129 pm_runtime_get_sync(dwc->dev);
131 hcd = dev_get_drvdata(&dwc->xhci->dev);
133 spin_lock_irqsave(&dwc->lock, flags);
134 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
135 spin_unlock_irqrestore(&dwc->lock, flags);
137 if (hcd->state == HC_STATE_HALT) {
138 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
139 usb_add_hcd(hcd->shared_hcd, hcd->irq, IRQF_SHARED);
142 rockchip->connected = true;
143 dev_info(rockchip->dev, "USB HOST connected\n");
145 if (!rockchip->connected)
148 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
150 if (DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_HOST ||
151 DWC3_GCTL_PRTCAP(reg) == DWC3_GCTL_PRTCAP_OTG) {
152 hcd = dev_get_drvdata(&dwc->xhci->dev);
154 if (hcd->state != HC_STATE_HALT) {
155 usb_remove_hcd(hcd->shared_hcd);
159 phy_power_off(dwc->usb2_generic_phy);
160 phy_power_off(dwc->usb3_generic_phy);
164 pm_runtime_put_sync(dwc->dev);
166 rockchip->connected = false;
167 dev_info(rockchip->dev, "USB unconnected\n");
171 static int dwc3_rockchip_extcon_register(struct dwc3_rockchip *rockchip)
174 struct device *dev = rockchip->dev;
175 struct extcon_dev *edev;
177 if (device_property_read_bool(dev, "extcon")) {
178 edev = extcon_get_edev_by_phandle(dev, 0);
180 if (PTR_ERR(edev) != -EPROBE_DEFER)
181 dev_err(dev, "couldn't get extcon device\n");
182 return PTR_ERR(edev);
185 INIT_WORK(&rockchip->otg_work,
186 dwc3_rockchip_otg_extcon_evt_work);
188 rockchip->device_nb.notifier_call =
189 dwc3_rockchip_device_notifier;
190 ret = extcon_register_notifier(edev, EXTCON_USB,
191 &rockchip->device_nb);
193 dev_err(dev, "failed to register notifier for USB\n");
197 rockchip->host_nb.notifier_call =
198 dwc3_rockchip_host_notifier;
199 ret = extcon_register_notifier(edev, EXTCON_USB_HOST,
202 dev_err(dev, "failed to register notifier for USB HOST\n");
203 extcon_unregister_notifier(edev, EXTCON_USB,
204 &rockchip->device_nb);
208 rockchip->edev = edev;
214 static void dwc3_rockchip_extcon_unregister(struct dwc3_rockchip *rockchip)
219 extcon_unregister_notifier(rockchip->edev, EXTCON_USB,
220 &rockchip->device_nb);
221 extcon_unregister_notifier(rockchip->edev, EXTCON_USB_HOST,
225 static int dwc3_rockchip_probe(struct platform_device *pdev)
227 struct dwc3_rockchip *rockchip;
228 struct device *dev = &pdev->dev;
229 struct device_node *np = dev->of_node, *child;
230 struct platform_device *child_pdev;
236 rockchip = devm_kzalloc(dev, sizeof(*rockchip), GFP_KERNEL);
241 count = of_clk_get_parent_count(np);
245 rockchip->num_clocks = count;
247 rockchip->clks = devm_kcalloc(dev, rockchip->num_clocks,
248 sizeof(struct clk *), GFP_KERNEL);
252 platform_set_drvdata(pdev, rockchip);
255 rockchip->edev = NULL;
257 for (i = 0; i < rockchip->num_clocks; i++) {
260 clk = of_clk_get(np, i);
263 clk_put(rockchip->clks[i]);
267 ret = clk_prepare_enable(clk);
270 clk_disable_unprepare(rockchip->clks[i]);
271 clk_put(rockchip->clks[i]);
278 rockchip->clks[i] = clk;
281 pm_runtime_set_active(dev);
282 pm_runtime_enable(dev);
283 ret = pm_runtime_get_sync(dev);
285 dev_err(dev, "get_sync failed with err %d\n", ret);
289 rockchip->otg_rst = devm_reset_control_get(dev, "usb3-otg");
290 if (IS_ERR(rockchip->otg_rst)) {
291 dev_err(dev, "could not get reset controller\n");
292 ret = PTR_ERR(rockchip->otg_rst);
296 ret = dwc3_rockchip_extcon_register(rockchip);
300 child = of_get_child_by_name(np, "dwc3");
302 dev_err(dev, "failed to find dwc3 core node\n");
307 /* Allocate and initialize the core */
308 ret = of_platform_populate(np, NULL, NULL, dev);
310 dev_err(dev, "failed to create dwc3 core\n");
314 child_pdev = of_find_device_by_node(child);
316 dev_err(dev, "failed to find dwc3 core device\n");
321 rockchip->dwc = platform_get_drvdata(child_pdev);
322 if (!rockchip->dwc) {
323 dev_err(dev, "failed to get drvdata dwc3\n");
328 if (rockchip->edev) {
329 pm_runtime_set_autosuspend_delay(&child_pdev->dev,
330 DWC3_ROCKCHIP_AUTOSUSPEND_DELAY);
331 pm_runtime_allow(&child_pdev->dev);
333 if (rockchip->dwc->dr_mode == USB_DR_MODE_HOST ||
334 rockchip->dwc->dr_mode == USB_DR_MODE_OTG) {
335 struct usb_hcd *hcd =
336 dev_get_drvdata(&rockchip->dwc->xhci->dev);
338 if (hcd->state != HC_STATE_HALT) {
339 usb_remove_hcd(hcd->shared_hcd);
344 pm_runtime_put_sync(dev);
350 of_platform_depopulate(dev);
353 dwc3_rockchip_extcon_unregister(rockchip);
356 pm_runtime_put_sync(dev);
357 pm_runtime_disable(dev);
359 for (i = 0; i < rockchip->num_clocks; i++) {
360 clk_unprepare(rockchip->clks[i]);
361 clk_put(rockchip->clks[i]);
367 static int dwc3_rockchip_remove(struct platform_device *pdev)
369 struct dwc3_rockchip *rockchip = platform_get_drvdata(pdev);
370 struct device *dev = &pdev->dev;
373 dwc3_rockchip_extcon_unregister(rockchip);
375 of_platform_depopulate(dev);
377 pm_runtime_put_sync(dev);
378 pm_runtime_disable(dev);
380 for (i = 0; i < rockchip->num_clocks; i++) {
381 clk_unprepare(rockchip->clks[i]);
382 clk_put(rockchip->clks[i]);
389 static int dwc3_rockchip_runtime_suspend(struct device *dev)
391 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
394 for (i = 0; i < rockchip->num_clocks; i++)
395 clk_disable(rockchip->clks[i]);
400 static int dwc3_rockchip_runtime_resume(struct device *dev)
402 struct dwc3_rockchip *rockchip = dev_get_drvdata(dev);
405 for (i = 0; i < rockchip->num_clocks; i++)
406 clk_enable(rockchip->clks[i]);
411 static const struct dev_pm_ops dwc3_rockchip_dev_pm_ops = {
412 SET_RUNTIME_PM_OPS(dwc3_rockchip_runtime_suspend,
413 dwc3_rockchip_runtime_resume, NULL)
416 #define DEV_PM_OPS (&dwc3_rockchip_dev_pm_ops)
418 #define DEV_PM_OPS NULL
419 #endif /* CONFIG_PM */
421 static const struct of_device_id rockchip_dwc3_match[] = {
422 { .compatible = "rockchip,rk3399-dwc3" },
426 MODULE_DEVICE_TABLE(of, rockchip_dwc3_match);
428 static struct platform_driver dwc3_rockchip_driver = {
429 .probe = dwc3_rockchip_probe,
430 .remove = dwc3_rockchip_remove,
432 .name = "rockchip-dwc3",
433 .of_match_table = rockchip_dwc3_match,
438 module_platform_driver(dwc3_rockchip_driver);
440 MODULE_ALIAS("platform:rockchip-dwc3");
441 MODULE_AUTHOR("William Wu <william.wu@rock-chips.com>");
442 MODULE_LICENSE("GPL v2");
443 MODULE_DESCRIPTION("DesignWare USB3 ROCKCHIP Glue Layer");