Skip to content

Commit eb374f7

Browse files
author
Bartosz Golaszewski
committed
gpio: provide gpiod_is_shared()
Provide an interface allowing consumers to check if a GPIO descriptor represents a GPIO that can potentially be shared by multiple consumers at the same time. This is exposed to allow subsystems that already work around the limitations of the current non-exclusive GPIO handling in some ways, to gradually convert to relying on the new shared GPIO feature of GPIOLIB. Extend the gpiolib-shared module to mark the GPIO shared proxy descriptors with a flag checked by the new interface. 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-6-b51f97b1abd8@linaro.org Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
1 parent 1e4f6db commit eb374f7

4 files changed

Lines changed: 48 additions & 0 deletions

File tree

drivers/gpio/gpiolib-shared.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,24 @@ int gpio_device_setup_shared(struct gpio_device *gdev)
314314

315315
guard(mutex)(&gpio_shared_lock);
316316

317+
list_for_each_entry(entry, &gpio_shared_list, list) {
318+
list_for_each_entry(ref, &entry->refs, list) {
319+
if (gdev->dev.parent == &ref->adev.dev) {
320+
/*
321+
* This is a shared GPIO proxy. Mark its
322+
* descriptor as such and return here.
323+
*/
324+
__set_bit(GPIOD_FLAG_SHARED_PROXY,
325+
&gdev->descs[0].flags);
326+
return 0;
327+
}
328+
}
329+
}
330+
331+
/*
332+
* This is not a shared GPIO proxy but it still may be the device
333+
* exposing shared pins. Find them and create the proxy devices.
334+
*/
317335
list_for_each_entry(entry, &gpio_shared_list, list) {
318336
if (!device_match_fwnode(&gdev->dev, entry->fwnode))
319337
continue;

drivers/gpio/gpiolib.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3990,6 +3990,26 @@ int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
39903990
}
39913991
EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
39923992

3993+
/**
3994+
* gpiod_is_shared() - check if this GPIO can be shared by multiple consumers
3995+
* @desc: GPIO to inspect
3996+
*
3997+
* Returns:
3998+
* True if this GPIO can be shared by multiple consumers at once. False if it's
3999+
* a regular, exclusive GPIO.
4000+
*
4001+
* Note:
4002+
* This function returning true does not mean that this GPIO is currently being
4003+
* shared. It means the GPIO core has registered the fact that the firmware
4004+
* configuration indicates that it can be shared by multiple consumers and is
4005+
* in charge of arbitrating the access.
4006+
*/
4007+
bool gpiod_is_shared(const struct gpio_desc *desc)
4008+
{
4009+
return test_bit(GPIOD_FLAG_SHARED_PROXY, &desc->flags);
4010+
}
4011+
EXPORT_SYMBOL_GPL(gpiod_is_shared);
4012+
39934013
/**
39944014
* gpiod_to_irq() - return the IRQ corresponding to a GPIO
39954015
* @desc: gpio whose IRQ will be returned (already requested)

drivers/gpio/gpiolib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ struct gpio_desc {
205205
#define GPIOD_FLAG_EVENT_CLOCK_REALTIME 18 /* GPIO CDEV reports REALTIME timestamps in events */
206206
#define GPIOD_FLAG_EVENT_CLOCK_HTE 19 /* GPIO CDEV reports hardware timestamps in events */
207207
#define GPIOD_FLAG_SHARED 20 /* GPIO is shared by multiple consumers */
208+
#define GPIOD_FLAG_SHARED_PROXY 21 /* GPIO is a virtual proxy to a physically shared pin. */
208209

209210
/* Connection label */
210211
struct gpio_desc_label __rcu *label;

include/linux/gpio/consumer.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ int gpiod_cansleep(const struct gpio_desc *desc);
167167
int gpiod_to_irq(const struct gpio_desc *desc);
168168
int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name);
169169

170+
bool gpiod_is_shared(const struct gpio_desc *desc);
171+
170172
/* Convert between the old gpio_ and new gpiod_ interfaces */
171173
struct gpio_desc *gpio_to_desc(unsigned gpio);
172174
int desc_to_gpio(const struct gpio_desc *desc);
@@ -520,6 +522,13 @@ static inline int gpiod_set_consumer_name(struct gpio_desc *desc,
520522
return -EINVAL;
521523
}
522524

525+
static inline bool gpiod_is_shared(const struct gpio_desc *desc)
526+
{
527+
/* GPIO can never have been requested */
528+
WARN_ON(desc);
529+
return false;
530+
}
531+
523532
static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
524533
{
525534
return NULL;

0 commit comments

Comments
 (0)