Skip to contentSkip to conversation

media: v4l: async: add helper API v4l2_async_pad_init_and_register_subdev()

PATCH Superseded Frank Li 2 participants 7 messages Feb 10 – Feb 23

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Frank Li

author maintainer
#0 lore raw

cover media: v4l: async: add helper API v4l2_async_pad_init_and_register_subdev()

Add the helper API v4l2_async_pad_init_and_register_subdev(), which combines media_entity_pads_init(), v4l2_subdev_init_finalize(), and v4l2_async_register_subdev() into a single call.

Reduce code duplication and simplify error handling in drivers.

Only change dwc and cdns. If agree add API, more driver can be simplified.

---
Frank Li (3):
media: v4l: async: add helper API v4l2_async_pad_init_and_register_subdev()
media: synopsys: Use v4l2_async_pad_init_and_register_subdev() to simplify code
media: cadence: cdns-csi2rx: Use v4l2_async_pad_init_and_register_subdev() to simplify code

 drivers/media/platform/cadence/cdns-csi2rx.c     | 18 ++++----------
 drivers/media/platform/synopsys/dw-mipi-csi2rx.c | 15 +++---------
 drivers/media/v4l2-core/v4l2-async.c             | 30 ++++++++++++++++++++++++
 include/media/v4l2-async.h                       | 18 ++++++++++++++
 4 files changed, 55 insertions(+), 26 deletions(-)

---
base-commit: bc0bfce7d0f8204e0bbadcee72e87ad9ec105c73
change-id: 20260210-v4l2_init_register-0e6d0adcfa21

Best regards,
--
Frank Li <[email protected]>

author maintainer
#1 lore raw

PATCH 1/3 media: v4l: async: add helper API v4l2_async_pad_init_and_register_subdev()

Add the helper API v4l2_async_pad_init_and_register_subdev(), which combines media_entity_pads_init(), v4l2_subdev_init_finalize(), and v4l2_async_register_subdev() into a single call.

Reduce code duplication and simplify error handling in drivers.

---
 drivers/media/v4l2-core/v4l2-async.c | 30 ++++++++++++++++++++++++++++++
 include/media/v4l2-async.h           | 18 ++++++++++++++++++
 2 files changed, 48 insertions(+)
drivers/media/v4l2-core/v4l2-async.c +30 -0
index 1c08bba9ecb91f46b7479da613d6c1688d4b0b5c..65e4aa46372a02f9ddc04571436f46c90259e200 100644
@@ -19,6 +19,7 @@
 #include <linux/slab.h>
 #include <linux/types.h>
 
+#include <media/media-entity.h>
 #include <media/v4l2-async.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-fwnode.h>
@@ -881,6 +882,35 @@ int __v4l2_async_register_subdev(struct v4l2_subdev *sd, struct module *module)
 }
 EXPORT_SYMBOL(__v4l2_async_register_subdev);
 
+int __v4l2_async_pad_init_and_register_subdev(struct v4l2_subdev *sd,
+					      u16 num_pads,
+					      struct media_pad *pads,
+					      struct module *module)
+{
+	int ret;
+
+	ret = media_entity_pads_init(&sd->entity, num_pads, pads);
+	if (ret)
+		return ret;
+
+	ret = v4l2_subdev_init_finalize(sd);
+	if (ret)
+		goto err_entity_cleanup;
+
+	ret = __v4l2_async_register_subdev(sd, module);
+	if (ret)
+		goto err_subdev_cleanup;
+
+	return 0;
+
+err_subdev_cleanup:
+	v4l2_subdev_cleanup(sd);
+err_entity_cleanup:
+	media_entity_cleanup(&sd->entity);
+	return ret;
+}
+EXPORT_SYMBOL(__v4l2_async_pad_init_and_register_subdev);
+
 void v4l2_async_unregister_subdev(struct v4l2_subdev *sd)
 {
 	struct v4l2_async_connection *asc, *asc_tmp;
include/media/v4l2-async.h +18 -0
index f26c323e9c963065fd7c19d6d9835df1194bc069..81f1f418a246902178d5fa8f9d5e48b14301386f 100644
@@ -336,6 +336,24 @@ int __v4l2_async_register_subdev(struct v4l2_subdev *sd, struct module *module);
 int __must_check
 v4l2_async_register_subdev_sensor(struct v4l2_subdev *sd);
 
+struct media_pad;
+int __must_check
+__v4l2_async_pad_init_and_register_subdev(struct v4l2_subdev *sd, u16 num_pads,
+					  struct media_pad *pads,
+					  struct module *module);
+/**
+ * v4l2_async_pad_init_and_register_subdev - Initialize the entity pads and
+ *					     registers a sub-device to the
+ *					     asynchronous subdevice framework
+ * @sd: pointer to &struct v4l2_subdev
+ * @num_pads: total number of sink and source pads
+ * @pads: Array of @num_pads pads.
+ *
+ * Returns an error on failure, 0 on success.
+ */
+#define v4l2_async_pad_init_and_register_subdev(sd, num_pads, pads)	\
+	__v4l2_async_pad_init_and_register_subdev(sd, num_pads, pads,	\
+						  THIS_MODULE)
 /**
  * v4l2_async_unregister_subdev - unregisters a sub-device to the asynchronous
  *	subdevice framework
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH 2/3 media: synopsys: Use v4l2_async_pad_init_and_register_subdev() to simplify code

In reply to Frank Li

Use v4l2_async_pad_init_and_register_subdev() to simplify the code.

No functional changes.

---
 drivers/media/platform/synopsys/dw-mipi-csi2rx.c | 15 +++------------
 1 file changed, 3 insertions(+), 12 deletions(-)
drivers/media/platform/synopsys/dw-mipi-csi2rx.c +3 -12
index 5dc55b59d6aeed4b6cb207c8e2ebe0fb3c462644..64ad7ac62c085400e2544cceb91cfa55c66910bd 100644
@@ -681,15 +681,10 @@ static int dw_mipi_csi2rx_register(struct dw_mipi_csi2rx_device *csi2)
 	pads[DW_MIPI_CSI2RX_PAD_SINK].flags = MEDIA_PAD_FL_SINK |
 					      MEDIA_PAD_FL_MUST_CONNECT;
 	pads[DW_MIPI_CSI2RX_PAD_SRC].flags = MEDIA_PAD_FL_SOURCE;
-	ret = media_entity_pads_init(&sd->entity, DW_MIPI_CSI2RX_PAD_MAX, pads);
-	if (ret)
-		goto err_notifier_unregister;
-
-	ret = v4l2_subdev_init_finalize(sd);
-	if (ret)
-		goto err_entity_cleanup;
 
-	ret = v4l2_async_register_subdev(sd);
+	ret = v4l2_async_pad_init_and_register_subdev(sd,
+						      DW_MIPI_CSI2RX_PAD_MAX,
+						      pads);
 	if (ret) {
 		dev_err(sd->dev, "failed to register CSI-2 subdev\n");
 		goto err_subdev_cleanup;
@@ -698,10 +693,6 @@ static int dw_mipi_csi2rx_register(struct dw_mipi_csi2rx_device *csi2)
 	return 0;
 
 err_subdev_cleanup:
-	v4l2_subdev_cleanup(sd);
-err_entity_cleanup:
-	media_entity_cleanup(&sd->entity);
-err_notifier_unregister:
 	v4l2_async_nf_unregister(&csi2->notifier);
 	v4l2_async_nf_cleanup(&csi2->notifier);
 err:
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH 3/3 media: cadence: cdns-csi2rx: Use v4l2_async_pad_init_and_register_subdev() to simplify code

In reply to Frank Li

Use v4l2_async_pad_init_and_register_subdev() to simplify the code.

No functional changes.

---
build test only

---
 drivers/media/platform/cadence/cdns-csi2rx.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)
drivers/media/platform/cadence/cdns-csi2rx.c +4 -14
index 8c19f125da3e50f55a0ae280b05e7918ce115101..1a18a9d0ea602c91c07a4bf4573ec0346edc7650 100644
@@ -855,11 +855,6 @@ static int csi2rx_probe(struct platform_device *pdev)
 	csi2rx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
 	csi2rx->subdev.entity.ops = &csi2rx_media_ops;
 
-	ret = media_entity_pads_init(&csi2rx->subdev.entity, CSI2RX_PAD_MAX,
-				     csi2rx->pads);
-	if (ret)
-		goto err_cleanup;
-
 	csi2rx->error_irq = platform_get_irq_byname_optional(pdev, "error_irq");
 
 	if (csi2rx->error_irq < 0) {
@@ -875,13 +870,11 @@ static int csi2rx_probe(struct platform_device *pdev)
 		}
 	}
 
-	ret = v4l2_subdev_init_finalize(&csi2rx->subdev);
-	if (ret)
-		goto err_cleanup;
-
-	ret = v4l2_async_register_subdev(&csi2rx->subdev);
+	ret = v4l2_async_pad_init_and_register_subdev(&csi2rx->subdev,
+						      CSI2RX_PAD_MAX,
+						      csi2rx->pads);
 	if (ret < 0)
-		goto err_free_state;
+		goto err_cleanup;
 
 	dev_info(&pdev->dev,
 		 "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n",
@@ -891,12 +884,9 @@ static int csi2rx_probe(struct platform_device *pdev)
 
 	return 0;
 
-err_free_state:
-	v4l2_subdev_cleanup(&csi2rx->subdev);
 err_cleanup:
 	v4l2_async_nf_unregister(&csi2rx->notifier);
 	v4l2_async_nf_cleanup(&csi2rx->notifier);
-	media_entity_cleanup(&csi2rx->subdev.entity);
 err_free_priv:
 	kfree(csi2rx);
 	return ret;
Signature · 1 line
2.43.0
maintainer
#4 lore raw
In reply to Frank Li

Hi Frank,

Thanks for your patches.

Quoted 3 lines · Frank Li

Add the helper API v4l2_async_pad_init_and_register_subdev(), which combines media_entity_pads_init(), v4l2_subdev_init_finalize(), and v4l2_async_register_subdev() into a single call.

I think this is a good idea, but can you extend that to offer variants for v4l2_async_register_subdev_sensor and v4l2_device_register_subdev too?

The function names are awfully long, though. Can we call this

    media_register_subdev{_async,_sensor,}

maybe?

Best regards,
Michael

Quoted 25 lines

Reduce code duplication and simplify error handling in drivers.

Only change dwc and cdns. If agree add API, more driver can be simplified.

---
Frank Li (3):
media: v4l: async: add helper API v4l2_async_pad_init_and_register_subdev()
media: synopsys: Use v4l2_async_pad_init_and_register_subdev() to simplify code
media: cadence: cdns-csi2rx: Use v4l2_async_pad_init_and_register_subdev() to simplify code

 drivers/media/platform/cadence/cdns-csi2rx.c     | 18 ++++----------
 drivers/media/platform/synopsys/dw-mipi-csi2rx.c | 15 +++---------
 drivers/media/v4l2-core/v4l2-async.c             | 30 ++++++++++++++++++++++++
 include/media/v4l2-async.h                       | 18 ++++++++++++++
 4 files changed, 55 insertions(+), 26 deletions(-)

---
base-commit: bc0bfce7d0f8204e0bbadcee72e87ad9ec105c73
change-id: 20260210-v4l2_init_register-0e6d0adcfa21

Best regards,
--
Frank Li <[email protected]>

author maintainer
#5 lore raw
Quoted 9 lines · Michael Riesch

Hi Frank,

Thanks for your patches.

Quoted 3 lines · Frank Li

Add the helper API v4l2_async_pad_init_and_register_subdev(), which combines media_entity_pads_init(), v4l2_subdev_init_finalize(), and v4l2_async_register_subdev() into a single call.

Quoted 6 lines

I think this is a good idea, but can you extend that to offer variants for v4l2_async_register_subdev_sensor and v4l2_device_register_subdev too?

The function names are awfully long, though. Can we call this

    media_register_subdev{_async,_sensor,}

maybe?

How about media_async_register_subdev()? which basic align other API naming. I want to add more helper APIs.

drivers/media/platform/synopsys/dw-mipi-csi2rx.c have about 600 lines code but related hardware register codes are below 20 line, only few registers.

I just post this simple version to gather feedback from maintainers and reviewers on the overall approach.

Frank

Quoted 30 lines

Best regards,
Michael

Quoted 25 lines

Reduce code duplication and simplify error handling in drivers.

Only change dwc and cdns. If agree add API, more driver can be simplified.

---
Frank Li (3):
media: v4l: async: add helper API v4l2_async_pad_init_and_register_subdev()
media: synopsys: Use v4l2_async_pad_init_and_register_subdev() to simplify code
media: cadence: cdns-csi2rx: Use v4l2_async_pad_init_and_register_subdev() to simplify code

 drivers/media/platform/cadence/cdns-csi2rx.c     | 18 ++++----------
 drivers/media/platform/synopsys/dw-mipi-csi2rx.c | 15 +++---------
 drivers/media/v4l2-core/v4l2-async.c             | 30 ++++++++++++++++++++++++
 include/media/v4l2-async.h                       | 18 ++++++++++++++
 4 files changed, 55 insertions(+), 26 deletions(-)

---
base-commit: bc0bfce7d0f8204e0bbadcee72e87ad9ec105c73
change-id: 20260210-v4l2_init_register-0e6d0adcfa21

Best regards,
--
Frank Li <[email protected]>

maintainer
#6 lore raw

Hi Frank,

Quoted 13 lines · Frank Li
Quoted 12 lines · Michael Riesch

Hi Frank,

Thanks for your patches.

Quoted 3 lines · Frank Li

Add the helper API v4l2_async_pad_init_and_register_subdev(), which combines media_entity_pads_init(), v4l2_subdev_init_finalize(), and v4l2_async_register_subdev() into a single call.

I think this is a good idea, but can you extend that to offer variants for v4l2_async_register_subdev_sensor and v4l2_device_register_subdev too?

Quoted 6 lines
Quoted 3 lines

The function names are awfully long, though. Can we call this

    media_register_subdev{_async,_sensor,}

maybe?

How about media_async_register_subdev()? which basic align other API naming. I want to add more helper APIs.

This would work for me.

Quoted 5 lines

drivers/media/platform/synopsys/dw-mipi-csi2rx.c have about 600 lines code but related hardware register codes are below 20 line, only few registers.

I just post this simple version to gather feedback from maintainers and reviewers on the overall approach.

Sure, that's reasonable.

Just saying that if you plan to provide more helpers, there may be a smart way to do so without copying the new helper function code three times. Maybe

int helper(struct v4l2_subdev* sd, u16 num_pads, struct media_pad *pads,
struct module *module,
int (*cb)(struct v4l2_subdev *, struct module *))

and

#define media_async_register_subdev(sd, num_pads, pads) \
	helper(sd, num_pads, pads, THIS_MODULE,
	__v4l2_async_register_subdev)

or so?

Best regards,
Michael

Quoted 33 lines

Frank

Quoted 30 lines

Best regards,
Michael

Quoted 25 lines

Reduce code duplication and simplify error handling in drivers.

Only change dwc and cdns. If agree add API, more driver can be simplified.

---
Frank Li (3):
media: v4l: async: add helper API v4l2_async_pad_init_and_register_subdev()
media: synopsys: Use v4l2_async_pad_init_and_register_subdev() to simplify code
media: cadence: cdns-csi2rx: Use v4l2_async_pad_init_and_register_subdev() to simplify code

 drivers/media/platform/cadence/cdns-csi2rx.c     | 18 ++++----------
 drivers/media/platform/synopsys/dw-mipi-csi2rx.c | 15 +++---------
 drivers/media/v4l2-core/v4l2-async.c             | 30 ++++++++++++++++++++++++
 include/media/v4l2-async.h                       | 18 ++++++++++++++
 4 files changed, 55 insertions(+), 26 deletions(-)

---
base-commit: bc0bfce7d0f8204e0bbadcee72e87ad9ec105c73
change-id: 20260210-v4l2_init_register-0e6d0adcfa21

Best regards,
--
Frank Li <[email protected]>