As the granularity of the hardware supported values is lower than the
control value, implement a try_ctrl() function for
V4L2_CID_FLASH_DURATION. This function calculates the nearest possible
µs strobe duration for the given value and returns it back to the
caller.
Signed-off-by: Richard Leitner <richard.leitner@linux.dev>
---
drivers/media/i2c/ov9282.c | 56 ++++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 54 insertions(+), 2 deletions(-)
diff --git a/drivers/media/i2c/ov9282.c b/drivers/media/i2c/ov9282.c
index 09d522d5977ec6fb82028ddb6015f05c9328191d..f75882dcb73bea0e00e2cb37ffc19ec3c3a8b126 100644
--- a/drivers/media/i2c/ov9282.c
+++ b/drivers/media/i2c/ov9282.c
@@ -128,6 +128,8 @@
#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 */
@@ -691,7 +693,7 @@ static int ov9282_set_ctrl_flash_led_mode(struct ov9282 *ov9282, int mode)
current_val);
}
-static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
+static u32 ov9282_us_to_flash_duration(struct ov9282 *ov9282, u32 value)
{
/*
* Calculate "strobe_frame_span" increments from a given value (µs).
@@ -702,7 +704,31 @@ static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
* The formula below is interpolated from different modes/framerates
* and should work quite well for most settings.
*/
- u32 val = value * 192 / (ov9282->cur_mode->width + ov9282->hblank_ctrl->val);
+ 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)
+{
+ /*
+ * As the calculation in ov9282_us_to_flash_duration uses an integer
+ * divison calculate in ns here to get more precision. Then check if
+ * we need to compensate that divison by incrementing the µs result.
+ */
+ u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
+ u64 ns = value * 1000 * frame_width / OV9282_STROBE_SPAN_FACTOR;
+ u32 us = ns / 1000;
+ u32 remainder = ns % 1000;
+
+ if (remainder > 0)
+ us++;
+ return us;
+}
+
+static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
+{
+ u32 val = ov9282_us_to_flash_duration(ov9282, value);
ov9282_write_reg(ov9282, OV9282_REG_FLASH_DURATION, 1, (val >> 24) & 0xff);
ov9282_write_reg(ov9282, OV9282_REG_FLASH_DURATION + 1, 1, (val >> 16) & 0xff);
@@ -792,9 +818,35 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
return ret;
}
+static int ov9282_try_ctrl(struct v4l2_ctrl *ctrl)
+{
+ struct ov9282 *ov9282 =
+ container_of(ctrl->handler, struct ov9282, ctrl_handler);
+
+ if (ctrl->id == V4L2_CID_FLASH_DURATION) {
+ u32 fd = ov9282_us_to_flash_duration(ov9282, ctrl->val);
+ u32 us = ctrl->val;
+ u32 us0 = ov9282_flash_duration_to_us(ov9282, fd);
+ u32 us1 = ov9282_flash_duration_to_us(ov9282, (fd + 1));
+
+ if ((us1 - us) < (us - us0))
+ ctrl->val = us1;
+ else
+ ctrl->val = us0;
+
+ if (us != ctrl->val) {
+ dev_dbg(ov9282->dev, "using next valid strobe_duration %u instead of %u\n",
+ ctrl->val, us);
+ }
+ }
+
+ return 0;
+}
+
/* V4l2 subdevice control ops*/
static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
.s_ctrl = ov9282_set_ctrl,
+ .try_ctrl = ov9282_try_ctrl,
};
/**
--
2.47.2
Hi Richard,
Thanks for the update.
On Wed, May 07, 2025 at 09:51:39AM +0200, Richard Leitner wrote:
> As the granularity of the hardware supported values is lower than the
> control value, implement a try_ctrl() function for
> V4L2_CID_FLASH_DURATION. This function calculates the nearest possible
> µs strobe duration for the given value and returns it back to the
> caller.
>
> Signed-off-by: Richard Leitner <richard.leitner@linux.dev>
> ---
> drivers/media/i2c/ov9282.c | 56 ++++++++++++++++++++++++++++++++++++++++++++--
> 1 file changed, 54 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/i2c/ov9282.c b/drivers/media/i2c/ov9282.c
> index 09d522d5977ec6fb82028ddb6015f05c9328191d..f75882dcb73bea0e00e2cb37ffc19ec3c3a8b126 100644
> --- a/drivers/media/i2c/ov9282.c
> +++ b/drivers/media/i2c/ov9282.c
> @@ -128,6 +128,8 @@
> #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 */
> @@ -691,7 +693,7 @@ static int ov9282_set_ctrl_flash_led_mode(struct ov9282 *ov9282, int mode)
> current_val);
> }
>
> -static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
> +static u32 ov9282_us_to_flash_duration(struct ov9282 *ov9282, u32 value)
> {
> /*
> * Calculate "strobe_frame_span" increments from a given value (µs).
> @@ -702,7 +704,31 @@ static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
> * The formula below is interpolated from different modes/framerates
> * and should work quite well for most settings.
> */
> - u32 val = value * 192 / (ov9282->cur_mode->width + ov9282->hblank_ctrl->val);
> + 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)
> +{
> + /*
> + * As the calculation in ov9282_us_to_flash_duration uses an integer
> + * divison calculate in ns here to get more precision. Then check if
> + * we need to compensate that divison by incrementing the µs result.
> + */
> + u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
> + u64 ns = value * 1000 * frame_width / OV9282_STROBE_SPAN_FACTOR;
> + u32 us = ns / 1000;
> + u32 remainder = ns % 1000;
> +
> + if (remainder > 0)
> + us++;
It looks like you're trying to round up here. Wouldn't
return DIV_ROUND_UP(ns, 1000);
do the same?
> + return us;
> +}
> +
> +static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
> +{
> + u32 val = ov9282_us_to_flash_duration(ov9282, value);
>
> ov9282_write_reg(ov9282, OV9282_REG_FLASH_DURATION, 1, (val >> 24) & 0xff);
> ov9282_write_reg(ov9282, OV9282_REG_FLASH_DURATION + 1, 1, (val >> 16) & 0xff);
> @@ -792,9 +818,35 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
> return ret;
> }
>
> +static int ov9282_try_ctrl(struct v4l2_ctrl *ctrl)
> +{
> + struct ov9282 *ov9282 =
> + container_of(ctrl->handler, struct ov9282, ctrl_handler);
> +
> + if (ctrl->id == V4L2_CID_FLASH_DURATION) {
> + u32 fd = ov9282_us_to_flash_duration(ov9282, ctrl->val);
> + u32 us = ctrl->val;
> + u32 us0 = ov9282_flash_duration_to_us(ov9282, fd);
> + u32 us1 = ov9282_flash_duration_to_us(ov9282, (fd + 1));
> +
> + if ((us1 - us) < (us - us0))
Is this missing abs?
> + ctrl->val = us1;
> + else
> + ctrl->val = us0;
> +
> + if (us != ctrl->val) {
> + dev_dbg(ov9282->dev, "using next valid strobe_duration %u instead of %u\n",
> + ctrl->val, us);
> + }
> + }
> +
> + return 0;
> +}
> +
> /* V4l2 subdevice control ops*/
> static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
> .s_ctrl = ov9282_set_ctrl,
> + .try_ctrl = ov9282_try_ctrl,
> };
>
> /**
>
--
Regards,
Sakari Ailus
On Mon, May 26, 2025 at 08:22:15PM +0000, Sakari Ailus wrote:
> Hi Richard,
>
> Thanks for the update.
Thank you for the review!
>
> On Wed, May 07, 2025 at 09:51:39AM +0200, Richard Leitner wrote:
> > As the granularity of the hardware supported values is lower than the
> > control value, implement a try_ctrl() function for
> > V4L2_CID_FLASH_DURATION. This function calculates the nearest possible
> > µs strobe duration for the given value and returns it back to the
> > caller.
> >
> > Signed-off-by: Richard Leitner <richard.leitner@linux.dev>
> > ---
> > drivers/media/i2c/ov9282.c | 56 ++++++++++++++++++++++++++++++++++++++++++++--
> > 1 file changed, 54 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/media/i2c/ov9282.c b/drivers/media/i2c/ov9282.c
> > index 09d522d5977ec6fb82028ddb6015f05c9328191d..f75882dcb73bea0e00e2cb37ffc19ec3c3a8b126 100644
> > --- a/drivers/media/i2c/ov9282.c
> > +++ b/drivers/media/i2c/ov9282.c
> > @@ -128,6 +128,8 @@
> > #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 */
> > @@ -691,7 +693,7 @@ static int ov9282_set_ctrl_flash_led_mode(struct ov9282 *ov9282, int mode)
> > current_val);
> > }
> >
> > -static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
> > +static u32 ov9282_us_to_flash_duration(struct ov9282 *ov9282, u32 value)
> > {
> > /*
> > * Calculate "strobe_frame_span" increments from a given value (µs).
> > @@ -702,7 +704,31 @@ static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
> > * The formula below is interpolated from different modes/framerates
> > * and should work quite well for most settings.
> > */
> > - u32 val = value * 192 / (ov9282->cur_mode->width + ov9282->hblank_ctrl->val);
> > + 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)
> > +{
> > + /*
> > + * As the calculation in ov9282_us_to_flash_duration uses an integer
> > + * divison calculate in ns here to get more precision. Then check if
> > + * we need to compensate that divison by incrementing the µs result.
> > + */
> > + u32 frame_width = ov9282->cur_mode->width + ov9282->hblank_ctrl->val;
> > + u64 ns = value * 1000 * frame_width / OV9282_STROBE_SPAN_FACTOR;
> > + u32 us = ns / 1000;
> > + u32 remainder = ns % 1000;
> > +
> > + if (remainder > 0)
> > + us++;
>
> It looks like you're trying to round up here. Wouldn't
>
> return DIV_ROUND_UP(ns, 1000);
>
> do the same?
Yes, thanks for the suggestion. I wasn't thinking of DIV_ROUND_UP here,
but this makes the code way more readable :-)
Will adapt this in v5.
>
> > + return us;
> > +}
> > +
> > +static int ov9282_set_ctrl_flash_duration(struct ov9282 *ov9282, int value)
> > +{
> > + u32 val = ov9282_us_to_flash_duration(ov9282, value);
> >
> > ov9282_write_reg(ov9282, OV9282_REG_FLASH_DURATION, 1, (val >> 24) & 0xff);
> > ov9282_write_reg(ov9282, OV9282_REG_FLASH_DURATION + 1, 1, (val >> 16) & 0xff);
> > @@ -792,9 +818,35 @@ static int ov9282_set_ctrl(struct v4l2_ctrl *ctrl)
> > return ret;
> > }
> >
> > +static int ov9282_try_ctrl(struct v4l2_ctrl *ctrl)
> > +{
> > + struct ov9282 *ov9282 =
> > + container_of(ctrl->handler, struct ov9282, ctrl_handler);
> > +
> > + if (ctrl->id == V4L2_CID_FLASH_DURATION) {
> > + u32 fd = ov9282_us_to_flash_duration(ov9282, ctrl->val);
> > + u32 us = ctrl->val;
> > + u32 us0 = ov9282_flash_duration_to_us(ov9282, fd);
> > + u32 us1 = ov9282_flash_duration_to_us(ov9282, (fd + 1));
> > +
> > + if ((us1 - us) < (us - us0))
>
> Is this missing abs?
IMHO this abs() isn't required here, as the (fd + 1) value should be
always higher than the fd one. But nonetheless it doesn't hurt, so I'll
add it in v5. Thanks!
>
> > + ctrl->val = us1;
> > + else
> > + ctrl->val = us0;
> > +
> > + if (us != ctrl->val) {
> > + dev_dbg(ov9282->dev, "using next valid strobe_duration %u instead of %u\n",
> > + ctrl->val, us);
> > + }
> > + }
> > +
> > + return 0;
> > +}
> > +
> > /* V4l2 subdevice control ops*/
> > static const struct v4l2_ctrl_ops ov9282_ctrl_ops = {
> > .s_ctrl = ov9282_set_ctrl,
> > + .try_ctrl = ov9282_try_ctrl,
> > };
> >
> > /**
> >
>
> --
> Regards,
>
> Sakari Ailus
thanks & regards;rl
© 2016 - 2025 Red Hat, Inc.