Skip to contentSkip to conversation

i2c: core: Introduce i2c_client_get_device_id helper

RFC PATCH Superseded Angel Iglesias 5 participants 13 messages Oct 30, 2022 – Nov 6, 2022

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover i2c: core: Introduce i2c_client_get_device_id helper

Hello,

I don't want to step anyone's work here, so I'm sending this RFC to the devs involved in the original discussion. I read on Uwe Kleine-König's patchset submission thread the necessity for an i2c helper to aid with the migration to the new i2c_driver .probe_new callback. Following the suggestions made there, I wrote this small patchset implementing the suggested helper function and ported the bmp280 IIO i2c probe to the new probe using that helper.

Thanks for your time!
Angel

Original discussion thread for additional context:
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 |  8 ++++----
 include/linux/i2c.h               |  1 +
 3 files changed, 20 insertions(+), 4 deletions(-)

base-commit: c32793afc6976e170f6ab11ca3750fe94fb3454d

Signature · 1 line
2.38.1
#1 lore raw

RFC PATCH 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 +++++++++++++++
 include/linux/i2c.h         |  1 +
 2 files changed, 16 insertions(+)
drivers/i2c/i2c-core-base.c +15 -0
index b4edf10e8fd0..e6623226e393 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
+ *
+ * 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
#2 lore raw

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

Quoted 5 lines · Angel Iglesias

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 7 lines

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

Looks good to me!

#3 lore raw

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

In reply to Angel Iglesias
Quoted 27 lines · Angel Iglesias

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 +++++++++++++++
 include/linux/i2c.h         |  1 +
 2 files changed, 16 insertions(+)
drivers/i2c/i2c-core-base.c +2 -0
index b4edf10e8fd0..e6623226e393 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

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

Quoted 10 lines
drivers/i2c/i2c-core-base.c +10 -0
+ * @client: the device to query
+ *
+ * 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);
+

I'd drop the empty line after the return. Is it worth to note in a comment that it only works for bound clients? (Oopses otherwise)

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#4 lore raw

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

Quoted 36 lines · Uwe Kleine-König
Quoted 35 lines · Angel Iglesias

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.

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

---
 drivers/i2c/i2c-core-base.c | 15 +++++++++++++++
 include/linux/i2c.h         |  1 +

 2 files changed, 16 insertions(+)
drivers/i2c/i2c-core-base.c +0 -0
index b4edf10e8fd0..e6623226e393 100644
@@ -2236,6 +2236,21 @@ int i2c_get_device_id(const struct i2c_client
 *client,
  }
  EXPORT_SYMBOL_GPL(i2c_get_device_id);

drivers/i2c/i2c-core-base.c +7 -0
+/**
+ * i2c_client_get_device_id - get the driver match table entry of a device
+ * @client: the device to query
+ *
+ * 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)

Quoted 8 lines

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

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

I'd drop the empty line after the return. Is it worth to note in a comment that it only works for bound clients? (Oopses otherwise)

Yup, my bad, that newline shouldn't be there. Sure, seems reasonable to leave warn just in case.

Kind regards,
Angel

Quoted 3 lines

Best regards
Uwe

#5 lore raw

RFC PATCH 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 | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/pressure/bmp280-i2c.c +4 -4
index 0c27211f3ea0..20073b09b3e3 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 i2c_device_id *id = i2c_client_get_device_id(client);
 	const struct regmap_config *regmap_config;
+	struct regmap *regmap;
 
 	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
#6 lore raw

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

Hello,

Quoted 17 lines · 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 | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/pressure/bmp280-i2c.c +0 -1
index 0c27211f3ea0..20073b09b3e3 100644
@@ -5,11 +5,11 @@
 
 #include "bmp280.h"
 
-static int bmp280_i2c_probe(struct i2c_client *client,

drivers/iio/pressure/bmp280-i2c.c line 15

Quoted 10 lines
drivers/iio/pressure/bmp280-i2c.c +3 -2
-			    const struct i2c_device_id *id)
+static int bmp280_i2c_probe(struct i2c_client *client)
 {
-	struct regmap *regmap;
+	const struct i2c_device_id *id = i2c_client_get_device_id(client);
 	const struct regmap_config *regmap_config;
+	struct regmap *regmap;
 
 	switch (id->driver_data) {
 	case BMP180_CHIP_ID:

What is the motivation for moving regmap? I thought reverse christmas tree is only a thing in network code? I would have left the regmap declaration where it is.

drivers/iio/pressure/bmp280-i2c.c static struct i2c_driver bmp280_i2c_driver = { · line 71

Quoted 9 lines
drivers/iio/pressure/bmp280-i2c.c +1 -1
@@ -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);

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#7 lore raw

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

Quoted 27 lines · Uwe Kleine-König

Hello,

Quoted 24 lines · 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 | 8 ++++----

 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/pressure/bmp280-i2c.c +0 -0
index 0c27211f3ea0..20073b09b3e3 100644
@@ -5,11 +5,11 @@


 #include "bmp280.h"

drivers/iio/pressure/bmp280-i2c.c +1 -2
-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 i2c_device_id *id = i2c_client_get_device_id(client); const struct regmap_config *regmap_config;

Quoted 8 lines
Quoted 4 lines

+       struct regmap *regmap;

        switch (id->driver_data) {
        case BMP180_CHIP_ID:

What is the motivation for moving regmap? I thought reverse christmas tree is only a thing in network code? I would have left the regmap declaration where it is.

Long story short, I worked previously on a small refactor of this driver to add support for a new family of sensors. During the different iterations of the patchset, one thing that was agreed was unifying the driver coding style to reverse xmas tree. For some extra context, here's the thread:
https://lore.kernel.org/all/20220814145249.701f1261@jic23-huawei/

Quoted 4 lines
Quoted 4 lines
drivers/iio/pressure/bmp280-i2c.c +0 -0
@@ -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),
         },
Quoted 9 lines

drivers/iio/pressure/bmp280-i2c.c static struct i2c_driver bmp280_i2c_driver = { · line 71

Quoted 5 lines
drivers/iio/pressure/bmp280-i2c.c +1 -1
-       .probe          = bmp280_i2c_probe,
+       .probe_new      = bmp280_i2c_probe,
         .id_table       = bmp280_i2c_id,
  };
  module_i2c_driver(bmp280_i2c_driver);

Best regards
Uwe

Kind regards
Angel

maintainer
#8 lore raw

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

Quoted 37 lines · On Wed, 02 Nov 2022 01:16:44 +0100 Angel Iglesias <[email protected]>…
Quoted 27 lines · Uwe Kleine-König

Hello,

Quoted 24 lines · 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 | 8 ++++----

 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/iio/pressure/bmp280-i2c.c +0 -0
index 0c27211f3ea0..20073b09b3e3 100644
@@ -5,11 +5,11 @@


 #include "bmp280.h"

drivers/iio/pressure/bmp280-i2c.c +1 -2
-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 i2c_device_id *id = i2c_client_get_device_id(client); const struct regmap_config *regmap_config;

Quoted 8 lines
Quoted 4 lines

+       struct regmap *regmap;

        switch (id->driver_data) {
        case BMP180_CHIP_ID:

What is the motivation for moving regmap? I thought reverse christmas tree is only a thing in network code? I would have left the regmap declaration where it is.

Quoted 5 lines

Long story short, I worked previously on a small refactor of this driver to add support for a new family of sensors. During the different iterations of the patchset, one thing that was agreed was unifying the driver coding style to reverse xmas tree. For some extra context, here's the thread:
https://lore.kernel.org/all/20220814145249.701f1261@jic23-huawei/

Not something I feel strongly enough about either way, but has benefit of consistency. However, it's an unrelated change in this patch, so drop it to avoid the noise in a patch where you have more significant changes.

Jonathan

Quoted 16 lines
Quoted 14 lines

drivers/iio/pressure/bmp280-i2c.c static struct i2c_driver bmp280_i2c_driver = { · line 71

Quoted 10 lines
drivers/iio/pressure/bmp280-i2c.c +1 -1
@@ -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);  

Best regards
Uwe

Kind regards
Angel

Reviewed-by on the v1 series
#9 lore raw
In reply to Angel Iglesias
Quoted 9 lines · Angel Iglesias

Hello,

I don't want to step anyone's work here, so I'm sending this RFC to the devs involved in the original discussion. I read on Uwe Kleine-König's patchset submission thread the necessity for an i2c helper to aid with the migration to the new i2c_driver .probe_new callback. Following the suggestions made there, I wrote this small patchset implementing the suggested helper function and ported the bmp280 IIO i2c probe to the new probe using that helper.

For the entire series (please drop RFC in the next version)

Quoted 20 lines

Thanks for your time!
Angel

Original discussion thread for additional context:
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 |  8 ++++----
 include/linux/i2c.h               |  1 +
 3 files changed, 20 insertions(+), 4 deletions(-)

base-commit: c32793afc6976e170f6ab11ca3750fe94fb3454d

Signature · 1 line
2.38.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#10 lore raw
Quoted 11 lines · On Tue, 1 Nov 2022 16:58:13 +0200 Andy Shevchenko…
Quoted 9 lines · Angel Iglesias

Hello,

I don't want to step anyone's work here, so I'm sending this RFC to the devs involved in the original discussion. I read on Uwe Kleine-König's patchset submission thread the necessity for an i2c helper to aid with the migration to the new i2c_driver .probe_new callback. Following the suggestions made there, I wrote this small patchset implementing the suggested helper function and ported the bmp280 IIO i2c probe to the new probe using that helper.

Quoted 2 lines

For the entire series (please drop RFC in the next version)

I'm happy to pick up the next version but a question on 'route' in to the kernel.

I can do an immutable branch with just the new function call in it if that is useful given I assume this is applicable across a bunch of subsystems?

Jonathan

Quoted 22 lines
Quoted 21 lines

Thanks for your time!
Angel

Original discussion thread for additional context:
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 |  8 ++++----
 include/linux/i2c.h               |  1 +
 3 files changed, 20 insertions(+), 4 deletions(-)

base-commit: c32793afc6976e170f6ab11ca3750fe94fb3454d

Signature · 1 line
2.38.1
#11 lore raw
Quoted 3 lines

I can do an immutable branch with just the new function call in it if that is useful given I assume this is applicable across a bunch of subsystems?

I'd think I should provide the immutable branch with the new I2C API call. Feels a bit more logical. Will that work for you as well?

maintainer
#12 lore raw
Quoted 6 lines · On Sat, 5 Nov 2022 22:29:29 +0100 Wolfram Sang <[email protected]> wrote:
Quoted 2 lines

I can do an immutable branch with just the new function call in it if that is useful given I assume this is applicable across a bunch of subsystems?

I'd think I should provide the immutable branch with the new I2C API call. Feels a bit more logical. Will that work for you as well?

Absolutely. That's even better - I didn't want to assign you work to do :)

Jonathan