Skip to contentSkip to conversation

Support children for legacy device properties

PATCH In review Dmitry Torokhov 6 participants 26 messages Sep 17, 2018 – Oct 11, 2018

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

cover Support children for legacy device properties

The generic device properties APIs are very helpful as they allow abstracting away details of the platform (whether it is ACPI, device tree, or legacy board file), so that individual driver does not need separate code paths to support all variants. However there are drivers that currently can not use generic device properties API as they need notion of children properties, for example gpio_keys driver, that expects every button to be described as a sub-node of main device.

This patch series introduces notion of sub-nodes for static properties and ties it up with GPIO lookup tables so that they are usable with sub-nodes as well.

Thanks.

Signature · 1 line
Dmitry
#1 lore raw

PATCH 1/5 device property: split generic properties and property sets

drivers/base/property.c became a bit of a kitchen sink, with code handling generic device/fwnode properties intermingled with code handling static properties (pset). Let's split them apart to not pollute each other.

---
 drivers/base/Makefile        |   4 +-
 drivers/base/property.c      | 525 ----------------------------------
 drivers/base/pset_property.c | 540 +++++++++++++++++++++++++++++++++++
 3 files changed, 542 insertions(+), 527 deletions(-)
 create mode 100644 drivers/base/pset_property.c
drivers/base/Makefile +2 -2
index 704f44295810..5f72cd5ab8ba 100644
@@ -5,8 +5,8 @@ obj-y			:= component.o core.o bus.o dd.o syscore.o \
 			   driver.o class.o platform.o \
 			   cpu.o firmware.o init.o map.o devres.o \
 			   attribute_container.o transport_class.o \
-			   topology.o container.o property.o cacheinfo.o \
-			   devcon.o
+			   topology.o container.o cacheinfo.o \
+			   property.o pset_property.o devcon.o
 obj-$(CONFIG_DEVTMPFS)	+= devtmpfs.o
 obj-y			+= power/
 obj-$(CONFIG_ISA_BUS_API)	+= isa.o
drivers/base/property.c +0 -525

543 lines not on this page · raw message

drivers/base/pset_property.c new file +540 -0

540 lines not on this page · raw message

Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#2 lore raw

PATCH 2/5 device property: introduce notion of subnodes for legacy boards

In reply to Dmitry Torokhov

Several drivers rely on having notion of sub-nodes when describing hardware, let's allow static board-defined properties also have it.

The board files will then attach properties to devices in the following fashion:

	device_add_properties(&board_platform_device.dev,
			      main_device_props);
	device_add_child_properties(&board_platform_device.dev,
				    dev_fwnode(&board_platform_device.dev),
				    child1_device_props);
	device_add_child_properties(&board_platform_device.dev,
				    dev_fwnode(&board_platform_device.dev),
				    child2_device_props);
	...
	platform_device_register(&board_platform_device.dev);
---
 drivers/base/pset_property.c | 110 +++++++++++++++++++++++++++++++----
 include/linux/property.h     |   4 ++
 2 files changed, 102 insertions(+), 12 deletions(-)
drivers/base/pset_property.c +98 -12
index 08ecc13080ae..63f2377aefe8 100644
@@ -18,6 +18,11 @@ struct property_set {
 	struct device *dev;
 	struct fwnode_handle fwnode;
 	const struct property_entry *properties;
+
+	struct property_set *parent;
+	/* Entry in parent->children list */
+	struct list_head child_node;
+	struct list_head children;
 };
 
 static const struct fwnode_operations pset_fwnode_ops;
@@ -283,10 +288,47 @@ pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
 					   val, nval);
 }
 
+struct fwnode_handle *pset_fwnode_get_parent(const struct fwnode_handle *fwnode)
+{
+	struct property_set *pset = to_pset_node(fwnode);
+
+	return pset ? &pset->parent->fwnode : NULL;
+}
+
+struct fwnode_handle *
+pset_fwnode_get_next_subnode(const struct fwnode_handle *fwnode,
+			     struct fwnode_handle *child)
+{
+	const struct property_set *pset = to_pset_node(fwnode);
+	struct property_set *first_child;
+	struct property_set *next;
+
+	if (!pset)
+		return NULL;
+
+	if (list_empty(&pset->children))
+		return NULL;
+
+	first_child = list_first_entry(&pset->children, struct property_set,
+				       child_node);
+
+	if (child) {
+		next = list_next_entry(to_pset_node(child), child_node);
+		if (next == first_child)
+			return NULL;
+	} else {
+		next = first_child;
+	}
+
+	return &next->fwnode;
+}
+
 static const struct fwnode_operations pset_fwnode_ops = {
 	.property_present = pset_fwnode_property_present,
 	.property_read_int_array = pset_fwnode_read_int_array,
 	.property_read_string_array = pset_fwnode_property_read_string_array,
+	.get_parent = pset_fwnode_get_parent,
+	.get_next_child_node = pset_fwnode_get_next_subnode,
 };
 
 static void property_entry_free_data(const struct property_entry *p)
@@ -439,24 +481,31 @@ EXPORT_SYMBOL_GPL(property_entries_free);
  */
 static void pset_free_set(struct property_set *pset)
 {
+	struct property_set *child, *next;
+
 	if (!pset)
 		return;
 
+	list_for_each_entry_safe(child, next, &pset->children, child_node) {
+		list_del(&child->child_node);
+		pset_free_set(child);
+	}
+
 	property_entries_free(pset->properties);
 	kfree(pset);
 }
 
 /**
- * pset_copy_set - copies property set
- * @pset: Property set to copy
+ * pset_create_set - creates property set.
+ * @src: property entries for the set.
  *
- * This function takes a deep copy of the given property set and returns
- * pointer to the copy. Call device_free_property_set() to free resources
- * allocated in this function.
+ * This function takes a deep copy of the given property entries and creates
+ * property set. Call pset_free_set() to free resources allocated in this
+ * function.
  *
  * Return: Pointer to the new property set or error pointer.
  */
-static struct property_set *pset_copy_set(const struct property_set *pset)
+static struct property_set *pset_create_set(const struct property_entry *src)
 {
 	struct property_entry *properties;
 	struct property_set *p;
@@ -465,7 +514,11 @@ static struct property_set *pset_copy_set(const struct property_set *pset)
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
-	properties = property_entries_dup(pset->properties);
+	INIT_LIST_HEAD(&p->child_node);
+	INIT_LIST_HEAD(&p->children);
+	p->fwnode.ops = &pset_fwnode_ops;
+
+	properties = property_entries_dup(src);
 	if (IS_ERR(properties)) {
 		kfree(p);
 		return ERR_CAST(properties);
@@ -521,20 +574,53 @@ EXPORT_SYMBOL_GPL(device_remove_properties);
 int device_add_properties(struct device *dev,
 			  const struct property_entry *properties)
 {
-	struct property_set *p, pset;
+	struct property_set *p;
 
 	if (!properties)
 		return -EINVAL;
 
-	pset.properties = properties;
-
-	p = pset_copy_set(&pset);
+	p = pset_create_set(properties);
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 
-	p->fwnode.ops = &pset_fwnode_ops;
 	set_secondary_fwnode(dev, &p->fwnode);
 	p->dev = dev;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(device_add_properties);
+
+/**
+ * device_add_child_properties - Add a collection of properties to a device object.
+ * @dev: Device to add properties to.
+ * @properties: Collection of properties to add.
+ *
+ * Associate a collection of device properties represented by @properties as a
+ * child of given @parent firmware node.  The function takes a copy of
+ * @properties.
+ */
+struct fwnode_handle *
+device_add_child_properties(struct device *dev,
+			    struct fwnode_handle *parent,
+			    const struct property_entry *properties)
+{
+	struct property_set *p;
+	struct property_set *parent_pset;
+
+	if (!properties)
+		return ERR_PTR(-EINVAL);
+
+	parent_pset = to_pset_node(parent);
+	if (!parent_pset)
+		return ERR_PTR(-EINVAL);
+
+	p = pset_create_set(properties);
+	if (IS_ERR(p))
+		return ERR_CAST(p);
+
+	p->dev = dev;
+	p->parent = parent_pset;
+	list_add_tail(&p->child_node, &parent_pset->children);
+
+	return &p->fwnode;
+}
+EXPORT_SYMBOL_GPL(device_add_child_properties);
include/linux/property.h +4 -0
index ac8a1ebc4c1b..bb1cf4f30770 100644
@@ -275,6 +275,10 @@ void property_entries_free(const struct property_entry *properties);
 
 int device_add_properties(struct device *dev,
 			  const struct property_entry *properties);
+struct fwnode_handle *
+device_add_child_properties(struct device *dev,
+			    struct fwnode_handle *parent,
+			    const struct property_entry *properties);
 void device_remove_properties(struct device *dev);
 
 bool device_dma_supported(struct device *dev);
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#3 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Hi,

Quoted 28 lines · Dmitry Torokhov

Several drivers rely on having notion of sub-nodes when describing hardware, let's allow static board-defined properties also have it.

The board files will then attach properties to devices in the following fashion:

	device_add_properties(&board_platform_device.dev,
			      main_device_props);
	device_add_child_properties(&board_platform_device.dev,
				    dev_fwnode(&board_platform_device.dev),
				    child1_device_props);
	device_add_child_properties(&board_platform_device.dev,
				    dev_fwnode(&board_platform_device.dev),
				    child2_device_props);
	...
	platform_device_register(&board_platform_device.dev);
---
 drivers/base/pset_property.c | 110 +++++++++++++++++++++++++++++++----
 include/linux/property.h     |   4 ++
 2 files changed, 102 insertions(+), 12 deletions(-)
drivers/base/pset_property.c +0 -0
index 08ecc13080ae..63f2377aefe8 100644

drivers/base/pset_property.c struct property_set { · line 25

Quoted 8 lines
drivers/base/pset_property.c +5 -0
@@ -18,6 +18,11 @@ struct property_set {
 	struct device *dev;
 	struct fwnode_handle fwnode;
 	const struct property_entry *properties;
+
+	struct property_set *parent;
+	/* Entry in parent->children list */
+	struct list_head child_node;
+	struct list_head children;

Add

        const char *name;

and you can implement also pset_get_named_child_node().

Quoted 119 lines
drivers/base/pset_property.c +57 -11
 };
 
 static const struct fwnode_operations pset_fwnode_ops;
@@ -283,10 +288,47 @@ pset_fwnode_property_read_string_array(const struct fwnode_handle *fwnode,
 					   val, nval);
 }
 
+struct fwnode_handle *pset_fwnode_get_parent(const struct fwnode_handle *fwnode)
+{
+	struct property_set *pset = to_pset_node(fwnode);
+
+	return pset ? &pset->parent->fwnode : NULL;
+}
+
+struct fwnode_handle *
+pset_fwnode_get_next_subnode(const struct fwnode_handle *fwnode,
+			     struct fwnode_handle *child)
+{
+	const struct property_set *pset = to_pset_node(fwnode);
+	struct property_set *first_child;
+	struct property_set *next;
+
+	if (!pset)
+		return NULL;
+
+	if (list_empty(&pset->children))
+		return NULL;
+
+	first_child = list_first_entry(&pset->children, struct property_set,
+				       child_node);
+
+	if (child) {
+		next = list_next_entry(to_pset_node(child), child_node);
+		if (next == first_child)
+			return NULL;
+	} else {
+		next = first_child;
+	}
+
+	return &next->fwnode;
+}
+
 static const struct fwnode_operations pset_fwnode_ops = {
 	.property_present = pset_fwnode_property_present,
 	.property_read_int_array = pset_fwnode_read_int_array,
 	.property_read_string_array = pset_fwnode_property_read_string_array,
+	.get_parent = pset_fwnode_get_parent,
+	.get_next_child_node = pset_fwnode_get_next_subnode,
 };
 
 static void property_entry_free_data(const struct property_entry *p)
@@ -439,24 +481,31 @@ EXPORT_SYMBOL_GPL(property_entries_free);
  */
 static void pset_free_set(struct property_set *pset)
 {
+	struct property_set *child, *next;
+
 	if (!pset)
 		return;
 
+	list_for_each_entry_safe(child, next, &pset->children, child_node) {
+		list_del(&child->child_node);
+		pset_free_set(child);
+	}
+
 	property_entries_free(pset->properties);
 	kfree(pset);
 }
 
 /**
- * pset_copy_set - copies property set
- * @pset: Property set to copy
+ * pset_create_set - creates property set.
+ * @src: property entries for the set.
  *
- * This function takes a deep copy of the given property set and returns
- * pointer to the copy. Call device_free_property_set() to free resources
- * allocated in this function.
+ * This function takes a deep copy of the given property entries and creates
+ * property set. Call pset_free_set() to free resources allocated in this
+ * function.
  *
  * Return: Pointer to the new property set or error pointer.
  */
-static struct property_set *pset_copy_set(const struct property_set *pset)
+static struct property_set *pset_create_set(const struct property_entry *src)
 {
 	struct property_entry *properties;
 	struct property_set *p;
@@ -465,7 +514,11 @@ static struct property_set *pset_copy_set(const struct property_set *pset)
 	if (!p)
 		return ERR_PTR(-ENOMEM);
 
-	properties = property_entries_dup(pset->properties);
+	INIT_LIST_HEAD(&p->child_node);
+	INIT_LIST_HEAD(&p->children);
+	p->fwnode.ops = &pset_fwnode_ops;
+
+	properties = property_entries_dup(src);
 	if (IS_ERR(properties)) {
 		kfree(p);
 		return ERR_CAST(properties);
@@ -521,20 +574,53 @@ EXPORT_SYMBOL_GPL(device_remove_properties);
 int device_add_properties(struct device *dev,
 			  const struct property_entry *properties)
 {
-	struct property_set *p, pset;
+	struct property_set *p;
 
 	if (!properties)
 		return -EINVAL;
 
-	pset.properties = properties;
-
-	p = pset_copy_set(&pset);
+	p = pset_create_set(properties);
 	if (IS_ERR(p))
 		return PTR_ERR(p);
 

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 594

Quoted 10 lines
drivers/base/pset_property.c +4 -1
-	p->fwnode.ops = &pset_fwnode_ops;
 	set_secondary_fwnode(dev, &p->fwnode);
 	p->dev = dev;
 	return 0;
 }
 EXPORT_SYMBOL_GPL(device_add_properties);
+
+/**
+ * device_add_child_properties - Add a collection of properties to a device object.
+ * @dev: Device to add properties to.

@parent: missing

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 602

Quoted 8 lines
drivers/base/pset_property.c +8 -0
+ * @properties: Collection of properties to add.
+ *
+ * Associate a collection of device properties represented by @properties as a
+ * child of given @parent firmware node.  The function takes a copy of
+ * @properties.
+ */
+struct fwnode_handle *
+device_add_child_properties(struct device *dev,

device_add_child_properties(struct device *dev, const char *child_name,

Quoted 8 lines
drivers/base/pset_property.c +8 -0
+			    struct fwnode_handle *parent,
+			    const struct property_entry *properties)
+{
+	struct property_set *p;
+	struct property_set *parent_pset;
+
+	if (!properties)
+		return ERR_PTR(-EINVAL);

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 620

Quoted 10 lines
drivers/base/pset_property.c +10 -0
+
+	parent_pset = to_pset_node(parent);
+	if (!parent_pset)
+		return ERR_PTR(-EINVAL);
+
+	p = pset_create_set(properties);
+	if (IS_ERR(p))
+		return ERR_CAST(p);
+
+	p->dev = dev;
        p->name = kstrdup_const(child_name, GFP_KERNEL);

You'll need to kfree_const(pset->name) in pset_free_set() of course.

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 626

Quoted 6 lines
drivers/base/pset_property.c +6 -0
+	p->parent = parent_pset;
+	list_add_tail(&p->child_node, &parent_pset->children);
+
+	return &p->fwnode;
+}
+EXPORT_SYMBOL_GPL(device_add_child_properties);

Br,

Signature · 1 line
heikki
#4 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Hi Heikki,

Quoted 42 lines · Heikki Krogerus

Hi,

Quoted 28 lines · Dmitry Torokhov

Several drivers rely on having notion of sub-nodes when describing hardware, let's allow static board-defined properties also have it.

The board files will then attach properties to devices in the following fashion:

	device_add_properties(&board_platform_device.dev,
			      main_device_props);
	device_add_child_properties(&board_platform_device.dev,
				    dev_fwnode(&board_platform_device.dev),
				    child1_device_props);
	device_add_child_properties(&board_platform_device.dev,
				    dev_fwnode(&board_platform_device.dev),
				    child2_device_props);
	...
	platform_device_register(&board_platform_device.dev);
---
 drivers/base/pset_property.c | 110 +++++++++++++++++++++++++++++++----
 include/linux/property.h     |   4 ++
 2 files changed, 102 insertions(+), 12 deletions(-)
drivers/base/pset_property.c +0 -0
index 08ecc13080ae..63f2377aefe8 100644

drivers/base/pset_property.c struct property_set { · line 25

Quoted 8 lines
drivers/base/pset_property.c +5 -0
@@ -18,6 +18,11 @@ struct property_set {
 	struct device *dev;
 	struct fwnode_handle fwnode;
 	const struct property_entry *properties;
+
+	struct property_set *parent;
+	/* Entry in parent->children list */
+	struct list_head child_node;
+	struct list_head children;

Add

Quoted 3 lines
        const char *name;

and you can implement also pset_get_named_child_node().

Or

	char name[];

to avoid separate allocation.

Alternatively, we can add it later when we need it, and add device_add_named_child_properties().

I'll leave it up to Rafael to decide.

Thanks.

Signature · 1 line
Dmitry
#5 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Quoted 16 lines · Dmitry Torokhov
Quoted 16 lines

drivers/base/pset_property.c struct property_set { · line 25

Quoted 13 lines
drivers/base/pset_property.c +5 -0
index 08ecc13080ae..63f2377aefe8 100644
@@ -18,6 +18,11 @@ struct property_set {
 	struct device *dev;
 	struct fwnode_handle fwnode;
 	const struct property_entry *properties;
+
+	struct property_set *parent;
+	/* Entry in parent->children list */
+	struct list_head child_node;
+	struct list_head children;

Add

Quoted 8 lines
Quoted 3 lines
        const char *name;

and you can implement also pset_get_named_child_node().

Or

	char name[];

to avoid separate allocation.

Let's not do that, especially if you are planning on exporting this structure. If the name is coming from .rodata, there is no need to allocate anything for the name. Check kstrdup_const().

Quoted 4 lines

Alternatively, we can add it later when we need it, and add device_add_named_child_properties().

I'll leave it up to Rafael to decide.

Fair enough.

Thanks,

Signature · 1 line
heikki
#6 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Quoted 21 lines · Heikki Krogerus
Quoted 20 lines · Dmitry Torokhov
Quoted 19 lines

drivers/base/pset_property.c struct property_set { · line 25

Quoted 13 lines
drivers/base/pset_property.c +5 -0
index 08ecc13080ae..63f2377aefe8 100644
@@ -18,6 +18,11 @@ struct property_set {
 	struct device *dev;
 	struct fwnode_handle fwnode;
 	const struct property_entry *properties;
+
+	struct property_set *parent;
+	/* Entry in parent->children list */
+	struct list_head child_node;
+	struct list_head children;

Add

        const char *name;

and you can implement also pset_get_named_child_node().

Quoted 7 lines
Quoted 4 lines

Or

	char name[];

to avoid separate allocation.

Let's not do that, especially if you are planning on exporting this structure.

Can you please elaborate why? Not using pointer saves us 4/8 bytes + however much memory we need for bookkeeping for the extra chunk. Given that majority of pset nodes are unnamed this seems wasteful.

Quoted 2 lines

If the name is coming from .rodata, there is no need to allocate anything for the name. Check kstrdup_const().

The data is most likely coming as __initconst so we do need to copy it.

Quoted 4 lines
Quoted 4 lines

Alternatively, we can add it later when we need it, and add device_add_named_child_properties().

Quoted 9 lines
Quoted 1 line

I'll leave it up to Rafael to decide.

Fair enough.

Thanks,

Signature · 1 line
heikki

Thanks.

Signature · 1 line
Dmitry
#7 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Quoted 30 lines · Dmitry Torokhov
Quoted 21 lines · Heikki Krogerus
Quoted 20 lines · Dmitry Torokhov
Quoted 19 lines

drivers/base/pset_property.c struct property_set { · line 25

Quoted 13 lines
drivers/base/pset_property.c +5 -0
index 08ecc13080ae..63f2377aefe8 100644
@@ -18,6 +18,11 @@ struct property_set {
 	struct device *dev;
 	struct fwnode_handle fwnode;
 	const struct property_entry *properties;
+
+	struct property_set *parent;
+	/* Entry in parent->children list */
+	struct list_head child_node;
+	struct list_head children;

Add

        const char *name;

and you can implement also pset_get_named_child_node().

Quoted 7 lines
Quoted 4 lines

Or

	char name[];

to avoid separate allocation.

Let's not do that, especially if you are planning on exporting this structure.

Quoted 8 lines

Can you please elaborate why? Not using pointer saves us 4/8 bytes + however much memory we need for bookkeeping for the extra chunk. Given that majority of pset nodes are unnamed this seems wasteful.

Quoted 2 lines

If the name is coming from .rodata, there is no need to allocate anything for the name. Check kstrdup_const().

The data is most likely coming as __initconst so we do need to copy it.

OK, I did not consider that. Yes, it makes sense to always copy.

Thanks,

Signature · 1 line
heikki
#8 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

In reply to Dmitry Torokhov

Hi Dmitry,

Quoted 3 lines · Dmitry Torokhov

+/**
+ * device_add_child_properties - Add a collection of properties to a device object.
+ * @dev: Device to add properties to.

In case you didn't notice my comment for this, you are missing @parent here.

But why do you need both the parent and the dev?

Quoted 8 lines

+ * @properties: Collection of properties to add.
+ *
+ * Associate a collection of device properties represented by @properties as a
+ * child of given @parent firmware node. The function takes a copy of
+ * @properties.
+ */

drivers/base/pset_property.c +2 -0
+struct fwnode_handle *
+device_add_child_properties(struct device *dev,

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 612

Quoted 10 lines
drivers/base/pset_property.c +10 -0
+			    struct fwnode_handle *parent,
+			    const struct property_entry *properties)
+{
+	struct property_set *p;
+	struct property_set *parent_pset;
+
+	if (!properties)
+		return ERR_PTR(-EINVAL);
+
+	parent_pset = to_pset_node(parent);

For this function, the parent will in practice have to be dev_fwnode(dev), so I don't think you need @parent at all, no?

There is something wrong here..

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 620

Quoted 8 lines
drivers/base/pset_property.c +8 -0
+	if (!parent_pset)
+		return ERR_PTR(-EINVAL);
+
+	p = pset_create_set(properties);
+	if (IS_ERR(p))
+		return ERR_CAST(p);
+
+	p->dev = dev;

That looks wrong.

I'm guessing the assumption here is that the child nodes will never be assigned to their own devices, but you can't do that. It will limit the use of the child nodes to a very small number of cases, possibly only to gpios.

I think that has to be fixed. It should not be a big deal. Just expect the child nodes to be removed separately, and add ref counting to the struct property_set handling.

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 626

Quoted 6 lines
drivers/base/pset_property.c +6 -0
+	p->parent = parent_pset;
+	list_add_tail(&p->child_node, &parent_pset->children);
+
+	return &p->fwnode;
+}
+EXPORT_SYMBOL_GPL(device_add_child_properties);

The child nodes will change the purpose of the build-in property support. Originally the goal was just to support adding of build-in device properties to real firmware nodes, but things have changed quite a bit from that. These child nodes are purely tied to the build-in device property support, so we should be talking about adding pset type child nodes to pset type parent nodes in the API:
fwnode_pset_add_child_node(), or something like that.

I'm sorry to be a bit of pain in the butt with this. The build-in property support is a hack, it always was. A useful hack, but hack nevertheless. That means we need to be extra careful when expanding it, like here.

Thanks,

Signature · 1 line
heikki
#9 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Quoted 8 lines · Heikki Krogerus

The child nodes will change the purpose of the build-in property support. Originally the goal was just to support adding of build-in device properties to real firmware nodes, but things have changed quite a bit from that. These child nodes are purely tied to the build-in device property support, so we should be talking about adding pset type child nodes to pset type parent nodes in the API:
fwnode_pset_add_child_node(), or something like that.

Quoted 4 lines

I'm sorry to be a bit of pain in the butt with this. The build-in property support is a hack, it always was. A useful hack, but hack nevertheless. That means we need to be extra careful when expanding it, like here.

I dunno how we got to here, what I tried to solve and Dmitry tries to make more general is converting old boardfiles to use fwnode, because I initially tried to just support gpio descriptors from board files.

If boardfiles is what you mean with "build-in property support" I don't know, it predates both device tree and ACPI and any other hardware descriptions on the ARM architecture, from the perspective of these systems things are fine and the hardware description languages are the novelty...

But I guess you know because you worked with OMAP :)

So there is something I don't understand here.

Yours,
Linus Walleij

#10 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Hi Linus,

Quoted 11 lines · Linus Walleij
Quoted 8 lines · Heikki Krogerus

The child nodes will change the purpose of the build-in property support. Originally the goal was just to support adding of build-in device properties to real firmware nodes, but things have changed quite a bit from that. These child nodes are purely tied to the build-in device property support, so we should be talking about adding pset type child nodes to pset type parent nodes in the API:
fwnode_pset_add_child_node(), or something like that.

Quoted 8 lines
Quoted 4 lines

I'm sorry to be a bit of pain in the butt with this. The build-in property support is a hack, it always was. A useful hack, but hack nevertheless. That means we need to be extra careful when expanding it, like here.

I dunno how we got to here, what I tried to solve and Dmitry tries to make more general is converting old boardfiles to use fwnode, because I initially tried to just support gpio descriptors from board files.

I understand that, but what I'm trying to say is that the solution for the child nodes is not generic enough. I'll try to explain below.

Quoted 5 lines

If boardfiles is what you mean with "build-in property support" I don't know, it predates both device tree and ACPI and any other hardware descriptions on the ARM architecture, from the perspective of these systems things are fine and the hardware description languages are the novelty...

Rafael talked about "build-in properties" at one point, and I just started using that expression after that, but it's probable not the best term to describe this thing.

But please note that we are not talking about only static information with these property sets. They can be populated dynamically as well, so this kind of extensions must consider and support that. On top of board files we need to consider things like glue and probing drivers and even buses in some cases.

Quoted 3 lines

But I guess you know because you worked with OMAP :)

So there is something I don't understand here.

Sorry for the poor choice of words on my behalf. I guess I'm not explaining my point properly. Let me try again.

So I'm seeing a case that this solution does not seem to be able to support, but ultimately it will need to do so: with USB ports we are going to need to be able to assign a device to the child nodes that represent those ports.

To be honest, normally I would not care about that, and I would simply wait for this to go into mainline, and then propose a modification to it so it can also support that other case.

However, this time I feel that we should not do that. The solution for the child nodes should be implemented so that it can support all the known cases from the beginning. The reason for that is because I fear that we'll end up having a pile of ad-hoc style solutions, each solving an individual problem, and a whole lot of duplicated stuff for these property sets. In the end we will have spaghetti with meatballs that nobody is able fully understand nor handle. And I really see a strong possibility for that to happen here.

Thanks,

Signature · 1 line
heikki
#11 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

In reply to Heikki Krogerus

Hi Heikki,

Quoted 11 lines · Heikki Krogerus

Hi Dmitry,

Quoted 3 lines · Dmitry Torokhov

+/**
+ * device_add_child_properties - Add a collection of properties to a device object.
+ * @dev: Device to add properties to.

In case you didn't notice my comment for this, you are missing @parent here.

But why do you need both the parent and the dev?

I could go by parent only and fetch dev from parent.

Quoted 23 lines
Quoted 9 lines

+ * @properties: Collection of properties to add.
+ *
+ * Associate a collection of device properties represented by @properties as a
+ * child of given @parent firmware node. The function takes a copy of
+ * @properties.
+ */

drivers/base/pset_property.c +2 -0
+struct fwnode_handle *
+device_add_child_properties(struct device *dev,

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 612

Quoted 10 lines
drivers/base/pset_property.c +10 -0
+			    struct fwnode_handle *parent,
+			    const struct property_entry *properties)
+{
+	struct property_set *p;
+	struct property_set *parent_pset;
+
+	if (!properties)
+		return ERR_PTR(-EINVAL);
+
+	parent_pset = to_pset_node(parent);

For this function, the parent will in practice have to be dev_fwnode(dev), so I don't think you need @parent at all, no?

Quoted 1 line

There is something wrong here..

Yes, I expect majority of the calls will use dev_fwnode(dev) as parent, but nobody stops you from doing:

	device_add_properties(dev, props);
	c1 = device_add_child_properties(dev, dev_fwnode(dev), cp1);
	c2 = device_add_child_properties(dev, c1, cp2);
	c3 = device_add_child_properties(dev, c2, cp3);
	...
Quoted 12 lines

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 620

Quoted 9 lines
drivers/base/pset_property.c +8 -0
+	if (!parent_pset)
+		return ERR_PTR(-EINVAL);
+
+	p = pset_create_set(properties);
+	if (IS_ERR(p))
+		return ERR_CAST(p);
+
+	p->dev = dev;

That looks wrong.

Quoted 4 lines

I'm guessing the assumption here is that the child nodes will never be assigned to their own devices, but you can't do that. It will limit the use of the child nodes to a very small number of cases, possibly only to gpios.

If I need to assign a node to a device I'll use device_add_properties() API. device_add_child_properties() is for nodes living "below" the device.

All nodes (the primary/secondary and children) would point to the owning device, just for convenience.

Quoted 4 lines

I think that has to be fixed. It should not be a big deal. Just expect the child nodes to be removed separately, and add ref counting to the struct property_set handling.

Why do we need to remove them separately and what do we need refcounting for?

Quoted 15 lines

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 626

Quoted 7 lines
drivers/base/pset_property.c +6 -0
+	p->parent = parent_pset;
+	list_add_tail(&p->child_node, &parent_pset->children);
+
+	return &p->fwnode;
+}
+EXPORT_SYMBOL_GPL(device_add_child_properties);

The child nodes will change the purpose of the build-in property support. Originally the goal was just to support adding of build-in device properties to real firmware nodes, but things have changed quite a bit from that. These child nodes are purely tied to the build-in device property support, so we should be talking about adding pset type child nodes to pset type parent nodes in the API:
fwnode_pset_add_child_node(), or something like that.

OK, I can change device_add_child_properties() to fwnode_pset_add_child_node() if Rafael would prefer this name.

Thanks.

Signature · 1 line
Dmitry
#12 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Hi Dmitry,

Quoted 11 lines · Dmitry Torokhov
Quoted 11 lines

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 620

Quoted 8 lines
drivers/base/pset_property.c +8 -0
+	if (!parent_pset)
+		return ERR_PTR(-EINVAL);
+
+	p = pset_create_set(properties);
+	if (IS_ERR(p))
+		return ERR_CAST(p);
+
+	p->dev = dev;

That looks wrong.

Quoted 8 lines
Quoted 4 lines

I'm guessing the assumption here is that the child nodes will never be assigned to their own devices, but you can't do that. It will limit the use of the child nodes to a very small number of cases, possibly only to gpios.

If I need to assign a node to a device I'll use device_add_properties() API. device_add_child_properties() is for nodes living "below" the device.

device_add_properties() is not available to us before we have the actual struct device meant for the properties. If the child device is populated outside of the "boardfiles" then we have to be able to link it to the child node afterwards.

But I took a closer look at the code and realized that you are not using that p->dev with the child nodes for anything, so I may be wrong about this. You are not actually linking the child nodes to that device here.

Quoted 2 lines

All nodes (the primary/secondary and children) would point to the owning device, just for convenience.

Since you don't use that for anything, then drop that line. It's only confusing.

And besides, since we will have separate child devices for those child nodes, there is a small change that somebody needs to call device_remove_properties(child_dev). At least then that operation becomes safe, as it's a nop with the child pset nodes.

Quoted 6 lines
Quoted 3 lines

I think that has to be fixed. It should not be a big deal. Just expect the child nodes to be removed separately, and add ref counting to the struct property_set handling.

Why do we need to remove them separately and what do we need refcounting for?

If you could remove the nodes independently, then obviously the parent can not be removed before the children. The ref count would be there to prevent that.

Though my original comment is not valid, I still think that the child nodes should be created and destroyed separately. Actually, I don't think we should be talking about child nodes in the API at all. Check below..

Quoted 15 lines
Quoted 7 lines

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 626

Quoted 6 lines
drivers/base/pset_property.c +6 -0
+	p->parent = parent_pset;
+	list_add_tail(&p->child_node, &parent_pset->children);
+
+	return &p->fwnode;
+}
+EXPORT_SYMBOL_GPL(device_add_child_properties);
Quoted 7 lines

The child nodes will change the purpose of the build-in property support. Originally the goal was just to support adding of build-in device properties to real firmware nodes, but things have changed quite a bit from that. These child nodes are purely tied to the build-in device property support, so we should be talking about adding pset type child nodes to pset type parent nodes in the API:
fwnode_pset_add_child_node(), or something like that.

Quoted 2 lines

OK, I can change device_add_child_properties() to fwnode_pset_add_child_node() if Rafael would prefer this name.

So not even that. We should have API like this:

struct fwnode_handle *
fwnode_pset_create(struct fwnode_handle *parent, const char *name,

                   const struct property_entry *props);

void fwnode_pset_destroy(struct fwnode_handle *fwnode);

int device_add_properties(struct device *dev,

                          const struct fwnode_handle *pset_node);

void device_remove_properties(struct device *dev);

So basically we separate creation of the nodes from linking them to the devices completely. That would give this API much better structure. It would scale better, for example, it would then be possible to add child nodes from different locations if needed.

Thanks,

Signature · 1 line
heikki
#13 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Quoted 19 lines · Heikki Krogerus

Hi Dmitry,

Quoted 16 lines · Dmitry Torokhov
Quoted 15 lines

drivers/base/pset_property.c EXPORT_SYMBOL_GPL(device_remove_properties); · line 620

Quoted 8 lines
drivers/base/pset_property.c +8 -0
+	if (!parent_pset)
+		return ERR_PTR(-EINVAL);
+
+	p = pset_create_set(properties);
+	if (IS_ERR(p))
+		return ERR_CAST(p);
+
+	p->dev = dev;

That looks wrong.

I'm guessing the assumption here is that the child nodes will never be assigned to their own devices, but you can't do that. It will limit the use of the child nodes to a very small number of cases, possibly only to gpios.

Quoted 8 lines
Quoted 3 lines

If I need to assign a node to a device I'll use device_add_properties() API. device_add_child_properties() is for nodes living "below" the device.

device_add_properties() is not available to us before we have the actual struct device meant for the properties. If the child device is populated outside of the "boardfiles" then we have to be able to link it to the child node afterwards.

I think we are talking about totally different use cases and that is why we are having hard time coming to a mutually agreeable solution. Could you please describe in more detail what you would like to achieve, and preferably show how it is described now with DT and/or ACPI, so that I have a better frame of reference.

Thanks.

Signature · 1 line
Dmitry
#14 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Quoted 5 lines · Dmitry Torokhov

I think we are talking about totally different use cases and that is why we are having hard time coming to a mutually agreeable solution. Could you please describe in more detail what you would like to achieve, and preferably show how it is described now with DT and/or ACPI, so that I have a better frame of reference.

Yes, of course. Sorry.

USB ports are devices that usually the USB controller drivers register (or actually the USB core code). They are represented in both ACPI and DT as child nodes of the controller device node. The USB connector OF node is defined in file Documentation/devicetree/bindings/connector/usb-connector.txt

In short, the controller drivers will request handle to a child node that represents a port, and only after that register the actual port device.

The drivers I'm looking at currently are the USB Type-C port controller drivers and the port manager (in Greg's usb-next or linux-next):

drivers/usb/typec/tcpm/tcpci.c
drivers/usb/typec/tcpm/fusb302.c
drivers/usb/typec/tcpm/tcpm.c

The goal is simply to get rid of the platform data as usual, and ideally so that we don't need any extra code in order to support the "legacy" platforms.

Thanks,

Signature · 1 line
heikki
#15 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Hi Heikki,

Quoted 23 lines · Heikki Krogerus
Quoted 5 lines · Dmitry Torokhov

I think we are talking about totally different use cases and that is why we are having hard time coming to a mutually agreeable solution. Could you please describe in more detail what you would like to achieve, and preferably show how it is described now with DT and/or ACPI, so that I have a better frame of reference.

Yes, of course. Sorry.

USB ports are devices that usually the USB controller drivers register (or actually the USB core code). They are represented in both ACPI and DT as child nodes of the controller device node. The USB connector OF node is defined in file Documentation/devicetree/bindings/connector/usb-connector.txt

In short, the controller drivers will request handle to a child node that represents a port, and only after that register the actual port device.

The drivers I'm looking at currently are the USB Type-C port controller drivers and the port manager (in Greg's usb-next or linux-next):

Quoted 7 lines

drivers/usb/typec/tcpm/tcpci.c
drivers/usb/typec/tcpm/fusb302.c
drivers/usb/typec/tcpm/tcpm.c

The goal is simply to get rid of the platform data as usual, and ideally so that we don't need any extra code in order to support the "legacy" platforms.

Are these actually used on any of the "legacy" platforms? I fetched linux-next today, but I do not actually see anything in drivers/usb/typec touching platform data...

In the context of the connector, even before we descend to child nodes details, how do you propose implementing references between fwnodes? Especially since the other node (in case you complementing existing topology) may be ACPI or DT instance?

I want to say that "You aren't gonna need it" for what you are asking here and we can actually split it apart if and when we actually want to separate creation of pset-backed device nodes and instantiating corresponding device structures.

Thanks.

Signature · 1 line
Dmitry
#16 lore raw

Re: [RFC/PATCH 2/5] device property: introduce notion of subnodes for legacy boards

Hi Dmitry,

Sorry for the late reply. I decided to give this a try myself, and I've now prepared something (not for the gpio stuff!). I'll do a quick internal review round, and send it to you guys as RFC after that..

Quoted 30 lines · Dmitry Torokhov

Hi Heikki,

Quoted 27 lines · Heikki Krogerus
Quoted 5 lines · Dmitry Torokhov

I think we are talking about totally different use cases and that is why we are having hard time coming to a mutually agreeable solution. Could you please describe in more detail what you would like to achieve, and preferably show how it is described now with DT and/or ACPI, so that I have a better frame of reference.

Yes, of course. Sorry.

USB ports are devices that usually the USB controller drivers register (or actually the USB core code). They are represented in both ACPI and DT as child nodes of the controller device node. The USB connector OF node is defined in file Documentation/devicetree/bindings/connector/usb-connector.txt

In short, the controller drivers will request handle to a child node that represents a port, and only after that register the actual port device.

The drivers I'm looking at currently are the USB Type-C port controller drivers and the port manager (in Greg's usb-next or linux-next):

drivers/usb/typec/tcpm/tcpci.c
drivers/usb/typec/tcpm/fusb302.c
drivers/usb/typec/tcpm/tcpm.c

Quoted 7 lines
Quoted 3 lines

The goal is simply to get rid of the platform data as usual, and ideally so that we don't need any extra code in order to support the "legacy" platforms.

Are these actually used on any of the "legacy" platforms? I fetched linux-next today, but I do not actually see anything in drivers/usb/typec touching platform data...

It's not touching any platform data, and I would like to keep it that way :-). The device for fusb302 is created for example in drivers/platform/x86/intel_cht_int33fe.c.

The fusb302.c driver already tries to find that child node named "connector" (and so do the other port drivers). I'm trying to supply the driver that node here somehow so I can avoid quirks.

Quoted 4 lines

In the context of the connector, even before we descend to child nodes details, how do you propose implementing references between fwnodes? Especially since the other node (in case you complementing existing topology) may be ACPI or DT instance?

The different fwnode types (ACPI, OF, etc.) must live in parallel, i.e. you can not have something like ACPI parent node with property_set children if that's what you mean. The "secondary" member will link the different types of fwnodes together, but they will live their separate lives. I'm not planning on changing that.

I'm just looking for a smooth way of describing the devices in software when the firmware is not supplying the hardware description, just like you. What I really need is separation of the fwnode registration from device registration in those cases. For that I now have a proposal, and I believe my proposal will work for you as a baseline as well.

Thanks,

Signature · 1 line
heikki
#17 lore raw

PATCH 3/5 device property: export property_set structure

In reply to Dmitry Torokhov

Future gpiolib will need to handle property sets properly, and this we need to export property_set, is_pset_node() and to_pset_node().

---
 drivers/base/pset_property.c | 31 ++-----------------------------
 include/linux/property.h     | 29 +++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 29 deletions(-)
drivers/base/pset_property.c +2 -29
index 63f2377aefe8..7118528816a4 100644
@@ -14,34 +14,6 @@
 #include <linux/property.h>
 #include <linux/slab.h>
 
-struct property_set {
-	struct device *dev;
-	struct fwnode_handle fwnode;
-	const struct property_entry *properties;
-
-	struct property_set *parent;
-	/* Entry in parent->children list */
-	struct list_head child_node;
-	struct list_head children;
-};
-
-static const struct fwnode_operations pset_fwnode_ops;
-
-static inline bool is_pset_node(const struct fwnode_handle *fwnode)
-{
-	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
-}
-
-#define to_pset_node(__fwnode)						\
-	({								\
-		typeof(__fwnode) __to_pset_node_fwnode = __fwnode;	\
-									\
-		is_pset_node(__to_pset_node_fwnode) ?			\
-			container_of(__to_pset_node_fwnode,		\
-				     struct property_set, fwnode) :	\
-			NULL;						\
-	})
-
 static const struct property_entry *
 pset_prop_get(const struct property_set *pset, const char *name)
 {
@@ -323,13 +295,14 @@ pset_fwnode_get_next_subnode(const struct fwnode_handle *fwnode,
 	return &next->fwnode;
 }
 
-static const struct fwnode_operations pset_fwnode_ops = {
+const struct fwnode_operations pset_fwnode_ops = {
 	.property_present = pset_fwnode_property_present,
 	.property_read_int_array = pset_fwnode_read_int_array,
 	.property_read_string_array = pset_fwnode_property_read_string_array,
 	.get_parent = pset_fwnode_get_parent,
 	.get_next_child_node = pset_fwnode_get_next_subnode,
 };
+EXPORT_SYMBOL_GPL(pset_fwnode_ops);
 
 static void property_entry_free_data(const struct property_entry *p)
 {
include/linux/property.h +29 -0
index bb1cf4f30770..957f75d10cf9 100644
@@ -13,6 +13,7 @@
 #ifndef _LINUX_PROPERTY_H_
 #define _LINUX_PROPERTY_H_
 
+#include <linux/err.h>
 #include <linux/fwnode.h>
 #include <linux/types.h>
 
@@ -281,6 +282,34 @@ device_add_child_properties(struct device *dev,
 			    const struct property_entry *properties);
 void device_remove_properties(struct device *dev);
 
+struct property_set {
+	struct device *dev;
+	struct fwnode_handle fwnode;
+	const struct property_entry *properties;
+
+	struct property_set *parent;
+	/* Entry in parent->children list */
+	struct list_head child_node;
+	struct list_head children;
+};
+
+extern const struct fwnode_operations pset_fwnode_ops;
+
+static inline bool is_pset_node(const struct fwnode_handle *fwnode)
+{
+	return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &pset_fwnode_ops;
+}
+
+#define to_pset_node(__fwnode)						\
+	({								\
+		typeof(__fwnode) __to_pset_node_fwnode = __fwnode;	\
+									\
+		is_pset_node(__to_pset_node_fwnode) ?			\
+			container_of(__to_pset_node_fwnode,		\
+				     struct property_set, fwnode) :	\
+			NULL;						\
+	})
+
 bool device_dma_supported(struct device *dev);
 
 enum dev_dma_attr device_get_dma_attr(struct device *dev);
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#18 lore raw

PATCH 4/5 gpiolib: add support for fetching descriptors from static properties

In reply to Dmitry Torokhov

Now that static device properties understand notion of child nodes, let's teach gpiolib to tie such children and machine GPIO descriptor tables. We will continue using a single table for entire device, but instead of using connection ID as a lookup key in the GPIO descriptor table directly, we will perform additional translation: fwnode_get_named_gpiod() when dealing with property_set-backed fwnodes will try parsing string property with name matching connection ID and use result of the lookup as the key in the table:

static const struct property_entry dev_child1_props[] __initconst = {
	...
	PROPERTY_ENTRY_STRING("gpios",		"child-1-gpios"),
	{ }
};
static struct gpiod_lookup_table dev_gpiod_table = {
	.dev_id = "some-device",
	.table = {
		...
		GPIO_LOOKUP_IDX("B", 1, "child-1-gpios", 1, GPIO_ACTIVE_LOW),
		...
	},
};
---
 drivers/gpio/gpiolib.c | 109 +++++++++++++++++++++++++++++------------
 1 file changed, 79 insertions(+), 30 deletions(-)
drivers/gpio/gpiolib.c +79 -30
index e11a3bb03820..f0e51d34a1c1 100644
@@ -3959,39 +3959,44 @@ struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
 }
 EXPORT_SYMBOL(gpiod_get_from_of_node);
 
-/**
- * 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
- * @index:	index of the GPIO to obtain for the consumer
- * @dflags:	GPIO initialization flags
- * @label:	label to attach to the requested GPIO
- *
- * This function can be used for drivers that get their configuration
- * from opaque firmware.
- *
- * The function properly finds the corresponding GPIO using whatever is the
- * underlying firmware interface and then makes sure that the GPIO
- * descriptor is requested before it is returned to the caller.
- *
- * Returns:
- * On successful 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, int index,
-					 enum gpiod_flags dflags,
-					 const char *label)
+static struct gpio_desc *pset_node_get_gpiod(struct fwnode_handle *fwnode,
+					     const char *propname, int index,
+					     enum gpio_lookup_flags *flags)
 {
-	struct gpio_desc *desc = ERR_PTR(-ENODEV);
-	unsigned long lflags = 0;
-	int ret;
+	struct property_set *pset;
+	const char *con_id;
 
-	if (!fwnode)
+	pset = to_pset_node(fwnode);
+	if (!pset)
 		return ERR_PTR(-EINVAL);
 
+	if (fwnode_property_read_string(fwnode, propname, &con_id)) {
+		/*
+		 * We could not find string mapping property name to
+		 * entry in gpio lookup table. Let's see if we are
+		 * dealing with firmware node corresponding to the
+		 * device (and not a child node): for such nodes we can
+		 * try doing lookup directly with property name.
+		 */
+		if (pset->parent)
+			return ERR_PTR(-ENOENT);
+
+		con_id = propname;
+	}
+
+	return gpiod_find(pset->dev, con_id, index, flags);
+}
+
+static struct gpio_desc *__fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
+						  const char *propname,
+						  int index,
+						  enum gpiod_flags dflags,
+						  const char *label)
+{
+	struct gpio_desc *desc = ERR_PTR(-ENODEV);
+	enum gpio_lookup_flags lflags = 0;
+	int ret;
+
 	if (is_of_node(fwnode)) {
 		desc = gpiod_get_from_of_node(to_of_node(fwnode),
 					      propname, index,
@@ -4009,9 +4014,12 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
 
 		if (info.polarity == GPIO_ACTIVE_LOW)
 			lflags |= GPIO_ACTIVE_LOW;
+	} else if (is_pset_node(fwnode)) {
+		desc = pset_node_get_gpiod(fwnode, propname, index, &lflags);
+		if (IS_ERR(desc))
+			return desc;
 	}
 
-	/* Currently only ACPI takes this path */
 	ret = gpiod_request(desc, label);
 	if (ret)
 		return ERR_PTR(ret);
@@ -4024,6 +4032,47 @@ struct gpio_desc *fwnode_get_named_gpiod(struct fwnode_handle *fwnode,
 
 	return desc;
 }
+
+/**
+ * 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
+ * @index:	index of the GPIO to obtain for the consumer
+ * @dflags:	GPIO initialization flags
+ * @label:	label to attach to the requested GPIO
+ *
+ * This function can be used for drivers that get their configuration
+ * from opaque firmware.
+ *
+ * The function properly finds the corresponding GPIO using whatever is the
+ * underlying firmware interface and then makes sure that the GPIO
+ * descriptor is requested before it is returned to the caller.
+ *
+ * Returns:
+ * On successful 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, int index,
+					 enum gpiod_flags dflags,
+					 const char *label)
+{
+	struct gpio_desc *desc;
+
+	if (!fwnode)
+		return ERR_PTR(-EINVAL);
+
+	desc = __fwnode_get_named_gpiod(fwnode, propname, index, dflags, label);
+	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT &&
+	    !IS_ERR_OR_NULL(fwnode->secondary)) {
+		desc = __fwnode_get_named_gpiod(fwnode->secondary,
+						propname, index, dflags, label);
+	}
+
+	return desc;
+}
 EXPORT_SYMBOL_GPL(fwnode_get_named_gpiod);
 
 /**
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#19 lore raw

Re: [RFC/PATCH 4/5] gpiolib: add support for fetching descriptors from static properties

Hi,

Quoted 15 lines · Dmitry Torokhov

Now that static device properties understand notion of child nodes, let's teach gpiolib to tie such children and machine GPIO descriptor tables. We will continue using a single table for entire device, but instead of using connection ID as a lookup key in the GPIO descriptor table directly, we will perform additional translation: fwnode_get_named_gpiod() when dealing with property_set-backed fwnodes will try parsing string property with name matching connection ID and use result of the lookup as the key in the table:

static const struct property_entry dev_child1_props[] __initconst = {
	...
	PROPERTY_ENTRY_STRING("gpios",		"child-1-gpios"),
	{ }
};
Quoted 8 lines
static struct gpiod_lookup_table dev_gpiod_table = {
	.dev_id = "some-device",
	.table = {
		...
		GPIO_LOOKUP_IDX("B", 1, "child-1-gpios", 1, GPIO_ACTIVE_LOW),
		...
	},
};

I wonder if instead of passing and parsing strings (and hoping there are no typos) we could get the compiler to help us bit more?

Something like below:

static const struct property_entry dev_child1_props[] __initconst = {
...
PROPERTY_ENTRY_STRING("gpios","child-1-gpios"),
{ }
};

static struct gpiod_lookup_table dev_gpiod_table = {
.dev_id = "some-device",
.table = {
...
GPIO_LOOKUP_IDX("B", 1, dev_child1_props, SIZEOF(dev_child1_props),

				1, GPIO_ACTIVE_LOW),

...
},
};

#20 lore raw

Re: [RFC/PATCH 4/5] gpiolib: add support for fetching descriptors from static properties

Hi Mika,

Quoted 44 lines · Mika Westerberg

Hi,

Quoted 15 lines · Dmitry Torokhov

Now that static device properties understand notion of child nodes, let's teach gpiolib to tie such children and machine GPIO descriptor tables. We will continue using a single table for entire device, but instead of using connection ID as a lookup key in the GPIO descriptor table directly, we will perform additional translation: fwnode_get_named_gpiod() when dealing with property_set-backed fwnodes will try parsing string property with name matching connection ID and use result of the lookup as the key in the table:

static const struct property_entry dev_child1_props[] __initconst = {
	...
	PROPERTY_ENTRY_STRING("gpios",		"child-1-gpios"),
	{ }
};
Quoted 8 lines
static struct gpiod_lookup_table dev_gpiod_table = {
	.dev_id = "some-device",
	.table = {
		...
		GPIO_LOOKUP_IDX("B", 1, "child-1-gpios", 1, GPIO_ACTIVE_LOW),
		...
	},
};

I wonder if instead of passing and parsing strings (and hoping there are no typos) we could get the compiler to help us bit more?

Something like below:

static const struct property_entry dev_child1_props[] __initconst = {

(quoted diff) +0 -0
...
 	PROPERTY_ENTRY_STRING("gpios","child-1-gpios"),
 	{ }

};

static struct gpiod_lookup_table dev_gpiod_table = {

(quoted diff) +0 -0
 	.dev_id = "some-device",
 	.table = {
...
 		GPIO_LOOKUP_IDX("B", 1, dev_child1_props, SIZEOF(dev_child1_props),
				1, GPIO_ACTIVE_LOW),
Quoted 3 lines

...
},
};

I am not sure how that would work, as there are multiple properties in that child array, so we can't simply take the first entry or assume that all entries describe GPIOs. Here is the fuller example:

static const struct property_entry simone_key_enter_props[] __initconst = {
	PROPERTY_ENTRY_U32("linux,code",	KEY_ENTER),
	PROPERTY_ENTRY_STRING("label",		"enter"),
	PROPERTY_ENTRY_STRING("gpios",		"enter-gpios"),
	{ }
};
static const struct property_entry simone_key_up_props[] __initconst = {
	PROPERTY_ENTRY_U32("linux,code",	KEY_UP),
	PROPERTY_ENTRY_STRING("label",		"up"),
	PROPERTY_ENTRY_STRING("gpios",		"up-gpios"),
	{ }
};
static const struct property_entry simone_key_up_props[] __initconst = {
	PROPERTY_ENTRY_U32("linux,code",	KEY_LEFT),
	PROPERTY_ENTRY_STRING("label",		"left"),
	PROPERTY_ENTRY_STRING("gpios",		"left-gpios"),
	{ }
};
static const struct property_entry simone_key_props[] __initconst = {
	/* There are no properties at device level on this device */
	{ }
};
static struct gpiod_lookup_table simone_keys_gpiod_table = {
	.dev_id = "gpio-keys",
	.table = {
		/* Use local offsets on gpiochip/port "B" */
		GPIO_LOOKUP_IDX("B", 0, "enter-gpios", 0, GPIO_ACTIVE_LOW),
		GPIO_LOOKUP_IDX("B", 1, "up-gpios", 1, GPIO_ACTIVE_LOW),
		GPIO_LOOKUP_IDX("B", 2, "left-gpios", 2, GPIO_ACTIVE_LOW),
	},
};
static struct platform_device simone_keys_device = {
	.name = "gpio-keys",
	.id = -1,
};

static void __init simone_init_machine(void)

{
	...
	gpiod_add_lookup_table(&simone_keys_gpiod_table);
	device_add_properties(&simone_keys_device.dev,
			      simone_keys_device_props);
	device_add_child_properties(&simone_keys_device.dev,
				    dev_fwnode(&simone_keys_device.dev),
				    simone_key_enter_props);
	device_add_child_properties(&simone_keys_device.dev,
				    dev_fwnode(&simone_keys_device.dev),
				    simone_key_up_props);
	device_add_child_properties(&simone_keys_device.dev,
				    dev_fwnode(&simone_keys_device.dev),
				    simone_key_left_props);
	platform_device_register(&simone_keys_device);
	...
}

Thanks.

Signature · 1 line
Dmitry
#21 lore raw

Re: [RFC/PATCH 4/5] gpiolib: add support for fetching descriptors from static properties

Quoted 55 lines · Dmitry Torokhov

I am not sure how that would work, as there are multiple properties in that child array, so we can't simply take the first entry or assume that all entries describe GPIOs. Here is the fuller example:

static const struct property_entry simone_key_enter_props[] __initconst = {
	PROPERTY_ENTRY_U32("linux,code",	KEY_ENTER),
	PROPERTY_ENTRY_STRING("label",		"enter"),
	PROPERTY_ENTRY_STRING("gpios",		"enter-gpios"),
	{ }
};
static const struct property_entry simone_key_up_props[] __initconst = {
	PROPERTY_ENTRY_U32("linux,code",	KEY_UP),
	PROPERTY_ENTRY_STRING("label",		"up"),
	PROPERTY_ENTRY_STRING("gpios",		"up-gpios"),
	{ }
};
static const struct property_entry simone_key_up_props[] __initconst = {
	PROPERTY_ENTRY_U32("linux,code",	KEY_LEFT),
	PROPERTY_ENTRY_STRING("label",		"left"),
	PROPERTY_ENTRY_STRING("gpios",		"left-gpios"),
	{ }
};
static const struct property_entry simone_key_props[] __initconst = {
	/* There are no properties at device level on this device */
	{ }
};
static struct gpiod_lookup_table simone_keys_gpiod_table = {
	.dev_id = "gpio-keys",
	.table = {
		/* Use local offsets on gpiochip/port "B" */
		GPIO_LOOKUP_IDX("B", 0, "enter-gpios", 0, GPIO_ACTIVE_LOW),
		GPIO_LOOKUP_IDX("B", 1, "up-gpios", 1, GPIO_ACTIVE_LOW),
		GPIO_LOOKUP_IDX("B", 2, "left-gpios", 2, GPIO_ACTIVE_LOW),
	},
};
static struct platform_device simone_keys_device = {
	.name = "gpio-keys",
	.id = -1,
};

static void __init simone_init_machine(void)

{
	...
	gpiod_add_lookup_table(&simone_keys_gpiod_table);
	device_add_properties(&simone_keys_device.dev,
			      simone_keys_device_props);
	device_add_child_properties(&simone_keys_device.dev,
				    dev_fwnode(&simone_keys_device.dev),
				    simone_key_enter_props);
	device_add_child_properties(&simone_keys_device.dev,
Quoted 8 lines
				    dev_fwnode(&simone_keys_device.dev),
				    simone_key_up_props);
	device_add_child_properties(&simone_keys_device.dev,
				    dev_fwnode(&simone_keys_device.dev),
				    simone_key_left_props);
	platform_device_register(&simone_keys_device);
	...
}

Thanks for clarifying. I missed this last part where you feed the properties to the device.

So looks fine by me :)

#22 lore raw

PATCH 5/5 RFC: ARM: simone: Hacked in keys

In reply to Dmitry Torokhov

From: Linus Walleij <[email protected]>

This serves as an illustration of how to use the gpio-keys
in board files using static device properties and machine GPIO descriptor tables.

It is a hack for the joystick connector on the entirely boardfile-based SIM.ONE. It will probably not be applied.

---
 arch/arm/mach-ep93xx/simone.c | 60 +++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)
arch/arm/mach-ep93xx/simone.c +60 -0
index 41aa57581356..b998d6772158 100644
@@ -24,7 +24,11 @@
 #include <linux/spi/mmc_spi.h>
 #include <linux/platform_data/video-ep93xx.h>
 #include <linux/platform_data/spi-ep93xx.h>
+#include <linux/gpio/machine.h>
 #include <linux/gpio.h>
+#include <linux/input.h>
+#include <linux/gpio_keys.h>
+#include <linux/property.h>
 
 #include <mach/hardware.h>
 #include <mach/gpio-ep93xx.h>
@@ -34,6 +38,47 @@
 
 #include "soc.h"
 
+static const struct property_entry simone_key_enter_props[] __initconst = {
+	PROPERTY_ENTRY_U32("linux,code",	KEY_ENTER),
+	PROPERTY_ENTRY_STRING("label",		"enter"),
+	PROPERTY_ENTRY_STRING("gpios",		"enter-gpios"),
+	{ }
+};
+
+static const struct property_entry simone_key_up_props[] __initconst = {
+	PROPERTY_ENTRY_U32("linux,code",	KEY_UP),
+	PROPERTY_ENTRY_STRING("label",		"up"),
+	PROPERTY_ENTRY_STRING("gpios",		"up-gpios"),
+	{ }
+};
+
+static const struct property_entry simone_key_up_props[] __initconst = {
+	PROPERTY_ENTRY_U32("linux,code",	KEY_LEFT),
+	PROPERTY_ENTRY_STRING("label",		"left"),
+	PROPERTY_ENTRY_STRING("gpios",		"left-gpios"),
+	{ }
+};
+
+static const struct property_entry simone_key_props[] __initconst = {
+	/* There are no properties at device level on this device */
+	{ }
+};
+
+static struct gpiod_lookup_table simone_keys_gpiod_table = {
+	.dev_id = "gpio-keys",
+	.table = {
+		/* Use local offsets on gpiochip/port "B" */
+		GPIO_LOOKUP_IDX("B", 0, "enter-gpios", 0, GPIO_ACTIVE_LOW),
+		GPIO_LOOKUP_IDX("B", 1, "up-gpios", 1, GPIO_ACTIVE_LOW),
+		GPIO_LOOKUP_IDX("B", 2, "left-gpios", 2, GPIO_ACTIVE_LOW),
+	},
+};
+
+static struct platform_device simone_keys_device = {
+	.name = "gpio-keys",
+	.id = -1,
+};
+
 static struct ep93xx_eth_data __initdata simone_eth_data = {
 	.phy_id		= 1,
 };
@@ -107,6 +152,21 @@ static void __init simone_init_machine(void)
 			    ARRAY_SIZE(simone_i2c_board_info));
 	ep93xx_register_spi(&simone_spi_info, simone_spi_devices,
 			    ARRAY_SIZE(simone_spi_devices));
+
+	gpiod_add_lookup_table(&simone_keys_gpiod_table);
+	device_add_properties(&simone_keys_device.dev,
+			      simone_keys_device_props);
+	device_add_child_properties(&simone_keys_device.dev,
+				    dev_fwnode(&simone_keys_device.dev),
+				    simone_key_enter_props);
+	device_add_child_properties(&simone_keys_device.dev,
+				    dev_fwnode(&simone_keys_device.dev),
+				    simone_key_up_props);
+	device_add_child_properties(&simone_keys_device.dev,
+				    dev_fwnode(&simone_keys_device.dev),
+				    simone_key_left_props);
+	platform_device_register(&simone_keys_device);
+
 	simone_register_audio();
 }
 
Signature · 1 line
2.19.0.397.gdd90340f6a-goog
#23 lore raw
In reply to Dmitry Torokhov
Quoted 11 lines · Dmitry Torokhov

The generic device properties APIs are very helpful as they allow abstracting away details of the platform (whether it is ACPI, device tree, or legacy board file), so that individual driver does not need separate code paths to support all variants. However there are drivers that currently can not use generic device properties API as they need notion of children properties, for example gpio_keys driver, that expects every button to be described as a sub-node of main device.

This patch series introduces notion of sub-nodes for static properties and ties it up with GPIO lookup tables so that they are usable with sub-nodes as well.

I like the idea.
Though, I Cc'ed this to Mika and Sakari who might be interested to see and, perhaps, comment on it.

Quoted 6 lines

Thanks.

--
Dmitry

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#24 lore raw
In reply to Dmitry Torokhov
Quoted 10 lines · Dmitry Torokhov

The generic device properties APIs are very helpful as they allow abstracting away details of the platform (whether it is ACPI, device tree, or legacy board file), so that individual driver does not need separate code paths to support all variants. However there are drivers that currently can not use generic device properties API as they need notion of children properties, for example gpio_keys driver, that expects every button to be described as a sub-node of main device.

This patch series introduces notion of sub-nodes for static properties and ties it up with GPIO lookup tables so that they are usable with sub-nodes as well.

I don't have any objections against this series.

Thanks,
Rafael

Reviewed-by on the v1 series
#25 lore raw
In reply to Dmitry Torokhov
Quoted 10 lines · Dmitry Torokhov

The generic device properties APIs are very helpful as they allow abstracting away details of the platform (whether it is ACPI, device tree, or legacy board file), so that individual driver does not need separate code paths to support all variants. However there are drivers that currently can not use generic device properties API as they need notion of children properties, for example gpio_keys driver, that expects every button to be described as a sub-node of main device.

This patch series introduces notion of sub-nodes for static properties and ties it up with GPIO lookup tables so that they are usable with sub-nodes as well.

This is the patch series I would have written, had I been smart enough.

for the series.

I can't test the SIM.ONE board with this until next week but the approach is definately what we want, not just for legacy boards, but also for any other non-discoverable hardware we currently poke into drivers/platform or arch/x86/platform etc.

Yours,
Linus Walleij

Yours,
Linus Walleij