[PATCH 1/4] media: mc: Add devm_media_entity_pads_init() helper

From: Tarang Raval
Date: Wed Jul 23 2025 - 06:26:55 EST


Add a devm-managed version of media_entity_pads_init() to simplify
pad initialization and cleanup using devres.

Signed-off-by: Tarang Raval <tarang.raval@xxxxxxxxxxxxxxxxx>
---
drivers/media/mc/mc-entity.c | 19 +++++++++++++++++++
include/media/media-entity.h | 20 ++++++++++++++++++++
2 files changed, 39 insertions(+)

diff --git a/drivers/media/mc/mc-entity.c b/drivers/media/mc/mc-entity.c
index 045590905582..fe50da3faf08 100644
--- a/drivers/media/mc/mc-entity.c
+++ b/drivers/media/mc/mc-entity.c
@@ -8,6 +8,7 @@
* Sakari Ailus <sakari.ailus@xxxxxx>
*/

+#include <linux/device/devres.h>
#include <linux/bitmap.h>
#include <linux/list.h>
#include <linux/property.h>
@@ -235,6 +236,24 @@ int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
}
EXPORT_SYMBOL_GPL(media_entity_pads_init);

+static void devm_media_entity_cleanup(void *data)
+{
+ media_entity_cleanup(data);
+}
+
+int devm_media_entity_pads_init(struct device *dev, struct media_entity *entity,
+ u16 num_pads, struct media_pad *pads)
+{
+ int err;
+
+ err = media_entity_pads_init(entity, num_pads, pads);
+ if (err)
+ return err;
+
+ return devm_add_action_or_reset(dev, devm_media_entity_cleanup, entity);
+}
+EXPORT_SYMBOL_GPL(devm_media_entity_pads_init);
+
/* -----------------------------------------------------------------------------
* Graph traversal
*/
diff --git a/include/media/media-entity.h b/include/media/media-entity.h
index 64cf590b1134..28b904fe34ae 100644
--- a/include/media/media-entity.h
+++ b/include/media/media-entity.h
@@ -717,6 +717,26 @@ void media_gobj_destroy(struct media_gobj *gobj);
int media_entity_pads_init(struct media_entity *entity, u16 num_pads,
struct media_pad *pads);

+/**
+ * devm_media_entity_pads_init - Managed initialization of media entity pads
+ *
+ * @dev: Device that manages the lifecycle of the media entity.
+ * @entity: Entity where the pads belong.
+ * @num_pads: Total number of sink and source pads.
+ * @pads: Array of @num_pads pads.
+ *
+ * This function initializes the pads for the given media entity and registers
+ * a managed cleanup action to be performed automatically when the device is
+ * detached or the driver is unloaded.
+ *
+ * This is a managed version of media_entity_pads_init(), and simplifies resource
+ * management using devres.
+ *
+ * Return: 0 on success or a negative error code on failure.
+ */
+int devm_media_entity_pads_init(struct device *dev, struct media_entity *entity,
+ u16 num_pads, struct media_pad *pads);
+
/**
* media_entity_cleanup() - free resources associated with an entity
*
--
2.34.1