mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/6] spi: fix NULL pointer dereference for board info devices
@ 2026-11-19 18:58 Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 2/6] driver: pass the requested node to the rescan hook Ahmad Fatoum
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2026-11-19 18:58 UTC (permalink / raw)
  To: barebox; +Cc: Alexander Shiyan, Ahmad Fatoum

Devices registered with spi_register_board_info() have no device node,
but spi_new_device() unconditionally stores the new device in it.

The at91sam9m10g45ek board, which has the Atmel SPI driver enabled in
its defconfig, still registers its devices this way. Let's add an
of_node_set_dev() helper that only sets the node's device if there is
a node and use it here.

Fixes: cd23b6facf33 ("common: add initial barebox deep-probe support")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/spi/spi.c | 2 +-
 include/of.h      | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index b64e20664985..53bcd22b8c56 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -125,7 +125,7 @@ struct spi_device *spi_new_device(struct spi_controller *ctrl,
 	if (status)
 		goto fail;
 
-	chip->device_node->dev = &proxy->dev;
+	of_node_set_dev(chip->device_node, &proxy->dev);
 
 	return proxy;
 fail:
diff --git a/include/of.h b/include/of.h
index 07d435f91475..993315247bf0 100644
--- a/include/of.h
+++ b/include/of.h
@@ -134,6 +134,12 @@ static inline struct device_node *of_node_get(struct device_node *node)
 static inline void of_node_put(struct device_node *node) { }
 DEFINE_FREE(device_node, struct device_node *, if (_T) of_node_put(_T))
 
+static inline void of_node_set_dev(struct device_node *np, struct device *dev)
+{
+	if (np)
+		np->dev = dev;
+}
+
 void of_print_property(const void *data, int len);
 void of_print_cmdline(struct device_node *root);
 
-- 
2.47.3




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/6] driver: pass the requested node to the rescan hook
  2026-11-19 18:58 [PATCH 1/6] spi: fix NULL pointer dereference for board info devices Ahmad Fatoum
@ 2026-11-19 18:58 ` Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 3/6] i2c: factor out of_i2c_register_device() Ahmad Fatoum
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2026-11-19 18:58 UTC (permalink / raw)
  To: barebox; +Cc: Alexander Shiyan, Ahmad Fatoum

of_device_create_on_demand() rescans the parent device, so a bus like
I2C or SPI registers the device for the requested node. The hooks
register every child that has no device yet though, not only the one
asked for.

Let's pass the node along, so the hooks can restrict themselves to it.
NULL still means all children. The I2C and SPI hooks ignore the node
for now, so there is no functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/i2c/i2c.c     | 2 +-
 drivers/of/platform.c | 4 ++--
 drivers/spi/spi.c     | 2 +-
 include/device.h      | 5 +++--
 include/driver.h      | 4 ++--
 5 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index 407c4db8a76a..cfc340736309 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -485,7 +485,7 @@ int of_i2c_register_devices_by_node(struct device_node *node)
 	return 0;
 }
 
-static void i2c_hw_rescan(struct device *dev)
+static void i2c_hw_rescan(struct device *dev, struct device_node *np)
 {
 	struct i2c_adapter *adap;
 
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
index 0346d87a2b61..12a1b759fc07 100644
--- a/drivers/of/platform.c
+++ b/drivers/of/platform.c
@@ -138,7 +138,7 @@ struct device *of_platform_device_create(struct device_node *np,
 	 * devices.
 	 */
 	if (np->dev) {
-		device_rescan(np->dev);
+		device_rescan(np->dev, NULL);
 		return np->dev;
 	}
 
@@ -450,7 +450,7 @@ static struct device *of_device_create_on_demand(struct device_node *np)
 		return NULL;
 
 	if (!np->dev && parent->dev)
-		device_rescan(parent->dev);
+		device_rescan(parent->dev, np);
 
 	/* Create all parent devices needed for the requested device */
 	parent_dev = parent->dev ? : of_device_create_on_demand(parent);
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 53bcd22b8c56..b1806a2ea445 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -199,7 +199,7 @@ static void spi_of_register_slaves(struct spi_controller *ctrl)
 	}
 }
 
-static void spi_controller_rescan(struct device *dev)
+static void spi_controller_rescan(struct device *dev, struct device_node *np)
 {
 	struct spi_controller *ctrl;
 
diff --git a/include/device.h b/include/device.h
index 7abeed9c460f..34b45a9bb014 100644
--- a/include/device.h
+++ b/include/device.h
@@ -59,7 +59,8 @@ struct of_device_id;
  * @dma_offset: DMA offset.
  * @detect: For devices which take longer to probe, this is called when the driver
  *          should actually detect client devices.
- * @rescan: Callback to rescan the device.
+ * @rescan: Callback to rescan the device. If @np is not NULL, only the
+ *          device for that child node needs to be created.
  * @deferred_probe_reason: If a driver probe is deferred, this stores the last error.
  */
 struct device {
@@ -119,7 +120,7 @@ struct device {
 #endif
 
 	int (*detect)(struct device *);
-	void (*rescan)(struct device *);
+	void (*rescan)(struct device *dev, struct device_node *np);
 
 	char *deferred_probe_reason;
 };
diff --git a/include/driver.h b/include/driver.h
index a93da2e06f62..ec1f1ccfb85e 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -98,10 +98,10 @@ static inline void put_device(struct device *dev) {}
 void free_device_res(struct device *dev);
 void free_device(struct device *dev);
 
-static inline void device_rescan(struct device *dev)
+static inline void device_rescan(struct device *dev, struct device_node *np)
 {
 	if (dev->rescan)
-		dev->rescan(dev);
+		dev->rescan(dev, np);
 }
 
 /* Iterate over a devices children
-- 
2.47.3




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/6] i2c: factor out of_i2c_register_device()
  2026-11-19 18:58 [PATCH 1/6] spi: fix NULL pointer dereference for board info devices Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 2/6] driver: pass the requested node to the rescan hook Ahmad Fatoum
@ 2026-11-19 18:58 ` Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 4/6] i2c: allow creating siblings on demand while populating the bus Ahmad Fatoum
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2026-11-19 18:58 UTC (permalink / raw)
  To: barebox; +Cc: Alexander Shiyan, Ahmad Fatoum

Let's move the registration of a single device node into its own
function. A follow-up commit will use it to register only the node
requested by the rescan hook.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/i2c/i2c.c | 72 +++++++++++++++++++++++++----------------------
 1 file changed, 38 insertions(+), 34 deletions(-)

diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index cfc340736309..ef92854eefe0 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -427,6 +427,42 @@ void i2c_unregister_device(struct i2c_client *client)
 	devinfo_del(&client->dev, i2c_info);
 }
 
+static void of_i2c_register_device(struct i2c_adapter *adap,
+				   struct device_node *n)
+{
+	struct i2c_board_info info = {};
+	struct i2c_client *result;
+	const __be32 *addr;
+	int len;
+
+	if (n->dev) {
+		dev_dbg(&adap->dev, "of_i2c: skipping already registered %s\n",
+			dev_name(n->dev));
+		return;
+	}
+
+	of_modalias_node(n, info.type, I2C_NAME_SIZE);
+
+	info.of_node = n;
+
+	addr = of_get_property(n, "reg", &len);
+	if (!addr || (len < sizeof(int))) {
+		dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", n);
+		return;
+	}
+
+	info.addr = be32_to_cpup(addr);
+	if (info.addr > (1 << 10) - 1) {
+		dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
+			info.addr, n);
+		return;
+	}
+
+	result = i2c_new_device(adap, &info);
+	if (!result)
+		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", n);
+}
+
 static void of_i2c_register_devices(struct i2c_adapter *adap)
 {
 	struct device_node *n;
@@ -435,40 +471,8 @@ static void of_i2c_register_devices(struct i2c_adapter *adap)
 	if (!IS_ENABLED(CONFIG_OFDEVICE) || !adap->dev.of_node)
 		return;
 
-	for_each_available_child_of_node(adap->dev.of_node, n) {
-		struct i2c_board_info info = {};
-		struct i2c_client *result;
-		const __be32 *addr;
-		int len;
-
-		if (n->dev) {
-			dev_dbg(&adap->dev, "of_i2c: skipping already registered %s\n",
-				dev_name(n->dev));
-			continue;
-		}
-
-		of_modalias_node(n, info.type, I2C_NAME_SIZE);
-
-		info.of_node = n;
-
-		addr = of_get_property(n, "reg", &len);
-		if (!addr || (len < sizeof(int))) {
-			dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", n);
-			continue;
-		}
-
-		info.addr = be32_to_cpup(addr);
-		if (info.addr > (1 << 10) - 1) {
-			dev_err(&adap->dev, "of_i2c: invalid addr=%x on %pOF\n",
-				info.addr, n);
-			continue;
-		}
-
-		result = i2c_new_device(adap, &info);
-		if (!result)
-			dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n",
-			        n);
-	}
+	for_each_available_child_of_node(adap->dev.of_node, n)
+		of_i2c_register_device(adap, n);
 }
 
 int of_i2c_register_devices_by_node(struct device_node *node)
-- 
2.47.3




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 4/6] i2c: allow creating siblings on demand while populating the bus
  2026-11-19 18:58 [PATCH 1/6] spi: fix NULL pointer dereference for board info devices Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 2/6] driver: pass the requested node to the rescan hook Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 3/6] i2c: factor out of_i2c_register_device() Ahmad Fatoum
@ 2026-11-19 18:58 ` Ahmad Fatoum
  2026-11-20 11:04   ` Alexander Shiyan
  2026-11-19 18:58 ` [PATCH 5/6] spi: factor out spi_of_register_slave() Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 6/6] spi: allow creating siblings on demand while populating the bus Ahmad Fatoum
  4 siblings, 1 reply; 7+ messages in thread
From: Ahmad Fatoum @ 2026-11-19 18:58 UTC (permalink / raw)
  To: barebox; +Cc: Alexander Shiyan, Ahmad Fatoum

With deep probe, a device probed while its adapter populates the bus
may depend on a sibling that comes later in the device tree, e.g. a
GPIO expander whose vcc-supply is a PMIC regulator. To create the PMIC,
of_device_create_on_demand() rescans the controller, but the rescan
hook is only installed after population. So the PMIC node gets a
platform device without a driver instead, the bus then skips it as
already registered and the PMIC never probes.

Install the hook before populating the bus. Alone, that makes the
nested rescan register the expander again, recursing until the stack
overflows, because its node only gets its device after probe. So set
it before registering the client, like platform devices already do.

The nested rescan must also register only the requested node.
Otherwise, all later siblings are probed while the expander is still
probing, and one using the expander's GPIOs fails to get them.

Reported-by: Alexander Shiyan <eagle.alexander923@gmail.com>
Link: https://lore.barebox.org/CAP1tNvRtU8munqnUTiqU_zEP_rxdm_hb42u_oU1FVL93czrtFA@mail.gmail.com
Fixes: 7d8f15b36489 ("i2c: implement rescan")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/i2c/i2c.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
index ef92854eefe0..d0f9be826025 100644
--- a/drivers/i2c/i2c.c
+++ b/drivers/i2c/i2c.c
@@ -405,16 +405,17 @@ static struct i2c_client *i2c_new_device(struct i2c_adapter *adapter,
 
 	client->dev.parent = &adapter->dev;
 
+	/* claim the node before probe can rescan the bus */
+	of_node_set_dev(chip->of_node, &client->dev);
+
 	status = register_device(&client->dev);
 	if (status) {
+		of_node_set_dev(chip->of_node, NULL);
 		free(client);
 		return NULL;
 	}
 	devinfo_add(&client->dev, i2c_info);
 
-	if (chip->of_node)
-		chip->of_node->dev = &client->dev;
-
 	dev_dbg(&client->dev, "registered on bus %d, chip->addr 0x%02x\n",
 		adapter->nr, client->addr);
 
@@ -496,7 +497,12 @@ static void i2c_hw_rescan(struct device *dev, struct device_node *np)
 	for_each_i2c_adapter(adap) {
 		if (dev != adap->dev.parent)
 			continue;
-		of_i2c_register_devices(adap);
+
+		if (!np)
+			of_i2c_register_devices(adap);
+		else if (np->parent == adap->dev.of_node &&
+			 of_device_is_available(np))
+			of_i2c_register_device(adap, np);
 		break;
 	}
 }
@@ -742,17 +748,18 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
 
 	slice_init(&adapter->slice, dev_name(&adapter->dev));
 
-	/* populate children from any i2c device tables */
-	scan_boardinfo(adapter);
-
-	of_i2c_register_devices(adapter);
-
+	/* children probed below may need their siblings created on demand */
 	hw_dev = adapter->dev.parent;
 	if (hw_dev && dev_of_node(hw_dev)) {
 		if (!hw_dev->rescan)
 			hw_dev->rescan = i2c_hw_rescan;
 	}
 
+	/* populate children from any i2c device tables */
+	scan_boardinfo(adapter);
+
+	of_i2c_register_devices(adapter);
+
 	return 0;
 }
 EXPORT_SYMBOL(i2c_add_numbered_adapter);
-- 
2.47.3




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 5/6] spi: factor out spi_of_register_slave()
  2026-11-19 18:58 [PATCH 1/6] spi: fix NULL pointer dereference for board info devices Ahmad Fatoum
                   ` (2 preceding siblings ...)
  2026-11-19 18:58 ` [PATCH 4/6] i2c: allow creating siblings on demand while populating the bus Ahmad Fatoum
@ 2026-11-19 18:58 ` Ahmad Fatoum
  2026-11-19 18:58 ` [PATCH 6/6] spi: allow creating siblings on demand while populating the bus Ahmad Fatoum
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2026-11-19 18:58 UTC (permalink / raw)
  To: barebox; +Cc: Alexander Shiyan, Ahmad Fatoum

Let's move the registration of a single device node into its own
function. A follow-up commit will use it to register only the node
requested by the rescan hook.

No functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/spi/spi.c | 77 +++++++++++++++++++++++++----------------------
 1 file changed, 41 insertions(+), 36 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index b1806a2ea445..850ea2c7b62f 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -150,10 +150,48 @@ static void of_spi_parse_dt_cs_delay(struct device_node *nc,
 	}
 }
 
+static void spi_of_register_slave(struct spi_controller *ctrl,
+				  struct device_node *n)
+{
+	struct spi_board_info chip = {};
+	struct property *reg;
+
+	if (n->dev) {
+		dev_dbg(ctrl->dev, "skipping already registered %s\n",
+			dev_name(n->dev));
+		return;
+	}
+
+	chip.name = xstrdup(n->name);
+	chip.bus_num = ctrl->bus_num;
+	/* Mode (clock phase/polarity/etc.) */
+	if (of_property_read_bool(n, "spi-cpha"))
+		chip.mode |= SPI_CPHA;
+	if (of_property_read_bool(n, "spi-cpol"))
+		chip.mode |= SPI_CPOL;
+	if (of_property_read_bool(n, "spi-cs-high"))
+		chip.mode |= SPI_CS_HIGH;
+	if (of_property_read_bool(n, "spi-3wire"))
+		chip.mode |= SPI_3WIRE;
+	of_property_read_u32(n, "spi-max-frequency",
+			&chip.max_speed_hz);
+
+	/* Device CS delays */
+	of_spi_parse_dt_cs_delay(n, &chip.cs_setup, "spi-cs-setup-delay-ns");
+	of_spi_parse_dt_cs_delay(n, &chip.cs_hold, "spi-cs-hold-delay-ns");
+	of_spi_parse_dt_cs_delay(n, &chip.cs_inactive, "spi-cs-inactive-delay-ns");
+
+	reg = of_find_property(n, "reg", NULL);
+	if (!reg)
+		return;
+	chip.chip_select = of_read_number(reg->value, 1);
+	chip.device_node = n;
+	spi_new_device(ctrl, &chip);
+}
+
 static void spi_of_register_slaves(struct spi_controller *ctrl)
 {
 	struct device_node *n;
-	struct property *reg;
 	struct device_node *node = ctrl->dev->of_node;
 
 	if (!IS_ENABLED(CONFIG_OFDEVICE))
@@ -162,41 +200,8 @@ static void spi_of_register_slaves(struct spi_controller *ctrl)
 	if (!node)
 		return;
 
-	for_each_available_child_of_node(node, n) {
-		struct spi_board_info chip = {};
-
-		if (n->dev) {
-			dev_dbg(ctrl->dev, "skipping already registered %s\n",
-				dev_name(n->dev));
-			continue;
-		}
-
-		chip.name = xstrdup(n->name);
-		chip.bus_num = ctrl->bus_num;
-		/* Mode (clock phase/polarity/etc.) */
-		if (of_property_read_bool(n, "spi-cpha"))
-			chip.mode |= SPI_CPHA;
-		if (of_property_read_bool(n, "spi-cpol"))
-			chip.mode |= SPI_CPOL;
-		if (of_property_read_bool(n, "spi-cs-high"))
-			chip.mode |= SPI_CS_HIGH;
-		if (of_property_read_bool(n, "spi-3wire"))
-			chip.mode |= SPI_3WIRE;
-		of_property_read_u32(n, "spi-max-frequency",
-				&chip.max_speed_hz);
-
-		/* Device CS delays */
-		of_spi_parse_dt_cs_delay(n, &chip.cs_setup, "spi-cs-setup-delay-ns");
-		of_spi_parse_dt_cs_delay(n, &chip.cs_hold, "spi-cs-hold-delay-ns");
-		of_spi_parse_dt_cs_delay(n, &chip.cs_inactive, "spi-cs-inactive-delay-ns");
-
-		reg = of_find_property(n, "reg", NULL);
-		if (!reg)
-			continue;
-		chip.chip_select = of_read_number(reg->value, 1);
-		chip.device_node = n;
-		spi_new_device(ctrl, &chip);
-	}
+	for_each_available_child_of_node(node, n)
+		spi_of_register_slave(ctrl, n);
 }
 
 static void spi_controller_rescan(struct device *dev, struct device_node *np)
-- 
2.47.3




^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 6/6] spi: allow creating siblings on demand while populating the bus
  2026-11-19 18:58 [PATCH 1/6] spi: fix NULL pointer dereference for board info devices Ahmad Fatoum
                   ` (3 preceding siblings ...)
  2026-11-19 18:58 ` [PATCH 5/6] spi: factor out spi_of_register_slave() Ahmad Fatoum
@ 2026-11-19 18:58 ` Ahmad Fatoum
  4 siblings, 0 replies; 7+ messages in thread
From: Ahmad Fatoum @ 2026-11-19 18:58 UTC (permalink / raw)
  To: barebox; +Cc: Alexander Shiyan, Ahmad Fatoum

SPI orders things like I2C: the rescan hook is installed only after the
controller registered its devices, and a device's node only gets its
device after probe. A device that needs a later sibling while probing
thus gets that sibling created as a platform device instead of an SPI
device.

Fix it the same way as for I2C, including that the nested rescan only
registers the requested node.

Fixes: 2366fa3a808e ("spi: implement rescan")
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
 drivers/spi/spi.c | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 850ea2c7b62f..15b654b750db 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -121,12 +121,15 @@ struct spi_device *spi_new_device(struct spi_controller *ctrl,
 	if (status)
 		goto fail;
 
-	status = register_device(&proxy->dev);
-	if (status)
-		goto fail;
-
+	/* claim the node before probe can rescan the bus */
 	of_node_set_dev(chip->device_node, &proxy->dev);
 
+	status = register_device(&proxy->dev);
+	if (status) {
+		of_node_set_dev(chip->device_node, NULL);
+		goto fail;
+	}
+
 	return proxy;
 fail:
 	free(proxy);
@@ -211,7 +214,12 @@ static void spi_controller_rescan(struct device *dev, struct device_node *np)
 	list_for_each_entry(ctrl, &spi_controller_list, list) {
 		if (ctrl->dev != dev)
 			continue;
-		spi_of_register_slaves(ctrl);
+
+		if (!np)
+			spi_of_register_slaves(ctrl);
+		else if (np->parent == ctrl->dev->of_node &&
+			 of_device_is_available(np))
+			spi_of_register_slave(ctrl, np);
 	}
 }
 
@@ -579,16 +587,16 @@ int spi_register_controller(struct spi_controller *ctrl)
 
 	list_add_tail(&ctrl->list, &spi_controller_list);
 
+	/* children probed below may need their siblings created on demand */
+	if (!ctrl->dev->rescan)
+		ctrl->dev->rescan = spi_controller_rescan;
+
 	spi_of_register_slaves(ctrl);
 
 	/* populate children from any spi device tables */
 	scan_boardinfo(ctrl);
-	status = 0;
 
-	if (!ctrl->dev->rescan)
-		ctrl->dev->rescan = spi_controller_rescan;
-
-	return status;
+	return 0;
 }
 EXPORT_SYMBOL(spi_register_controller);
 
-- 
2.47.3




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 4/6] i2c: allow creating siblings on demand while populating the bus
  2026-11-19 18:58 ` [PATCH 4/6] i2c: allow creating siblings on demand while populating the bus Ahmad Fatoum
@ 2026-11-20 11:04   ` Alexander Shiyan
  0 siblings, 0 replies; 7+ messages in thread
From: Alexander Shiyan @ 2026-11-20 11:04 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

Tested-by: Alexander Shiyan <eagle.alexander923@gmail.com>

чт, 19 нояб. 2026 г. в 21:58, Ahmad Fatoum <a.fatoum@barebox.org>:
>
> With deep probe, a device probed while its adapter populates the bus
> may depend on a sibling that comes later in the device tree, e.g. a
> GPIO expander whose vcc-supply is a PMIC regulator. To create the PMIC,
> of_device_create_on_demand() rescans the controller, but the rescan
> hook is only installed after population. So the PMIC node gets a
> platform device without a driver instead, the bus then skips it as
> already registered and the PMIC never probes.
>
> Install the hook before populating the bus. Alone, that makes the
> nested rescan register the expander again, recursing until the stack
> overflows, because its node only gets its device after probe. So set
> it before registering the client, like platform devices already do.
>
> The nested rescan must also register only the requested node.
> Otherwise, all later siblings are probed while the expander is still
> probing, and one using the expander's GPIOs fails to get them.
>
> Reported-by: Alexander Shiyan <eagle.alexander923@gmail.com>
> Link: https://lore.barebox.org/CAP1tNvRtU8munqnUTiqU_zEP_rxdm_hb42u_oU1FVL93czrtFA@mail.gmail.com
> Fixes: 7d8f15b36489 ("i2c: implement rescan")
> Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
> ---
>  drivers/i2c/i2c.c | 25 ++++++++++++++++---------
>  1 file changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c
> index ef92854eefe0..d0f9be826025 100644
> --- a/drivers/i2c/i2c.c
> +++ b/drivers/i2c/i2c.c
> @@ -405,16 +405,17 @@ static struct i2c_client *i2c_new_device(struct i2c_adapter *adapter,
>
>         client->dev.parent = &adapter->dev;
>
> +       /* claim the node before probe can rescan the bus */
> +       of_node_set_dev(chip->of_node, &client->dev);
> +
>         status = register_device(&client->dev);
>         if (status) {
> +               of_node_set_dev(chip->of_node, NULL);
>                 free(client);
>                 return NULL;
>         }
>         devinfo_add(&client->dev, i2c_info);
>
> -       if (chip->of_node)
> -               chip->of_node->dev = &client->dev;
> -
>         dev_dbg(&client->dev, "registered on bus %d, chip->addr 0x%02x\n",
>                 adapter->nr, client->addr);
>
> @@ -496,7 +497,12 @@ static void i2c_hw_rescan(struct device *dev, struct device_node *np)
>         for_each_i2c_adapter(adap) {
>                 if (dev != adap->dev.parent)
>                         continue;
> -               of_i2c_register_devices(adap);
> +
> +               if (!np)
> +                       of_i2c_register_devices(adap);
> +               else if (np->parent == adap->dev.of_node &&
> +                        of_device_is_available(np))
> +                       of_i2c_register_device(adap, np);
>                 break;
>         }
>  }
> @@ -742,17 +748,18 @@ int i2c_add_numbered_adapter(struct i2c_adapter *adapter)
>
>         slice_init(&adapter->slice, dev_name(&adapter->dev));
>
> -       /* populate children from any i2c device tables */
> -       scan_boardinfo(adapter);
> -
> -       of_i2c_register_devices(adapter);
> -
> +       /* children probed below may need their siblings created on demand */
>         hw_dev = adapter->dev.parent;
>         if (hw_dev && dev_of_node(hw_dev)) {
>                 if (!hw_dev->rescan)
>                         hw_dev->rescan = i2c_hw_rescan;
>         }
>
> +       /* populate children from any i2c device tables */
> +       scan_boardinfo(adapter);
> +
> +       of_i2c_register_devices(adapter);
> +
>         return 0;
>  }
>  EXPORT_SYMBOL(i2c_add_numbered_adapter);
> --
> 2.47.3
>
>



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2026-11-20 12:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 18:58 [PATCH 1/6] spi: fix NULL pointer dereference for board info devices Ahmad Fatoum
2026-11-19 18:58 ` [PATCH 2/6] driver: pass the requested node to the rescan hook Ahmad Fatoum
2026-11-19 18:58 ` [PATCH 3/6] i2c: factor out of_i2c_register_device() Ahmad Fatoum
2026-11-19 18:58 ` [PATCH 4/6] i2c: allow creating siblings on demand while populating the bus Ahmad Fatoum
2026-11-20 11:04   ` Alexander Shiyan
2026-11-19 18:58 ` [PATCH 5/6] spi: factor out spi_of_register_slave() Ahmad Fatoum
2026-11-19 18:58 ` [PATCH 6/6] spi: allow creating siblings on demand while populating the bus Ahmad Fatoum

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox