* [PATCH v3 1/4] media: i2c: ov5693: Keep driver-managed registers out of the global list
2026-11-27 23:11 [PATCH v3 0/4] media: i2c: ov5693: Fix mode changes, flips and autosuspend Marco Ziliani
@ 2026-11-27 23:11 ` Marco Ziliani
2026-11-27 23:11 ` [PATCH v3 2/4] media: i2c: ov5693: Write format and crop changes to a powered sensor Marco Ziliani
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Marco Ziliani @ 2026-11-27 23:11 UTC (permalink / raw)
To: Daniel Scally, Sakari Ailus, Mauro Carvalho Chehab
Cc: linux-media, Chris Simpson, Tony Garcia, Simon Kruse, D. Manresa,
linux-kernel, Marco Ziliani
ov5693_global_regs writes three registers that the driver also programs:
0x350b, the low byte of the analogue gain set by the gain control, and
FORMAT1/FORMAT2 (0x3820/0x3821), whose binning and flip bits follow the
format and the flip controls. The values from the list are overwritten
anyway, so the overlap only hides which code owns those registers.
Drop the three entries. Program the FORMAT registers in full from
ov5693_mode_configure() instead, except for the flip bits, which stay
with the flip controls. That keeps the values the list used to write:
0x00 for FORMAT1, and bits 4:3 of FORMAT2, which the vendor sequence
sets and whose purpose isn't documented.
Suggested-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Signed-off-by: Marco Ziliani <rylos78@gmail.com>
---
drivers/media/i2c/ov5693.c | 27 ++++++++++++++++-----------
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/drivers/media/i2c/ov5693.c b/drivers/media/i2c/ov5693.c
index 56599f9ca..d221f7951 100644
--- a/drivers/media/i2c/ov5693.c
+++ b/drivers/media/i2c/ov5693.c
@@ -94,11 +94,17 @@
#define OV5693_FORMAT1_FLIP_VERT_ISP_EN BIT(6)
#define OV5693_FORMAT1_FLIP_VERT_SENSOR_EN BIT(1)
#define OV5693_FORMAT1_VBIN_EN BIT(0)
+#define OV5693_FORMAT1_FLIP_VERT_MASK (OV5693_FORMAT1_FLIP_VERT_ISP_EN | \
+ OV5693_FORMAT1_FLIP_VERT_SENSOR_EN)
#define OV5693_FORMAT2_REG CCI_REG8(0x3821)
#define OV5693_FORMAT2_HDR_EN BIT(7)
#define OV5693_FORMAT2_FLIP_HORZ_ISP_EN BIT(2)
#define OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN BIT(1)
#define OV5693_FORMAT2_HBIN_EN BIT(0)
+#define OV5693_FORMAT2_FLIP_HORZ_MASK (OV5693_FORMAT2_FLIP_HORZ_ISP_EN | \
+ OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN)
+/* Set by the vendor initialisation sequence, purpose undocumented */
+#define OV5693_FORMAT2_VENDOR_BITS GENMASK(4, 3)
#define OV5693_ISP_CTRL2_REG CCI_REG8(0x5002)
#define OV5693_ISP_SCALE_ENABLE BIT(7)
@@ -203,7 +209,6 @@ static const struct cci_reg_sequence ov5693_global_regs[] = {
{CCI_REG8(0x3106), 0x00},
{CCI_REG8(0x3406), 0x01},
{CCI_REG8(0x3503), 0x07},
- {CCI_REG8(0x350b), 0x40},
{CCI_REG8(0x3601), 0x0a},
{CCI_REG8(0x3602), 0x38},
{CCI_REG8(0x3612), 0x80},
@@ -322,8 +327,6 @@ static const struct cci_reg_sequence ov5693_global_regs[] = {
{CCI_REG8(0x5848), 0x01},
{CCI_REG8(0x5849), 0x0c},
{CCI_REG8(0x5e10), 0x0c},
- {CCI_REG8(0x3820), 0x00},
- {CCI_REG8(0x3821), 0x1e},
{CCI_REG8(0x5041), 0x14}
};
@@ -364,8 +367,7 @@ static const u8 ov5693_test_pattern_bits[] = {
static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
bool enable)
{
- u8 bits = OV5693_FORMAT1_FLIP_VERT_ISP_EN |
- OV5693_FORMAT1_FLIP_VERT_SENSOR_EN;
+ u8 bits = OV5693_FORMAT1_FLIP_VERT_MASK;
int ret;
ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG, bits,
@@ -379,8 +381,7 @@ static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
bool enable)
{
- u8 bits = OV5693_FORMAT2_FLIP_HORZ_ISP_EN |
- OV5693_FORMAT2_FLIP_HORZ_SENSOR_EN;
+ u8 bits = OV5693_FORMAT2_FLIP_HORZ_MASK;
int ret;
ret = cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG, bits,
@@ -603,14 +604,18 @@ static int ov5693_mode_configure(struct ov5693_device *ov5693)
cci_write(ov5693->regmap, OV5693_SUB_INC_Y_REG,
((mode->inc_y_odd << 4) & 0xf0) | 0x01, &ret);
- /* Binning */
+ /*
+ * Binning. Program all the bits of the FORMAT registers except the
+ * flips, which follow the flip controls.
+ */
cci_update_bits(ov5693->regmap, OV5693_FORMAT1_REG,
- OV5693_FORMAT1_VBIN_EN,
+ GENMASK(7, 0) & ~OV5693_FORMAT1_FLIP_VERT_MASK,
mode->binning_y ? OV5693_FORMAT1_VBIN_EN : 0, &ret);
cci_update_bits(ov5693->regmap, OV5693_FORMAT2_REG,
- OV5693_FORMAT2_HBIN_EN,
- mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0, &ret);
+ GENMASK(7, 0) & ~OV5693_FORMAT2_FLIP_HORZ_MASK,
+ OV5693_FORMAT2_VENDOR_BITS |
+ (mode->binning_x ? OV5693_FORMAT2_HBIN_EN : 0), &ret);
return ret;
}
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v3 2/4] media: i2c: ov5693: Write format and crop changes to a powered sensor
2026-11-27 23:11 [PATCH v3 0/4] media: i2c: ov5693: Fix mode changes, flips and autosuspend Marco Ziliani
2026-11-27 23:11 ` [PATCH v3 1/4] media: i2c: ov5693: Keep driver-managed registers out of the global list Marco Ziliani
@ 2026-11-27 23:11 ` Marco Ziliani
2026-11-27 23:11 ` [PATCH v3 3/4] media: i2c: ov5693: Keep the Bayer order when flipping at full resolution Marco Ziliani
2026-11-27 23:11 ` [PATCH v3 4/4] media: i2c: ov5693: Use autosuspend when stopping the stream Marco Ziliani
3 siblings, 0 replies; 5+ messages in thread
From: Marco Ziliani @ 2026-11-27 23:11 UTC (permalink / raw)
To: Daniel Scally, Sakari Ailus, Mauro Carvalho Chehab
Cc: linux-media, Chris Simpson, Tony Garcia, Simon Kruse, D. Manresa,
linux-kernel, Marco Ziliani
ov5693_mode_configure() is only called on power-on, from
ov5693_sensor_init(). If the sensor stays powered between two streams
with different formats, for instance during the runtime PM autosuspend
delay or with runtime PM disabled, the registers for the second format
are never written and the sensor keeps streaming the first one.
Keep writing the mode registers on power-on, and also write them from
set_fmt() and set_selection() on the active state when the sensor is
powered. pm_runtime_get_if_active() is used rather than
pm_runtime_get_if_in_use() so that a change made during the autosuspend
delay is written too.
Writing them while streaming would change the readout under the
receiver, so refuse format and crop changes on the active state with
-EBUSY while the sensor streams. set_fmt() and set_selection() now take
the driver lock for the whole operation.
Fixes: 89aef879cb53 ("media: i2c: Add support for ov5693 sensor")
Link: https://github.com/linux-surface/kernel/pull/181
Signed-off-by: Marco Ziliani <rylos78@gmail.com>
---
drivers/media/i2c/ov5693.c | 46 +++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 41 insertions(+), 5 deletions(-)
diff --git a/drivers/media/i2c/ov5693.c b/drivers/media/i2c/ov5693.c
index d221f7951..3cab73b89 100644
--- a/drivers/media/i2c/ov5693.c
+++ b/drivers/media/i2c/ov5693.c
@@ -168,6 +168,9 @@ struct ov5693_device {
unsigned int vts;
} mode;
+ /* Protected by lock */
+ bool streaming;
+
struct v4l2_subdev sd;
struct media_pad pad;
@@ -832,6 +835,27 @@ static int ov5693_get_fmt(struct v4l2_subdev *sd,
return 0;
}
+/*
+ * Program a new format or crop rectangle on a powered sensor. A sensor that is
+ * powered off gets it at the next power-on, from ov5693_sensor_init().
+ */
+static int ov5693_mode_update(struct ov5693_device *ov5693)
+{
+ int pm, ret;
+
+ pm = pm_runtime_get_if_active(ov5693->dev);
+ if (!pm)
+ return 0;
+
+ ret = ov5693_mode_configure(ov5693);
+
+ /* A negative value means runtime PM is disabled, with the sensor on */
+ if (pm > 0)
+ pm_runtime_put(ov5693->dev);
+
+ return ret;
+}
+
static int ov5693_set_fmt(struct v4l2_subdev *sd,
const struct v4l2_subdev_client_info *ci,
struct v4l2_subdev_state *state,
@@ -845,6 +869,11 @@ static int ov5693_set_fmt(struct v4l2_subdev *sd,
unsigned int hblank;
int exposure_max;
+ guard(mutex)(&ov5693->lock);
+
+ if (format->which == V4L2_SUBDEV_FORMAT_ACTIVE && ov5693->streaming)
+ return -EBUSY;
+
crop = __ov5693_get_pad_crop(ov5693, state, format->pad, format->which);
/*
@@ -877,8 +906,6 @@ static int ov5693_set_fmt(struct v4l2_subdev *sd,
if (format->which == V4L2_SUBDEV_FORMAT_TRY)
return 0;
- mutex_lock(&ov5693->lock);
-
ov5693->mode.binning_x = hratio > 1;
ov5693->mode.inc_x_odd = hratio > 1 ? 3 : 1;
ov5693->mode.binning_y = vratio > 1;
@@ -904,8 +931,7 @@ static int ov5693_set_fmt(struct v4l2_subdev *sd,
min(ov5693->ctrls.exposure->val,
exposure_max));
- mutex_unlock(&ov5693->lock);
- return 0;
+ return ov5693_mode_update(ov5693);
}
static int ov5693_get_selection(struct v4l2_subdev *sd,
@@ -955,6 +981,11 @@ static int ov5693_set_selection(struct v4l2_subdev *sd,
if (sel->target != V4L2_SEL_TGT_CROP)
return -EINVAL;
+ guard(mutex)(&ov5693->lock);
+
+ if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE && ov5693->streaming)
+ return -EBUSY;
+
/*
* Clamp the boundaries of the crop rectangle to the size of the sensor
* pixel array. Align to multiples of 2 to ensure Bayer pattern isn't
@@ -991,7 +1022,10 @@ static int ov5693_set_selection(struct v4l2_subdev *sd,
*__crop = rect;
sel->r = rect;
- return 0;
+ if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
+ return 0;
+
+ return ov5693_mode_update(ov5693);
}
static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
@@ -1012,10 +1046,12 @@ static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
}
ret = ov5693_enable_streaming(ov5693, true);
+ ov5693->streaming = !ret;
mutex_unlock(&ov5693->lock);
} else {
mutex_lock(&ov5693->lock);
ret = ov5693_enable_streaming(ov5693, false);
+ ov5693->streaming = false;
mutex_unlock(&ov5693->lock);
}
if (ret)
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v3 3/4] media: i2c: ov5693: Keep the Bayer order when flipping at full resolution
2026-11-27 23:11 [PATCH v3 0/4] media: i2c: ov5693: Fix mode changes, flips and autosuspend Marco Ziliani
2026-11-27 23:11 ` [PATCH v3 1/4] media: i2c: ov5693: Keep driver-managed registers out of the global list Marco Ziliani
2026-11-27 23:11 ` [PATCH v3 2/4] media: i2c: ov5693: Write format and crop changes to a powered sensor Marco Ziliani
@ 2026-11-27 23:11 ` Marco Ziliani
2026-11-27 23:11 ` [PATCH v3 4/4] media: i2c: ov5693: Use autosuspend when stopping the stream Marco Ziliani
3 siblings, 0 replies; 5+ messages in thread
From: Marco Ziliani @ 2026-11-27 23:11 UTC (permalink / raw)
To: Daniel Scally, Sakari Ailus, Mauro Carvalho Chehab
Cc: linux-media, Chris Simpson, Tony Garcia, Simon Kruse, D. Manresa,
linux-kernel, Marco Ziliani
In the full (unbinned) readout each of the horizontal and vertical
flips moves the Bayer phase by one column. With exactly one flip
enabled the CSI-2 receiver gets GBRG instead of the SBGGR10 the driver
reports, which gives a magenta and green image; with both enabled the
two shifts cancel out.
The crop start is only honoured in steps of two pixels, so compensate
by shifting the output window by one column when hflip XOR vflip, and
widen the crop by two columns, within the pixel array, so that the
window still fits. Reprogram the window when either flip changes. The
binned readout is left unchanged.
With this change, raw 2592x1944 frames come out BGGR at all four
hflip/vflip settings on the Surface Pro 5 (IPU3); the same change in
the linux-surface tree gave BGGR at all four settings on a Surface
Pro 7 (IPU4P) and a Surface Pro 9 (IPU6). Without it, the order is only
right with both flips off or both on.
Fixes: 89aef879cb53 ("media: i2c: Add support for ov5693 sensor")
Link: https://github.com/linux-surface/kernel/pull/181
Tested-by: Chris Simpson <chris@nilk.net>
Tested-by: Tony Garcia <tony@loafecho.com>
Signed-off-by: Marco Ziliani <rylos78@gmail.com>
---
drivers/media/i2c/ov5693.c | 56 +++++++++++++++++++++++++++++++++++++++-------
1 file changed, 48 insertions(+), 8 deletions(-)
diff --git a/drivers/media/i2c/ov5693.c b/drivers/media/i2c/ov5693.c
index 3cab73b89..6e76e289f 100644
--- a/drivers/media/i2c/ov5693.c
+++ b/drivers/media/i2c/ov5693.c
@@ -367,6 +367,49 @@ static const u8 ov5693_test_pattern_bits[] = {
/* V4L2 Controls Functions */
+/*
+ * In the full (unbinned) readout each flip moves the Bayer phase by one
+ * column, so with exactly one of them enabled the CSI-2 receiver gets GBRG
+ * instead of the SBGGR10 the driver reports; with both enabled the two shifts
+ * cancel out. The crop start is only honoured in steps of two pixels, so
+ * compensate by shifting the output window by one column instead, and widen
+ * the crop so that the window still fits.
+ */
+static bool ov5693_bayer_shift(struct ov5693_device *ov5693)
+{
+ const struct ov5693_mode *mode = &ov5693->mode;
+ bool hflip, vflip;
+
+ if (mode->binning_x || mode->binning_y)
+ return false;
+
+ hflip = ov5693->ctrls.hflip && ov5693->ctrls.hflip->val;
+ vflip = ov5693->ctrls.vflip && ov5693->ctrls.vflip->val;
+
+ return hflip ^ vflip;
+}
+
+static int ov5693_window_x_configure(struct ov5693_device *ov5693)
+{
+ const struct ov5693_mode *mode = &ov5693->mode;
+ bool shift = ov5693_bayer_shift(ov5693);
+ u32 crop_end = mode->crop.left + mode->crop.width;
+ int ret = 0;
+
+ /* Widen the crop for the shifted window, within the pixel array */
+ if (shift)
+ crop_end = min(crop_end + 2,
+ max_t(u32, crop_end, OV5693_NATIVE_WIDTH - 1));
+
+ /* Offset X */
+ cci_write(ov5693->regmap, OV5693_OFFSET_START_X_REG, shift, &ret);
+
+ /* Crop End X */
+ cci_write(ov5693->regmap, OV5693_CROP_END_X_REG, crop_end, &ret);
+
+ return ret;
+}
+
static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
bool enable)
{
@@ -378,7 +421,7 @@ static int ov5693_flip_vert_configure(struct ov5693_device *ov5693,
if (ret)
return ret;
- return 0;
+ return ov5693_window_x_configure(ov5693);
}
static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
@@ -392,7 +435,7 @@ static int ov5693_flip_horz_configure(struct ov5693_device *ov5693,
if (ret)
return ret;
- return 0;
+ return ov5693_window_x_configure(ov5693);
}
static int ov5693_get_exposure(struct ov5693_device *ov5693, s32 *value)
@@ -570,16 +613,13 @@ static int ov5693_mode_configure(struct ov5693_device *ov5693)
cci_write(ov5693->regmap, OV5693_CROP_START_X_REG, mode->crop.left,
&ret);
- /* Offset X */
- cci_write(ov5693->regmap, OV5693_OFFSET_START_X_REG, 0, &ret);
-
/* Output Size X */
cci_write(ov5693->regmap, OV5693_OUTPUT_SIZE_X_REG, mode->format.width,
&ret);
- /* Crop End X */
- cci_write(ov5693->regmap, OV5693_CROP_END_X_REG,
- mode->crop.left + mode->crop.width, &ret);
+ /* Offset X and Crop End X follow the flips, see ov5693_bayer_shift() */
+ if (!ret)
+ ret = ov5693_window_x_configure(ov5693);
/* Horizontal Total Size */
cci_write(ov5693->regmap, OV5693_TIMING_HTS_REG, OV5693_FIXED_PPL,
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread* [PATCH v3 4/4] media: i2c: ov5693: Use autosuspend when stopping the stream
2026-11-27 23:11 [PATCH v3 0/4] media: i2c: ov5693: Fix mode changes, flips and autosuspend Marco Ziliani
` (2 preceding siblings ...)
2026-11-27 23:11 ` [PATCH v3 3/4] media: i2c: ov5693: Keep the Bayer order when flipping at full resolution Marco Ziliani
@ 2026-11-27 23:11 ` Marco Ziliani
3 siblings, 0 replies; 5+ messages in thread
From: Marco Ziliani @ 2026-11-27 23:11 UTC (permalink / raw)
To: Daniel Scally, Sakari Ailus, Mauro Carvalho Chehab
Cc: linux-media, Chris Simpson, Tony Garcia, Simon Kruse, D. Manresa,
linux-kernel, Marco Ziliani
The driver enables runtime PM autosuspend with a 1 second delay at
probe, but drops the usage count at stream stop with pm_runtime_put(),
which doesn't update the device's last busy time. The autosuspend
delay is then counted from the runtime resume at stream start rather
than from stream stop, so after any stream longer than the delay the
sensor is powered off right away.
Use pm_runtime_put_autosuspend(), which marks the device busy, so that
the delay counts from stream stop as intended, and a following stream
started within it doesn't power the sensor up from scratch again. Now
that format changes are written to a powered sensor, keeping it powered
between streams no longer leaves it in a stale mode.
On a Surface Pro 9 (IPU6) with autosuspend_delay_ms set to 3000, the
sensor suspended 1-2 ms after a 5 s stream stopped; with this change,
3.7 s after it, i.e. the delay plus the autosuspend timer slack. On a
Surface Pro 5 (IPU3) with the default 1 s delay, the sensor suspended
as soon as a 2 s stream stopped; with this change, 1.1 s after it.
Fixes: 89aef879cb53 ("media: i2c: Add support for ov5693 sensor")
Reported-by: D. Manresa <dmanresa@gmail.com>
Closes: https://github.com/linux-surface/kernel/pull/181#issuecomment-6436016335
Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com>
Tested-by: Chris Simpson <chris@nilk.net>
Tested-by: Tony Garcia <tony@loafecho.com>
Signed-off-by: Marco Ziliani <rylos78@gmail.com>
---
drivers/media/i2c/ov5693.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/media/i2c/ov5693.c b/drivers/media/i2c/ov5693.c
index 6e76e289f..5c64ae063 100644
--- a/drivers/media/i2c/ov5693.c
+++ b/drivers/media/i2c/ov5693.c
@@ -1098,7 +1098,7 @@ static int ov5693_s_stream(struct v4l2_subdev *sd, int enable)
goto err_power_down;
if (!enable)
- pm_runtime_put(ov5693->dev);
+ pm_runtime_put_autosuspend(ov5693->dev);
return 0;
err_power_down:
--
2.56.1
^ permalink raw reply [flat|nested] 5+ messages in thread