1 // SPDX-License-Identifier: GPL-2.0
3 * Cadence USBSS DRD Driver.
5 * Copyright (C) 2018-2020 Cadence.
6 * Copyright (C) 2017-2018 NXP
7 * Copyright (C) 2019 Texas Instruments
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
21 #include "gadget-export.h"
24 static int set_phy_power_on(struct cdns *cdns)
28 ret = phy_power_on(cdns->usb2_phy);
32 ret = phy_power_on(cdns->usb3_phy);
34 phy_power_off(cdns->usb2_phy);
39 static void set_phy_power_off(struct cdns *cdns)
41 phy_power_off(cdns->usb3_phy);
42 phy_power_off(cdns->usb2_phy);
46 * cdns3_plat_probe - probe for cdns3 core device
47 * @pdev: Pointer to cdns3 core platform device
49 * Returns 0 on success otherwise negative errno
51 static int cdns3_plat_probe(struct platform_device *pdev)
53 struct device *dev = &pdev->dev;
59 cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
64 cdns->pdata = dev_get_platdata(dev);
66 platform_set_drvdata(pdev, cdns);
68 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "host");
70 dev_err(dev, "missing host IRQ\n");
74 cdns->xhci_res[0] = *res;
76 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
78 dev_err(dev, "couldn't get xhci resource\n");
82 cdns->xhci_res[1] = *res;
84 cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
86 if (cdns->dev_irq < 0)
89 regs = devm_platform_ioremap_resource_byname(pdev, "dev");
92 cdns->dev_regs = regs;
94 cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
95 if (cdns->otg_irq < 0)
98 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
100 dev_err(dev, "couldn't get otg resource\n");
104 cdns->phyrst_a_enable = device_property_read_bool(dev, "cdns,phyrst-a-enable");
106 cdns->otg_res = *res;
108 cdns->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
109 if (cdns->wakeup_irq == -EPROBE_DEFER)
110 return cdns->wakeup_irq;
111 else if (cdns->wakeup_irq == 0)
114 if (cdns->wakeup_irq < 0) {
115 dev_dbg(dev, "couldn't get wakeup irq\n");
116 cdns->wakeup_irq = 0x0;
119 cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
120 if (IS_ERR(cdns->usb2_phy))
121 return PTR_ERR(cdns->usb2_phy);
123 ret = phy_init(cdns->usb2_phy);
127 cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
128 if (IS_ERR(cdns->usb3_phy))
129 return PTR_ERR(cdns->usb3_phy);
131 ret = phy_init(cdns->usb3_phy);
135 ret = set_phy_power_on(cdns);
137 goto err_phy_power_on;
139 cdns->gadget_init = cdns3_gadget_init;
141 ret = cdns_init(cdns);
145 device_set_wakeup_capable(dev, true);
146 pm_runtime_set_active(dev);
147 pm_runtime_enable(dev);
148 if (!(cdns->pdata && (cdns->pdata->quirks & CDNS3_DEFAULT_PM_RUNTIME_ALLOW)))
149 pm_runtime_forbid(dev);
152 * The controller needs less time between bus and controller suspend,
153 * and we also needs a small delay to avoid frequently entering low
156 pm_runtime_set_autosuspend_delay(dev, 20);
157 pm_runtime_mark_last_busy(dev);
158 pm_runtime_use_autosuspend(dev);
163 set_phy_power_off(cdns);
165 phy_exit(cdns->usb3_phy);
167 phy_exit(cdns->usb2_phy);
173 * cdns3_plat_remove() - unbind drd driver and clean up
174 * @pdev: Pointer to Linux platform device
176 * Returns 0 on success otherwise negative errno
178 static int cdns3_plat_remove(struct platform_device *pdev)
180 struct cdns *cdns = platform_get_drvdata(pdev);
181 struct device *dev = cdns->dev;
183 pm_runtime_get_sync(dev);
184 pm_runtime_disable(dev);
185 pm_runtime_put_noidle(dev);
187 set_phy_power_off(cdns);
188 phy_exit(cdns->usb2_phy);
189 phy_exit(cdns->usb3_phy);
195 static int cdns3_set_platform_suspend(struct device *dev,
196 bool suspend, bool wakeup)
198 struct cdns *cdns = dev_get_drvdata(dev);
201 if (cdns->pdata && cdns->pdata->platform_suspend)
202 ret = cdns->pdata->platform_suspend(dev, suspend, wakeup);
207 static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
209 struct cdns *cdns = dev_get_drvdata(dev);
216 if (PMSG_IS_AUTO(msg))
219 wakeup = device_may_wakeup(dev);
221 cdns3_set_platform_suspend(cdns->dev, true, wakeup);
222 set_phy_power_off(cdns);
223 spin_lock_irqsave(&cdns->lock, flags);
225 spin_unlock_irqrestore(&cdns->lock, flags);
226 dev_dbg(cdns->dev, "%s ends\n", __func__);
231 static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
233 struct cdns *cdns = dev_get_drvdata(dev);
240 if (cdns_power_is_lost(cdns)) {
241 phy_exit(cdns->usb2_phy);
242 ret = phy_init(cdns->usb2_phy);
246 phy_exit(cdns->usb3_phy);
247 ret = phy_init(cdns->usb3_phy);
252 ret = set_phy_power_on(cdns);
256 cdns3_set_platform_suspend(cdns->dev, false, false);
258 spin_lock_irqsave(&cdns->lock, flags);
259 cdns_resume(cdns, !PMSG_IS_AUTO(msg));
260 cdns->in_lpm = false;
261 spin_unlock_irqrestore(&cdns->lock, flags);
262 if (cdns->wakeup_pending) {
263 cdns->wakeup_pending = false;
264 enable_irq(cdns->wakeup_irq);
266 dev_dbg(cdns->dev, "%s ends\n", __func__);
271 static int cdns3_plat_runtime_suspend(struct device *dev)
273 return cdns3_controller_suspend(dev, PMSG_AUTO_SUSPEND);
276 static int cdns3_plat_runtime_resume(struct device *dev)
278 return cdns3_controller_resume(dev, PMSG_AUTO_RESUME);
281 #ifdef CONFIG_PM_SLEEP
283 static int cdns3_plat_suspend(struct device *dev)
285 struct cdns *cdns = dev_get_drvdata(dev);
290 ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
294 if (device_may_wakeup(dev) && cdns->wakeup_irq)
295 enable_irq_wake(cdns->wakeup_irq);
300 static int cdns3_plat_resume(struct device *dev)
302 return cdns3_controller_resume(dev, PMSG_RESUME);
304 #endif /* CONFIG_PM_SLEEP */
305 #endif /* CONFIG_PM */
307 static const struct dev_pm_ops cdns3_pm_ops = {
308 SET_SYSTEM_SLEEP_PM_OPS(cdns3_plat_suspend, cdns3_plat_resume)
309 SET_RUNTIME_PM_OPS(cdns3_plat_runtime_suspend,
310 cdns3_plat_runtime_resume, NULL)
314 static const struct of_device_id of_cdns3_match[] = {
315 { .compatible = "cdns,usb3" },
318 MODULE_DEVICE_TABLE(of, of_cdns3_match);
321 static struct platform_driver cdns3_driver = {
322 .probe = cdns3_plat_probe,
323 .remove = cdns3_plat_remove,
326 .of_match_table = of_match_ptr(of_cdns3_match),
331 module_platform_driver(cdns3_driver);
333 MODULE_ALIAS("platform:cdns3");
335 MODULE_LICENSE("GPL v2");
336 MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");