Skip to contentSkip to conversation

Use V4L2 mbus config for conveying link frequency

PATCH Superseded Sakari Ailus 4 participants 23 messages Dec 17, 2024 – Jan 8, 2025

Subsystems (17)

v8 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Sakari Ailus

author maintainer
#0 lore raw

v8 cover Use V4L2 mbus config for conveying link frequency

Hi folks,

This set adds a few helpers for obtaining the link frequency from the V4L2 mbus config for devices that don't need to provide an UAPI to change it, and finally move drivers to call v4l2_get_link_freq() on a pad.

since v7:

- Drop ctrl_to_csi() as it's now unused.

- Add patches to document how CSI-2 receiver should obtain the link frequency, moving stopping streaming out of CSI-2 specific part and adding a note on setting all fields in get_mbus_config() op.

- Added a patch to convert existing users of v4l2_get_link_freq() to operate on a media pad (vs. the control handler). I'll postpone merging this however to test it a bit as it touches a number of drivers for which I have no hardware for.

- Revert the order or the ivsc and ipu6 patches.

- Improved documentation as discussed with Laurent in v7 review.

since v6:

- Remove comments on #else / #endif, it's trivial.

- Add a patch to convert the ipu6 driver.

since v5:

- Only support pad-based operation with CONFIG_MEDIA_CONTROLLER (1st and 2nd patches).

since v4:

- Rework documentation a little.

- Remove wrong alignment change in 2nd patch.

- Move link_freq field after the type field in struct v4l2_mbus_config.

since v3:

- Add back missing ret I accidentally removed rather than moved to the 2nd patch.

since v2:

- Switch to V4L2 mbus config for conveying the link frequency.

since v1:

- Add a new 64-bit integer control V4L2_CID_CUR_LINK_FREQ instead of re-using V4L2_CID_LINK_FREQ.

Sakari Ailus (9):
media: v4l: Support passing media pad argument to v4l2_get_link_freq()
media: v4l: Support obtaining link frequency via get_mbus_config
media: Documentation: Update link frequency driver documentation
media: Documentation: tx-rx: Move transmitter control out of CSI-2
part
media: Documentation: Receiver drivers should call

    v4l2_get_link_freq()

media: Documentation: Add a note to set all fields in get_mbus_config
op
media: intel/ipu6: Obtain link frequency from the remote subdev pad
media: ivsc: csi: Obtain link frequency from the media pad
media: v4l: Convert the users of v4l2_get_link_freq to call it on a
pad

 Documentation/driver-api/media/tx-rx.rst      | 26 ++++---
 drivers/media/i2c/st-mipid02.c                |  5 +-
 drivers/media/i2c/tc358746.c                  |  4 +-
 drivers/media/pci/intel/ipu3/ipu3-cio2.c      | 11 ++-
 drivers/media/pci/intel/ipu6/ipu6-isys-csi2.c | 12 +--
 drivers/media/pci/intel/ivsc/mei_csi.c        | 78 +++++++------------
 drivers/media/platform/cadence/cdns-csi2rx.c  |  4 +-
 drivers/media/platform/nxp/imx-mipi-csis.c    |  5 +-
 drivers/media/platform/nxp/imx8mq-mipi-csi2.c | 11 ++-
 .../media/platform/qcom/camss/camss-vfe-17x.c |  8 +-
 .../platform/qcom/camss/camss-vfe-gen1.c      |  8 +-
 drivers/media/platform/qcom/camss/camss.c     | 17 ++--
 drivers/media/platform/qcom/camss/camss.h     |  2 +-
 .../media/platform/raspberrypi/rp1-cfe/cfe.c  |  4 +-
 drivers/media/platform/st/stm32/stm32-csi.c   |  4 +-
 drivers/media/platform/ti/cal/cal-camerarx.c  |  3 +-
 drivers/media/platform/ti/cal/cal.c           |  4 +-
 drivers/media/platform/ti/cal/cal.h           |  1 +
 drivers/media/v4l2-core/v4l2-common.c         | 32 +++++++-
 include/media/v4l2-common.h                   | 19 ++++-
 include/media/v4l2-mediabus.h                 |  2 +
 include/media/v4l2-subdev.h                   |  5 +-
 22 files changed, 155 insertions(+), 110 deletions(-)

base-commit: c2bb0ff95c29040483ac93b4c10fdf1bd8071b9d

Signature · 1 line
2.39.5
author maintainer
#1 lore raw

PATCH v8 1/9 media: v4l: Support passing media pad argument to v4l2_get_link_freq()

v4l2_get_link_freq() accepts a V4L2 control handler for now, but it needs to take struct media_pad argument in order to obtain the link frequency using get_mbus_config() pad op. Prepare for this by allowing struct media_pad as well.

---
 drivers/media/v4l2-core/v4l2-common.c | 21 ++++++++++++++++++---
 include/media/v4l2-common.h           | 19 ++++++++++++++++---
 2 files changed, 34 insertions(+), 6 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +18 -3
index 0a2f4f0d0a07..9fe74c7e064f 100644
@@ -466,8 +466,8 @@ int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
 }
 EXPORT_SYMBOL_GPL(v4l2_fill_pixfmt);
 
-s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
-		       unsigned int div)
+s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,
+			      unsigned int mul, unsigned int div)
 {
 	struct v4l2_ctrl *ctrl;
 	s64 freq;
@@ -502,7 +502,22 @@ s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
 
 	return freq > 0 ? freq : -EINVAL;
 }
-EXPORT_SYMBOL_GPL(v4l2_get_link_freq);
+EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_ctrl);
+
+#ifdef CONFIG_MEDIA_CONTROLLER
+s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
+			     unsigned int div)
+{
+	struct v4l2_subdev *sd;
+
+	sd = media_entity_to_v4l2_subdev(pad->entity);
+	if (!sd)
+		return -ENODEV;
+
+	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
+}
+EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
+#endif /* CONFIG_MEDIA_CONTROLLER */
 
 /*
  * Simplify a fraction using a simple continued fraction decomposition. The
include/media/v4l2-common.h +16 -3
index 63ad36f04f72..fda903bb3674 100644
@@ -525,7 +525,8 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
 /**
  * v4l2_get_link_freq - Get link rate from transmitter
  *
- * @handler: The transmitter's control handler
+ * @pad: The transmitter's media pad (or control handler for non-MC users or
+ *	 compatibility reasons, don't use in new code)
  * @mul: The multiplier between pixel rate and link frequency. Bits per pixel on
  *	 D-PHY, samples per clock on parallel. 0 otherwise.
  * @div: The divisor between pixel rate and link frequency. Number of data lanes
@@ -541,8 +542,20 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
  * * %-ENOENT: Link frequency or pixel rate control not found
  * * %-EINVAL: Invalid link frequency value
  */
-s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
-		       unsigned int div);
+#ifdef CONFIG_MEDIA_CONTROLLER
+#define v4l2_get_link_freq(pad, mul, div)				\
+	_Generic(pad,							\
+		 struct media_pad *: __v4l2_get_link_freq_pad,		\
+		 struct v4l2_ctrl_handler *: __v4l2_get_link_freq_ctrl)	\
+	(pad, mul, div)
+s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
+			     unsigned int div);
+#else
+#define v4l2_get_link_freq(handler, mul, div)		\
+	__v4l2_get_link_freq_ctrl(handler, mul, div)
+#endif
+s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,
+			      unsigned int mul, unsigned int div);
 
 void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
 		unsigned int n_terms, unsigned int threshold);
Signature · 1 line
2.39.5
author maintainer
#4 lore raw

PATCH v8 2/9 media: v4l: Support obtaining link frequency via get_mbus_config

In reply to Sakari Ailus

Add link_freq field to struct v4l2_mbus_config in order to pass the link frequency to the receiving sub-device.

---
 drivers/media/v4l2-core/v4l2-common.c | 15 +++++++++++++--
 include/media/v4l2-mediabus.h         |  2 ++
 2 files changed, 15 insertions(+), 2 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +13 -2
index 9fe74c7e064f..e4b2de3833ee 100644
@@ -508,12 +508,23 @@ EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_ctrl);
 s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
 			     unsigned int div)
 {
+	struct v4l2_mbus_config mbus_config = {};
 	struct v4l2_subdev *sd;
+	int ret;
 
 	sd = media_entity_to_v4l2_subdev(pad->entity);
-	if (!sd)
-		return -ENODEV;
+	ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad->index,
+			       &mbus_config);
+	if (ret < 0 && ret != -ENOIOCTLCMD)
+		return ret;
+
+	if (mbus_config.link_freq)
+		return mbus_config.link_freq;
 
+	/*
+	 * Fall back to using the link frequency control if the media bus config
+	 * doesn't provide a link frequency.
+	 */
 	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
 }
 EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
include/media/v4l2-mediabus.h +2 -0
index e7f019f68c8d..24c738cd7894 100644
@@ -169,6 +169,7 @@ enum v4l2_mbus_type {
 /**
  * struct v4l2_mbus_config - media bus configuration
  * @type: interface type
+ * @link_freq: The link frequency. See also V4L2_CID_LINK_FREQ control.
  * @bus: bus configuration data structure
  * @bus.parallel: embedded &struct v4l2_mbus_config_parallel.
  *		  Used if the bus is parallel or BT.656.
@@ -183,6 +184,7 @@ enum v4l2_mbus_type {
  */
 struct v4l2_mbus_config {
 	enum v4l2_mbus_type type;
+	u64 link_freq;
 	union {
 		struct v4l2_mbus_config_parallel parallel;
 		struct v4l2_mbus_config_mipi_csi1 mipi_csi1;
Signature · 1 line
2.39.5
author maintainer
#7 lore raw

PATCH v8 3/9 media: Documentation: Update link frequency driver documentation

In reply to Sakari Ailus

Add the get_mbus_config() as the means for conveying the link frequency towards the receiver drivers.

---
 Documentation/driver-api/media/tx-rx.rst | 4 ++++
 1 file changed, 4 insertions(+)
Documentation/driver-api/media/tx-rx.rst +4 -0
index c71003f74b1c..6f9eba189a9f 100644
@@ -49,6 +49,10 @@ Link frequency
 The :ref:`V4L2_CID_LINK_FREQ <v4l2-cid-link-freq>` control is used to tell the
 receiver the frequency of the bus (i.e. it is not the same as the symbol rate).
 
+Drivers that do not have user-configurable link frequency should report it
+through the ``.get_mbus_config()`` subdev pad operation, in the ``link_freq``
+field of struct v4l2_mbus_config, instead of through controls.
+
 ``.enable_streams()`` and ``.disable_streams()`` callbacks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Signature · 1 line
2.39.5
author maintainer
#8 lore raw

PATCH v8 4/9 media: Documentation: tx-rx: Move transmitter control out of CSI-2 part

In reply to Sakari Ailus

The subsection on stopping the transmitter belongs to the generic part and is not specific to CSI-2. Move it out of the CSI-2 section.

---
 Documentation/driver-api/media/tx-rx.rst | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)
Documentation/driver-api/media/tx-rx.rst +9 -10
index 6f9eba189a9f..03768e5aa88f 100644
@@ -62,6 +62,15 @@ to control the transmitter driver's streaming state. These callbacks may not be
 called directly, but by using ``v4l2_subdev_enable_streams()`` and
 ``v4l2_subdev_disable_streams()``.
 
+Stopping the transmitter
+^^^^^^^^^^^^^^^^^^^^^^^^
+
+A transmitter stops sending the stream of images as a result of
+calling the ``.disable_streams()`` callback. Some transmitters may stop the
+stream at a frame boundary whereas others stop immediately,
+effectively leaving the current frame unfinished. The receiver driver
+should not make assumptions either way, but function properly in both
+cases.
 
 CSI-2 transmitter drivers
 -------------------------
@@ -130,13 +139,3 @@ device, so this should be only done when it is needed.
 
 Receiver drivers that do not need explicit LP-11 or LP-111 state setup are
 waived from calling the two callbacks.
-
-Stopping the transmitter
-^^^^^^^^^^^^^^^^^^^^^^^^
-
-A transmitter stops sending the stream of images as a result of
-calling the ``.disable_streams()`` callback. Some transmitters may stop the
-stream at a frame boundary whereas others stop immediately,
-effectively leaving the current frame unfinished. The receiver driver
-should not make assumptions either way, but function properly in both
-cases.
Signature · 1 line
2.39.5
author maintainer
#10 lore raw

PATCH v8 5/9 media: Documentation: Receiver drivers should call v4l2_get_link_freq()

In reply to Sakari Ailus

Document that receiver drivers should call v4l2_get_link_freq() to obtain the link frequency.

---
 Documentation/driver-api/media/tx-rx.rst | 3 +++
 1 file changed, 3 insertions(+)
Documentation/driver-api/media/tx-rx.rst +3 -0
index 03768e5aa88f..0b8c9cde8ee4 100644
@@ -53,6 +53,9 @@ Drivers that do not have user-configurable link frequency should report it
 through the ``.get_mbus_config()`` subdev pad operation, in the ``link_freq``
 field of struct v4l2_mbus_config, instead of through controls.
 
+Receiver drivers should use :c:func:`v4l2_get_link_freq` helper to obtain the
+link frequency from the transmitter sub-device.
+
 ``.enable_streams()`` and ``.disable_streams()`` callbacks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Signature · 1 line
2.39.5
author maintainer
#12 lore raw

PATCH v8 6/9 media: Documentation: Add a note to set all fields in get_mbus_config op

In reply to Sakari Ailus

Document that the implementations of the V4L2 sub-device pad operation get_mbus_config shall set all fields of the struct to defined values.

---
 include/media/v4l2-subdev.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
include/media/v4l2-subdev.h +4 -1
index 2f2200875b03..abcc879fabb2 100644
@@ -822,7 +822,10 @@ struct v4l2_subdev_state {
  *		     possible configuration from the remote end, likely calling
  *		     this operation as close as possible to stream on time. The
  *		     operation shall fail if the pad index it has been called on
- *		     is not valid or in case of unrecoverable failures.
+ *		     is not valid or in case of unrecoverable failures. The
+ *		     implementations of the get_mbus_config operation shall set
+ *		     all the values of struct v4l2_mbus_config to defined
+ *		     values.
  *
  * @set_routing: Enable or disable data connection routes described in the
  *		 subdevice routing table. Subdevs that implement this operation
Signature · 1 line
2.39.5
author maintainer
#15 lore raw

PATCH v8 7/9 media: intel/ipu6: Obtain link frequency from the remote subdev pad

In reply to Sakari Ailus

Obtain the link frequency from the sub-device's pad instead of a control handler. This allows obtaining it using the get_mbus_config() sub-device pad op which is the only method supported by the IVSC driver.

---
 drivers/media/pci/intel/ipu6/ipu6-isys-csi2.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)
drivers/media/pci/intel/ipu6/ipu6-isys-csi2.c +3 -9
index 051898ce53f4..da8581a37e22 100644
@@ -80,25 +80,19 @@ static const struct ipu6_csi2_error dphy_rx_errors[] = {
 s64 ipu6_isys_csi2_get_link_freq(struct ipu6_isys_csi2 *csi2)
 {
 	struct media_pad *src_pad;
-	struct v4l2_subdev *ext_sd;
-	struct device *dev;
 
 	if (!csi2)
 		return -EINVAL;
 
-	dev = &csi2->isys->adev->auxdev.dev;
 	src_pad = media_entity_remote_source_pad_unique(&csi2->asd.sd.entity);
 	if (IS_ERR(src_pad)) {
-		dev_err(dev, "can't get source pad of %s (%ld)\n",
+		dev_err(&csi2->isys->adev->auxdev.dev,
+			"can't get source pad of %s (%ld)\n",
 			csi2->asd.sd.name, PTR_ERR(src_pad));
 		return PTR_ERR(src_pad);
 	}
 
-	ext_sd = media_entity_to_v4l2_subdev(src_pad->entity);
-	if (WARN(!ext_sd, "Failed to get subdev for %s\n", csi2->asd.sd.name))
-		return -ENODEV;
-
-	return v4l2_get_link_freq(ext_sd->ctrl_handler, 0, 0);
+	return v4l2_get_link_freq(src_pad, 0, 0);
 }
 
 static int csi2_subscribe_event(struct v4l2_subdev *sd, struct v4l2_fh *fh,
Signature · 1 line
2.39.5
author maintainer
#16 lore raw

PATCH v8 8/9 media: ivsc: csi: Obtain link frequency from the media pad

In reply to Sakari Ailus

Support the use of the media pad for obtaining the link frequency. Similarly, call the v4l2_get_link_freq() on the media pad, not on the remote's control handler.

---
 drivers/media/pci/intel/ivsc/mei_csi.c | 78 +++++++++-----------------
 1 file changed, 26 insertions(+), 52 deletions(-)
drivers/media/pci/intel/ivsc/mei_csi.c +26 -52
index 2a9c12c975ca..545de4654609 100644
@@ -35,8 +35,6 @@
 
 #define MEI_CSI_ENTITY_NAME "Intel IVSC CSI"
 
-#define MEI_CSI_LINK_FREQ_400MHZ 400000000ULL
-
 /* the 5s used here is based on experiment */
 #define CSI_CMD_TIMEOUT (5 * HZ)
 /* to setup CSI-2 link an extra delay needed and determined experimentally */
@@ -121,14 +119,13 @@ struct mei_csi {
 	struct mutex lock;
 
 	struct v4l2_subdev subdev;
-	struct v4l2_subdev *remote;
+	struct media_pad *remote;
 	struct v4l2_async_notifier notifier;
 	struct v4l2_ctrl_handler ctrl_handler;
 	struct v4l2_ctrl *freq_ctrl;
 	struct v4l2_ctrl *privacy_ctrl;
 	/* lock for v4l2 controls */
 	struct mutex ctrl_lock;
-	unsigned int remote_pad;
 	/* start streaming or not */
 	int streaming;
 
@@ -147,10 +144,6 @@ static const struct v4l2_mbus_framefmt mei_csi_format_mbus_default = {
 	.field = V4L2_FIELD_NONE,
 };
 
-static s64 link_freq_menu_items[] = {
-	MEI_CSI_LINK_FREQ_400MHZ
-};
-
 static inline struct mei_csi *notifier_to_csi(struct v4l2_async_notifier *n)
 {
 	return container_of(n, struct mei_csi, notifier);
@@ -161,11 +154,6 @@ static inline struct mei_csi *sd_to_csi(struct v4l2_subdev *sd)
 	return container_of(sd, struct mei_csi, subdev);
 }
 
-static inline struct mei_csi *ctrl_to_csi(struct v4l2_ctrl *ctrl)
-{
-	return container_of(ctrl->handler, struct mei_csi, ctrl_handler);
-}
-
 /* send a command to firmware and mutex must be held by caller */
 static int mei_csi_send(struct mei_csi *csi, u8 *buf, size_t len)
 {
@@ -286,11 +274,13 @@ static void mei_csi_rx(struct mei_cl_device *cldev)
 static int mei_csi_set_stream(struct v4l2_subdev *sd, int enable)
 {
 	struct mei_csi *csi = sd_to_csi(sd);
+	struct v4l2_subdev *remote_sd =
+		media_entity_to_v4l2_subdev(csi->remote->entity);
 	s64 freq;
 	int ret;
 
 	if (enable && csi->streaming == 0) {
-		freq = v4l2_get_link_freq(csi->remote->ctrl_handler, 0, 0);
+		freq = v4l2_get_link_freq(csi->remote, 0, 0);
 		if (freq < 0) {
 			dev_err(&csi->cldev->dev,
 				"error %lld, invalid link_freq\n", freq);
@@ -309,11 +299,11 @@ static int mei_csi_set_stream(struct v4l2_subdev *sd, int enable)
 		if (ret < 0)
 			goto err_switch;
 
-		ret = v4l2_subdev_call(csi->remote, video, s_stream, 1);
+		ret = v4l2_subdev_call(remote_sd, video, s_stream, 1);
 		if (ret)
 			goto err_switch;
 	} else if (!enable && csi->streaming == 1) {
-		v4l2_subdev_call(csi->remote, video, s_stream, 0);
+		v4l2_subdev_call(remote_sd, video, s_stream, 0);
 
 		/* switch CSI-2 link to IVSC */
 		ret = csi_set_link_owner(csi, CSI_LINK_IVSC);
@@ -470,34 +460,30 @@ static int mei_csi_set_fmt(struct v4l2_subdev *sd,
 	return 0;
 }
 
-static int mei_csi_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
+static int mei_csi_get_mbus_config(struct v4l2_subdev *sd, unsigned int pad,
+				   struct v4l2_mbus_config *mbus_config)
 {
-	struct mei_csi *csi = ctrl_to_csi(ctrl);
+	struct mei_csi *csi = sd_to_csi(sd);
+	unsigned int i;
 	s64 freq;
 
-	if (ctrl->id == V4L2_CID_LINK_FREQ) {
-		if (!csi->remote)
-			return -EINVAL;
+	mbus_config->type = V4L2_MBUS_CSI2_DPHY;
+	for (i = 0; i < V4L2_MBUS_CSI2_MAX_DATA_LANES; i++)
+		mbus_config->bus.mipi_csi2.data_lanes[i] = i + 1;
+	mbus_config->bus.mipi_csi2.num_data_lanes = csi->nr_of_lanes;
 
-		freq = v4l2_get_link_freq(csi->remote->ctrl_handler, 0, 0);
-		if (freq < 0) {
-			dev_err(&csi->cldev->dev,
-				"error %lld, invalid link_freq\n", freq);
-			return -EINVAL;
-		}
-
-		link_freq_menu_items[0] = freq;
-		ctrl->val = 0;
-
-		return 0;
+	freq = v4l2_get_link_freq(csi->remote, 0, 0);
+	if (freq < 0) {
+		dev_err(&csi->cldev->dev,
+			"error %lld, invalid link_freq\n", freq);
+		return -EINVAL;
 	}
 
-	return -EINVAL;
-}
+	csi->link_freq = freq;
+	mbus_config->link_freq = freq;
 
-static const struct v4l2_ctrl_ops mei_csi_ctrl_ops = {
-	.g_volatile_ctrl = mei_csi_g_volatile_ctrl,
-};
+	return 0;
+}
 
 static const struct v4l2_subdev_video_ops mei_csi_video_ops = {
 	.s_stream = mei_csi_set_stream,
@@ -506,6 +492,7 @@ static const struct v4l2_subdev_video_ops mei_csi_video_ops = {
 static const struct v4l2_subdev_pad_ops mei_csi_pad_ops = {
 	.get_fmt = v4l2_subdev_get_fmt,
 	.set_fmt = mei_csi_set_fmt,
+	.get_mbus_config = mei_csi_get_mbus_config,
 };
 
 static const struct v4l2_subdev_ops mei_csi_subdev_ops = {
@@ -533,8 +520,7 @@ static int mei_csi_notify_bound(struct v4l2_async_notifier *notifier,
 	if (pad < 0)
 		return pad;
 
-	csi->remote = subdev;
-	csi->remote_pad = pad;
+	csi->remote = &subdev->entity.pads[pad];
 
 	return media_create_pad_link(&subdev->entity, pad,
 				     &csi->subdev.entity, CSI_PAD_SINK,
@@ -558,28 +544,16 @@ static const struct v4l2_async_notifier_operations mei_csi_notify_ops = {
 
 static int mei_csi_init_controls(struct mei_csi *csi)
 {
-	u32 max;
 	int ret;
 
 	mutex_init(&csi->ctrl_lock);
 
-	ret = v4l2_ctrl_handler_init(&csi->ctrl_handler, 2);
+	ret = v4l2_ctrl_handler_init(&csi->ctrl_handler, 1);
 	if (ret)
 		return ret;
 
 	csi->ctrl_handler.lock = &csi->ctrl_lock;
 
-	max = ARRAY_SIZE(link_freq_menu_items) - 1;
-	csi->freq_ctrl = v4l2_ctrl_new_int_menu(&csi->ctrl_handler,
-						&mei_csi_ctrl_ops,
-						V4L2_CID_LINK_FREQ,
-						max,
-						0,
-						link_freq_menu_items);
-	if (csi->freq_ctrl)
-		csi->freq_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY |
-					 V4L2_CTRL_FLAG_VOLATILE;
-
 	csi->privacy_ctrl = v4l2_ctrl_new_std(&csi->ctrl_handler, NULL,
 					      V4L2_CID_PRIVACY, 0, 1, 1, 0);
 	if (csi->privacy_ctrl)
Signature · 1 line
2.39.5
author maintainer
#17 lore raw

PATCH v8 9/9 media: v4l: Convert the users of v4l2_get_link_freq to call it on a pad

In reply to Sakari Ailus

Call v4l2_get_link_freq() on a pad, instead of a control handler. This way we can soon convert v4l2_get_link_freq() to be callable only on a pad and remove the compatibility code.

---
 drivers/media/i2c/st-mipid02.c                  |  5 +++--
 drivers/media/i2c/tc358746.c                    |  4 +++-
 drivers/media/pci/intel/ipu3/ipu3-cio2.c        | 11 ++++++++---
 drivers/media/platform/cadence/cdns-csi2rx.c    |  4 +++-
 drivers/media/platform/nxp/imx-mipi-csis.c      |  5 +++--
 drivers/media/platform/nxp/imx8mq-mipi-csi2.c   | 11 +++++++++--
 .../media/platform/qcom/camss/camss-vfe-17x.c   |  8 +++++---
 .../media/platform/qcom/camss/camss-vfe-gen1.c  |  8 +++++---
 drivers/media/platform/qcom/camss/camss.c       | 17 +++++++----------
 drivers/media/platform/qcom/camss/camss.h       |  2 +-
 .../media/platform/raspberrypi/rp1-cfe/cfe.c    |  4 +++-
 drivers/media/platform/st/stm32/stm32-csi.c     |  4 +++-
 drivers/media/platform/ti/cal/cal-camerarx.c    |  3 ++-
 drivers/media/platform/ti/cal/cal.c             |  4 +++-
 drivers/media/platform/ti/cal/cal.h             |  1 +
 15 files changed, 59 insertions(+), 32 deletions(-)
drivers/media/i2c/st-mipid02.c +3 -2
index f08db3cfe076..f4568e87f018 100644
@@ -301,8 +301,9 @@ static int mipid02_detect(struct mipid02_dev *bridge)
 static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
 					   struct v4l2_mbus_framefmt *fmt)
 {
+	struct media_pad *remote =
+		&bridge->s_subdev->entity.pads[bridge->s_subdev_pad_id];
 	struct i2c_client *client = bridge->i2c_client;
-	struct v4l2_subdev *subdev = bridge->s_subdev;
 	struct v4l2_fwnode_endpoint *ep = &bridge->rx;
 	u32 bpp = bpp_from_code(fmt->code);
 	/*
@@ -312,7 +313,7 @@ static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
 	u64 ui_4 = 2000000000;
 	s64 link_freq;
 
-	link_freq = v4l2_get_link_freq(subdev->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(remote, bpp,
 				       2 * ep->bus.mipi_csi2.num_data_lanes);
 	if (link_freq < 0) {
 		dev_err(&client->dev, "Failed to get link frequency");
drivers/media/i2c/tc358746.c +3 -1
index 389582420ba7..31586f8e4be4 100644
@@ -896,6 +896,7 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
 	const struct tc358746_format *fmt;
 	unsigned int fifo_sz, tmp, n;
 	struct v4l2_subdev *source;
+	struct media_pad *src_pad;
 	s64 source_link_freq;
 	int err;
 
@@ -910,7 +911,8 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
 	fmt = tc358746_get_format_by_code(TC358746_SINK, mbusfmt->code);
 
 	source = media_entity_to_v4l2_subdev(link->source->entity);
-	source_link_freq = v4l2_get_link_freq(source->ctrl_handler, 0, 0);
+	src_pad = &source->entity.pads[source_fmt->pad];
+	source_link_freq = v4l2_get_link_freq(src_pad, 0, 0);
 	if (source_link_freq <= 0) {
 		dev_err(tc358746->sd.dev,
 			"Failed to query or invalid source link frequency\n");
drivers/media/pci/intel/ipu3/ipu3-cio2.c +8 -3
index 4e98f432ed55..7731eb70fc8e 100644
@@ -309,12 +309,17 @@ static int cio2_csi2_calc_timing(struct cio2_device *cio2, struct cio2_queue *q,
 				 unsigned int bpp, unsigned int lanes)
 {
 	struct device *dev = &cio2->pci_dev->dev;
+	struct media_pad *src_pad;
 	s64 freq;
 
-	if (!q->sensor)
-		return -ENODEV;
+	src_pad = media_entity_remote_source_pad_unique(&q->subdev.entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(dev, "can't get source pad of %s (%ld)\n",
+			q->subdev.name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
 
-	freq = v4l2_get_link_freq(q->sensor->ctrl_handler, bpp, lanes * 2);
+	freq = v4l2_get_link_freq(src_pad, bpp, lanes * 2);
 	if (freq < 0) {
 		dev_err(dev, "error %lld, invalid link_freq\n", freq);
 		return freq;
drivers/media/platform/cadence/cdns-csi2rx.c +3 -1
index 4d64df829e75..cebcae196eec 100644
@@ -164,6 +164,8 @@ static void csi2rx_reset(struct csi2rx_priv *csi2rx)
 
 static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 {
+	struct media_pad *src_pad =
+		&csi2rx->source_subdev->entity.pads[csi2rx->source_pad];
 	union phy_configure_opts opts = { };
 	struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
 	struct v4l2_subdev_format sd_fmt = {
@@ -181,7 +183,7 @@ static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 
 	fmt = csi2rx_get_fmt_by_code(sd_fmt.format.code);
 
-	link_freq = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
 				       fmt->bpp, 2 * csi2rx->num_lanes);
 	if (link_freq < 0)
 		return link_freq;
drivers/media/platform/nxp/imx-mipi-csis.c +3 -2
index 29523bb84d95..d060eadebc7a 100644
@@ -597,12 +597,13 @@ static void __mipi_csis_set_format(struct mipi_csis_device *csis,
 static int mipi_csis_calculate_params(struct mipi_csis_device *csis,
 				      const struct csis_pix_format *csis_fmt)
 {
+	struct media_pad *src_pad =
+		&csis->source.sd->entity.pads[csis->source.pad->index];
 	s64 link_freq;
 	u32 lane_rate;
 
 	/* Calculate the line rate from the pixel rate. */
-	link_freq = v4l2_get_link_freq(csis->source.sd->ctrl_handler,
-				       csis_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csis_fmt->width,
 				       csis->bus.num_data_lanes * 2);
 	if (link_freq < 0) {
 		dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/nxp/imx8mq-mipi-csi2.c +9 -2
index 1f2657cf6e82..a8bcf60e2f37 100644
@@ -287,6 +287,7 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
 					  struct v4l2_subdev_state *sd_state,
 					  u32 *hs_settle)
 {
+	struct media_pad *src_pad;
 	s64 link_freq;
 	u32 lane_rate;
 	unsigned long esc_clk_rate;
@@ -294,13 +295,19 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
 	const struct v4l2_mbus_framefmt *fmt;
 	const struct csi2_pix_format *csi2_fmt;
 
+	src_pad = media_entity_remote_source_pad_unique(&sd_state->sd->entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(state->dev, "can't get source pad of %s (%ld)\n",
+			sd_state->sd->name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
+
 	/* Calculate the line rate from the pixel rate. */
 
 	fmt = v4l2_subdev_state_get_format(sd_state, MIPI_CSI2_PAD_SINK);
 	csi2_fmt = find_csi2_format(fmt->code);
 
-	link_freq = v4l2_get_link_freq(state->src_sd->ctrl_handler,
-				       csi2_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csi2_fmt->width,
 				       state->bus.num_data_lanes * 2);
 	if (link_freq < 0) {
 		dev_err(state->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/qcom/camss/camss-vfe-17x.c +5 -3
index 380c99321030..cc93f79179dd 100644
@@ -443,16 +443,18 @@ static int vfe_enable_output(struct vfe_line *line)
 	struct vfe_device *vfe = to_vfe(line);
 	struct vfe_output *output = &line->output;
 	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	unsigned long flags;
 	unsigned int frame_skip = 0;
 	unsigned int i;
 
 	sensor = camss_find_sensor(&line->subdev.entity);
 	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);
 		/* Max frame skip is 29 frames */
 		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
 			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +5 -3
index eb33c03df27e..1970f7aa6d4d 100644
@@ -170,7 +170,7 @@ static int vfe_enable_output(struct vfe_line *line)
 	struct vfe_device *vfe = to_vfe(line);
 	struct vfe_output *output = &line->output;
 	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	unsigned long flags;
 	unsigned int frame_skip = 0;
 	unsigned int i;
@@ -182,9 +182,11 @@ static int vfe_enable_output(struct vfe_line *line)
 
 	sensor = camss_find_sensor(&line->subdev.entity);
 	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);
 		/* Max frame skip is 29 frames */
 		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
 			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss.c +7 -10
index 004a74f6b2f6..e86de4b59723 100644
@@ -1996,12 +1996,12 @@ void camss_disable_clocks(int nclocks, struct camss_clock *clock)
 }
 
 /*
- * camss_find_sensor - Find a linked media entity which represents a sensor
+ * camss_find_sensor - Find the media pad via which the sensor is linked
  * @entity: Media entity to start searching from
  *
  * Return a pointer to sensor media entity or NULL if not found
  */
-struct media_entity *camss_find_sensor(struct media_entity *entity)
+struct media_pad *camss_find_sensor(struct media_entity *entity)
 {
 	struct media_pad *pad;
 
@@ -2017,7 +2017,7 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
 		entity = pad->entity;
 
 		if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
-			return entity;
+			return pad;
 	}
 }
 
@@ -2032,16 +2032,13 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
 			unsigned int lanes)
 {
-	struct media_entity *sensor;
-	struct v4l2_subdev *subdev;
+	struct media_pad *sensor;
 
 	sensor = camss_find_sensor(entity);
 	if (!sensor)
 		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
-
-	return v4l2_get_link_freq(subdev->ctrl_handler, bpp, 2 * lanes);
+	return v4l2_get_link_freq(sensor, bpp, 2 * lanes);
 }
 
 /*
@@ -2053,7 +2050,7 @@ s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
  */
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 {
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	struct v4l2_subdev *subdev;
 	struct v4l2_ctrl *ctrl;
 
@@ -2061,7 +2058,7 @@ int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 	if (!sensor)
 		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
+	subdev = media_entity_to_v4l2_subdev(sensor->entity);
 
 	ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
 
drivers/media/platform/qcom/camss/camss.h +1 -1
index 9a046eea334f..6da7131c0aaa 100644
@@ -153,7 +153,7 @@ void camss_add_clock_margin(u64 *rate);
 int camss_enable_clocks(int nclocks, struct camss_clock *clock,
 			struct device *dev);
 void camss_disable_clocks(int nclocks, struct camss_clock *clock);
-struct media_entity *camss_find_sensor(struct media_entity *entity);
+struct media_pad *camss_find_sensor(struct media_entity *entity);
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
 			unsigned int lanes);
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock);
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c +3 -1
index 12660087b12f..ed3d18917f2d 100644
@@ -1102,6 +1102,8 @@ static void cfe_buffer_queue(struct vb2_buffer *vb)
 
 static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
 {
+	struct media_pad *src_pad =
+		&cfe->source_sd->entity.pads[cfe->source_pad];
 	struct v4l2_subdev_state *state;
 	s64 link_freq;
 	u32 bpp;
@@ -1136,7 +1138,7 @@ static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
 		bpp = 0;
 	}
 
-	link_freq = v4l2_get_link_freq(cfe->source_sd->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(src_pad, bpp,
 				       2 * cfe->csi2.dphy.active_lanes);
 	if (link_freq < 0)
 		cfe_err(cfe, "failed to get link freq for subdev '%s'\n",
drivers/media/platform/st/stm32/stm32-csi.c +3 -1
index 48941aae8c9b..b25afeee4f24 100644
@@ -444,6 +444,8 @@ static void stm32_csi_phy_reg_write(struct stm32_csi_dev *csidev,
 static int stm32_csi_start(struct stm32_csi_dev *csidev,
 			   struct v4l2_subdev_state *state)
 {
+	struct media_pad *src_pad =
+		&csidev->s_subdev->entity.pads[csidev->s_subdev_pad_nb];
 	const struct stm32_csi_mbps_phy_reg *phy_regs;
 	struct v4l2_mbus_framefmt *sink_fmt;
 	const struct stm32_csi_fmts *fmt;
@@ -465,7 +467,7 @@ static int stm32_csi_start(struct stm32_csi_dev *csidev,
 	if (!csidev->s_subdev)
 		return -EIO;
 
-	link_freq = v4l2_get_link_freq(csidev->s_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
 				       fmt->bpp, 2 * csidev->num_lanes);
 	if (link_freq < 0)
 		return link_freq;
drivers/media/platform/ti/cal/cal-camerarx.c +2 -1
index 42dfe08b765f..9cc875665695 100644
@@ -65,7 +65,8 @@ static s64 cal_camerarx_get_ext_link_freq(struct cal_camerarx *phy)
 
 	bpp = fmtinfo->bpp;
 
-	freq = v4l2_get_link_freq(phy->source->ctrl_handler, bpp, 2 * num_lanes);
+	freq = v4l2_get_link_freq(&phy->source->entity.pads[phy->source_pad],
+				  bpp, 2 * num_lanes);
 	if (freq < 0) {
 		phy_err(phy, "failed to get link freq for subdev '%s'\n",
 			phy->source->name);
drivers/media/platform/ti/cal/cal.c +3 -1
index 4bd2092e0255..6cb3e5f49686 100644
@@ -798,7 +798,6 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
 		return 0;
 	}
 
-	phy->source = subdev;
 	phy_dbg(1, phy, "Using source %s for capture\n", subdev->name);
 
 	pad = media_entity_get_fwnode_pad(&subdev->entity,
@@ -820,6 +819,9 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
 		return ret;
 	}
 
+	phy->source = subdev;
+	phy->source_pad = pad;
+
 	return 0;
 }
 
drivers/media/platform/ti/cal/cal.h +1 -0
index 0856297adc0b..72a246a64d9e 100644
@@ -174,6 +174,7 @@ struct cal_camerarx {
 	struct device_node	*source_ep_node;
 	struct device_node	*source_node;
 	struct v4l2_subdev	*source;
+	unsigned int		source_pad;
 
 	struct v4l2_subdev	subdev;
 	struct media_pad	pads[CAL_CAMERARX_NUM_PADS];
Signature · 1 line
2.39.5
author maintainer
#18 lore raw

PATCH RESEND v8 9/9 media: v4l: Convert the users of v4l2_get_link_freq to call it on a pad

Call v4l2_get_link_freq() on a pad, instead of a control handler. This way we can soon convert v4l2_get_link_freq() to be callable only on a pad and remove the compatibility code.

---
Included driver maintainers and reviewers.

The full set is here <URL:https://lore.kernel.org/linux-media/[email protected]/T/#t>.

 drivers/media/i2c/st-mipid02.c                  |  5 +++--
 drivers/media/i2c/tc358746.c                    |  4 +++-
 drivers/media/pci/intel/ipu3/ipu3-cio2.c        | 11 ++++++++---
 drivers/media/platform/cadence/cdns-csi2rx.c    |  4 +++-
 drivers/media/platform/nxp/imx-mipi-csis.c      |  5 +++--
 drivers/media/platform/nxp/imx8mq-mipi-csi2.c   | 11 +++++++++--
 .../media/platform/qcom/camss/camss-vfe-17x.c   |  8 +++++---
 .../media/platform/qcom/camss/camss-vfe-gen1.c  |  8 +++++---
 drivers/media/platform/qcom/camss/camss.c       | 17 +++++++----------
 drivers/media/platform/qcom/camss/camss.h       |  2 +-
 .../media/platform/raspberrypi/rp1-cfe/cfe.c    |  4 +++-
 drivers/media/platform/st/stm32/stm32-csi.c     |  4 +++-
 drivers/media/platform/ti/cal/cal-camerarx.c    |  3 ++-
 drivers/media/platform/ti/cal/cal.c             |  4 +++-
 drivers/media/platform/ti/cal/cal.h             |  1 +
 15 files changed, 59 insertions(+), 32 deletions(-)
drivers/media/i2c/st-mipid02.c +3 -2
index f08db3cfe076..f4568e87f018 100644
@@ -301,8 +301,9 @@ static int mipid02_detect(struct mipid02_dev *bridge)
 static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
 					   struct v4l2_mbus_framefmt *fmt)
 {
+	struct media_pad *remote =
+		&bridge->s_subdev->entity.pads[bridge->s_subdev_pad_id];
 	struct i2c_client *client = bridge->i2c_client;
-	struct v4l2_subdev *subdev = bridge->s_subdev;
 	struct v4l2_fwnode_endpoint *ep = &bridge->rx;
 	u32 bpp = bpp_from_code(fmt->code);
 	/*
@@ -312,7 +313,7 @@ static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
 	u64 ui_4 = 2000000000;
 	s64 link_freq;
 
-	link_freq = v4l2_get_link_freq(subdev->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(remote, bpp,
 				       2 * ep->bus.mipi_csi2.num_data_lanes);
 	if (link_freq < 0) {
 		dev_err(&client->dev, "Failed to get link frequency");
drivers/media/i2c/tc358746.c +3 -1
index 389582420ba7..31586f8e4be4 100644
@@ -896,6 +896,7 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
 	const struct tc358746_format *fmt;
 	unsigned int fifo_sz, tmp, n;
 	struct v4l2_subdev *source;
+	struct media_pad *src_pad;
 	s64 source_link_freq;
 	int err;
 
@@ -910,7 +911,8 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
 	fmt = tc358746_get_format_by_code(TC358746_SINK, mbusfmt->code);
 
 	source = media_entity_to_v4l2_subdev(link->source->entity);
-	source_link_freq = v4l2_get_link_freq(source->ctrl_handler, 0, 0);
+	src_pad = &source->entity.pads[source_fmt->pad];
+	source_link_freq = v4l2_get_link_freq(src_pad, 0, 0);
 	if (source_link_freq <= 0) {
 		dev_err(tc358746->sd.dev,
 			"Failed to query or invalid source link frequency\n");
drivers/media/pci/intel/ipu3/ipu3-cio2.c +8 -3
index 4e98f432ed55..7731eb70fc8e 100644
@@ -309,12 +309,17 @@ static int cio2_csi2_calc_timing(struct cio2_device *cio2, struct cio2_queue *q,
 				 unsigned int bpp, unsigned int lanes)
 {
 	struct device *dev = &cio2->pci_dev->dev;
+	struct media_pad *src_pad;
 	s64 freq;
 
-	if (!q->sensor)
-		return -ENODEV;
+	src_pad = media_entity_remote_source_pad_unique(&q->subdev.entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(dev, "can't get source pad of %s (%ld)\n",
+			q->subdev.name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
 
-	freq = v4l2_get_link_freq(q->sensor->ctrl_handler, bpp, lanes * 2);
+	freq = v4l2_get_link_freq(src_pad, bpp, lanes * 2);
 	if (freq < 0) {
 		dev_err(dev, "error %lld, invalid link_freq\n", freq);
 		return freq;
drivers/media/platform/cadence/cdns-csi2rx.c +3 -1
index 4d64df829e75..cebcae196eec 100644
@@ -164,6 +164,8 @@ static void csi2rx_reset(struct csi2rx_priv *csi2rx)
 
 static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 {
+	struct media_pad *src_pad =
+		&csi2rx->source_subdev->entity.pads[csi2rx->source_pad];
 	union phy_configure_opts opts = { };
 	struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
 	struct v4l2_subdev_format sd_fmt = {
@@ -181,7 +183,7 @@ static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 
 	fmt = csi2rx_get_fmt_by_code(sd_fmt.format.code);
 
-	link_freq = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
 				       fmt->bpp, 2 * csi2rx->num_lanes);
 	if (link_freq < 0)
 		return link_freq;
drivers/media/platform/nxp/imx-mipi-csis.c +3 -2
index 29523bb84d95..d060eadebc7a 100644
@@ -597,12 +597,13 @@ static void __mipi_csis_set_format(struct mipi_csis_device *csis,
 static int mipi_csis_calculate_params(struct mipi_csis_device *csis,
 				      const struct csis_pix_format *csis_fmt)
 {
+	struct media_pad *src_pad =
+		&csis->source.sd->entity.pads[csis->source.pad->index];
 	s64 link_freq;
 	u32 lane_rate;
 
 	/* Calculate the line rate from the pixel rate. */
-	link_freq = v4l2_get_link_freq(csis->source.sd->ctrl_handler,
-				       csis_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csis_fmt->width,
 				       csis->bus.num_data_lanes * 2);
 	if (link_freq < 0) {
 		dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/nxp/imx8mq-mipi-csi2.c +9 -2
index 1f2657cf6e82..a8bcf60e2f37 100644
@@ -287,6 +287,7 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
 					  struct v4l2_subdev_state *sd_state,
 					  u32 *hs_settle)
 {
+	struct media_pad *src_pad;
 	s64 link_freq;
 	u32 lane_rate;
 	unsigned long esc_clk_rate;
@@ -294,13 +295,19 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
 	const struct v4l2_mbus_framefmt *fmt;
 	const struct csi2_pix_format *csi2_fmt;
 
+	src_pad = media_entity_remote_source_pad_unique(&sd_state->sd->entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(state->dev, "can't get source pad of %s (%ld)\n",
+			sd_state->sd->name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
+
 	/* Calculate the line rate from the pixel rate. */
 
 	fmt = v4l2_subdev_state_get_format(sd_state, MIPI_CSI2_PAD_SINK);
 	csi2_fmt = find_csi2_format(fmt->code);
 
-	link_freq = v4l2_get_link_freq(state->src_sd->ctrl_handler,
-				       csi2_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csi2_fmt->width,
 				       state->bus.num_data_lanes * 2);
 	if (link_freq < 0) {
 		dev_err(state->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/qcom/camss/camss-vfe-17x.c +5 -3
index 380c99321030..cc93f79179dd 100644
@@ -443,16 +443,18 @@ static int vfe_enable_output(struct vfe_line *line)
 	struct vfe_device *vfe = to_vfe(line);
 	struct vfe_output *output = &line->output;
 	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	unsigned long flags;
 	unsigned int frame_skip = 0;
 	unsigned int i;
 
 	sensor = camss_find_sensor(&line->subdev.entity);
 	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);
 		/* Max frame skip is 29 frames */
 		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
 			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +5 -3
index eb33c03df27e..1970f7aa6d4d 100644
@@ -170,7 +170,7 @@ static int vfe_enable_output(struct vfe_line *line)
 	struct vfe_device *vfe = to_vfe(line);
 	struct vfe_output *output = &line->output;
 	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	unsigned long flags;
 	unsigned int frame_skip = 0;
 	unsigned int i;
@@ -182,9 +182,11 @@ static int vfe_enable_output(struct vfe_line *line)
 
 	sensor = camss_find_sensor(&line->subdev.entity);
 	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);
 		/* Max frame skip is 29 frames */
 		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
 			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss.c +7 -10
index 004a74f6b2f6..e86de4b59723 100644
@@ -1996,12 +1996,12 @@ void camss_disable_clocks(int nclocks, struct camss_clock *clock)
 }
 
 /*
- * camss_find_sensor - Find a linked media entity which represents a sensor
+ * camss_find_sensor - Find the media pad via which the sensor is linked
  * @entity: Media entity to start searching from
  *
  * Return a pointer to sensor media entity or NULL if not found
  */
-struct media_entity *camss_find_sensor(struct media_entity *entity)
+struct media_pad *camss_find_sensor(struct media_entity *entity)
 {
 	struct media_pad *pad;
 
@@ -2017,7 +2017,7 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
 		entity = pad->entity;
 
 		if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
-			return entity;
+			return pad;
 	}
 }
 
@@ -2032,16 +2032,13 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
 			unsigned int lanes)
 {
-	struct media_entity *sensor;
-	struct v4l2_subdev *subdev;
+	struct media_pad *sensor;
 
 	sensor = camss_find_sensor(entity);
 	if (!sensor)
 		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
-
-	return v4l2_get_link_freq(subdev->ctrl_handler, bpp, 2 * lanes);
+	return v4l2_get_link_freq(sensor, bpp, 2 * lanes);
 }
 
 /*
@@ -2053,7 +2050,7 @@ s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
  */
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 {
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	struct v4l2_subdev *subdev;
 	struct v4l2_ctrl *ctrl;
 
@@ -2061,7 +2058,7 @@ int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 	if (!sensor)
 		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
+	subdev = media_entity_to_v4l2_subdev(sensor->entity);
 
 	ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
 
drivers/media/platform/qcom/camss/camss.h +1 -1
index 9a046eea334f..6da7131c0aaa 100644
@@ -153,7 +153,7 @@ void camss_add_clock_margin(u64 *rate);
 int camss_enable_clocks(int nclocks, struct camss_clock *clock,
 			struct device *dev);
 void camss_disable_clocks(int nclocks, struct camss_clock *clock);
-struct media_entity *camss_find_sensor(struct media_entity *entity);
+struct media_pad *camss_find_sensor(struct media_entity *entity);
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
 			unsigned int lanes);
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock);
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c +3 -1
index 12660087b12f..ed3d18917f2d 100644
@@ -1102,6 +1102,8 @@ static void cfe_buffer_queue(struct vb2_buffer *vb)
 
 static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
 {
+	struct media_pad *src_pad =
+		&cfe->source_sd->entity.pads[cfe->source_pad];
 	struct v4l2_subdev_state *state;
 	s64 link_freq;
 	u32 bpp;
@@ -1136,7 +1138,7 @@ static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
 		bpp = 0;
 	}
 
-	link_freq = v4l2_get_link_freq(cfe->source_sd->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(src_pad, bpp,
 				       2 * cfe->csi2.dphy.active_lanes);
 	if (link_freq < 0)
 		cfe_err(cfe, "failed to get link freq for subdev '%s'\n",
drivers/media/platform/st/stm32/stm32-csi.c +3 -1
index 48941aae8c9b..b25afeee4f24 100644
@@ -444,6 +444,8 @@ static void stm32_csi_phy_reg_write(struct stm32_csi_dev *csidev,
 static int stm32_csi_start(struct stm32_csi_dev *csidev,
 			   struct v4l2_subdev_state *state)
 {
+	struct media_pad *src_pad =
+		&csidev->s_subdev->entity.pads[csidev->s_subdev_pad_nb];
 	const struct stm32_csi_mbps_phy_reg *phy_regs;
 	struct v4l2_mbus_framefmt *sink_fmt;
 	const struct stm32_csi_fmts *fmt;
@@ -465,7 +467,7 @@ static int stm32_csi_start(struct stm32_csi_dev *csidev,
 	if (!csidev->s_subdev)
 		return -EIO;
 
-	link_freq = v4l2_get_link_freq(csidev->s_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
 				       fmt->bpp, 2 * csidev->num_lanes);
 	if (link_freq < 0)
 		return link_freq;
drivers/media/platform/ti/cal/cal-camerarx.c +2 -1
index 42dfe08b765f..9cc875665695 100644
@@ -65,7 +65,8 @@ static s64 cal_camerarx_get_ext_link_freq(struct cal_camerarx *phy)
 
 	bpp = fmtinfo->bpp;
 
-	freq = v4l2_get_link_freq(phy->source->ctrl_handler, bpp, 2 * num_lanes);
+	freq = v4l2_get_link_freq(&phy->source->entity.pads[phy->source_pad],
+				  bpp, 2 * num_lanes);
 	if (freq < 0) {
 		phy_err(phy, "failed to get link freq for subdev '%s'\n",
 			phy->source->name);
drivers/media/platform/ti/cal/cal.c +3 -1
index 4bd2092e0255..6cb3e5f49686 100644
@@ -798,7 +798,6 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
 		return 0;
 	}
 
-	phy->source = subdev;
 	phy_dbg(1, phy, "Using source %s for capture\n", subdev->name);
 
 	pad = media_entity_get_fwnode_pad(&subdev->entity,
@@ -820,6 +819,9 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
 		return ret;
 	}
 
+	phy->source = subdev;
+	phy->source_pad = pad;
+
 	return 0;
 }
 
drivers/media/platform/ti/cal/cal.h +1 -0
index 0856297adc0b..72a246a64d9e 100644
@@ -174,6 +174,7 @@ struct cal_camerarx {
 	struct device_node	*source_ep_node;
 	struct device_node	*source_node;
 	struct v4l2_subdev	*source;
+	unsigned int		source_pad;
 
 	struct v4l2_subdev	subdev;
 	struct media_pad	pads[CAL_CAMERARX_NUM_PADS];
Signature · 1 line
2.39.5
Acked-by on v8 9/9
#19 lore raw

Re: [RESEND PATCH v8 9/9] media: v4l: Convert the users of v4l2_get_link_freq to call it on a pad

Hi Sakari,

Thank you for your patch.

Quoted 6 lines · Sakari Ailus

Call v4l2_get_link_freq() on a pad, instead of a control handler. This way we can soon convert v4l2_get_link_freq() to be callable only on a pad and remove the compatibility code.

The change to rp1-cfe looks reasonable:

Quoted 396 lines

---
Included driver maintainers and reviewers.

The full set is here <URL:https://lore.kernel.org/linux-media/[email protected]/T/#t>.

 drivers/media/i2c/st-mipid02.c                  |  5 +++--
 drivers/media/i2c/tc358746.c                    |  4 +++-
 drivers/media/pci/intel/ipu3/ipu3-cio2.c        | 11 ++++++++---
 drivers/media/platform/cadence/cdns-csi2rx.c    |  4 +++-
 drivers/media/platform/nxp/imx-mipi-csis.c      |  5 +++--
 drivers/media/platform/nxp/imx8mq-mipi-csi2.c   | 11 +++++++++--
 .../media/platform/qcom/camss/camss-vfe-17x.c   |  8 +++++---
 .../media/platform/qcom/camss/camss-vfe-gen1.c  |  8 +++++---
 drivers/media/platform/qcom/camss/camss.c       | 17 +++++++----------
 drivers/media/platform/qcom/camss/camss.h       |  2 +-
 .../media/platform/raspberrypi/rp1-cfe/cfe.c    |  4 +++-
 drivers/media/platform/st/stm32/stm32-csi.c     |  4 +++-
 drivers/media/platform/ti/cal/cal-camerarx.c    |  3 ++-
 drivers/media/platform/ti/cal/cal.c             |  4 +++-
 drivers/media/platform/ti/cal/cal.h             |  1 +
 15 files changed, 59 insertions(+), 32 deletions(-)
drivers/media/i2c/st-mipid02.c +3 -2
index f08db3cfe076..f4568e87f018 100644
@@ -301,8 +301,9 @@ static int mipid02_detect(struct mipid02_dev *bridge)
 static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
                                           struct v4l2_mbus_framefmt *fmt)
 {
+       struct media_pad *remote =
+               &bridge->s_subdev->entity.pads[bridge->s_subdev_pad_id];
        struct i2c_client *client = bridge->i2c_client;
-       struct v4l2_subdev *subdev = bridge->s_subdev;
        struct v4l2_fwnode_endpoint *ep = &bridge->rx;
        u32 bpp = bpp_from_code(fmt->code);
        /*
@@ -312,7 +313,7 @@ static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
        u64 ui_4 = 2000000000;
        s64 link_freq;
 
-       link_freq = v4l2_get_link_freq(subdev->ctrl_handler, bpp,
+       link_freq = v4l2_get_link_freq(remote, bpp,
                                       2 * ep->bus.mipi_csi2.num_data_lanes);
        if (link_freq < 0) {
                dev_err(&client->dev, "Failed to get link frequency");
drivers/media/i2c/tc358746.c +3 -1
index 389582420ba7..31586f8e4be4 100644
@@ -896,6 +896,7 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
        const struct tc358746_format *fmt;
        unsigned int fifo_sz, tmp, n;
        struct v4l2_subdev *source;
+       struct media_pad *src_pad;
        s64 source_link_freq;
        int err;
 
@@ -910,7 +911,8 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
        fmt = tc358746_get_format_by_code(TC358746_SINK, mbusfmt->code);
 
        source = media_entity_to_v4l2_subdev(link->source->entity);
-       source_link_freq = v4l2_get_link_freq(source->ctrl_handler, 0, 0);
+       src_pad = &source->entity.pads[source_fmt->pad];
+       source_link_freq = v4l2_get_link_freq(src_pad, 0, 0);
        if (source_link_freq <= 0) {
                dev_err(tc358746->sd.dev,
                        "Failed to query or invalid source link frequency\n");
drivers/media/pci/intel/ipu3/ipu3-cio2.c +8 -3
index 4e98f432ed55..7731eb70fc8e 100644
@@ -309,12 +309,17 @@ static int cio2_csi2_calc_timing(struct cio2_device *cio2, struct cio2_queue *q,
                                 unsigned int bpp, unsigned int lanes)
 {
        struct device *dev = &cio2->pci_dev->dev;
+       struct media_pad *src_pad;
        s64 freq;
 
-       if (!q->sensor)
-               return -ENODEV;
+       src_pad = media_entity_remote_source_pad_unique(&q->subdev.entity);
+       if (IS_ERR(src_pad)) {
+               dev_err(dev, "can't get source pad of %s (%ld)\n",
+                       q->subdev.name, PTR_ERR(src_pad));
+               return PTR_ERR(src_pad);
+       }
 
-       freq = v4l2_get_link_freq(q->sensor->ctrl_handler, bpp, lanes * 2);
+       freq = v4l2_get_link_freq(src_pad, bpp, lanes * 2);
        if (freq < 0) {
                dev_err(dev, "error %lld, invalid link_freq\n", freq);
                return freq;
drivers/media/platform/cadence/cdns-csi2rx.c +3 -1
index 4d64df829e75..cebcae196eec 100644
@@ -164,6 +164,8 @@ static void csi2rx_reset(struct csi2rx_priv *csi2rx)
 
 static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 {
+       struct media_pad *src_pad =
+               &csi2rx->source_subdev->entity.pads[csi2rx->source_pad];
        union phy_configure_opts opts = { };
        struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
        struct v4l2_subdev_format sd_fmt = {
@@ -181,7 +183,7 @@ static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 
        fmt = csi2rx_get_fmt_by_code(sd_fmt.format.code);
 
-       link_freq = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
+       link_freq = v4l2_get_link_freq(src_pad,
                                       fmt->bpp, 2 * csi2rx->num_lanes);
        if (link_freq < 0)
                return link_freq;
drivers/media/platform/nxp/imx-mipi-csis.c +3 -2
index 29523bb84d95..d060eadebc7a 100644
@@ -597,12 +597,13 @@ static void __mipi_csis_set_format(struct mipi_csis_device *csis,
 static int mipi_csis_calculate_params(struct mipi_csis_device *csis,
                                      const struct csis_pix_format *csis_fmt)
 {
+       struct media_pad *src_pad =
+               &csis->source.sd->entity.pads[csis->source.pad->index];
        s64 link_freq;
        u32 lane_rate;
 
        /* Calculate the line rate from the pixel rate. */
-       link_freq = v4l2_get_link_freq(csis->source.sd->ctrl_handler,
-                                      csis_fmt->width,
+       link_freq = v4l2_get_link_freq(src_pad, csis_fmt->width,
                                       csis->bus.num_data_lanes * 2);
        if (link_freq < 0) {
                dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/nxp/imx8mq-mipi-csi2.c +9 -2
index 1f2657cf6e82..a8bcf60e2f37 100644
@@ -287,6 +287,7 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
                                          struct v4l2_subdev_state *sd_state,
                                          u32 *hs_settle)
 {
+       struct media_pad *src_pad;
        s64 link_freq;
        u32 lane_rate;
        unsigned long esc_clk_rate;
@@ -294,13 +295,19 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
        const struct v4l2_mbus_framefmt *fmt;
        const struct csi2_pix_format *csi2_fmt;
 
+       src_pad = media_entity_remote_source_pad_unique(&sd_state->sd->entity);
+       if (IS_ERR(src_pad)) {
+               dev_err(state->dev, "can't get source pad of %s (%ld)\n",
+                       sd_state->sd->name, PTR_ERR(src_pad));
+               return PTR_ERR(src_pad);
+       }
+
        /* Calculate the line rate from the pixel rate. */
 
        fmt = v4l2_subdev_state_get_format(sd_state, MIPI_CSI2_PAD_SINK);
        csi2_fmt = find_csi2_format(fmt->code);
 
-       link_freq = v4l2_get_link_freq(state->src_sd->ctrl_handler,
-                                      csi2_fmt->width,
+       link_freq = v4l2_get_link_freq(src_pad, csi2_fmt->width,
                                       state->bus.num_data_lanes * 2);
        if (link_freq < 0) {
                dev_err(state->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/qcom/camss/camss-vfe-17x.c +5 -3
index 380c99321030..cc93f79179dd 100644
@@ -443,16 +443,18 @@ static int vfe_enable_output(struct vfe_line *line)
        struct vfe_device *vfe = to_vfe(line);
        struct vfe_output *output = &line->output;
        const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-       struct media_entity *sensor;
+       struct media_pad *sensor;
        unsigned long flags;
        unsigned int frame_skip = 0;
        unsigned int i;
 
        sensor = camss_find_sensor(&line->subdev.entity);
        if (sensor) {
-               struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+               struct v4l2_subdev *subdev =
+                       media_entity_to_v4l2_subdev(sensor->entity);
 
-               v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+               v4l2_subdev_call(subdev, sensor, g_skip_frames,
+                                &frame_skip);
                /* Max frame skip is 29 frames */
                if (frame_skip > VFE_FRAME_DROP_VAL - 1)
                        frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +5 -3
index eb33c03df27e..1970f7aa6d4d 100644
@@ -170,7 +170,7 @@ static int vfe_enable_output(struct vfe_line *line)
        struct vfe_device *vfe = to_vfe(line);
        struct vfe_output *output = &line->output;
        const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-       struct media_entity *sensor;
+       struct media_pad *sensor;
        unsigned long flags;
        unsigned int frame_skip = 0;
        unsigned int i;
@@ -182,9 +182,11 @@ static int vfe_enable_output(struct vfe_line *line)
 
        sensor = camss_find_sensor(&line->subdev.entity);
        if (sensor) {
-               struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+               struct v4l2_subdev *subdev =
+                       media_entity_to_v4l2_subdev(sensor->entity);
 
-               v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+               v4l2_subdev_call(subdev, sensor, g_skip_frames,
+                                &frame_skip);
                /* Max frame skip is 29 frames */
                if (frame_skip > VFE_FRAME_DROP_VAL - 1)
                        frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss.c +7 -10
index 004a74f6b2f6..e86de4b59723 100644
@@ -1996,12 +1996,12 @@ void camss_disable_clocks(int nclocks, struct camss_clock *clock)
 }
 
 /*
- * camss_find_sensor - Find a linked media entity which represents a sensor
+ * camss_find_sensor - Find the media pad via which the sensor is linked
  * @entity: Media entity to start searching from
  *
  * Return a pointer to sensor media entity or NULL if not found
  */
-struct media_entity *camss_find_sensor(struct media_entity *entity)
+struct media_pad *camss_find_sensor(struct media_entity *entity)
 {
        struct media_pad *pad;
 
@@ -2017,7 +2017,7 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
                entity = pad->entity;
 
                if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
-                       return entity;
+                       return pad;
        }
 }
 
@@ -2032,16 +2032,13 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
                        unsigned int lanes)
 {
-       struct media_entity *sensor;
-       struct v4l2_subdev *subdev;
+       struct media_pad *sensor;
 
        sensor = camss_find_sensor(entity);
        if (!sensor)
                return -ENODEV;
 
-       subdev = media_entity_to_v4l2_subdev(sensor);
-
-       return v4l2_get_link_freq(subdev->ctrl_handler, bpp, 2 * lanes);
+       return v4l2_get_link_freq(sensor, bpp, 2 * lanes);
 }
 
 /*
@@ -2053,7 +2050,7 @@ s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
  */
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 {
-       struct media_entity *sensor;
+       struct media_pad *sensor;
        struct v4l2_subdev *subdev;
        struct v4l2_ctrl *ctrl;
 
@@ -2061,7 +2058,7 @@ int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
        if (!sensor)
                return -ENODEV;
 
-       subdev = media_entity_to_v4l2_subdev(sensor);
+       subdev = media_entity_to_v4l2_subdev(sensor->entity);
 
        ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
 
drivers/media/platform/qcom/camss/camss.h +1 -1
index 9a046eea334f..6da7131c0aaa 100644
@@ -153,7 +153,7 @@ void camss_add_clock_margin(u64 *rate);
 int camss_enable_clocks(int nclocks, struct camss_clock *clock,
                        struct device *dev);
 void camss_disable_clocks(int nclocks, struct camss_clock *clock);
-struct media_entity *camss_find_sensor(struct media_entity *entity);
+struct media_pad *camss_find_sensor(struct media_entity *entity);
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
                        unsigned int lanes);
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock);
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c +3 -1
index 12660087b12f..ed3d18917f2d 100644
@@ -1102,6 +1102,8 @@ static void cfe_buffer_queue(struct vb2_buffer *vb)
 
 static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
 {
+       struct media_pad *src_pad =
+               &cfe->source_sd->entity.pads[cfe->source_pad];
        struct v4l2_subdev_state *state;
        s64 link_freq;
        u32 bpp;
@@ -1136,7 +1138,7 @@ static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
                bpp = 0;
        }
 
-       link_freq = v4l2_get_link_freq(cfe->source_sd->ctrl_handler, bpp,
+       link_freq = v4l2_get_link_freq(src_pad, bpp,
                                       2 * cfe->csi2.dphy.active_lanes);
        if (link_freq < 0)
                cfe_err(cfe, "failed to get link freq for subdev '%s'\n",
drivers/media/platform/st/stm32/stm32-csi.c +3 -1
index 48941aae8c9b..b25afeee4f24 100644
@@ -444,6 +444,8 @@ static void stm32_csi_phy_reg_write(struct stm32_csi_dev *csidev,
 static int stm32_csi_start(struct stm32_csi_dev *csidev,
                           struct v4l2_subdev_state *state)
 {
+       struct media_pad *src_pad =
+               &csidev->s_subdev->entity.pads[csidev->s_subdev_pad_nb];
        const struct stm32_csi_mbps_phy_reg *phy_regs;
        struct v4l2_mbus_framefmt *sink_fmt;
        const struct stm32_csi_fmts *fmt;
@@ -465,7 +467,7 @@ static int stm32_csi_start(struct stm32_csi_dev *csidev,
        if (!csidev->s_subdev)
                return -EIO;
 
-       link_freq = v4l2_get_link_freq(csidev->s_subdev->ctrl_handler,
+       link_freq = v4l2_get_link_freq(src_pad,
                                       fmt->bpp, 2 * csidev->num_lanes);
        if (link_freq < 0)
                return link_freq;
drivers/media/platform/ti/cal/cal-camerarx.c +2 -1
index 42dfe08b765f..9cc875665695 100644
@@ -65,7 +65,8 @@ static s64 cal_camerarx_get_ext_link_freq(struct cal_camerarx *phy)
 
        bpp = fmtinfo->bpp;
 
-       freq = v4l2_get_link_freq(phy->source->ctrl_handler, bpp, 2 * num_lanes);
+       freq = v4l2_get_link_freq(&phy->source->entity.pads[phy->source_pad],
+                                 bpp, 2 * num_lanes);
        if (freq < 0) {
                phy_err(phy, "failed to get link freq for subdev '%s'\n",
                        phy->source->name);
drivers/media/platform/ti/cal/cal.c +3 -1
index 4bd2092e0255..6cb3e5f49686 100644
@@ -798,7 +798,6 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
                return 0;
        }
 
-       phy->source = subdev;
        phy_dbg(1, phy, "Using source %s for capture\n", subdev->name);
 
        pad = media_entity_get_fwnode_pad(&subdev->entity,
@@ -820,6 +819,9 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
                return ret;
        }
 
+       phy->source = subdev;
+       phy->source_pad = pad;
+
        return 0;
 }
 
drivers/media/platform/ti/cal/cal.h +1 -1
index 0856297adc0b..72a246a64d9e 100644
@@ -174,6 +174,7 @@ struct cal_camerarx {
        struct device_node      *source_ep_node;
        struct device_node      *source_node;
        struct v4l2_subdev      *source;
+       unsigned int            source_pad;
 
        struct v4l2_subdev      subdev;
        struct media_pad        pads[CAL_CAMERARX_NUM_PADS];
--

2.39.5

maintainer
#5 lore raw

Re: [PATCH v8 2/9] media: v4l: Support obtaining link frequency via get_mbus_config

In reply to Sakari Ailus

Hi,

Quoted 52 lines · Sakari Ailus

Add link_freq field to struct v4l2_mbus_config in order to pass the link frequency to the receiving sub-device.

---
  drivers/media/v4l2-core/v4l2-common.c | 15 +++++++++++++--
  include/media/v4l2-mediabus.h         |  2 ++
  2 files changed, 15 insertions(+), 2 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +13 -2
index 9fe74c7e064f..e4b2de3833ee 100644
@@ -508,12 +508,23 @@ EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_ctrl);
  s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
  			     unsigned int div)
  {
+	struct v4l2_mbus_config mbus_config = {};
  	struct v4l2_subdev *sd;
+	int ret;
 
  	sd = media_entity_to_v4l2_subdev(pad->entity);
-	if (!sd)
-		return -ENODEV;
+	ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad->index,
+			       &mbus_config);
+	if (ret < 0 && ret != -ENOIOCTLCMD)
+		return ret;
+
+	if (mbus_config.link_freq)
+		return mbus_config.link_freq;
 
+	/*
+	 * Fall back to using the link frequency control if the media bus config
+	 * doesn't provide a link frequency.
+	 */
  	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
  }
  EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
include/media/v4l2-mediabus.h +1 -0
index e7f019f68c8d..24c738cd7894 100644
@@ -169,6 +169,7 @@ enum v4l2_mbus_type {
  /**
   * struct v4l2_mbus_config - media bus configuration
   * @type: interface type
+ * @link_freq: The link frequency. See also V4L2_CID_LINK_FREQ control.
   * @bus: bus configuration data structure
   * @bus.parallel: embedded &struct v4l2_mbus_config_parallel.
   *		  Used if the bus is parallel or BT.656.

include/media/v4l2-mediabus.h enum v4l2_mbus_type { · line 190

Quoted 7 lines
include/media/v4l2-mediabus.h +1 -0
@@ -183,6 +184,7 @@ enum v4l2_mbus_type {
   */
  struct v4l2_mbus_config {
  	enum v4l2_mbus_type type;
+	u64 link_freq;
  	union {
  		struct v4l2_mbus_config_parallel parallel;
  		struct v4l2_mbus_config_mipi_csi1 mipi_csi1;

Maybe the docs (V4L2_CID_LINK_FREQ and/or struct v4l2_mbus_config?) should mention that only if v4l2_mbus_config.link_freq is == 0, V4L2_CID_LINK_FREQ is used. Or, in other words, the driver must only use one of those methods.

Tomi

maintainer
#2 lore raw

Re: [PATCH v8 1/9] media: v4l: Support passing media pad argument to v4l2_get_link_freq()

In reply to Sakari Ailus

Hi,

Quoted 66 lines · Sakari Ailus

v4l2_get_link_freq() accepts a V4L2 control handler for now, but it needs to take struct media_pad argument in order to obtain the link frequency using get_mbus_config() pad op. Prepare for this by allowing struct media_pad as well.

---
  drivers/media/v4l2-core/v4l2-common.c | 21 ++++++++++++++++++---
  include/media/v4l2-common.h           | 19 ++++++++++++++++---
  2 files changed, 34 insertions(+), 6 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +18 -3
index 0a2f4f0d0a07..9fe74c7e064f 100644
@@ -466,8 +466,8 @@ int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
  }
  EXPORT_SYMBOL_GPL(v4l2_fill_pixfmt);
 
-s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
-		       unsigned int div)
+s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,
+			      unsigned int mul, unsigned int div)
  {
  	struct v4l2_ctrl *ctrl;
  	s64 freq;
@@ -502,7 +502,22 @@ s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
 
  	return freq > 0 ? freq : -EINVAL;
  }
-EXPORT_SYMBOL_GPL(v4l2_get_link_freq);
+EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_ctrl);
+
+#ifdef CONFIG_MEDIA_CONTROLLER
+s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
+			     unsigned int div)
+{
+	struct v4l2_subdev *sd;
+
+	sd = media_entity_to_v4l2_subdev(pad->entity);
+	if (!sd)
+		return -ENODEV;
+
+	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
+}
+EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
+#endif /* CONFIG_MEDIA_CONTROLLER */
 
  /*
   * Simplify a fraction using a simple continued fraction decomposition. The
include/media/v4l2-common.h +2 -1
index 63ad36f04f72..fda903bb3674 100644
@@ -525,7 +525,8 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
  /**
   * v4l2_get_link_freq - Get link rate from transmitter
   *
- * @handler: The transmitter's control handler
+ * @pad: The transmitter's media pad (or control handler for non-MC users or
+ *	 compatibility reasons, don't use in new code)
   * @mul: The multiplier between pixel rate and link frequency. Bits per pixel on
   *	 D-PHY, samples per clock on parallel. 0 otherwise.
   * @div: The divisor between pixel rate and link frequency. Number of data lanes
@@ -541,8 +542,20 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
   * * %-ENOENT: Link frequency or pixel rate control not found

include/media/v4l2-common.h int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat, · line 550

Quoted 10 lines
include/media/v4l2-common.h +6 -2
   * * %-EINVAL: Invalid link frequency value
   */
-s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
-		       unsigned int div);
+#ifdef CONFIG_MEDIA_CONTROLLER
+#define v4l2_get_link_freq(pad, mul, div)				\
+	_Generic(pad,							\
+		 struct media_pad *: __v4l2_get_link_freq_pad,		\
+		 struct v4l2_ctrl_handler *: __v4l2_get_link_freq_ctrl)	\
+	(pad, mul, div)

I can't decide if I love or hate _Generic =).

include/media/v4l2-common.h int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat, · line 561

Quoted 11 lines
include/media/v4l2-common.h +8 -0
+s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
+			     unsigned int div);
+#else
+#define v4l2_get_link_freq(handler, mul, div)		\
+	__v4l2_get_link_freq_ctrl(handler, mul, div)
+#endif
+s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,
+			      unsigned int mul, unsigned int div);
 
  void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
  		unsigned int n_terms, unsigned int threshold);

This makes sense in the transition period. But I wonder, should we later have:

v4l2_get_link_freq(struct v4l2_subdev *sd, unsigned int pad, ...) like all the subdev ops? Perhaps not... Subdev ops take subdev as the first argument as, well, they're subdev ops.

It did look to me that in many drivers we already have the source subdev and pad stored somewhere, and getting the media_pad to call v4l2_get_link_freq() is an extra step.

Tomi

maintainer
#21 lore raw

Re: [PATCH v8 9/9] media: v4l: Convert the users of v4l2_get_link_freq to call it on a pad

In reply to Sakari Ailus

Hi,

Quoted 183 lines · Sakari Ailus

Call v4l2_get_link_freq() on a pad, instead of a control handler. This way we can soon convert v4l2_get_link_freq() to be callable only on a pad and remove the compatibility code.

---
  drivers/media/i2c/st-mipid02.c                  |  5 +++--
  drivers/media/i2c/tc358746.c                    |  4 +++-
  drivers/media/pci/intel/ipu3/ipu3-cio2.c        | 11 ++++++++---
  drivers/media/platform/cadence/cdns-csi2rx.c    |  4 +++-
  drivers/media/platform/nxp/imx-mipi-csis.c      |  5 +++--
  drivers/media/platform/nxp/imx8mq-mipi-csi2.c   | 11 +++++++++--
  .../media/platform/qcom/camss/camss-vfe-17x.c   |  8 +++++---
  .../media/platform/qcom/camss/camss-vfe-gen1.c  |  8 +++++---
  drivers/media/platform/qcom/camss/camss.c       | 17 +++++++----------
  drivers/media/platform/qcom/camss/camss.h       |  2 +-
  .../media/platform/raspberrypi/rp1-cfe/cfe.c    |  4 +++-
  drivers/media/platform/st/stm32/stm32-csi.c     |  4 +++-
  drivers/media/platform/ti/cal/cal-camerarx.c    |  3 ++-
  drivers/media/platform/ti/cal/cal.c             |  4 +++-
  drivers/media/platform/ti/cal/cal.h             |  1 +
  15 files changed, 59 insertions(+), 32 deletions(-)
drivers/media/i2c/st-mipid02.c +3 -2
index f08db3cfe076..f4568e87f018 100644
@@ -301,8 +301,9 @@ static int mipid02_detect(struct mipid02_dev *bridge)
  static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
  					   struct v4l2_mbus_framefmt *fmt)
  {
+	struct media_pad *remote =
+		&bridge->s_subdev->entity.pads[bridge->s_subdev_pad_id];
  	struct i2c_client *client = bridge->i2c_client;
-	struct v4l2_subdev *subdev = bridge->s_subdev;
  	struct v4l2_fwnode_endpoint *ep = &bridge->rx;
  	u32 bpp = bpp_from_code(fmt->code);
  	/*
@@ -312,7 +313,7 @@ static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
  	u64 ui_4 = 2000000000;
  	s64 link_freq;
 
-	link_freq = v4l2_get_link_freq(subdev->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(remote, bpp,
  				       2 * ep->bus.mipi_csi2.num_data_lanes);
  	if (link_freq < 0) {
  		dev_err(&client->dev, "Failed to get link frequency");
drivers/media/i2c/tc358746.c +3 -1
index 389582420ba7..31586f8e4be4 100644
@@ -896,6 +896,7 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
  	const struct tc358746_format *fmt;
  	unsigned int fifo_sz, tmp, n;
  	struct v4l2_subdev *source;
+	struct media_pad *src_pad;
  	s64 source_link_freq;
  	int err;
 
@@ -910,7 +911,8 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
  	fmt = tc358746_get_format_by_code(TC358746_SINK, mbusfmt->code);
 
  	source = media_entity_to_v4l2_subdev(link->source->entity);
-	source_link_freq = v4l2_get_link_freq(source->ctrl_handler, 0, 0);
+	src_pad = &source->entity.pads[source_fmt->pad];
+	source_link_freq = v4l2_get_link_freq(src_pad, 0, 0);
  	if (source_link_freq <= 0) {
  		dev_err(tc358746->sd.dev,
  			"Failed to query or invalid source link frequency\n");
drivers/media/pci/intel/ipu3/ipu3-cio2.c +8 -3
index 4e98f432ed55..7731eb70fc8e 100644
@@ -309,12 +309,17 @@ static int cio2_csi2_calc_timing(struct cio2_device *cio2, struct cio2_queue *q,
  				 unsigned int bpp, unsigned int lanes)
  {
  	struct device *dev = &cio2->pci_dev->dev;
+	struct media_pad *src_pad;
  	s64 freq;
 
-	if (!q->sensor)
-		return -ENODEV;
+	src_pad = media_entity_remote_source_pad_unique(&q->subdev.entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(dev, "can't get source pad of %s (%ld)\n",
+			q->subdev.name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
 
-	freq = v4l2_get_link_freq(q->sensor->ctrl_handler, bpp, lanes * 2);
+	freq = v4l2_get_link_freq(src_pad, bpp, lanes * 2);
  	if (freq < 0) {
  		dev_err(dev, "error %lld, invalid link_freq\n", freq);
  		return freq;
drivers/media/platform/cadence/cdns-csi2rx.c +3 -1
index 4d64df829e75..cebcae196eec 100644
@@ -164,6 +164,8 @@ static void csi2rx_reset(struct csi2rx_priv *csi2rx)
 
  static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
  {
+	struct media_pad *src_pad =
+		&csi2rx->source_subdev->entity.pads[csi2rx->source_pad];
  	union phy_configure_opts opts = { };
  	struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
  	struct v4l2_subdev_format sd_fmt = {
@@ -181,7 +183,7 @@ static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 
  	fmt = csi2rx_get_fmt_by_code(sd_fmt.format.code);
 
-	link_freq = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
  				       fmt->bpp, 2 * csi2rx->num_lanes);
  	if (link_freq < 0)
  		return link_freq;
drivers/media/platform/nxp/imx-mipi-csis.c +3 -2
index 29523bb84d95..d060eadebc7a 100644
@@ -597,12 +597,13 @@ static void __mipi_csis_set_format(struct mipi_csis_device *csis,
  static int mipi_csis_calculate_params(struct mipi_csis_device *csis,
  				      const struct csis_pix_format *csis_fmt)
  {
+	struct media_pad *src_pad =
+		&csis->source.sd->entity.pads[csis->source.pad->index];
  	s64 link_freq;
  	u32 lane_rate;
 
  	/* Calculate the line rate from the pixel rate. */
-	link_freq = v4l2_get_link_freq(csis->source.sd->ctrl_handler,
-				       csis_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csis_fmt->width,
  				       csis->bus.num_data_lanes * 2);
  	if (link_freq < 0) {
  		dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/nxp/imx8mq-mipi-csi2.c +9 -2
index 1f2657cf6e82..a8bcf60e2f37 100644
@@ -287,6 +287,7 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
  					  struct v4l2_subdev_state *sd_state,
  					  u32 *hs_settle)
  {
+	struct media_pad *src_pad;
  	s64 link_freq;
  	u32 lane_rate;
  	unsigned long esc_clk_rate;
@@ -294,13 +295,19 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
  	const struct v4l2_mbus_framefmt *fmt;
  	const struct csi2_pix_format *csi2_fmt;
 
+	src_pad = media_entity_remote_source_pad_unique(&sd_state->sd->entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(state->dev, "can't get source pad of %s (%ld)\n",
+			sd_state->sd->name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
+
  	/* Calculate the line rate from the pixel rate. */
 
  	fmt = v4l2_subdev_state_get_format(sd_state, MIPI_CSI2_PAD_SINK);
  	csi2_fmt = find_csi2_format(fmt->code);
 
-	link_freq = v4l2_get_link_freq(state->src_sd->ctrl_handler,
-				       csi2_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csi2_fmt->width,
  				       state->bus.num_data_lanes * 2);
  	if (link_freq < 0) {
  		dev_err(state->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/qcom/camss/camss-vfe-17x.c +1 -1
index 380c99321030..cc93f79179dd 100644
@@ -443,16 +443,18 @@ static int vfe_enable_output(struct vfe_line *line)
  	struct vfe_device *vfe = to_vfe(line);
  	struct vfe_output *output = &line->output;
  	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
  	unsigned long flags;
  	unsigned int frame_skip = 0;
  	unsigned int i;

drivers/media/platform/qcom/camss/camss-vfe-17x.c static int vfe_enable_output(struct vfe_line *line) · line 457

Quoted 10 lines
drivers/media/platform/qcom/camss/camss-vfe-17x.c +4 -2
 
  	sensor = camss_find_sensor(&line->subdev.entity);
  	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);

This looks a bit odd... The sensor parameter was media_entity, but now it's media_pad, but I don't think the called op has changed. Is that right?

Quoted 17 lines
drivers/media/platform/qcom/camss/camss-vfe-17x.c +0 -0
  		/* Max frame skip is 29 frames */
  		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
  			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +1 -1
index eb33c03df27e..1970f7aa6d4d 100644
@@ -170,7 +170,7 @@ static int vfe_enable_output(struct vfe_line *line)
  	struct vfe_device *vfe = to_vfe(line);
  	struct vfe_output *output = &line->output;
  	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
  	unsigned long flags;
  	unsigned int frame_skip = 0;
  	unsigned int i;

drivers/media/platform/qcom/camss/camss-vfe-gen1.c static int vfe_enable_output(struct vfe_line *line) · line 189

Quoted 10 lines
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +4 -2
@@ -182,9 +182,11 @@ static int vfe_enable_output(struct vfe_line *line)
 
  	sensor = camss_find_sensor(&line->subdev.entity);
  	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);

Here too.

Quoted 9 lines
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +0 -0
  		/* Max frame skip is 29 frames */
  		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
  			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss.c +0 -0
index 004a74f6b2f6..e86de4b59723 100644
@@ -1996,12 +1996,12 @@ void camss_disable_clocks(int nclocks, struct camss_clock *clock)
  }

drivers/media/platform/qcom/camss/camss.c void camss_disable_clocks(int nclocks, struct camss_clock *clock) · line 2004

Quoted 10 lines
drivers/media/platform/qcom/camss/camss.c +2 -2
 
  /*
- * camss_find_sensor - Find a linked media entity which represents a sensor
+ * camss_find_sensor - Find the media pad via which the sensor is linked
   * @entity: Media entity to start searching from
   *
   * Return a pointer to sensor media entity or NULL if not found
   */
-struct media_entity *camss_find_sensor(struct media_entity *entity)
+struct media_pad *camss_find_sensor(struct media_entity *entity)

I don't like this change. Maybe rename the function to camss_find_sensor_pad(), and rename the "sensor" variables to "sensor_pad". That would make the diff more readable, too, to show all the places where we used to have a media_entity, but now have media_pad.

But even then, after this change, I think mostly the callers of camss_find_sensor() will just end up changing it back to entity. Maybe that's fine, but is there a way to keep the camss_find_sensor as it is now, and just do something extra when calling v4l2_get_link_freq() to get the media_pad...

Tomi

Quoted 154 lines
drivers/media/platform/qcom/camss/camss.c +5 -8
  {
  	struct media_pad *pad;
 
@@ -2017,7 +2017,7 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
  		entity = pad->entity;
 
  		if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
-			return entity;
+			return pad;
  	}
  }
 
@@ -2032,16 +2032,13 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
  s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
  			unsigned int lanes)
  {
-	struct media_entity *sensor;
-	struct v4l2_subdev *subdev;
+	struct media_pad *sensor;
 
  	sensor = camss_find_sensor(entity);
  	if (!sensor)
  		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
-
-	return v4l2_get_link_freq(subdev->ctrl_handler, bpp, 2 * lanes);
+	return v4l2_get_link_freq(sensor, bpp, 2 * lanes);
  }
 
  /*
@@ -2053,7 +2050,7 @@ s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
   */
  int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
  {
-	struct media_entity *sensor;
+	struct media_pad *sensor;
  	struct v4l2_subdev *subdev;
  	struct v4l2_ctrl *ctrl;
 
@@ -2061,7 +2058,7 @@ int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
  	if (!sensor)
  		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
+	subdev = media_entity_to_v4l2_subdev(sensor->entity);
 
  	ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
 
drivers/media/platform/qcom/camss/camss.h +1 -1
index 9a046eea334f..6da7131c0aaa 100644
@@ -153,7 +153,7 @@ void camss_add_clock_margin(u64 *rate);
  int camss_enable_clocks(int nclocks, struct camss_clock *clock,
  			struct device *dev);
  void camss_disable_clocks(int nclocks, struct camss_clock *clock);
-struct media_entity *camss_find_sensor(struct media_entity *entity);
+struct media_pad *camss_find_sensor(struct media_entity *entity);
  s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
  			unsigned int lanes);
  int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock);
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c +3 -1
index 12660087b12f..ed3d18917f2d 100644
@@ -1102,6 +1102,8 @@ static void cfe_buffer_queue(struct vb2_buffer *vb)
 
  static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
  {
+	struct media_pad *src_pad =
+		&cfe->source_sd->entity.pads[cfe->source_pad];
  	struct v4l2_subdev_state *state;
  	s64 link_freq;
  	u32 bpp;
@@ -1136,7 +1138,7 @@ static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
  		bpp = 0;
  	}
 
-	link_freq = v4l2_get_link_freq(cfe->source_sd->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(src_pad, bpp,
  				       2 * cfe->csi2.dphy.active_lanes);
  	if (link_freq < 0)
  		cfe_err(cfe, "failed to get link freq for subdev '%s'\n",
drivers/media/platform/st/stm32/stm32-csi.c +3 -1
index 48941aae8c9b..b25afeee4f24 100644
@@ -444,6 +444,8 @@ static void stm32_csi_phy_reg_write(struct stm32_csi_dev *csidev,
  static int stm32_csi_start(struct stm32_csi_dev *csidev,
  			   struct v4l2_subdev_state *state)
  {
+	struct media_pad *src_pad =
+		&csidev->s_subdev->entity.pads[csidev->s_subdev_pad_nb];
  	const struct stm32_csi_mbps_phy_reg *phy_regs;
  	struct v4l2_mbus_framefmt *sink_fmt;
  	const struct stm32_csi_fmts *fmt;
@@ -465,7 +467,7 @@ static int stm32_csi_start(struct stm32_csi_dev *csidev,
  	if (!csidev->s_subdev)
  		return -EIO;
 
-	link_freq = v4l2_get_link_freq(csidev->s_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
  				       fmt->bpp, 2 * csidev->num_lanes);
  	if (link_freq < 0)
  		return link_freq;
drivers/media/platform/ti/cal/cal-camerarx.c +2 -1
index 42dfe08b765f..9cc875665695 100644
@@ -65,7 +65,8 @@ static s64 cal_camerarx_get_ext_link_freq(struct cal_camerarx *phy)
 
  	bpp = fmtinfo->bpp;
 
-	freq = v4l2_get_link_freq(phy->source->ctrl_handler, bpp, 2 * num_lanes);
+	freq = v4l2_get_link_freq(&phy->source->entity.pads[phy->source_pad],
+				  bpp, 2 * num_lanes);
  	if (freq < 0) {
  		phy_err(phy, "failed to get link freq for subdev '%s'\n",
  			phy->source->name);
drivers/media/platform/ti/cal/cal.c +3 -1
index 4bd2092e0255..6cb3e5f49686 100644
@@ -798,7 +798,6 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
  		return 0;
  	}
 
-	phy->source = subdev;
  	phy_dbg(1, phy, "Using source %s for capture\n", subdev->name);
 
  	pad = media_entity_get_fwnode_pad(&subdev->entity,
@@ -820,6 +819,9 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
  		return ret;
  	}
 
+	phy->source = subdev;
+	phy->source_pad = pad;
+
  	return 0;
  }
 
drivers/media/platform/ti/cal/cal.h +1 -0
index 0856297adc0b..72a246a64d9e 100644
@@ -174,6 +174,7 @@ struct cal_camerarx {
  	struct device_node	*source_ep_node;
  	struct device_node	*source_node;
  	struct v4l2_subdev	*source;
+	unsigned int		source_pad;
 
  	struct v4l2_subdev	subdev;
  	struct media_pad	pads[CAL_CAMERARX_NUM_PADS];
maintainer Reviewed-by on v8 4/9
#9 lore raw

Re: [PATCH v8 4/9] media: Documentation: tx-rx: Move transmitter control out of CSI-2 part

In reply to Sakari Ailus
Quoted 32 lines · Sakari Ailus

The subsection on stopping the transmitter belongs to the generic part and is not specific to CSI-2. Move it out of the CSI-2 section.

---
  Documentation/driver-api/media/tx-rx.rst | 19 +++++++++----------
  1 file changed, 9 insertions(+), 10 deletions(-)
Documentation/driver-api/media/tx-rx.rst +9 -0
index 6f9eba189a9f..03768e5aa88f 100644
@@ -62,6 +62,15 @@ to control the transmitter driver's streaming state. These callbacks may not be
  called directly, but by using ``v4l2_subdev_enable_streams()`` and
  ``v4l2_subdev_disable_streams()``.
 
+Stopping the transmitter
+^^^^^^^^^^^^^^^^^^^^^^^^
+
+A transmitter stops sending the stream of images as a result of
+calling the ``.disable_streams()`` callback. Some transmitters may stop the
+stream at a frame boundary whereas others stop immediately,
+effectively leaving the current frame unfinished. The receiver driver
+should not make assumptions either way, but function properly in both
+cases.
 
  CSI-2 transmitter drivers
  -------------------------
@@ -130,13 +139,3 @@ device, so this should be only done when it is needed.
 
  Receiver drivers that do not need explicit LP-11 or LP-111 state setup are
  waived from calling the two callbacks.

Documentation/driver-api/media/tx-rx.rst device, so this should be only done when it is needed. · line 142

Quoted 10 lines
Documentation/driver-api/media/tx-rx.rst +0 -10
-
-Stopping the transmitter
-^^^^^^^^^^^^^^^^^^^^^^^^
-
-A transmitter stops sending the stream of images as a result of
-calling the ``.disable_streams()`` callback. Some transmitters may stop the
-stream at a frame boundary whereas others stop immediately,
-effectively leaving the current frame unfinished. The receiver driver
-should not make assumptions either way, but function properly in both
-cases.

Tomi

maintainer Reviewed-by on v8 5/9
#11 lore raw

Re: [PATCH v8 5/9] media: Documentation: Receiver drivers should call v4l2_get_link_freq()

In reply to Sakari Ailus
Quoted 13 lines · Sakari Ailus

Document that receiver drivers should call v4l2_get_link_freq() to obtain the link frequency.

---
  Documentation/driver-api/media/tx-rx.rst | 3 +++
  1 file changed, 3 insertions(+)
Documentation/driver-api/media/tx-rx.rst +0 -0
index 03768e5aa88f..0b8c9cde8ee4 100644

Documentation/driver-api/media/tx-rx.rst Drivers that do not have user-configurable link frequency should report it · line 61

Quoted 9 lines
Documentation/driver-api/media/tx-rx.rst +3 -0
@@ -53,6 +53,9 @@ Drivers that do not have user-configurable link frequency should report it
  through the ``.get_mbus_config()`` subdev pad operation, in the ``link_freq``
  field of struct v4l2_mbus_config, instead of through controls.
 
+Receiver drivers should use :c:func:`v4l2_get_link_freq` helper to obtain the
+link frequency from the transmitter sub-device.
+
  ``.enable_streams()`` and ``.disable_streams()`` callbacks
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 

Tomi

maintainer
#13 lore raw

Re: [PATCH v8 6/9] media: Documentation: Add a note to set all fields in get_mbus_config op

In reply to Sakari Ailus
Quoted 13 lines · Sakari Ailus

Document that the implementations of the V4L2 sub-device pad operation get_mbus_config shall set all fields of the struct to defined values.

---
  include/media/v4l2-subdev.h | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
include/media/v4l2-subdev.h +0 -0
index 2f2200875b03..abcc879fabb2 100644

include/media/v4l2-subdev.h struct v4l2_subdev_state { · line 828

Quoted 8 lines
include/media/v4l2-subdev.h +4 -1
@@ -822,7 +822,10 @@ struct v4l2_subdev_state {
   *		     possible configuration from the remote end, likely calling
   *		     this operation as close as possible to stream on time. The
   *		     operation shall fail if the pad index it has been called on
- *		     is not valid or in case of unrecoverable failures.
+ *		     is not valid or in case of unrecoverable failures. The
+ *		     implementations of the get_mbus_config operation shall set
+ *		     all the values of struct v4l2_mbus_config to defined
+ *		     values.

The drivers can't set all fields, as we can add new fields (like you do in this series). So I assume this means that the drivers should memset() the struct to 0, and only then set the values it wants?

I think we should have a wrapper for get_mbus_config, which would clear the struct before calling the op. And the drivers should always use that wrapper.

Tomi

maintainer Acked-by on v8 9/9
#20 lore raw

Re: [RESEND PATCH v8 9/9] media: v4l: Convert the users of v4l2_get_link_freq to call it on a pad

In reply to Sakari Ailus

Hi Sakari,

Thank you for your patch.

Quoted 5 lines · Sakari Ailus

Call v4l2_get_link_freq() on a pad, instead of a control handler. This way we can soon convert v4l2_get_link_freq() to be callable only on a pad and remove the compatibility code.

For the st-mipid02 part :

Quoted 393 lines

---
Included driver maintainers and reviewers.

The full set is here <URL:https://lore.kernel.org/linux-media/[email protected]/T/#t>.

 drivers/media/i2c/st-mipid02.c                  |  5 +++--
 drivers/media/i2c/tc358746.c                    |  4 +++-
 drivers/media/pci/intel/ipu3/ipu3-cio2.c        | 11 ++++++++---
 drivers/media/platform/cadence/cdns-csi2rx.c    |  4 +++-
 drivers/media/platform/nxp/imx-mipi-csis.c      |  5 +++--
 drivers/media/platform/nxp/imx8mq-mipi-csi2.c   | 11 +++++++++--
 .../media/platform/qcom/camss/camss-vfe-17x.c   |  8 +++++---
 .../media/platform/qcom/camss/camss-vfe-gen1.c  |  8 +++++---
 drivers/media/platform/qcom/camss/camss.c       | 17 +++++++----------
 drivers/media/platform/qcom/camss/camss.h       |  2 +-
 .../media/platform/raspberrypi/rp1-cfe/cfe.c    |  4 +++-
 drivers/media/platform/st/stm32/stm32-csi.c     |  4 +++-
 drivers/media/platform/ti/cal/cal-camerarx.c    |  3 ++-
 drivers/media/platform/ti/cal/cal.c             |  4 +++-
 drivers/media/platform/ti/cal/cal.h             |  1 +
 15 files changed, 59 insertions(+), 32 deletions(-)
drivers/media/i2c/st-mipid02.c +3 -2
index f08db3cfe076..f4568e87f018 100644
@@ -301,8 +301,9 @@ static int mipid02_detect(struct mipid02_dev *bridge)
 static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
 					   struct v4l2_mbus_framefmt *fmt)
 {
+	struct media_pad *remote =
+		&bridge->s_subdev->entity.pads[bridge->s_subdev_pad_id];
 	struct i2c_client *client = bridge->i2c_client;
-	struct v4l2_subdev *subdev = bridge->s_subdev;
 	struct v4l2_fwnode_endpoint *ep = &bridge->rx;
 	u32 bpp = bpp_from_code(fmt->code);
 	/*
@@ -312,7 +313,7 @@ static int mipid02_configure_from_rx_speed(struct mipid02_dev *bridge,
 	u64 ui_4 = 2000000000;
 	s64 link_freq;
 
-	link_freq = v4l2_get_link_freq(subdev->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(remote, bpp,
 				       2 * ep->bus.mipi_csi2.num_data_lanes);
 	if (link_freq < 0) {
 		dev_err(&client->dev, "Failed to get link frequency");
drivers/media/i2c/tc358746.c +3 -1
index 389582420ba7..31586f8e4be4 100644
@@ -896,6 +896,7 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
 	const struct tc358746_format *fmt;
 	unsigned int fifo_sz, tmp, n;
 	struct v4l2_subdev *source;
+	struct media_pad *src_pad;
 	s64 source_link_freq;
 	int err;
 
@@ -910,7 +911,8 @@ tc358746_link_validate(struct v4l2_subdev *sd, struct media_link *link,
 	fmt = tc358746_get_format_by_code(TC358746_SINK, mbusfmt->code);
 
 	source = media_entity_to_v4l2_subdev(link->source->entity);
-	source_link_freq = v4l2_get_link_freq(source->ctrl_handler, 0, 0);
+	src_pad = &source->entity.pads[source_fmt->pad];
+	source_link_freq = v4l2_get_link_freq(src_pad, 0, 0);
 	if (source_link_freq <= 0) {
 		dev_err(tc358746->sd.dev,
 			"Failed to query or invalid source link frequency\n");
drivers/media/pci/intel/ipu3/ipu3-cio2.c +8 -3
index 4e98f432ed55..7731eb70fc8e 100644
@@ -309,12 +309,17 @@ static int cio2_csi2_calc_timing(struct cio2_device *cio2, struct cio2_queue *q,
 				 unsigned int bpp, unsigned int lanes)
 {
 	struct device *dev = &cio2->pci_dev->dev;
+	struct media_pad *src_pad;
 	s64 freq;
 
-	if (!q->sensor)
-		return -ENODEV;
+	src_pad = media_entity_remote_source_pad_unique(&q->subdev.entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(dev, "can't get source pad of %s (%ld)\n",
+			q->subdev.name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
 
-	freq = v4l2_get_link_freq(q->sensor->ctrl_handler, bpp, lanes * 2);
+	freq = v4l2_get_link_freq(src_pad, bpp, lanes * 2);
 	if (freq < 0) {
 		dev_err(dev, "error %lld, invalid link_freq\n", freq);
 		return freq;
drivers/media/platform/cadence/cdns-csi2rx.c +3 -1
index 4d64df829e75..cebcae196eec 100644
@@ -164,6 +164,8 @@ static void csi2rx_reset(struct csi2rx_priv *csi2rx)
 
 static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 {
+	struct media_pad *src_pad =
+		&csi2rx->source_subdev->entity.pads[csi2rx->source_pad];
 	union phy_configure_opts opts = { };
 	struct phy_configure_opts_mipi_dphy *cfg = &opts.mipi_dphy;
 	struct v4l2_subdev_format sd_fmt = {
@@ -181,7 +183,7 @@ static int csi2rx_configure_ext_dphy(struct csi2rx_priv *csi2rx)
 
 	fmt = csi2rx_get_fmt_by_code(sd_fmt.format.code);
 
-	link_freq = v4l2_get_link_freq(csi2rx->source_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
 				       fmt->bpp, 2 * csi2rx->num_lanes);
 	if (link_freq < 0)
 		return link_freq;
drivers/media/platform/nxp/imx-mipi-csis.c +3 -2
index 29523bb84d95..d060eadebc7a 100644
@@ -597,12 +597,13 @@ static void __mipi_csis_set_format(struct mipi_csis_device *csis,
 static int mipi_csis_calculate_params(struct mipi_csis_device *csis,
 				      const struct csis_pix_format *csis_fmt)
 {
+	struct media_pad *src_pad =
+		&csis->source.sd->entity.pads[csis->source.pad->index];
 	s64 link_freq;
 	u32 lane_rate;
 
 	/* Calculate the line rate from the pixel rate. */
-	link_freq = v4l2_get_link_freq(csis->source.sd->ctrl_handler,
-				       csis_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csis_fmt->width,
 				       csis->bus.num_data_lanes * 2);
 	if (link_freq < 0) {
 		dev_err(csis->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/nxp/imx8mq-mipi-csi2.c +9 -2
index 1f2657cf6e82..a8bcf60e2f37 100644
@@ -287,6 +287,7 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
 					  struct v4l2_subdev_state *sd_state,
 					  u32 *hs_settle)
 {
+	struct media_pad *src_pad;
 	s64 link_freq;
 	u32 lane_rate;
 	unsigned long esc_clk_rate;
@@ -294,13 +295,19 @@ static int imx8mq_mipi_csi_calc_hs_settle(struct csi_state *state,
 	const struct v4l2_mbus_framefmt *fmt;
 	const struct csi2_pix_format *csi2_fmt;
 
+	src_pad = media_entity_remote_source_pad_unique(&sd_state->sd->entity);
+	if (IS_ERR(src_pad)) {
+		dev_err(state->dev, "can't get source pad of %s (%ld)\n",
+			sd_state->sd->name, PTR_ERR(src_pad));
+		return PTR_ERR(src_pad);
+	}
+
 	/* Calculate the line rate from the pixel rate. */
 
 	fmt = v4l2_subdev_state_get_format(sd_state, MIPI_CSI2_PAD_SINK);
 	csi2_fmt = find_csi2_format(fmt->code);
 
-	link_freq = v4l2_get_link_freq(state->src_sd->ctrl_handler,
-				       csi2_fmt->width,
+	link_freq = v4l2_get_link_freq(src_pad, csi2_fmt->width,
 				       state->bus.num_data_lanes * 2);
 	if (link_freq < 0) {
 		dev_err(state->dev, "Unable to obtain link frequency: %d\n",
drivers/media/platform/qcom/camss/camss-vfe-17x.c +5 -3
index 380c99321030..cc93f79179dd 100644
@@ -443,16 +443,18 @@ static int vfe_enable_output(struct vfe_line *line)
 	struct vfe_device *vfe = to_vfe(line);
 	struct vfe_output *output = &line->output;
 	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	unsigned long flags;
 	unsigned int frame_skip = 0;
 	unsigned int i;
 
 	sensor = camss_find_sensor(&line->subdev.entity);
 	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);
 		/* Max frame skip is 29 frames */
 		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
 			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +5 -3
index eb33c03df27e..1970f7aa6d4d 100644
@@ -170,7 +170,7 @@ static int vfe_enable_output(struct vfe_line *line)
 	struct vfe_device *vfe = to_vfe(line);
 	struct vfe_output *output = &line->output;
 	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	unsigned long flags;
 	unsigned int frame_skip = 0;
 	unsigned int i;
@@ -182,9 +182,11 @@ static int vfe_enable_output(struct vfe_line *line)
 
 	sensor = camss_find_sensor(&line->subdev.entity);
 	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
 
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);
 		/* Max frame skip is 29 frames */
 		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
 			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss.c +7 -10
index 004a74f6b2f6..e86de4b59723 100644
@@ -1996,12 +1996,12 @@ void camss_disable_clocks(int nclocks, struct camss_clock *clock)
 }
 
 /*
- * camss_find_sensor - Find a linked media entity which represents a sensor
+ * camss_find_sensor - Find the media pad via which the sensor is linked
  * @entity: Media entity to start searching from
  *
  * Return a pointer to sensor media entity or NULL if not found
  */
-struct media_entity *camss_find_sensor(struct media_entity *entity)
+struct media_pad *camss_find_sensor(struct media_entity *entity)
 {
 	struct media_pad *pad;
 
@@ -2017,7 +2017,7 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
 		entity = pad->entity;
 
 		if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
-			return entity;
+			return pad;
 	}
 }
 
@@ -2032,16 +2032,13 @@ struct media_entity *camss_find_sensor(struct media_entity *entity)
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
 			unsigned int lanes)
 {
-	struct media_entity *sensor;
-	struct v4l2_subdev *subdev;
+	struct media_pad *sensor;
 
 	sensor = camss_find_sensor(entity);
 	if (!sensor)
 		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
-
-	return v4l2_get_link_freq(subdev->ctrl_handler, bpp, 2 * lanes);
+	return v4l2_get_link_freq(sensor, bpp, 2 * lanes);
 }
 
 /*
@@ -2053,7 +2050,7 @@ s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
  */
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 {
-	struct media_entity *sensor;
+	struct media_pad *sensor;
 	struct v4l2_subdev *subdev;
 	struct v4l2_ctrl *ctrl;
 
@@ -2061,7 +2058,7 @@ int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 	if (!sensor)
 		return -ENODEV;
 
-	subdev = media_entity_to_v4l2_subdev(sensor);
+	subdev = media_entity_to_v4l2_subdev(sensor->entity);
 
 	ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);
 
drivers/media/platform/qcom/camss/camss.h +1 -1
index 9a046eea334f..6da7131c0aaa 100644
@@ -153,7 +153,7 @@ void camss_add_clock_margin(u64 *rate);
 int camss_enable_clocks(int nclocks, struct camss_clock *clock,
 			struct device *dev);
 void camss_disable_clocks(int nclocks, struct camss_clock *clock);
-struct media_entity *camss_find_sensor(struct media_entity *entity);
+struct media_pad *camss_find_sensor(struct media_entity *entity);
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
 			unsigned int lanes);
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock);
drivers/media/platform/raspberrypi/rp1-cfe/cfe.c +3 -1
index 12660087b12f..ed3d18917f2d 100644
@@ -1102,6 +1102,8 @@ static void cfe_buffer_queue(struct vb2_buffer *vb)
 
 static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
 {
+	struct media_pad *src_pad =
+		&cfe->source_sd->entity.pads[cfe->source_pad];
 	struct v4l2_subdev_state *state;
 	s64 link_freq;
 	u32 bpp;
@@ -1136,7 +1138,7 @@ static s64 cfe_get_source_link_freq(struct cfe_device *cfe)
 		bpp = 0;
 	}
 
-	link_freq = v4l2_get_link_freq(cfe->source_sd->ctrl_handler, bpp,
+	link_freq = v4l2_get_link_freq(src_pad, bpp,
 				       2 * cfe->csi2.dphy.active_lanes);
 	if (link_freq < 0)
 		cfe_err(cfe, "failed to get link freq for subdev '%s'\n",
drivers/media/platform/st/stm32/stm32-csi.c +3 -1
index 48941aae8c9b..b25afeee4f24 100644
@@ -444,6 +444,8 @@ static void stm32_csi_phy_reg_write(struct stm32_csi_dev *csidev,
 static int stm32_csi_start(struct stm32_csi_dev *csidev,
 			   struct v4l2_subdev_state *state)
 {
+	struct media_pad *src_pad =
+		&csidev->s_subdev->entity.pads[csidev->s_subdev_pad_nb];
 	const struct stm32_csi_mbps_phy_reg *phy_regs;
 	struct v4l2_mbus_framefmt *sink_fmt;
 	const struct stm32_csi_fmts *fmt;
@@ -465,7 +467,7 @@ static int stm32_csi_start(struct stm32_csi_dev *csidev,
 	if (!csidev->s_subdev)
 		return -EIO;
 
-	link_freq = v4l2_get_link_freq(csidev->s_subdev->ctrl_handler,
+	link_freq = v4l2_get_link_freq(src_pad,
 				       fmt->bpp, 2 * csidev->num_lanes);
 	if (link_freq < 0)
 		return link_freq;
drivers/media/platform/ti/cal/cal-camerarx.c +2 -1
index 42dfe08b765f..9cc875665695 100644
@@ -65,7 +65,8 @@ static s64 cal_camerarx_get_ext_link_freq(struct cal_camerarx *phy)
 
 	bpp = fmtinfo->bpp;
 
-	freq = v4l2_get_link_freq(phy->source->ctrl_handler, bpp, 2 * num_lanes);
+	freq = v4l2_get_link_freq(&phy->source->entity.pads[phy->source_pad],
+				  bpp, 2 * num_lanes);
 	if (freq < 0) {
 		phy_err(phy, "failed to get link freq for subdev '%s'\n",
 			phy->source->name);
drivers/media/platform/ti/cal/cal.c +3 -1
index 4bd2092e0255..6cb3e5f49686 100644
@@ -798,7 +798,6 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
 		return 0;
 	}
 
-	phy->source = subdev;
 	phy_dbg(1, phy, "Using source %s for capture\n", subdev->name);
 
 	pad = media_entity_get_fwnode_pad(&subdev->entity,
@@ -820,6 +819,9 @@ static int cal_async_notifier_bound(struct v4l2_async_notifier *notifier,
 		return ret;
 	}
 
+	phy->source = subdev;
+	phy->source_pad = pad;
+
 	return 0;
 }
 
drivers/media/platform/ti/cal/cal.h +1 -0
index 0856297adc0b..72a246a64d9e 100644
@@ -174,6 +174,7 @@ struct cal_camerarx {
 	struct device_node	*source_ep_node;
 	struct device_node	*source_node;
 	struct v4l2_subdev	*source;
+	unsigned int		source_pad;
 
 	struct v4l2_subdev	subdev;
 	struct media_pad	pads[CAL_CAMERARX_NUM_PADS];
Signature · 3 lines
Regards,

Benjamin
author maintainer
#22 lore raw

Re: [PATCH v8 9/9] media: v4l: Convert the users of v4l2_get_link_freq to call it on a pad

In reply to Tomi Valkeinen

Moi,

Quoted 1 line · Tomi Valkeinen

Hi,

Thanks for the review.

Quoted 18 lines · Sakari Ailus
Quoted 16 lines
drivers/media/platform/qcom/camss/camss-vfe-17x.c +1 -2
index 380c99321030..cc93f79179dd 100644
@@ -443,16 +443,18 @@ static int vfe_enable_output(struct vfe_line *line)
  	struct vfe_device *vfe = to_vfe(line);
  	struct vfe_output *output = &line->output;
  	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
  	unsigned long flags;
  	unsigned int frame_skip = 0;
  	unsigned int i;
  	sensor = camss_find_sensor(&line->subdev.entity);
  	if (sensor) {
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
Quoted 8 lines

drivers/media/platform/qcom/camss/camss-vfe-17x.c static int vfe_enable_output(struct vfe_line *line) · line 457

Quoted 5 lines
drivers/media/platform/qcom/camss/camss-vfe-17x.c +4 -1
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);

This looks a bit odd... The sensor parameter was media_entity, but now it's media_pad, but I don't think the called op has changed. Is that right?

"sensor" in this case is the name of the operations struct, it's unrelated to the variable with the same name. :-)

But the second line wrap is unnecessary, I'll fix it.

Quoted 20 lines
Quoted 20 lines
drivers/media/platform/qcom/camss/camss-vfe-17x.c +0 -0
  		/* Max frame skip is 29 frames */
  		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
  			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +1 -1
index eb33c03df27e..1970f7aa6d4d 100644
@@ -170,7 +170,7 @@ static int vfe_enable_output(struct vfe_line *line)
  	struct vfe_device *vfe = to_vfe(line);
  	struct vfe_output *output = &line->output;
  	const struct vfe_hw_ops *ops = vfe->res->hw_ops;
-	struct media_entity *sensor;
+	struct media_pad *sensor;
  	unsigned long flags;
  	unsigned int frame_skip = 0;
  	unsigned int i;
@@ -182,9 +182,11 @@ static int vfe_enable_output(struct vfe_line *line)
  	sensor = camss_find_sensor(&line->subdev.entity);
  	if (sensor) {
Quoted 8 lines

drivers/media/platform/qcom/camss/camss-vfe-gen1.c static int vfe_enable_output(struct vfe_line *line) · line 187

Quoted 6 lines
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +4 -2
-		struct v4l2_subdev *subdev = media_entity_to_v4l2_subdev(sensor);
+		struct v4l2_subdev *subdev =
+			media_entity_to_v4l2_subdev(sensor->entity);
-		v4l2_subdev_call(subdev, sensor, g_skip_frames, &frame_skip);
+		v4l2_subdev_call(subdev, sensor, g_skip_frames,
+				 &frame_skip);

Here too.

Ditto.

Quoted 25 lines
Quoted 9 lines
drivers/media/platform/qcom/camss/camss-vfe-gen1.c +0 -0
  		/* Max frame skip is 29 frames */
  		if (frame_skip > VFE_FRAME_DROP_VAL - 1)
  			frame_skip = VFE_FRAME_DROP_VAL - 1;
drivers/media/platform/qcom/camss/camss.c +0 -0
index 004a74f6b2f6..e86de4b59723 100644

drivers/media/platform/qcom/camss/camss.c void camss_disable_clocks(int nclocks, struct camss_clock *clock) · line 2003

Quoted 10 lines
drivers/media/platform/qcom/camss/camss.c +2 -2
@@ -1996,12 +1996,12 @@ void camss_disable_clocks(int nclocks, struct camss_clock *clock)
  }
  /*
- * camss_find_sensor - Find a linked media entity which represents a sensor
+ * camss_find_sensor - Find the media pad via which the sensor is linked
   * @entity: Media entity to start searching from
   *
   * Return a pointer to sensor media entity or NULL if not found
   */
-struct media_entity *camss_find_sensor(struct media_entity *entity)
+struct media_pad *camss_find_sensor(struct media_entity *entity)

I don't like this change. Maybe rename the function to camss_find_sensor_pad(), and rename the "sensor" variables to "sensor_pad". That would make the diff more readable, too, to show all the places where we used to have a media_entity, but now have media_pad.

Quoted 5 lines

But even then, after this change, I think mostly the callers of camss_find_sensor() will just end up changing it back to entity. Maybe that's fine, but is there a way to keep the camss_find_sensor as it is now, and just do something extra when calling v4l2_get_link_freq() to get the media_pad...

There are just a few callers of this function, we don't need a specific helper for that reason within a driver. I'll rename the function.

Signature · 3 lines
Ystävällisin terveisin,

Sakari Ailus
author maintainer
#3 lore raw

Re: [PATCH v8 1/9] media: v4l: Support passing media pad argument to v4l2_get_link_freq()

In reply to Tomi Valkeinen

Moi,

Quoted 70 lines · Tomi Valkeinen

Hi,

Quoted 67 lines · Sakari Ailus

v4l2_get_link_freq() accepts a V4L2 control handler for now, but it needs to take struct media_pad argument in order to obtain the link frequency using get_mbus_config() pad op. Prepare for this by allowing struct media_pad as well.

---
  drivers/media/v4l2-core/v4l2-common.c | 21 ++++++++++++++++++---
  include/media/v4l2-common.h           | 19 ++++++++++++++++---
  2 files changed, 34 insertions(+), 6 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +18 -3
index 0a2f4f0d0a07..9fe74c7e064f 100644
@@ -466,8 +466,8 @@ int v4l2_fill_pixfmt(struct v4l2_pix_format *pixfmt, u32 pixelformat,
  }
  EXPORT_SYMBOL_GPL(v4l2_fill_pixfmt);
-s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
-		       unsigned int div)
+s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,
+			      unsigned int mul, unsigned int div)
  {
  	struct v4l2_ctrl *ctrl;
  	s64 freq;
@@ -502,7 +502,22 @@ s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
  	return freq > 0 ? freq : -EINVAL;
  }
-EXPORT_SYMBOL_GPL(v4l2_get_link_freq);
+EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_ctrl);
+
+#ifdef CONFIG_MEDIA_CONTROLLER
+s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
+			     unsigned int div)
+{
+	struct v4l2_subdev *sd;
+
+	sd = media_entity_to_v4l2_subdev(pad->entity);
+	if (!sd)
+		return -ENODEV;
+
+	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
+}
+EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
+#endif /* CONFIG_MEDIA_CONTROLLER */
  /*
   * Simplify a fraction using a simple continued fraction decomposition. The
include/media/v4l2-common.h +2 -3
index 63ad36f04f72..fda903bb3674 100644
@@ -525,7 +525,8 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
  /**
   * v4l2_get_link_freq - Get link rate from transmitter
   *
- * @handler: The transmitter's control handler
+ * @pad: The transmitter's media pad (or control handler for non-MC users or
+ *	 compatibility reasons, don't use in new code)
   * @mul: The multiplier between pixel rate and link frequency. Bits per pixel on
   *	 D-PHY, samples per clock on parallel. 0 otherwise.
   * @div: The divisor between pixel rate and link frequency. Number of data lanes
@@ -541,8 +542,20 @@ int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat,
   * * %-ENOENT: Link frequency or pixel rate control not found
   * * %-EINVAL: Invalid link frequency value
   */
-s64 v4l2_get_link_freq(struct v4l2_ctrl_handler *handler, unsigned int mul,
-		       unsigned int div);
Quoted 8 lines

include/media/v4l2-common.h int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat, · line 550

Quoted 6 lines
include/media/v4l2-common.h +6 -0
+#ifdef CONFIG_MEDIA_CONTROLLER
+#define v4l2_get_link_freq(pad, mul, div)				\
+	_Generic(pad,							\
+		 struct media_pad *: __v4l2_get_link_freq_pad,		\
+		 struct v4l2_ctrl_handler *: __v4l2_get_link_freq_ctrl)	\
+	(pad, mul, div)

I can't decide if I love or hate _Generic =).

I really like it. It enables certain things that didn't use to be possible in C, and really helps in transitions like this. We also have container_of_const() etc. for added constness checking.

Quoted 15 lines

include/media/v4l2-common.h int v4l2_fill_pixfmt_mp(struct v4l2_pix_format_mplane *pixfmt, u32 pixelformat, · line 561

Quoted 11 lines
include/media/v4l2-common.h +8 -0
+s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
+			     unsigned int div);
+#else
+#define v4l2_get_link_freq(handler, mul, div)		\
+	__v4l2_get_link_freq_ctrl(handler, mul, div)
+#endif
+s64 __v4l2_get_link_freq_ctrl(struct v4l2_ctrl_handler *handler,
+			      unsigned int mul, unsigned int div);
  void v4l2_simplify_fraction(u32 *numerator, u32 *denominator,
  		unsigned int n_terms, unsigned int threshold);

This makes sense in the transition period. But I wonder, should we later have:

Quoted 7 lines

v4l2_get_link_freq(struct v4l2_subdev *sd, unsigned int pad, ...) like all the subdev ops? Perhaps not... Subdev ops take subdev as the first argument as, well, they're subdev ops.

It did look to me that in many drivers we already have the source subdev and pad stored somewhere, and getting the media_pad to call v4l2_get_link_freq() is an extra step.

I could add a couple of macros to cover that need. ;-) But to be fair, drivers should instead store the media pad of the sensor or even better, not store it at all but use media_entity_remote_pad_unique() to obtain it at runtime.

Signature · 3 lines
Terveisin,

Sakari Ailus
author maintainer
#14 lore raw

Re: [PATCH v8 6/9] media: Documentation: Add a note to set all fields in get_mbus_config op

In reply to Tomi Valkeinen
Quoted 28 lines · Tomi Valkeinen
Quoted 13 lines · Sakari Ailus

Document that the implementations of the V4L2 sub-device pad operation get_mbus_config shall set all fields of the struct to defined values.

---
  include/media/v4l2-subdev.h | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
include/media/v4l2-subdev.h +0 -0
index 2f2200875b03..abcc879fabb2 100644

include/media/v4l2-subdev.h struct v4l2_subdev_state { · line 828

Quoted 8 lines
include/media/v4l2-subdev.h +4 -1
@@ -822,7 +822,10 @@ struct v4l2_subdev_state {
   *		     possible configuration from the remote end, likely calling
   *		     this operation as close as possible to stream on time. The
   *		     operation shall fail if the pad index it has been called on
- *		     is not valid or in case of unrecoverable failures.
+ *		     is not valid or in case of unrecoverable failures. The
+ *		     implementations of the get_mbus_config operation shall set
+ *		     all the values of struct v4l2_mbus_config to defined
+ *		     values.

The drivers can't set all fields, as we can add new fields (like you do in this series). So I assume this means that the drivers should memset() the struct to 0, and only then set the values it wants?

Quoted 3 lines

I think we should have a wrapper for get_mbus_config, which would clear the struct before calling the op. And the drivers should always use that wrapper.

It seems we already have a wrapper actually, one that checks the pad argument is valid. I'll add the memset().

Signature · 1 line
Sakari Ailus
author maintainer
#6 lore raw

Re: [PATCH v8 2/9] media: v4l: Support obtaining link frequency via get_mbus_config

In reply to Tomi Valkeinen

Moi,

Quoted 56 lines · Tomi Valkeinen

Hi,

Quoted 53 lines · Sakari Ailus

Add link_freq field to struct v4l2_mbus_config in order to pass the link frequency to the receiving sub-device.

---
  drivers/media/v4l2-core/v4l2-common.c | 15 +++++++++++++--
  include/media/v4l2-mediabus.h         |  2 ++
  2 files changed, 15 insertions(+), 2 deletions(-)
drivers/media/v4l2-core/v4l2-common.c +13 -2
index 9fe74c7e064f..e4b2de3833ee 100644
@@ -508,12 +508,23 @@ EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_ctrl);
  s64 __v4l2_get_link_freq_pad(struct media_pad *pad, unsigned int mul,
  			     unsigned int div)
  {
+	struct v4l2_mbus_config mbus_config = {};
  	struct v4l2_subdev *sd;
+	int ret;
  	sd = media_entity_to_v4l2_subdev(pad->entity);
-	if (!sd)
-		return -ENODEV;
+	ret = v4l2_subdev_call(sd, pad, get_mbus_config, pad->index,
+			       &mbus_config);
+	if (ret < 0 && ret != -ENOIOCTLCMD)
+		return ret;
+
+	if (mbus_config.link_freq)
+		return mbus_config.link_freq;
+	/*
+	 * Fall back to using the link frequency control if the media bus config
+	 * doesn't provide a link frequency.
+	 */
  	return __v4l2_get_link_freq_ctrl(sd->ctrl_handler, mul, div);
  }
  EXPORT_SYMBOL_GPL(__v4l2_get_link_freq_pad);
include/media/v4l2-mediabus.h +1 -0
index e7f019f68c8d..24c738cd7894 100644
@@ -169,6 +169,7 @@ enum v4l2_mbus_type {
  /**
   * struct v4l2_mbus_config - media bus configuration
   * @type: interface type
+ * @link_freq: The link frequency. See also V4L2_CID_LINK_FREQ control.
   * @bus: bus configuration data structure
   * @bus.parallel: embedded &struct v4l2_mbus_config_parallel.
   *		  Used if the bus is parallel or BT.656.
@@ -183,6 +184,7 @@ enum v4l2_mbus_type {
   */
  struct v4l2_mbus_config {
  	enum v4l2_mbus_type type;
Quoted 8 lines

include/media/v4l2-mediabus.h enum v4l2_mbus_type { · line 190

Quoted 4 lines
include/media/v4l2-mediabus.h +1 -0
+	u64 link_freq;
  	union {
  		struct v4l2_mbus_config_parallel parallel;
  		struct v4l2_mbus_config_mipi_csi1 mipi_csi1;

Maybe the docs (V4L2_CID_LINK_FREQ and/or struct v4l2_mbus_config?) should mention that only if v4l2_mbus_config.link_freq is == 0, V4L2_CID_LINK_FREQ is used. Or, in other words, the driver must only use one of those methods.

Such documentation is added by the 3rd patch ("media: Documentation: Update link frequency driver documentation"):

Documentation/driver-api/media/tx-rx.rst +4 -0
index c71003f74b1c..6f9eba189a9f 100644
@@ -49,6 +49,10 @@ Link frequency
 The :ref:`V4L2_CID_LINK_FREQ <v4l2-cid-link-freq>` control is used to tell the
 receiver the frequency of the bus (i.e. it is not the same as the symbol rate).
 
+Drivers that do not have user-configurable link frequency should report it
+through the ``.get_mbus_config()`` subdev pad operation, in the ``link_freq``
+field of struct v4l2_mbus_config, instead of through controls.
+
 ``.enable_streams()`` and ``.disable_streams()`` callbacks
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Signature · 1 line
Sakari Ailus