All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] i2c: core: add devm_i2c_new_ancillary_device()
@ 2023-01-30 21:20 Heiner Kallweit
  2023-01-30 21:22 ` [PATCH 1/2] " Heiner Kallweit
  2023-01-30 21:24 ` [PATCH 2/2] i2c: core: extend use case for dflt addr in i2c_new_ancillary_device Heiner Kallweit
  0 siblings, 2 replies; 3+ messages in thread
From: Heiner Kallweit @ 2023-01-30 21:20 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c

Add a device-managed version of i2c_new_ancillary_device().

I have an own use case, but was surprised to find it on an old wish list.
https://elinux.org/images/b/b3/I2c_21st-ELCE-2019-Sang.pdf

Heiner Kallweit (2):
  i2c: core: add devm_i2c_new_ancillary_device()
  i2c: core: extend use case for dflt addr in i2c_new_ancillary_device

 drivers/i2c/i2c-core-base.c | 50 ++++++++++++++++++++++++++++---------
 include/linux/i2c.h         |  5 ++++
 2 files changed, 43 insertions(+), 12 deletions(-)

-- 
2.39.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] i2c: core: add devm_i2c_new_ancillary_device()
  2023-01-30 21:20 [PATCH 0/2] i2c: core: add devm_i2c_new_ancillary_device() Heiner Kallweit
@ 2023-01-30 21:22 ` Heiner Kallweit
  2023-01-30 21:24 ` [PATCH 2/2] i2c: core: extend use case for dflt addr in i2c_new_ancillary_device Heiner Kallweit
  1 sibling, 0 replies; 3+ messages in thread
From: Heiner Kallweit @ 2023-01-30 21:22 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c

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 --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);
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] i2c: core: extend use case for dflt addr in i2c_new_ancillary_device
  2023-01-30 21:20 [PATCH 0/2] i2c: core: add devm_i2c_new_ancillary_device() Heiner Kallweit
  2023-01-30 21:22 ` [PATCH 1/2] " Heiner Kallweit
@ 2023-01-30 21:24 ` Heiner Kallweit
  1 sibling, 0 replies; 3+ messages in thread
From: Heiner Kallweit @ 2023-01-30 21:24 UTC (permalink / raw)
  To: Wolfram Sang; +Cc: linux-i2c

Interpret default_addr 0 in a way that the caller doesn't want a default.
Note: i2c_new_dummy_device() would fail anyway, but it would leave an ugly
error message complaining about invalid address 0.

Signed-off-by: Heiner Kallweit <hkallweit1@gmail.com>
---
 drivers/i2c/i2c-core-base.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index 315ecd960..6ff245ef1 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -1146,7 +1146,8 @@ static u32 i2c_prepare_ancillary_device(struct i2c_client *client,
  * and create the associated device
  * @client: Handle to the primary client
  * @name: Handle to specify which secondary address to get
- * @default_addr: Used as a fallback if no secondary address was specified
+ * @default_addr: Used as a fallback if no secondary address was specified.
+ *                Don't try to fall back if default_addr is zero.
  * Context: can sleep
  *
  * I2C clients can be composed of multiple I2C slaves bound together in a single
@@ -1169,6 +1170,9 @@ struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
 {
 	u32 addr = i2c_prepare_ancillary_device(client, name, default_addr);
 
+	if (!addr && !default_addr)
+		return ERR_PTR(-ENOENT);
+
 	return i2c_new_dummy_device(client->adapter, addr);
 }
 EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
@@ -1179,6 +1183,9 @@ struct i2c_client *devm_i2c_new_ancillary_device(struct i2c_client *client,
 {
 	u32 addr = i2c_prepare_ancillary_device(client, name, default_addr);
 
+	if (!addr && !default_addr)
+		return ERR_PTR(-ENOENT);
+
 	return devm_i2c_new_dummy_device(&client->dev, client->adapter, addr);
 }
 EXPORT_SYMBOL_GPL(devm_i2c_new_ancillary_device);
-- 
2.39.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-01-30 21:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-30 21:20 [PATCH 0/2] i2c: core: add devm_i2c_new_ancillary_device() Heiner Kallweit
2023-01-30 21:22 ` [PATCH 1/2] " Heiner Kallweit
2023-01-30 21:24 ` [PATCH 2/2] i2c: core: extend use case for dflt addr in i2c_new_ancillary_device Heiner Kallweit

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.