[PATCH v3 2/4] media: i2c: ov5693: Write format and crop changes to a powered sensor

From: Marco Ziliani

Date: Fri Nov 27 2026 - 18:11:58 EST


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@xxxxxxxxx>
---
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