1 From: Anton Vorontsov <avorontsov@ru.mvista.com>
2 Date: Mon, 28 Apr 2008 09:14:46 +0000 (-0700)
3 Subject: gpiolib: dynamic gpio number allocation
4 X-Git-Tag: v2.6.26-rc1~848
5 X-Git-Url: http://git.kernel.org/?p=linux%2Fkernel%2Fgit%2Ftorvalds%2Flinux-2.6.git;a=commitdiff_plain;h=8d0aab2f16c4fa170f32e7a74a52cd0122bbafef
7 gpiolib: dynamic gpio number allocation
9 If gpio_chip->base is negative during registration, gpiolib performs dynamic
10 base allocation. This is useful for devices that aren't always present, such
11 as GPIOs on hotplugged devices rather than mainboards. (This behavior was
12 previously specified but not implemented.)
14 To avoid using any numbers that may have been explicitly assigned but not yet
15 registered, this dynamic allocation assigns GPIO numbers from the biggest
16 number on down, instead of from the smallest on up.
18 Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
19 Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
20 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
21 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
24 diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
25 index 623fcd9..2ba6127 100644
26 --- a/drivers/gpio/gpiolib.c
27 +++ b/drivers/gpio/gpiolib.c
28 @@ -80,6 +80,33 @@ static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
29 return gpio_desc[gpio].chip;
32 +/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
33 +static int gpiochip_find_base(int ngpio)
39 + for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
40 + struct gpio_chip *chip = gpio_desc[i].chip;
44 + if (spare == ngpio) {
50 + i -= chip->ngpio - 1;
54 + if (gpio_is_valid(base))
55 + pr_debug("%s: found new base at %d\n", __func__, base);
60 * gpiochip_add() - register a gpio_chip
61 * @chip: the chip to register, with chip->base initialized
62 @@ -88,38 +115,49 @@ static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
63 * Returns a negative errno if the chip can't be registered, such as
64 * because the chip->base is invalid or already associated with a
65 * different chip. Otherwise it returns zero as a success code.
67 + * If chip->base is negative, this requests dynamic assignment of
68 + * a range of valid GPIOs.
70 int gpiochip_add(struct gpio_chip *chip)
75 + int base = chip->base;
77 - /* NOTE chip->base negative is reserved to mean a request for
78 - * dynamic allocation. We don't currently support that.
81 - if (chip->base < 0 || !gpio_is_valid(chip->base + chip->ngpio)) {
82 + if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio))
88 spin_lock_irqsave(&gpio_lock, flags);
91 + base = gpiochip_find_base(chip->ngpio);
99 /* these GPIO numbers must not be managed by another gpio_chip */
100 - for (id = chip->base; id < chip->base + chip->ngpio; id++) {
101 + for (id = base; id < base + chip->ngpio; id++) {
102 if (gpio_desc[id].chip != NULL) {
108 - for (id = chip->base; id < chip->base + chip->ngpio; id++) {
109 + for (id = base; id < base + chip->ngpio; id++) {
110 gpio_desc[id].chip = chip;
111 gpio_desc[id].flags = 0;
116 spin_unlock_irqrestore(&gpio_lock, flags);
118 /* failures here can mean systems won't boot... */