firmware-utils: bump to git HEAD
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0715-media-i2c-imx477-Add-support-for-adaptive-frame-cont.patch
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
5 control
6
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.
10
11 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
12 ---
13 drivers/media/i2c/imx477.c | 113 +++++++++++++++++++++++++++++--------
14 1 file changed, 91 insertions(+), 22 deletions(-)
15
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;
24
25 /* Current mode */
26 const struct imx477_mode *mode;
27 @@ -1278,6 +1280,72 @@ static int imx477_open(struct v4l2_subde
28 return 0;
29 }
30
31 +static int imx477_set_exposure(struct imx477 *imx477, unsigned int val)
32 +{
33 + int ret;
34 +
35 + ret = imx477_write_reg(imx477, IMX477_REG_EXPOSURE,
36 + IMX477_REG_VALUE_16BIT, val);
37 +
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;
41 +
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);
51 + }
52 +
53 + return ret;
54 +}
55 +
56 +static void imx477_adjust_exposure_range(struct imx477 *imx477,
57 + struct v4l2_ctrl *ctrl)
58 +{
59 + int exposure_max, exposure_def;
60 +
61 + if (ctrl->id == V4L2_CID_VBLANK || !ctrl->val) {
62 + /*
63 + * Either VBLANK has been changed or auto framerate
64 + * adjusting has been disabled. Honour the VBLANK limits
65 + * when setting exposure.
66 + */
67 + exposure_max = imx477->mode->height + imx477->vblank->val -
68 + IMX477_EXPOSURE_OFFSET;
69 +
70 + if (ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
71 + /*
72 + * Allow VBLANK adjustments since the driver is not
73 + * handling frame length control automatically.
74 + */
75 + __v4l2_ctrl_grab(imx477->vblank, false);
76 + }
77 + } else {
78 + /*
79 + * Auto framerate adjusting has been enabled. VBLANK
80 + * ctrl has been disabled and exposure can ramp up
81 + * to the maximum allowable value.
82 + */
83 + exposure_max = IMX477_EXPOSURE_MAX;
84 + /*
85 + * Do not allow VBLANK adjustments if the driver is
86 + * handling it frame length control automatically.
87 + */
88 + __v4l2_ctrl_grab(imx477->vblank, true);
89 + }
90 +
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,
94 + exposure_def);
95 +}
96 +
97 static int imx477_set_ctrl(struct v4l2_ctrl *ctrl)
98 {
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);
102 int ret = 0;
103
104 - if (ctrl->id == V4L2_CID_VBLANK) {
105 - int exposure_max, exposure_def;
106 -
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,
114 - exposure_def);
115 + if (ctrl->id == V4L2_CID_VBLANK ||
116 + ctrl->id == V4L2_CID_EXPOSURE_AUTO_PRIORITY) {
117 + /*
118 + * These controls may change the limits of usable exposure,
119 + * so check and adjust if necessary.
120 + */
121 + imx477_adjust_exposure_range(imx477, ctrl);
122 }
123
124 /*
125 @@ -1311,8 +1375,14 @@ static int imx477_set_ctrl(struct v4l2_c
126 IMX477_REG_VALUE_16BIT, ctrl->val);
127 break;
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);
132 + break;
133 + case V4L2_CID_EXPOSURE_AUTO_PRIORITY:
134 + /*
135 + * imx477_set_exposure() will recalculate the frame length
136 + * to adjust the framerate to match the exposure.
137 + */
138 + ret = imx477_set_exposure(imx477, imx477->exposure->val);
139 break;
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
143
144 static void imx477_set_framing_limits(struct imx477 *imx477)
145 {
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;
150
151 frm_length_min = imx477_get_frame_length(mode, &mode->timeperframe_min);
152 frm_length_default =
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);
157 +
158 + /* Setting this will adjust the exposure limits as well. */
159 __v4l2_ctrl_s_ctrl(imx477->vblank, frm_length_default - mode->height);
160
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,
167 - exposure_def);
168 /*
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);
174
175 + imx477->exposure_auto =
176 + v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
177 + V4L2_CID_EXPOSURE_AUTO_PRIORITY,
178 + 0, 1, 1, 0);
179 +
180 imx477->hflip = v4l2_ctrl_new_std(ctrl_hdlr, &imx477_ctrl_ops,
181 V4L2_CID_HFLIP, 0, 1, 1, 0);
182 if (imx477->hflip)