From f1c327d2ae5c1ffdb7a17660bf87b4a22d2ae313 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Tue, 29 Apr 2025 15:38:06 +0100
Subject: [PATCH] media: i2c: imx296: Add OF option for vsync sink for XTRIG

Copying our downstream patch for imx477 that allows configuration
of external synchronisation signals via DT, add the same to imx296.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 drivers/media/i2c/imx296.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

--- a/drivers/media/i2c/imx296.c
+++ b/drivers/media/i2c/imx296.c
@@ -205,6 +205,7 @@ struct imx296 {
 
 	const struct imx296_clk_params *clk_params;
 	bool mono;
+	int trigger_mode_of;
 
 	struct v4l2_subdev subdev;
 	struct media_pad pad;
@@ -645,16 +646,17 @@ static int imx296_setup(struct imx296 *s
 
 static int imx296_stream_on(struct imx296 *sensor)
 {
-	int ret = 0;
+	int ret = 0, tm;
 
 	imx296_write(sensor, IMX296_CTRL00, 0, &ret);
 	usleep_range(2000, 5000);
 
 	/* external trigger mode: 0=normal, 1=triggered */
+	tm = (sensor->trigger_mode_of >= 0) ? sensor->trigger_mode_of : trigger_mode;
 	imx296_write(sensor, IMX296_CTRL0B,
-		     (trigger_mode == 1) ? IMX296_CTRL0B_TRIGEN : 0, &ret);
+		     (tm == 1) ? IMX296_CTRL0B_TRIGEN : 0, &ret);
 	imx296_write(sensor, IMX296_LOWLAGTRG,
-		     (trigger_mode == 1) ? IMX296_LOWLAGTRG_FAST : 0, &ret);
+		     (tm == 1) ? IMX296_LOWLAGTRG_FAST : 0, &ret);
 
 	imx296_write(sensor, IMX296_CTRL0A, 0, &ret);
 
@@ -1079,6 +1081,7 @@ static int imx296_probe(struct i2c_clien
 	unsigned long clk_rate;
 	struct imx296 *sensor;
 	unsigned int i;
+	u32 tm_of;
 	int ret;
 
 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
@@ -1145,6 +1148,10 @@ static int imx296_probe(struct i2c_clien
 	if (ret < 0)
 		goto err_power;
 
+	/* Default the trigger mode from OF to -1, which means invalid */
+	ret = of_property_read_u32(client->dev.of_node, "trigger-mode", &tm_of);
+	sensor->trigger_mode_of = (ret == 0) ? tm_of : -1;
+
 	/* Initialize the V4L2 subdev. */
 	ret = imx296_subdev_init(sensor);
 	if (ret < 0)
