surprise :p
[openwrt/svn-archive/archive.git] / target / linux / generic-2.6 / patches-2.6.25 / 962-backport_gpiolib_dynamic_gpio_number_allocation.patch
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
6
7 gpiolib: dynamic gpio number allocation
8
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.)
13
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.
17
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>
22 ---
23
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;
30 }
31
32 +/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
33 +static int gpiochip_find_base(int ngpio)
34 +{
35 + int i;
36 + int spare = 0;
37 + int base = -ENOSPC;
38 +
39 + for (i = ARCH_NR_GPIOS - 1; i >= 0 ; i--) {
40 + struct gpio_chip *chip = gpio_desc[i].chip;
41 +
42 + if (!chip) {
43 + spare++;
44 + if (spare == ngpio) {
45 + base = i;
46 + break;
47 + }
48 + } else {
49 + spare = 0;
50 + i -= chip->ngpio - 1;
51 + }
52 + }
53 +
54 + if (gpio_is_valid(base))
55 + pr_debug("%s: found new base at %d\n", __func__, base);
56 + return base;
57 +}
58 +
59 /**
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.
66 + *
67 + * If chip->base is negative, this requests dynamic assignment of
68 + * a range of valid GPIOs.
69 */
70 int gpiochip_add(struct gpio_chip *chip)
71 {
72 unsigned long flags;
73 int status = 0;
74 unsigned id;
75 + int base = chip->base;
76
77 - /* NOTE chip->base negative is reserved to mean a request for
78 - * dynamic allocation. We don't currently support that.
79 - */
80 -
81 - if (chip->base < 0 || !gpio_is_valid(chip->base + chip->ngpio)) {
82 + if ((!gpio_is_valid(base) || !gpio_is_valid(base + chip->ngpio))
83 + && base >= 0) {
84 status = -EINVAL;
85 goto fail;
86 }
87
88 spin_lock_irqsave(&gpio_lock, flags);
89
90 + if (base < 0) {
91 + base = gpiochip_find_base(chip->ngpio);
92 + if (base < 0) {
93 + status = base;
94 + goto fail_unlock;
95 + }
96 + chip->base = base;
97 + }
98 +
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) {
103 status = -EBUSY;
104 break;
105 }
106 }
107 if (status == 0) {
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;
112 }
113 }
114
115 +fail_unlock:
116 spin_unlock_irqrestore(&gpio_lock, flags);
117 fail:
118 /* failures here can mean systems won't boot... */