bcm27xx: update to latest patches from RPi foundation
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0688-media-i2c-ov5647-Add-support-for-V4L2_CID_VBLANK.patch
1 From fbb943e35b519549eac8ee17bf20d651388a27dd 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
5
6 Adds vblank control to allow for frame rate control.
7
8 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
9 ---
10 drivers/media/i2c/ov5647.c | 65 ++++++++++++++++++++++++++++++++------
11 1 file changed, 55 insertions(+), 10 deletions(-)
12
13 --- a/drivers/media/i2c/ov5647.c
14 +++ b/drivers/media/i2c/ov5647.c
15 @@ -61,6 +61,8 @@
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
24 @@ -79,6 +81,9 @@
25 #define OV5647_PIXEL_ARRAY_WIDTH 2592U
26 #define OV5647_PIXEL_ARRAY_HEIGHT 1944U
27
28 +#define OV5647_VBLANK_MIN 4
29 +#define OV5647_VTS_MAX 32767
30 +
31 struct regval_list {
32 u16 addr;
33 u8 data;
34 @@ -92,6 +97,8 @@ struct ov5647_mode {
35 u64 pixel_rate;
36 /* HTS as defined in the register set (0x380C/0x380D) */
37 int hts;
38 + /* Default VTS value for this mode */
39 + int vts_def;
40
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;
48 bool write_mode_regs;
49 };
50
51 @@ -161,8 +169,6 @@ static struct regval_list ov5647_640x480
52 {0x3b07, 0x0c},
53 {0x380c, 0x07},
54 {0x380d, 0x68},
55 - {0x380e, 0x03},
56 - {0x380f, 0xd8},
57 {0x3814, 0x31},
58 {0x3815, 0x31},
59 {0x3708, 0x64},
60 @@ -251,8 +257,6 @@ static struct regval_list ov5647_2592x19
61 {0x3b07, 0x0c},
62 {0x380c, 0x0b},
63 {0x380d, 0x1c},
64 - {0x380e, 0x07},
65 - {0x380f, 0xb0},
66 {0x3814, 0x11},
67 {0x3815, 0x11},
68 {0x3708, 0x64},
69 @@ -342,8 +346,6 @@ static struct regval_list ov5647_1080p30
70 {0x3b07, 0x0c},
71 {0x380c, 0x09},
72 {0x380d, 0x70},
73 - {0x380e, 0x04},
74 - {0x380f, 0x50},
75 {0x3814, 0x11},
76 {0x3815, 0x11},
77 {0x3708, 0x64},
78 @@ -485,8 +487,6 @@ static struct regval_list ov5647_2x2binn
79 {0x3503, 0x03},
80 {0x3820, 0x41},
81 {0x3821, 0x07},
82 - {0x380E, 0x05},
83 - {0x380F, 0x9B},
84 {0x350A, 0x00},
85 {0x350B, 0x10},
86 {0x3500, 0x00},
87 @@ -520,8 +520,6 @@ static struct regval_list ov5647_640x480
88 {0x3b07, 0x0c},
89 {0x380c, 0x07},
90 {0x380d, 0x3c},
91 - {0x380e, 0x01},
92 - {0x380f, 0xf8},
93 {0x3814, 0x35},
94 {0x3815, 0x35},
95 {0x3708, 0x64},
96 @@ -609,6 +607,7 @@ static struct ov5647_mode supported_mode
97 },
98 .pixel_rate = 77291670,
99 .hts = 1896,
100 + .vts_def = 0x3d8,
101 ov5647_640x480_8bit,
102 ARRAY_SIZE(ov5647_640x480_8bit)
103 },
104 @@ -634,6 +633,7 @@ static struct ov5647_mode supported_mode
105 },
106 .pixel_rate = 87500000,
107 .hts = 2844,
108 + .vts_def = 0x7b0,
109 ov5647_2592x1944_10bit,
110 ARRAY_SIZE(ov5647_2592x1944_10bit)
111 },
112 @@ -657,6 +657,7 @@ static struct ov5647_mode supported_mode
113 },
114 .pixel_rate = 81666700,
115 .hts = 2416,
116 + .vts_def = 0x450,
117 ov5647_1080p30_10bit,
118 ARRAY_SIZE(ov5647_1080p30_10bit)
119 },
120 @@ -679,6 +680,7 @@ static struct ov5647_mode supported_mode
121 },
122 .pixel_rate = 81666700,
123 .hts = 1896,
124 + .vts_def = 0x59b,
125 ov5647_2x2binned_10bit,
126 ARRAY_SIZE(ov5647_2x2binned_10bit)
127 },
128 @@ -702,6 +704,7 @@ static struct ov5647_mode supported_mode
129 },
130 .pixel_rate = 55000000,
131 .hts = 1852,
132 + .vts_def = 0x1f8,
133 ov5647_640x480_10bit,
134 ARRAY_SIZE(ov5647_640x480_10bit)
135 },
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])
139
140 +static int ov5647_write16(struct v4l2_subdev *sd, u16 reg, u16 val)
141 +{
142 + int ret;
143 + unsigned char data[4] = { reg >> 8, reg & 0xff, val >> 8, val & 0xff};
144 + struct i2c_client *client = v4l2_get_subdevdata(sd);
145 +
146 + ret = i2c_master_send(client, data, 4);
147 + /*
148 + * Writing the wrong number of bytes also needs to be flagged as an
149 + * error. Success needs to produce a 0 return code.
150 + */
151 + if (ret == 4) {
152 + ret = 0;
153 + } else {
154 + dev_dbg(&client->dev, "%s: i2c write error, reg: %x\n",
155 + __func__, reg);
156 + if (ret >= 0)
157 + ret = -EINVAL;
158 + }
159 +
160 + return ret;
161 +}
162 +
163 static int ov5647_write(struct v4l2_subdev *sd, u16 reg, u8 val)
164 {
165 int ret;
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,
169 hblank);
170 +
171 + __v4l2_ctrl_modify_range(state->vblank,
172 + OV5647_VBLANK_MIN,
173 + OV5647_VTS_MAX - mode->format.height,
174 + 1,
175 + mode->vts_def - mode->format.height);
176 + __v4l2_ctrl_s_ctrl(state->vblank,
177 + mode->vts_def - mode->format.height);
178 }
179
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. */
184 break;
185 + case V4L2_CID_VBLANK:
186 + ret = ov5647_write16(sd, OV5647_REG_VTS_HI,
187 + state->mode->format.height + ctrl->val);
188 + break;
189 default:
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
193 hblank);
194 sensor->hblank->flags |= V4L2_CTRL_FLAG_READ_ONLY;
195
196 + sensor->vblank = v4l2_ctrl_new_std(&sensor->ctrls, &ov5647_ctrl_ops,
197 + V4L2_CID_VBLANK, OV5647_VBLANK_MIN,
198 + OV5647_VTS_MAX -
199 + sensor->mode->format.height, 1,
200 + sensor->mode->vts_def -
201 + sensor->mode->format.height);
202 +
203 if (sensor->ctrls.error) {
204 ret = sensor->ctrls.error;
205 dev_err(&client->dev, "%s control init failed (%d)\n",