Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * ST OHCI driver
0004  *
0005  * Copyright (C) 2014 STMicroelectronics – All Rights Reserved
0006  *
0007  * Author: Peter Griffin <peter.griffin@linaro.org>
0008  *
0009  * Derived from ohci-platform.c
0010  */
0011 
0012 #include <linux/clk.h>
0013 #include <linux/dma-mapping.h>
0014 #include <linux/hrtimer.h>
0015 #include <linux/io.h>
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/err.h>
0019 #include <linux/phy/phy.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/reset.h>
0022 #include <linux/usb/ohci_pdriver.h>
0023 #include <linux/usb.h>
0024 #include <linux/usb/hcd.h>
0025 
0026 #include "ohci.h"
0027 
0028 #define USB_MAX_CLKS 3
0029 
0030 struct st_ohci_platform_priv {
0031     struct clk *clks[USB_MAX_CLKS];
0032     struct clk *clk48;
0033     struct reset_control *rst;
0034     struct reset_control *pwr;
0035     struct phy *phy;
0036 };
0037 
0038 #define DRIVER_DESC "OHCI STMicroelectronics driver"
0039 
0040 #define hcd_to_ohci_priv(h) \
0041     ((struct st_ohci_platform_priv *)hcd_to_ohci(h)->priv)
0042 
0043 static const char hcd_name[] = "ohci-st";
0044 
0045 static int st_ohci_platform_power_on(struct platform_device *dev)
0046 {
0047     struct usb_hcd *hcd = platform_get_drvdata(dev);
0048     struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
0049     int clk, ret;
0050 
0051     ret = reset_control_deassert(priv->pwr);
0052     if (ret)
0053         return ret;
0054 
0055     ret = reset_control_deassert(priv->rst);
0056     if (ret)
0057         goto err_assert_power;
0058 
0059     /* some SoCs don't have a dedicated 48Mhz clock, but those that do
0060        need the rate to be explicitly set */
0061     if (priv->clk48) {
0062         ret = clk_set_rate(priv->clk48, 48000000);
0063         if (ret)
0064             goto err_assert_reset;
0065     }
0066 
0067     for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++) {
0068         ret = clk_prepare_enable(priv->clks[clk]);
0069         if (ret)
0070             goto err_disable_clks;
0071     }
0072 
0073     ret = phy_init(priv->phy);
0074     if (ret)
0075         goto err_disable_clks;
0076 
0077     ret = phy_power_on(priv->phy);
0078     if (ret)
0079         goto err_exit_phy;
0080 
0081     return 0;
0082 
0083 err_exit_phy:
0084     phy_exit(priv->phy);
0085 err_disable_clks:
0086     while (--clk >= 0)
0087         clk_disable_unprepare(priv->clks[clk]);
0088 err_assert_reset:
0089     reset_control_assert(priv->rst);
0090 err_assert_power:
0091     reset_control_assert(priv->pwr);
0092 
0093     return ret;
0094 }
0095 
0096 static void st_ohci_platform_power_off(struct platform_device *dev)
0097 {
0098     struct usb_hcd *hcd = platform_get_drvdata(dev);
0099     struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
0100 
0101     int clk;
0102 
0103     reset_control_assert(priv->pwr);
0104 
0105     reset_control_assert(priv->rst);
0106 
0107     phy_power_off(priv->phy);
0108 
0109     phy_exit(priv->phy);
0110 
0111     for (clk = USB_MAX_CLKS - 1; clk >= 0; clk--)
0112         if (priv->clks[clk])
0113             clk_disable_unprepare(priv->clks[clk]);
0114 }
0115 
0116 static struct hc_driver __read_mostly ohci_platform_hc_driver;
0117 
0118 static const struct ohci_driver_overrides platform_overrides __initconst = {
0119     .product_desc =     "ST OHCI controller",
0120     .extra_priv_size =  sizeof(struct st_ohci_platform_priv),
0121 };
0122 
0123 static struct usb_ohci_pdata ohci_platform_defaults = {
0124     .power_on =     st_ohci_platform_power_on,
0125     .power_suspend =    st_ohci_platform_power_off,
0126     .power_off =        st_ohci_platform_power_off,
0127 };
0128 
0129 static int st_ohci_platform_probe(struct platform_device *dev)
0130 {
0131     struct usb_hcd *hcd;
0132     struct resource *res_mem;
0133     struct usb_ohci_pdata *pdata = &ohci_platform_defaults;
0134     struct st_ohci_platform_priv *priv;
0135     int err, irq, clk = 0;
0136 
0137     if (usb_disabled())
0138         return -ENODEV;
0139 
0140     irq = platform_get_irq(dev, 0);
0141     if (irq < 0)
0142         return irq;
0143 
0144     res_mem = platform_get_resource(dev, IORESOURCE_MEM, 0);
0145     if (!res_mem) {
0146         dev_err(&dev->dev, "no memory resource provided");
0147         return -ENXIO;
0148     }
0149 
0150     hcd = usb_create_hcd(&ohci_platform_hc_driver, &dev->dev,
0151             dev_name(&dev->dev));
0152     if (!hcd)
0153         return -ENOMEM;
0154 
0155     platform_set_drvdata(dev, hcd);
0156     dev->dev.platform_data = pdata;
0157     priv = hcd_to_ohci_priv(hcd);
0158 
0159     priv->phy = devm_phy_get(&dev->dev, "usb");
0160     if (IS_ERR(priv->phy)) {
0161         err = PTR_ERR(priv->phy);
0162         goto err_put_hcd;
0163     }
0164 
0165     for (clk = 0; clk < USB_MAX_CLKS; clk++) {
0166         priv->clks[clk] = of_clk_get(dev->dev.of_node, clk);
0167         if (IS_ERR(priv->clks[clk])) {
0168             err = PTR_ERR(priv->clks[clk]);
0169             if (err == -EPROBE_DEFER)
0170                 goto err_put_clks;
0171             priv->clks[clk] = NULL;
0172             break;
0173         }
0174     }
0175 
0176     /* some SoCs don't have a dedicated 48Mhz clock, but those that
0177        do need the rate to be explicitly set */
0178     priv->clk48 = devm_clk_get(&dev->dev, "clk48");
0179     if (IS_ERR(priv->clk48)) {
0180         dev_info(&dev->dev, "48MHz clk not found\n");
0181         priv->clk48 = NULL;
0182     }
0183 
0184     priv->pwr =
0185         devm_reset_control_get_optional_shared(&dev->dev, "power");
0186     if (IS_ERR(priv->pwr)) {
0187         err = PTR_ERR(priv->pwr);
0188         goto err_put_clks;
0189     }
0190 
0191     priv->rst =
0192         devm_reset_control_get_optional_shared(&dev->dev, "softreset");
0193     if (IS_ERR(priv->rst)) {
0194         err = PTR_ERR(priv->rst);
0195         goto err_put_clks;
0196     }
0197 
0198     if (pdata->power_on) {
0199         err = pdata->power_on(dev);
0200         if (err < 0)
0201             goto err_power;
0202     }
0203 
0204     hcd->rsrc_start = res_mem->start;
0205     hcd->rsrc_len = resource_size(res_mem);
0206 
0207     hcd->regs = devm_ioremap_resource(&dev->dev, res_mem);
0208     if (IS_ERR(hcd->regs)) {
0209         err = PTR_ERR(hcd->regs);
0210         goto err_power;
0211     }
0212     err = usb_add_hcd(hcd, irq, IRQF_SHARED);
0213     if (err)
0214         goto err_power;
0215 
0216     device_wakeup_enable(hcd->self.controller);
0217 
0218     platform_set_drvdata(dev, hcd);
0219 
0220     return err;
0221 
0222 err_power:
0223     if (pdata->power_off)
0224         pdata->power_off(dev);
0225 
0226 err_put_clks:
0227     while (--clk >= 0)
0228         clk_put(priv->clks[clk]);
0229 err_put_hcd:
0230     if (pdata == &ohci_platform_defaults)
0231         dev->dev.platform_data = NULL;
0232 
0233     usb_put_hcd(hcd);
0234 
0235     return err;
0236 }
0237 
0238 static int st_ohci_platform_remove(struct platform_device *dev)
0239 {
0240     struct usb_hcd *hcd = platform_get_drvdata(dev);
0241     struct usb_ohci_pdata *pdata = dev_get_platdata(&dev->dev);
0242     struct st_ohci_platform_priv *priv = hcd_to_ohci_priv(hcd);
0243     int clk;
0244 
0245     usb_remove_hcd(hcd);
0246 
0247     if (pdata->power_off)
0248         pdata->power_off(dev);
0249 
0250 
0251     for (clk = 0; clk < USB_MAX_CLKS && priv->clks[clk]; clk++)
0252         clk_put(priv->clks[clk]);
0253 
0254     usb_put_hcd(hcd);
0255 
0256     if (pdata == &ohci_platform_defaults)
0257         dev->dev.platform_data = NULL;
0258 
0259     return 0;
0260 }
0261 
0262 #ifdef CONFIG_PM_SLEEP
0263 
0264 static int st_ohci_suspend(struct device *dev)
0265 {
0266     struct usb_hcd *hcd = dev_get_drvdata(dev);
0267     struct usb_ohci_pdata *pdata = dev->platform_data;
0268     struct platform_device *pdev = to_platform_device(dev);
0269     bool do_wakeup = device_may_wakeup(dev);
0270     int ret;
0271 
0272     ret = ohci_suspend(hcd, do_wakeup);
0273     if (ret)
0274         return ret;
0275 
0276     if (pdata->power_suspend)
0277         pdata->power_suspend(pdev);
0278 
0279     return ret;
0280 }
0281 
0282 static int st_ohci_resume(struct device *dev)
0283 {
0284     struct usb_hcd *hcd = dev_get_drvdata(dev);
0285     struct usb_ohci_pdata *pdata = dev_get_platdata(dev);
0286     struct platform_device *pdev = to_platform_device(dev);
0287     int err;
0288 
0289     if (pdata->power_on) {
0290         err = pdata->power_on(pdev);
0291         if (err < 0)
0292             return err;
0293     }
0294 
0295     ohci_resume(hcd, false);
0296     return 0;
0297 }
0298 
0299 static SIMPLE_DEV_PM_OPS(st_ohci_pm_ops, st_ohci_suspend, st_ohci_resume);
0300 
0301 #endif /* CONFIG_PM_SLEEP */
0302 
0303 static const struct of_device_id st_ohci_platform_ids[] = {
0304     { .compatible = "st,st-ohci-300x", },
0305     { /* sentinel */ }
0306 };
0307 MODULE_DEVICE_TABLE(of, st_ohci_platform_ids);
0308 
0309 static struct platform_driver ohci_platform_driver = {
0310     .probe      = st_ohci_platform_probe,
0311     .remove     = st_ohci_platform_remove,
0312     .shutdown   = usb_hcd_platform_shutdown,
0313     .driver     = {
0314         .name   = "st-ohci",
0315 #ifdef CONFIG_PM_SLEEP
0316         .pm = &st_ohci_pm_ops,
0317 #endif
0318         .of_match_table = st_ohci_platform_ids,
0319     }
0320 };
0321 
0322 static int __init ohci_platform_init(void)
0323 {
0324     if (usb_disabled())
0325         return -ENODEV;
0326 
0327     pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0328 
0329     ohci_init_driver(&ohci_platform_hc_driver, &platform_overrides);
0330     return platform_driver_register(&ohci_platform_driver);
0331 }
0332 module_init(ohci_platform_init);
0333 
0334 static void __exit ohci_platform_cleanup(void)
0335 {
0336     platform_driver_unregister(&ohci_platform_driver);
0337 }
0338 module_exit(ohci_platform_cleanup);
0339 
0340 MODULE_DESCRIPTION(DRIVER_DESC);
0341 MODULE_AUTHOR("Peter Griffin <peter.griffin@linaro.org>");
0342 MODULE_LICENSE("GPL");