diff mbox series

[05/23] media: atomisp: Ensure CSI-receiver[x] -> ISP links correctly reflect current sensor

Message ID 20240415120220.219480-6-hdegoede@redhat.com (mailing list archive)
State New, archived
Headers show
Series media: atomisp: Further media-controller related fixes + dead code removal | expand

Commit Message

Hans de Goede April 15, 2024, 12:02 p.m. UTC
Add a new atomisp_setup_input_links() helper which ensures that
the CSI-receiver -> ISP link for input_curr is marked as enabled and
the other CSI-receiver -> ISP links are disabled.

And call this helper from atomisp_register_device_nodes() for the initial
setup and from atomisp_select_input() for runtime input_curr changes.

Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
 .../staging/media/atomisp/pci/atomisp_cmd.c   | 34 +++++++++++++++++++
 .../staging/media/atomisp/pci/atomisp_cmd.h   |  3 ++
 .../staging/media/atomisp/pci/atomisp_ioctl.c |  6 +++-
 .../staging/media/atomisp/pci/atomisp_v4l2.c  |  4 +++
 4 files changed, 46 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/drivers/staging/media/atomisp/pci/atomisp_cmd.c b/drivers/staging/media/atomisp/pci/atomisp_cmd.c
index 6c93bab17955..b273234dc439 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_cmd.c
+++ b/drivers/staging/media/atomisp/pci/atomisp_cmd.c
@@ -3755,9 +3755,43 @@  int atomisp_select_input(struct atomisp_device *isp, unsigned int input)
 	if (input != input_orig)
 		atomisp_s_sensor_power(isp, input_orig, 0);
 
+	atomisp_setup_input_links(isp);
 	return 0;
 }
 
+/*
+ * Ensure the CSI-receiver -> ISP link for input_curr is marked as enabled and
+ * the other CSI-receiver -> ISP links are disabled.
+ */
+void atomisp_setup_input_links(struct atomisp_device *isp)
+{
+	struct media_link *link;
+
+	lockdep_assert_held(&isp->media_dev.graph_mutex);
+
+	for (int i = 0; i < ATOMISP_CAMERA_NR_PORTS; i++) {
+		link = media_entity_find_link(
+				&isp->csi2_port[i].subdev.entity.pads[CSI2_PAD_SOURCE],
+				&isp->asd.subdev.entity.pads[ATOMISP_SUBDEV_PAD_SINK]);
+		if (!link) {
+			dev_err(isp->dev, "Error cannot find CSI2-port[%d] -> ISP link\n", i);
+			continue; /* Should never happen */
+		}
+
+		/*
+		 * Modify the flags directly, calling media_entity_setup_link()
+		 * will end up calling atomisp_link_setup() which calls this
+		 * function again leading to endless recursion.
+		 */
+		if (isp->sensor_subdevs[i] == isp->inputs[isp->asd.input_curr].camera)
+			link->flags |= MEDIA_LNK_FL_ENABLED;
+		else
+			link->flags &= ~MEDIA_LNK_FL_ENABLED;
+
+		link->reverse->flags = link->flags;
+	}
+}
+
 static int atomisp_set_sensor_crop_and_fmt(struct atomisp_device *isp,
 					   struct v4l2_mbus_framefmt *ffmt,
 					   int which)
diff --git a/drivers/staging/media/atomisp/pci/atomisp_cmd.h b/drivers/staging/media/atomisp/pci/atomisp_cmd.h
index f302763b7b2f..03703eed86fa 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_cmd.h
+++ b/drivers/staging/media/atomisp/pci/atomisp_cmd.h
@@ -247,6 +247,9 @@  int atomisp_s_sensor_power(struct atomisp_device *isp, unsigned int input, bool
 /* Select which sensor to use, must be called with a valid input */
 int atomisp_select_input(struct atomisp_device *isp, unsigned int input);
 
+/* Setup media-controller links to reflect input_curr setting */
+void atomisp_setup_input_links(struct atomisp_device *isp);
+
 /* This function looks up the closest available resolution. */
 int atomisp_try_fmt(struct atomisp_device *isp, struct v4l2_pix_format *f,
 		    const struct atomisp_format_bridge **fmt_ret,
diff --git a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c b/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
index 1f8d44900a82..c16942051cfb 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
+++ b/drivers/staging/media/atomisp/pci/atomisp_ioctl.c
@@ -461,7 +461,11 @@  static int atomisp_s_input(struct file *file, void *fh, unsigned int input)
 	if (ret)
 		return ret;
 
-	return atomisp_select_input(isp, input);
+	mutex_lock(&isp->media_dev.graph_mutex);
+	ret = atomisp_select_input(isp, input);
+	mutex_unlock(&isp->media_dev.graph_mutex);
+
+	return ret;
 }
 
 /*
diff --git a/drivers/staging/media/atomisp/pci/atomisp_v4l2.c b/drivers/staging/media/atomisp/pci/atomisp_v4l2.c
index 1d7d38e2f904..1008a709b589 100644
--- a/drivers/staging/media/atomisp/pci/atomisp_v4l2.c
+++ b/drivers/staging/media/atomisp/pci/atomisp_v4l2.c
@@ -1085,6 +1085,10 @@  int atomisp_register_device_nodes(struct atomisp_device *isp)
 		dev_warn(isp->dev, "too many atomisp inputs, TPG ignored.\n");
 	}
 
+	mutex_lock(&isp->media_dev.graph_mutex);
+	atomisp_setup_input_links(isp);
+	mutex_unlock(&isp->media_dev.graph_mutex);
+
 	isp->asd.video_out.vdev.v4l2_dev = &isp->v4l2_dev;
 	isp->asd.video_out.vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
 	err = video_register_device(&isp->asd.video_out.vdev, VFL_TYPE_VIDEO, -1);