diff mbox series

[v8,02/10] property: Add functions to iterate named child

Message ID 9c3880f74476436f39d796b5c10c540ae50b722c.1742225817.git.mazziesaccount@gmail.com (mailing list archive)
State Superseded, archived
Headers show
Series Support ROHM BD79124 ADC | expand

Commit Message

Matti Vaittinen March 17, 2025, 3:50 p.m. UTC
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: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
---
Revision history:
v7 => v8:
 - Fix the example in fwnode_get_named_child_node_count() documentation
   to use the fwnode_get_named_child_node_count() and not the
   device_get_named_child_node_count()
 - Fix the rest of the new macro's indentiations
v6 => v7:
 - Improve kerneldoc
 - Inline device_get_named_child_node_count() and change it to call
   fwnode_get_named_child_node_count() inside
 - Fix indentiation of the new macros
v5 => v6:
 - Add helpers to also iterate through the nodes.
v4 => v5:
 - Use given name instead of string 'channel' when counting the nodes
 - Add also fwnode_get_child_node_count_named() as suggested by Rob.
v3 => v4:
 - New patch as suggested by Jonathan, see discussion in:
https://lore.kernel.org/lkml/20250223161338.5c896280@jic23-huawei/
---
 drivers/base/property.c  | 27 +++++++++++++++++++++++++++
 include/linux/property.h | 24 ++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

Comments

Sakari Ailus March 18, 2025, 3:24 p.m. UTC | #1
Moi,

On Mon, Mar 17, 2025 at 05:50:38PM +0200, Matti Vaittinen wrote:
> 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: Jonathan Cameron <jic23@kernel.org>
> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Reviewed-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
> ---
> Revision history:
> v7 => v8:
>  - Fix the example in fwnode_get_named_child_node_count() documentation
>    to use the fwnode_get_named_child_node_count() and not the
>    device_get_named_child_node_count()
>  - Fix the rest of the new macro's indentiations
> v6 => v7:
>  - Improve kerneldoc
>  - Inline device_get_named_child_node_count() and change it to call
>    fwnode_get_named_child_node_count() inside
>  - Fix indentiation of the new macros
> v5 => v6:
>  - Add helpers to also iterate through the nodes.
> v4 => v5:
>  - Use given name instead of string 'channel' when counting the nodes
>  - Add also fwnode_get_child_node_count_named() as suggested by Rob.
> v3 => v4:
>  - New patch as suggested by Jonathan, see discussion in:
> https://lore.kernel.org/lkml/20250223161338.5c896280@jic23-huawei/
> ---
>  drivers/base/property.c  | 27 +++++++++++++++++++++++++++
>  include/linux/property.h | 24 ++++++++++++++++++++++++
>  2 files changed, 51 insertions(+)
> 
> diff --git a/drivers/base/property.c b/drivers/base/property.c
> index c1392743df9c..f42f32ff45fc 100644
> --- a/drivers/base/property.c
> +++ b/drivers/base/property.c
> @@ -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);
> diff --git a/include/linux/property.h b/include/linux/property.h
> index e214ecd241eb..a1856e6b714c 100644
> --- a/include/linux/property.h
> +++ b/include/linux/property.h
> @@ -167,10 +167,18 @@ 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))
>  
> +#define fwnode_for_each_available_named_child_node(fwnode, child, name)	\
> +	fwnode_for_each_available_child_node(fwnode, child)		\
> +		if (!fwnode_name_eq(child, name)) { } else
> +

OF only enumerates available nodes via the fwnode API, software nodes don't
have the concept but on ACPI I guess you could have a difference in nodes
where you have device sub-nodes that aren't available. Still, these ACPI
device nodes don't have meaningful names in this context (they're
4-character object names) so you wouldn't use them like this anyway.

So my question is: is it useful to provide this besides
fwnode_for_each_named_child_node(), given that both are effectively the
same?

>  struct fwnode_handle *device_get_next_child_node(const struct device *dev,
>  						 struct fwnode_handle *child);
>  
> @@ -178,11 +186,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 +226,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)
>  {
Matti Vaittinen March 19, 2025, 6:02 a.m. UTC | #2
Moro Sakari,

Thanks for the review.

On 18/03/2025 17:24, Sakari Ailus wrote:
> Moi,
> 
> On Mon, Mar 17, 2025 at 05:50:38PM +0200, Matti Vaittinen wrote:
>> 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: Jonathan Cameron <jic23@kernel.org>
>> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
>> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> Reviewed-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
>> ---
>> Revision history:
>> v7 => v8:
>>   - Fix the example in fwnode_get_named_child_node_count() documentation
>>     to use the fwnode_get_named_child_node_count() and not the
>>     device_get_named_child_node_count()
>>   - Fix the rest of the new macro's indentiations
>> v6 => v7:
>>   - Improve kerneldoc
>>   - Inline device_get_named_child_node_count() and change it to call
>>     fwnode_get_named_child_node_count() inside
>>   - Fix indentiation of the new macros
>> v5 => v6:
>>   - Add helpers to also iterate through the nodes.
>> v4 => v5:
>>   - Use given name instead of string 'channel' when counting the nodes
>>   - Add also fwnode_get_child_node_count_named() as suggested by Rob.
>> v3 => v4:
>>   - New patch as suggested by Jonathan, see discussion in:
>> https://lore.kernel.org/lkml/20250223161338.5c896280@jic23-huawei/
>> ---
>>   drivers/base/property.c  | 27 +++++++++++++++++++++++++++
>>   include/linux/property.h | 24 ++++++++++++++++++++++++
>>   2 files changed, 51 insertions(+)
>>
>> diff --git a/drivers/base/property.c b/drivers/base/property.c
>> index c1392743df9c..f42f32ff45fc 100644
>> --- a/drivers/base/property.c
>> +++ b/drivers/base/property.c
>> @@ -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);
>> diff --git a/include/linux/property.h b/include/linux/property.h
>> index e214ecd241eb..a1856e6b714c 100644
>> --- a/include/linux/property.h
>> +++ b/include/linux/property.h
>> @@ -167,10 +167,18 @@ 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))
>>   
>> +#define fwnode_for_each_available_named_child_node(fwnode, child, name)	\
>> +	fwnode_for_each_available_child_node(fwnode, child)		\
>> +		if (!fwnode_name_eq(child, name)) { } else
>> +
> 
> OF only enumerates available nodes via the fwnode API, software nodes don't
> have the concept but on ACPI I guess you could have a difference in nodes
> where you have device sub-nodes that aren't available. Still, these ACPI
> device nodes don't have meaningful names in this context (they're
> 4-character object names) so you wouldn't use them like this anyway.

I believe you have far better understanding on these concepts than I do. 
The reason behind adding fwnode_for_each_available_child_node() was the 
patch 10/10:

-	fwnode_for_each_available_child_node(sensors, node) {
-		if (fwnode_name_eq(node, "sensor")) {
-			if (!thp7312_sensor_parse_dt(thp7312, node))
-				num_sensors++;
-		}
+	fwnode_for_each_available_named_child_node(sensors, node, "sensor") {
+		if (!thp7312_sensor_parse_dt(thp7312, node))
+			num_sensors++;
  	}


> So my question is: is it useful to provide this besides
> fwnode_for_each_named_child_node(), given that both are effectively the
> same?

So, I suppose you're saying the existing thp7312 -driver has no real 
reason to use the 'fwnode_for_each_available_child_node()', but it could 
be using fwnode_for_each_child_node() instead?

If so, I am Ok with dropping the 
'fwnode_for_each_available_named_child_node()' and changing the 10/10 to:

-	fwnode_for_each_available_child_node(sensors, node) {
-		if (fwnode_name_eq(node, "sensor")) {
-			if (!thp7312_sensor_parse_dt(thp7312, node))
-				num_sensors++;
-		}
+	fwnode_for_each_named_child_node(sensors, node, "sensor") {
+		if (!thp7312_sensor_parse_dt(thp7312, node))
+			num_sensors++;
  	}

Do you think that'd be correct?

Yours,
	-- Matti
Sakari Ailus March 19, 2025, 3:23 p.m. UTC | #3
Hei Matti,

On Wed, Mar 19, 2025 at 08:02:24AM +0200, Matti Vaittinen wrote:
> Moro Sakari,
> 
> Thanks for the review.
> 
> On 18/03/2025 17:24, Sakari Ailus wrote:
> > Moi,
> > 
> > On Mon, Mar 17, 2025 at 05:50:38PM +0200, Matti Vaittinen wrote:
> > > 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: Jonathan Cameron <jic23@kernel.org>
> > > Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
> > > Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > > Reviewed-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
> > > ---
> > > Revision history:
> > > v7 => v8:
> > >   - Fix the example in fwnode_get_named_child_node_count() documentation
> > >     to use the fwnode_get_named_child_node_count() and not the
> > >     device_get_named_child_node_count()
> > >   - Fix the rest of the new macro's indentiations
> > > v6 => v7:
> > >   - Improve kerneldoc
> > >   - Inline device_get_named_child_node_count() and change it to call
> > >     fwnode_get_named_child_node_count() inside
> > >   - Fix indentiation of the new macros
> > > v5 => v6:
> > >   - Add helpers to also iterate through the nodes.
> > > v4 => v5:
> > >   - Use given name instead of string 'channel' when counting the nodes
> > >   - Add also fwnode_get_child_node_count_named() as suggested by Rob.
> > > v3 => v4:
> > >   - New patch as suggested by Jonathan, see discussion in:
> > > https://lore.kernel.org/lkml/20250223161338.5c896280@jic23-huawei/
> > > ---
> > >   drivers/base/property.c  | 27 +++++++++++++++++++++++++++
> > >   include/linux/property.h | 24 ++++++++++++++++++++++++
> > >   2 files changed, 51 insertions(+)
> > > 
> > > diff --git a/drivers/base/property.c b/drivers/base/property.c
> > > index c1392743df9c..f42f32ff45fc 100644
> > > --- a/drivers/base/property.c
> > > +++ b/drivers/base/property.c
> > > @@ -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);
> > > diff --git a/include/linux/property.h b/include/linux/property.h
> > > index e214ecd241eb..a1856e6b714c 100644
> > > --- a/include/linux/property.h
> > > +++ b/include/linux/property.h
> > > @@ -167,10 +167,18 @@ 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))
> > > +#define fwnode_for_each_available_named_child_node(fwnode, child, name)	\
> > > +	fwnode_for_each_available_child_node(fwnode, child)		\
> > > +		if (!fwnode_name_eq(child, name)) { } else
> > > +
> > 
> > OF only enumerates available nodes via the fwnode API, software nodes don't
> > have the concept but on ACPI I guess you could have a difference in nodes
> > where you have device sub-nodes that aren't available. Still, these ACPI
> > device nodes don't have meaningful names in this context (they're
> > 4-character object names) so you wouldn't use them like this anyway.
> 
> I believe you have far better understanding on these concepts than I do. The
> reason behind adding fwnode_for_each_available_child_node() was the patch
> 10/10:
> 
> -	fwnode_for_each_available_child_node(sensors, node) {
> -		if (fwnode_name_eq(node, "sensor")) {
> -			if (!thp7312_sensor_parse_dt(thp7312, node))
> -				num_sensors++;
> -		}
> +	fwnode_for_each_available_named_child_node(sensors, node, "sensor") {
> +		if (!thp7312_sensor_parse_dt(thp7312, node))
> +			num_sensors++;
>  	}
> 
> 
> > So my question is: is it useful to provide this besides
> > fwnode_for_each_named_child_node(), given that both are effectively the
> > same?
> 
> So, I suppose you're saying the existing thp7312 -driver has no real reason
> to use the 'fwnode_for_each_available_child_node()', but it could be using
> fwnode_for_each_child_node() instead?
> 
> If so, I am Ok with dropping the
> 'fwnode_for_each_available_named_child_node()' and changing the 10/10 to:
> 
> -	fwnode_for_each_available_child_node(sensors, node) {
> -		if (fwnode_name_eq(node, "sensor")) {
> -			if (!thp7312_sensor_parse_dt(thp7312, node))
> -				num_sensors++;
> -		}
> +	fwnode_for_each_named_child_node(sensors, node, "sensor") {
> +		if (!thp7312_sensor_parse_dt(thp7312, node))
> +			num_sensors++;
>  	}
> 
> Do you think that'd be correct?

I'd say so. Feel free to cc me to the last patch as well.

I guess one way to make this clearer is to switch to
fwnode_for_each_child_node() in a separate patch before
fwnode_for_each_named_child_node() conversion.

There are also just a handful of users of
fwnode_for_each_available_child_node() and I guess these could be
converted, too, but I think it's outside the scope of the set.
Matti Vaittinen March 20, 2025, 6:43 a.m. UTC | #4
On 19/03/2025 17:23, Sakari Ailus wrote:
> On Wed, Mar 19, 2025 at 08:02:24AM +0200, Matti Vaittinen wrote:
>> On 18/03/2025 17:24, Sakari Ailus wrote:
>>> On Mon, Mar 17, 2025 at 05:50:38PM +0200, Matti Vaittinen wrote:
>>>> There are a few use-cases where child nodes with a specific name need to
>>>> be parsed. Code like:

...

>>>> --- a/include/linux/property.h
>>>> +++ b/include/linux/property.h
>>>> @@ -167,10 +167,18 @@ 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))
>>>> +#define fwnode_for_each_available_named_child_node(fwnode, child, name)	\
>>>> +	fwnode_for_each_available_child_node(fwnode, child)		\
>>>> +		if (!fwnode_name_eq(child, name)) { } else
>>>> +
>>>
>>> OF only enumerates available nodes via the fwnode API, software nodes don't
>>> have the concept but on ACPI I guess you could have a difference in nodes
>>> where you have device sub-nodes that aren't available. Still, these ACPI
>>> device nodes don't have meaningful names in this context (they're
>>> 4-character object names) so you wouldn't use them like this anyway.
>>
>> I believe you have far better understanding on these concepts than I do. The
>> reason behind adding fwnode_for_each_available_child_node() was the patch
>> 10/10:
>>
>> -	fwnode_for_each_available_child_node(sensors, node) {
>> -		if (fwnode_name_eq(node, "sensor")) {
>> -			if (!thp7312_sensor_parse_dt(thp7312, node))
>> -				num_sensors++;
>> -		}
>> +	fwnode_for_each_available_named_child_node(sensors, node, "sensor") {
>> +		if (!thp7312_sensor_parse_dt(thp7312, node))
>> +			num_sensors++;
>>   	}
>>
>>
>>> So my question is: is it useful to provide this besides
>>> fwnode_for_each_named_child_node(), given that both are effectively the
>>> same?
>>
>> So, I suppose you're saying the existing thp7312 -driver has no real reason
>> to use the 'fwnode_for_each_available_child_node()', but it could be using
>> fwnode_for_each_child_node() instead?
>>
>> If so, I am Ok with dropping the
>> 'fwnode_for_each_available_named_child_node()' and changing the 10/10 to:
>>
>> -	fwnode_for_each_available_child_node(sensors, node) {
>> -		if (fwnode_name_eq(node, "sensor")) {
>> -			if (!thp7312_sensor_parse_dt(thp7312, node))
>> -				num_sensors++;
>> -		}
>> +	fwnode_for_each_named_child_node(sensors, node, "sensor") {
>> +		if (!thp7312_sensor_parse_dt(thp7312, node))
>> +			num_sensors++;
>>   	}
>>
>> Do you think that'd be correct?
> 
> I'd say so. Feel free to cc me to the last patch as well.

Thanks. I'll drop the fwnode_for_each_available_named_child_node() then.

> I guess one way to make this clearer is to switch to
> fwnode_for_each_child_node() in a separate patch before
> fwnode_for_each_named_child_node() conversion.

I suppose this makes sense.

I think this series can't make it to 6.15-rc1. Meaning, these 
*_named_*() APIs perhaps land in 6.16-rc1. I assume these *_named_*() 
APIs will go through the IIO. This rather simple IIO driver's review 
took longer than I predicted, with more versions I intended (as always) 
- and I kind of dislike respinning the whole series, with this large 
audience, when changes are not interesting to the most.

Maybe it is simplest to drop the thp7312 (and gianfar) from this series, 
and respin them only when the 6.16-rc1 is out. It's going to be couple 
of months though - so there's always a risk that I forget.

The proposed change for the thp7312, from 
fwnode_for_each_available_child_node() to fwnode_for_each_child_node() 
can be done earlier though.

> There are also just a handful of users of
> fwnode_for_each_available_child_node() and I guess these could be
> converted, too, but I think it's outside the scope of the set.

Definitely not in the scope of the bd79124 support :)

Yours,
	-- Matti
diff mbox series

Patch

diff --git a/drivers/base/property.c b/drivers/base/property.c
index c1392743df9c..f42f32ff45fc 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -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);
diff --git a/include/linux/property.h b/include/linux/property.h
index e214ecd241eb..a1856e6b714c 100644
--- a/include/linux/property.h
+++ b/include/linux/property.h
@@ -167,10 +167,18 @@  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))
 
+#define fwnode_for_each_available_named_child_node(fwnode, child, name)	\
+	fwnode_for_each_available_child_node(fwnode, child)		\
+		if (!fwnode_name_eq(child, name)) { } else
+
 struct fwnode_handle *device_get_next_child_node(const struct device *dev,
 						 struct fwnode_handle *child);
 
@@ -178,11 +186,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 +226,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)
 {