Skip to contentSkip to conversation

media: i2c: ov9282: fix strobe timing and control handling

PATCH Superseded Richard Leitner 2 participants 15 messages Oct 14 – Oct 29

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover media: i2c: ov9282: fix strobe timing and control handling

This series fixes a set of related issues in the OV9282 strobe timing and control handling code.

The driver updated exposure- and flash duration-related control ranges in several places, but some of those updates used an incorrect timebase or were triggered too late, which could leave the controls clamped against stale values.

The series:
- checks the return value of the exposure range update
- drops the redundant vblank field
- converts probe error messages to dev_err_probe()
- fixes the 8-bit pixel rate (192 MHz instead of 200 MHz) and the corresponding line time calculation
- treats the strobe frame span as a line count and converts the flash duration accordingly
- fixes the flash duration range (min/max/default) and refreshes it on HBLANK changes, independent of the sensor power state
- refreshes the exposure and flash duration ranges on mode changes
- reprograms the strobe span when HBLANK changes

This series was tested with an ov9282 sensor on a vision components camera module attached to an i.MX8MP mainboard in a defined (dark) environment with a custom made LED flash pcb.

Any feedback is warmly welcome.

Thanks!

---
Changes in v2:
- New patch: fix the 8-bit pixel rate.
- New patch: re-write strobe span and HTS in s_ctrl when HBLANK changes
- Calculate line time in ns using 64-bit math instead of 32-bit µs
- Treat the strobe frame span as a line count and reuse the helpers of the exposure -> drop OV9282_STROBE_SPAN_FACTOR
- Flash duration range: min is 1 line, max is S32_MAX instead of the current exposure time, default is converted from the register default
- Refresh the flash duration range on HBLANK changes instead of on exposure/gain writes, so it is also correct while the sensor is idle
- Add missing "\n" to dev_err_probe() messages
- Add Fixes: and Cc: stable tags where applicable
- Reorder patches to keep non-fixes at the end
- Reword commit messages and subjects
- Thanks to Dave, Bryan and Sakari for the review!
- Link to v1: https://patch.msgid.link/[email protected]

To: Sakari Ailus <[email protected]>
To: Dave Stevenson <[email protected]>
To: Mauro Carvalho Chehab <[email protected]>
To: Daniele Alessandrelli <[email protected]>
To: Paul J. Murphy <[email protected]>
To: Martina Krasteva <[email protected]>
To: Richard Leitner <[email protected]>
To: Hans Verkuil <[email protected]>
To: Bryan O'Donoghue <[email protected]>
To: Jacopo Mondi <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>
Cc: Gyula Kelemen <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
Richard Leitner (8):
media: i2c: ov9282: handle error from exposure range update
media: i2c: ov9282: fix pixel rate and line time calculation
media: i2c: ov9282: fix flash duration to/from microseconds conversion
media: i2c: ov9282: fix flash duration range calculation
media: i2c: ov9282: add refresh of additional ranges on a mode change
media: i2c: ov9282: recalculate strobe span on HBLANK change
media: i2c: ov9282: drop redundant vblank field
media: i2c: ov9282: harmonize dev_err_probe usage

 drivers/media/i2c/ov9282.c | 236 +++++++++++++++++++++++++++------------------
 1 file changed, 144 insertions(+), 92 deletions(-)

---
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
change-id: 20260909-ov9282-fixes-da986dabc238

Best regards,

Signature · 1 line
Richard Leitner <[email protected]>
#1 lore raw

PATCH v2 1/8 media: i2c: ov9282: handle error from exposure range update

When setting V4L2_CID_VBLANK the V4L2 range for the exposure control is updated by v4l2_ctrl_modify_range(). This range update missed a return value check, which resulted in ov9282_set_ctrl() not returning on an error from that function call.

Fix this by checking the return value and returning it on error.

Fixes 14ea315bbeb75 ("media: i2c: Add ov9282 camera sensor driver")
---
 drivers/media/i2c/ov9282.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/media/i2c/ov9282.c +2 -0
index 5d301660a87d8..c10b2e205834e 100644
@@ -608,6 +608,8 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 					       ov9282->cur_mode->height -
 					       OV9282_EXPOSURE_OFFSET,
 					       1, OV9282_EXPOSURE_DEFAULT);
+		if (ret)
+			return ret;
 		break;
 	}
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH v2 2/8 media: i2c: ov9282: fix pixel rate and line time calculation

In reply to Richard Leitner

The OV9282_PIXEL_RATE_8BIT is calculated incorrectly from the OV9282_LINK_FREQ. The hardware scales the pixel rate by changing the multiplier from 0x50 (for 10 bit) to 0x60 (for 8 bit). Resulting in a x1.2 factor.

The current OV9282_PIXEL_RATE_8BIT calculation scales from /10 (for 10 bit) to /8 (for 8 bit). This results in a x1.25 factor.

As all the inputs to the pixel rate calculation are constant in this driver also define the OV9282_PIXEL_RATE_{8,10}BIT as constants. Additionally introduce a new function to calculate the current line time in nanoseconds and use it in ov9282_exposure_to_us().

Kudos to Dave Stevenson for pointing me into the right direction!

Fixes df60764ea5210 ("media: i2c: ov9282: dynamic flash_duration maximum")
Fixes 43ac0cac18f41 ("media: i2c: ov9282: Add support for 8bit readout")
---
 drivers/media/i2c/ov9282.c | 50 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 14 deletions(-)
drivers/media/i2c/ov9282.c +36 -14
index c10b2e205834e..5907f57b34523 100644
@@ -10,10 +10,12 @@
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/math.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/time64.h>
 
 #include <media/v4l2-cci.h>
 #include <media/v4l2-ctrls.h>
@@ -111,11 +113,14 @@
 #define OV9282_LINK_FREQ	400000000
 #define OV9282_NUM_DATA_LANES	2
 
-/* Pixel rate */
-#define OV9282_PIXEL_RATE_10BIT		(OV9282_LINK_FREQ * 2 * \
-					 OV9282_NUM_DATA_LANES / 10)
-#define OV9282_PIXEL_RATE_8BIT		(OV9282_LINK_FREQ * 2 * \
-					 OV9282_NUM_DATA_LANES / 8)
+/*
+ * Pixel rate: Derived from the PLL2 clock tree.
+ * For 10 bit it's exactly the same as the sample PLL configuration.
+ * For 8 bit the PLL2 multiplier (REG_PLL_CTRL_0D) is changed 0x50 -> 0x60,
+ * which results in x1.2 and therefore 160 MHz x 1.2 = 192 MHz.
+ */
+#define OV9282_PIXEL_RATE_10BIT	160000000
+#define OV9282_PIXEL_RATE_8BIT	192000000
 
 /*
  * OV9282 native and active pixel array size.
@@ -472,6 +477,32 @@ static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
 	return container_of(subdev, struct ov9282, sd);
 }
 
+/**
+ * ov9282_line_time_ns() - Calculate duration of one sensor line.
+ * @ov9282: pointer to ov9282 device
+ *
+ * Return: line time in nanoseconds.
+ */
+static u32 ov9282_line_time_ns(struct ov9282 *ov9282)
+{
+	u64 hts = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
+
+	return div_u64(hts * NSEC_PER_SEC, *ov9282->pixel_rate->p_cur.p_s64);
+}
+
+/**
+ * ov9282_exposure_to_us() - Convert an exposure register value to microseconds
+ * @ov9282: pointer to ov9282 device
+ * @exposure: exposure register value to convert
+ *
+ * Return: microsecond representation of the given exposure register value.
+ */
+static u32 ov9282_exposure_to_us(struct ov9282 *ov9282, u32 exposure)
+{
+	return div_u64((u64)exposure * ov9282_line_time_ns(ov9282),
+		       NSEC_PER_USEC);
+}
+
 /**
  * ov9282_update_controls() - Update control ranges based on streaming mode
  * @ov9282: pointer to ov9282 device
@@ -510,15 +541,6 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
 					mode->vblank_max, 1, mode->vblank);
 }
 
-static u32 ov9282_exposure_to_us(struct ov9282 *ov9282, u32 exposure)
-{
-	/* calculate exposure time in µs */
-	u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
-	u32 trow_us = frame_width / (ov9282->pixel_rate->val / 1000000UL);
-
-	return exposure * trow_us;
-}
-
 /**
  * ov9282_update_exp_gain() - Set updated exposure and gain
  * @ov9282: pointer to ov9282 device
Signature · 1 line
2.53.0
#4 lore raw

PATCH v2 3/8 media: i2c: ov9282: fix flash duration to/from microseconds conversion

In reply to Richard Leitner

Currently the flash duration is converted to/from microseconds using a fixed OV9282_STROBE_SPAN_FACTOR constant. This is inaccurate as it was found that the "step width of shift and span" (which is not documented further in the datasheet) scales with the line, so the span is counted in lines. This was verified by comparing the stobe pulses measured on the camera module against the calculated line times.

As this is the same conversion as for the exposure: rename those functions to a more generic ov9282_us_to_lines/ov9282_lines_to_us and use them also for the flash/strobe duration.

Fixes b95d8058a2786 ("media: i2c: ov9282: implement try_ctrl for strobe_duration")
---
 drivers/media/i2c/ov9282.c | 65 +++++++++++++++++-----------------------------
 1 file changed, 24 insertions(+), 41 deletions(-)
drivers/media/i2c/ov9282.c +24 -41
index 5907f57b34523..e2a1f875160b9 100644
@@ -136,8 +136,6 @@
 #define OV9282_REG_MIN		0x00
 #define OV9282_REG_MAX		0xfffff
 
-#define OV9282_STROBE_SPAN_FACTOR	192
-
 static const char * const ov9282_supply_names[] = {
 	"avdd",		/* Analog power */
 	"dovdd",	/* Digital I/O power */
@@ -491,18 +489,31 @@ static u32 ov9282_line_time_ns(struct ov9282 *ov9282)
 }
 
 /**
- * ov9282_exposure_to_us() - Convert an exposure register value to microseconds
+ * ov9282_lines_to_us() - Convert a number of lines to microseconds
  * @ov9282: pointer to ov9282 device
- * @exposure: exposure register value to convert
+ * @lines: line count to convert
  *
- * Return: microsecond representation of the given exposure register value.
+ * Return: microsecond representation of the given line count.
  */
-static u32 ov9282_exposure_to_us(struct ov9282 *ov9282, u32 exposure)
+static u32 ov9282_lines_to_us(struct ov9282 *ov9282, u32 lines)
 {
-	return div_u64((u64)exposure * ov9282_line_time_ns(ov9282),
+	return div_u64((u64)lines * ov9282_line_time_ns(ov9282),
 		       NSEC_PER_USEC);
 }
 
+/**
+ * ov9282_us_to_lines() - Convert µs to line count
+ * @ov9282: pointer to ov9282 device
+ * @duration: microseconds value to convert
+ *
+ * Return: number of lines fitting into the given duration
+ */
+static u32 ov9282_us_to_lines(struct ov9282 *ov9282, u32 duration)
+{
+	return DIV_ROUND_UP((u64)duration * NSEC_PER_USEC,
+			    ov9282_line_time_ns(ov9282));
+}
+
 /**
  * ov9282_update_controls() - Update control ranges based on streaming mode
  * @ov9282: pointer to ov9282 device
@@ -551,7 +562,7 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
  */
 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
 {
-	u32 exposure_us = ov9282_exposure_to_us(ov9282, exposure);
+	u32 exposure_us = ov9282_lines_to_us(ov9282, exposure);
 	int ret, ret_hold;
 
 	dev_dbg(ov9282->dev, "Set exp %u (~%u us), analog gain %u",
@@ -579,34 +590,6 @@ static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
 	return ret ? ret : ret_hold;
 }
 
-static u32 ov9282_us_to_flash_duration(struct ov9282 *ov9282, u32 value)
-{
-	/*
-	 * Calculate "strobe_frame_span" increments from a given value (µs).
-	 * This is quite tricky as "The step width of shift and span is
-	 * programmable under system clock domain.", but it's not documented
-	 * how to program this step width (at least in the datasheet available
-	 * to the author at time of writing).
-	 * The formula below is interpolated from different modes/framerates
-	 * and should work quite well for most settings.
-	 */
-	u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
-
-	return value * OV9282_STROBE_SPAN_FACTOR / frame_width;
-}
-
-static u32 ov9282_flash_duration_to_us(struct ov9282 *ov9282, u32 value)
-{
-	/*
-	 * Calculate back to microseconds from "strobe_frame_span" increments.
-	 * As the calculation in ov9282_us_to_flash_duration uses an integer
-	 * divison round up here.
-	 */
-	u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
-
-	return DIV_ROUND_UP(value * frame_width, OV9282_STROBE_SPAN_FACTOR);
-}
-
 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 {
 	struct ov9282 *ov9282 =
@@ -672,7 +655,7 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 		break;
 	case V4L2_CID_FLASH_DURATION:
 		ret = cci_write(ov9282->regmap, OV9282_REG_STROBE_FRAME_SPAN,
-				ov9282_us_to_flash_duration(ov9282, ctrl->val), NULL);
+				ov9282_us_to_lines(ov9282, ctrl->val), NULL);
 		break;
 	default:
 		dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
@@ -691,11 +674,11 @@ static int ov9282_try_ctrl(struct v4l2_ctrl *ctrl)
 
 	if (ctrl->id == V4L2_CID_FLASH_DURATION) {
 		u32 us = ctrl->val;
-		u32 fd = ov9282_us_to_flash_duration(ov9282, us);
+		u32 fd = ov9282_us_to_lines(ov9282, us);
 
 		/* get nearest strobe_duration value */
-		u32 us0 = ov9282_flash_duration_to_us(ov9282, fd);
-		u32 us1 = ov9282_flash_duration_to_us(ov9282, fd + 1);
+		u32 us0 = ov9282_lines_to_us(ov9282, fd);
+		u32 us1 = ov9282_lines_to_us(ov9282, fd + 1);
 
 		if (abs(us1 - us) < abs(us - us0))
 			ctrl->val = us1;
@@ -1277,7 +1260,7 @@ static int ov9282_init_controls(struct ov9282 *ov9282)
 	v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
 			  V4L2_CID_FLASH_STROBE_OE, 0, 1, 1, 0);
 
-	exposure_us = ov9282_exposure_to_us(ov9282, OV9282_EXPOSURE_DEFAULT);
+	exposure_us = ov9282_lines_to_us(ov9282, OV9282_EXPOSURE_DEFAULT);
 	ov9282->flash_duration =
 		v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
 				  V4L2_CID_FLASH_DURATION, 0, exposure_us, 1,
Signature · 1 line
2.53.0
#6 lore raw

PATCH v2 4/8 media: i2c: ov9282: fix flash duration range calculation

In reply to Richard Leitner

The flash_duration range calculation was broken. The maximum was calculated from the current exposure time. This limited the hardware without a strong reason. The minimum was fixed to 0, although the hardware treats register values 0 and 1 equally for the strobe_duration. The default value was never calculated from the raw register value to actual microseconds.

Furthermore the flash_duration range update sat inside ov9282_update_exp_gain(), which ov9282_set_ctrl() only reaches after the pm_runtime_get_if_in_use() early return. So with the sensor idle the range kept whatever value it had when it last streamed.

Fix those issues by correctly calculating the values on line time changes (i.e. HBLANK updates) as this is the "time base" for the register value.

The maximum is chosen to be S32_MAX microseconds. This was done to keep the code short and avoid additional back-and-forth conversions. As the hardware allows strobe durations from 1 up to U32_MAX line times, S32_MAX microseconds is always in this range. Line times for the supported modes, hblank values and pixel rates may vary between ~7.5 and ~205 microseconds.

Fixes b95d8058a2786 ("media: i2c: ov9282: implement try_ctrl for strobe_duration")
Fixes df60764ea5210 ("media: i2c: ov9282: dynamic flash_duration maximum")
---
 drivers/media/i2c/ov9282.c | 41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)
drivers/media/i2c/ov9282.c +31 -10
index e2a1f875160b9..e6920e18b7818 100644
@@ -514,6 +514,26 @@ static u32 ov9282_us_to_lines(struct ov9282 *ov9282, u32 duration)
 			    ov9282_line_time_ns(ov9282));
 }
 
+/**
+ * ov9282_update_ctrl_flash_duration() - Update flash_duration control range
+ * @ov9282: pointer to ov9282 device
+ *
+ * As the actual microseconds value of the flash_duration range (default,
+ * minimum, maximum) are dependent on the line time recalculate and apply them
+ * to the v4l2 property in this function.
+ *
+ * Return: 0 if successful, error code otherwise.
+ */
+static int ov9282_update_ctrl_flash_duration(struct ov9282 *ov9282)
+{
+	u32 fd_min_us = ov9282_lines_to_us(ov9282, 1);
+	u32 fd_def_us = ov9282_lines_to_us(ov9282,
+					   OV9282_STROBE_FRAME_SPAN_DEFAULT);
+
+	return __v4l2_ctrl_modify_range(ov9282->flash_duration, fd_min_us,
+					S32_MAX, 1, fd_def_us);
+}
+
 /**
  * ov9282_update_controls() - Update control ranges based on streaming mode
  * @ov9282: pointer to ov9282 device
@@ -577,12 +597,6 @@ static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
 		goto error_release_group_hold;
 
 	ret = cci_write(ov9282->regmap, OV9282_REG_AGAIN, gain, NULL);
-	if (ret)
-		goto error_release_group_hold;
-
-	ret = __v4l2_ctrl_modify_range(ov9282->flash_duration,
-				       0, exposure_us, 1,
-				       OV9282_STROBE_FRAME_SPAN_DEFAULT);
 
 error_release_group_hold:
 	ret_hold = cci_write(ov9282->regmap, OV9282_REG_HOLD, 0, NULL);
@@ -616,6 +630,11 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 		if (ret)
 			return ret;
 		break;
+	case V4L2_CID_HBLANK:
+		ret = ov9282_update_ctrl_flash_duration(ov9282);
+		if (ret)
+			return ret;
+		break;
 	}
 
 	/* Set controls only if sensor is in power on state */
@@ -1190,8 +1209,9 @@ static int ov9282_init_controls(struct ov9282 *ov9282)
 	const struct ov9282_mode *mode = ov9282->cur_mode;
 	struct v4l2_fwnode_device_properties props;
 	u32 hblank_min;
-	u32 exposure_us;
 	u32 lpfr;
+	u32 fd_min;
+	u32 fd_def;
 	int ret;
 
 	ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
@@ -1260,11 +1280,12 @@ static int ov9282_init_controls(struct ov9282 *ov9282)
 	v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
 			  V4L2_CID_FLASH_STROBE_OE, 0, 1, 1, 0);
 
-	exposure_us = ov9282_lines_to_us(ov9282, OV9282_EXPOSURE_DEFAULT);
+	fd_min = ov9282_lines_to_us(ov9282, 1);
+	fd_def = ov9282_lines_to_us(ov9282, OV9282_STROBE_FRAME_SPAN_DEFAULT);
 	ov9282->flash_duration =
 		v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
-				  V4L2_CID_FLASH_DURATION, 0, exposure_us, 1,
-				  OV9282_STROBE_FRAME_SPAN_DEFAULT);
+				  V4L2_CID_FLASH_DURATION, fd_min, S32_MAX, 1,
+				  fd_def);
 
 	ret = v4l2_fwnode_device_parse(ov9282->dev, &props);
 	if (!ret) {
Signature · 1 line
2.53.0
#8 lore raw

PATCH v2 5/8 media: i2c: ov9282: add refresh of additional ranges on a mode change

In reply to Richard Leitner

ov9282_update_controls() updates the pixel rate, hblank and vblank, but currently misses the exposure range and flash_duration, which are dependent on the frame height and therefore vblank.

Refresh both, and commit cur_mode and code in ov9282_set_pad_format() before the call so the refresh and any nested s_ctrl see the incoming format.

Fixes 14ea315bbeb75 ("media: i2c: Add ov9282 camera sensor driver")
---
 drivers/media/i2c/ov9282.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
drivers/media/i2c/ov9282.c +31 -5
index e6920e18b7818..b48c959b9c908 100644
@@ -548,6 +548,7 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
 {
 	u32 hblank_min;
 	s64 pixel_rate;
+	u32 lpfr;
 	int ret;
 
 	ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
@@ -568,8 +569,20 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
 	if (ret)
 		return ret;
 
-	return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
-					mode->vblank_max, 1, mode->vblank);
+	ret = __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
+				       mode->vblank_max, 1, mode->vblank);
+	if (ret)
+		return ret;
+
+	lpfr = ov9282->vblank_ctrl->val + mode->height;
+	ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl, OV9282_EXPOSURE_MIN,
+				       lpfr - OV9282_EXPOSURE_OFFSET,
+				       OV9282_EXPOSURE_STEP,
+				       OV9282_EXPOSURE_DEFAULT);
+	if (ret)
+		return ret;
+
+	return ov9282_update_ctrl_flash_duration(ov9282);
 }
 
 /**
@@ -832,10 +845,23 @@ static int ov9282_set_pad_format(struct v4l2_subdev *sd,
 		framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
 		*framefmt = fmt->format;
 	} else {
+		const struct ov9282_mode *old_mode = ov9282->cur_mode;
+		u32 old_code = ov9282->code;
+
+		/*
+		 * Commit before refreshing the ranges. ov9282_update_controls()
+		 * and the nested ov9282_set_ctrl() calls it triggers derive the
+		 * frame length and the line time from cur_mode and code, so
+		 * they have to describe the incoming format, not the outgoing
+		 * one.
+		 */
+		ov9282->cur_mode = mode;
+		ov9282->code = code;
+
 		ret = ov9282_update_controls(ov9282, mode, fmt);
-		if (!ret) {
-			ov9282->cur_mode = mode;
-			ov9282->code = code;
+		if (ret) {
+			ov9282->cur_mode = old_mode;
+			ov9282->code = old_code;
 		}
 	}
 
Signature · 1 line
2.53.0
#10 lore raw

PATCH v2 6/8 media: i2c: ov9282: recalculate strobe span on HBLANK change

In reply to Richard Leitner

When HBLANK changes the line time changes. This means the time base for the strobe duration calculation changed. This results in the v4l2 strobe_duration control value and the strobe_span register value being out of sync.

Fix this by recalculating the strobe_span register value on HBLANK writes using the currently configured strobe_duration. This results in the strobe_duration configured by userspace being kept, but the underlying register value being changed. To ensure the values are applied on the same frame guard the writes with OV9282_REG_HOLD.

Fixes 84ec7597ae1af ("media: i2c: ov9282: add strobe_duration v4l2 control")
---
 drivers/media/i2c/ov9282.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/media/i2c/ov9282.c +9 -2
index b48c959b9c908..5114fbf28a26c 100644
@@ -625,6 +625,7 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 	u32 exposure;
 	u32 lpfr;
 	int ret;
+	int ret_hold;
 
 	switch (ctrl->id) {
 	case V4L2_CID_VBLANK:
@@ -677,8 +678,14 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 				      OV9282_FLIP_BIT, ctrl->val ? OV9282_FLIP_BIT : 0, NULL);
 		break;
 	case V4L2_CID_HBLANK:
-		ret = cci_write(ov9282->regmap, OV9282_REG_TIMING_HTS,
-				(ctrl->val + ov9282->cur_mode->width) >> 1, NULL);
+		ret = cci_write(ov9282->regmap, OV9282_REG_HOLD, 0x01, NULL);
+		cci_write(ov9282->regmap, OV9282_REG_TIMING_HTS,
+			  (ctrl->val + ov9282->cur_mode->width) >> 1, &ret);
+		cci_write(ov9282->regmap, OV9282_REG_STROBE_FRAME_SPAN,
+			  ov9282_us_to_lines(ov9282, ov9282->flash_duration->cur.val),
+			  &ret);
+		ret_hold = cci_write(ov9282->regmap, OV9282_REG_HOLD, 0, NULL);
+		ret = ret ? ret : ret_hold;
 		break;
 	case V4L2_CID_FLASH_STROBE_OE:
 		ret = cci_update_bits(ov9282->regmap, OV9282_REG_OUTPUT_ENABLE6,
Signature · 1 line
2.53.0
#12 lore raw

PATCH v2 7/8 media: i2c: ov9282: drop redundant vblank field

In reply to Richard Leitner

The ov9282 main struct has a dedicated vblank field which was only used to duplicate the value of the vblank v4l2 control.

Therefore drop this struct field.

---
 drivers/media/i2c/ov9282.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/media/i2c/ov9282.c +4 -11
index 5114fbf28a26c..f877cb9e78c38 100644
@@ -196,7 +196,6 @@ struct ov9282_mode {
  * @again_ctrl: Pointer to analog gain control
  * @pixel_rate: Pointer to pixel rate control
  * @flash_duration: Pointer to flash duration control
- * @vblank: Vertical blanking in lines
  * @noncontinuous_clock: Selection of CSI2 noncontinuous clock mode
  * @cur_mode: Pointer to current selected sensor mode
  * @code: Mbus code currently selected
@@ -219,7 +218,6 @@ struct ov9282 {
 	};
 	struct v4l2_ctrl *pixel_rate;
 	struct v4l2_ctrl *flash_duration;
-	u32 vblank;
 	bool noncontinuous_clock;
 	const struct ov9282_mode *cur_mode;
 	u32 code;
@@ -629,17 +627,13 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 
 	switch (ctrl->id) {
 	case V4L2_CID_VBLANK:
-		ov9282->vblank = ov9282->vblank_ctrl->val;
-
+		lpfr = ctrl->val + ov9282->cur_mode->height;
 		dev_dbg(ov9282->dev, "Received vblank %u, new lpfr %u",
-			ov9282->vblank,
-			ov9282->vblank + ov9282->cur_mode->height);
+			ctrl->val, lpfr);
 
 		ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl,
 					       OV9282_EXPOSURE_MIN,
-					       ov9282->vblank +
-					       ov9282->cur_mode->height -
-					       OV9282_EXPOSURE_OFFSET,
+					       lpfr - OV9282_EXPOSURE_OFFSET,
 					       1, OV9282_EXPOSURE_DEFAULT);
 		if (ret)
 			return ret;
@@ -666,7 +660,7 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 		ret = ov9282_update_exp_gain(ov9282, exposure, analog_gain);
 		break;
 	case V4L2_CID_VBLANK:
-		lpfr = ov9282->vblank + ov9282->cur_mode->height;
+		lpfr = ctrl->val + ov9282->cur_mode->height;
 		ret = cci_write(ov9282->regmap, OV9282_REG_LPFR, lpfr, NULL);
 		break;
 	case V4L2_CID_HFLIP:
@@ -1382,7 +1376,6 @@ static int ov9282_probe(struct i2c_client *client)
 	/* Set default mode to first mode */
 	ov9282->cur_mode = &supported_modes[DEFAULT_MODE];
 	ov9282->code = MEDIA_BUS_FMT_Y10_1X10;
-	ov9282->vblank = ov9282->cur_mode->vblank;
 
 	ret = ov9282_init_controls(ov9282);
 	if (ret) {
Signature · 1 line
2.53.0
#13 lore raw

PATCH v2 8/8 media: i2c: ov9282: harmonize dev_err_probe usage

In reply to Richard Leitner

Use dev_err_probe() for all error messages during probing. This ensures there's a common "look-and-feel" in the drivers source code as well as the system log.

---
 drivers/media/i2c/ov9282.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
drivers/media/i2c/ov9282.c +15 -17
index f877cb9e78c38..8b01d9d43f0df 100644
@@ -1077,9 +1077,8 @@ static int ov9282_parse_hw_config(struct ov9282 *ov9282)
 	ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
 						     GPIOD_OUT_LOW);
 	if (IS_ERR(ov9282->reset_gpio)) {
-		dev_err(ov9282->dev, "failed to get reset gpio %pe",
-			ov9282->reset_gpio);
-		return PTR_ERR(ov9282->reset_gpio);
+		return dev_err_probe(ov9282->dev, PTR_ERR(ov9282->reset_gpio),
+				     "failed to get reset gpio\n");
 	}
 
 	/* Get sensor input clock */
@@ -1095,8 +1094,8 @@ static int ov9282_parse_hw_config(struct ov9282 *ov9282)
 
 	rate = clk_get_rate(ov9282->inclk);
 	if (rate != OV9282_INCLK_RATE) {
-		dev_err(ov9282->dev, "inclk frequency mismatch");
-		return -EINVAL;
+		return dev_err_probe(ov9282->dev, -EINVAL,
+				     "inclk frequency mismatch\n");
 	}
 
 	ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
@@ -1112,16 +1111,15 @@ static int ov9282_parse_hw_config(struct ov9282 *ov9282)
 		bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
 
 	if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
-		dev_err(ov9282->dev,
-			"number of CSI2 data lanes %d is not supported",
-			bus_cfg.bus.mipi_csi2.num_data_lanes);
-		ret = -EINVAL;
+		ret = dev_err_probe(ov9282->dev, -EINVAL,
+				    "number of CSI2 data lanes %d is not supported\n",
+				    bus_cfg.bus.mipi_csi2.num_data_lanes);
 		goto done_endpoint_free;
 	}
 
 	if (!bus_cfg.nr_of_link_frequencies) {
-		dev_err(ov9282->dev, "no link frequencies defined");
-		ret = -EINVAL;
+		ret = dev_err_probe(ov9282->dev, -EINVAL,
+				    "no link frequencies defined\n");
 		goto done_endpoint_free;
 	}
 
@@ -1352,8 +1350,8 @@ static int ov9282_probe(struct i2c_client *client)
 
 	ret = ov9282_parse_hw_config(ov9282);
 	if (ret) {
-		dev_err(ov9282->dev, "HW configuration is not supported");
-		return ret;
+		return dev_err_probe(ov9282->dev, ret,
+				     "HW configuration is not supported\n");
 	}
 
 	ov9282->regmap = devm_cci_regmap_init_i2c(client, 16);
@@ -1364,12 +1362,12 @@ static int ov9282_probe(struct i2c_client *client)
 	ret = ov9282_power_on(ov9282->dev);
 	if (ret)
 		return dev_err_probe(ov9282->dev, ret,
-				     "failed to power-on the sensor");
+				     "failed to power-on the sensor\n");
 
 	/* Check module identity */
 	ret = ov9282_detect(ov9282);
 	if (ret) {
-		dev_err(ov9282->dev, "failed to find sensor: %d", ret);
+		dev_err_probe(ov9282->dev, ret, "failed to find sensor\n");
 		goto error_power_off;
 	}
 
@@ -1379,7 +1377,7 @@ static int ov9282_probe(struct i2c_client *client)
 
 	ret = ov9282_init_controls(ov9282);
 	if (ret) {
-		dev_err(ov9282->dev, "failed to init controls: %d", ret);
+		dev_err_probe(ov9282->dev, ret, "failed to init controls\n");
 		goto error_power_off;
 	}
 
@@ -1392,7 +1390,7 @@ static int ov9282_probe(struct i2c_client *client)
 	ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
 	ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
 	if (ret) {
-		dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
+		dev_err_probe(ov9282->dev, ret, "failed to init entity pads\n");
 		goto error_handler_free;
 	}
 
Signature · 1 line
2.53.0
maintainer Reviewed-by on v2 2/8
#3 lore raw

Re: [PATCH v2 2/8] media: i2c: ov9282: fix pixel rate and line time calculation

In reply to Richard Leitner
Quoted 14 lines · Richard Leitner

The OV9282_PIXEL_RATE_8BIT is calculated incorrectly from the OV9282_LINK_FREQ. The hardware scales the pixel rate by changing the multiplier from 0x50 (for 10 bit) to 0x60 (for 8 bit). Resulting in a x1.2 factor.

The current OV9282_PIXEL_RATE_8BIT calculation scales from /10 (for 10 bit) to /8 (for 8 bit). This results in a x1.25 factor.

As all the inputs to the pixel rate calculation are constant in this driver also define the OV9282_PIXEL_RATE_{8,10}BIT as constants. Additionally introduce a new function to calculate the current line time in nanoseconds and use it in ov9282_exposure_to_us().

Quoted 7 lines

Kudos to Dave Stevenson for pointing me into the right direction!

Fixes df60764ea5210 ("media: i2c: ov9282: dynamic flash_duration maximum")
Fixes 43ac0cac18f41 ("media: i2c: ov9282: Add support for 8bit readout")
Quoted 95 lines
---
 drivers/media/i2c/ov9282.c | 50 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 14 deletions(-)
drivers/media/i2c/ov9282.c +36 -15
index c10b2e205834e..5907f57b34523 100644
@@ -10,10 +10,12 @@
 #include <linux/delay.h>
 #include <linux/i2c.h>
 #include <linux/math.h>
+#include <linux/math64.h>
 #include <linux/module.h>
 #include <linux/pm_runtime.h>
 #include <linux/regmap.h>
 #include <linux/regulator/consumer.h>
+#include <linux/time64.h>
 
 #include <media/v4l2-cci.h>
 #include <media/v4l2-ctrls.h>
@@ -111,11 +113,14 @@
 #define OV9282_LINK_FREQ       400000000
 #define OV9282_NUM_DATA_LANES  2
 
-/* Pixel rate */
-#define OV9282_PIXEL_RATE_10BIT                (OV9282_LINK_FREQ * 2 * \
-                                        OV9282_NUM_DATA_LANES / 10)
-#define OV9282_PIXEL_RATE_8BIT         (OV9282_LINK_FREQ * 2 * \
-                                        OV9282_NUM_DATA_LANES / 8)
+/*
+ * Pixel rate: Derived from the PLL2 clock tree.
+ * For 10 bit it's exactly the same as the sample PLL configuration.
+ * For 8 bit the PLL2 multiplier (REG_PLL_CTRL_0D) is changed 0x50 -> 0x60,
+ * which results in x1.2 and therefore 160 MHz x 1.2 = 192 MHz.
+ */
+#define OV9282_PIXEL_RATE_10BIT        160000000
+#define OV9282_PIXEL_RATE_8BIT 192000000
 
 /*
  * OV9282 native and active pixel array size.
@@ -472,6 +477,32 @@ static inline struct ov9282 *to_ov9282(struct v4l2_subdev *subdev)
        return container_of(subdev, struct ov9282, sd);
 }
 
+/**
+ * ov9282_line_time_ns() - Calculate duration of one sensor line.
+ * @ov9282: pointer to ov9282 device
+ *
+ * Return: line time in nanoseconds.
+ */
+static u32 ov9282_line_time_ns(struct ov9282 *ov9282)
+{
+       u64 hts = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
+
+       return div_u64(hts * NSEC_PER_SEC, *ov9282->pixel_rate->p_cur.p_s64);
+}
+
+/**
+ * ov9282_exposure_to_us() - Convert an exposure register value to microseconds
+ * @ov9282: pointer to ov9282 device
+ * @exposure: exposure register value to convert
+ *
+ * Return: microsecond representation of the given exposure register value.
+ */
+static u32 ov9282_exposure_to_us(struct ov9282 *ov9282, u32 exposure)
+{
+       return div_u64((u64)exposure * ov9282_line_time_ns(ov9282),
+                      NSEC_PER_USEC);
+}
+
 /**
  * ov9282_update_controls() - Update control ranges based on streaming mode
  * @ov9282: pointer to ov9282 device
@@ -510,15 +541,6 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
                                        mode->vblank_max, 1, mode->vblank);
 }
 
-static u32 ov9282_exposure_to_us(struct ov9282 *ov9282, u32 exposure)
-{
-       /* calculate exposure time in µs */
-       u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
-       u32 trow_us = frame_width / (ov9282->pixel_rate->val / 1000000UL);
-
-       return exposure * trow_us;
-}
-
 /**
  * ov9282_update_exp_gain() - Set updated exposure and gain
  * @ov9282: pointer to ov9282 device
 
--

2.53.0

maintainer Reviewed-by on v2 3/8
#5 lore raw

Re: [PATCH v2 3/8] media: i2c: ov9282: fix flash duration to/from microseconds conversion

In reply to Richard Leitner
Quoted 8 lines · Richard Leitner

Currently the flash duration is converted to/from microseconds using a fixed OV9282_STROBE_SPAN_FACTOR constant. This is inaccurate as it was found that the "step width of shift and span" (which is not documented further in the datasheet) scales with the line, so the span is counted in lines. This was verified by comparing the stobe pulses measured on the camera module against the calculated line times.

Quoted 7 lines

As this is the same conversion as for the exposure: rename those functions to a more generic ov9282_us_to_lines/ov9282_lines_to_us and use them also for the flash/strobe duration.

Fixes b95d8058a2786 ("media: i2c: ov9282: implement try_ctrl for strobe_duration")
Quoted 136 lines
---
 drivers/media/i2c/ov9282.c | 65 +++++++++++++++++-----------------------------
 1 file changed, 24 insertions(+), 41 deletions(-)
drivers/media/i2c/ov9282.c +24 -42
index 5907f57b34523..e2a1f875160b9 100644
@@ -136,8 +136,6 @@
 #define OV9282_REG_MIN         0x00
 #define OV9282_REG_MAX         0xfffff
 
-#define OV9282_STROBE_SPAN_FACTOR      192
-
 static const char * const ov9282_supply_names[] = {
        "avdd",         /* Analog power */
        "dovdd",        /* Digital I/O power */
@@ -491,18 +489,31 @@ static u32 ov9282_line_time_ns(struct ov9282 *ov9282)
 }
 
 /**
- * ov9282_exposure_to_us() - Convert an exposure register value to microseconds
+ * ov9282_lines_to_us() - Convert a number of lines to microseconds
  * @ov9282: pointer to ov9282 device
- * @exposure: exposure register value to convert
+ * @lines: line count to convert
  *
- * Return: microsecond representation of the given exposure register value.
+ * Return: microsecond representation of the given line count.
  */
-static u32 ov9282_exposure_to_us(struct ov9282 *ov9282, u32 exposure)
+static u32 ov9282_lines_to_us(struct ov9282 *ov9282, u32 lines)
 {
-       return div_u64((u64)exposure * ov9282_line_time_ns(ov9282),
+       return div_u64((u64)lines * ov9282_line_time_ns(ov9282),
                       NSEC_PER_USEC);
 }
 
+/**
+ * ov9282_us_to_lines() - Convert µs to line count
+ * @ov9282: pointer to ov9282 device
+ * @duration: microseconds value to convert
+ *
+ * Return: number of lines fitting into the given duration
+ */
+static u32 ov9282_us_to_lines(struct ov9282 *ov9282, u32 duration)
+{
+       return DIV_ROUND_UP((u64)duration * NSEC_PER_USEC,
+                           ov9282_line_time_ns(ov9282));
+}
+
 /**
  * ov9282_update_controls() - Update control ranges based on streaming mode
  * @ov9282: pointer to ov9282 device
@@ -551,7 +562,7 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
  */
 static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
 {
-       u32 exposure_us = ov9282_exposure_to_us(ov9282, exposure);
+       u32 exposure_us = ov9282_lines_to_us(ov9282, exposure);
        int ret, ret_hold;
 
        dev_dbg(ov9282->dev, "Set exp %u (~%u us), analog gain %u",
@@ -579,34 +590,6 @@ static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
        return ret ? ret : ret_hold;
 }
 
-static u32 ov9282_us_to_flash_duration(struct ov9282 *ov9282, u32 value)
-{
-       /*
-        * Calculate "strobe_frame_span" increments from a given value (µs).
-        * This is quite tricky as "The step width of shift and span is
-        * programmable under system clock domain.", but it's not documented
-        * how to program this step width (at least in the datasheet available
-        * to the author at time of writing).
-        * The formula below is interpolated from different modes/framerates
-        * and should work quite well for most settings.
-        */
-       u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
-
-       return value * OV9282_STROBE_SPAN_FACTOR / frame_width;
-}
-
-static u32 ov9282_flash_duration_to_us(struct ov9282 *ov9282, u32 value)
-{
-       /*
-        * Calculate back to microseconds from "strobe_frame_span" increments.
-        * As the calculation in ov9282_us_to_flash_duration uses an integer
-        * divison round up here.
-        */
-       u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
-
-       return DIV_ROUND_UP(value * frame_width, OV9282_STROBE_SPAN_FACTOR);
-}
-
 static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
 {
        struct ov9282 *ov9282 =
@@ -672,7 +655,7 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
                break;
        case V4L2_CID_FLASH_DURATION:
                ret = cci_write(ov9282->regmap, OV9282_REG_STROBE_FRAME_SPAN,
-                               ov9282_us_to_flash_duration(ov9282, ctrl->val), NULL);
+                               ov9282_us_to_lines(ov9282, ctrl->val), NULL);
                break;
        default:
                dev_err(ov9282->dev, "Invalid control %d", ctrl->id);
@@ -691,11 +674,11 @@ static int ov9282_try_ctrl(struct v4l2_ctrl *ctrl)
 
        if (ctrl->id == V4L2_CID_FLASH_DURATION) {
                u32 us = ctrl->val;
-               u32 fd = ov9282_us_to_flash_duration(ov9282, us);
+               u32 fd = ov9282_us_to_lines(ov9282, us);
 
                /* get nearest strobe_duration value */
-               u32 us0 = ov9282_flash_duration_to_us(ov9282, fd);
-               u32 us1 = ov9282_flash_duration_to_us(ov9282, fd + 1);
+               u32 us0 = ov9282_lines_to_us(ov9282, fd);
+               u32 us1 = ov9282_lines_to_us(ov9282, fd + 1);
 
                if (abs(us1 - us) < abs(us - us0))
                        ctrl->val = us1;
@@ -1277,7 +1260,7 @@ static int ov9282_init_controls(struct ov9282 *ov9282)
        v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
                          V4L2_CID_FLASH_STROBE_OE, 0, 1, 1, 0);
 
-       exposure_us = ov9282_exposure_to_us(ov9282, OV9282_EXPOSURE_DEFAULT);
+       exposure_us = ov9282_lines_to_us(ov9282, OV9282_EXPOSURE_DEFAULT);
        ov9282->flash_duration =
                v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
                                  V4L2_CID_FLASH_DURATION, 0, exposure_us, 1,
 
--

2.53.0

maintainer Reviewed-by on v2 4/8
#7 lore raw

Re: [PATCH v2 4/8] media: i2c: ov9282: fix flash duration range calculation

In reply to Richard Leitner
Quoted 11 lines · Richard Leitner

The flash_duration range calculation was broken. The maximum was calculated from the current exposure time. This limited the hardware without a strong reason. The minimum was fixed to 0, although the hardware treats register values 0 and 1 equally for the strobe_duration. The default value was never calculated from the raw register value to actual microseconds.

Furthermore the flash_duration range update sat inside ov9282_update_exp_gain(), which ov9282_set_ctrl() only reaches after the pm_runtime_get_if_in_use() early return.

only reaches if pm_runtime_get_if_in_use() doesn't return early.

Quoted 91 lines

So with the sensor idle
the range kept whatever value it had when it last streamed.

Fix those issues by correctly calculating the values on line time changes (i.e. HBLANK updates) as this is the "time base" for the register value.

The maximum is chosen to be S32_MAX microseconds. This was done to keep the code short and avoid additional back-and-forth conversions. As the hardware allows strobe durations from 1 up to U32_MAX line times, S32_MAX microseconds is always in this range. Line times for the supported modes, hblank values and pixel rates may vary between ~7.5 and ~205 microseconds.

Fixes b95d8058a2786 ("media: i2c: ov9282: implement try_ctrl for strobe_duration")
Fixes df60764ea5210 ("media: i2c: ov9282: dynamic flash_duration maximum")
---
 drivers/media/i2c/ov9282.c | 41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)
drivers/media/i2c/ov9282.c +27 -7
index e2a1f875160b9..e6920e18b7818 100644
@@ -514,6 +514,26 @@ static u32 ov9282_us_to_lines(struct ov9282 *ov9282, u32 duration)
                            ov9282_line_time_ns(ov9282));
 }
 
+/**
+ * ov9282_update_ctrl_flash_duration() - Update flash_duration control range
+ * @ov9282: pointer to ov9282 device
+ *
+ * As the actual microseconds value of the flash_duration range (default,
+ * minimum, maximum) are dependent on the line time recalculate and apply them
+ * to the v4l2 property in this function.
+ *
+ * Return: 0 if successful, error code otherwise.
+ */
+static int ov9282_update_ctrl_flash_duration(struct ov9282 *ov9282)
+{
+       u32 fd_min_us = ov9282_lines_to_us(ov9282, 1);
+       u32 fd_def_us = ov9282_lines_to_us(ov9282,
+                                          OV9282_STROBE_FRAME_SPAN_DEFAULT);
+
+       return __v4l2_ctrl_modify_range(ov9282->flash_duration, fd_min_us,
+                                       S32_MAX, 1, fd_def_us);
+}
+
 /**
  * ov9282_update_controls() - Update control ranges based on streaming mode
  * @ov9282: pointer to ov9282 device
@@ -577,12 +597,6 @@ static int ov9282_update_exp_gain(struct ov9282 *ov9282, u32 exposure, u32 gain)
                goto error_release_group_hold;
 
        ret = cci_write(ov9282->regmap, OV9282_REG_AGAIN, gain, NULL);
-       if (ret)
-               goto error_release_group_hold;
-
-       ret = __v4l2_ctrl_modify_range(ov9282->flash_duration,
-                                      0, exposure_us, 1,
-                                      OV9282_STROBE_FRAME_SPAN_DEFAULT);
 
 error_release_group_hold:
        ret_hold = cci_write(ov9282->regmap, OV9282_REG_HOLD, 0, NULL);
@@ -616,6 +630,11 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
                if (ret)
                        return ret;
                break;
+       case V4L2_CID_HBLANK:
+               ret = ov9282_update_ctrl_flash_duration(ov9282);
+               if (ret)
+                       return ret;
+               break;
        }
 
        /* Set controls only if sensor is in power on state */
@@ -1190,8 +1209,9 @@ static int ov9282_init_controls(struct ov9282 *ov9282)
        const struct ov9282_mode *mode = ov9282->cur_mode;
        struct v4l2_fwnode_device_properties props;
        u32 hblank_min;
-       u32 exposure_us;
        u32 lpfr;
+       u32 fd_min;
+       u32 fd_def;
        int ret;
 
        ret = v4l2_ctrl_handler_init(ctrl_hdlr, 12);
@@ -1260,11 +1280,12 @@ static int ov9282_init_controls(struct ov9282 *ov9282)
        v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
                          V4L2_CID_FLASH_STROBE_OE, 0, 1, 1, 0);

drivers/media/i2c/ov9282.c static int ov9282_init_controls(struct ov9282 *ov9282) · line 1288

Quoted 10 lines
drivers/media/i2c/ov9282.c +4 -3
 
-       exposure_us = ov9282_lines_to_us(ov9282, OV9282_EXPOSURE_DEFAULT);
+       fd_min = ov9282_lines_to_us(ov9282, 1);
+       fd_def = ov9282_lines_to_us(ov9282, OV9282_STROBE_FRAME_SPAN_DEFAULT);
        ov9282->flash_duration =
                v4l2_ctrl_new_std(ctrl_hdlr, &ov9282_ctrl_ops,
-                                 V4L2_CID_FLASH_DURATION, 0, exposure_us, 1,
-                                 OV9282_STROBE_FRAME_SPAN_DEFAULT);
+                                 V4L2_CID_FLASH_DURATION, fd_min, S32_MAX, 1,
+                                 fd_def);

Observation that this changes the default from OV9282_STROBE_FRAME_SPAN_DEFAULT to ov9282_lines_to_us(ov9282, OV9282_STROBE_FRAME_SPAN_DEFAULT);. I won't quibble over that though as any sane user will be setting their desired duration.

Ideally with the commit text fixed:

Quoted 8 lines
drivers/media/i2c/ov9282.c +0 -1
        ret = v4l2_fwnode_device_parse(ov9282->dev, &props);
        if (!ret) {
 
--

2.53.0

maintainer Reviewed-by on v2 5/8
#9 lore raw

Re: [PATCH v2 5/8] media: i2c: ov9282: add refresh of additional ranges on a mode change

In reply to Richard Leitner
Quoted 4 lines · Richard Leitner

ov9282_update_controls() updates the pixel rate, hblank and vblank, but currently misses the exposure range and flash_duration, which are dependent on the frame height and therefore vblank.

This ends up being the opposite of my comment on v1 which claimed "Both are dependent on the line time and therefore the pixel format."

exposure control range is dependent on height and vblank. flash_duration control range is dependent on width and hblank.

Just omit the dependency comment.

With that:

Quoted 78 lines

Refresh both, and commit cur_mode and code in ov9282_set_pad_format() before the call so the refresh and any nested s_ctrl see the incoming format.

Fixes 14ea315bbeb75 ("media: i2c: Add ov9282 camera sensor driver")
---
 drivers/media/i2c/ov9282.c | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
drivers/media/i2c/ov9282.c +31 -6
index e6920e18b7818..b48c959b9c908 100644
@@ -548,6 +548,7 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
 {
        u32 hblank_min;
        s64 pixel_rate;
+       u32 lpfr;
        int ret;
 
        ret = __v4l2_ctrl_s_ctrl(ov9282->link_freq_ctrl, mode->link_freq_idx);
@@ -568,8 +569,20 @@ static int ov9282_update_controls(struct ov9282 *ov9282,
        if (ret)
                return ret;
 
-       return __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
-                                       mode->vblank_max, 1, mode->vblank);
+       ret = __v4l2_ctrl_modify_range(ov9282->vblank_ctrl, mode->vblank_min,
+                                      mode->vblank_max, 1, mode->vblank);
+       if (ret)
+               return ret;
+
+       lpfr = ov9282->vblank_ctrl->val + mode->height;
+       ret = __v4l2_ctrl_modify_range(ov9282->exp_ctrl, OV9282_EXPOSURE_MIN,
+                                      lpfr - OV9282_EXPOSURE_OFFSET,
+                                      OV9282_EXPOSURE_STEP,
+                                      OV9282_EXPOSURE_DEFAULT);
+       if (ret)
+               return ret;
+
+       return ov9282_update_ctrl_flash_duration(ov9282);
 }
 
 /**
@@ -832,10 +845,23 @@ static int ov9282_set_pad_format(struct v4l2_subdev *sd,
                framefmt = v4l2_subdev_state_get_format(sd_state, fmt->pad);
                *framefmt = fmt->format;
        } else {
+               const struct ov9282_mode *old_mode = ov9282->cur_mode;
+               u32 old_code = ov9282->code;
+
+               /*
+                * Commit before refreshing the ranges. ov9282_update_controls()
+                * and the nested ov9282_set_ctrl() calls it triggers derive the
+                * frame length and the line time from cur_mode and code, so
+                * they have to describe the incoming format, not the outgoing
+                * one.
+                */
+               ov9282->cur_mode = mode;
+               ov9282->code = code;
+
                ret = ov9282_update_controls(ov9282, mode, fmt);
-               if (!ret) {
-                       ov9282->cur_mode = mode;
-                       ov9282->code = code;
+               if (ret) {
+                       ov9282->cur_mode = old_mode;
+                       ov9282->code = old_code;
                }
        }
 
 
--

2.53.0

maintainer Reviewed-by on v2 6/8
#11 lore raw

Re: [PATCH v2 6/8] media: i2c: ov9282: recalculate strobe span on HBLANK change

In reply to Richard Leitner
Quoted 11 lines · Richard Leitner

When HBLANK changes the line time changes. This means the time base for the strobe duration calculation changed. This results in the v4l2 strobe_duration control value and the strobe_span register value being out of sync.

Fix this by recalculating the strobe_span register value on HBLANK writes using the currently configured strobe_duration. This results in the strobe_duration configured by userspace being kept, but the underlying register value being changed. To ensure the values are applied on the same frame guard the writes with OV9282_REG_HOLD.

Group hold will do what is intended, but if control updates from userspace for eg gain and exposure generally get too widely spread then, we've got much bigger problems.

Quoted 41 lines
Fixes 84ec7597ae1af ("media: i2c: ov9282: add strobe_duration v4l2 control")
---
 drivers/media/i2c/ov9282.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/media/i2c/ov9282.c +9 -3
index b48c959b9c908..5114fbf28a26c 100644
@@ -625,6 +625,7 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
        u32 exposure;
        u32 lpfr;
        int ret;
+       int ret_hold;
 
        switch (ctrl->id) {
        case V4L2_CID_VBLANK:
@@ -677,8 +678,14 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
                                      OV9282_FLIP_BIT, ctrl->val ? OV9282_FLIP_BIT : 0, NULL);
                break;
        case V4L2_CID_HBLANK:
-               ret = cci_write(ov9282->regmap, OV9282_REG_TIMING_HTS,
-                               (ctrl->val + ov9282->cur_mode->width) >> 1, NULL);
+               ret = cci_write(ov9282->regmap, OV9282_REG_HOLD, 0x01, NULL);
+               cci_write(ov9282->regmap, OV9282_REG_TIMING_HTS,
+                         (ctrl->val + ov9282->cur_mode->width) >> 1, &ret);
+               cci_write(ov9282->regmap, OV9282_REG_STROBE_FRAME_SPAN,
+                         ov9282_us_to_lines(ov9282, ov9282->flash_duration->cur.val),
+                         &ret);
+               ret_hold = cci_write(ov9282->regmap, OV9282_REG_HOLD, 0, NULL);
+               ret = ret ? ret : ret_hold;
                break;
        case V4L2_CID_FLASH_STROBE_OE:
                ret = cci_update_bits(ov9282->regmap, OV9282_REG_OUTPUT_ENABLE6,
 
--

2.53.0

maintainer Reviewed-by on v2 8/8
#14 lore raw

Re: [PATCH v2 8/8] media: i2c: ov9282: harmonize dev_err_probe usage

In reply to Richard Leitner
Quoted 6 lines · Richard Leitner

Use dev_err_probe() for all error messages during probing. This ensures there's a common "look-and-feel" in the drivers source code as well as the system log.

Quoted 102 lines
---
 drivers/media/i2c/ov9282.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
drivers/media/i2c/ov9282.c +15 -18
index f877cb9e78c38..8b01d9d43f0df 100644
@@ -1077,9 +1077,8 @@ static int ov9282_parse_hw_config(struct ov9282 *ov9282)
        ov9282->reset_gpio = devm_gpiod_get_optional(ov9282->dev, "reset",
                                                     GPIOD_OUT_LOW);
        if (IS_ERR(ov9282->reset_gpio)) {
-               dev_err(ov9282->dev, "failed to get reset gpio %pe",
-                       ov9282->reset_gpio);
-               return PTR_ERR(ov9282->reset_gpio);
+               return dev_err_probe(ov9282->dev, PTR_ERR(ov9282->reset_gpio),
+                                    "failed to get reset gpio\n");
        }
 
        /* Get sensor input clock */
@@ -1095,8 +1094,8 @@ static int ov9282_parse_hw_config(struct ov9282 *ov9282)
 
        rate = clk_get_rate(ov9282->inclk);
        if (rate != OV9282_INCLK_RATE) {
-               dev_err(ov9282->dev, "inclk frequency mismatch");
-               return -EINVAL;
+               return dev_err_probe(ov9282->dev, -EINVAL,
+                                    "inclk frequency mismatch\n");
        }
 
        ep = fwnode_graph_get_next_endpoint(fwnode, NULL);
@@ -1112,16 +1111,15 @@ static int ov9282_parse_hw_config(struct ov9282 *ov9282)
                bus_cfg.bus.mipi_csi2.flags & V4L2_MBUS_CSI2_NONCONTINUOUS_CLOCK;
 
        if (bus_cfg.bus.mipi_csi2.num_data_lanes != OV9282_NUM_DATA_LANES) {
-               dev_err(ov9282->dev,
-                       "number of CSI2 data lanes %d is not supported",
-                       bus_cfg.bus.mipi_csi2.num_data_lanes);
-               ret = -EINVAL;
+               ret = dev_err_probe(ov9282->dev, -EINVAL,
+                                   "number of CSI2 data lanes %d is not supported\n",
+                                   bus_cfg.bus.mipi_csi2.num_data_lanes);
                goto done_endpoint_free;
        }
 
        if (!bus_cfg.nr_of_link_frequencies) {
-               dev_err(ov9282->dev, "no link frequencies defined");
-               ret = -EINVAL;
+               ret = dev_err_probe(ov9282->dev, -EINVAL,
+                                   "no link frequencies defined\n");
                goto done_endpoint_free;
        }
 
@@ -1352,8 +1350,8 @@ static int ov9282_probe(struct i2c_client *client)
 
        ret = ov9282_parse_hw_config(ov9282);
        if (ret) {
-               dev_err(ov9282->dev, "HW configuration is not supported");
-               return ret;
+               return dev_err_probe(ov9282->dev, ret,
+                                    "HW configuration is not supported\n");
        }
 
        ov9282->regmap = devm_cci_regmap_init_i2c(client, 16);
@@ -1364,12 +1362,12 @@ static int ov9282_probe(struct i2c_client *client)
        ret = ov9282_power_on(ov9282->dev);
        if (ret)
                return dev_err_probe(ov9282->dev, ret,
-                                    "failed to power-on the sensor");
+                                    "failed to power-on the sensor\n");
 
        /* Check module identity */
        ret = ov9282_detect(ov9282);
        if (ret) {
-               dev_err(ov9282->dev, "failed to find sensor: %d", ret);
+               dev_err_probe(ov9282->dev, ret, "failed to find sensor\n");
                goto error_power_off;
        }
 
@@ -1379,7 +1377,7 @@ static int ov9282_probe(struct i2c_client *client)
 
        ret = ov9282_init_controls(ov9282);
        if (ret) {
-               dev_err(ov9282->dev, "failed to init controls: %d", ret);
+               dev_err_probe(ov9282->dev, ret, "failed to init controls\n");
                goto error_power_off;
        }
 
@@ -1392,7 +1390,7 @@ static int ov9282_probe(struct i2c_client *client)
        ov9282->pad.flags = MEDIA_PAD_FL_SOURCE;
        ret = media_entity_pads_init(&ov9282->sd.entity, 1, &ov9282->pad);
        if (ret) {
-               dev_err(ov9282->dev, "failed to init entity pads: %d", ret);
+               dev_err_probe(ov9282->dev, ret, "failed to init entity pads\n");
                goto error_handler_free;
        }
 
 
--

2.53.0