Skip to contentSkip to conversation

use device_for_each_child_node_scoped to access device child nodes

PATCH Applied Javier Carrasco 4 participants 6 messages Aug 20, 2024 – Aug 26, 2024

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

v3 cover use device_for_each_child_node_scoped to access device child nodes

This series removes accesses to the device fwnode to iterate over its own child nodes. Using the device_for_each_child_node macro provides direct access to the device child nodes, and given that in all cases they are only required within the loop, the scoped variant of the macro can be used.

It has been stated in previous discussions [1] that device_for_each_* should be used to access device child nodes, removing the need to access its internal fwnode, and restricting fwnode_for_each_* to traversing subnodes when required.

Note that device_for_each_* implies availability, which means that after this conversion, unavailable nodes will not be accessible within the loop. The affected drivers does not seem to have any reason to iterate over unavailable nodes, though. But if someone has a case where the affected drivers might require accessing unavailable nodes, please let me know.

---
Changes in v3:
- leds-as3645a: swap the parameters in as3645a_parse_node() to have dev
at the beginning.
- Rebase onto next-20240820, drop upstreamed patches (changes for coresight-cti-platform).
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Rebase onto next-20240808, drop upstreamed patches (changes for ad7768-1)
- xilinx-ams.c: drop fwnode_device_is_available(child) (implicit in the
loop).
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Javier Carrasco (2):
iio: adc: xilinx-ams: use device_* to iterate over device child nodes
leds: as3645a: use device_* to iterate over device child nodes

 drivers/iio/adc/xilinx-ams.c      | 15 +++++----------
 drivers/leds/flash/leds-as3645a.c |  8 +++-----
 2 files changed, 8 insertions(+), 15 deletions(-)

---
base-commit: bb1b0acdcd66e0d8eedee3570d249e076b89ab32
change-id: 20240725-device_child_node_access-442533910a6f

Best regards,

Signature · 1 line
Javier Carrasco <[email protected]>
#1 lore raw

PATCH v3 1/2 iio: adc: xilinx-ams: use device_* to iterate over device child nodes

Use device_for_each_child_node_scoped() in ams_parse_firmware() to explicitly state device child node access, and simplify the child node handling as it is not required outside the loop.

---
 drivers/iio/adc/xilinx-ams.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/iio/adc/xilinx-ams.c +5 -10
index f051358d6b50..ebc583b07e0c 100644
@@ -1275,7 +1275,6 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 	struct ams *ams = iio_priv(indio_dev);
 	struct iio_chan_spec *ams_channels, *dev_channels;
 	struct device *dev = indio_dev->dev.parent;
-	struct fwnode_handle *child = NULL;
 	struct fwnode_handle *fwnode = dev_fwnode(dev);
 	size_t ams_size;
 	int ret, ch_cnt = 0, i, rising_off, falling_off;
@@ -1297,16 +1296,12 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 		num_channels += ret;
 	}
 
-	fwnode_for_each_child_node(fwnode, child) {
-		if (fwnode_device_is_available(child)) {
-			ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
-			if (ret < 0) {
-				fwnode_handle_put(child);
-				return ret;
-			}
+	device_for_each_child_node_scoped(dev, child) {
+		ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
+		if (ret < 0)
+			return ret;
 
-			num_channels += ret;
-		}
+		num_channels += ret;
 	}
 
 	for (i = 0; i < num_channels; i++) {
Signature · 1 line
2.43.0
#4 lore raw

PATCH v3 2/2 leds: as3645a: use device_* to iterate over device child nodes

In reply to Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that the child variable is only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

---
 drivers/leds/flash/leds-as3645a.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/leds/flash/leds-as3645a.c +3 -5
index 2c6ef321b7c8..2f2d783c62c3 100644
@@ -478,14 +478,12 @@ static int as3645a_detect(struct as3645a *flash)
 	return as3645a_write(flash, AS_BOOST_REG, AS_BOOST_CURRENT_DISABLE);
 }
 
-static int as3645a_parse_node(struct as3645a *flash,
-			      struct fwnode_handle *fwnode)
+static int as3645a_parse_node(struct device *dev, struct as3645a *flash)
 {
 	struct as3645a_config *cfg = &flash->cfg;
-	struct fwnode_handle *child;
 	int rval;
 
-	fwnode_for_each_child_node(fwnode, child) {
+	device_for_each_child_node_scoped(dev, child) {
 		u32 id = 0;
 
 		fwnode_property_read_u32(child, "reg", &id);
@@ -686,7 +684,7 @@ static int as3645a_probe(struct i2c_client *client)
 
 	flash->client = client;
 
-	rval = as3645a_parse_node(flash, dev_fwnode(&client->dev));
+	rval = as3645a_parse_node(&client->dev, flash);
 	if (rval < 0)
 		return rval;
 
Signature · 1 line
2.43.0
maintainer Applied
#5 lore raw

Re: (subset) [PATCH v3 2/2] leds: as3645a: use device_* to iterate over device child nodes

Quoted 9 lines · Javier Carrasco

Drop the manual access to the fwnode of the device to iterate over its child nodes. device_for_each_child_node macro provides direct access to the child nodes, and given that the child variable is only required within the loop, the scoped variant of the macro can be used.

Use the device_for_each_child_node_scoped macro to iterate over the direct child nodes of the device.

[...]

Applied, thanks!

[2/2] leds: as3645a: use device_* to iterate over device child nodes

      commit: 1041e1d4f061e65a3c47cff34709864293f07284

--
Lee Jones [李琼斯]

maintainer
#2 lore raw

Re: [PATCH v3 1/2] iio: adc: xilinx-ams: use device_* to iterate over device child nodes

In reply to Javier Carrasco
Quoted 5 lines · On Tue, 20 Aug 2024 21:02:26 +0200 Javier Carrasco…

Use device_for_each_child_node_scoped() in ams_parse_firmware() to explicitly state device child node access, and simplify the child node handling as it is not required outside the loop.

Applied, but I would ideally still like one of the xilinx folk others familiar with this driver to take a look. It'll be a few days before this ends up in next anyway as I need to rebase after Greg (hopefully) takes the pull request from last week.

It would be lovely to get rid of the direct fwnode usage in here but I'm not 100% sure if there is a path that will land on a disabled fwnode.

Thanks,

Jonathan

Quoted 39 lines
---
 drivers/iio/adc/xilinx-ams.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
drivers/iio/adc/xilinx-ams.c +5 -10
index f051358d6b50..ebc583b07e0c 100644
@@ -1275,7 +1275,6 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 	struct ams *ams = iio_priv(indio_dev);
 	struct iio_chan_spec *ams_channels, *dev_channels;
 	struct device *dev = indio_dev->dev.parent;
-	struct fwnode_handle *child = NULL;
 	struct fwnode_handle *fwnode = dev_fwnode(dev);
 	size_t ams_size;
 	int ret, ch_cnt = 0, i, rising_off, falling_off;
@@ -1297,16 +1296,12 @@ static int ams_parse_firmware(struct iio_dev *indio_dev)
 		num_channels += ret;
 	}
 
-	fwnode_for_each_child_node(fwnode, child) {
-		if (fwnode_device_is_available(child)) {
-			ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
-			if (ret < 0) {
-				fwnode_handle_put(child);
-				return ret;
-			}
+	device_for_each_child_node_scoped(dev, child) {
+		ret = ams_init_module(indio_dev, child, ams_channels + num_channels);
+		if (ret < 0)
+			return ret;
 
-			num_channels += ret;
-		}
+		num_channels += ret;
 	}
 
 	for (i = 0; i < num_channels; i++) {
maintainer
#3 lore raw

Re: [PATCH v3 1/2] iio: adc: xilinx-ams: use device_* to iterate over device child nodes

Hi Jonathan,

Quoted 7 lines · Jonathan Cameron
Quoted 4 lines · On Tue, 20 Aug 2024 21:02:26 +0200 Javier Carrasco…

Use device_for_each_child_node_scoped() in ams_parse_firmware() to explicitly state device child node access, and simplify the child node handling as it is not required outside the loop.

Quoted 5 lines
Quoted 1 line

Applied, but I would ideally still like one of the xilinx folk others familiar with this driver to take a look. It'll be a few days before this ends up in next anyway as I need to rebase after Greg (hopefully) takes the pull request from last week.

I just get back from vacation.

Quoted 4 lines

It would be lovely to get rid of the direct fwnode usage in here but I'm not 100% sure if there is a path that will land on a disabled fwnode.

Conall: Please take a look at this and test it.

Thanks,
Michal