mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] iio: afe: rescale: Get driver data using platform_get_match_data()
@ 2026-11-27 23:22 Fabio Forni via B4 Relay
  2026-11-27 23:22 ` [PATCH v2 1/2] drivers: core: platform: Add platform_get_match_data() helper Fabio Forni via B4 Relay
  2026-11-27 23:22 ` [PATCH v2 2/2] iio: afe: iio-rescale: Add platform IDs Fabio Forni via B4 Relay
  0 siblings, 2 replies; 5+ messages in thread
From: Fabio Forni via B4 Relay @ 2026-11-27 23:22 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Peter Rosin, Jonathan Cameron, David Lechner,
	Nuno Sá,
	Andy Shevchenko
  Cc: driver-core, linux-kernel, linux-iio, Fabio Forni

Add a new helper to get data when drivers are bound using platform_device
plus swnodes, that typically happpens when such drivers are dynamically
instantiated.

As swnodes do not implement device_get_match_data(), a fallback mechanism
is needed to access driver_data inside platform_device_id structs. While
drivers can have such logic locally, Andy Shevchenko suggested to add a
global helper for this situation. See discussion in v1. 

v1 was about adding the logic above in iio-rescale: the same goal applies
to this revision. iio-rescale is modified to call the new
platform_get_match_data() helper.

Signed-off-by: Fabio Forni <development@redaril.me>
---
Changes in v2:
- Add platform_get_match_data()
- Use platform_get_match_data() in iio-rescale
- Use imperative mood throughout commits and cover letter
- Link to v1: https://lore.kernel.org/r/20261008-iio_rescale_platform_ids-v1-1-019f1c144022@redaril.me

---
Fabio Forni (2):
      drivers: core: platform: Add platform_get_match_data() helper
      iio: afe: iio-rescale: Add platform IDs

 drivers/base/platform.c         | 28 ++++++++++++++++++++++++++++
 drivers/iio/afe/iio-rescale.c   | 18 +++++++++++++++++-
 include/linux/platform_device.h |  2 ++
 3 files changed, 47 insertions(+), 1 deletion(-)
---
base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5
change-id: 20261008-iio_rescale_platform_ids-0d2a7dac974a

Best regards,
-- 
Fabio Forni <development@redaril.me>



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

* [PATCH v2 1/2] drivers: core: platform: Add platform_get_match_data() helper
  2026-11-27 23:22 [PATCH v2 0/2] iio: afe: rescale: Get driver data using platform_get_match_data() Fabio Forni via B4 Relay
@ 2026-11-27 23:22 ` Fabio Forni via B4 Relay
  2026-11-28  0:27   ` Jonathan Cameron
  2026-11-27 23:22 ` [PATCH v2 2/2] iio: afe: iio-rescale: Add platform IDs Fabio Forni via B4 Relay
  1 sibling, 1 reply; 5+ messages in thread
From: Fabio Forni via B4 Relay @ 2026-11-27 23:22 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Peter Rosin, Jonathan Cameron, David Lechner,
	Nuno Sá,
	Andy Shevchenko
  Cc: driver-core, linux-kernel, linux-iio, Fabio Forni

From: Fabio Forni <development@redaril.me>

device_get_match_data() retrieves driver data through the device's
fwnode. However, software nodes don't implement
fwnode->device_get_match_data(), so for a platform device that is
dynamically instantiated, device_get_match_data() is of no use.
platform_get_device_id() can be used instead, but this doesn't make the
driver code abstract enough.

Add the platform_get_match_data() helper, so that driver data can be
retrieved from platform_device's fwnode *or* device_id as a fallback.

Signed-off-by: Fabio Forni <development@redaril.me>
---
 drivers/base/platform.c         | 28 ++++++++++++++++++++++++++++
 include/linux/platform_device.h |  2 ++
 2 files changed, 30 insertions(+)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 3593d45f4a7a..7db56ffbb6b1 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -1090,6 +1090,34 @@ int __init_or_module __platform_driver_probe(struct platform_driver *drv,
 }
 EXPORT_SYMBOL_GPL(__platform_driver_probe);
 
+/**
+ * platform_get_match_data - get match data from fwnode or device ID
+ * @pdev: platform device to get data of
+ *
+ * Get match data from fwnode if available, otherwise get match data
+ * from pdev's platform_device_id->driver_data. driver_data is
+ * cast into a void* pointer.
+ *
+ * Return: -ENODATA if no match data is available.
+ */
+const void *platform_get_match_data(const struct platform_device *pdev)
+{
+	const struct platform_device_id *id;
+	const void *ret;
+
+	ret = device_get_match_data(&pdev->dev);
+	if (ret)
+		return ret;
+
+	id = platform_get_device_id(pdev);
+	if (id && id->driver_data)
+		return (const void *)id->driver_data;
+
+	return ERR_PTR(dev_err_probe(&pdev->dev, -ENODATA,
+				     "no match data available"));
+}
+EXPORT_SYMBOL_GPL(platform_get_match_data);
+
 /**
  * __platform_create_bundle - register driver and create corresponding device
  * @driver: platform driver structure
diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
index 3bd0f1f0d870..426148768b38 100644
--- a/include/linux/platform_device.h
+++ b/include/linux/platform_device.h
@@ -326,6 +326,8 @@ static inline void platform_set_drvdata(struct platform_device *pdev,
 	dev_set_drvdata(&pdev->dev, data);
 }
 
+const void *platform_get_match_data(const struct platform_device *pdev);
+
 /* module_platform_driver() - Helper macro for drivers that don't do
  * anything special in module init/exit.  This eliminates a lot of
  * boilerplate.  Each module may only use this macro once, and

-- 
2.56.1



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

* [PATCH v2 2/2] iio: afe: iio-rescale: Add platform IDs
  2026-11-27 23:22 [PATCH v2 0/2] iio: afe: rescale: Get driver data using platform_get_match_data() Fabio Forni via B4 Relay
  2026-11-27 23:22 ` [PATCH v2 1/2] drivers: core: platform: Add platform_get_match_data() helper Fabio Forni via B4 Relay
@ 2026-11-27 23:22 ` Fabio Forni via B4 Relay
  2026-11-28  0:30   ` Jonathan Cameron
  1 sibling, 1 reply; 5+ messages in thread
From: Fabio Forni via B4 Relay @ 2026-11-27 23:22 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J. Wysocki, Danilo Krummrich,
	Johan Hovold, Peter Rosin, Jonathan Cameron, David Lechner,
	Nuno Sá,
	Andy Shevchenko
  Cc: driver-core, linux-kernel, linux-iio, Fabio Forni

From: Fabio Forni <development@redaril.me>

When a rescaler is dynamically instantiated and configured with swnode,
device_get_match_data() will always set rescale->cfg to NULL, because
device_get_match_data() is unimplemented for software nodes.

Define a platform_device_id table (a replica of the of_device_id table)
and call the platform_get_match_data() helper, to deal with both
swnode-based and non-swnode-based instantiation methods.

Signed-off-by: Fabio Forni <development@redaril.me>
---
 drivers/iio/afe/iio-rescale.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
index c01292a15a29..c268dbaaaea6 100644
--- a/drivers/iio/afe/iio-rescale.c
+++ b/drivers/iio/afe/iio-rescale.c
@@ -518,6 +518,21 @@ static const struct of_device_id rescale_match[] = {
 };
 MODULE_DEVICE_TABLE(of, rescale_match);
 
+static const struct platform_device_id rescale_platform_ids[] = {
+	{ .name = "current-sense-amplifier",
+	  .driver_data = (kernel_ulong_t)&rescale_cfg[CURRENT_SENSE_AMPLIFIER] },
+	{ .name = "current-sense-shunt",
+	  .driver_data = (kernel_ulong_t)&rescale_cfg[CURRENT_SENSE_SHUNT] },
+	{ .name = "voltage-divider",
+	  .driver_data = (kernel_ulong_t)&rescale_cfg[VOLTAGE_DIVIDER] },
+	{ .name = "temperature-sense-rtd",
+	  .driver_data = (kernel_ulong_t)&rescale_cfg[TEMP_SENSE_RTD] },
+	{ .name = "temperature-transducer",
+	  .driver_data = (kernel_ulong_t)&rescale_cfg[TEMP_TRANSDUCER] },
+	{ }
+};
+MODULE_DEVICE_TABLE(platform, rescale_platform_ids);
+
 static int rescale_probe(struct platform_device *pdev)
 {
 	struct device *dev = &pdev->dev;
@@ -548,7 +563,7 @@ static int rescale_probe(struct platform_device *pdev)
 
 	rescale = iio_priv(indio_dev);
 
-	rescale->cfg = device_get_match_data(dev);
+	rescale->cfg = platform_get_match_data(pdev);
 	rescale->numerator = 1;
 	rescale->denominator = 1;
 	rescale->offset = 0;
@@ -603,6 +618,7 @@ static struct platform_driver rescale_driver = {
 		.name = "iio-rescale",
 		.of_match_table = rescale_match,
 	},
+	.id_table = rescale_platform_ids,
 };
 module_platform_driver(rescale_driver);
 

-- 
2.56.1



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

* Re: [PATCH v2 1/2] drivers: core: platform: Add platform_get_match_data() helper
  2026-11-27 23:22 ` [PATCH v2 1/2] drivers: core: platform: Add platform_get_match_data() helper Fabio Forni via B4 Relay
@ 2026-11-28  0:27   ` Jonathan Cameron
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2026-11-28  0:27 UTC (permalink / raw)
  To: Fabio Forni via B4 Relay
  Cc: development, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, Johan Hovold, Peter Rosin, David Lechner,
	Nuno Sá,
	Andy Shevchenko, driver-core, linux-kernel, linux-iio

On Sat, 28 Nov 2026 00:22:09 +0100
Fabio Forni via B4 Relay <devnull+development.redaril.me@kernel.org> wrote:

> From: Fabio Forni <development@redaril.me>
> 
> device_get_match_data() retrieves driver data through the device's
> fwnode. However, software nodes don't implement
> fwnode->device_get_match_data(), so for a platform device that is
> dynamically instantiated, device_get_match_data() is of no use.
> platform_get_device_id() can be used instead, but this doesn't make the
> driver code abstract enough.
> 
> Add the platform_get_match_data() helper, so that driver data can be
> retrieved from platform_device's fwnode *or* device_id as a fallback.
I'd have added a reference to similar cases like i2c_get_match_data()
or spi_get_device_match_data()
and perhaps have followed the naming in there for the return variable.
Both call it match.

Other than that, LGTM.

Thanks,

Jonathan

> 
> Signed-off-by: Fabio Forni <development@redaril.me>
> ---
>  drivers/base/platform.c         | 28 ++++++++++++++++++++++++++++
>  include/linux/platform_device.h |  2 ++
>  2 files changed, 30 insertions(+)
> 
> diff --git a/drivers/base/platform.c b/drivers/base/platform.c
> index 3593d45f4a7a..7db56ffbb6b1 100644
> --- a/drivers/base/platform.c
> +++ b/drivers/base/platform.c
> @@ -1090,6 +1090,34 @@ int __init_or_module __platform_driver_probe(struct platform_driver *drv,
>  }
>  EXPORT_SYMBOL_GPL(__platform_driver_probe);
>  
> +/**
> + * platform_get_match_data - get match data from fwnode or device ID
> + * @pdev: platform device to get data of
> + *
> + * Get match data from fwnode if available, otherwise get match data
> + * from pdev's platform_device_id->driver_data. driver_data is
> + * cast into a void* pointer.
> + *
> + * Return: -ENODATA if no match data is available.
> + */
> +const void *platform_get_match_data(const struct platform_device *pdev)
> +{
> +	const struct platform_device_id *id;
> +	const void *ret;
To follow similar cases this would be named match.

> +
> +	ret = device_get_match_data(&pdev->dev);
> +	if (ret)
> +		return ret;
> +
> +	id = platform_get_device_id(pdev);
> +	if (id && id->driver_data)
> +		return (const void *)id->driver_data;
> +
> +	return ERR_PTR(dev_err_probe(&pdev->dev, -ENODATA,
> +				     "no match data available"));
> +}
> +EXPORT_SYMBOL_GPL(platform_get_match_data);
> +
>  /**
>   * __platform_create_bundle - register driver and create corresponding device
>   * @driver: platform driver structure
> diff --git a/include/linux/platform_device.h b/include/linux/platform_device.h
> index 3bd0f1f0d870..426148768b38 100644
> --- a/include/linux/platform_device.h
> +++ b/include/linux/platform_device.h
> @@ -326,6 +326,8 @@ static inline void platform_set_drvdata(struct platform_device *pdev,
>  	dev_set_drvdata(&pdev->dev, data);
>  }
>  
> +const void *platform_get_match_data(const struct platform_device *pdev);
> +
>  /* module_platform_driver() - Helper macro for drivers that don't do
>   * anything special in module init/exit.  This eliminates a lot of
>   * boilerplate.  Each module may only use this macro once, and
> 


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

* Re: [PATCH v2 2/2] iio: afe: iio-rescale: Add platform IDs
  2026-11-27 23:22 ` [PATCH v2 2/2] iio: afe: iio-rescale: Add platform IDs Fabio Forni via B4 Relay
@ 2026-11-28  0:30   ` Jonathan Cameron
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2026-11-28  0:30 UTC (permalink / raw)
  To: Fabio Forni via B4 Relay
  Cc: development, Greg Kroah-Hartman, Rafael J. Wysocki,
	Danilo Krummrich, Johan Hovold, Peter Rosin, David Lechner,
	Nuno Sá,
	Andy Shevchenko, driver-core, linux-kernel, linux-iio

On Sat, 28 Nov 2026 00:22:10 +0100
Fabio Forni via B4 Relay <devnull+development.redaril.me@kernel.org> wrote:

> From: Fabio Forni <development@redaril.me>
> 
> When a rescaler is dynamically instantiated and configured with swnode,
> device_get_match_data() will always set rescale->cfg to NULL, because
> device_get_match_data() is unimplemented for software nodes.
> 
> Define a platform_device_id table (a replica of the of_device_id table)
> and call the platform_get_match_data() helper, to deal with both
> swnode-based and non-swnode-based instantiation methods.
> 
> Signed-off-by: Fabio Forni <development@redaril.me>
> ---
>  drivers/iio/afe/iio-rescale.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
> index c01292a15a29..c268dbaaaea6 100644
> --- a/drivers/iio/afe/iio-rescale.c
> +++ b/drivers/iio/afe/iio-rescale.c
> @@ -518,6 +518,21 @@ static const struct of_device_id rescale_match[] = {
>  };
>  MODULE_DEVICE_TABLE(of, rescale_match);
>  
> +static const struct platform_device_id rescale_platform_ids[] = {
> +	{ .name = "current-sense-amplifier",
> +	  .driver_data = (kernel_ulong_t)&rescale_cfg[CURRENT_SENSE_AMPLIFIER] },
	{
		.name = ...
		.driver_data = ...
	}, {
		.name = ...
Or 
	},
	{

I don't mind on that bit, but use a few more lines to make it easier
to add additional fields in future and generally make it easier to read.


> +	{ .name = "current-sense-shunt",
> +	  .driver_data = (kernel_ulong_t)&rescale_cfg[CURRENT_SENSE_SHUNT] },
> +	{ .name = "voltage-divider",
> +	  .driver_data = (kernel_ulong_t)&rescale_cfg[VOLTAGE_DIVIDER] },
> +	{ .name = "temperature-sense-rtd",
> +	  .driver_data = (kernel_ulong_t)&rescale_cfg[TEMP_SENSE_RTD] },
> +	{ .name = "temperature-transducer",
> +	  .driver_data = (kernel_ulong_t)&rescale_cfg[TEMP_TRANSDUCER] },
> +	{ }
> +};
> +MODULE_DEVICE_TABLE(platform, rescale_platform_ids);
> +
>  static int rescale_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
> @@ -548,7 +563,7 @@ static int rescale_probe(struct platform_device *pdev)
>  
>  	rescale = iio_priv(indio_dev);
>  
> -	rescale->cfg = device_get_match_data(dev);
> +	rescale->cfg = platform_get_match_data(pdev);

Can we get a failure here?  Forcing a driver bind to a device not in the tables
tends to end up with NULL and it is common to just fail probe if that happens.

>  	rescale->numerator = 1;
>  	rescale->denominator = 1;
>  	rescale->offset = 0;
> @@ -603,6 +618,7 @@ static struct platform_driver rescale_driver = {
>  		.name = "iio-rescale",
>  		.of_match_table = rescale_match,
>  	},
> +	.id_table = rescale_platform_ids,
>  };
>  module_platform_driver(rescale_driver);
>  
> 


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

end of thread, other threads:[~2026-11-28  0:30 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 23:22 [PATCH v2 0/2] iio: afe: rescale: Get driver data using platform_get_match_data() Fabio Forni via B4 Relay
2026-11-27 23:22 ` [PATCH v2 1/2] drivers: core: platform: Add platform_get_match_data() helper Fabio Forni via B4 Relay
2026-11-28  0:27   ` Jonathan Cameron
2026-11-27 23:22 ` [PATCH v2 2/2] iio: afe: iio-rescale: Add platform IDs Fabio Forni via B4 Relay
2026-11-28  0:30   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®