Skip to contentSkip to conversation

i2c: core: Introduce i2c_client_get_device_id helper

PATCH Superseded Angel Iglesias 2 participants 6 messages Nov 6, 2022

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover i2c: core: Introduce i2c_client_get_device_id helper

Introduces a new i2c helper to query driver match tables and recover the identity of the bound device. This helper should help with the migration of existing drivers to use the new i2c_driver .probe_new callback, which does not provide the device id with the pointer to the driver data as the previous .probe callback.

As part of the patchset, migrated the bmp280 IIO driver to .new_probe callback using the new helper.

Changes from v1 --> v2:
* Add further clarification that the device queried needs to be bound to the driver.
* Drops extra newline after function return.
* Discard unrelated style changes.

Previous version:
https://lore.kernel.org/all/[email protected]/

Angel Iglesias (2):
i2c: core: Introduce i2c_client_get_device_id helper function
iio: pressure: bmp280: convert to i2c's .probe_new()

 drivers/i2c/i2c-core-base.c       | 15 +++++++++++++++
 drivers/iio/pressure/bmp280-i2c.c |  6 +++---
 include/linux/i2c.h               |  1 +
 3 files changed, 19 insertions(+), 3 deletions(-)

base-commit: e38fb57870172ed920c206e8d73e2639d83c8847

Signature · 1 line
2.38.1
#1 lore raw

PATCH v2 1/2 i2c: core: Introduce i2c_client_get_device_id helper function

Introduces new helper function to aid in .probe_new() refactors. In order to use existing i2c_get_device_id() on the probe callback, the device match table needs to be accessible in that function, which would require bigger refactors in some drivers using the deprecated .probe callback.

This issue was discussed in more detail in the IIO mailing list.

drivers/i2c/i2c-core-base.c +15 -0
index b4edf10e8fd0..920676e62c22 100644
@@ -2236,6 +2236,21 @@ int i2c_get_device_id(const struct i2c_client *client,
 }
 EXPORT_SYMBOL_GPL(i2c_get_device_id);
 
+/**
+ * i2c_client_get_device_id - get the driver match table entry of a device
+ * @client: the device to query. The device must be bound to a driver or
+ *	    the function oopses.
+ *
+ * Returns a pointer to the matching entry if found, NULL otherwise.
+ */
+const struct i2c_device_id *i2c_client_get_device_id(const struct i2c_client *client)
+{
+	const struct i2c_driver *drv = to_i2c_driver(client->dev.driver);
+
+	return i2c_match_id(drv->id_table, client);
+}
+EXPORT_SYMBOL_GPL(i2c_client_get_device_id);
+
 /* ----------------------------------------------------
  * the i2c address scanning function
  * Will not work for 10-bit addresses!
include/linux/i2c.h +1 -0
index f7c49bbdb8a1..d84e0e99f084 100644
@@ -189,6 +189,7 @@ s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
 					      u8 *values);
 int i2c_get_device_id(const struct i2c_client *client,
 		      struct i2c_device_identity *id);
+const struct i2c_device_id *i2c_client_get_device_id(const struct i2c_client *client);
 #endif /* I2C */
 
 /**
Signature · 1 line
2.38.1
#4 lore raw

PATCH v2 2/2 iio: pressure: bmp280: convert to i2c's .probe_new()

In reply to Angel Iglesias

Use i2c_client_get_device_id() to get the i2c_device_id* parameter in the .new_probe() callback.

drivers/iio/pressure/bmp280-i2c.c +3 -3
index 0c27211f3ea0..14eab086d24a 100644
@@ -5,11 +5,11 @@
 
 #include "bmp280.h"
 
-static int bmp280_i2c_probe(struct i2c_client *client,
-			    const struct i2c_device_id *id)
+static int bmp280_i2c_probe(struct i2c_client *client)
 {
 	struct regmap *regmap;
 	const struct regmap_config *regmap_config;
+	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 
 	switch (id->driver_data) {
 	case BMP180_CHIP_ID:
@@ -65,7 +65,7 @@ static struct i2c_driver bmp280_i2c_driver = {
 		.of_match_table = bmp280_of_i2c_match,
 		.pm = pm_ptr(&bmp280_dev_pm_ops),
 	},
-	.probe		= bmp280_i2c_probe,
+	.probe_new	= bmp280_i2c_probe,
 	.id_table	= bmp280_i2c_id,
 };
 module_i2c_driver(bmp280_i2c_driver);
Signature · 1 line
2.38.1
maintainer Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] i2c: core: Introduce i2c_client_get_device_id helper function

In reply to Angel Iglesias
Quoted 5 lines · On Sun, 6 Nov 2022 17:42:24 +0100 Angel Iglesias <[email protected]>…

Introduces new helper function to aid in .probe_new() refactors. In order to use existing i2c_get_device_id() on the probe callback, the device match table needs to be accessible in that function, which would require bigger refactors in some drivers using the deprecated .probe callback.

Quoted 8 lines

This issue was discussed in more detail in the IIO mailing list.

Trivial comment inline - otherwise lgtm

Thanks,

drivers/i2c/i2c-core-base.c int i2c_get_device_id(const struct i2c_client *client, · line 2242

Quoted 13 lines
drivers/i2c/i2c-core-base.c +4 -0
index b4edf10e8fd0..920676e62c22 100644
@@ -2236,6 +2236,21 @@ int i2c_get_device_id(const struct i2c_client *client,
 }
 EXPORT_SYMBOL_GPL(i2c_get_device_id);
 
+/**
+ * i2c_client_get_device_id - get the driver match table entry of a device
+ * @client: the device to query. The device must be bound to a driver or
+ *	    the function oopses.

Maybe don't state what happens otherwise if something changes to make it instead do something else unfortunate, this comment will need updating. "The device must be bound to a driver."

Quoted 26 lines
drivers/i2c/i2c-core-base.c +11 -0
+ *
+ * Returns a pointer to the matching entry if found, NULL otherwise.
+ */
+const struct i2c_device_id *i2c_client_get_device_id(const struct i2c_client *client)
+{
+	const struct i2c_driver *drv = to_i2c_driver(client->dev.driver);
+
+	return i2c_match_id(drv->id_table, client);
+}
+EXPORT_SYMBOL_GPL(i2c_client_get_device_id);
+
 /* ----------------------------------------------------
  * the i2c address scanning function
  * Will not work for 10-bit addresses!
include/linux/i2c.h +1 -0
index f7c49bbdb8a1..d84e0e99f084 100644
@@ -189,6 +189,7 @@ s32 i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client *client,
 					      u8 *values);
 int i2c_get_device_id(const struct i2c_client *client,
 		      struct i2c_device_identity *id);
+const struct i2c_device_id *i2c_client_get_device_id(const struct i2c_client *client);
 #endif /* I2C */
 
 /**
maintainer Acked-by on v2 2/2
#5 lore raw

Re: [PATCH v2 2/2] iio: pressure: bmp280: convert to i2c's .probe_new()

In reply to Angel Iglesias
Quoted 5 lines · On Sun, 6 Nov 2022 17:43:16 +0100 Angel Iglesias <[email protected]>…

Use i2c_client_get_device_id() to get the i2c_device_id* parameter in the .new_probe() callback.

I'm fine with Wolfram picking this up as an example of using the new code, or I can take it through IIO after merging Wolfram's immutable branch.

So for option 1:

Quoted 28 lines
drivers/iio/pressure/bmp280-i2c.c +3 -3
index 0c27211f3ea0..14eab086d24a 100644
@@ -5,11 +5,11 @@
 
 #include "bmp280.h"
 
-static int bmp280_i2c_probe(struct i2c_client *client,
-			    const struct i2c_device_id *id)
+static int bmp280_i2c_probe(struct i2c_client *client)
 {
 	struct regmap *regmap;
 	const struct regmap_config *regmap_config;
+	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 
 	switch (id->driver_data) {
 	case BMP180_CHIP_ID:
@@ -65,7 +65,7 @@ static struct i2c_driver bmp280_i2c_driver = {
 		.of_match_table = bmp280_of_i2c_match,
 		.pm = pm_ptr(&bmp280_dev_pm_ops),
 	},
-	.probe		= bmp280_i2c_probe,
+	.probe_new	= bmp280_i2c_probe,
 	.id_table	= bmp280_i2c_id,
 };
 module_i2c_driver(bmp280_i2c_driver);
#3 lore raw

Re: [PATCH v2 1/2] i2c: core: Introduce i2c_client_get_device_id helper function

In reply to Jonathan Cameron
Quoted 33 lines · Jonathan Cameron
Quoted 7 lines · On Sun,  6 Nov 2022 17:42:24 +0100 Angel Iglesias <[email protected]>…

Introduces new helper function to aid in .probe_new() refactors. In order to use existing i2c_get_device_id() on the probe callback, the device match table needs to be accessible in that function, which would require bigger refactors in some drivers using the deprecated .probe callback.

This issue was discussed in more detail in the IIO mailing list.

Quoted 7 lines

Link:
https://lore.kernel.org/all/[email protected]/

Trivial comment inline - otherwise lgtm

Thanks,

Quoted 10 lines
drivers/i2c/i2c-core-base.c +0 -0
index b4edf10e8fd0..920676e62c22 100644
@@ -2236,6 +2236,21 @@ int i2c_get_device_id(const struct i2c_client
 *client,
  }
  EXPORT_SYMBOL_GPL(i2c_get_device_id);

Quoted 8 lines
Quoted 4 lines

+/**
+ * i2c_client_get_device_id - get the driver match table entry of a device
+ * @client: the device to query. The device must be bound to a driver or
+ *         the function oopses.

Maybe don't state what happens otherwise if something changes to make it instead do something else unfortunate, this comment will need updating. "The device must be bound to a driver."

Sure, sorry for the trouble. I'll wait a bit for more comments and then send v3 fixed.

Thanks for your time,
Angel

Quoted 31 lines
Quoted 30 lines

+ *
+ * Returns a pointer to the matching entry if found, NULL otherwise.
+ */
+const struct i2c_device_id *i2c_client_get_device_id(const struct i2c_client *client)

drivers/i2c/i2c-core-base.c +7 -0
+{
+       const struct i2c_driver *drv = to_i2c_driver(client->dev.driver);
+
+       return i2c_match_id(drv->id_table, client);
+}
+EXPORT_SYMBOL_GPL(i2c_client_get_device_id);
+

 /* ----------------------------------------------------
  * the i2c address scanning function
  * Will not work for 10-bit addresses!

include/linux/i2c.h +1 -0
index f7c49bbdb8a1..d84e0e99f084 100644
@@ -189,6 +189,7 @@ s32 i2c_smbus_read_i2c_block_data_or_emulated(const
 struct i2c_client *client,
                                               u8 *values);
  int i2c_get_device_id(const struct i2c_client *client,
                       struct i2c_device_identity *id);
+const struct i2c_device_id *i2c_client_get_device_id(const struct
 i2c_client *client);
  #endif /* I2C */


 /**