public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] media: v4l2-subdev: Introduce v4l2_subdev_get_frame_desc()
@ 2026-03-29 19:56 Sakari Ailus
  2026-03-30 13:58 ` Lad, Prabhakar
  0 siblings, 1 reply; 2+ messages in thread
From: Sakari Ailus @ 2026-03-29 19:56 UTC (permalink / raw)
  To: linux-media
  Cc: Prabhakar, Mauro Carvalho Chehab, Hans Verkuil, Hans de Goede,
	Laurent Pinchart, Vladimir Zapolskiy, Mehdi Djait, Xiaolei Wang,
	Benjamin Mugnier, Sylvain Petinot, Jacopo Mondi,
	Hardevsinh Palaniya, linux-kernel, linux-renesas-soc, Biju Das,
	Fabrizio Castro, Lad Prabhakar, Laurent Pinchart

Introduce v4l2_subdev_get_frame_desc() in order to facilitate implementing
drivers that need frame descriptors. If the remote sub-device does not
support frame descriptors, v4l2_subdev_get_frame_desc() creates one (with
a single entry) opportunistically, thus avoiding the need to add frame
descriptor support to sensor drivers the device for which only generates a
single stream, or managing the situation on the caller side.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 drivers/media/v4l2-core/v4l2-subdev.c | 96 +++++++++++++++++++++++++++
 include/media/v4l2-subdev.h           | 20 ++++++
 2 files changed, 116 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
index 04a5cb2ad3e3..6032b7c4b949 100644
--- a/drivers/media/v4l2-core/v4l2-subdev.c
+++ b/drivers/media/v4l2-core/v4l2-subdev.c
@@ -20,6 +20,7 @@
 #include <linux/version.h>
 #include <linux/videodev2.h>
 
+#include <media/mipi-csi2.h>
 #include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-event.h>
@@ -2790,3 +2791,98 @@ void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
 #endif
 }
 EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);
+
+static int get_mipi_dt_for_mbus(u32 code)
+{
+	switch (code) {
+	case MEDIA_BUS_FMT_BGR888_1X24:
+		return MIPI_CSI2_DT_RGB888;
+	case MEDIA_BUS_FMT_Y8_1X8:
+	case MEDIA_BUS_FMT_SBGGR8_1X8:
+	case MEDIA_BUS_FMT_SGBRG8_1X8:
+	case MEDIA_BUS_FMT_SGRBG8_1X8:
+	case MEDIA_BUS_FMT_SRGGB8_1X8:
+		return MIPI_CSI2_DT_RAW8;
+	case MEDIA_BUS_FMT_Y10_1X10:
+	case MEDIA_BUS_FMT_SBGGR10_1X10:
+	case MEDIA_BUS_FMT_SGBRG10_1X10:
+	case MEDIA_BUS_FMT_SGRBG10_1X10:
+	case MEDIA_BUS_FMT_SRGGB10_1X10:
+		return MIPI_CSI2_DT_RAW10;
+	case MEDIA_BUS_FMT_Y12_1X12:
+	case MEDIA_BUS_FMT_SBGGR12_1X12:
+	case MEDIA_BUS_FMT_SGBRG12_1X12:
+	case MEDIA_BUS_FMT_SGRBG12_1X12:
+	case MEDIA_BUS_FMT_SRGGB12_1X12:
+		return MIPI_CSI2_DT_RAW12;
+	case MEDIA_BUS_FMT_Y14_1X14:
+	case MEDIA_BUS_FMT_SBGGR14_1X14:
+	case MEDIA_BUS_FMT_SGBRG14_1X14:
+	case MEDIA_BUS_FMT_SGRBG14_1X14:
+	case MEDIA_BUS_FMT_SRGGB14_1X14:
+		return MIPI_CSI2_DT_RAW14;
+	case MEDIA_BUS_FMT_Y16_1X16:
+	case MEDIA_BUS_FMT_SBGGR16_1X16:
+	case MEDIA_BUS_FMT_SGBRG16_1X16:
+	case MEDIA_BUS_FMT_SGRBG16_1X16:
+	case MEDIA_BUS_FMT_SRGGB16_1X16:
+		return MIPI_CSI2_DT_RAW16;
+	case MEDIA_BUS_FMT_SBGGR20_1X20:
+	case MEDIA_BUS_FMT_SGBRG20_1X20:
+	case MEDIA_BUS_FMT_SGRBG20_1X20:
+	case MEDIA_BUS_FMT_SRGGB20_1X20:
+		return MIPI_CSI2_DT_RAW20;
+	default:
+		return -EINVAL;
+	}
+}
+
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc)
+{
+	if (v4l2_subdev_has_op(sd, pad, get_frame_desc)) {
+		unsigned int type = desc->type;
+		int ret;
+
+		ret = v4l2_subdev_call(sd, pad, get_frame_desc, pad, desc);
+
+		if (desc->type != type)
+			return -EINVAL;
+
+		return ret;
+	}
+
+	if (desc->type != V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL &&
+	    desc->type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
+		return -EINVAL;
+
+	struct v4l2_subdev_state *state =
+		v4l2_subdev_lock_and_get_active_state(sd);
+	if (!state)
+		return -EINVAL;
+
+	struct v4l2_mbus_framefmt *fmt =
+		v4l2_subdev_state_get_format(state, pad, 0);
+	if (!fmt)
+		return -EINVAL;
+
+	struct v4l2_mbus_frame_desc_entry entry = {
+		.pixelcode = fmt->code,
+	};
+
+	if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
+		int dt;
+
+		dt = get_mipi_dt_for_mbus(fmt->code);
+		if (dt < 0)
+			return dt;
+
+		entry.bus.csi2.dt = dt;
+	}
+
+	desc->entry[0] = entry;
+	desc->num_entries = 1;
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
index 97b487b1507a..9b0e091c30c1 100644
--- a/include/media/v4l2-subdev.h
+++ b/include/media/v4l2-subdev.h
@@ -2100,4 +2100,24 @@ void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
  */
 bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd);
 
+/**
+ * v4l2_subdev_get_frame_desc() - Get a pad's frame descriptor
+ * @sd: The sub-device
+ * @pad: The number of the pad in @sd from which to obtain the frame descriptor
+ * @desc: A pointer to a frame descriptor, with its type field set
+ *
+ * Obtain a frame descriptor from a sub-device. If the sub-device supports the
+ * get_frame_desc pad operation, its result is returned, just like calling it
+ * directly using v4l2_subdev_call(). If the sub-device driver does not support
+ * it, then one containing a single entry is created using the information from
+ * the sub-device active state, which this function locks for the duration of
+ * the call to obtain it.
+ *
+ * The caller is required to set @desc->type to the expected bus type.
+ *
+ * Return: %0 on success or negative error code on failure.
+ */
+int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
+			       struct v4l2_mbus_frame_desc *desc);
+
 #endif /* _V4L2_SUBDEV_H */
-- 
2.47.3


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

* Re: [PATCH 1/1] media: v4l2-subdev: Introduce v4l2_subdev_get_frame_desc()
  2026-03-29 19:56 [PATCH 1/1] media: v4l2-subdev: Introduce v4l2_subdev_get_frame_desc() Sakari Ailus
@ 2026-03-30 13:58 ` Lad, Prabhakar
  0 siblings, 0 replies; 2+ messages in thread
From: Lad, Prabhakar @ 2026-03-30 13:58 UTC (permalink / raw)
  To: Sakari Ailus
  Cc: linux-media, Mauro Carvalho Chehab, Hans Verkuil, Hans de Goede,
	Laurent Pinchart, Vladimir Zapolskiy, Mehdi Djait, Xiaolei Wang,
	Benjamin Mugnier, Sylvain Petinot, Jacopo Mondi,
	Hardevsinh Palaniya, linux-kernel, linux-renesas-soc, Biju Das,
	Fabrizio Castro, Lad Prabhakar, Laurent Pinchart

Hi Sakari,

Thank you for the patch.

On Sun, Mar 29, 2026 at 8:56 PM Sakari Ailus
<sakari.ailus@linux.intel.com> wrote:
>
> Introduce v4l2_subdev_get_frame_desc() in order to facilitate implementing
> drivers that need frame descriptors. If the remote sub-device does not
> support frame descriptors, v4l2_subdev_get_frame_desc() creates one (with
> a single entry) opportunistically, thus avoiding the need to add frame
> descriptor support to sensor drivers the device for which only generates a
> single stream, or managing the situation on the caller side.
>
> Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
> ---
>  drivers/media/v4l2-core/v4l2-subdev.c | 96 +++++++++++++++++++++++++++
>  include/media/v4l2-subdev.h           | 20 ++++++
>  2 files changed, 116 insertions(+)
>
> diff --git a/drivers/media/v4l2-core/v4l2-subdev.c b/drivers/media/v4l2-core/v4l2-subdev.c
> index 04a5cb2ad3e3..6032b7c4b949 100644
> --- a/drivers/media/v4l2-core/v4l2-subdev.c
> +++ b/drivers/media/v4l2-core/v4l2-subdev.c
> @@ -20,6 +20,7 @@
>  #include <linux/version.h>
>  #include <linux/videodev2.h>
>
> +#include <media/mipi-csi2.h>
>  #include <media/v4l2-ctrls.h>
>  #include <media/v4l2-device.h>
>  #include <media/v4l2-event.h>
> @@ -2790,3 +2791,98 @@ void v4l2_subdev_put_privacy_led(struct v4l2_subdev *sd)
>  #endif
>  }
>  EXPORT_SYMBOL_GPL(v4l2_subdev_put_privacy_led);
> +
> +static int get_mipi_dt_for_mbus(u32 code)
> +{
> +       switch (code) {
> +       case MEDIA_BUS_FMT_BGR888_1X24:
> +               return MIPI_CSI2_DT_RGB888;
> +       case MEDIA_BUS_FMT_Y8_1X8:
> +       case MEDIA_BUS_FMT_SBGGR8_1X8:
> +       case MEDIA_BUS_FMT_SGBRG8_1X8:
> +       case MEDIA_BUS_FMT_SGRBG8_1X8:
> +       case MEDIA_BUS_FMT_SRGGB8_1X8:
> +               return MIPI_CSI2_DT_RAW8;
> +       case MEDIA_BUS_FMT_Y10_1X10:
> +       case MEDIA_BUS_FMT_SBGGR10_1X10:
> +       case MEDIA_BUS_FMT_SGBRG10_1X10:
> +       case MEDIA_BUS_FMT_SGRBG10_1X10:
> +       case MEDIA_BUS_FMT_SRGGB10_1X10:
> +               return MIPI_CSI2_DT_RAW10;
> +       case MEDIA_BUS_FMT_Y12_1X12:
> +       case MEDIA_BUS_FMT_SBGGR12_1X12:
> +       case MEDIA_BUS_FMT_SGBRG12_1X12:
> +       case MEDIA_BUS_FMT_SGRBG12_1X12:
> +       case MEDIA_BUS_FMT_SRGGB12_1X12:
> +               return MIPI_CSI2_DT_RAW12;
> +       case MEDIA_BUS_FMT_Y14_1X14:
> +       case MEDIA_BUS_FMT_SBGGR14_1X14:
> +       case MEDIA_BUS_FMT_SGBRG14_1X14:
> +       case MEDIA_BUS_FMT_SGRBG14_1X14:
> +       case MEDIA_BUS_FMT_SRGGB14_1X14:
> +               return MIPI_CSI2_DT_RAW14;
> +       case MEDIA_BUS_FMT_Y16_1X16:
> +       case MEDIA_BUS_FMT_SBGGR16_1X16:
> +       case MEDIA_BUS_FMT_SGBRG16_1X16:
> +       case MEDIA_BUS_FMT_SGRBG16_1X16:
> +       case MEDIA_BUS_FMT_SRGGB16_1X16:
> +               return MIPI_CSI2_DT_RAW16;
> +       case MEDIA_BUS_FMT_SBGGR20_1X20:
> +       case MEDIA_BUS_FMT_SGBRG20_1X20:
> +       case MEDIA_BUS_FMT_SGRBG20_1X20:
> +       case MEDIA_BUS_FMT_SRGGB20_1X20:
> +               return MIPI_CSI2_DT_RAW20;
Other bus formats are missing.

> +       default:
> +               return -EINVAL;
> +       }
> +}
> +
> +int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
> +                              struct v4l2_mbus_frame_desc *desc)
> +{
> +       if (v4l2_subdev_has_op(sd, pad, get_frame_desc)) {
> +               unsigned int type = desc->type;
> +               int ret;
> +
> +               ret = v4l2_subdev_call(sd, pad, get_frame_desc, pad, desc);
> +
> +               if (desc->type != type)
> +                       return -EINVAL;
> +
> +               return ret;
> +       }
> +
> +       if (desc->type != V4L2_MBUS_FRAME_DESC_TYPE_PARALLEL &&
> +           desc->type != V4L2_MBUS_FRAME_DESC_TYPE_CSI2)
> +               return -EINVAL;
Maybe we can move this check to the beginning?

> +
> +       struct v4l2_subdev_state *state =
Should we define this and fmt at the top of the function?

> +               v4l2_subdev_lock_and_get_active_state(sd);
> +       if (!state)
> +               return -EINVAL;
> +
> +       struct v4l2_mbus_framefmt *fmt =
> +               v4l2_subdev_state_get_format(state, pad, 0);
Missing v4l2_subdev_unlock_state().

> +       if (!fmt)
> +               return -EINVAL;
> +
> +       struct v4l2_mbus_frame_desc_entry entry = {
> +               .pixelcode = fmt->code,
> +       };
> +
> +       if (desc->type == V4L2_MBUS_FRAME_DESC_TYPE_CSI2) {
> +               int dt;
> +
> +               dt = get_mipi_dt_for_mbus(fmt->code);
> +               if (dt < 0)
> +                       return dt;
> +
> +               entry.bus.csi2.dt = dt;
> +       }
> +
> +       desc->entry[0] = entry;
> +       desc->num_entries = 1;
> +
> +       return 0;
> +}
> +EXPORT_SYMBOL_GPL(v4l2_subdev_get_frame_desc);
> diff --git a/include/media/v4l2-subdev.h b/include/media/v4l2-subdev.h
> index 97b487b1507a..9b0e091c30c1 100644
> --- a/include/media/v4l2-subdev.h
> +++ b/include/media/v4l2-subdev.h
> @@ -2100,4 +2100,24 @@ void v4l2_subdev_notify_event(struct v4l2_subdev *sd,
>   */
>  bool v4l2_subdev_is_streaming(struct v4l2_subdev *sd);
>
> +/**
> + * v4l2_subdev_get_frame_desc() - Get a pad's frame descriptor
> + * @sd: The sub-device
> + * @pad: The number of the pad in @sd from which to obtain the frame descriptor
> + * @desc: A pointer to a frame descriptor, with its type field set
> + *
> + * Obtain a frame descriptor from a sub-device. If the sub-device supports the
> + * get_frame_desc pad operation, its result is returned, just like calling it
> + * directly using v4l2_subdev_call(). If the sub-device driver does not support
> + * it, then one containing a single entry is created using the information from
> + * the sub-device active state, which this function locks for the duration of
> + * the call to obtain it.
> + *
> + * The caller is required to set @desc->type to the expected bus type.
> + *
> + * Return: %0 on success or negative error code on failure.
stray "%" char.

Rest LGTM. I tested it by adding the missing bits on my side; all seems OK.

Cheers,
Prabhakar
> + */
> +int v4l2_subdev_get_frame_desc(struct v4l2_subdev *sd, unsigned int pad,
> +                              struct v4l2_mbus_frame_desc *desc);
> +
>  #endif /* _V4L2_SUBDEV_H */
> --
> 2.47.3
>

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

end of thread, other threads:[~2026-03-30 13:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-29 19:56 [PATCH 1/1] media: v4l2-subdev: Introduce v4l2_subdev_get_frame_desc() Sakari Ailus
2026-03-30 13:58 ` Lad, Prabhakar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox