22.03.2021 14:10, Yicong Yang пишет: > Some I2C controller drivers will only unregister the I2C > adapter in their .remove() callback, which can be done > by simply using a managed variant to add the I2C adapter. > > So add the managed functions for adding the I2C adapter. > > Signed-off-by: Yicong Yang <yangyicong@xxxxxxxxxxxxx> > --- > drivers/i2c/i2c-core-base.c | 39 +++++++++++++++++++++++++++++++++++++++ > include/linux/i2c.h | 1 + > 2 files changed, 40 insertions(+) > > diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c > index 63ebf72..61486dc 100644 > --- a/drivers/i2c/i2c-core-base.c > +++ b/drivers/i2c/i2c-core-base.c > @@ -1550,6 +1550,38 @@ int i2c_add_adapter(struct i2c_adapter *adapter) > } > EXPORT_SYMBOL(i2c_add_adapter); > > +static void devm_i2c_del_adapter(struct device *dev, void *ptr); > + > +/** > + * devm_i2c_add_adapter - device-managed variant of i2c_add_adapter() > + * @dev: managing device for adding this I2C adapter > + * @adapter: the adapter to add > + * Context: can sleep > + * > + * Add adapter with dynamic bus number, same with i2c_add_adapter() > + * but the adapter will be auto deleted on driver detach. > + */ > +int devm_i2c_add_adapter(struct device *dev, struct i2c_adapter *adapter) > +{ > + struct i2c_adapter **ptr; > + int ret; > + > + ptr = devres_alloc(devm_i2c_del_adapter, sizeof(*ptr), GFP_KERNEL); > + if (!ptr) > + return -ENOMEM; > + > + ret = i2c_add_adapter(adapter); > + if (!ret) { > + *ptr = adapter; > + devres_add(dev, ptr); > + } else { > + devres_free(ptr); > + } This could be simplified using devm_add_action_or_reset(). > + return ret; > +} > +EXPORT_SYMBOL(devm_i2c_add_adapter); EXPORT_SYMBOL_GPL