Skip to contentSkip to conversation

media: i2c: ov8865: Fix long frame lengths, VBLANK on mode change and gain steps

PATCH New Marco Ziliani 1 participant 4 messages Dec 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover media: i2c: ov8865: Fix long frame lengths, VBLANK on mode change and gain steps

Three fixes for the ov8865, found on the Microsoft Surface Pro 5 and Surface Book 2 (IPU3) and tested on a Surface Pro 7+ (IPU6) as well.

Patch 1 fixes the VTS high byte mask, which keeps only bits 11:8 and wraps any frame length above 4095 lines. libcamera now sets VBLANK on the IPU3 pipeline and its AGC raises the exposure up to the frame length, so with libcamera master the rear camera of the Surface Book 2 goes black, or the stream stalls. It would be good to have this fixed before the next libcamera release.

Patch 2 resets VBLANK to the mode default on every active format change, so that a frame length left over from the previous mode or user doesn't stick.

Patch 3 lowers the analogue gain step from 128 (integer gains only) to 1, which the sensor resolves to about 1/128 of the current gain.

Tested on a Surface Pro 5 with the media next driver plus this series, built for the running kernel, against the unpatched driver, at 800x600:

unpatched patched

  VBLANK 5400          VTS read 1904, 30.25 fps  VTS 6000, 9.60 fps
  VBLANK 14760         VTS read 3072, 18.75 fps  VTS 15360, 3.75 fps
  VBLANK after full -> 800x600
                       14760 (stale)            40 (mode default)

analogue gain step 128 1

The patches come from the linux-surface tree [1], where irishchug (Surface Book 2) and Firas Sharary (Surface Pro 7+) tested them.

I used an AI coding assistant to draft these patches and the changelogs; I reviewed them and can answer for them.

[1] https://github.com/linux-surface/kernel/pull/181

---
Marco Ziliani (3):
media: i2c: ov8865: Fix the VTS high byte mask
media: i2c: ov8865: Reset VBLANK on mode change
media: i2c: ov8865: Allow fine analogue gain steps

 drivers/media/i2c/ov8865.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

---
base-commit: d7e7bca7169873b46e857c025c10f3336c1258d8
change-id: 20261203-ov8865-fixes-033b44cfd7a4

Best regards,

Signature · 1 line
Marco Ziliani <[email protected]>
#1 lore raw

PATCH 1/3 media: i2c: ov8865: Fix the VTS high byte mask

OV8865_VTS_H() keeps only bits 11:8 of the frame length, while the VBLANK control allows frame lengths up to 0xffff lines. Any frame length above 4095 lines therefore wraps around: the sensor gets a much shorter frame than the driver and userspace believe, and an exposure that can be far longer than the frame. The frame then stretches to the exposure and the image is black, or the frame ends up shorter than the active lines and the stream stalls.

libcamera now sets VBLANK on the IPU3 pipeline and its AGC raises the exposure up to the frame length, so this is hit in practice: on a Surface Book 2, with the 800x600 mode, VBLANK 14760 (VTS 15360, 0x3c00) reached the sensor as 0x0c00 (3072 lines) with an exposure of 15352 lines, and the rear camera went black at 3.75 fps. In the 1632x1224 mode, VBLANK 3769 (VTS 4993) reached it as 897 lines, shorter than the 1224 active lines, and the stream stalled with CSI-2 errors.

The VTS register is 15 bits wide (0x380e bits 6:0 and 0x380f). Keep bits 14:8 in the high byte and limit the frame length to 0x7fff.

---
 drivers/media/i2c/ov8865.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/i2c/ov8865.c +2 -2
index 5a59cca8e..855da1d04 100644
@@ -180,10 +180,10 @@
 #define OV8865_HTS_L_REG			0x380d
 #define OV8865_HTS_L(v)				((v) & GENMASK(7, 0))
 #define OV8865_VTS_H_REG			0x380e
-#define OV8865_VTS_H(v)				(((v) & GENMASK(11, 8)) >> 8)
+#define OV8865_VTS_H(v)				(((v) & GENMASK(14, 8)) >> 8)
 #define OV8865_VTS_L_REG			0x380f
 #define OV8865_VTS_L(v)				((v) & GENMASK(7, 0))
-#define OV8865_TIMING_MAX_VTS			0xffff
+#define OV8865_TIMING_MAX_VTS			0x7fff
 #define OV8865_TIMING_MIN_VTS			0x04
 #define OV8865_OFFSET_X_H_REG			0x3810
 #define OV8865_OFFSET_X_H(v)			(((v) & GENMASK(15, 8)) >> 8)
Signature · 1 line
2.56.1
#2 lore raw

PATCH 2/3 media: i2c: ov8865: Reset VBLANK on mode change

In reply to Marco Ziliani

set_fmt() updates the VBLANK range and default for the new mode but keeps the current value when it is still in range. A value left over from the previous mode then keeps that mode's frame length: after the full-size mode (VBLANK 22) the 800x600 mode runs faster than its nominal 90 fps, and the VTS a previous user set stays on the sensor for users that never set VBLANK themselves. On a Surface Book 2, after one run of libcamera master, which sets VBLANK, a libcamera 0.7.2 run kept that frame length and ran at 18.8 fps instead of 91 fps.

Set VBLANK to the mode default on every format change on the active state, as other sensor drivers do.

Fixes 9293aafe3745 ("media: i2c: Add vblank control to ov8865")
---
 drivers/media/i2c/ov8865.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/media/i2c/ov8865.c +8 -0
index 855da1d04..e85e4c974 100644
@@ -2756,6 +2756,14 @@ static int ov8865_set_fmt(struct v4l2_subdev *subdev,
 				 OV8865_TIMING_MAX_VTS - mode->output_size_y,
 				 1, mode->vts - mode->output_size_y);
 
+	/*
+	 * Start every mode from its default frame length: a VBLANK value left
+	 * over from the previous mode would keep that mode's timing.
+	 */
+	if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE)
+		__v4l2_ctrl_s_ctrl(sensor->ctrls.vblank,
+				   mode->vts - mode->output_size_y);
+
 	hblank = mode->hts - mode->output_size_x;
 	__v4l2_ctrl_modify_range(sensor->ctrls.hblank, hblank, hblank, 1,
 				 hblank);
Signature · 1 line
2.56.1
#3 lore raw

PATCH 3/3 media: i2c: ov8865: Allow fine analogue gain steps

In reply to Marco Ziliani

The analogue gain register (0x3508/0x3509) holds the gain with 7 fractional bits, 128 being 1x, but the control has a step of 128, which limits the gain to the integer values 1x to 16x. An AGC can then only move in steps of a whole stop or more at low gain, and it oscillates between adjacent steps: on a Surface Book 2 at about 2.5x-2.9x gain, libcamera's AGC gave a luma standard deviation of 1.4-1.7 with a step of 16, 0.5-0.8 with a step of 8 and 0.03-0.10 with a step of 1. On a Surface Pro 7+ the software ISP stayed at a low gain in a dark room with a step of 128, and ramped up to the maximum with a step of 1.

Set the step to 1. The sensor resolves about 1/128 of the current gain (1/64x at 2x, 1/32x at 4x, 1/16x at 8x) and ignores finer codes, so the finer steps stay monotonic.

---
 drivers/media/i2c/ov8865.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/ov8865.c +1 -1
index e85e4c974..0977f2b3b 100644
@@ -2528,7 +2528,7 @@ static int ov8865_ctrls_init(struct ov8865_sensor *sensor)
 
 	/* Gain */
 
-	v4l2_ctrl_new_std(handler, ops, V4L2_CID_ANALOGUE_GAIN, 128, 2048, 128,
+	v4l2_ctrl_new_std(handler, ops, V4L2_CID_ANALOGUE_GAIN, 128, 2048, 1,
 			  128);
 
 	/* White Balance */
Signature · 1 line
2.56.1