regulator/gpio: Allow nonexclusive GPIO access
authorLinus Walleij <linus.walleij@linaro.org>
Fri, 12 Oct 2018 12:54:12 +0000 (14:54 +0200)
committerMark Brown <broonie@kernel.org>
Fri, 12 Oct 2018 16:55:02 +0000 (18:55 +0200)
This allows nonexclusive (simultaneous) access to a single
GPIO line for the fixed regulator enable line. This happens
when several regulators use the same GPIO for enabling and
disabling a regulator, and all need a handle on their GPIO
descriptor.

This solution with a special flag is not entirely elegant
and should ideally be replaced by something more careful as
this makes it possible for several consumers to
enable/disable the same GPIO line to the left and right
without any consistency. The current use inside the regulator
core should however be fine as it takes special care to
handle this.

For the state of the GPIO backend, this is still the
lesser evil compared to going back to global GPIO
numbers.

Cc: Marek Szyprowski <m.szyprowski@samsung.com>
Cc: Jon Hunter <jonathanh@nvidia.com>
Fixes: efdfeb079cc3 ("regulator: fixed: Convert to use GPIO descriptor only")
Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Tested-by: Jon Hunter <jonathanh@nvidia.com>
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/gpio/gpiolib.c
drivers/regulator/fixed.c
include/linux/gpio/consumer.h

index e8f8a199939350a97754031781d3733849a49525..8228306aba467c5277a2f0210ebd6e60283a7401 100644 (file)
@@ -3908,8 +3908,23 @@ struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
         * the device name as label
         */
        status = gpiod_request(desc, con_id ? con_id : devname);
-       if (status < 0)
-               return ERR_PTR(status);
+       if (status < 0) {
+               if (status == -EBUSY && flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) {
+                       /*
+                        * This happens when there are several consumers for
+                        * the same GPIO line: we just return here without
+                        * further initialization. It is a bit if a hack.
+                        * This is necessary to support fixed regulators.
+                        *
+                        * FIXME: Make this more sane and safe.
+                        */
+                       dev_info(dev, "nonexclusive access to GPIO for %s\n",
+                                con_id ? con_id : devname);
+                       return desc;
+               } else {
+                       return ERR_PTR(status);
+               }
+       }
 
        status = gpiod_configure_flags(desc, con_id, lookupflags, flags);
        if (status < 0) {
index 7d639ad953b6114c29b0f6dfdc0b4c87ebc4bacb..ccc29038f19a98c8d7b292ab60536cc616345c0a 100644 (file)
@@ -170,6 +170,19 @@ static int reg_fixed_voltage_probe(struct platform_device *pdev)
                        gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
        }
 
+       /*
+        * Some fixed regulators share the enable line between two
+        * regulators which makes it necessary to get a handle on the
+        * same descriptor for two different consumers. This will get
+        * the GPIO descriptor, but only the first call will initialize
+        * it so any flags such as inversion or open drain will only
+        * be set up by the first caller and assumed identical on the
+        * next caller.
+        *
+        * FIXME: find a better way to deal with this.
+        */
+       gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
+
        cfg.ena_gpiod = devm_gpiod_get_optional(&pdev->dev, NULL, gflags);
        if (IS_ERR(cfg.ena_gpiod))
                return PTR_ERR(cfg.ena_gpiod);
index 21ddbe4400308ee51e8b29fe85acfe08022879ec..33695a1d8b35bf32b2eb3566513b3a16236fc151 100644 (file)
@@ -30,6 +30,7 @@ struct gpio_descs {
 #define GPIOD_FLAGS_BIT_DIR_OUT                BIT(1)
 #define GPIOD_FLAGS_BIT_DIR_VAL                BIT(2)
 #define GPIOD_FLAGS_BIT_OPEN_DRAIN     BIT(3)
+#define GPIOD_FLAGS_BIT_NONEXCLUSIVE   BIT(4)
 
 /**
  * Optional flags that can be passed to one of gpiod_* to configure direction