2 * A V4L2 driver for OmniVision OV7670 cameras.
4 * Copyright 2006 One Laptop Per Child Association, Inc. Written
5 * by Jonathan Corbet with substantial inspiration from Mark
6 * McClelland's ovcamchip code.
8 * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
10 * This file may be distributed under the terms of the GNU General
11 * Public License, version 2.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/slab.h>
17 #include <linux/delay.h>
18 #include <linux/videodev.h>
19 #include <media/v4l2-common.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <linux/i2c.h>
25 MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net>");
26 MODULE_DESCRIPTION("A low-level driver for OmniVision ov7670 sensors");
27 MODULE_LICENSE("GPL");
29 #define VERSION "2.00"
32 * 1.0 - by Momtchil Momtchev
33 * 1.03 - stability patches (Bri)
34 * 1.04 - added exposition control (Bri)
38 * Basic window sizes. These probably belong somewhere more globally
42 #define VGA_HEIGHT 480
43 #define QVGA_WIDTH 320
44 #define QVGA_HEIGHT 240
46 #define CIF_HEIGHT 288
47 #define QCIF_WIDTH 176
48 #define QCIF_HEIGHT 144
53 * Our nominal (default) frame rate.
55 #define OV7670_FRAME_RATE 30
58 * The 7670 sits on i2c with ID 0x21
60 #define OV7670_I2C_ADDR 0x21
61 #define OV7670_I2C_MAXRETRIES 3
64 #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
65 #define REG_BLUE 0x01 /* blue gain */
66 #define REG_RED 0x02 /* red gain */
67 #define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
68 #define REG_COM1 0x04 /* Control 1 */
69 #define COM1_CCIR656 0x40 /* CCIR656 enable */
70 #define REG_BAVE 0x05 /* U/B Average level */
71 #define REG_GbAVE 0x06 /* Y/Gb Average level */
72 #define REG_AECHH 0x07 /* AEC MS 5 bits */
73 #define REG_RAVE 0x08 /* V/R Average level */
74 #define REG_COM2 0x09 /* Control 2 */
75 #define COM2_SSLEEP 0x10 /* Soft sleep mode */
76 #define REG_PID 0x0a /* Product ID MSB */
77 #define REG_VER 0x0b /* Product ID LSB */
78 #define REG_COM3 0x0c /* Control 3 */
79 #define COM3_SWAP 0x40 /* Byte swap */
80 #define COM3_SCALEEN 0x08 /* Enable scaling */
81 #define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */
82 #define REG_COM4 0x0d /* Control 4 */
83 #define REG_COM5 0x0e /* All "reserved" */
84 #define REG_COM6 0x0f /* Control 6 */
85 #define REG_AECH 0x10 /* More bits of AEC value */
86 #define REG_CLKRC 0x11 /* Clocl control */
87 #define CLK_EXT 0x40 /* Use external clock directly */
88 #define CLK_SCALE 0x3f /* Mask for internal clock scale */
89 #define REG_COM7 0x12 /* Control 7 */
90 #define COM7_RESET 0x80 /* Register reset */
91 #define COM7_FMT_MASK 0x38
92 #define COM7_FMT_VGA 0x00
93 #define COM7_FMT_CIF 0x20 /* CIF format */
94 #define COM7_FMT_QVGA 0x10 /* QVGA format */
95 #define COM7_FMT_QCIF 0x08 /* QCIF format */
96 #define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */
97 #define COM7_YUV 0x00 /* YUV */
98 #define COM7_BAYER 0x01 /* Bayer format */
99 #define COM7_PBAYER 0x05 /* "Processed bayer" */
100 #define REG_COM8 0x13 /* Control 8 */
101 #define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
102 #define COM8_AECSTEP 0x40 /* Unlimited AEC step size */
103 #define COM8_BFILT 0x20 /* Band filter enable */
104 #define COM8_AGC 0x04 /* Auto gain enable */
105 #define COM8_AWB 0x02 /* White balance enable */
106 #define COM8_AEC 0x01 /* Auto exposure enable */
107 #define REG_COM9 0x14 /* Control 9 - gain ceiling */
108 #define REG_COM10 0x15 /* Control 10 */
109 #define COM10_HSYNC 0x40 /* HSYNC instead of HREF */
110 #define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */
111 #define COM10_HREF_REV 0x08 /* Reverse HREF */
112 #define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */
113 #define COM10_VS_NEG 0x02 /* VSYNC negative */
114 #define COM10_HS_NEG 0x01 /* HSYNC negative */
115 #define REG_HSTART 0x17 /* Horiz start high bits */
116 #define REG_HSTOP 0x18 /* Horiz stop high bits */
117 #define REG_VSTART 0x19 /* Vert start high bits */
118 #define REG_VSTOP 0x1a /* Vert stop high bits */
119 #define REG_PSHFT 0x1b /* Pixel delay after HREF */
120 #define REG_MIDH 0x1c /* Manuf. ID high */
121 #define REG_MIDL 0x1d /* Manuf. ID low */
122 #define REG_MVFP 0x1e /* Mirror / vflip */
123 #define MVFP_MIRROR 0x20 /* Mirror image */
124 #define MVFP_FLIP 0x10 /* Vertical flip */
126 #define REG_AEW 0x24 /* AGC upper limit */
127 #define REG_AEB 0x25 /* AGC lower limit */
128 #define REG_VPT 0x26 /* AGC/AEC fast mode op region */
129 #define REG_HSYST 0x30 /* HSYNC rising edge delay */
130 #define REG_HSYEN 0x31 /* HSYNC falling edge delay */
131 #define REG_HREF 0x32 /* HREF pieces */
132 #define REG_TSLB 0x3a /* lots of stuff */
133 #define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */
134 #define REG_COM11 0x3b /* Control 11 */
135 #define COM11_NIGHT 0x80 /* NIght mode enable */
136 #define COM11_NMFR 0x60 /* Two bit NM frame rate (1/8 of normal mode frame rate) */
137 #define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
138 #define COM11_50HZ 0x08 /* Manual 50Hz select */
139 #define COM11_EXP 0x02
140 #define REG_COM12 0x3c /* Control 12 */
141 #define COM12_HREF 0x80 /* HREF always */
142 #define REG_COM13 0x3d /* Control 13 */
143 #define COM13_GAMMA 0x80 /* Gamma enable */
144 #define COM13_UVSAT 0x40 /* UV saturation auto adjustment */
145 #define COM13_UVSWAP 0x01 /* V before U - w/TSLB */
146 #define REG_COM14 0x3e /* Control 14 */
147 #define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */
148 #define REG_EDGE 0x3f /* Edge enhancement factor */
149 #define REG_COM15 0x40 /* Control 15 */
150 #define COM15_R10F0 0x00 /* Data range 10 to F0 */
151 #define COM15_R01FE 0x80 /* 01 to FE */
152 #define COM15_R00FF 0xc0 /* 00 to FF */
153 #define COM15_RGB565 0x10 /* RGB565 output */
154 #define COM15_RGB555 0x30 /* RGB555 output */
155 #define REG_COM16 0x41 /* Control 16 */
156 #define COM16_AWBGAIN 0x08 /* AWB gain enable */
157 #define REG_COM17 0x42 /* Control 17 */
158 #define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */
159 #define COM17_CBAR 0x08 /* DSP Color bar */
162 * This matrix defines how the colors are generated, must be
163 * tweaked to adjust hue and saturation.
165 * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
167 * They are nine-bit signed quantities, with the sign bit
168 * stored in 0x58. Sign for v-red is bit 0, and up from there.
170 #define REG_CMATRIX_BASE 0x4f
171 #define CMATRIX_LEN 6
172 #define REG_CMATRIX_SIGN 0x58
175 #define REG_BRIGHT 0x55 /* Brightness */
176 #define REG_CONTRAS 0x56 /* Contrast control */
178 #define REG_LCC1 0x62 /* Lens Corrections C1 */
179 #define REG_LCC2 0x63 /* Lens Corrections C2 */
181 #define REG_GFIX 0x69 /* Fix gain control */
183 #define REG_REG76 0x76 /* OV's name */
184 #define R76_BLKPCOR 0x80 /* Black pixel correction enable */
185 #define R76_WHTPCOR 0x40 /* White pixel correction enable */
187 #define REG_RGB444 0x8c /* RGB 444 control */
188 #define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */
189 #define R444_RGBX 0x01 /* Empty nibble at end */
191 #define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
192 #define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
194 #define REG_BD50MAX 0xa5 /* 50hz banding step limit */
195 #define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
196 #define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
197 #define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
198 #define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
199 #define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
200 #define REG_BD60MAX 0xab /* 60hz banding step limit */
204 #define B1_CAMSELECT 0x01 /* one PIN/bit : 0 0 0 0 0 0 B1bis B1 */
205 #define B1_CAM_SAME_MODES 0x02 /* if false, only same resolution and pixel format. If true, same gain, wb, contrast, etc. settings */
208 static struct ov7670_param
{
209 struct v4l2_format format
; // size and pixel format (e.g. VGA, QVGA, YUV:4:2:2)
210 struct v4l2_streamparm framerate
;
211 int redbalance
; // REG_RED
212 int bluebalance
;// REG_BLUE
213 unsigned char agc_aec_awb
; // REG_COM8
215 int brightness
;// REG_BRIGHT
216 int contrast
; // REG_CONTRAS
217 int gain
; // REG_GAIN
218 int expo
; // REG_AECHH,REG_AECH,REG_COM1
219 //bool agc; // REG_COM8
220 //bool awb; // REG_COM8
221 //bool aec; // REG_COM8
226 * Information we maintain about a known sensor.
228 struct ov7670_format_struct
; /* coming later */
230 struct ov7670_format_struct
*fmt
; /* Current format */
231 unsigned char sat
; /* Saturation value */
232 int hue
; /* Hue value */
233 struct i2c_client
*i2c_ctrl
;
236 struct i2c_client
*ov7670_i2c_client
;
239 * The default register settings, as obtained from OmniVision. There
240 * is really no making sense of most of these - lots of "reserved" values
243 * These settings give VGA YUYV.
247 unsigned char reg_num
;
251 static struct regval_list ov7670_default_regs
[] = {
252 { REG_COM7
, COM7_RESET
},
253 #if 1 /* Linux or OmniVision values? */
255 * Clock scale: 3 = 15fps
259 { REG_CLKRC
, 0x1 }, /* OV: clock scale (30 fps) */
260 { REG_TSLB
, 0x04 }, /* OV */
261 { REG_COM7
, 0 }, /* VGA */
263 * Set the hardware window. These values from OV don't entirely
264 * make sense - hstop is less than hstart. But they work...
266 { REG_HSTART
, 0x13 }, { REG_HSTOP
, 0x01 },
267 { REG_HREF
, 0xb6 }, { REG_VSTART
, 0x02 },
268 { REG_VSTOP
, 0x7a }, { REG_VREF
, 0x0a },
270 { REG_COM3
, 0 }, { REG_COM14
, 0 },
271 /* Mystery scaling numbers */
272 { 0x70, 0x3a }, { 0x71, 0x35 },
273 { 0x72, 0x11 }, { 0x73, 0xf0 },
274 { 0xa2, 0x02 }, { REG_COM10
, 0x0 },
276 /* Gamma curve values */
277 { 0x7a, 0x20 }, { 0x7b, 0x10 },
278 { 0x7c, 0x1e }, { 0x7d, 0x35 },
279 { 0x7e, 0x5a }, { 0x7f, 0x69 },
280 { 0x80, 0x76 }, { 0x81, 0x80 },
281 { 0x82, 0x88 }, { 0x83, 0x8f },
282 { 0x84, 0x96 }, { 0x85, 0xa3 },
283 { 0x86, 0xaf }, { 0x87, 0xc4 },
284 { 0x88, 0xd7 }, { 0x89, 0xe8 },
286 /* AGC and AEC parameters. Note we start by disabling those features,
287 then turn them only after tweaking the values. */
288 { REG_COM8
, COM8_FASTAEC
| COM8_AECSTEP
| COM8_BFILT
},
289 { REG_GAIN
, 0 }, { REG_AECH
, 0 },
290 { REG_COM4
, 0x40 }, /* magic reserved bit */
291 { REG_COM9
, 0x38 }, /* 4x gain + magic rsvd bit */
292 { REG_BD50MAX
, 0x05 }, { REG_BD60MAX
, 0x07 },
293 { REG_AEW
, 0x95 }, { REG_AEB
, 0x33 },
294 { REG_VPT
, 0xe3 }, { REG_HAECC1
, 0x78 },
295 { REG_HAECC2
, 0x68 }, { 0xa1, 0x03 }, /* magic */
296 { REG_HAECC3
, 0xd8 }, { REG_HAECC4
, 0xd8 },
297 { REG_HAECC5
, 0xf0 }, { REG_HAECC6
, 0x90 },
298 { REG_HAECC7
, 0x94 },
299 { REG_COM8
, COM8_FASTAEC
|COM8_AECSTEP
|COM8_BFILT
|COM8_AGC
|COM8_AEC
},
301 /* Almost all of these are magic "reserved" values. */
302 { REG_COM5
, 0x61 }, { REG_COM6
, 0x4b },
303 { 0x16, 0x02 }, { REG_MVFP
, 0x07 },
304 { 0x21, 0x02 }, { 0x22, 0x91 },
305 { 0x29, 0x07 }, { 0x33, 0x0b },
306 { 0x35, 0x0b }, { 0x37, 0x1d },
307 { 0x38, 0x71 }, { 0x39, 0x2a },
308 { REG_COM12
, 0x78 }, { 0x4d, 0x40 },
309 { 0x4e, 0x20 }, { REG_GFIX
, 0 },
310 { 0x6b, 0x4a }, { 0x74, 0x10 },
311 { 0x8d, 0x4f }, { 0x8e, 0 },
312 { 0x8f, 0 }, { 0x90, 0 },
313 { 0x91, 0 }, { 0x96, 0 },
314 { 0x9a, 0 }, { 0xb0, 0x84 },
315 { 0xb1, 0x0c }, { 0xb2, 0x0e },
316 { 0xb3, 0x82 }, { 0xb8, 0x0a },
318 /* More reserved magic, some of which tweaks white balance */
319 { 0x43, 0x0a }, { 0x44, 0xf0 },
320 { 0x45, 0x34 }, { 0x46, 0x58 },
321 { 0x47, 0x28 }, { 0x48, 0x3a },
322 { 0x59, 0x88 }, { 0x5a, 0x88 },
323 { 0x5b, 0x44 }, { 0x5c, 0x67 },
324 { 0x5d, 0x49 }, { 0x5e, 0x0e },
325 { 0x6c, 0x0a }, { 0x6d, 0x55 },
326 { 0x6e, 0x11 }, { 0x6f, 0x9f }, /* "9e for advance AWB" */
327 { 0x6a, 0x40 }, { REG_BLUE
, 0x40 },
329 { REG_COM8
, COM8_FASTAEC
|COM8_AECSTEP
|COM8_BFILT
|COM8_AGC
|COM8_AEC
|COM8_AWB
},
331 /* Matrix coefficients */
332 { 0x4f, 0x80 }, { 0x50, 0x80 },
333 { 0x51, 0 }, { 0x52, 0x22 },
334 { 0x53, 0x5e }, { 0x54, 0x80 },
337 { REG_COM16
, COM16_AWBGAIN
}, { REG_EDGE
, 0 },
338 { 0x75, 0x02 }, { 0x76, 0xe0 },
339 { 0x4c, 0 }, { 0x77, 0x01 },
340 { REG_COM13
, 0xc0 }, { 0x4b, 0x09 },
341 { 0xc9, 0x60 }, { REG_COM16
, 0x38 },
344 { 0x34, 0x11 }, { REG_COM11
, COM11_EXP
|COM11_HZAUTO
},
345 { 0xa4, 0x88 }, { 0x96, 0 },
346 { 0x97, 0x30 }, { 0x98, 0x20 },
347 { 0x99, 0x30 }, { 0x9a, 0x84 },
348 { 0x9b, 0x29 }, { 0x9c, 0x03 },
349 { 0x9d, 0x4c }, { 0x9e, 0x3f },
352 //{ REG_COM17, COM17_CBAR },
354 /* Extra-weird stuff. Some sort of multiplexor register */
355 { 0x79, 0x01 }, { 0xc8, 0xf0 },
356 { 0x79, 0x0f }, { 0xc8, 0x00 },
357 { 0x79, 0x10 }, { 0xc8, 0x7e },
358 { 0x79, 0x0a }, { 0xc8, 0x80 },
359 { 0x79, 0x0b }, { 0xc8, 0x01 },
360 { 0x79, 0x0c }, { 0xc8, 0x0f },
361 { 0x79, 0x0d }, { 0xc8, 0x20 },
362 { 0x79, 0x09 }, { 0xc8, 0x80 },
363 { 0x79, 0x02 }, { 0xc8, 0xc0 },
364 { 0x79, 0x03 }, { 0xc8, 0x40 },
365 { 0x79, 0x05 }, { 0xc8, 0x30 },
368 { 0x12, 0x80 }, { 0x11, 0x03 },
369 { 0x3a, 0x04 }, { 0x12, 0x00 },
370 { 0x17, 0x13 }, { 0x18, 0x01 },
371 { 0x32, 0xb6 }, { 0x19, 0x02 },
372 { 0x1a, 0x7a }, { 0x03, 0x0a },
373 { 0x0c, 0x00 }, { 0x3e, 0x00 },
374 { 0x70, 0x3a }, { 0x71, 0x35 },
375 { 0x72, 0x11 }, { 0x73, 0xf0 },
376 { 0xa2, 0x02 }, { 0x7a, 0x20 },
377 { 0x7b, 0x1c }, { 0x7c, 0x28 },
378 { 0x7d, 0x3c }, { 0x7e, 0x5a },
379 { 0x7f, 0x68 }, { 0x80, 0x76 },
380 { 0x81, 0x80 }, { 0x82, 0x88 },
381 { 0x83, 0x8f }, { 0x84, 0x96 },
382 { 0x85, 0xa3 }, { 0x86, 0xaf },
383 { 0x87, 0xc4 }, { 0x88, 0xd7 },
384 { 0x89, 0xe8 }, { 0x13, 0xe0 },
385 { 0x00, 0x00 }, { 0x10, 0x00 },
386 { 0x0d, 0x40 }, { 0x14, 0x38 },
387 { 0xa5, 0x05 }, { 0xab, 0x07 },
388 { 0x24, 0x95 }, { 0x25, 0x33 },
389 { 0x26, 0xe3 }, { 0x9f, 0x78 },
390 { 0xa0, 0x68 }, { 0xa1, 0x0b },
391 { 0xa6, 0xd8 }, { 0xa7, 0xd8 },
392 { 0xa8, 0xf0 }, { 0xa9, 0x90 },
393 { 0xaa, 0x94 }, { 0x13, 0xe5 },
394 { 0x0e, 0x61 }, { 0x0f, 0x4b },
395 { 0x16, 0x02 }, { 0x21, 0x02 },
396 { 0x22, 0x91 }, { 0x29, 0x07 },
397 { 0x33, 0x03 }, { 0x35, 0x0b },
398 { 0x37, 0x1c }, { 0x38, 0x71 },
399 { 0x3c, 0x78 }, { 0x4d, 0x40 },
400 { 0x4e, 0x20 }, { 0x69, 0x55 },
401 { 0x6b, 0x4a }, { 0x74, 0x19 },
402 { 0x8d, 0x4f }, { 0x8e, 0x00 },
403 { 0x8f, 0x00 }, { 0x90, 0x00 },
404 { 0x91, 0x00 }, { 0x96, 0x00 },
405 { 0x9a, 0x80 }, { 0xb0, 0x8c },
406 { 0xb1, 0x0c }, { 0xb2, 0x0e },
407 { 0xb3, 0x82 }, { 0xb8, 0x0a },
408 { 0x43, 0x0a }, { 0x44, 0xf0 },
409 { 0x45, 0x34 }, { 0x46, 0x62 },
410 { 0x47, 0x24 }, { 0x48, 0x39 },
411 { 0x59, 0x88 }, { 0x5a, 0x92 },
412 { 0x5b, 0x44 }, { 0x5c, 0x80 },
413 { 0x5d, 0x48 }, { 0x5e, 0x0e },
414 { 0x6c, 0x0e }, { 0x6d, 0x55 },
415 { 0x6e, 0x11 }, { 0x6f, 0x9e },
416 { 0x6a, 0x40 }, { 0x01, 0x40 },
417 { 0x02, 0x40 }, { 0x13, 0xe7 },
418 { 0x4f, 0x80 }, { 0x50, 0x80 },
419 { 0x51, 0x00 }, { 0x52, 0x22 },
420 { 0x53, 0x5e }, { 0x54, 0x80 },
421 { 0x58, 0x9e }, { 0x41, 0x08 },
422 { 0x3f, 0x00 }, { 0x75, 0x05 },
423 { 0x76, 0xe1 }, { 0x4c, 0x00 },
424 { 0x77, 0x01 }, { 0x3d, 0xc2 },
425 { 0x4b, 0x09 }, { 0xc9, 0x60 },
426 { 0x41, 0x38 }, { 0x56, 0x40 },
427 { 0x34, 0x11 }, { 0x3b, 0x02 },
428 { 0xa4, 0x88 }, { 0x96, 0x00 },
429 { 0x97, 0x30 }, { 0x98, 0x20 },
430 { 0x99, 0x20 }, { 0x9a, 0x84 },
431 { 0x9b, 0x29 }, { 0x9c, 0x03 },
432 { 0x9d, 0x4c }, { 0x9e, 0x3f },
433 { 0x78, 0x04 }, { 0x79, 0x01 },
434 { 0xc8, 0xf0 }, { 0x79, 0x0f },
435 { 0xc8, 0x20 }, { 0x79, 0x10 },
436 { 0xc8, 0x7e }, { 0x79, 0x0b },
437 { 0xc8, 0x01 }, { 0x79, 0x0c },
438 { 0xc8, 0x07 }, { 0x79, 0x0d },
439 { 0xc8, 0x20 }, { 0x79, 0x09 },
440 { 0xc8, 0x80 }, { 0x79, 0x02 },
441 { 0xc8, 0xc0 }, { 0x79, 0x03 },
442 { 0xc8, 0x40 }, { 0x79, 0x05 },
443 { 0xc8, 0x30 }, { 0x79, 0x26 },
444 { 0x64, 0x10 }, { 0x65, 0x00 },
445 { 0x94, 0x0e }, { 0x95, 0x19 },
446 { 0x66, 0x05 }, { 0x04, 0x40 },
448 { 0xff, 0xff }, /* END MARKER */
453 * Here we'll try to encapsulate the changes for just the output
456 * RGB656 and YUV422 come from OV; RGB444 is homebrewed.
458 * IMPORTANT RULE: the first entry must be for COM7, see ov7670_s_fmt for why.
462 static struct regval_list ov7670_fmt_yuv422
[] = {
463 { REG_COM7
, 0x0 }, /* Selects YUV mode */
464 { REG_RGB444
, 0 }, /* No RGB444 please */
465 { REG_COM1
, COM1_CCIR656
},
466 { REG_COM15
, COM15_R01FE
},
467 { REG_COM9
, 0x38 }, /* 4x gain ceiling; 0x8 is reserved bit */
468 { 0x4f, 0x80 }, /* "matrix coefficient 1" */
469 { 0x50, 0x80 }, /* "matrix coefficient 2" */
470 { 0x51, 0 }, /* vb */
471 { 0x52, 0x22 }, /* "matrix coefficient 4" */
472 { 0x53, 0x5e }, /* "matrix coefficient 5" */
473 { 0x54, 0x80 }, /* "matrix coefficient 6" */
475 { REG_COM13
, COM13_GAMMA
|COM13_UVSAT
},
479 static struct regval_list ov7670_fmt_rgb565
[] = {
480 { REG_COM7
, COM7_RGB
}, /* Selects RGB mode */
481 { REG_RGB444
, 0 }, /* No RGB444 please */
482 { REG_COM1
, COM1_CCIR656
},
483 { REG_COM15
, COM15_R01FE
|COM15_RGB565
},
484 { REG_COM9
, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
485 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
486 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
487 { 0x51, 0 }, /* vb */
488 { 0x52, 0x3d }, /* "matrix coefficient 4" */
489 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
490 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
491 { REG_COM13
, COM13_GAMMA
|COM13_UVSAT
},
495 static struct regval_list ov7670_fmt_rgb444
[] = {
496 { REG_COM7
, COM7_RGB
}, /* Selects RGB mode */
497 { REG_RGB444
, R444_ENABLE
}, /* Enable xxxxrrrr ggggbbbb */
498 { REG_COM1
, COM1_CCIR656
}, /* Magic reserved bit */
499 { REG_COM15
, COM15_R01FE
|COM15_RGB565
}, /* Data range needed? */
500 { REG_COM9
, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */
501 { 0x4f, 0xb3 }, /* "matrix coefficient 1" */
502 { 0x50, 0xb3 }, /* "matrix coefficient 2" */
503 { 0x51, 0 }, /* vb */
504 { 0x52, 0x3d }, /* "matrix coefficient 4" */
505 { 0x53, 0xa7 }, /* "matrix coefficient 5" */
506 { 0x54, 0xe4 }, /* "matrix coefficient 6" */
507 { REG_COM13
, COM13_GAMMA
|COM13_UVSAT
|0x2 }, /* Magic rsvd bit */
511 static struct regval_list ov7670_fmt_raw
[] = {
512 { REG_COM7
, COM7_BAYER
},
513 { REG_COM1
, COM1_CCIR656
}, /* Magic reserved bit */
514 { REG_COM13
, 0x08 }, /* No gamma, magic rsvd bit */
515 { REG_COM16
, 0x3d }, /* Edge enhancement, denoise */
516 { REG_REG76
, 0xe0 }, /* Pix correction, magic rsvd */
523 * Low-level register I/O.
526 static int ov7670_read(struct i2c_client
*c
, unsigned char reg
,
527 unsigned char *value
)
529 //Bri. : added retries
533 ret
= i2c_smbus_read_byte_data(c
, reg
);
538 } while ((ret
< 0) && (nbretries
<= OV7670_I2C_MAXRETRIES
) );
543 (*value
) = (unsigned char) (ret
& 0xff);
549 static int ov7670_write(struct i2c_client
*c
, unsigned char reg
,
552 //Bri. : added retries
556 ret
= i2c_smbus_write_byte_data(c
, reg
, value
);
561 } while ((ret
< 0) && (nbretries
<= OV7670_I2C_MAXRETRIES
) );
567 * Write a list of register settings; ff/ff stops the process.
569 static int ov7670_write_array(struct i2c_client
*c
, struct regval_list
*vals
)
572 while (vals
->reg_num
!= 0xff || vals
->value
!= 0xff) {
573 ret
= ov7670_write(c
, vals
->reg_num
, vals
->value
);
583 * Dump the registers.
586 static int ov7670_read_dump(struct i2c_client
*c
)
588 unsigned char reg
, val
;
590 printk(KERN_INFO
"OV7670 Register dump\n");
591 for (reg
= 0; reg
< 0xff; reg
++)
592 if (ov7670_read(c
, reg
, &val
) >= 0)
593 printk(KERN_INFO
"\t%x\t%x\n", (unsigned)reg
, (unsigned)val
);
595 printk(KERN_INFO
"\t%x\tfailed\n", (unsigned)reg
);
603 * Stuff that knows about the sensor.
605 static void ov7670_reset(struct i2c_client
*client
)
607 ov7670_write(client
, REG_COM7
, COM7_RESET
);
612 static int ov7670_init(struct i2c_client
*client
)
614 return ov7670_write_array(client
, ov7670_default_regs
);
618 static int ov7670_detect(struct i2c_client
*client
)
623 printk(KERN_INFO
"Trying to detect OmniVision 7670/7672 I2C adapters\n");
624 ret
= ov7670_init(client
);
625 printk(KERN_DEBUG
"ov7670_init returned %d\n", ret
);
628 printk(KERN_DEBUG
"Phase 1\n");
629 ret
= ov7670_read(client
, REG_MIDH
, &v
);
632 printk(KERN_DEBUG
"Phase 2\n");
633 if (v
!= 0x7f) /* OV manuf. id. */
635 printk(KERN_DEBUG
"Phase 3\n");
636 ret
= ov7670_read(client
, REG_MIDL
, &v
);
639 printk(KERN_DEBUG
"Phase 4\n");
642 printk(KERN_DEBUG
"Phase 5\n");
644 * OK, we know we have an OmniVision chip...but which one?
646 ret
= ov7670_read(client
, REG_PID
, &v
);
649 printk(KERN_DEBUG
"Phase 6\n");
650 if (v
!= 0x76) /* PID + VER = 0x76 / 0x73 */
652 printk(KERN_DEBUG
"Phase 7\n");
653 ret
= ov7670_read(client
, REG_VER
, &v
);
656 printk(KERN_DEBUG
"Phase 8\n");
657 if (v
!= 0x73) /* PID + VER = 0x76 / 0x73 */
659 printk(KERN_DEBUG
"Phase 9\n");
660 printk(KERN_INFO
"OmniVision 7670/7671 I2C Found\n");
667 * Store information about the video data format. The color matrix
668 * is deeply tied into the format, so keep the relevant values here.
669 * The magic matrix nubmers come from OmniVision.
671 static struct ov7670_format_struct
{
674 struct regval_list
*regs
;
675 int cmatrix
[CMATRIX_LEN
];
676 int bpp
; /* Bytes per pixel */
677 } ov7670_formats
[] = {
679 .desc
= "YUYV 4:2:2",
680 .pixelformat
= V4L2_PIX_FMT_YUYV
,
681 .regs
= ov7670_fmt_yuv422
,
682 .cmatrix
= { 128, -128, 0, -34, -94, 128 },
683 // Why is is null ?? This leads to B&W images when hue/saturation is
685 //.cmatrix = { 0, 0, 0, 0, 0, 0 },
690 .pixelformat
= V4L2_PIX_FMT_RGB444
,
691 .regs
= ov7670_fmt_rgb444
,
692 .cmatrix
= { 179, -179, 0, -61, -176, 228 },
697 .pixelformat
= V4L2_PIX_FMT_RGB565
,
698 .regs
= ov7670_fmt_rgb565
,
699 .cmatrix
= { 179, -179, 0, -61, -176, 228 },
703 .desc
= "Raw RGB Bayer",
704 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
705 .regs
= ov7670_fmt_raw
,
706 .cmatrix
= { 0, 0, 0, 0, 0, 0 },
710 #define N_OV7670_FMTS ARRAY_SIZE(ov7670_formats)
714 * Then there is the issue of window sizes. Try to capture the info here.
718 * QCIF mode is done (by OV) in a very strange way - it actually looks like
719 * VGA with weird scaling options - they do *not* use the canned QCIF mode
720 * which is allegedly provided by the sensor. So here's the weird register
723 static struct regval_list ov7670_qcif_regs
[] = {
724 { REG_COM3
, COM3_SCALEEN
|COM3_DCWEN
},
725 { REG_COM3
, COM3_DCWEN
},
726 { REG_COM14
, COM14_DCWEN
| 0x01},
742 static struct ov7670_win_size
{
745 unsigned char com7_bit
;
746 int hstart
; /* Start/stop values for the camera. Note */
747 int hstop
; /* that they do not always make complete */
748 int vstart
; /* sense to humans, but evidently the sensor */
749 int vstop
; /* will do the right thing... */
750 struct regval_list
*regs
; /* Regs to tweak */
752 } ov7670_win_sizes
[] = {
756 .height
= VGA_HEIGHT
,
757 .com7_bit
= COM7_FMT_VGA
,
758 .hstart
= 158, /* These values from */
759 .hstop
= 14, /* Omnivision */
767 .height
= CIF_HEIGHT
,
768 .com7_bit
= COM7_FMT_CIF
,
769 .hstart
= 170, /* Empirically determined */
778 .height
= QVGA_HEIGHT
,
779 .com7_bit
= COM7_FMT_QVGA
,
780 .hstart
= 164, /* Empirically determined */
789 .height
= QCIF_HEIGHT
,
790 .com7_bit
= COM7_FMT_VGA
, /* see comment above */
791 .hstart
= 456, /* Empirically determined */
795 .regs
= ov7670_qcif_regs
,
799 #define N_WIN_SIZES (sizeof(ov7670_win_sizes)/sizeof(ov7670_win_sizes[0]))
803 * Store a set of start/stop values into the camera.
805 static int ov7670_set_hw(struct i2c_client
*client
, int hstart
, int hstop
,
806 int vstart
, int vstop
)
811 * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of
812 * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is
813 * a mystery "edge offset" value in the top two bits of href.
815 ret
= ov7670_write(client
, REG_HSTART
, (hstart
>> 3) & 0xff);
816 ret
+= ov7670_write(client
, REG_HSTOP
, (hstop
>> 3) & 0xff);
817 ret
+= ov7670_read(client
, REG_HREF
, &v
);
818 v
= (v
& 0xc0) | ((hstop
& 0x7) << 3) | (hstart
& 0x7);
820 ret
+= ov7670_write(client
, REG_HREF
, v
);
822 * Vertical: similar arrangement, but only 10 bits.
824 ret
+= ov7670_write(client
, REG_VSTART
, (vstart
>> 2) & 0xff);
825 ret
+= ov7670_write(client
, REG_VSTOP
, (vstop
>> 2) & 0xff);
826 ret
+= ov7670_read(client
, REG_VREF
, &v
);
827 v
= (v
& 0xf0) | ((vstop
& 0x3) << 2) | (vstart
& 0x3);
829 ret
+= ov7670_write(client
, REG_VREF
, v
);
837 static int ov7670_enum_fmt(struct i2c_client
*c
, struct v4l2_fmtdesc
*fmt
)
839 struct ov7670_format_struct
*ofmt
;
841 if (fmt
->index
>= N_OV7670_FMTS
)
844 ofmt
= ov7670_formats
+ fmt
->index
;
846 strcpy(fmt
->description
, ofmt
->desc
);
847 fmt
->pixelformat
= ofmt
->pixelformat
;
855 static int ov7670_try_fmt(struct i2c_client
*c
, struct v4l2_format
*fmt
,
856 struct ov7670_format_struct
**ret_fmt
,
857 struct ov7670_win_size
**ret_wsize
)
860 struct ov7670_win_size
*wsize
;
861 struct v4l2_pix_format
*pix
= &fmt
->fmt
.pix
;
864 printk(KERN_INFO
"Asking for format %x\n", (int)pix
->pixelformat
);
866 for (index
= 0; index
< N_OV7670_FMTS
; index
++) {
868 printk(KERN_INFO
"Have format %x\n", (int)ov7670_formats
[index
].pixelformat
);
870 if (ov7670_formats
[index
].pixelformat
== pix
->pixelformat
)
873 if (index
>= N_OV7670_FMTS
) {
874 printk(KERN_INFO
"No such format %d\n", (int)index
);
878 *ret_fmt
= ov7670_formats
+ index
;
880 * Fields: the OV devices claim to be progressive.
882 if (pix
->field
== V4L2_FIELD_ANY
)
883 pix
->field
= V4L2_FIELD_NONE
;
884 else if (pix
->field
!= V4L2_FIELD_NONE
) {
885 printk(KERN_INFO
"V4L2_FIELD_ANY required\n");
889 * Round requested image size down to the nearest
890 * we support, but not below the smallest.
893 printk(KERN_INFO
"Asking for size %dx%d\n", (int)pix
->width
, (int)pix
->height
);
895 for (wsize
= ov7670_win_sizes
; wsize
< ov7670_win_sizes
+ N_WIN_SIZES
;
897 if (pix
->width
>= wsize
->width
&& pix
->height
>= wsize
->height
)
899 if (wsize
>= ov7670_win_sizes
+ N_WIN_SIZES
)
900 wsize
--; /* Take the smallest one */
901 if (ret_wsize
!= NULL
)
904 * Note the size we'll actually handle.
906 pix
->width
= wsize
->width
;
907 pix
->height
= wsize
->height
;
908 pix
->bytesperline
= pix
->width
*ov7670_formats
[index
].bpp
;
909 pix
->sizeimage
= pix
->height
*pix
->bytesperline
;
911 printk(KERN_INFO
"Have size %dx%d\n", (int)pix
->width
, (int)pix
->height
);
919 static int ov7670_s_fmt(struct i2c_client
*c
, struct v4l2_format
*fmt
)
922 struct ov7670_format_struct
*ovfmt
;
923 struct ov7670_win_size
*wsize
;
924 struct ov7670_info
*info
= i2c_get_clientdata(c
);
925 unsigned char com7
, clkrc
;
927 ret
= ov7670_try_fmt(c
, fmt
, &ovfmt
, &wsize
);
931 * HACK: if we're running rgb565 we need to grab then rewrite
932 * CLKRC. If we're *not*, however, then rewriting clkrc hoses
935 if (fmt
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_RGB565
) {
936 ret
= ov7670_read(c
, REG_CLKRC
, &clkrc
);
941 * COM7 is a pain in the ass, it doesn't like to be read then
942 * quickly written afterward. But we have everything we need
943 * to set it absolutely here, as long as the format-specific
944 * register sets list it first.
946 com7
= ovfmt
->regs
[0].value
;
947 com7
|= wsize
->com7_bit
;
948 ov7670_write(c
, REG_COM7
, com7
);
950 * Now write the rest of the array. Also store start/stops
952 ov7670_write_array(c
, ovfmt
->regs
+ 1);
953 ov7670_set_hw(c
, wsize
->hstart
, wsize
->hstop
, wsize
->vstart
,
957 ret
= ov7670_write_array(c
, wsize
->regs
);
960 if (fmt
->fmt
.pix
.pixelformat
== V4L2_PIX_FMT_RGB565
&& ret
== 0)
961 ret
= ov7670_write(c
, REG_CLKRC
, clkrc
);
963 /* PATCH to avoid page default on fmt->fmt.pix.pixelformat later */
964 fmt
->fmt
.pix
.pixelformat
= 0x56595559;
970 * Implement G/S_PARM. There is a "high quality" mode we could try
971 * to do someday; for now, we just do the frame rate tweak.
973 static int ov7670_g_parm(struct i2c_client
*c
, struct v4l2_streamparm
*parms
)
975 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
979 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
981 ret
= ov7670_read(c
, REG_CLKRC
, &clkrc
);
984 memset(cp
, 0, sizeof(struct v4l2_captureparm
));
985 cp
->capability
= V4L2_CAP_TIMEPERFRAME
;
986 cp
->timeperframe
.numerator
= 1;
987 cp
->timeperframe
.denominator
= OV7670_FRAME_RATE
;
988 if ((clkrc
& CLK_EXT
) == 0 && (clkrc
& CLK_SCALE
) > 1)
989 cp
->timeperframe
.denominator
/= (clkrc
& CLK_SCALE
);
993 static int ov7670_s_parm(struct i2c_client
*c
, struct v4l2_streamparm
*parms
)
995 struct v4l2_captureparm
*cp
= &parms
->parm
.capture
;
996 struct v4l2_fract
*tpf
= &cp
->timeperframe
;
1000 if (parms
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1002 if (cp
->extendedmode
!= 0)
1005 * CLKRC has a reserved bit, so let's preserve it.
1007 ret
= ov7670_read(c
, REG_CLKRC
, &clkrc
);
1010 if (tpf
->numerator
== 0 || tpf
->denominator
== 0)
1011 div
= 1; /* Reset to full rate */
1013 div
= (tpf
->numerator
*OV7670_FRAME_RATE
)/tpf
->denominator
;
1016 else if (div
> CLK_SCALE
)
1018 clkrc
= (clkrc
& 0x80) | div
;
1020 tpf
->denominator
= OV7670_FRAME_RATE
/div
;
1021 return ov7670_write(c
, REG_CLKRC
, clkrc
);
1027 * Code for dealing with controls.
1034 static int ov7670_store_cmatrix(struct i2c_client
*client
,
1035 int matrix
[CMATRIX_LEN
])
1038 unsigned char signbits
;
1041 * Weird crap seems to exist in the upper part of
1042 * the sign bits register, so let's preserve it.
1044 ret
= ov7670_read(client
, REG_CMATRIX_SIGN
, &signbits
);
1047 for (i
= 0; i
< CMATRIX_LEN
; i
++) {
1050 if (matrix
[i
] < 0) {
1051 signbits
|= (1 << i
);
1052 if (matrix
[i
] < -255)
1055 raw
= (-1 * matrix
[i
]) & 0xff;
1058 if (matrix
[i
] > 255)
1061 raw
= matrix
[i
] & 0xff;
1065 ret
+= ov7670_write(client
, REG_CMATRIX_BASE
+ i
, raw
);
1069 ret
+= ov7670_write(client
, REG_CMATRIX_SIGN
, signbits
);
1075 * Hue also requires messing with the color matrix. It also requires
1076 * trig functions, which tend not to be well supported in the kernel.
1077 * So here is a simple table of sine values, 0-90 degrees, in steps
1078 * of five degrees. Values are multiplied by 1000.
1080 * The following naive approximate trig functions require an argument
1081 * carefully limited to -180 <= theta <= 180.
1084 static const int ov7670_sin_table
[] = {
1085 0, 87, 173, 258, 342, 422,
1086 499, 573, 642, 707, 766, 819,
1087 866, 906, 939, 965, 984, 996,
1091 static int ov7670_sine(int theta
)
1101 sine
= ov7670_sin_table
[theta
/SIN_STEP
];
1104 sine
= 1000 - ov7670_sin_table
[theta
/SIN_STEP
];
1109 static int ov7670_cosine(int theta
)
1114 else if (theta
< -180)
1116 return ov7670_sine(theta
);
1122 static void ov7670_calc_cmatrix(struct ov7670_info
*info
,
1123 int matrix
[CMATRIX_LEN
])
1127 * Apply the current saturation setting first.
1129 for (i
= 0; i
< CMATRIX_LEN
; i
++)
1130 matrix
[i
] = (info
->fmt
->cmatrix
[i
]*info
->sat
) >> 7;
1132 /* Test debug, Julien
1133 for (i = 0; i < CMATRIX_LEN; i++)
1134 printk(KERN_INFO "Val %d=%d from %d and %d\n", i, matrix[i], info->fmt->cmatrix[i], info->sat);
1137 * Then, if need be, rotate the hue value.
1139 if (info
->hue
!= 0) {
1140 int sinth
, costh
, tmpmatrix
[CMATRIX_LEN
];
1142 memcpy(tmpmatrix
, matrix
, CMATRIX_LEN
*sizeof(int));
1143 sinth
= ov7670_sine(info
->hue
);
1144 costh
= ov7670_cosine(info
->hue
);
1146 //Bri. rightside matrix changed to tmpmatrix
1147 matrix
[0] = (tmpmatrix
[3]*sinth
+ tmpmatrix
[0]*costh
)/1000;
1148 matrix
[1] = (tmpmatrix
[4]*sinth
+ tmpmatrix
[1]*costh
)/1000;
1149 matrix
[2] = (tmpmatrix
[5]*sinth
+ tmpmatrix
[2]*costh
)/1000;
1150 matrix
[3] = (tmpmatrix
[3]*costh
- tmpmatrix
[0]*sinth
)/1000;
1151 matrix
[4] = (tmpmatrix
[4]*costh
- tmpmatrix
[1]*sinth
)/1000;
1152 matrix
[5] = (tmpmatrix
[5]*costh
- tmpmatrix
[2]*sinth
)/1000;
1158 static int ov7670_t_sat(struct i2c_client
*client
, int value
)
1160 struct ov7670_info
*info
= i2c_get_clientdata(client
);
1161 int matrix
[CMATRIX_LEN
];
1165 if (info
== NULL
) return -EINVAL
;
1167 ov7670_calc_cmatrix(info
, matrix
);
1168 ret
= ov7670_store_cmatrix(client
, matrix
);
1172 static int ov7670_q_sat(struct i2c_client
*client
, __s32
*value
)
1174 struct ov7670_info
*info
= i2c_get_clientdata(client
);
1177 if (info
== NULL
) return -EINVAL
;
1178 if (value
== NULL
) return -EINVAL
;
1183 static int ov7670_t_hue(struct i2c_client
*client
, int value
)
1185 struct ov7670_info
*info
= i2c_get_clientdata(client
);
1186 int matrix
[CMATRIX_LEN
];
1189 if (value
< -180 || value
> 180)
1192 ov7670_calc_cmatrix(info
, matrix
);
1193 ret
= ov7670_store_cmatrix(client
, matrix
);
1198 static int ov7670_q_hue(struct i2c_client
*client
, __s32
*value
)
1200 struct ov7670_info
*info
= i2c_get_clientdata(client
);
1203 if (info
== NULL
) return -EINVAL
;
1204 if (value
== NULL
) return -EINVAL
;
1211 * Some weird registers seem to store values in a sign/magnitude format!
1213 static unsigned char ov7670_sm_to_abs(unsigned char v
)
1215 if ((v
& 0x80) == 0)
1218 return 128 - (v
& 0x7f);
1222 static unsigned char ov7670_abs_to_sm(unsigned char v
)
1227 return (128 - v
) | 0x80;
1230 static int ov7670_t_brightness(struct i2c_client
*client
, int value
)
1232 unsigned char com8
, v
;
1235 ov7670_read(client
, REG_COM8
, &com8
);
1237 ov7670_write(client
, REG_COM8
, com8
);
1238 v
= ov7670_abs_to_sm(value
);
1239 ret
= ov7670_write(client
, REG_BRIGHT
, v
);
1243 static int ov7670_q_brightness(struct i2c_client
*client
, __s32
*value
)
1246 int ret
= ov7670_read(client
, REG_BRIGHT
, &v
);
1248 *value
= ov7670_sm_to_abs(v
);
1252 static int ov7670_t_contrast(struct i2c_client
*client
, int value
)
1254 return ov7670_write(client
, REG_CONTRAS
, (unsigned char) value
);
1257 static int ov7670_q_contrast(struct i2c_client
*client
, __s32
*value
)
1260 int ret
= ov7670_read(client
, REG_CONTRAS
, &v
);
1266 static int ov7670_t_redbalance(struct i2c_client
*client
, int value
)
1268 return ov7670_write(client
, REG_RED
, (unsigned char) value
);
1271 static int ov7670_q_redbalance(struct i2c_client
*client
, __s32
*value
)
1274 int ret
= ov7670_read(client
, REG_RED
, &v
);
1280 static int ov7670_t_bluebalance(struct i2c_client
*client
, int value
)
1282 return ov7670_write(client
, REG_BLUE
, (unsigned char) value
);
1285 static int ov7670_q_bluebalance(struct i2c_client
*client
, __s32
*value
)
1288 int ret
= ov7670_read(client
, REG_BLUE
, &v
);
1294 static int ov7670_t_gain(struct i2c_client
*client
, int value
)
1296 return ov7670_write(client
, REG_GAIN
, (unsigned char) value
);
1299 static int ov7670_q_gain(struct i2c_client
*client
, __s32
*value
)
1302 int ret
= ov7670_read(client
, REG_GAIN
, &v
);
1308 static int ov7670_t_hcenter(struct i2c_client
*client
, int value
)
1310 return ov7670_write(client
, REG_LCC1
, (unsigned char) value
);
1313 static int ov7670_q_hcenter(struct i2c_client
*client
, __s32
*value
)
1316 int ret
= ov7670_read(client
, REG_LCC1
, &v
);
1322 static int ov7670_t_vcenter(struct i2c_client
*client
, int value
)
1324 return ov7670_write(client
, REG_LCC2
, (unsigned char) value
);
1327 static int ov7670_q_vcenter(struct i2c_client
*client
, __s32
*value
)
1330 int ret
= ov7670_read(client
, REG_LCC2
, &v
);
1336 static int ov7670_q_hflip(struct i2c_client
*client
, __s32
*value
)
1341 ret
= ov7670_read(client
, REG_MVFP
, &v
);
1342 *value
= (v
& MVFP_MIRROR
) == MVFP_MIRROR
;
1347 static int ov7670_t_hflip(struct i2c_client
*client
, int value
)
1352 ret
= ov7670_read(client
, REG_MVFP
, &v
);
1357 ret
+= ov7670_write(client
, REG_MVFP
, v
);
1363 static int ov7670_q_vflip(struct i2c_client
*client
, __s32
*value
)
1368 ret
= ov7670_read(client
, REG_MVFP
, &v
);
1369 *value
= (v
& MVFP_FLIP
) == MVFP_FLIP
;
1373 static int ov7670_t_vflip(struct i2c_client
*client
, int value
)
1378 ret
= ov7670_read(client
, REG_MVFP
, &v
);
1383 ret
+= ov7670_write(client
, REG_MVFP
, v
);
1387 static int ov7670_q_agc(struct i2c_client
*client
, __s32
*value
)
1392 ret
= ov7670_read(client
, REG_COM8
, &v
);
1393 *value
= (v
& COM8_AGC
) == COM8_AGC
;
1397 static int ov7670_t_agc(struct i2c_client
*client
, int value
)
1402 ret
= ov7670_read(client
, REG_COM8
, &v
);
1407 ret
+= ov7670_write(client
, REG_COM8
, v
);
1411 static int ov7670_q_awb(struct i2c_client
*client
, __s32
*value
)
1416 ret
= ov7670_read(client
, REG_COM8
, &v
);
1417 *value
= (v
& COM8_AWB
) == COM8_AWB
;
1421 static int ov7670_t_awb(struct i2c_client
*client
, int value
)
1426 ret
= ov7670_read(client
, REG_COM8
, &v
);
1432 ret
+= ov7670_write(client
, REG_COM8
, v
);
1436 static int ov7670_q_aec(struct i2c_client
*client
, __s32
*value
)
1441 ret
= ov7670_read(client
, REG_COM8
, &v
);
1442 *value
= (v
& COM8_AEC
) == COM8_AEC
;
1446 static int ov7670_t_aec(struct i2c_client
*client
, int value
)
1451 ret
= ov7670_read(client
, REG_COM8
, &v
);
1457 ret
+= ov7670_write(client
, REG_COM8
, v
);
1461 static int ov7670_q_expo(struct i2c_client
*client
, __s32
*expo
)
1464 unsigned char com1
, aech
, aechh
;
1466 ret
= ov7670_read(client
, REG_COM1
, &com1
);
1467 ret
+= ov7670_read(client
, REG_AECH
, &aech
);
1468 ret
+= ov7670_read(client
, REG_AECHH
, &aechh
);
1474 *expo
= ((int)(aechh
& 0x3f) << 10) + ((int)aech
<< 2) + (com1
& 0x03);
1479 /* exposition is splited in 3 registers:
1480 * REG_COM1[1:0] = expo[1:0]
1481 * REG_AECH[7:0] = expo[9:2]
1482 * REG_AECHH[5:0] = expo[15:10]
1485 static int ov7670_t_expo(struct i2c_client
*client
, int expo
)
1487 unsigned char com1
, aech
, aechh
;
1490 ret
= ov7670_read(client
, REG_COM1
, &com1
);
1491 ret
+= ov7670_read(client
, REG_AECH
, &aech
);
1492 ret
+= ov7670_read(client
, REG_AECHH
, &aechh
);
1499 com1
= (com1
& 0xfc) + (expo
& 0x0003);
1500 aech
= (expo
& 0x03fc) >> 2;
1501 aechh
= (aechh
& 0xc0) + ((expo
& 0xfc00) >> 10);
1503 ret
= ov7670_write(client
, REG_AECHH
, aechh
);
1504 ret
+= ov7670_write(client
, REG_AECH
, aech
);
1505 ret
+= ov7670_write(client
, REG_COM1
, com1
);
1511 static int ov7670_q_aec_algorithm(struct i2c_client
*client
, __s32
*value
)
1514 unsigned char haecc7
;
1516 ret
= ov7670_read(client
, REG_HAECC7
, &haecc7
);
1522 *value
= ((int)(haecc7
& 0x80) >> 7);
1527 static int ov7670_t_aec_algorithm(struct i2c_client
*client
, int value
)
1529 unsigned char haecc7
;
1532 ret
= ov7670_read(client
, REG_HAECC7
, &haecc7
);
1537 haecc7
= (haecc7
& 0x7f) + ((value
!=0) << 7);
1539 ret
= ov7670_write(client
, REG_HAECC7
, haecc7
);
1547 static int ov7670_q_expo_correction(struct i2c_client
*client
, __s32
*expo_correction
)
1552 /* testing one value is enough */
1553 ret
= ov7670_read(client
, REG_AEW
, &aew
);
1560 *expo_correction
= 6;
1562 *expo_correction
= 5;
1564 *expo_correction
= 4;
1566 *expo_correction
= 3;
1568 *expo_correction
= 2;
1570 *expo_correction
= 1;
1572 *expo_correction
= 0;
1574 *expo_correction
= -1;
1576 *expo_correction
= -2;
1578 *expo_correction
= -3;
1580 *expo_correction
= -4;
1582 *expo_correction
= -5;
1584 *expo_correction
= -6;
1589 static int ov7670_t_expo_correction(struct i2c_client
*client
, int expo_correction
)
1591 unsigned char aew
, aeb
;
1595 /* set the aec algorithm to average-based mode
1596 (in case it was in histogram-based mode)*/
1597 unsigned char haecc7
;
1598 ret
= ov7670_read(client
, REG_HAECC7
, &haecc7
);
1602 haecc7
= (haecc7
& 0x7f);
1604 ret
= ov7670_write(client
, REG_HAECC7
, haecc7
);
1610 /* pow() not available in kernel space
1612 double power = (double)expo_correction / 3.,
1613 tmpVal = (int)(117. * pow(2., power);
1614 aew = ( tmpVal>255 ) ? 255 : tmpVal;
1616 tmpVal = (int)(99. * pow(2., power);
1617 aeb = ( tmpVal>255 ) ? 255 : tmpVal;*/
1619 switch(expo_correction
)
1649 /* set the aec algorithm back to histogram-based mode */
1650 haecc7
= (haecc7
| 0x80);
1652 ret
= ov7670_write(client
, REG_HAECC7
, haecc7
);
1687 ret
= ov7670_write(client
, REG_AEW
, aew
);
1689 ret
+= ov7670_write(client
, REG_AEB
, aeb
);
1695 /*static int ov7670_q_sharpness(struct i2c_client *client, __s32 *value)
1698 unsigned char autoSharpness, sharpnessMagnitude;
1700 ret = ov7670_read(client, 0xB4, &autoSharpness);
1701 ret += ov7670_read(client, 0xB6, &sharpnessMagnitude);
1707 *value = (int)((autoSharpness & 0x20) + (sharpnessMagnitude & 0x1F));
1712 static int ov7670_t_sharpness(struct i2c_client *client, int value)
1714 unsigned char autoSharpness, sharpnessMagnitude;
1717 ret = ov7670_read(client, 0xB4, &autoSharpness);
1718 ret += ov7670_read(client, 0xB6, &sharpnessMagnitude);
1724 autoSharpness = (autoSharpness & 0xdf) + (value & 0x20);
1725 sharpnessMagnitude = (sharpnessMagnitude & 0xe0) + (value & 0x1f);
1727 ret = ov7670_write(client, 0xB4, autoSharpness);
1728 ret += ov7670_write(client, 0xB6, sharpnessMagnitude);
1733 static int ov7670_q_sharpness(struct i2c_client
*client
, __s32
*value
)
1736 unsigned char sharpness
;
1738 ret
= ov7670_read(client
, 0x75, &sharpness
);
1744 *value
= (int)(sharpness
& 0x1F);
1749 static int ov7670_t_sharpness(struct i2c_client
*client
, int value
)
1751 unsigned char sharpness
;
1754 ret
= ov7670_read(client
, 0x75, &sharpness
);
1760 sharpness
= (sharpness
& 0xE0) + (value
& 0x1F);
1762 ret
= ov7670_write(client
, 0x75, sharpness
);
1770 * Initialise camera with default parameters
1772 static int ov7670_t_setDefaultParam(struct i2c_client
*client
, int value
)
1774 int ret
= ov7670_init(client
);
1779 static struct ov7670_control
{
1780 struct v4l2_queryctrl qc
;
1781 int (*query
)(struct i2c_client
*c
, __s32
*value
);
1782 int (*tweak
)(struct i2c_client
*c
, int value
);
1783 } ov7670_controls
[] =
1787 .id
= V4L2_CID_BRIGHTNESS
,
1788 .type
= V4L2_CTRL_TYPE_INTEGER
,
1789 .name
= "Brightness",
1793 .default_value
= 0x80,
1794 .flags
= V4L2_CTRL_FLAG_SLIDER
1796 .tweak
= ov7670_t_brightness
,
1797 .query
= ov7670_q_brightness
,
1801 .id
= V4L2_CID_CONTRAST
,
1802 .type
= V4L2_CTRL_TYPE_INTEGER
,
1807 .default_value
= 0x40, /* XXX ov7670 spec */
1808 .flags
= V4L2_CTRL_FLAG_SLIDER
1810 .tweak
= ov7670_t_contrast
,
1811 .query
= ov7670_q_contrast
,
1815 .id
= V4L2_CID_SATURATION
,
1816 .type
= V4L2_CTRL_TYPE_INTEGER
,
1817 .name
= "Saturation",
1821 .default_value
= 0x80,
1822 .flags
= V4L2_CTRL_FLAG_SLIDER
1824 .tweak
= ov7670_t_sat
,
1825 .query
= ov7670_q_sat
,
1830 .type
= V4L2_CTRL_TYPE_INTEGER
,
1836 .flags
= V4L2_CTRL_FLAG_SLIDER
1838 .tweak
= ov7670_t_hue
,
1839 .query
= ov7670_q_hue
,
1843 .id
= V4L2_CID_VFLIP
,
1844 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1845 .name
= "Vertical flip",
1851 .tweak
= ov7670_t_vflip
,
1852 .query
= ov7670_q_vflip
,
1856 .id
= V4L2_CID_HFLIP
,
1857 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1858 .name
= "Horizontal mirror",
1864 .tweak
= ov7670_t_hflip
,
1865 .query
= ov7670_q_hflip
,
1869 .id
= V4L2_CID_RED_BALANCE
,
1870 .type
= V4L2_CTRL_TYPE_INTEGER
,
1871 .name
= "Red Balance",
1875 .default_value
= 0x80,
1876 .flags
= V4L2_CTRL_FLAG_SLIDER
1878 .tweak
= ov7670_t_redbalance
,
1879 .query
= ov7670_q_redbalance
,
1883 .id
= V4L2_CID_BLUE_BALANCE
,
1884 .type
= V4L2_CTRL_TYPE_INTEGER
,
1885 .name
= "Blue Balance",
1889 .default_value
= 0x80,
1890 .flags
= V4L2_CTRL_FLAG_SLIDER
1892 .tweak
= ov7670_t_bluebalance
,
1893 .query
= ov7670_q_bluebalance
,
1897 .id
= V4L2_CID_GAIN
,
1898 .type
= V4L2_CTRL_TYPE_INTEGER
,
1903 .default_value
= 0x00,
1904 .flags
= V4L2_CTRL_FLAG_SLIDER
1906 .tweak
= ov7670_t_gain
,
1907 .query
= ov7670_q_gain
,
1911 .id
= V4L2_CID_HCENTER
,
1912 .type
= V4L2_CTRL_TYPE_INTEGER
,
1913 .name
= "Lens Correction X",
1917 .default_value
= 0x00,
1918 .flags
= V4L2_CTRL_FLAG_SLIDER
1920 .tweak
= ov7670_t_hcenter
,
1921 .query
= ov7670_q_hcenter
,
1925 .id
= V4L2_CID_VCENTER
,
1926 .type
= V4L2_CTRL_TYPE_INTEGER
,
1927 .name
= "Lens Correction Y",
1931 .default_value
= 0x00,
1932 .flags
= V4L2_CTRL_FLAG_SLIDER
1934 .tweak
= ov7670_t_vcenter
,
1935 .query
= ov7670_q_vcenter
,
1939 .id
= V4L2_CID_AUTOGAIN
,
1940 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1941 .name
= "AGC Enable",
1945 .default_value
= 0x00,
1947 .tweak
= ov7670_t_agc
,
1948 .query
= ov7670_q_agc
,
1952 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
1953 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1954 .name
= "AWB Enable",
1958 .default_value
= 0x00,
1960 .tweak
= ov7670_t_awb
,
1961 .query
= ov7670_q_awb
,
1965 .id
= V4L2_CID_AUTOEXPOSURE
,
1966 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1967 .name
= "AEC Enable",
1971 .default_value
= 0x00,
1973 .tweak
= ov7670_t_aec
,
1974 .query
= ov7670_q_aec
,
1978 .id
= V4L2_CID_EXPOSURE
,
1979 .type
= V4L2_CTRL_TYPE_INTEGER
,
1982 .maximum
= 4096, //65535,
1984 .default_value
= 60,
1985 .flags
= V4L2_CTRL_FLAG_SLIDER
1987 .tweak
= ov7670_t_expo
,
1988 .query
= ov7670_q_expo
,
1992 .id
= V4L2_CID_CAM_INIT
,
1993 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
1994 .name
= "Set default parameters",
1995 .minimum
= 0, //these parameters
1996 .maximum
= 3, //don't mean
1997 .step
= 1, // anything
1998 .default_value
= 0, // anymore
2000 .tweak
= ov7670_t_setDefaultParam
,
2004 .id
= V4L2_CID_AEC_ALGORITHM
,
2005 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
2006 .name
= "AEC Algorithm",
2012 .tweak
= ov7670_t_aec_algorithm
,
2013 .query
= ov7670_q_aec_algorithm
,
2017 .id
= V4L2_CID_EXPOSURE_CORRECTION
,
2018 .type
= V4L2_CTRL_TYPE_INTEGER
,
2019 .name
= "Exposure Correction",
2024 .flags
= V4L2_CTRL_FLAG_SLIDER
2026 .tweak
= ov7670_t_expo_correction
,
2027 .query
= ov7670_q_expo_correction
,
2031 .id = V4L2_CID_SHARPNESS,
2032 .type = V4L2_CTRL_TYPE_INTEGER,
2033 .name = "Sharpness:[5]auto,[0-4]magnitude",
2038 .flags = V4L2_CTRL_FLAG_SLIDER
2040 .tweak = ov7670_t_sharpness,
2041 .query = ov7670_q_sharpness,
2045 .id
= V4L2_CID_SHARPNESS
,
2046 .type
= V4L2_CTRL_TYPE_INTEGER
,
2047 .name
= "Sharpness",
2052 .flags
= V4L2_CTRL_FLAG_SLIDER
2054 .tweak
= ov7670_t_sharpness
,
2055 .query
= ov7670_q_sharpness
,
2059 #define N_CONTROLS (sizeof(ov7670_controls)/sizeof(ov7670_controls[0]))
2062 static struct ov7670_control
*ov7670_find_control(__u32 id
)
2066 for (i
= 0; i
< N_CONTROLS
; i
++)
2067 if (ov7670_controls
[i
].qc
.id
== id
)
2068 return ov7670_controls
+ i
;
2073 static int ov7670_queryctrl(struct i2c_client
*client
,
2074 struct v4l2_queryctrl
*qc
)
2076 struct ov7670_control
*ctrl
= ov7670_find_control(qc
->id
);
2084 static int ov7670_g_ctrl(struct i2c_client
*client
, struct v4l2_control
*ctrl
)
2086 struct ov7670_control
*octrl
= ov7670_find_control(ctrl
->id
);
2091 ret
= octrl
->query(client
, &ctrl
->value
);
2097 static int ov7670_s_ctrl(struct i2c_client
*client
, struct v4l2_control
*ctrl
)
2099 struct ov7670_control
*octrl
= ov7670_find_control(ctrl
->id
);
2104 ret
= octrl
->tweak(client
, ctrl
->value
);
2114 static struct i2c_driver ov7670_driver
;
2116 static int ov7670_attach(struct i2c_adapter
*adapter
)
2119 struct i2c_client
*client
;
2120 struct ov7670_info
*info
;
2121 printk(KERN_INFO
"ov7670/1: driver attached: adapter id: %x\n", (int)adapter
->id
);
2123 * For now: only deal with adapters we recognize.
2125 if (adapter
->id
!= I2C_HW_SMBUS_CAFE
&&
2126 adapter
->id
!= I2C_HW_SMBUS_SCX200
&&
2127 adapter
->id
!= I2C_HW_B_SER
)
2130 client
= kzalloc(sizeof (struct i2c_client
), GFP_KERNEL
);
2133 client
->adapter
= adapter
;
2134 client
->addr
= OV7670_I2C_ADDR
;
2135 client
->driver
= &ov7670_driver
,
2136 strcpy(client
->name
, "OV7670");
2138 * Set up our info structure.
2140 info
= kzalloc(sizeof (struct ov7670_info
), GFP_KERNEL
);
2145 info
->fmt
= &ov7670_formats
[0];
2146 info
->sat
= 128; /* Review this */
2147 i2c_set_clientdata(client
, info
);
2150 * Make sure it's an ov7670
2152 ret
= ov7670_detect(client
);
2155 ret
= i2c_attach_client(client
);
2159 ov7670_i2c_client
= client
;
2170 static int ov7670_detach(struct i2c_client
*client
)
2173 ov7670_read_dump(client
);
2175 i2c_detach_client(client
);
2176 kfree(i2c_get_clientdata(client
));
2182 int ov7670_command(struct i2c_client
*client
, unsigned int cmd
,
2185 struct v4l2_capability
*cap
;
2187 printk(KERN_INFO
"Executing command %x\n", (int)cmd
);
2191 case VIDIOC_QUERYCAP
:
2193 strcpy(cap
->driver
, "OV7670");
2194 strcpy(cap
->card
, "OV7670");
2196 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
2197 V4L2_CAP_STREAMING
|
2201 case VIDIOC_DBG_G_CHIP_IDENT
:
2202 return v4l2_chip_ident_i2c_client(client
, arg
, V4L2_IDENT_OV7670
, 0);
2204 case VIDIOC_INT_RESET
:
2205 ov7670_reset(client
);
2208 case VIDIOC_INT_INIT
:
2209 return ov7670_init(client
);
2211 case VIDIOC_ENUM_FMT
:
2212 return ov7670_enum_fmt(client
, (struct v4l2_fmtdesc
*) arg
);
2213 case VIDIOC_TRY_FMT
:
2214 return ov7670_try_fmt(client
, (struct v4l2_format
*) arg
, NULL
, NULL
);
2216 { ov7670_params
.format
.type
= ((struct v4l2_format
*)arg
)->type
;
2217 ov7670_params
.format
.fmt
.pix
.field
= ((struct v4l2_format
*)arg
)->fmt
.pix
.field
;
2218 ov7670_params
.format
.fmt
.pix
.width
= ((struct v4l2_format
*)arg
)->fmt
.pix
.width
;
2219 ov7670_params
.format
.fmt
.pix
.height
= ((struct v4l2_format
*)arg
)->fmt
.pix
.height
;
2220 ov7670_params
.format
.fmt
.pix
.pixelformat
= ((struct v4l2_format
*)arg
)->fmt
.pix
.pixelformat
;
2221 return ov7670_s_fmt(client
, (struct v4l2_format
*) arg
);
2223 case VIDIOC_QUERYCTRL
:
2224 return ov7670_queryctrl(client
, (struct v4l2_queryctrl
*) arg
);
2226 return ov7670_s_ctrl(client
, (struct v4l2_control
*) arg
);
2228 return ov7670_g_ctrl(client
, (struct v4l2_control
*) arg
);
2230 { ov7670_params
.framerate
.type
= ((struct v4l2_streamparm
*)arg
)->type
;
2231 ov7670_params
.framerate
.parm
.capture
.timeperframe
.numerator
= ((struct v4l2_streamparm
*)arg
)->parm
.capture
.timeperframe
.numerator
;
2232 ov7670_params
.framerate
.parm
.capture
.timeperframe
.denominator
= ((struct v4l2_streamparm
*)arg
)->parm
.capture
.timeperframe
.denominator
;
2233 ov7670_params
.framerate
.parm
.capture
.capability
= ((struct v4l2_streamparm
*)arg
)->parm
.capture
.capability
;
2234 return ov7670_s_parm(client
, (struct v4l2_streamparm
*) arg
);
2237 return ov7670_g_parm(client
, (struct v4l2_streamparm
*) arg
);
2245 static struct i2c_driver ov7670_driver
= {
2249 .id
= I2C_DRIVERID_OV7670
,
2250 // .class = I2C_CLASS_CAM_DIGITAL,
2251 .attach_adapter
= ov7670_attach
,
2252 .detach_client
= ov7670_detach
,
2253 .command
= ov7670_command
,
2257 * Module initialization
2259 int ov7670_mod_init(void)
2261 printk(KERN_NOTICE
"OmniVision ov7670 sensor driver, at your service (v %s)\n", VERSION
);
2262 return i2c_add_driver(&ov7670_driver
);
2265 void ov7670_mod_exit(void)
2267 i2c_del_driver(&ov7670_driver
);