mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH v2] GPIO: Add gpio_to_desc helper
@ 2013-05-15 22:13 Alexander Shiyan
  2013-05-17  5:46 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Alexander Shiyan @ 2013-05-15 22:13 UTC (permalink / raw)
  To: barebox

Patch adds gpio_to_desc helper for validate GPIO.
A bit optimization is performed (about -160 bytes on ARM).

Signed-off-by: Alexander Shiyan <shc_work@mail.ru>
---
 drivers/gpio/gpiolib.c | 95 +++++++++++++++++++++++++++-----------------------
 1 file changed, 51 insertions(+), 44 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 6398268..ca6e8ad 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1,3 +1,5 @@
+#define pr_fmt(fmt)	"gpiolib: " fmt
+
 #include <init.h>
 #include <common.h>
 #include <command.h>
@@ -32,20 +34,30 @@ static int gpio_ensure_requested(struct gpio_info *gi, int gpio)
 	return gpio_request(gpio, "gpio");
 }
 
+static struct gpio_info *gpio_to_desc(unsigned gpio)
+{
+	if (gpio_is_valid(gpio))
+		if (gpio_desc[gpio].chip)
+			return &gpio_desc[gpio];
+
+	pr_warning("invalid GPIO %d\n", gpio);
+
+	return NULL;
+}
+
 int gpio_request(unsigned gpio, const char *label)
 {
-	struct gpio_info *gi = &gpio_desc[gpio];
-	struct gpio_chip *chip = gi->chip;
+	struct gpio_info *gi = gpio_to_desc(gpio);
 	int ret;
 
-	if (!gpio_is_valid(gpio))
-		return -EINVAL;
-	if (!chip)
-		return -EINVAL;
+	if (!gi)
+		return -ENODEV;
+
 	if (gi->requested)
 		return -EBUSY;
-	if (chip->ops->request) {
-		ret = chip->ops->request(chip, gpio - chip->base);
+
+	if (gi->chip->ops->request) {
+		ret = gi->chip->ops->request(gi->chip, gpio - gi->chip->base);
 		if (ret)
 			return ret;
 	}
@@ -58,17 +70,16 @@ int gpio_request(unsigned gpio, const char *label)
 
 void gpio_free(unsigned gpio)
 {
-	struct gpio_info *gi = &gpio_desc[gpio];
-	struct gpio_chip *chip = gi->chip;
+	struct gpio_info *gi = gpio_to_desc(gpio);
 
-	if (!gpio_is_valid(gpio))
-		return;
-	if (!chip)
+	if (!gi)
 		return;
+
 	if (!gi->requested)
 		return;
-	if (chip->ops->free)
-		chip->ops->free(chip, gpio - chip->base);
+
+	if (gi->chip->ops->free)
+		gi->chip->ops->free(gi->chip, gpio - gi->chip->base);
 
 	gi->requested = false;
 	free(gi->label);
@@ -76,75 +87,71 @@ void gpio_free(unsigned gpio)
 
 void gpio_set_value(unsigned gpio, int value)
 {
-	struct gpio_info *gi = &gpio_desc[gpio];
-	struct gpio_chip *chip = gi->chip;
+	struct gpio_info *gi = gpio_to_desc(gpio);
 
-	if (!gpio_is_valid(gpio))
-		return;
-	if (!chip)
+	if (!gi)
 		return;
+
 	if (gpio_ensure_requested(gi, gpio))
 		return;
-	if (!chip->ops->set)
-		return;
-	chip->ops->set(chip, gpio - chip->base, value);
+
+	if (gi->chip->ops->set)
+		gi->chip->ops->set(gi->chip, gpio - gi->chip->base, value);
 }
 EXPORT_SYMBOL(gpio_set_value);
 
 int gpio_get_value(unsigned gpio)
 {
-	struct gpio_info *gi = &gpio_desc[gpio];
-	struct gpio_chip *chip = gi->chip;
+	struct gpio_info *gi = gpio_to_desc(gpio);
 	int ret;
 
-	if (!gpio_is_valid(gpio))
-		return -EINVAL;
-	if (!chip)
+	if (!gi)
 		return -ENODEV;
+
 	ret = gpio_ensure_requested(gi, gpio);
 	if (ret)
 		return ret;
-	if (!chip->ops->get)
+
+	if (!gi->chip->ops->get)
 		return -ENOSYS;
-	return chip->ops->get(chip, gpio - chip->base);
+	return gi->chip->ops->get(gi->chip, gpio - gi->chip->base);
 }
 EXPORT_SYMBOL(gpio_get_value);
 
 int gpio_direction_output(unsigned gpio, int value)
 {
-	struct gpio_info *gi = &gpio_desc[gpio];
-	struct gpio_chip *chip = gi->chip;
+	struct gpio_info *gi = gpio_to_desc(gpio);
 	int ret;
 
-	if (!gpio_is_valid(gpio))
-		return -EINVAL;
-	if (!chip)
+	if (!gi)
 		return -ENODEV;
+
 	ret = gpio_ensure_requested(gi, gpio);
 	if (ret)
 		return ret;
-	if (!chip->ops->direction_output)
+
+	if (!gi->chip->ops->direction_output)
 		return -ENOSYS;
-	return chip->ops->direction_output(chip, gpio - chip->base, value);
+	return gi->chip->ops->direction_output(gi->chip, gpio - gi->chip->base,
+					       value);
 }
 EXPORT_SYMBOL(gpio_direction_output);
 
 int gpio_direction_input(unsigned gpio)
 {
-	struct gpio_info *gi = &gpio_desc[gpio];
-	struct gpio_chip *chip = gi->chip;
+	struct gpio_info *gi = gpio_to_desc(gpio);
 	int ret;
 
-	if (!gpio_is_valid(gpio))
-		return -EINVAL;
-	if (!chip)
+	if (!gi)
 		return -ENODEV;
+
 	ret = gpio_ensure_requested(gi, gpio);
 	if (ret)
 		return ret;
-	if (!chip->ops->direction_input)
+
+	if (!gi->chip->ops->direction_input)
 		return -ENOSYS;
-	return chip->ops->direction_input(chip, gpio - chip->base);
+	return gi->chip->ops->direction_input(gi->chip, gpio - gi->chip->base);
 }
 EXPORT_SYMBOL(gpio_direction_input);
 
-- 
1.8.1.5


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] GPIO: Add gpio_to_desc helper
  2013-05-15 22:13 [PATCH v2] GPIO: Add gpio_to_desc helper Alexander Shiyan
@ 2013-05-17  5:46 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2013-05-17  5:46 UTC (permalink / raw)
  To: Alexander Shiyan; +Cc: barebox

On Thu, May 16, 2013 at 02:13:28AM +0400, Alexander Shiyan wrote:
> Patch adds gpio_to_desc helper for validate GPIO.
> A bit optimization is performed (about -160 bytes on ARM).
> 
> Signed-off-by: Alexander Shiyan <shc_work@mail.ru>

Applied, thanks

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-05-17  5:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-15 22:13 [PATCH v2] GPIO: Add gpio_to_desc helper Alexander Shiyan
2013-05-17  5:46 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox