]> git.karo-electronics.de Git - linux-beck.git/commitdiff
gpio: Add devm_ apis for gpiochip_add_data and gpiochip_remove
authorLaxman Dewangan <ldewangan@nvidia.com>
Mon, 15 Feb 2016 11:02:09 +0000 (16:32 +0530)
committerLaxman Dewangan <ldewangan@nvidia.com>
Tue, 23 Feb 2016 14:10:33 +0000 (19:40 +0530)
Add device managed APIs devm_gpiochip_add_data() and
devm_gpiochip_remove() for the APIs gpiochip_add_data()
and gpiochip_remove().

This helps in reducing code in error path and sometimes
removal of .remove callback for driver unbind.

Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
drivers/gpio/gpiolib.c
include/linux/gpio/driver.h

index 872774a404f1752549753975082298e2381315c7..e31d0a1e6f7c941677440525cb70cf2703c50716 100644 (file)
@@ -723,6 +723,80 @@ void gpiochip_remove(struct gpio_chip *chip)
 }
 EXPORT_SYMBOL_GPL(gpiochip_remove);
 
+static void devm_gpio_chip_release(struct device *dev, void *res)
+{
+       struct gpio_chip *chip = *(struct gpio_chip **)res;
+
+       gpiochip_remove(chip);
+}
+
+static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
+
+{
+       struct gpio_chip **r = res;
+
+       if (!r || !*r) {
+               WARN_ON(!r || !*r);
+               return 0;
+       }
+
+       return *r == data;
+}
+
+/**
+ * devm_gpiochip_add_data() - Resource manager piochip_add_data()
+ * @dev: the device pointer on which irq_chip belongs to.
+ * @chip: the chip to register, with chip->base initialized
+ * Context: potentially before irqs will work
+ *
+ * Returns a negative errno if the chip can't be registered, such as
+ * because the chip->base is invalid or already associated with a
+ * different chip.  Otherwise it returns zero as a success code.
+ *
+ * The gpio chip automatically be released when the device is unbound.
+ */
+int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
+                          void *data)
+{
+       struct gpio_chip **ptr;
+       int ret;
+
+       ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
+                            GFP_KERNEL);
+       if (!ptr)
+               return -ENOMEM;
+
+       ret = gpiochip_add_data(chip, data);
+       if (ret < 0) {
+               devres_free(ptr);
+               return ret;
+       }
+
+       *ptr = chip;
+       devres_add(dev, ptr);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
+
+/**
+ * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
+ * @dev: device for which which resource was allocated
+ * @chip: the chip to remove
+ *
+ * A gpio_chip with any GPIOs still requested may not be removed.
+ */
+void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
+{
+       int ret;
+
+       ret = devres_release(dev, devm_gpio_chip_release,
+                            devm_gpio_chip_match, chip);
+       if (!ret)
+               WARN_ON(ret);
+}
+EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
+
 /**
  * gpiochip_find() - iterator for locating a specific gpio_chip
  * @data: data to pass to match function
index 639607658ed892ce35e259a3f5fcf1c44540b35d..bee976f827889d1b7261d44dde499ccaadac5d71 100644 (file)
@@ -192,6 +192,10 @@ static inline int gpiochip_add(struct gpio_chip *chip)
        return gpiochip_add_data(chip, NULL);
 }
 extern void gpiochip_remove(struct gpio_chip *chip);
+extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
+                                 void *data);
+extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
+
 extern struct gpio_chip *gpiochip_find(void *data,
                              int (*match)(struct gpio_chip *chip, void *data));