1 /*
2  * Copyright (c) 2021 Antmicro <www.antmicro.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define DT_DRV_COMPAT ovti_ov2640
8 #include <zephyr.h>
9 #include <device.h>
10 
11 #include <drivers/video.h>
12 #include <drivers/i2c.h>
13 #include <drivers/gpio.h>
14 
15 #define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
16 #include <logging/log.h>
17 LOG_MODULE_REGISTER(ov2640);
18 
19 /* DSP register bank FF=0x00*/
20 #define QS                  0x44
21 #define HSIZE               0x51
22 #define VSIZE               0x52
23 #define XOFFL               0x53
24 #define YOFFL               0x54
25 #define VHYX                0x55
26 #define TEST                0x57
27 #define ZMOW                0x5A
28 #define ZMOH                0x5B
29 #define ZMHH                0x5C
30 #define BPADDR              0x7C
31 #define BPDATA              0x7D
32 #define SIZEL               0x8C
33 #define HSIZE8              0xC0
34 #define VSIZE8              0xC1
35 #define CTRL1               0xC3
36 
37 #define CTRLI               0x50
38 #define CTRLI_LP_DP         0x80
39 
40 #define CTRL0               0xC2
41 #define CTRL0_YUV422        0x08
42 #define CTRL0_YUV_EN        0x04
43 #define CTRL0_RGB_EN        0x02
44 
45 #define CTRL2               0x86
46 #define CTRL2_DCW_EN        0x20
47 #define CTRL2_SDE_EN        0x10
48 #define CTRL2_UV_ADJ_EN     0x08
49 #define CTRL2_UV_AVG_EN     0x04
50 #define CTRL2_CMX_EN        0x01
51 
52 #define CTRL3               0x87
53 #define CTRL3_BPC_EN        0x80
54 #define CTRL3_WPC_EN        0x40
55 #define R_DVP_SP            0xD3
56 #define R_DVP_SP_AUTO_MODE  0x80
57 
58 #define R_BYPASS                0x05
59 #define R_BYPASS_DSP_EN         0x00
60 #define R_BYPASS_DSP_BYPAS      0x01
61 
62 #define IMAGE_MODE              0xDA
63 #define IMAGE_MODE_JPEG_EN      0x10
64 #define IMAGE_MODE_RGB565       0x08
65 
66 #define RESET                   0xE0
67 #define RESET_JPEG              0x10
68 #define RESET_DVP               0x04
69 
70 #define MC_BIST                 0xF9
71 #define MC_BIST_RESET           0x80
72 #define MC_BIST_BOOT_ROM_SEL    0x40
73 
74 #define BANK_SEL                0xFF
75 #define BANK_SEL_DSP            0x00
76 #define BANK_SEL_SENSOR         0x01
77 
78 /* Sensor register bank FF=0x01*/
79 #define COM1                0x03
80 #define REG_PID             0x0A
81 #define REG_PID_VAL         0x26
82 #define REG_VER             0x0B
83 #define REG_VER_VAL         0x42
84 #define AEC                 0x10
85 #define CLKRC               0x11
86 #define COM10               0x15
87 #define HSTART              0x17
88 #define HSTOP               0x18
89 #define VSTART              0x19
90 #define VSTOP               0x1A
91 #define AEW                 0x24
92 #define AEB                 0x25
93 #define ARCOM2              0x34
94 #define FLL                 0x46
95 #define FLH                 0x47
96 #define COM19               0x48
97 #define ZOOMS               0x49
98 #define BD50                0x4F
99 #define BD60                0x50
100 #define REG5D               0x5D
101 #define REG5E               0x5E
102 #define REG5F               0x5F
103 #define REG60               0x60
104 #define HISTO_LOW           0x61
105 #define HISTO_HIGH          0x62
106 
107 #define REG04               0x04
108 #define REG04_DEFAULT       0x28
109 #define REG04_HFLIP_IMG     0x80
110 #define REG04_VFLIP_IMG     0x40
111 #define REG04_HREF_EN       0x08
112 #define REG04_SET(x)        (REG04_DEFAULT | x)
113 
114 #define COM2                0x09
115 #define COM2_OUT_DRIVE_3x   0x02
116 
117 #define COM3                0x0C
118 #define COM3_DEFAULT        0x38
119 #define COM3_BAND_AUTO      0x02
120 #define COM3_BAND_SET(x)    (COM3_DEFAULT | x)
121 
122 #define COM7                0x12
123 #define COM7_SRST           0x80
124 #define COM7_RES_UXGA       0x00 /* UXGA */
125 #define COM7_ZOOM_EN        0x04 /* Enable Zoom */
126 #define COM7_COLOR_BAR      0x02 /* Enable Color Bar Test */
127 
128 #define COM8                0x13
129 #define COM8_DEFAULT        0xC0
130 #define COM8_BNDF_EN        0x20 /* Enable Banding filter */
131 #define COM8_AGC_EN         0x04 /* AGC Auto/Manual control selection */
132 #define COM8_AEC_EN         0x01 /* Auto/Manual Exposure control */
133 #define COM8_SET(x)         (COM8_DEFAULT | x)
134 
135 #define COM9                0x14 /* AGC gain ceiling */
136 #define COM9_DEFAULT        0x08
137 #define COM9_AGC_GAIN_8x    0x02 /* AGC:    8x */
138 #define COM9_AGC_SET(x)     (COM9_DEFAULT | (x << 5))
139 
140 #define COM10               0x15
141 
142 #define CTRL1_AWB           0x08 /* Enable AWB */
143 
144 #define VV                  0x26
145 #define VV_AGC_TH_SET(h, l) ((h << 4) | (l & 0x0F))
146 
147 #define REG32               0x32
148 #define REG32_UXGA          0x36
149 
150 /* Configuration arrays */
151 #define SVGA_HSIZE     (800)
152 #define SVGA_VSIZE     (600)
153 
154 #define UXGA_HSIZE     (1600)
155 #define UXGA_VSIZE     (1200)
156 
157 struct ov2640_reg {
158 	uint8_t addr;
159 	uint8_t value;
160 };
161 
162 static const struct ov2640_reg default_regs[] = {
163 	{ BANK_SEL, BANK_SEL_DSP },
164 	{ 0x2c,     0xff },
165 	{ 0x2e,     0xdf },
166 	{ BANK_SEL, BANK_SEL_SENSOR },
167 	{ 0x3c,     0x32 },
168 	{ CLKRC,    0x80 }, /* Set PCLK divider */
169 	{ COM2,     COM2_OUT_DRIVE_3x }, /* Output drive x2 */
170 	{ REG04,    REG04_SET(REG04_HREF_EN)},
171 	{ COM8,     COM8_SET(COM8_BNDF_EN | COM8_AGC_EN | COM8_AEC_EN) },
172 	{ COM9,     COM9_AGC_SET(COM9_AGC_GAIN_8x)},
173 	{ COM10,    0x00 }, /* Invert VSYNC */
174 	{ 0x2c,     0x0c },
175 	{ 0x33,     0x78 },
176 	{ 0x3a,     0x33 },
177 	{ 0x3b,     0xfb },
178 	{ 0x3e,     0x00 },
179 	{ 0x43,     0x11 },
180 	{ 0x16,     0x10 },
181 	{ 0x39,     0x02 },
182 	{ 0x35,     0x88 },
183 	{ 0x22,     0x0a },
184 	{ 0x37,     0x40 },
185 	{ 0x23,     0x00 },
186 	{ ARCOM2,   0xa0 },
187 	{ 0x06,     0x02 },
188 	{ 0x06,     0x88 },
189 	{ 0x07,     0xc0 },
190 	{ 0x0d,     0xb7 },
191 	{ 0x0e,     0x01 },
192 	{ 0x4c,     0x00 },
193 	{ 0x4a,     0x81 },
194 	{ 0x21,     0x99 },
195 	{ AEW,      0x40 },
196 	{ AEB,      0x38 },
197 	/* AGC/AEC fast mode operating region */
198 	{ VV,       VV_AGC_TH_SET(0x08, 0x02) },
199 	{ COM19,    0x00 }, /* Zoom control 2 LSBs */
200 	{ ZOOMS,    0x00 }, /* Zoom control 8 MSBs */
201 	{ 0x5c,     0x00 },
202 	{ 0x63,     0x00 },
203 	{ FLL,      0x00 },
204 	{ FLH,      0x00 },
205 
206 	/* Set banding filter */
207 	{ COM3,     COM3_BAND_SET(COM3_BAND_AUTO) },
208 	{ REG5D,    0x55 },
209 	{ REG5E,    0x7d },
210 	{ REG5F,    0x7d },
211 	{ REG60,    0x55 },
212 	{ HISTO_LOW,   0x70 },
213 	{ HISTO_HIGH,  0x80 },
214 	{ 0x7c,     0x05 },
215 	{ 0x20,     0x80 },
216 	{ 0x28,     0x30 },
217 	{ 0x6c,     0x00 },
218 	{ 0x6d,     0x80 },
219 	{ 0x6e,     0x00 },
220 	{ 0x70,     0x02 },
221 	{ 0x71,     0x94 },
222 	{ 0x73,     0xc1 },
223 	{ 0x3d,     0x34 },
224 	/* { COM7,   COM7_RES_UXGA | COM7_ZOOM_EN }, */
225 	{ 0x5a,     0x57 },
226 	{ BD50,     0xbb },
227 	{ BD60,     0x9c },
228 
229 	{ BANK_SEL, BANK_SEL_DSP },
230 	{ 0xe5,     0x7f },
231 	{ MC_BIST,  MC_BIST_RESET | MC_BIST_BOOT_ROM_SEL },
232 	{ 0x41,     0x24 },
233 	{ RESET,    RESET_JPEG | RESET_DVP },
234 	{ 0x76,     0xff },
235 	{ 0x33,     0xa0 },
236 	{ 0x42,     0x20 },
237 	{ 0x43,     0x18 },
238 	{ 0x4c,     0x00 },
239 	{ CTRL3,    CTRL3_BPC_EN | CTRL3_WPC_EN | 0x10 },
240 	{ 0x88,     0x3f },
241 	{ 0xd7,     0x03 },
242 	{ 0xd9,     0x10 },
243 	{ R_DVP_SP, R_DVP_SP_AUTO_MODE | 0x2 },
244 	{ 0xc8,     0x08 },
245 	{ 0xc9,     0x80 },
246 	{ BPADDR,   0x00 },
247 	{ BPDATA,   0x00 },
248 	{ BPADDR,   0x03 },
249 	{ BPDATA,   0x48 },
250 	{ BPDATA,   0x48 },
251 	{ BPADDR,   0x08 },
252 	{ BPDATA,   0x20 },
253 	{ BPDATA,   0x10 },
254 	{ BPDATA,   0x0e },
255 	{ 0x90,     0x00 },
256 	{ 0x91,     0x0e },
257 	{ 0x91,     0x1a },
258 	{ 0x91,     0x31 },
259 	{ 0x91,     0x5a },
260 	{ 0x91,     0x69 },
261 	{ 0x91,     0x75 },
262 	{ 0x91,     0x7e },
263 	{ 0x91,     0x88 },
264 	{ 0x91,     0x8f },
265 	{ 0x91,     0x96 },
266 	{ 0x91,     0xa3 },
267 	{ 0x91,     0xaf },
268 	{ 0x91,     0xc4 },
269 	{ 0x91,     0xd7 },
270 	{ 0x91,     0xe8 },
271 	{ 0x91,     0x20 },
272 	{ 0x92,     0x00 },
273 	{ 0x93,     0x06 },
274 	{ 0x93,     0xe3 },
275 	{ 0x93,     0x03 },
276 	{ 0x93,     0x03 },
277 	{ 0x93,     0x00 },
278 	{ 0x93,     0x02 },
279 	{ 0x93,     0x00 },
280 	{ 0x93,     0x00 },
281 	{ 0x93,     0x00 },
282 	{ 0x93,     0x00 },
283 	{ 0x93,     0x00 },
284 	{ 0x93,     0x00 },
285 	{ 0x93,     0x00 },
286 	{ 0x96,     0x00 },
287 	{ 0x97,     0x08 },
288 	{ 0x97,     0x19 },
289 	{ 0x97,     0x02 },
290 	{ 0x97,     0x0c },
291 	{ 0x97,     0x24 },
292 	{ 0x97,     0x30 },
293 	{ 0x97,     0x28 },
294 	{ 0x97,     0x26 },
295 	{ 0x97,     0x02 },
296 	{ 0x97,     0x98 },
297 	{ 0x97,     0x80 },
298 	{ 0x97,     0x00 },
299 	{ 0x97,     0x00 },
300 	{ 0xa4,     0x00 },
301 	{ 0xa8,     0x00 },
302 	{ 0xc5,     0x11 },
303 	{ 0xc6,     0x51 },
304 	{ 0xbf,     0x80 },
305 	{ 0xc7,     0x10 },
306 	{ 0xb6,     0x66 },
307 	{ 0xb8,     0xA5 },
308 	{ 0xb7,     0x64 },
309 	{ 0xb9,     0x7C },
310 	{ 0xb3,     0xaf },
311 	{ 0xb4,     0x97 },
312 	{ 0xb5,     0xFF },
313 	{ 0xb0,     0xC5 },
314 	{ 0xb1,     0x94 },
315 	{ 0xb2,     0x0f },
316 	{ 0xc4,     0x5c },
317 	{ 0xa6,     0x00 },
318 	{ 0xa7,     0x20 },
319 	{ 0xa7,     0xd8 },
320 	{ 0xa7,     0x1b },
321 	{ 0xa7,     0x31 },
322 	{ 0xa7,     0x00 },
323 	{ 0xa7,     0x18 },
324 	{ 0xa7,     0x20 },
325 	{ 0xa7,     0xd8 },
326 	{ 0xa7,     0x19 },
327 	{ 0xa7,     0x31 },
328 	{ 0xa7,     0x00 },
329 	{ 0xa7,     0x18 },
330 	{ 0xa7,     0x20 },
331 	{ 0xa7,     0xd8 },
332 	{ 0xa7,     0x19 },
333 	{ 0xa7,     0x31 },
334 	{ 0xa7,     0x00 },
335 	{ 0xa7,     0x18 },
336 	{ 0x7f,     0x00 },
337 	{ 0xe5,     0x1f },
338 	{ 0xe1,     0x77 },
339 	{ 0xdd,     0x7f },
340 	{ CTRL0,    CTRL0_YUV422 | CTRL0_YUV_EN | CTRL0_RGB_EN },
341 	{ 0x00,     0x00 }
342 };
343 
344 static const struct ov2640_reg uxga_regs[] = {
345 	{ BANK_SEL, BANK_SEL_SENSOR },
346 	/* DSP input image resoultion and window size control */
347 	{ COM7,    COM7_RES_UXGA},
348 	{ COM1,    0x0F }, /* UXGA=0x0F, SVGA=0x0A, CIF=0x06 */
349 	{ REG32,   REG32_UXGA }, /* UXGA=0x36, SVGA/CIF=0x09 */
350 
351 	{ HSTART,  0x11 }, /* UXGA=0x11, SVGA/CIF=0x11 */
352 	{ HSTOP,   0x75 }, /* UXGA=0x75, SVGA/CIF=0x43 */
353 
354 	{ VSTART,  0x01 }, /* UXGA=0x01, SVGA/CIF=0x00 */
355 	{ VSTOP,   0x97 }, /* UXGA=0x97, SVGA/CIF=0x4b */
356 	{ 0x3d,    0x34 }, /* UXGA=0x34, SVGA/CIF=0x38 */
357 
358 	{ 0x35,    0x88 },
359 	{ 0x22,    0x0a },
360 	{ 0x37,    0x40 },
361 	{ 0x34,    0xa0 },
362 	{ 0x06,    0x02 },
363 	{ 0x0d,    0xb7 },
364 	{ 0x0e,    0x01 },
365 	{ 0x42,    0x83 },
366 
367 	/*
368 	 * Set DSP input image size and offset.
369 	 * The sensor output image can be scaled with OUTW/OUTH
370 	 */
371 	{ BANK_SEL, BANK_SEL_DSP },
372 	{ R_BYPASS, R_BYPASS_DSP_BYPAS },
373 
374 	{ RESET,   RESET_DVP },
375 	{ HSIZE8,  (UXGA_HSIZE>>3)}, /* Image Horizontal Size HSIZE[10:3] */
376 	{ VSIZE8,  (UXGA_VSIZE>>3)}, /* Image Vertiacl Size VSIZE[10:3] */
377 
378 	/* {HSIZE[11], HSIZE[2:0], VSIZE[2:0]} */
379 	{ SIZEL,   ((UXGA_HSIZE>>6)&0x40) | ((UXGA_HSIZE&0x7)<<3) | (UXGA_VSIZE&0x7)},
380 
381 	{ XOFFL,   0x00 }, /* OFFSET_X[7:0] */
382 	{ YOFFL,   0x00 }, /* OFFSET_Y[7:0] */
383 	{ HSIZE,   ((UXGA_HSIZE>>2)&0xFF) }, /* H_SIZE[7:0] real/4 */
384 	{ VSIZE,   ((UXGA_VSIZE>>2)&0xFF) }, /* V_SIZE[7:0] real/4 */
385 
386 	/* V_SIZE[8]/OFFSET_Y[10:8]/H_SIZE[8]/OFFSET_X[10:8] */
387 	{ VHYX,    ((UXGA_VSIZE>>3)&0x80) | ((UXGA_HSIZE>>7)&0x08) },
388 	{ TEST,    (UXGA_HSIZE>>4)&0x80}, /* H_SIZE[9] */
389 
390 	{ CTRL2,   CTRL2_DCW_EN | CTRL2_SDE_EN |
391 		CTRL2_UV_AVG_EN | CTRL2_CMX_EN | CTRL2_UV_ADJ_EN },
392 
393 	/* H_DIVIDER/V_DIVIDER */
394 	{ CTRLI,   CTRLI_LP_DP | 0x00},
395 	/* DVP prescalar */
396 	{ R_DVP_SP, R_DVP_SP_AUTO_MODE | 0x04},
397 
398 	{ R_BYPASS, R_BYPASS_DSP_EN },
399 	{ RESET,    0x00 },
400 	{0, 0},
401 };
402 
403 #define NUM_BRIGHTNESS_LEVELS (5)
404 static const uint8_t brightness_regs[NUM_BRIGHTNESS_LEVELS + 1][5] = {
405 	{ BPADDR, BPDATA, BPADDR, BPDATA, BPDATA },
406 	{ 0x00, 0x04, 0x09, 0x00, 0x00 }, /* -2 */
407 	{ 0x00, 0x04, 0x09, 0x10, 0x00 }, /* -1 */
408 	{ 0x00, 0x04, 0x09, 0x20, 0x00 }, /*  0 */
409 	{ 0x00, 0x04, 0x09, 0x30, 0x00 }, /* +1 */
410 	{ 0x00, 0x04, 0x09, 0x40, 0x00 }, /* +2 */
411 };
412 
413 #define NUM_CONTRAST_LEVELS (5)
414 static const uint8_t contrast_regs[NUM_CONTRAST_LEVELS + 1][7] = {
415 	{ BPADDR, BPDATA, BPADDR, BPDATA, BPDATA, BPDATA, BPDATA },
416 	{ 0x00, 0x04, 0x07, 0x20, 0x18, 0x34, 0x06 }, /* -2 */
417 	{ 0x00, 0x04, 0x07, 0x20, 0x1c, 0x2a, 0x06 }, /* -1 */
418 	{ 0x00, 0x04, 0x07, 0x20, 0x20, 0x20, 0x06 }, /*  0 */
419 	{ 0x00, 0x04, 0x07, 0x20, 0x24, 0x16, 0x06 }, /* +1 */
420 	{ 0x00, 0x04, 0x07, 0x20, 0x28, 0x0c, 0x06 }, /* +2 */
421 };
422 
423 #define NUM_SATURATION_LEVELS (5)
424 static const uint8_t saturation_regs[NUM_SATURATION_LEVELS + 1][5] = {
425 	{ BPADDR, BPDATA, BPADDR, BPDATA, BPDATA },
426 	{ 0x00, 0x02, 0x03, 0x28, 0x28 }, /* -2 */
427 	{ 0x00, 0x02, 0x03, 0x38, 0x38 }, /* -1 */
428 	{ 0x00, 0x02, 0x03, 0x48, 0x48 }, /*  0 */
429 	{ 0x00, 0x02, 0x03, 0x58, 0x58 }, /* +1 */
430 	{ 0x00, 0x02, 0x03, 0x58, 0x58 }, /* +2 */
431 };
432 
433 struct ov2640_data {
434 	const struct device *i2c;
435 	const struct device *reset_gpio;
436 	uint8_t reset_pin;
437 	gpio_dt_flags_t reset_pin_flags;
438 	struct video_format fmt;
439 	uint8_t i2c_addr;
440 };
441 
442 #define OV2640_VIDEO_FORMAT_CAP(width, height, format) \
443 	{ \
444 		.pixelformat = (format), \
445 		.width_min = (width), \
446 		.width_max = (width), \
447 		.height_min = (height), \
448 		.height_max = (height), \
449 		.width_step = 0, \
450 		.height_step = 0 \
451 	}
452 
453 static const struct video_format_cap fmts[] = {
454 	OV2640_VIDEO_FORMAT_CAP(160, 120, VIDEO_PIX_FMT_RGB565),   /* QQVGA */
455 	OV2640_VIDEO_FORMAT_CAP(176, 144, VIDEO_PIX_FMT_RGB565),   /* QCIF  */
456 	OV2640_VIDEO_FORMAT_CAP(240, 160, VIDEO_PIX_FMT_RGB565),   /* HQVGA */
457 	OV2640_VIDEO_FORMAT_CAP(320, 240, VIDEO_PIX_FMT_RGB565),   /* QVGA  */
458 	OV2640_VIDEO_FORMAT_CAP(352, 288, VIDEO_PIX_FMT_RGB565),   /* CIF   */
459 	OV2640_VIDEO_FORMAT_CAP(640, 480, VIDEO_PIX_FMT_RGB565),   /* VGA   */
460 	OV2640_VIDEO_FORMAT_CAP(800, 600, VIDEO_PIX_FMT_RGB565),   /* SVGA  */
461 	OV2640_VIDEO_FORMAT_CAP(1024, 768, VIDEO_PIX_FMT_RGB565),  /* XVGA  */
462 	OV2640_VIDEO_FORMAT_CAP(1280, 1024, VIDEO_PIX_FMT_RGB565), /* SXGA  */
463 	OV2640_VIDEO_FORMAT_CAP(1600, 1200, VIDEO_PIX_FMT_RGB565), /* UXGA  */
464 	OV2640_VIDEO_FORMAT_CAP(160, 120, VIDEO_PIX_FMT_JPEG),     /* QQVGA */
465 	OV2640_VIDEO_FORMAT_CAP(176, 144, VIDEO_PIX_FMT_JPEG),     /* QCIF  */
466 	OV2640_VIDEO_FORMAT_CAP(240, 160, VIDEO_PIX_FMT_JPEG),     /* HQVGA */
467 	OV2640_VIDEO_FORMAT_CAP(320, 240, VIDEO_PIX_FMT_JPEG),     /* QVGA  */
468 	OV2640_VIDEO_FORMAT_CAP(352, 288, VIDEO_PIX_FMT_JPEG),     /* CIF   */
469 	OV2640_VIDEO_FORMAT_CAP(640, 480, VIDEO_PIX_FMT_JPEG),     /* VGA   */
470 	OV2640_VIDEO_FORMAT_CAP(800, 600, VIDEO_PIX_FMT_JPEG),     /* SVGA  */
471 	OV2640_VIDEO_FORMAT_CAP(1024, 768, VIDEO_PIX_FMT_JPEG),    /* XVGA  */
472 	OV2640_VIDEO_FORMAT_CAP(1280, 1024, VIDEO_PIX_FMT_JPEG),   /* SXGA  */
473 	OV2640_VIDEO_FORMAT_CAP(1600, 1200, VIDEO_PIX_FMT_JPEG),   /* UXGA  */
474 	{ 0 }
475 };
476 
ov2640_write_reg(const struct device * dev,uint8_t reg_addr,uint8_t value)477 static int ov2640_write_reg(const struct device *dev, uint8_t reg_addr,
478 				uint8_t value)
479 {
480 	struct ov2640_data *drv_data = dev->data;
481 	uint8_t tries = 3;
482 
483 	/**
484 	 * It rarely happens that the camera does not respond with ACK signal.
485 	 * In that case it usually responds on 2nd try but there is a 3rd one
486 	 * just to be sure that the connection error is not caused by driver
487 	 * itself.
488 	 */
489 	while (tries--) {
490 		if (!i2c_reg_write_byte(drv_data->i2c, drv_data->i2c_addr,
491 								reg_addr, value)) {
492 			return 0;
493 		}
494 		/* If writing failed wait 5ms before next attempt */
495 		k_msleep(5);
496 	}
497 	LOG_ERR("failed to write 0x%x to 0x%x", value, reg_addr);
498 
499 	return -1;
500 }
501 
ov2640_read_reg(const struct device * dev,uint8_t reg_addr)502 static int ov2640_read_reg(const struct device *dev, uint8_t reg_addr)
503 {
504 	struct ov2640_data *drv_data = dev->data;
505 	uint8_t tries = 3;
506 	uint8_t value;
507 
508 	/**
509 	 * It rarely happens that the camera does not respond with ACK signal.
510 	 * In that case it usually responds on 2nd try but there is a 3rd one
511 	 * just to be sure that the connection error is not caused by driver
512 	 * itself.
513 	 */
514 	while (tries--) {
515 		if (!i2c_reg_read_byte(drv_data->i2c, drv_data->i2c_addr,
516 								reg_addr, &value)) {
517 			return value;
518 		}
519 		/* If reading failed wait 5ms before next attempt */
520 		k_msleep(5);
521 	}
522 	LOG_ERR("failed to read 0x%x register", reg_addr);
523 
524 	return -1;
525 }
526 
ov2640_write_all(const struct device * dev,const struct ov2640_reg * regs,uint16_t reg_num)527 static int ov2640_write_all(const struct device *dev,
528 				const struct ov2640_reg *regs, uint16_t reg_num)
529 {
530 	uint16_t i = 0;
531 
532 	for (i = 0; i < reg_num; i++) {
533 		int err;
534 
535 		err = ov2640_write_reg(dev, regs[i].addr, regs[i].value);
536 		if (err) {
537 			return err;
538 		}
539 	}
540 
541 	return 0;
542 }
543 
ov2640_soft_reset(const struct device * dev)544 static int ov2640_soft_reset(const struct device *dev)
545 {
546 	int ret = 0;
547 
548 	/* Switch to DSP register bank */
549 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
550 
551 	/* Initiate system reset */
552 	ret |= ov2640_write_reg(dev, COM7, COM7_SRST);
553 
554 	return ret;
555 }
556 
ov2640_set_level(const struct device * dev,int level,int max_level,int cols,const uint8_t regs[][cols])557 static int ov2640_set_level(const struct device *dev, int level,
558 				int max_level, int cols, const uint8_t regs[][cols])
559 {
560 	int ret = 0;
561 
562 	level += (max_level / 2 + 1);
563 	if (level < 0 || level > max_level) {
564 		return -ENOTSUP;
565 	}
566 
567 	/* Switch to DSP register bank */
568 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_DSP);
569 
570 	for (int i = 0; i < (ARRAY_SIZE(regs[0]) / sizeof(regs[0][0])); i++)	{
571 		ret |= ov2640_write_reg(dev, regs[0][i], regs[level][i]);
572 	}
573 
574 	return ret;
575 }
576 
ov2640_set_brightness(const struct device * dev,int level)577 static int ov2640_set_brightness(const struct device *dev, int level)
578 {
579 	int ret = 0;
580 
581 	ret = ov2640_set_level(dev, level, NUM_BRIGHTNESS_LEVELS,
582 			ARRAY_SIZE(brightness_regs[0]), brightness_regs);
583 
584 	if (ret == -ENOTSUP) {
585 		LOG_ERR("Brightness level %d not supported", level);
586 	}
587 
588 	return ret;
589 }
590 
ov2640_set_saturation(const struct device * dev,int level)591 static int ov2640_set_saturation(const struct device *dev, int level)
592 {
593 	int ret = 0;
594 
595 	ret = ov2640_set_level(dev, level, NUM_SATURATION_LEVELS,
596 			ARRAY_SIZE(saturation_regs[0]), saturation_regs);
597 
598 	if (ret == -ENOTSUP) {
599 		LOG_ERR("Saturation level %d not supported", level);
600 	}
601 
602 	return ret;
603 }
604 
ov2640_set_contrast(const struct device * dev,int level)605 static int ov2640_set_contrast(const struct device *dev, int level)
606 {
607 	int ret = 0;
608 
609 	ret = ov2640_set_level(dev, level, NUM_CONTRAST_LEVELS,
610 			ARRAY_SIZE(contrast_regs[0]), contrast_regs);
611 
612 	if (ret == -ENOTSUP) {
613 		LOG_ERR("Contrast level %d not supported", level);
614 	}
615 
616 	return ret;
617 }
618 
ov2640_set_output_format(const struct device * dev,int output_format)619 static int ov2640_set_output_format(const struct device *dev,
620 				int output_format)
621 {
622 	int ret = 0;
623 
624 	/* Switch to DSP register bank */
625 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_DSP);
626 
627 	if (output_format == VIDEO_PIX_FMT_JPEG)	{
628 		/* Enable JPEG compression */
629 		ret |= ov2640_write_reg(dev, IMAGE_MODE, IMAGE_MODE_JPEG_EN);
630 	} else if (output_format == VIDEO_PIX_FMT_RGB565)	{
631 		/* Disable JPEG compression and set output to RGB565 */
632 		ret |= ov2640_write_reg(dev, IMAGE_MODE, IMAGE_MODE_RGB565);
633 	} else {
634 		LOG_ERR("Image format not supported");
635 		return -ENOTSUP;
636 	}
637 	k_msleep(30);
638 
639 	return ret;
640 }
641 
ov2640_set_quality(const struct device * dev,int qs)642 static int ov2640_set_quality(const struct device *dev, int qs)
643 {
644 	int ret = 0;
645 
646 	/* Switch to DSP register bank */
647 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_DSP);
648 
649 	/* Write QS register */
650 	ret |= ov2640_write_reg(dev, QS, qs);
651 
652 	return ret;
653 }
654 
ov2640_set_colorbar(const struct device * dev,uint8_t enable)655 static int ov2640_set_colorbar(const struct device *dev, uint8_t enable)
656 {
657 	int ret = 0;
658 
659 	uint8_t reg;
660 
661 	/* Switch to SENSOR register bank */
662 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
663 
664 	/* Update COM7 to enable/disable color bar test pattern */
665 	reg = ov2640_read_reg(dev, COM7);
666 
667 	if (enable) {
668 		reg |= COM7_COLOR_BAR;
669 	} else {
670 		reg &= ~COM7_COLOR_BAR;
671 	}
672 
673 	ret |= ov2640_write_reg(dev, COM7, reg);
674 
675 	return ret;
676 }
677 
ov2640_set_white_bal(const struct device * dev,int enable)678 static int ov2640_set_white_bal(const struct device *dev, int enable)
679 {
680 	int ret = 0;
681 
682 	uint8_t reg;
683 
684 	/* Switch to SENSOR register bank */
685 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
686 
687 	/* Update CTRL1 to enable/disable automatic white balance*/
688 	reg = ov2640_read_reg(dev, CTRL1);
689 
690 	if (enable) {
691 		reg |= CTRL1_AWB;
692 	} else {
693 		reg &= ~CTRL1_AWB;
694 	}
695 
696 	ret |= ov2640_write_reg(dev, CTRL1, reg);
697 
698 	return ret;
699 }
700 
ov2640_set_gain_ctrl(const struct device * dev,int enable)701 static int ov2640_set_gain_ctrl(const struct device *dev, int enable)
702 {
703 	int ret = 0;
704 
705 	uint8_t reg;
706 
707 	/* Switch to SENSOR register bank */
708 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
709 
710 	/* Update COM8 to enable/disable automatic gain control */
711 	reg = ov2640_read_reg(dev, COM8);
712 
713 	if (enable) {
714 		reg |= COM8_AGC_EN;
715 	} else {
716 		reg &= ~COM8_AGC_EN;
717 	}
718 
719 	ret |= ov2640_write_reg(dev, COM8, reg);
720 
721 	return ret;
722 }
723 
ov2640_set_exposure_ctrl(const struct device * dev,int enable)724 static int ov2640_set_exposure_ctrl(const struct device *dev, int enable)
725 {
726 	int ret = 0;
727 
728 	uint8_t reg;
729 
730 	/* Switch to SENSOR register bank */
731 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
732 
733 	/* Update COM8  to enable/disable automatic exposure control */
734 	reg = ov2640_read_reg(dev, COM8);
735 
736 	if (enable) {
737 		reg |= COM8_AEC_EN;
738 	} else {
739 		reg &= ~COM8_AEC_EN;
740 	}
741 
742 	ret |= ov2640_write_reg(dev, COM8, reg);
743 
744 	return ret;
745 }
746 
ov2640_set_horizontal_mirror(const struct device * dev,int enable)747 static int ov2640_set_horizontal_mirror(const struct device *dev,
748 				int enable)
749 {
750 	int ret = 0;
751 
752 	uint8_t reg;
753 
754 	/* Switch to SENSOR register bank */
755 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
756 
757 	/* Update REG04 to enable/disable horizontal mirror */
758 	reg = ov2640_read_reg(dev, REG04);
759 
760 	if (enable) {
761 		reg |= REG04_HFLIP_IMG;
762 	} else {
763 		reg &= ~REG04_HFLIP_IMG;
764 	}
765 
766 	ret |= ov2640_write_reg(dev, REG04, reg);
767 
768 	return ret;
769 }
770 
ov2640_set_vertical_flip(const struct device * dev,int enable)771 static int ov2640_set_vertical_flip(const struct device *dev, int enable)
772 {
773 	int ret = 0;
774 
775 	uint8_t reg;
776 
777 	/* Switch to SENSOR register bank */
778 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
779 
780 	/* Update REG04 to enable/disable vertical flip */
781 	reg = ov2640_read_reg(dev, REG04);
782 
783 	if (enable) {
784 		reg |= REG04_VFLIP_IMG;
785 	} else {
786 		reg &= ~REG04_VFLIP_IMG;
787 	}
788 
789 	ret |= ov2640_write_reg(dev, REG04, reg);
790 
791 	return ret;
792 }
793 
ov2640_set_resolution(const struct device * dev,uint16_t img_width,uint16_t img_height)794 static int ov2640_set_resolution(const struct device *dev,
795 				uint16_t img_width, uint16_t img_height)
796 {
797 	int ret = 0;
798 
799 	uint16_t w = img_width;
800 	uint16_t h = img_height;
801 
802 	/* Disable DSP */
803 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_DSP);
804 	ret |= ov2640_write_reg(dev, R_BYPASS, R_BYPASS_DSP_BYPAS);
805 
806 	/* Write output width */
807 	ret |= ov2640_write_reg(dev, ZMOW, (w >> 2) & 0xFF); /* OUTW[7:0] (real/4) */
808 	ret |= ov2640_write_reg(dev, ZMOH, (h >> 2) & 0xFF); /* OUTH[7:0] (real/4) */
809 	ret |= ov2640_write_reg(dev, ZMHH, ((h >> 8) & 0x04) |
810 							((w>>10) & 0x03)); /* OUTH[8]/OUTW[9:8] */
811 
812 	/* Set CLKRC */
813 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
814 	ret |= ov2640_write_reg(dev, CLKRC, 0x87);
815 
816 	/* Write DSP input registers */
817 	ov2640_write_all(dev, uxga_regs, ARRAY_SIZE(uxga_regs));
818 
819 	/* Enable DSP */
820 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_DSP);
821 	ret |= ov2640_write_reg(dev, R_BYPASS, R_BYPASS_DSP_EN);
822 
823 	k_msleep(30);
824 
825 	return ret;
826 }
827 
ov2640_check_connection(const struct device * dev)828 uint8_t ov2640_check_connection(const struct device *dev)
829 {
830 	int ret = 0;
831 
832 	uint8_t reg_pid_val, reg_ver_val;
833 
834 	ret |= ov2640_write_reg(dev, BANK_SEL, BANK_SEL_SENSOR);
835 	reg_pid_val = ov2640_read_reg(dev, REG_PID);
836 	reg_ver_val = ov2640_read_reg(dev, REG_VER);
837 
838 	if (REG_PID_VAL != reg_pid_val || REG_VER_VAL != reg_ver_val) {
839 		LOG_ERR("OV2640 not detected\n");
840 		return -ENODEV;
841 	}
842 
843 	return ret;
844 }
845 
ov2640_set_fmt(const struct device * dev,enum video_endpoint_id ep,struct video_format * fmt)846 static int ov2640_set_fmt(const struct device *dev,
847 			enum video_endpoint_id ep, struct video_format *fmt)
848 {
849 	struct ov2640_data *drv_data = dev->data;
850 	uint16_t width, height;
851 	int ret = 0;
852 	int i = 0;
853 
854 	/* We only support RGB565 and JPEG pixel formats */
855 	if (fmt->pixelformat != VIDEO_PIX_FMT_RGB565 && fmt->pixelformat != VIDEO_PIX_FMT_JPEG) {
856 		LOG_ERR("ov2640 camera supports only RGB565 and JPG pixelformats!");
857 		return -ENOTSUP;
858 	}
859 
860 	width = fmt->width;
861 	height = fmt->height;
862 
863 	if (!memcmp(&drv_data->fmt, fmt, sizeof(drv_data->fmt))) {
864 		/* nothing to do */
865 		return 0;
866 	}
867 
868 	drv_data->fmt = *fmt;
869 
870 	/* Set output format */
871 	ret |= ov2640_set_output_format(dev, fmt->pixelformat);
872 
873 	/* Check if camera is capable of handling given format */
874 	while (fmts[i].pixelformat) {
875 		if (fmts[i].width_min == width && fmts[i].height_min == height &&
876 			fmts[i].pixelformat == fmt->pixelformat) {
877 			/* Set window size */
878 			ret |= ov2640_set_resolution(dev, fmt->width, fmt->height);
879 			return ret;
880 		}
881 		i++;
882 	}
883 
884 	/* Camera is not capable of handling given format */
885 	LOG_ERR("Image format not supported\n");
886 	return -ENOTSUP;
887 }
888 
ov2640_get_fmt(const struct device * dev,enum video_endpoint_id ep,struct video_format * fmt)889 static int ov2640_get_fmt(const struct device *dev,
890 			enum video_endpoint_id ep, struct video_format *fmt)
891 {
892 	struct ov2640_data *drv_data = dev->data;
893 
894 	*fmt = drv_data->fmt;
895 
896 	return 0;
897 }
898 
ov2640_stream_start(const struct device * dev)899 static int ov2640_stream_start(const struct device *dev)
900 {
901 	return 0;
902 }
903 
ov2640_stream_stop(const struct device * dev)904 static int ov2640_stream_stop(const struct device *dev)
905 {
906 	return 0;
907 }
908 
ov2640_get_caps(const struct device * dev,enum video_endpoint_id ep,struct video_caps * caps)909 static int ov2640_get_caps(const struct device *dev,
910 			   enum video_endpoint_id ep,
911 			   struct video_caps *caps)
912 {
913 	caps->format_caps = fmts;
914 	return 0;
915 }
916 
ov2640_set_ctrl(const struct device * dev,unsigned int cid,void * value)917 static int ov2640_set_ctrl(const struct device *dev,
918 				unsigned int cid, void *value)
919 {
920 	int ret = 0;
921 
922 	switch (cid) {
923 	case VIDEO_CID_HFLIP:
924 		ret |= ov2640_set_horizontal_mirror(dev, (int)value);
925 		break;
926 	case VIDEO_CID_VFLIP:
927 		ret |= ov2640_set_vertical_flip(dev, (int)value);
928 		break;
929 	case VIDEO_CID_CAMERA_EXPOSURE:
930 		ret |= ov2640_set_exposure_ctrl(dev, (int)value);
931 		break;
932 	case VIDEO_CID_CAMERA_GAIN:
933 		ret |= ov2640_set_gain_ctrl(dev, (int)value);
934 		break;
935 	case VIDEO_CID_CAMERA_BRIGHTNESS:
936 		ret |= ov2640_set_brightness(dev, (int)value);
937 		break;
938 	case VIDEO_CID_CAMERA_SATURATION:
939 		ret |= ov2640_set_saturation(dev, (int)value);
940 		break;
941 	case VIDEO_CID_CAMERA_WHITE_BAL:
942 		ret |= ov2640_set_white_bal(dev, (int)value);
943 		break;
944 	case VIDEO_CID_CAMERA_CONTRAST:
945 		ret |= ov2640_set_contrast(dev, (int)value);
946 		break;
947 	case VIDEO_CID_CAMERA_COLORBAR:
948 		ret |= ov2640_set_colorbar(dev, (int)value);
949 		break;
950 	case VIDEO_CID_CAMERA_QUALITY:
951 		ret |= ov2640_set_quality(dev, (int)value);
952 		break;
953 	default:
954 		return -ENOTSUP;
955 	}
956 
957 	return ret;
958 }
959 
960 static const struct video_driver_api ov2640_driver_api = {
961 	.set_format = ov2640_set_fmt,
962 	.get_format = ov2640_get_fmt,
963 	.get_caps = ov2640_get_caps,
964 	.stream_start = ov2640_stream_start,
965 	.stream_stop = ov2640_stream_stop,
966 	.set_ctrl = ov2640_set_ctrl,
967 };
968 
ov2640_init(const struct device * dev)969 static int ov2640_init(const struct device *dev)
970 {
971 	struct video_format fmt;
972 	struct ov2640_data *drv_data = dev->data;
973 	int ret = 0;
974 
975 	if (drv_data->reset_gpio) {
976 		ret = gpio_pin_configure(drv_data->reset_gpio,
977 					 drv_data->reset_pin,
978 					 GPIO_OUTPUT_ACTIVE |
979 					 drv_data->reset_pin_flags);
980 		if (ret) {
981 			return ret;
982 		}
983 
984 		/* Perform hardware reset */
985 		gpio_pin_set(drv_data->reset_gpio,
986 				 drv_data->reset_pin, 0);
987 		k_msleep(1);
988 		gpio_pin_set(drv_data->reset_gpio,
989 				 drv_data->reset_pin, 1);
990 		k_msleep(1);
991 	}
992 
993 	ret = ov2640_check_connection(dev);
994 
995 	if (ret) {
996 		return ret;
997 	}
998 
999 	ov2640_soft_reset(dev);
1000 	k_msleep(300);
1001 
1002 	ov2640_write_all(dev, default_regs, ARRAY_SIZE(default_regs));
1003 
1004 	/* set default/init format SVGA RGB565 */
1005 	fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
1006 	fmt.width = SVGA_HSIZE;
1007 	fmt.height = SVGA_VSIZE;
1008 	fmt.pitch = SVGA_HSIZE * 2;
1009 	ret = ov2640_set_fmt(dev, VIDEO_EP_OUT, &fmt);
1010 	if (ret) {
1011 		LOG_ERR("Unable to configure default format");
1012 		return -EIO;
1013 	}
1014 
1015 	ret |= ov2640_set_exposure_ctrl(dev, 1);
1016 	ret |= ov2640_set_white_bal(dev, 1);
1017 
1018 	return ret;
1019 }
1020 
1021 /* Unique Instance */
1022 static struct ov2640_data ov2640_data_0;
1023 
ov2640_init_0(const struct device * dev)1024 static int ov2640_init_0(const struct device *dev)
1025 {
1026 	struct ov2640_data *drv_data = dev->data;
1027 
1028 #if DT_NODE_EXISTS(reset_gpios)
1029 	char *gpio_name = DT_INST_GPIO_LABEL(0, reset_gpios);
1030 
1031 	drv_data->reset_pin = DT_INST_GPIO_PIN(0, reset_gpios);
1032 	drv_data->reset_pin_flags = DT_INST_GPIO_FLAGS(0, reset_gpios);
1033 
1034 	if (gpio_name) {
1035 		drv_data->reset_gpio = device_get_binding(gpio_name);
1036 		if (drv_data->reset_gpio == NULL) {
1037 			LOG_ERR("Failed to get pointer to %s device!",
1038 				gpio_name);
1039 		}
1040 	}
1041 #endif
1042 
1043 	drv_data->i2c = device_get_binding(DT_INST_BUS_LABEL(0));
1044 
1045 	if (drv_data->i2c == NULL) {
1046 		LOG_ERR("Failed to get pointer to %s device!",
1047 			DT_INST_LABEL(0));
1048 		return -EINVAL;
1049 	}
1050 
1051 	uint32_t i2c_cfg = I2C_MODE_MASTER |
1052 					I2C_SPEED_SET(I2C_SPEED_STANDARD);
1053 
1054 	if (i2c_configure(drv_data->i2c, i2c_cfg)) {
1055 		LOG_ERR("Failed to configure ov2640 i2c interface.");
1056 	}
1057 
1058 	drv_data->i2c_addr = DT_INST_REG_ADDR(0);
1059 
1060 	return ov2640_init(dev);
1061 }
1062 
1063 DEVICE_DT_INST_DEFINE(0, &ov2640_init_0, device_pm_control_nop,
1064 			&ov2640_data_0, NULL,
1065 			POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY,
1066 			&ov2640_driver_api);
1067