1 From 0cf3292cde22f8843ae5d1eeb8466d8121243c1a Mon Sep 17 00:00:00 2001
2 From: Laxman Dewangan <ldewangan@nvidia.com>
3 Date: Mon, 15 Feb 2016 16:32:09 +0530
4 Subject: [PATCH] gpio: Add devm_ apis for gpiochip_add_data and
7 Add device managed APIs devm_gpiochip_add_data() and
8 devm_gpiochip_remove() for the APIs gpiochip_add_data()
11 This helps in reducing code in error path and sometimes
12 removal of .remove callback for driver unbind.
14 Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
16 drivers/gpio/gpiolib.c | 74 +++++++++++++++++++++++++++++++++++++++++++++
17 include/linux/gpio/driver.h | 4 +++
18 2 files changed, 78 insertions(+)
20 --- a/drivers/gpio/gpiolib.c
21 +++ b/drivers/gpio/gpiolib.c
22 @@ -433,6 +433,80 @@ void gpiochip_remove(struct gpio_chip *c
24 EXPORT_SYMBOL_GPL(gpiochip_remove);
26 +static void devm_gpio_chip_release(struct device *dev, void *res)
28 + struct gpio_chip *chip = *(struct gpio_chip **)res;
30 + gpiochip_remove(chip);
33 +static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
36 + struct gpio_chip **r = res;
47 + * devm_gpiochip_add_data() - Resource manager piochip_add_data()
48 + * @dev: the device pointer on which irq_chip belongs to.
49 + * @chip: the chip to register, with chip->base initialized
50 + * Context: potentially before irqs will work
52 + * Returns a negative errno if the chip can't be registered, such as
53 + * because the chip->base is invalid or already associated with a
54 + * different chip. Otherwise it returns zero as a success code.
56 + * The gpio chip automatically be released when the device is unbound.
58 +int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
61 + struct gpio_chip **ptr;
64 + ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
69 + ret = gpiochip_add_data(chip, data);
76 + devres_add(dev, ptr);
80 +EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
83 + * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
84 + * @dev: device for which which resource was allocated
85 + * @chip: the chip to remove
87 + * A gpio_chip with any GPIOs still requested may not be removed.
89 +void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
93 + ret = devres_release(dev, devm_gpio_chip_release,
94 + devm_gpio_chip_match, chip);
98 +EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
101 * gpiochip_find() - iterator for locating a specific gpio_chip
102 * @data: data to pass to match function
103 --- a/include/linux/gpio/driver.h
104 +++ b/include/linux/gpio/driver.h
105 @@ -206,6 +206,10 @@ static inline int gpiochip_add(struct gp
106 return gpiochip_add_data(chip, NULL);
108 extern void gpiochip_remove(struct gpio_chip *chip);
109 +extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
111 +extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
113 extern struct gpio_chip *gpiochip_find(void *data,
114 int (*match)(struct gpio_chip *chip, void *data));