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 --- a/drivers/gpio/gpiolib.c
25 +++ b/drivers/gpio/gpiolib.c
26 @@ -80,6 +80,33 @@ static inline struct gpio_chip *gpio_to_
27 return gpio_desc[gpio].chip;
30 +/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
31 +static int gpiochip_find_base(int ngpio)
37 + for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
38 + struct gpio_chip *chip = gpio_desc[i].chip;
42 + if (spare == ngpio) {
48 + i -= chip->ngpio - 1;
52 + if (gpio_is_valid(base))
53 + pr_debug("%s: found new base at %d\n", __func__, base);
58 * gpiochip_add() - register a gpio_chip
59 * @chip: the chip to register, with chip->base initialized
60 @@ -88,38 +115,49 @@ static inline struct gpio_chip *gpio_to_
61 * Returns a negative errno if the chip can't be registered, such as
62 * because the chip->base is invalid or already associated with a
63 * different chip. Otherwise it returns zero as a success code.
65 + * If chip->base is negative, this requests dynamic assignment of
66 + * a range of valid GPIOs.
68 int gpiochip_add(struct gpio_chip *chip)
73 + int base = chip->base;
75 - /* NOTE chip->base negative is reserved to mean a request for
76 - * dynamic allocation. We don't currently support that.
79 - if (chip->base < 0 || !gpio_is_valid(chip->base + chip->ngpio)) {
80 + if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio))
86 spin_lock_irqsave(&gpio_lock, flags);
89 + base = gpiochip_find_base(chip->ngpio);
97 /* these GPIO numbers must not be managed by another gpio_chip */
98 - for (id = chip->base; id < chip->base + chip->ngpio; id++) {
99 + for (id = base; id < base + chip->ngpio; id++) {
100 if (gpio_desc[id].chip != NULL) {
106 - for (id = chip->base; id < chip->base + chip->ngpio; id++) {
107 + for (id = base; id < base + chip->ngpio; id++) {
108 gpio_desc[id].chip = chip;
109 gpio_desc[id].flags = 0;
114 spin_unlock_irqrestore(&gpio_lock, flags);
116 /* failures here can mean systems won't boot... */