[PATCH 01/12] base: soc: Implement devm_soc_device_register() helper function

From: Andrew Davis

Date: Wed Nov 11 2026 - 13:13:42 EST


Add device managed devm_soc_device_register() to automatically unregister
soc_devices thus leading to simplified SOC driver code.

Signed-off-by: Andrew Davis <afd@xxxxxx>
---
drivers/base/soc.c | 23 +++++++++++++++++++++++
include/linux/sys_soc.h | 16 ++++++++++++++++
2 files changed, 39 insertions(+)

diff --git a/drivers/base/soc.c b/drivers/base/soc.c
index af7d71393774b..81a96e56fbc29 100644
--- a/drivers/base/soc.c
+++ b/drivers/base/soc.c
@@ -189,6 +189,29 @@ void soc_device_unregister(struct soc_device *soc_dev)
}
EXPORT_SYMBOL_GPL(soc_device_unregister);

+static void devm_soc_device_unregister(void *soc_dev)
+{
+ soc_device_unregister(soc_dev);
+}
+
+struct soc_device *devm_soc_device_register(struct device *dev,
+ struct soc_device_attribute *soc_dev_attr)
+{
+ struct soc_device *soc_dev;
+ int ret;
+
+ soc_dev = soc_device_register(soc_dev_attr);
+ if (IS_ERR_OR_NULL(soc_dev))
+ return soc_dev;
+
+ ret = devm_add_action_or_reset(dev, devm_soc_device_unregister, soc_dev);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return soc_dev;
+}
+EXPORT_SYMBOL_GPL(devm_soc_device_register);
+
static int __init soc_bus_register(void)
{
struct soc_device *soc_dev;
diff --git a/include/linux/sys_soc.h b/include/linux/sys_soc.h
index cedabf177f470..02b22add818b3 100644
--- a/include/linux/sys_soc.h
+++ b/include/linux/sys_soc.h
@@ -36,6 +36,22 @@ struct soc_device *soc_device_register(
*/
void soc_device_unregister(struct soc_device *soc_dev);

+/**
+ * devm_soc_device_register - Resource-managed soc_device_register()
+ * @dev: Device to register soc_dev for.
+ * @soc_plat_dev_attr: Attributes passed from platform to be attributed to a SoC
+ *
+ * Managed soc_device_register. soc_dev registered with this function is
+ * automatically unregistered on driver detach.
+ *
+ * Returns:
+ * - %NULL if the SoC bus is not yet registered;
+ * - on success, the newly allocated &struct soc_device pointer;
+ * - on failure, a negative error code as an ERR_PTR().
+ */
+struct soc_device *devm_soc_device_register(struct device *dev,
+ struct soc_device_attribute *soc_dev_attr);
+
/**
* soc_device_to_device - helper function to fetch struct device
* @soc: Previously registered SoC device container
--
2.43.0