regulator: implement basic reference counter
PATCH v2 1/2 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 v2:
- add info in header regarding the function
drivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++ drivers/power/regulator/regulator_common.h | 13 +++++++++++++ 2 files changed, 35 insertions(+)
drivers/power/regulator/regulator_common.c +22 -0
@@ -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 +13 -0
@@ -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,18 @@ 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 +*/ int regulator_common_set_enable (const struct udevice *dev,struct regulator_common_plat *dev_pdata,bool enable);
Signature · 1 line
2.34.1
PATCH v2 2/2 regulator: rename dev_pdata to plat
Simplify the subsystem by renaming dev_pdata to just plat. No functional change, just trivial renaming.
---
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
@@ -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
@@ -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
@@ -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,10 +30,10 @@ 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 */ 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
Hi Eugen,
Quoted 27 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.Signed-off-by Eugen Hristev <[email protected]>---
Changes in v2:
- add info in header regarding the functiondrivers/power/regulator/regulator_common.c | 22 ++++++++++++++++++++++ drivers/power/regulator/regulator_common.h | 13 +++++++++++++ 2 files changed, 35 insertions(+)drivers/power/regulator/regulator_common.c +1
-0@@ -73,6 +73,23 @@ int regulator_common_set_enable(const struct udevice *dev,return 0 ; } +/* If previously enabled, increase count */
drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 86
Quoted 10 lines
drivers/power/regulator/regulator_common.c +10
-0+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 ;
Perhaps -EBUSY for this?
Quoted 10 lines · You
drivers/power/regulator/regulator_common.c +0
-0Maybe 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 ?
No, that doesn't seem like a good argument. Suppressing an error for security reasons?
Any driver can access any other in the system. This is a bootloader, not an OS. Knowing the state of the system is important. Supressing errors creates a lot of confusion and makes bugs hard to track.
drivers/power/regulator/regulator_common.c int regulator_common_set_enable(const struct udevice *dev, · line 89
Quoted 3 lines
drivers/power/regulator/regulator_common.c +3
-0+ }else if (!dev_pdata->enable_count) { +/* If already disabled, do nothing */ +return 0 ;
This is a bug, so we should return -EALREADY or something else here. If this error code is reported, we need to fix the bug.
Quoted 34 lines
drivers/power/regulator/regulator_common.c +8
-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 +2
-0@@ -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,18 @@ 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); +/*
drivers/power/regulator/regulator_common.h int regulator_common_of_to_plat(struct udevice *dev, · line 34
Quoted 10 lines
drivers/power/regulator/regulator_common.h +10
-0+* 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
Returns
Quoted 7 lines
drivers/power/regulator/regulator_common.h +1
-1+*/ int regulator_common_set_enable (const struct udevice *dev,struct regulator_common_plat *dev_pdata,bool enable); --2.34.1
Regards,
Simon