Skip to contentSkip to conversation

gpiolib: implement dynamic base allocation

PATCH In review Anton Vorontsov 4 participants 4 messages Mar 4, 2008 – Mar 6, 2008 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: implement dynamic base allocation

If gpio_chip->base is negative during registration, gpiolib requests dynamic base allocation. This is useful for devices being registered at run-time (in contrast to platform devices).

---
 drivers/gpio/gpiolib.c |   42 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 6 deletions(-)
drivers/gpio/gpiolib.c +36 -6
index d8db2f8..1d25104 100644
@@ -77,6 +77,32 @@ static inline struct gpio_chip *gpio_to_chip(unsigned gpio)
 	return gpio_desc[gpio].chip;
 }
 
+static int gpiochip_find_base(int ngpio)
+{
+	int i;
+	int spare = 0;
+	int next_base = 0;
+	int base = -ENOSPC;
+
+	for (i = 0; i < ARCH_NR_GPIOS; i++) {
+		struct gpio_chip *c = gpio_desc[i].chip;
+
+		if (!c) {
+			spare++;
+			if (spare == ngpio) {
+				base = next_base;
+				break;
+			}
+		} else {
+			spare = 0;
+			i += c->ngpio;
+			next_base = i;
+		}
+	}
+
+	return base;
+}
+
 /**
  * gpiochip_add() - register a gpio_chip
  * @chip: the chip to register, with chip->base initialized
@@ -92,17 +118,21 @@ int gpiochip_add(struct gpio_chip *chip)
 	int		status = 0;
 	unsigned	id;
 
-	/* NOTE chip->base negative is reserved to mean a request for
-	 * dynamic allocation.  We don't currently support that.
-	 */
-
-	if (chip->base < 0 || (chip->base  + chip->ngpio) >= ARCH_NR_GPIOS) {
+	if (chip->base >= 0 && chip->base + chip->ngpio >= ARCH_NR_GPIOS) {
 		status = -EINVAL;
 		goto fail;
 	}
 
 	spin_lock_irqsave(&gpio_lock, flags);
 
+	if (chip->base < 0) {
+		chip->base = gpiochip_find_base(chip->ngpio);
+		if (chip->base < 0) {
+			status = chip->base;
+			goto fail_unlock;
+		}
+	}
+
 	/* these GPIO numbers must not be managed by another gpio_chip */
 	for (id = chip->base; id < chip->base + chip->ngpio; id++) {
 		if (gpio_desc[id].chip != NULL) {
@@ -116,7 +146,7 @@ int gpiochip_add(struct gpio_chip *chip)
 			gpio_desc[id].flags = 0;
 		}
 	}
-
+fail_unlock:
 	spin_unlock_irqrestore(&gpio_lock, flags);
 fail:
 	/* failures here can mean systems won't boot... */
Signature · 1 line
1.5.2.2
#1 lore raw
Quoted 9 lines · On Tue, 4 Mar 2008 20:03:07 +0300 Anton Vorontsov <[email protected]>…

If gpio_chip->base is negative during registration, gpiolib requests dynamic base allocation. This is useful for devices being registered at run-time (in contrast to platform devices).

---
 drivers/gpio/gpiolib.c |   42 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 6 deletions(-)

This conflicts in non-trivial ways with gpio-define-gpio_is_valid.patch. Could you please redo and retest against 2.6.25-rc3-mm1?

Thanks.

#2 lore raw
Quoted 7 lines · Andrew Morton
Quoted 4 lines · On Tue, 4 Mar 2008 20:03:07 +0300 Anton Vorontsov <[email protected]>…

If gpio_chip->base is negative during registration, gpiolib requests dynamic base allocation. This is useful for devices being registered at run-time (in contrast to platform devices).

Quoted 8 lines
Quoted 5 lines
---
 drivers/gpio/gpiolib.c |   42 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 6 deletions(-)

This conflicts in non-trivial ways with gpio-define-gpio_is_valid.patch. Could you please redo and retest against 2.6.25-rc3-mm1?

And when you do that, could you make it assign GPIO numbers from the biggest number on down, instead of from the smallest on up?

Platforms normally assign those numbers from the bottom up ... so dynamic assignment should try to avoid reusing any numbers that may have been explicitly assigned, but not yet registered. (Of course, if you can come up with a cleaner solution than that, it'd be great. Maybe platforms should also be able to mark ranges as "in use" as they start up, or something.)

And please add a pr_debug level message reporting dynamically assigned ranges. That way, when problems crop up it'll be that much easier to notice what went wrong.

- Dave

#3 lore raw
Quoted 8 lines · David Brownell
Quoted 7 lines · Andrew Morton
Quoted 4 lines · On Tue, 4 Mar 2008 20:03:07 +0300 Anton Vorontsov <[email protected]>…

If gpio_chip->base is negative during registration, gpiolib requests dynamic base allocation. This is useful for devices being registered at run-time (in contrast to platform devices).

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
---
 drivers/gpio/gpiolib.c |   42 ++++++++++++++++++++++++++++++++++++------
 1 files changed, 36 insertions(+), 6 deletions(-)

This conflicts in non-trivial ways with gpio-define-gpio_is_valid.patch. Could you please redo and retest against 2.6.25-rc3-mm1?

No problem, will rebase.

Quoted 9 lines

And when you do that, could you make it assign GPIO numbers from the biggest number on down, instead of from the smallest on up?

Platforms normally assign those numbers from the bottom up ... so dynamic assignment should try to avoid reusing any numbers that may have been explicitly assigned, but not yet registered. (Of course, if you can come up with a cleaner solution than that, it'd be great. Maybe platforms should also be able to mark ranges as "in use" as they start up, or something.)

Well, it is also trivial to implement gpiochip_reserve(start, end) function that will mark reserved gpio->chip as ERR_PTR(-EACCESS), and these ranges will be unavailable for the dynamic allocation.

Then, platforms could do gpiochip_reserve(0, GPIO_ARCH_END). I think This is good solution from all and every POV, but this needs a bit of platform code assistance.

What would you choice?

Quoted 3 lines

And please add a pr_debug level message reporting dynamically assigned ranges. That way, when problems crop up it'll be that much easier to notice what went wrong.

Will do.

Thanks,

Signature · 3 lines
Anton Vorontsov
email: [email protected]
irc://irc.freenode.net/bd2