From 4e8d73ce89c6dd6fdcb8dd7df8310762707c5b1a Mon Sep 17 00:00:00 2001 From: Dave Stevenson Date: Wed, 1 Jan 2025 14:18:25 +0000 Subject: [PATCH] media: i2c: imx415: Add read/write control of VBLANK This also requires that the ranges for the exposure control are updated. Signed-off-by: Dave Stevenson --- drivers/media/i2c/imx415.c | 52 +++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 18 deletions(-) --- a/drivers/media/i2c/imx415.c +++ b/drivers/media/i2c/imx415.c @@ -25,6 +25,7 @@ #define IMX415_PIXEL_ARRAY_WIDTH 3864 #define IMX415_PIXEL_ARRAY_HEIGHT 2192 #define IMX415_PIXEL_ARRAY_VBLANK 58 +#define IMX415_EXPOSURE_OFFSET 8 #define IMX415_NUM_CLK_PARAM_REGS 11 @@ -56,6 +57,7 @@ #define IMX415_OUTSEL IMX415_REG_8BIT(0x30C0) #define IMX415_DRV IMX415_REG_8BIT(0x30C1) #define IMX415_VMAX IMX415_REG_24BIT(0x3024) +#define IMX415_VMAX_MAX 0xfffff #define IMX415_HMAX IMX415_REG_16BIT(0x3028) #define IMX415_SHR0 IMX415_REG_24BIT(0x3050) #define IMX415_GAIN_PCG_0 IMX415_REG_16BIT(0x3090) @@ -457,7 +459,6 @@ static const struct imx415_clk_params im /* all-pixel 2-lane 720 Mbps 15.74 Hz mode */ static const struct imx415_reg imx415_mode_2_720[] = { - { IMX415_VMAX, 0x08CA }, { IMX415_HMAX, 0x07F0 }, { IMX415_LANEMODE, IMX415_LANEMODE_2 }, { IMX415_TCLKPOST, 0x006F }, @@ -473,7 +474,6 @@ static const struct imx415_reg imx415_mo /* all-pixel 2-lane 1440 Mbps 30.01 Hz mode */ static const struct imx415_reg imx415_mode_2_1440[] = { - { IMX415_VMAX, 0x08CA }, { IMX415_HMAX, 0x042A }, { IMX415_LANEMODE, IMX415_LANEMODE_2 }, { IMX415_TCLKPOST, 0x009F }, @@ -489,7 +489,6 @@ static const struct imx415_reg imx415_mo /* all-pixel 4-lane 891 Mbps 30 Hz mode */ static const struct imx415_reg imx415_mode_4_891[] = { - { IMX415_VMAX, 0x08CA }, { IMX415_HMAX, 0x044C }, { IMX415_LANEMODE, IMX415_LANEMODE_4 }, { IMX415_TCLKPOST, 0x007F }, @@ -617,6 +616,7 @@ struct imx415 { struct v4l2_ctrl *vblank; struct v4l2_ctrl *hflip; struct v4l2_ctrl *vflip; + struct v4l2_ctrl *exposure; unsigned int cur_mode; unsigned int num_data_lanes; @@ -795,16 +795,37 @@ static int imx415_s_ctrl(struct v4l2_ctr ctrls); const struct v4l2_mbus_framefmt *format; struct v4l2_subdev_state *state; + u32 exposure_max; unsigned int vmax; unsigned int flip; - - if (!sensor->streaming) - return 0; + int ret; state = v4l2_subdev_get_locked_active_state(&sensor->subdev); format = v4l2_subdev_get_pad_format(&sensor->subdev, state, 0); + if (ctrl->id == V4L2_CID_VBLANK) { + exposure_max = format->height + ctrl->val - + IMX415_EXPOSURE_OFFSET; + __v4l2_ctrl_modify_range(sensor->exposure, + sensor->exposure->minimum, + exposure_max, sensor->exposure->step, + sensor->exposure->default_value); + } + + if (!sensor->streaming) + return 0; + switch (ctrl->id) { + case V4L2_CID_VBLANK: + ret = imx415_write(sensor, IMX415_VMAX, + format->height + ctrl->val); + if (ret) + return ret; + /* + * Deliberately fall through as exposure is set based on VMAX + * which has just changed. + */ + fallthrough; case V4L2_CID_EXPOSURE: /* clamp the exposure value to VMAX. */ vmax = format->height + sensor->vblank->cur.val; @@ -840,7 +861,8 @@ static int imx415_ctrls_init(struct imx4 u64 pixel_rate = supported_modes[sensor->cur_mode].pixel_rate; u64 lane_rate = supported_modes[sensor->cur_mode].lane_rate; u32 exposure_max = IMX415_PIXEL_ARRAY_HEIGHT + - IMX415_PIXEL_ARRAY_VBLANK - 8; + IMX415_PIXEL_ARRAY_VBLANK - + IMX415_EXPOSURE_OFFSET; u32 hblank; unsigned int i; int ret; @@ -869,8 +891,9 @@ static int imx415_ctrls_init(struct imx4 if (ctrl) ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; - v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, V4L2_CID_EXPOSURE, - 4, exposure_max, 1, exposure_max); + sensor->exposure = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, + V4L2_CID_EXPOSURE, 4, + exposure_max, 1, exposure_max); v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, V4L2_CID_ANALOGUE_GAIN, IMX415_AGAIN_MIN, @@ -887,16 +910,9 @@ static int imx415_ctrls_init(struct imx4 sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &imx415_ctrl_ops, V4L2_CID_VBLANK, IMX415_PIXEL_ARRAY_VBLANK, - IMX415_PIXEL_ARRAY_VBLANK, 1, - IMX415_PIXEL_ARRAY_VBLANK); - if (sensor->vblank) - sensor->vblank->flags |= V4L2_CTRL_FLAG_READ_ONLY; - - /* - * The pixel rate used here is a virtual value and can be used for - * calculating the frame rate together with hblank. It may not - * necessarily be the physically correct pixel clock. - */ + IMX415_VMAX_MAX - IMX415_PIXEL_ARRAY_HEIGHT, + 1, IMX415_PIXEL_ARRAY_VBLANK); + v4l2_ctrl_new_std(&sensor->ctrls, NULL, V4L2_CID_PIXEL_RATE, pixel_rate, pixel_rate, 1, pixel_rate);