linux/drivers/usb/host/ohci-exynos.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * SAMSUNG EXYNOS USB HOST OHCI Controller
   4 *
   5 * Copyright (C) 2011 Samsung Electronics Co.Ltd
   6 * Author: Jingoo Han <jg1.han@samsung.com>
   7 */
   8
   9#include <linux/clk.h>
  10#include <linux/dma-mapping.h>
  11#include <linux/io.h>
  12#include <linux/kernel.h>
  13#include <linux/module.h>
  14#include <linux/of.h>
  15#include <linux/platform_device.h>
  16#include <linux/phy/phy.h>
  17#include <linux/usb.h>
  18#include <linux/usb/hcd.h>
  19
  20#include "ohci.h"
  21
  22#define DRIVER_DESC "OHCI EXYNOS driver"
  23
  24static const char hcd_name[] = "ohci-exynos";
  25static struct hc_driver __read_mostly exynos_ohci_hc_driver;
  26
  27#define to_exynos_ohci(hcd) (struct exynos_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
  28
  29#define PHY_NUMBER 3
  30
  31struct exynos_ohci_hcd {
  32        struct clk *clk;
  33        struct device_node *of_node;
  34        struct phy *phy[PHY_NUMBER];
  35};
  36
  37static int exynos_ohci_get_phy(struct device *dev,
  38                                struct exynos_ohci_hcd *exynos_ohci)
  39{
  40        struct device_node *child;
  41        struct phy *phy;
  42        int phy_number;
  43        int ret;
  44
  45        /* Get PHYs for the controller */
  46        for_each_available_child_of_node(dev->of_node, child) {
  47                ret = of_property_read_u32(child, "reg", &phy_number);
  48                if (ret) {
  49                        dev_err(dev, "Failed to parse device tree\n");
  50                        of_node_put(child);
  51                        return ret;
  52                }
  53
  54                if (phy_number >= PHY_NUMBER) {
  55                        dev_err(dev, "Invalid number of PHYs\n");
  56                        of_node_put(child);
  57                        return -EINVAL;
  58                }
  59
  60                phy = devm_of_phy_get(dev, child, NULL);
  61                exynos_ohci->phy[phy_number] = phy;
  62                if (IS_ERR(phy)) {
  63                        ret = PTR_ERR(phy);
  64                        if (ret == -EPROBE_DEFER) {
  65                                of_node_put(child);
  66                                return ret;
  67                        } else if (ret != -ENOSYS && ret != -ENODEV) {
  68                                dev_err(dev,
  69                                        "Error retrieving usb2 phy: %d\n", ret);
  70                                of_node_put(child);
  71                                return ret;
  72                        }
  73                }
  74        }
  75
  76        return 0;
  77}
  78
  79static int exynos_ohci_phy_enable(struct device *dev)
  80{
  81        struct usb_hcd *hcd = dev_get_drvdata(dev);
  82        struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
  83        int i;
  84        int ret = 0;
  85
  86        for (i = 0; ret == 0 && i < PHY_NUMBER; i++)
  87                if (!IS_ERR(exynos_ohci->phy[i]))
  88                        ret = phy_power_on(exynos_ohci->phy[i]);
  89        if (ret)
  90                for (i--; i >= 0; i--)
  91                        if (!IS_ERR(exynos_ohci->phy[i]))
  92                                phy_power_off(exynos_ohci->phy[i]);
  93
  94        return ret;
  95}
  96
  97static void exynos_ohci_phy_disable(struct device *dev)
  98{
  99        struct usb_hcd *hcd = dev_get_drvdata(dev);
 100        struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
 101        int i;
 102
 103        for (i = 0; i < PHY_NUMBER; i++)
 104                if (!IS_ERR(exynos_ohci->phy[i]))
 105                        phy_power_off(exynos_ohci->phy[i]);
 106}
 107
 108static int exynos_ohci_probe(struct platform_device *pdev)
 109{
 110        struct exynos_ohci_hcd *exynos_ohci;
 111        struct usb_hcd *hcd;
 112        struct resource *res;
 113        int irq;
 114        int err;
 115
 116        /*
 117         * Right now device-tree probed devices don't get dma_mask set.
 118         * Since shared usb code relies on it, set it here for now.
 119         * Once we move to full device tree support this will vanish off.
 120         */
 121        err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
 122        if (err)
 123                return err;
 124
 125        hcd = usb_create_hcd(&exynos_ohci_hc_driver,
 126                                &pdev->dev, dev_name(&pdev->dev));
 127        if (!hcd) {
 128                dev_err(&pdev->dev, "Unable to create HCD\n");
 129                return -ENOMEM;
 130        }
 131
 132        exynos_ohci = to_exynos_ohci(hcd);
 133
 134        err = exynos_ohci_get_phy(&pdev->dev, exynos_ohci);
 135        if (err)
 136                goto fail_clk;
 137
 138        exynos_ohci->clk = devm_clk_get(&pdev->dev, "usbhost");
 139
 140        if (IS_ERR(exynos_ohci->clk)) {
 141                dev_err(&pdev->dev, "Failed to get usbhost clock\n");
 142                err = PTR_ERR(exynos_ohci->clk);
 143                goto fail_clk;
 144        }
 145
 146        err = clk_prepare_enable(exynos_ohci->clk);
 147        if (err)
 148                goto fail_clk;
 149
 150        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 151        hcd->regs = devm_ioremap_resource(&pdev->dev, res);
 152        if (IS_ERR(hcd->regs)) {
 153                err = PTR_ERR(hcd->regs);
 154                goto fail_io;
 155        }
 156        hcd->rsrc_start = res->start;
 157        hcd->rsrc_len = resource_size(res);
 158
 159        irq = platform_get_irq(pdev, 0);
 160        if (!irq) {
 161                dev_err(&pdev->dev, "Failed to get IRQ\n");
 162                err = -ENODEV;
 163                goto fail_io;
 164        }
 165
 166        platform_set_drvdata(pdev, hcd);
 167
 168        err = exynos_ohci_phy_enable(&pdev->dev);
 169        if (err) {
 170                dev_err(&pdev->dev, "Failed to enable USB phy\n");
 171                goto fail_io;
 172        }
 173
 174        /*
 175         * Workaround: reset of_node pointer to avoid conflict between Exynos
 176         * OHCI port subnodes and generic USB device bindings
 177         */
 178        exynos_ohci->of_node = pdev->dev.of_node;
 179        pdev->dev.of_node = NULL;
 180
 181        err = usb_add_hcd(hcd, irq, IRQF_SHARED);
 182        if (err) {
 183                dev_err(&pdev->dev, "Failed to add USB HCD\n");
 184                goto fail_add_hcd;
 185        }
 186        device_wakeup_enable(hcd->self.controller);
 187        return 0;
 188
 189fail_add_hcd:
 190        exynos_ohci_phy_disable(&pdev->dev);
 191        pdev->dev.of_node = exynos_ohci->of_node;
 192fail_io:
 193        clk_disable_unprepare(exynos_ohci->clk);
 194fail_clk:
 195        usb_put_hcd(hcd);
 196        return err;
 197}
 198
 199static int exynos_ohci_remove(struct platform_device *pdev)
 200{
 201        struct usb_hcd *hcd = platform_get_drvdata(pdev);
 202        struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
 203
 204        pdev->dev.of_node = exynos_ohci->of_node;
 205
 206        usb_remove_hcd(hcd);
 207
 208        exynos_ohci_phy_disable(&pdev->dev);
 209
 210        clk_disable_unprepare(exynos_ohci->clk);
 211
 212        usb_put_hcd(hcd);
 213
 214        return 0;
 215}
 216
 217static void exynos_ohci_shutdown(struct platform_device *pdev)
 218{
 219        struct usb_hcd *hcd = platform_get_drvdata(pdev);
 220
 221        if (hcd->driver->shutdown)
 222                hcd->driver->shutdown(hcd);
 223}
 224
 225#ifdef CONFIG_PM
 226static int exynos_ohci_suspend(struct device *dev)
 227{
 228        struct usb_hcd *hcd = dev_get_drvdata(dev);
 229        struct exynos_ohci_hcd *exynos_ohci = to_exynos_ohci(hcd);
 230        bool do_wakeup = device_may_wakeup(dev);
 231        int rc = ohci_suspend(hcd, do_wakeup);
 232
 233        if (rc)
 234                return rc;
 235
 236        exynos_ohci_phy_disable(dev);
 237
 238        clk_disable_unprepare(exynos_ohci->clk);
 239
 240        return 0;
 241}
 242
 243static int exynos_ohci_resume(struct device *dev)
 244{
 245        struct usb_hcd *hcd                     = dev_get_drvdata(dev);
 246        struct exynos_ohci_hcd *exynos_ohci     = to_exynos_ohci(hcd);
 247        int ret;
 248
 249        clk_prepare_enable(exynos_ohci->clk);
 250
 251        ret = exynos_ohci_phy_enable(dev);
 252        if (ret) {
 253                dev_err(dev, "Failed to enable USB phy\n");
 254                clk_disable_unprepare(exynos_ohci->clk);
 255                return ret;
 256        }
 257
 258        ohci_resume(hcd, false);
 259
 260        return 0;
 261}
 262#else
 263#define exynos_ohci_suspend     NULL
 264#define exynos_ohci_resume      NULL
 265#endif
 266
 267static const struct ohci_driver_overrides exynos_overrides __initconst = {
 268        .extra_priv_size =      sizeof(struct exynos_ohci_hcd),
 269};
 270
 271static const struct dev_pm_ops exynos_ohci_pm_ops = {
 272        .suspend        = exynos_ohci_suspend,
 273        .resume         = exynos_ohci_resume,
 274};
 275
 276#ifdef CONFIG_OF
 277static const struct of_device_id exynos_ohci_match[] = {
 278        { .compatible = "samsung,exynos4210-ohci" },
 279        {},
 280};
 281MODULE_DEVICE_TABLE(of, exynos_ohci_match);
 282#endif
 283
 284static struct platform_driver exynos_ohci_driver = {
 285        .probe          = exynos_ohci_probe,
 286        .remove         = exynos_ohci_remove,
 287        .shutdown       = exynos_ohci_shutdown,
 288        .driver = {
 289                .name   = "exynos-ohci",
 290                .pm     = &exynos_ohci_pm_ops,
 291                .of_match_table = of_match_ptr(exynos_ohci_match),
 292        }
 293};
 294static int __init ohci_exynos_init(void)
 295{
 296        if (usb_disabled())
 297                return -ENODEV;
 298
 299        pr_info("%s: " DRIVER_DESC "\n", hcd_name);
 300        ohci_init_driver(&exynos_ohci_hc_driver, &exynos_overrides);
 301        return platform_driver_register(&exynos_ohci_driver);
 302}
 303module_init(ohci_exynos_init);
 304
 305static void __exit ohci_exynos_cleanup(void)
 306{
 307        platform_driver_unregister(&exynos_ohci_driver);
 308}
 309module_exit(ohci_exynos_cleanup);
 310
 311MODULE_ALIAS("platform:exynos-ohci");
 312MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
 313MODULE_LICENSE("GPL v2");
 314