From: Heiner Kallweit <hkallwe...@gmail.com>

i2c_new_dummy is typically called from the probe function of the
driver for the primary i2c client. It requires calls to
i2c_unregister_device in the error path of the probe function and
in the remove function.
This can be simplified by introducing a device-managed version.

Note the changed error case return value type:
i2c_new_dummy returns NULL whilst devm_new_i2c_dummy returns an ERR_PTR.

Signed-off-by: Heiner Kallweit <hkallwe...@gmail.com>
[wsa: rename new functions to 'errptr' style and fix minor kdoc issues]
Signed-off-by: Wolfram Sang <wsa+rene...@sang-engineering.com>
---
 Documentation/driver-model/devres.txt |  3 +++
 drivers/i2c/i2c-core-base.c           | 44 +++++++++++++++++++++++++++++++++++
 include/linux/i2c.h                   |  3 +++
 3 files changed, 50 insertions(+)

diff --git a/Documentation/driver-model/devres.txt 
b/Documentation/driver-model/devres.txt
index b277cafce71e..e36dc8041857 100644
--- a/Documentation/driver-model/devres.txt
+++ b/Documentation/driver-model/devres.txt
@@ -266,6 +266,9 @@ GPIO
   devm_gpio_request_one()
   devm_gpio_free()
 
+I2C
+  devm_i2c_new_dummy_errptr()
+
 IIO
   devm_iio_device_alloc()
   devm_iio_device_free()
diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index bbca2e8bb019..7f20c6b8857f 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -921,6 +921,50 @@ struct i2c_client *i2c_new_dummy(struct i2c_adapter 
*adapter, u16 address)
 }
 EXPORT_SYMBOL_GPL(i2c_new_dummy);
 
+struct i2c_dummy_devres {
+       struct i2c_client *client;
+};
+
+static void devm_i2c_release_dummy(struct device *dev, void *res)
+{
+       struct i2c_dummy_devres *this = res;
+
+       i2c_unregister_device(this->client);
+}
+
+/**
+ * devm_i2c_new_dummy_errptr - return a new i2c device bound to a dummy driver
+ * @dev: device the managed resource is bound to
+ * @adapter: the adapter managing the device
+ * @address: seven bit address to be used
+ * Context: can sleep
+ *
+ * This is the device-managed version of @i2c_new_dummy. It returns the new i2c
+ * client or an ERR_PTR in case of an error.
+ */
+struct i2c_client *devm_i2c_new_dummy_errptr(struct device *dev,
+                                            struct i2c_adapter *adapter,
+                                            u16 address)
+{
+       struct i2c_dummy_devres *dr;
+       struct i2c_client *client;
+
+       dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL);
+       if (!dr)
+               return ERR_PTR(-ENOMEM);
+
+       client = i2c_new_dummy_errptr(adapter, address);
+       if (IS_ERR(client)) {
+               devres_free(dr);
+       } else {
+               dr->client = client;
+               devres_add(dev, dr);
+       }
+
+       return client;
+}
+EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_errptr);
+
 /**
  * i2c_new_secondary_device - Helper to get the instantiated secondary address
  * and create the associated device
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 383510b4f083..93075c2ad9dc 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -470,6 +470,9 @@ extern struct i2c_client *
 i2c_new_dummy(struct i2c_adapter *adap, u16 address);
 
 extern struct i2c_client *
+devm_i2c_new_dummy_errptr(struct device *dev, struct i2c_adapter *adap, u16 
address);
+
+extern struct i2c_client *
 i2c_new_secondary_device(struct i2c_client *client,
                                const char *name,
                                u16 default_addr);
-- 
2.11.0

Reply via email to