]> Gentwo Git Trees - linux/.git/commitdiff
gpiolib: support shared GPIOs in core subsystem code
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 12 Nov 2025 13:55:34 +0000 (14:55 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Mon, 17 Nov 2025 09:16:51 +0000 (10:16 +0100)
As the final step in adding official support for shared GPIOs, enable
the previously added elements in core GPIO subsystem code. Set-up shared
GPIOs when adding a GPIO chip, tear it down on removal and check if a
GPIO descriptor looked up during the firmware-node stage is shared and
fall-back to machine lookup in this case.

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/20251112-gpio-shared-v4-5-b51f97b1abd8@linaro.org
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
drivers/gpio/gpiolib.c

index 9952e412da505c12ceddfefd9cfd778aad3bd5e6..1e4c991797126d0d91233bbe7c3ae3831bf77d6c 100644 (file)
@@ -37,6 +37,7 @@
 #include "gpiolib-acpi.h"
 #include "gpiolib-cdev.h"
 #include "gpiolib-of.h"
+#include "gpiolib-shared.h"
 #include "gpiolib-swnode.h"
 #include "gpiolib-sysfs.h"
 #include "gpiolib.h"
@@ -1200,6 +1201,10 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
        if (ret)
                goto err_remove_irqchip_mask;
 
+       ret = gpio_device_setup_shared(gdev);
+       if (ret)
+               goto err_remove_irqchip;
+
        /*
         * By first adding the chardev, and then adding the device,
         * we get a device node entry in sysfs under
@@ -1211,10 +1216,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
        if (gpiolib_initialized) {
                ret = gpiochip_setup_dev(gdev);
                if (ret)
-                       goto err_remove_irqchip;
+                       goto err_teardown_shared;
        }
+
        return 0;
 
+err_teardown_shared:
+       gpio_device_teardown_shared(gdev);
 err_remove_irqchip:
        gpiochip_irqchip_remove(gc);
 err_remove_irqchip_mask:
@@ -1283,6 +1291,7 @@ void gpiochip_remove(struct gpio_chip *gc)
        /* Numb the device, cancelling all outstanding operations */
        rcu_assign_pointer(gdev->chip, NULL);
        synchronize_srcu(&gdev->srcu);
+       gpio_device_teardown_shared(gdev);
        gpiochip_irqchip_remove(gc);
        acpi_gpiochip_remove(gc);
        of_gpiochip_remove(gc);
@@ -4652,11 +4661,29 @@ struct gpio_desc *gpiod_find_and_request(struct device *consumer,
        scoped_guard(srcu, &gpio_devices_srcu) {
                desc = gpiod_fwnode_lookup(fwnode, consumer, con_id, idx,
                                           &flags, &lookupflags);
+               if (!IS_ERR_OR_NULL(desc) &&
+                   test_bit(GPIOD_FLAG_SHARED, &desc->flags)) {
+                       /*
+                        * We're dealing with a GPIO shared by multiple
+                        * consumers. This is the moment to add the machine
+                        * lookup table for the proxy device as previously
+                        * we only knew the consumer's fwnode.
+                        */
+                       ret = gpio_shared_add_proxy_lookup(consumer, lookupflags);
+                       if (ret)
+                               return ERR_PTR(ret);
+
+                       /* Trigger platform lookup for shared GPIO proxy. */
+                       desc = ERR_PTR(-ENOENT);
+                       /* Trigger it even for fwnode-only gpiod_get(). */
+                       platform_lookup_allowed = true;
+               }
+
                if (gpiod_not_found(desc) && platform_lookup_allowed) {
                        /*
                         * Either we are not using DT or ACPI, or their lookup
-                        * did not return a result. In that case, use platform
-                        * lookup as a fallback.
+                        * did not return a result or this is a shared GPIO. In
+                        * that case, use platform lookup as a fallback.
                         */
                        dev_dbg(consumer,
                                "using lookup tables for GPIO lookup\n");
@@ -4679,14 +4706,19 @@ struct gpio_desc *gpiod_find_and_request(struct device *consumer,
                        return ERR_PTR(ret);
 
                /*
-                * This happens when there are several consumers for
-                * the same GPIO line: we just return here without
-                * further initialization. It is a bit of a hack.
-                * This is necessary to support fixed regulators.
+                * This happens when there are several consumers for the same
+                * GPIO line: we just return here without further
+                * initialization. It's a hack introduced long ago to support
+                * fixed regulators. We now have a better solution with
+                * automated scanning where affected platforms just need to
+                * select the provided Kconfig option.
                 *
-                * FIXME: Make this more sane and safe.
+                * FIXME: Remove the GPIOD_FLAGS_BIT_NONEXCLUSIVE flag after
+                * making sure all platforms use the new mechanism.
                 */
-               dev_info(consumer, "nonexclusive access to GPIO for %s\n", name);
+               dev_info(consumer,
+                        "nonexclusive access to GPIO for %s, consider updating your code to using gpio-shared-proxy\n",
+                        name);
                return desc;
        }