gpiolib: allow multiple lookup tables per consumer

The GPIO machine lookup mechanism dates back to old ARM board files
where lookup tables would all be defined in a single place. Since then,
lookup tables have also been used to address various DT and ACPI quirks
like missing GPIOs and - as of recently - setting up shared GPIO proxy
devices.

The lookup itself stops when we find the first matching table for a
consumer and - if it doesn't contain the right entry - the lookup fails.

Since the tables can now be defined in multiple places and we can't know
how many there are, effectively limiting a consumer to only a single
lookup table is no longer viable.

Rework the code to always look through all tables matching the consumer.

Link: https://lore.kernel.org/r/20251222-gpio-shared-reset-gpio-proxy-v1-1-8d4bba7d8c14@oss.qualcomm.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@oss.qualcomm.com>
This commit is contained in:
Bartosz Golaszewski 2025-12-22 11:01:26 +01:00
parent a05543d6b0
commit 9700b0fccf

View File

@ -4508,45 +4508,41 @@ void gpiod_remove_hogs(struct gpiod_hog *hogs)
} }
EXPORT_SYMBOL_GPL(gpiod_remove_hogs); EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev) static bool gpiod_match_lookup_table(struct device *dev,
const struct gpiod_lookup_table *table)
{ {
const char *dev_id = dev ? dev_name(dev) : NULL; const char *dev_id = dev ? dev_name(dev) : NULL;
struct gpiod_lookup_table *table;
list_for_each_entry(table, &gpio_lookup_list, list) { lockdep_assert_held(&gpio_lookup_lock);
if (table->dev_id && dev_id) { if (table->dev_id && dev_id) {
/* /*
* Valid strings on both ends, must be identical to have * Valid strings on both ends, must be identical to have
* a match * a match
*/ */
if (!strcmp(table->dev_id, dev_id)) if (!strcmp(table->dev_id, dev_id))
return table; return true;
} else { } else {
/* /*
* One of the pointers is NULL, so both must be to have * One of the pointers is NULL, so both must be to have
* a match * a match
*/ */
if (dev_id == table->dev_id) if (dev_id == table->dev_id)
return table; return true;
}
} }
return NULL; return false;
} }
static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id, static struct gpio_desc *gpio_desc_table_match(struct device *dev, const char *con_id,
unsigned int idx, unsigned long *flags) unsigned int idx, unsigned long *flags,
struct gpiod_lookup_table *table)
{ {
struct gpio_desc *desc = ERR_PTR(-ENOENT); struct gpio_desc *desc;
struct gpiod_lookup_table *table;
struct gpiod_lookup *p; struct gpiod_lookup *p;
struct gpio_chip *gc; struct gpio_chip *gc;
guard(mutex)(&gpio_lookup_lock); lockdep_assert_held(&gpio_lookup_lock);
table = gpiod_find_lookup_table(dev);
if (!table)
return desc;
for (p = &table->table[0]; p->key; p++) { for (p = &table->table[0]; p->key; p++) {
/* idx must always match exactly */ /* idx must always match exactly */
@ -4600,6 +4596,29 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
return desc; return desc;
} }
return NULL;
}
static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
unsigned int idx, unsigned long *flags)
{
struct gpio_desc *desc = ERR_PTR(-ENOENT);
struct gpiod_lookup_table *table;
guard(mutex)(&gpio_lookup_lock);
list_for_each_entry(table, &gpio_lookup_list, list) {
if (!gpiod_match_lookup_table(dev, table))
continue;
desc = gpio_desc_table_match(dev, con_id, idx, flags, table);
if (!desc)
continue;
/* On IS_ERR() or match. */
return desc;
}
return desc; return desc;
} }
@ -4610,16 +4629,18 @@ static int platform_gpio_count(struct device *dev, const char *con_id)
unsigned int count = 0; unsigned int count = 0;
scoped_guard(mutex, &gpio_lookup_lock) { scoped_guard(mutex, &gpio_lookup_lock) {
table = gpiod_find_lookup_table(dev); list_for_each_entry(table, &gpio_lookup_list, list) {
if (!table) if (!gpiod_match_lookup_table(dev, table))
return -ENOENT; continue;
for (p = &table->table[0]; p->key; p++) { for (p = &table->table[0]; p->key; p++) {
if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) || if ((con_id && p->con_id &&
!strcmp(con_id, p->con_id)) ||
(!con_id && !p->con_id)) (!con_id && !p->con_id))
count++; count++;
} }
} }
}
if (!count) if (!count)
return -ENOENT; return -ENOENT;