1 // SPDX-License-Identifier: GPL-2.0
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>
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>
28 #define USB_MAX_CLKS 3
30 struct st_ohci_platform_priv {
31 struct clk *clks[USB_MAX_CLKS];
33 struct reset_control *rst;
34 struct reset_control *pwr;
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 int st_ohci_platform_power_on(struct platform_device *dev)
45 struct usb_hcd *hcd = platform_get_drvdata(dev);
46 struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
49 ret = reset_control_deassert(priv->pwr);
53 ret = reset_control_deassert(priv->rst);
55 goto err_assert_power;
57 /* some SoCs don't have a dedicated 48Mhz clock, but those that do
58 need the rate to be explicitly set */
60 ret = clk_set_rate(priv->clk48, 48000000);
62 goto err_assert_reset;
65 for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
66 ret = clk_prepare_enable(priv->clks[clk]);
68 goto err_disable_clks;
71 ret = phy_init(priv->phy);
73 goto err_disable_clks;
75 ret = phy_power_on(priv->phy);
85 clk_disable_unprepare(priv->clks[clk]);
87 reset_control_assert(priv->rst);
89 reset_control_assert(priv->pwr);
94 static void st_ohci_platform_power_off(struct platform_device *dev)
96 struct usb_hcd *hcd = platform_get_drvdata(dev);
97 struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
101 reset_control_assert(priv->pwr);
103 reset_control_assert(priv->rst);
105 phy_power_off(priv->phy);
109 for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
111 clk_disable_unprepare(priv->clks[clk]);
114 static struct hc_driver __read_mostly ohci_platform_hc_driver;
116 static const struct ohci_driver_overrides platform_overrides __initconst = {
117 .product_desc = "ST OHCI controller",
118 .extra_priv_size = sizeof(struct st_ohci_platform_priv),
121 static struct usb_ohci_pdata ohci_platform_defaults = {
122 .power_on = st_ohci_platform_power_on,
123 .power_suspend = st_ohci_platform_power_off,
124 .power_off = st_ohci_platform_power_off,
127 static int st_ohci_platform_probe(struct platform_device *dev)
130 struct resource *res_mem;
131 struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
132 struct st_ohci_platform_priv *priv;
133 int err, irq, clk = 0;
138 irq = platform_get_irq(dev, 0);
142 hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
143 dev_name(&dev->dev));
147 platform_set_drvdata(dev, hcd);
148 dev->dev.platform_data = pdata;
149 priv = hcd_to_ohci_priv(hcd);
151 priv->phy = devm_phy_get(&dev->dev, "usb");
152 if (IS_ERR(priv->phy)) {
153 err = PTR_ERR(priv->phy);
157 for (clk = 0; clk < USB_MAX_CLKS; clk++) {
158 priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
159 if (IS_ERR(priv->clks[clk])) {
160 err = PTR_ERR(priv->clks[clk]);
161 if (err == -EPROBE_DEFER)
163 priv->clks[clk] = NULL;
168 /* some SoCs don't have a dedicated 48Mhz clock, but those that
169 do need the rate to be explicitly set */
170 priv->clk48 = devm_clk_get(&dev->dev, "clk48");
171 if (IS_ERR(priv->clk48)) {
172 dev_info(&dev->dev, "48MHz clk not found\n");
177 devm_reset_control_get_optional_shared(&dev->dev, "power");
178 if (IS_ERR(priv->pwr)) {
179 err = PTR_ERR(priv->pwr);
184 devm_reset_control_get_optional_shared(&dev->dev, "softreset");
185 if (IS_ERR(priv->rst)) {
186 err = PTR_ERR(priv->rst);
190 if (pdata->power_on) {
191 err = pdata->power_on(dev);
196 hcd->regs = devm_platform_get_and_ioremap_resource(dev, 0, &res_mem);
197 if (IS_ERR(hcd->regs)) {
198 err = PTR_ERR(hcd->regs);
201 hcd->rsrc_start = res_mem->start;
202 hcd->rsrc_len = resource_size(res_mem);
204 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
208 device_wakeup_enable(hcd->self.controller);
210 platform_set_drvdata(dev, hcd);
215 if (pdata->power_off)
216 pdata->power_off(dev);
220 clk_put(priv->clks[clk]);
222 if (pdata == &ohci_platform_defaults)
223 dev->dev.platform_data = NULL;
230 static void st_ohci_platform_remove(struct platform_device *dev)
232 struct usb_hcd *hcd = platform_get_drvdata(dev);
233 struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
234 struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
239 if (pdata->power_off)
240 pdata->power_off(dev);
243 for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
244 clk_put(priv->clks[clk]);
248 if (pdata == &ohci_platform_defaults)
249 dev->dev.platform_data = NULL;
252 #ifdef CONFIG_PM_SLEEP
254 static int st_ohci_suspend(struct device *dev)
256 struct usb_hcd *hcd = dev_get_drvdata(dev);
257 struct usb_ohci_pdata *pdata = dev->platform_data;
258 struct platform_device *pdev = to_platform_device(dev);
259 bool do_wakeup = device_may_wakeup(dev);
262 ret = ohci_suspend(hcd, do_wakeup);
266 if (pdata->power_suspend)
267 pdata->power_suspend(pdev);
272 static int st_ohci_resume(struct device *dev)
274 struct usb_hcd *hcd = dev_get_drvdata(dev);
275 struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
276 struct platform_device *pdev = to_platform_device(dev);
279 if (pdata->power_on) {
280 err = pdata->power_on(pdev);
285 ohci_resume(hcd, false);
289 static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
291 #endif /* CONFIG_PM_SLEEP */
293 static const struct of_device_id st_ohci_platform_ids[] = {
294 { .compatible = "st,st-ohci-300x", },
297 MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
299 static struct platform_driver ohci_platform_driver = {
300 .probe = st_ohci_platform_probe,
301 .remove_new = st_ohci_platform_remove,
302 .shutdown = usb_hcd_platform_shutdown,
305 #ifdef CONFIG_PM_SLEEP
306 .pm = &st_ohci_pm_ops,
308 .of_match_table = st_ohci_platform_ids,
312 static int __init ohci_platform_init(void)
317 ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
318 return platform_driver_register(&ohci_platform_driver);
320 module_init(ohci_platform_init);
322 static void __exit ohci_platform_cleanup(void)
324 platform_driver_unregister(&ohci_platform_driver);
326 module_exit(ohci_platform_cleanup);
328 MODULE_DESCRIPTION(DRIVER_DESC);
329 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
330 MODULE_LICENSE("GPL");