Skip to contentSkip to conversation

gpiolib: Add GPIO name support

PATCH Superseded Markus Pargmann 3 participants 22 messages Aug 14, 2015 – Sep 24, 2015 GPIO

lore
Share & tools
lore
#0 lore raw

v3 cover gpiolib: Add GPIO name support

Changes in v3:
As discussed in the v2 thread, we are using the already existing gpiochip names now. To avoid races between kernel and userspace, the setup of gpio names in the gpio descriptors has to happen right before the gpiochip is added for use and after the gpio descriptors are available. Unfortunately this is exactly in the function gpiochip_add().

So we can't use a a function as proposed in the discussion like gpiochip_set_names(), as this function would only be possible after we executed gpiochip_add(). Before that point the gpio descriptors are not available. But this can potentially lead to races between userspace and the gpio chip.

That is the reason why I left the names array in the gpioscript. But this array is now only used to initialize the GPIO descriptors. The named sysfs directories are now created using the name stored in the gpio descriptor.

As the name is now directly represented through the sysfs directory name, I am not sure if it makes sense to keep the patch which adds a 'name' property to the sysfs GPIO directories. For the moment it is still in this series.

Other changes in v3:
- Renamed generic of_parse_gpio() to of_parse_own_gpio()
- Replaced pr_warn with dev_warn as the device is available in the parsing code in gpiolib-of
- Introduced name collision checks when adding the names from gpiochip->names to the gpio descriptors
- Added documentation about /sys/class/gpio/<GPIO>/name
- Added check for DT in case the GPIO already has a name. This may happen as there are different ways to set a name, through 'line-name' property and node name

Best Regards,

Markus

Changes in v2:
- Removed patch 'gpiolib: Fix possible use of wrong name'
- Removed discussed patch 'gpio: Allow hogged gpios to be requested' from this series
- Fixed show gpio name patch (locks, (null) printing, and getting the descriptor)
- Merged gpio name patch into gpio_name_to_desc() patch

Description from v1:

This is a proposal to add GPIO names to the kernel based on devicetree descriptions.

This series adds GPIO name support. Until now it is only possible to use names for already requested GPIOs (for example what they are used for). It is not possible to identify GPIOs by a name although most of them have a name for example in the schematics of the board. This makes it difficult to identify a specific GPIO from userspace.

As the GPIO name information is a hardware description this series uses the devicetree bindings introduced by the GPIO hogging mechanism, specifically 'line-name', to identify GPIOs. The sysfs 'export' file is changed to accept names as fallback. The gpio numbers still have a higher priority to ensure backwards compatibility.

Exported GPIOs are still using their number as directory name (gpio<ID>). But the directories now contain a 'name' file which is '' for non-existent names and the name otherwise.

This series can be used to have an easy name mapping for udev with a quite simple rule similar to this:

	SUBSYSTEM=="gpio", KERNEL=="gpio*", ATTR{name}!="(null)", ACTION=="add", \
	PROGRAM+="/bin/sh -c 'mkdir -p /dev/gpios; rm -f /dev/gpios/$attr{name}; ln -s /sys%p/ /dev/gpios/$attr{name}"

With this rule udev adds a link for each exported GPIO with a name into /dev/gpios/. This way it is not necessary to know the number of a GPIO to use it.

Markus Pargmann (9):
gpiolib-of: Rename gpio_hog functions to be generic
gpio: Introduce gpio descriptor 'name'
gpiolib: Use GPIO name from names array for gpio descriptor
gpio-sysfs: Use gpio descriptor name instead of gpiochip names array
gpiolib: Add gpio name information to /sys/kernel/debug/gpio
gpiolib-sysfs: Show gpio-name in /sys/class/gpio/gpio*/name
gpiolib-sysfs: Add gpio name parsing for sysfs export
gpiolib-of: Reuse 'line-name' from DT as gpio descriptor name
gpiolib-of: Use dev_warn instead of pr_warn

 Documentation/ABI/testing/sysfs-gpio |  1 +
 drivers/gpio/gpiolib-of.c            | 50 +++++++++++++---------
 drivers/gpio/gpiolib-sysfs.c         | 67 +++++++++++++++++++++---------
 drivers/gpio/gpiolib.c               | 80 ++++++++++++++++++++++++++++++++++--
 drivers/gpio/gpiolib.h               |  3 ++
 include/linux/gpio/consumer.h        |  7 ++++
 6 files changed, 166 insertions(+), 42 deletions(-)
Signature · 1 line
2.4.6
#1 lore raw

PATCH v3 1/9 gpiolib-of: Rename gpio_hog functions to be generic

The gpio hogging functions are currently only used for gpio-hogging. But these functions are widely generic ones which parse gpio device nodes in the DT.

---
 drivers/gpio/gpiolib-of.c | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
drivers/gpio/gpiolib-of.c +10 -10
index 9a0ec48a4737..fea80bbccf12 100644
@@ -119,20 +119,20 @@ int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
 EXPORT_SYMBOL(of_get_named_gpio_flags);
 
 /**
- * of_get_gpio_hog() - Get a GPIO hog descriptor, names and flags for GPIO API
+ * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
  * @np:		device node to get GPIO from
  * @name:	GPIO line name
  * @lflags:	gpio_lookup_flags - returned from of_find_gpio() or
- *		of_get_gpio_hog()
+ *		of_parse_own_gpio()
  * @dflags:	gpiod_flags - optional GPIO initialization flags
  *
  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  * value on the error condition.
  */
-static struct gpio_desc *of_get_gpio_hog(struct device_node *np,
-				  const char **name,
-				  enum gpio_lookup_flags *lflags,
-				  enum gpiod_flags *dflags)
+static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
+					   const char **name,
+					   enum gpio_lookup_flags *lflags,
+					   enum gpiod_flags *dflags)
 {
 	struct device_node *chip_np;
 	enum of_gpio_flags xlate_flags;
@@ -199,13 +199,13 @@ static struct gpio_desc *of_get_gpio_hog(struct device_node *np,
 }
 
 /**
- * of_gpiochip_scan_hogs - Scan gpio-controller and apply GPIO hog as requested
+ * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  * @chip:	gpio chip to act on
  *
  * This is only used by of_gpiochip_add to request/set GPIO initial
  * configuration.
  */
-static void of_gpiochip_scan_hogs(struct gpio_chip *chip)
+static void of_gpiochip_scan_gpios(struct gpio_chip *chip)
 {
 	struct gpio_desc *desc = NULL;
 	struct device_node *np;
@@ -217,7 +217,7 @@ static void of_gpiochip_scan_hogs(struct gpio_chip *chip)
 		if (!of_property_read_bool(np, "gpio-hog"))
 			continue;
 
-		desc = of_get_gpio_hog(np, &name, &lflags, &dflags);
+		desc = of_parse_own_gpio(np, &name, &lflags, &dflags);
 		if (IS_ERR(desc))
 			continue;
 
@@ -436,7 +436,7 @@ void of_gpiochip_add(struct gpio_chip *chip)
 	of_gpiochip_add_pin_range(chip);
 	of_node_get(chip->of_node);
 
-	of_gpiochip_scan_hogs(chip);
+	of_gpiochip_scan_gpios(chip);
 }
 
 void of_gpiochip_remove(struct gpio_chip *chip)
Signature · 1 line
2.4.6
#2 lore raw

PATCH v3 1/9 gpiolib-of: Rename gpio_hog functions to be generic

Quoted 5 lines · Markus Pargmann

The gpio hogging functions are currently only used for gpio-hogging. But these functions are widely generic ones which parse gpio device nodes in the DT.

This patch applied after a small rebasing.

BTW: at this point, please base your subsequent submissions on my "devel" branch from linux-gpio as I am starting to apply some stuff.

Yours,
Linus Walleij

#3 lore raw

PATCH v3 2/9 gpio: Introduce gpio descriptor 'name'

In reply to Markus Pargmann

The latest gpio hogging mechanism assigns each gpio a 'line-name' in the devicetree. The 'name' field is different from the 'label' field. 'label' is only used for requested GPIOs to describe its current use by driver or userspace.

The 'name' field describes the GPIO itself, not the use. This is most likely identical to the label in the schematic on the GPIO line and should help to find this particular GPIO.

This is equivalent to the gpiochip->names array. However names should be stored in the GPIO descriptor. We will use gpiochip->names in the future only as initializer for the GPIO descriptors for drivers that assign GPIO names hardcoded. All other GPIO names will be parsed from DT and directly assigned to the GPIO descriptor.

This patch adds a helper function to find gpio descriptors by name instead of gpio number.

---
 drivers/gpio/gpiolib.c        | 32 ++++++++++++++++++++++++++++++++
 drivers/gpio/gpiolib.h        |  3 +++
 include/linux/gpio/consumer.h |  7 +++++++
 3 files changed, 42 insertions(+)
drivers/gpio/gpiolib.c +32 -0
index bf4bd1d120c3..eed3751fe14f 100644
@@ -90,6 +90,38 @@ struct gpio_desc *gpio_to_desc(unsigned gpio)
 EXPORT_SYMBOL_GPL(gpio_to_desc);
 
 /**
+ * Convert a GPIO name to its descriptor
+ */
+struct gpio_desc *gpio_name_to_desc(const char * const name)
+{
+	struct gpio_chip *chip;
+	unsigned long flags;
+
+	spin_lock_irqsave(&gpio_lock, flags);
+
+	list_for_each_entry(chip, &gpio_chips, list) {
+		int i;
+
+		for (i = 0; i != chip->ngpio; ++i) {
+			struct gpio_desc *gpio = &chip->desc[i];
+
+			if (!gpio->name)
+				continue;
+
+			if (!strcmp(gpio->name, name)) {
+				spin_unlock_irqrestore(&gpio_lock, flags);
+				return gpio;
+			}
+		}
+	}
+
+	spin_unlock_irqrestore(&gpio_lock, flags);
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(gpio_name_to_desc);
+
+/**
  * Get the GPIO descriptor corresponding to the given hw number for this chip.
  */
 struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
drivers/gpio/gpiolib.h +3 -0
index bf343004b008..78e634d1c719 100644
@@ -89,7 +89,10 @@ struct gpio_desc {
 #define FLAG_USED_AS_IRQ 9	/* GPIO is connected to an IRQ */
 #define FLAG_IS_HOGGED	11	/* GPIO is hogged */
 
+	/* Connection label */
 	const char		*label;
+	/* Name of the GPIO */
+	const char		*name;
 };
 
 int gpiod_request(struct gpio_desc *desc, const char *label);
include/linux/gpio/consumer.h +7 -0
index adac255aee86..a873b8b47ab3 100644
@@ -130,6 +130,7 @@ int gpiod_to_irq(const struct gpio_desc *desc);
 /* Convert between the old gpio_ and new gpiod_ interfaces */
 struct gpio_desc *gpio_to_desc(unsigned gpio);
 int desc_to_gpio(const struct gpio_desc *desc);
+struct gpio_desc *gpio_name_to_desc(const char *name);
 
 /* Child properties interface */
 struct fwnode_handle;
@@ -400,6 +401,12 @@ static inline struct gpio_desc *gpio_to_desc(unsigned gpio)
 {
 	return ERR_PTR(-EINVAL);
 }
+
+static inline struct gpio_desc *gpio_name_to_desc(const char *name)
+{
+	return ERR_PTR(-EINVAL);
+}
+
 static inline int desc_to_gpio(const struct gpio_desc *desc)
 {
 	/* GPIO can never have been requested */
Signature · 1 line
2.4.6
#4 lore raw

PATCH v3 2/9 gpio: Introduce gpio descriptor 'name'

Quoted 15 lines · Markus Pargmann

The latest gpio hogging mechanism assigns each gpio a 'line-name' in the devicetree. The 'name' field is different from the 'label' field. 'label' is only used for requested GPIOs to describe its current use by driver or userspace.

The 'name' field describes the GPIO itself, not the use. This is most likely identical to the label in the schematic on the GPIO line and should help to find this particular GPIO.

This is equivalent to the gpiochip->names array. However names should be stored in the GPIO descriptor. We will use gpiochip->names in the future only as initializer for the GPIO descriptors for drivers that assign GPIO names hardcoded. All other GPIO names will be parsed from DT and directly assigned to the GPIO descriptor.

Quoted 4 lines

This patch adds a helper function to find gpio descriptors by name instead of gpio number.

Patch applied.

Yours,
Linus Walleij

#5 lore raw

PATCH v3 3/9 gpiolib: Use GPIO name from names array for gpio descriptor

In reply to Markus Pargmann

This patch adds GPIO names to the GPIO descriptors when initializing the gpiochip. It also introduces a check whether any of the new names will conflict with an existing GPIO name.

---
 drivers/gpio/gpiolib.c | 37 +++++++++++++++++++++++++++++++++++++
 1 file changed, 37 insertions(+)
drivers/gpio/gpiolib.c +37 -0
index eed3751fe14f..3908503bebfe 100644
@@ -250,6 +250,38 @@ static int gpiochip_add_to_list(struct gpio_chip *chip)
 	return err;
 }
 
+/*
+ * Takes the names from gc->names and checks if they are all unique. If they
+ * are, they are assigned to their gpio descriptors.
+ *
+ * Returns -EEXIST if one of the names is already used for a different GPIO.
+ */
+static int gpiochip_set_desc_names(struct gpio_chip *gc)
+{
+	int i;
+
+	if (!gc->names)
+		return 0;
+
+	/* First check all names if they are unique */
+	for (i = 0; i != gc->ngpio; ++i) {
+		struct gpio_desc *gpio;
+
+		gpio = gpio_name_to_desc(gc->names[i]);
+		if (gpio) {
+			dev_err(gc->dev, "Detected name collision for GPIO name '%s'\n",
+				gc->names[i]);
+			return -EEXIST;
+		}
+	}
+
+	/* Then add all names to the GPIO descriptors */
+	for (i = 0; i != gc->ngpio; ++i)
+		gc->desc[i].name = gc->names[i];
+
+	return 0;
+}
+
 /**
  * gpiochip_add() - register a gpio_chip
  * @chip: the chip to register, with chip->base initialized
@@ -319,6 +351,10 @@ int gpiochip_add(struct gpio_chip *chip)
 	INIT_LIST_HEAD(&chip->pin_ranges);
 #endif
 
+	status = gpiochip_set_desc_names(chip);
+	if (status)
+		goto err_remove_from_list;
+
 	of_gpiochip_add(chip);
 	acpi_gpiochip_add(chip);
 
@@ -336,6 +372,7 @@ err_remove_chip:
 	acpi_gpiochip_remove(chip);
 	gpiochip_free_hogs(chip);
 	of_gpiochip_remove(chip);
+err_remove_from_list:
 	spin_lock_irqsave(&gpio_lock, flags);
 	list_del(&chip->list);
 	spin_unlock_irqrestore(&gpio_lock, flags);
Signature · 1 line
2.4.6
#6 lore raw

PATCH v3 3/9 gpiolib: Use GPIO name from names array for gpio descriptor

Quoted 5 lines · Markus Pargmann

This patch adds GPIO names to the GPIO descriptors when initializing the gpiochip. It also introduces a check whether any of the new names will conflict with an existing GPIO name.

Patch applied. To me this is the magic transition patch that make things really smooth.

Yours,
Linus Walleij

#7 lore raw

PATCH v3 3/9 gpiolib: Use GPIO name from names array for gpio descriptor

In reply to Markus Pargmann
Quoted 3 lines · Markus Pargmann

This patch adds GPIO names to the GPIO descriptors when initializing the gpiochip. It also introduces a check whether any of the new names will conflict with an existing GPIO name.

And this is why we should not do this. I've said it it before: we need to consider dynamic buses, not just static device trees.

We have USB and Greybus that will soon be exposing gpiochips and their line-names can not be globally unique.

Before figuring out what a sane userspace interface should look like we should not make ABI changes that we need to support indefinitely. The legacy sysfs interface should instead be frozen.

Exposing a name attribute for a not-necessarily globally unique name could perhaps be ok, but by enforcing uniqueness at this point we will then need to come up with another mechanism for naming gpios on dynamic buses (while maintaining this one indefinitely in parallel).

Linus, please consider dropping these changes for now.

Thanks,
Johan

#8 lore raw

PATCH v3 3/9 gpiolib: Use GPIO name from names array for gpio descriptor

Quoted 10 lines · Johan Hovold

Before figuring out what a sane userspace interface should look like we should not make ABI changes that we need to support indefinitely. The legacy sysfs interface should instead be frozen.

Exposing a name attribute for a not-necessarily globally unique name could perhaps be ok, but by enforcing uniqueness at this point we will then need to come up with another mechanism for naming gpios on dynamic buses (while maintaining this one indefinitely in parallel).

Linus, please consider dropping these changes for now.

I rounded off the series after moving the name attribute to the descriptor before doing any ABI changes, as it seems we need to discuss this.

This patch concludes that part of the series, sent a separate patch concluding this first half of the series.

Yours,
Linus Walleij

#9 lore raw

PATCH v3 4/9 gpio-sysfs: Use gpio descriptor name instead of gpiochip names array

In reply to Markus Pargmann

The name is now stored in the gpio descriptor as well, for example to allow to store names from DT. This patch changes the sysfs gpio files to use the gpio descriptor name.

---
 drivers/gpio/gpiolib-sysfs.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/gpio/gpiolib-sysfs.c +1 -7
index b57ed8e55ab5..3e81f28e3aee 100644
@@ -550,9 +550,7 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
 	struct gpiod_data	*data;
 	unsigned long		flags;
 	int			status;
-	const char		*ioname = NULL;
 	struct device		*dev;
-	int			offset;
 
 	/* can't export until sysfs is available ... */
 	if (!gpio_class.p) {
@@ -601,13 +599,9 @@ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
 	else
 		data->direction_can_change = false;
 
-	offset = gpio_chip_hwgpio(desc);
-	if (chip->names && chip->names[offset])
-		ioname = chip->names[offset];
-
 	dev = device_create_with_groups(&gpio_class, chip->dev,
 					MKDEV(0, 0), data, gpio_groups,
-					ioname ? ioname : "gpio%u",
+					desc->name ? desc->name : "gpio%u",
 					desc_to_gpio(desc));
 	if (IS_ERR(dev)) {
 		status = PTR_ERR(dev);
Signature · 1 line
2.4.6
#10 lore raw

PATCH v3 4/9 gpio-sysfs: Use gpio descriptor name instead of gpiochip names array

Quoted 5 lines · Markus Pargmann

The name is now stored in the gpio descriptor as well, for example to allow to store names from DT. This patch changes the sysfs gpio files to use the gpio descriptor name.

Patch applied. This is a very nice patch too.

Yours,
Linus Walleij

#11 lore raw

PATCH v3 5/9 gpiolib: Add gpio name information to /sys/kernel/debug/gpio

In reply to Markus Pargmann

Add some information about gpio names to the debugfs gpio file. name and label of a GPIO are then displayed next to each other. This way it is easy to see what the real name of GPIO is and what the driver requested it for.

---
 Documentation/ABI/testing/sysfs-gpio |  1 +
 drivers/gpio/gpiolib.c               | 11 ++++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-gpio +1 -0
index 55ffa2df1c10..2c1e199fd4e1 100644
@@ -21,6 +21,7 @@ Description:
 	    /value ... always readable, writes fail for input GPIOs
 	    /direction ... r/w as: in, out (default low); write: high, low
 	    /edge ... r/w as: none, falling, rising, both
+	    /name ... r/o, unique name of the GPIO
 	/gpiochipN ... for each gpiochip; #N is its first GPIO
 	    /base ... (r/o) same as N
 	    /label ... (r/o) descriptive, not necessarily unique
drivers/gpio/gpiolib.c +8 -3
index 3908503bebfe..79a0b41ce57b 100644
@@ -2344,14 +2344,19 @@ static void gpiolib_dbg_show(struct seq_file *s, struct gpio_chip *chip)
 	int			is_irq;
 
 	for (i = 0; i < chip->ngpio; i++, gpio++, gdesc++) {
-		if (!test_bit(FLAG_REQUESTED, &gdesc->flags))
+		if (!test_bit(FLAG_REQUESTED, &gdesc->flags)) {
+			if (gdesc->name) {
+				seq_printf(s, " gpio-%-3d (%-20.20s)\n",
+					   gpio, gdesc->name);
+			}
 			continue;
+		}
 
 		gpiod_get_direction(gdesc);
 		is_out = test_bit(FLAG_IS_OUT, &gdesc->flags);
 		is_irq = test_bit(FLAG_USED_AS_IRQ, &gdesc->flags);
-		seq_printf(s, " gpio-%-3d (%-20.20s) %s %s %s",
-			gpio, gdesc->label,
+		seq_printf(s, " gpio-%-3d (%-20.20s|%-20.20s) %s %s %s",
+			gpio, gdesc->name ? gdesc->name : "", gdesc->label,
 			is_out ? "out" : "in ",
 			chip->get
 				? (chip->get(chip, i) ? "hi" : "lo")
Signature · 1 line
2.4.6
#12 lore raw

PATCH v3 5/9 gpiolib: Add gpio name information to /sys/kernel/debug/gpio

Quoted 6 lines · Markus Pargmann

Add some information about gpio names to the debugfs gpio file. name and label of a GPIO are then displayed next to each other. This way it is easy to see what the real name of GPIO is and what the driver requested it for.

PARTLY applied.

Documentation/ABI/testing/sysfs-gpio Description: · line 24

Quoted 6 lines

+++ b/Documentation/ABI/testing/sysfs-gpio

Documentation/ABI/testing/sysfs-gpio +1 -0
@@ -21,6 +21,7 @@ Description:
            /value ... always readable, writes fail for input GPIOs
            /direction ... r/w as: in, out (default low); write: high, low
            /edge ... r/w as: none, falling, rising, both
+           /name ... r/o, unique name of the GPIO

This patch has nothing to do with sysfs and this is not added to the ABI. I removed this hunk from the patch.

The rest is applied.

Yours,
Linus Walleij

#13 lore raw

PATCH v3 6/9 gpiolib-sysfs: Show gpio-name in /sys/class/gpio/gpio*/name

In reply to Markus Pargmann

This patch adds a sysfs attribute 'name' to gpios that were exported. It exposes the newly added name property of gpio descriptors.

---
 drivers/gpio/gpiolib-sysfs.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/gpio/gpiolib-sysfs.c +12 -0
index 3e81f28e3aee..2022cfb00aeb 100644
@@ -139,6 +139,17 @@ static ssize_t value_store(struct device *dev,
 }
 static DEVICE_ATTR_RW(value);
 
+static ssize_t name_show(struct device *dev, struct device_attribute *attr,
+			 char *buf)
+{
+	struct gpiod_data *data = dev_get_drvdata(dev);
+	struct gpio_desc *desc = data->desc;
+
+	return sprintf(buf, "%s\n", desc->name ? : "");
+}
+
+static DEVICE_ATTR_RO(name);
+
 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
 {
 	struct gpiod_data *data = priv;
@@ -377,6 +388,7 @@ static struct attribute *gpio_attrs[] = {
 	&dev_attr_edge.attr,
 	&dev_attr_value.attr,
 	&dev_attr_active_low.attr,
+	&dev_attr_name.attr,
 	NULL,
 };
 
Signature · 1 line
2.4.6
#14 lore raw

PATCH v3 6/9 gpiolib-sysfs: Show gpio-name in /sys/class/gpio/gpio*/name

Quoted 4 lines · Markus Pargmann

This patch adds a sysfs attribute 'name' to gpios that were exported. It exposes the newly added name property of gpio descriptors.

The previous hunk adding the name to the sysfs ABI should be in this patch.

But I have a problem with this. What is the rationale? The directory that contains the file already has this name. Why should this information be duplicated?

Yours,
Linus Walleij

#15 lore raw

PATCH v3 6/9 gpiolib-sysfs: Show gpio-name in /sys/class/gpio/gpio*/name

Quoted 5 lines · Linus Walleij
Quoted 3 lines · Markus Pargmann

This patch adds a sysfs attribute 'name' to gpios that were exported. It exposes the newly added name property of gpio descriptors.

Quoted 8 lines
Quoted 1 line

The previous hunk adding the name to the sysfs ABI should be in this patch.

But I have a problem with this. What is the rationale? The directory that contains the file already has this name. Why should this information be duplicated?

No reason for that. As described in the summary, I wasn't sure if this makes sense but included it anyway from the last series version. So it can simply be dropped.

Thanks,

Markus

Signature · 11 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150924/3ca9ff0d/attachment.sig>
#16 lore raw

PATCH v3 7/9 gpiolib-sysfs: Add gpio name parsing for sysfs export

In reply to Markus Pargmann
---
 drivers/gpio/gpiolib-sysfs.c | 47 ++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 13 deletions(-)
drivers/gpio/gpiolib-sysfs.c +34 -13
index 2022cfb00aeb..e401878d05ce 100644
@@ -455,18 +455,28 @@ static ssize_t export_store(struct class *class,
 				const char *buf, size_t len)
 {
 	long			gpio;
-	struct gpio_desc	*desc;
+	struct gpio_desc	*desc = NULL;
 	int			status;
 
 	status = kstrtol(buf, 0, &gpio);
-	if (status < 0)
-		goto done;
+	if (!status)
+		desc = gpio_to_desc(gpio);
 
-	desc = gpio_to_desc(gpio);
-	/* reject invalid GPIOs */
+	/* Fall back on detection by name */
 	if (!desc) {
-		pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
-		return -EINVAL;
+		char *gpio_name = kstrdup(buf, GFP_KERNEL);
+
+		if (!gpio_name)
+			return -ENOMEM;
+
+		desc = gpio_name_to_desc(strim(gpio_name));
+		kfree(gpio_name);
+
+		/* reject invalid GPIOs */
+		if (!desc) {
+			pr_warn("%s: invalid GPIO %s\n", __func__, buf);
+			return -EINVAL;
+		}
 	}
 
 	/* No extra locking here; FLAG_SYSFS just signifies that the
@@ -497,17 +507,28 @@ static ssize_t unexport_store(struct class *class,
 				const char *buf, size_t len)
 {
 	long			gpio;
-	struct gpio_desc	*desc;
+	struct gpio_desc	*desc = NULL;
 	int			status;
 
 	status = kstrtol(buf, 0, &gpio);
-	if (status < 0)
-		goto done;
+	if (!status)
+		desc = gpio_to_desc(gpio);
+
+	/* Fall back on detection by name */
+	if (!desc) {
+		char *gpio_name = kstrdup(buf, GFP_KERNEL);
+
+		if (!gpio_name)
+			return -ENOMEM;
+
+		desc = gpio_name_to_desc(strim(gpio_name));
+		kfree(gpio_name);
+	}
+
 
-	desc = gpio_to_desc(gpio);
 	/* reject bogus commands (gpio_unexport ignores them) */
 	if (!desc) {
-		pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
+		pr_warn("%s: invalid GPIO %s\n", __func__, buf);
 		return -EINVAL;
 	}
 
@@ -521,7 +542,7 @@ static ssize_t unexport_store(struct class *class,
 		status = 0;
 		gpiod_free(desc);
 	}
-done:
+
 	if (status)
 		pr_debug("%s: status %d\n", __func__, status);
 	return status ? : len;
Signature · 1 line
2.4.6
#17 lore raw

PATCH v3 7/9 gpiolib-sysfs: Add gpio name parsing for sysfs export

Quoted 1 line · Markus Pargmann

This has too terse commit description. I don't understand the patch or what it is supposed to do.

Yours,
Linus Walleij

#18 lore raw

PATCH v3 7/9 gpiolib-sysfs: Add gpio name parsing for sysfs export

Quoted 6 lines · Linus Walleij
Quoted 1 line · Markus Pargmann

This has too terse commit description. I don't understand the patch or what it is supposed to do.

Sorry, indeed, this requires a commit description. I would add something like this:

In '/sys/class/gpio/' GPIOs can be exported by using their GPIO number. If the exported GPIO has a name, it will show up as a new directory in '/sys/class/gpio/<GPIO_NAME>'. It is somehow inconsistent to use a number for exporting and the name for accessing the GPIO.

This patch adds the possibility to use the GPIO name to export it. This introduces a consistent behaviour for exporting and accessing a GPIO from userspace.

The mechanism of parsing the GPIO name is a fallback mechanism. This ensures backwards compatibility of the gpio interface. If the string passed to '/sys/class/gpio/export' can not be parsed as number, it tries to match a GPIO name. The same behaviour is implemented for the 'unexport' file.

The implementation uses the previously defined gpio_name_to_desc().

Thanks,

Markus

Signature · 11 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150924/591166c9/attachment-0001.sig>
#19 lore raw

PATCH v3 8/9 gpiolib-of: Reuse 'line-name' from DT as gpio descriptor name

In reply to Markus Pargmann

The DT describes gpios with subnodes. These subnodes can contain a property 'line-name'. This information can be useful in other areas where we want to identify a GPIO by its name.

This patch stores the line-name in the gpio descriptor.

---
 drivers/gpio/gpiolib-of.c | 30 +++++++++++++++++++++---------
 1 file changed, 21 insertions(+), 9 deletions(-)
drivers/gpio/gpiolib-of.c +21 -9
index fea80bbccf12..1911b692f735 100644
@@ -184,11 +184,6 @@ static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
 		*dflags |= GPIOD_OUT_LOW;
 	else if (of_property_read_bool(np, "output-high"))
 		*dflags |= GPIOD_OUT_HIGH;
-	else {
-		pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
-			desc_to_gpio(gg_data.out_gpio), np->name);
-		return ERR_PTR(-EINVAL);
-	}
 
 	if (name && of_property_read_string(np, "line-name", name))
 		*name = np->name;
@@ -214,15 +209,32 @@ static void of_gpiochip_scan_gpios(struct gpio_chip *chip)
 	enum gpiod_flags dflags;
 
 	for_each_child_of_node(chip->of_node, np) {
-		if (!of_property_read_bool(np, "gpio-hog"))
-			continue;
+		struct gpio_desc *name_desc;
 
 		desc = of_parse_own_gpio(np, &name, &lflags, &dflags);
 		if (IS_ERR(desc))
 			continue;
 
-		if (gpiod_hog(desc, name, lflags, dflags))
-			continue;
+		name_desc = gpio_name_to_desc(name);
+		if (name_desc)
+			dev_warn(chip->dev, "GPIO name collision for '%s' detected at GPIO line %d (%s)\n",
+				 name, desc_to_gpio(desc), np->name);
+		else if (desc->name)
+			dev_warn(chip->dev, "GPIO has already a name '%s' new name would be '%s' at GPIO %d\n",
+				 desc->name, name, desc_to_gpio(desc));
+		else
+			desc->name = name;
+
+		if (of_property_read_bool(np, "gpio-hog")) {
+			if (!dflags) {
+				pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
+					desc_to_gpio(desc), np->name);
+				continue;
+			}
+
+			if (gpiod_hog(desc, name, lflags, dflags))
+				continue;
+		}
 	}
 }
 
Signature · 1 line
2.4.6
#20 lore raw

PATCH v3 9/9 gpiolib-of: Use dev_warn instead of pr_warn

In reply to Markus Pargmann

chip->dev should be initialized at this point so we can use dev_* here instead of pr_warn.

---
 drivers/gpio/gpiolib-of.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/gpio/gpiolib-of.c +2 -2
index 1911b692f735..48a7579dd62d 100644
@@ -227,8 +227,8 @@ static void of_gpiochip_scan_gpios(struct gpio_chip *chip)
 
 		if (of_property_read_bool(np, "gpio-hog")) {
 			if (!dflags) {
-				pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
-					desc_to_gpio(desc), np->name);
+				dev_warn(chip->dev, "GPIO line %d (%s): no hogging state specified, bailing out\n",
+					 desc_to_gpio(desc), np->name);
 				continue;
 			}
 
Signature · 1 line
2.4.6
#21 lore raw

v3 cover gpiolib: Add GPIO name support

In reply to Markus Pargmann

Hi,

Quoted 12 lines · Markus Pargmann

Changes in v3:
As discussed in the v2 thread, we are using the already existing gpiochip names now. To avoid races between kernel and userspace, the setup of gpio names in the gpio descriptors has to happen right before the gpiochip is added for use and after the gpio descriptors are available. Unfortunately this is exactly in the function gpiochip_add().

So we can't use a a function as proposed in the discussion like gpiochip_set_names(), as this function would only be possible after we executed gpiochip_add(). Before that point the gpio descriptors are not available. But this can potentially lead to races between userspace and the gpio chip.

Quoted 7 lines

That is the reason why I left the names array in the gpioscript. But this array is now only used to initialize the GPIO descriptors. The named sysfs directories are now created using the name stored in the gpio descriptor.

As the name is now directly represented through the sysfs directory name, I am not sure if it makes sense to keep the patch which adds a 'name' property to the sysfs GPIO directories. For the moment it is still in this series.

Any feedback on this series?

Thanks,

Markus

Quoted 82 lines

Other changes in v3:
- Renamed generic of_parse_gpio() to of_parse_own_gpio()
- Replaced pr_warn with dev_warn as the device is available in the parsing code in gpiolib-of
- Introduced name collision checks when adding the names from gpiochip->names to the gpio descriptors
- Added documentation about /sys/class/gpio/<GPIO>/name
- Added check for DT in case the GPIO already has a name. This may happen as there are different ways to set a name, through 'line-name' property and node name

Best Regards,

Markus

Changes in v2:
- Removed patch 'gpiolib: Fix possible use of wrong name'
- Removed discussed patch 'gpio: Allow hogged gpios to be requested' from this series
- Fixed show gpio name patch (locks, (null) printing, and getting the descriptor)
- Merged gpio name patch into gpio_name_to_desc() patch

Description from v1:

This is a proposal to add GPIO names to the kernel based on devicetree descriptions.

This series adds GPIO name support. Until now it is only possible to use names for already requested GPIOs (for example what they are used for). It is not possible to identify GPIOs by a name although most of them have a name for example in the schematics of the board. This makes it difficult to identify a specific GPIO from userspace.

As the GPIO name information is a hardware description this series uses the devicetree bindings introduced by the GPIO hogging mechanism, specifically 'line-name', to identify GPIOs. The sysfs 'export' file is changed to accept names as fallback. The gpio numbers still have a higher priority to ensure backwards compatibility.

Exported GPIOs are still using their number as directory name (gpio<ID>). But the directories now contain a 'name' file which is '' for non-existent names and the name otherwise.

This series can be used to have an easy name mapping for udev with a quite simple rule similar to this:

	SUBSYSTEM=="gpio", KERNEL=="gpio*", ATTR{name}!="(null)", ACTION=="add", \
	PROGRAM+="/bin/sh -c 'mkdir -p /dev/gpios; rm -f /dev/gpios/$attr{name}; ln -s /sys%p/ /dev/gpios/$attr{name}"

With this rule udev adds a link for each exported GPIO with a name into /dev/gpios/. This way it is not necessary to know the number of a GPIO to use it.

Markus Pargmann (9):
gpiolib-of: Rename gpio_hog functions to be generic
gpio: Introduce gpio descriptor 'name'
gpiolib: Use GPIO name from names array for gpio descriptor
gpio-sysfs: Use gpio descriptor name instead of gpiochip names array
gpiolib: Add gpio name information to /sys/kernel/debug/gpio
gpiolib-sysfs: Show gpio-name in /sys/class/gpio/gpio*/name
gpiolib-sysfs: Add gpio name parsing for sysfs export
gpiolib-of: Reuse 'line-name' from DT as gpio descriptor name
gpiolib-of: Use dev_warn instead of pr_warn

 Documentation/ABI/testing/sysfs-gpio |  1 +
 drivers/gpio/gpiolib-of.c            | 50 +++++++++++++---------
 drivers/gpio/gpiolib-sysfs.c         | 67 +++++++++++++++++++++---------
 drivers/gpio/gpiolib.c               | 80 ++++++++++++++++++++++++++++++++++--
 drivers/gpio/gpiolib.h               |  3 ++
 include/linux/gpio/consumer.h        |  7 ++++
 6 files changed, 166 insertions(+), 42 deletions(-)
Signature · 7 lines
2.4.6


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Signature · 11 lines
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20150921/3eecdc29/attachment-0001.sig>