[PATCH 6/6] spi: allow creating siblings on demand while populating the bus

Ahmad Fatoum a.fatoum at barebox.org
Thu Nov 19 10:58:16 PST 2026


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 at 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




More information about the barebox mailing list