Skip to contentSkip to conversation

phy: phy-imx8mq-usb: add vbus regulator support

PATCH Superseded Tim Harvey 2 participants 3 messages Jun 9, 2023

Subsystems (2)

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Tim Harvey

author
#0 lore raw

PATCH 1/1 phy: phy-imx8mq-usb: add vbus regulator support

Add support for enabling and disabling vbus-supply regulator found on several imx8mp boards in the usb3_phy0 and usb3_phy1 nodes.

Without this I suspect U-Boot usb does not work on the following:
- imx8mp-beacon-kit
- imx8mp-msc-sm2s
- imx8mp-verdin-wifi-dev

---
 drivers/phy/phy-imx8mq-usb.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
drivers/phy/phy-imx8mq-usb.c +22 -2
index 69f01de55538..eed9c07848f4 100644
@@ -14,6 +14,7 @@
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <clk.h>
+#include <power/regulator.h>
 
 #define PHY_CTRL0			0x0
 #define PHY_CTRL0_REF_SSP_EN		BIT(2)
@@ -81,6 +82,7 @@ struct imx8mq_usb_phy {
 #endif
 	void __iomem *base;
 	enum imx8mpq_phy_type type;
+	struct udevice *vbus_supply;
 };
 
 static const struct udevice_id imx8mq_usb_phy_of_match[] = {
@@ -173,9 +175,9 @@ static int imx8mq_usb_phy_power_on(struct phy *usb_phy)
 	struct udevice *dev = usb_phy->dev;
 	struct imx8mq_usb_phy *imx_phy = dev_get_priv(dev);
 	u32 value;
+	int ret;
 
 #if CONFIG_IS_ENABLED(CLK)
-	int ret;
 	ret = clk_enable(&imx_phy->phy_clk);
 	if (ret) {
 		printf("Failed to enable usb phy clock\n");
@@ -183,6 +185,12 @@ static int imx8mq_usb_phy_power_on(struct phy *usb_phy)
 	}
 #endif
 
+	if (CONFIG_IS_ENABLED(DM_REGULATOR) && imx_phy->vbus_supply) {
+		ret = regulator_set_enable(imx_phy->vbus_supply, true);
+		if (ret)
+			return ret;
+	}
+
 	/* Disable rx term override */
 	value = readl(imx_phy->base + PHY_CTRL6);
 	value &= ~PHY_CTRL6_RXTERM_OVERRIDE_SEL;
@@ -206,6 +214,9 @@ static int imx8mq_usb_phy_power_off(struct phy *usb_phy)
 	clk_disable(&imx_phy->phy_clk);
 #endif
 
+	if (CONFIG_IS_ENABLED(DM_REGULATOR) && imx_phy->vbus_supply)
+		return regulator_set_enable(imx_phy->vbus_supply, false);
+
 	return 0;
 }
 
@@ -224,6 +235,7 @@ struct phy_ops imx8mq_usb_phy_ops = {
 int imx8mq_usb_phy_probe(struct udevice *dev)
 {
 	struct imx8mq_usb_phy *priv = dev_get_priv(dev);
+	int ret;
 
 	priv->type = dev_get_driver_data(dev);
 	priv->base = dev_read_addr_ptr(dev);
@@ -232,7 +244,6 @@ int imx8mq_usb_phy_probe(struct udevice *dev)
 		return -EINVAL;
 
 #if CONFIG_IS_ENABLED(CLK)
-	int ret;
 
 	/* Assigned clock already set clock */
 	ret = clk_get_by_name(dev, "phy", &priv->phy_clk);
@@ -242,6 +253,15 @@ int imx8mq_usb_phy_probe(struct udevice *dev)
 	}
 #endif
 
+	if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
+		ret = device_get_supply_regulator(dev, "vbus-supply",
+						  &priv->vbus_supply);
+		if (ret && ret != -ENOENT) {
+			pr_err("Failed to get PHY regulator\n");
+			return ret;
+		}
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Tim Harvey

Add support for enabling and disabling vbus-supply regulator found on several imx8mp boards in the usb3_phy0 and usb3_phy1 nodes.

Without this I suspect U-Boot usb does not work on the following:
- imx8mp-beacon-kit

The Host-only port works on the Beacon board, but the dual-role, usb type-c port doesn't appear to have been impacted. I am guessing it's due to the lack of a proper type-c driver. Despite that, I think it's the right thing to do for this platform, and I'll give some feedback below.

Quoted 3 lines

- imx8mp-msc-sm2s
- imx8mp-verdin-wifi-dev

Quoted 26 lines
---
 drivers/phy/phy-imx8mq-usb.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
drivers/phy/phy-imx8mq-usb.c +2 -0
index 69f01de55538..eed9c07848f4 100644
@@ -14,6 +14,7 @@
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <clk.h>
+#include <power/regulator.h>
 
 #define PHY_CTRL0                      0x0
 #define PHY_CTRL0_REF_SSP_EN           BIT(2)
@@ -81,6 +82,7 @@ struct imx8mq_usb_phy {
 #endif
        void __iomem *base;
        enum imx8mpq_phy_type type;
+       struct udevice *vbus_supply;
 };
 
 static const struct udevice_id imx8mq_usb_phy_of_match[] = {

drivers/phy/phy-imx8mq-usb.c static int imx8mq_usb_phy_power_on(struct phy *usb_phy) · line 178

Quoted 4 lines
drivers/phy/phy-imx8mq-usb.c +1 -0
@@ -173,9 +175,9 @@ static int imx8mq_usb_phy_power_on(struct phy *usb_phy)
        struct udevice *dev = usb_phy->dev;
        struct imx8mq_usb_phy *imx_phy = dev_get_priv(dev);
        u32 value;
+       int ret;

In the unlikely event that neither CONFIG_CLK nor CONFIG_DM_REGULATOR is configured, will defining ret here throw a compiler warning that it's unused? I wonder if __maybe_unused attribute would be permissible here or if this should be inside an #if statement.

drivers/phy/phy-imx8mq-usb.c static int imx8mq_usb_phy_power_on(struct phy *usb_phy) · line 190

Quoted 13 lines
drivers/phy/phy-imx8mq-usb.c +3 -1
 #if CONFIG_IS_ENABLED(CLK)
-       int ret;
        ret = clk_enable(&imx_phy->phy_clk);
        if (ret) {
                printf("Failed to enable usb phy clock\n");
@@ -183,6 +185,12 @@ static int imx8mq_usb_phy_power_on(struct phy *usb_phy)
        }
 #endif
 
+       if (CONFIG_IS_ENABLED(DM_REGULATOR) && imx_phy->vbus_supply) {
+               ret = regulator_set_enable(imx_phy->vbus_supply, true);
+               if (ret)

I am personally a fan of error messages. There is an error message if the clock fails, so unless there is an objection, can we have one here too? One was also added to the probe function.

Quoted 17 lines
drivers/phy/phy-imx8mq-usb.c +6 -0
+                       return ret;
+       }
+
        /* Disable rx term override */
        value = readl(imx_phy->base + PHY_CTRL6);
        value &= ~PHY_CTRL6_RXTERM_OVERRIDE_SEL;
@@ -206,6 +214,9 @@ static int imx8mq_usb_phy_power_off(struct phy *usb_phy)
        clk_disable(&imx_phy->phy_clk);
 #endif
 
+       if (CONFIG_IS_ENABLED(DM_REGULATOR) && imx_phy->vbus_supply)
+               return regulator_set_enable(imx_phy->vbus_supply, false);
+
        return 0;
 }
 

drivers/phy/phy-imx8mq-usb.c struct phy_ops imx8mq_usb_phy_ops = { · line 238

Quoted 4 lines
drivers/phy/phy-imx8mq-usb.c +1 -0
@@ -224,6 +235,7 @@ struct phy_ops imx8mq_usb_phy_ops = {
 int imx8mq_usb_phy_probe(struct udevice *dev)
 {
        struct imx8mq_usb_phy *priv = dev_get_priv(dev);
+       int ret;

Same comment as above regarding whether or not this might be unused.

Quoted 30 lines
drivers/phy/phy-imx8mq-usb.c +9 -2
        priv->type = dev_get_driver_data(dev);
        priv->base = dev_read_addr_ptr(dev);
@@ -232,7 +244,6 @@ int imx8mq_usb_phy_probe(struct udevice *dev)
                return -EINVAL;
 
 #if CONFIG_IS_ENABLED(CLK)
-       int ret;
 
        /* Assigned clock already set clock */
        ret = clk_get_by_name(dev, "phy", &priv->phy_clk);
@@ -242,6 +253,15 @@ int imx8mq_usb_phy_probe(struct udevice *dev)
        }
 #endif
 
+       if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
+               ret = device_get_supply_regulator(dev, "vbus-supply",
+                                                 &priv->vbus_supply);
+               if (ret && ret != -ENOENT) {
+                       pr_err("Failed to get PHY regulator\n");
+                       return ret;
+               }
+       }
+
        return 0;
 }
 
--

2.25.1

author
#2 lore raw
Quoted 6 lines · Adam Ford
Quoted 4 lines · Tim Harvey

Add support for enabling and disabling vbus-supply regulator found on several imx8mp boards in the usb3_phy0 and usb3_phy1 nodes.

Quoted 8 lines
Quoted 2 lines

Without this I suspect U-Boot usb does not work on the following:
- imx8mp-beacon-kit

The Host-only port works on the Beacon board, but the dual-role, usb type-c port doesn't appear to have been impacted. I am guessing it's due to the lack of a proper type-c driver. Despite that, I think it's the right thing to do for this platform, and I'll give some feedback below.

Adam,

Is perhaps the pca6416 gpio0 pulled up externally or defaults to driving high? If you put a print in regulator_set_enable() you'll see that reg_usb1_host_vbus never gets called without this patch so I don't see how a device on that host would work.

Right - the type-c doesn't work because there is no driver for the ptn5110. Even if there were I'm not clear what the right mechanism is for calling out from the dwc3 driver to see what role the usb controller should be. I don't believe U-Boot already has any common method for usb host controllers to ask for role other than reading the dr_mode prop. Perhaps there needs to be a usb connector uclass added with a global function to check the role that various typec controller drivers could use. I have some imx8mp boards that have OTG connectors where I need to check the USB_ID gpio for the role which can be handled that way as well.

If you did want to default your type-c to host mode until such support exists you can add 'dr_mode = "host"' to the usb_dwc3_0 node in your imx8mp-beacon-kit-u-boot.dtsi:

Quoted 31 lines
Quoted 4 lines

- imx8mp-msc-sm2s
- imx8mp-verdin-wifi-dev

Quoted 24 lines
---
 drivers/phy/phy-imx8mq-usb.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
drivers/phy/phy-imx8mq-usb.c +2 -0
index 69f01de55538..eed9c07848f4 100644
@@ -14,6 +14,7 @@
 #include <linux/delay.h>
 #include <linux/err.h>
 #include <clk.h>
+#include <power/regulator.h>
 
 #define PHY_CTRL0                      0x0
 #define PHY_CTRL0_REF_SSP_EN           BIT(2)
@@ -81,6 +82,7 @@ struct imx8mq_usb_phy {
 #endif
        void __iomem *base;
        enum imx8mpq_phy_type type;
+       struct udevice *vbus_supply;
 };
 
Quoted 12 lines

drivers/phy/phy-imx8mq-usb.c static int imx8mq_usb_phy_power_on(struct phy *usb_phy) · line 178

Quoted 6 lines
drivers/phy/phy-imx8mq-usb.c +1 -0
 static const struct udevice_id imx8mq_usb_phy_of_match[] = {
@@ -173,9 +175,9 @@ static int imx8mq_usb_phy_power_on(struct phy *usb_phy)
        struct udevice *dev = usb_phy->dev;
        struct imx8mq_usb_phy *imx_phy = dev_get_priv(dev);
        u32 value;
+       int ret;

In the unlikely event that neither CONFIG_CLK nor CONFIG_DM_REGULATOR is configured, will defining ret here throw a compiler warning that it's unused? I wonder if __maybe_unused attribute would be permissible here or if this should be inside an #if statement.

good point, I'll add for v2

Quoted 10 lines
Quoted 10 lines
drivers/phy/phy-imx8mq-usb.c +0 -1
 #if CONFIG_IS_ENABLED(CLK)
-       int ret;
        ret = clk_enable(&imx_phy->phy_clk);
        if (ret) {
                printf("Failed to enable usb phy clock\n");
@@ -183,6 +185,12 @@ static int imx8mq_usb_phy_power_on(struct phy *usb_phy)
        }
 #endif
 
Quoted 7 lines

drivers/phy/phy-imx8mq-usb.c static int imx8mq_usb_phy_power_on(struct phy *usb_phy) · line 190

Quoted 3 lines
drivers/phy/phy-imx8mq-usb.c +3 -0
+       if (CONFIG_IS_ENABLED(DM_REGULATOR) && imx_phy->vbus_supply) {
+               ret = regulator_set_enable(imx_phy->vbus_supply, true);
+               if (ret)

I am personally a fan of error messages. There is an error message if the clock fails, so unless there is an objection, can we have one here too? One was also added to the probe function.

ok, I'll add this for v2

Quoted 17 lines
Quoted 17 lines
drivers/phy/phy-imx8mq-usb.c +6 -0
+                       return ret;
+       }
+
        /* Disable rx term override */
        value = readl(imx_phy->base + PHY_CTRL6);
        value &= ~PHY_CTRL6_RXTERM_OVERRIDE_SEL;
@@ -206,6 +214,9 @@ static int imx8mq_usb_phy_power_off(struct phy *usb_phy)
        clk_disable(&imx_phy->phy_clk);
 #endif
 
+       if (CONFIG_IS_ENABLED(DM_REGULATOR) && imx_phy->vbus_supply)
+               return regulator_set_enable(imx_phy->vbus_supply, false);
+
        return 0;
 }
 
Quoted 7 lines

drivers/phy/phy-imx8mq-usb.c struct phy_ops imx8mq_usb_phy_ops = { · line 238

Quoted 5 lines
drivers/phy/phy-imx8mq-usb.c +1 -0
@@ -224,6 +235,7 @@ struct phy_ops imx8mq_usb_phy_ops = {
 int imx8mq_usb_phy_probe(struct udevice *dev)
 {
        struct imx8mq_usb_phy *priv = dev_get_priv(dev);
+       int ret;

Same comment as above regarding whether or not this might be unused.

yep - will address.

Thanks for the review!

Tim