From 1c779e83e5f78cadcd8dfc6cfca33e7aeb323f27 Mon Sep 17 00:00:00 2001
From: Dave Stevenson <dave.stevenson@raspberrypi.com>
Date: Thu, 17 Feb 2022 16:44:33 +0000
Subject: [PATCH] media: i2c: ov7251: Limit exposure based on VTS

The maximum exposure is dictated by VTS, so compute it rather
than having the value in the mode table.

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

--- a/drivers/media/i2c/ov7251.c
+++ b/drivers/media/i2c/ov7251.c
@@ -37,6 +37,8 @@
 #define OV7251_AEC_EXPO_2		0x3502
 #define OV7251_AEC_AGC_ADJ_0		0x350a
 #define OV7251_AEC_AGC_ADJ_1		0x350b
+/* Exposure must be at least 20 lines shorter than VTS */
+#define OV7251_EXPOSURE_OFFSET		20
  /* HTS is registers 0x380c and 0x380d */
 #define OV7251_HTS			0x3a0
 #define OV7251_VTS_HIGH			0x380e
@@ -82,7 +84,6 @@ struct ov7251_mode_info {
 	u32 height;
 	const struct reg_value *data;
 	u32 data_size;
-	u16 exposure_max;
 	u16 exposure_def;
 	u16 vts;
 	struct v4l2_fract timeperframe;
@@ -348,7 +349,6 @@ static const struct ov7251_mode_info ov7
 		.height = 480,
 		.data = ov7251_setting_vga,
 		.data_size = ARRAY_SIZE(ov7251_setting_vga),
-		.exposure_max = 1704,
 		.exposure_def = 504,
 		.vts = 0x6bc,
 		.timeperframe = {
@@ -361,7 +361,6 @@ static const struct ov7251_mode_info ov7
 		.height = 480,
 		.data = ov7251_setting_vga,
 		.data_size = ARRAY_SIZE(ov7251_setting_vga),
-		.exposure_max = 840,
 		.exposure_def = 504,
 		.vts = 0x35c,
 		.timeperframe = {
@@ -374,7 +373,6 @@ static const struct ov7251_mode_info ov7
 		.height = 480,
 		.data = ov7251_setting_vga,
 		.data_size = ARRAY_SIZE(ov7251_setting_vga),
-		.exposure_max = 552,
 		.exposure_def = 504,
 		.vts = 0x23c,
 		.timeperframe = {
@@ -915,8 +913,8 @@ static int ov7251_set_format(struct v4l2
 					 h_blank, 1, h_blank);
 		__v4l2_ctrl_s_ctrl(ov7251->hblank, h_blank);
 
-		ret = __v4l2_ctrl_modify_range(ov7251->exposure,
-					       1, new_mode->exposure_max,
+		ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1,
+					       new_mode->vts - OV7251_EXPOSURE_OFFSET,
 					       1, new_mode->exposure_def);
 		if (ret < 0)
 			goto exit;
@@ -1092,8 +1090,8 @@ static int ov7251_set_frame_interval(str
 	new_mode = ov7251_find_mode_by_ival(ov7251, &fi->interval);
 
 	if (new_mode != ov7251->current_mode) {
-		ret = __v4l2_ctrl_modify_range(ov7251->exposure,
-					       1, new_mode->exposure_max,
+		ret = __v4l2_ctrl_modify_range(ov7251->exposure, 1,
+					       new_mode->vts - OV7251_EXPOSURE_OFFSET,
 					       1, new_mode->exposure_def);
 		if (ret < 0)
 			goto exit;