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/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
22 #include "gadget-export.h"
25 static int set_phy_power_on(struct cdns *cdns)
29 ret = phy_power_on(cdns->usb2_phy);
33 ret = phy_power_on(cdns->usb3_phy);
35 phy_power_off(cdns->usb2_phy);
40 static void set_phy_power_off(struct cdns *cdns)
42 phy_power_off(cdns->usb3_phy);
43 phy_power_off(cdns->usb2_phy);
47 * cdns3_plat_probe - probe for cdns3 core device
48 * @pdev: Pointer to cdns3 core platform device
50 * Returns 0 on success otherwise negative errno
52 static int cdns3_plat_probe(struct platform_device *pdev)
54 struct device *dev = &pdev->dev;
60 cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
65 cdns->pdata = dev_get_platdata(dev);
67 platform_set_drvdata(pdev, cdns);
69 ret = platform_get_irq_byname(pdev, "host");
73 cdns->xhci_res[0].start = ret;
74 cdns->xhci_res[0].end = ret;
75 cdns->xhci_res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(ret);
76 cdns->xhci_res[0].name = "host";
78 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
80 dev_err(dev, "couldn't get xhci resource\n");
84 cdns->xhci_res[1] = *res;
86 cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
88 if (cdns->dev_irq < 0)
91 regs = devm_platform_ioremap_resource_byname(pdev, "dev");
94 cdns->dev_regs = regs;
96 cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
97 if (cdns->otg_irq < 0)
100 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
102 dev_err(dev, "couldn't get otg resource\n");
106 cdns->phyrst_a_enable = device_property_read_bool(dev, "cdns,phyrst-a-enable");
108 cdns->otg_res = *res;
110 cdns->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
111 if (cdns->wakeup_irq == -EPROBE_DEFER)
112 return cdns->wakeup_irq;
113 else if (cdns->wakeup_irq == 0)
116 if (cdns->wakeup_irq < 0) {
117 dev_dbg(dev, "couldn't get wakeup irq\n");
118 cdns->wakeup_irq = 0x0;
121 cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
122 if (IS_ERR(cdns->usb2_phy))
123 return PTR_ERR(cdns->usb2_phy);
125 ret = phy_init(cdns->usb2_phy);
129 cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
130 if (IS_ERR(cdns->usb3_phy))
131 return PTR_ERR(cdns->usb3_phy);
133 ret = phy_init(cdns->usb3_phy);
137 ret = set_phy_power_on(cdns);
139 goto err_phy_power_on;
141 cdns->gadget_init = cdns3_gadget_init;
143 ret = cdns_init(cdns);
147 device_set_wakeup_capable(dev, true);
148 pm_runtime_set_active(dev);
149 pm_runtime_enable(dev);
150 if (!(cdns->pdata && (cdns->pdata->quirks & CDNS3_DEFAULT_PM_RUNTIME_ALLOW)))
151 pm_runtime_forbid(dev);
154 * The controller needs less time between bus and controller suspend,
155 * and we also needs a small delay to avoid frequently entering low
158 pm_runtime_set_autosuspend_delay(dev, 20);
159 pm_runtime_mark_last_busy(dev);
160 pm_runtime_use_autosuspend(dev);
165 set_phy_power_off(cdns);
167 phy_exit(cdns->usb3_phy);
169 phy_exit(cdns->usb2_phy);
175 * cdns3_plat_remove() - unbind drd driver and clean up
176 * @pdev: Pointer to Linux platform device
178 * Returns 0 on success otherwise negative errno
180 static int cdns3_plat_remove(struct platform_device *pdev)
182 struct cdns *cdns = platform_get_drvdata(pdev);
183 struct device *dev = cdns->dev;
185 pm_runtime_get_sync(dev);
186 pm_runtime_disable(dev);
187 pm_runtime_put_noidle(dev);
189 set_phy_power_off(cdns);
190 phy_exit(cdns->usb2_phy);
191 phy_exit(cdns->usb3_phy);
197 static int cdns3_set_platform_suspend(struct device *dev,
198 bool suspend, bool wakeup)
200 struct cdns *cdns = dev_get_drvdata(dev);
203 if (cdns->pdata && cdns->pdata->platform_suspend)
204 ret = cdns->pdata->platform_suspend(dev, suspend, wakeup);
209 static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
211 struct cdns *cdns = dev_get_drvdata(dev);
218 if (PMSG_IS_AUTO(msg))
221 wakeup = device_may_wakeup(dev);
223 cdns3_set_platform_suspend(cdns->dev, true, wakeup);
224 set_phy_power_off(cdns);
225 spin_lock_irqsave(&cdns->lock, flags);
227 spin_unlock_irqrestore(&cdns->lock, flags);
228 dev_dbg(cdns->dev, "%s ends\n", __func__);
233 static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
235 struct cdns *cdns = dev_get_drvdata(dev);
242 if (cdns_power_is_lost(cdns)) {
243 phy_exit(cdns->usb2_phy);
244 ret = phy_init(cdns->usb2_phy);
248 phy_exit(cdns->usb3_phy);
249 ret = phy_init(cdns->usb3_phy);
254 ret = set_phy_power_on(cdns);
258 cdns3_set_platform_suspend(cdns->dev, false, false);
260 spin_lock_irqsave(&cdns->lock, flags);
261 cdns_resume(cdns, !PMSG_IS_AUTO(msg));
262 cdns->in_lpm = false;
263 spin_unlock_irqrestore(&cdns->lock, flags);
264 if (cdns->wakeup_pending) {
265 cdns->wakeup_pending = false;
266 enable_irq(cdns->wakeup_irq);
268 dev_dbg(cdns->dev, "%s ends\n", __func__);
273 static int cdns3_plat_runtime_suspend(struct device *dev)
275 return cdns3_controller_suspend(dev, PMSG_AUTO_SUSPEND);
278 static int cdns3_plat_runtime_resume(struct device *dev)
280 return cdns3_controller_resume(dev, PMSG_AUTO_RESUME);
283 #ifdef CONFIG_PM_SLEEP
285 static int cdns3_plat_suspend(struct device *dev)
287 struct cdns *cdns = dev_get_drvdata(dev);
292 ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
296 if (device_may_wakeup(dev) && cdns->wakeup_irq)
297 enable_irq_wake(cdns->wakeup_irq);
302 static int cdns3_plat_resume(struct device *dev)
304 return cdns3_controller_resume(dev, PMSG_RESUME);
306 #endif /* CONFIG_PM_SLEEP */
307 #endif /* CONFIG_PM */
309 static const struct dev_pm_ops cdns3_pm_ops = {
310 SET_SYSTEM_SLEEP_PM_OPS(cdns3_plat_suspend, cdns3_plat_resume)
311 SET_RUNTIME_PM_OPS(cdns3_plat_runtime_suspend,
312 cdns3_plat_runtime_resume, NULL)
316 static const struct of_device_id of_cdns3_match[] = {
317 { .compatible = "cdns,usb3" },
320 MODULE_DEVICE_TABLE(of, of_cdns3_match);
323 static struct platform_driver cdns3_driver = {
324 .probe = cdns3_plat_probe,
325 .remove = cdns3_plat_remove,
328 .of_match_table = of_match_ptr(of_cdns3_match),
333 module_platform_driver(cdns3_driver);
335 MODULE_ALIAS("platform:cdns3");
337 MODULE_LICENSE("GPL v2");
338 MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");