[PATCH v2 1/2] drivers: core: platform: Add platform_get_match_data() helper
From: Fabio Forni via B4 Relay
Date: Fri Nov 27 2026 - 18:22:37 EST
From: Fabio Forni <development@xxxxxxxxxx>
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@xxxxxxxxxx>
---
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