Commit 83cabe33 authored by Alexandre Courbot's avatar Alexandre Courbot Committed by Grant Likely

gpiolib: use gpio_chips list in gpiochip_find_base

Re-implement gpiochip_find_base using the list of chips instead of the
global gpio_desc[] array. This makes it both simpler and more efficient,
and is needed to remove the global descriptors array.

The new code should preserve the exact same GPIO number assignment
policy as the code it is replacing. There shouldn't be any visible
change to the assigned GPIO numbers.
Signed-off-by: default avatarAlexandre Courbot <acourbot@nvidia.com>
[grant.likely: Added comment about assignment policy]
Signed-off-by: default avatarGrant Likely <grant.likely@secretlab.ca>
parent cb1650d4
...@@ -126,30 +126,25 @@ struct gpio_chip *gpio_to_chip(unsigned gpio) ...@@ -126,30 +126,25 @@ struct gpio_chip *gpio_to_chip(unsigned gpio)
/* dynamic allocation of GPIOs, e.g. on a hotplugged device */ /* dynamic allocation of GPIOs, e.g. on a hotplugged device */
static int gpiochip_find_base(int ngpio) static int gpiochip_find_base(int ngpio)
{ {
int i; struct gpio_chip *chip;
int spare = 0; int base = ARCH_NR_GPIOS - ngpio;
int base = -ENOSPC;
list_for_each_entry_reverse(chip, &gpio_chips, list) {
for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) { /* found a free space? */
struct gpio_desc *desc = &gpio_desc[i]; if (chip->base + chip->ngpio <= base)
struct gpio_chip *chip = desc->chip; break;
else
if (!chip) { /* nope, check the space right before the chip */
spare++; base = chip->base - ngpio;
if (spare == ngpio) {
base = i;
break;
}
} else {
spare = 0;
if (chip)
i -= chip->ngpio - 1;
}
} }
if (gpio_is_valid(base)) if (gpio_is_valid(base)) {
pr_debug("%s: found new base at %d\n", __func__, base); pr_debug("%s: found new base at %d\n", __func__, base);
return base; return base;
} else {
pr_err("%s: cannot find free range\n", __func__);
return -ENOSPC;
}
} }
/* caller ensures gpio is valid and requested, chip->get_direction may sleep */ /* caller ensures gpio is valid and requested, chip->get_direction may sleep */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment