]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
regulator: implement basic reference counter
authorEugen Hristev <eugen.hristev@collabora.com>
Wed, 19 Apr 2023 13:45:24 +0000 (16:45 +0300)
committerJaehoon Chung <jh80.chung@samsung.com>
Tue, 4 Jul 2023 02:20:56 +0000 (11:20 +0900)
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 <eugen.hristev@collabora.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Patrice Chotard <patrice.chotard@foss.st.com>
drivers/power/regulator/regulator_common.c
drivers/power/regulator/regulator_common.h

index 93d8196b381ea5ec02b3c81c5da645ab6c67cd92..484a4fc31ef7584456ebe9c3cd614e9a205cb1cd 100644 (file)
@@ -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;
 }
index c10492f01675e38c4c31481abfa3ec1b0894fea6..0faab447d099e647b1e18afaf7fdc0c83f548241 100644 (file)
@@ -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);