diff mbox series

[1/2] i2c: core: add devm_i2c_new_ancillary_device()

Message ID c7e39e5e-7c08-a66c-c2fb-b1bed470cf71@gmail.com
State New
Delegated to: Wolfram Sang
Headers show
Series i2c: core: add devm_i2c_new_ancillary_device() | expand

Commit Message

Heiner Kallweit Jan. 30, 2023, 9:22 p.m. UTC
This adds a device-managed version of i2c_new_ancillary_device().
Create a helper i2c_prepare_ancillary_device() to avoid code duplication.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/i2c/i2c-core-base.c | 43 ++++++++++++++++++++++++++-----------
 include/linux/i2c.h         |  5 +++++
 2 files changed, 36 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 4a72b98ac..315ecd960 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1123,6 +1123,24 @@  struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
 }
 EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
 
+static u32 i2c_prepare_ancillary_device(struct i2c_client *client,
+					const char *name, u16 default_addr)
+{
+	struct device_node *np = client->dev.of_node;
+	u32 addr = default_addr;
+	int i;
+
+	if (np) {
+		i = of_property_match_string(np, "reg-names", name);
+		if (i >= 0)
+			of_property_read_u32_index(np, "reg", i, &addr);
+	}
+
+	dev_dbg(&client->adapter->dev, "Address for %s : %#x\n", name, addr);
+
+	return addr;
+}
+
 /**
  * i2c_new_ancillary_device - Helper to get the instantiated secondary address
  * and create the associated device
@@ -1146,24 +1164,25 @@  EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
  * i2c_unregister_device(); or an ERR_PTR to describe the error.
  */
 struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
-						const char *name,
-						u16 default_addr)
+					    const char *name,
+					    u16 default_addr)
 {
-	struct device_node *np = client->dev.of_node;
-	u32 addr = default_addr;
-	int i;
-
-	if (np) {
-		i = of_property_match_string(np, "reg-names", name);
-		if (i >= 0)
-			of_property_read_u32_index(np, "reg", i, &addr);
-	}
+	u32 addr = i2c_prepare_ancillary_device(client, name, default_addr);
 
-	dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
 	return i2c_new_dummy_device(client->adapter, addr);
 }
 EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
 
+struct i2c_client *devm_i2c_new_ancillary_device(struct i2c_client *client,
+						 const char *name,
+						 u16 default_addr)
+{
+	u32 addr = i2c_prepare_ancillary_device(client, name, default_addr);
+
+	return devm_i2c_new_dummy_device(&client->dev, client->adapter, addr);
+}
+EXPORT_SYMBOL_GPL(devm_i2c_new_ancillary_device);
+
 /* ------------------------------------------------------------------------- */
 
 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
diff --git a/include/linux/i2c.h b/include/linux/i2c.h
index 500404d85..33710a5ff 100644
--- a/include/linux/i2c.h
+++ b/include/linux/i2c.h
@@ -487,6 +487,11 @@  i2c_new_ancillary_device(struct i2c_client *client,
 			 const char *name,
 			 u16 default_addr);
 
+struct i2c_client *
+devm_i2c_new_ancillary_device(struct i2c_client *client,
+			      const char *name,
+			      u16 default_addr);
+
 void i2c_unregister_device(struct i2c_client *client);
 
 struct i2c_client *i2c_verify_client(struct device *dev);