From: Sakari Ailus <sakari.ailus@linux.intel.com>
To: Matti Vaittinen <mazziesaccount@gmail.com>
Cc: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>,
Jonathan Cameron <jic23@kernel.org>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Daniel Scally <djrscally@gmail.com>,
Heikki Krogerus <heikki.krogerus@linux.intel.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"Rafael J. Wysocki" <rafael@kernel.org>,
Danilo Krummrich <dakr@kernel.org>,
Claudiu Manoil <claudiu.manoil@nxp.com>,
Andrew Lunn <andrew+netdev@lunn.ch>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@google.com>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org,
netdev@vger.kernel.org
Subject: Re: [PATCH v8 02/10] property: Add functions to iterate named child
Date: Tue, 18 Mar 2025 15:24:44 +0000 [thread overview]
Message-ID: <Z9mQPJwnKAkPHriT@kekkonen.localdomain> (raw)
In-Reply-To: <9c3880f74476436f39d796b5c10c540ae50b722c.1742225817.git.mazziesaccount@gmail.com>
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)
> {
--
Terveisin,
Sakari Ailus
next prev parent reply other threads:[~2025-03-18 15:24 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-17 15:49 [PATCH v8 00/10] Support ROHM BD79124 ADC Matti Vaittinen
2025-03-17 15:50 ` [PATCH v8 01/10] dt-bindings: ROHM BD79124 ADC/GPO Matti Vaittinen
2025-03-17 15:50 ` [PATCH v8 02/10] property: Add functions to iterate named child Matti Vaittinen
2025-03-18 15:24 ` Sakari Ailus [this message]
2025-03-19 6:02 ` Matti Vaittinen
2025-03-19 15:23 ` Sakari Ailus
2025-03-20 6:43 ` Matti Vaittinen
2025-03-30 16:07 ` Jonathan Cameron
2025-03-17 15:50 ` [PATCH v8 03/10] iio: adc: add helpers for parsing ADC nodes Matti Vaittinen
2025-03-17 16:01 ` Andy Shevchenko
2025-03-17 15:51 ` [PATCH v8 04/10] iio: adc: rzg2l_adc: Use adc-helpers Matti Vaittinen
2025-03-17 16:02 ` Andy Shevchenko
2025-03-17 15:51 ` [PATCH v8 05/10] iio: adc: sun20i-gpadc: " Matti Vaittinen
2025-03-17 16:03 ` Andy Shevchenko
2025-03-17 15:51 ` [PATCH v8 06/10] iio: adc: Support ROHM BD79124 ADC Matti Vaittinen
2025-03-17 16:43 ` Andy Shevchenko
2025-03-18 7:35 ` Matti Vaittinen
2025-03-17 15:51 ` [PATCH v8 07/10] MAINTAINERS: Add IIO ADC helpers Matti Vaittinen
2025-03-17 15:51 ` [PATCH v8 08/10] MAINTAINERS: Add ROHM BD79124 ADC/GPO Matti Vaittinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Z9mQPJwnKAkPHriT@kekkonen.localdomain \
--to=sakari.ailus@linux.intel.com \
--cc=andrew+netdev@lunn.ch \
--cc=andriy.shevchenko@linux.intel.com \
--cc=claudiu.manoil@nxp.com \
--cc=conor+dt@kernel.org \
--cc=dakr@kernel.org \
--cc=davem@davemloft.net \
--cc=devicetree@vger.kernel.org \
--cc=djrscally@gmail.com \
--cc=edumazet@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=kuba@kernel.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=matti.vaittinen@fi.rohmeurope.com \
--cc=mazziesaccount@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rafael@kernel.org \
--cc=robh@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox