i2c controllers probe the devices on their bus themselves, so in the case the i2c controller was already probed, we currently have no way to have it reread the device tree after fixing it up or applying an overlay. Add a new helper that retriggers the device creation. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- drivers/i2c/i2c.c | 20 ++++++++++++++++++++ include/i2c/i2c.h | 1 + 2 files changed, 21 insertions(+) diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c index b24fd88f5b39..e70e51618850 100644 --- a/drivers/i2c/i2c.c +++ b/drivers/i2c/i2c.c @@ -427,6 +427,12 @@ static void of_i2c_register_devices(struct i2c_adapter *adap) 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; @@ -452,6 +458,20 @@ static void of_i2c_register_devices(struct i2c_adapter *adap) } } +int of_i2c_register_devices_by_node(struct device_node *node) +{ + struct i2c_adapter *adap; + + adap = of_find_i2c_adapter_by_node(node); + if (!adap) + return -ENODEV; + if (IS_ERR(adap)) + return PTR_ERR(adap); + + of_i2c_register_devices(adap); + return 0; +} + /** * i2c_new_dummy - return a new i2c device bound to a dummy driver * @adapter: the adapter managing the device diff --git a/include/i2c/i2c.h b/include/i2c/i2c.h index 7207b1180e1d..f5e2dc511ed1 100644 --- a/include/i2c/i2c.h +++ b/include/i2c/i2c.h @@ -295,6 +295,7 @@ static inline int i2c_register_board_info(int busnum, extern int i2c_add_numbered_adapter(struct i2c_adapter *adapter); struct i2c_adapter *i2c_get_adapter(int busnum); struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node); +int of_i2c_register_devices_by_node(struct device_node *node); struct i2c_client *of_find_i2c_device_by_node(struct device_node *node); void i2c_parse_fw_timings(struct device_d *dev, struct i2c_timings *t, bool use_defaults); -- 2.30.2
This new helper is the i2c equivalent of the existing of_device_enable_and_register_by_alias(). That function isn't applicable to i2c devices as it would create a platform device, but we need the controller to create an i2c device instead. This function was implemented for board code that uses an I2C EEPROM to determine whether a specific I2C device is available on the same bus. As reading the EEPROM requires the i2c controller be probed, there was no way to reprobe the i2c controller children to create a device for the previously disabled node. With of_i2c_register_devices_by_node(), this is now possible and this helper makes it easier to use from board code. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> --- drivers/i2c/i2c.c | 13 +++++++++++++ include/i2c/i2c.h | 1 + 2 files changed, 14 insertions(+) diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c index e70e51618850..40590b7d11be 100644 --- a/drivers/i2c/i2c.c +++ b/drivers/i2c/i2c.c @@ -597,6 +597,19 @@ struct i2c_client *of_find_i2c_device_by_node(struct device_node *node) return to_i2c_client(dev); } +int of_i2c_device_enable_and_register_by_alias(const char *alias) +{ + struct device_node *np; + + np = of_find_node_by_alias(NULL, alias); + if (!np) + return -ENODEV; + + of_device_enable(np); + return of_i2c_register_devices_by_node(np->parent); +} + + static void i2c_parse_timing(struct device_d *dev, char *prop_name, u32 *cur_val_p, u32 def_val, bool use_def) { diff --git a/include/i2c/i2c.h b/include/i2c/i2c.h index f5e2dc511ed1..e37a1770dc1e 100644 --- a/include/i2c/i2c.h +++ b/include/i2c/i2c.h @@ -297,6 +297,7 @@ struct i2c_adapter *i2c_get_adapter(int busnum); struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node); int of_i2c_register_devices_by_node(struct device_node *node); struct i2c_client *of_find_i2c_device_by_node(struct device_node *node); +int of_i2c_device_enable_and_register_by_alias(const char *alias); void i2c_parse_fw_timings(struct device_d *dev, struct i2c_timings *t, bool use_defaults); -- 2.30.2
On Mon, Sep 05, 2022 at 12:36:12PM +0200, Ahmad Fatoum wrote: > i2c controllers probe the devices on their bus themselves, so in the > case the i2c controller was already probed, we currently have no way to > have it reread the device tree after fixing it up or applying an > overlay. Add a new helper that retriggers the device creation. > > Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de> > --- > drivers/i2c/i2c.c | 20 ++++++++++++++++++++ > include/i2c/i2c.h | 1 + > 2 files changed, 21 insertions(+) Applied, thanks Sascha > > diff --git a/drivers/i2c/i2c.c b/drivers/i2c/i2c.c > index b24fd88f5b39..e70e51618850 100644 > --- a/drivers/i2c/i2c.c > +++ b/drivers/i2c/i2c.c > @@ -427,6 +427,12 @@ static void of_i2c_register_devices(struct i2c_adapter *adap) > 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; > @@ -452,6 +458,20 @@ static void of_i2c_register_devices(struct i2c_adapter *adap) > } > } > > +int of_i2c_register_devices_by_node(struct device_node *node) > +{ > + struct i2c_adapter *adap; > + > + adap = of_find_i2c_adapter_by_node(node); > + if (!adap) > + return -ENODEV; > + if (IS_ERR(adap)) > + return PTR_ERR(adap); > + > + of_i2c_register_devices(adap); > + return 0; > +} > + > /** > * i2c_new_dummy - return a new i2c device bound to a dummy driver > * @adapter: the adapter managing the device > diff --git a/include/i2c/i2c.h b/include/i2c/i2c.h > index 7207b1180e1d..f5e2dc511ed1 100644 > --- a/include/i2c/i2c.h > +++ b/include/i2c/i2c.h > @@ -295,6 +295,7 @@ static inline int i2c_register_board_info(int busnum, > extern int i2c_add_numbered_adapter(struct i2c_adapter *adapter); > struct i2c_adapter *i2c_get_adapter(int busnum); > struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node); > +int of_i2c_register_devices_by_node(struct device_node *node); > struct i2c_client *of_find_i2c_device_by_node(struct device_node *node); > > void i2c_parse_fw_timings(struct device_d *dev, struct i2c_timings *t, bool use_defaults); > -- > 2.30.2 > > > -- Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |