drm/connector: Introduce drmm_connector_init
authorMaxime Ripard <maxime@cerno.tech>
Mon, 11 Jul 2022 17:38:39 +0000 (19:38 +0200)
committerMaxime Ripard <maxime@cerno.tech>
Wed, 13 Jul 2022 08:46:06 +0000 (10:46 +0200)
Unlike other DRM entities, there's no helper to create a DRM-managed
initialisation of a connector.

Let's create an helper to initialise a connector that would be passed as an
argument, and handle the cleanup through a DRM-managed action.

Acked-by: Sam Ravnborg <sam@ravnborg.org>
Acked-by: Thomas Zimmermann <tzimmermann@suse.de>
Signed-off-by: Maxime Ripard <maxime@cerno.tech>
Link: https://lore.kernel.org/r/20220711173939.1132294-10-maxime@cerno.tech
drivers/gpu/drm/drm_connector.c
include/drm/drm_connector.h

index dfd1702..e3142c8 100644 (file)
@@ -26,6 +26,7 @@
 #include <drm/drm_edid.h>
 #include <drm/drm_encoder.h>
 #include <drm/drm_file.h>
+#include <drm/drm_managed.h>
 #include <drm/drm_panel.h>
 #include <drm/drm_print.h>
 #include <drm/drm_privacy_screen_consumer.h>
@@ -340,6 +341,10 @@ out_put:
  * should call drm_connector_cleanup() and free the connector structure.
  * The connector structure should not be allocated with devm_kzalloc().
  *
+ * Note: consider using drmm_connector_init() instead of
+ * drm_connector_init() to let the DRM managed resource infrastructure
+ * take care of cleanup and deallocation.
+ *
  * Returns:
  * Zero on success, error code on failure.
  */
@@ -372,6 +377,10 @@ EXPORT_SYMBOL(drm_connector_init);
  *
  * Ensures that the ddc field of the connector is correctly set.
  *
+ * Note: consider using drmm_connector_init() instead of
+ * drm_connector_init_with_ddc() to let the DRM managed resource
+ * infrastructure take care of cleanup and deallocation.
+ *
  * Returns:
  * Zero on success, error code on failure.
  */
@@ -388,6 +397,57 @@ int drm_connector_init_with_ddc(struct drm_device *dev,
 }
 EXPORT_SYMBOL(drm_connector_init_with_ddc);
 
+static void drm_connector_cleanup_action(struct drm_device *dev,
+                                        void *ptr)
+{
+       struct drm_connector *connector = ptr;
+
+       drm_connector_cleanup(connector);
+}
+
+/**
+ * drmm_connector_init - Init a preallocated connector
+ * @dev: DRM device
+ * @connector: the connector to init
+ * @funcs: callbacks for this connector
+ * @connector_type: user visible type of the connector
+ * @ddc: optional pointer to the associated ddc adapter
+ *
+ * Initialises a preallocated connector. Connectors should be
+ * subclassed as part of driver connector objects.
+ *
+ * Cleanup is automatically handled with a call to
+ * drm_connector_cleanup() in a DRM-managed action.
+ *
+ * The connector structure should be allocated with drmm_kzalloc().
+ *
+ * Returns:
+ * Zero on success, error code on failure.
+ */
+int drmm_connector_init(struct drm_device *dev,
+                       struct drm_connector *connector,
+                       const struct drm_connector_funcs *funcs,
+                       int connector_type,
+                       struct i2c_adapter *ddc)
+{
+       int ret;
+
+       if (drm_WARN_ON(dev, funcs && funcs->destroy))
+               return -EINVAL;
+
+       ret = __drm_connector_init(dev, connector, funcs, connector_type, NULL);
+       if (ret)
+               return ret;
+
+       ret = drmm_add_action_or_reset(dev, drm_connector_cleanup_action,
+                                      connector);
+       if (ret)
+               return ret;
+
+       return 0;
+}
+EXPORT_SYMBOL(drmm_connector_init);
+
 /**
  * drm_connector_attach_edid_property - attach edid property.
  * @connector: the connector
index a1705d6..2c6fa74 100644 (file)
@@ -1677,6 +1677,11 @@ int drm_connector_init_with_ddc(struct drm_device *dev,
                                const struct drm_connector_funcs *funcs,
                                int connector_type,
                                struct i2c_adapter *ddc);
+int drmm_connector_init(struct drm_device *dev,
+                       struct drm_connector *connector,
+                       const struct drm_connector_funcs *funcs,
+                       int connector_type,
+                       struct i2c_adapter *ddc);
 void drm_connector_attach_edid_property(struct drm_connector *connector);
 int drm_connector_register(struct drm_connector *connector);
 void drm_connector_unregister(struct drm_connector *connector);