xref: /linux/drivers/usb/cdns3/cdns3-plat.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
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>
18484468fbSRob 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)
90*776630beSAlexander Stein 		return dev_err_probe(dev, cdns->dev_irq,
91*776630beSAlexander Stein 				     "Failed to get peripheral IRQ\n");
92f7389572SPawel Laszczak 
93f7389572SPawel Laszczak 	regs = devm_platform_ioremap_resource_byname(pdev, "dev");
94f7389572SPawel Laszczak 	if (IS_ERR(regs))
95*776630beSAlexander Stein 		return dev_err_probe(dev, PTR_ERR(regs),
96*776630beSAlexander Stein 				     "Failed to get dev base\n");
97*776630beSAlexander Stein 
98f7389572SPawel Laszczak 	cdns->dev_regs	= regs;
99f7389572SPawel Laszczak 
100f7389572SPawel Laszczak 	cdns->otg_irq = platform_get_irq_byname(pdev, "otg");
101f7389572SPawel Laszczak 	if (cdns->otg_irq < 0)
102*776630beSAlexander Stein 		return dev_err_probe(dev, cdns->otg_irq,
103*776630beSAlexander Stein 				     "Failed to get otg IRQ\n");
104f7389572SPawel Laszczak 
105f7389572SPawel Laszczak 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "otg");
106f7389572SPawel Laszczak 	if (!res) {
107f7389572SPawel Laszczak 		dev_err(dev, "couldn't get otg resource\n");
108f7389572SPawel Laszczak 		return -ENXIO;
109f7389572SPawel Laszczak 	}
110f7389572SPawel Laszczak 
111f7389572SPawel Laszczak 	cdns->phyrst_a_enable = device_property_read_bool(dev, "cdns,phyrst-a-enable");
112f7389572SPawel Laszczak 
113f7389572SPawel Laszczak 	cdns->otg_res = *res;
114f7389572SPawel Laszczak 
115f7389572SPawel Laszczak 	cdns->wakeup_irq = platform_get_irq_byname_optional(pdev, "wakeup");
116f7389572SPawel Laszczak 	if (cdns->wakeup_irq == -EPROBE_DEFER)
117f7389572SPawel Laszczak 		return cdns->wakeup_irq;
118f7389572SPawel Laszczak 
119f7389572SPawel Laszczak 	if (cdns->wakeup_irq < 0) {
120f7389572SPawel Laszczak 		dev_dbg(dev, "couldn't get wakeup irq\n");
121f7389572SPawel Laszczak 		cdns->wakeup_irq = 0x0;
122f7389572SPawel Laszczak 	}
123f7389572SPawel Laszczak 
124f7389572SPawel Laszczak 	cdns->usb2_phy = devm_phy_optional_get(dev, "cdns3,usb2-phy");
125f7389572SPawel Laszczak 	if (IS_ERR(cdns->usb2_phy))
126*776630beSAlexander Stein 		return dev_err_probe(dev, PTR_ERR(cdns->usb2_phy),
127*776630beSAlexander Stein 				     "Failed to get cdn3,usb2-phy\n");
128f7389572SPawel Laszczak 
129f7389572SPawel Laszczak 	ret = phy_init(cdns->usb2_phy);
130f7389572SPawel Laszczak 	if (ret)
131f7389572SPawel Laszczak 		return ret;
132f7389572SPawel Laszczak 
133f7389572SPawel Laszczak 	cdns->usb3_phy = devm_phy_optional_get(dev, "cdns3,usb3-phy");
134f7389572SPawel Laszczak 	if (IS_ERR(cdns->usb3_phy))
135*776630beSAlexander Stein 		return dev_err_probe(dev, PTR_ERR(cdns->usb3_phy),
136*776630beSAlexander Stein 				     "Failed to get cdn3,usb3-phy\n");
137f7389572SPawel Laszczak 
138f7389572SPawel Laszczak 	ret = phy_init(cdns->usb3_phy);
139f7389572SPawel Laszczak 	if (ret)
140f7389572SPawel Laszczak 		goto err_phy3_init;
141f7389572SPawel Laszczak 
142f7389572SPawel Laszczak 	ret = set_phy_power_on(cdns);
143f7389572SPawel Laszczak 	if (ret)
144f7389572SPawel Laszczak 		goto err_phy_power_on;
145f7389572SPawel Laszczak 
146394c3a14SPawel Laszczak 	cdns->gadget_init = cdns3_gadget_init;
1470b490046SPawel Laszczak 
1480b490046SPawel Laszczak 	ret = cdns_init(cdns);
149f7389572SPawel Laszczak 	if (ret)
150f7389572SPawel Laszczak 		goto err_cdns_init;
151f7389572SPawel Laszczak 
152f7389572SPawel Laszczak 	device_set_wakeup_capable(dev, true);
153f7389572SPawel Laszczak 	pm_runtime_set_active(dev);
154f7389572SPawel Laszczak 	pm_runtime_enable(dev);
155f7389572SPawel Laszczak 	if (!(cdns->pdata && (cdns->pdata->quirks & CDNS3_DEFAULT_PM_RUNTIME_ALLOW)))
156f7389572SPawel Laszczak 		pm_runtime_forbid(dev);
157f7389572SPawel Laszczak 
158f7389572SPawel Laszczak 	/*
159f7389572SPawel Laszczak 	 * The controller needs less time between bus and controller suspend,
160f7389572SPawel Laszczak 	 * and we also needs a small delay to avoid frequently entering low
161f7389572SPawel Laszczak 	 * power mode.
162f7389572SPawel Laszczak 	 */
163f7389572SPawel Laszczak 	pm_runtime_set_autosuspend_delay(dev, 20);
164f7389572SPawel Laszczak 	pm_runtime_mark_last_busy(dev);
165f7389572SPawel Laszczak 	pm_runtime_use_autosuspend(dev);
166f7389572SPawel Laszczak 
167f7389572SPawel Laszczak 	return 0;
168f7389572SPawel Laszczak 
169f7389572SPawel Laszczak err_cdns_init:
170f7389572SPawel Laszczak 	set_phy_power_off(cdns);
171f7389572SPawel Laszczak err_phy_power_on:
172f7389572SPawel Laszczak 	phy_exit(cdns->usb3_phy);
173f7389572SPawel Laszczak err_phy3_init:
174f7389572SPawel Laszczak 	phy_exit(cdns->usb2_phy);
175f7389572SPawel Laszczak 
176f7389572SPawel Laszczak 	return ret;
177f7389572SPawel Laszczak }
178f7389572SPawel Laszczak 
179f7389572SPawel Laszczak /**
1806dd1efebSLee Jones  * cdns3_plat_remove() - unbind drd driver and clean up
181f7389572SPawel Laszczak  * @pdev: Pointer to Linux platform device
182f7389572SPawel Laszczak  *
183f7389572SPawel Laszczak  * Returns 0 on success otherwise negative errno
184f7389572SPawel Laszczak  */
cdns3_plat_remove(struct platform_device * pdev)1855411fa0eSUwe Kleine-König static void cdns3_plat_remove(struct platform_device *pdev)
186f7389572SPawel Laszczak {
1870b490046SPawel Laszczak 	struct cdns *cdns = platform_get_drvdata(pdev);
188f7389572SPawel Laszczak 	struct device *dev = cdns->dev;
189f7389572SPawel Laszczak 
190f7389572SPawel Laszczak 	pm_runtime_get_sync(dev);
191f7389572SPawel Laszczak 	pm_runtime_disable(dev);
192f7389572SPawel Laszczak 	pm_runtime_put_noidle(dev);
1930b490046SPawel Laszczak 	cdns_remove(cdns);
194f7389572SPawel Laszczak 	set_phy_power_off(cdns);
195f7389572SPawel Laszczak 	phy_exit(cdns->usb2_phy);
196f7389572SPawel Laszczak 	phy_exit(cdns->usb3_phy);
197f7389572SPawel Laszczak }
198f7389572SPawel Laszczak 
199f7389572SPawel Laszczak #ifdef CONFIG_PM
200f7389572SPawel Laszczak 
cdns3_set_platform_suspend(struct device * dev,bool suspend,bool wakeup)201f7389572SPawel Laszczak static int cdns3_set_platform_suspend(struct device *dev,
202f7389572SPawel Laszczak 				      bool suspend, bool wakeup)
203f7389572SPawel Laszczak {
2040b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
205f7389572SPawel Laszczak 	int ret = 0;
206f7389572SPawel Laszczak 
207f7389572SPawel Laszczak 	if (cdns->pdata && cdns->pdata->platform_suspend)
208f7389572SPawel Laszczak 		ret = cdns->pdata->platform_suspend(dev, suspend, wakeup);
209f7389572SPawel Laszczak 
210f7389572SPawel Laszczak 	return ret;
211f7389572SPawel Laszczak }
212f7389572SPawel Laszczak 
cdns3_controller_suspend(struct device * dev,pm_message_t msg)213f7389572SPawel Laszczak static int cdns3_controller_suspend(struct device *dev, pm_message_t msg)
214f7389572SPawel Laszczak {
2150b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
216f7389572SPawel Laszczak 	bool wakeup;
217f7389572SPawel Laszczak 	unsigned long flags;
218f7389572SPawel Laszczak 
219f7389572SPawel Laszczak 	if (cdns->in_lpm)
220f7389572SPawel Laszczak 		return 0;
221f7389572SPawel Laszczak 
222f7389572SPawel Laszczak 	if (PMSG_IS_AUTO(msg))
223f7389572SPawel Laszczak 		wakeup = true;
224f7389572SPawel Laszczak 	else
225f7389572SPawel Laszczak 		wakeup = device_may_wakeup(dev);
226f7389572SPawel Laszczak 
227f7389572SPawel Laszczak 	cdns3_set_platform_suspend(cdns->dev, true, wakeup);
228f7389572SPawel Laszczak 	set_phy_power_off(cdns);
229f7389572SPawel Laszczak 	spin_lock_irqsave(&cdns->lock, flags);
230f7389572SPawel Laszczak 	cdns->in_lpm = true;
231f7389572SPawel Laszczak 	spin_unlock_irqrestore(&cdns->lock, flags);
232f7389572SPawel Laszczak 	dev_dbg(cdns->dev, "%s ends\n", __func__);
233f7389572SPawel Laszczak 
234f7389572SPawel Laszczak 	return 0;
235f7389572SPawel Laszczak }
236f7389572SPawel Laszczak 
cdns3_controller_resume(struct device * dev,pm_message_t msg)237f7389572SPawel Laszczak static int cdns3_controller_resume(struct device *dev, pm_message_t msg)
238f7389572SPawel Laszczak {
2390b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
240f7389572SPawel Laszczak 	int ret;
241f7389572SPawel Laszczak 	unsigned long flags;
242f7389572SPawel Laszczak 
243f7389572SPawel Laszczak 	if (!cdns->in_lpm)
244f7389572SPawel Laszczak 		return 0;
245f7389572SPawel Laszczak 
2462cf2581cSFrank Li 	if (cdns_power_is_lost(cdns)) {
2472cf2581cSFrank Li 		phy_exit(cdns->usb2_phy);
2482cf2581cSFrank Li 		ret = phy_init(cdns->usb2_phy);
2492cf2581cSFrank Li 		if (ret)
2502cf2581cSFrank Li 			return ret;
2512cf2581cSFrank Li 
2522cf2581cSFrank Li 		phy_exit(cdns->usb3_phy);
2532cf2581cSFrank Li 		ret = phy_init(cdns->usb3_phy);
2542cf2581cSFrank Li 		if (ret)
2552cf2581cSFrank Li 			return ret;
2562cf2581cSFrank Li 	}
2572cf2581cSFrank Li 
258f7389572SPawel Laszczak 	ret = set_phy_power_on(cdns);
259f7389572SPawel Laszczak 	if (ret)
260f7389572SPawel Laszczak 		return ret;
261f7389572SPawel Laszczak 
262f7389572SPawel Laszczak 	cdns3_set_platform_suspend(cdns->dev, false, false);
263f7389572SPawel Laszczak 
264f7389572SPawel Laszczak 	spin_lock_irqsave(&cdns->lock, flags);
2652319b9c8SXiaolei Wang 	cdns_resume(cdns);
266f7389572SPawel Laszczak 	cdns->in_lpm = false;
267f7389572SPawel Laszczak 	spin_unlock_irqrestore(&cdns->lock, flags);
2682319b9c8SXiaolei Wang 	cdns_set_active(cdns, !PMSG_IS_AUTO(msg));
269f7389572SPawel Laszczak 	if (cdns->wakeup_pending) {
270f7389572SPawel Laszczak 		cdns->wakeup_pending = false;
271f7389572SPawel Laszczak 		enable_irq(cdns->wakeup_irq);
272f7389572SPawel Laszczak 	}
273f7389572SPawel Laszczak 	dev_dbg(cdns->dev, "%s ends\n", __func__);
274f7389572SPawel Laszczak 
275f7389572SPawel Laszczak 	return ret;
276f7389572SPawel Laszczak }
277f7389572SPawel Laszczak 
cdns3_plat_runtime_suspend(struct device * dev)278f7389572SPawel Laszczak static int cdns3_plat_runtime_suspend(struct device *dev)
279f7389572SPawel Laszczak {
280f7389572SPawel Laszczak 	return cdns3_controller_suspend(dev, PMSG_AUTO_SUSPEND);
281f7389572SPawel Laszczak }
282f7389572SPawel Laszczak 
cdns3_plat_runtime_resume(struct device * dev)283f7389572SPawel Laszczak static int cdns3_plat_runtime_resume(struct device *dev)
284f7389572SPawel Laszczak {
285f7389572SPawel Laszczak 	return cdns3_controller_resume(dev, PMSG_AUTO_RESUME);
286f7389572SPawel Laszczak }
287f7389572SPawel Laszczak 
288f7389572SPawel Laszczak #ifdef CONFIG_PM_SLEEP
289f7389572SPawel Laszczak 
cdns3_plat_suspend(struct device * dev)290f7389572SPawel Laszczak static int cdns3_plat_suspend(struct device *dev)
291f7389572SPawel Laszczak {
2920b490046SPawel Laszczak 	struct cdns *cdns = dev_get_drvdata(dev);
2932cf2581cSFrank Li 	int ret;
294f7389572SPawel Laszczak 
2950b490046SPawel Laszczak 	cdns_suspend(cdns);
296f7389572SPawel Laszczak 
2972cf2581cSFrank Li 	ret = cdns3_controller_suspend(dev, PMSG_SUSPEND);
2982cf2581cSFrank Li 	if (ret)
2992cf2581cSFrank Li 		return ret;
3002cf2581cSFrank Li 
3012cf2581cSFrank Li 	if (device_may_wakeup(dev) && cdns->wakeup_irq)
3022cf2581cSFrank Li 		enable_irq_wake(cdns->wakeup_irq);
3032cf2581cSFrank Li 
3042cf2581cSFrank Li 	return ret;
305f7389572SPawel Laszczak }
306f7389572SPawel Laszczak 
cdns3_plat_resume(struct device * dev)307f7389572SPawel Laszczak static int cdns3_plat_resume(struct device *dev)
308f7389572SPawel Laszczak {
309f7389572SPawel Laszczak 	return cdns3_controller_resume(dev, PMSG_RESUME);
310f7389572SPawel Laszczak }
311f7389572SPawel Laszczak #endif /* CONFIG_PM_SLEEP */
312f7389572SPawel Laszczak #endif /* CONFIG_PM */
313f7389572SPawel Laszczak 
314f7389572SPawel Laszczak static const struct dev_pm_ops cdns3_pm_ops = {
315f7389572SPawel Laszczak 	SET_SYSTEM_SLEEP_PM_OPS(cdns3_plat_suspend, cdns3_plat_resume)
316f7389572SPawel Laszczak 	SET_RUNTIME_PM_OPS(cdns3_plat_runtime_suspend,
317f7389572SPawel Laszczak 			   cdns3_plat_runtime_resume, NULL)
318f7389572SPawel Laszczak };
319f7389572SPawel Laszczak 
320f7389572SPawel Laszczak #ifdef CONFIG_OF
321f7389572SPawel Laszczak static const struct of_device_id of_cdns3_match[] = {
322f7389572SPawel Laszczak 	{ .compatible = "cdns,usb3" },
323f7389572SPawel Laszczak 	{ },
324f7389572SPawel Laszczak };
325f7389572SPawel Laszczak MODULE_DEVICE_TABLE(of, of_cdns3_match);
326f7389572SPawel Laszczak #endif
327f7389572SPawel Laszczak 
328f7389572SPawel Laszczak static struct platform_driver cdns3_driver = {
329f7389572SPawel Laszczak 	.probe		= cdns3_plat_probe,
3305411fa0eSUwe Kleine-König 	.remove_new	= cdns3_plat_remove,
331f7389572SPawel Laszczak 	.driver		= {
332f7389572SPawel Laszczak 		.name	= "cdns-usb3",
333f7389572SPawel Laszczak 		.of_match_table	= of_match_ptr(of_cdns3_match),
334f7389572SPawel Laszczak 		.pm	= &cdns3_pm_ops,
335f7389572SPawel Laszczak 	},
336f7389572SPawel Laszczak };
337f7389572SPawel Laszczak 
338f7389572SPawel Laszczak module_platform_driver(cdns3_driver);
339f7389572SPawel Laszczak 
340f7389572SPawel Laszczak MODULE_ALIAS("platform:cdns3");
341f7389572SPawel Laszczak MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
342f7389572SPawel Laszczak MODULE_LICENSE("GPL v2");
343f7389572SPawel Laszczak MODULE_DESCRIPTION("Cadence USB3 DRD Controller Driver");
344