diff mbox series

[v5,RESEND,4/4] i2c: Extend i2c_device_get_match_data() to support i2c sysfs

Message ID 20230803103102.323987-5-biju.das.jz@bp.renesas.com (mailing list archive)
State Superseded
Delegated to: Geert Uytterhoeven
Headers show
Series Extend device_get_match_data() to struct bus_type | expand

Commit Message

Biju Das Aug. 3, 2023, 10:31 a.m. UTC
Extend i2c_device_get_match_data() to i2c sysfs interface for
retrieving match data from the match table.

Suggested-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Suggested-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Biju Das <biju.das.jz@bp.renesas.com>
---
v5:
 * Separated non-static function change as separate patch#3
 * Replaced 'dev->driver'->'drv'.
 * Replaced return value data->NULL to avoid (potentially) stale pointers,
   if there is no match.
v4:
 * split from patch #2
 * Added space after of_device_id for i2c_of_match_device_sysfs()
 * Added const parameter for struct i2c_client, to prevent overriding it's
   pointer.
 * Moved declaration from public i2c.h->i2c-core.h
---
 drivers/i2c/i2c-core-base.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

Comments

Andy Shevchenko Aug. 3, 2023, 12:10 p.m. UTC | #1
On Thu, Aug 03, 2023 at 11:31:02AM +0100, Biju Das wrote:
> Extend i2c_device_get_match_data() to i2c sysfs interface for
> retrieving match data from the match table.

...

> +	const void *data;

> -	return i2c_get_match_data_helper(client);
> +	data = i2c_get_match_data_helper(client);
> +	if (data)
> +		return data;

> +	return NULL;
>  }

These may be incorporated to the previous patch that introduces the helper.
This patch after that will look much better.
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c
index e738cfba9c3b..d543460e47c2 100644
--- a/drivers/i2c/i2c-core-base.c
+++ b/drivers/i2c/i2c-core-base.c
@@ -130,6 +130,7 @@  static const void *i2c_device_get_match_data(const struct device *dev)
 {
 	const struct device_driver *drv = dev->driver;
 	const struct i2c_client *client;
+	const void *data;
 
 	/*
 	 * It is not guaranteed that the function is always called on a device
@@ -144,7 +145,19 @@  static const void *i2c_device_get_match_data(const struct device *dev)
 	if (!client)
 		return NULL;
 
-	return i2c_get_match_data_helper(client);
+	data = i2c_get_match_data_helper(client);
+	if (data)
+		return data;
+
+	if (drv->of_match_table) {
+		const struct of_device_id *match;
+
+		match = i2c_of_match_device_sysfs(drv->of_match_table, client);
+		if (match)
+			return match->data;
+	}
+
+	return NULL;
 }
 
 const void *i2c_get_match_data(const struct i2c_client *client)