All of lore.kernel.org
 help / color / mirror / Atom feed
From: Biju Das <biju.das.jz@bp.renesas.com>
To: cip-dev@lists.cip-project.org,
	Nobuhiro Iwamatsu <nobuhiro1.iwamatsu@toshiba.co.jp>,
	Pavel Machek <pavel@denx.de>
Cc: Biju Das <biju.das.jz@bp.renesas.com>,
	Lad Prabhakar <prabhakar.mahadev-lad.rj@bp.renesas.com>
Subject: [PATCH 5.10.y-cip v2 22/25] drm: [HACK DO NOT APPLY] add drmm_encoder_alloc()
Date: Fri,  8 Dec 2023 13:20:04 +0000	[thread overview]
Message-ID: <20231208132007.108762-23-biju.das.jz@bp.renesas.com> (raw)
In-Reply-To: <20231208132007.108762-1-biju.das.jz@bp.renesas.com>

From: Philipp Zabel <p.zabel@pengutronix.de>

commit ca5092d04d86986c03b6e0042e2f1cd119c50f5d upstream.

Add an alternative to drm_encoder_init() that allocates and initializes
an encoder and registers drm_encoder_cleanup() with
drmm_add_action_or_reset().

Signed-off-by: Philipp Zabel <p.zabel@pengutronix.de>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Biju Das <biju.das.jz@bp.renesas.com>
---
 drivers/gpu/drm/drm_encoder.c | 109 +++++++++++++++++++++++++++-------
 include/drm/drm_encoder.h     |  30 ++++++++++
 2 files changed, 116 insertions(+), 23 deletions(-)

diff --git a/drivers/gpu/drm/drm_encoder.c b/drivers/gpu/drm/drm_encoder.c
index e555281f43d4..24ac9a2350ea 100644
--- a/drivers/gpu/drm/drm_encoder.c
+++ b/drivers/gpu/drm/drm_encoder.c
@@ -26,6 +26,7 @@
 #include <drm/drm_device.h>
 #include <drm/drm_drv.h>
 #include <drm/drm_encoder.h>
+#include <drm/drm_managed.h>
 
 #include "drm_crtc_internal.h"
 
@@ -91,25 +92,11 @@ void drm_encoder_unregister_all(struct drm_device *dev)
 	}
 }
 
-/**
- * drm_encoder_init - Init a preallocated encoder
- * @dev: drm device
- * @encoder: the encoder to init
- * @funcs: callbacks for this encoder
- * @encoder_type: user visible type of the encoder
- * @name: printf style format string for the encoder name, or NULL for default name
- *
- * Initialises a preallocated encoder. Encoder should be subclassed as part of
- * driver encoder objects. At driver unload time drm_encoder_cleanup() should be
- * called from the driver's &drm_encoder_funcs.destroy hook.
- *
- * Returns:
- * Zero on success, error code on failure.
- */
-int drm_encoder_init(struct drm_device *dev,
-		     struct drm_encoder *encoder,
-		     const struct drm_encoder_funcs *funcs,
-		     int encoder_type, const char *name, ...)
+__printf(5, 0)
+static int __drm_encoder_init(struct drm_device *dev,
+			      struct drm_encoder *encoder,
+			      const struct drm_encoder_funcs *funcs,
+			      int encoder_type, const char *name, va_list ap)
 {
 	int ret;
 
@@ -125,11 +112,7 @@ int drm_encoder_init(struct drm_device *dev,
 	encoder->encoder_type = encoder_type;
 	encoder->funcs = funcs;
 	if (name) {
-		va_list ap;
-
-		va_start(ap, name);
 		encoder->name = kvasprintf(GFP_KERNEL, name, ap);
-		va_end(ap);
 	} else {
 		encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
 					  drm_encoder_enum_list[encoder_type].name,
@@ -150,6 +133,44 @@ int drm_encoder_init(struct drm_device *dev,
 
 	return ret;
 }
+
+/**
+ * drm_encoder_init - Init a preallocated encoder
+ * @dev: drm device
+ * @encoder: the encoder to init
+ * @funcs: callbacks for this encoder
+ * @encoder_type: user visible type of the encoder
+ * @name: printf style format string for the encoder name, or NULL for default name
+ *
+ * Initializes a preallocated encoder. Encoder should be subclassed as part of
+ * driver encoder objects. At driver unload time the driver's
+ * &drm_encoder_funcs.destroy hook should call drm_encoder_cleanup() and kfree()
+ * the encoder structure. The encoder structure should not be allocated with
+ * devm_kzalloc().
+ *
+ * Note: consider using drmm_encoder_alloc() instead of drm_encoder_init() to
+ * let the DRM managed resource infrastructure take care of cleanup and
+ * deallocation.
+ *
+ * Returns:
+ * Zero on success, error code on failure.
+ */
+int drm_encoder_init(struct drm_device *dev,
+		     struct drm_encoder *encoder,
+		     const struct drm_encoder_funcs *funcs,
+		     int encoder_type, const char *name, ...)
+{
+	va_list ap;
+	int ret;
+
+	WARN_ON(!funcs->destroy);
+
+	va_start(ap, name);
+	ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, ap);
+	va_end(ap);
+
+	return ret;
+}
 EXPORT_SYMBOL(drm_encoder_init);
 
 /**
@@ -181,6 +202,48 @@ void drm_encoder_cleanup(struct drm_encoder *encoder)
 }
 EXPORT_SYMBOL(drm_encoder_cleanup);
 
+static void drmm_encoder_alloc_release(struct drm_device *dev, void *ptr)
+{
+	struct drm_encoder *encoder = ptr;
+
+	if (WARN_ON(!encoder->dev))
+		return;
+
+	drm_encoder_cleanup(encoder);
+}
+
+void *__drmm_encoder_alloc(struct drm_device *dev, size_t size, size_t offset,
+			   const struct drm_encoder_funcs *funcs,
+			   int encoder_type, const char *name, ...)
+{
+	void *container;
+	struct drm_encoder *encoder;
+	va_list ap;
+	int ret;
+
+	if (WARN_ON(funcs && funcs->destroy))
+		return ERR_PTR(-EINVAL);
+
+	container = drmm_kzalloc(dev, size, GFP_KERNEL);
+	if (!container)
+		return ERR_PTR(-EINVAL);
+
+	encoder = container + offset;
+
+	va_start(ap, name);
+	ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, ap);
+	va_end(ap);
+	if (ret)
+		return ERR_PTR(ret);
+
+	ret = drmm_add_action_or_reset(dev, drmm_encoder_alloc_release, encoder);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return container;
+}
+EXPORT_SYMBOL(__drmm_encoder_alloc);
+
 static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
 {
 	struct drm_connector *connector;
diff --git a/include/drm/drm_encoder.h b/include/drm/drm_encoder.h
index a60f5f1555ac..8e1316caba28 100644
--- a/include/drm/drm_encoder.h
+++ b/include/drm/drm_encoder.h
@@ -195,6 +195,36 @@ int drm_encoder_init(struct drm_device *dev,
 		     const struct drm_encoder_funcs *funcs,
 		     int encoder_type, const char *name, ...);
 
+__printf(6, 7)
+void *__drmm_encoder_alloc(struct drm_device *dev,
+			   size_t size, size_t offset,
+			   const struct drm_encoder_funcs *funcs,
+			   int encoder_type,
+			   const char *name, ...);
+
+/**
+ * drmm_encoder_alloc - Allocate and initialize an encoder
+ * @dev: drm device
+ * @type: the type of the struct which contains struct &drm_encoder
+ * @member: the name of the &drm_encoder within @type
+ * @funcs: callbacks for this encoder (optional)
+ * @encoder_type: user visible type of the encoder
+ * @name: printf style format string for the encoder name, or NULL for default name
+ *
+ * Allocates and initializes an encoder. Encoder should be subclassed as part of
+ * driver encoder objects. Cleanup is automatically handled through registering
+ * drm_encoder_cleanup() with drmm_add_action().
+ *
+ * The @drm_encoder_funcs.destroy hook must be NULL.
+ *
+ * Returns:
+ * Pointer to new encoder, or ERR_PTR on failure.
+ */
+#define drmm_encoder_alloc(dev, type, member, funcs, encoder_type, name, ...) \
+	((type *)__drmm_encoder_alloc(dev, sizeof(type), \
+				      offsetof(type, member), funcs, \
+				      encoder_type, name, ##__VA_ARGS__))
+
 /**
  * drm_encoder_index - find the index of a registered encoder
  * @encoder: encoder to find index for
-- 
2.25.1



  parent reply	other threads:[~2023-12-08 13:21 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-08 13:19 [PATCH 5.10.y-cip v2 00/25] Add RZ/G2L DSI support Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 01/25] drm/bridge: Add a function to abstract away panels Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 02/25] drm/bridge: Add stubs for devm_drm_of_get_bridge when OF is disabled Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 03/25] drm/bridge: Move devm_drm_of_get_bridge to bridge/panel.c Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 04/25] drm: of: Add drm_of_get_data_lanes_count and drm_of_get_data_lanes_ep Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 05/25] drm: of: Mark empty drm_of_get_data_lanes_count and drm_of_get_data_lanes_ep static Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 06/25] dt-bindings: display: bridge: Document RZ/G2L MIPI DSI TX bindings Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 07/25] dt-bindings: display: bridge: renesas,rzg2l-mipi-dsi: Document RZ/V2L support Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 08/25] drm: rcar-du: Add RZ/G2L DSI driver Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 09/25] drm: rcar-du: Fix Kconfig dependency between DRM and RZG2L_MIPI_DSI Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 10/25] drm: rcar-du: rzg2l_mipi_dsi: Enhance device lanes check Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 11/25] drm: rcar-du: rzg2l_mipi_dsi: Reorder bridge attach Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 12/25] arm64: defconfig: Enable Renesas RZ/G2L MIPI DSI driver Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 13/25] arm64: dts: renesas: r9a07g044: Add DSI node Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 14/25] arm64: dts: renesas: r9a07g054: " Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 15/25] arm64: dts: renesas: rzg2l-smarc: Link DSI with ADV7535 Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 16/25] arm64: dts: renesas: rzg2lc-smarc: " Biju Das
2023-12-08 13:19 ` [PATCH 5.10.y-cip v2 17/25] arm64: dts: renesas: Drop ADV7535 IRQ Biju Das
2023-12-08 13:20 ` [PATCH 5.10.y-cip v2 18/25] arm64: dts: renesas: r9a07g044: [HACK DO NOT APPLY] Add DU node Biju Das
2023-12-08 13:20 ` [PATCH 5.10.y-cip v2 19/25] arm64: dts: renesas: r9a07g054: " Biju Das
2023-12-08 13:20 ` [PATCH 5.10.y-cip v2 20/25] arm64: dts: renesas: rzg2l-smarc: [HACK DO NOT APPLY] Enable DU and link with DSI Biju Das
2023-12-08 13:20 ` [PATCH 5.10.y-cip v2 21/25] arm64: dts: renesas: rzg2lc-smarc: " Biju Das
2023-12-08 13:20 ` Biju Das [this message]
2023-12-08 13:20 ` [PATCH 5.10.y-cip v2 23/25] drm: [HACK DO NOT APPLY] Allow const struct drm_driver Biju Das
2023-12-08 13:20 ` [PATCH 5.10.y-cip v2 24/25] drm: [HACK DO NOT APPLY] Add RZ/G2L DU Support Biju Das
2023-12-08 13:20 ` [PATCH 5.10.y-cip v2 25/25] defconfig: [HACK DO NOT APPLY] Enable display on RZ/G2L SMARC EVK Biju Das
2023-12-09 10:21 ` [PATCH 5.10.y-cip v2 00/25] Add RZ/G2L DSI support Pavel Machek
2023-12-11  8:06 ` Pavel Machek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231208132007.108762-23-biju.das.jz@bp.renesas.com \
    --to=biju.das.jz@bp.renesas.com \
    --cc=cip-dev@lists.cip-project.org \
    --cc=nobuhiro1.iwamatsu@toshiba.co.jp \
    --cc=pavel@denx.de \
    --cc=prabhakar.mahadev-lad.rj@bp.renesas.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.