Commit f2430363 authored by Matti Vaittinen's avatar Matti Vaittinen Committed by Jonathan Cameron
Browse files

property: Add functions to iterate named child



There are a few use-cases where child nodes with a specific name need to
be parsed. Code like:

fwnode_for_each_child_node()
	if (fwnode_name_eq())
		...

can be found from a various drivers/subsystems. Adding a macro for this
can simplify things a bit.

In a few cases the data from the found nodes is later added to an array,
which is allocated based on the number of found nodes. One example of
such use is the IIO subsystem's ADC channel nodes, where the relevant
nodes are named as channel[@N].

Add helpers for iterating and counting device's sub-nodes with certain
name instead of open-coding this in every user.

Suggested-by: default avatarJonathan Cameron <jic23@kernel.org>
Signed-off-by: default avatarMatti Vaittinen <mazziesaccount@gmail.com>
Reviewed-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: default avatarMarcelo Schmitt <marcelo.schmitt1@gmail.com>
Link: https://patch.msgid.link/2767173b7b18e974c0bac244688214bd3863ff06.1742560649.git.mazziesaccount@gmail.com


Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 4a135e92
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -945,6 +945,33 @@ unsigned int device_get_child_node_count(const struct device *dev)
}
EXPORT_SYMBOL_GPL(device_get_child_node_count);

/**
 * fwnode_get_named_child_node_count - number of child nodes with given name
 * @fwnode: Node which child nodes are counted.
 * @name: String to match child node name against.
 *
 * Scan child nodes and count all the nodes with a specific name. Potential
 * 'number' -ending after the 'at sign' for scanned names is ignored.
 * E.g.::
 *   fwnode_get_named_child_node_count(fwnode, "channel");
 * would match all the nodes::
 *   channel { }, channel@0 {}, channel@0xabba {}...
 *
 * Return: the number of child nodes with a matching name for a given device.
 */
unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
					       const char *name)
{
	struct fwnode_handle *child;
	unsigned int count = 0;

	fwnode_for_each_named_child_node(fwnode, child, name)
		count++;

	return count;
}
EXPORT_SYMBOL_GPL(fwnode_get_named_child_node_count);

bool device_dma_supported(const struct device *dev)
{
	return fwnode_call_bool_op(dev_fwnode(dev), device_dma_supported);
+20 −0
Original line number Diff line number Diff line
@@ -167,6 +167,10 @@ struct fwnode_handle *fwnode_get_next_available_child_node(
	for (child = fwnode_get_next_child_node(fwnode, NULL); child;	\
	     child = fwnode_get_next_child_node(fwnode, child))

#define fwnode_for_each_named_child_node(fwnode, child, name)		\
	fwnode_for_each_child_node(fwnode, child)			\
		if (!fwnode_name_eq(child, name)) { } else

#define fwnode_for_each_available_child_node(fwnode, child)		       \
	for (child = fwnode_get_next_available_child_node(fwnode, NULL); child;\
	     child = fwnode_get_next_available_child_node(fwnode, child))
@@ -178,11 +182,19 @@ struct fwnode_handle *device_get_next_child_node(const struct device *dev,
	for (child = device_get_next_child_node(dev, NULL); child;	\
	     child = device_get_next_child_node(dev, child))

#define device_for_each_named_child_node(dev, child, name)		\
	device_for_each_child_node(dev, child)				\
		if (!fwnode_name_eq(child, name)) { } else

#define device_for_each_child_node_scoped(dev, child)			\
	for (struct fwnode_handle *child __free(fwnode_handle) =	\
		device_get_next_child_node(dev, NULL);			\
	     child; child = device_get_next_child_node(dev, child))

#define device_for_each_named_child_node_scoped(dev, child, name)	\
	device_for_each_child_node_scoped(dev, child)			\
		if (!fwnode_name_eq(child, name)) { } else

struct fwnode_handle *fwnode_get_named_child_node(const struct fwnode_handle *fwnode,
						  const char *childname);
struct fwnode_handle *device_get_named_child_node(const struct device *dev,
@@ -210,6 +222,14 @@ int fwnode_irq_get_byname(const struct fwnode_handle *fwnode, const char *name);

unsigned int device_get_child_node_count(const struct device *dev);

unsigned int fwnode_get_named_child_node_count(const struct fwnode_handle *fwnode,
					       const char *name);
static inline unsigned int device_get_named_child_node_count(const struct device *dev,
							     const char *name)
{
	return fwnode_get_named_child_node_count(dev_fwnode(dev), name);
}

static inline int device_property_read_u8(const struct device *dev,
					  const char *propname, u8 *val)
{