xref: /openbmc/linux/drivers/usb/cdns3/cdns3-plat.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1f7389572SPawel Laszczak // SPDX-License-Identifier: GPL-2.0
2f7389572SPawel Laszczak /*
3f7389572SPawel Laszczak  * Cadence USBSS DRD Driver.
4f7389572SPawel Laszczak  *
5f7389572SPawel Laszczak  * Copyright (C) 2018-2020 Cadence.
6f7389572SPawel Laszczak  * Copyright (C) 2017-2018 NXP
7f7389572SPawel Laszczak  * Copyright (C) 2019 Texas Instruments
8f7389572SPawel Laszczak  *
9f7389572SPawel Laszczak  *
10f7389572SPawel Laszczak  * Author: Peter Chen <peter.chen@nxp.com>
11f7389572SPawel Laszczak  *         Pawel Laszczak <pawell@cadence.com>
12f7389572SPawel Laszczak  *         Roger Quadros <rogerq@ti.com>
13f7389572SPawel Laszczak  */
14f7389572SPawel Laszczak 
15f7389572SPawel Laszczak #include <linux/module.h>
1678e17d69SLad Prabhakar #include <linux/irq.h>
17f7389572SPawel Laszczak #include <linux/kernel.h>
18*484468fbSRob Herring #include <linux/of.h>
19f7389572SPawel Laszczak #include <linux/platform_device.h>
20f7389572SPawel Laszczak #include <linux/pm_runtime.h>
21f7389572SPawel Laszczak 
22f7389572SPawel Laszczak #include "core.h"
23394c3a14SPawel Laszczak #include "gadget-export.h"
242cf2581cSFrank Li #include "drd.h"
25f7389572SPawel Laszczak 
set_phy_power_on(struct cdns * cdns)260b490046SPawel Laszczak static int set_phy_power_on(struct cdns *cdns)
27f7389572SPawel Laszczak {
28f7389572SPawel Laszczak 	int ret;
29f7389572SPawel Laszczak 
30f7389572SPawel Laszczak 	ret = phy_power_on(cdns->usb2_phy);
31f7389572SPawel Laszczak 	if (ret)
32f7389572SPawel Laszczak 		return ret;
33f7389572SPawel Laszczak 
34f7389572SPawel Laszczak 	ret = phy_power_on(cdns->usb3_phy);
35f7389572SPawel Laszczak 	if (ret)
36f7389572SPawel Laszczak 		phy_power_off(cdns->usb2_phy);
37f7389572SPawel Laszczak 
38f7389572SPawel Laszczak 	return ret;
39f7389572SPawel Laszczak }
40f7389572SPawel Laszczak 
set_phy_power_off(struct cdns * cdns)410b490046SPawel Laszczak static void set_phy_power_off(struct cdns *cdns)
42f7389572SPawel Laszczak {
43f7389572SPawel Laszczak 	phy_power_off(cdns->usb3_phy);
44f7389572SPawel Laszczak 	phy_power_off(cdns->usb2_phy);
45f7389572SPawel Laszczak }
46f7389572SPawel Laszczak 
47f7389572SPawel Laszczak /**
48f7389572SPawel Laszczak  * cdns3_plat_probe - probe for cdns3 core device
49f7389572SPawel Laszczak  * @pdev: Pointer to cdns3 core platform device
50f7389572SPawel Laszczak  *
51f7389572SPawel Laszczak  * Returns 0 on success otherwise negative errno
52f7389572SPawel Laszczak  */
cdns3_plat_probe(struct platform_device * pdev)53f7389572SPawel Laszczak static int cdns3_plat_probe(struct platform_device *pdev)
54f7389572SPawel Laszczak {
55f7389572SPawel Laszczak 	struct device *dev = &pdev->dev;
56f7389572SPawel Laszczak 	struct resource	*res;
570b490046SPawel Laszczak 	struct cdns *cdns;
58f7389572SPawel Laszczak 	void __iomem *regs;
59f7389572SPawel Laszczak 	int ret;
60f7389572SPawel Laszczak 
61f7389572SPawel Laszczak 	cdns = devm_kzalloc(dev, sizeof(*cdns), GFP_KERNEL);
62f7389572SPawel Laszczak 	if (!cdns)
63f7389572SPawel Laszczak 		return -ENOMEM;
64f7389572SPawel Laszczak 
65f7389572SPawel Laszczak 	cdns->dev = dev;
66f7389572SPawel Laszczak 	cdns->pdata = dev_get_platdata(dev);
67f7389572SPawel Laszczak 
68f7389572SPawel Laszczak 	platform_set_drvdata(pdev, cdns);
69f7389572SPawel Laszczak 
7078e17d69SLad Prabhakar 	ret = platform_get_irq_byname(pdev, "host");
7178e17d69SLad Prabhakar 	if (ret < 0)
7278e17d69SLad Prabhakar 		return ret;
73f7389572SPawel Laszczak 
7478e17d69SLad Prabhakar 	cdns->xhci_res[0].start = ret;
7578e17d69SLad Prabhakar 	cdns->xhci_res[0].end = ret;
7678e17d69SLad Prabhakar 	cdns->xhci_res[0].flags = IORESOURCE_IRQ | irq_get_trigger_type(ret);
7778e17d69SLad Prabhakar 	cdns->xhci_res[0].name = "host";
78f7389572SPawel Laszczak 
79f7389572SPawel Laszczak 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "xhci");
80f7389572SPawel Laszczak 	if (!res) {
81f7389572SPawel Laszczak 		dev_err(dev, "couldn't get xhci resource\n");
82f7389572SPawel Laszczak 		return -ENXIO;
83f7389572SPawel Laszczak 	}
84f7389572SPawel Laszczak 
85f7389572SPawel Laszczak 	cdns->xhci_res[1] = *res;
86f7389572SPawel Laszczak 
87f7389572SPawel Laszczak 	cdns->dev_irq = platform_get_irq_byname(pdev, "peripheral");
88f7389572SPawel Laszczak 
89f7389572SPawel Laszczak 	if (cdns->dev_irq < 0)
90f7389572SPawel Laszczak 		return cdns->dev_irq;
91f7389572SPawel Laszczak 
92f7389572SPawel Laszczak 	regs = devm_platform_ioremap_resource_byname(pdev, "dev");
93f7389572SPawel Laszczak 	if (IS_ERR(regs))
94f7389572SPawel Laszczak 		return PTR_ERR(regs);
95f7389572SPawel Laszczak 	cdns->dev_regs	= regs;
96f7389572SPawel Laszczak 
97f7389572SPawel Laszczak 	cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
98f7389572SPawel Laszczak 	if (cdns->otg_irq < 0)
99f7389572SPawel Laszczak 		return cdns->otg_irq;
100f7389572SPawel Laszczak 
101f7389572SPawel Laszczak 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
102f7389572SPawel Laszczak 	if (!res) {
103f7389572SPawel Laszczak 		dev_err(dev, "couldn't get otg resource\n");
104f7389572SPawel Laszczak 		return -ENXIO;
105f7389572SPawel Laszczak 	}
106f7389572SPawel Laszczak 
107f7389572SPawel Laszczak 	cdns->phyrst_a_enable = device_property_read_bool(dev, "cdns,phyrst-a-enable");
108f7389572SPawel Laszczak 
109f7389572SPawel Laszczak 	cdns->otg_res = *res;
110f7389572SPawel Laszczak 
111f7389572SPawel Laszczak 	cdns->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
112f7389572SPawel Laszczak 	if (cdns->wakeup_irq == -EPROBE_DEFER)
113f7389572SPawel Laszczak 		return cdns->wakeup_irq;
114f7389572SPawel Laszczak 
115f7389572SPawel Laszczak 	if (cdns->wakeup_irq < 0) {
116f7389572SPawel Laszczak 		dev_dbg(dev, "couldn't get wakeup irq\n");
117f7389572SPawel Laszczak 		cdns->wakeup_irq = 0x0;
118f7389572SPawel Laszczak 	}
119f7389572SPawel Laszczak 
120f7389572SPawel Laszczak 	cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
121f7389572SPawel Laszczak 	if (IS_ERR(cdns->usb2_phy))
122f7389572SPawel Laszczak 		return PTR_ERR(cdns->usb2_phy);
123f7389572SPawel Laszczak 
124f7389572SPawel Laszczak 	ret = phy_init(cdns->usb2_phy);
125f7389572SPawel Laszczak 	if (ret)
126f7389572SPawel Laszczak 		return ret;
127f7389572SPawel Laszczak 
128f7389572SPawel Laszczak 	cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
129f7389572SPawel Laszczak 	if (IS_ERR(cdns->usb3_phy))
130f7389572SPawel Laszczak 		return PTR_ERR(cdns->usb3_phy);
131f7389572SPawel Laszczak 
132f7389572SPawel Laszczak 	ret = phy_init(cdns->usb3_phy);
133f7389572SPawel Laszczak 	if (ret)
134f7389572SPawel Laszczak 		goto err_phy3_init;
135f7389572SPawel Laszczak 
136f7389572SPawel Laszczak 	ret = set_phy_power_on(cdns);
137f7389572SPawel Laszczak 	if (ret)
138f7389572SPawel Laszczak 		goto err_phy_power_on;
139f7389572SPawel Laszczak 
140394c3a14SPawel Laszczak 	cdns->gadget_init = cdns3_gadget_init;
1410b490046SPawel Laszczak 
1420b490046SPawel Laszczak 	ret = cdns_init(cdns);
143f7389572SPawel Laszczak 	if (ret)
144f7389572SPawel Laszczak 		goto err_cdns_init;
145f7389572SPawel Laszczak 
146f7389572SPawel Laszczak 	device_set_wakeup_capable(dev, true);
147f7389572SPawel Laszczak 	pm_runtime_set_active(dev);
148f7389572SPawel Laszczak 	pm_runtime_enable(dev);
149f7389572SPawel Laszczak 	if (!(cdns->pdata && (cdns->pdata->quirks & CDNS3_DEFAULT_PM_RUNTIME_ALLOW)))
150f7389572SPawel Laszczak 		pm_runtime_forbid(dev);
151f7389572SPawel Laszczak 
152f7389572SPawel Laszczak 	/*
153f7389572SPawel Laszczak 	 * The controller needs less time between bus and controller suspend,
154f7389572SPawel Laszczak 	 * and we also needs a small delay to avoid frequently entering low
155f7389572SPawel Laszczak 	 * power mode.
156f7389572SPawel Laszczak 	 */
157f7389572SPawel Laszczak 	pm_runtime_set_autosuspend_delay(dev, 20);
158f7389572SPawel Laszczak 	pm_runtime_mark_last_busy(dev);
159f7389572SPawel Laszczak 	pm_runtime_use_autosuspend(dev);
160f7389572SPawel Laszczak 
161f7389572SPawel Laszczak 	return 0;
162f7389572SPawel Laszczak 
163f7389572SPawel Laszczak err_cdns_init:
164f7389572SPawel Laszczak 	set_phy_power_off(cdns);
165f7389572SPawel Laszczak err_phy_power_on:
166f7389572SPawel Laszczak 	phy_exit(cdns->usb3_phy);
167f7389572SPawel Laszczak err_phy3_init:
168f7389572SPawel Laszczak 	phy_exit(cdns->usb2_phy);
169f7389572SPawel Laszczak 
170f7389572SPawel Laszczak 	return ret;
171f7389572SPawel Laszczak }
172f7389572SPawel Laszczak 
173f7389572SPawel Laszczak /**
1746dd1efebSLee Jones  * cdns3_plat_remove() - unbind drd driver and clean up
175f7389572SPawel Laszczak  * @pdev: Pointer to Linux platform device
176f7389572SPawel Laszczak  *
177f7389572SPawel Laszczak  * Returns 0 on success otherwise negative errno
178f7389572SPawel Laszczak  */
cdns3_plat_remove(struct platform_device * pdev)1795411fa0eSUwe Kleine-König static void cdns3_plat_remove(struct platform_device *pdev)
180f7389572SPawel Laszczak {
1810b490046SPawel Laszczak 	struct cdns *cdns = platform_get_drvdata(pdev);
182f7389572SPawel Laszczak 	struct device *dev = cdns->dev;
183f7389572SPawel Laszczak 
184f7389572SPawel Laszczak 	pm_runtime_get_sync(dev);
185f7389572SPawel Laszczak 	pm_runtime_disable(dev);
186f7389572SPawel Laszczak 	pm_runtime_put_noidle(dev);
1870b490046SPawel Laszczak 	cdns_remove(cdns);
188f7389572SPawel Laszczak 	set_phy_power_off(cdns);
189f7389572SPawel Laszczak 	phy_exit(cdns->usb2_phy);
190f7389572SPawel Laszczak 	phy_exit(cdns->usb3_phy);
191f7389572SPawel Laszczak }
192f7389572SPawel Laszczak 
193f7389572SPawel Laszczak #ifdef CONFIG_PM
194f7389572SPawel Laszczak 
cdns3_set_platform_suspend(struct device * dev,bool suspend,bool wakeup)195f7389572SPawel Laszczak static int cdns3_set_platform_suspend(struct device *dev,
196f7389572SPawel Laszczak 				      bool suspend, bool wakeup)
197f7389572SPawel Laszczak {
1980b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
199f7389572SPawel Laszczak 	int ret = 0;
200f7389572SPawel Laszczak 
201f7389572SPawel Laszczak 	if (cdns->pdata && cdns->pdata->platform_suspend)
202f7389572SPawel Laszczak 		ret = cdns->pdata->platform_suspend(dev, suspend, wakeup);
203f7389572SPawel Laszczak 
204f7389572SPawel Laszczak 	return ret;
205f7389572SPawel Laszczak }
206f7389572SPawel Laszczak 
cdns3_controller_suspend(struct device * dev,pm_message_t msg)207f7389572SPawel Laszczak static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
208f7389572SPawel Laszczak {
2090b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
210f7389572SPawel Laszczak 	bool wakeup;
211f7389572SPawel Laszczak 	unsigned long flags;
212f7389572SPawel Laszczak 
213f7389572SPawel Laszczak 	if (cdns->in_lpm)
214f7389572SPawel Laszczak 		return 0;
215f7389572SPawel Laszczak 
216f7389572SPawel Laszczak 	if (PMSG_IS_AUTO(msg))
217f7389572SPawel Laszczak 		wakeup = true;
218f7389572SPawel Laszczak 	else
219f7389572SPawel Laszczak 		wakeup = device_may_wakeup(dev);
220f7389572SPawel Laszczak 
221f7389572SPawel Laszczak 	cdns3_set_platform_suspend(cdns->dev, true, wakeup);
222f7389572SPawel Laszczak 	set_phy_power_off(cdns);
223f7389572SPawel Laszczak 	spin_lock_irqsave(&cdns->lock, flags);
224f7389572SPawel Laszczak 	cdns->in_lpm = true;
225f7389572SPawel Laszczak 	spin_unlock_irqrestore(&cdns->lock, flags);
226f7389572SPawel Laszczak 	dev_dbg(cdns->dev, "%s ends\n", __func__);
227f7389572SPawel Laszczak 
228f7389572SPawel Laszczak 	return 0;
229f7389572SPawel Laszczak }
230f7389572SPawel Laszczak 
cdns3_controller_resume(struct device * dev,pm_message_t msg)231f7389572SPawel Laszczak static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
232f7389572SPawel Laszczak {
2330b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
234f7389572SPawel Laszczak 	int ret;
235f7389572SPawel Laszczak 	unsigned long flags;
236f7389572SPawel Laszczak 
237f7389572SPawel Laszczak 	if (!cdns->in_lpm)
238f7389572SPawel Laszczak 		return 0;
239f7389572SPawel Laszczak 
2402cf2581cSFrank Li 	if (cdns_power_is_lost(cdns)) {
2412cf2581cSFrank Li 		phy_exit(cdns->usb2_phy);
2422cf2581cSFrank Li 		ret = phy_init(cdns->usb2_phy);
2432cf2581cSFrank Li 		if (ret)
2442cf2581cSFrank Li 			return ret;
2452cf2581cSFrank Li 
2462cf2581cSFrank Li 		phy_exit(cdns->usb3_phy);
2472cf2581cSFrank Li 		ret = phy_init(cdns->usb3_phy);
2482cf2581cSFrank Li 		if (ret)
2492cf2581cSFrank Li 			return ret;
2502cf2581cSFrank Li 	}
2512cf2581cSFrank Li 
252f7389572SPawel Laszczak 	ret = set_phy_power_on(cdns);
253f7389572SPawel Laszczak 	if (ret)
254f7389572SPawel Laszczak 		return ret;
255f7389572SPawel Laszczak 
256f7389572SPawel Laszczak 	cdns3_set_platform_suspend(cdns->dev, false, false);
257f7389572SPawel Laszczak 
258f7389572SPawel Laszczak 	spin_lock_irqsave(&cdns->lock, flags);
2592319b9c8SXiaolei Wang 	cdns_resume(cdns);
260f7389572SPawel Laszczak 	cdns->in_lpm = false;
261f7389572SPawel Laszczak 	spin_unlock_irqrestore(&cdns->lock, flags);
2622319b9c8SXiaolei Wang 	cdns_set_active(cdns, !PMSG_IS_AUTO(msg));
263f7389572SPawel Laszczak 	if (cdns->wakeup_pending) {
264f7389572SPawel Laszczak 		cdns->wakeup_pending = false;
265f7389572SPawel Laszczak 		enable_irq(cdns->wakeup_irq);
266f7389572SPawel Laszczak 	}
267f7389572SPawel Laszczak 	dev_dbg(cdns->dev, "%s ends\n", __func__);
268f7389572SPawel Laszczak 
269f7389572SPawel Laszczak 	return ret;
270f7389572SPawel Laszczak }
271f7389572SPawel Laszczak 
cdns3_plat_runtime_suspend(struct device * dev)272f7389572SPawel Laszczak static int cdns3_plat_runtime_suspend(struct device *dev)
273f7389572SPawel Laszczak {
274f7389572SPawel Laszczak 	return cdns3_controller_suspend(dev, PMSG_AUTO_SUSPEND);
275f7389572SPawel Laszczak }
276f7389572SPawel Laszczak 
cdns3_plat_runtime_resume(struct device * dev)277f7389572SPawel Laszczak static int cdns3_plat_runtime_resume(struct device *dev)
278f7389572SPawel Laszczak {
279f7389572SPawel Laszczak 	return cdns3_controller_resume(dev, PMSG_AUTO_RESUME);
280f7389572SPawel Laszczak }
281f7389572SPawel Laszczak 
282f7389572SPawel Laszczak #ifdef CONFIG_PM_SLEEP
283f7389572SPawel Laszczak 
cdns3_plat_suspend(struct device * dev)284f7389572SPawel Laszczak static int cdns3_plat_suspend(struct device *dev)
285f7389572SPawel Laszczak {
2860b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
2872cf2581cSFrank Li 	int ret;
288f7389572SPawel Laszczak 
2890b490046SPawel Laszczak 	cdns_suspend(cdns);
290f7389572SPawel Laszczak 
2912cf2581cSFrank Li 	ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
2922cf2581cSFrank Li 	if (ret)
2932cf2581cSFrank Li 		return ret;
2942cf2581cSFrank Li 
2952cf2581cSFrank Li 	if (device_may_wakeup(dev) && cdns->wakeup_irq)
2962cf2581cSFrank Li 		enable_irq_wake(cdns->wakeup_irq);
2972cf2581cSFrank Li 
2982cf2581cSFrank Li 	return ret;
299f7389572SPawel Laszczak }
300f7389572SPawel Laszczak 
cdns3_plat_resume(struct device * dev)301f7389572SPawel Laszczak static int cdns3_plat_resume(struct device *dev)
302f7389572SPawel Laszczak {
303f7389572SPawel Laszczak 	return cdns3_controller_resume(dev, PMSG_RESUME);
304f7389572SPawel Laszczak }
305f7389572SPawel Laszczak #endif /* CONFIG_PM_SLEEP */
306f7389572SPawel Laszczak #endif /* CONFIG_PM */
307f7389572SPawel Laszczak 
308f7389572SPawel Laszczak static const struct dev_pm_ops cdns3_pm_ops = {
309f7389572SPawel Laszczak 	SET_SYSTEM_SLEEP_PM_OPS(cdns3_plat_suspend, cdns3_plat_resume)
310f7389572SPawel Laszczak 	SET_RUNTIME_PM_OPS(cdns3_plat_runtime_suspend,
311f7389572SPawel Laszczak 			   cdns3_plat_runtime_resume, NULL)
312f7389572SPawel Laszczak };
313f7389572SPawel Laszczak 
314f7389572SPawel Laszczak #ifdef CONFIG_OF
315f7389572SPawel Laszczak static const struct of_device_id of_cdns3_match[] = {
316f7389572SPawel Laszczak 	{ .compatible = "cdns,usb3" },
317f7389572SPawel Laszczak 	{ },
318f7389572SPawel Laszczak };
319f7389572SPawel Laszczak MODULE_DEVICE_TABLE(of, of_cdns3_match);
320f7389572SPawel Laszczak #endif
321f7389572SPawel Laszczak 
322f7389572SPawel Laszczak static struct platform_driver cdns3_driver = {
323f7389572SPawel Laszczak 	.probe		= cdns3_plat_probe,
3245411fa0eSUwe Kleine-König 	.remove_new	= cdns3_plat_remove,
325f7389572SPawel Laszczak 	.driver		= {
326f7389572SPawel Laszczak 		.name	= "cdns-usb3",
327f7389572SPawel Laszczak 		.of_match_table	= of_match_ptr(of_cdns3_match),
328f7389572SPawel Laszczak 		.pm	= &cdns3_pm_ops,
329f7389572SPawel Laszczak 	},
330f7389572SPawel Laszczak };
331f7389572SPawel Laszczak 
332f7389572SPawel Laszczak module_platform_driver(cdns3_driver);
333f7389572SPawel Laszczak 
334f7389572SPawel Laszczak MODULE_ALIAS("platform:cdns3");
335f7389572SPawel Laszczak MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
336f7389572SPawel Laszczak MODULE_LICENSE("GPL v2");
337f7389572SPawel Laszczak MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");
338