Skip to contentSkip to conversation

gpio: Add missing device-managed documentation

PATCH In review Thierry Reding 3 participants 5 messages Apr 25, 2014 – May 9, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 gpio: Add missing device-managed documentation

From: Thierry Reding <[email protected]>

Add the GPIO-related device-managed functions to the list of functions in Documentation/driver-model/devres.txt.

---
 Documentation/driver-model/devres.txt | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/driver-model/devres.txt +5 -0
index a3477c3d69a2..7b7d1c4d06bf 100644
@@ -310,3 +310,8 @@ SLAVE DMA ENGINE
 
 SPI
   devm_spi_register_master()
+
+GPIO
+  devm_gpiod_get()
+  devm_gpiod_get_index()
+  devm_gpiod_put()
Signature · 1 line
1.9.2
#1 lore raw

PATCH 2/2 gpio: Add helpers for optional GPIOs

From: Thierry Reding <[email protected]>

Introduce gpiod_get_optional() and gpiod_get_index_optional() helpers that make it easier for drivers to handle optional GPIOs.

Currently in order to handle optional GPIOs, a driver needs to special case error handling for -ENOENT, such as this:

	gpio = gpiod_get(dev, "foo");
	if (IS_ERR(gpio)) {
		if (PTR_ERR(gpio) != -ENOENT)
			return PTR_ERR(gpio);

		gpio = NULL;
	}

	if (gpio) {
		/* set up GPIO */
	}

With these new helpers the above is reduced to:

	gpio = gpiod_get_optional(dev, "foo");
	if (IS_ERR(gpio))
		return PTR_ERR(gpio);

	if (gpio) {
		/* set up GPIO */
	}

While at it, device-managed variants of these functions are also provided.

---
 Documentation/driver-model/devres.txt |  2 ++
 drivers/gpio/devres.c                 | 43 +++++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.c                | 43 +++++++++++++++++++++++++++++++++++
 include/linux/gpio/consumer.h         | 40 ++++++++++++++++++++++++++++++++
 4 files changed, 128 insertions(+)
Documentation/driver-model/devres.txt +2 -0
index 7b7d1c4d06bf..4796e2ab3349 100644
@@ -314,4 +314,6 @@ SPI
 GPIO
   devm_gpiod_get()
   devm_gpiod_get_index()
+  devm_gpiod_get_optional()
+  devm_gpiod_get_index_optional()
   devm_gpiod_put()
drivers/gpio/devres.c +43 -0

55 lines not on this page · raw message

drivers/gpio/gpiolib.c +43 -0

55 lines not on this page · raw message

include/linux/gpio/consumer.h +40 -0
index bed128e8f4b1..6a37ef0dc59c 100644
@@ -23,6 +23,12 @@ struct gpio_desc *__must_check gpiod_get(struct device *dev,
 struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
 					       const char *con_id,
 					       unsigned int idx);
+struct gpio_desc *__must_check gpiod_get_optional(struct device *dev,
+						  const char *con_id);
+struct gpio_desc *__must_check gpiod_get_index_optional(struct device *dev,
+							const char *con_id,
+							unsigned int index);
+
 void gpiod_put(struct gpio_desc *desc);
 
 struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
@@ -30,6 +36,12 @@ struct gpio_desc *__must_check devm_gpiod_get(struct device *dev,
 struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
 						    const char *con_id,
 						    unsigned int idx);
+struct gpio_desc *__must_check devm_gpiod_get_optional(struct device *dev,
+						       const char *con_id);
+struct gpio_desc *__must_check
+devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
+			      unsigned int index);
+
 void devm_gpiod_put(struct device *dev, struct gpio_desc *desc);
 
 int gpiod_get_direction(const struct gpio_desc *desc);
@@ -73,6 +85,20 @@ static inline struct gpio_desc *__must_check gpiod_get_index(struct device *dev,
 {
 	return ERR_PTR(-ENOSYS);
 }
+
+static inline struct gpio_desc *__must_check
+gpiod_get_optional(struct device *dev, const char *con_id)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
+static inline struct gpio_desc *__must_check
+gpiod_get_index_optional(struct device *dev, const char *con_id,
+			 unsigned int index)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
 static inline void gpiod_put(struct gpio_desc *desc)
 {
 	might_sleep();
@@ -93,6 +119,20 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev,
 {
 	return ERR_PTR(-ENOSYS);
 }
+
+static inline struct gpio_desc *__must_check
+devm_gpiod_get_optional(struct device *dev, const char *con_id)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
+static inline struct gpio_desc *__must_check
+devm_gpiod_get_index_optional(struct device *dev, const char *con_id,
+			      unsigned int index)
+{
+	return ERR_PTR(-ENOSYS);
+}
+
 static inline void devm_gpiod_put(struct device *dev, struct gpio_desc *desc)
 {
 	might_sleep();
Signature · 1 line
1.9.2
Reviewed-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] gpio: Add helpers for optional GPIOs

Quoted 30 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Introduce gpiod_get_optional() and gpiod_get_index_optional() helpers that make it easier for drivers to handle optional GPIOs.

Currently in order to handle optional GPIOs, a driver needs to special case error handling for -ENOENT, such as this:

        gpio = gpiod_get(dev, "foo");
        if (IS_ERR(gpio)) {
                if (PTR_ERR(gpio) != -ENOENT)
                        return PTR_ERR(gpio);

                gpio = NULL;
        }

        if (gpio) {
                /* set up GPIO */
        }

With these new helpers the above is reduced to:

        gpio = gpiod_get_optional(dev, "foo");
        if (IS_ERR(gpio))
                return PTR_ERR(gpio);

        if (gpio) {
                /* set up GPIO */
        }
Quoted 2 lines

While at it, device-managed variants of these functions are also provided.

Patch is sound and the introduced functions are useful indeed.

It makes me wonder whether this should not have been the behavior of gpiod_get() directly... My aversion for IS_ERR_OR_NULL drove the current design, which may not have been optimal.

Oh, well.

#3 lore raw

Re: [PATCH 2/2] gpio: Add helpers for optional GPIOs

In reply to Thierry Reding
Quoted 4 lines · Thierry Reding

From: Thierry Reding <[email protected]>

Introduce gpiod_get_optional() and gpiod_get_index_optional() helpers that make it easier for drivers to handle optional GPIOs.

Excellent Thierry, patch applied with Alexandre's review tag.

Can you *please* also produce a patch to
Documentation/gpio/consumer.txt
explaining the new interface to potential users?

Yours,
Linus Walleij