Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / usb / host / ohci-st.c
blobac796ccd93ef45e3a1b233b1cb53c4b49cb0dfa7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * ST OHCI driver
5 * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
7 * Author: Peter Griffin <peter.griffin@linaro.org>
9 * Derived from ohci-platform.c
12 #include <linux/clk.h>
13 #include <linux/dma-mapping.h>
14 #include <linux/hrtimer.h>
15 #include <linux/io.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/err.h>
19 #include <linux/phy/phy.h>
20 #include <linux/platform_device.h>
21 #include <linux/reset.h>
22 #include <linux/usb/ohci_pdriver.h>
23 #include <linux/usb.h>
24 #include <linux/usb/hcd.h>
26 #include "ohci.h"
28 #define USB_MAX_CLKS 3
30 struct st_ohci_platform_priv {
31 struct clk *clks[USB_MAX_CLKS];
32 struct clk *clk48;
33 struct reset_control *rst;
34 struct reset_control *pwr;
35 struct phy *phy;
38 #define DRIVER_DESC "OHCI STMicroelectronics driver"
40 #define hcd_to_ohci_priv(h) \
41 ((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv)
43 static const char hcd_name[] = "ohci-st";
45 static int st_ohci_platform_power_on(struct platform_device *dev)
47 struct usb_hcd *hcd = platform_get_drvdata(dev);
48 struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
49 int clk, ret;
51 ret = reset_control_deassert(priv->pwr);
52 if (ret)
53 return ret;
55 ret = reset_control_deassert(priv->rst);
56 if (ret)
57 goto err_assert_power;
59 /* some SoCs don't have a dedicated 48Mhz clock, but those that do
60 need the rate to be explicitly set */
61 if (priv->clk48) {
62 ret = clk_set_rate(priv->clk48, 48000000);
63 if (ret)
64 goto err_assert_reset;
67 for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
68 ret = clk_prepare_enable(priv->clks[clk]);
69 if (ret)
70 goto err_disable_clks;
73 ret = phy_init(priv->phy);
74 if (ret)
75 goto err_disable_clks;
77 ret = phy_power_on(priv->phy);
78 if (ret)
79 goto err_exit_phy;
81 return 0;
83 err_exit_phy:
84 phy_exit(priv->phy);
85 err_disable_clks:
86 while (--clk >= 0)
87 clk_disable_unprepare(priv->clks[clk]);
88 err_assert_reset:
89 reset_control_assert(priv->rst);
90 err_assert_power:
91 reset_control_assert(priv->pwr);
93 return ret;
96 static void st_ohci_platform_power_off(struct platform_device *dev)
98 struct usb_hcd *hcd = platform_get_drvdata(dev);
99 struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
101 int clk;
103 reset_control_assert(priv->pwr);
105 reset_control_assert(priv->rst);
107 phy_power_off(priv->phy);
109 phy_exit(priv->phy);
111 for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
112 if (priv->clks[clk])
113 clk_disable_unprepare(priv->clks[clk]);
116 static struct hc_driver __read_mostly ohci_platform_hc_driver;
118 static const struct ohci_driver_overrides platform_overrides __initconst = {
119 .product_desc = "ST OHCI controller",
120 .extra_priv_size = sizeof(struct st_ohci_platform_priv),
123 static struct usb_ohci_pdata ohci_platform_defaults = {
124 .power_on = st_ohci_platform_power_on,
125 .power_suspend = st_ohci_platform_power_off,
126 .power_off = st_ohci_platform_power_off,
129 static int st_ohci_platform_probe(struct platform_device *dev)
131 struct usb_hcd *hcd;
132 struct resource *res_mem;
133 struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
134 struct st_ohci_platform_priv *priv;
135 int err, irq, clk = 0;
137 if (usb_disabled())
138 return -ENODEV;
140 irq = platform_get_irq(dev, 0);
141 if (irq < 0)
142 return irq;
144 res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
145 if (!res_mem) {
146 dev_err(&dev->dev, "no memory resource provided");
147 return -ENXIO;
150 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
151 dev_name(&dev->dev));
152 if (!hcd)
153 return -ENOMEM;
155 platform_set_drvdata(dev, hcd);
156 dev->dev.platform_data = pdata;
157 priv = hcd_to_ohci_priv(hcd);
159 priv->phy = devm_phy_get(&dev->dev, "usb");
160 if (IS_ERR(priv->phy)) {
161 err = PTR_ERR(priv->phy);
162 goto err_put_hcd;
165 for (clk = 0; clk < USB_MAX_CLKS; clk++) {
166 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
167 if (IS_ERR(priv->clks[clk])) {
168 err = PTR_ERR(priv->clks[clk]);
169 if (err == -EPROBE_DEFER)
170 goto err_put_clks;
171 priv->clks[clk] = NULL;
172 break;
176 /* some SoCs don't have a dedicated 48Mhz clock, but those that
177 do need the rate to be explicitly set */
178 priv->clk48 = devm_clk_get(&dev->dev, "clk48");
179 if (IS_ERR(priv->clk48)) {
180 dev_info(&dev->dev, "48MHz clk not found\n");
181 priv->clk48 = NULL;
184 priv->pwr =
185 devm_reset_control_get_optional_shared(&dev->dev, "power");
186 if (IS_ERR(priv->pwr)) {
187 err = PTR_ERR(priv->pwr);
188 goto err_put_clks;
191 priv->rst =
192 devm_reset_control_get_optional_shared(&dev->dev, "softreset");
193 if (IS_ERR(priv->rst)) {
194 err = PTR_ERR(priv->rst);
195 goto err_put_clks;
198 if (pdata->power_on) {
199 err = pdata->power_on(dev);
200 if (err < 0)
201 goto err_power;
204 hcd->rsrc_start = res_mem->start;
205 hcd->rsrc_len = resource_size(res_mem);
207 hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
208 if (IS_ERR(hcd->regs)) {
209 err = PTR_ERR(hcd->regs);
210 goto err_power;
212 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
213 if (err)
214 goto err_power;
216 device_wakeup_enable(hcd->self.controller);
218 platform_set_drvdata(dev, hcd);
220 return err;
222 err_power:
223 if (pdata->power_off)
224 pdata->power_off(dev);
226 err_put_clks:
227 while (--clk >= 0)
228 clk_put(priv->clks[clk]);
229 err_put_hcd:
230 if (pdata == &ohci_platform_defaults)
231 dev->dev.platform_data = NULL;
233 usb_put_hcd(hcd);
235 return err;
238 static int st_ohci_platform_remove(struct platform_device *dev)
240 struct usb_hcd *hcd = platform_get_drvdata(dev);
241 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
242 struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
243 int clk;
245 usb_remove_hcd(hcd);
247 if (pdata->power_off)
248 pdata->power_off(dev);
251 for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
252 clk_put(priv->clks[clk]);
254 usb_put_hcd(hcd);
256 if (pdata == &ohci_platform_defaults)
257 dev->dev.platform_data = NULL;
259 return 0;
262 #ifdef CONFIG_PM_SLEEP
264 static int st_ohci_suspend(struct device *dev)
266 struct usb_hcd *hcd = dev_get_drvdata(dev);
267 struct usb_ohci_pdata *pdata = dev->platform_data;
268 struct platform_device *pdev = to_platform_device(dev);
269 bool do_wakeup = device_may_wakeup(dev);
270 int ret;
272 ret = ohci_suspend(hcd, do_wakeup);
273 if (ret)
274 return ret;
276 if (pdata->power_suspend)
277 pdata->power_suspend(pdev);
279 return ret;
282 static int st_ohci_resume(struct device *dev)
284 struct usb_hcd *hcd = dev_get_drvdata(dev);
285 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
286 struct platform_device *pdev = to_platform_device(dev);
287 int err;
289 if (pdata->power_on) {
290 err = pdata->power_on(pdev);
291 if (err < 0)
292 return err;
295 ohci_resume(hcd, false);
296 return 0;
299 static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
301 #endif /* CONFIG_PM_SLEEP */
303 static const struct of_device_id st_ohci_platform_ids[] = {
304 { .compatible = "st,st-ohci-300x", },
305 { /* sentinel */ }
307 MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
309 static struct platform_driver ohci_platform_driver = {
310 .probe = st_ohci_platform_probe,
311 .remove = st_ohci_platform_remove,
312 .shutdown = usb_hcd_platform_shutdown,
313 .driver = {
314 .name = "st-ohci",
315 #ifdef CONFIG_PM_SLEEP
316 .pm = &st_ohci_pm_ops,
317 #endif
318 .of_match_table = st_ohci_platform_ids,
322 static int __init ohci_platform_init(void)
324 if (usb_disabled())
325 return -ENODEV;
327 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
329 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
330 return platform_driver_register(&ohci_platform_driver);
332 module_init(ohci_platform_init);
334 static void __exit ohci_platform_cleanup(void)
336 platform_driver_unregister(&ohci_platform_driver);
338 module_exit(ohci_platform_cleanup);
340 MODULE_DESCRIPTION(DRIVER_DESC);
341 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
342 MODULE_LICENSE("GPL");