ab2fb07ff32f541e0439794a6147d18a0f5ca700
[openwrt/staging/neocturne.git] /
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
5 gpiochip_remove
6
7 Add device managed APIs devm_gpiochip_add_data() and
8 devm_gpiochip_remove() for the APIs gpiochip_add_data()
9 and gpiochip_remove().
10
11 This helps in reducing code in error path and sometimes
12 removal of .remove callback for driver unbind.
13
14 Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
15 ---
16 drivers/gpio/gpiolib.c | 74 +++++++++++++++++++++++++++++++++++++++++++++
17 include/linux/gpio/driver.h | 4 +++
18 2 files changed, 78 insertions(+)
19
20 --- a/drivers/gpio/gpiolib.c
21 +++ b/drivers/gpio/gpiolib.c
22 @@ -433,6 +433,80 @@ void gpiochip_remove(struct gpio_chip *c
23 }
24 EXPORT_SYMBOL_GPL(gpiochip_remove);
25
26 +static void devm_gpio_chip_release(struct device *dev, void *res)
27 +{
28 + struct gpio_chip *chip = *(struct gpio_chip **)res;
29 +
30 + gpiochip_remove(chip);
31 +}
32 +
33 +static int devm_gpio_chip_match(struct device *dev, void *res, void *data)
34 +
35 +{
36 + struct gpio_chip **r = res;
37 +
38 + if (!r || !*r) {
39 + WARN_ON(!r || !*r);
40 + return 0;
41 + }
42 +
43 + return *r == data;
44 +}
45 +
46 +/**
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
51 + *
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.
55 + *
56 + * The gpio chip automatically be released when the device is unbound.
57 + */
58 +int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
59 + void *data)
60 +{
61 + struct gpio_chip **ptr;
62 + int ret;
63 +
64 + ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
65 + GFP_KERNEL);
66 + if (!ptr)
67 + return -ENOMEM;
68 +
69 + ret = gpiochip_add_data(chip, data);
70 + if (ret < 0) {
71 + devres_free(ptr);
72 + return ret;
73 + }
74 +
75 + *ptr = chip;
76 + devres_add(dev, ptr);
77 +
78 + return 0;
79 +}
80 +EXPORT_SYMBOL_GPL(devm_gpiochip_add_data);
81 +
82 +/**
83 + * devm_gpiochip_remove() - Resource manager of gpiochip_remove()
84 + * @dev: device for which which resource was allocated
85 + * @chip: the chip to remove
86 + *
87 + * A gpio_chip with any GPIOs still requested may not be removed.
88 + */
89 +void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip)
90 +{
91 + int ret;
92 +
93 + ret = devres_release(dev, devm_gpio_chip_release,
94 + devm_gpio_chip_match, chip);
95 + if (!ret)
96 + WARN_ON(ret);
97 +}
98 +EXPORT_SYMBOL_GPL(devm_gpiochip_remove);
99 +
100 /**
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);
107 }
108 extern void gpiochip_remove(struct gpio_chip *chip);
109 +extern int devm_gpiochip_add_data(struct device *dev, struct gpio_chip *chip,
110 + void *data);
111 +extern void devm_gpiochip_remove(struct device *dev, struct gpio_chip *chip);
112 +
113 extern struct gpio_chip *gpiochip_find(void *data,
114 int (*match)(struct gpio_chip *chip, void *data));
115