xref: /openbmc/linux/drivers/usb/cdns3/cdns3-plat.c (revision d088d6b6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cadence USBSS DRD Driver.
4  *
5  * Copyright (C) 2018-2020 Cadence.
6  * Copyright (C) 2017-2018 NXP
7  * Copyright (C) 2019 Texas Instruments
8  *
9  *
10  * Author: Peter Chen <peter.chen@nxp.com>
11  *         Pawel Laszczak <pawell@cadence.com>
12  *         Roger Quadros <rogerq@ti.com>
13  */
14 
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>
20 
21 #include "core.h"
22 #include "gadget-export.h"
23 #include "drd.h"
24 
25 static int set_phy_power_on(struct cdns *cdns)
26 {
27 	int ret;
28 
29 	ret = phy_power_on(cdns->usb2_phy);
30 	if (ret)
31 		return ret;
32 
33 	ret = phy_power_on(cdns->usb3_phy);
34 	if (ret)
35 		phy_power_off(cdns->usb2_phy);
36 
37 	return ret;
38 }
39 
40 static void set_phy_power_off(struct cdns *cdns)
41 {
42 	phy_power_off(cdns->usb3_phy);
43 	phy_power_off(cdns->usb2_phy);
44 }
45 
46 /**
47  * cdns3_plat_probe - probe for cdns3 core device
48  * @pdev: Pointer to cdns3 core platform device
49  *
50  * Returns 0 on success otherwise negative errno
51  */
52 static int cdns3_plat_probe(struct platform_device *pdev)
53 {
54 	struct device *dev = &pdev->dev;
55 	struct resource	*res;
56 	struct cdns *cdns;
57 	void __iomem *regs;
58 	int ret;
59 
60 	cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
61 	if (!cdns)
62 		return -ENOMEM;
63 
64 	cdns->dev = dev;
65 	cdns->pdata = dev_get_platdata(dev);
66 
67 	platform_set_drvdata(pdev, cdns);
68 
69 	ret = platform_get_irq_byname(pdev, "host");
70 	if (ret < 0)
71 		return ret;
72 
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";
77 
78 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
79 	if (!res) {
80 		dev_err(dev, "couldn't get xhci resource\n");
81 		return -ENXIO;
82 	}
83 
84 	cdns->xhci_res[1] = *res;
85 
86 	cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
87 
88 	if (cdns->dev_irq < 0)
89 		return cdns->dev_irq;
90 
91 	regs = devm_platform_ioremap_resource_byname(pdev, "dev");
92 	if (IS_ERR(regs))
93 		return PTR_ERR(regs);
94 	cdns->dev_regs	= regs;
95 
96 	cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
97 	if (cdns->otg_irq < 0)
98 		return cdns->otg_irq;
99 
100 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
101 	if (!res) {
102 		dev_err(dev, "couldn't get otg resource\n");
103 		return -ENXIO;
104 	}
105 
106 	cdns->phyrst_a_enable = device_property_read_bool(dev, "cdns,phyrst-a-enable");
107 
108 	cdns->otg_res = *res;
109 
110 	cdns->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
111 	if (cdns->wakeup_irq == -EPROBE_DEFER)
112 		return cdns->wakeup_irq;
113 
114 	if (cdns->wakeup_irq < 0) {
115 		dev_dbg(dev, "couldn't get wakeup irq\n");
116 		cdns->wakeup_irq = 0x0;
117 	}
118 
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);
122 
123 	ret = phy_init(cdns->usb2_phy);
124 	if (ret)
125 		return ret;
126 
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);
130 
131 	ret = phy_init(cdns->usb3_phy);
132 	if (ret)
133 		goto err_phy3_init;
134 
135 	ret = set_phy_power_on(cdns);
136 	if (ret)
137 		goto err_phy_power_on;
138 
139 	cdns->gadget_init = cdns3_gadget_init;
140 
141 	ret = cdns_init(cdns);
142 	if (ret)
143 		goto err_cdns_init;
144 
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);
150 
151 	/*
152 	 * The controller needs less time between bus and controller suspend,
153 	 * and we also needs a small delay to avoid frequently entering low
154 	 * power mode.
155 	 */
156 	pm_runtime_set_autosuspend_delay(dev, 20);
157 	pm_runtime_mark_last_busy(dev);
158 	pm_runtime_use_autosuspend(dev);
159 
160 	return 0;
161 
162 err_cdns_init:
163 	set_phy_power_off(cdns);
164 err_phy_power_on:
165 	phy_exit(cdns->usb3_phy);
166 err_phy3_init:
167 	phy_exit(cdns->usb2_phy);
168 
169 	return ret;
170 }
171 
172 /**
173  * cdns3_plat_remove() - unbind drd driver and clean up
174  * @pdev: Pointer to Linux platform device
175  *
176  * Returns 0 on success otherwise negative errno
177  */
178 static void cdns3_plat_remove(struct platform_device *pdev)
179 {
180 	struct cdns *cdns = platform_get_drvdata(pdev);
181 	struct device *dev = cdns->dev;
182 
183 	pm_runtime_get_sync(dev);
184 	pm_runtime_disable(dev);
185 	pm_runtime_put_noidle(dev);
186 	cdns_remove(cdns);
187 	set_phy_power_off(cdns);
188 	phy_exit(cdns->usb2_phy);
189 	phy_exit(cdns->usb3_phy);
190 }
191 
192 #ifdef CONFIG_PM
193 
194 static int cdns3_set_platform_suspend(struct device *dev,
195 				      bool suspend, bool wakeup)
196 {
197 	struct cdns *cdns = dev_get_drvdata(dev);
198 	int ret = 0;
199 
200 	if (cdns->pdata && cdns->pdata->platform_suspend)
201 		ret = cdns->pdata->platform_suspend(dev, suspend, wakeup);
202 
203 	return ret;
204 }
205 
206 static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
207 {
208 	struct cdns *cdns = dev_get_drvdata(dev);
209 	bool wakeup;
210 	unsigned long flags;
211 
212 	if (cdns->in_lpm)
213 		return 0;
214 
215 	if (PMSG_IS_AUTO(msg))
216 		wakeup = true;
217 	else
218 		wakeup = device_may_wakeup(dev);
219 
220 	cdns3_set_platform_suspend(cdns->dev, true, wakeup);
221 	set_phy_power_off(cdns);
222 	spin_lock_irqsave(&cdns->lock, flags);
223 	cdns->in_lpm = true;
224 	spin_unlock_irqrestore(&cdns->lock, flags);
225 	dev_dbg(cdns->dev, "%s ends\n", __func__);
226 
227 	return 0;
228 }
229 
230 static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
231 {
232 	struct cdns *cdns = dev_get_drvdata(dev);
233 	int ret;
234 	unsigned long flags;
235 
236 	if (!cdns->in_lpm)
237 		return 0;
238 
239 	if (cdns_power_is_lost(cdns)) {
240 		phy_exit(cdns->usb2_phy);
241 		ret = phy_init(cdns->usb2_phy);
242 		if (ret)
243 			return ret;
244 
245 		phy_exit(cdns->usb3_phy);
246 		ret = phy_init(cdns->usb3_phy);
247 		if (ret)
248 			return ret;
249 	}
250 
251 	ret = set_phy_power_on(cdns);
252 	if (ret)
253 		return ret;
254 
255 	cdns3_set_platform_suspend(cdns->dev, false, false);
256 
257 	spin_lock_irqsave(&cdns->lock, flags);
258 	cdns_resume(cdns, !PMSG_IS_AUTO(msg));
259 	cdns->in_lpm = false;
260 	spin_unlock_irqrestore(&cdns->lock, flags);
261 	if (cdns->wakeup_pending) {
262 		cdns->wakeup_pending = false;
263 		enable_irq(cdns->wakeup_irq);
264 	}
265 	dev_dbg(cdns->dev, "%s ends\n", __func__);
266 
267 	return ret;
268 }
269 
270 static int cdns3_plat_runtime_suspend(struct device *dev)
271 {
272 	return cdns3_controller_suspend(dev, PMSG_AUTO_SUSPEND);
273 }
274 
275 static int cdns3_plat_runtime_resume(struct device *dev)
276 {
277 	return cdns3_controller_resume(dev, PMSG_AUTO_RESUME);
278 }
279 
280 #ifdef CONFIG_PM_SLEEP
281 
282 static int cdns3_plat_suspend(struct device *dev)
283 {
284 	struct cdns *cdns = dev_get_drvdata(dev);
285 	int ret;
286 
287 	cdns_suspend(cdns);
288 
289 	ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
290 	if (ret)
291 		return ret;
292 
293 	if (device_may_wakeup(dev) && cdns->wakeup_irq)
294 		enable_irq_wake(cdns->wakeup_irq);
295 
296 	return ret;
297 }
298 
299 static int cdns3_plat_resume(struct device *dev)
300 {
301 	return cdns3_controller_resume(dev, PMSG_RESUME);
302 }
303 #endif /* CONFIG_PM_SLEEP */
304 #endif /* CONFIG_PM */
305 
306 static const struct dev_pm_ops cdns3_pm_ops = {
307 	SET_SYSTEM_SLEEP_PM_OPS(cdns3_plat_suspend, cdns3_plat_resume)
308 	SET_RUNTIME_PM_OPS(cdns3_plat_runtime_suspend,
309 			   cdns3_plat_runtime_resume, NULL)
310 };
311 
312 #ifdef CONFIG_OF
313 static const struct of_device_id of_cdns3_match[] = {
314 	{ .compatible = "cdns,usb3" },
315 	{ },
316 };
317 MODULE_DEVICE_TABLE(of, of_cdns3_match);
318 #endif
319 
320 static struct platform_driver cdns3_driver = {
321 	.probe		= cdns3_plat_probe,
322 	.remove_new	= cdns3_plat_remove,
323 	.driver		= {
324 		.name	= "cdns-usb3",
325 		.of_match_table	= of_match_ptr(of_cdns3_match),
326 		.pm	= &cdns3_pm_ops,
327 	},
328 };
329 
330 module_platform_driver(cdns3_driver);
331 
332 MODULE_ALIAS("platform:cdns3");
333 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
334 MODULE_LICENSE("GPL v2");
335 MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");
336