Skip to contentSkip to conversation

regulator: implement basic reference counter

PATCH In review Eugen Hristev 5 participants 15 messages Apr 19, 2023 – May 1, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/3 regulator: implement basic reference counter

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

 drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
 drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
 2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +22 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
 		return 0;
 	}
 
+	/* If previously enabled, increase count */
+	if (enable && dev_pdata->enable_count > 0) {
+		dev_pdata->enable_count++;
+		return -EALREADY;
+	}
+
+	if (!enable) {
+		if (dev_pdata->enable_count > 1) {
+			/* If enabled multiple times, decrease count */
+			dev_pdata->enable_count--;
+			return -EBUSY;
+		} else if (!dev_pdata->enable_count) {
+			/* If already disabled, do nothing */
+			return -EALREADY;
+		}
+	}
+
 	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
 	if (ret) {
 		pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
@@ -87,5 +104,10 @@ int regulator_common_set_enable(const struct udevice *dev,
 	if (!enable && dev_pdata->off_on_delay_us)
 		udelay(dev_pdata->off_on_delay_us);
 
+	if (enable)
+		dev_pdata->enable_count++;
+	else
+		dev_pdata->enable_count--;
+
 	return 0;
 }
drivers/power/regulator/regulator_common.h +21 -0
index c10492f01675..0faab447d099 100644
@@ -13,6 +13,7 @@ struct regulator_common_plat {
 	struct gpio_desc gpio; /* GPIO for regulator enable control */
 	unsigned int startup_delay_us;
 	unsigned int off_on_delay_us;
+	unsigned int enable_count;
 };
 
 int regulator_common_of_to_plat(struct udevice *dev,
@@ -20,6 +21,26 @@ int regulator_common_of_to_plat(struct udevice *dev,
 				char *enable_gpio_name);
 int regulator_common_get_enable(const struct udevice *dev,
 	struct regulator_common_plat *dev_pdata);
+/*
+ * Enable or Disable a regulator
+ *
+ * This is a reentrant function and subsequent calls that enable will
+ * increase an internal counter, and disable calls will decrease the counter.
+ * The actual resource will be enabled when the counter gets to 1 coming from 0,
+ * and disabled when it reaches 0 coming from 1.
+ *
+ * @dev: regulator device
+ * @dev_pdata: Platform data
+ * @enable: bool indicating whether to enable or disable the regulator
+ * @return:
+ * 0 on Success
+ * -EBUSY if the regulator cannot be disabled because it's requested by
+ *        another device
+ * -EALREADY if the regulator has already been enabled or has already been
+ *        disabled
+ * -EACCES if there is no possibility to enable/disable the regulator
+ * -ve on different error situation
+ */
 int regulator_common_set_enable(const struct udevice *dev,
 	struct regulator_common_plat *dev_pdata, bool enable);
 
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 2/3 regulator: rename dev_pdata to plat

Simplify the subsystem by renaming dev_pdata to just plat. No functional change, just trivial renaming.

---
Changes in v3,v4,v5:
- none
Changes in v2:
- this is a new patch

 drivers/power/regulator/fan53555.c         | 10 ++--
 drivers/power/regulator/fixed.c            | 16 +++----
 drivers/power/regulator/gpio-regulator.c   | 46 +++++++++----------
 drivers/power/regulator/regulator_common.c | 53 +++++++++++-----------
 drivers/power/regulator/regulator_common.h |  8 ++--
 5 files changed, 66 insertions(+), 67 deletions(-)
drivers/power/regulator/fan53555.c +5 -5
index 5681206bbafe..815f96beef61 100644
@@ -101,7 +101,7 @@ struct fan53555_priv {
 
 static int fan53555_regulator_of_to_plat(struct udevice *dev)
 {
-	struct fan53555_plat *dev_pdata = dev_get_plat(dev);
+	struct fan53555_plat *plat = dev_get_plat(dev);
 	struct dm_regulator_uclass_plat *uc_pdata =
 		dev_get_uclass_plat(dev);
 	u32 sleep_vsel;
@@ -118,12 +118,12 @@ static int fan53555_regulator_of_to_plat(struct udevice *dev)
 	 */
 	switch (sleep_vsel) {
 	case FAN53555_VSEL0:
-		dev_pdata->sleep_reg = FAN53555_VSEL0;
-		dev_pdata->vol_reg = FAN53555_VSEL1;
+		plat->sleep_reg = FAN53555_VSEL0;
+		plat->vol_reg = FAN53555_VSEL1;
 		break;
 	case FAN53555_VSEL1:
-		dev_pdata->sleep_reg = FAN53555_VSEL1;
-		dev_pdata->vol_reg = FAN53555_VSEL0;
+		plat->sleep_reg = FAN53555_VSEL1;
+		plat->vol_reg = FAN53555_VSEL0;
 		break;
 	default:
 		pr_err("%s: invalid vsel id %d\n", dev->name, sleep_vsel);
drivers/power/regulator/fixed.c +8 -8
index 90004d1601a9..ad3b4b98d667 100644
@@ -24,16 +24,16 @@ struct fixed_clock_regulator_plat {
 static int fixed_regulator_of_to_plat(struct udevice *dev)
 {
 	struct dm_regulator_uclass_plat *uc_pdata;
-	struct regulator_common_plat *dev_pdata;
+	struct regulator_common_plat *plat;
 
-	dev_pdata = dev_get_plat(dev);
+	plat = dev_get_plat(dev);
 	uc_pdata = dev_get_uclass_plat(dev);
 	if (!uc_pdata)
 		return -ENXIO;
 
 	uc_pdata->type = REGULATOR_TYPE_FIXED;
 
-	return regulator_common_of_to_plat(dev, dev_pdata, "gpio");
+	return regulator_common_of_to_plat(dev, plat, "gpio");
 }
 
 static int fixed_regulator_get_value(struct udevice *dev)
@@ -88,7 +88,7 @@ static int fixed_clock_regulator_get_enable(struct udevice *dev)
 static int fixed_clock_regulator_set_enable(struct udevice *dev, bool enable)
 {
 	struct fixed_clock_regulator_plat *priv = dev_get_priv(dev);
-	struct regulator_common_plat *dev_pdata = dev_get_plat(dev);
+	struct regulator_common_plat *plat = dev_get_plat(dev);
 	int ret = 0;
 
 	if (enable) {
@@ -101,11 +101,11 @@ static int fixed_clock_regulator_set_enable(struct udevice *dev, bool enable)
 	if (ret)
 		return ret;
 
-	if (enable && dev_pdata->startup_delay_us)
-		udelay(dev_pdata->startup_delay_us);
+	if (enable && plat->startup_delay_us)
+		udelay(plat->startup_delay_us);
 
-	if (!enable && dev_pdata->off_on_delay_us)
-		udelay(dev_pdata->off_on_delay_us);
+	if (!enable && plat->off_on_delay_us)
+		udelay(plat->off_on_delay_us);
 
 	return ret;
 }
drivers/power/regulator/gpio-regulator.c +23 -23

107 lines not on this page · raw message

drivers/power/regulator/regulator_common.c +26 -27

117 lines not on this page · raw message

drivers/power/regulator/regulator_common.h +4 -4
index 0faab447d099..d4962899d830 100644
@@ -17,10 +17,10 @@ struct regulator_common_plat {
 };
 
 int regulator_common_of_to_plat(struct udevice *dev,
-				struct regulator_common_plat *dev_pdata, const
+				struct regulator_common_plat *plat, const
 				char *enable_gpio_name);
 int regulator_common_get_enable(const struct udevice *dev,
-	struct regulator_common_plat *dev_pdata);
+	struct regulator_common_plat *plat);
 /*
  * Enable or Disable a regulator
  *
@@ -30,7 +30,7 @@ int regulator_common_get_enable(const struct udevice *dev,
  * and disabled when it reaches 0 coming from 1.
  *
  * @dev: regulator device
- * @dev_pdata: Platform data
+ * @plat: Platform data
  * @enable: bool indicating whether to enable or disable the regulator
  * @return:
  * 0 on Success
@@ -42,6 +42,6 @@ int regulator_common_get_enable(const struct udevice *dev,
  * -ve on different error situation
  */
 int regulator_common_set_enable(const struct udevice *dev,
-	struct regulator_common_plat *dev_pdata, bool enable);
+	struct regulator_common_plat *plat, bool enable);
 
 #endif /* _REGULATOR_COMMON_H */
Signature · 1 line
2.34.1
Reviewed-by on v5 2/3
#2 lore raw

Re: [PATCH v5 2/3] regulator: rename dev_pdata to plat

Quoted 366 lines · Eugen Hristev

Simplify the subsystem by renaming dev_pdata to just plat. No functional change, just trivial renaming.

---
Changes in v3,v4,v5:
- none
Changes in v2:
- this is a new patch

 drivers/power/regulator/fan53555.c         | 10 ++--
 drivers/power/regulator/fixed.c            | 16 +++----
 drivers/power/regulator/gpio-regulator.c   | 46 +++++++++----------
 drivers/power/regulator/regulator_common.c | 53 +++++++++++-----------
 drivers/power/regulator/regulator_common.h |  8 ++--
 5 files changed, 66 insertions(+), 67 deletions(-)
drivers/power/regulator/fan53555.c +5 -5
index 5681206bbafe..815f96beef61 100644
@@ -101,7 +101,7 @@ struct fan53555_priv {
 
 static int fan53555_regulator_of_to_plat(struct udevice *dev)
 {
-	struct fan53555_plat *dev_pdata = dev_get_plat(dev);
+	struct fan53555_plat *plat = dev_get_plat(dev);
 	struct dm_regulator_uclass_plat *uc_pdata =
 		dev_get_uclass_plat(dev);
 	u32 sleep_vsel;
@@ -118,12 +118,12 @@ static int fan53555_regulator_of_to_plat(struct udevice *dev)
 	 */
 	switch (sleep_vsel) {
 	case FAN53555_VSEL0:
-		dev_pdata->sleep_reg = FAN53555_VSEL0;
-		dev_pdata->vol_reg = FAN53555_VSEL1;
+		plat->sleep_reg = FAN53555_VSEL0;
+		plat->vol_reg = FAN53555_VSEL1;
 		break;
 	case FAN53555_VSEL1:
-		dev_pdata->sleep_reg = FAN53555_VSEL1;
-		dev_pdata->vol_reg = FAN53555_VSEL0;
+		plat->sleep_reg = FAN53555_VSEL1;
+		plat->vol_reg = FAN53555_VSEL0;
 		break;
 	default:
 		pr_err("%s: invalid vsel id %d\n", dev->name, sleep_vsel);
drivers/power/regulator/fixed.c +8 -8
index 90004d1601a9..ad3b4b98d667 100644
@@ -24,16 +24,16 @@ struct fixed_clock_regulator_plat {
 static int fixed_regulator_of_to_plat(struct udevice *dev)
 {
 	struct dm_regulator_uclass_plat *uc_pdata;
-	struct regulator_common_plat *dev_pdata;
+	struct regulator_common_plat *plat;
 
-	dev_pdata = dev_get_plat(dev);
+	plat = dev_get_plat(dev);
 	uc_pdata = dev_get_uclass_plat(dev);
 	if (!uc_pdata)
 		return -ENXIO;
 
 	uc_pdata->type = REGULATOR_TYPE_FIXED;
 
-	return regulator_common_of_to_plat(dev, dev_pdata, "gpio");
+	return regulator_common_of_to_plat(dev, plat, "gpio");
 }
 
 static int fixed_regulator_get_value(struct udevice *dev)
@@ -88,7 +88,7 @@ static int fixed_clock_regulator_get_enable(struct udevice *dev)
 static int fixed_clock_regulator_set_enable(struct udevice *dev, bool enable)
 {
 	struct fixed_clock_regulator_plat *priv = dev_get_priv(dev);
-	struct regulator_common_plat *dev_pdata = dev_get_plat(dev);
+	struct regulator_common_plat *plat = dev_get_plat(dev);
 	int ret = 0;
 
 	if (enable) {
@@ -101,11 +101,11 @@ static int fixed_clock_regulator_set_enable(struct udevice *dev, bool enable)
 	if (ret)
 		return ret;
 
-	if (enable && dev_pdata->startup_delay_us)
-		udelay(dev_pdata->startup_delay_us);
+	if (enable && plat->startup_delay_us)
+		udelay(plat->startup_delay_us);
 
-	if (!enable && dev_pdata->off_on_delay_us)
-		udelay(dev_pdata->off_on_delay_us);
+	if (!enable && plat->off_on_delay_us)
+		udelay(plat->off_on_delay_us);
 
 	return ret;
 }
drivers/power/regulator/gpio-regulator.c +23 -23
index 9c0a68aa5af4..ded7be059bb0 100644
@@ -27,12 +27,12 @@ struct gpio_regulator_plat {
 static int gpio_regulator_of_to_plat(struct udevice *dev)
 {
 	struct dm_regulator_uclass_plat *uc_pdata;
-	struct gpio_regulator_plat *dev_pdata;
+	struct gpio_regulator_plat *plat;
 	struct gpio_desc *gpio;
 	int ret, count, i, j;
 	u32 states_array[GPIO_REGULATOR_MAX_STATES * 2];
 
-	dev_pdata = dev_get_plat(dev);
+	plat = dev_get_plat(dev);
 	uc_pdata = dev_get_uclass_plat(dev);
 	if (!uc_pdata)
 		return -ENXIO;
@@ -47,7 +47,7 @@ static int gpio_regulator_of_to_plat(struct udevice *dev)
 	 * per gpio-regulator. As of now no instance with multiple
 	 * gpios is presnt
 	 */
-	gpio = &dev_pdata->gpio;
+	gpio = &plat->gpio;
 	ret = gpio_request_by_name(dev, "gpios", 0, gpio, GPIOD_IS_OUT);
 	if (ret)
 		debug("regulator gpio - not found! Error: %d", ret);
@@ -68,21 +68,21 @@ static int gpio_regulator_of_to_plat(struct udevice *dev)
 		return ret;
 
 	for (i = 0, j = 0; i < count; i += 2) {
-		dev_pdata->voltages[j] = states_array[i];
-		dev_pdata->states[j] = states_array[i + 1];
+		plat->voltages[j] = states_array[i];
+		plat->states[j] = states_array[i + 1];
 		j++;
 	}
 
-	return regulator_common_of_to_plat(dev, &dev_pdata->common, "enable-gpios");
+	return regulator_common_of_to_plat(dev, &plat->common, "enable-gpios");
 }
 
 static int gpio_regulator_get_value(struct udevice *dev)
 {
 	struct dm_regulator_uclass_plat *uc_pdata;
-	struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
+	struct gpio_regulator_plat *plat = dev_get_plat(dev);
 	int enable;
 
-	if (!dev_pdata->gpio.dev)
+	if (!plat->gpio.dev)
 		return -ENOSYS;
 
 	uc_pdata = dev_get_uclass_plat(dev);
@@ -91,30 +91,30 @@ static int gpio_regulator_get_value(struct udevice *dev)
 		return -EINVAL;
 	}
 
-	enable = dm_gpio_get_value(&dev_pdata->gpio);
-	if (enable == dev_pdata->states[0])
-		return dev_pdata->voltages[0];
+	enable = dm_gpio_get_value(&plat->gpio);
+	if (enable == plat->states[0])
+		return plat->voltages[0];
 	else
-		return dev_pdata->voltages[1];
+		return plat->voltages[1];
 }
 
 static int gpio_regulator_set_value(struct udevice *dev, int uV)
 {
-	struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
+	struct gpio_regulator_plat *plat = dev_get_plat(dev);
 	int ret;
 	bool enable;
 
-	if (!dev_pdata->gpio.dev)
+	if (!plat->gpio.dev)
 		return -ENOSYS;
 
-	if (uV == dev_pdata->voltages[0])
-		enable = dev_pdata->states[0];
-	else if (uV == dev_pdata->voltages[1])
-		enable = dev_pdata->states[1];
+	if (uV == plat->voltages[0])
+		enable = plat->states[0];
+	else if (uV == plat->voltages[1])
+		enable = plat->states[1];
 	else
 		return -EINVAL;
 
-	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
+	ret = dm_gpio_set_value(&plat->gpio, enable);
 	if (ret) {
 		pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
 		      enable);
@@ -126,14 +126,14 @@ static int gpio_regulator_set_value(struct udevice *dev, int uV)
 
 static int gpio_regulator_get_enable(struct udevice *dev)
 {
-	struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
-	return regulator_common_get_enable(dev, &dev_pdata->common);
+	struct gpio_regulator_plat *plat = dev_get_plat(dev);
+	return regulator_common_get_enable(dev, &plat->common);
 }
 
 static int gpio_regulator_set_enable(struct udevice *dev, bool enable)
 {
-	struct gpio_regulator_plat *dev_pdata = dev_get_plat(dev);
-	return regulator_common_set_enable(dev, &dev_pdata->common, enable);
+	struct gpio_regulator_plat *plat = dev_get_plat(dev);
+	return regulator_common_set_enable(dev, &plat->common, enable);
 }
 
 static const struct dm_regulator_ops gpio_regulator_ops = {
drivers/power/regulator/regulator_common.c +26 -27
index 484a4fc31ef7..e26f5ebec347 100644
@@ -13,7 +13,7 @@
 #include "regulator_common.h"
 
 int regulator_common_of_to_plat(struct udevice *dev,
-				struct regulator_common_plat *dev_pdata,
+				struct regulator_common_plat *plat,
 				const char *enable_gpio_name)
 {
 	struct gpio_desc *gpio;
@@ -26,7 +26,7 @@ int regulator_common_of_to_plat(struct udevice *dev,
 		flags |= GPIOD_IS_OUT_ACTIVE;
 
 	/* Get optional enable GPIO desc */
-	gpio = &dev_pdata->gpio;
+	gpio = &plat->gpio;
 	ret = gpio_request_by_name(dev, enable_gpio_name, 0, gpio, flags);
 	if (ret) {
 		debug("Regulator '%s' optional enable GPIO - not found! Error: %d\n",
@@ -36,12 +36,11 @@ int regulator_common_of_to_plat(struct udevice *dev,
 	}
 
 	/* Get optional ramp up delay */
-	dev_pdata->startup_delay_us = dev_read_u32_default(dev,
-							"startup-delay-us", 0);
-	dev_pdata->off_on_delay_us =
-		dev_read_u32_default(dev, "off-on-delay-us", 0);
-	if (!dev_pdata->off_on_delay_us) {
-		dev_pdata->off_on_delay_us =
+	plat->startup_delay_us = dev_read_u32_default(dev,
+						      "startup-delay-us", 0);
+	plat->off_on_delay_us = dev_read_u32_default(dev, "off-on-delay-us", 0);
+	if (!plat->off_on_delay_us) {
+		plat->off_on_delay_us =
 			dev_read_u32_default(dev, "u-boot,off-on-delay-us", 0);
 	}
 
@@ -49,65 +48,65 @@ int regulator_common_of_to_plat(struct udevice *dev,
 }
 
 int regulator_common_get_enable(const struct udevice *dev,
-	struct regulator_common_plat *dev_pdata)
+	struct regulator_common_plat *plat)
 {
 	/* Enable GPIO is optional */
-	if (!dev_pdata->gpio.dev)
+	if (!plat->gpio.dev)
 		return true;
 
-	return dm_gpio_get_value(&dev_pdata->gpio);
+	return dm_gpio_get_value(&plat->gpio);
 }
 
 int regulator_common_set_enable(const struct udevice *dev,
-	struct regulator_common_plat *dev_pdata, bool enable)
+	struct regulator_common_plat *plat, bool enable)
 {
 	int ret;
 
 	debug("%s: dev='%s', enable=%d, delay=%d, has_gpio=%d\n", __func__,
-	      dev->name, enable, dev_pdata->startup_delay_us,
-	      dm_gpio_is_valid(&dev_pdata->gpio));
+	      dev->name, enable, plat->startup_delay_us,
+	      dm_gpio_is_valid(&plat->gpio));
 	/* Enable GPIO is optional */
-	if (!dm_gpio_is_valid(&dev_pdata->gpio)) {
+	if (!dm_gpio_is_valid(&plat->gpio)) {
 		if (!enable)
 			return -ENOSYS;
 		return 0;
 	}
 
 	/* If previously enabled, increase count */
-	if (enable && dev_pdata->enable_count > 0) {
-		dev_pdata->enable_count++;
+	if (enable && plat->enable_count > 0) {
+		plat->enable_count++;
 		return -EALREADY;
 	}
 
 	if (!enable) {
-		if (dev_pdata->enable_count > 1) {
+		if (plat->enable_count > 1) {
 			/* If enabled multiple times, decrease count */
-			dev_pdata->enable_count--;
+			plat->enable_count--;
 			return -EBUSY;
-		} else if (!dev_pdata->enable_count) {
+		} else if (!plat->enable_count) {
 			/* If already disabled, do nothing */
 			return -EALREADY;
 		}
 	}
 
-	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
+	ret = dm_gpio_set_value(&plat->gpio, enable);
 	if (ret) {
 		pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
 		      enable);
 		return ret;
 	}
 
-	if (enable && dev_pdata->startup_delay_us)
-		udelay(dev_pdata->startup_delay_us);
+	if (enable && plat->startup_delay_us)
+		udelay(plat->startup_delay_us);
 	debug("%s: done\n", __func__);
 
-	if (!enable && dev_pdata->off_on_delay_us)
-		udelay(dev_pdata->off_on_delay_us);
+	if (!enable && plat->off_on_delay_us)
+		udelay(plat->off_on_delay_us);
 
 	if (enable)
-		dev_pdata->enable_count++;
+		plat->enable_count++;
 	else
-		dev_pdata->enable_count--;
+		plat->enable_count--;
 
 	return 0;
 }
drivers/power/regulator/regulator_common.h +3 -3
index 0faab447d099..d4962899d830 100644
@@ -17,10 +17,10 @@ struct regulator_common_plat {
 };
 
 int regulator_common_of_to_plat(struct udevice *dev,
-				struct regulator_common_plat *dev_pdata, const
+				struct regulator_common_plat *plat, const
 				char *enable_gpio_name);
 int regulator_common_get_enable(const struct udevice *dev,
-	struct regulator_common_plat *dev_pdata);
+	struct regulator_common_plat *plat);
 /*
  * Enable or Disable a regulator
  *
@@ -30,7 +30,7 @@ int regulator_common_get_enable(const struct udevice *dev,
  * and disabled when it reaches 0 coming from 1.
  *
  * @dev: regulator device
- * @dev_pdata: Platform data
+ * @plat: Platform data
  * @enable: bool indicating whether to enable or disable the regulator
  * @return:
  * 0 on Success

drivers/power/regulator/regulator_common.h int regulator_common_get_enable(const struct udevice *dev, · line 47

Quoted 7 lines
drivers/power/regulator/regulator_common.h +1 -1
@@ -42,6 +42,6 @@ int regulator_common_get_enable(const struct udevice *dev,
  * -ve on different error situation
  */
 int regulator_common_set_enable(const struct udevice *dev,
-	struct regulator_common_plat *dev_pdata, bool enable);
+	struct regulator_common_plat *plat, bool enable);
 
 #endif /* _REGULATOR_COMMON_H */

Thanks
Patrice

#3 lore raw

PATCH v5 3/3 regulator: handle different error codes in regulator_set_enable_if_allowed

In reply to Eugen Hristev

The regulator core can return different codes which are not considered a real error for this function.
Return success in such cases.

---
Changes in v5:
- this is a new patch

 drivers/power/regulator/regulator-uclass.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/power/regulator/regulator-uclass.c +6 -0
index d608f7c23657..3a6ba69f6d5f 100644
@@ -197,6 +197,12 @@ int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
 	ret = regulator_set_enable(dev, enable);
 	if (ret == -ENOSYS || ret == -EACCES)
 		return 0;
+	/* if we want to disable but it's in use by someone else */
+	if (!enable && ret == -EBUSY)
+		return 0;
+	/* if it's already enabled/disabled */
+	if (ret == -EALREADY)
+		return 0;
 
 	return ret;
 }
Signature · 1 line
2.34.1
Reviewed-by on v5 3/3
#4 lore raw

Re: [PATCH v5 3/3] regulator: handle different error codes in regulator_set_enable_if_allowed

Quoted 5 lines · Eugen Hristev

The regulator core can return different codes which are not considered a real error for this function.
Return success in such cases.

Quoted 7 lines

---
Changes in v5:
- this is a new patch

 drivers/power/regulator/regulator-uclass.c | 6 ++++++
 1 file changed, 6 insertions(+)
Reviewed-by on v5 3/3
#5 lore raw

Re: [PATCH v5 3/3] regulator: handle different error codes in regulator_set_enable_if_allowed

In reply to Eugen Hristev
Quoted 19 lines · Eugen Hristev

The regulator core can return different codes which are not considered a real error for this function.
Return success in such cases.

---
Changes in v5:
- this is a new patch

 drivers/power/regulator/regulator-uclass.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/power/regulator/regulator-uclass.c +0 -0
index d608f7c23657..3a6ba69f6d5f 100644
@@ -197,6 +197,12 @@ int regulator_set_enable_if_allowed(struct udevice *dev, bool enable)
 	ret = regulator_set_enable(dev, enable);
 	if (ret == -ENOSYS || ret == -EACCES)

drivers/power/regulator/regulator-uclass.c int regulator_set_enable_if_allowed(struct udevice *dev, bool enable) · line 208

Quoted 10 lines
drivers/power/regulator/regulator-uclass.c +6 -0
 		return 0;
+	/* if we want to disable but it's in use by someone else */
+	if (!enable && ret == -EBUSY)
+		return 0;
+	/* if it's already enabled/disabled */
+	if (ret == -EALREADY)
+		return 0;
 
 	return ret;
 }

Thanks
Patrice

Reviewed-by on v5 1/3
#6 lore raw
In reply to Eugen Hristev
Quoted 92 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

 drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
 drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
 2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +22 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
 		return 0;
 	}
 
+	/* If previously enabled, increase count */
+	if (enable && dev_pdata->enable_count > 0) {
+		dev_pdata->enable_count++;
+		return -EALREADY;
+	}
+
+	if (!enable) {
+		if (dev_pdata->enable_count > 1) {
+			/* If enabled multiple times, decrease count */
+			dev_pdata->enable_count--;
+			return -EBUSY;
+		} else if (!dev_pdata->enable_count) {
+			/* If already disabled, do nothing */
+			return -EALREADY;
+		}
+	}
+
 	ret = dm_gpio_set_value(&dev_pdata->gpio, enable);
 	if (ret) {
 		pr_err("Can't set regulator : %s gpio to: %d\n", dev->name,
@@ -87,5 +104,10 @@ int regulator_common_set_enable(const struct udevice *dev,
 	if (!enable && dev_pdata->off_on_delay_us)
 		udelay(dev_pdata->off_on_delay_us);
 
+	if (enable)
+		dev_pdata->enable_count++;
+	else
+		dev_pdata->enable_count--;
+
 	return 0;
 }
drivers/power/regulator/regulator_common.h +14 -0
index c10492f01675..0faab447d099 100644
@@ -13,6 +13,7 @@ struct regulator_common_plat {
 	struct gpio_desc gpio; /* GPIO for regulator enable control */
 	unsigned int startup_delay_us;
 	unsigned int off_on_delay_us;
+	unsigned int enable_count;
 };
 
 int regulator_common_of_to_plat(struct udevice *dev,
@@ -20,6 +21,26 @@ int regulator_common_of_to_plat(struct udevice *dev,
 				char *enable_gpio_name);
 int regulator_common_get_enable(const struct udevice *dev,
 	struct regulator_common_plat *dev_pdata);
+/*
+ * Enable or Disable a regulator
+ *
+ * This is a reentrant function and subsequent calls that enable will
+ * increase an internal counter, and disable calls will decrease the counter.
+ * The actual resource will be enabled when the counter gets to 1 coming from 0,
+ * and disabled when it reaches 0 coming from 1.
+ *
+ * @dev: regulator device
+ * @dev_pdata: Platform data
+ * @enable: bool indicating whether to enable or disable the regulator
+ * @return:
+ * 0 on Success

drivers/power/regulator/regulator_common.h int regulator_common_of_to_plat(struct udevice *dev, · line 46

Quoted 10 lines
drivers/power/regulator/regulator_common.h +7 -0
+ * -EBUSY if the regulator cannot be disabled because it's requested by
+ *        another device
+ * -EALREADY if the regulator has already been enabled or has already been
+ *        disabled
+ * -EACCES if there is no possibility to enable/disable the regulator
+ * -ve on different error situation
+ */
 int regulator_common_set_enable(const struct udevice *dev,
 	struct regulator_common_plat *dev_pdata, bool enable);
 

Patrice

#7 lore raw
In reply to Eugen Hristev
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

 drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
 drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
 2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                return 0;
        }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Thank you for working on this series!

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls (like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

Best Regards,

Tim

#8 lore raw
Quoted 55 lines · Tim Harvey
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

  drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
  drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
  2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                 return 0;
         }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Quoted 1 line

Thank you for working on this series!

Hi Tim,

Thank you for testing and having a look on my patches.

Quoted 3 lines

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls

Initially I did that, but Simon forced me to return error as you see now.

Quoted 6 lines

(like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I believe Jonas (in CC) fixed those with a patch, but at the moment I am lost in providing you a link to it

Quoted 8 lines

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

That's a good catch.
I expected such things would happen if I would return error in those cases, so it might be that this is not the only case. If you are able to test that board, do you wish me to send you a patch that changes the code to using regulator_set_enable_if_allowed() ?

Quoted 3 lines

Best Regards,

Tim

#9 lore raw
Quoted 59 lines · Eugen Hristev
Quoted 55 lines · Tim Harvey
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

  drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
  drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
  2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                 return 0;
         }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Quoted 1 line

Thank you for working on this series!

Quoted 8 lines

Hi Tim,

Thank you for testing and having a look on my patches.

Quoted 3 lines

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls

Initially I did that, but Simon forced me to return error as you see now.

Ok, I see that discussion here:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/#3086660

Quoted 10 lines
Quoted 7 lines

(like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I believe Jonas (in CC) fixed those with a patch, but at the moment I am lost in providing you a link to it

Are you thinking of "pci: pcie_dw_rockchip: Use
regulator_set_enable_if_allowed"
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
?

I added some debug prints to regulator_set_enable and see:
starting USB...
Bus usb@32e40000: regulator_set_enable regulator-usb-otg1 enable regulator_set_enable regulator-usb-otg1 enable ret=0 Bus usb@32e50000: regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=0 regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=-114 Error enabling VBUS supply (ret=-114) regulator_set_enable regulator-usb-otg2 disable regulator_set_enable regulator-usb-otg2 disable ret=-16 probe failed, error -114

So clearly something is trying to enable regulator-usb-otg2 when it's already enabled but I don't think that should be considered an error and cause a failure.

Simon, is this what you were intending with your feedback?

Quoted 9 lines
Quoted 8 lines

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

Quoted 6 lines

That's a good catch.
I expected such things would happen if I would return error in those cases, so it might be that this is not the only case.
If you are able to test that board, do you wish me to send you a patch that changes the code to using regulator_set_enable_if_allowed() ?

Yes, I can test. Are you thinking changing the calls to regulator_common_set_enable (used in drivers/power/regulator/fixed{gpio-regulator,regulator_common} to a wrapper calling regulator_common_set_enable_if_allowed instead? I think that would just be the same thing as removing the error as no callers of that function would be left.

Your series solves a necessary issue where a regulator_disable call would disable a regulator that was still being used by another consumer but I don't think it should break calls to regulator_enable just because another consumer is also using it.

Best Regards,

Tim

#10 lore raw

Hi Tim,

Quoted 124 lines · Tim Harvey
Quoted 59 lines · Eugen Hristev
Quoted 55 lines · Tim Harvey
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

  drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
  drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
  2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                 return 0;
         }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Quoted 1 line

Thank you for working on this series!

Quoted 8 lines

Hi Tim,

Thank you for testing and having a look on my patches.

Quoted 3 lines

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls

Initially I did that, but Simon forced me to return error as you see now.

Ok, I see that discussion here:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/#3086660

Quoted 10 lines
Quoted 7 lines

(like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I believe Jonas (in CC) fixed those with a patch, but at the moment I am lost in providing you a link to it

Are you thinking of "pci: pcie_dw_rockchip: Use
regulator_set_enable_if_allowed"
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
?

I added some debug prints to regulator_set_enable and see:
starting USB...
Bus usb@32e40000: regulator_set_enable regulator-usb-otg1 enable regulator_set_enable regulator-usb-otg1 enable ret=0 Bus usb@32e50000: regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=0 regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=-114 Error enabling VBUS supply (ret=-114) regulator_set_enable regulator-usb-otg2 disable regulator_set_enable regulator-usb-otg2 disable ret=-16 probe failed, error -114

So clearly something is trying to enable regulator-usb-otg2 when it's already enabled but I don't think that should be considered an error and cause a failure.

Simon, is this what you were intending with your feedback?

Quoted 9 lines
Quoted 8 lines

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

Quoted 6 lines

That's a good catch.
I expected such things would happen if I would return error in those cases, so it might be that this is not the only case.
If you are able to test that board, do you wish me to send you a patch that changes the code to using regulator_set_enable_if_allowed() ?

Quoted 6 lines

Yes, I can test. Are you thinking changing the calls to regulator_common_set_enable (used in drivers/power/regulator/fixed{gpio-regulator,regulator_common} to a wrapper calling regulator_common_set_enable_if_allowed instead? I think that would just be the same thing as removing the error as no callers of that function would be left.

This is nothing that should be changed in the fixed/gpio/common regulator code. Such change should happen in the drivers that call the regulator_set_enable function. In your case the usb driver that tries to enable/disable the regulator.

Quoted 6 lines

Your series solves a necessary issue where a regulator_disable call would disable a regulator that was still being used by another consumer but I don't think it should break calls to regulator_enable just because another consumer is also using it.

With this patch at least fixed/gpio regulators have a basic reference counter and because of this the regulator_set_enable may now return a more detailed errno, "0 on success or -errno val if fails".

The call to regulator_set_enable can be replaced with a call to regulator_set_enable_if_allowed when the caller requires less strict errors, "0 on success or if enabling is not supported or -errno val if fails."

Another option could be to also relax the errors returned by regulator_set_enable, and instead print an error message for the two new errno.

Regards,
Jonas

Quoted 3 lines

Best Regards,

Tim

#11 lore raw
Quoted 145 lines · Jonas Karlman

Hi Tim,

Quoted 124 lines · Tim Harvey
Quoted 59 lines · Eugen Hristev
Quoted 55 lines · Tim Harvey
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

   drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
   drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
   2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                  return 0;
          }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Quoted 1 line

Thank you for working on this series!

Quoted 8 lines

Hi Tim,

Thank you for testing and having a look on my patches.

Quoted 3 lines

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls

Initially I did that, but Simon forced me to return error as you see now.

Ok, I see that discussion here:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/#3086660

Quoted 10 lines
Quoted 7 lines

(like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I believe Jonas (in CC) fixed those with a patch, but at the moment I am lost in providing you a link to it

Are you thinking of "pci: pcie_dw_rockchip: Use
regulator_set_enable_if_allowed"
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
?

I added some debug prints to regulator_set_enable and see:
starting USB...
Bus usb@32e40000: regulator_set_enable regulator-usb-otg1 enable regulator_set_enable regulator-usb-otg1 enable ret=0 Bus usb@32e50000: regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=0 regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=-114 Error enabling VBUS supply (ret=-114) regulator_set_enable regulator-usb-otg2 disable regulator_set_enable regulator-usb-otg2 disable ret=-16 probe failed, error -114

So clearly something is trying to enable regulator-usb-otg2 when it's already enabled but I don't think that should be considered an error and cause a failure.

Simon, is this what you were intending with your feedback?

Quoted 9 lines
Quoted 8 lines

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

Quoted 6 lines

That's a good catch.
I expected such things would happen if I would return error in those cases, so it might be that this is not the only case.
If you are able to test that board, do you wish me to send you a patch that changes the code to using regulator_set_enable_if_allowed() ?

Quoted 6 lines

Yes, I can test. Are you thinking changing the calls to regulator_common_set_enable (used in drivers/power/regulator/fixed{gpio-regulator,regulator_common} to a wrapper calling regulator_common_set_enable_if_allowed instead? I think that would just be the same thing as removing the error as no callers of that function would be left.

This is nothing that should be changed in the fixed/gpio/common regulator code. Such change should happen in the drivers that call the regulator_set_enable function. In your case the usb driver that tries to enable/disable the regulator.

Quoted 6 lines

Your series solves a necessary issue where a regulator_disable call would disable a regulator that was still being used by another consumer but I don't think it should break calls to regulator_enable just because another consumer is also using it.

Quoted 8 lines

With this patch at least fixed/gpio regulators have a basic reference counter and because of this the regulator_set_enable may now return a more detailed errno, "0 on success or -errno val if fails".

The call to regulator_set_enable can be replaced with a call to regulator_set_enable_if_allowed when the caller requires less strict errors, "0 on success or if enabling is not supported or -errno val if fails."

I think that the best option is to replace calls to regulator_set_enable with regulator_set_enable_if_allowed.

Tim, what is the driver that is now broken ? I will try to create a patch and send it your way so you can test ?

Quoted 12 lines

Another option could be to also relax the errors returned by regulator_set_enable, and instead print an error message for the two new errno.

Regards,
Jonas

Quoted 3 lines

Best Regards,

Tim

#12 lore raw
Quoted 156 lines · Eugen Hristev
Quoted 145 lines · Jonas Karlman

Hi Tim,

Quoted 124 lines · Tim Harvey
Quoted 59 lines · Eugen Hristev
Quoted 55 lines · Tim Harvey
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

   drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
   drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
   2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                  return 0;
          }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Quoted 1 line

Thank you for working on this series!

Quoted 8 lines

Hi Tim,

Thank you for testing and having a look on my patches.

Quoted 3 lines

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls

Initially I did that, but Simon forced me to return error as you see now.

Ok, I see that discussion here:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/#3086660

Quoted 10 lines
Quoted 7 lines

(like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I believe Jonas (in CC) fixed those with a patch, but at the moment I am lost in providing you a link to it

Are you thinking of "pci: pcie_dw_rockchip: Use
regulator_set_enable_if_allowed"
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
?

I added some debug prints to regulator_set_enable and see:
starting USB...
Bus usb@32e40000: regulator_set_enable regulator-usb-otg1 enable regulator_set_enable regulator-usb-otg1 enable ret=0 Bus usb@32e50000: regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=0 regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=-114 Error enabling VBUS supply (ret=-114) regulator_set_enable regulator-usb-otg2 disable regulator_set_enable regulator-usb-otg2 disable ret=-16 probe failed, error -114

So clearly something is trying to enable regulator-usb-otg2 when it's already enabled but I don't think that should be considered an error and cause a failure.

Simon, is this what you were intending with your feedback?

Quoted 9 lines
Quoted 8 lines

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

Quoted 6 lines

That's a good catch.
I expected such things would happen if I would return error in those cases, so it might be that this is not the only case.
If you are able to test that board, do you wish me to send you a patch that changes the code to using regulator_set_enable_if_allowed() ?

Quoted 6 lines

Yes, I can test. Are you thinking changing the calls to regulator_common_set_enable (used in drivers/power/regulator/fixed{gpio-regulator,regulator_common} to a wrapper calling regulator_common_set_enable_if_allowed instead? I think that would just be the same thing as removing the error as no callers of that function would be left.

This is nothing that should be changed in the fixed/gpio/common regulator code. Such change should happen in the drivers that call the regulator_set_enable function. In your case the usb driver that tries to enable/disable the regulator.

Quoted 6 lines

Your series solves a necessary issue where a regulator_disable call would disable a regulator that was still being used by another consumer but I don't think it should break calls to regulator_enable just because another consumer is also using it.

Quoted 8 lines

With this patch at least fixed/gpio regulators have a basic reference counter and because of this the regulator_set_enable may now return a more detailed errno, "0 on success or -errno val if fails".

The call to regulator_set_enable can be replaced with a call to regulator_set_enable_if_allowed when the caller requires less strict errors, "0 on success or if enabling is not supported or -errno val if fails."

Quoted 5 lines

I think that the best option is to replace calls to regulator_set_enable with regulator_set_enable_if_allowed.

Tim, what is the driver that is now broken ? I will try to create a patch and send it your way so you can test ?

The breakage I saw was from drivers/usb/host/ehci-mx6.c with several calls to enable the vbus_supply. I assume there are many other drivers that would have the same problem as there are just over 100 calls to regulator_set_enable throughout U-Boot. I'm not sure I understand what cases there are where a call to enable a regulator should fail just because another consumer still needs it.

Best Regards,

Tim

Quoted 13 lines
Quoted 12 lines

Another option could be to also relax the errors returned by regulator_set_enable, and instead print an error message for the two new errno.

Regards,
Jonas

Quoted 3 lines

Best Regards,

Tim

#13 lore raw
In reply to Eugen Hristev

Hi Eugen,

Quoted 151 lines · Eugen Hristev
Quoted 149 lines · Jonas Karlman

Hi Tim,

Quoted 124 lines · Tim Harvey
Quoted 59 lines · Eugen Hristev
Quoted 55 lines · Tim Harvey
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

   drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
   drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
   2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                  return 0;
          }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Quoted 1 line

Thank you for working on this series!

Quoted 8 lines

Hi Tim,

Thank you for testing and having a look on my patches.

Quoted 3 lines

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls

Initially I did that, but Simon forced me to return error as you see now.

Ok, I see that discussion here:
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/#3086660

Quoted 10 lines
Quoted 7 lines

(like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I believe Jonas (in CC) fixed those with a patch, but at the moment I am lost in providing you a link to it

Are you thinking of "pci: pcie_dw_rockchip: Use
regulator_set_enable_if_allowed"
https://patchwork.ozlabs.org/project/uboot/patch/[email protected]/
?

I added some debug prints to regulator_set_enable and see:
starting USB...
Bus usb@32e40000: regulator_set_enable regulator-usb-otg1 enable regulator_set_enable regulator-usb-otg1 enable ret=0 Bus usb@32e50000: regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=0 regulator_set_enable regulator-usb-otg2 enable regulator_set_enable regulator-usb-otg2 enable ret=-114 Error enabling VBUS supply (ret=-114) regulator_set_enable regulator-usb-otg2 disable regulator_set_enable regulator-usb-otg2 disable ret=-16 probe failed, error -114

So clearly something is trying to enable regulator-usb-otg2 when it's already enabled but I don't think that should be considered an error and cause a failure.

Simon, is this what you were intending with your feedback?

Quoted 9 lines
Quoted 8 lines

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

Quoted 6 lines

That's a good catch.
I expected such things would happen if I would return error in those cases, so it might be that this is not the only case.
If you are able to test that board, do you wish me to send you a patch that changes the code to using regulator_set_enable_if_allowed() ?

Quoted 6 lines

Yes, I can test. Are you thinking changing the calls to regulator_common_set_enable (used in drivers/power/regulator/fixed{gpio-regulator,regulator_common} to a wrapper calling regulator_common_set_enable_if_allowed instead? I think that would just be the same thing as removing the error as no callers of that function would be left.

This is nothing that should be changed in the fixed/gpio/common regulator code. Such change should happen in the drivers that call the regulator_set_enable function. In your case the usb driver that tries to enable/disable the regulator.

Quoted 6 lines

Your series solves a necessary issue where a regulator_disable call would disable a regulator that was still being used by another consumer but I don't think it should break calls to regulator_enable just because another consumer is also using it.

With this patch at least fixed/gpio regulators have a basic reference counter and because of this the regulator_set_enable may now return a more detailed errno, "0 on success or -errno val if fails".

Quoted 7 lines
Quoted 4 lines

The call to regulator_set_enable can be replaced with a call to regulator_set_enable_if_allowed when the caller requires less strict errors, "0 on success or if enabling is not supported or -errno val if fails."

I think that the best option is to replace calls to regulator_set_enable with regulator_set_enable_if_allowed.

That sounds good. Or even regulator_set_enable_maybe() since it is shorter and the question is not whether it is allowed, but whether we want to for other reasons (i.e other device's desires). I can imagine having quite a few 'maybe' functions about the place eventually.

Another option would be to make it more explicit, like regulator_inc_set_enable()/ dec_set_enable(), but that seems a bit wordy and confusing.

Quoted 16 lines

Tim, what is the driver that is now broken ? I will try to create a patch and send it your way so you can test ?

Quoted 12 lines

Another option could be to also relax the errors returned by regulator_set_enable, and instead print an error message for the two new errno.

Regards,
Jonas

Quoted 3 lines

Best Regards,

Tim

Regards,
Simon

#14 lore raw
In reply to Eugen Hristev

Hi Eugen,

Quoted 59 lines · Eugen Hristev
Quoted 55 lines · Tim Harvey
Quoted 40 lines · Eugen Hristev

Some devices share a regulator supply, when the first one will request regulator disable, the second device will have it's supply cut off before graciously shutting down. Hence there will be timeouts and other failed operations.
Implement a reference counter mechanism similar with what is done in Linux, to keep track of enable and disable requests, and only disable the regulator when the last of the consumers has requested shutdown.

---
Changes in v5:
- none
Changes in v4:
- add documentation for error codes
Changes in v3:
- add error return codes
Changes in v2:
- add info in header regarding the function

  drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
  drivers/power/regulator/regulator_common.h | 21 +++++++++++++++++++++
  2 files changed, 43 insertions(+)
drivers/power/regulator/regulator_common.c +7 -0
index 93d8196b381e..484a4fc31ef7 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,
                 return 0;
         }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return -EALREADY;
+       }
+
+       if (!enable) {

drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 92

Quoted 10 lines
drivers/power/regulator/regulator_common.c +10 -0
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;
+                       return -EBUSY;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */
+                       return -EALREADY;
+               }
+       }
+

Eugen,

Quoted 1 line

Thank you for working on this series!

Quoted 8 lines

Hi Tim,

Thank you for testing and having a look on my patches.

Quoted 3 lines

I wonder if instead of returning a failure you should print an error here but return 0 in order to not break unbalanced regulator calls

Initially I did that, but Simon forced me to return error as you see now.

If you want to have a special function that consumes errors, you can add one, e.g. that wraps the one that does report errors. My objection is when something goes wrong and it is silently ignored, since it tends to make problems build up, people add work-arounds, etc.

Quoted 19 lines
Quoted 7 lines

(like what is done in clk_disable()). I see that you have another patch in this series which handles htis for regulator_set_enable_if_allowed() but that doesn't cover drivers that call regulator_common_set_enable() directly such as drivers/power/regulator/fixed.c and drivers/power/regulator/gpio-regulator.c.

I believe Jonas (in CC) fixed those with a patch, but at the moment I am lost in providing you a link to it

Quoted 8 lines

I know there is an unbalanced call currently on imx8mm that this patch causes a failure where it previously did not:
u-boot=> usb start && usb tree
starting USB...
Bus usb@32e40000: Bus usb@32e50000: Error enabling VBUS supply (ret=-114) probe failed, error -114

Quoted 5 lines

That's a good catch.
I expected such things would happen if I would return error in those cases, so it might be that this is not the only case.
If you are able to test that board, do you wish me to send you a patch that changes the code to using regulator_set_enable_if_allowed() ?

Regards,
Simon