1 From 69f1022fc3c155f8cd5cf7dacaf283b1778835f3 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Fri, 8 May 2020 09:41:17 +0100
4 Subject: [PATCH] media: i2c: imx477: Add support for adaptive frame
7 Use V4L2_CID_EXPOSURE_AUTO_PRIORITY to control if the driver should
8 automatically adjust the sensor frame length based on exposure time,
9 allowing variable frame rates and longer exposures.
11 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
13 drivers/media/i2c/imx477.c | 113 +++++++++++++++++++++++++++++--------
14 1 file changed, 91 insertions(+), 22 deletions(-)
16 --- a/drivers/media/i2c/imx477.c
17 +++ b/drivers/media/i2c/imx477.c
18 @@ -1082,6 +1082,8 @@ struct imx477 {
19 struct v4l2_ctrl *hflip;
20 struct v4l2_ctrl *vblank;
21 struct v4l2_ctrl *hblank;
22 + /* This ctrl allows automatic variable framerate */
23 + struct v4l2_ctrl *exposure_auto;
26 const struct imx477_mode *mode;
27 @@ -1278,6 +1280,72 @@ static int imx477_open(struct v4l2_subde
31 +static int imx477_set_exposure(struct imx477 *imx477, unsigned int val)
35 + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
36 + IMX477_REG_VALUE_16BIT, val);
38 + /* Setup the frame length in the case of auto framerate mode. */
39 + if (imx477->exposure_auto->val) {
40 + unsigned int frame_length, frame_length_max, frame_length_min;
42 + frame_length_min = imx477->vblank->minimum +
43 + imx477->mode->height;
44 + frame_length_max = imx477->vblank->maximum +
45 + imx477->mode->height;
46 + frame_length = max(frame_length_min,
47 + val + IMX477_EXPOSURE_OFFSET);
48 + frame_length = min(frame_length_max, frame_length);
49 + ret += imx477_write_reg(imx477, IMX477_REG_FRAME_LENGTH,
50 + IMX477_REG_VALUE_16BIT, frame_length);
56 +static void imx477_adjust_exposure_range(struct imx477 *imx477,
57 + struct v4l2_ctrl *ctrl)
59 + int exposure_max, exposure_def;
61 + if (ctrl->id == V4L2_CID_VBLANK || !ctrl->val) {
63 + * Either VBLANK has been changed or auto framerate
64 + * adjusting has been disabled. Honour the VBLANK limits
65 + * when setting exposure.
67 + exposure_max = imx477->mode->height + imx477->vblank->val -
68 + IMX477_EXPOSURE_OFFSET;
70 + if (ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
72 + * Allow VBLANK adjustments since the driver is not
73 + * handling frame length control automatically.
75 + __v4l2_ctrl_grab(imx477->vblank, false);
79 + * Auto framerate adjusting has been enabled. VBLANK
80 + * ctrl has been disabled and exposure can ramp up
81 + * to the maximum allowable value.
83 + exposure_max = IMX477_EXPOSURE_MAX;
85 + * Do not allow VBLANK adjustments if the driver is
86 + * handling it frame length control automatically.
88 + __v4l2_ctrl_grab(imx477->vblank, true);
91 + exposure_def = min(exposure_max, imx477->exposure->val);
92 + __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
93 + exposure_max, imx477->exposure->step,
97 static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
99 struct imx477 *imx477 =
100 @@ -1285,17 +1353,13 @@ static int imx477_set_ctrl(struct v4l2_c
101 struct i2c_client *client = v4l2_get_subdevdata(&imx477->sd);
104 - if (ctrl->id == V4L2_CID_VBLANK) {
105 - int exposure_max, exposure_def;
107 - /* Update max exposure while meeting expected vblanking */
108 - exposure_max = imx477->mode->height + ctrl->val -
109 - IMX477_EXPOSURE_OFFSET;
110 - exposure_def = min(exposure_max, imx477->exposure->val);
111 - __v4l2_ctrl_modify_range(imx477->exposure,
112 - imx477->exposure->minimum,
113 - exposure_max, imx477->exposure->step,
115 + if (ctrl->id == V4L2_CID_VBLANK ||
116 + ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
118 + * These controls may change the limits of usable exposure,
119 + * so check and adjust if necessary.
121 + imx477_adjust_exposure_range(imx477, ctrl);
125 @@ -1311,8 +1375,14 @@ static int imx477_set_ctrl(struct v4l2_c
126 IMX477_REG_VALUE_16BIT, ctrl->val);
128 case V4L2_CID_EXPOSURE:
129 - ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
130 - IMX477_REG_VALUE_16BIT, ctrl->val);
131 + ret = imx477_set_exposure(imx477, ctrl->val);
133 + case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
135 + * imx477_set_exposure() will recalculate the frame length
136 + * to adjust the framerate to match the exposure.
138 + ret = imx477_set_exposure(imx477, imx477->exposure->val);
140 case V4L2_CID_DIGITAL_GAIN:
141 ret = imx477_write_reg(imx477, IMX477_REG_DIGITAL_GAIN,
142 @@ -1510,9 +1580,8 @@ unsigned int imx477_get_frame_length(con
144 static void imx477_set_framing_limits(struct imx477 *imx477)
146 + unsigned int frm_length_min, frm_length_default, hblank;
147 const struct imx477_mode *mode = imx477->mode;
148 - unsigned int frm_length_min, frm_length_default;
149 - unsigned int exposure_max, exposure_def, hblank;
151 frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
153 @@ -1522,15 +1591,10 @@ static void imx477_set_framing_limits(st
154 __v4l2_ctrl_modify_range(imx477->vblank, frm_length_min - mode->height,
155 IMX477_FRAME_LENGTH_MAX - mode->height,
156 1, frm_length_default - mode->height);
158 + /* Setting this will adjust the exposure limits as well. */
159 __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
161 - /* Update max exposure while meeting expected vblanking */
162 - exposure_max = IMX477_FRAME_LENGTH_MAX - IMX477_EXPOSURE_OFFSET;
163 - exposure_def = frm_length_default - mode->height -
164 - IMX477_EXPOSURE_OFFSET;
165 - __v4l2_ctrl_modify_range(imx477->exposure, imx477->exposure->minimum,
166 - exposure_max, imx477->exposure->step,
169 * Currently PPL is fixed to the mode specified value, so hblank
170 * depends on mode->width only, and is not changeable in any
171 @@ -1939,6 +2003,11 @@ static int imx477_init_controls(struct i
172 IMX477_DGTL_GAIN_MIN, IMX477_DGTL_GAIN_MAX,
173 IMX477_DGTL_GAIN_STEP, IMX477_DGTL_GAIN_DEFAULT);
175 + imx477->exposure_auto =
176 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
177 + V4L2_CID_EXPOSURE_AUTO_PRIORITY,
180 imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
181 V4L2_CID_HFLIP, 0, 1, 1, 0);