devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7 00/13] CSI2RX support on J721E
@ 2023-03-14 11:55 Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 01/13] media: cadence: csi2rx: Unregister v4l2 async notifier Vaishnav Achath
                   ` (14 more replies)
  0 siblings, 15 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

Hi,

This series adds support for CSI2 capture on J721E. It includes some
fixes to the Cadence CSI2RX driver, and adds the TI CSI2RX wrapper driver.

This is a V7 of the below V6 series,
https://lore.kernel.org/all/20220121142904.4091481-1-p.yadav@ti.com/

Since Pratyush moved out of TI, I will be working on upstreaming the
TI CSI2RX wrapper support.

Tested on TI's J721E EVM with LI OV5640 sensor module.
https://gist.github.com/vaishnavachath/f030a257d5b6569817bc9deab1c4fa77

Also, Tested on TI AM62-SK with Pcam5C OV5640 module.
https://gist.github.com/vaishnavachath/ff2605faa92f1a6ab5670426da28ccee

For all newer TI platforms that TI J721E Silicon Revision 1.0, below update
to DPHY RX driver is needed:
https://lore.kernel.org/all/20230314073137.2153-1-vaishnav.a@ti.com/

Changes in v7:
- For patch 10/13 ("Add CSI2RX support for J721E"):
- Fix incorrect value written in SHIM_PSI_CFG0_DST_TAG
- Drop support for 2X8 formats.
- Update maintainer to Vaishnav as Pratyush moved out of TI.
- Address Sakari's review comments:
- Update MAX_HEIGHT_LINES, MAX_WIDTH_BYTES to prevent overflow.
- Assign dma_slave_config during declaration, drop memset().
- dma_release_channel() on slave_config failure.
- provide entity ops for the vdev entity with link_validate().
- mutex_destroy() on ti_csi2rx_probe failure path.
- Drop busy check in remove().
- mutex_destroy() in ti_csi2rx_remove().
- Address Laurent's review comments:
- Update entries in Makefile in alphabetical order.
- include headers in alphabetical order.
- Drop redundant CSI DT defines and use from media/mipi-csi2.h.
- Rename csi_df to csi_dt.
- Drop v4l2_colorspace from ti_csi2rx_fmt and set default in
  ti_csi2rx_v4l2_init()
- Adjust field and not return EINVAL in ti_csi2rx_try_fmt_vid_cap().
- inline ti_csi2rx_video_register().
- start DMA before starting source subdev.
- move buffer cleanup to separate function ti_csi2rx_cleanup_buffers()
  to be used in ti_csi2rx_stop_streaming() and ti_csi2rx_start_streaming()
  failure path.
- Drop VB2_USERPTR, VB2_READ and V4L2_CAP_READWRITE.
- For patch 4/13 ("media: cadence: csi2rx: Add external DPHY support"):
- Fix multiplier and divider in v4l2_get_link_freq() which caused
  failures during streaming.

Changes in v6:
- Move the lock around the dereference for framefmt in
  csi2rx_{get,set}_fmt() instead of when we get the pointer.
- Do not return an error when an unsupported format is set. Instead
  adjust the code to the first format in the list.
- Drop variable bpp and use fmt->bpp directly.
- Drop variable got_pm. Call phy_pm_runtime_put() unconditionally since
  it will just return an error if runtime PM is not enabled.
- Drop transcoding from the commit message.
- Make csi2rx_media_ops const.

Changes in v5:
- Cleanup notifier in csi2rx_parse_dt() after the call to
  v4l2_async_nf_add_fwnode_remote().
- Use YUV 1X16 formats instead of 2X8.
- Only error out when phy_pm_runtime_get_sync() returns a negative
  value. A positive value can be returned if the phy was already
  resumed.
- Do not query the source subdev for format. Use the newly added
  internal format instead.
- Make i unsigned.
- Change %d to %u
- Add dependency on PHY_CADENCE_DPHY_RX instead of PHY_CADENCE_DPHY
  since the Rx mode DPHY now has a separate driver.
- Drop ti_csi2rx_validate_pipeline(). Pipeline validation should be done
  at media_pipeline_start().
- Do not assign flags.
- Fix error handling in ti_csi2rx_start_streaming(). Free up vb2 buffers
  when media_pipeline_start() fails.
- Move clock description in comments under the clocks property.
- Make ports required.
- Add link validation to cdns-csi2rx driver.

Changes in v4:
- Drop the call to set PHY submode. It is now being done via compatible
  on the DPHY side.
- Acquire the media device's graph_mutex before starting the graph walk.
- Call media_graph_walk_init() and media_graph_walk_cleanup() when
  starting and ending the graph walk respectively.
- Reduce max frame height and width in enum_framesizes. Currently they
  are set to UINT_MAX but they must be a multiple of step_width, so they
  need to be rounded down. Also, these values are absurdly large which
  causes some userspace applications like gstreamer to trip up. While it
  is not generally right to change the kernel for an application bug, it
  is not such a big deal here. This change is replacing one set of
  absurdly large arbitrary values with another set of smaller but still
  absurdly large arbitrary values. Both limits are unlikely to be hit in
  practice.
- Add power-domains property.
- Drop maxItems from clock-names.
- Drop the type for data-lanes.
- Drop uniqueItems from data-lanes. Move it to video-interfaces.yaml
  instead.
- Drop OV5640 runtime pm patch. It seems to be a bit complicated and it
  is not exactly necessary for this series. Any CSI-2 camera will work
  just fine, OV5640 just happens to be the one I tested with. I don't
  want it to block this series. I will submit it as a separate patch
  later.

Changes in v3:
- Use v4l2_get_link_freq() to calculate pixel clock.
- Move DMA related fields in struct ti_csi2rx_dma.
- Protect DMA buffer queue with a spinlock to make sure the queue buffer
  and DMA callback don't race on it.
- Track the current DMA state. It might go idle because of a lack of
  buffers. This state can be used to restart it if needed.
- Do not include the current buffer in the pending queue. It is slightly
  better modelling than leaving it at the head of the pending queue.
- Use the buffer as the callback argument, and add a reference to csi in it.
- If queueing a buffer to DMA fails, the buffer gets leaked and DMA gets
  stalled with. Instead, report the error to vb2 and queue the next
  buffer in the pending queue.
- DMA gets stalled if we run out of buffers since the callback is the
  only one that fires subsequent transfers and it is no longer being
  called. Check for that when queueing buffers and restart DMA if
  needed.
- Do not put of node until we are done using the fwnode.
- Set inital format to UYVY 640x480.
- Add compatible: contains: const: cdns,csi2rx to allow SoC specific
  compatible.
- Add more constraints for data-lanes property.

Changes in v2:
- Use phy_pm_runtime_get_sync() and phy_pm_runtime_put() before making
  calls to set PHY mode, etc. to make sure it is ready.
- Use dmaengine_get_dma_device() instead of directly accessing
  dma->device->dev.
- Do not set dst_addr_width when configuring slave DMA.
- Move to a separate subdir and rename to j721e-csi2rx.c
- Convert compatible to ti,j721e-csi2rx.
- Move to use Media Controller centric APIs.
- Improve cleanup in probe when one of the steps fails.
- Add colorspace to formats database.
- Set hw_revision on media_device.
- Move video device initialization to probe time instead of register time.
- Rename to ti,j721e-csi2rx.yaml
- Add an entry in MAINTAINERS.
- Add a description for the binding.
- Change compatible to ti,j721e-csi2rx to make it SoC specific.
- Remove description from dmas, reg, power-domains.
- Remove a limit of 2 from #address-cells and #size-cells.
- Fix add ^ to csi-bridge subnode regex.
- Make ranges mandatory.
- Add unit address in example.
- Add a reference to cdns,csi2rx in csi-bridge subnode.
- Expand the example to include the csi-bridge subnode as well.
- Re-order subject prefixes.
- Convert OV5640 to use runtime PM and drop Cadence CSI2RX s_power patch.
- Drop subdev call wrappers from cdns-csi2rx.
- Move VPE and CAL to a separate subdir.
- Rename ti-csi2rx.c to j721e-csi2rx.c

Pratyush Yadav (13):
  media: cadence: csi2rx: Unregister v4l2 async notifier
  media: cadence: csi2rx: Cleanup media entity properly
  media: cadence: csi2rx: Add get_fmt and set_fmt pad ops
  media: cadence: csi2rx: Add external DPHY support
  media: cadence: csi2rx: Soft reset the streams before starting capture
  media: cadence: csi2rx: Set the STOP bit when stopping a stream
  media: cadence: csi2rx: Fix stream data configuration
  media: cadence: csi2rx: Populate subdev devnode
  media: cadence: csi2rx: Add link validation
  media: ti: Add CSI2RX support for J721E
  media: dt-bindings: Make sure items in data-lanes are unique
  media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver
  media: dt-bindings: Convert Cadence CSI2RX binding to YAML

 .../devicetree/bindings/media/cdns,csi2rx.txt |  100 --
 .../bindings/media/cdns,csi2rx.yaml           |  176 +++
 .../bindings/media/ti,j721e-csi2rx.yaml       |  101 ++
 .../bindings/media/video-interfaces.yaml      |    1 +
 MAINTAINERS                                   |    7 +
 drivers/media/platform/cadence/cdns-csi2rx.c  |  273 ++++-
 drivers/media/platform/ti/Kconfig             |   12 +
 drivers/media/platform/ti/Makefile            |    1 +
 .../media/platform/ti/j721e-csi2rx/Makefile   |    2 +
 .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 1022 +++++++++++++++++
 10 files changed, 1580 insertions(+), 115 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/media/cdns,csi2rx.txt
 create mode 100644 Documentation/devicetree/bindings/media/cdns,csi2rx.yaml
 create mode 100644 Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml
 create mode 100644 drivers/media/platform/ti/j721e-csi2rx/Makefile
 create mode 100644 drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c

-- 
2.17.1


^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v7 01/13] media: cadence: csi2rx: Unregister v4l2 async notifier
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 02/13] media: cadence: csi2rx: Cleanup media entity properly Vaishnav Achath
                   ` (13 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

The notifier is added to the global notifier list when registered. When
the module is removed, the struct csi2rx_priv in which the notifier is
embedded, is destroyed. As a result the notifier list has a reference to
a notifier that no longer exists. This causes invalid memory accesses
when the list is iterated over. Similar for when the probe fails.
Unregister and clean up the notifier to avoid this.

Fixes: 1fc3b37f34f6 ("media: v4l: cadence: Add Cadence MIPI-CSI2 RX driver")

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v5)

Changes in v5:
- Cleanup notifier in csi2rx_parse_dt() after the call to
  v4l2_async_nf_add_fwnode_remote().

Changes in v3:
- New in v3.

 drivers/media/platform/cadence/cdns-csi2rx.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index cc3ebb0d96f6..2a23da6a0b8e 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -404,8 +404,10 @@ static int csi2rx_parse_dt(struct csi2rx_priv *csi2rx)
 	asd = v4l2_async_nf_add_fwnode_remote(&csi2rx->notifier, fwh,
 					      struct v4l2_async_subdev);
 	of_node_put(ep);
-	if (IS_ERR(asd))
+	if (IS_ERR(asd)) {
+		v4l2_async_nf_cleanup(&csi2rx->notifier);
 		return PTR_ERR(asd);
+	}
 
 	csi2rx->notifier.ops = &csi2rx_notifier_ops;
 
@@ -467,6 +469,7 @@ static int csi2rx_probe(struct platform_device *pdev)
 	return 0;
 
 err_cleanup:
+	v4l2_async_nf_unregister(&csi2rx->notifier);
 	v4l2_async_nf_cleanup(&csi2rx->notifier);
 err_free_priv:
 	kfree(csi2rx);
@@ -477,6 +480,8 @@ static int csi2rx_remove(struct platform_device *pdev)
 {
 	struct csi2rx_priv *csi2rx = platform_get_drvdata(pdev);
 
+	v4l2_async_nf_unregister(&csi2rx->notifier);
+	v4l2_async_nf_cleanup(&csi2rx->notifier);
 	v4l2_async_unregister_subdev(&csi2rx->subdev);
 	kfree(csi2rx);
 
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 02/13] media: cadence: csi2rx: Cleanup media entity properly
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 01/13] media: cadence: csi2rx: Unregister v4l2 async notifier Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 03/13] media: cadence: csi2rx: Add get_fmt and set_fmt pad ops Vaishnav Achath
                   ` (12 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

Call media_entity_cleanup() in probe error path and remove to make sure
the media entity is cleaned up properly.

Suggested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v6)

Changes in v6:
- Add Laurent's R-by.

Changes in v5:
- New in v5.

 drivers/media/platform/cadence/cdns-csi2rx.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index 2a23da6a0b8e..2547903f2e8e 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -471,6 +471,7 @@ static int csi2rx_probe(struct platform_device *pdev)
 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;
@@ -483,6 +484,7 @@ static int csi2rx_remove(struct platform_device *pdev)
 	v4l2_async_nf_unregister(&csi2rx->notifier);
 	v4l2_async_nf_cleanup(&csi2rx->notifier);
 	v4l2_async_unregister_subdev(&csi2rx->subdev);
+	media_entity_cleanup(&csi2rx->subdev.entity);
 	kfree(csi2rx);
 
 	return 0;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 03/13] media: cadence: csi2rx: Add get_fmt and set_fmt pad ops
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 01/13] media: cadence: csi2rx: Unregister v4l2 async notifier Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 02/13] media: cadence: csi2rx: Cleanup media entity properly Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 04/13] media: cadence: csi2rx: Add external DPHY support Vaishnav Achath
                   ` (11 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

The format is needed to calculate the link speed for the external DPHY
configuration. It is not right to query the format from the source
subdev. Add get_fmt and set_fmt pad operations so that the format can be
configured and correct bpp be selected.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

Changes in v7:
- Add Laurent's R-by.

Changes in v6:
- Move the lock around the dereference for framefmt in
  csi2rx_{get,set}_fmt() instead of when we get the pointer.
- Do not return an error when an unsupported format is set. Instead
  adjust the code to the first format in the list.
- Not adding Laurent's R-by since I am making changes in
  csi2rx_set_fmt() that he didn't explicitly recommend.

Changes in v5:
- Use YUV 1X16 formats instead of 2X8.
- New in v5.

 drivers/media/platform/cadence/cdns-csi2rx.c | 131 +++++++++++++++++++
 1 file changed, 131 insertions(+)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index 2547903f2e8e..ae3ebdb3890d 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -54,6 +54,11 @@ enum csi2rx_pads {
 	CSI2RX_PAD_MAX,
 };
 
+struct csi2rx_fmt {
+	u32				code;
+	u8				bpp;
+};
+
 struct csi2rx_priv {
 	struct device			*dev;
 	unsigned int			count;
@@ -79,12 +84,43 @@ struct csi2rx_priv {
 	struct v4l2_subdev		subdev;
 	struct v4l2_async_notifier	notifier;
 	struct media_pad		pads[CSI2RX_PAD_MAX];
+	struct v4l2_mbus_framefmt	fmt;
 
 	/* Remote source */
 	struct v4l2_subdev		*source_subdev;
 	int				source_pad;
 };
 
+static const struct csi2rx_fmt formats[] = {
+	{
+		.code	= MEDIA_BUS_FMT_YUYV8_1X16,
+		.bpp	= 16,
+	},
+	{
+		.code	= MEDIA_BUS_FMT_UYVY8_1X16,
+		.bpp	= 16,
+	},
+	{
+		.code	= MEDIA_BUS_FMT_YVYU8_1X16,
+		.bpp	= 16,
+	},
+	{
+		.code	= MEDIA_BUS_FMT_VYUY8_1X16,
+		.bpp	= 16,
+	},
+};
+
+static const struct csi2rx_fmt *csi2rx_get_fmt_by_code(u32 code)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(formats); i++)
+		if (formats[i].code == code)
+			return &formats[i];
+
+	return NULL;
+}
+
 static inline
 struct csi2rx_priv *v4l2_subdev_to_csi2rx(struct v4l2_subdev *subdev)
 {
@@ -236,12 +272,103 @@ static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable)
 	return ret;
 }
 
+static struct v4l2_mbus_framefmt *
+csi2rx_get_pad_format(struct csi2rx_priv *csi2rx,
+		      struct v4l2_subdev_state *state,
+		      unsigned int pad, u32 which)
+{
+	switch (which) {
+	case V4L2_SUBDEV_FORMAT_TRY:
+		return v4l2_subdev_get_try_format(&csi2rx->subdev, state, pad);
+	case V4L2_SUBDEV_FORMAT_ACTIVE:
+		return &csi2rx->fmt;
+	default:
+		return NULL;
+	}
+}
+
+static int csi2rx_get_fmt(struct v4l2_subdev *subdev,
+			  struct v4l2_subdev_state *state,
+			  struct v4l2_subdev_format *format)
+{
+	struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
+	struct v4l2_mbus_framefmt *framefmt;
+
+	framefmt = csi2rx_get_pad_format(csi2rx, state, format->pad,
+					 format->which);
+	if (!framefmt)
+		return -EINVAL;
+
+	mutex_lock(&csi2rx->lock);
+	format->format = *framefmt;
+	mutex_unlock(&csi2rx->lock);
+
+	return 0;
+}
+
+static int csi2rx_set_fmt(struct v4l2_subdev *subdev,
+			  struct v4l2_subdev_state *state,
+			  struct v4l2_subdev_format *format)
+{
+	struct csi2rx_priv *csi2rx = v4l2_subdev_to_csi2rx(subdev);
+	struct v4l2_mbus_framefmt *framefmt;
+
+	/* No transcoding, source and sink formats must match. */
+	if (format->pad != CSI2RX_PAD_SINK)
+		return csi2rx_get_fmt(subdev, state, format);
+
+	if (!csi2rx_get_fmt_by_code(format->format.code))
+		format->format.code = formats[0].code;
+
+	format->format.field = V4L2_FIELD_NONE;
+
+	framefmt = csi2rx_get_pad_format(csi2rx, state, format->pad,
+					 format->which);
+	if (!framefmt)
+		return -EINVAL;
+
+	mutex_lock(&csi2rx->lock);
+	*framefmt = format->format;
+	mutex_unlock(&csi2rx->lock);
+
+	return 0;
+}
+
+static int csi2rx_init_cfg(struct v4l2_subdev *subdev,
+			   struct v4l2_subdev_state *state)
+{
+	struct v4l2_subdev_format format = {
+		.which = state ? V4L2_SUBDEV_FORMAT_TRY
+			: V4L2_SUBDEV_FORMAT_ACTIVE,
+		.pad = CSI2RX_PAD_SINK,
+		.format = {
+			.width = 640,
+			.height = 480,
+			.code = MEDIA_BUS_FMT_UYVY8_1X16,
+			.field = V4L2_FIELD_NONE,
+			.colorspace = V4L2_COLORSPACE_SRGB,
+			.ycbcr_enc = V4L2_YCBCR_ENC_601,
+			.quantization = V4L2_QUANTIZATION_LIM_RANGE,
+			.xfer_func = V4L2_XFER_FUNC_SRGB,
+		},
+	};
+
+	return csi2rx_set_fmt(subdev, state, &format);
+}
+
+static const struct v4l2_subdev_pad_ops csi2rx_pad_ops = {
+	.get_fmt	= csi2rx_get_fmt,
+	.set_fmt	= csi2rx_set_fmt,
+	.init_cfg	= csi2rx_init_cfg,
+};
+
 static const struct v4l2_subdev_video_ops csi2rx_video_ops = {
 	.s_stream	= csi2rx_s_stream,
 };
 
 static const struct v4l2_subdev_ops csi2rx_subdev_ops = {
 	.video		= &csi2rx_video_ops,
+	.pad		= &csi2rx_pad_ops,
 };
 
 static int csi2rx_async_bound(struct v4l2_async_notifier *notifier,
@@ -457,6 +584,10 @@ static int csi2rx_probe(struct platform_device *pdev)
 	if (ret)
 		goto err_cleanup;
 
+	ret = csi2rx_init_cfg(&csi2rx->subdev, NULL);
+	if (ret)
+		goto err_cleanup;
+
 	ret = v4l2_async_register_subdev(&csi2rx->subdev);
 	if (ret < 0)
 		goto err_cleanup;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 04/13] media: cadence: csi2rx: Add external DPHY support
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (2 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 03/13] media: cadence: csi2rx: Add get_fmt and set_fmt pad ops Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-31 13:19   ` Sakari Ailus
  2023-03-14 11:55 ` [PATCH v7 05/13] media: cadence: csi2rx: Soft reset the streams before starting capture Vaishnav Achath
                   ` (10 subsequent siblings)
  14 siblings, 1 reply; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

Some platforms like TI's J721E can have the CSI2RX paired with an
external DPHY. Add support to enable and configure the DPHY using the
generic PHY framework.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

Changes in v7:
- Fix multiplier and divider in v4l2_get_link_freq() which caused
  failures during streaming.

Changes in v6:
- Drop variable bpp and use fmt->bpp directly.
- Drop variable got_pm. Call phy_pm_runtime_put() unconditionally since
  it will just return an error if runtime PM is not enabled.
- Add Laurent's R-by.

Changes in v5:
- Only error out when phy_pm_runtime_get_sync() returns a negative
  value. A positive value can be returned if the phy was already
  resumed.
- Do not query the source subdev for format. Use the newly added
  internal format instead.

Changes in v4:
- Drop the call to set PHY submode. It is now being done via compatible
  on the DPHY side.

Changes in v3:
- Use v4l2_get_link_freq() to calculate pixel clock.

Changes in v2:
- Use phy_pm_runtime_get_sync() and phy_pm_runtime_put() before making
  calls to set PHY mode, etc. to make sure it is ready.

 drivers/media/platform/cadence/cdns-csi2rx.c | 90 +++++++++++++++++---
 1 file changed, 80 insertions(+), 10 deletions(-)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index ae3ebdb3890d..6933626adfc8 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -30,6 +30,12 @@
 #define CSI2RX_STATIC_CFG_DLANE_MAP(llane, plane)	((plane) << (16 + (llane) * 4))
 #define CSI2RX_STATIC_CFG_LANES_MASK			GENMASK(11, 8)
 
+#define CSI2RX_DPHY_LANE_CTRL_REG		0x40
+#define CSI2RX_DPHY_CL_RST			BIT(16)
+#define CSI2RX_DPHY_DL_RST(i)			BIT((i) + 12)
+#define CSI2RX_DPHY_CL_EN			BIT(4)
+#define CSI2RX_DPHY_DL_EN(i)			BIT(i)
+
 #define CSI2RX_STREAM_BASE(n)		(((n) + 1) * 0x100)
 
 #define CSI2RX_STREAM_CTRL_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x000)
@@ -137,6 +143,49 @@ static void csi2rx_reset(struct csi2rx_priv *csi2rx)
 	writel(0, csi2rx->base + CSI2RX_SOFT_RESET_REG);
 }
 
+static int csi2rx_configure_external_dphy(struct csi2rx_priv *csi2rx)
+{
+	union phy_configure_opts opts = { };
+	struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
+	const struct csi2rx_fmt *fmt;
+	s64 pixel_clock;
+	int ret;
+
+	fmt = csi2rx_get_fmt_by_code(csi2rx->fmt.code);
+
+	/*
+	 * Do not divide by the number of lanes here. That will be done by
+	 * phy_mipi_dphy_get_default_config().
+	 */
+	pixel_clock = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
+					 fmt->bpp, 2 * csi2rx->num_lanes);
+	if (pixel_clock < 0)
+		return pixel_clock;
+
+	ret = phy_mipi_dphy_get_default_config(pixel_clock, 1, 1, cfg);
+	if (ret)
+		return ret;
+
+	ret = phy_pm_runtime_get_sync(csi2rx->dphy);
+	if (ret < 0 && ret != -ENOTSUPP)
+		return ret;
+
+	ret = phy_power_on(csi2rx->dphy);
+	if (ret)
+		goto out;
+
+	ret = phy_configure(csi2rx->dphy, &opts);
+	if (ret) {
+		/* Can't do anything if it fails. Ignore the return value. */
+		phy_power_off(csi2rx->dphy);
+		goto out;
+	}
+
+out:
+	phy_pm_runtime_put(csi2rx->dphy);
+	return ret;
+}
+
 static int csi2rx_start(struct csi2rx_priv *csi2rx)
 {
 	unsigned int i;
@@ -175,6 +224,17 @@ static int csi2rx_start(struct csi2rx_priv *csi2rx)
 	if (ret)
 		goto err_disable_pclk;
 
+	/* Enable DPHY clk and data lanes. */
+	if (csi2rx->dphy) {
+		reg = CSI2RX_DPHY_CL_EN | CSI2RX_DPHY_CL_RST;
+		for (i = 0; i < csi2rx->num_lanes; i++) {
+			reg |= CSI2RX_DPHY_DL_EN(csi2rx->lanes[i] - 1);
+			reg |= CSI2RX_DPHY_DL_RST(csi2rx->lanes[i] - 1);
+		}
+
+		writel(reg, csi2rx->base + CSI2RX_DPHY_LANE_CTRL_REG);
+	}
+
 	/*
 	 * Create a static mapping between the CSI virtual channels
 	 * and the output stream.
@@ -205,10 +265,21 @@ static int csi2rx_start(struct csi2rx_priv *csi2rx)
 	if (ret)
 		goto err_disable_pixclk;
 
+	if (csi2rx->dphy) {
+		ret = csi2rx_configure_external_dphy(csi2rx);
+		if (ret) {
+			dev_err(csi2rx->dev,
+				"Failed to configure external DPHY: %d\n", ret);
+			goto err_disable_sysclk;
+		}
+	}
+
 	clk_disable_unprepare(csi2rx->p_clk);
 
 	return 0;
 
+err_disable_sysclk:
+	clk_disable_unprepare(csi2rx->sys_clk);
 err_disable_pixclk:
 	for (; i > 0; i--)
 		clk_disable_unprepare(csi2rx->pixel_clk[i - 1]);
@@ -236,6 +307,13 @@ static void csi2rx_stop(struct csi2rx_priv *csi2rx)
 
 	if (v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, false))
 		dev_warn(csi2rx->dev, "Couldn't disable our subdev\n");
+
+	if (csi2rx->dphy) {
+		writel(0, csi2rx->base + CSI2RX_DPHY_LANE_CTRL_REG);
+
+		if (phy_power_off(csi2rx->dphy))
+			dev_warn(csi2rx->dev, "Couldn't power off DPHY\n");
+	}
 }
 
 static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable)
@@ -432,15 +510,6 @@ static int csi2rx_get_resources(struct csi2rx_priv *csi2rx,
 		return PTR_ERR(csi2rx->dphy);
 	}
 
-	/*
-	 * FIXME: Once we'll have external D-PHY support, the check
-	 * will need to be removed.
-	 */
-	if (csi2rx->dphy) {
-		dev_err(&pdev->dev, "External D-PHY not supported yet\n");
-		return -EINVAL;
-	}
-
 	ret = clk_prepare_enable(csi2rx->p_clk);
 	if (ret) {
 		dev_err(&pdev->dev, "Couldn't prepare and enable P clock\n");
@@ -470,7 +539,7 @@ static int csi2rx_get_resources(struct csi2rx_priv *csi2rx,
 	 * FIXME: Once we'll have internal D-PHY support, the check
 	 * will need to be removed.
 	 */
-	if (csi2rx->has_internal_dphy) {
+	if (!csi2rx->dphy && csi2rx->has_internal_dphy) {
 		dev_err(&pdev->dev, "Internal D-PHY not supported yet\n");
 		return -EINVAL;
 	}
@@ -595,6 +664,7 @@ static int csi2rx_probe(struct platform_device *pdev)
 	dev_info(&pdev->dev,
 		 "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n",
 		 csi2rx->num_lanes, csi2rx->max_lanes, csi2rx->max_streams,
+		 csi2rx->dphy ? "external" :
 		 csi2rx->has_internal_dphy ? "internal" : "no");
 
 	return 0;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 05/13] media: cadence: csi2rx: Soft reset the streams before starting capture
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (3 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 04/13] media: cadence: csi2rx: Add external DPHY support Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 06/13] media: cadence: csi2rx: Set the STOP bit when stopping a stream Vaishnav Achath
                   ` (9 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

This resets the stream state machines and FIFOs, giving them a clean
slate. On J721E if the streams are not reset before starting the
capture, the captured frame gets wrapped around vertically on every run
after the first.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v5)

Changes in v5:
- Make i unsigned.
- Add Laurent's R-by.

 drivers/media/platform/cadence/cdns-csi2rx.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index 6933626adfc8..8ecd889690b7 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -39,6 +39,7 @@
 #define CSI2RX_STREAM_BASE(n)		(((n) + 1) * 0x100)
 
 #define CSI2RX_STREAM_CTRL_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x000)
+#define CSI2RX_STREAM_CTRL_SOFT_RST			BIT(4)
 #define CSI2RX_STREAM_CTRL_START			BIT(0)
 
 #define CSI2RX_STREAM_DATA_CFG_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x008)
@@ -135,12 +136,22 @@ struct csi2rx_priv *v4l2_subdev_to_csi2rx(struct v4l2_subdev *subdev)
 
 static void csi2rx_reset(struct csi2rx_priv *csi2rx)
 {
+	unsigned int i;
+
 	writel(CSI2RX_SOFT_RESET_PROTOCOL | CSI2RX_SOFT_RESET_FRONT,
 	       csi2rx->base + CSI2RX_SOFT_RESET_REG);
 
 	udelay(10);
 
 	writel(0, csi2rx->base + CSI2RX_SOFT_RESET_REG);
+
+	/* Reset individual streams. */
+	for (i = 0; i < csi2rx->max_streams; i++) {
+		writel(CSI2RX_STREAM_CTRL_SOFT_RST,
+		       csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
+		usleep_range(10, 20);
+		writel(0, csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
+	}
 }
 
 static int csi2rx_configure_external_dphy(struct csi2rx_priv *csi2rx)
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 06/13] media: cadence: csi2rx: Set the STOP bit when stopping a stream
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (4 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 05/13] media: cadence: csi2rx: Soft reset the streams before starting capture Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 07/13] media: cadence: csi2rx: Fix stream data configuration Vaishnav Achath
                   ` (8 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

The stream stop procedure says that the STOP bit should be set when the
stream is to be stopped, and then the ready bit in stream status
register polled to make sure the STOP operation is finished.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v5)

Changes in v5:
- Change %d to %u
- Add Laurent's R-by.

 drivers/media/platform/cadence/cdns-csi2rx.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index 8ecd889690b7..e80220840e93 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -8,6 +8,7 @@
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/of_graph.h>
@@ -40,8 +41,12 @@
 
 #define CSI2RX_STREAM_CTRL_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x000)
 #define CSI2RX_STREAM_CTRL_SOFT_RST			BIT(4)
+#define CSI2RX_STREAM_CTRL_STOP				BIT(1)
 #define CSI2RX_STREAM_CTRL_START			BIT(0)
 
+#define CSI2RX_STREAM_STATUS_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x004)
+#define CSI2RX_STREAM_STATUS_RDY			BIT(31)
+
 #define CSI2RX_STREAM_DATA_CFG_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x008)
 #define CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT		BIT(31)
 #define CSI2RX_STREAM_DATA_CFG_VC_SELECT(n)		BIT((n) + 16)
@@ -304,12 +309,23 @@ static int csi2rx_start(struct csi2rx_priv *csi2rx)
 static void csi2rx_stop(struct csi2rx_priv *csi2rx)
 {
 	unsigned int i;
+	u32 val;
+	int ret;
 
 	clk_prepare_enable(csi2rx->p_clk);
 	clk_disable_unprepare(csi2rx->sys_clk);
 
 	for (i = 0; i < csi2rx->max_streams; i++) {
-		writel(0, csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
+		writel(CSI2RX_STREAM_CTRL_STOP,
+		       csi2rx->base + CSI2RX_STREAM_CTRL_REG(i));
+
+		ret = readl_relaxed_poll_timeout(csi2rx->base +
+						 CSI2RX_STREAM_STATUS_REG(i),
+						 val,
+						 (val & CSI2RX_STREAM_STATUS_RDY),
+						 10, 10000);
+		if (ret)
+			dev_warn(csi2rx->dev, "Failed to stop stream%u\n", i);
 
 		clk_disable_unprepare(csi2rx->pixel_clk[i]);
 	}
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 07/13] media: cadence: csi2rx: Fix stream data configuration
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (5 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 06/13] media: cadence: csi2rx: Set the STOP bit when stopping a stream Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 08/13] media: cadence: csi2rx: Populate subdev devnode Vaishnav Achath
                   ` (7 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

Firstly, there is no VC_EN bit present in the STREAM_DATA_CFG register.
Bit 31 is part of the VL_SELECT field. Remove it completely.

Secondly, it makes little sense to enable ith virtual channel for ith
stream. Sure, there might be a use-case that demands it. But there might
also be a use case that demands all streams to use the 0th virtual
channel. Prefer this case over the former because it is less arbitrary
and also makes it very clear what the limitations of the current driver
is instead of giving a false impression that multiple virtual channels
are supported.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v1)

 drivers/media/platform/cadence/cdns-csi2rx.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index e80220840e93..f8ce9360bfb0 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -48,7 +48,6 @@
 #define CSI2RX_STREAM_STATUS_RDY			BIT(31)
 
 #define CSI2RX_STREAM_DATA_CFG_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x008)
-#define CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT		BIT(31)
 #define CSI2RX_STREAM_DATA_CFG_VC_SELECT(n)		BIT((n) + 16)
 
 #define CSI2RX_STREAM_CFG_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x00c)
@@ -269,8 +268,11 @@ static int csi2rx_start(struct csi2rx_priv *csi2rx)
 		writel(CSI2RX_STREAM_CFG_FIFO_MODE_LARGE_BUF,
 		       csi2rx->base + CSI2RX_STREAM_CFG_REG(i));
 
-		writel(CSI2RX_STREAM_DATA_CFG_EN_VC_SELECT |
-		       CSI2RX_STREAM_DATA_CFG_VC_SELECT(i),
+		/*
+		 * Enable one virtual channel. When multiple virtual channels
+		 * are supported this will have to be changed.
+		 */
+		writel(CSI2RX_STREAM_DATA_CFG_VC_SELECT(0),
 		       csi2rx->base + CSI2RX_STREAM_DATA_CFG_REG(i));
 
 		writel(CSI2RX_STREAM_CTRL_START,
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 08/13] media: cadence: csi2rx: Populate subdev devnode
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (6 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 07/13] media: cadence: csi2rx: Fix stream data configuration Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 09/13] media: cadence: csi2rx: Add link validation Vaishnav Achath
                   ` (6 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

The devnode can be used by media-ctl and other userspace tools to
perform configurations on the subdev. Without it, media-ctl returns
ENOENT when setting format on the sensor subdev.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v5)

Changes in v5:
- Add Laurent's R-by.

Changes in v2:
- New in v2.

 drivers/media/platform/cadence/cdns-csi2rx.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index f8ce9360bfb0..639ade97b998 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -676,6 +676,7 @@ static int csi2rx_probe(struct platform_device *pdev)
 	csi2rx->pads[CSI2RX_PAD_SINK].flags = MEDIA_PAD_FL_SINK;
 	for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++)
 		csi2rx->pads[i].flags = MEDIA_PAD_FL_SOURCE;
+	csi2rx->subdev.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
 
 	ret = media_entity_pads_init(&csi2rx->subdev.entity, CSI2RX_PAD_MAX,
 				     csi2rx->pads);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 09/13] media: cadence: csi2rx: Add link validation
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (7 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 08/13] media: cadence: csi2rx: Populate subdev devnode Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E Vaishnav Achath
                   ` (5 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

Add media link validation to make sure incorrectly configured pipelines
are caught.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v6)

Changes in v6:
- Drop transcoding from the commit message.
- Make csi2rx_media_ops const.
- Add Laurent's R-by.

Changes in v5:
- New in v5.

 drivers/media/platform/cadence/cdns-csi2rx.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
index 639ade97b998..026d95d60654 100644
--- a/drivers/media/platform/cadence/cdns-csi2rx.c
+++ b/drivers/media/platform/cadence/cdns-csi2rx.c
@@ -478,6 +478,10 @@ static const struct v4l2_subdev_ops csi2rx_subdev_ops = {
 	.pad		= &csi2rx_pad_ops,
 };
 
+static const struct media_entity_operations csi2rx_media_ops = {
+	.link_validate = v4l2_subdev_link_validate,
+};
+
 static int csi2rx_async_bound(struct v4l2_async_notifier *notifier,
 			      struct v4l2_subdev *s_subdev,
 			      struct v4l2_async_subdev *asd)
@@ -677,6 +681,7 @@ static int csi2rx_probe(struct platform_device *pdev)
 	for (i = CSI2RX_PAD_SOURCE_STREAM0; i < CSI2RX_PAD_MAX; i++)
 		csi2rx->pads[i].flags = MEDIA_PAD_FL_SOURCE;
 	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);
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (8 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 09/13] media: cadence: csi2rx: Add link validation Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-20 12:51   ` Jai Luthra
                     ` (2 more replies)
  2023-03-14 11:55 ` [PATCH v7 11/13] media: dt-bindings: Make sure items in data-lanes are unique Vaishnav Achath
                   ` (4 subsequent siblings)
  14 siblings, 3 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

TI's J721E uses the Cadence CSI2RX and DPHY peripherals to facilitate
capture over a CSI-2 bus.

The Cadence CSI2RX IP acts as a bridge between the TI specific parts and
the CSI-2 protocol parts. TI then has a wrapper on top of this bridge
called the SHIM layer. It takes in data from stream 0, repacks it, and
sends it to memory over PSI-L DMA.

This driver acts as the "front end" to V4L2 client applications. It
implements the required ioctls and buffer operations, passes the
necessary calls on to the bridge, programs the SHIM layer, and performs
DMA via the dmaengine API to finally return the data to a buffer
supplied by the application.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Jai Luthra <j-luthra@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
---

Changes in v7:
- Fix incorrect value written in SHIM_PSI_CFG0_DST_TAG
- Drop support for 2X8 formats.
- Update maintainer to Vaishnav as Pratyush moved out of TI.
- Address Sakari's review comments:
- Update MAX_HEIGHT_LINES, MAX_WIDTH_BYTES to prevent overflow.
- Assign dma_slave_config during declaration, drop memset().
- dma_release_channel() on slave_config failure.
- provide entity ops for the vdev entity with link_validate().
- mutex_destroy() on ti_csi2rx_probe failure path.
- Drop busy check in remove().
- mutex_destroy() in ti_csi2rx_remove().
- Address Laurent's review comments:
- Update entries in Makefile in alphabetical order.
- include headers in alphabetical order.
- Drop redundant CSI DT defines and use from media/mipi-csi2.h.
- Rename csi_df to csi_dt.
- Drop v4l2_colorspace from ti_csi2rx_fmt and set default in
  ti_csi2rx_v4l2_init()
- Adjust field and not return EINVAL in ti_csi2rx_try_fmt_vid_cap().
- inline ti_csi2rx_video_register().
- start DMA before starting source subdev.
- move buffer cleanup to separate function ti_csi2rx_cleanup_buffers()
  to be used in ti_csi2rx_stop_streaming() and ti_csi2rx_start_streaming()
  failure path.
- Drop VB2_USERPTR, VB2_READ and V4L2_CAP_READWRITE.

Changes in v5:
- Add dependency on PHY_CADENCE_DPHY_RX instead of PHY_CADENCE_DPHY
  since the Rx mode DPHY now has a separate driver.
- Drop ti_csi2rx_validate_pipeline(). Pipeline validation should be done
  at media_pipeline_start().
- Do not assign flags.
- Fix error handling in ti_csi2rx_start_streaming(). Free up vb2 buffers
  when media_pipeline_start() fails.

Changes in v4:
- Acquire the media device's graph_mutex before starting the graph walk.
- Call media_graph_walk_init() and media_graph_walk_cleanup() when
  starting and ending the graph walk respectively.
- Reduce max frame height and width in enum_framesizes. Currently they
  are set to UINT_MAX but they must be a multiple of step_width, so they
  need to be rounded down. Also, these values are absurdly large which
  causes some userspace applications like gstreamer to trip up. While it
  is not generally right to change the kernel for an application bug, it
  is not such a big deal here. This change is replacing one set of
  absurdly large arbitrary values with another set of smaller but still
  absurdly large arbitrary values. Both limits are unlikely to be hit in
  practice.

Changes in v3:
- Move DMA related fields in struct ti_csi2rx_dma.
- Protect DMA buffer queue with a spinlock to make sure the queue buffer
  and DMA callback don't race on it.
- Track the current DMA state. It might go idle because of a lack of
  buffers. This state can be used to restart it if needed.
- Do not include the current buffer in the pending queue. It is slightly
  better modelling than leaving it at the head of the pending queue.
- Use the buffer as the callback argument, and add a reference to csi in it.
- If queueing a buffer to DMA fails, the buffer gets leaked and DMA gets
  stalled with. Instead, report the error to vb2 and queue the next
  buffer in the pending queue.
- DMA gets stalled if we run out of buffers since the callback is the
  only one that fires subsequent transfers and it is no longer being
  called. Check for that when queueing buffers and restart DMA if
  needed.
- Do not put of node until we are done using the fwnode.
- Set inital format to UYVY 640x480.

Changes in v2:
- Use dmaengine_get_dma_device() instead of directly accessing
  dma->device->dev.
- Do not set dst_addr_width when configuring slave DMA.
- Move to a separate subdir and rename to j721e-csi2rx.c
- Convert compatible to ti,j721e-csi2rx.
- Move to use Media Controller centric APIs.
- Improve cleanup in probe when one of the steps fails.
- Add colorspace to formats database.
- Set hw_revision on media_device.
- Move video device initialization to probe time instead of register time.

 MAINTAINERS                                   |    6 +
 drivers/media/platform/ti/Kconfig             |   12 +
 drivers/media/platform/ti/Makefile            |    1 +
 .../media/platform/ti/j721e-csi2rx/Makefile   |    2 +
 .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 1022 +++++++++++++++++
 5 files changed, 1043 insertions(+)
 create mode 100644 drivers/media/platform/ti/j721e-csi2rx/Makefile
 create mode 100644 drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 2892858cb040..69c2b0f48073 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -20901,6 +20901,12 @@ S:	Odd Fixes
 F:	drivers/clk/ti/
 F:	include/linux/clk/ti.h
 
+TI J721E CSI2RX DRIVER
+M:	Vaishnav Achath <vaishnav.a@ti.com>
+L:	linux-media@vger.kernel.org
+S:	Supported
+F:	drivers/media/platform/ti/j721e-csi2rx/
+
 TI DAVINCI MACHINE SUPPORT
 M:	Bartosz Golaszewski <brgl@bgdev.pl>
 L:	linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
diff --git a/drivers/media/platform/ti/Kconfig b/drivers/media/platform/ti/Kconfig
index e1ab56c3be1f..42c908f6e1ae 100644
--- a/drivers/media/platform/ti/Kconfig
+++ b/drivers/media/platform/ti/Kconfig
@@ -63,6 +63,18 @@ config VIDEO_TI_VPE_DEBUG
 	help
 	  Enable debug messages on VPE driver.
 
+config VIDEO_TI_J721E_CSI2RX
+	tristate "TI J721E CSI2RX wrapper layer driver"
+	depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
+	depends on MEDIA_SUPPORT && MEDIA_CONTROLLER
+	depends on PHY_CADENCE_DPHY_RX && VIDEO_CADENCE_CSI2RX
+	depends on ARCH_K3 || COMPILE_TEST
+	select VIDEOBUF2_DMA_CONTIG
+	select V4L2_FWNODE
+	help
+	  Support for TI CSI2RX wrapper layer. This just enables the wrapper driver.
+	  The Cadence CSI2RX bridge driver needs to be enabled separately.
+
 source "drivers/media/platform/ti/am437x/Kconfig"
 source "drivers/media/platform/ti/davinci/Kconfig"
 source "drivers/media/platform/ti/omap/Kconfig"
diff --git a/drivers/media/platform/ti/Makefile b/drivers/media/platform/ti/Makefile
index 98c5fe5c40d6..8a2f74c9380e 100644
--- a/drivers/media/platform/ti/Makefile
+++ b/drivers/media/platform/ti/Makefile
@@ -3,5 +3,6 @@ obj-y += am437x/
 obj-y += cal/
 obj-y += vpe/
 obj-y += davinci/
+obj-y += j721e-csi2rx/
 obj-y += omap/
 obj-y += omap3isp/
diff --git a/drivers/media/platform/ti/j721e-csi2rx/Makefile b/drivers/media/platform/ti/j721e-csi2rx/Makefile
new file mode 100644
index 000000000000..377afc1d6280
--- /dev/null
+++ b/drivers/media/platform/ti/j721e-csi2rx/Makefile
@@ -0,0 +1,2 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_VIDEO_TI_J721E_CSI2RX) += j721e-csi2rx.o
diff --git a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
new file mode 100644
index 000000000000..0c8dad049f5b
--- /dev/null
+++ b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
@@ -0,0 +1,1022 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * TI CSI2 RX driver.
+ *
+ * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com/
+ *
+ * Author: Pratyush Yadav <p.yadav@ti.com>
+ */
+
+#include <linux/bitfield.h>
+#include <linux/dmaengine.h>
+#include <linux/module.h>
+#include <linux/of_platform.h>
+#include <linux/platform_device.h>
+
+#include <media/mipi-csi2.h>
+#include <media/v4l2-device.h>
+#include <media/v4l2-ioctl.h>
+#include <media/videobuf2-dma-contig.h>
+
+#define TI_CSI2RX_MODULE_NAME		"j721e-csi2rx"
+
+#define SHIM_CNTL			0x10
+#define SHIM_CNTL_PIX_RST		BIT(0)
+
+#define SHIM_DMACNTX			0x20
+#define SHIM_DMACNTX_EN			BIT(31)
+#define SHIM_DMACNTX_YUV422		GENMASK(27, 26)
+#define SHIM_DMACNTX_FMT		GENMASK(5, 0)
+#define SHIM_DMACNTX_UYVY		0
+#define SHIM_DMACNTX_VYUY		1
+#define SHIM_DMACNTX_YUYV		2
+#define SHIM_DMACNTX_YVYU		3
+
+#define SHIM_PSI_CFG0			0x24
+#define SHIM_PSI_CFG0_SRC_TAG		GENMASK(15, 0)
+#define SHIM_PSI_CFG0_DST_TAG		GENMASK(31, 15)
+
+#define PSIL_WORD_SIZE_BYTES		16
+/*
+ * There are no hard limits on the width or height. The DMA engine can handle
+ * all sizes. The max width and height are arbitrary numbers for this driver.
+ * Use 16K * 16K as the arbitrary limit. It is large enough that it is unlikely
+ * the limit will be hit in practice.
+ */
+#define MAX_WIDTH_BYTES			SZ_16K
+#define MAX_HEIGHT_LINES		SZ_16K
+
+struct ti_csi2rx_fmt {
+	u32				fourcc;	/* Four character code. */
+	u32				code;	/* Mbus code. */
+	u32				csi_dt;	/* CSI Data type. */
+	u8				bpp;	/* Bits per pixel. */
+};
+
+struct ti_csi2rx_buffer {
+	/* Common v4l2 buffer. Must be first. */
+	struct vb2_v4l2_buffer		vb;
+	struct list_head		list;
+	struct ti_csi2rx_dev		*csi;
+};
+
+enum ti_csi2rx_dma_state {
+	TI_CSI2RX_DMA_STOPPED,	/* Streaming not started yet. */
+	TI_CSI2RX_DMA_IDLE,	/* Streaming but no pending DMA operation. */
+	TI_CSI2RX_DMA_ACTIVE,	/* Streaming and pending DMA operation. */
+};
+
+struct ti_csi2rx_dma {
+	/* Protects all fields in this struct. */
+	spinlock_t			lock;
+	struct dma_chan			*chan;
+	/* Buffers queued to the driver, waiting to be processed by DMA. */
+	struct list_head		queue;
+	enum ti_csi2rx_dma_state	state;
+	/*
+	 * Current buffer being processed by DMA. NULL if no buffer is being
+	 * processed.
+	 */
+	struct ti_csi2rx_buffer		*curr;
+};
+
+struct ti_csi2rx_dev {
+	struct device			*dev;
+	void __iomem			*shim;
+	struct v4l2_device		v4l2_dev;
+	struct video_device		vdev;
+	struct media_device		mdev;
+	struct media_pipeline		pipe;
+	struct media_pad		pad;
+	struct v4l2_async_notifier	notifier;
+	struct v4l2_subdev		*subdev;
+	struct vb2_queue		vidq;
+	struct mutex			mutex; /* To serialize ioctls. */
+	struct v4l2_format		v_fmt;
+	struct ti_csi2rx_dma		dma;
+	u32				sequence;
+};
+
+static const struct ti_csi2rx_fmt formats[] = {
+	{
+		.fourcc			= V4L2_PIX_FMT_YUYV,
+		.code			= MEDIA_BUS_FMT_YUYV8_1X16,
+		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
+		.bpp			= 16,
+	}, {
+		.fourcc			= V4L2_PIX_FMT_UYVY,
+		.code			= MEDIA_BUS_FMT_UYVY8_1X16,
+		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
+		.bpp			= 16,
+	}, {
+		.fourcc			= V4L2_PIX_FMT_YVYU,
+		.code			= MEDIA_BUS_FMT_YVYU8_1X16,
+		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
+		.bpp			= 16,
+	}, {
+		.fourcc			= V4L2_PIX_FMT_VYUY,
+		.code			= MEDIA_BUS_FMT_VYUY8_1X16,
+		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
+		.bpp			= 16,
+	},
+
+	/* More formats can be supported but they are not listed for now. */
+};
+
+static const unsigned int num_formats = ARRAY_SIZE(formats);
+
+/* Forward declaration needed by ti_csi2rx_dma_callback. */
+static int ti_csi2rx_start_dma(struct ti_csi2rx_dev *csi,
+			       struct ti_csi2rx_buffer *buf);
+
+static const struct ti_csi2rx_fmt *find_format_by_pix(u32 pixelformat)
+{
+	unsigned int i;
+
+	for (i = 0; i < num_formats; i++) {
+		if (formats[i].fourcc == pixelformat)
+			return &formats[i];
+	}
+
+	return NULL;
+}
+
+static const struct ti_csi2rx_fmt *find_format_by_code(u32 code)
+{
+	unsigned int i;
+
+	for (i = 0; i < num_formats; i++) {
+		if (formats[i].code == code)
+			return &formats[i];
+	}
+
+	return NULL;
+}
+
+static void ti_csi2rx_fill_fmt(const struct ti_csi2rx_fmt *csi_fmt,
+			       struct v4l2_format *v4l2_fmt)
+{
+	struct v4l2_pix_format *pix = &v4l2_fmt->fmt.pix;
+	unsigned int pixels_in_word;
+	u8 bpp = csi_fmt->bpp;
+	u32 bpl;
+
+	/* TODO: Figure this out. */
+
+	pixels_in_word = PSIL_WORD_SIZE_BYTES * 8 / bpp;
+
+	pix->width = clamp_t(unsigned int, pix->width,
+			     pixels_in_word,
+			     MAX_WIDTH_BYTES * 8 / bpp);
+	pix->width = rounddown(pix->width, pixels_in_word);
+
+	pix->height = clamp_t(unsigned int, pix->height, 1, MAX_HEIGHT_LINES);
+
+	v4l2_fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	pix->pixelformat = csi_fmt->fourcc;
+	pix->colorspace = V4L2_COLORSPACE_SRGB;
+	pix->sizeimage = pix->height * pix->width * (bpp / 8);
+
+	bpl = (pix->width * ALIGN(bpp, 8)) >> 3;
+	pix->bytesperline = ALIGN(bpl, 16);
+}
+
+static int ti_csi2rx_querycap(struct file *file, void *priv,
+			      struct v4l2_capability *cap)
+{
+	struct ti_csi2rx_dev *csi = video_drvdata(file);
+
+	strscpy(cap->driver, TI_CSI2RX_MODULE_NAME, sizeof(cap->driver));
+	strscpy(cap->card, TI_CSI2RX_MODULE_NAME, sizeof(cap->card));
+
+	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
+		 dev_name(csi->dev));
+
+	return 0;
+}
+
+static int ti_csi2rx_enum_fmt_vid_cap(struct file *file, void *priv,
+				      struct v4l2_fmtdesc *f)
+{
+	if (f->index >= num_formats)
+		return -EINVAL;
+
+	memset(f->reserved, 0, sizeof(f->reserved));
+	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	f->pixelformat = formats[f->index].fourcc;
+
+	return 0;
+}
+
+static int ti_csi2rx_g_fmt_vid_cap(struct file *file, void *prov,
+				   struct v4l2_format *f)
+{
+	struct ti_csi2rx_dev *csi = video_drvdata(file);
+
+	*f = csi->v_fmt;
+
+	return 0;
+}
+
+static int ti_csi2rx_try_fmt_vid_cap(struct file *file, void *priv,
+				     struct v4l2_format *f)
+{
+	const struct ti_csi2rx_fmt *fmt;
+
+	/*
+	 * Default to the first format if the requested pixel format code isn't
+	 * supported.
+	 */
+	fmt = find_format_by_pix(f->fmt.pix.pixelformat);
+	if (!fmt)
+		fmt = &formats[0];
+
+	if (f->fmt.pix.field == V4L2_FIELD_ANY)
+		f->fmt.pix.field = V4L2_FIELD_NONE;
+
+	if (f->fmt.pix.field != V4L2_FIELD_NONE)
+		/* Interlaced formats are not supported. */
+		f->fmt.pix.field = V4L2_FIELD_NONE;
+
+	ti_csi2rx_fill_fmt(fmt, f);
+
+	return 0;
+}
+
+static int ti_csi2rx_s_fmt_vid_cap(struct file *file, void *priv,
+				   struct v4l2_format *f)
+{
+	struct ti_csi2rx_dev *csi = video_drvdata(file);
+	struct vb2_queue *q = &csi->vidq;
+	int ret;
+
+	if (vb2_is_busy(q))
+		return -EBUSY;
+
+	ret = ti_csi2rx_try_fmt_vid_cap(file, priv, f);
+	if (ret < 0)
+		return ret;
+
+	csi->v_fmt = *f;
+
+	return 0;
+}
+
+static int ti_csi2rx_enum_framesizes(struct file *file, void *fh,
+				     struct v4l2_frmsizeenum *fsize)
+{
+	const struct ti_csi2rx_fmt *fmt;
+	unsigned int pixels_in_word;
+	u8 bpp;
+
+	fmt = find_format_by_pix(fsize->pixel_format);
+	if (!fmt)
+		return -EINVAL;
+
+	bpp = ALIGN(fmt->bpp, 8);
+
+	/*
+	 * Number of pixels in one PSI-L word. The transfer happens in multiples
+	 * of PSI-L word sizes.
+	 */
+	pixels_in_word = PSIL_WORD_SIZE_BYTES * 8 / bpp;
+
+	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
+	fsize->stepwise.min_width = pixels_in_word;
+	fsize->stepwise.max_width = rounddown(MAX_WIDTH_BYTES * 8 / bpp,
+					      pixels_in_word);
+	fsize->stepwise.step_width = pixels_in_word;
+	fsize->stepwise.min_height = 1;
+	fsize->stepwise.max_height = MAX_HEIGHT_LINES;
+	fsize->stepwise.step_height = 1;
+
+	return 0;
+}
+
+static const struct v4l2_ioctl_ops csi_ioctl_ops = {
+	.vidioc_querycap      = ti_csi2rx_querycap,
+	.vidioc_enum_fmt_vid_cap = ti_csi2rx_enum_fmt_vid_cap,
+	.vidioc_try_fmt_vid_cap = ti_csi2rx_try_fmt_vid_cap,
+	.vidioc_g_fmt_vid_cap = ti_csi2rx_g_fmt_vid_cap,
+	.vidioc_s_fmt_vid_cap = ti_csi2rx_s_fmt_vid_cap,
+	.vidioc_enum_framesizes = ti_csi2rx_enum_framesizes,
+	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
+	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
+	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
+	.vidioc_querybuf      = vb2_ioctl_querybuf,
+	.vidioc_qbuf          = vb2_ioctl_qbuf,
+	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
+	.vidioc_expbuf        = vb2_ioctl_expbuf,
+	.vidioc_streamon      = vb2_ioctl_streamon,
+	.vidioc_streamoff     = vb2_ioctl_streamoff,
+};
+
+static const struct v4l2_file_operations csi_fops = {
+	.owner = THIS_MODULE,
+	.open = v4l2_fh_open,
+	.release = vb2_fop_release,
+	.read = vb2_fop_read,
+	.poll = vb2_fop_poll,
+	.unlocked_ioctl = video_ioctl2,
+	.mmap = vb2_fop_mmap,
+};
+
+static inline int ti_csi2rx_video_register(struct ti_csi2rx_dev *csi)
+{
+	struct video_device *vdev = &csi->vdev;
+	int ret, src_pad;
+
+	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
+	if (ret)
+		return ret;
+
+	src_pad = media_entity_get_fwnode_pad(&csi->subdev->entity,
+					      csi->subdev->fwnode,
+					      MEDIA_PAD_FL_SOURCE);
+	if (src_pad < 0) {
+		dev_err(csi->dev, "Couldn't find source pad for subdev\n");
+		return src_pad;
+	}
+
+	ret = media_create_pad_link(&csi->subdev->entity, src_pad,
+				    &vdev->entity, 0,
+				    MEDIA_LNK_FL_IMMUTABLE |
+				    MEDIA_LNK_FL_ENABLED);
+	if (ret) {
+		video_unregister_device(vdev);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int csi_async_notifier_bound(struct v4l2_async_notifier *notifier,
+				    struct v4l2_subdev *subdev,
+				    struct v4l2_async_subdev *asd)
+{
+	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
+
+	csi->subdev = subdev;
+
+	return 0;
+}
+
+static int csi_async_notifier_complete(struct v4l2_async_notifier *notifier)
+{
+	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
+	int ret;
+
+	ret = ti_csi2rx_video_register(csi);
+	if (ret)
+		return ret;
+
+	return v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
+}
+
+static const struct v4l2_async_notifier_operations csi_async_notifier_ops = {
+	.bound = csi_async_notifier_bound,
+	.complete = csi_async_notifier_complete,
+};
+
+static int ti_csi2rx_init_subdev(struct ti_csi2rx_dev *csi)
+{
+	struct fwnode_handle *fwnode;
+	struct v4l2_async_subdev *asd;
+	struct device_node *node;
+	int ret;
+
+	node = of_get_child_by_name(csi->dev->of_node, "csi-bridge");
+	if (!node)
+		return -EINVAL;
+
+	fwnode = of_fwnode_handle(node);
+	if (!fwnode) {
+		of_node_put(node);
+		return -EINVAL;
+	}
+
+	v4l2_async_nf_init(&csi->notifier);
+	csi->notifier.ops = &csi_async_notifier_ops;
+
+	asd = v4l2_async_nf_add_fwnode(&csi->notifier, fwnode,
+				       struct v4l2_async_subdev);
+	of_node_put(node);
+	if (IS_ERR(asd)) {
+		v4l2_async_nf_cleanup(&csi->notifier);
+		return PTR_ERR(asd);
+	}
+
+	ret = v4l2_async_nf_register(&csi->v4l2_dev, &csi->notifier);
+	if (ret) {
+		v4l2_async_nf_cleanup(&csi->notifier);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void ti_csi2rx_setup_shim(struct ti_csi2rx_dev *csi)
+{
+	const struct ti_csi2rx_fmt *fmt;
+	unsigned int reg;
+
+	fmt = find_format_by_pix(csi->v_fmt.fmt.pix.pixelformat);
+	if (!fmt) {
+		dev_err(csi->dev, "Unknown format\n");
+		return;
+	}
+
+	/* De-assert the pixel interface reset. */
+	reg = SHIM_CNTL_PIX_RST;
+	writel(reg, csi->shim + SHIM_CNTL);
+
+	reg = SHIM_DMACNTX_EN;
+	reg |= FIELD_PREP(SHIM_DMACNTX_FMT, fmt->csi_dt);
+
+	/*
+	 * Using the values from the documentation gives incorrect ordering for
+	 * the luma and chroma components. In practice, the "reverse" format
+	 * gives the correct image. So for example, if the image is in UYVY, the
+	 * reverse would be YVYU.
+	 */
+	switch (fmt->fourcc) {
+	case V4L2_PIX_FMT_UYVY:
+		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
+					SHIM_DMACNTX_YVYU);
+		break;
+	case V4L2_PIX_FMT_VYUY:
+		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
+					SHIM_DMACNTX_YUYV);
+		break;
+	case V4L2_PIX_FMT_YUYV:
+		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
+					SHIM_DMACNTX_VYUY);
+		break;
+	case V4L2_PIX_FMT_YVYU:
+		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
+					SHIM_DMACNTX_UYVY);
+		break;
+	default:
+		/* Ignore if not YUV 4:2:2 */
+		break;
+	}
+
+	writel(reg, csi->shim + SHIM_DMACNTX);
+
+	reg = FIELD_PREP(SHIM_PSI_CFG0_SRC_TAG, 0) |
+	      FIELD_PREP(SHIM_PSI_CFG0_DST_TAG, 0);
+	writel(reg, csi->shim + SHIM_PSI_CFG0);
+}
+
+static void ti_csi2rx_dma_callback(void *param)
+{
+	struct ti_csi2rx_buffer *buf = param;
+	struct ti_csi2rx_dev *csi = buf->csi;
+	struct ti_csi2rx_dma *dma = &csi->dma;
+	unsigned long flags;
+
+	buf->vb.vb2_buf.timestamp = ktime_get_ns();
+	buf->vb.sequence = csi->sequence++;
+
+	spin_lock_irqsave(&dma->lock, flags);
+
+	WARN_ON(dma->curr != buf);
+	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
+
+	/* If there are more buffers to process then start their transfer. */
+	dma->curr = NULL;
+	while (!list_empty(&dma->queue)) {
+		buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
+		list_del(&buf->list);
+
+		if (ti_csi2rx_start_dma(csi, buf)) {
+			dev_err(csi->dev, "Failed to queue the next buffer for DMA\n");
+			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+		} else {
+			dma->curr = buf;
+			break;
+		}
+	}
+
+	if (!dma->curr)
+		dma->state = TI_CSI2RX_DMA_IDLE;
+
+	spin_unlock_irqrestore(&dma->lock, flags);
+}
+
+static int ti_csi2rx_start_dma(struct ti_csi2rx_dev *csi,
+			       struct ti_csi2rx_buffer *buf)
+{
+	unsigned long addr;
+	struct dma_async_tx_descriptor *desc;
+	size_t len = csi->v_fmt.fmt.pix.sizeimage;
+	dma_cookie_t cookie;
+	int ret = 0;
+
+	addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
+	desc = dmaengine_prep_slave_single(csi->dma.chan, addr, len,
+					   DMA_DEV_TO_MEM,
+					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+	if (!desc)
+		return -EIO;
+
+	desc->callback = ti_csi2rx_dma_callback;
+	desc->callback_param = buf;
+
+	cookie = dmaengine_submit(desc);
+	ret = dma_submit_error(cookie);
+	if (ret)
+		return ret;
+
+	dma_async_issue_pending(csi->dma.chan);
+
+	return 0;
+}
+
+static void ti_csi2rx_cleanup_buffers(struct ti_csi2rx_dev *csi,
+				      enum vb2_buffer_state state)
+{
+	struct ti_csi2rx_dma *dma = &csi->dma;
+	struct ti_csi2rx_buffer *buf = NULL, *tmp;
+	unsigned long flags;
+
+	spin_lock_irqsave(&dma->lock, flags);
+	list_for_each_entry_safe(buf, tmp, &csi->dma.queue, list) {
+		list_del(&buf->list);
+		vb2_buffer_done(&buf->vb.vb2_buf, state);
+	}
+
+	if (dma->curr)
+		vb2_buffer_done(&dma->curr->vb.vb2_buf, state);
+
+	dma->curr = NULL;
+	dma->state = TI_CSI2RX_DMA_STOPPED;
+	spin_unlock_irqrestore(&dma->lock, flags);
+}
+
+static int ti_csi2rx_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
+				 unsigned int *nplanes, unsigned int sizes[],
+				 struct device *alloc_devs[])
+{
+	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(q);
+	unsigned int size = csi->v_fmt.fmt.pix.sizeimage;
+
+	if (*nplanes) {
+		if (sizes[0] < size)
+			return -EINVAL;
+		size = sizes[0];
+	}
+
+	*nplanes = 1;
+	sizes[0] = size;
+
+	return 0;
+}
+
+static int ti_csi2rx_buffer_prepare(struct vb2_buffer *vb)
+{
+	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
+	unsigned long size = csi->v_fmt.fmt.pix.sizeimage;
+
+	if (vb2_plane_size(vb, 0) < size) {
+		dev_err(csi->dev, "Data will not fit into plane\n");
+		return -EINVAL;
+	}
+
+	vb2_set_plane_payload(vb, 0, size);
+	return 0;
+}
+
+static void ti_csi2rx_buffer_queue(struct vb2_buffer *vb)
+{
+	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
+	struct ti_csi2rx_buffer *buf;
+	struct ti_csi2rx_dma *dma = &csi->dma;
+	unsigned long flags = 0;
+	int ret;
+
+	buf = container_of(vb, struct ti_csi2rx_buffer, vb.vb2_buf);
+	buf->csi = csi;
+
+	spin_lock_irqsave(&dma->lock, flags);
+	/*
+	 * Usually the DMA callback takes care of queueing the pending buffers.
+	 * But if DMA has stalled due to lack of buffers, restart it now.
+	 */
+	if (dma->state == TI_CSI2RX_DMA_IDLE) {
+		ret = ti_csi2rx_start_dma(csi, buf);
+		if (ret) {
+			dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
+			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
+			goto unlock;
+		}
+
+		dma->curr = buf;
+		dma->state = TI_CSI2RX_DMA_ACTIVE;
+	} else {
+		list_add_tail(&buf->list, &dma->queue);
+	}
+
+unlock:
+	spin_unlock_irqrestore(&dma->lock, flags);
+}
+
+static int ti_csi2rx_start_streaming(struct vb2_queue *vq, unsigned int count)
+{
+	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
+	struct ti_csi2rx_dma *dma = &csi->dma;
+	struct ti_csi2rx_buffer *buf;
+	unsigned long flags;
+	int ret = 0;
+
+	spin_lock_irqsave(&dma->lock, flags);
+	if (list_empty(&dma->queue))
+		ret = -EIO;
+	spin_unlock_irqrestore(&dma->lock, flags);
+	if (ret)
+		return ret;
+
+	ret = media_pipeline_start(&csi->vdev.entity.pads[0], &csi->pipe);
+	if (ret)
+		goto err;
+
+	ti_csi2rx_setup_shim(csi);
+
+	csi->sequence = 0;
+
+	spin_lock_irqsave(&dma->lock, flags);
+	buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
+	list_del(&buf->list);
+	dma->state = TI_CSI2RX_DMA_ACTIVE;
+	dma->curr = buf;
+
+	ret = ti_csi2rx_start_dma(csi, buf);
+	if (ret) {
+		dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
+		spin_unlock_irqrestore(&dma->lock, flags);
+		goto err_pipeline;
+	}
+
+	spin_unlock_irqrestore(&dma->lock, flags);
+
+	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 1);
+	if (ret)
+		goto err_dma;
+
+	return 0;
+
+err_dma:
+	dmaengine_terminate_sync(csi->dma.chan);
+	writel(0, csi->shim + SHIM_DMACNTX);
+err_pipeline:
+	media_pipeline_stop(&csi->vdev.entity.pads[0]);
+err:
+	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_QUEUED);
+	return ret;
+}
+
+static void ti_csi2rx_stop_streaming(struct vb2_queue *vq)
+{
+	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
+	int ret;
+
+	media_pipeline_stop(&csi->vdev.entity.pads[0]);
+
+	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 0);
+	if (ret)
+		dev_err(csi->dev, "Failed to stop subdev stream\n");
+
+	writel(0, csi->shim + SHIM_CNTL);
+
+	ret = dmaengine_terminate_sync(csi->dma.chan);
+	if (ret)
+		dev_err(csi->dev, "Failed to stop DMA: %d\n", ret);
+
+	writel(0, csi->shim + SHIM_DMACNTX);
+
+	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_ERROR);
+}
+
+static const struct vb2_ops csi_vb2_qops = {
+	.queue_setup = ti_csi2rx_queue_setup,
+	.buf_prepare = ti_csi2rx_buffer_prepare,
+	.buf_queue = ti_csi2rx_buffer_queue,
+	.start_streaming = ti_csi2rx_start_streaming,
+	.stop_streaming = ti_csi2rx_stop_streaming,
+	.wait_prepare = vb2_ops_wait_prepare,
+	.wait_finish = vb2_ops_wait_finish,
+};
+
+static int ti_csi2rx_init_vb2q(struct ti_csi2rx_dev *csi)
+{
+	struct vb2_queue *q = &csi->vidq;
+	int ret;
+
+	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
+	q->drv_priv = csi;
+	q->buf_struct_size = sizeof(struct ti_csi2rx_buffer);
+	q->ops = &csi_vb2_qops;
+	q->mem_ops = &vb2_dma_contig_memops;
+	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	q->dev = dmaengine_get_dma_device(csi->dma.chan);
+	q->lock = &csi->mutex;
+	q->min_buffers_needed = 1;
+
+	ret = vb2_queue_init(q);
+	if (ret)
+		return ret;
+
+	csi->vdev.queue = q;
+
+	return 0;
+}
+
+static int ti_csi2rx_link_validate_get_fmt(struct media_pad *pad,
+					   struct v4l2_subdev_format *fmt)
+{
+	if (is_media_entity_v4l2_subdev(pad->entity)) {
+		struct v4l2_subdev *sd =
+			media_entity_to_v4l2_subdev(pad->entity);
+
+		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
+		fmt->pad = pad->index;
+		return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
+	}
+
+	WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
+	     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
+	     pad->entity->function, pad->entity->name);
+
+	return -EINVAL;
+}
+
+static int ti_csi2rx_link_validate(struct media_link *link)
+{
+	struct media_entity *entity = link->sink->entity;
+	struct video_device *vdev = media_entity_to_video_device(entity);
+	struct ti_csi2rx_dev *csi = container_of(vdev, struct ti_csi2rx_dev, vdev);
+	struct v4l2_pix_format *csi_fmt = &csi->v_fmt.fmt.pix;
+	struct v4l2_subdev_format source_fmt;
+	const struct ti_csi2rx_fmt *ti_fmt;
+	int ret;
+
+	if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
+		dev_info(csi->dev, "video node %s pad not connected\n",
+			 vdev->name);
+		return -ENOTCONN;
+	}
+
+	ret = ti_csi2rx_link_validate_get_fmt(link->source, &source_fmt);
+	if (ret)
+		return ret;
+
+	if (source_fmt.format.width != csi_fmt->width) {
+		dev_err(csi->dev, "Width does not match (source %u, sink %u)\n",
+			source_fmt.format.width, csi_fmt->width);
+		return -EPIPE;
+	}
+
+	if (source_fmt.format.height != csi_fmt->height) {
+		dev_err(csi->dev, "Height does not match (source %u, sink %u)\n",
+			source_fmt.format.height, csi_fmt->height);
+		return -EPIPE;
+	}
+
+	if (source_fmt.format.field != csi_fmt->field &&
+	    csi_fmt->field != V4L2_FIELD_NONE) {
+		dev_err(csi->dev, "Field does not match (source %u, sink %u)\n",
+			source_fmt.format.field, csi_fmt->field);
+		return -EPIPE;
+	}
+
+	ti_fmt = find_format_by_code(source_fmt.format.code);
+	if (!ti_fmt) {
+		dev_err(csi->dev, "Media bus format 0x%x not supported\n",
+			source_fmt.format.code);
+		return -EPIPE;
+	}
+
+	if (source_fmt.format.code == MEDIA_BUS_FMT_YUYV8_2X8 ||
+	    source_fmt.format.code == MEDIA_BUS_FMT_VYUY8_2X8 ||
+	    source_fmt.format.code == MEDIA_BUS_FMT_YVYU8_2X8) {
+		dev_err(csi->dev,
+			"Only UYVY input allowed for YUV422 8-bit. Output format can be configured.\n");
+		return -EPIPE;
+	}
+
+	if (source_fmt.format.code != ti_fmt->code) {
+		dev_err(csi->dev,
+			"Cannot transform source fmt 0x%x to sink fmt 0x%x\n",
+			source_fmt.format.code, ti_fmt->code);
+		return -EPIPE;
+	}
+
+	return 0;
+}
+
+static const struct media_entity_operations ti_csi2rx_video_entity_ops = {
+	.link_validate = ti_csi2rx_link_validate,
+};
+
+static int ti_csi2rx_init_dma(struct ti_csi2rx_dev *csi)
+{
+	struct dma_slave_config cfg = {
+		.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES };
+	int ret;
+
+	INIT_LIST_HEAD(&csi->dma.queue);
+	spin_lock_init(&csi->dma.lock);
+
+	csi->dma.state = TI_CSI2RX_DMA_STOPPED;
+
+	csi->dma.chan = dma_request_chan(csi->dev, "rx0");
+	if (IS_ERR(csi->dma.chan))
+		return PTR_ERR(csi->dma.chan);
+
+	ret = dmaengine_slave_config(csi->dma.chan, &cfg);
+	if (ret) {
+		dma_release_channel(csi->dma.chan);
+		return ret;
+	}
+
+	return 0;
+}
+
+static int ti_csi2rx_v4l2_init(struct ti_csi2rx_dev *csi)
+{
+	struct media_device *mdev = &csi->mdev;
+	struct video_device *vdev = &csi->vdev;
+	const struct ti_csi2rx_fmt *fmt;
+	struct v4l2_pix_format *pix_fmt = &csi->v_fmt.fmt.pix;
+	int ret;
+
+	fmt = find_format_by_pix(V4L2_PIX_FMT_UYVY);
+	if (!fmt)
+		return -EINVAL;
+
+	pix_fmt->width = 640;
+	pix_fmt->height = 480;
+	pix_fmt->field = V4L2_FIELD_NONE;
+
+	ti_csi2rx_fill_fmt(fmt, &csi->v_fmt);
+
+	mdev->dev = csi->dev;
+	mdev->hw_revision = 1;
+	strscpy(mdev->model, "TI-CSI2RX", sizeof(mdev->model));
+	snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
+		 dev_name(mdev->dev));
+
+	media_device_init(mdev);
+
+	strscpy(vdev->name, TI_CSI2RX_MODULE_NAME, sizeof(vdev->name));
+	vdev->v4l2_dev = &csi->v4l2_dev;
+	vdev->vfl_dir = VFL_DIR_RX;
+	vdev->fops = &csi_fops;
+	vdev->ioctl_ops = &csi_ioctl_ops;
+	vdev->release = video_device_release_empty;
+	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
+			    V4L2_CAP_IO_MC;
+	vdev->lock = &csi->mutex;
+	video_set_drvdata(vdev, csi);
+
+	csi->pad.flags = MEDIA_PAD_FL_SINK;
+	vdev->entity.ops = &ti_csi2rx_video_entity_ops;
+	ret = media_entity_pads_init(&csi->vdev.entity, 1, &csi->pad);
+	if (ret)
+		return ret;
+
+	csi->v4l2_dev.mdev = mdev;
+
+	ret = v4l2_device_register(csi->dev, &csi->v4l2_dev);
+	if (ret)
+		return ret;
+
+	ret = media_device_register(mdev);
+	if (ret) {
+		v4l2_device_unregister(&csi->v4l2_dev);
+		media_device_cleanup(mdev);
+		return ret;
+	}
+
+	return 0;
+}
+
+static void ti_csi2rx_cleanup_dma(struct ti_csi2rx_dev *csi)
+{
+	dma_release_channel(csi->dma.chan);
+}
+
+static void ti_csi2rx_cleanup_v4l2(struct ti_csi2rx_dev *csi)
+{
+	media_device_unregister(&csi->mdev);
+	v4l2_device_unregister(&csi->v4l2_dev);
+	media_device_cleanup(&csi->mdev);
+}
+
+static void ti_csi2rx_cleanup_subdev(struct ti_csi2rx_dev *csi)
+{
+	v4l2_async_nf_unregister(&csi->notifier);
+	v4l2_async_nf_cleanup(&csi->notifier);
+}
+
+static void ti_csi2rx_cleanup_vb2q(struct ti_csi2rx_dev *csi)
+{
+	vb2_queue_release(&csi->vidq);
+}
+
+static int ti_csi2rx_probe(struct platform_device *pdev)
+{
+	struct ti_csi2rx_dev *csi;
+	struct resource *res;
+	int ret;
+
+	csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
+	if (!csi)
+		return -ENOMEM;
+
+	csi->dev = &pdev->dev;
+	platform_set_drvdata(pdev, csi);
+
+	mutex_init(&csi->mutex);
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	csi->shim = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(csi->shim)) {
+		ret = PTR_ERR(csi->shim);
+		goto err_mutex;
+	}
+
+	ret = ti_csi2rx_init_dma(csi);
+	if (ret)
+		goto err_mutex;
+
+	ret = ti_csi2rx_v4l2_init(csi);
+	if (ret)
+		goto err_dma;
+
+	ret = ti_csi2rx_init_vb2q(csi);
+	if (ret)
+		goto err_v4l2;
+
+	ret = ti_csi2rx_init_subdev(csi);
+	if (ret)
+		goto err_vb2q;
+
+	ret = of_platform_populate(csi->dev->of_node, NULL, NULL, csi->dev);
+	if (ret) {
+		dev_err(csi->dev, "Failed to create children: %d\n", ret);
+		goto err_subdev;
+	}
+
+	return 0;
+
+err_subdev:
+	ti_csi2rx_cleanup_subdev(csi);
+err_vb2q:
+	ti_csi2rx_cleanup_vb2q(csi);
+err_v4l2:
+	ti_csi2rx_cleanup_v4l2(csi);
+err_dma:
+	ti_csi2rx_cleanup_dma(csi);
+err_mutex:
+	mutex_destroy(&csi->mutex);
+	return ret;
+}
+
+static int ti_csi2rx_remove(struct platform_device *pdev)
+{
+	struct ti_csi2rx_dev *csi = platform_get_drvdata(pdev);
+
+	video_unregister_device(&csi->vdev);
+
+	ti_csi2rx_cleanup_vb2q(csi);
+	ti_csi2rx_cleanup_subdev(csi);
+	ti_csi2rx_cleanup_v4l2(csi);
+	ti_csi2rx_cleanup_dma(csi);
+
+	mutex_destroy(&csi->mutex);
+
+	return 0;
+}
+
+static const struct of_device_id ti_csi2rx_of_match[] = {
+	{ .compatible = "ti,j721e-csi2rx", },
+	{ },
+};
+MODULE_DEVICE_TABLE(of, ti_csi2rx_of_match);
+
+static struct platform_driver ti_csi2rx_pdrv = {
+	.probe = ti_csi2rx_probe,
+	.remove = ti_csi2rx_remove,
+	.driver = {
+		.name = TI_CSI2RX_MODULE_NAME,
+		.of_match_table = ti_csi2rx_of_match,
+	},
+};
+
+module_platform_driver(ti_csi2rx_pdrv);
+
+MODULE_DESCRIPTION("TI J721E CSI2 RX Driver");
+MODULE_AUTHOR("Pratyush Yadav <p.yadav@ti.com>");
+MODULE_LICENSE("GPL");
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 11/13] media: dt-bindings: Make sure items in data-lanes are unique
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (9 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 11:55 ` [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver Vaishnav Achath
                   ` (3 subsequent siblings)
  14 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

The data-lanes property maps the logical lane numbers to the physical
lane numbers. The position of an entry is the logical lane number and
its value is the physical lane number. Since one physical lane can only
map to one logical lane, no number in the list should repeat. Add the
uniqueItems constraint on the property to enforce this.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Acked-by: Rob Herring <robh@kernel.org>
---

(no changes since v5)

Changes in v5:
- Add Laurent's R-by and Rob's Ack.

Changes in v4:
- New in v4.

 Documentation/devicetree/bindings/media/video-interfaces.yaml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/media/video-interfaces.yaml b/Documentation/devicetree/bindings/media/video-interfaces.yaml
index a211d49dc2ac..26e3e7d7c67b 100644
--- a/Documentation/devicetree/bindings/media/video-interfaces.yaml
+++ b/Documentation/devicetree/bindings/media/video-interfaces.yaml
@@ -160,6 +160,7 @@ properties:
     $ref: /schemas/types.yaml#/definitions/uint32-array
     minItems: 1
     maxItems: 8
+    uniqueItems: true
     items:
       # Assume up to 9 physical lane indices
       maximum: 8
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (10 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 11/13] media: dt-bindings: Make sure items in data-lanes are unique Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-14 14:10   ` Rob Herring
  2023-03-15  7:44   ` Krzysztof Kozlowski
  2023-03-14 11:55 ` [PATCH v7 13/13] media: dt-bindings: Convert Cadence CSI2RX binding to YAML Vaishnav Achath
                   ` (2 subsequent siblings)
  14 siblings, 2 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

TI's J721E uses the Cadence CSI2RX and DPHY peripherals to facilitate
capture over a CSI-2 bus. The TI CSI2RX platform driver glues all the
parts together.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Rob Herring <robh@kernel.org>
---

(no changes since v5)

Changes in v5:
- Add Laurent's and Rob's R-by.

Changes in v2:
- Rename to ti,j721e-csi2rx.yaml
- Add an entry in MAINTAINERS.
- Add a description for the binding.
- Change compatible to ti,j721e-csi2rx to make it SoC specific.
- Remove description from dmas, reg, power-domains.
- Remove a limit of 2 from #address-cells and #size-cells.
- Fix add ^ to csi-bridge subnode regex.
- Make ranges mandatory.
- Add unit address in example.
- Add a reference to cdns,csi2rx in csi-bridge subnode.
- Expand the example to include the csi-bridge subnode as well.
- Re-order subject prefixes.

 .../bindings/media/ti,j721e-csi2rx.yaml       | 101 ++++++++++++++++++
 MAINTAINERS                                   |   1 +
 2 files changed, 102 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml

diff --git a/Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml b/Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml
new file mode 100644
index 000000000000..db87cfd65bed
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml
@@ -0,0 +1,101 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/ti,j721e-csi2rx.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: TI J721E CSI2RX Wrapper Device Tree Bindings
+
+description: |
+  The TI J721E CSI2RX Wrapper is a wrapper around Cadence CSI2RX bridge that
+  enables sending captured frames to memory over PSI-L DMA. In the J721E
+  Technical Reference Manual (SPRUIL1B) it is referred to as "SHIM" under the
+  CSI_RX_IF section.
+
+maintainers:
+  - Pratyush Yadav <p.yadav@ti.com>
+
+properties:
+  compatible:
+    items:
+      - const: ti,j721e-csi2rx
+
+  dmas:
+    maxItems: 1
+
+  dma-names:
+    items:
+      - const: rx0
+
+  reg:
+    maxItems: 1
+
+  power-domains:
+    maxItems: 1
+
+  ranges: true
+
+  "#address-cells": true
+
+  "#size-cells": true
+
+patternProperties:
+  "^csi-bridge@":
+    type: object
+    description: CSI2 bridge node.
+    $ref: cdns,csi2rx.yaml#
+
+required:
+  - compatible
+  - reg
+  - dmas
+  - dma-names
+  - power-domains
+  - ranges
+  - "#address-cells"
+  - "#size-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/soc/ti,sci_pm_domain.h>
+
+    ti_csi2rx0: ticsi2rx@4500000 {
+        compatible = "ti,j721e-csi2rx";
+        dmas = <&main_udmap 0x4940>;
+        dma-names = "rx0";
+        reg = <0x4500000 0x1000>;
+        power-domains = <&k3_pds 26 TI_SCI_PD_EXCLUSIVE>;
+        #address-cells = <1>;
+        #size-cells = <1>;
+        ranges;
+
+        cdns_csi2rx: csi-bridge@4504000 {
+            compatible = "cdns,csi2rx";
+            reg = <0x4504000 0x1000>;
+            clocks = <&k3_clks 26 2>, <&k3_clks 26 0>, <&k3_clks 26 2>,
+              <&k3_clks 26 2>, <&k3_clks 26 3>, <&k3_clks 26 3>;
+            clock-names = "sys_clk", "p_clk", "pixel_if0_clk",
+              "pixel_if1_clk", "pixel_if2_clk", "pixel_if3_clk";
+            phys = <&dphy0>;
+            phy-names = "dphy";
+
+            ports {
+                #address-cells = <1>;
+                #size-cells = <0>;
+
+                csi2_0: port@0 {
+
+                    reg = <0>;
+
+                    csi2rx0_in_sensor: endpoint {
+                        remote-endpoint = <&csi2_cam0>;
+                        bus-type = <4>; /* CSI2 DPHY. */
+                        clock-lanes = <0>;
+                        data-lanes = <1 2>;
+                    };
+                };
+            };
+        };
+    };
diff --git a/MAINTAINERS b/MAINTAINERS
index 69c2b0f48073..796be64bc54f 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -20905,6 +20905,7 @@ TI J721E CSI2RX DRIVER
 M:	Vaishnav Achath <vaishnav.a@ti.com>
 L:	linux-media@vger.kernel.org
 S:	Supported
+F:	Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml
 F:	drivers/media/platform/ti/j721e-csi2rx/
 
 TI DAVINCI MACHINE SUPPORT
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v7 13/13] media: dt-bindings: Convert Cadence CSI2RX binding to YAML
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (11 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver Vaishnav Achath
@ 2023-03-14 11:55 ` Vaishnav Achath
  2023-03-15  7:45   ` Krzysztof Kozlowski
  2023-03-23 19:36 ` [PATCH v7 00/13] CSI2RX support on J721E Martyn Welch
  2023-04-04  8:44 ` Tomi Valkeinen
  14 siblings, 1 reply; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-14 11:55 UTC (permalink / raw)
  To: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	vaishnav.a

From: Pratyush Yadav <p.yadav@ti.com>

Convert the Cadence CSI2RX binding to use YAML schema.

Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
Reviewed-by: Rob Herring <robh@kernel.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---

(no changes since v5)

Changes in v5:
- Move clock description in comments under the clocks property.
- Make ports required.
- Add Rob's and Laurent's R-by.

Changes in v4:
- Add power-domains property.
- Drop maxItems from clock-names.
- Drop the type for data-lanes.
- Drop uniqueItems from data-lanes. Move it to video-interfaces.yaml
  instead.

Changes in v3:
- Add compatible: contains: const: cdns,csi2rx to allow SoC specific
  compatible.
- Add more constraints for data-lanes property.

Changes in v2:
- New in v2.

 .../devicetree/bindings/media/cdns,csi2rx.txt | 100 ----------
 .../bindings/media/cdns,csi2rx.yaml           | 176 ++++++++++++++++++
 2 files changed, 176 insertions(+), 100 deletions(-)
 delete mode 100644 Documentation/devicetree/bindings/media/cdns,csi2rx.txt
 create mode 100644 Documentation/devicetree/bindings/media/cdns,csi2rx.yaml

diff --git a/Documentation/devicetree/bindings/media/cdns,csi2rx.txt b/Documentation/devicetree/bindings/media/cdns,csi2rx.txt
deleted file mode 100644
index 6b02a0657ad9..000000000000
--- a/Documentation/devicetree/bindings/media/cdns,csi2rx.txt
+++ /dev/null
@@ -1,100 +0,0 @@
-Cadence MIPI-CSI2 RX controller
-===============================
-
-The Cadence MIPI-CSI2 RX controller is a CSI-2 bridge supporting up to 4 CSI
-lanes in input, and 4 different pixel streams in output.
-
-Required properties:
-  - compatible: must be set to "cdns,csi2rx" and an SoC-specific compatible
-  - reg: base address and size of the memory mapped region
-  - clocks: phandles to the clocks driving the controller
-  - clock-names: must contain:
-    * sys_clk: main clock
-    * p_clk: register bank clock
-    * pixel_if[0-3]_clk: pixel stream output clock, one for each stream
-                         implemented in hardware, between 0 and 3
-
-Optional properties:
-  - phys: phandle to the external D-PHY, phy-names must be provided
-  - phy-names: must contain "dphy", if the implementation uses an
-               external D-PHY
-
-Required subnodes:
-  - ports: A ports node with one port child node per device input and output
-           port, in accordance with the video interface bindings defined in
-           Documentation/devicetree/bindings/media/video-interfaces.txt. The
-           port nodes are numbered as follows:
-
-           Port Description
-           -----------------------------
-           0    CSI-2 input
-           1    Stream 0 output
-           2    Stream 1 output
-           3    Stream 2 output
-           4    Stream 3 output
-
-           The stream output port nodes are optional if they are not
-           connected to anything at the hardware level or implemented
-           in the design.Since there is only one endpoint per port,
-           the endpoints are not numbered.
-
-
-Example:
-
-csi2rx: csi-bridge@0d060000 {
-	compatible = "cdns,csi2rx";
-	reg = <0x0d060000 0x1000>;
-	clocks = <&byteclock>, <&byteclock>
-		 <&coreclock>, <&coreclock>,
-		 <&coreclock>, <&coreclock>;
-	clock-names = "sys_clk", "p_clk",
-		      "pixel_if0_clk", "pixel_if1_clk",
-		      "pixel_if2_clk", "pixel_if3_clk";
-
-	ports {
-		#address-cells = <1>;
-		#size-cells = <0>;
-
-		port@0 {
-			reg = <0>;
-
-			csi2rx_in_sensor: endpoint {
-				remote-endpoint = <&sensor_out_csi2rx>;
-				clock-lanes = <0>;
-				data-lanes = <1 2>;
-			};
-		};
-
-		port@1 {
-			reg = <1>;
-
-			csi2rx_out_grabber0: endpoint {
-				remote-endpoint = <&grabber0_in_csi2rx>;
-			};
-		};
-
-		port@2 {
-			reg = <2>;
-
-			csi2rx_out_grabber1: endpoint {
-				remote-endpoint = <&grabber1_in_csi2rx>;
-			};
-		};
-
-		port@3 {
-			reg = <3>;
-
-			csi2rx_out_grabber2: endpoint {
-				remote-endpoint = <&grabber2_in_csi2rx>;
-			};
-		};
-
-		port@4 {
-			reg = <4>;
-
-			csi2rx_out_grabber3: endpoint {
-				remote-endpoint = <&grabber3_in_csi2rx>;
-			};
-		};
-	};
-};
diff --git a/Documentation/devicetree/bindings/media/cdns,csi2rx.yaml b/Documentation/devicetree/bindings/media/cdns,csi2rx.yaml
new file mode 100644
index 000000000000..399fe619377d
--- /dev/null
+++ b/Documentation/devicetree/bindings/media/cdns,csi2rx.yaml
@@ -0,0 +1,176 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/media/cdns,csi2rx.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Cadence MIPI-CSI2 RX controller
+
+description: |
+  The Cadence MIPI-CSI2 RX controller is a CSI-2 bridge supporting up to 4 CSI
+  lanes in input, and 4 different pixel streams in output.
+
+maintainers:
+  - Pratyush Yadav <p.yadav@ti.com>
+
+properties:
+  compatible:
+    contains:
+      const: cdns,csi2rx
+
+  reg:
+    maxItems: 1
+
+  clocks:
+    minItems: 3
+    items:
+      - description: Main clock
+      - description: Register bank clock
+      - description: Pixel stream 0 output clock
+      - description: Pixel stream 1 output clock
+      - description: Pixel stream 2 output clock
+      - description: Pixel stream 3 output clock
+
+  clock-names:
+    minItems: 3
+    items:
+      - const: sys_clk
+      - const: p_clk
+      - const: pixel_if0_clk
+      - const: pixel_if1_clk
+      - const: pixel_if2_clk
+      - const: pixel_if3_clk
+
+  phys:
+    maxItems: 1
+    description: phandle to the external D-PHY
+
+  phy-names:
+    items:
+      - const: dphy
+
+  power-domains:
+    maxItems: 1
+
+  ports:
+    $ref: /schemas/graph.yaml#/properties/ports
+
+    properties:
+      port@0:
+        $ref: /schemas/graph.yaml#/$defs/port-base
+        unevaluatedProperties: false
+        description: CSI-2 input
+
+        properties:
+          endpoint:
+            $ref: video-interfaces.yaml#
+            unevaluatedProperties: false
+
+            properties:
+              clock-lanes:
+                maxItems: 1
+
+              data-lanes:
+                minItems: 1
+                maxItems: 4
+                items:
+                  maximum: 4
+
+            required:
+              - clock-lanes
+              - data-lanes
+
+      port@1:
+        $ref: /schemas/graph.yaml#/properties/port
+        description: Stream 0 output
+
+      port@2:
+        $ref: /schemas/graph.yaml#/properties/port
+        description: Stream 1 output
+
+      port@3:
+        $ref: /schemas/graph.yaml#/properties/port
+        description: Stream 2 output
+
+      port@4:
+        $ref: /schemas/graph.yaml#/properties/port
+        description: Stream 3 output
+
+    required:
+      - port@0
+
+
+dependencies:
+  phys: [ 'phy-names' ]
+  phy-names: [ 'phys' ]
+
+required:
+  - compatible
+  - reg
+  - clocks
+  - clock-names
+  - ports
+
+additionalProperties: false
+
+examples:
+  - |
+    csi2rx: csi-bridge@d060000 {
+      compatible = "cdns,csi2rx";
+      reg = <0x0d060000 0x1000>;
+      clocks = <&byteclock>, <&byteclock>,
+        <&coreclock>, <&coreclock>,
+        <&coreclock>, <&coreclock>;
+      clock-names = "sys_clk", "p_clk",
+              "pixel_if0_clk", "pixel_if1_clk",
+              "pixel_if2_clk", "pixel_if3_clk";
+      phys = <&dphy0>;
+      phy-names = "dphy";
+
+      ports {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        port@0 {
+          reg = <0>;
+
+          csi2rx_in_sensor: endpoint {
+            remote-endpoint = <&sensor_out_csi2rx>;
+            clock-lanes = <0>;
+            data-lanes = <1 2>;
+          };
+        };
+
+        port@1 {
+          reg = <1>;
+
+          csi2rx_out_grabber0: endpoint {
+            remote-endpoint = <&grabber0_in_csi2rx>;
+          };
+        };
+
+        port@2 {
+          reg = <2>;
+
+          csi2rx_out_grabber1: endpoint {
+            remote-endpoint = <&grabber1_in_csi2rx>;
+          };
+        };
+
+        port@3 {
+          reg = <3>;
+
+          csi2rx_out_grabber2: endpoint {
+            remote-endpoint = <&grabber2_in_csi2rx>;
+          };
+        };
+
+        port@4 {
+          reg = <4>;
+
+          csi2rx_out_grabber3: endpoint {
+            remote-endpoint = <&grabber3_in_csi2rx>;
+          };
+        };
+      };
+    };
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver
  2023-03-14 11:55 ` [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver Vaishnav Achath
@ 2023-03-14 14:10   ` Rob Herring
  2023-03-15  7:44   ` Krzysztof Kozlowski
  1 sibling, 0 replies; 30+ messages in thread
From: Rob Herring @ 2023-03-14 14:10 UTC (permalink / raw)
  To: Vaishnav Achath
  Cc: bparrot, robh+dt, mchehab, tomi.valkeinen, laurent.pinchart,
	linux-kernel, linux-media, martyn.welch, devicetree,
	krzysztof.kozlowski+dt, mripard, u-kumar1, praneeth, nm,
	j-luthra, vigneshr, devarsht, niklas.soderlund+renesas,
	sakari.ailus


On Tue, 14 Mar 2023 17:25:15 +0530, Vaishnav Achath wrote:
> From: Pratyush Yadav <p.yadav@ti.com>
> 
> TI's J721E uses the Cadence CSI2RX and DPHY peripherals to facilitate
> capture over a CSI-2 bus. The TI CSI2RX platform driver glues all the
> parts together.
> 
> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Reviewed-by: Rob Herring <robh@kernel.org>
> ---
> 
> (no changes since v5)
> 
> Changes in v5:
> - Add Laurent's and Rob's R-by.
> 
> Changes in v2:
> - Rename to ti,j721e-csi2rx.yaml
> - Add an entry in MAINTAINERS.
> - Add a description for the binding.
> - Change compatible to ti,j721e-csi2rx to make it SoC specific.
> - Remove description from dmas, reg, power-domains.
> - Remove a limit of 2 from #address-cells and #size-cells.
> - Fix add ^ to csi-bridge subnode regex.
> - Make ranges mandatory.
> - Add unit address in example.
> - Add a reference to cdns,csi2rx in csi-bridge subnode.
> - Expand the example to include the csi-bridge subnode as well.
> - Re-order subject prefixes.
> 
>  .../bindings/media/ti,j721e-csi2rx.yaml       | 101 ++++++++++++++++++
>  MAINTAINERS                                   |   1 +
>  2 files changed, 102 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml
> 

My bot found errors running 'make DT_CHECKER_FLAGS=-m dt_binding_check'
on your patch (DT_CHECKER_FLAGS is new in v5.13):

yamllint warnings/errors:

dtschema/dtc warnings/errors:
./Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml: Unable to find schema file matching $id: http://devicetree.org/schemas/media/cdns,csi2rx.yaml
/builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/media/ti,j721e-csi2rx.example.dtb: ticsi2rx@4500000: csi-bridge@4504000: False schema does not allow {'compatible': ['cdns,csi2rx'], 'reg': [[72368128, 4096]], 'clocks': [[4294967295, 26, 2], [4294967295, 26, 0], [4294967295, 26, 2], [4294967295, 26, 2], [4294967295, 26, 3], [4294967295, 26, 3]], 'clock-names': ['sys_clk', 'p_clk', 'pixel_if0_clk', 'pixel_if1_clk', 'pixel_if2_clk', 'pixel_if3_clk'], 'phys': [[4294967295]], 'phy-names': ['dphy'], 'ports': {'#address-cells': [[1]], '#size-cells': [[0]], 'port@0': {'reg': [[0]], 'endpoint': {'remote-endpoint': [[4294967295]], 'bus-type': [[4]], 'clock-lanes': [[0]], 'data-lanes': [[1, 2]]}}}}
	From schema: /builds/robherring/dt-review-ci/linux/Documentation/devicetree/bindings/media/ti,j721e-csi2rx.yaml
Documentation/devicetree/bindings/media/ti,j721e-csi2rx.example.dtb: /example-0/ticsi2rx@4500000/csi-bridge@4504000: failed to match any schema with compatible: ['cdns,csi2rx']

doc reference errors (make refcheckdocs):

See https://patchwork.ozlabs.org/project/devicetree-bindings/patch/20230314115516.667-13-vaishnav.a@ti.com

The base for the series is generally the latest rc1. A different dependency
should be noted in *this* patch.

If you already ran 'make dt_binding_check' and didn't see the above
error(s), then make sure 'yamllint' is installed and dt-schema is up to
date:

pip3 install dtschema --upgrade

Please check and re-submit after running the above command yourself. Note
that DT_SCHEMA_FILES can be set to your schema file to speed up checking
your schema. However, it must be unset to test all examples with your schema.


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver
  2023-03-14 11:55 ` [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver Vaishnav Achath
  2023-03-14 14:10   ` Rob Herring
@ 2023-03-15  7:44   ` Krzysztof Kozlowski
  1 sibling, 0 replies; 30+ messages in thread
From: Krzysztof Kozlowski @ 2023-03-15  7:44 UTC (permalink / raw)
  To: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

On 14/03/2023 12:55, Vaishnav Achath wrote:
> From: Pratyush Yadav <p.yadav@ti.com>
> 
> TI's J721E uses the Cadence CSI2RX and DPHY peripherals to facilitate
> capture over a CSI-2 bus. The TI CSI2RX platform driver glues all the
> parts together.
> 
> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Reviewed-by: Rob Herring <robh@kernel.org>
> ---


> +$id: http://devicetree.org/schemas/media/ti,j721e-csi2rx.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: TI J721E CSI2RX Wrapper Device Tree Bindings

Drop "Device Tree Bindings"

> +
> +description: |
> +  The TI J721E CSI2RX Wrapper is a wrapper around Cadence CSI2RX bridge that
> +  enables sending captured frames to memory over PSI-L DMA. In the J721E
> +  Technical Reference Manual (SPRUIL1B) it is referred to as "SHIM" under the
> +  CSI_RX_IF section.
> +
> +maintainers:
> +  - Pratyush Yadav <p.yadav@ti.com>
> +
> +properties:
> +  compatible:
> +    items:

Drop items

> +      - const: ti,j721e-csi2rx
> +
> +  dmas:
> +    maxItems: 1
> +
> +  dma-names:
> +    items:
> +      - const: rx0
> +
> +  reg:
> +    maxItems: 1
> +
> +  power-domains:
> +    maxItems: 1
> +
> +  ranges: true
> +
> +  "#address-cells": true
> +
> +  "#size-cells": true
> +
> +patternProperties:
> +  "^csi-bridge@":
> +    type: object
> +    description: CSI2 bridge node.
> +    $ref: cdns,csi2rx.yaml#

Your patch fails here. This error must be fixed or explain as dependency
in the patch changelog. Patch cannot be taken without that dependency.

Best regards,
Krzysztof


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 13/13] media: dt-bindings: Convert Cadence CSI2RX binding to YAML
  2023-03-14 11:55 ` [PATCH v7 13/13] media: dt-bindings: Convert Cadence CSI2RX binding to YAML Vaishnav Achath
@ 2023-03-15  7:45   ` Krzysztof Kozlowski
  2023-03-20  5:34     ` Vaishnav Achath
  0 siblings, 1 reply; 30+ messages in thread
From: Krzysztof Kozlowski @ 2023-03-15  7:45 UTC (permalink / raw)
  To: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

On 14/03/2023 12:55, Vaishnav Achath wrote:
> From: Pratyush Yadav <p.yadav@ti.com>
> 
> Convert the Cadence CSI2RX binding to use YAML schema.
> 
> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
> Reviewed-by: Rob Herring <robh@kernel.org>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
> 
> (no changes since v5)
> 

So it seems your patchset is not bisectable. Fix this and test
bisectability. All patchsets are expected to be fully bisectable.

Best regards,
Krzysztof


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 13/13] media: dt-bindings: Convert Cadence CSI2RX binding to YAML
  2023-03-15  7:45   ` Krzysztof Kozlowski
@ 2023-03-20  5:34     ` Vaishnav Achath
  0 siblings, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-20  5:34 UTC (permalink / raw)
  To: Krzysztof Kozlowski, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch


Hi Krzysztof,

On 15/03/23 13:15, Krzysztof Kozlowski wrote:
> On 14/03/2023 12:55, Vaishnav Achath wrote:
>> From: Pratyush Yadav <p.yadav@ti.com>
>>
>> Convert the Cadence CSI2RX binding to use YAML schema.
>>
>> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
>> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
>> Reviewed-by: Rob Herring <robh@kernel.org>
>> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> ---
>>
>> (no changes since v5)
>>
> 
> So it seems your patchset is not bisectable. Fix this and test
> bisectability. All patchsets are expected to be fully bisectable.
> 

Thank you for the review, I will fix for bisect and address the rest of your
feedback in next revision and fix the order of bindings vs driver changes (will
wait for few more days for more feedback on the main j721e-csirx driver).

-- 
Thanks and Regards,
Vaishnav

> Best regards,
> Krzysztof
> 


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E
  2023-03-14 11:55 ` [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E Vaishnav Achath
@ 2023-03-20 12:51   ` Jai Luthra
  2023-03-24 18:14   ` Laurent Pinchart
  2023-03-31 13:27   ` Sakari Ailus
  2 siblings, 0 replies; 30+ messages in thread
From: Jai Luthra @ 2023-03-20 12:51 UTC (permalink / raw)
  To: Vaishnav Achath
  Cc: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen, linux-kernel, bparrot, niklas.soderlund+renesas,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

[-- Attachment #1: Type: text/plain, Size: 2526 bytes --]

Hi Vaishnav,

Thanks for the series.

On Mar 14, 2023 at 17:25:13 +0530, Vaishnav Achath wrote:
> From: Pratyush Yadav <p.yadav@ti.com>
> 
> TI's J721E uses the Cadence CSI2RX and DPHY peripherals to facilitate
> capture over a CSI-2 bus.
> 
> The Cadence CSI2RX IP acts as a bridge between the TI specific parts and
> the CSI-2 protocol parts. TI then has a wrapper on top of this bridge
> called the SHIM layer. It takes in data from stream 0, repacks it, and
> sends it to memory over PSI-L DMA.
> 
> This driver acts as the "front end" to V4L2 client applications. It
> implements the required ioctls and buffer operations, passes the
> necessary calls on to the bridge, programs the SHIM layer, and performs
> DMA via the dmaengine API to finally return the data to a buffer
> supplied by the application.
> 
> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> Signed-off-by: Jai Luthra <j-luthra@ti.com>
> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
> ---
[...]
> diff --git a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c 
> b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> new file mode 100644
> index 000000000000..0c8dad049f5b
> --- /dev/null
> +++ b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> @@ -0,0 +1,1022 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * TI CSI2 RX driver.
> + *
> + * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com/
> + *
> + * Author: Pratyush Yadav <p.yadav@ti.com>
> + */
> +
> +#include <linux/bitfield.h>
> +#include <linux/dmaengine.h>
> +#include <linux/module.h>
> +#include <linux/of_platform.h>
> +#include <linux/platform_device.h>
> +
> +#include <media/mipi-csi2.h>
> +#include <media/v4l2-device.h>
> +#include <media/v4l2-ioctl.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#define TI_CSI2RX_MODULE_NAME		"j721e-csi2rx"
> +
> +#define SHIM_CNTL			0x10
> +#define SHIM_CNTL_PIX_RST		BIT(0)
> +
> +#define SHIM_DMACNTX			0x20
> +#define SHIM_DMACNTX_EN			BIT(31)
> +#define SHIM_DMACNTX_YUV422		GENMASK(27, 26)
> +#define SHIM_DMACNTX_FMT		GENMASK(5, 0)
> +#define SHIM_DMACNTX_UYVY		0
> +#define SHIM_DMACNTX_VYUY		1
> +#define SHIM_DMACNTX_YUYV		2
> +#define SHIM_DMACNTX_YVYU		3
> +
> +#define SHIM_PSI_CFG0			0x24
> +#define SHIM_PSI_CFG0_SRC_TAG		GENMASK(15, 0)
> +#define SHIM_PSI_CFG0_DST_TAG		GENMASK(31, 15)

This should be GENMASK(31, 16) instead.

> +
> +#define PSIL_WORD_SIZE_BYTES		16
> +/*
[...]

Thanks,
Jai

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 00/13] CSI2RX support on J721E
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (12 preceding siblings ...)
  2023-03-14 11:55 ` [PATCH v7 13/13] media: dt-bindings: Convert Cadence CSI2RX binding to YAML Vaishnav Achath
@ 2023-03-23 19:36 ` Martyn Welch
  2023-03-28  6:01   ` Jai Luthra
  2023-04-04  8:44 ` Tomi Valkeinen
  14 siblings, 1 reply; 30+ messages in thread
From: Martyn Welch @ 2023-03-23 19:36 UTC (permalink / raw)
  To: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm

On Tue, 2023-03-14 at 17:25 +0530, Vaishnav Achath wrote:
> Hi,
> 
> This series adds support for CSI2 capture on J721E. It includes some
> fixes to the Cadence CSI2RX driver, and adds the TI CSI2RX wrapper
> driver.
> 
> This is a V7 of the below V6 series,
> https://lore.kernel.org/all/20220121142904.4091481-1-p.yadav@ti.com/
> 
> Since Pratyush moved out of TI, I will be working on upstreaming the
> TI CSI2RX wrapper support.
> 
> Tested on TI's J721E EVM with LI OV5640 sensor module.
> https://gist.github.com/vaishnavachath/f030a257d5b6569817bc9deab1c4fa77
> 
> Also, Tested on TI AM62-SK with Pcam5C OV5640 module.
> https://gist.github.com/vaishnavachath/ff2605faa92f1a6ab5670426da28ccee
> 

Hi Vaishnav,

I assume I'm doing something wrong. I have a TI AM62-SK and the Pcam5C
OV5640 module. I've been trying to test this with gstreamer using the
following command:

gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=10 ! video/x-
raw, width=640, height=480, format=UYVY, framerate=30/1 ! jpegenc !
multifilesink location=test%d.jpg

However I've not been able to get this working, failing with this
failure unless I patch in some changes I found in the TI BSP:

[   28.083635] cdns-mipi-dphy-rx 30110000.phy: DPHY wait for lane ready
timeout
[   28.090905] cdns-csi2rx 30101000.csi-bridge: Failed to configure
external DPHY: -110

The changes (and the device tree nodes I added, which might be
wrong...) can be found here:

https://gitlab.collabora.com/martyn/linux/-/commits/am625-sk-ov5640

Any ideas what I'm doing wrong?

Martyn

> For all newer TI platforms that TI J721E Silicon Revision 1.0, below
> update
> to DPHY RX driver is needed:
> https://lore.kernel.org/all/20230314073137.2153-1-vaishnav.a@ti.com/
> 
> Changes in v7:
> - For patch 10/13 ("Add CSI2RX upport for J721E"):
> - Fix incorrect value written in SHIM_PSI_CFG0_DST_TAG
> - Drop support for 2X8 formats.
> - Update maintainer to Vaishnav as Pratyush moved out of TI.
> - Address Sakari's review comments:
> - Update MAX_HEIGHT_LINES, MAX_WIDTH_BYTES to prevent overflow.
> - Assign dma_slave_config during declaration, drop memset().
> - dma_release_channel() on slave_config failure.
> - provide entity ops for the vdev entity with link_validate().
> - mutex_destroy() on ti_csi2rx_probe failure path.
> - Drop busy check in remove().
> - mutex_destroy() in ti_csi2rx_remove().
> - Address Laurent's review comments:
> - Update entries in Makefile in alphabetical order.
> - include headers in alphabetical order.
> - Drop redundant CSI DT defines and use from media/mipi-csi2.h.
> - Rename csi_df to csi_dt.
> - Drop v4l2_colorspace from ti_csi2rx_fmt and set default in
>   ti_csi2rx_v4l2_init()
> - Adjust field and not return EINVAL in ti_csi2rx_try_fmt_vid_cap().
> - inline ti_csi2rx_video_register().
> - start DMA before starting source subdev.
> - move buffer cleanup to separate function
> ti_csi2rx_cleanup_buffers()
>   to be used in ti_csi2rx_stop_streaming() and
> ti_csi2rx_start_streaming()
>   failure path.
> - Drop VB2_USERPTR, VB2_READ and V4L2_CAP_READWRITE.
> - For patch 4/13 ("media: cadence: csi2rx: Add external DPHY
> support"):
> - Fix multiplier and divider in v4l2_get_link_freq() which caused
>   failures during streaming.
> 
> Changes in v6:
> - Move the lock around the dereference for framefmt in
>   csi2rx_{get,set}_fmt() instead of when we get the pointer.
> - Do not return an error when an unsupported format is set. Instead
>   adjust the code to the first format in the list.
> - Drop variable bpp and use fmt->bpp directly.
> - Drop variable got_pm. Call phy_pm_runtime_put() unconditionally
> since
>   it will just return an error if runtime PM is not enabled.
> - Drop transcoding from the commit message.
> - Make csi2rx_media_ops const.
> 
> Changes in v5:
> - Cleanup notifier in csi2rx_parse_dt() after the call to
>   v4l2_async_nf_add_fwnode_remote().
> - Use YUV 1X16 formats instead of 2X8.
> - Only error out when phy_pm_runtime_get_sync() returns a negative
>   value. A positive value can be returned if the phy was already
>   resumed.
> - Do not query the source subdev for format. Use the newly added
>   internal format instead.
> - Make i unsigned.
> - Change %d to %u
> - Add dependency on PHY_CADENCE_DPHY_RX instead of PHY_CADENCE_DPHY
>   since the Rx mode DPHY now has a separate driver.
> - Drop ti_csi2rx_validate_pipeline(). Pipeline validation should be
> done
>   at media_pipeline_start().
> - Do not assign flags.
> - Fix error handling in ti_csi2rx_start_streaming(). Free up vb2
> buffers
>   when media_pipeline_start() fails.
> - Move clock description in comments under the clocks property.
> - Make ports required.
> - Add link validation to cdns-csi2rx driver.
> 
> Changes in v4:
> - Drop the call to set PHY submode. It is now being done via
> compatible
>   on the DPHY side.
> - Acquire the media device's graph_mutex before starting the graph
> walk.
> - Call media_graph_walk_init() and media_graph_walk_cleanup() when
>   starting and ending the graph walk respectively.
> - Reduce max frame height and width in enum_framesizes. Currently
> they
>   are set to UINT_MAX but they must be a multiple of step_width, so
> they
>   need to be rounded down. Also, these values are absurdly large
> which
>   causes some userspace applications like gstreamer to trip up. While
> it
>   is not generally right to change the kernel for an application bug,
> it
>   is not such a big deal here. This change is replacing one set of
>   absurdly large arbitrary values with another set of smaller but
> still
>   absurdly large arbitrary values. Both limits are unlikely to be hit
> in
>   practice.
> - Add power-domains property.
> - Drop maxItems from clock-names.
> - Drop the type for data-lanes.
> - Drop uniqueItems from data-lanes. Move it to video-interfaces.yaml
>   instead.
> - Drop OV5640 runtime pm patch. It seems to be a bit complicated and
> it
>   is not exactly necessary for this series. Any CSI-2 camera will
> work
>   just fine, OV5640 just happens to be the one I tested with. I don't
>   want it to block this series. I will submit it as a separate patch
>   later.
> 
> Changes in v3:
> - Use v4l2_get_link_freq() to calculate pixel clock.
> - Move DMA related fields in struct ti_csi2rx_dma.
> - Protect DMA buffer queue with a spinlock to make sure the queue
> buffer
>   and DMA callback don't race on it.
> - Track the current DMA state. It might go idle because of a lack of
>   buffers. This state can be used to restart it if needed.
> - Do not include the current buffer in the pending queue. It is
> slightly
>   better modelling than leaving it at the head of the pending queue.
> - Use the buffer as the callback argument, and add a reference to csi
> in it.
> - If queueing a buffer to DMA fails, the buffer gets leaked and DMA
> gets
>   stalled with. Instead, report the error to vb2 and queue the next
>   buffer in the pending queue.
> - DMA gets stalled if we run out of buffers since the callback is the
>   only one that fires subsequent transfers and it is no longer being
>   called. Check for that when queueing buffers and restart DMA if
>   needed.
> - Do not put of node until we are done using the fwnode.
> - Set inital format to UYVY 640x480.
> - Add compatible: contains: const: cdns,csi2rx to allow SoC specific
>   compatible.
> - Add more constraints for data-lanes property.
> 
> Changes in v2:
> - Use phy_pm_runtime_get_sync() and phy_pm_runtime_put() before
> making
>   calls to set PHY mode, etc. to make sure it is ready.
> - Use dmaengine_get_dma_device() instead of directly accessing
>   dma->device->dev.
> - Do not set dst_addr_width when configuring slave DMA.
> - Move to a separate subdir and rename to j721e-csi2rx.c
> - Convert compatible to ti,j721e-csi2rx.
> - Move to use Media Controller centric APIs.
> - Improve cleanup in probe when one of the steps fails.
> - Add colorspace to formats database.
> - Set hw_revision on media_device.
> - Move video device initialization to probe time instead of register
> time.
> - Rename to ti,j721e-csi2rx.yaml
> - Add an entry in MAINTAINERS.
> - Add a description for the binding.
> - Change compatible to ti,j721e-csi2rx to make it SoC specific.
> - Remove description from dmas, reg, power-domains.
> - Remove a limit of 2 from #address-cells and #size-cells.
> - Fix add ^ to csi-bridge subnode regex.
> - Make ranges mandatory.
> - Add unit address in example.
> - Add a reference to cdns,csi2rx in csi-bridge subnode.
> - Expand the example to include the csi-bridge subnode as well.
> - Re-order subject prefixes.
> - Convert OV5640 to use runtime PM and drop Cadence CSI2RX s_power
> patch.
> - Drop subdev call wrappers from cdns-csi2rx.
> - Move VPE and CAL to a separate subdir.
> - Rename ti-csi2rx.c to j721e-csi2rx.c
> 
> Pratyush Yadav (13):
>   media: cadence: csi2rx: Unregister v4l2 async notifier
>   media: cadence: csi2rx: Cleanup media entity properly
>   media: cadence: csi2rx: Add get_fmt and set_fmt pad ops
>   media: cadence: csi2rx: Add external DPHY support
>   media: cadence: csi2rx: Soft reset the streams before starting
> capture
>   media: cadence: csi2rx: Set the STOP bit when stopping a stream
>   media: cadence: csi2rx: Fix stream data configuration
>   media: cadence: csi2rx: Populate subdev devnode
>   media: cadence: csi2rx: Add link validation
>   media: ti: Add CSI2RX support for J721E
>   media: dt-bindings: Make sure items in data-lanes are unique
>   media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver
>   media: dt-bindings: Convert Cadence CSI2RX binding to YAML
> 
>  .../devicetree/bindings/media/cdns,csi2rx.txt |  100 --
>  .../bindings/media/cdns,csi2rx.yaml           |  176 +++
>  .../bindings/media/ti,j721e-csi2rx.yaml       |  101 ++
>  .../bindings/media/video-interfaces.yaml      |    1 +
>  MAINTAINERS                                   |    7 +
>  drivers/media/platform/cadence/cdns-csi2rx.c  |  273 ++++-
>  drivers/media/platform/ti/Kconfig             |   12 +
>  drivers/media/platform/ti/Makefile            |    1 +
>  .../media/platform/ti/j721e-csi2rx/Makefile   |    2 +
>  .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 1022
> +++++++++++++++++
>  10 files changed, 1580 insertions(+), 115 deletions(-)
>  delete mode 100644
> Documentation/devicetree/bindings/media/cdns,csi2rx.txt
>  create mode 100644
> Documentation/devicetree/bindings/media/cdns,csi2rx.yaml
>  create mode 100644 Documentation/devicetree/bindings/media/ti,j721e-
> csi2rx.yaml
>  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/Makefile
>  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/j721e-
> csi2rx.c
> 


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E
  2023-03-14 11:55 ` [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E Vaishnav Achath
  2023-03-20 12:51   ` Jai Luthra
@ 2023-03-24 18:14   ` Laurent Pinchart
  2023-03-28  8:19     ` Vaishnav Achath
  2023-04-04 11:43     ` Tomi Valkeinen
  2023-03-31 13:27   ` Sakari Ailus
  2 siblings, 2 replies; 30+ messages in thread
From: Laurent Pinchart @ 2023-03-24 18:14 UTC (permalink / raw)
  To: Vaishnav Achath
  Cc: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, sakari.ailus, tomi.valkeinen,
	linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

Hi Vaishnav,

Thank you for the patch.

On Tue, Mar 14, 2023 at 05:25:13PM +0530, Vaishnav Achath wrote:
> From: Pratyush Yadav <p.yadav@ti.com>
> 
> TI's J721E uses the Cadence CSI2RX and DPHY peripherals to facilitate
> capture over a CSI-2 bus.
> 
> The Cadence CSI2RX IP acts as a bridge between the TI specific parts and
> the CSI-2 protocol parts. TI then has a wrapper on top of this bridge
> called the SHIM layer. It takes in data from stream 0, repacks it, and
> sends it to memory over PSI-L DMA.
> 
> This driver acts as the "front end" to V4L2 client applications. It
> implements the required ioctls and buffer operations, passes the
> necessary calls on to the bridge, programs the SHIM layer, and performs
> DMA via the dmaengine API to finally return the data to a buffer
> supplied by the application.
> 
> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> Signed-off-by: Jai Luthra <j-luthra@ti.com>
> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
> ---
> 
> Changes in v7:
> - Fix incorrect value written in SHIM_PSI_CFG0_DST_TAG
> - Drop support for 2X8 formats.
> - Update maintainer to Vaishnav as Pratyush moved out of TI.
> - Address Sakari's review comments:
> - Update MAX_HEIGHT_LINES, MAX_WIDTH_BYTES to prevent overflow.
> - Assign dma_slave_config during declaration, drop memset().
> - dma_release_channel() on slave_config failure.
> - provide entity ops for the vdev entity with link_validate().
> - mutex_destroy() on ti_csi2rx_probe failure path.
> - Drop busy check in remove().
> - mutex_destroy() in ti_csi2rx_remove().
> - Address Laurent's review comments:
> - Update entries in Makefile in alphabetical order.
> - include headers in alphabetical order.
> - Drop redundant CSI DT defines and use from media/mipi-csi2.h.
> - Rename csi_df to csi_dt.
> - Drop v4l2_colorspace from ti_csi2rx_fmt and set default in
>   ti_csi2rx_v4l2_init()
> - Adjust field and not return EINVAL in ti_csi2rx_try_fmt_vid_cap().
> - inline ti_csi2rx_video_register().
> - start DMA before starting source subdev.
> - move buffer cleanup to separate function ti_csi2rx_cleanup_buffers()
>   to be used in ti_csi2rx_stop_streaming() and ti_csi2rx_start_streaming()
>   failure path.
> - Drop VB2_USERPTR, VB2_READ and V4L2_CAP_READWRITE.
> 
> Changes in v5:
> - Add dependency on PHY_CADENCE_DPHY_RX instead of PHY_CADENCE_DPHY
>   since the Rx mode DPHY now has a separate driver.
> - Drop ti_csi2rx_validate_pipeline(). Pipeline validation should be done
>   at media_pipeline_start().
> - Do not assign flags.
> - Fix error handling in ti_csi2rx_start_streaming(). Free up vb2 buffers
>   when media_pipeline_start() fails.
> 
> Changes in v4:
> - Acquire the media device's graph_mutex before starting the graph walk.
> - Call media_graph_walk_init() and media_graph_walk_cleanup() when
>   starting and ending the graph walk respectively.
> - Reduce max frame height and width in enum_framesizes. Currently they
>   are set to UINT_MAX but they must be a multiple of step_width, so they
>   need to be rounded down. Also, these values are absurdly large which
>   causes some userspace applications like gstreamer to trip up. While it
>   is not generally right to change the kernel for an application bug, it
>   is not such a big deal here. This change is replacing one set of
>   absurdly large arbitrary values with another set of smaller but still
>   absurdly large arbitrary values. Both limits are unlikely to be hit in
>   practice.
> 
> Changes in v3:
> - Move DMA related fields in struct ti_csi2rx_dma.
> - Protect DMA buffer queue with a spinlock to make sure the queue buffer
>   and DMA callback don't race on it.
> - Track the current DMA state. It might go idle because of a lack of
>   buffers. This state can be used to restart it if needed.
> - Do not include the current buffer in the pending queue. It is slightly
>   better modelling than leaving it at the head of the pending queue.
> - Use the buffer as the callback argument, and add a reference to csi in it.
> - If queueing a buffer to DMA fails, the buffer gets leaked and DMA gets
>   stalled with. Instead, report the error to vb2 and queue the next
>   buffer in the pending queue.
> - DMA gets stalled if we run out of buffers since the callback is the
>   only one that fires subsequent transfers and it is no longer being
>   called. Check for that when queueing buffers and restart DMA if
>   needed.
> - Do not put of node until we are done using the fwnode.
> - Set inital format to UYVY 640x480.
> 
> Changes in v2:
> - Use dmaengine_get_dma_device() instead of directly accessing
>   dma->device->dev.
> - Do not set dst_addr_width when configuring slave DMA.
> - Move to a separate subdir and rename to j721e-csi2rx.c
> - Convert compatible to ti,j721e-csi2rx.
> - Move to use Media Controller centric APIs.
> - Improve cleanup in probe when one of the steps fails.
> - Add colorspace to formats database.
> - Set hw_revision on media_device.
> - Move video device initialization to probe time instead of register time.
> 
>  MAINTAINERS                                   |    6 +
>  drivers/media/platform/ti/Kconfig             |   12 +
>  drivers/media/platform/ti/Makefile            |    1 +
>  .../media/platform/ti/j721e-csi2rx/Makefile   |    2 +
>  .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 1022 +++++++++++++++++
>  5 files changed, 1043 insertions(+)
>  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/Makefile
>  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> 
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 2892858cb040..69c2b0f48073 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -20901,6 +20901,12 @@ S:	Odd Fixes
>  F:	drivers/clk/ti/
>  F:	include/linux/clk/ti.h
>  
> +TI J721E CSI2RX DRIVER
> +M:	Vaishnav Achath <vaishnav.a@ti.com>
> +L:	linux-media@vger.kernel.org
> +S:	Supported
> +F:	drivers/media/platform/ti/j721e-csi2rx/
> +
>  TI DAVINCI MACHINE SUPPORT
>  M:	Bartosz Golaszewski <brgl@bgdev.pl>
>  L:	linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
> diff --git a/drivers/media/platform/ti/Kconfig b/drivers/media/platform/ti/Kconfig
> index e1ab56c3be1f..42c908f6e1ae 100644
> --- a/drivers/media/platform/ti/Kconfig
> +++ b/drivers/media/platform/ti/Kconfig
> @@ -63,6 +63,18 @@ config VIDEO_TI_VPE_DEBUG
>  	help
>  	  Enable debug messages on VPE driver.
>  
> +config VIDEO_TI_J721E_CSI2RX
> +	tristate "TI J721E CSI2RX wrapper layer driver"
> +	depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
> +	depends on MEDIA_SUPPORT && MEDIA_CONTROLLER
> +	depends on PHY_CADENCE_DPHY_RX && VIDEO_CADENCE_CSI2RX
> +	depends on ARCH_K3 || COMPILE_TEST
> +	select VIDEOBUF2_DMA_CONTIG
> +	select V4L2_FWNODE
> +	help
> +	  Support for TI CSI2RX wrapper layer. This just enables the wrapper driver.
> +	  The Cadence CSI2RX bridge driver needs to be enabled separately.
> +
>  source "drivers/media/platform/ti/am437x/Kconfig"
>  source "drivers/media/platform/ti/davinci/Kconfig"
>  source "drivers/media/platform/ti/omap/Kconfig"
> diff --git a/drivers/media/platform/ti/Makefile b/drivers/media/platform/ti/Makefile
> index 98c5fe5c40d6..8a2f74c9380e 100644
> --- a/drivers/media/platform/ti/Makefile
> +++ b/drivers/media/platform/ti/Makefile
> @@ -3,5 +3,6 @@ obj-y += am437x/
>  obj-y += cal/
>  obj-y += vpe/
>  obj-y += davinci/
> +obj-y += j721e-csi2rx/
>  obj-y += omap/
>  obj-y += omap3isp/
> diff --git a/drivers/media/platform/ti/j721e-csi2rx/Makefile b/drivers/media/platform/ti/j721e-csi2rx/Makefile
> new file mode 100644
> index 000000000000..377afc1d6280
> --- /dev/null
> +++ b/drivers/media/platform/ti/j721e-csi2rx/Makefile
> @@ -0,0 +1,2 @@
> +# SPDX-License-Identifier: GPL-2.0
> +obj-$(CONFIG_VIDEO_TI_J721E_CSI2RX) += j721e-csi2rx.o
> diff --git a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> new file mode 100644
> index 000000000000..0c8dad049f5b
> --- /dev/null
> +++ b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> @@ -0,0 +1,1022 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * TI CSI2 RX driver.
> + *
> + * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com/
> + *
> + * Author: Pratyush Yadav <p.yadav@ti.com>
> + */
> +
> +#include <linux/bitfield.h>
> +#include <linux/dmaengine.h>
> +#include <linux/module.h>
> +#include <linux/of_platform.h>
> +#include <linux/platform_device.h>
> +
> +#include <media/mipi-csi2.h>
> +#include <media/v4l2-device.h>
> +#include <media/v4l2-ioctl.h>
> +#include <media/videobuf2-dma-contig.h>
> +
> +#define TI_CSI2RX_MODULE_NAME		"j721e-csi2rx"
> +
> +#define SHIM_CNTL			0x10
> +#define SHIM_CNTL_PIX_RST		BIT(0)
> +
> +#define SHIM_DMACNTX			0x20
> +#define SHIM_DMACNTX_EN			BIT(31)
> +#define SHIM_DMACNTX_YUV422		GENMASK(27, 26)
> +#define SHIM_DMACNTX_FMT		GENMASK(5, 0)
> +#define SHIM_DMACNTX_UYVY		0
> +#define SHIM_DMACNTX_VYUY		1
> +#define SHIM_DMACNTX_YUYV		2
> +#define SHIM_DMACNTX_YVYU		3
> +
> +#define SHIM_PSI_CFG0			0x24
> +#define SHIM_PSI_CFG0_SRC_TAG		GENMASK(15, 0)
> +#define SHIM_PSI_CFG0_DST_TAG		GENMASK(31, 15)
> +
> +#define PSIL_WORD_SIZE_BYTES		16
> +/*
> + * There are no hard limits on the width or height. The DMA engine can handle
> + * all sizes. The max width and height are arbitrary numbers for this driver.
> + * Use 16K * 16K as the arbitrary limit. It is large enough that it is unlikely
> + * the limit will be hit in practice.
> + */
> +#define MAX_WIDTH_BYTES			SZ_16K
> +#define MAX_HEIGHT_LINES		SZ_16K
> +
> +struct ti_csi2rx_fmt {
> +	u32				fourcc;	/* Four character code. */
> +	u32				code;	/* Mbus code. */
> +	u32				csi_dt;	/* CSI Data type. */
> +	u8				bpp;	/* Bits per pixel. */
> +};
> +
> +struct ti_csi2rx_buffer {
> +	/* Common v4l2 buffer. Must be first. */
> +	struct vb2_v4l2_buffer		vb;
> +	struct list_head		list;
> +	struct ti_csi2rx_dev		*csi;
> +};
> +
> +enum ti_csi2rx_dma_state {
> +	TI_CSI2RX_DMA_STOPPED,	/* Streaming not started yet. */
> +	TI_CSI2RX_DMA_IDLE,	/* Streaming but no pending DMA operation. */
> +	TI_CSI2RX_DMA_ACTIVE,	/* Streaming and pending DMA operation. */
> +};
> +
> +struct ti_csi2rx_dma {
> +	/* Protects all fields in this struct. */
> +	spinlock_t			lock;
> +	struct dma_chan			*chan;
> +	/* Buffers queued to the driver, waiting to be processed by DMA. */
> +	struct list_head		queue;
> +	enum ti_csi2rx_dma_state	state;
> +	/*
> +	 * Current buffer being processed by DMA. NULL if no buffer is being
> +	 * processed.
> +	 */
> +	struct ti_csi2rx_buffer		*curr;
> +};
> +
> +struct ti_csi2rx_dev {
> +	struct device			*dev;
> +	void __iomem			*shim;
> +	struct v4l2_device		v4l2_dev;
> +	struct video_device		vdev;
> +	struct media_device		mdev;
> +	struct media_pipeline		pipe;
> +	struct media_pad		pad;
> +	struct v4l2_async_notifier	notifier;
> +	struct v4l2_subdev		*subdev;
> +	struct vb2_queue		vidq;
> +	struct mutex			mutex; /* To serialize ioctls. */
> +	struct v4l2_format		v_fmt;
> +	struct ti_csi2rx_dma		dma;
> +	u32				sequence;
> +};
> +
> +static const struct ti_csi2rx_fmt formats[] = {
> +	{
> +		.fourcc			= V4L2_PIX_FMT_YUYV,
> +		.code			= MEDIA_BUS_FMT_YUYV8_1X16,
> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
> +		.bpp			= 16,
> +	}, {
> +		.fourcc			= V4L2_PIX_FMT_UYVY,
> +		.code			= MEDIA_BUS_FMT_UYVY8_1X16,
> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
> +		.bpp			= 16,
> +	}, {
> +		.fourcc			= V4L2_PIX_FMT_YVYU,
> +		.code			= MEDIA_BUS_FMT_YVYU8_1X16,
> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
> +		.bpp			= 16,
> +	}, {
> +		.fourcc			= V4L2_PIX_FMT_VYUY,
> +		.code			= MEDIA_BUS_FMT_VYUY8_1X16,
> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
> +		.bpp			= 16,
> +	},
> +
> +	/* More formats can be supported but they are not listed for now. */
> +};
> +
> +static const unsigned int num_formats = ARRAY_SIZE(formats);
> +
> +/* Forward declaration needed by ti_csi2rx_dma_callback. */
> +static int ti_csi2rx_start_dma(struct ti_csi2rx_dev *csi,
> +			       struct ti_csi2rx_buffer *buf);
> +
> +static const struct ti_csi2rx_fmt *find_format_by_pix(u32 pixelformat)
> +{
> +	unsigned int i;
> +
> +	for (i = 0; i < num_formats; i++) {
> +		if (formats[i].fourcc == pixelformat)
> +			return &formats[i];
> +	}
> +
> +	return NULL;
> +}
> +
> +static const struct ti_csi2rx_fmt *find_format_by_code(u32 code)
> +{
> +	unsigned int i;
> +
> +	for (i = 0; i < num_formats; i++) {
> +		if (formats[i].code == code)
> +			return &formats[i];
> +	}
> +
> +	return NULL;
> +}
> +
> +static void ti_csi2rx_fill_fmt(const struct ti_csi2rx_fmt *csi_fmt,
> +			       struct v4l2_format *v4l2_fmt)
> +{
> +	struct v4l2_pix_format *pix = &v4l2_fmt->fmt.pix;
> +	unsigned int pixels_in_word;
> +	u8 bpp = csi_fmt->bpp;
> +	u32 bpl;
> +
> +	/* TODO: Figure this out. */

Figure what out ? :-) Please expand this comment to explain what needs
to be done.

> +
> +	pixels_in_word = PSIL_WORD_SIZE_BYTES * 8 / bpp;
> +
> +	pix->width = clamp_t(unsigned int, pix->width,
> +			     pixels_in_word,
> +			     MAX_WIDTH_BYTES * 8 / bpp);
> +	pix->width = rounddown(pix->width, pixels_in_word);
> +
> +	pix->height = clamp_t(unsigned int, pix->height, 1, MAX_HEIGHT_LINES);
> +
> +	v4l2_fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> +	pix->pixelformat = csi_fmt->fourcc;
> +	pix->colorspace = V4L2_COLORSPACE_SRGB;
> +	pix->sizeimage = pix->height * pix->width * (bpp / 8);
> +
> +	bpl = (pix->width * ALIGN(bpp, 8)) >> 3;
> +	pix->bytesperline = ALIGN(bpl, 16);
> +}
> +
> +static int ti_csi2rx_querycap(struct file *file, void *priv,
> +			      struct v4l2_capability *cap)
> +{
> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
> +
> +	strscpy(cap->driver, TI_CSI2RX_MODULE_NAME, sizeof(cap->driver));
> +	strscpy(cap->card, TI_CSI2RX_MODULE_NAME, sizeof(cap->card));
> +
> +	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
> +		 dev_name(csi->dev));

https://lore.kernel.org/linux-media/20220122163656.168440-1-sakari.ailus@linux.intel.com/
has been merged in v5.19, so you don't need to set bus_info manually
anymore.

> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_enum_fmt_vid_cap(struct file *file, void *priv,
> +				      struct v4l2_fmtdesc *f)
> +{
> +	if (f->index >= num_formats)
> +		return -EINVAL;
> +
> +	memset(f->reserved, 0, sizeof(f->reserved));
> +	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> +	f->pixelformat = formats[f->index].fourcc;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_g_fmt_vid_cap(struct file *file, void *prov,
> +				   struct v4l2_format *f)
> +{
> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
> +
> +	*f = csi->v_fmt;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_try_fmt_vid_cap(struct file *file, void *priv,
> +				     struct v4l2_format *f)
> +{
> +	const struct ti_csi2rx_fmt *fmt;
> +
> +	/*
> +	 * Default to the first format if the requested pixel format code isn't
> +	 * supported.
> +	 */
> +	fmt = find_format_by_pix(f->fmt.pix.pixelformat);
> +	if (!fmt)
> +		fmt = &formats[0];
> +
> +	if (f->fmt.pix.field == V4L2_FIELD_ANY)
> +		f->fmt.pix.field = V4L2_FIELD_NONE;
> +
> +	if (f->fmt.pix.field != V4L2_FIELD_NONE)
> +		/* Interlaced formats are not supported. */
> +		f->fmt.pix.field = V4L2_FIELD_NONE;
> +
> +	ti_csi2rx_fill_fmt(fmt, f);
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_s_fmt_vid_cap(struct file *file, void *priv,
> +				   struct v4l2_format *f)
> +{
> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
> +	struct vb2_queue *q = &csi->vidq;
> +	int ret;
> +
> +	if (vb2_is_busy(q))
> +		return -EBUSY;
> +
> +	ret = ti_csi2rx_try_fmt_vid_cap(file, priv, f);
> +	if (ret < 0)
> +		return ret;
> +
> +	csi->v_fmt = *f;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_enum_framesizes(struct file *file, void *fh,
> +				     struct v4l2_frmsizeenum *fsize)
> +{
> +	const struct ti_csi2rx_fmt *fmt;
> +	unsigned int pixels_in_word;
> +	u8 bpp;
> +
> +	fmt = find_format_by_pix(fsize->pixel_format);
> +	if (!fmt)
> +		return -EINVAL;
> +
> +	bpp = ALIGN(fmt->bpp, 8);
> +
> +	/*
> +	 * Number of pixels in one PSI-L word. The transfer happens in multiples
> +	 * of PSI-L word sizes.
> +	 */
> +	pixels_in_word = PSIL_WORD_SIZE_BYTES * 8 / bpp;
> +
> +	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> +	fsize->stepwise.min_width = pixels_in_word;
> +	fsize->stepwise.max_width = rounddown(MAX_WIDTH_BYTES * 8 / bpp,
> +					      pixels_in_word);
> +	fsize->stepwise.step_width = pixels_in_word;
> +	fsize->stepwise.min_height = 1;
> +	fsize->stepwise.max_height = MAX_HEIGHT_LINES;
> +	fsize->stepwise.step_height = 1;
> +
> +	return 0;
> +}
> +
> +static const struct v4l2_ioctl_ops csi_ioctl_ops = {
> +	.vidioc_querycap      = ti_csi2rx_querycap,
> +	.vidioc_enum_fmt_vid_cap = ti_csi2rx_enum_fmt_vid_cap,
> +	.vidioc_try_fmt_vid_cap = ti_csi2rx_try_fmt_vid_cap,
> +	.vidioc_g_fmt_vid_cap = ti_csi2rx_g_fmt_vid_cap,
> +	.vidioc_s_fmt_vid_cap = ti_csi2rx_s_fmt_vid_cap,
> +	.vidioc_enum_framesizes = ti_csi2rx_enum_framesizes,
> +	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
> +	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
> +	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
> +	.vidioc_querybuf      = vb2_ioctl_querybuf,
> +	.vidioc_qbuf          = vb2_ioctl_qbuf,
> +	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
> +	.vidioc_expbuf        = vb2_ioctl_expbuf,
> +	.vidioc_streamon      = vb2_ioctl_streamon,
> +	.vidioc_streamoff     = vb2_ioctl_streamoff,
> +};
> +
> +static const struct v4l2_file_operations csi_fops = {
> +	.owner = THIS_MODULE,
> +	.open = v4l2_fh_open,
> +	.release = vb2_fop_release,
> +	.read = vb2_fop_read,
> +	.poll = vb2_fop_poll,
> +	.unlocked_ioctl = video_ioctl2,
> +	.mmap = vb2_fop_mmap,
> +};
> +
> +static inline int ti_csi2rx_video_register(struct ti_csi2rx_dev *csi)

You can drop the "inline" keyword, the compiler will inline the function
automatically if it believes it makes sense.

> +{
> +	struct video_device *vdev = &csi->vdev;
> +	int ret, src_pad;
> +
> +	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
> +	if (ret)
> +		return ret;
> +
> +	src_pad = media_entity_get_fwnode_pad(&csi->subdev->entity,
> +					      csi->subdev->fwnode,
> +					      MEDIA_PAD_FL_SOURCE);
> +	if (src_pad < 0) {
> +		dev_err(csi->dev, "Couldn't find source pad for subdev\n");
> +		return src_pad;
> +	}
> +
> +	ret = media_create_pad_link(&csi->subdev->entity, src_pad,
> +				    &vdev->entity, 0,
> +				    MEDIA_LNK_FL_IMMUTABLE |
> +				    MEDIA_LNK_FL_ENABLED);
> +	if (ret) {
> +		video_unregister_device(vdev);
> +		return ret;
> +	}

I've sent a patch that makes v4l2_create_fwnode_links_to_pad() usable
when the sink is not a subdev. Could you test it to see if it can
simplify this function ?

> +
> +	return 0;
> +}
> +
> +static int csi_async_notifier_bound(struct v4l2_async_notifier *notifier,
> +				    struct v4l2_subdev *subdev,
> +				    struct v4l2_async_subdev *asd)
> +{
> +	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
> +
> +	csi->subdev = subdev;
> +
> +	return 0;
> +}
> +
> +static int csi_async_notifier_complete(struct v4l2_async_notifier *notifier)
> +{
> +	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
> +	int ret;
> +
> +	ret = ti_csi2rx_video_register(csi);

I commented on the previous version with

  I would inline the ti_csi2rx_video_register() function here, it's not
  large, and it's only called in a single place.

which I assume you've understood as asking for the "inline" keyword
above. What I meant was to take the contents of the
ti_csi2rx_video_register() function and move them into this function.

> +	if (ret)
> +		return ret;
> +
> +	return v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
> +}
> +
> +static const struct v4l2_async_notifier_operations csi_async_notifier_ops = {
> +	.bound = csi_async_notifier_bound,
> +	.complete = csi_async_notifier_complete,
> +};
> +
> +static int ti_csi2rx_init_subdev(struct ti_csi2rx_dev *csi)
> +{
> +	struct fwnode_handle *fwnode;
> +	struct v4l2_async_subdev *asd;
> +	struct device_node *node;
> +	int ret;
> +
> +	node = of_get_child_by_name(csi->dev->of_node, "csi-bridge");
> +	if (!node)
> +		return -EINVAL;
> +
> +	fwnode = of_fwnode_handle(node);
> +	if (!fwnode) {
> +		of_node_put(node);
> +		return -EINVAL;
> +	}
> +
> +	v4l2_async_nf_init(&csi->notifier);
> +	csi->notifier.ops = &csi_async_notifier_ops;
> +
> +	asd = v4l2_async_nf_add_fwnode(&csi->notifier, fwnode,
> +				       struct v4l2_async_subdev);
> +	of_node_put(node);
> +	if (IS_ERR(asd)) {
> +		v4l2_async_nf_cleanup(&csi->notifier);
> +		return PTR_ERR(asd);
> +	}
> +
> +	ret = v4l2_async_nf_register(&csi->v4l2_dev, &csi->notifier);
> +	if (ret) {
> +		v4l2_async_nf_cleanup(&csi->notifier);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static void ti_csi2rx_setup_shim(struct ti_csi2rx_dev *csi)
> +{
> +	const struct ti_csi2rx_fmt *fmt;
> +	unsigned int reg;
> +
> +	fmt = find_format_by_pix(csi->v_fmt.fmt.pix.pixelformat);
> +	if (!fmt) {
> +		dev_err(csi->dev, "Unknown format\n");
> +		return;
> +	}
> +
> +	/* De-assert the pixel interface reset. */
> +	reg = SHIM_CNTL_PIX_RST;
> +	writel(reg, csi->shim + SHIM_CNTL);
> +
> +	reg = SHIM_DMACNTX_EN;
> +	reg |= FIELD_PREP(SHIM_DMACNTX_FMT, fmt->csi_dt);
> +
> +	/*
> +	 * Using the values from the documentation gives incorrect ordering for
> +	 * the luma and chroma components. In practice, the "reverse" format
> +	 * gives the correct image. So for example, if the image is in UYVY, the
> +	 * reverse would be YVYU.
> +	 */
> +	switch (fmt->fourcc) {
> +	case V4L2_PIX_FMT_UYVY:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_YVYU);
> +		break;
> +	case V4L2_PIX_FMT_VYUY:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_YUYV);
> +		break;
> +	case V4L2_PIX_FMT_YUYV:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_VYUY);
> +		break;
> +	case V4L2_PIX_FMT_YVYU:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_UYVY);
> +		break;
> +	default:
> +		/* Ignore if not YUV 4:2:2 */
> +		break;
> +	}
> +
> +	writel(reg, csi->shim + SHIM_DMACNTX);
> +
> +	reg = FIELD_PREP(SHIM_PSI_CFG0_SRC_TAG, 0) |
> +	      FIELD_PREP(SHIM_PSI_CFG0_DST_TAG, 0);
> +	writel(reg, csi->shim + SHIM_PSI_CFG0);
> +}
> +
> +static void ti_csi2rx_dma_callback(void *param)
> +{
> +	struct ti_csi2rx_buffer *buf = param;
> +	struct ti_csi2rx_dev *csi = buf->csi;
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	unsigned long flags;
> +
> +	buf->vb.vb2_buf.timestamp = ktime_get_ns();
> +	buf->vb.sequence = csi->sequence++;

In a reply to the previous version, Pratyush mentioned that there is a
register in the CSI2RX core that provides the frame number. While it may
not be very straightforward to get the information from there, it would
still be desirable. Could you please add a TODO comment to record this ?

	/*
	 * TODO: Derive the sequence number from the CSI2RX frame number
	 * hardware register.
	 */

> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +
> +	WARN_ON(dma->curr != buf);
> +	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
> +
> +	/* If there are more buffers to process then start their transfer. */
> +	dma->curr = NULL;
> +	while (!list_empty(&dma->queue)) {
> +		buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
> +		list_del(&buf->list);
> +
> +		if (ti_csi2rx_start_dma(csi, buf)) {
> +			dev_err(csi->dev, "Failed to queue the next buffer for DMA\n");
> +			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
> +		} else {
> +			dma->curr = buf;
> +			break;

In the same reply, Pratyush also mentioned that all pending buffers
should be queued to the DMA engine here.

> +		}
> +	}
> +
> +	if (!dma->curr)
> +		dma->state = TI_CSI2RX_DMA_IDLE;
> +
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +}
> +
> +static int ti_csi2rx_start_dma(struct ti_csi2rx_dev *csi,
> +			       struct ti_csi2rx_buffer *buf)
> +{
> +	unsigned long addr;
> +	struct dma_async_tx_descriptor *desc;
> +	size_t len = csi->v_fmt.fmt.pix.sizeimage;
> +	dma_cookie_t cookie;
> +	int ret = 0;
> +
> +	addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
> +	desc = dmaengine_prep_slave_single(csi->dma.chan, addr, len,
> +					   DMA_DEV_TO_MEM,
> +					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
> +	if (!desc)
> +		return -EIO;
> +
> +	desc->callback = ti_csi2rx_dma_callback;
> +	desc->callback_param = buf;
> +
> +	cookie = dmaengine_submit(desc);
> +	ret = dma_submit_error(cookie);
> +	if (ret)
> +		return ret;
> +
> +	dma_async_issue_pending(csi->dma.chan);
> +
> +	return 0;
> +}
> +
> +static void ti_csi2rx_cleanup_buffers(struct ti_csi2rx_dev *csi,
> +				      enum vb2_buffer_state state)
> +{
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	struct ti_csi2rx_buffer *buf = NULL, *tmp;

No need to initialize buf to NULL.

> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	list_for_each_entry_safe(buf, tmp, &csi->dma.queue, list) {
> +		list_del(&buf->list);
> +		vb2_buffer_done(&buf->vb.vb2_buf, state);
> +	}
> +
> +	if (dma->curr)
> +		vb2_buffer_done(&dma->curr->vb.vb2_buf, state);
> +
> +	dma->curr = NULL;
> +	dma->state = TI_CSI2RX_DMA_STOPPED;
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +}
> +
> +static int ti_csi2rx_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
> +				 unsigned int *nplanes, unsigned int sizes[],
> +				 struct device *alloc_devs[])
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(q);
> +	unsigned int size = csi->v_fmt.fmt.pix.sizeimage;
> +
> +	if (*nplanes) {
> +		if (sizes[0] < size)
> +			return -EINVAL;
> +		size = sizes[0];
> +	}
> +
> +	*nplanes = 1;
> +	sizes[0] = size;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_buffer_prepare(struct vb2_buffer *vb)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
> +	unsigned long size = csi->v_fmt.fmt.pix.sizeimage;
> +
> +	if (vb2_plane_size(vb, 0) < size) {
> +		dev_err(csi->dev, "Data will not fit into plane\n");
> +		return -EINVAL;
> +	}
> +
> +	vb2_set_plane_payload(vb, 0, size);
> +	return 0;
> +}
> +
> +static void ti_csi2rx_buffer_queue(struct vb2_buffer *vb)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
> +	struct ti_csi2rx_buffer *buf;
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	unsigned long flags = 0;
> +	int ret;
> +
> +	buf = container_of(vb, struct ti_csi2rx_buffer, vb.vb2_buf);
> +	buf->csi = csi;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	/*
> +	 * Usually the DMA callback takes care of queueing the pending buffers.
> +	 * But if DMA has stalled due to lack of buffers, restart it now.
> +	 */
> +	if (dma->state == TI_CSI2RX_DMA_IDLE) {
> +		ret = ti_csi2rx_start_dma(csi, buf);
> +		if (ret) {
> +			dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
> +			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
> +			goto unlock;
> +		}
> +
> +		dma->curr = buf;
> +		dma->state = TI_CSI2RX_DMA_ACTIVE;
> +	} else {
> +		list_add_tail(&buf->list, &dma->queue);
> +	}
> +
> +unlock:
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +}
> +
> +static int ti_csi2rx_start_streaming(struct vb2_queue *vq, unsigned int count)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	struct ti_csi2rx_buffer *buf;
> +	unsigned long flags;
> +	int ret = 0;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	if (list_empty(&dma->queue))
> +		ret = -EIO;
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +	if (ret)
> +		return ret;
> +
> +	ret = media_pipeline_start(&csi->vdev.entity.pads[0], &csi->pipe);

Please use the video_device_pipeline_start() helper for video nodes.
Same for media_pipeline_stop(), there's now
video_device_pipeline_stop().

> +	if (ret)
> +		goto err;
> +
> +	ti_csi2rx_setup_shim(csi);
> +
> +	csi->sequence = 0;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
> +	list_del(&buf->list);
> +	dma->state = TI_CSI2RX_DMA_ACTIVE;
> +	dma->curr = buf;
> +
> +	ret = ti_csi2rx_start_dma(csi, buf);
> +	if (ret) {
> +		dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
> +		spin_unlock_irqrestore(&dma->lock, flags);
> +		goto err_pipeline;
> +	}
> +
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +
> +	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 1);
> +	if (ret)
> +		goto err_dma;
> +
> +	return 0;
> +
> +err_dma:
> +	dmaengine_terminate_sync(csi->dma.chan);
> +	writel(0, csi->shim + SHIM_DMACNTX);
> +err_pipeline:
> +	media_pipeline_stop(&csi->vdev.entity.pads[0]);
> +err:
> +	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_QUEUED);
> +	return ret;
> +}
> +
> +static void ti_csi2rx_stop_streaming(struct vb2_queue *vq)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
> +	int ret;
> +
> +	media_pipeline_stop(&csi->vdev.entity.pads[0]);
> +
> +	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 0);
> +	if (ret)
> +		dev_err(csi->dev, "Failed to stop subdev stream\n");
> +
> +	writel(0, csi->shim + SHIM_CNTL);
> +
> +	ret = dmaengine_terminate_sync(csi->dma.chan);
> +	if (ret)
> +		dev_err(csi->dev, "Failed to stop DMA: %d\n", ret);
> +
> +	writel(0, csi->shim + SHIM_DMACNTX);
> +
> +	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_ERROR);
> +}
> +
> +static const struct vb2_ops csi_vb2_qops = {
> +	.queue_setup = ti_csi2rx_queue_setup,
> +	.buf_prepare = ti_csi2rx_buffer_prepare,
> +	.buf_queue = ti_csi2rx_buffer_queue,
> +	.start_streaming = ti_csi2rx_start_streaming,
> +	.stop_streaming = ti_csi2rx_stop_streaming,
> +	.wait_prepare = vb2_ops_wait_prepare,
> +	.wait_finish = vb2_ops_wait_finish,
> +};
> +
> +static int ti_csi2rx_init_vb2q(struct ti_csi2rx_dev *csi)
> +{
> +	struct vb2_queue *q = &csi->vidq;
> +	int ret;
> +
> +	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> +	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;

Support for VB2_READ is not recommended as it's very inefficient. Please
drop it.

> +	q->drv_priv = csi;
> +	q->buf_struct_size = sizeof(struct ti_csi2rx_buffer);
> +	q->ops = &csi_vb2_qops;
> +	q->mem_ops = &vb2_dma_contig_memops;
> +	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> +	q->dev = dmaengine_get_dma_device(csi->dma.chan);
> +	q->lock = &csi->mutex;
> +	q->min_buffers_needed = 1;
> +
> +	ret = vb2_queue_init(q);
> +	if (ret)
> +		return ret;
> +
> +	csi->vdev.queue = q;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_link_validate_get_fmt(struct media_pad *pad,
> +					   struct v4l2_subdev_format *fmt)
> +{
> +	if (is_media_entity_v4l2_subdev(pad->entity)) {
> +		struct v4l2_subdev *sd =
> +			media_entity_to_v4l2_subdev(pad->entity);
> +
> +		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
> +		fmt->pad = pad->index;
> +		return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);

This will crash with any source that uses the subdev active state. You
can't pass NULL for the state here.

How about exporting v4l2_subdev_link_validate_get_format() instead, to
replace this function ?

> +	}
> +
> +	WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
> +	     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
> +	     pad->entity->function, pad->entity->name);
> +
> +	return -EINVAL;
> +}
> +
> +static int ti_csi2rx_link_validate(struct media_link *link)
> +{
> +	struct media_entity *entity = link->sink->entity;
> +	struct video_device *vdev = media_entity_to_video_device(entity);
> +	struct ti_csi2rx_dev *csi = container_of(vdev, struct ti_csi2rx_dev, vdev);
> +	struct v4l2_pix_format *csi_fmt = &csi->v_fmt.fmt.pix;
> +	struct v4l2_subdev_format source_fmt;
> +	const struct ti_csi2rx_fmt *ti_fmt;
> +	int ret;
> +
> +	if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
> +		dev_info(csi->dev, "video node %s pad not connected\n",
> +			 vdev->name);
> +		return -ENOTCONN;
> +	}

This can't happen, __media_pipeline_start() skips links that are not
enabled before calling .link_validate().

> +
> +	ret = ti_csi2rx_link_validate_get_fmt(link->source, &source_fmt);
> +	if (ret)
> +		return ret;
> +
> +	if (source_fmt.format.width != csi_fmt->width) {
> +		dev_err(csi->dev, "Width does not match (source %u, sink %u)\n",
> +			source_fmt.format.width, csi_fmt->width);

Make it dev_dbg(), we don't want to output error messages in the kernel
log for something that can be easily triggered by userspace. Same below.

> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.height != csi_fmt->height) {
> +		dev_err(csi->dev, "Height does not match (source %u, sink %u)\n",
> +			source_fmt.format.height, csi_fmt->height);
> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.field != csi_fmt->field &&
> +	    csi_fmt->field != V4L2_FIELD_NONE) {
> +		dev_err(csi->dev, "Field does not match (source %u, sink %u)\n",
> +			source_fmt.format.field, csi_fmt->field);
> +		return -EPIPE;
> +	}
> +
> +	ti_fmt = find_format_by_code(source_fmt.format.code);
> +	if (!ti_fmt) {
> +		dev_err(csi->dev, "Media bus format 0x%x not supported\n",
> +			source_fmt.format.code);
> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.code == MEDIA_BUS_FMT_YUYV8_2X8 ||
> +	    source_fmt.format.code == MEDIA_BUS_FMT_VYUY8_2X8 ||
> +	    source_fmt.format.code == MEDIA_BUS_FMT_YVYU8_2X8) {

Those three formats are not listed in the ti_csi2rx_fmt formats array,
so find_format_by_code() will have returned NULL already, and this check
can never be true.

> +		dev_err(csi->dev,
> +			"Only UYVY input allowed for YUV422 8-bit. Output format can be configured.\n");
> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.code != ti_fmt->code) {

That can never be true, as ti_fmt is looked up based on
source_fmt.format.code. Did you mean to check if ti_fmt->fourcc ==
csi->v_fmt.fmt.pix.pixelformat ?

> +		dev_err(csi->dev,
> +			"Cannot transform source fmt 0x%x to sink fmt 0x%x\n",
> +			source_fmt.format.code, ti_fmt->code);
> +		return -EPIPE;
> +	}
> +
> +	return 0;
> +}
> +
> +static const struct media_entity_operations ti_csi2rx_video_entity_ops = {
> +	.link_validate = ti_csi2rx_link_validate,
> +};
> +
> +static int ti_csi2rx_init_dma(struct ti_csi2rx_dev *csi)
> +{
> +	struct dma_slave_config cfg = {
> +		.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES };

	struct dma_slave_config cfg = {
		.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES,
	};

> +	int ret;
> +
> +	INIT_LIST_HEAD(&csi->dma.queue);
> +	spin_lock_init(&csi->dma.lock);
> +
> +	csi->dma.state = TI_CSI2RX_DMA_STOPPED;
> +
> +	csi->dma.chan = dma_request_chan(csi->dev, "rx0");
> +	if (IS_ERR(csi->dma.chan))
> +		return PTR_ERR(csi->dma.chan);
> +
> +	ret = dmaengine_slave_config(csi->dma.chan, &cfg);
> +	if (ret) {
> +		dma_release_channel(csi->dma.chan);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_v4l2_init(struct ti_csi2rx_dev *csi)
> +{
> +	struct media_device *mdev = &csi->mdev;
> +	struct video_device *vdev = &csi->vdev;
> +	const struct ti_csi2rx_fmt *fmt;
> +	struct v4l2_pix_format *pix_fmt = &csi->v_fmt.fmt.pix;
> +	int ret;
> +
> +	fmt = find_format_by_pix(V4L2_PIX_FMT_UYVY);
> +	if (!fmt)
> +		return -EINVAL;
> +
> +	pix_fmt->width = 640;
> +	pix_fmt->height = 480;
> +	pix_fmt->field = V4L2_FIELD_NONE;
> +
> +	ti_csi2rx_fill_fmt(fmt, &csi->v_fmt);
> +
> +	mdev->dev = csi->dev;
> +	mdev->hw_revision = 1;
> +	strscpy(mdev->model, "TI-CSI2RX", sizeof(mdev->model));
> +	snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
> +		 dev_name(mdev->dev));

You can drop manual setting of bus_info here too.

> +
> +	media_device_init(mdev);
> +
> +	strscpy(vdev->name, TI_CSI2RX_MODULE_NAME, sizeof(vdev->name));
> +	vdev->v4l2_dev = &csi->v4l2_dev;
> +	vdev->vfl_dir = VFL_DIR_RX;
> +	vdev->fops = &csi_fops;
> +	vdev->ioctl_ops = &csi_ioctl_ops;
> +	vdev->release = video_device_release_empty;
> +	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
> +			    V4L2_CAP_IO_MC;
> +	vdev->lock = &csi->mutex;
> +	video_set_drvdata(vdev, csi);
> +
> +	csi->pad.flags = MEDIA_PAD_FL_SINK;
> +	vdev->entity.ops = &ti_csi2rx_video_entity_ops;
> +	ret = media_entity_pads_init(&csi->vdev.entity, 1, &csi->pad);
> +	if (ret)
> +		return ret;
> +
> +	csi->v4l2_dev.mdev = mdev;
> +
> +	ret = v4l2_device_register(csi->dev, &csi->v4l2_dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = media_device_register(mdev);
> +	if (ret) {
> +		v4l2_device_unregister(&csi->v4l2_dev);
> +		media_device_cleanup(mdev);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static void ti_csi2rx_cleanup_dma(struct ti_csi2rx_dev *csi)
> +{
> +	dma_release_channel(csi->dma.chan);
> +}
> +
> +static void ti_csi2rx_cleanup_v4l2(struct ti_csi2rx_dev *csi)
> +{
> +	media_device_unregister(&csi->mdev);
> +	v4l2_device_unregister(&csi->v4l2_dev);
> +	media_device_cleanup(&csi->mdev);
> +}
> +
> +static void ti_csi2rx_cleanup_subdev(struct ti_csi2rx_dev *csi)
> +{
> +	v4l2_async_nf_unregister(&csi->notifier);
> +	v4l2_async_nf_cleanup(&csi->notifier);
> +}
> +
> +static void ti_csi2rx_cleanup_vb2q(struct ti_csi2rx_dev *csi)
> +{
> +	vb2_queue_release(&csi->vidq);
> +}
> +
> +static int ti_csi2rx_probe(struct platform_device *pdev)
> +{
> +	struct ti_csi2rx_dev *csi;
> +	struct resource *res;
> +	int ret;
> +
> +	csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
> +	if (!csi)
> +		return -ENOMEM;
> +
> +	csi->dev = &pdev->dev;
> +	platform_set_drvdata(pdev, csi);
> +
> +	mutex_init(&csi->mutex);
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	csi->shim = devm_ioremap_resource(&pdev->dev, res);
> +	if (IS_ERR(csi->shim)) {
> +		ret = PTR_ERR(csi->shim);
> +		goto err_mutex;
> +	}
> +
> +	ret = ti_csi2rx_init_dma(csi);
> +	if (ret)
> +		goto err_mutex;
> +
> +	ret = ti_csi2rx_v4l2_init(csi);
> +	if (ret)
> +		goto err_dma;
> +
> +	ret = ti_csi2rx_init_vb2q(csi);
> +	if (ret)
> +		goto err_v4l2;
> +
> +	ret = ti_csi2rx_init_subdev(csi);
> +	if (ret)
> +		goto err_vb2q;
> +
> +	ret = of_platform_populate(csi->dev->of_node, NULL, NULL, csi->dev);
> +	if (ret) {
> +		dev_err(csi->dev, "Failed to create children: %d\n", ret);
> +		goto err_subdev;
> +	}
> +
> +	return 0;
> +
> +err_subdev:
> +	ti_csi2rx_cleanup_subdev(csi);
> +err_vb2q:
> +	ti_csi2rx_cleanup_vb2q(csi);
> +err_v4l2:
> +	ti_csi2rx_cleanup_v4l2(csi);
> +err_dma:
> +	ti_csi2rx_cleanup_dma(csi);
> +err_mutex:
> +	mutex_destroy(&csi->mutex);
> +	return ret;
> +}
> +
> +static int ti_csi2rx_remove(struct platform_device *pdev)
> +{
> +	struct ti_csi2rx_dev *csi = platform_get_drvdata(pdev);
> +
> +	video_unregister_device(&csi->vdev);
> +
> +	ti_csi2rx_cleanup_vb2q(csi);
> +	ti_csi2rx_cleanup_subdev(csi);
> +	ti_csi2rx_cleanup_v4l2(csi);
> +	ti_csi2rx_cleanup_dma(csi);
> +
> +	mutex_destroy(&csi->mutex);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id ti_csi2rx_of_match[] = {
> +	{ .compatible = "ti,j721e-csi2rx", },
> +	{ },
> +};
> +MODULE_DEVICE_TABLE(of, ti_csi2rx_of_match);
> +
> +static struct platform_driver ti_csi2rx_pdrv = {
> +	.probe = ti_csi2rx_probe,
> +	.remove = ti_csi2rx_remove,
> +	.driver = {
> +		.name = TI_CSI2RX_MODULE_NAME,
> +		.of_match_table = ti_csi2rx_of_match,
> +	},
> +};
> +
> +module_platform_driver(ti_csi2rx_pdrv);
> +
> +MODULE_DESCRIPTION("TI J721E CSI2 RX Driver");
> +MODULE_AUTHOR("Pratyush Yadav <p.yadav@ti.com>");
> +MODULE_LICENSE("GPL");

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 00/13] CSI2RX support on J721E
  2023-03-23 19:36 ` [PATCH v7 00/13] CSI2RX support on J721E Martyn Welch
@ 2023-03-28  6:01   ` Jai Luthra
  2023-03-29 15:17     ` Martyn Welch
  0 siblings, 1 reply; 30+ messages in thread
From: Jai Luthra @ 2023-03-28  6:01 UTC (permalink / raw)
  To: Martyn Welch
  Cc: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen, linux-kernel, bparrot, niklas.soderlund+renesas,
	devarsht, praneeth, u-kumar1, vigneshr, nm

[-- Attachment #1: Type: text/plain, Size: 12161 bytes --]

Hi Martyn,

Thanks for the tests.

On Mar 23, 2023 at 19:36:18 +0000, Martyn Welch wrote:
> On Tue, 2023-03-14 at 17:25 +0530, Vaishnav Achath wrote:
> > Hi,
> > 
> > This series adds support for CSI2 capture on J721E. It includes some
> > fixes to the Cadence CSI2RX driver, and adds the TI CSI2RX wrapper
> > driver.
> > 
> > This is a V7 of the below V6 series,
> > https://lore.kernel.org/all/20220121142904.4091481-1-p.yadav@ti.com/
> > 
> > Since Pratyush moved out of TI, I will be working on upstreaming the
> > TI CSI2RX wrapper support.
> > 
> > Tested on TI's J721E EVM with LI OV5640 sensor module.
> > https://gist.github.com/vaishnavachath/f030a257d5b6569817bc9deab1c4fa77
> > 
> > Also, Tested on TI AM62-SK with Pcam5C OV5640 module.
> > https://gist.github.com/vaishnavachath/ff2605faa92f1a6ab5670426da28ccee
> > 
> 
> Hi Vaishnav,
> 
> I assume I'm doing something wrong. I have a TI AM62-SK and the Pcam5C
> OV5640 module. I've been trying to test this with gstreamer using the
> following command:
> 
> gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=10 ! video/x-
> raw, width=640, height=480, format=UYVY, framerate=30/1 ! jpegenc !
> multifilesink location=test%d.jpg
> 
> However I've not been able to get this working, failing with this
> failure unless I patch in some changes I found in the TI BSP:
> 
> [   28.083635] cdns-mipi-dphy-rx 30110000.phy: DPHY wait for lane ready
> timeout
> [   28.090905] cdns-csi2rx 30101000.csi-bridge: Failed to configure
> external DPHY: -110

That is expected as currently the DPHY driver does not release the SW 
reset which all SoCs since J721E SR1.0 expect. See the patch linked by 
Vaishnav below.

> 
> The changes (and the device tree nodes I added, which might be
> wrong...) can be found here:
> 
> https://gitlab.collabora.com/martyn/linux/-/commits/am625-sk-ov5640
> 
> Any ideas what I'm doing wrong?
> 
> Martyn
> 
> > For all newer TI platforms that TI J721E Silicon Revision 1.0, below
> > update
> > to DPHY RX driver is needed:
> > https://lore.kernel.org/all/20230314073137.2153-1-vaishnav.a@ti.com/

^

With that patch and this series on top of linux-next, I was able to get 
Pcam5C capturing with SK-AM62. Although I did face issues with the 
sensor framerate and had to revert a few recent sensor commits, not sure 
why exactly yet. But here is the working branch with all the changes: 
https://github.com/jailuthra/linux/commits/b4/csi_single

Let us know if you face any other issues during your tests.

> > 
> > Changes in v7:
> > - For patch 10/13 ("Add CSI2RX upport for J721E"):
> > - Fix incorrect value written in SHIM_PSI_CFG0_DST_TAG
> > - Drop support for 2X8 formats.
> > - Update maintainer to Vaishnav as Pratyush moved out of TI.
> > - Address Sakari's review comments:
> > - Update MAX_HEIGHT_LINES, MAX_WIDTH_BYTES to prevent overflow.
> > - Assign dma_slave_config during declaration, drop memset().
> > - dma_release_channel() on slave_config failure.
> > - provide entity ops for the vdev entity with link_validate().
> > - mutex_destroy() on ti_csi2rx_probe failure path.
> > - Drop busy check in remove().
> > - mutex_destroy() in ti_csi2rx_remove().
> > - Address Laurent's review comments:
> > - Update entries in Makefile in alphabetical order.
> > - include headers in alphabetical order.
> > - Drop redundant CSI DT defines and use from media/mipi-csi2.h.
> > - Rename csi_df to csi_dt.
> > - Drop v4l2_colorspace from ti_csi2rx_fmt and set default in
> >   ti_csi2rx_v4l2_init()
> > - Adjust field and not return EINVAL in ti_csi2rx_try_fmt_vid_cap().
> > - inline ti_csi2rx_video_register().
> > - start DMA before starting source subdev.
> > - move buffer cleanup to separate function
> > ti_csi2rx_cleanup_buffers()
> >   to be used in ti_csi2rx_stop_streaming() and
> > ti_csi2rx_start_streaming()
> >   failure path.
> > - Drop VB2_USERPTR, VB2_READ and V4L2_CAP_READWRITE.
> > - For patch 4/13 ("media: cadence: csi2rx: Add external DPHY
> > support"):
> > - Fix multiplier and divider in v4l2_get_link_freq() which caused
> >   failures during streaming.
> > 
> > Changes in v6:
> > - Move the lock around the dereference for framefmt in
> >   csi2rx_{get,set}_fmt() instead of when we get the pointer.
> > - Do not return an error when an unsupported format is set. Instead
> >   adjust the code to the first format in the list.
> > - Drop variable bpp and use fmt->bpp directly.
> > - Drop variable got_pm. Call phy_pm_runtime_put() unconditionally
> > since
> >   it will just return an error if runtime PM is not enabled.
> > - Drop transcoding from the commit message.
> > - Make csi2rx_media_ops const.
> > 
> > Changes in v5:
> > - Cleanup notifier in csi2rx_parse_dt() after the call to
> >   v4l2_async_nf_add_fwnode_remote().
> > - Use YUV 1X16 formats instead of 2X8.
> > - Only error out when phy_pm_runtime_get_sync() returns a negative
> >   value. A positive value can be returned if the phy was already
> >   resumed.
> > - Do not query the source subdev for format. Use the newly added
> >   internal format instead.
> > - Make i unsigned.
> > - Change %d to %u
> > - Add dependency on PHY_CADENCE_DPHY_RX instead of PHY_CADENCE_DPHY
> >   since the Rx mode DPHY now has a separate driver.
> > - Drop ti_csi2rx_validate_pipeline(). Pipeline validation should be
> > done
> >   at media_pipeline_start().
> > - Do not assign flags.
> > - Fix error handling in ti_csi2rx_start_streaming(). Free up vb2
> > buffers
> >   when media_pipeline_start() fails.
> > - Move clock description in comments under the clocks property.
> > - Make ports required.
> > - Add link validation to cdns-csi2rx driver.
> > 
> > Changes in v4:
> > - Drop the call to set PHY submode. It is now being done via
> > compatible
> >   on the DPHY side.
> > - Acquire the media device's graph_mutex before starting the graph
> > walk.
> > - Call media_graph_walk_init() and media_graph_walk_cleanup() when
> >   starting and ending the graph walk respectively.
> > - Reduce max frame height and width in enum_framesizes. Currently
> > they
> >   are set to UINT_MAX but they must be a multiple of step_width, so
> > they
> >   need to be rounded down. Also, these values are absurdly large
> > which
> >   causes some userspace applications like gstreamer to trip up. While
> > it
> >   is not generally right to change the kernel for an application bug,
> > it
> >   is not such a big deal here. This change is replacing one set of
> >   absurdly large arbitrary values with another set of smaller but
> > still
> >   absurdly large arbitrary values. Both limits are unlikely to be hit
> > in
> >   practice.
> > - Add power-domains property.
> > - Drop maxItems from clock-names.
> > - Drop the type for data-lanes.
> > - Drop uniqueItems from data-lanes. Move it to video-interfaces.yaml
> >   instead.
> > - Drop OV5640 runtime pm patch. It seems to be a bit complicated and
> > it
> >   is not exactly necessary for this series. Any CSI-2 camera will
> > work
> >   just fine, OV5640 just happens to be the one I tested with. I don't
> >   want it to block this series. I will submit it as a separate patch
> >   later.
> > 
> > Changes in v3:
> > - Use v4l2_get_link_freq() to calculate pixel clock.
> > - Move DMA related fields in struct ti_csi2rx_dma.
> > - Protect DMA buffer queue with a spinlock to make sure the queue
> > buffer
> >   and DMA callback don't race on it.
> > - Track the current DMA state. It might go idle because of a lack of
> >   buffers. This state can be used to restart it if needed.
> > - Do not include the current buffer in the pending queue. It is
> > slightly
> >   better modelling than leaving it at the head of the pending queue.
> > - Use the buffer as the callback argument, and add a reference to csi
> > in it.
> > - If queueing a buffer to DMA fails, the buffer gets leaked and DMA
> > gets
> >   stalled with. Instead, report the error to vb2 and queue the next
> >   buffer in the pending queue.
> > - DMA gets stalled if we run out of buffers since the callback is the
> >   only one that fires subsequent transfers and it is no longer being
> >   called. Check for that when queueing buffers and restart DMA if
> >   needed.
> > - Do not put of node until we are done using the fwnode.
> > - Set inital format to UYVY 640x480.
> > - Add compatible: contains: const: cdns,csi2rx to allow SoC specific
> >   compatible.
> > - Add more constraints for data-lanes property.
> > 
> > Changes in v2:
> > - Use phy_pm_runtime_get_sync() and phy_pm_runtime_put() before
> > making
> >   calls to set PHY mode, etc. to make sure it is ready.
> > - Use dmaengine_get_dma_device() instead of directly accessing
> >   dma->device->dev.
> > - Do not set dst_addr_width when configuring slave DMA.
> > - Move to a separate subdir and rename to j721e-csi2rx.c
> > - Convert compatible to ti,j721e-csi2rx.
> > - Move to use Media Controller centric APIs.
> > - Improve cleanup in probe when one of the steps fails.
> > - Add colorspace to formats database.
> > - Set hw_revision on media_device.
> > - Move video device initialization to probe time instead of register
> > time.
> > - Rename to ti,j721e-csi2rx.yaml
> > - Add an entry in MAINTAINERS.
> > - Add a description for the binding.
> > - Change compatible to ti,j721e-csi2rx to make it SoC specific.
> > - Remove description from dmas, reg, power-domains.
> > - Remove a limit of 2 from #address-cells and #size-cells.
> > - Fix add ^ to csi-bridge subnode regex.
> > - Make ranges mandatory.
> > - Add unit address in example.
> > - Add a reference to cdns,csi2rx in csi-bridge subnode.
> > - Expand the example to include the csi-bridge subnode as well.
> > - Re-order subject prefixes.
> > - Convert OV5640 to use runtime PM and drop Cadence CSI2RX s_power
> > patch.
> > - Drop subdev call wrappers from cdns-csi2rx.
> > - Move VPE and CAL to a separate subdir.
> > - Rename ti-csi2rx.c to j721e-csi2rx.c
> > 
> > Pratyush Yadav (13):
> >   media: cadence: csi2rx: Unregister v4l2 async notifier
> >   media: cadence: csi2rx: Cleanup media entity properly
> >   media: cadence: csi2rx: Add get_fmt and set_fmt pad ops
> >   media: cadence: csi2rx: Add external DPHY support
> >   media: cadence: csi2rx: Soft reset the streams before starting
> > capture
> >   media: cadence: csi2rx: Set the STOP bit when stopping a stream
> >   media: cadence: csi2rx: Fix stream data configuration
> >   media: cadence: csi2rx: Populate subdev devnode
> >   media: cadence: csi2rx: Add link validation
> >   media: ti: Add CSI2RX support for J721E
> >   media: dt-bindings: Make sure items in data-lanes are unique
> >   media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver
> >   media: dt-bindings: Convert Cadence CSI2RX binding to YAML
> > 
> >  .../devicetree/bindings/media/cdns,csi2rx.txt |  100 --
> >  .../bindings/media/cdns,csi2rx.yaml           |  176 +++
> >  .../bindings/media/ti,j721e-csi2rx.yaml       |  101 ++
> >  .../bindings/media/video-interfaces.yaml      |    1 +
> >  MAINTAINERS                                   |    7 +
> >  drivers/media/platform/cadence/cdns-csi2rx.c  |  273 ++++-
> >  drivers/media/platform/ti/Kconfig             |   12 +
> >  drivers/media/platform/ti/Makefile            |    1 +
> >  .../media/platform/ti/j721e-csi2rx/Makefile   |    2 +
> >  .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 1022
> > +++++++++++++++++
> >  10 files changed, 1580 insertions(+), 115 deletions(-)
> >  delete mode 100644
> > Documentation/devicetree/bindings/media/cdns,csi2rx.txt
> >  create mode 100644
> > Documentation/devicetree/bindings/media/cdns,csi2rx.yaml
> >  create mode 100644 Documentation/devicetree/bindings/media/ti,j721e-
> > csi2rx.yaml
> >  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/Makefile
> >  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/j721e-
> > csi2rx.c
> > 
> 

Thanks,
Jai

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E
  2023-03-24 18:14   ` Laurent Pinchart
@ 2023-03-28  8:19     ` Vaishnav Achath
  2023-04-04 11:43     ` Tomi Valkeinen
  1 sibling, 0 replies; 30+ messages in thread
From: Vaishnav Achath @ 2023-03-28  8:19 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, sakari.ailus, tomi.valkeinen,
	linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

Hi Laurent,

Thank you for the review,

On 24/03/23 23:44, Laurent Pinchart wrote:
> Hi Vaishnav,
> 
> Thank you for the patch.
> 
> On Tue, Mar 14, 2023 at 05:25:13PM +0530, Vaishnav Achath wrote:
>> From: Pratyush Yadav <p.yadav@ti.com>
>>
>> TI's J721E uses the Cadence CSI2RX and DPHY peripherals to facilitate
>> capture over a CSI-2 bus.
>>
>> The Cadence CSI2RX IP acts as a bridge between the TI specific parts and
>> the CSI-2 protocol parts. TI then has a wrapper on top of this bridge
>> called the SHIM layer. It takes in data from stream 0, repacks it, and
>> sends it to memory over PSI-L DMA.
>>
>> This driver acts as the "front end" to V4L2 client applications. It
>> implements the required ioctls and buffer operations, passes the
>> necessary calls on to the bridge, programs the SHIM layer, and performs
>> DMA via the dmaengine API to finally return the data to a buffer
>> supplied by the application.
>>
>> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
>> Signed-off-by: Jai Luthra <j-luthra@ti.com>
>> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
>> ---
>>
>> Changes in v7:
>> - Fix incorrect value written in SHIM_PSI_CFG0_DST_TAG
>> - Drop support for 2X8 formats.
>> - Update maintainer to Vaishnav as Pratyush moved out of TI.
>> - Address Sakari's review comments:
>> - Update MAX_HEIGHT_LINES, MAX_WIDTH_BYTES to prevent overflow.
>> - Assign dma_slave_config during declaration, drop memset().
>> - dma_release_channel() on slave_config failure.
>> - provide entity ops for the vdev entity with link_validate().
>> - mutex_destroy() on ti_csi2rx_probe failure path.
>> - Drop busy check in remove().
>> - mutex_destroy() in ti_csi2rx_remove().
>> - Address Laurent's review comments:
>> - Update entries in Makefile in alphabetical order.
>> - include headers in alphabetical order.
>> - Drop redundant CSI DT defines and use from media/mipi-csi2.h.
>> - Rename csi_df to csi_dt.
>> - Drop v4l2_colorspace from ti_csi2rx_fmt and set default in
>>   ti_csi2rx_v4l2_init()
>> - Adjust field and not return EINVAL in ti_csi2rx_try_fmt_vid_cap().
>> - inline ti_csi2rx_video_register().
>> - start DMA before starting source subdev.
>> - move buffer cleanup to separate function ti_csi2rx_cleanup_buffers()
>>   to be used in ti_csi2rx_stop_streaming() and ti_csi2rx_start_streaming()
>>   failure path.
>> - Drop VB2_USERPTR, VB2_READ and V4L2_CAP_READWRITE.
>>
>> Changes in v5:
>> - Add dependency on PHY_CADENCE_DPHY_RX instead of PHY_CADENCE_DPHY
>>   since the Rx mode DPHY now has a separate driver.
>> - Drop ti_csi2rx_validate_pipeline(). Pipeline validation should be done
>>   at media_pipeline_start().
>> - Do not assign flags.
>> - Fix error handling in ti_csi2rx_start_streaming(). Free up vb2 buffers
>>   when media_pipeline_start() fails.
>>
>> Changes in v4:
>> - Acquire the media device's graph_mutex before starting the graph walk.
>> - Call media_graph_walk_init() and media_graph_walk_cleanup() when
>>   starting and ending the graph walk respectively.
>> - Reduce max frame height and width in enum_framesizes. Currently they
>>   are set to UINT_MAX but they must be a multiple of step_width, so they
>>   need to be rounded down. Also, these values are absurdly large which
>>   causes some userspace applications like gstreamer to trip up. While it
>>   is not generally right to change the kernel for an application bug, it
>>   is not such a big deal here. This change is replacing one set of
>>   absurdly large arbitrary values with another set of smaller but still
>>   absurdly large arbitrary values. Both limits are unlikely to be hit in
>>   practice.
>>
>> Changes in v3:
>> - Move DMA related fields in struct ti_csi2rx_dma.
>> - Protect DMA buffer queue with a spinlock to make sure the queue buffer
>>   and DMA callback don't race on it.
>> - Track the current DMA state. It might go idle because of a lack of
>>   buffers. This state can be used to restart it if needed.
>> - Do not include the current buffer in the pending queue. It is slightly
>>   better modelling than leaving it at the head of the pending queue.
>> - Use the buffer as the callback argument, and add a reference to csi in it.
>> - If queueing a buffer to DMA fails, the buffer gets leaked and DMA gets
>>   stalled with. Instead, report the error to vb2 and queue the next
>>   buffer in the pending queue.
>> - DMA gets stalled if we run out of buffers since the callback is the
>>   only one that fires subsequent transfers and it is no longer being
>>   called. Check for that when queueing buffers and restart DMA if
>>   needed.
>> - Do not put of node until we are done using the fwnode.
>> - Set inital format to UYVY 640x480.
>>
>> Changes in v2:
>> - Use dmaengine_get_dma_device() instead of directly accessing
>>   dma->device->dev.
>> - Do not set dst_addr_width when configuring slave DMA.
>> - Move to a separate subdir and rename to j721e-csi2rx.c
>> - Convert compatible to ti,j721e-csi2rx.
>> - Move to use Media Controller centric APIs.
>> - Improve cleanup in probe when one of the steps fails.
>> - Add colorspace to formats database.
>> - Set hw_revision on media_device.
>> - Move video device initialization to probe time instead of register time.
>>
>>  MAINTAINERS                                   |    6 +
>>  drivers/media/platform/ti/Kconfig             |   12 +
>>  drivers/media/platform/ti/Makefile            |    1 +
>>  .../media/platform/ti/j721e-csi2rx/Makefile   |    2 +
>>  .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 1022 +++++++++++++++++
>>  5 files changed, 1043 insertions(+)
>>  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/Makefile
>>  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
>>
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index 2892858cb040..69c2b0f48073 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -20901,6 +20901,12 @@ S:	Odd Fixes
>>  F:	drivers/clk/ti/
>>  F:	include/linux/clk/ti.h
>>  
>> +TI J721E CSI2RX DRIVER
>> +M:	Vaishnav Achath <vaishnav.a@ti.com>
>> +L:	linux-media@vger.kernel.org
>> +S:	Supported
>> +F:	drivers/media/platform/ti/j721e-csi2rx/
>> +
>>  TI DAVINCI MACHINE SUPPORT
>>  M:	Bartosz Golaszewski <brgl@bgdev.pl>
>>  L:	linux-arm-kernel@lists.infradead.org (moderated for non-subscribers)
>> diff --git a/drivers/media/platform/ti/Kconfig b/drivers/media/platform/ti/Kconfig
>> index e1ab56c3be1f..42c908f6e1ae 100644
>> --- a/drivers/media/platform/ti/Kconfig
>> +++ b/drivers/media/platform/ti/Kconfig
>> @@ -63,6 +63,18 @@ config VIDEO_TI_VPE_DEBUG
>>  	help
>>  	  Enable debug messages on VPE driver.
>>  
>> +config VIDEO_TI_J721E_CSI2RX
>> +	tristate "TI J721E CSI2RX wrapper layer driver"
>> +	depends on VIDEO_DEV && VIDEO_V4L2_SUBDEV_API
>> +	depends on MEDIA_SUPPORT && MEDIA_CONTROLLER
>> +	depends on PHY_CADENCE_DPHY_RX && VIDEO_CADENCE_CSI2RX
>> +	depends on ARCH_K3 || COMPILE_TEST
>> +	select VIDEOBUF2_DMA_CONTIG
>> +	select V4L2_FWNODE
>> +	help
>> +	  Support for TI CSI2RX wrapper layer. This just enables the wrapper driver.
>> +	  The Cadence CSI2RX bridge driver needs to be enabled separately.
>> +
>>  source "drivers/media/platform/ti/am437x/Kconfig"
>>  source "drivers/media/platform/ti/davinci/Kconfig"
>>  source "drivers/media/platform/ti/omap/Kconfig"
>> diff --git a/drivers/media/platform/ti/Makefile b/drivers/media/platform/ti/Makefile
>> index 98c5fe5c40d6..8a2f74c9380e 100644
>> --- a/drivers/media/platform/ti/Makefile
>> +++ b/drivers/media/platform/ti/Makefile
>> @@ -3,5 +3,6 @@ obj-y += am437x/
>>  obj-y += cal/
>>  obj-y += vpe/
>>  obj-y += davinci/
>> +obj-y += j721e-csi2rx/
>>  obj-y += omap/
>>  obj-y += omap3isp/
>> diff --git a/drivers/media/platform/ti/j721e-csi2rx/Makefile b/drivers/media/platform/ti/j721e-csi2rx/Makefile
>> new file mode 100644
>> index 000000000000..377afc1d6280
>> --- /dev/null
>> +++ b/drivers/media/platform/ti/j721e-csi2rx/Makefile
>> @@ -0,0 +1,2 @@
>> +# SPDX-License-Identifier: GPL-2.0
>> +obj-$(CONFIG_VIDEO_TI_J721E_CSI2RX) += j721e-csi2rx.o
>> diff --git a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
>> new file mode 100644
>> index 000000000000..0c8dad049f5b
>> --- /dev/null
>> +++ b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
>> @@ -0,0 +1,1022 @@
>> +// SPDX-License-Identifier: GPL-2.0-only
>> +/*
>> + * TI CSI2 RX driver.
>> + *
>> + * Copyright (C) 2021 Texas Instruments Incorporated - https://www.ti.com/
>> + *
>> + * Author: Pratyush Yadav <p.yadav@ti.com>
>> + */
>> +
>> +#include <linux/bitfield.h>
>> +#include <linux/dmaengine.h>
>> +#include <linux/module.h>
>> +#include <linux/of_platform.h>
>> +#include <linux/platform_device.h>
>> +
>> +#include <media/mipi-csi2.h>
>> +#include <media/v4l2-device.h>
>> +#include <media/v4l2-ioctl.h>
>> +#include <media/videobuf2-dma-contig.h>
>> +
>> +#define TI_CSI2RX_MODULE_NAME		"j721e-csi2rx"
>> +
>> +#define SHIM_CNTL			0x10
>> +#define SHIM_CNTL_PIX_RST		BIT(0)
>> +
>> +#define SHIM_DMACNTX			0x20
>> +#define SHIM_DMACNTX_EN			BIT(31)
>> +#define SHIM_DMACNTX_YUV422		GENMASK(27, 26)
>> +#define SHIM_DMACNTX_FMT		GENMASK(5, 0)
>> +#define SHIM_DMACNTX_UYVY		0
>> +#define SHIM_DMACNTX_VYUY		1
>> +#define SHIM_DMACNTX_YUYV		2
>> +#define SHIM_DMACNTX_YVYU		3
>> +
>> +#define SHIM_PSI_CFG0			0x24
>> +#define SHIM_PSI_CFG0_SRC_TAG		GENMASK(15, 0)
>> +#define SHIM_PSI_CFG0_DST_TAG		GENMASK(31, 15)
>> +
>> +#define PSIL_WORD_SIZE_BYTES		16
>> +/*
>> + * There are no hard limits on the width or height. The DMA engine can handle
>> + * all sizes. The max width and height are arbitrary numbers for this driver.
>> + * Use 16K * 16K as the arbitrary limit. It is large enough that it is unlikely
>> + * the limit will be hit in practice.
>> + */
>> +#define MAX_WIDTH_BYTES			SZ_16K
>> +#define MAX_HEIGHT_LINES		SZ_16K
>> +
>> +struct ti_csi2rx_fmt {
>> +	u32				fourcc;	/* Four character code. */
>> +	u32				code;	/* Mbus code. */
>> +	u32				csi_dt;	/* CSI Data type. */
>> +	u8				bpp;	/* Bits per pixel. */
>> +};
>> +
>> +struct ti_csi2rx_buffer {
>> +	/* Common v4l2 buffer. Must be first. */
>> +	struct vb2_v4l2_buffer		vb;
>> +	struct list_head		list;
>> +	struct ti_csi2rx_dev		*csi;
>> +};
>> +
>> +enum ti_csi2rx_dma_state {
>> +	TI_CSI2RX_DMA_STOPPED,	/* Streaming not started yet. */
>> +	TI_CSI2RX_DMA_IDLE,	/* Streaming but no pending DMA operation. */
>> +	TI_CSI2RX_DMA_ACTIVE,	/* Streaming and pending DMA operation. */
>> +};
>> +
>> +struct ti_csi2rx_dma {
>> +	/* Protects all fields in this struct. */
>> +	spinlock_t			lock;
>> +	struct dma_chan			*chan;
>> +	/* Buffers queued to the driver, waiting to be processed by DMA. */
>> +	struct list_head		queue;
>> +	enum ti_csi2rx_dma_state	state;
>> +	/*
>> +	 * Current buffer being processed by DMA. NULL if no buffer is being
>> +	 * processed.
>> +	 */
>> +	struct ti_csi2rx_buffer		*curr;
>> +};
>> +
>> +struct ti_csi2rx_dev {
>> +	struct device			*dev;
>> +	void __iomem			*shim;
>> +	struct v4l2_device		v4l2_dev;
>> +	struct video_device		vdev;
>> +	struct media_device		mdev;
>> +	struct media_pipeline		pipe;
>> +	struct media_pad		pad;
>> +	struct v4l2_async_notifier	notifier;
>> +	struct v4l2_subdev		*subdev;
>> +	struct vb2_queue		vidq;
>> +	struct mutex			mutex; /* To serialize ioctls. */
>> +	struct v4l2_format		v_fmt;
>> +	struct ti_csi2rx_dma		dma;
>> +	u32				sequence;
>> +};
>> +
>> +static const struct ti_csi2rx_fmt formats[] = {
>> +	{
>> +		.fourcc			= V4L2_PIX_FMT_YUYV,
>> +		.code			= MEDIA_BUS_FMT_YUYV8_1X16,
>> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
>> +		.bpp			= 16,
>> +	}, {
>> +		.fourcc			= V4L2_PIX_FMT_UYVY,
>> +		.code			= MEDIA_BUS_FMT_UYVY8_1X16,
>> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
>> +		.bpp			= 16,
>> +	}, {
>> +		.fourcc			= V4L2_PIX_FMT_YVYU,
>> +		.code			= MEDIA_BUS_FMT_YVYU8_1X16,
>> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
>> +		.bpp			= 16,
>> +	}, {
>> +		.fourcc			= V4L2_PIX_FMT_VYUY,
>> +		.code			= MEDIA_BUS_FMT_VYUY8_1X16,
>> +		.csi_dt			= MIPI_CSI2_DT_YUV422_8B,
>> +		.bpp			= 16,
>> +	},
>> +
>> +	/* More formats can be supported but they are not listed for now. */
>> +};
>> +
>> +static const unsigned int num_formats = ARRAY_SIZE(formats);
>> +
>> +/* Forward declaration needed by ti_csi2rx_dma_callback. */
>> +static int ti_csi2rx_start_dma(struct ti_csi2rx_dev *csi,
>> +			       struct ti_csi2rx_buffer *buf);
>> +
>> +static const struct ti_csi2rx_fmt *find_format_by_pix(u32 pixelformat)
>> +{
>> +	unsigned int i;
>> +
>> +	for (i = 0; i < num_formats; i++) {
>> +		if (formats[i].fourcc == pixelformat)
>> +			return &formats[i];
>> +	}
>> +
>> +	return NULL;
>> +}
>> +
>> +static const struct ti_csi2rx_fmt *find_format_by_code(u32 code)
>> +{
>> +	unsigned int i;
>> +
>> +	for (i = 0; i < num_formats; i++) {
>> +		if (formats[i].code == code)
>> +			return &formats[i];
>> +	}
>> +
>> +	return NULL;
>> +}
>> +
>> +static void ti_csi2rx_fill_fmt(const struct ti_csi2rx_fmt *csi_fmt,
>> +			       struct v4l2_format *v4l2_fmt)
>> +{
>> +	struct v4l2_pix_format *pix = &v4l2_fmt->fmt.pix;
>> +	unsigned int pixels_in_word;
>> +	u8 bpp = csi_fmt->bpp;
>> +	u32 bpl;
>> +
>> +	/* TODO: Figure this out. */
> 
> Figure what out ? :-) Please expand this comment to explain what needs
> to be done.
> 

Will fix this in next revision.

>> +
>> +	pixels_in_word = PSIL_WORD_SIZE_BYTES * 8 / bpp;
>> +
>> +	pix->width = clamp_t(unsigned int, pix->width,
>> +			     pixels_in_word,
>> +			     MAX_WIDTH_BYTES * 8 / bpp);
>> +	pix->width = rounddown(pix->width, pixels_in_word);
>> +
>> +	pix->height = clamp_t(unsigned int, pix->height, 1, MAX_HEIGHT_LINES);
>> +
>> +	v4l2_fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
>> +	pix->pixelformat = csi_fmt->fourcc;
>> +	pix->colorspace = V4L2_COLORSPACE_SRGB;
>> +	pix->sizeimage = pix->height * pix->width * (bpp / 8);
>> +
>> +	bpl = (pix->width * ALIGN(bpp, 8)) >> 3;
>> +	pix->bytesperline = ALIGN(bpl, 16);
>> +}
>> +
>> +static int ti_csi2rx_querycap(struct file *file, void *priv,
>> +			      struct v4l2_capability *cap)
>> +{
>> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
>> +
>> +	strscpy(cap->driver, TI_CSI2RX_MODULE_NAME, sizeof(cap->driver));
>> +	strscpy(cap->card, TI_CSI2RX_MODULE_NAME, sizeof(cap->card));
>> +
>> +	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
>> +		 dev_name(csi->dev));
> 
> https://lore.kernel.org/linux-media/20220122163656.168440-1-sakari.ailus@linux.intel.com/
> has been merged in v5.19, so you don't need to set bus_info manually
> anymore.
> 

Okay, I will update here and the rest of the places for this change.

>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_enum_fmt_vid_cap(struct file *file, void *priv,
>> +				      struct v4l2_fmtdesc *f)
>> +{
>> +	if (f->index >= num_formats)
>> +		return -EINVAL;
>> +
>> +	memset(f->reserved, 0, sizeof(f->reserved));
>> +	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
>> +	f->pixelformat = formats[f->index].fourcc;
>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_g_fmt_vid_cap(struct file *file, void *prov,
>> +				   struct v4l2_format *f)
>> +{
>> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
>> +
>> +	*f = csi->v_fmt;
>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_try_fmt_vid_cap(struct file *file, void *priv,
>> +				     struct v4l2_format *f)
>> +{
>> +	const struct ti_csi2rx_fmt *fmt;
>> +
>> +	/*
>> +	 * Default to the first format if the requested pixel format code isn't
>> +	 * supported.
>> +	 */
>> +	fmt = find_format_by_pix(f->fmt.pix.pixelformat);
>> +	if (!fmt)
>> +		fmt = &formats[0];
>> +
>> +	if (f->fmt.pix.field == V4L2_FIELD_ANY)
>> +		f->fmt.pix.field = V4L2_FIELD_NONE;
>> +
>> +	if (f->fmt.pix.field != V4L2_FIELD_NONE)
>> +		/* Interlaced formats are not supported. */
>> +		f->fmt.pix.field = V4L2_FIELD_NONE;
>> +
>> +	ti_csi2rx_fill_fmt(fmt, f);
>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_s_fmt_vid_cap(struct file *file, void *priv,
>> +				   struct v4l2_format *f)
>> +{
>> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
>> +	struct vb2_queue *q = &csi->vidq;
>> +	int ret;
>> +
>> +	if (vb2_is_busy(q))
>> +		return -EBUSY;
>> +
>> +	ret = ti_csi2rx_try_fmt_vid_cap(file, priv, f);
>> +	if (ret < 0)
>> +		return ret;
>> +
>> +	csi->v_fmt = *f;
>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_enum_framesizes(struct file *file, void *fh,
>> +				     struct v4l2_frmsizeenum *fsize)
>> +{
>> +	const struct ti_csi2rx_fmt *fmt;
>> +	unsigned int pixels_in_word;
>> +	u8 bpp;
>> +
>> +	fmt = find_format_by_pix(fsize->pixel_format);
>> +	if (!fmt)
>> +		return -EINVAL;
>> +
>> +	bpp = ALIGN(fmt->bpp, 8);
>> +
>> +	/*
>> +	 * Number of pixels in one PSI-L word. The transfer happens in multiples
>> +	 * of PSI-L word sizes.
>> +	 */
>> +	pixels_in_word = PSIL_WORD_SIZE_BYTES * 8 / bpp;
>> +
>> +	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
>> +	fsize->stepwise.min_width = pixels_in_word;
>> +	fsize->stepwise.max_width = rounddown(MAX_WIDTH_BYTES * 8 / bpp,
>> +					      pixels_in_word);
>> +	fsize->stepwise.step_width = pixels_in_word;
>> +	fsize->stepwise.min_height = 1;
>> +	fsize->stepwise.max_height = MAX_HEIGHT_LINES;
>> +	fsize->stepwise.step_height = 1;
>> +
>> +	return 0;
>> +}
>> +
>> +static const struct v4l2_ioctl_ops csi_ioctl_ops = {
>> +	.vidioc_querycap      = ti_csi2rx_querycap,
>> +	.vidioc_enum_fmt_vid_cap = ti_csi2rx_enum_fmt_vid_cap,
>> +	.vidioc_try_fmt_vid_cap = ti_csi2rx_try_fmt_vid_cap,
>> +	.vidioc_g_fmt_vid_cap = ti_csi2rx_g_fmt_vid_cap,
>> +	.vidioc_s_fmt_vid_cap = ti_csi2rx_s_fmt_vid_cap,
>> +	.vidioc_enum_framesizes = ti_csi2rx_enum_framesizes,
>> +	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
>> +	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
>> +	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
>> +	.vidioc_querybuf      = vb2_ioctl_querybuf,
>> +	.vidioc_qbuf          = vb2_ioctl_qbuf,
>> +	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
>> +	.vidioc_expbuf        = vb2_ioctl_expbuf,
>> +	.vidioc_streamon      = vb2_ioctl_streamon,
>> +	.vidioc_streamoff     = vb2_ioctl_streamoff,
>> +};
>> +
>> +static const struct v4l2_file_operations csi_fops = {
>> +	.owner = THIS_MODULE,
>> +	.open = v4l2_fh_open,
>> +	.release = vb2_fop_release,
>> +	.read = vb2_fop_read,
>> +	.poll = vb2_fop_poll,
>> +	.unlocked_ioctl = video_ioctl2,
>> +	.mmap = vb2_fop_mmap,
>> +};
>> +
>> +static inline int ti_csi2rx_video_register(struct ti_csi2rx_dev *csi)
> 
> You can drop the "inline" keyword, the compiler will inline the function
> automatically if it believes it makes sense.
> 

Will fix in next revision.

>> +{
>> +	struct video_device *vdev = &csi->vdev;
>> +	int ret, src_pad;
>> +
>> +	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
>> +	if (ret)
>> +		return ret;
>> +
>> +	src_pad = media_entity_get_fwnode_pad(&csi->subdev->entity,
>> +					      csi->subdev->fwnode,
>> +					      MEDIA_PAD_FL_SOURCE);
>> +	if (src_pad < 0) {
>> +		dev_err(csi->dev, "Couldn't find source pad for subdev\n");
>> +		return src_pad;
>> +	}
>> +
>> +	ret = media_create_pad_link(&csi->subdev->entity, src_pad,
>> +				    &vdev->entity, 0,
>> +				    MEDIA_LNK_FL_IMMUTABLE |
>> +				    MEDIA_LNK_FL_ENABLED);
>> +	if (ret) {
>> +		video_unregister_device(vdev);
>> +		return ret;
>> +	}
> 
> I've sent a patch that makes v4l2_create_fwnode_links_to_pad() usable
> when the sink is not a subdev. Could you test it to see if it can
> simplify this function ?
> 

Sure, I am testing the changes now.

>> +
>> +	return 0;
>> +}
>> +
>> +static int csi_async_notifier_bound(struct v4l2_async_notifier *notifier,
>> +				    struct v4l2_subdev *subdev,
>> +				    struct v4l2_async_subdev *asd)
>> +{
>> +	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
>> +
>> +	csi->subdev = subdev;
>> +
>> +	return 0;
>> +}
>> +
>> +static int csi_async_notifier_complete(struct v4l2_async_notifier *notifier)
>> +{
>> +	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
>> +	int ret;
>> +
>> +	ret = ti_csi2rx_video_register(csi);
> 
> I commented on the previous version with
> 
>   I would inline the ti_csi2rx_video_register() function here, it's not
>   large, and it's only called in a single place.
> 
> which I assume you've understood as asking for the "inline" keyword
> above. What I meant was to take the contents of the
> ti_csi2rx_video_register() function and move them into this function.
> 

Understood now, Thanks for clarifying.

>> +	if (ret)
>> +		return ret;
>> +
>> +	return v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
>> +}
>> +
>> +static const struct v4l2_async_notifier_operations csi_async_notifier_ops = {
>> +	.bound = csi_async_notifier_bound,
>> +	.complete = csi_async_notifier_complete,
>> +};
>> +
>> +static int ti_csi2rx_init_subdev(struct ti_csi2rx_dev *csi)
>> +{
>> +	struct fwnode_handle *fwnode;
>> +	struct v4l2_async_subdev *asd;
>> +	struct device_node *node;
>> +	int ret;
>> +
>> +	node = of_get_child_by_name(csi->dev->of_node, "csi-bridge");
>> +	if (!node)
>> +		return -EINVAL;
>> +
>> +	fwnode = of_fwnode_handle(node);
>> +	if (!fwnode) {
>> +		of_node_put(node);
>> +		return -EINVAL;
>> +	}
>> +
>> +	v4l2_async_nf_init(&csi->notifier);
>> +	csi->notifier.ops = &csi_async_notifier_ops;
>> +
>> +	asd = v4l2_async_nf_add_fwnode(&csi->notifier, fwnode,
>> +				       struct v4l2_async_subdev);
>> +	of_node_put(node);
>> +	if (IS_ERR(asd)) {
>> +		v4l2_async_nf_cleanup(&csi->notifier);
>> +		return PTR_ERR(asd);
>> +	}
>> +
>> +	ret = v4l2_async_nf_register(&csi->v4l2_dev, &csi->notifier);
>> +	if (ret) {
>> +		v4l2_async_nf_cleanup(&csi->notifier);
>> +		return ret;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static void ti_csi2rx_setup_shim(struct ti_csi2rx_dev *csi)
>> +{
>> +	const struct ti_csi2rx_fmt *fmt;
>> +	unsigned int reg;
>> +
>> +	fmt = find_format_by_pix(csi->v_fmt.fmt.pix.pixelformat);
>> +	if (!fmt) {
>> +		dev_err(csi->dev, "Unknown format\n");
>> +		return;
>> +	}
>> +
>> +	/* De-assert the pixel interface reset. */
>> +	reg = SHIM_CNTL_PIX_RST;
>> +	writel(reg, csi->shim + SHIM_CNTL);
>> +
>> +	reg = SHIM_DMACNTX_EN;
>> +	reg |= FIELD_PREP(SHIM_DMACNTX_FMT, fmt->csi_dt);
>> +
>> +	/*
>> +	 * Using the values from the documentation gives incorrect ordering for
>> +	 * the luma and chroma components. In practice, the "reverse" format
>> +	 * gives the correct image. So for example, if the image is in UYVY, the
>> +	 * reverse would be YVYU.
>> +	 */
>> +	switch (fmt->fourcc) {
>> +	case V4L2_PIX_FMT_UYVY:
>> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
>> +					SHIM_DMACNTX_YVYU);
>> +		break;
>> +	case V4L2_PIX_FMT_VYUY:
>> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
>> +					SHIM_DMACNTX_YUYV);
>> +		break;
>> +	case V4L2_PIX_FMT_YUYV:
>> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
>> +					SHIM_DMACNTX_VYUY);
>> +		break;
>> +	case V4L2_PIX_FMT_YVYU:
>> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
>> +					SHIM_DMACNTX_UYVY);
>> +		break;
>> +	default:
>> +		/* Ignore if not YUV 4:2:2 */
>> +		break;
>> +	}
>> +
>> +	writel(reg, csi->shim + SHIM_DMACNTX);
>> +
>> +	reg = FIELD_PREP(SHIM_PSI_CFG0_SRC_TAG, 0) |
>> +	      FIELD_PREP(SHIM_PSI_CFG0_DST_TAG, 0);
>> +	writel(reg, csi->shim + SHIM_PSI_CFG0);
>> +}
>> +
>> +static void ti_csi2rx_dma_callback(void *param)
>> +{
>> +	struct ti_csi2rx_buffer *buf = param;
>> +	struct ti_csi2rx_dev *csi = buf->csi;
>> +	struct ti_csi2rx_dma *dma = &csi->dma;
>> +	unsigned long flags;
>> +
>> +	buf->vb.vb2_buf.timestamp = ktime_get_ns();
>> +	buf->vb.sequence = csi->sequence++;
> 
> In a reply to the previous version, Pratyush mentioned that there is a
> register in the CSI2RX core that provides the frame number. While it may
> not be very straightforward to get the information from there, it would
> still be desirable. Could you please add a TODO comment to record this ?
> 

I will add the TODO to mention this, I think Pratyush was referring to the
MONITOR registers in the CSI2RX core, the approach would work fine for single
stream, but since the monitor registers can only monitor a single virtual
channel at a time this changes cannot be scaled for multi-stream support.

> 	/*
> 	 * TODO: Derive the sequence number from the CSI2RX frame number
> 	 * hardware register.
> 	 */
> 
>> +
>> +	spin_lock_irqsave(&dma->lock, flags);
>> +
>> +	WARN_ON(dma->curr != buf);
>> +	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
>> +
>> +	/* If there are more buffers to process then start their transfer. */
>> +	dma->curr = NULL;
>> +	while (!list_empty(&dma->queue)) {
>> +		buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
>> +		list_del(&buf->list);
>> +
>> +		if (ti_csi2rx_start_dma(csi, buf)) {
>> +			dev_err(csi->dev, "Failed to queue the next buffer for DMA\n");
>> +			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
>> +		} else {
>> +			dma->curr = buf;
>> +			break;
> 
> In the same reply, Pratyush also mentioned that all pending buffers
> should be queued to the DMA engine here.
> 

I will add the change for queueing all pending buffers in the next revision,
sorry, this was missed.

>> +		}
>> +	}
>> +
>> +	if (!dma->curr)
>> +		dma->state = TI_CSI2RX_DMA_IDLE;
>> +
>> +	spin_unlock_irqrestore(&dma->lock, flags);
>> +}
>> +
>> +static int ti_csi2rx_start_dma(struct ti_csi2rx_dev *csi,
>> +			       struct ti_csi2rx_buffer *buf)
>> +{
>> +	unsigned long addr;
>> +	struct dma_async_tx_descriptor *desc;
>> +	size_t len = csi->v_fmt.fmt.pix.sizeimage;
>> +	dma_cookie_t cookie;
>> +	int ret = 0;
>> +
>> +	addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
>> +	desc = dmaengine_prep_slave_single(csi->dma.chan, addr, len,
>> +					   DMA_DEV_TO_MEM,
>> +					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
>> +	if (!desc)
>> +		return -EIO;
>> +
>> +	desc->callback = ti_csi2rx_dma_callback;
>> +	desc->callback_param = buf;
>> +
>> +	cookie = dmaengine_submit(desc);
>> +	ret = dma_submit_error(cookie);
>> +	if (ret)
>> +		return ret;
>> +
>> +	dma_async_issue_pending(csi->dma.chan);
>> +
>> +	return 0;
>> +}
>> +
>> +static void ti_csi2rx_cleanup_buffers(struct ti_csi2rx_dev *csi,
>> +				      enum vb2_buffer_state state)
>> +{
>> +	struct ti_csi2rx_dma *dma = &csi->dma;
>> +	struct ti_csi2rx_buffer *buf = NULL, *tmp;
> 
> No need to initialize buf to NULL.
> 
>> +	unsigned long flags;
>> +
>> +	spin_lock_irqsave(&dma->lock, flags);
>> +	list_for_each_entry_safe(buf, tmp, &csi->dma.queue, list) {
>> +		list_del(&buf->list);
>> +		vb2_buffer_done(&buf->vb.vb2_buf, state);
>> +	}
>> +
>> +	if (dma->curr)
>> +		vb2_buffer_done(&dma->curr->vb.vb2_buf, state);
>> +
>> +	dma->curr = NULL;
>> +	dma->state = TI_CSI2RX_DMA_STOPPED;
>> +	spin_unlock_irqrestore(&dma->lock, flags);
>> +}
>> +
>> +static int ti_csi2rx_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
>> +				 unsigned int *nplanes, unsigned int sizes[],
>> +				 struct device *alloc_devs[])
>> +{
>> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(q);
>> +	unsigned int size = csi->v_fmt.fmt.pix.sizeimage;
>> +
>> +	if (*nplanes) {
>> +		if (sizes[0] < size)
>> +			return -EINVAL;
>> +		size = sizes[0];
>> +	}
>> +
>> +	*nplanes = 1;
>> +	sizes[0] = size;
>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_buffer_prepare(struct vb2_buffer *vb)
>> +{
>> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
>> +	unsigned long size = csi->v_fmt.fmt.pix.sizeimage;
>> +
>> +	if (vb2_plane_size(vb, 0) < size) {
>> +		dev_err(csi->dev, "Data will not fit into plane\n");
>> +		return -EINVAL;
>> +	}
>> +
>> +	vb2_set_plane_payload(vb, 0, size);
>> +	return 0;
>> +}
>> +
>> +static void ti_csi2rx_buffer_queue(struct vb2_buffer *vb)
>> +{
>> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
>> +	struct ti_csi2rx_buffer *buf;
>> +	struct ti_csi2rx_dma *dma = &csi->dma;
>> +	unsigned long flags = 0;
>> +	int ret;
>> +
>> +	buf = container_of(vb, struct ti_csi2rx_buffer, vb.vb2_buf);
>> +	buf->csi = csi;
>> +
>> +	spin_lock_irqsave(&dma->lock, flags);
>> +	/*
>> +	 * Usually the DMA callback takes care of queueing the pending buffers.
>> +	 * But if DMA has stalled due to lack of buffers, restart it now.
>> +	 */
>> +	if (dma->state == TI_CSI2RX_DMA_IDLE) {
>> +		ret = ti_csi2rx_start_dma(csi, buf);
>> +		if (ret) {
>> +			dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
>> +			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
>> +			goto unlock;
>> +		}
>> +
>> +		dma->curr = buf;
>> +		dma->state = TI_CSI2RX_DMA_ACTIVE;
>> +	} else {
>> +		list_add_tail(&buf->list, &dma->queue);
>> +	}
>> +
>> +unlock:
>> +	spin_unlock_irqrestore(&dma->lock, flags);
>> +}
>> +
>> +static int ti_csi2rx_start_streaming(struct vb2_queue *vq, unsigned int count)
>> +{
>> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
>> +	struct ti_csi2rx_dma *dma = &csi->dma;
>> +	struct ti_csi2rx_buffer *buf;
>> +	unsigned long flags;
>> +	int ret = 0;
>> +
>> +	spin_lock_irqsave(&dma->lock, flags);
>> +	if (list_empty(&dma->queue))
>> +		ret = -EIO;
>> +	spin_unlock_irqrestore(&dma->lock, flags);
>> +	if (ret)
>> +		return ret;
>> +
>> +	ret = media_pipeline_start(&csi->vdev.entity.pads[0], &csi->pipe);
> 
> Please use the video_device_pipeline_start() helper for video nodes.
> Same for media_pipeline_stop(), there's now
> video_device_pipeline_stop().
> 

will update in next revision.

>> +	if (ret)
>> +		goto err;
>> +
>> +	ti_csi2rx_setup_shim(csi);
>> +
>> +	csi->sequence = 0;
>> +
>> +	spin_lock_irqsave(&dma->lock, flags);
>> +	buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
>> +	list_del(&buf->list);
>> +	dma->state = TI_CSI2RX_DMA_ACTIVE;
>> +	dma->curr = buf;
>> +
>> +	ret = ti_csi2rx_start_dma(csi, buf);
>> +	if (ret) {
>> +		dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
>> +		spin_unlock_irqrestore(&dma->lock, flags);
>> +		goto err_pipeline;
>> +	}
>> +
>> +	spin_unlock_irqrestore(&dma->lock, flags);
>> +
>> +	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 1);
>> +	if (ret)
>> +		goto err_dma;
>> +
>> +	return 0;
>> +
>> +err_dma:
>> +	dmaengine_terminate_sync(csi->dma.chan);
>> +	writel(0, csi->shim + SHIM_DMACNTX);
>> +err_pipeline:
>> +	media_pipeline_stop(&csi->vdev.entity.pads[0]);
>> +err:
>> +	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_QUEUED);
>> +	return ret;
>> +}
>> +
>> +static void ti_csi2rx_stop_streaming(struct vb2_queue *vq)
>> +{
>> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
>> +	int ret;
>> +
>> +	media_pipeline_stop(&csi->vdev.entity.pads[0]);
>> +
>> +	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 0);
>> +	if (ret)
>> +		dev_err(csi->dev, "Failed to stop subdev stream\n");
>> +
>> +	writel(0, csi->shim + SHIM_CNTL);
>> +
>> +	ret = dmaengine_terminate_sync(csi->dma.chan);
>> +	if (ret)
>> +		dev_err(csi->dev, "Failed to stop DMA: %d\n", ret);
>> +
>> +	writel(0, csi->shim + SHIM_DMACNTX);
>> +
>> +	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_ERROR);
>> +}
>> +
>> +static const struct vb2_ops csi_vb2_qops = {
>> +	.queue_setup = ti_csi2rx_queue_setup,
>> +	.buf_prepare = ti_csi2rx_buffer_prepare,
>> +	.buf_queue = ti_csi2rx_buffer_queue,
>> +	.start_streaming = ti_csi2rx_start_streaming,
>> +	.stop_streaming = ti_csi2rx_stop_streaming,
>> +	.wait_prepare = vb2_ops_wait_prepare,
>> +	.wait_finish = vb2_ops_wait_finish,
>> +};
>> +
>> +static int ti_csi2rx_init_vb2q(struct ti_csi2rx_dev *csi)
>> +{
>> +	struct vb2_queue *q = &csi->vidq;
>> +	int ret;
>> +
>> +	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
>> +	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
> 
> Support for VB2_READ is not recommended as it's very inefficient. Please
> drop it.
> 
>> +	q->drv_priv = csi;
>> +	q->buf_struct_size = sizeof(struct ti_csi2rx_buffer);
>> +	q->ops = &csi_vb2_qops;
>> +	q->mem_ops = &vb2_dma_contig_memops;
>> +	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
>> +	q->dev = dmaengine_get_dma_device(csi->dma.chan);
>> +	q->lock = &csi->mutex;
>> +	q->min_buffers_needed = 1;
>> +
>> +	ret = vb2_queue_init(q);
>> +	if (ret)
>> +		return ret;
>> +
>> +	csi->vdev.queue = q;
>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_link_validate_get_fmt(struct media_pad *pad,
>> +					   struct v4l2_subdev_format *fmt)
>> +{
>> +	if (is_media_entity_v4l2_subdev(pad->entity)) {
>> +		struct v4l2_subdev *sd =
>> +			media_entity_to_v4l2_subdev(pad->entity);
>> +
>> +		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
>> +		fmt->pad = pad->index;
>> +		return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
> 
> This will crash with any source that uses the subdev active state. You
> can't pass NULL for the state here.
> 
> How about exporting v4l2_subdev_link_validate_get_format() instead, to
> replace this function ?
> 

Yes, will update this.

>> +	}
>> +
>> +	WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
>> +	     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
>> +	     pad->entity->function, pad->entity->name);
>> +
>> +	return -EINVAL;
>> +}
>> +
>> +static int ti_csi2rx_link_validate(struct media_link *link)
>> +{
>> +	struct media_entity *entity = link->sink->entity;
>> +	struct video_device *vdev = media_entity_to_video_device(entity);
>> +	struct ti_csi2rx_dev *csi = container_of(vdev, struct ti_csi2rx_dev, vdev);
>> +	struct v4l2_pix_format *csi_fmt = &csi->v_fmt.fmt.pix;
>> +	struct v4l2_subdev_format source_fmt;
>> +	const struct ti_csi2rx_fmt *ti_fmt;
>> +	int ret;
>> +
>> +	if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
>> +		dev_info(csi->dev, "video node %s pad not connected\n",
>> +			 vdev->name);
>> +		return -ENOTCONN;
>> +	}
> 
> This can't happen, __media_pipeline_start() skips links that are not
> enabled before calling .link_validate().
> 

Got it, will drop the check.

>> +
>> +	ret = ti_csi2rx_link_validate_get_fmt(link->source, &source_fmt);
>> +	if (ret)
>> +		return ret;
>> +
>> +	if (source_fmt.format.width != csi_fmt->width) {
>> +		dev_err(csi->dev, "Width does not match (source %u, sink %u)\n",
>> +			source_fmt.format.width, csi_fmt->width);
> 
> Make it dev_dbg(), we don't want to output error messages in the kernel
> log for something that can be easily triggered by userspace. Same below.
> 

Will update these to dev_dbg().

>> +		return -EPIPE;
>> +	}
>> +
>> +	if (source_fmt.format.height != csi_fmt->height) {
>> +		dev_err(csi->dev, "Height does not match (source %u, sink %u)\n",
>> +			source_fmt.format.height, csi_fmt->height);
>> +		return -EPIPE;
>> +	}
>> +
>> +	if (source_fmt.format.field != csi_fmt->field &&
>> +	    csi_fmt->field != V4L2_FIELD_NONE) {
>> +		dev_err(csi->dev, "Field does not match (source %u, sink %u)\n",
>> +			source_fmt.format.field, csi_fmt->field);
>> +		return -EPIPE;
>> +	}
>> +
>> +	ti_fmt = find_format_by_code(source_fmt.format.code);
>> +	if (!ti_fmt) {
>> +		dev_err(csi->dev, "Media bus format 0x%x not supported\n",
>> +			source_fmt.format.code);
>> +		return -EPIPE;
>> +	}
>> +
>> +	if (source_fmt.format.code == MEDIA_BUS_FMT_YUYV8_2X8 ||
>> +	    source_fmt.format.code == MEDIA_BUS_FMT_VYUY8_2X8 ||
>> +	    source_fmt.format.code == MEDIA_BUS_FMT_YVYU8_2X8) {
> 
> Those three formats are not listed in the ti_csi2rx_fmt formats array,
> so find_format_by_code() will have returned NULL already, and this check
> can never be true.

will fix this in next revision, this was missed while dropping support for 2X8
formats from previous revision.

> 
>> +		dev_err(csi->dev,
>> +			"Only UYVY input allowed for YUV422 8-bit. Output format can be configured.\n");
>> +		return -EPIPE;
>> +	}
>> +
>> +	if (source_fmt.format.code != ti_fmt->code) {
> 
> That can never be true, as ti_fmt is looked up based on
> source_fmt.format.code. Did you mean to check if ti_fmt->fourcc ==
> csi->v_fmt.fmt.pix.pixelformat ?
> 

Yes the above the check will never be hit, I will update the check to
ti_fmt->fourcc == csi->v_fmt.fmt.pix.pixelformat in next revision.

I will address the rest of the review comments and send next revision with the
updates.

-- 
Thanks and Regards,
Vaishnav

>> +		dev_err(csi->dev,
>> +			"Cannot transform source fmt 0x%x to sink fmt 0x%x\n",
>> +			source_fmt.format.code, ti_fmt->code);
>> +		return -EPIPE;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static const struct media_entity_operations ti_csi2rx_video_entity_ops = {
>> +	.link_validate = ti_csi2rx_link_validate,
>> +};
>> +
>> +static int ti_csi2rx_init_dma(struct ti_csi2rx_dev *csi)
>> +{
>> +	struct dma_slave_config cfg = {
>> +		.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES };
> 
> 	struct dma_slave_config cfg = {
> 		.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES,
> 	};
> 
>> +	int ret;
>> +
>> +	INIT_LIST_HEAD(&csi->dma.queue);
>> +	spin_lock_init(&csi->dma.lock);
>> +
>> +	csi->dma.state = TI_CSI2RX_DMA_STOPPED;
>> +
>> +	csi->dma.chan = dma_request_chan(csi->dev, "rx0");
>> +	if (IS_ERR(csi->dma.chan))
>> +		return PTR_ERR(csi->dma.chan);
>> +
>> +	ret = dmaengine_slave_config(csi->dma.chan, &cfg);
>> +	if (ret) {
>> +		dma_release_channel(csi->dma.chan);
>> +		return ret;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static int ti_csi2rx_v4l2_init(struct ti_csi2rx_dev *csi)
>> +{
>> +	struct media_device *mdev = &csi->mdev;
>> +	struct video_device *vdev = &csi->vdev;
>> +	const struct ti_csi2rx_fmt *fmt;
>> +	struct v4l2_pix_format *pix_fmt = &csi->v_fmt.fmt.pix;
>> +	int ret;
>> +
>> +	fmt = find_format_by_pix(V4L2_PIX_FMT_UYVY);
>> +	if (!fmt)
>> +		return -EINVAL;
>> +
>> +	pix_fmt->width = 640;
>> +	pix_fmt->height = 480;
>> +	pix_fmt->field = V4L2_FIELD_NONE;
>> +
>> +	ti_csi2rx_fill_fmt(fmt, &csi->v_fmt);
>> +
>> +	mdev->dev = csi->dev;
>> +	mdev->hw_revision = 1;
>> +	strscpy(mdev->model, "TI-CSI2RX", sizeof(mdev->model));
>> +	snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
>> +		 dev_name(mdev->dev));
> 
> You can drop manual setting of bus_info here too.
> 
>> +
>> +	media_device_init(mdev);
>> +
>> +	strscpy(vdev->name, TI_CSI2RX_MODULE_NAME, sizeof(vdev->name));
>> +	vdev->v4l2_dev = &csi->v4l2_dev;
>> +	vdev->vfl_dir = VFL_DIR_RX;
>> +	vdev->fops = &csi_fops;
>> +	vdev->ioctl_ops = &csi_ioctl_ops;
>> +	vdev->release = video_device_release_empty;
>> +	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
>> +			    V4L2_CAP_IO_MC;
>> +	vdev->lock = &csi->mutex;
>> +	video_set_drvdata(vdev, csi);
>> +
>> +	csi->pad.flags = MEDIA_PAD_FL_SINK;
>> +	vdev->entity.ops = &ti_csi2rx_video_entity_ops;
>> +	ret = media_entity_pads_init(&csi->vdev.entity, 1, &csi->pad);
>> +	if (ret)
>> +		return ret;
>> +
>> +	csi->v4l2_dev.mdev = mdev;
>> +
>> +	ret = v4l2_device_register(csi->dev, &csi->v4l2_dev);
>> +	if (ret)
>> +		return ret;
>> +
>> +	ret = media_device_register(mdev);
>> +	if (ret) {
>> +		v4l2_device_unregister(&csi->v4l2_dev);
>> +		media_device_cleanup(mdev);
>> +		return ret;
>> +	}
>> +
>> +	return 0;
>> +}
>> +
>> +static void ti_csi2rx_cleanup_dma(struct ti_csi2rx_dev *csi)
>> +{
>> +	dma_release_channel(csi->dma.chan);
>> +}
>> +
>> +static void ti_csi2rx_cleanup_v4l2(struct ti_csi2rx_dev *csi)
>> +{
>> +	media_device_unregister(&csi->mdev);
>> +	v4l2_device_unregister(&csi->v4l2_dev);
>> +	media_device_cleanup(&csi->mdev);
>> +}
>> +
>> +static void ti_csi2rx_cleanup_subdev(struct ti_csi2rx_dev *csi)
>> +{
>> +	v4l2_async_nf_unregister(&csi->notifier);
>> +	v4l2_async_nf_cleanup(&csi->notifier);
>> +}
>> +
>> +static void ti_csi2rx_cleanup_vb2q(struct ti_csi2rx_dev *csi)
>> +{
>> +	vb2_queue_release(&csi->vidq);
>> +}
>> +
>> +static int ti_csi2rx_probe(struct platform_device *pdev)
>> +{
>> +	struct ti_csi2rx_dev *csi;
>> +	struct resource *res;
>> +	int ret;
>> +
>> +	csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
>> +	if (!csi)
>> +		return -ENOMEM;
>> +
>> +	csi->dev = &pdev->dev;
>> +	platform_set_drvdata(pdev, csi);
>> +
>> +	mutex_init(&csi->mutex);
>> +
>> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>> +	csi->shim = devm_ioremap_resource(&pdev->dev, res);
>> +	if (IS_ERR(csi->shim)) {
>> +		ret = PTR_ERR(csi->shim);
>> +		goto err_mutex;
>> +	}
>> +
>> +	ret = ti_csi2rx_init_dma(csi);
>> +	if (ret)
>> +		goto err_mutex;
>> +
>> +	ret = ti_csi2rx_v4l2_init(csi);
>> +	if (ret)
>> +		goto err_dma;
>> +
>> +	ret = ti_csi2rx_init_vb2q(csi);
>> +	if (ret)
>> +		goto err_v4l2;
>> +
>> +	ret = ti_csi2rx_init_subdev(csi);
>> +	if (ret)
>> +		goto err_vb2q;
>> +
>> +	ret = of_platform_populate(csi->dev->of_node, NULL, NULL, csi->dev);
>> +	if (ret) {
>> +		dev_err(csi->dev, "Failed to create children: %d\n", ret);
>> +		goto err_subdev;
>> +	}
>> +
>> +	return 0;
>> +
>> +err_subdev:
>> +	ti_csi2rx_cleanup_subdev(csi);
>> +err_vb2q:
>> +	ti_csi2rx_cleanup_vb2q(csi);
>> +err_v4l2:
>> +	ti_csi2rx_cleanup_v4l2(csi);
>> +err_dma:
>> +	ti_csi2rx_cleanup_dma(csi);
>> +err_mutex:
>> +	mutex_destroy(&csi->mutex);
>> +	return ret;
>> +}
>> +
>> +static int ti_csi2rx_remove(struct platform_device *pdev)
>> +{
>> +	struct ti_csi2rx_dev *csi = platform_get_drvdata(pdev);
>> +
>> +	video_unregister_device(&csi->vdev);
>> +
>> +	ti_csi2rx_cleanup_vb2q(csi);
>> +	ti_csi2rx_cleanup_subdev(csi);
>> +	ti_csi2rx_cleanup_v4l2(csi);
>> +	ti_csi2rx_cleanup_dma(csi);
>> +
>> +	mutex_destroy(&csi->mutex);
>> +
>> +	return 0;
>> +}
>> +
>> +static const struct of_device_id ti_csi2rx_of_match[] = {
>> +	{ .compatible = "ti,j721e-csi2rx", },
>> +	{ },
>> +};
>> +MODULE_DEVICE_TABLE(of, ti_csi2rx_of_match);
>> +
>> +static struct platform_driver ti_csi2rx_pdrv = {
>> +	.probe = ti_csi2rx_probe,
>> +	.remove = ti_csi2rx_remove,
>> +	.driver = {
>> +		.name = TI_CSI2RX_MODULE_NAME,
>> +		.of_match_table = ti_csi2rx_of_match,
>> +	},
>> +};
>> +
>> +module_platform_driver(ti_csi2rx_pdrv);
>> +
>> +MODULE_DESCRIPTION("TI J721E CSI2 RX Driver");
>> +MODULE_AUTHOR("Pratyush Yadav <p.yadav@ti.com>");
>> +MODULE_LICENSE("GPL");
> 

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 00/13] CSI2RX support on J721E
  2023-03-28  6:01   ` Jai Luthra
@ 2023-03-29 15:17     ` Martyn Welch
  0 siblings, 0 replies; 30+ messages in thread
From: Martyn Welch @ 2023-03-29 15:17 UTC (permalink / raw)
  To: Jai Luthra
  Cc: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	tomi.valkeinen, linux-kernel, bparrot, niklas.soderlund+renesas,
	devarsht, praneeth, u-kumar1, vigneshr, nm

On Tue, 2023-03-28 at 11:31 +0530, Jai Luthra wrote:
> Hi Martyn,
> 
> Thanks for the tests.
> 
> On Mar 23, 2023 at 19:36:18 +0000, Martyn Welch wrote:
> > On Tue, 2023-03-14 at 17:25 +0530, Vaishnav Achath wrote:
> > > Hi,
> > > 
> > > This series adds support for CSI2 capture on J721E. It includes
> > > some
> > > fixes to the Cadence CSI2RX driver, and adds the TI CSI2RX
> > > wrapper
> > > driver.
> > > 
> > > This is a V7 of the below V6 series,
> > > https://lore.kernel.org/all/20220121142904.4091481-1-p.yadav@ti.com/
> > > 
> > > Since Pratyush moved out of TI, I will be working on upstreaming
> > > the
> > > TI CSI2RX wrapper support.
> > > 
> > > Tested on TI's J721E EVM with LI OV5640 sensor module.
> > > https://gist.github.com/vaishnavachath/f030a257d5b6569817bc9deab1c4fa77
> > > 
> > > Also, Tested on TI AM62-SK with Pcam5C OV5640 module.
> > > https://gist.github.com/vaishnavachath/ff2605faa92f1a6ab5670426da28ccee
> > > 
> > 
> > Hi Vaishnav,
> > 
> > I assume I'm doing something wrong. I have a TI AM62-SK and the
> > Pcam5C
> > OV5640 module. I've been trying to test this with gstreamer using
> > the
> > following command:
> > 
> > gst-launch-1.0 -v v4l2src device=/dev/video0 num-buffers=10 !
> > video/x-
> > raw, width=640, height=480, format=UYVY, framerate=30/1 ! jpegenc !
> > multifilesink location=test%d.jpg
> > 
> > However I've not been able to get this working, failing with this
> > failure unless I patch in some changes I found in the TI BSP:
> > 
> > [   28.083635] cdns-mipi-dphy-rx 30110000.phy: DPHY wait for lane
> > ready
> > timeout
> > [   28.090905] cdns-csi2rx 30101000.csi-bridge: Failed to configure
> > external DPHY: -110
> 
> That is expected as currently the DPHY driver does not release the SW
> reset which all SoCs since J721E SR1.0 expect. See the patch linked
> by 
> Vaishnav below.
> 

Ah! Sorry - somehow missed that...

Yep, with those changes it's working for me, thanks!

Tested-by: Martyn Welch <martyn.welch@collabora.com>

> > 
> > The changes (and the device tree nodes I added, which might be
> > wrong...) can be found here:
> > 
> > https://gitlab.collabora.com/martyn/linux/-/commits/am625-sk-ov5640
> > 
> > Any ideas what I'm doing wrong?
> > 
> > Martyn
> > 
> > > For all newer TI platforms that TI J721E Silicon Revision 1.0,
> > > below
> > > update
> > > to DPHY RX driver is needed:
> > > https://lore.kernel.org/all/20230314073137.2153-1-vaishnav.a@ti.com/
> 
> ^
> 
> With that patch and this series on top of linux-next, I was able to
> get 
> Pcam5C capturing with SK-AM62. Although I did face issues with the 
> sensor framerate and had to revert a few recent sensor commits, not
> sure 
> why exactly yet. But here is the working branch with all the changes:
> https://github.com/jailuthra/linux/commits/b4/csi_single
> 
> Let us know if you face any other issues during your tests.
> 
> > > 
> > > Changes in v7:
> > > - For patch 10/13 ("Add CSI2RX upport for J721E"):
> > > - Fix incorrect value written in SHIM_PSI_CFG0_DST_TAG
> > > - Drop support for 2X8 formats.
> > > - Update maintainer to Vaishnav as Pratyush moved out of TI.
> > > - Address Sakari's review comments:
> > > - Update MAX_HEIGHT_LINES, MAX_WIDTH_BYTES to prevent overflow.
> > > - Assign dma_slave_config during declaration, drop memset().
> > > - dma_release_channel() on slave_config failure.
> > > - provide entity ops for the vdev entity with link_validate().
> > > - mutex_destroy() on ti_csi2rx_probe failure path.
> > > - Drop busy check in remove().
> > > - mutex_destroy() in ti_csi2rx_remove().
> > > - Address Laurent's review comments:
> > > - Update entries in Makefile in alphabetical order.
> > > - include headers in alphabetical order.
> > > - Drop redundant CSI DT defines and use from media/mipi-csi2.h.
> > > - Rename csi_df to csi_dt.
> > > - Drop v4l2_colorspace from ti_csi2rx_fmt and set default in
> > >   ti_csi2rx_v4l2_init()
> > > - Adjust field and not return EINVAL in
> > > ti_csi2rx_try_fmt_vid_cap().
> > > - inline ti_csi2rx_video_register().
> > > - start DMA before starting source subdev.
> > > - move buffer cleanup to separate function
> > > ti_csi2rx_cleanup_buffers()
> > >   to be used in ti_csi2rx_stop_streaming() and
> > > ti_csi2rx_start_streaming()
> > >   failure path.
> > > - Drop VB2_USERPTR, VB2_READ and V4L2_CAP_READWRITE.
> > > - For patch 4/13 ("media: cadence: csi2rx: Add external DPHY
> > > support"):
> > > - Fix multiplier and divider in v4l2_get_link_freq() which caused
> > >   failures during streaming.
> > > 
> > > Changes in v6:
> > > - Move the lock around the dereference for framefmt in
> > >   csi2rx_{get,set}_fmt() instead of when we get the pointer.
> > > - Do not return an error when an unsupported format is set.
> > > Instead
> > >   adjust the code to the first format in the list.
> > > - Drop variable bpp and use fmt->bpp directly.
> > > - Drop variable got_pm. Call phy_pm_runtime_put() unconditionally
> > > since
> > >   it will just return an error if runtime PM is not enabled.
> > > - Drop transcoding from the commit message.
> > > - Make csi2rx_media_ops const.
> > > 
> > > Changes in v5:
> > > - Cleanup notifier in csi2rx_parse_dt() after the call to
> > >   v4l2_async_nf_add_fwnode_remote().
> > > - Use YUV 1X16 formats instead of 2X8.
> > > - Only error out when phy_pm_runtime_get_sync() returns a
> > > negative
> > >   value. A positive value can be returned if the phy was already
> > >   resumed.
> > > - Do not query the source subdev for format. Use the newly added
> > >   internal format instead.
> > > - Make i unsigned.
> > > - Change %d to %u
> > > - Add dependency on PHY_CADENCE_DPHY_RX instead of
> > > PHY_CADENCE_DPHY
> > >   since the Rx mode DPHY now has a separate driver.
> > > - Drop ti_csi2rx_validate_pipeline(). Pipeline validation should
> > > be
> > > done
> > >   at media_pipeline_start().
> > > - Do not assign flags.
> > > - Fix error handling in ti_csi2rx_start_streaming(). Free up vb2
> > > buffers
> > >   when media_pipeline_start() fails.
> > > - Move clock description in comments under the clocks property.
> > > - Make ports required.
> > > - Add link validation to cdns-csi2rx driver.
> > > 
> > > Changes in v4:
> > > - Drop the call to set PHY submode. It is now being done via
> > > compatible
> > >   on the DPHY side.
> > > - Acquire the media device's graph_mutex before starting the
> > > graph
> > > walk.
> > > - Call media_graph_walk_init() and media_graph_walk_cleanup()
> > > when
> > >   starting and ending the graph walk respectively.
> > > - Reduce max frame height and width in enum_framesizes. Currently
> > > they
> > >   are set to UINT_MAX but they must be a multiple of step_width,
> > > so
> > > they
> > >   need to be rounded down. Also, these values are absurdly large
> > > which
> > >   causes some userspace applications like gstreamer to trip up.
> > > While
> > > it
> > >   is not generally right to change the kernel for an application
> > > bug,
> > > it
> > >   is not such a big deal here. This change is replacing one set
> > > of
> > >   absurdly large arbitrary values with another set of smaller but
> > > still
> > >   absurdly large arbitrary values. Both limits are unlikely to be
> > > hit
> > > in
> > >   practice.
> > > - Add power-domains property.
> > > - Drop maxItems from clock-names.
> > > - Drop the type for data-lanes.
> > > - Drop uniqueItems from data-lanes. Move it to video-
> > > interfaces.yaml
> > >   instead.
> > > - Drop OV5640 runtime pm patch. It seems to be a bit complicated
> > > and
> > > it
> > >   is not exactly necessary for this series. Any CSI-2 camera will
> > > work
> > >   just fine, OV5640 just happens to be the one I tested with. I
> > > don't
> > >   want it to block this series. I will submit it as a separate
> > > patch
> > >   later.
> > > 
> > > Changes in v3:
> > > - Use v4l2_get_link_freq() to calculate pixel clock.
> > > - Move DMA related fields in struct ti_csi2rx_dma.
> > > - Protect DMA buffer queue with a spinlock to make sure the queue
> > > buffer
> > >   and DMA callback don't race on it.
> > > - Track the current DMA state. It might go idle because of a lack
> > > of
> > >   buffers. This state can be used to restart it if needed.
> > > - Do not include the current buffer in the pending queue. It is
> > > slightly
> > >   better modelling than leaving it at the head of the pending
> > > queue.
> > > - Use the buffer as the callback argument, and add a reference to
> > > csi
> > > in it.
> > > - If queueing a buffer to DMA fails, the buffer gets leaked and
> > > DMA
> > > gets
> > >   stalled with. Instead, report the error to vb2 and queue the
> > > next
> > >   buffer in the pending queue.
> > > - DMA gets stalled if we run out of buffers since the callback is
> > > the
> > >   only one that fires subsequent transfers and it is no longer
> > > being
> > >   called. Check for that when queueing buffers and restart DMA if
> > >   needed.
> > > - Do not put of node until we are done using the fwnode.
> > > - Set inital format to UYVY 640x480.
> > > - Add compatible: contains: const: cdns,csi2rx to allow SoC
> > > specific
> > >   compatible.
> > > - Add more constraints for data-lanes property.
> > > 
> > > Changes in v2:
> > > - Use phy_pm_runtime_get_sync() and phy_pm_runtime_put() before
> > > making
> > >   calls to set PHY mode, etc. to make sure it is ready.
> > > - Use dmaengine_get_dma_device() instead of directly accessing
> > >   dma->device->dev.
> > > - Do not set dst_addr_width when configuring slave DMA.
> > > - Move to a separate subdir and rename to j721e-csi2rx.c
> > > - Convert compatible to ti,j721e-csi2rx.
> > > - Move to use Media Controller centric APIs.
> > > - Improve cleanup in probe when one of the steps fails.
> > > - Add colorspace to formats database.
> > > - Set hw_revision on media_device.
> > > - Move video device initialization to probe time instead of
> > > register
> > > time.
> > > - Rename to ti,j721e-csi2rx.yaml
> > > - Add an entry in MAINTAINERS.
> > > - Add a description for the binding.
> > > - Change compatible to ti,j721e-csi2rx to make it SoC specific.
> > > - Remove description from dmas, reg, power-domains.
> > > - Remove a limit of 2 from #address-cells and #size-cells.
> > > - Fix add ^ to csi-bridge subnode regex.
> > > - Make ranges mandatory.
> > > - Add unit address in example.
> > > - Add a reference to cdns,csi2rx in csi-bridge subnode.
> > > - Expand the example to include the csi-bridge subnode as well.
> > > - Re-order subject prefixes.
> > > - Convert OV5640 to use runtime PM and drop Cadence CSI2RX
> > > s_power
> > > patch.
> > > - Drop subdev call wrappers from cdns-csi2rx.
> > > - Move VPE and CAL to a separate subdir.
> > > - Rename ti-csi2rx.c to j721e-csi2rx.c
> > > 
> > > Pratyush Yadav (13):
> > >   media: cadence: csi2rx: Unregister v4l2 async notifier
> > >   media: cadence: csi2rx: Cleanup media entity properly
> > >   media: cadence: csi2rx: Add get_fmt and set_fmt pad ops
> > >   media: cadence: csi2rx: Add external DPHY support
> > >   media: cadence: csi2rx: Soft reset the streams before starting
> > > capture
> > >   media: cadence: csi2rx: Set the STOP bit when stopping a stream
> > >   media: cadence: csi2rx: Fix stream data configuration
> > >   media: cadence: csi2rx: Populate subdev devnode
> > >   media: cadence: csi2rx: Add link validation
> > >   media: ti: Add CSI2RX support for J721E
> > >   media: dt-bindings: Make sure items in data-lanes are unique
> > >   media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver
> > >   media: dt-bindings: Convert Cadence CSI2RX binding to YAML
> > > 
> > >  .../devicetree/bindings/media/cdns,csi2rx.txt |  100 --
> > >  .../bindings/media/cdns,csi2rx.yaml           |  176 +++
> > >  .../bindings/media/ti,j721e-csi2rx.yaml       |  101 ++
> > >  .../bindings/media/video-interfaces.yaml      |    1 +
> > >  MAINTAINERS                                   |    7 +
> > >  drivers/media/platform/cadence/cdns-csi2rx.c  |  273 ++++-
> > >  drivers/media/platform/ti/Kconfig             |   12 +
> > >  drivers/media/platform/ti/Makefile            |    1 +
> > >  .../media/platform/ti/j721e-csi2rx/Makefile   |    2 +
> > >  .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 1022
> > > +++++++++++++++++
> > >  10 files changed, 1580 insertions(+), 115 deletions(-)
> > >  delete mode 100644
> > > Documentation/devicetree/bindings/media/cdns,csi2rx.txt
> > >  create mode 100644
> > > Documentation/devicetree/bindings/media/cdns,csi2rx.yaml
> > >  create mode 100644
> > > Documentation/devicetree/bindings/media/ti,j721e-
> > > csi2rx.yaml
> > >  create mode 100644 drivers/media/platform/ti/j721e-
> > > csi2rx/Makefile
> > >  create mode 100644 drivers/media/platform/ti/j721e-csi2rx/j721e-
> > > csi2rx.c
> > > 
> > 
> 
> Thanks,
> Jai


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 04/13] media: cadence: csi2rx: Add external DPHY support
  2023-03-14 11:55 ` [PATCH v7 04/13] media: cadence: csi2rx: Add external DPHY support Vaishnav Achath
@ 2023-03-31 13:19   ` Sakari Ailus
  0 siblings, 0 replies; 30+ messages in thread
From: Sakari Ailus @ 2023-03-31 13:19 UTC (permalink / raw)
  To: Vaishnav Achath
  Cc: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, tomi.valkeinen,
	linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

Hi Vaishnav,

On Tue, Mar 14, 2023 at 05:25:07PM +0530, Vaishnav Achath wrote:
> From: Pratyush Yadav <p.yadav@ti.com>
> 
> Some platforms like TI's J721E can have the CSI2RX paired with an
> external DPHY. Add support to enable and configure the DPHY using the
> generic PHY framework.
> 
> Signed-off-by: Pratyush Yadav <p.yadav@ti.com>
> Signed-off-by: Vaishnav Achath <vaishnav.a@ti.com>
> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
> 
> Changes in v7:
> - Fix multiplier and divider in v4l2_get_link_freq() which caused
>   failures during streaming.
> 
> Changes in v6:
> - Drop variable bpp and use fmt->bpp directly.
> - Drop variable got_pm. Call phy_pm_runtime_put() unconditionally since
>   it will just return an error if runtime PM is not enabled.
> - Add Laurent's R-by.
> 
> Changes in v5:
> - Only error out when phy_pm_runtime_get_sync() returns a negative
>   value. A positive value can be returned if the phy was already
>   resumed.
> - Do not query the source subdev for format. Use the newly added
>   internal format instead.
> 
> Changes in v4:
> - Drop the call to set PHY submode. It is now being done via compatible
>   on the DPHY side.
> 
> Changes in v3:
> - Use v4l2_get_link_freq() to calculate pixel clock.
> 
> Changes in v2:
> - Use phy_pm_runtime_get_sync() and phy_pm_runtime_put() before making
>   calls to set PHY mode, etc. to make sure it is ready.
> 
>  drivers/media/platform/cadence/cdns-csi2rx.c | 90 +++++++++++++++++---
>  1 file changed, 80 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/media/platform/cadence/cdns-csi2rx.c b/drivers/media/platform/cadence/cdns-csi2rx.c
> index ae3ebdb3890d..6933626adfc8 100644
> --- a/drivers/media/platform/cadence/cdns-csi2rx.c
> +++ b/drivers/media/platform/cadence/cdns-csi2rx.c
> @@ -30,6 +30,12 @@
>  #define CSI2RX_STATIC_CFG_DLANE_MAP(llane, plane)	((plane) << (16 + (llane) * 4))
>  #define CSI2RX_STATIC_CFG_LANES_MASK			GENMASK(11, 8)
>  
> +#define CSI2RX_DPHY_LANE_CTRL_REG		0x40
> +#define CSI2RX_DPHY_CL_RST			BIT(16)
> +#define CSI2RX_DPHY_DL_RST(i)			BIT((i) + 12)
> +#define CSI2RX_DPHY_CL_EN			BIT(4)
> +#define CSI2RX_DPHY_DL_EN(i)			BIT(i)
> +
>  #define CSI2RX_STREAM_BASE(n)		(((n) + 1) * 0x100)
>  
>  #define CSI2RX_STREAM_CTRL_REG(n)		(CSI2RX_STREAM_BASE(n) + 0x000)
> @@ -137,6 +143,49 @@ static void csi2rx_reset(struct csi2rx_priv *csi2rx)
>  	writel(0, csi2rx->base + CSI2RX_SOFT_RESET_REG);
>  }
>  
> +static int csi2rx_configure_external_dphy(struct csi2rx_priv *csi2rx)
> +{
> +	union phy_configure_opts opts = { };
> +	struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
> +	const struct csi2rx_fmt *fmt;
> +	s64 pixel_clock;
> +	int ret;
> +
> +	fmt = csi2rx_get_fmt_by_code(csi2rx->fmt.code);
> +
> +	/*
> +	 * Do not divide by the number of lanes here. That will be done by
> +	 * phy_mipi_dphy_get_default_config().
> +	 */
> +	pixel_clock = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
> +					 fmt->bpp, 2 * csi2rx->num_lanes);
> +	if (pixel_clock < 0)
> +		return pixel_clock;
> +
> +	ret = phy_mipi_dphy_get_default_config(pixel_clock, 1, 1, cfg);
> +	if (ret)
> +		return ret;
> +
> +	ret = phy_pm_runtime_get_sync(csi2rx->dphy);
> +	if (ret < 0 && ret != -ENOTSUPP)
> +		return ret;
> +
> +	ret = phy_power_on(csi2rx->dphy);
> +	if (ret)
> +		goto out;

You shouldn't need both phy_pm_runtime_get_sync() and phy_power_on(). It
would seem like that phy_power_on() is what drivers are intended to use.

> +
> +	ret = phy_configure(csi2rx->dphy, &opts);
> +	if (ret) {
> +		/* Can't do anything if it fails. Ignore the return value. */
> +		phy_power_off(csi2rx->dphy);
> +		goto out;
> +	}
> +
> +out:
> +	phy_pm_runtime_put(csi2rx->dphy);
> +	return ret;
> +}
> +
>  static int csi2rx_start(struct csi2rx_priv *csi2rx)
>  {
>  	unsigned int i;
> @@ -175,6 +224,17 @@ static int csi2rx_start(struct csi2rx_priv *csi2rx)
>  	if (ret)
>  		goto err_disable_pclk;
>  
> +	/* Enable DPHY clk and data lanes. */
> +	if (csi2rx->dphy) {
> +		reg = CSI2RX_DPHY_CL_EN | CSI2RX_DPHY_CL_RST;
> +		for (i = 0; i < csi2rx->num_lanes; i++) {
> +			reg |= CSI2RX_DPHY_DL_EN(csi2rx->lanes[i] - 1);
> +			reg |= CSI2RX_DPHY_DL_RST(csi2rx->lanes[i] - 1);
> +		}
> +
> +		writel(reg, csi2rx->base + CSI2RX_DPHY_LANE_CTRL_REG);
> +	}
> +
>  	/*
>  	 * Create a static mapping between the CSI virtual channels
>  	 * and the output stream.
> @@ -205,10 +265,21 @@ static int csi2rx_start(struct csi2rx_priv *csi2rx)
>  	if (ret)
>  		goto err_disable_pixclk;
>  
> +	if (csi2rx->dphy) {
> +		ret = csi2rx_configure_external_dphy(csi2rx);
> +		if (ret) {
> +			dev_err(csi2rx->dev,
> +				"Failed to configure external DPHY: %d\n", ret);
> +			goto err_disable_sysclk;
> +		}
> +	}
> +
>  	clk_disable_unprepare(csi2rx->p_clk);
>  
>  	return 0;
>  
> +err_disable_sysclk:
> +	clk_disable_unprepare(csi2rx->sys_clk);
>  err_disable_pixclk:
>  	for (; i > 0; i--)
>  		clk_disable_unprepare(csi2rx->pixel_clk[i - 1]);
> @@ -236,6 +307,13 @@ static void csi2rx_stop(struct csi2rx_priv *csi2rx)
>  
>  	if (v4l2_subdev_call(csi2rx->source_subdev, video, s_stream, false))
>  		dev_warn(csi2rx->dev, "Couldn't disable our subdev\n");
> +
> +	if (csi2rx->dphy) {
> +		writel(0, csi2rx->base + CSI2RX_DPHY_LANE_CTRL_REG);
> +
> +		if (phy_power_off(csi2rx->dphy))
> +			dev_warn(csi2rx->dev, "Couldn't power off DPHY\n");
> +	}
>  }
>  
>  static int csi2rx_s_stream(struct v4l2_subdev *subdev, int enable)
> @@ -432,15 +510,6 @@ static int csi2rx_get_resources(struct csi2rx_priv *csi2rx,
>  		return PTR_ERR(csi2rx->dphy);
>  	}
>  
> -	/*
> -	 * FIXME: Once we'll have external D-PHY support, the check
> -	 * will need to be removed.
> -	 */
> -	if (csi2rx->dphy) {
> -		dev_err(&pdev->dev, "External D-PHY not supported yet\n");
> -		return -EINVAL;
> -	}
> -
>  	ret = clk_prepare_enable(csi2rx->p_clk);
>  	if (ret) {
>  		dev_err(&pdev->dev, "Couldn't prepare and enable P clock\n");
> @@ -470,7 +539,7 @@ static int csi2rx_get_resources(struct csi2rx_priv *csi2rx,
>  	 * FIXME: Once we'll have internal D-PHY support, the check
>  	 * will need to be removed.
>  	 */
> -	if (csi2rx->has_internal_dphy) {
> +	if (!csi2rx->dphy && csi2rx->has_internal_dphy) {
>  		dev_err(&pdev->dev, "Internal D-PHY not supported yet\n");
>  		return -EINVAL;
>  	}
> @@ -595,6 +664,7 @@ static int csi2rx_probe(struct platform_device *pdev)
>  	dev_info(&pdev->dev,
>  		 "Probed CSI2RX with %u/%u lanes, %u streams, %s D-PHY\n",
>  		 csi2rx->num_lanes, csi2rx->max_lanes, csi2rx->max_streams,
> +		 csi2rx->dphy ? "external" :
>  		 csi2rx->has_internal_dphy ? "internal" : "no");
>  
>  	return 0;

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E
  2023-03-14 11:55 ` [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E Vaishnav Achath
  2023-03-20 12:51   ` Jai Luthra
  2023-03-24 18:14   ` Laurent Pinchart
@ 2023-03-31 13:27   ` Sakari Ailus
  2 siblings, 0 replies; 30+ messages in thread
From: Sakari Ailus @ 2023-03-31 13:27 UTC (permalink / raw)
  To: Vaishnav Achath
  Cc: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, laurent.pinchart, tomi.valkeinen,
	linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

Hi Vaishnav,

On Tue, Mar 14, 2023 at 05:25:13PM +0530, Vaishnav Achath wrote:
...
> +static int ti_csi2rx_querycap(struct file *file, void *priv,
> +			      struct v4l2_capability *cap)
> +{
> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
> +
> +	strscpy(cap->driver, TI_CSI2RX_MODULE_NAME, sizeof(cap->driver));
> +	strscpy(cap->card, TI_CSI2RX_MODULE_NAME, sizeof(cap->card));
> +
> +	snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
> +		 dev_name(csi->dev));

You don't need to assign bus_info for platform devices anymore, this is
done in v4l_querycap().

> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_enum_fmt_vid_cap(struct file *file, void *priv,
> +				      struct v4l2_fmtdesc *f)
> +{
> +	if (f->index >= num_formats)
> +		return -EINVAL;
> +
> +	memset(f->reserved, 0, sizeof(f->reserved));

This is done by the framework already.

> +	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;

Not needed.

> +	f->pixelformat = formats[f->index].fourcc;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_g_fmt_vid_cap(struct file *file, void *prov,
> +				   struct v4l2_format *f)
> +{
> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
> +
> +	*f = csi->v_fmt;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_try_fmt_vid_cap(struct file *file, void *priv,
> +				     struct v4l2_format *f)
> +{
> +	const struct ti_csi2rx_fmt *fmt;
> +
> +	/*
> +	 * Default to the first format if the requested pixel format code isn't
> +	 * supported.
> +	 */
> +	fmt = find_format_by_pix(f->fmt.pix.pixelformat);
> +	if (!fmt)
> +		fmt = &formats[0];
> +
> +	if (f->fmt.pix.field == V4L2_FIELD_ANY)
> +		f->fmt.pix.field = V4L2_FIELD_NONE;
> +
> +	if (f->fmt.pix.field != V4L2_FIELD_NONE)
> +		/* Interlaced formats are not supported. */
> +		f->fmt.pix.field = V4L2_FIELD_NONE;
> +
> +	ti_csi2rx_fill_fmt(fmt, f);
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_s_fmt_vid_cap(struct file *file, void *priv,
> +				   struct v4l2_format *f)
> +{
> +	struct ti_csi2rx_dev *csi = video_drvdata(file);
> +	struct vb2_queue *q = &csi->vidq;
> +	int ret;
> +
> +	if (vb2_is_busy(q))
> +		return -EBUSY;
> +
> +	ret = ti_csi2rx_try_fmt_vid_cap(file, priv, f);
> +	if (ret < 0)
> +		return ret;
> +
> +	csi->v_fmt = *f;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_enum_framesizes(struct file *file, void *fh,
> +				     struct v4l2_frmsizeenum *fsize)
> +{
> +	const struct ti_csi2rx_fmt *fmt;
> +	unsigned int pixels_in_word;
> +	u8 bpp;
> +
> +	fmt = find_format_by_pix(fsize->pixel_format);
> +	if (!fmt)
> +		return -EINVAL;
> +
> +	bpp = ALIGN(fmt->bpp, 8);
> +
> +	/*
> +	 * Number of pixels in one PSI-L word. The transfer happens in multiples
> +	 * of PSI-L word sizes.
> +	 */
> +	pixels_in_word = PSIL_WORD_SIZE_BYTES * 8 / bpp;
> +
> +	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
> +	fsize->stepwise.min_width = pixels_in_word;
> +	fsize->stepwise.max_width = rounddown(MAX_WIDTH_BYTES * 8 / bpp,
> +					      pixels_in_word);
> +	fsize->stepwise.step_width = pixels_in_word;
> +	fsize->stepwise.min_height = 1;
> +	fsize->stepwise.max_height = MAX_HEIGHT_LINES;
> +	fsize->stepwise.step_height = 1;
> +
> +	return 0;
> +}
> +
> +static const struct v4l2_ioctl_ops csi_ioctl_ops = {
> +	.vidioc_querycap      = ti_csi2rx_querycap,
> +	.vidioc_enum_fmt_vid_cap = ti_csi2rx_enum_fmt_vid_cap,
> +	.vidioc_try_fmt_vid_cap = ti_csi2rx_try_fmt_vid_cap,
> +	.vidioc_g_fmt_vid_cap = ti_csi2rx_g_fmt_vid_cap,
> +	.vidioc_s_fmt_vid_cap = ti_csi2rx_s_fmt_vid_cap,
> +	.vidioc_enum_framesizes = ti_csi2rx_enum_framesizes,
> +	.vidioc_reqbufs       = vb2_ioctl_reqbufs,
> +	.vidioc_create_bufs   = vb2_ioctl_create_bufs,
> +	.vidioc_prepare_buf   = vb2_ioctl_prepare_buf,
> +	.vidioc_querybuf      = vb2_ioctl_querybuf,
> +	.vidioc_qbuf          = vb2_ioctl_qbuf,
> +	.vidioc_dqbuf         = vb2_ioctl_dqbuf,
> +	.vidioc_expbuf        = vb2_ioctl_expbuf,
> +	.vidioc_streamon      = vb2_ioctl_streamon,
> +	.vidioc_streamoff     = vb2_ioctl_streamoff,
> +};
> +
> +static const struct v4l2_file_operations csi_fops = {
> +	.owner = THIS_MODULE,
> +	.open = v4l2_fh_open,
> +	.release = vb2_fop_release,
> +	.read = vb2_fop_read,
> +	.poll = vb2_fop_poll,
> +	.unlocked_ioctl = video_ioctl2,
> +	.mmap = vb2_fop_mmap,
> +};
> +
> +static inline int ti_csi2rx_video_register(struct ti_csi2rx_dev *csi)
> +{
> +	struct video_device *vdev = &csi->vdev;
> +	int ret, src_pad;
> +
> +	ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
> +	if (ret)
> +		return ret;
> +
> +	src_pad = media_entity_get_fwnode_pad(&csi->subdev->entity,
> +					      csi->subdev->fwnode,
> +					      MEDIA_PAD_FL_SOURCE);
> +	if (src_pad < 0) {
> +		dev_err(csi->dev, "Couldn't find source pad for subdev\n");
> +		return src_pad;
> +	}
> +
> +	ret = media_create_pad_link(&csi->subdev->entity, src_pad,
> +				    &vdev->entity, 0,
> +				    MEDIA_LNK_FL_IMMUTABLE |
> +				    MEDIA_LNK_FL_ENABLED);
> +	if (ret) {
> +		video_unregister_device(vdev);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static int csi_async_notifier_bound(struct v4l2_async_notifier *notifier,
> +				    struct v4l2_subdev *subdev,
> +				    struct v4l2_async_subdev *asd)
> +{
> +	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
> +
> +	csi->subdev = subdev;
> +
> +	return 0;
> +}
> +
> +static int csi_async_notifier_complete(struct v4l2_async_notifier *notifier)
> +{
> +	struct ti_csi2rx_dev *csi = dev_get_drvdata(notifier->v4l2_dev->dev);
> +	int ret;
> +
> +	ret = ti_csi2rx_video_register(csi);
> +	if (ret)
> +		return ret;
> +
> +	return v4l2_device_register_subdev_nodes(&csi->v4l2_dev);
> +}
> +
> +static const struct v4l2_async_notifier_operations csi_async_notifier_ops = {
> +	.bound = csi_async_notifier_bound,
> +	.complete = csi_async_notifier_complete,
> +};
> +
> +static int ti_csi2rx_init_subdev(struct ti_csi2rx_dev *csi)
> +{
> +	struct fwnode_handle *fwnode;
> +	struct v4l2_async_subdev *asd;
> +	struct device_node *node;
> +	int ret;
> +
> +	node = of_get_child_by_name(csi->dev->of_node, "csi-bridge");
> +	if (!node)
> +		return -EINVAL;
> +
> +	fwnode = of_fwnode_handle(node);
> +	if (!fwnode) {
> +		of_node_put(node);
> +		return -EINVAL;
> +	}
> +
> +	v4l2_async_nf_init(&csi->notifier);
> +	csi->notifier.ops = &csi_async_notifier_ops;
> +
> +	asd = v4l2_async_nf_add_fwnode(&csi->notifier, fwnode,
> +				       struct v4l2_async_subdev);
> +	of_node_put(node);
> +	if (IS_ERR(asd)) {
> +		v4l2_async_nf_cleanup(&csi->notifier);
> +		return PTR_ERR(asd);
> +	}
> +
> +	ret = v4l2_async_nf_register(&csi->v4l2_dev, &csi->notifier);
> +	if (ret) {
> +		v4l2_async_nf_cleanup(&csi->notifier);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static void ti_csi2rx_setup_shim(struct ti_csi2rx_dev *csi)
> +{
> +	const struct ti_csi2rx_fmt *fmt;
> +	unsigned int reg;
> +
> +	fmt = find_format_by_pix(csi->v_fmt.fmt.pix.pixelformat);
> +	if (!fmt) {
> +		dev_err(csi->dev, "Unknown format\n");
> +		return;
> +	}
> +
> +	/* De-assert the pixel interface reset. */
> +	reg = SHIM_CNTL_PIX_RST;
> +	writel(reg, csi->shim + SHIM_CNTL);
> +
> +	reg = SHIM_DMACNTX_EN;
> +	reg |= FIELD_PREP(SHIM_DMACNTX_FMT, fmt->csi_dt);
> +
> +	/*
> +	 * Using the values from the documentation gives incorrect ordering for
> +	 * the luma and chroma components. In practice, the "reverse" format
> +	 * gives the correct image. So for example, if the image is in UYVY, the
> +	 * reverse would be YVYU.
> +	 */
> +	switch (fmt->fourcc) {
> +	case V4L2_PIX_FMT_UYVY:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_YVYU);
> +		break;
> +	case V4L2_PIX_FMT_VYUY:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_YUYV);
> +		break;
> +	case V4L2_PIX_FMT_YUYV:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_VYUY);
> +		break;
> +	case V4L2_PIX_FMT_YVYU:
> +		reg |= FIELD_PREP(SHIM_DMACNTX_YUV422,
> +					SHIM_DMACNTX_UYVY);
> +		break;
> +	default:
> +		/* Ignore if not YUV 4:2:2 */
> +		break;
> +	}
> +
> +	writel(reg, csi->shim + SHIM_DMACNTX);
> +
> +	reg = FIELD_PREP(SHIM_PSI_CFG0_SRC_TAG, 0) |
> +	      FIELD_PREP(SHIM_PSI_CFG0_DST_TAG, 0);
> +	writel(reg, csi->shim + SHIM_PSI_CFG0);
> +}
> +
> +static void ti_csi2rx_dma_callback(void *param)
> +{
> +	struct ti_csi2rx_buffer *buf = param;
> +	struct ti_csi2rx_dev *csi = buf->csi;
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	unsigned long flags;
> +
> +	buf->vb.vb2_buf.timestamp = ktime_get_ns();
> +	buf->vb.sequence = csi->sequence++;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +
> +	WARN_ON(dma->curr != buf);
> +	vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
> +
> +	/* If there are more buffers to process then start their transfer. */
> +	dma->curr = NULL;
> +	while (!list_empty(&dma->queue)) {
> +		buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
> +		list_del(&buf->list);
> +
> +		if (ti_csi2rx_start_dma(csi, buf)) {
> +			dev_err(csi->dev, "Failed to queue the next buffer for DMA\n");
> +			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
> +		} else {
> +			dma->curr = buf;
> +			break;
> +		}
> +	}
> +
> +	if (!dma->curr)
> +		dma->state = TI_CSI2RX_DMA_IDLE;
> +
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +}
> +
> +static int ti_csi2rx_start_dma(struct ti_csi2rx_dev *csi,
> +			       struct ti_csi2rx_buffer *buf)
> +{
> +	unsigned long addr;
> +	struct dma_async_tx_descriptor *desc;
> +	size_t len = csi->v_fmt.fmt.pix.sizeimage;
> +	dma_cookie_t cookie;
> +	int ret = 0;
> +
> +	addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
> +	desc = dmaengine_prep_slave_single(csi->dma.chan, addr, len,
> +					   DMA_DEV_TO_MEM,
> +					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
> +	if (!desc)
> +		return -EIO;
> +
> +	desc->callback = ti_csi2rx_dma_callback;
> +	desc->callback_param = buf;
> +
> +	cookie = dmaengine_submit(desc);
> +	ret = dma_submit_error(cookie);
> +	if (ret)
> +		return ret;
> +
> +	dma_async_issue_pending(csi->dma.chan);
> +
> +	return 0;
> +}
> +
> +static void ti_csi2rx_cleanup_buffers(struct ti_csi2rx_dev *csi,
> +				      enum vb2_buffer_state state)
> +{
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	struct ti_csi2rx_buffer *buf = NULL, *tmp;
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	list_for_each_entry_safe(buf, tmp, &csi->dma.queue, list) {
> +		list_del(&buf->list);
> +		vb2_buffer_done(&buf->vb.vb2_buf, state);
> +	}
> +
> +	if (dma->curr)
> +		vb2_buffer_done(&dma->curr->vb.vb2_buf, state);
> +
> +	dma->curr = NULL;
> +	dma->state = TI_CSI2RX_DMA_STOPPED;
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +}
> +
> +static int ti_csi2rx_queue_setup(struct vb2_queue *q, unsigned int *nbuffers,
> +				 unsigned int *nplanes, unsigned int sizes[],
> +				 struct device *alloc_devs[])
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(q);
> +	unsigned int size = csi->v_fmt.fmt.pix.sizeimage;
> +
> +	if (*nplanes) {
> +		if (sizes[0] < size)
> +			return -EINVAL;
> +		size = sizes[0];
> +	}
> +
> +	*nplanes = 1;
> +	sizes[0] = size;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_buffer_prepare(struct vb2_buffer *vb)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
> +	unsigned long size = csi->v_fmt.fmt.pix.sizeimage;
> +
> +	if (vb2_plane_size(vb, 0) < size) {
> +		dev_err(csi->dev, "Data will not fit into plane\n");
> +		return -EINVAL;
> +	}
> +
> +	vb2_set_plane_payload(vb, 0, size);
> +	return 0;
> +}
> +
> +static void ti_csi2rx_buffer_queue(struct vb2_buffer *vb)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
> +	struct ti_csi2rx_buffer *buf;
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	unsigned long flags = 0;
> +	int ret;
> +
> +	buf = container_of(vb, struct ti_csi2rx_buffer, vb.vb2_buf);
> +	buf->csi = csi;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	/*
> +	 * Usually the DMA callback takes care of queueing the pending buffers.
> +	 * But if DMA has stalled due to lack of buffers, restart it now.
> +	 */
> +	if (dma->state == TI_CSI2RX_DMA_IDLE) {
> +		ret = ti_csi2rx_start_dma(csi, buf);
> +		if (ret) {
> +			dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
> +			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
> +			goto unlock;
> +		}
> +
> +		dma->curr = buf;
> +		dma->state = TI_CSI2RX_DMA_ACTIVE;
> +	} else {
> +		list_add_tail(&buf->list, &dma->queue);
> +	}
> +
> +unlock:
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +}
> +
> +static int ti_csi2rx_start_streaming(struct vb2_queue *vq, unsigned int count)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	struct ti_csi2rx_buffer *buf;
> +	unsigned long flags;
> +	int ret = 0;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	if (list_empty(&dma->queue))
> +		ret = -EIO;
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +	if (ret)
> +		return ret;
> +
> +	ret = media_pipeline_start(&csi->vdev.entity.pads[0], &csi->pipe);
> +	if (ret)
> +		goto err;
> +
> +	ti_csi2rx_setup_shim(csi);
> +
> +	csi->sequence = 0;
> +
> +	spin_lock_irqsave(&dma->lock, flags);
> +	buf = list_entry(dma->queue.next, struct ti_csi2rx_buffer, list);
> +	list_del(&buf->list);
> +	dma->state = TI_CSI2RX_DMA_ACTIVE;
> +	dma->curr = buf;
> +
> +	ret = ti_csi2rx_start_dma(csi, buf);
> +	if (ret) {
> +		dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
> +		spin_unlock_irqrestore(&dma->lock, flags);
> +		goto err_pipeline;
> +	}
> +
> +	spin_unlock_irqrestore(&dma->lock, flags);
> +
> +	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 1);
> +	if (ret)
> +		goto err_dma;
> +
> +	return 0;
> +
> +err_dma:
> +	dmaengine_terminate_sync(csi->dma.chan);
> +	writel(0, csi->shim + SHIM_DMACNTX);
> +err_pipeline:
> +	media_pipeline_stop(&csi->vdev.entity.pads[0]);
> +err:
> +	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_QUEUED);
> +	return ret;
> +}
> +
> +static void ti_csi2rx_stop_streaming(struct vb2_queue *vq)
> +{
> +	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vq);
> +	int ret;
> +
> +	media_pipeline_stop(&csi->vdev.entity.pads[0]);
> +
> +	ret = v4l2_subdev_call(csi->subdev, video, s_stream, 0);
> +	if (ret)
> +		dev_err(csi->dev, "Failed to stop subdev stream\n");
> +
> +	writel(0, csi->shim + SHIM_CNTL);
> +
> +	ret = dmaengine_terminate_sync(csi->dma.chan);
> +	if (ret)
> +		dev_err(csi->dev, "Failed to stop DMA: %d\n", ret);
> +
> +	writel(0, csi->shim + SHIM_DMACNTX);
> +
> +	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_ERROR);
> +}
> +
> +static const struct vb2_ops csi_vb2_qops = {
> +	.queue_setup = ti_csi2rx_queue_setup,
> +	.buf_prepare = ti_csi2rx_buffer_prepare,
> +	.buf_queue = ti_csi2rx_buffer_queue,
> +	.start_streaming = ti_csi2rx_start_streaming,
> +	.stop_streaming = ti_csi2rx_stop_streaming,
> +	.wait_prepare = vb2_ops_wait_prepare,
> +	.wait_finish = vb2_ops_wait_finish,
> +};
> +
> +static int ti_csi2rx_init_vb2q(struct ti_csi2rx_dev *csi)
> +{
> +	struct vb2_queue *q = &csi->vidq;
> +	int ret;
> +
> +	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
> +	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
> +	q->drv_priv = csi;
> +	q->buf_struct_size = sizeof(struct ti_csi2rx_buffer);
> +	q->ops = &csi_vb2_qops;
> +	q->mem_ops = &vb2_dma_contig_memops;
> +	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
> +	q->dev = dmaengine_get_dma_device(csi->dma.chan);
> +	q->lock = &csi->mutex;
> +	q->min_buffers_needed = 1;
> +
> +	ret = vb2_queue_init(q);
> +	if (ret)
> +		return ret;
> +
> +	csi->vdev.queue = q;
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_link_validate_get_fmt(struct media_pad *pad,
> +					   struct v4l2_subdev_format *fmt)
> +{
> +	if (is_media_entity_v4l2_subdev(pad->entity)) {
> +		struct v4l2_subdev *sd =
> +			media_entity_to_v4l2_subdev(pad->entity);
> +
> +		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
> +		fmt->pad = pad->index;
> +		return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
> +	}
> +
> +	WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
> +	     "Driver bug! Wrong media entity type 0x%08x, entity %s\n",
> +	     pad->entity->function, pad->entity->name);
> +
> +	return -EINVAL;
> +}
> +
> +static int ti_csi2rx_link_validate(struct media_link *link)
> +{
> +	struct media_entity *entity = link->sink->entity;
> +	struct video_device *vdev = media_entity_to_video_device(entity);
> +	struct ti_csi2rx_dev *csi = container_of(vdev, struct ti_csi2rx_dev, vdev);
> +	struct v4l2_pix_format *csi_fmt = &csi->v_fmt.fmt.pix;
> +	struct v4l2_subdev_format source_fmt;
> +	const struct ti_csi2rx_fmt *ti_fmt;
> +	int ret;
> +
> +	if (!(link->flags & MEDIA_LNK_FL_ENABLED)) {
> +		dev_info(csi->dev, "video node %s pad not connected\n",
> +			 vdev->name);
> +		return -ENOTCONN;
> +	}
> +
> +	ret = ti_csi2rx_link_validate_get_fmt(link->source, &source_fmt);
> +	if (ret)
> +		return ret;
> +
> +	if (source_fmt.format.width != csi_fmt->width) {
> +		dev_err(csi->dev, "Width does not match (source %u, sink %u)\n",
> +			source_fmt.format.width, csi_fmt->width);
> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.height != csi_fmt->height) {
> +		dev_err(csi->dev, "Height does not match (source %u, sink %u)\n",
> +			source_fmt.format.height, csi_fmt->height);
> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.field != csi_fmt->field &&
> +	    csi_fmt->field != V4L2_FIELD_NONE) {
> +		dev_err(csi->dev, "Field does not match (source %u, sink %u)\n",
> +			source_fmt.format.field, csi_fmt->field);
> +		return -EPIPE;
> +	}
> +
> +	ti_fmt = find_format_by_code(source_fmt.format.code);
> +	if (!ti_fmt) {
> +		dev_err(csi->dev, "Media bus format 0x%x not supported\n",
> +			source_fmt.format.code);
> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.code == MEDIA_BUS_FMT_YUYV8_2X8 ||
> +	    source_fmt.format.code == MEDIA_BUS_FMT_VYUY8_2X8 ||
> +	    source_fmt.format.code == MEDIA_BUS_FMT_YVYU8_2X8) {
> +		dev_err(csi->dev,
> +			"Only UYVY input allowed for YUV422 8-bit. Output format can be configured.\n");
> +		return -EPIPE;
> +	}
> +
> +	if (source_fmt.format.code != ti_fmt->code) {
> +		dev_err(csi->dev,
> +			"Cannot transform source fmt 0x%x to sink fmt 0x%x\n",
> +			source_fmt.format.code, ti_fmt->code);
> +		return -EPIPE;
> +	}
> +
> +	return 0;
> +}
> +
> +static const struct media_entity_operations ti_csi2rx_video_entity_ops = {
> +	.link_validate = ti_csi2rx_link_validate,
> +};
> +
> +static int ti_csi2rx_init_dma(struct ti_csi2rx_dev *csi)
> +{
> +	struct dma_slave_config cfg = {
> +		.src_addr_width = DMA_SLAVE_BUSWIDTH_16_BYTES };
> +	int ret;
> +
> +	INIT_LIST_HEAD(&csi->dma.queue);
> +	spin_lock_init(&csi->dma.lock);
> +
> +	csi->dma.state = TI_CSI2RX_DMA_STOPPED;
> +
> +	csi->dma.chan = dma_request_chan(csi->dev, "rx0");
> +	if (IS_ERR(csi->dma.chan))
> +		return PTR_ERR(csi->dma.chan);
> +
> +	ret = dmaengine_slave_config(csi->dma.chan, &cfg);
> +	if (ret) {
> +		dma_release_channel(csi->dma.chan);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static int ti_csi2rx_v4l2_init(struct ti_csi2rx_dev *csi)
> +{
> +	struct media_device *mdev = &csi->mdev;
> +	struct video_device *vdev = &csi->vdev;
> +	const struct ti_csi2rx_fmt *fmt;
> +	struct v4l2_pix_format *pix_fmt = &csi->v_fmt.fmt.pix;
> +	int ret;
> +
> +	fmt = find_format_by_pix(V4L2_PIX_FMT_UYVY);
> +	if (!fmt)
> +		return -EINVAL;
> +
> +	pix_fmt->width = 640;
> +	pix_fmt->height = 480;
> +	pix_fmt->field = V4L2_FIELD_NONE;
> +
> +	ti_csi2rx_fill_fmt(fmt, &csi->v_fmt);
> +
> +	mdev->dev = csi->dev;
> +	mdev->hw_revision = 1;
> +	strscpy(mdev->model, "TI-CSI2RX", sizeof(mdev->model));
> +	snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
> +		 dev_name(mdev->dev));

Not needed anymore, done by media_device_init().

> +
> +	media_device_init(mdev);
> +
> +	strscpy(vdev->name, TI_CSI2RX_MODULE_NAME, sizeof(vdev->name));
> +	vdev->v4l2_dev = &csi->v4l2_dev;
> +	vdev->vfl_dir = VFL_DIR_RX;
> +	vdev->fops = &csi_fops;
> +	vdev->ioctl_ops = &csi_ioctl_ops;
> +	vdev->release = video_device_release_empty;
> +	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
> +			    V4L2_CAP_IO_MC;
> +	vdev->lock = &csi->mutex;
> +	video_set_drvdata(vdev, csi);
> +
> +	csi->pad.flags = MEDIA_PAD_FL_SINK;
> +	vdev->entity.ops = &ti_csi2rx_video_entity_ops;
> +	ret = media_entity_pads_init(&csi->vdev.entity, 1, &csi->pad);
> +	if (ret)
> +		return ret;
> +
> +	csi->v4l2_dev.mdev = mdev;
> +
> +	ret = v4l2_device_register(csi->dev, &csi->v4l2_dev);
> +	if (ret)
> +		return ret;
> +
> +	ret = media_device_register(mdev);
> +	if (ret) {
> +		v4l2_device_unregister(&csi->v4l2_dev);
> +		media_device_cleanup(mdev);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +static void ti_csi2rx_cleanup_dma(struct ti_csi2rx_dev *csi)
> +{
> +	dma_release_channel(csi->dma.chan);
> +}
> +
> +static void ti_csi2rx_cleanup_v4l2(struct ti_csi2rx_dev *csi)
> +{
> +	media_device_unregister(&csi->mdev);
> +	v4l2_device_unregister(&csi->v4l2_dev);
> +	media_device_cleanup(&csi->mdev);
> +}
> +
> +static void ti_csi2rx_cleanup_subdev(struct ti_csi2rx_dev *csi)
> +{
> +	v4l2_async_nf_unregister(&csi->notifier);
> +	v4l2_async_nf_cleanup(&csi->notifier);
> +}
> +
> +static void ti_csi2rx_cleanup_vb2q(struct ti_csi2rx_dev *csi)
> +{
> +	vb2_queue_release(&csi->vidq);
> +}
> +
> +static int ti_csi2rx_probe(struct platform_device *pdev)
> +{
> +	struct ti_csi2rx_dev *csi;
> +	struct resource *res;
> +	int ret;
> +
> +	csi = devm_kzalloc(&pdev->dev, sizeof(*csi), GFP_KERNEL);
> +	if (!csi)
> +		return -ENOMEM;
> +
> +	csi->dev = &pdev->dev;
> +	platform_set_drvdata(pdev, csi);
> +
> +	mutex_init(&csi->mutex);
> +
> +	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> +	csi->shim = devm_ioremap_resource(&pdev->dev, res);
> +	if (IS_ERR(csi->shim)) {
> +		ret = PTR_ERR(csi->shim);
> +		goto err_mutex;
> +	}
> +
> +	ret = ti_csi2rx_init_dma(csi);
> +	if (ret)
> +		goto err_mutex;
> +
> +	ret = ti_csi2rx_v4l2_init(csi);
> +	if (ret)
> +		goto err_dma;
> +
> +	ret = ti_csi2rx_init_vb2q(csi);
> +	if (ret)
> +		goto err_v4l2;
> +
> +	ret = ti_csi2rx_init_subdev(csi);
> +	if (ret)
> +		goto err_vb2q;
> +
> +	ret = of_platform_populate(csi->dev->of_node, NULL, NULL, csi->dev);
> +	if (ret) {
> +		dev_err(csi->dev, "Failed to create children: %d\n", ret);
> +		goto err_subdev;
> +	}
> +
> +	return 0;
> +
> +err_subdev:
> +	ti_csi2rx_cleanup_subdev(csi);
> +err_vb2q:
> +	ti_csi2rx_cleanup_vb2q(csi);
> +err_v4l2:
> +	ti_csi2rx_cleanup_v4l2(csi);
> +err_dma:
> +	ti_csi2rx_cleanup_dma(csi);
> +err_mutex:
> +	mutex_destroy(&csi->mutex);
> +	return ret;
> +}
> +
> +static int ti_csi2rx_remove(struct platform_device *pdev)
> +{
> +	struct ti_csi2rx_dev *csi = platform_get_drvdata(pdev);
> +
> +	video_unregister_device(&csi->vdev);
> +
> +	ti_csi2rx_cleanup_vb2q(csi);
> +	ti_csi2rx_cleanup_subdev(csi);
> +	ti_csi2rx_cleanup_v4l2(csi);
> +	ti_csi2rx_cleanup_dma(csi);
> +
> +	mutex_destroy(&csi->mutex);
> +
> +	return 0;
> +}
> +
> +static const struct of_device_id ti_csi2rx_of_match[] = {
> +	{ .compatible = "ti,j721e-csi2rx", },
> +	{ },
> +};
> +MODULE_DEVICE_TABLE(of, ti_csi2rx_of_match);
> +
> +static struct platform_driver ti_csi2rx_pdrv = {
> +	.probe = ti_csi2rx_probe,
> +	.remove = ti_csi2rx_remove,
> +	.driver = {
> +		.name = TI_CSI2RX_MODULE_NAME,
> +		.of_match_table = ti_csi2rx_of_match,
> +	},
> +};
> +
> +module_platform_driver(ti_csi2rx_pdrv);
> +
> +MODULE_DESCRIPTION("TI J721E CSI2 RX Driver");
> +MODULE_AUTHOR("Pratyush Yadav <p.yadav@ti.com>");
> +MODULE_LICENSE("GPL");

-- 
Kind regards,

Sakari Ailus

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 00/13] CSI2RX support on J721E
  2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
                   ` (13 preceding siblings ...)
  2023-03-23 19:36 ` [PATCH v7 00/13] CSI2RX support on J721E Martyn Welch
@ 2023-04-04  8:44 ` Tomi Valkeinen
  2023-06-22  9:18   ` Julien Massot
  14 siblings, 1 reply; 30+ messages in thread
From: Tomi Valkeinen @ 2023-04-04  8:44 UTC (permalink / raw)
  To: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch

On 14/03/2023 13:55, Vaishnav Achath wrote:
> Hi,
> 
> This series adds support for CSI2 capture on J721E. It includes some
> fixes to the Cadence CSI2RX driver, and adds the TI CSI2RX wrapper driver.

I get some errors with 'v4l2-compliance -m /dev/media0', which also causes:

[   51.185172] ------------[ cut here ]------------
[   51.189786] WARNING: CPU: 1 PID: 174 at mm/page_alloc.c:5568 __alloc_pages+0x684/0xd24
[   51.197698] Modules linked in:
[   51.200743] CPU: 1 PID: 174 Comm: v4l2-compliance Not tainted 6.3.0-rc5+ #12
[   51.207772] Hardware name: Texas Instruments J721e EVM (DT)
[   51.213326] pstate: 20000005 (nzCv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   51.220269] pc : __alloc_pages+0x684/0xd24
[   51.224353] lr : __dma_direct_alloc_pages.constprop.0+0x1d4/0x2a0
[   51.230431] sp : ffff800009fd37b0
[   51.233732] x29: ffff800009fd37b0 x28: ffff000801ad18e0 x27: 0000000000000001
[   51.240852] x26: 0000000000000010 x25: ffff800009fd3b18 x24: ffff800008112c60
[   51.247971] x23: 0000000000000010 x22: 00000000ffffffff x21: 0000000000000cc0
[   51.255089] x20: 0000000010000000 x19: 0000000000000000 x18: 0000000000000000
[   51.262208] x17: 0000000000000000 x16: 0000000000000000 x15: 00000000fff7a238
[   51.269326] x14: 0000000000000000 x13: 0000000000000000 x12: 0000000000000000
[   51.276444] x11: 0000000000000000 x10: 0000000000000000 x9 : 0000000000000000
[   51.283562] x8 : ffff000802c4a200 x7 : 0000000000000000 x6 : 0000000000000000
[   51.290680] x5 : 0000000000000000 x4 : ffff0008022a0000 x3 : 0000000000000000
[   51.297798] x2 : 0000000000000000 x1 : 0000000000000001 x0 : ffff80000937a000
[   51.304916] Call trace:
[   51.307350]  __alloc_pages+0x684/0xd24
[   51.311086]  __dma_direct_alloc_pages.constprop.0+0x1d4/0x2a0
[   51.316814]  dma_direct_alloc+0x210/0x33c
[   51.320809]  dma_alloc_attrs+0x80/0xf4
[   51.324548]  vb2_dc_alloc+0xa0/0x184
[   51.328114]  __vb2_queue_alloc+0x19c/0x490
[   51.332197]  vb2_core_reqbufs+0x250/0x45c
[   51.336192]  vb2_ioctl_reqbufs+0xb0/0xe8
[   51.340103]  v4l_reqbufs+0x50/0x64
[   51.343494]  __video_do_ioctl+0x18c/0x3ec
[   51.347489]  video_usercopy+0x214/0x6c4
[   51.351312]  video_ioctl2+0x18/0x24
[   51.354788]  v4l2_ioctl+0x40/0x60
[   51.358088]  v4l2_compat_ioctl32+0x90/0xb4
[   51.362171]  __arm64_compat_sys_ioctl+0x14c/0x170
[   51.366860]  invoke_syscall+0x48/0x114
[   51.370599]  el0_svc_common.constprop.0+0x44/0xf4
[   51.375288]  do_el0_svc_compat+0x20/0x44
[   51.379198]  el0_svc_compat+0x2c/0x84
[   51.382848]  el0t_32_sync_handler+0x98/0x148
[   51.387102]  el0t_32_sync+0x194/0x198
[   51.390752] ---[ end trace 0000000000000000 ]---
[   51.395408] ti-udma 31150000.dma-controller: dma alloc of size 268435456 failed

  Tomi


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E
  2023-03-24 18:14   ` Laurent Pinchart
  2023-03-28  8:19     ` Vaishnav Achath
@ 2023-04-04 11:43     ` Tomi Valkeinen
  1 sibling, 0 replies; 30+ messages in thread
From: Tomi Valkeinen @ 2023-04-04 11:43 UTC (permalink / raw)
  To: Laurent Pinchart, Vaishnav Achath
  Cc: linux-media, devicetree, mripard, mchehab, robh+dt,
	krzysztof.kozlowski+dt, sakari.ailus, linux-kernel, bparrot,
	niklas.soderlund+renesas, j-luthra, devarsht, praneeth, u-kumar1,
	vigneshr, nm, martyn.welch

On 24/03/2023 20:14, Laurent Pinchart wrote:

>> +static int ti_csi2rx_link_validate_get_fmt(struct media_pad *pad,
>> +					   struct v4l2_subdev_format *fmt)
>> +{
>> +	if (is_media_entity_v4l2_subdev(pad->entity)) {
>> +		struct v4l2_subdev *sd =
>> +			media_entity_to_v4l2_subdev(pad->entity);
>> +
>> +		fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
>> +		fmt->pad = pad->index;
>> +		return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
> 
> This will crash with any source that uses the subdev active state. You
> can't pass NULL for the state here.
> 
> How about exporting v4l2_subdev_link_validate_get_format() instead, to
> replace this function ?

I don't think that's a good idea, or needed. 
v4l2_subdev_link_validate_get_format() is an internal helper. Exporting 
it would potentially make maintenance more difficult in the future.

Here, you know the source is the cdns csi2rx. It's always a subdev, so 
is_media_entity_v4l2_subdev() is not needed (I'm not sure why it's used 
in v4l2_subdev_link_validate_get_format either...).

You can use v4l2_subdev_call_state_active() to call the get_fmt here.

Also, the fmt doesn't seem to be initialized to zero, so it'll contain 
garbage in, e.g. the stream field.

Moving to another topic, cdns-csi2rx driver is already (before this 
series) in upstream, but afaics j7 is the only user for it. In other 
words, it's not in use before this series.

I think it would make sense to convert cdns-csi2rx to use subdev state 
first, before adding the j7 csi2rx. That shouldn't be much work, and 
will clean up the driver nicely. With that you could also simplify the 
ti_csi2rx_link_validate by just locking the cdns csi2rx state, and using 
it directly, without using get_fmt and getting the copy of the format. 
It would also help later when adding streams support.

Personally I'd go straight to streams support, as adding it afterwards 
might have some pain points (based on my CAL experience...). At least 
WIP streams patches on top would be a very good thing to have, as they 
will highlight if there are any major changes needed.

  Tomi


^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 00/13] CSI2RX support on J721E
  2023-04-04  8:44 ` Tomi Valkeinen
@ 2023-06-22  9:18   ` Julien Massot
  2023-06-22 11:04     ` Jai Luthra
  0 siblings, 1 reply; 30+ messages in thread
From: Julien Massot @ 2023-06-22  9:18 UTC (permalink / raw)
  To: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus
  Cc: linux-kernel, bparrot, niklas.soderlund+renesas, j-luthra,
	devarsht, praneeth, u-kumar1, vigneshr, nm, martyn.welch,
	Tomi Valkeinen

Hi Vaishnav,

  On 14/03/2023 13:55, Vaishnav Achath wrote:
> Hi,
>
> This series adds support for CSI2 capture on J721E. It includes some
> fixes to the Cadence CSI2RX driver, and adds the TI CSI2RX wrapper 
> driver.
We are testing this patch series and experienced some strange behaviour,
with the same sequence of 5-10 frames repeated over and over.
(Almost the same sequence since frames have different md5sum)

To solve this issue we had to forward port some functions from the TI 
BSP Kernel[1] such as ti_csi2rx_restart_dma, and ti_csi2rx_drain_dma.

Can you consider this issue for the next patchset version?

Thank you,
Julien


Here are the modifications we made for information only.

---
  .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 138 +++++++++++++++---
  1 file changed, 117 insertions(+), 21 deletions(-)

diff --git a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c 
b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
index 1af7b0b09cfc..e8579dbf07b4 100644
--- a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
+++ b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
@@ -46,6 +46,8 @@
  #define MAX_WIDTH_BYTES			SZ_16K
  #define MAX_HEIGHT_LINES		SZ_16K

+#define DRAIN_TIMEOUT_MS		50
+
  struct ti_csi2rx_fmt {
  	u32				fourcc;	/* Four character code. */
  	u32				code;	/* Mbus code. */
@@ -498,6 +500,59 @@ static void ti_csi2rx_setup_shim(struct 
ti_csi2rx_dev *csi)
  	writel(reg, csi->shim + SHIM_PSI_CFG0);
  }

+static void ti_csi2rx_drain_callback(void *param)
+{
+	struct completion *drain_complete = param;
+
+	complete(drain_complete);
+}
+
+static int ti_csi2rx_drain_dma(struct ti_csi2rx_dev *csi)
+{
+	struct dma_async_tx_descriptor *desc;
+	struct device *dev = csi->dma.chan->device->dev;
+	struct completion drain_complete;
+	void *buf;
+	size_t len = csi->v_fmt.fmt.pix.sizeimage;
+	dma_addr_t addr;
+	dma_cookie_t cookie;
+	int ret;
+
+	init_completion(&drain_complete);
+
+	buf = dma_alloc_coherent(dev, len, &addr, GFP_KERNEL | GFP_ATOMIC);
+	if (!buf)
+		return -ENOMEM;
+
+	desc = dmaengine_prep_slave_single(csi->dma.chan, addr, len,
+					   DMA_DEV_TO_MEM,
+					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
+	if (!desc) {
+		ret = -EIO;
+		goto out;
+	}
+
+	desc->callback = ti_csi2rx_drain_callback;
+	desc->callback_param = &drain_complete;
+
+	cookie = dmaengine_submit(desc);
+	ret = dma_submit_error(cookie);
+	if (ret)
+		goto out;
+
+	dma_async_issue_pending(csi->dma.chan);
+
+	if (!wait_for_completion_timeout(&drain_complete,
+					 msecs_to_jiffies(DRAIN_TIMEOUT_MS))) {
+		dmaengine_terminate_sync(csi->dma.chan);
+		ret = -ETIMEDOUT;
+		goto out;
+	}
+out:
+	dma_free_coherent(dev, len, buf, addr);
+	return ret;
+}
+
  static void ti_csi2rx_dma_callback(void *param)
  {
  	struct ti_csi2rx_buffer *buf = param;
@@ -564,24 +619,61 @@ static int ti_csi2rx_start_dma(struct 
ti_csi2rx_dev *csi,
  }

  static void ti_csi2rx_cleanup_buffers(struct ti_csi2rx_dev *csi,
-				      enum vb2_buffer_state state)
+				      enum vb2_buffer_state buf_state)
  {
  	struct ti_csi2rx_dma *dma = &csi->dma;
-	struct ti_csi2rx_buffer *buf = NULL, *tmp;
+	struct ti_csi2rx_buffer *buf = NULL, *tmp, *curr;;
+	enum ti_csi2rx_dma_state state;
  	unsigned long flags;
+	int ret;

  	spin_lock_irqsave(&dma->lock, flags);
  	list_for_each_entry_safe(buf, tmp, &csi->dma.queue, list) {
  		list_del(&buf->list);
-		vb2_buffer_done(&buf->vb.vb2_buf, state);
+		vb2_buffer_done(&buf->vb.vb2_buf, buf_state);
  	}

-	if (dma->curr)
-		vb2_buffer_done(&dma->curr->vb.vb2_buf, state);
-
+	curr = csi->dma.curr;
+	state = csi->dma.state;
  	dma->curr = NULL;
  	dma->state = TI_CSI2RX_DMA_STOPPED;
  	spin_unlock_irqrestore(&dma->lock, flags);
+
+	if (state != TI_CSI2RX_DMA_STOPPED) {
+		ret = ti_csi2rx_drain_dma(csi);
+		if (ret)
+			dev_dbg(csi->dev,
+				"Failed to drain DMA. Next frame might be bogus\n");
+		dmaengine_terminate_sync(csi->dma.chan);
+	}
+
+	if (curr)
+		vb2_buffer_done(&curr->vb.vb2_buf, buf_state);
+}
+
+static int ti_csi2rx_restart_dma(struct ti_csi2rx_dev *csi,
+				 struct ti_csi2rx_buffer *buf)
+{
+	struct ti_csi2rx_dma *dma = &csi->dma;
+	unsigned long flags = 0;
+	int ret = 0;
+
+	ret = ti_csi2rx_drain_dma(csi);
+	if (ret)
+		dev_warn(csi->dev,
+			 "Failed to drain DMA. Next frame might be bogus\n");
+
+	ret = ti_csi2rx_start_dma(csi, buf);
+	if (ret) {
+		dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
+		spin_lock_irqsave(&dma->lock, flags);
+		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+		dma->curr = NULL;
+		dma->state = TI_CSI2RX_DMA_IDLE;
+		spin_unlock_irqrestore(&dma->lock, flags);
+	}
+
+	return ret;
  }

  static int ti_csi2rx_queue_setup(struct vb2_queue *q, unsigned int 
*nbuffers,
@@ -622,6 +714,7 @@ static void ti_csi2rx_buffer_queue(struct vb2_buffer 
*vb)
  	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
  	struct ti_csi2rx_buffer *buf;
  	struct ti_csi2rx_dma *dma = &csi->dma;
+	bool restart_dma = false;
  	unsigned long flags = 0;
  	int ret;

@@ -634,21 +727,30 @@ static void ti_csi2rx_buffer_queue(struct 
vb2_buffer *vb)
  	 * But if DMA has stalled due to lack of buffers, restart it now.
  	 */
  	if (dma->state == TI_CSI2RX_DMA_IDLE) {
-		ret = ti_csi2rx_start_dma(csi, buf);
-		if (ret) {
-			dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
-			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
-			goto unlock;
-		}
-
+		/*
+		 * Do not restart DMA with the lock held because
+		 * ti_csi2rx_drain_dma() might block when allocating a buffer.
+		 * There won't be a race on queueing DMA anyway since the
+		 * callback is not being fired.
+		 */
+		restart_dma = true;
  		dma->curr = buf;
  		dma->state = TI_CSI2RX_DMA_ACTIVE;
  	} else {
  		list_add_tail(&buf->list, &dma->queue);
  	}
-
-unlock:
  	spin_unlock_irqrestore(&dma->lock, flags);
+
+	if (restart_dma) {
+		/*
+		 * Once frames start dropping, some data gets stuck in the DMA
+		 * pipeline somewhere. So the first DMA transfer after frame
+		 * drops gives a partial frame. This is obviously not useful to
+		 * the application and will only confuse it. Issue a DMA
+		 * transaction to drain that up.
+		 */
+		ti_csi2rx_restart_dma(csi, buf);
+	}
  }

  static int ti_csi2rx_start_streaming(struct vb2_queue *vq, unsigned 
int count)
@@ -718,12 +820,6 @@ static void ti_csi2rx_stop_streaming(struct 
vb2_queue *vq)

  	writel(0, csi->shim + SHIM_CNTL);

-	ret = dmaengine_terminate_sync(csi->dma.chan);
-	if (ret)
-		dev_err(csi->dev, "Failed to stop DMA: %d\n", ret);
-
-	writel(0, csi->shim + SHIM_DMACNTX);
-
  	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_ERROR);
  }

-- 
2.41.0


[1] TI BSP kernel : 
https://git.ti.com/cgit/ti-linux-kernel/ti-linux-kernel/tree/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c?h=ti-linux-6.1.y-cicd

-- 
Julien Massot
Senior Software Engineer
Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK
Registered in England & Wales, no. 5513718

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v7 00/13] CSI2RX support on J721E
  2023-06-22  9:18   ` Julien Massot
@ 2023-06-22 11:04     ` Jai Luthra
  0 siblings, 0 replies; 30+ messages in thread
From: Jai Luthra @ 2023-06-22 11:04 UTC (permalink / raw)
  To: Julien Massot
  Cc: Vaishnav Achath, linux-media, devicetree, mripard, mchehab,
	robh+dt, krzysztof.kozlowski+dt, laurent.pinchart, sakari.ailus,
	linux-kernel, bparrot, niklas.soderlund+renesas, devarsht,
	praneeth, u-kumar1, vigneshr, nm, martyn.welch, Tomi Valkeinen

[-- Attachment #1: Type: text/plain, Size: 8063 bytes --]

Hi Julien,

On Jun 22, 2023 at 11:18:28 +0200, Julien Massot wrote:
> Hi Vaishnav,
> 
>  On 14/03/2023 13:55, Vaishnav Achath wrote:
> > Hi,
> > 
> > This series adds support for CSI2 capture on J721E. It includes some
> > fixes to the Cadence CSI2RX driver, and adds the TI CSI2RX wrapper
> > driver.
> We are testing this patch series and experienced some strange behaviour,
> with the same sequence of 5-10 frames repeated over and over.
> (Almost the same sequence since frames have different md5sum)
> 
> To solve this issue we had to forward port some functions from the TI BSP
> Kernel[1] such as ti_csi2rx_restart_dma, and ti_csi2rx_drain_dma.
> 
> Can you consider this issue for the next patchset version?

While we haven't seen this particular issue (of repeating frames) due to 
a lack of draining, I agree that it is a useful fix.

Will include it in v8 of this series along with some other features and 
fixes around stream stop sequence.

Thanks,

> 
> Thank you,
> Julien
> 
> 
> Here are the modifications we made for information only.
> 
> ---
>  .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 138 +++++++++++++++---
>  1 file changed, 117 insertions(+), 21 deletions(-)
> 
> diff --git a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> index 1af7b0b09cfc..e8579dbf07b4 100644
> --- a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> +++ b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
> @@ -46,6 +46,8 @@
>  #define MAX_WIDTH_BYTES			SZ_16K
>  #define MAX_HEIGHT_LINES		SZ_16K
> 
> +#define DRAIN_TIMEOUT_MS		50
> +
>  struct ti_csi2rx_fmt {
>  	u32				fourcc;	/* Four character code. */
>  	u32				code;	/* Mbus code. */
> @@ -498,6 +500,59 @@ static void ti_csi2rx_setup_shim(struct ti_csi2rx_dev
> *csi)
>  	writel(reg, csi->shim + SHIM_PSI_CFG0);
>  }
> 
> +static void ti_csi2rx_drain_callback(void *param)
> +{
> +	struct completion *drain_complete = param;
> +
> +	complete(drain_complete);
> +}
> +
> +static int ti_csi2rx_drain_dma(struct ti_csi2rx_dev *csi)
> +{
> +	struct dma_async_tx_descriptor *desc;
> +	struct device *dev = csi->dma.chan->device->dev;
> +	struct completion drain_complete;
> +	void *buf;
> +	size_t len = csi->v_fmt.fmt.pix.sizeimage;
> +	dma_addr_t addr;
> +	dma_cookie_t cookie;
> +	int ret;
> +
> +	init_completion(&drain_complete);
> +
> +	buf = dma_alloc_coherent(dev, len, &addr, GFP_KERNEL | GFP_ATOMIC);
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	desc = dmaengine_prep_slave_single(csi->dma.chan, addr, len,
> +					   DMA_DEV_TO_MEM,
> +					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
> +	if (!desc) {
> +		ret = -EIO;
> +		goto out;
> +	}
> +
> +	desc->callback = ti_csi2rx_drain_callback;
> +	desc->callback_param = &drain_complete;
> +
> +	cookie = dmaengine_submit(desc);
> +	ret = dma_submit_error(cookie);
> +	if (ret)
> +		goto out;
> +
> +	dma_async_issue_pending(csi->dma.chan);
> +
> +	if (!wait_for_completion_timeout(&drain_complete,
> +					 msecs_to_jiffies(DRAIN_TIMEOUT_MS))) {
> +		dmaengine_terminate_sync(csi->dma.chan);
> +		ret = -ETIMEDOUT;
> +		goto out;
> +	}
> +out:
> +	dma_free_coherent(dev, len, buf, addr);
> +	return ret;
> +}
> +
>  static void ti_csi2rx_dma_callback(void *param)
>  {
>  	struct ti_csi2rx_buffer *buf = param;
> @@ -564,24 +619,61 @@ static int ti_csi2rx_start_dma(struct ti_csi2rx_dev
> *csi,
>  }
> 
>  static void ti_csi2rx_cleanup_buffers(struct ti_csi2rx_dev *csi,
> -				      enum vb2_buffer_state state)
> +				      enum vb2_buffer_state buf_state)
>  {
>  	struct ti_csi2rx_dma *dma = &csi->dma;
> -	struct ti_csi2rx_buffer *buf = NULL, *tmp;
> +	struct ti_csi2rx_buffer *buf = NULL, *tmp, *curr;;
> +	enum ti_csi2rx_dma_state state;
>  	unsigned long flags;
> +	int ret;
> 
>  	spin_lock_irqsave(&dma->lock, flags);
>  	list_for_each_entry_safe(buf, tmp, &csi->dma.queue, list) {
>  		list_del(&buf->list);
> -		vb2_buffer_done(&buf->vb.vb2_buf, state);
> +		vb2_buffer_done(&buf->vb.vb2_buf, buf_state);
>  	}
> 
> -	if (dma->curr)
> -		vb2_buffer_done(&dma->curr->vb.vb2_buf, state);
> -
> +	curr = csi->dma.curr;
> +	state = csi->dma.state;
>  	dma->curr = NULL;
>  	dma->state = TI_CSI2RX_DMA_STOPPED;
>  	spin_unlock_irqrestore(&dma->lock, flags);
> +
> +	if (state != TI_CSI2RX_DMA_STOPPED) {
> +		ret = ti_csi2rx_drain_dma(csi);
> +		if (ret)
> +			dev_dbg(csi->dev,
> +				"Failed to drain DMA. Next frame might be bogus\n");
> +		dmaengine_terminate_sync(csi->dma.chan);
> +	}
> +
> +	if (curr)
> +		vb2_buffer_done(&curr->vb.vb2_buf, buf_state);
> +}
> +
> +static int ti_csi2rx_restart_dma(struct ti_csi2rx_dev *csi,
> +				 struct ti_csi2rx_buffer *buf)
> +{
> +	struct ti_csi2rx_dma *dma = &csi->dma;
> +	unsigned long flags = 0;
> +	int ret = 0;
> +
> +	ret = ti_csi2rx_drain_dma(csi);
> +	if (ret)
> +		dev_warn(csi->dev,
> +			 "Failed to drain DMA. Next frame might be bogus\n");
> +
> +	ret = ti_csi2rx_start_dma(csi, buf);
> +	if (ret) {
> +		dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
> +		spin_lock_irqsave(&dma->lock, flags);
> +		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
> +		dma->curr = NULL;
> +		dma->state = TI_CSI2RX_DMA_IDLE;
> +		spin_unlock_irqrestore(&dma->lock, flags);
> +	}
> +
> +	return ret;
>  }
> 
>  static int ti_csi2rx_queue_setup(struct vb2_queue *q, unsigned int
> *nbuffers,
> @@ -622,6 +714,7 @@ static void ti_csi2rx_buffer_queue(struct vb2_buffer
> *vb)
>  	struct ti_csi2rx_dev *csi = vb2_get_drv_priv(vb->vb2_queue);
>  	struct ti_csi2rx_buffer *buf;
>  	struct ti_csi2rx_dma *dma = &csi->dma;
> +	bool restart_dma = false;
>  	unsigned long flags = 0;
>  	int ret;
> 
> @@ -634,21 +727,30 @@ static void ti_csi2rx_buffer_queue(struct vb2_buffer
> *vb)
>  	 * But if DMA has stalled due to lack of buffers, restart it now.
>  	 */
>  	if (dma->state == TI_CSI2RX_DMA_IDLE) {
> -		ret = ti_csi2rx_start_dma(csi, buf);
> -		if (ret) {
> -			dev_err(csi->dev, "Failed to start DMA: %d\n", ret);
> -			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
> -			goto unlock;
> -		}
> -
> +		/*
> +		 * Do not restart DMA with the lock held because
> +		 * ti_csi2rx_drain_dma() might block when allocating a buffer.
> +		 * There won't be a race on queueing DMA anyway since the
> +		 * callback is not being fired.
> +		 */
> +		restart_dma = true;
>  		dma->curr = buf;
>  		dma->state = TI_CSI2RX_DMA_ACTIVE;
>  	} else {
>  		list_add_tail(&buf->list, &dma->queue);
>  	}
> -
> -unlock:
>  	spin_unlock_irqrestore(&dma->lock, flags);
> +
> +	if (restart_dma) {
> +		/*
> +		 * Once frames start dropping, some data gets stuck in the DMA
> +		 * pipeline somewhere. So the first DMA transfer after frame
> +		 * drops gives a partial frame. This is obviously not useful to
> +		 * the application and will only confuse it. Issue a DMA
> +		 * transaction to drain that up.
> +		 */
> +		ti_csi2rx_restart_dma(csi, buf);
> +	}
>  }
> 
>  static int ti_csi2rx_start_streaming(struct vb2_queue *vq, unsigned int
> count)
> @@ -718,12 +820,6 @@ static void ti_csi2rx_stop_streaming(struct vb2_queue
> *vq)
> 
>  	writel(0, csi->shim + SHIM_CNTL);
> 
> -	ret = dmaengine_terminate_sync(csi->dma.chan);
> -	if (ret)
> -		dev_err(csi->dev, "Failed to stop DMA: %d\n", ret);
> -
> -	writel(0, csi->shim + SHIM_DMACNTX);
> -
>  	ti_csi2rx_cleanup_buffers(csi, VB2_BUF_STATE_ERROR);
>  }
> 
> -- 
> 2.41.0
> 
> 
> [1] TI BSP kernel : https://git.ti.com/cgit/ti-linux-kernel/ti-linux-kernel/tree/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c?h=ti-linux-6.1.y-cicd
> 
> -- 
> Julien Massot
> Senior Software Engineer
> Collabora Ltd.
> Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, UK
> Registered in England & Wales, no. 5513718

--
Jai

GPG Fingerprint: 4DE0 D818 E5D5 75E8 D45A AFC5 43DE 91F9 249A 7145

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2023-06-22 11:04 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 11:55 [PATCH v7 00/13] CSI2RX support on J721E Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 01/13] media: cadence: csi2rx: Unregister v4l2 async notifier Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 02/13] media: cadence: csi2rx: Cleanup media entity properly Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 03/13] media: cadence: csi2rx: Add get_fmt and set_fmt pad ops Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 04/13] media: cadence: csi2rx: Add external DPHY support Vaishnav Achath
2023-03-31 13:19   ` Sakari Ailus
2023-03-14 11:55 ` [PATCH v7 05/13] media: cadence: csi2rx: Soft reset the streams before starting capture Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 06/13] media: cadence: csi2rx: Set the STOP bit when stopping a stream Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 07/13] media: cadence: csi2rx: Fix stream data configuration Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 08/13] media: cadence: csi2rx: Populate subdev devnode Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 09/13] media: cadence: csi2rx: Add link validation Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 10/13] media: ti: Add CSI2RX support for J721E Vaishnav Achath
2023-03-20 12:51   ` Jai Luthra
2023-03-24 18:14   ` Laurent Pinchart
2023-03-28  8:19     ` Vaishnav Achath
2023-04-04 11:43     ` Tomi Valkeinen
2023-03-31 13:27   ` Sakari Ailus
2023-03-14 11:55 ` [PATCH v7 11/13] media: dt-bindings: Make sure items in data-lanes are unique Vaishnav Achath
2023-03-14 11:55 ` [PATCH v7 12/13] media: dt-bindings: Add DT bindings for TI J721E CSI2RX driver Vaishnav Achath
2023-03-14 14:10   ` Rob Herring
2023-03-15  7:44   ` Krzysztof Kozlowski
2023-03-14 11:55 ` [PATCH v7 13/13] media: dt-bindings: Convert Cadence CSI2RX binding to YAML Vaishnav Achath
2023-03-15  7:45   ` Krzysztof Kozlowski
2023-03-20  5:34     ` Vaishnav Achath
2023-03-23 19:36 ` [PATCH v7 00/13] CSI2RX support on J721E Martyn Welch
2023-03-28  6:01   ` Jai Luthra
2023-03-29 15:17     ` Martyn Welch
2023-04-04  8:44 ` Tomi Valkeinen
2023-06-22  9:18   ` Julien Massot
2023-06-22 11:04     ` Jai Luthra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).