From c92b64465d1f6dbfaf189dbf68128ec52fcb0521 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Thu, 30 Apr 2020 11:03:00 +0100
Subject: [PATCH] media: i2c: ov5647: Advertise the correct exposure
 range

Exposure is clipped by the VTS of the mode, so needs to be updated as
and when this is changed.

Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
---
 drivers/media/i2c/ov5647.c | 47 +++++++++++++++++++++++++++++++-------
 1 file changed, 39 insertions(+), 8 deletions(-)

--- a/drivers/media/i2c/ov5647.c
+++ b/drivers/media/i2c/ov5647.c
@@ -84,6 +84,11 @@
 #define OV5647_VBLANK_MIN		4
 #define OV5647_VTS_MAX			32767
 
+#define OV5647_EXPOSURE_MIN		4
+#define OV5647_EXPOSURE_STEP		1
+#define OV5647_EXPOSURE_DEFAULT		1000
+#define OV5647_EXPOSURE_MAX		65535
+
 struct regval_list {
 	u16 addr;
 	u8 data;
@@ -117,6 +122,7 @@ struct ov5647 {
 	struct v4l2_ctrl		*pixel_rate;
 	struct v4l2_ctrl		*hblank;
 	struct v4l2_ctrl		*vblank;
+	struct v4l2_ctrl		*exposure;
 	bool				write_mode_regs;
 };
 
@@ -1202,7 +1208,7 @@ static int ov5647_set_fmt(struct v4l2_su
 		 * If we have changed modes, write the I2C register list on
 		 * a stream_on().
 		 */
-		int hblank;
+		int exposure_max, exposure_def, hblank;
 
 		if (state->mode != mode)
 			state->write_mode_regs = true;
@@ -1223,6 +1229,15 @@ static int ov5647_set_fmt(struct v4l2_su
 					 mode->vts_def - mode->format.height);
 		__v4l2_ctrl_s_ctrl(state->vblank,
 				   mode->vts_def - mode->format.height);
+
+		exposure_max = mode->vts_def - 4;
+		exposure_def = (exposure_max < OV5647_EXPOSURE_DEFAULT) ?
+					exposure_max : OV5647_EXPOSURE_DEFAULT;
+		__v4l2_ctrl_modify_range(state->exposure,
+					 state->exposure->minimum,
+					 exposure_max,
+					 state->exposure->step,
+					 exposure_def);
 	}
 
 	mutex_unlock(&state->lock);
@@ -1415,6 +1430,19 @@ static int ov5647_s_ctrl(struct v4l2_ctr
 
 	/* v4l2_ctrl_lock() locks our own mutex */
 
+	if (ctrl->id == V4L2_CID_VBLANK) {
+		int exposure_max, exposure_def;
+
+		/* Update max exposure while meeting expected vblanking */
+		exposure_max = state->mode->format.height + ctrl->val - 4;
+		exposure_def = (exposure_max < OV5647_EXPOSURE_DEFAULT) ?
+			exposure_max : OV5647_EXPOSURE_DEFAULT;
+		__v4l2_ctrl_modify_range(state->exposure,
+					 state->exposure->minimum,
+					 exposure_max, state->exposure->step,
+					 exposure_def);
+	}
+
 	/*
 	 * If the device is not powered up by the host driver do
 	 * not apply any controls to H/W at this time. Instead
@@ -1472,7 +1500,7 @@ static int ov5647_probe(struct i2c_clien
 	struct v4l2_subdev *sd;
 	struct device_node *np = client->dev.of_node;
 	u32 xclk_freq;
-	int hblank;
+	int hblank, exposure_max, exposure_def;
 
 	sensor = devm_kzalloc(dev, sizeof(*sensor), GFP_KERNEL);
 	if (!sensor)
@@ -1525,12 +1553,6 @@ static int ov5647_probe(struct i2c_clien
 			       0,                     /* skip_mask */
 			       V4L2_EXPOSURE_MANUAL); /* default */
 	v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
-			  V4L2_CID_EXPOSURE,
-			  4,     /* min lines */
-			  65535, /* max lines (4+8+4 bits)*/
-			  1,     /* step */
-			  1000); /* default number of lines */
-	v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
 			  V4L2_CID_ANALOGUE_GAIN,
 			  16,   /* min, 16 = 1.0x */
 			  1023, /* max (10 bits) */
@@ -1540,6 +1562,15 @@ static int ov5647_probe(struct i2c_clien
 	/* Set the default mode before we init the subdev */
 	sensor->mode = OV5647_DEFAULT_MODE;
 
+	exposure_max = sensor->mode->vts_def - 4;
+	exposure_def = (exposure_max < OV5647_EXPOSURE_DEFAULT) ?
+		exposure_max : OV5647_EXPOSURE_DEFAULT;
+	sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
+					     V4L2_CID_EXPOSURE,
+					     OV5647_EXPOSURE_MIN, exposure_max,
+					     OV5647_EXPOSURE_STEP,
+					     exposure_def);
+
 	/* By default, PIXEL_RATE is read only, but it does change per mode */
 	sensor->pixel_rate = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
 					       V4L2_CID_PIXEL_RATE,
