Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / drivers / usb / host / ehci-generic.c
index b012d8651f1239c2a4048d3d5369c3eb08e23d4d..304a3437d56cb60dcea097e44f1bafb5d719f184 100644 (file)
@@ -1,17 +1,20 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2015 Alexey Brodkin <abrodkin@synopsys.com>
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <clk.h>
+#include <log.h>
+#include <dm/device_compat.h>
+#include <dm/devres.h>
 #include <dm/ofnode.h>
 #include <generic-phy.h>
 #include <reset.h>
 #include <asm/io.h>
 #include <dm.h>
 #include "ehci.h"
+#include <power/regulator.h>
 
 /*
  * Even though here we don't explicitly use "struct ehci_ctrl"
@@ -23,59 +26,55 @@ struct generic_ehci {
        struct clk *clocks;
        struct reset_ctl *resets;
        struct phy phy;
+#ifdef CONFIG_DM_REGULATOR
+       struct udevice *vbus_supply;
+#endif
        int clock_count;
        int reset_count;
 };
 
-static int ehci_setup_phy(struct udevice *dev, int index)
+#ifdef CONFIG_DM_REGULATOR
+static int ehci_enable_vbus_supply(struct udevice *dev)
 {
        struct generic_ehci *priv = dev_get_priv(dev);
        int ret;
 
-       ret = generic_phy_get_by_index(dev, index, &priv->phy);
-       if (ret) {
-               if (ret != -ENOENT) {
-                       dev_err(dev, "failed to get usb phy\n");
-                       return ret;
-               }
-       } else {
-               ret = generic_phy_init(&priv->phy);
-               if (ret) {
-                       dev_err(dev, "failed to init usb phy\n");
-                       return ret;
-               }
+       ret = device_get_supply_regulator(dev, "vbus-supply",
+                                         &priv->vbus_supply);
+       if (ret && ret != -ENOENT)
+               return ret;
 
-               ret = generic_phy_power_on(&priv->phy);
+       if (priv->vbus_supply) {
+               ret = regulator_set_enable(priv->vbus_supply, true);
                if (ret) {
-                       dev_err(dev, "failed to power on usb phy\n");
-                       return generic_phy_exit(&priv->phy);
+                       dev_err(dev, "Error enabling VBUS supply\n");
+                       return ret;
                }
+       } else {
+               dev_dbg(dev, "No vbus supply\n");
        }
 
        return 0;
 }
 
-static int ehci_shutdown_phy(struct udevice *dev)
+static int ehci_disable_vbus_supply(struct generic_ehci *priv)
 {
-       struct generic_ehci *priv = dev_get_priv(dev);
-       int ret = 0;
-
-       if (generic_phy_valid(&priv->phy)) {
-               ret = generic_phy_power_off(&priv->phy);
-               if (ret) {
-                       dev_err(dev, "failed to power off usb phy\n");
-                       return ret;
-               }
-
-               ret = generic_phy_exit(&priv->phy);
-               if (ret) {
-                       dev_err(dev, "failed to power off usb phy\n");
-                       return ret;
-               }
-       }
+       if (priv->vbus_supply)
+               return regulator_set_enable(priv->vbus_supply, false);
+       else
+               return 0;
+}
+#else
+static int ehci_enable_vbus_supply(struct udevice *dev)
+{
+       return 0;
+}
 
+static int ehci_disable_vbus_supply(struct generic_ehci *priv)
+{
        return 0;
 }
+#endif
 
 static int ehci_usb_probe(struct udevice *dev)
 {
@@ -100,7 +99,7 @@ static int ehci_usb_probe(struct udevice *dev)
                        if (err < 0)
                                break;
                        err = clk_enable(&priv->clocks[i]);
-                       if (err) {
+                       if (err && err != -ENOSYS) {
                                dev_err(dev, "failed to enable clock %d\n", i);
                                clk_free(&priv->clocks[i]);
                                goto clk_err;
@@ -146,10 +145,14 @@ static int ehci_usb_probe(struct udevice *dev)
                }
        }
 
-       err = ehci_setup_phy(dev, 0);
+       err = ehci_enable_vbus_supply(dev);
        if (err)
                goto reset_err;
 
+       err = ehci_setup_phy(dev, &priv->phy, 0);
+       if (err)
+               goto regulator_err;
+
        hccr = map_physmem(dev_read_addr(dev), 0x100, MAP_NOCACHE);
        hcor = (struct ehci_hcor *)((uintptr_t)hccr +
                                    HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
@@ -161,10 +164,15 @@ static int ehci_usb_probe(struct udevice *dev)
        return 0;
 
 phy_err:
-       ret = ehci_shutdown_phy(dev);
+       ret = ehci_shutdown_phy(dev, &priv->phy);
        if (ret)
                dev_err(dev, "failed to shutdown usb phy\n");
 
+regulator_err:
+       ret = ehci_disable_vbus_supply(priv);
+       if (ret)
+               dev_err(dev, "failed to disable VBUS supply\n");
+
 reset_err:
        ret = reset_release_all(priv->resets, priv->reset_count);
        if (ret)
@@ -186,7 +194,11 @@ static int ehci_usb_remove(struct udevice *dev)
        if (ret)
                return ret;
 
-       ret = ehci_shutdown_phy(dev);
+       ret = ehci_shutdown_phy(dev, &priv->phy);
+       if (ret)
+               return ret;
+
+       ret = ehci_disable_vbus_supply(priv);
        if (ret)
                return ret;