[PATCH 4/6] i2c: allow creating siblings on demand while populating the bus
Alexander Shiyan
eagle.alexander923 at gmail.com
Fri Nov 20 03:04:17 PST 2026
Tested-by: Alexander Shiyan <eagle.alexander923 at gmail.com>
чт, 19 нояб. 2026 г. в 21:58, Ahmad Fatoum <a.fatoum at 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 at 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 at 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
>
>
More information about the barebox
mailing list