[PATCH v3 1/3] media: v4l: Add helper to get number of active lanes via a pad

Isaac Scott posted 3 patches 2 weeks, 3 days ago
There is a newer version of this series
[PATCH v3 1/3] media: v4l: Add helper to get number of active lanes via a pad
Posted by Isaac Scott 2 weeks, 3 days ago
Sometimes, users will not use all of the MIPI CSI 2 lanes available when
connecting to the MIPI CSI receiver of their device. Add a helper
function that checks the mbus_config for the device driver to allow
users to define the number of active data lanes through the
get_mbus_config op.

If the driver does not implement this op, fall back to using the number
of data lanes specified in device tree.

Reviewed-by: Frank Li <Frank.Li@nxp.com>
Signed-off-by: Isaac Scott <isaac.scott@ideasonboard.com>
---
 drivers/media/v4l2-core/v4l2-common.c | 32 ++++++++++++++++++++++++++++++++
 include/media/v4l2-common.h           | 21 +++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
index 6e585bc76367..2ce8407f1397 100644
--- a/drivers/media/v4l2-core/v4l2-common.c
+++ b/drivers/media/v4l2-core/v4l2-common.c
@@ -571,6 +571,38 @@ s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
 	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
 }
 EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
+
+unsigned int v4l2_get_active_data_lanes(const struct media_pad *pad,
+					unsigned int max_data_lanes)
+{
+	struct v4l2_mbus_config mbus_config = {};
+	struct v4l2_subdev *sd;
+	unsigned int lanes;
+	int ret;
+
+	sd = media_entity_to_v4l2_subdev(pad->entity);
+	ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad->index,
+			       &mbus_config);
+	if (ret < 0 && ret != -ENOIOCTLCMD)
+		return ret;
+
+	/*
+	 * This relies on the mbus_config being zeroed at init time.
+	 */
+	if (!mbus_config.bus.mipi_csi2.num_data_lanes)
+		return max_data_lanes;
+
+	lanes = mbus_config.bus.mipi_csi2.num_data_lanes;
+
+	if (lanes > max_data_lanes) {
+		dev_dbg(sd->dev, "Active data lanes (%u) exceeds max (%u)\n",
+			lanes, max_data_lanes);
+		return -EINVAL;
+	}
+
+	return lanes;
+}
+EXPORT_SYMBOL_GPL(v4l2_get_active_data_lanes);
 #endif /* CONFIG_MEDIA_CONTROLLER */
 
 /*
diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
index 0a43f56578bc..6af0695460ab 100644
--- a/include/media/v4l2-common.h
+++ b/include/media/v4l2-common.h
@@ -584,6 +584,27 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
 	(pad, mul, div)
 s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
 			     unsigned int div);
+
+/**
+ * v4l2_get_active_data_lanes - Get number of active data lanes from driver
+ *
+ * @pad: The transmitter's media pad.
+ * @max_data_lanes: The maximum number of active data lanes supported by
+ * 		    the MIPI CSI link in hardware. This can be configured
+ * 		    in device tree.
+ *
+ * This function is intended for obtaining the number of data lanes that are
+ * actively being used by the driver for a MIPI CSI-2 device on a given media pad.
+ * This information is derived from a mbus_config fetched from a device driver
+ * using the get_mbus_config v4l2_subdev pad op.
+ *
+ * Return:
+ * * >0: Number of active data lanes
+ * * %-EINVAL: Number of active data lanes is invalid, as it exceeds the maximum
+ *	       supported data lanes listed in device tree.
+ */
+unsigned int v4l2_get_active_data_lanes(const struct media_pad *pad,
+					unsigned int max_data_lanes);
 #else
 #define v4l2_get_link_freq(handler, mul, div)		\
 	__v4l2_get_link_freq_ctrl(handler, mul, div)

-- 
2.43.0
Re: [PATCH v3 1/3] media: v4l: Add helper to get number of active lanes via a pad
Posted by Sakari Ailus 1 week, 3 days ago
Hi Isaac,

Thanks for the update.

On Mon, Sep 15, 2025 at 02:18:33PM +0100, Isaac Scott wrote:
> Sometimes, users will not use all of the MIPI CSI 2 lanes available when
> connecting to the MIPI CSI receiver of their device. Add a helper
> function that checks the mbus_config for the device driver to allow
> users to define the number of active data lanes through the
> get_mbus_config op.
> 
> If the driver does not implement this op, fall back to using the number
> of data lanes specified in device tree.
> 
> Reviewed-by: Frank Li <Frank.Li@nxp.com>
> Signed-off-by: Isaac Scott <isaac.scott@ideasonboard.com>
> ---
>  drivers/media/v4l2-core/v4l2-common.c | 32 ++++++++++++++++++++++++++++++++
>  include/media/v4l2-common.h           | 21 +++++++++++++++++++++
>  2 files changed, 53 insertions(+)
> 
> diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
> index 6e585bc76367..2ce8407f1397 100644
> --- a/drivers/media/v4l2-core/v4l2-common.c
> +++ b/drivers/media/v4l2-core/v4l2-common.c
> @@ -571,6 +571,38 @@ s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
>  	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
>  }
>  EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
> +
> +unsigned int v4l2_get_active_data_lanes(const struct media_pad *pad,
> +					unsigned int max_data_lanes)
> +{
> +	struct v4l2_mbus_config mbus_config = {};
> +	struct v4l2_subdev *sd;
> +	unsigned int lanes;
> +	int ret;
> +
> +	sd = media_entity_to_v4l2_subdev(pad->entity);
> +	ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad->index,
> +			       &mbus_config);
> +	if (ret < 0 && ret != -ENOIOCTLCMD)
> +		return ret;
> +
> +	/*
> +	 * This relies on the mbus_config being zeroed at init time.
> +	 */
> +	if (!mbus_config.bus.mipi_csi2.num_data_lanes)

I'd either use the local variable for this (lanes) either all the time, or
not at all.

> +		return max_data_lanes;
> +
> +	lanes = mbus_config.bus.mipi_csi2.num_data_lanes;
> +
> +	if (lanes > max_data_lanes) {
> +		dev_dbg(sd->dev, "Active data lanes (%u) exceeds max (%u)\n",
> +			lanes, max_data_lanes);
> +		return -EINVAL;
> +	}
> +
> +	return lanes;
> +}
> +EXPORT_SYMBOL_GPL(v4l2_get_active_data_lanes);
>  #endif /* CONFIG_MEDIA_CONTROLLER */
>  
>  /*
> diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> index 0a43f56578bc..6af0695460ab 100644
> --- a/include/media/v4l2-common.h
> +++ b/include/media/v4l2-common.h
> @@ -584,6 +584,27 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
>  	(pad, mul, div)
>  s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
>  			     unsigned int div);

Is your tree up to date?

> +
> +/**
> + * v4l2_get_active_data_lanes - Get number of active data lanes from driver
> + *
> + * @pad: The transmitter's media pad.
> + * @max_data_lanes: The maximum number of active data lanes supported by
> + * 		    the MIPI CSI link in hardware. This can be configured
> + * 		    in device tree.

I'd remove the latter sentence. Alternatively, it needs to be improved:
there are other sources for this information than DT.

> + *
> + * This function is intended for obtaining the number of data lanes that are
> + * actively being used by the driver for a MIPI CSI-2 device on a given media pad.
> + * This information is derived from a mbus_config fetched from a device driver
> + * using the get_mbus_config v4l2_subdev pad op.
> + *
> + * Return:
> + * * >0: Number of active data lanes
> + * * %-EINVAL: Number of active data lanes is invalid, as it exceeds the maximum
> + *	       supported data lanes listed in device tree.
> + */
> +unsigned int v4l2_get_active_data_lanes(const struct media_pad *pad,
> +					unsigned int max_data_lanes);
>  #else
>  #define v4l2_get_link_freq(handler, mul, div)		\
>  	__v4l2_get_link_freq_ctrl(handler, mul, div)
> 

-- 
Regards,

Sakari Ailus
Re: [PATCH v3 1/3] media: v4l: Add helper to get number of active lanes via a pad
Posted by Laurent Pinchart 1 week, 3 days ago
On Mon, Sep 22, 2025 at 09:48:39AM +0300, Sakari Ailus wrote:
> On Mon, Sep 15, 2025 at 02:18:33PM +0100, Isaac Scott wrote:
> > Sometimes, users will not use all of the MIPI CSI 2 lanes available when
> > connecting to the MIPI CSI receiver of their device. Add a helper
> > function that checks the mbus_config for the device driver to allow
> > users to define the number of active data lanes through the
> > get_mbus_config op.
> > 
> > If the driver does not implement this op, fall back to using the number
> > of data lanes specified in device tree.
> > 
> > Reviewed-by: Frank Li <Frank.Li@nxp.com>
> > Signed-off-by: Isaac Scott <isaac.scott@ideasonboard.com>
> > ---
> >  drivers/media/v4l2-core/v4l2-common.c | 32 ++++++++++++++++++++++++++++++++
> >  include/media/v4l2-common.h           | 21 +++++++++++++++++++++
> >  2 files changed, 53 insertions(+)
> > 
> > diff --git a/drivers/media/v4l2-core/v4l2-common.c b/drivers/media/v4l2-core/v4l2-common.c
> > index 6e585bc76367..2ce8407f1397 100644
> > --- a/drivers/media/v4l2-core/v4l2-common.c
> > +++ b/drivers/media/v4l2-core/v4l2-common.c
> > @@ -571,6 +571,38 @@ s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
> >  	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
> >  }
> >  EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
> > +
> > +unsigned int v4l2_get_active_data_lanes(const struct media_pad *pad,
> > +					unsigned int max_data_lanes)
> > +{
> > +	struct v4l2_mbus_config mbus_config = {};
> > +	struct v4l2_subdev *sd;
> > +	unsigned int lanes;
> > +	int ret;
> > +
> > +	sd = media_entity_to_v4l2_subdev(pad->entity);
> > +	ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad->index,
> > +			       &mbus_config);
> > +	if (ret < 0 && ret != -ENOIOCTLCMD)
> > +		return ret;
> > +
> > +	/*
> > +	 * This relies on the mbus_config being zeroed at init time.
> > +	 */

This holds on a single line.

> > +	if (!mbus_config.bus.mipi_csi2.num_data_lanes)
> 
> I'd either use the local variable for this (lanes) either all the time, or
> not at all.
> 
> > +		return max_data_lanes;
> > +
> > +	lanes = mbus_config.bus.mipi_csi2.num_data_lanes;
> > +
> > +	if (lanes > max_data_lanes) {
> > +		dev_dbg(sd->dev, "Active data lanes (%u) exceeds max (%u)\n",
> > +			lanes, max_data_lanes);
> > +		return -EINVAL;
> > +	}
> > +
> > +	return lanes;
> > +}
> > +EXPORT_SYMBOL_GPL(v4l2_get_active_data_lanes);
> >  #endif /* CONFIG_MEDIA_CONTROLLER */
> >  
> >  /*
> > diff --git a/include/media/v4l2-common.h b/include/media/v4l2-common.h
> > index 0a43f56578bc..6af0695460ab 100644
> > --- a/include/media/v4l2-common.h
> > +++ b/include/media/v4l2-common.h
> > @@ -584,6 +584,27 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
> >  	(pad, mul, div)
> >  s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
> >  			     unsigned int div);
> 
> Is your tree up to date?

We seem to be moving faster than we used to :-)

> > +
> > +/**
> > + * v4l2_get_active_data_lanes - Get number of active data lanes from driver
> > + *
> > + * @pad: The transmitter's media pad.
> > + * @max_data_lanes: The maximum number of active data lanes supported by
> > + * 		    the MIPI CSI link in hardware. This can be configured
> > + * 		    in device tree.
> 
> I'd remove the latter sentence. Alternatively, it needs to be improved:
> there are other sources for this information than DT.
> 
> > + *
> > + * This function is intended for obtaining the number of data lanes that are
> > + * actively being used by the driver for a MIPI CSI-2 device on a given media pad.
> > + * This information is derived from a mbus_config fetched from a device driver
> > + * using the get_mbus_config v4l2_subdev pad op.
> > + *
> > + * Return:
> > + * * >0: Number of active data lanes
> > + * * %-EINVAL: Number of active data lanes is invalid, as it exceeds the maximum
> > + *	       supported data lanes listed in device tree.

Drop "listed in the device tree" here too.

With those small issues fixed,

Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

> > + */
> > +unsigned int v4l2_get_active_data_lanes(const struct media_pad *pad,
> > +					unsigned int max_data_lanes);
> >  #else
> >  #define v4l2_get_link_freq(handler, mul, div)		\
> >  	__v4l2_get_link_freq_ctrl(handler, mul, div)

-- 
Regards,

Laurent Pinchart