gpio: better lookup method for platform GPIOs
Subsystems (2)
PATCH 1/1 gpio: better lookup method for platform GPIOs
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, the con_id argument of gpiod_get() will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
---
Let's change this bad design before more users start using it. ;)
Documentation/gpio/board.txt | 25 ++++++----- drivers/gpio/gpiolib.c | 99 ++++++++++++++++++++++---------------------- include/linux/gpio/driver.h | 22 +++++----- 3 files changed, 76 insertions(+), 70 deletions(-)
Documentation/gpio/board.txt +15 -10
@@ -72,10 +72,11 @@ where - chip_label is the label of the gpiod_chip instance providing the GPIO - chip_hwnum is the hardware number of the GPIO within the chip - - dev_id is the identifier of the device that will make use of this GPIO. If - NULL, the GPIO will be available to all devices. + - dev_id is the identifier of the device that will make use of this GPIO. It + can be NULL, in which case it will be matched for calls to gpiod_get() + with a NULL device. - con_id is the name of the GPIO function from the device point of view. It - can be NULL. + can be NULL, in which case it will match any function. - idx is the index of the GPIO within the function. - flags is defined to specify the following properties: * GPIOF_ACTIVE_LOW - to configure the GPIO as active-low
@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +}; And the table can be added by the board code as follows: - gpiod_add_table(gpios_table, ARRAY_SIZE(gpios_table)); + gpiod_add_lookup_table(&gpios_table); The driver controlling "foo.0" will then be able to obtain its GPIOs as follows:
drivers/gpio/gpiolib.c +50 -49
140 lines not on this page · raw message
include/linux/gpio/driver.h +11 -11
@@ -140,7 +140,6 @@ enum gpio_lookup_flags {* platform data. */ struct gpiod_lookup { -struct list_head list;/* * name of the chip the GPIO belongs to */
@@ -150,10 +149,6 @@ struct gpiod_lookup {*/ u16 chip_hwnum;/* -* name of device that can claim this GPIO -*/ -const char *dev_id; -/* * name of the GPIO from the device's point of view */ const char *con_id;
@@ -167,28 +162,33 @@ struct gpiod_lookup { enum gpio_lookup_flags flags; }; +struct gpiod_lookup_table { +struct list_head list; +const char *dev_id; +unsigned int size; +struct gpiod_lookup table[]; +}; +/* * Simple definition of a single GPIO under a con_id */ -#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \ - GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags) +#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _con_id, _flags) \ + GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags)/* * Use this macro if you need to have several GPIOs under the same con_id. * Each GPIO needs to use a different index and can be accessed using * gpiod_get_index() */ -#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \ - _flags) \ +#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _con_id, _idx, _flags) \ { \ .chip_label = _chip_label, \ .chip_hwnum = _chip_hwnum, \ - .dev_id = _dev_id, \ .con_id = _con_id, \ .idx = _idx, \ .flags = _flags, \ } -void gpiod_add_table (struct gpiod_lookup *table,size_t size); +void gpiod_add_lookup_table (struct gpiod_lookup_table *table);#endif
Signature · 1 line
1.8.4.2
Quoted 17 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
Quoted 3 lines
Signed-off-by Alexandre Courbot <[email protected]>---
Let's change this bad design before more users start using it. ;)
OK given that Mika has based his patches on this I'll request that he ACK this and then I'll merge this so that he can put this patch at the bottom of his patch stack.
Mika: requesting ACK.
Yours,
Linus Walleij
Quoted 1 line · Linus Walleij
On Thu, Nov 28, 2013 at 9:46 AM, Alexandre Courbot <[email protected]> wrote:
[]
Quoted 3 lines
Quoted 1 line
Let's change this bad design before more users start using it. ;)
OK given that Mika has based his patches on this
Not only him, since I\have started to look for SFI GPIO solution :-)
P.S. Whenever Mika ACKs I will agree with that.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Mika Westerberg
Acked-byQuoted 23 lines · Linus Walleij
Quoted 17 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.Quoted 3 lines
Signed-off-by Alexandre Courbot <[email protected]>---
Let's change this bad design before more users start using it. ;)
Quoted 5 lines
OK given that Mika has based his patches on this I'll request that he ACK this and then I'll merge this so that he can put this patch at the bottom of his patch stack.
Mika: requesting ACK.
Alexandre, please check Andy's comments.
Apart from that this looks good to me.
Quoted 9 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
Quoted 7 lines
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
Thanks for that, since I'm also was a bit confused of those dev_id/con_id stuff. Few comments below (mostly about style).
Quoted 11 lines
Documentation/gpio/board.txt +0
-4@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH),
Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 99
Quoted 10 lines
Documentation/gpio/board.txt +8
-2- GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW),
Can you use deeper indentation for GPIO_* lines here?
Quoted 14 lines
drivers/gpio/gpiolib.c +1
-4@@ -2326,72 +2322,77 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,return desc; } -static struct gpio_desc *gpiod_find (struct device *dev,const char *con_id, -unsigned int idx, -enum gpio_lookup_flags *flags) +static struct gpiod_lookup_table *gpiod_find_lookup_table (struct device *dev) {const char *dev_id = dev ?dev_name (dev) :NULL ; -struct gpio_desc *desc =ERR_PTR (-ENODEV );
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2333
Quoted 10 lines
drivers/gpio/gpiolib.c +3
-4-unsigned int match, best =0 ; -struct gpiod_lookup *p; +struct gpiod_lookup_table *table;mutex_lock (&gpio_lookup_lock); -list_for_each_entry (p, &gpio_lookup_list, list) { - match =0 ; +list_for_each_entry (table, &gpio_lookup_list, list) { +if (table->dev_id && dev_id &&strcmp (table->dev_id, dev_id))
Maybe check !dev_id outside of loop?
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2339
Quoted 11 lines
drivers/gpio/gpiolib.c +4
-5+continue ; -if (p->dev_id) { -if (!dev_id ||strcmp (p->dev_id, dev_id)) -continue ; +if (dev_id != table->dev_id) +continue ; - match +=2 ; - } +return table;
What about
if (dev_id == table->dev_id)
return table;
?
Quoted 15 lines
drivers/gpio/gpiolib.c +5
-7+ } -if (p->con_id) { -if (!con_id ||strcmp (p->con_id, con_id)) -continue ; +mutex_unlock (&gpio_lookup_lock); - match +=1 ; - } +return NULL ; +} -if (p->idx != idx) -continue ; +static struct gpio_desc *gpiod_find(struct device *dev, const char *con_id,
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2355
Quoted 10 lines
drivers/gpio/gpiolib.c +6
-2+unsigned int idx, + enum gpio_lookup_flags *flags) +{ + struct gpio_desc *desc = ERR_PTR(-ENODEV); +struct gpiod_lookup_table *table; +int i; -if (match > best) { -struct gpio_chip *chip;
Looks like redundant empty line.
Quoted 12 lines
drivers/gpio/gpiolib.c +5
-6- chip =find_chip_by_name (p->chip_label); + table =gpiod_find_lookup_table (dev); +if (!table) +return desc; -if (!chip) { -dev_warn (dev,"cannot find GPIO chip %s\n" , - p->chip_label); -continue ; - } +for (i =0 ; i < table->size; i++) { +struct gpio_chip *chip;
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2368
Quoted 10 lines
drivers/gpio/gpiolib.c +6
-3+struct gpiod_lookup *p = &table->table[i]; -if (chip->ngpio <= p->chip_hwnum) { -dev_warn (dev,"GPIO chip %s has %d GPIOs\n" , - chip->label, chip->ngpio); +if (p->idx != idx) +continue ; + +if (p->con_id) { +if (!con_id ||strcmp (p->con_id, con_id))
Could be one 'if' and moreover !con_id check might be outside a loop.
Quoted 32 lines
drivers/gpio/gpiolib.c +15
-9continue ; - } + } - desc =gpio_to_desc (chip->base + p->chip_hwnum); - *flags = p->flags; + chip =find_chip_by_name (p->chip_label); -if (match !=3 ) - best = match; -else -break ; +if (!chip) { +dev_warn (dev,"cannot find GPIO chip %s\n" , + p->chip_label); +continue ; } - } -mutex_unlock (&gpio_lookup_lock); +if (chip->ngpio <= p->chip_hwnum) { +dev_warn (dev,"GPIO chip %s has %d GPIOs\n" , + chip->label, chip->ngpio); +continue ; + } + + desc =gpiochip_offset_to_desc (chip, p->chip_hwnum); + *flags = p->flags; + }return desc; }
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 1 line · Alexandre Courbot
One missed, but not least, thing.
Quoted 8 lines
Quoted 8 lines
drivers/gpio/gpiolib.c +2
-3+static struct gpiod_lookup_table *gpiod_find_lookup_table (struct device *dev) {const char *dev_id = dev ?dev_name (dev) :NULL ; -struct gpio_desc *desc =ERR_PTR (-ENODEV ); -unsigned int match, best =0 ; -struct gpiod_lookup *p; +struct gpiod_lookup_table *table;
Quoted 5 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2332
Quoted 5 lines
mutex_lock(&gpio_lookup_lock);drivers/gpio/gpiolib.c +1
-2-list_for_each_entry (p, &gpio_lookup_list, list) { - match =0 ; +list_for_each_entry (table, &gpio_lookup_list, list) {
[]
Quoted 7 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2339
Quoted 6 lines
drivers/gpio/gpiolib.c +3
-2+if (dev_id != table->dev_id) +continue ; - match +=2 ; - } +return table;
Quoted 6 lines
What about
if (
dev_id==table->dev_id)
return table;?
And unlock mutex, of course! It seems you missed this in the first place.
Quoted 12 lines
Quoted 12 lines
drivers/gpio/gpiolib.c +4
-5+ } -if (p->con_id) { -if (!con_id ||strcmp (p->con_id, con_id)) -continue ; +mutex_unlock (&gpio_lookup_lock); - match +=1 ; - } +return NULL ; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 29 lines · Andy Shevchenko
Quoted 9 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
Quoted 7 lines
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.Thanks for that, since I'm also was a bit confused of those dev_id/con_id stuff. Few comments below (mostly about style).
Quoted 7 lines
Documentation/gpio/board.txt +0
-0@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows:
Quoted 16 lines
Quoted 4 lines
Documentation/gpio/board.txt +0
-4- struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH),Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 99
Quoted 10 lines
Documentation/gpio/board.txt +8
-2- GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW),Can you use deeper indentation for GPIO_* lines here?
Fixed.
Quoted 18 lines
Quoted 18 lines
drivers/gpio/gpiolib.c +2
-6@@ -2326,72 +2322,77 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,return desc; } -static struct gpio_desc *gpiod_find (struct device *dev,const char *con_id, -unsigned int idx, -enum gpio_lookup_flags *flags) +static struct gpiod_lookup_table *gpiod_find_lookup_table (struct device *dev) {const char *dev_id = dev ?dev_name (dev) :NULL ; -struct gpio_desc *desc =ERR_PTR (-ENODEV ); -unsigned int match, best =0 ; -struct gpiod_lookup *p; +struct gpiod_lookup_table *table;
Quoted 8 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2333
Quoted 6 lines
drivers/gpio/gpiolib.c +2
-2mutex_lock (&gpio_lookup_lock); -list_for_each_entry (p, &gpio_lookup_list, list) { - match =0 ; +list_for_each_entry (table, &gpio_lookup_list, list) { +if (table->dev_id && dev_id &&strcmp (table->dev_id, dev_id))Maybe check !dev_id outside of loop?
And create two loops, one for each case? Might complicate the code for little benefit IMHO, but please elaborate if I missed your point.
Quoted 13 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2339
Quoted 12 lines
drivers/gpio/gpiolib.c +4
-5+continue ; -if (p->dev_id) { -if (!dev_id ||strcmp (p->dev_id, dev_id)) -continue ; +if (dev_id != table->dev_id) +continue ; - match +=2 ; - } +return table;
Quoted 6 lines
What about
if (
dev_id==table->dev_id)
return table;?
Actually my algorithm is broken to start with - and dangerous, as the missed mutex_unlock() you spotted later testifies. I will rewrite it in a (hopefully) sounder way.
Quoted 8 lines
Quoted 8 lines
drivers/gpio/gpiolib.c +7
-0+static struct gpio_desc *gpiod_find (struct device *dev,const char *con_id, +unsigned int idx, +enum gpio_lookup_flags *flags) +{ +struct gpio_desc *desc =ERR_PTR (-ENODEV ); +struct gpiod_lookup_table *table; +int i;
Quoted 5 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2355
Quoted 3 lines
drivers/gpio/gpiolib.c +0
-2-if (match > best) { -struct gpio_chip *chip;Looks like redundant empty line.
Fixed.
Quoted 24 lines
Quoted 13 lines
drivers/gpio/gpiolib.c +5
-6- chip =find_chip_by_name (p->chip_label); + table =gpiod_find_lookup_table (dev); +if (!table) +return desc; -if (!chip) { -dev_warn (dev,"cannot find GPIO chip %s\n" , - p->chip_label); -continue ; - } +for (i =0 ; i < table->size; i++) { +struct gpio_chip *chip;drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2368
Quoted 10 lines
drivers/gpio/gpiolib.c +6
-3+struct gpiod_lookup *p = &table->table[i]; -if (chip->ngpio <= p->chip_hwnum) { -dev_warn (dev,"GPIO chip %s has %d GPIOs\n" , - chip->label, chip->ngpio); +if (p->idx != idx) +continue ; + +if (p->con_id) { +if (!con_id ||strcmp (p->con_id, con_id))
Quoted 1 line
Could be one 'if' and moreover !con_id check might be outside a loop.
Again, wouldn't that require two separate loops?
Hi,
Quoted 10 lines · Alexandre Courbot
Documentation/gpio/board.txt +0
-6@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - };
Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 101
Quoted 10 lines
Documentation/gpio/board.txt +10
-0+struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +};
Instead of using the size variable, wouldn't it be more clear to expect the array to be null terminated?
Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 105
Quoted 4 lines
Documentation/gpio/board.txt +1
-1And the table can be added by the board code as follows: - gpiod_add_table(gpios_table, ARRAY_SIZE(gpios_table)); + gpiod_add_lookup_table(&gpios_table);
Remove "&" from the above.
Thanks,
Signature · 1 line
heikki
Quoted 4 lines · Heikki Krogerus
Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 105
Quoted 2 lines
Documentation/gpio/board.txt +1
-1- gpiod_add_table(gpios_table, ARRAY_SIZE(gpios_table)); + gpiod_add_lookup_table(&gpios_table);Remove "&" from the above.
Sorry, ignore that :).
Signature · 1 line
heikki
Quoted 18 lines · Heikki Krogerus
Hi,
Quoted 15 lines · Alexandre Courbot
Documentation/gpio/board.txt +5
-6@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),
Quoted 8 lines
Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 101
Quoted 5 lines
Documentation/gpio/board.txt +5
-0+ GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +};Instead of using the size variable, wouldn't it be more clear to expect the array to be null terminated?
This is a zero-length array, its entries are not pointers but flattened lookup entries. Thus you cannot simply null-terminate it. It would be possible to use { NULL } as a terminator, but this would expand into a whole gpiod_lookup and is not very pleasant esthetically-speaking. So I think the size member is maybe better suited here.
Hi,
Quoted 28 lines · Alex Courbot
Quoted 18 lines · Heikki Krogerus
Hi,
Quoted 15 lines · Alexandre Courbot
Documentation/gpio/board.txt +5
-6@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),Quoted 8 lines
Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 101
Quoted 5 lines
Documentation/gpio/board.txt +5
-0+ GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +};Instead of using the size variable, wouldn't it be more clear to expect the array to be null terminated?
Quoted 6 lines
This is a zero-length array, its entries are not pointers but flattened lookup entries. Thus you cannot simply null-terminate it. It would be possible to use { NULL } as a terminator, but this would expand into a whole
gpiod_lookupand is not very pleasant esthetically-speaking. So I think the size member is maybe better suited here.
The gpio_loopup_table would look like this, which IMO is more nicer looking compared to the extra size variable:
struct gpiod_lookup_table gpios_table = {
.dev_id = "foo.0",
.table = {
GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW),
{ },
},
};That would also make it more straight forward to handle in gbiolib.c:
struct gpiod_lookup *p;
...
for (p = table->table; p->chip_label; p++) {
...
Thanks,
Signature · 1 line
heikki
Quoted 52 lines · Heikki Krogerus
Hi,
Quoted 28 lines · Alex Courbot
Quoted 18 lines · Heikki Krogerus
Hi,
Quoted 15 lines · Alexandre Courbot
Documentation/gpio/board.txt +5
-6@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH),Quoted 8 lines
Documentation/gpio/board.txt Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. · line 101
Quoted 5 lines
Documentation/gpio/board.txt +5
-0+ GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +};Instead of using the size variable, wouldn't it be more clear to expect the array to be null terminated?
Quoted 6 lines
This is a zero-length array, its entries are not pointers but flattened lookup entries. Thus you cannot simply null-terminate it. It would be possible to use { NULL } as a terminator, but this would expand into a whole
gpiod_lookupand is not very pleasant esthetically-speaking. So I think the size member is maybe better suited here.The
gpio_loopup_tablewould look like this, which IMO is more nicer looking compared to the extra size variable:struct gpiod_lookup_table gpios_table = { .dev_id = "foo.0", .table = { GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), { }, }, };
Quoted 6 lines
That would also make it more straight forward to handle in
gbiolib.c:struct
gpiod_lookup*p;
...
for (p =table->table;p->chip_label; p++) {
...
Ok, you're obviously right here. Will send a v3 tomorrow morning JST.
Thanks for the review!
PATCH v3 1/1 gpio: better lookup method for platform GPIOs
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, the con_id argument of gpiod_get() will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
---
Changes since v2:
- Removed size member of the gpiod_lookup_table struct (thanks Heikki!)
- Added Andy's reviewed-by
Documentation/gpio/board.txt | 28 ++++++----- drivers/gpio/gpiolib.c | 108 +++++++++++++++++++++++-------------------- include/linux/gpio/driver.h | 21 ++++----- 3 files changed, 85 insertions(+), 72 deletions(-)
Documentation/gpio/board.txt +17 -11
@@ -72,10 +72,11 @@ where - chip_label is the label of the gpiod_chip instance providing the GPIO - chip_hwnum is the hardware number of the GPIO within the chip - - dev_id is the identifier of the device that will make use of this GPIO. If - NULL, the GPIO will be available to all devices. + - dev_id is the identifier of the device that will make use of this GPIO. It + can be NULL, in which case it will be matched for calls to gpiod_get() + with a NULL device. - con_id is the name of the GPIO function from the device point of view. It - can be NULL. + can be NULL, in which case it will match any function. - idx is the index of the GPIO within the function. - flags is defined to specify the following properties: * GPIOF_ACTIVE_LOW - to configure the GPIO as active-low
@@ -86,18 +87,23 @@ In the future, these flags might be extended to support more properties. Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. -A lookup table can then be defined as follows: +A lookup table can then be defined as follows, with an empty entry defining its +end: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + { }, + }, +}; And the table can be added by the board code as follows: - gpiod_add_table(gpios_table, ARRAY_SIZE(gpios_table)); + gpiod_add_lookup_table(&gpios_table); The driver controlling "foo.0" will then be able to obtain its GPIOs as follows:
drivers/gpio/gpiolib.c +58 -50
148 lines not on this page · raw message
include/linux/gpio/driver.h +10 -11
@@ -140,7 +140,6 @@ enum gpio_lookup_flags {* platform data. */ struct gpiod_lookup { -struct list_head list;/* * name of the chip the GPIO belongs to */
@@ -150,10 +149,6 @@ struct gpiod_lookup {*/ u16 chip_hwnum;/* -* name of device that can claim this GPIO -*/ -const char *dev_id; -/* * name of the GPIO from the device's point of view */ const char *con_id;
@@ -167,28 +162,32 @@ struct gpiod_lookup { enum gpio_lookup_flags flags; }; +struct gpiod_lookup_table { +struct list_head list; +const char *dev_id; +struct gpiod_lookup table[]; +}; +/* * Simple definition of a single GPIO under a con_id */ -#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \ - GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags) +#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _con_id, _flags) \ + GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags)/* * Use this macro if you need to have several GPIOs under the same con_id. * Each GPIO needs to use a different index and can be accessed using * gpiod_get_index() */ -#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \ - _flags) \ +#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _con_id, _idx, _flags) \ { \ .chip_label = _chip_label, \ .chip_hwnum = _chip_hwnum, \ - .dev_id = _dev_id, \ .con_id = _con_id, \ .idx = _idx, \ .flags = _flags, \ } -void gpiod_add_table (struct gpiod_lookup *table,size_t size); +void gpiod_add_lookup_table (struct gpiod_lookup_table *table);#endif
Signature · 1 line
1.8.4.2
Hi,
Quoted 25 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.Signed-off-by Alexandre Courbot <[email protected]>Acked-by Mika Westerberg <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>---
Changes since v2:
- Removed size member of thegpiod_lookup_tablestruct (thanks Heikki!)
- Added Andy's reviewed-by
Quoted 4 lines
Documentation/gpio/board.txt | 28 ++++++----- drivers/gpio/gpiolib.c | 108 +++++++++++++++++++++++-------------------- include/linux/gpio/driver.h | 21 ++++----- 3 files changed, 85 insertions(+), 72 deletions(-)
Looks good. Thanks Alexandre!
Signature · 1 line
heikki
Quoted 17 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
Quoted 7 lines
Signed-off-by Alexandre Courbot <[email protected]>Acked-by Mika Westerberg <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>---
Changes since v2:
- Removed size member of thegpiod_lookup_tablestruct (thanks Heikki!)
- Added Andy's reviewed-by
OK looks good, however I must get my fixes stack to Torvalds and pull -rc3 back in before I can apply this, it seems, as it builds upon pending fixes.
Yours,
Linus Walleij
Quoted 17 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
Quoted 7 lines
Signed-off-by Alexandre Courbot <[email protected]>Acked-by Mika Westerberg <[email protected]>Reviewed-by Andy Shevchenko <[email protected]>---
Changes since v2:
- Removed size member of thegpiod_lookup_tablestruct (thanks Heikki!)
- Added Andy's reviewed-by
Merged in v3.13-rc3 into my devel branch and applied this on top today.
Thanks!
Linus Walleij
PATCH v2 1/1 gpio: better lookup method for platform GPIOs
In reply to Alexandre CourbotChange the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, the con_id argument of gpiod_get() will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
---
Changes since v1:
- Applied most fixes suggested by Andy
- Hopefully safer and less confusing table lookup algorithm
- Added Mika's ack
Documentation/gpio/board.txt | 25 ++++++---- drivers/gpio/gpiolib.c | 109 +++++++++++++++++++++++-------------------- include/linux/gpio/driver.h | 22 ++++----- 3 files changed, 85 insertions(+), 71 deletions(-)
Documentation/gpio/board.txt +15 -10
@@ -72,10 +72,11 @@ where - chip_label is the label of the gpiod_chip instance providing the GPIO - chip_hwnum is the hardware number of the GPIO within the chip - - dev_id is the identifier of the device that will make use of this GPIO. If - NULL, the GPIO will be available to all devices. + - dev_id is the identifier of the device that will make use of this GPIO. It + can be NULL, in which case it will be matched for calls to gpiod_get() + with a NULL device. - con_id is the name of the GPIO function from the device point of view. It - can be NULL. + can be NULL, in which case it will match any function. - idx is the index of the GPIO within the function. - flags is defined to specify the following properties: * GPIOF_ACTIVE_LOW - to configure the GPIO as active-low
@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +}; And the table can be added by the board code as follows: - gpiod_add_table(gpios_table, ARRAY_SIZE(gpios_table)); + gpiod_add_lookup_table(&gpios_table); The driver controlling "foo.0" will then be able to obtain its GPIOs as follows:
drivers/gpio/gpiolib.c +59 -50
149 lines not on this page · raw message
include/linux/gpio/driver.h +11 -11
@@ -140,7 +140,6 @@ enum gpio_lookup_flags {* platform data. */ struct gpiod_lookup { -struct list_head list;/* * name of the chip the GPIO belongs to */
@@ -150,10 +149,6 @@ struct gpiod_lookup {*/ u16 chip_hwnum;/* -* name of device that can claim this GPIO -*/ -const char *dev_id; -/* * name of the GPIO from the device's point of view */ const char *con_id;
@@ -167,28 +162,33 @@ struct gpiod_lookup { enum gpio_lookup_flags flags; }; +struct gpiod_lookup_table { +struct list_head list; +const char *dev_id; +unsigned int size; +struct gpiod_lookup table[]; +}; +/* * Simple definition of a single GPIO under a con_id */ -#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \ - GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags) +#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _con_id, _flags) \ + GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags)/* * Use this macro if you need to have several GPIOs under the same con_id. * Each GPIO needs to use a different index and can be accessed using * gpiod_get_index() */ -#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \ - _flags) \ +#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _con_id, _idx, _flags) \ { \ .chip_label = _chip_label, \ .chip_hwnum = _chip_hwnum, \ - .dev_id = _dev_id, \ .con_id = _con_id, \ .idx = _idx, \ .flags = _flags, \ } -void gpiod_add_table (struct gpiod_lookup *table,size_t size); +void gpiod_add_lookup_table (struct gpiod_lookup_table *table);#endif
Signature · 1 line
1.8.4.2
Quoted 9 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
Quoted 7 lines
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
Thanks for an updated version.
Few minor comments below.
Comments about loops are here as well.
I any case:
Quoted 113 lines
Signed-off-by Alexandre Courbot <[email protected]>Acked-by Mika Westerberg <[email protected]>---
Changes since v1:
- Applied most fixes suggested by Andy
- Hopefully safer and less confusing table lookup algorithm
- Added Mika's ackDocumentation/gpio/board.txt | 25 ++++++---- drivers/gpio/gpiolib.c | 109 +++++++++++++++++++++++-------------------- include/linux/gpio/driver.h | 22 ++++----- 3 files changed, 85 insertions(+), 71 deletions(-)Documentation/gpio/board.txt +15
-10@@ -72,10 +72,11 @@ where - chip_label is the label of the gpiod_chip instance providing the GPIO - chip_hwnum is the hardware number of the GPIO within the chip - - dev_id is the identifier of the device that will make use of this GPIO. If - NULL, the GPIO will be available to all devices. + - dev_id is the identifier of the device that will make use of this GPIO. It + can be NULL, in which case it will be matched for calls to gpiod_get() + with a NULL device. - con_id is the name of the GPIO function from the device point of view. It - can be NULL. + can be NULL, in which case it will match any function. - idx is the index of the GPIO within the function. - flags is defined to specify the following properties: * GPIOF_ACTIVE_LOW - to configure the GPIO as active-low@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +}; And the table can be added by the board code as follows: - gpiod_add_table(gpios_table, ARRAY_SIZE(gpios_table)); + gpiod_add_lookup_table(&gpios_table); The driver controlling "foo.0" will then be able to obtain its GPIOs as follows:drivers/gpio/gpiolib.c +12
-16@@ -2259,18 +2259,14 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)EXPORT_SYMBOL_GPL (gpiod_set_value_cansleep);/** -* gpiod_add_table() - register GPIO device consumers -* @table: array of consumers to register -* @num: number of consumers in table +* gpiod_add_lookup_table() - register GPIO device consumers +* @table: table of consumers to register */ -void gpiod_add_table (struct gpiod_lookup *table,size_t size) +void gpiod_add_lookup_table (struct gpiod_lookup_table *table) {mutex_lock (&gpio_lookup_lock); -while (size--) { -list_add_tail (&table->list, &gpio_lookup_list); - table++; - } +list_add_tail (&table->list, &gpio_lookup_list);mutex_unlock (&gpio_lookup_lock); }@@ -2326,72 +2322,85 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,return desc; } -static struct gpio_desc *gpiod_find (struct device *dev,const char *con_id, -unsigned int idx, -enum gpio_lookup_flags *flags) +static struct gpiod_lookup_table *gpiod_find_lookup_table (struct device *dev) {const char *dev_id = dev ?dev_name (dev) :NULL ; -struct gpio_desc *desc =ERR_PTR (-ENODEV ); -unsigned int match, best =0 ; -struct gpiod_lookup *p; +struct gpiod_lookup_table *table;mutex_lock (&gpio_lookup_lock); -list_for_each_entry (p, &gpio_lookup_list, list) { - match =0 ; +list_for_each_entry (table, &gpio_lookup_list, list) { +if (table->dev_id && dev_id) { +/* +* Valid strings on both ends, must be identical to have +* a match +*/
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2347
Quoted 10 lines
drivers/gpio/gpiolib.c +10
-0+if (!strcmp (table->dev_id, dev_id)) + goto end; + }else { +/* +* One of the pointers is NULL, so both must be to have +* a match +*/ +if (dev_id == table->dev_id) + goto end; + }
Yes, in this case it looks clearer. Though, you might join last else and if in one line.
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2349
Quoted 2 lines
drivers/gpio/gpiolib.c +2
-0+ } + table =NULL ;
Up to you, though I think it's clearer to return NULL explicitly (and unlock mutex before).
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2351
Quoted 5 lines
drivers/gpio/gpiolib.c +1
-3-if (p->dev_id) { -if (!dev_id ||strcmp (p->dev_id, dev_id)) -continue ; +end:
Maybe 'found' suits better?
Quoted 48 lines
drivers/gpio/gpiolib.c +20
-20+mutex_unlock (&gpio_lookup_lock); +return table; +} - match +=2 ; - } +static struct gpio_desc *gpiod_find (struct device *dev,const char *con_id, +unsigned int idx, +enum gpio_lookup_flags *flags) +{ +struct gpio_desc *desc =ERR_PTR (-ENODEV ); +struct gpiod_lookup_table *table; +int i; -if (p->con_id) { -if (!con_id ||strcmp (p->con_id, con_id)) -continue ; + table =gpiod_find_lookup_table (dev); +if (!table) +return desc; - match +=1 ; - } +for (i =0 ; i < table->size; i++) { +struct gpio_chip *chip; +struct gpiod_lookup *p = &table->table[i]; +/* idx must always match exactly */ if (p->idx != idx)continue ; -if (match > best) { -struct gpio_chip *chip; - - chip =find_chip_by_name (p->chip_label); - -if (!chip) { -dev_warn (dev,"cannot find GPIO chip %s\n" , - p->chip_label); -continue ; - } +/* If the lookup entry has a con_id, require exact match */ +if (p->con_id && (!con_id ||strcmp (p->con_id, con_id))) +continue ; -if (chip->ngpio <= p->chip_hwnum) { -dev_warn (dev,"GPIO chip %s has %d GPIOs\n" , - chip->label, chip->ngpio);
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Quoted 10 lines
drivers/gpio/gpiolib.c +4
-4-continue ; - } + chip =find_chip_by_name (p->chip_label); - desc =gpio_to_desc (chip->base + p->chip_hwnum); - *flags = p->flags; +if (!chip) { +dev_warn (dev,"cannot find GPIO chip %s\n" , + p->chip_label);
Could it be one line?
Quoted 95 lines
drivers/gpio/gpiolib.c +10
-7+continue ; + } -if (match !=3 ) - best = match; -else -break ; +if (chip->ngpio <= p->chip_hwnum) { +dev_warn (dev,"GPIO chip %s has %d GPIOs\n" , + chip->label, chip->ngpio); +continue ; } - } -mutex_unlock (&gpio_lookup_lock); + desc =gpiochip_offset_to_desc (chip, p->chip_hwnum); + *flags = p->flags; + }return desc; }/** * gpio_get - obtain a GPIO for a given GPIO function -* @dev: GPIO consumer +* @dev: GPIO consumer, can be NULL for system-global GPIOs * @con_id: function within the GPIO consumer * * Return the GPIO descriptor corresponding to the function con_id of device include/linux/gpio/driver.h +11
-12@@ -140,7 +140,6 @@ enum gpio_lookup_flags {* platform data. */ struct gpiod_lookup { -struct list_head list;/* * name of the chip the GPIO belongs to */ @@ -150,10 +149,6 @@ struct gpiod_lookup {*/ u16 chip_hwnum;/* -* name of device that can claim this GPIO -*/ -const char *dev_id; -/* * name of the GPIO from the device's point of view */ const char *con_id;@@ -167,28 +162,33 @@ struct gpiod_lookup { enum gpio_lookup_flags flags; }; +struct gpiod_lookup_table { +struct list_head list; +const char *dev_id; +unsigned int size; +struct gpiod_lookup table[]; +}; +/* * Simple definition of a single GPIO under a con_id */ -#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _dev_id, _con_id, _flags) \ - GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _dev_id, _con_id, 0, _flags) +#define GPIO_LOOKUP (_chip_label, _chip_hwnum, _con_id, _flags) \ + GPIO_LOOKUP_IDX(_chip_label, _chip_hwnum, _con_id, 0, _flags)/* * Use this macro if you need to have several GPIOs under the same con_id. * Each GPIO needs to use a different index and can be accessed using * gpiod_get_index() */ -#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _dev_id, _con_id, _idx, \ - _flags) \ +#define GPIO_LOOKUP_IDX (_chip_label, _chip_hwnum, _con_id, _idx, _flags) \ { \ .chip_label = _chip_label, \ .chip_hwnum = _chip_hwnum, \ - .dev_id = _dev_id, \ .con_id = _con_id, \ .idx = _idx, \ .flags = _flags, \ } -void gpiod_add_table (struct gpiod_lookup *table,size_t size); +void gpiod_add_lookup_table (struct gpiod_lookup_table *table);#endif --1.8.4.2
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 18 lines · Andy Shevchenko
Quoted 9 lines · Alexandre Courbot
Change the format of the platform GPIO lookup tables to make them less confusing and improve lookup efficiency.
The previous format was a single linked-list that required to compare the device name and function ID of every single GPIO defined for each lookup. Switch that to a list of per-device tables, so that the lookup can be done in two steps, omitting the GPIOs that are not relevant for a particular device.
Quoted 7 lines
The matching rules are now defined as follows:
- The device name must match *exactly*, and can be NULL for GPIOs not assigned to a particular device,
- If the function ID in the lookup table is NULL, thecon_idargument ofgpiod_get()will not be used for lookup. However, if it is defined, it must match exactly.
- The index must always match.
Quoted 6 lines
Thanks for an updated version.
Few minor comments below.
Comments about loops are here as well.I any case:
Reviewed-by Andy Shevchenko <[email protected]>
Thanks!
Quoted 119 lines
Quoted 119 lines
Signed-off-by Alexandre Courbot <[email protected]>Acked-by Mika Westerberg <[email protected]>---
Changes since v1:
- Applied most fixes suggested by Andy
- Hopefully safer and less confusing table lookup algorithm
- Added Mika's ackDocumentation/gpio/board.txt | 25 ++++++---- drivers/gpio/gpiolib.c | 109 +++++++++++++++++++++++-------------------- include/linux/gpio/driver.h | 22 ++++----- 3 files changed, 85 insertions(+), 71 deletions(-)Documentation/gpio/board.txt +15
-10@@ -72,10 +72,11 @@ where - chip_label is the label of the gpiod_chip instance providing the GPIO - chip_hwnum is the hardware number of the GPIO within the chip - - dev_id is the identifier of the device that will make use of this GPIO. If - NULL, the GPIO will be available to all devices. + - dev_id is the identifier of the device that will make use of this GPIO. It + can be NULL, in which case it will be matched for calls to gpiod_get() + with a NULL device. - con_id is the name of the GPIO function from the device point of view. It - can be NULL. + can be NULL, in which case it will match any function. - idx is the index of the GPIO within the function. - flags is defined to specify the following properties: * GPIOF_ACTIVE_LOW - to configure the GPIO as active-low@@ -88,16 +89,20 @@ Note that GPIO_LOOKUP() is just a shortcut to GPIO_LOOKUP_IDX() where idx = 0. A lookup table can then be defined as follows: - struct gpiod_lookup gpios_table[] = { - GPIO_LOOKUP_IDX("gpio.0", 15, "foo.0", "led", 0, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 16, "foo.0", "led", 1, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP_IDX("gpio.0", 17, "foo.0", "led", 2, GPIO_ACTIVE_HIGH), - GPIO_LOOKUP("gpio.0", 1, "foo.0", "power", GPIO_ACTIVE_LOW), - }; +struct gpiod_lookup_table gpios_table = { + .dev_id = "foo.0", + .size = 4, + .table = { + GPIO_LOOKUP_IDX("gpio.0", 15, "led", 0, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 16, "led", 1, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP_IDX("gpio.0", 17, "led", 2, GPIO_ACTIVE_HIGH), + GPIO_LOOKUP("gpio.0", 1, "power", GPIO_ACTIVE_LOW), + }, +}; And the table can be added by the board code as follows: - gpiod_add_table(gpios_table, ARRAY_SIZE(gpios_table)); + gpiod_add_lookup_table(&gpios_table); The driver controlling "foo.0" will then be able to obtain its GPIOs as follows:drivers/gpio/gpiolib.c +17
-16@@ -2259,18 +2259,14 @@ void gpiod_set_value_cansleep(struct gpio_desc *desc, int value)EXPORT_SYMBOL_GPL (gpiod_set_value_cansleep);/** -* gpiod_add_table() - register GPIO device consumers -* @table: array of consumers to register -* @num: number of consumers in table +* gpiod_add_lookup_table() - register GPIO device consumers +* @table: table of consumers to register */ -void gpiod_add_table (struct gpiod_lookup *table,size_t size) +void gpiod_add_lookup_table (struct gpiod_lookup_table *table) {mutex_lock (&gpio_lookup_lock); -while (size--) { -list_add_tail (&table->list, &gpio_lookup_list); - table++; - } +list_add_tail (&table->list, &gpio_lookup_list);mutex_unlock (&gpio_lookup_lock); }@@ -2326,72 +2322,85 @@ static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,return desc; } -static struct gpio_desc *gpiod_find (struct device *dev,const char *con_id, -unsigned int idx, -enum gpio_lookup_flags *flags) +static struct gpiod_lookup_table *gpiod_find_lookup_table (struct device *dev) {const char *dev_id = dev ?dev_name (dev) :NULL ; -struct gpio_desc *desc =ERR_PTR (-ENODEV ); -unsigned int match, best =0 ; -struct gpiod_lookup *p; +struct gpiod_lookup_table *table;mutex_lock (&gpio_lookup_lock); -list_for_each_entry (p, &gpio_lookup_list, list) { - match =0 ; +list_for_each_entry (table, &gpio_lookup_list, list) { +if (table->dev_id && dev_id) { +/* +* Valid strings on both ends, must be identical to have +* a match +*/ +if (!strcmp (table->dev_id, dev_id)) + goto end; + }else { +/* +* One of the pointers is NULL, so both must be to have
Quoted 8 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2347
Quoted 5 lines
drivers/gpio/gpiolib.c +5
-0+* a match +*/ +if (dev_id == table->dev_id) + goto end; + }Yes, in this case it looks clearer. Though, you might join last else and if in one line.
I don't think that would conform to the coding conventions. Will try and see if checkpatch complains, but I'm rather confident it will...
Quoted 6 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2349
Quoted 3 lines
drivers/gpio/gpiolib.c +2
-0+ } + table =NULL ;Up to you, though I think it's clearer to return NULL explicitly (and unlock mutex before).
As v1 of this patch can attest, the less return statements in a lock-holding function, the better. :)
Quoted 8 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id, · line 2351
Quoted 6 lines
drivers/gpio/gpiolib.c +1
-3-if (p->dev_id) { -if (!dev_id ||strcmp (p->dev_id, dev_id)) -continue ; +end:Maybe 'found' suits better?
But we end up here even if we haven't "found" anything...
Quoted 53 lines
Quoted 53 lines
drivers/gpio/gpiolib.c +21
-22+mutex_unlock (&gpio_lookup_lock); +return table; +} - match +=2 ; - } +static struct gpio_desc *gpiod_find (struct device *dev,const char *con_id, +unsigned int idx, +enum gpio_lookup_flags *flags) +{ +struct gpio_desc *desc =ERR_PTR (-ENODEV ); +struct gpiod_lookup_table *table; +int i; -if (p->con_id) { -if (!con_id ||strcmp (p->con_id, con_id)) -continue ; + table =gpiod_find_lookup_table (dev); +if (!table) +return desc; - match +=1 ; - } +for (i =0 ; i < table->size; i++) { +struct gpio_chip *chip; +struct gpiod_lookup *p = &table->table[i]; +/* idx must always match exactly */ if (p->idx != idx)continue ; -if (match > best) { -struct gpio_chip *chip; - - chip =find_chip_by_name (p->chip_label); - -if (!chip) { -dev_warn (dev,"cannot find GPIO chip %s\n" , - p->chip_label); -continue ; - } +/* If the lookup entry has a con_id, require exact match */ +if (p->con_id && (!con_id ||strcmp (p->con_id, con_id))) +continue ; -if (chip->ngpio <= p->chip_hwnum) { -dev_warn (dev,"GPIO chip %s has %d GPIOs\n" , - chip->label, chip->ngpio); -continue ; - } + chip =find_chip_by_name (p->chip_label);
Quoted 8 lines
drivers/gpio/gpiolib.c static struct gpio_desc *acpi_find_gpio(struct device *dev, const char *con_id,
Quoted 6 lines
drivers/gpio/gpiolib.c +3
-2- desc =gpio_to_desc (chip->base + p->chip_hwnum); - *flags = p->flags; +if (!chip) { +dev_warn (dev,"cannot find GPIO chip %s\n" , + p->chip_label);Could it be one line?
The line would be 84 characters if we do that, unfortunately.
Thanks for the review!
Alex.