* [PATCH 1/1] gpio: add devm_ apis for gpio_chip_add and remove
@ 2016-02-15 13:17 ` Laxman Dewangan
0 siblings, 0 replies; 4+ messages in thread
From: Laxman Dewangan @ 2016-02-15 13:17 UTC (permalink / raw)
To: linus.walleij, gnurou; +Cc: linux-gpio, linux-kernel, Laxman Dewangan
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 | 74 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/gpio/driver.h | 4 +++
2 files changed, 78 insertions(+)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 5c1ba87..d9a40bc 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -462,6 +462,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
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 82fda48..d5d19cc 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -205,6 +205,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));
--
2.1.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 1/1] gpio: add devm_ apis for gpio_chip_add and remove
@ 2016-02-15 13:17 ` Laxman Dewangan
0 siblings, 0 replies; 4+ messages in thread
From: Laxman Dewangan @ 2016-02-15 13:17 UTC (permalink / raw)
To: linus.walleij, gnurou; +Cc: linux-gpio, linux-kernel, Laxman Dewangan
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 | 74 +++++++++++++++++++++++++++++++++++++++++++++
include/linux/gpio/driver.h | 4 +++
2 files changed, 78 insertions(+)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 5c1ba87..d9a40bc 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -462,6 +462,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
diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 82fda48..d5d19cc 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -205,6 +205,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));
--
2.1.4
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/1] gpio: add devm_ apis for gpio_chip_add and remove
2016-02-15 13:17 ` Laxman Dewangan
(?)
@ 2016-02-16 15:03 ` Linus Walleij
2016-02-20 4:04 ` Laxman Dewangan
-1 siblings, 1 reply; 4+ messages in thread
From: Linus Walleij @ 2016-02-16 15:03 UTC (permalink / raw)
To: Laxman Dewangan; +Cc: Alexandre Courbot, linux-gpio, linux-kernel
On Mon, Feb 15, 2016 at 2:17 PM, Laxman Dewangan <ldewangan@nvidia.com> wrote:
> 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>
I like the looks of this.
One comment: you must also patch
Documentation/driver-model/devres.txt
Will you also follow up with a massive patch set making use of
this new API? I feel bad about merging an API if it's not used.
Thanks for only supporting this for the new _data() function.
Yours,
Linus Walleij
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/1] gpio: add devm_ apis for gpio_chip_add and remove
2016-02-16 15:03 ` Linus Walleij
@ 2016-02-20 4:04 ` Laxman Dewangan
0 siblings, 0 replies; 4+ messages in thread
From: Laxman Dewangan @ 2016-02-20 4:04 UTC (permalink / raw)
To: Linus Walleij; +Cc: Alexandre Courbot, linux-gpio, linux-kernel
On Tuesday 16 February 2016 08:33 PM, Linus Walleij wrote:
> On Mon, Feb 15, 2016 at 2:17 PM, Laxman Dewangan <ldewangan@nvidia.com> wrote:
>
>> 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>
> I like the looks of this.
>
> One comment: you must also patch
> Documentation/driver-model/devres.txt
Sure,
>
> Will you also follow up with a massive patch set making use of
> this new API? I feel bad about merging an API if it's not used.
>
> Thanks for only supporting this for the new _data() function.
>
>
Yes, I have planned for this to use this new API. This is just for
getting review whether fine or not to have devm_* APIs so that I can
make changes on rest of driver to use this new APIs.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-02-20 4:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-02-15 13:17 [PATCH 1/1] gpio: add devm_ apis for gpio_chip_add and remove Laxman Dewangan
2016-02-15 13:17 ` Laxman Dewangan
2016-02-16 15:03 ` Linus Walleij
2016-02-20 4:04 ` Laxman Dewangan
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.