1 From 95a10a4709eea202f3ce833fdeb9b30c17d80148 Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Wed, 29 Apr 2020 21:39:58 +0100
4 Subject: [PATCH] media: i2c: ov5647: Add support for V4L2_CID_VBLANK
6 Adds vblank control to allow for frame rate control.
8 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
10 drivers/media/i2c/ov5647.c | 65 ++++++++++++++++++++++++++++++++------
11 1 file changed, 55 insertions(+), 10 deletions(-)
13 --- a/drivers/media/i2c/ov5647.c
14 +++ b/drivers/media/i2c/ov5647.c
16 #define OV5647_REG_AEC_AGC 0x3503
17 #define OV5647_REG_GAIN_HI 0x350A
18 #define OV5647_REG_GAIN_LO 0x350B
19 +#define OV5647_REG_VTS_HI 0x380e
20 +#define OV5647_REG_VTS_LO 0x380f
21 #define OV5647_REG_FRAME_OFF_NUMBER 0x4202
22 #define OV5647_REG_MIPI_CTRL00 0x4800
23 #define OV5647_REG_MIPI_CTRL14 0x4814
25 #define OV5647_PIXEL_ARRAY_WIDTH 2592U
26 #define OV5647_PIXEL_ARRAY_HEIGHT 1944U
28 +#define OV5647_VBLANK_MIN 4
29 +#define OV5647_VTS_MAX 32767
34 @@ -92,6 +97,8 @@ struct ov5647_mode {
36 /* HTS as defined in the register set (0x380C/0x380D) */
38 + /* Default VTS value for this mode */
41 struct regval_list *reg_list;
42 unsigned int num_regs;
43 @@ -109,6 +116,7 @@ struct ov5647 {
44 struct v4l2_ctrl_handler ctrls;
45 struct v4l2_ctrl *pixel_rate;
46 struct v4l2_ctrl *hblank;
47 + struct v4l2_ctrl *vblank;
51 @@ -161,8 +169,6 @@ static struct regval_list ov5647_640x480
60 @@ -251,8 +257,6 @@ static struct regval_list ov5647_2592x19
69 @@ -342,8 +346,6 @@ static struct regval_list ov5647_1080p30
78 @@ -485,8 +487,6 @@ static struct regval_list ov5647_2x2binn
87 @@ -520,8 +520,6 @@ static struct regval_list ov5647_640x480
96 @@ -609,6 +607,7 @@ static struct ov5647_mode supported_mode
98 .pixel_rate = 77291670,
102 ARRAY_SIZE(ov5647_640x480_8bit)
104 @@ -634,6 +633,7 @@ static struct ov5647_mode supported_mode
106 .pixel_rate = 87500000,
109 ov5647_2592x1944_10bit,
110 ARRAY_SIZE(ov5647_2592x1944_10bit)
112 @@ -657,6 +657,7 @@ static struct ov5647_mode supported_mode
114 .pixel_rate = 81666700,
117 ov5647_1080p30_10bit,
118 ARRAY_SIZE(ov5647_1080p30_10bit)
120 @@ -679,6 +680,7 @@ static struct ov5647_mode supported_mode
122 .pixel_rate = 81666700,
125 ov5647_2x2binned_10bit,
126 ARRAY_SIZE(ov5647_2x2binned_10bit)
128 @@ -702,6 +704,7 @@ static struct ov5647_mode supported_mode
130 .pixel_rate = 55000000,
133 ov5647_640x480_10bit,
134 ARRAY_SIZE(ov5647_640x480_10bit)
136 @@ -710,6 +713,29 @@ static struct ov5647_mode supported_mode
137 /* Use 2x2 binned 10-bit mode as default. */
138 #define OV5647_DEFAULT_MODE (&supported_modes_10bit[2])
140 +static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
143 + unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
144 + struct i2c_client *client = v4l2_get_subdevdata(sd);
146 + ret = i2c_master_send(client, data, 4);
148 + * Writing the wrong number of bytes also needs to be flagged as an
149 + * error. Success needs to produce a 0 return code.
154 + dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
163 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
166 @@ -1189,6 +1215,14 @@ static int ov5647_set_fmt(struct v4l2_su
167 hblank = mode->hts - mode->format.width;
168 __v4l2_ctrl_modify_range(state->hblank, hblank, hblank, 1,
171 + __v4l2_ctrl_modify_range(state->vblank,
173 + OV5647_VTS_MAX - mode->format.height,
175 + mode->vts_def - mode->format.height);
176 + __v4l2_ctrl_s_ctrl(state->vblank,
177 + mode->vts_def - mode->format.height);
180 mutex_unlock(&state->lock);
181 @@ -1411,6 +1445,10 @@ static int ov5647_s_ctrl(struct v4l2_ctr
182 case V4L2_CID_HBLANK:
183 /* Read-only, but we adjust it based on mode. */
185 + case V4L2_CID_VBLANK:
186 + ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
187 + state->mode->format.height + ctrl->val);
190 dev_info(&client->dev,
191 "ctrl(id:0x%x,val:0x%x) is not handled\n",
192 @@ -1519,6 +1557,13 @@ static int ov5647_probe(struct i2c_clien
194 sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
196 + sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
197 + V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
199 + sensor->mode->format.height, 1,
200 + sensor->mode->vts_def -
201 + sensor->mode->format.height);
203 if (sensor->ctrls.error) {
204 ret = sensor->ctrls.error;
205 dev_err(&client->dev, "%s control init failed (%d)\n",