]> git.kernelconcepts.de Git - karo-tx-uboot.git/commitdiff
dm: gpio: Add error handling and a function to claim vector GPIOs
authorSimon Glass <sjg@chromium.org>
Wed, 15 Apr 2015 03:03:20 +0000 (21:03 -0600)
committerLothar Waßmann <LW@KARO-electronics.de>
Tue, 8 Sep 2015 20:39:33 +0000 (22:39 +0200)
gpio_get_values_as_int() should return an error if something goes wrong.
Also provide gpio_claim_vector(), a function to request the GPIOs and set
them to input mode. Otherwise callers have to do this themselves.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Tom Warren <twarren@nvidia.com>
drivers/gpio/gpio-uclass.c
include/asm-generic/gpio.h

index 381868bfb154648c6d56f0af899d293eff68eef2..530bb3e1283a7681a5ee58d47ea128f2f2b1eea6 100644 (file)
@@ -495,22 +495,54 @@ int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
        return 0;
 }
 
+int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
+{
+       int i, ret;
+       int gpio;
+
+       for (i = 0; i < 32; i++) {
+               gpio = gpio_num_array[i];
+               if (gpio == -1)
+                       break;
+               ret = gpio_requestf(gpio, fmt, i);
+               if (ret)
+                       goto err;
+               ret = gpio_direction_input(gpio);
+               if (ret) {
+                       gpio_free(gpio);
+                       goto err;
+               }
+       }
+
+       return 0;
+err:
+       for (i--; i >= 0; i--)
+               gpio_free(gpio_num_array[i]);
+
+       return ret;
+}
+
 /*
  * get a number comprised of multiple GPIO values. gpio_num_array points to
  * the array of gpio pin numbers to scan, terminated by -1.
  */
-unsigned gpio_get_values_as_int(const int *gpio_num_array)
+int gpio_get_values_as_int(const int *gpio_list)
 {
        int gpio;
        unsigned bitmask = 1;
        unsigned vector = 0;
+       int ret;
 
        while (bitmask &&
-              ((gpio = *gpio_num_array++) != -1)) {
-               if (gpio_get_value(gpio))
+              ((gpio = *gpio_list++) != -1)) {
+               ret = gpio_get_value(gpio);
+               if (ret < 0)
+                       return ret;
+               else if (ret)
                        vector |= bitmask;
                bitmask <<= 1;
        }
+
        return vector;
 }
 
index e728c1e776e2a6da2206b4c64ab83f88381dd2f3..acd2993efc3daf509f26eeda20dfe42b4a564b75 100644 (file)
@@ -348,15 +348,24 @@ int gpio_lookup_name(const char *name, struct udevice **devp,
                     unsigned int *offsetp, unsigned int *gpiop);
 
 /**
- * get_gpios() - Turn the values of a list of GPIOs into an integer
+ * gpio_get_values_as_int() - Turn the values of a list of GPIOs into an int
  *
  * This puts the value of the first GPIO into bit 0, the second into bit 1,
  * etc. then returns the resulting integer.
  *
  * @gpio_list: List of GPIOs to collect
- * @return resulting integer value
+ * @return resulting integer value, or -ve on error
  */
-unsigned gpio_get_values_as_int(const int *gpio_list);
+int gpio_get_values_as_int(const int *gpio_list);
+
+/**
+ * gpio_claim_vector() - claim a number of GPIOs for input
+ *
+ * @gpio_num_array:    array of gpios to claim, terminated by -1
+ * @fmt:               format string for GPIO names, e.g. "board_id%d"
+ * @return 0 if OK, -ve on error
+ */
+int gpio_claim_vector(const int *gpio_num_array, const char *fmt);
 
 /**
  * gpio_request_by_name() - Locate and request a GPIO by name