Skip to content

Commit 9700b0f

Browse files
author
Bartosz Golaszewski
committed
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>
1 parent a05543d commit 9700b0f

1 file changed

Lines changed: 56 additions & 35 deletions

File tree

drivers/gpio/gpiolib.c

Lines changed: 56 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4508,45 +4508,41 @@ void gpiod_remove_hogs(struct gpiod_hog *hogs)
45084508
}
45094509
EXPORT_SYMBOL_GPL(gpiod_remove_hogs);
45104510

4511-
static struct gpiod_lookup_table *gpiod_find_lookup_table(struct device *dev)
4511+
static bool gpiod_match_lookup_table(struct device *dev,
4512+
const struct gpiod_lookup_table *table)
45124513
{
45134514
const char *dev_id = dev ? dev_name(dev) : NULL;
4514-
struct gpiod_lookup_table *table;
45154515

4516-
list_for_each_entry(table, &gpio_lookup_list, list) {
4517-
if (table->dev_id && dev_id) {
4518-
/*
4519-
* Valid strings on both ends, must be identical to have
4520-
* a match
4521-
*/
4522-
if (!strcmp(table->dev_id, dev_id))
4523-
return table;
4524-
} else {
4525-
/*
4526-
* One of the pointers is NULL, so both must be to have
4527-
* a match
4528-
*/
4529-
if (dev_id == table->dev_id)
4530-
return table;
4531-
}
4516+
lockdep_assert_held(&gpio_lookup_lock);
4517+
4518+
if (table->dev_id && dev_id) {
4519+
/*
4520+
* Valid strings on both ends, must be identical to have
4521+
* a match
4522+
*/
4523+
if (!strcmp(table->dev_id, dev_id))
4524+
return true;
4525+
} else {
4526+
/*
4527+
* One of the pointers is NULL, so both must be to have
4528+
* a match
4529+
*/
4530+
if (dev_id == table->dev_id)
4531+
return true;
45324532
}
45334533

4534-
return NULL;
4534+
return false;
45354535
}
45364536

4537-
static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4538-
unsigned int idx, unsigned long *flags)
4537+
static struct gpio_desc *gpio_desc_table_match(struct device *dev, const char *con_id,
4538+
unsigned int idx, unsigned long *flags,
4539+
struct gpiod_lookup_table *table)
45394540
{
4540-
struct gpio_desc *desc = ERR_PTR(-ENOENT);
4541-
struct gpiod_lookup_table *table;
4541+
struct gpio_desc *desc;
45424542
struct gpiod_lookup *p;
45434543
struct gpio_chip *gc;
45444544

4545-
guard(mutex)(&gpio_lookup_lock);
4546-
4547-
table = gpiod_find_lookup_table(dev);
4548-
if (!table)
4549-
return desc;
4545+
lockdep_assert_held(&gpio_lookup_lock);
45504546

45514547
for (p = &table->table[0]; p->key; p++) {
45524548
/* idx must always match exactly */
@@ -4600,6 +4596,29 @@ static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
46004596
return desc;
46014597
}
46024598

4599+
return NULL;
4600+
}
4601+
4602+
static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
4603+
unsigned int idx, unsigned long *flags)
4604+
{
4605+
struct gpio_desc *desc = ERR_PTR(-ENOENT);
4606+
struct gpiod_lookup_table *table;
4607+
4608+
guard(mutex)(&gpio_lookup_lock);
4609+
4610+
list_for_each_entry(table, &gpio_lookup_list, list) {
4611+
if (!gpiod_match_lookup_table(dev, table))
4612+
continue;
4613+
4614+
desc = gpio_desc_table_match(dev, con_id, idx, flags, table);
4615+
if (!desc)
4616+
continue;
4617+
4618+
/* On IS_ERR() or match. */
4619+
return desc;
4620+
}
4621+
46034622
return desc;
46044623
}
46054624

@@ -4610,14 +4629,16 @@ static int platform_gpio_count(struct device *dev, const char *con_id)
46104629
unsigned int count = 0;
46114630

46124631
scoped_guard(mutex, &gpio_lookup_lock) {
4613-
table = gpiod_find_lookup_table(dev);
4614-
if (!table)
4615-
return -ENOENT;
4632+
list_for_each_entry(table, &gpio_lookup_list, list) {
4633+
if (!gpiod_match_lookup_table(dev, table))
4634+
continue;
46164635

4617-
for (p = &table->table[0]; p->key; p++) {
4618-
if ((con_id && p->con_id && !strcmp(con_id, p->con_id)) ||
4619-
(!con_id && !p->con_id))
4620-
count++;
4636+
for (p = &table->table[0]; p->key; p++) {
4637+
if ((con_id && p->con_id &&
4638+
!strcmp(con_id, p->con_id)) ||
4639+
(!con_id && !p->con_id))
4640+
count++;
4641+
}
46214642
}
46224643
}
46234644

0 commit comments

Comments
 (0)