Skip to contentSkip to conversation

regulator: implement basic reference counter

PATCH Superseded Eugen Hristev 2 participants 3 messages Mar 16, 2023 – Mar 27, 2023

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
 drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
 drivers/power/regulator/regulator_common.h |  1 +
 2 files changed, 23 insertions(+)
drivers/power/regulator/regulator_common.c +22 -0
index 93d8196b381e..edda25336176 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 0;
+	}
+
+	if (!enable) {
+		if (dev_pdata->enable_count > 1) {
+			/* If enabled multiple times, decrease count */
+			dev_pdata->enable_count--;
+			return 0;
+		} else if (!dev_pdata->enable_count) {
+			/* If already disabled, do nothing */
+			return 0;
+		}
+	}
+
 	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 +1 -0
index c10492f01675..8167bc866261 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,
Signature · 1 line
2.34.1
#1 lore raw

Hi Eugen,

Quoted 9 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.

Quoted 5 lines
---
 drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
 drivers/power/regulator/regulator_common.h |  1 +
 2 files changed, 23 insertions(+)

This seems reasonable to me. I mostly worry about visibility, since it returns 0 in all cases so if you do actually want to shut it down, it silently ignored you in some situations. I've made some suggestions below.

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

Quoted 6 lines
drivers/power/regulator/regulator_common.c +0 -0
index 93d8196b381e..edda25336176 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,

Please update the docs for this function in the header file.

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

Quoted 7 lines
drivers/power/regulator/regulator_common.c +4 -0
                return 0;
        }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return 0;

Should we return -EALREADY ?

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

Quoted 6 lines
drivers/power/regulator/regulator_common.c +6 -0
+       }
+
+       if (!enable) {
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;

Return -EBUSY ?

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

Quoted 3 lines
drivers/power/regulator/regulator_common.c +3 -0
+                       return 0;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */

Is this case an error? Should we return -EPERM ?

Quoted 31 lines
drivers/power/regulator/regulator_common.c +9 -0
+                       return 0;
+               }
+       }
+
        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 +1 -1
index c10492f01675..8167bc866261 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,
--
Quoted 2 lines

2.34.1

Also I would appreciate a patch to rename dev_pdata to plat if you have time.

Regards,
Simon

#2 lore raw
Quoted 24 lines · Simon Glass

Hi Eugen,

Quoted 9 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.

Quoted 5 lines
---
  drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++
  drivers/power/regulator/regulator_common.h |  1 +
  2 files changed, 23 insertions(+)

This seems reasonable to me. I mostly worry about visibility, since it returns 0 in all cases so if you do actually want to shut it down, it silently ignored you in some situations. I've made some suggestions below.

Quoted 1 line
Quoted 7 lines

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

Quoted 5 lines
drivers/power/regulator/regulator_common.c +0 -0
index 93d8196b381e..edda25336176 100644
@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,

Please update the docs for this function in the header file.

Okay

Quoted 10 lines

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

Quoted 8 lines
drivers/power/regulator/regulator_common.c +4 -0
                 return 0;
         }
 
+       /* If previously enabled, increase count */
+       if (enable && dev_pdata->enable_count > 0) {
+               dev_pdata->enable_count++;
+               return 0;

Should we return -EALREADY ?

I think not. It's not an error, we should just increase count, the caller should be told that there is no error happening.

Quoted 9 lines

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

Quoted 7 lines
drivers/power/regulator/regulator_common.c +6 -0
+       }
+
+       if (!enable) {
+               if (dev_pdata->enable_count > 1) {
+                       /* If enabled multiple times, decrease count */
+                       dev_pdata->enable_count--;

Return -EBUSY ?

Same goes here. The disable was successful, hence no error, but the regulator is not turned off , because someone else is still using it.

Quoted 6 lines

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

Quoted 4 lines
drivers/power/regulator/regulator_common.c +3 -0
+                       return 0;
+               } else if (!dev_pdata->enable_count) {
+                       /* If already disabled, do nothing */

Is this case an error? Should we return -EPERM ?

Maybe here we could return an error, saying that it's already disabled, but since it's a shared resource, this could tell the caller whether someone else is using the resource, and I don't think we should leak this information. It might be used in a way to find out information about the state of the system which might be a security problem ? I think that a shared resource usage should be transparent to the caller , such that only the driver should manage it's power on/off and know who is using it.

Do you agree ?

Quoted 30 lines
Quoted 30 lines
drivers/power/regulator/regulator_common.c +9 -0
+                       return 0;
+               }
+       }
+
         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 +1 -0
index c10492f01675..8167bc866261 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;
  };
 
Quoted 6 lines
Quoted 4 lines
drivers/power/regulator/regulator_common.h +0 -1
  int regulator_common_of_to_plat(struct udevice *dev,
--

2.34.1

Also I would appreciate a patch to rename dev_pdata to plat if you have time.

I will have a look

Quoted 3 lines

Regards,
Simon