Skip to contentSkip to conversation

gpiolib: Switch to for_each_set_bit()

PATCH Superseded Andy Shevchenko 3 participants 10 messages Jan 3, 2017 – Jan 11, 2017

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 gpiolib: Switch to for_each_set_bit()

The macro for_each_set_bit() effectively looks up to the next set bit in array of bits.

Instead of open coding that switch to for_each_set_bit() in gpio_chip_set_multiple().

While here, make gpio_chip_set_multiple() non-destructive against its parameters. We are safe since all callers, i.e. gpiod_set_array_value_complex(), handle that already.

---
 drivers/gpio/gpiolib.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
drivers/gpio/gpiolib.c +6 -12
index f4c26c7826cd..7f51c9bf5533 100644
@@ -1,3 +1,4 @@
+#include <linux/bitops.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/interrupt.h>
@@ -2570,18 +2571,11 @@ static void gpio_chip_set_multiple(struct gpio_chip *chip,
 	if (chip->set_multiple) {
 		chip->set_multiple(chip, mask, bits);
 	} else {
-		int i;
-		for (i = 0; i < chip->ngpio; i++) {
-			if (mask[BIT_WORD(i)] == 0) {
-				/* no more set bits in this mask word;
-				 * skip ahead to the next word */
-				i = (BIT_WORD(i) + 1) * BITS_PER_LONG - 1;
-				continue;
-			}
-			/* set outputs if the corresponding mask bit is set */
-			if (__test_and_clear_bit(i, mask))
-				chip->set(chip, i, test_bit(i, bits));
-		}
+		unsigned int i;
+
+		/* set outputs if the corresponding mask bit is set */
+		for_each_set_bit(i, mask, chip->ngpio)
+			chip->set(chip, i, test_bit(i, bits));
 	}
 }
 
Signature · 1 line
2.11.0
#1 lore raw

PATCH 2/3 gpiolib: Update documentation of struct acpi_gpio_info

It seems the code had been changed, but description left untouched.

Update description of the struct acpi_gpio_info and relative comments accordingly.

Fixes commit 52044723cd27 ("ACPI / gpio: Add irq_type when a GPIO is used as an interrupt")
Cc Christophe RICARD <[email protected]>
---
 drivers/gpio/gpiolib-acpi.c | 5 ++---
 drivers/gpio/gpiolib.h      | 3 ++-
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/gpio/gpiolib-acpi.c +2 -3
index a3faefa44f68..9b37a3692b3f 100644
@@ -416,9 +416,8 @@ static int acpi_populate_gpio_lookup(struct acpi_resource *ares, void *data)
 			agpio->connection_type == ACPI_RESOURCE_GPIO_TYPE_INT;
 
 		/*
-		 * ActiveLow is only specified for GpioInt resource. If
-		 * GpioIo is used then the only way to set the flag is
-		 * to use _DSD "gpios" property.
+		 * Polarity and triggering are only specified for GpioInt
+		 * resource.
 		 * Note: we expect here:
 		 * - ACPI_ACTIVE_LOW == GPIO_ACTIVE_LOW
 		 * - ACPI_ACTIVE_HIGH == GPIO_ACTIVE_HIGH
drivers/gpio/gpiolib.h +2 -1
index d10eaf520860..2495b7ee1b42 100644
@@ -76,7 +76,8 @@ struct gpio_device {
 /**
  * struct acpi_gpio_info - ACPI GPIO specific information
  * @gpioint: if %true this GPIO is of type GpioInt otherwise type is GpioIo
- * @active_low: in case of @gpioint, the pin is active low
+ * @polarity: interrupt polarity as provided by ACPI
+ * @triggering: triggering type as provided by ACPI
  */
 struct acpi_gpio_info {
 	bool gpioint;
Signature · 1 line
2.11.0
Acked-by on v1 2/3
#2 lore raw

Re: [PATCH v1 2/3] gpiolib: Update documentation of struct acpi_gpio_info

Quoted 8 lines · Andy Shevchenko

It seems the code had been changed, but description left untouched.

Update description of the struct acpi_gpio_info and relative comments accordingly.

Fixes commit 52044723cd27 ("ACPI / gpio: Add irq_type when a GPIO is used as an interrupt")
Cc Christophe RICARD <[email protected]>
#3 lore raw

Re: [PATCH v1 2/3] gpiolib: Update documentation of struct acpi_gpio_info

In reply to Andy Shevchenko
Quoted 8 lines · Andy Shevchenko

It seems the code had been changed, but description left untouched.

Update description of the struct acpi_gpio_info and relative comments accordingly.

Fixes commit 52044723cd27 ("ACPI / gpio: Add irq_type when a GPIO is used as an interrupt")
Cc Christophe RICARD <[email protected]>

Patch applied with Mika's ACK.

Yours,
Linus Walleij

#4 lore raw

PATCH 3/3 gpiolib: Convert fwnode_get_named_gpiod() to configure GPIO

In reply to Andy Shevchenko

Make fwnode_get_named_gpiod() consistent with the rest of gpiod_get() like API, i.e. configure GPIO pin immediately after request.

Besides obvious clean up it will help to configure pins based on firmware provided resources.

---
 drivers/gpio/devres.c                     |  9 +++++++--
 drivers/gpio/gpiolib.c                    | 20 ++++++++++++++++----
 drivers/input/keyboard/gpio_keys.c        |  9 +--------
 drivers/input/keyboard/gpio_keys_polled.c | 11 ++---------
 drivers/leds/leds-gpio.c                  |  2 +-
 drivers/video/fbdev/amba-clcd-nomadik.c   | 15 +++++----------
 include/linux/gpio/consumer.h             | 19 +++++++++++++------
 7 files changed, 45 insertions(+), 40 deletions(-)
drivers/gpio/devres.c +7 -2
index 54da61112752..3da9c39fed04 100644
@@ -129,13 +129,18 @@ EXPORT_SYMBOL(devm_gpiod_get_index);
  * @dev:	GPIO consumer
  * @con_id:	function within the GPIO consumer
  * @child:	firmware node (child of @dev)
+ * @flags:	GPIO initialization flags
  *
  * GPIO descriptors returned from this function are automatically disposed on
  * driver detach.
+ *
+ * On successfull request the GPIO pin is configured in accordance with
+ * provided @flags.
  */
 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
 					    const char *con_id,
-					    struct fwnode_handle *child)
+					    struct fwnode_handle *child,
+					    enum gpiod_flags flags)
 {
 	char prop_name[32]; /* 32 is max size of property name */
 	struct gpio_desc **dr;
@@ -155,7 +160,7 @@ struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
 			snprintf(prop_name, sizeof(prop_name), "%s",
 					    gpio_suffixes[i]);
 
-		desc = fwnode_get_named_gpiod(child, prop_name);
+		desc = fwnode_get_named_gpiod(child, prop_name, flags);
 		if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
 			break;
 	}
drivers/gpio/gpiolib.c +16 -4
index 7f51c9bf5533..38e415642e03 100644
@@ -3303,6 +3303,7 @@ EXPORT_SYMBOL_GPL(gpiod_get_index);
  * fwnode_get_named_gpiod - obtain a GPIO from firmware node
  * @fwnode:	handle of the firmware node
  * @propname:	name of the firmware property representing the GPIO
+ * @dflags:	GPIO initialization flags
  *
  * This function can be used for drivers that get their configuration
  * from firmware.
@@ -3311,12 +3312,17 @@ EXPORT_SYMBOL_GPL(gpiod_get_index);
  * underlying firmware interface and then makes sure that the GPIO
  * descriptor is requested before it is returned to the caller.
  *
+ * On successfull request the GPIO pin is configured in accordance with
+ * provided @dflags.
+ *
  * In case of error an ERR_PTR() is returned.
  */
 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
-					 const char *propname)
+					 const char *propname,
+					 enum gpiod_flags dflags)
 {
 	struct gpio_desc *desc = ERR_PTR(-ENODEV);
+	unsigned long lflags = 0;
 	bool active_low = false;
 	bool single_ended = false;
 	int ret;
@@ -3349,13 +3355,19 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
 		return ERR_PTR(ret);
 
 	if (active_low)
-		set_bit(FLAG_ACTIVE_LOW, &desc->flags);
+		lflags |= GPIO_ACTIVE_LOW;
 
 	if (single_ended) {
 		if (active_low)
-			set_bit(FLAG_OPEN_DRAIN, &desc->flags);
+			lflags |= GPIO_OPEN_DRAIN;
 		else
-			set_bit(FLAG_OPEN_SOURCE, &desc->flags);
+			lflags |= GPIO_OPEN_SOURCE;
+	}
+
+	ret = gpiod_configure_flags(desc, propname, lflags, dflags);
+	if (ret < 0) {
+		gpiod_put(desc);
+		return ERR_PTR(ret);
 	}
 
 	return desc;
drivers/input/keyboard/gpio_keys.c +1 -8
index 582462d0af75..9de4b876100a 100644
@@ -481,7 +481,7 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
 	spin_lock_init(&bdata->lock);
 
 	if (child) {
-		bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child);
+		bdata->gpiod = devm_get_gpiod_from_child(dev, NULL, child, GPIOD_IN);
 		if (IS_ERR(bdata->gpiod)) {
 			error = PTR_ERR(bdata->gpiod);
 			if (error == -ENOENT) {
@@ -496,13 +496,6 @@ static int gpio_keys_setup_key(struct platform_device *pdev,
 						error);
 				return error;
 			}
-		} else {
-			error = gpiod_direction_input(bdata->gpiod);
-			if (error) {
-				dev_err(dev, "Failed to configure GPIO %d as input: %d\n",
-					desc_to_gpio(bdata->gpiod), error);
-				return error;
-			}
 		}
 	} else if (gpio_is_valid(button->gpio)) {
 		/*
drivers/input/keyboard/gpio_keys_polled.c +2 -9
index bed4f2086158..fd7ab4dad87b 100644
@@ -304,7 +304,8 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
 			}
 
 			bdata->gpiod = devm_get_gpiod_from_child(dev, NULL,
-								 child);
+								 child,
+								 GPIOD_IN);
 			if (IS_ERR(bdata->gpiod)) {
 				error = PTR_ERR(bdata->gpiod);
 				if (error != -EPROBE_DEFER)
@@ -314,14 +315,6 @@ static int gpio_keys_polled_probe(struct platform_device *pdev)
 				fwnode_handle_put(child);
 				return error;
 			}
-
-			error = gpiod_direction_input(bdata->gpiod);
-			if (error) {
-				dev_err(dev, "Failed to configure GPIO %d as input: %d\n",
-					desc_to_gpio(bdata->gpiod), error);
-				fwnode_handle_put(child);
-				return error;
-			}
 		} else if (gpio_is_valid(button->gpio)) {
 			/*
 			 * Legacy GPIO number so request the GPIO here and
drivers/leds/leds-gpio.c +1 -1
index d400dcaf4d29..00cc671cddcc 100644
@@ -174,7 +174,7 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 		const char *state = NULL;
 		struct device_node *np = to_of_node(child);
 
-		led.gpiod = devm_get_gpiod_from_child(dev, NULL, child);
+		led.gpiod = devm_get_gpiod_from_child(dev, NULL, child, GPIOD_ASIS);
 		if (IS_ERR(led.gpiod)) {
 			fwnode_handle_put(child);
 			return ERR_CAST(led.gpiod);
drivers/video/fbdev/amba-clcd-nomadik.c +5 -10
index 0c06fcaaa6e8..2e800d70b0e5 100644
@@ -184,32 +184,27 @@ static void tpg110_init(struct device *dev, struct device_node *np,
 {
 	dev_info(dev, "TPG110 display init\n");
 
-	grestb = devm_get_gpiod_from_child(dev, "grestb", &np->fwnode);
+	/* This asserts the GRESTB signal, putting the display into reset */
+	grestb = devm_get_gpiod_from_child(dev, "grestb", &np->fwnode, GPIOD_OUT_HIGH);
 	if (IS_ERR(grestb)) {
 		dev_err(dev, "no GRESTB GPIO\n");
 		return;
 	}
-	/* This asserts the GRESTB signal, putting the display into reset */
-	gpiod_direction_output(grestb, 1);
-
-	scen = devm_get_gpiod_from_child(dev, "scen", &np->fwnode);
+	scen = devm_get_gpiod_from_child(dev, "scen", &np->fwnode, GPIOD_OUT_LOW);
 	if (IS_ERR(scen)) {
 		dev_err(dev, "no SCEN GPIO\n");
 		return;
 	}
-	gpiod_direction_output(scen, 0);
-	scl = devm_get_gpiod_from_child(dev, "scl", &np->fwnode);
+	scl = devm_get_gpiod_from_child(dev, "scl", &np->fwnode, GPIOD_OUT_LOW);
 	if (IS_ERR(scl)) {
 		dev_err(dev, "no SCL GPIO\n");
 		return;
 	}
-	gpiod_direction_output(scl, 0);
-	sda = devm_get_gpiod_from_child(dev, "sda", &np->fwnode);
+	sda = devm_get_gpiod_from_child(dev, "sda", &np->fwnode, GPIOD_OUT_LOW);
 	if (IS_ERR(sda)) {
 		dev_err(dev, "no SDA GPIO\n");
 		return;
 	}
-	gpiod_direction_output(sda, 0);
 	board->enable = tpg110_enable;
 	board->disable = tpg110_disable;
 }
include/linux/gpio/consumer.h +13 -6
index fb0fde686cb1..930d10049d8d 100644
@@ -135,10 +135,12 @@ int desc_to_gpio(const struct gpio_desc *desc);
 struct fwnode_handle;
 
 struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
-					 const char *propname);
+					 const char *propname,
+					 enum gpiod_flags dflags);
 struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
 					    const char *con_id,
-					    struct fwnode_handle *child);
+					    struct fwnode_handle *child,
+					    enum gpiod_flags flags);
 #else /* CONFIG_GPIOLIB */
 
 static inline int gpiod_count(struct device *dev, const char *con_id)
@@ -411,14 +413,19 @@ static inline int desc_to_gpio(const struct gpio_desc *desc)
 /* Child properties interface */
 struct fwnode_handle;
 
-static inline struct gpio_desc *fwnode_get_named_gpiod(
-	struct fwnode_handle *fwnode, const char *propname)
+static inline
+struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
+					 const char *propname,
+					 enum gpiod_flags dflags)
 {
 	return ERR_PTR(-ENOSYS);
 }
 
-static inline struct gpio_desc *devm_get_gpiod_from_child(
-	struct device *dev, const char *con_id, struct fwnode_handle *child)
+static inline
+struct gpio_desc *devm_get_gpiod_from_child(struct device *dev,
+					    const char *con_id,
+					    struct fwnode_handle *child,
+					    enum gpiod_flags flags)
 {
 	return ERR_PTR(-ENOSYS);
 }
Signature · 1 line
2.11.0
Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH v1 3/3] gpiolib: Convert fwnode_get_named_gpiod() to configure GPIO

Quoted 22 lines · Andy Shevchenko

Make fwnode_get_named_gpiod() consistent with the rest of gpiod_get() like API, i.e. configure GPIO pin immediately after request.

Besides obvious clean up it will help to configure pins based on firmware provided resources.

---
 drivers/gpio/devres.c                     |  9 +++++++--
 drivers/gpio/gpiolib.c                    | 20 ++++++++++++++++----
 drivers/input/keyboard/gpio_keys.c        |  9 +--------
 drivers/input/keyboard/gpio_keys_polled.c | 11 ++---------
 drivers/leds/leds-gpio.c                  |  2 +-
 drivers/video/fbdev/amba-clcd-nomadik.c   | 15 +++++----------
 include/linux/gpio/consumer.h             | 19 +++++++++++++------
 7 files changed, 45 insertions(+), 40 deletions(-)
drivers/gpio/devres.c +0 -0
index 54da61112752..3da9c39fed04 100644

drivers/gpio/devres.c EXPORT_SYMBOL(devm_gpiod_get_index); · line 132

Quoted 4 lines
drivers/gpio/devres.c +1 -0
@@ -129,13 +129,18 @@ EXPORT_SYMBOL(devm_gpiod_get_index);
  * @dev:	GPIO consumer
  * @con_id:	function within the GPIO consumer
  * @child:	firmware node (child of @dev)
+ * @flags:	GPIO initialization flags

Please call this 'dflags' as it is called elsewhere as well.

Otherwise looks reasonable to me.

#6 lore raw

Re: [PATCH v1 3/3] gpiolib: Convert fwnode_get_named_gpiod() to configure GPIO

Quoted 20 lines · Mika Westerberg
Quoted 19 lines · Andy Shevchenko

Make fwnode_get_named_gpiod() consistent with the rest of gpiod_get() like API,
i.e. configure GPIO pin immediately after request.

Besides obvious clean up it will help to configure pins based on firmware
provided resources.

---
 drivers/gpio/devres.c                     |  9 +++++++-- drivers/gpio/gpiolib.c                    | 20 ++++++++++++++++---- drivers/input/keyboard/gpio_keys.c        |  9 +-------- drivers/input/keyboard/gpio_keys_polled.c | 11 ++--------- drivers/leds/leds-gpio.c                  |  2 +- drivers/video/fbdev/amba-clcd-nomadik.c   | 15 +++++---------- include/linux/gpio/consumer.h             | 19 +++++++++++++------

 7 files changed, 45 insertions(+), 40 deletions(-)
Quoted 11 lines

drivers/gpio/devres.c EXPORT_SYMBOL(devm_gpiod_get_index); · line 132

Quoted 9 lines
drivers/gpio/devres.c +1 -0
index 54da61112752..3da9c39fed04 100644
@@ -129,13 +129,18 @@ EXPORT_SYMBOL(devm_gpiod_get_index);
   * @dev:	GPIO consumer
   * @con_id:	function within the GPIO consumer
   * @child:	firmware node (child of @dev)
+ * @flags:	GPIO initialization flags

Please call this 'dflags' as it is called elsewhere as well.

Elsewhere in this file it's called 'flags'. I would stick with that (btw, my internal first version used to have 'dflags').

Quoted 4 lines

Otherwise looks reasonable to me.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#7 lore raw

Re: [PATCH v1 3/3] gpiolib: Convert fwnode_get_named_gpiod() to configure GPIO

Quoted 4 lines · Andy Shevchenko
Quoted 1 line

Please call this 'dflags' as it is called elsewhere as well.

Elsewhere in this file it's called 'flags'. I would stick with that (btw, my internal first version used to have 'dflags').

Fair enough :)

Reviewed-by on v1 1/3
#8 lore raw
In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko

The macro for_each_set_bit() effectively looks up to the next set bit in array of bits.

Instead of open coding that switch to for_each_set_bit() in gpio_chip_set_multiple().

While here, make gpio_chip_set_multiple() non-destructive against its parameters. We are safe since all callers, i.e. gpiod_set_array_value_complex(), handle that already.

#9 lore raw
In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko

The macro for_each_set_bit() effectively looks up to the next set bit in array of bits.

Instead of open coding that switch to for_each_set_bit() in gpio_chip_set_multiple().

While here, make gpio_chip_set_multiple() non-destructive against its parameters. We are safe since all callers, i.e. gpiod_set_array_value_complex(), handle that already.

Irrestible clarification patch, applied with Mika's review tag. Thanks!

Yours,
Linus Walleij