Skip to contentSkip to conversation

phy: sun4i-usb: Support VBUS detection via power supply

PATCH In review Samuel Holland 2 participants 7 messages Sep 12, 2021 – Oct 25, 2021

Subsystems (2)
lore
Share & tools
lore

Samuel Holland

author maintainer
#0 lore raw

cover phy: sun4i-usb: Support VBUS detection via power supply

This converts a hack (a virtual GPIO pin) to less of a hack (not-really a regulator), but at least it now supports the device tree. This series adds the consumer side. The new code won't do anything useful until the regulator driver is merged (which depends on the DM_PMIC driver), but it shouldn't hurt anything either.

Samuel Holland (3):
phy: sun4i-usb: Remove a couple of debug messages
phy: sun4i-usb: Refactor VBUS detection to match Linux
phy: sun4i-usb: Support VBUS detection via power supply

 drivers/phy/allwinner/Kconfig         |  1 +
 drivers/phy/allwinner/phy-sun4i-usb.c | 34 ++++++++++++++-------------
 2 files changed, 19 insertions(+), 16 deletions(-)
Signature · 1 line
2.31.1
author maintainer
#1 lore raw

PATCH 1/3 phy: sun4i-usb: Remove a couple of debug messages

Both of these messages log the GPIO number of the ID detection GPIO, which is not terribly useful, especially in the VBUS detection function.

---

 drivers/phy/allwinner/phy-sun4i-usb.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/phy/allwinner/phy-sun4i-usb.c +0 -4
index 82713b83815..5302b809ee6 100644
@@ -393,8 +393,6 @@ int sun4i_usb_phy_vbus_detect(struct phy *phy)
 	struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
 	int err, retries = 3;
 
-	debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
-
 	if (usb_phy->gpio_vbus_det < 0)
 		return usb_phy->gpio_vbus_det;
 
@@ -417,8 +415,6 @@ int sun4i_usb_phy_id_detect(struct phy *phy)
 	struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
 	struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
 
-	debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
-
 	if (usb_phy->gpio_id_det < 0)
 		return usb_phy->gpio_id_det;
 
Signature · 1 line
2.31.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] phy: sun4i-usb: Remove a couple of debug messages

Quoted 4 lines · On Sun, 12 Sep 2021 09:22:40 -0500 Samuel Holland <[email protected]> wrote:

Both of these messages log the GPIO number of the ID detection GPIO, which is not terribly useful, especially in the VBUS detection function.

Cheers,
Andre

Quoted 27 lines

---

 drivers/phy/allwinner/phy-sun4i-usb.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/phy/allwinner/phy-sun4i-usb.c +0 -4
index 82713b83815..5302b809ee6 100644
@@ -393,8 +393,6 @@ int sun4i_usb_phy_vbus_detect(struct phy *phy)
 	struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
 	int err, retries = 3;
 
-	debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
-
 	if (usb_phy->gpio_vbus_det < 0)
 		return usb_phy->gpio_vbus_det;
 
@@ -417,8 +415,6 @@ int sun4i_usb_phy_id_detect(struct phy *phy)
 	struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
 	struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
 
-	debug("%s: id_det = %d\n", __func__, usb_phy->gpio_id_det);
-
 	if (usb_phy->gpio_id_det < 0)
 		return usb_phy->gpio_id_det;
 
author maintainer
#3 lore raw

PATCH 2/3 phy: sun4i-usb: Refactor VBUS detection to match Linux

In reply to Samuel Holland

The Linux driver checks the VBUS detection GPIO first; then VBUS power supply; then finally assumes VBUS is present. When adding VBUS power supply support, we want to match that order, so we get the same behavior in case both a GPIO and a power supply are provided in the device tree.

So refactor the function a bit to remove the early return, and use the same "assume VBUS is present" final fallback.

---

 drivers/phy/allwinner/phy-sun4i-usb.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/phy/allwinner/phy-sun4i-usb.c +12 -13
index 5302b809ee6..827ecd70f27 100644
@@ -391,20 +391,19 @@ int sun4i_usb_phy_vbus_detect(struct phy *phy)
 {
 	struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
 	struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
-	int err, retries = 3;
-
-	if (usb_phy->gpio_vbus_det < 0)
-		return usb_phy->gpio_vbus_det;
-
-	err = gpio_get_value(usb_phy->gpio_vbus_det);
-	/*
-	 * Vbus may have been provided by the board and just been turned of
-	 * some milliseconds ago on reset, what we're measuring then is a
-	 * residual charge on Vbus, sleep a bit and try again.
-	 */
-	while (err > 0 && retries--) {
-		mdelay(100);
+	int err = 1, retries = 3;
+
+	if (usb_phy->gpio_vbus_det >= 0) {
 		err = gpio_get_value(usb_phy->gpio_vbus_det);
+		/*
+		 * Vbus may have been provided by the board and just turned off
+		 * some milliseconds ago on reset. What we're measuring then is
+		 * a residual charge on Vbus. Sleep a bit and try again.
+		 */
+		while (err > 0 && retries--) {
+			mdelay(100);
+			err = gpio_get_value(usb_phy->gpio_vbus_det);
+		}
 	}
 
 	return err;
Signature · 1 line
2.31.1
Acked-by on v1 2/3
#4 lore raw

Re: [PATCH 2/3] phy: sun4i-usb: Refactor VBUS detection to match Linux

On Sun, 12 Sep 2021 09:22:41 -0500
Samuel Holland <[email protected]> wrote:

Hi,

Quoted 9 lines

The Linux driver checks the VBUS detection GPIO first; then VBUS power supply; then finally assumes VBUS is present. When adding VBUS power supply support, we want to match that order, so we get the same behavior in case both a GPIO and a power supply are provided in the device tree.

So refactor the function a bit to remove the early return, and use the same "assume VBUS is present" final fallback.

Let's see if testing reveals any subtle issues ;-)

Cheers,
Andre

Quoted 42 lines

---

 drivers/phy/allwinner/phy-sun4i-usb.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)
drivers/phy/allwinner/phy-sun4i-usb.c +12 -13
index 5302b809ee6..827ecd70f27 100644
@@ -391,20 +391,19 @@ int sun4i_usb_phy_vbus_detect(struct phy *phy)
 {
 	struct sun4i_usb_phy_data *data = dev_get_priv(phy->dev);
 	struct sun4i_usb_phy_plat *usb_phy = &data->usb_phy[phy->id];
-	int err, retries = 3;
-
-	if (usb_phy->gpio_vbus_det < 0)
-		return usb_phy->gpio_vbus_det;
-
-	err = gpio_get_value(usb_phy->gpio_vbus_det);
-	/*
-	 * Vbus may have been provided by the board and just been turned of
-	 * some milliseconds ago on reset, what we're measuring then is a
-	 * residual charge on Vbus, sleep a bit and try again.
-	 */
-	while (err > 0 && retries--) {
-		mdelay(100);
+	int err = 1, retries = 3;
+
+	if (usb_phy->gpio_vbus_det >= 0) {
 		err = gpio_get_value(usb_phy->gpio_vbus_det);
+		/*
+		 * Vbus may have been provided by the board and just turned off
+		 * some milliseconds ago on reset. What we're measuring then is
+		 * a residual charge on Vbus. Sleep a bit and try again.
+		 */
+		while (err > 0 && retries--) {
+			mdelay(100);
+			err = gpio_get_value(usb_phy->gpio_vbus_det);
+		}
 	}
 
 	return err;
author maintainer
#5 lore raw

PATCH 3/3 phy: sun4i-usb: Support VBUS detection via power supply

In reply to Samuel Holland

The device tree binding provides for getting VBUS state from a device referenced by phandle, as an optional alternative to using a GPIO. In U-Boot, where there is no power supply class, this VBUS detection will be implemented using a regulator device and its get_enable method. Let's hook this up to the PHY driver.

---

 drivers/phy/allwinner/Kconfig         | 1 +
 drivers/phy/allwinner/phy-sun4i-usb.c | 7 +++++++
 2 files changed, 8 insertions(+)
drivers/phy/allwinner/Kconfig +1 -0
index dba3bae61c4..aa9d0e7e6a5 100644
@@ -4,6 +4,7 @@
 config PHY_SUN4I_USB
 	bool "Allwinner Sun4I USB PHY driver"
 	depends on ARCH_SUNXI
+	select DM_REGULATOR
 	select PHY
 	help
 	  Enable this to support the transceiver that is part of Allwinner
drivers/phy/allwinner/phy-sun4i-usb.c +7 -0
index 827ecd70f27..ab2a5d17fcf 100644
@@ -26,6 +26,7 @@
 #include <linux/bitops.h>
 #include <linux/delay.h>
 #include <linux/err.h>
+#include <power/regulator.h>
 
 #define REG_ISCR			0x00
 #define REG_PHYCTL_A10			0x04
@@ -137,6 +138,7 @@ struct sun4i_usb_phy_data {
 	void __iomem *base;
 	const struct sun4i_usb_phy_cfg *cfg;
 	struct sun4i_usb_phy_plat *usb_phy;
+	struct udevice *vbus_power_supply;
 };
 
 static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
@@ -404,6 +406,8 @@ int sun4i_usb_phy_vbus_detect(struct phy *phy)
 			mdelay(100);
 			err = gpio_get_value(usb_phy->gpio_vbus_det);
 		}
+	} else if (data->vbus_power_supply) {
+		err = regulator_get_enable(data->vbus_power_supply);
 	}
 
 	return err;
@@ -447,6 +451,9 @@ static int sun4i_usb_phy_probe(struct udevice *dev)
 	if (IS_ERR(data->base))
 		return PTR_ERR(data->base);
 
+	device_get_supply_regulator(dev, "usb0_vbus_power-supply",
+				    &data->vbus_power_supply);
+
 	data->usb_phy = plat;
 	for (i = 0; i < data->cfg->num_phys; i++) {
 		struct sun4i_usb_phy_plat *phy = &plat[i];
Signature · 1 line
2.31.1
Acked-by on v1 3/3
#6 lore raw
Quoted 7 lines · On Sun, 12 Sep 2021 09:22:42 -0500 Samuel Holland <[email protected]> wrote:

The device tree binding provides for getting VBUS state from a device referenced by phandle, as an optional alternative to using a GPIO. In U-Boot, where there is no power supply class, this VBUS detection will be implemented using a regulator device and its get_enable method. Let's hook this up to the PHY driver.

Looks good, but curious if that works on all those boards ...

Cheers,
Andre

Quoted 57 lines

---

 drivers/phy/allwinner/Kconfig         | 1 +
 drivers/phy/allwinner/phy-sun4i-usb.c | 7 +++++++
 2 files changed, 8 insertions(+)
drivers/phy/allwinner/Kconfig +1 -0
index dba3bae61c4..aa9d0e7e6a5 100644
@@ -4,6 +4,7 @@
 config PHY_SUN4I_USB
 	bool "Allwinner Sun4I USB PHY driver"
 	depends on ARCH_SUNXI
+	select DM_REGULATOR
 	select PHY
 	help
 	  Enable this to support the transceiver that is part of Allwinner
drivers/phy/allwinner/phy-sun4i-usb.c +7 -0
index 827ecd70f27..ab2a5d17fcf 100644
@@ -26,6 +26,7 @@
 #include <linux/bitops.h>
 #include <linux/delay.h>
 #include <linux/err.h>
+#include <power/regulator.h>
 
 #define REG_ISCR			0x00
 #define REG_PHYCTL_A10			0x04
@@ -137,6 +138,7 @@ struct sun4i_usb_phy_data {
 	void __iomem *base;
 	const struct sun4i_usb_phy_cfg *cfg;
 	struct sun4i_usb_phy_plat *usb_phy;
+	struct udevice *vbus_power_supply;
 };
 
 static int initial_usb_scan_delay = CONFIG_INITIAL_USB_SCAN_DELAY;
@@ -404,6 +406,8 @@ int sun4i_usb_phy_vbus_detect(struct phy *phy)
 			mdelay(100);
 			err = gpio_get_value(usb_phy->gpio_vbus_det);
 		}
+	} else if (data->vbus_power_supply) {
+		err = regulator_get_enable(data->vbus_power_supply);
 	}
 
 	return err;
@@ -447,6 +451,9 @@ static int sun4i_usb_phy_probe(struct udevice *dev)
 	if (IS_ERR(data->base))
 		return PTR_ERR(data->base);
 
+	device_get_supply_regulator(dev, "usb0_vbus_power-supply",
+				    &data->vbus_power_supply);
+
 	data->usb_phy = plat;
 	for (i = 0; i < data->cfg->num_phys; i++) {
 		struct sun4i_usb_phy_plat *phy = &plat[i];