added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / media / video / lxv4l2 / ov7670.c
blobb93aa3ca3e22a8edc117f15bb39ec480c1cdc1d1
1 /*
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>
22 //#include <math.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"
31 /* History
32 * 1.0 - by Momtchil Momtchev
33 * 1.03 - stability patches (Bri)
34 * 1.04 - added exposition control (Bri)
35 * */
38 * Basic window sizes. These probably belong somewhere more globally
39 * useful.
41 #define VGA_WIDTH 640
42 #define VGA_HEIGHT 480
43 #define QVGA_WIDTH 320
44 #define QVGA_HEIGHT 240
45 #define CIF_WIDTH 352
46 #define CIF_HEIGHT 288
47 #define QCIF_WIDTH 176
48 #define QCIF_HEIGHT 144
50 #undef REGDUMP
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
63 /* Registers */
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
222 } ov7670_params;
226 * Information we maintain about a known sensor.
228 struct ov7670_format_struct; /* coming later */
229 struct ov7670_info {
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
241 * and such.
243 * These settings give VGA YUYV.
246 struct regval_list {
247 unsigned char reg_num;
248 unsigned char value;
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
256 * 2 = 20fps
257 * 1 = 30fps
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 },
328 { REG_RED, 0x60 },
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 },
335 { 0x58, 0x9e },
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 },
342 { 0x56, 0x40 },
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 },
350 { 0x78, 0x04 },
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 },
366 { 0x79, 0x26 },
367 #else
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 },
447 #endif
448 { 0xff, 0xff }, /* END MARKER */
453 * Here we'll try to encapsulate the changes for just the output
454 * video format.
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" */
474 { REG_TSLB, 0x04 },
475 { REG_COM13, COM13_GAMMA|COM13_UVSAT },
476 { 0xff, 0xff },
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 },
492 { 0xff, 0xff },
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 */
508 { 0xff, 0xff },
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 */
517 { 0xff, 0xff },
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
530 char nbretries=0;
531 int ret;
532 do {
533 ret = i2c_smbus_read_byte_data(c, reg);
534 if (ret<0) {
535 msleep(1);
536 nbretries++;
538 } while ((ret < 0) && (nbretries <= OV7670_I2C_MAXRETRIES) );
540 if (ret < 0) {
541 return ret;
542 } else {
543 (*value) = (unsigned char) (ret & 0xff);
544 return 0;
549 static int ov7670_write(struct i2c_client *c, unsigned char reg,
550 unsigned char value)
552 //Bri. : added retries
553 char nbretries=0;
554 int ret=0;
555 do {
556 ret = i2c_smbus_write_byte_data(c, reg, value);
557 if (ret<0) {
558 msleep(1);
559 nbretries++;
561 } while ((ret < 0) && (nbretries <= OV7670_I2C_MAXRETRIES) );
562 return ret;
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)
571 int ret=0;
572 while (vals->reg_num != 0xff || vals->value != 0xff) {
573 ret = ov7670_write(c, vals->reg_num, vals->value);
574 if (ret < 0)
575 return ret;
576 vals++;
577 mdelay(10);
579 return 0;
583 * Dump the registers.
585 #ifdef REGDUMP
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);
594 else
595 printk(KERN_INFO "\t%x\tfailed\n", (unsigned)reg);
597 return 0;
599 #endif
603 * Stuff that knows about the sensor.
605 static void ov7670_reset(struct i2c_client *client)
607 ov7670_write(client, REG_COM7, COM7_RESET);
608 msleep(1);
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)
620 unsigned char v;
621 int ret;
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);
626 if (ret < 0)
627 return ret;
628 printk(KERN_DEBUG "Phase 1\n");
629 ret = ov7670_read(client, REG_MIDH, &v);
630 if (ret < 0)
631 return ret;
632 printk(KERN_DEBUG "Phase 2\n");
633 if (v != 0x7f) /* OV manuf. id. */
634 return -ENODEV;
635 printk(KERN_DEBUG "Phase 3\n");
636 ret = ov7670_read(client, REG_MIDL, &v);
637 if (ret < 0)
638 return ret;
639 printk(KERN_DEBUG "Phase 4\n");
640 if (v != 0xa2)
641 return -ENODEV;
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);
647 if (ret < 0)
648 return ret;
649 printk(KERN_DEBUG "Phase 6\n");
650 if (v != 0x76) /* PID + VER = 0x76 / 0x73 */
651 return -ENODEV;
652 printk(KERN_DEBUG "Phase 7\n");
653 ret = ov7670_read(client, REG_VER, &v);
654 if (ret < 0)
655 return ret;
656 printk(KERN_DEBUG "Phase 8\n");
657 if (v != 0x73) /* PID + VER = 0x76 / 0x73 */
658 return -ENODEV;
659 printk(KERN_DEBUG "Phase 9\n");
660 printk(KERN_INFO "OmniVision 7670/7671 I2C Found\n");
662 return 0;
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 {
672 __u8 *desc;
673 __u32 pixelformat;
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
684 // changed. Julien
685 //.cmatrix = { 0, 0, 0, 0, 0, 0 },
686 .bpp = 2,
689 .desc = "RGB 444",
690 .pixelformat = V4L2_PIX_FMT_RGB444,
691 .regs = ov7670_fmt_rgb444,
692 .cmatrix = { 179, -179, 0, -61, -176, 228 },
693 .bpp = 2,
696 .desc = "RGB 565",
697 .pixelformat = V4L2_PIX_FMT_RGB565,
698 .regs = ov7670_fmt_rgb565,
699 .cmatrix = { 179, -179, 0, -61, -176, 228 },
700 .bpp = 2,
703 .desc = "Raw RGB Bayer",
704 .pixelformat = V4L2_PIX_FMT_SBGGR8,
705 .regs = ov7670_fmt_raw,
706 .cmatrix = { 0, 0, 0, 0, 0, 0 },
707 .bpp = 1
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
721 * settings.
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},
727 { 0x73, 0xf1 },
728 { 0xa2, 0x52 },
729 { 0x7b, 0x1c },
730 { 0x7c, 0x28 },
731 { 0x7d, 0x3c },
732 { 0x7f, 0x69 },
733 { REG_COM9, 0x38 },
734 { 0xa1, 0x0b },
735 { 0x74, 0x19 },
736 { 0x9a, 0x80 },
737 { 0x43, 0x14 },
738 { REG_COM13, 0xc0 },
739 { 0xff, 0xff },
742 static struct ov7670_win_size {
743 int width;
744 int height;
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 */
751 /* h/vref stuff */
752 } ov7670_win_sizes[] = {
753 /* VGA */
755 .width = VGA_WIDTH,
756 .height = VGA_HEIGHT,
757 .com7_bit = COM7_FMT_VGA,
758 .hstart = 158, /* These values from */
759 .hstop = 14, /* Omnivision */
760 .vstart = 10,
761 .vstop = 490,
762 .regs = NULL,
764 /* CIF */
766 .width = CIF_WIDTH,
767 .height = CIF_HEIGHT,
768 .com7_bit = COM7_FMT_CIF,
769 .hstart = 170, /* Empirically determined */
770 .hstop = 90,
771 .vstart = 14,
772 .vstop = 494,
773 .regs = NULL,
775 /* QVGA */
777 .width = QVGA_WIDTH,
778 .height = QVGA_HEIGHT,
779 .com7_bit = COM7_FMT_QVGA,
780 .hstart = 164, /* Empirically determined */
781 .hstop = 20,
782 .vstart = 14,
783 .vstop = 494,
784 .regs = NULL,
786 /* QCIF */
788 .width = QCIF_WIDTH,
789 .height = QCIF_HEIGHT,
790 .com7_bit = COM7_FMT_VGA, /* see comment above */
791 .hstart = 456, /* Empirically determined */
792 .hstop = 24,
793 .vstart = 14,
794 .vstop = 494,
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)
808 int ret;
809 unsigned char v;
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);
819 msleep(10);
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);
828 msleep(10);
829 ret += ov7670_write(client, REG_VREF, v);
830 return ret;
835 * Enumerate formats.
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)
842 return -EINVAL;
844 ofmt = ov7670_formats + fmt->index;
845 fmt->flags = 0;
846 strcpy(fmt->description, ofmt->desc);
847 fmt->pixelformat = ofmt->pixelformat;
848 return 0;
853 * Try a format.
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)
859 int index;
860 struct ov7670_win_size *wsize;
861 struct v4l2_pix_format *pix = &fmt->fmt.pix;
863 #if 0
864 printk(KERN_INFO "Asking for format %x\n", (int)pix->pixelformat);
865 #endif
866 for (index = 0; index < N_OV7670_FMTS; index++) {
867 #if 0
868 printk(KERN_INFO "Have format %x\n", (int)ov7670_formats[index].pixelformat);
869 #endif
870 if (ov7670_formats[index].pixelformat == pix->pixelformat)
871 break;
873 if (index >= N_OV7670_FMTS) {
874 printk(KERN_INFO "No such format %d\n", (int)index);
875 return -EINVAL;
877 if (ret_fmt != NULL)
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");
886 return -EINVAL;
889 * Round requested image size down to the nearest
890 * we support, but not below the smallest.
892 #if 0
893 printk(KERN_INFO "Asking for size %dx%d\n", (int)pix->width, (int)pix->height);
894 #endif
895 for (wsize = ov7670_win_sizes; wsize < ov7670_win_sizes + N_WIN_SIZES;
896 wsize++)
897 if (pix->width >= wsize->width && pix->height >= wsize->height)
898 break;
899 if (wsize >= ov7670_win_sizes + N_WIN_SIZES)
900 wsize--; /* Take the smallest one */
901 if (ret_wsize != NULL)
902 *ret_wsize = wsize;
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;
910 #if 0
911 printk(KERN_INFO "Have size %dx%d\n", (int)pix->width, (int)pix->height);
912 #endif
913 return 0;
917 * Set a format.
919 static int ov7670_s_fmt(struct i2c_client *c, struct v4l2_format *fmt)
921 int ret;
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);
928 if (ret)
929 return ret;
931 * HACK: if we're running rgb565 we need to grab then rewrite
932 * CLKRC. If we're *not*, however, then rewriting clkrc hoses
933 * the colors.
935 if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_RGB565) {
936 ret = ov7670_read(c, REG_CLKRC, &clkrc);
937 if (ret)
938 return ret;
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,
954 wsize->vstop);
955 ret = 0;
956 if (wsize->regs)
957 ret = ov7670_write_array(c, wsize->regs);
958 info->fmt = ovfmt;
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;
966 return ret;
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;
976 unsigned char clkrc;
977 int ret;
979 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
980 return -EINVAL;
981 ret = ov7670_read(c, REG_CLKRC, &clkrc);
982 if (ret < 0)
983 return ret;
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);
990 return 0;
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;
997 unsigned char clkrc;
998 int ret, div;
1000 if (parms->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1001 return -EINVAL;
1002 if (cp->extendedmode != 0)
1003 return -EINVAL;
1005 * CLKRC has a reserved bit, so let's preserve it.
1007 ret = ov7670_read(c, REG_CLKRC, &clkrc);
1008 if (ret < 0)
1009 return ret;
1010 if (tpf->numerator == 0 || tpf->denominator == 0)
1011 div = 1; /* Reset to full rate */
1012 else
1013 div = (tpf->numerator*OV7670_FRAME_RATE)/tpf->denominator;
1014 if (div == 0)
1015 div = 1;
1016 else if (div > CLK_SCALE)
1017 div = CLK_SCALE;
1018 clkrc = (clkrc & 0x80) | div;
1019 tpf->numerator = 1;
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])
1037 int i, ret;
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);
1045 signbits &= 0xc0;
1047 for (i = 0; i < CMATRIX_LEN; i++) {
1048 unsigned char raw;
1050 if (matrix[i] < 0) {
1051 signbits |= (1 << i);
1052 if (matrix[i] < -255)
1053 raw = 0xff;
1054 else
1055 raw = (-1 * matrix[i]) & 0xff;
1057 else {
1058 if (matrix[i] > 255)
1059 raw = 0xff;
1060 else
1061 raw = matrix[i] & 0xff;
1063 //Bri.
1064 msleep(10);
1065 ret += ov7670_write(client, REG_CMATRIX_BASE + i, raw);
1067 //Bri.
1068 msleep(10);
1069 ret += ov7670_write(client, REG_CMATRIX_SIGN, signbits);
1070 return ret;
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.
1083 #define SIN_STEP 5
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,
1088 1000
1091 static int ov7670_sine(int theta)
1093 int chs = 1;
1094 int sine;
1096 if (theta < 0) {
1097 theta = -theta;
1098 chs = -1;
1100 if (theta <= 90)
1101 sine = ov7670_sin_table[theta/SIN_STEP];
1102 else {
1103 theta -= 90;
1104 sine = 1000 - ov7670_sin_table[theta/SIN_STEP];
1106 return sine*chs;
1109 static int ov7670_cosine(int theta)
1111 theta = 90 - theta;
1112 if (theta > 180)
1113 theta -= 360;
1114 else if (theta < -180)
1115 theta += 360;
1116 return ov7670_sine(theta);
1122 static void ov7670_calc_cmatrix(struct ov7670_info *info,
1123 int matrix[CMATRIX_LEN])
1125 int i;
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];
1162 int ret;
1164 //Bri.
1165 if (info == NULL) return -EINVAL;
1166 info->sat = value;
1167 ov7670_calc_cmatrix(info, matrix);
1168 ret = ov7670_store_cmatrix(client, matrix);
1169 return ret;
1172 static int ov7670_q_sat(struct i2c_client *client, __s32 *value)
1174 struct ov7670_info *info = i2c_get_clientdata(client);
1176 //Bri.
1177 if (info == NULL) return -EINVAL;
1178 if (value == NULL) return -EINVAL;
1179 *value = info->sat;
1180 return 0;
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];
1187 int ret;
1189 if (value < -180 || value > 180)
1190 return -EINVAL;
1191 info->hue = value;
1192 ov7670_calc_cmatrix(info, matrix);
1193 ret = ov7670_store_cmatrix(client, matrix);
1194 return ret;
1198 static int ov7670_q_hue(struct i2c_client *client, __s32 *value)
1200 struct ov7670_info *info = i2c_get_clientdata(client);
1202 //Bri.
1203 if (info == NULL) return -EINVAL;
1204 if (value == NULL) return -EINVAL;
1205 *value = info->hue;
1206 return 0;
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)
1216 return v + 128;
1217 else
1218 return 128 - (v & 0x7f);
1222 static unsigned char ov7670_abs_to_sm(unsigned char v)
1224 if (v > 127)
1225 return v & 0x7f;
1226 else
1227 return (128 - v) | 0x80;
1230 static int ov7670_t_brightness(struct i2c_client *client, int value)
1232 unsigned char com8, v;
1233 int ret;
1235 ov7670_read(client, REG_COM8, &com8);
1236 com8 &= ~COM8_AEC;
1237 ov7670_write(client, REG_COM8, com8);
1238 v = ov7670_abs_to_sm(value);
1239 ret = ov7670_write(client, REG_BRIGHT, v);
1240 return ret;
1243 static int ov7670_q_brightness(struct i2c_client *client, __s32 *value)
1245 unsigned char v;
1246 int ret = ov7670_read(client, REG_BRIGHT, &v);
1248 *value = ov7670_sm_to_abs(v);
1249 return ret;
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)
1259 unsigned char v;
1260 int ret = ov7670_read(client, REG_CONTRAS, &v);
1262 *value = v;
1263 return ret;
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)
1273 unsigned char v;
1274 int ret = ov7670_read(client, REG_RED, &v);
1276 *value = v;
1277 return ret;
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)
1287 unsigned char v;
1288 int ret = ov7670_read(client, REG_BLUE, &v);
1290 *value = v;
1291 return ret;
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)
1301 unsigned char v;
1302 int ret = ov7670_read(client, REG_GAIN, &v);
1304 *value = v;
1305 return ret;
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)
1315 unsigned char v;
1316 int ret = ov7670_read(client, REG_LCC1, &v);
1318 *value = v;
1319 return ret;
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)
1329 unsigned char v;
1330 int ret = ov7670_read(client, REG_LCC2, &v);
1332 *value = v;
1333 return ret;
1336 static int ov7670_q_hflip(struct i2c_client *client, __s32 *value)
1338 int ret;
1339 unsigned char v;
1341 ret = ov7670_read(client, REG_MVFP, &v);
1342 *value = (v & MVFP_MIRROR) == MVFP_MIRROR;
1343 return ret;
1347 static int ov7670_t_hflip(struct i2c_client *client, int value)
1349 unsigned char v;
1350 int ret;
1352 ret = ov7670_read(client, REG_MVFP, &v);
1353 if (value)
1354 v |= MVFP_MIRROR;
1355 else
1356 v &= ~MVFP_MIRROR;
1357 ret += ov7670_write(client, REG_MVFP, v);
1358 return ret;
1363 static int ov7670_q_vflip(struct i2c_client *client, __s32 *value)
1365 int ret;
1366 unsigned char v;
1368 ret = ov7670_read(client, REG_MVFP, &v);
1369 *value = (v & MVFP_FLIP) == MVFP_FLIP;
1370 return ret;
1373 static int ov7670_t_vflip(struct i2c_client *client, int value)
1375 unsigned char v;
1376 int ret;
1378 ret = ov7670_read(client, REG_MVFP, &v);
1379 if (value)
1380 v |= MVFP_FLIP;
1381 else
1382 v &= ~MVFP_FLIP;
1383 ret += ov7670_write(client, REG_MVFP, v);
1384 return ret;
1387 static int ov7670_q_agc(struct i2c_client *client, __s32 *value)
1389 int ret;
1390 unsigned char v;
1392 ret = ov7670_read(client, REG_COM8, &v);
1393 *value = (v & COM8_AGC) == COM8_AGC;
1394 return ret;
1397 static int ov7670_t_agc(struct i2c_client *client, int value)
1399 unsigned char v;
1400 int ret;
1402 ret = ov7670_read(client, REG_COM8, &v);
1403 if (value)
1404 v |= COM8_AGC;
1405 else
1406 v &= ~COM8_AGC;
1407 ret += ov7670_write(client, REG_COM8, v);
1408 return ret;
1411 static int ov7670_q_awb(struct i2c_client *client, __s32 *value)
1413 int ret;
1414 unsigned char v;
1416 ret = ov7670_read(client, REG_COM8, &v);
1417 *value = (v & COM8_AWB) == COM8_AWB;
1418 return ret;
1421 static int ov7670_t_awb(struct i2c_client *client, int value)
1423 unsigned char v;
1424 int ret;
1426 ret = ov7670_read(client, REG_COM8, &v);
1427 if (value)
1428 v |= COM8_AWB;
1429 else
1430 v &= ~COM8_AWB;
1432 ret += ov7670_write(client, REG_COM8, v);
1433 return ret;
1436 static int ov7670_q_aec(struct i2c_client *client, __s32 *value)
1438 int ret;
1439 unsigned char v;
1441 ret = ov7670_read(client, REG_COM8, &v);
1442 *value = (v & COM8_AEC) == COM8_AEC;
1443 return ret;
1446 static int ov7670_t_aec(struct i2c_client *client, int value)
1448 unsigned char v;
1449 int ret;
1451 ret = ov7670_read(client, REG_COM8, &v);
1452 if (value)
1453 v |= COM8_AEC;
1454 else
1455 v &= ~COM8_AEC;
1457 ret += ov7670_write(client, REG_COM8, v);
1458 return ret;
1461 static int ov7670_q_expo(struct i2c_client *client, __s32 *expo)
1463 int ret = 0;
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);
1470 if (ret) {
1471 return ret;
1474 *expo = ((int)(aechh & 0x3f) << 10) + ((int)aech << 2) + (com1 & 0x03);
1476 return ret;
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]
1483 * */
1485 static int ov7670_t_expo(struct i2c_client *client, int expo)
1487 unsigned char com1, aech, aechh;
1488 int ret = 0;
1490 ret = ov7670_read(client, REG_COM1, &com1);
1491 ret += ov7670_read(client, REG_AECH, &aech);
1492 ret += ov7670_read(client, REG_AECHH, &aechh);
1494 if (ret) {
1495 return ret;
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);
1507 return ret;
1511 static int ov7670_q_aec_algorithm(struct i2c_client *client, __s32 *value)
1513 int ret = 0;
1514 unsigned char haecc7;
1516 ret = ov7670_read(client, REG_HAECC7, &haecc7);
1518 if (ret) {
1519 return ret;
1522 *value = ((int)(haecc7 & 0x80) >> 7);
1524 return ret;
1527 static int ov7670_t_aec_algorithm(struct i2c_client *client, int value)
1529 unsigned char haecc7;
1530 int ret = 0;
1532 ret = ov7670_read(client, REG_HAECC7, &haecc7);
1534 if (ret) {
1535 return ret;
1537 haecc7 = (haecc7 & 0x7f) + ((value!=0) << 7);
1539 ret = ov7670_write(client, REG_HAECC7, haecc7);
1541 return ret;
1547 static int ov7670_q_expo_correction(struct i2c_client *client, __s32 *expo_correction)
1549 int ret = 0;
1550 unsigned char aew;
1552 /* testing one value is enough */
1553 ret = ov7670_read(client, REG_AEW, &aew);
1555 if (ret) {
1556 return ret;
1559 if(aew>234)
1560 *expo_correction = 6;
1561 else if(aew>186)
1562 *expo_correction = 5;
1563 else if(aew>147)
1564 *expo_correction = 4;
1565 else if(aew>117)
1566 *expo_correction = 3;
1567 else if(aew>93)
1568 *expo_correction = 2;
1569 else if(aew>74)
1570 *expo_correction = 1;
1571 else if(aew>59)
1572 *expo_correction = 0;
1573 else if(aew>46)
1574 *expo_correction = -1;
1575 else if(aew>37)
1576 *expo_correction = -2;
1577 else if(aew>29)
1578 *expo_correction = -3;
1579 else if(aew>24)
1580 *expo_correction = -4;
1581 else if(aew>19)
1582 *expo_correction = -5;
1583 else
1584 *expo_correction = -6;
1586 return ret;
1589 static int ov7670_t_expo_correction(struct i2c_client *client, int expo_correction)
1591 unsigned char aew, aeb;
1592 //int tmpVal;
1593 int ret = 0;
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);
1599 if (ret) {
1600 return ret;
1602 haecc7 = (haecc7 & 0x7f);
1603 msleep(1);
1604 ret = ov7670_write(client, REG_HAECC7, haecc7);
1605 if (ret) {
1606 return ret;
1608 msleep(1);
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)
1621 case 6:
1622 aew = 255;
1623 aeb = 237;
1624 break;
1625 case 5:
1626 aew = 234;
1627 aeb = 198;
1628 break;
1629 case 4:
1630 aew = 186;
1631 aeb = 157;
1632 break;
1633 case 3:
1634 aew = 147;
1635 aeb = 125;
1636 break;
1637 case 2:
1638 aew = 117;
1639 aeb = 99;
1640 break;
1641 case 1:
1642 aew = 93;
1643 aeb = 75;//79;
1644 break;
1645 case 0:
1646 aew = 74;
1647 aeb = 56;//62;
1649 /* set the aec algorithm back to histogram-based mode */
1650 haecc7 = (haecc7 | 0x80);
1651 //msleep(1);
1652 ret = ov7670_write(client, REG_HAECC7, haecc7);
1653 if (ret) {
1654 return ret;
1656 msleep(1);
1658 break;
1659 case -1:
1660 aew = 59;
1661 aeb = 41;//49;
1662 break;
1663 case -2:
1664 aew = 46;
1665 aeb = 28;//39;
1666 break;
1667 case -3:
1668 aew = 37;
1669 aeb = 19;//31;
1670 break;
1671 case -4:
1672 aew = 29;
1673 aeb = 11;//25;
1674 break;
1675 case -5:
1676 aew = 24;
1677 aeb = 6;//19;
1678 break;
1679 case -6:
1680 aew = 19;
1681 aeb = 1;//16;
1682 break;
1683 default:
1684 break;
1687 ret = ov7670_write(client, REG_AEW, aew);
1688 msleep(1);
1689 ret += ov7670_write(client, REG_AEB, aeb);
1691 return ret;
1695 /*static int ov7670_q_sharpness(struct i2c_client *client, __s32 *value)
1697 int ret = 0;
1698 unsigned char autoSharpness, sharpnessMagnitude;
1700 ret = ov7670_read(client, 0xB4, &autoSharpness);
1701 ret += ov7670_read(client, 0xB6, &sharpnessMagnitude);
1703 if (ret) {
1704 return ret;
1707 *value = (int)((autoSharpness & 0x20) + (sharpnessMagnitude & 0x1F));
1709 return ret;
1712 static int ov7670_t_sharpness(struct i2c_client *client, int value)
1714 unsigned char autoSharpness, sharpnessMagnitude;
1715 int ret = 0;
1717 ret = ov7670_read(client, 0xB4, &autoSharpness);
1718 ret += ov7670_read(client, 0xB6, &sharpnessMagnitude);
1720 if (ret) {
1721 return ret;
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);
1730 return ret;
1733 static int ov7670_q_sharpness(struct i2c_client *client, __s32 *value)
1735 int ret = 0;
1736 unsigned char sharpness;
1738 ret = ov7670_read(client, 0x75, &sharpness);
1740 if (ret) {
1741 return ret;
1744 *value = (int)(sharpness & 0x1F);
1746 return ret;
1749 static int ov7670_t_sharpness(struct i2c_client *client, int value)
1751 unsigned char sharpness;
1752 int ret = 0;
1754 ret = ov7670_read(client, 0x75, &sharpness);
1756 if (ret) {
1757 return ret;
1760 sharpness = (sharpness & 0xE0) + (value & 0x1F);
1762 ret = ov7670_write(client, 0x75, sharpness);
1764 return ret;
1770 * Initialise camera with default parameters
1772 static int ov7670_t_setDefaultParam(struct i2c_client *client, int value)
1774 int ret = ov7670_init(client);
1775 return ret;
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[] =
1786 .qc = {
1787 .id = V4L2_CID_BRIGHTNESS,
1788 .type = V4L2_CTRL_TYPE_INTEGER,
1789 .name = "Brightness",
1790 .minimum = 0,
1791 .maximum = 255,
1792 .step = 1,
1793 .default_value = 0x80,
1794 .flags = V4L2_CTRL_FLAG_SLIDER
1796 .tweak = ov7670_t_brightness,
1797 .query = ov7670_q_brightness,
1800 .qc = {
1801 .id = V4L2_CID_CONTRAST,
1802 .type = V4L2_CTRL_TYPE_INTEGER,
1803 .name = "Contrast",
1804 .minimum = 0,
1805 .maximum = 127,
1806 .step = 1,
1807 .default_value = 0x40, /* XXX ov7670 spec */
1808 .flags = V4L2_CTRL_FLAG_SLIDER
1810 .tweak = ov7670_t_contrast,
1811 .query = ov7670_q_contrast,
1814 .qc = {
1815 .id = V4L2_CID_SATURATION,
1816 .type = V4L2_CTRL_TYPE_INTEGER,
1817 .name = "Saturation",
1818 .minimum = 0,
1819 .maximum = 256,
1820 .step = 1,
1821 .default_value = 0x80,
1822 .flags = V4L2_CTRL_FLAG_SLIDER
1824 .tweak = ov7670_t_sat,
1825 .query = ov7670_q_sat,
1828 .qc = {
1829 .id = V4L2_CID_HUE,
1830 .type = V4L2_CTRL_TYPE_INTEGER,
1831 .name = "HUE",
1832 .minimum = -180,
1833 .maximum = 180,
1834 .step = 5,
1835 .default_value = 0,
1836 .flags = V4L2_CTRL_FLAG_SLIDER
1838 .tweak = ov7670_t_hue,
1839 .query = ov7670_q_hue,
1842 .qc = {
1843 .id = V4L2_CID_VFLIP,
1844 .type = V4L2_CTRL_TYPE_BOOLEAN,
1845 .name = "Vertical flip",
1846 .minimum = 0,
1847 .maximum = 1,
1848 .step = 1,
1849 .default_value = 0,
1851 .tweak = ov7670_t_vflip,
1852 .query = ov7670_q_vflip,
1855 .qc = {
1856 .id = V4L2_CID_HFLIP,
1857 .type = V4L2_CTRL_TYPE_BOOLEAN,
1858 .name = "Horizontal mirror",
1859 .minimum = 0,
1860 .maximum = 1,
1861 .step = 1,
1862 .default_value = 0,
1864 .tweak = ov7670_t_hflip,
1865 .query = ov7670_q_hflip,
1868 .qc = {
1869 .id = V4L2_CID_RED_BALANCE,
1870 .type = V4L2_CTRL_TYPE_INTEGER,
1871 .name = "Red Balance",
1872 .minimum = 0,
1873 .maximum = 0xFF,
1874 .step = 1,
1875 .default_value = 0x80,
1876 .flags = V4L2_CTRL_FLAG_SLIDER
1878 .tweak = ov7670_t_redbalance,
1879 .query = ov7670_q_redbalance,
1882 .qc = {
1883 .id = V4L2_CID_BLUE_BALANCE,
1884 .type = V4L2_CTRL_TYPE_INTEGER,
1885 .name = "Blue Balance",
1886 .minimum = 0,
1887 .maximum = 0xFF,
1888 .step = 1,
1889 .default_value = 0x80,
1890 .flags = V4L2_CTRL_FLAG_SLIDER
1892 .tweak = ov7670_t_bluebalance,
1893 .query = ov7670_q_bluebalance,
1896 .qc = {
1897 .id = V4L2_CID_GAIN,
1898 .type = V4L2_CTRL_TYPE_INTEGER,
1899 .name = "Gain",
1900 .minimum = 0,
1901 .maximum = 0xFF,
1902 .step = 1,
1903 .default_value = 0x00,
1904 .flags = V4L2_CTRL_FLAG_SLIDER
1906 .tweak = ov7670_t_gain,
1907 .query = ov7670_q_gain,
1910 .qc = {
1911 .id = V4L2_CID_HCENTER,
1912 .type = V4L2_CTRL_TYPE_INTEGER,
1913 .name = "Lens Correction X",
1914 .minimum = 0,
1915 .maximum = 0xFF,
1916 .step = 1,
1917 .default_value = 0x00,
1918 .flags = V4L2_CTRL_FLAG_SLIDER
1920 .tweak = ov7670_t_hcenter,
1921 .query = ov7670_q_hcenter,
1924 .qc = {
1925 .id = V4L2_CID_VCENTER,
1926 .type = V4L2_CTRL_TYPE_INTEGER,
1927 .name = "Lens Correction Y",
1928 .minimum = 0,
1929 .maximum = 0xFF,
1930 .step = 1,
1931 .default_value = 0x00,
1932 .flags = V4L2_CTRL_FLAG_SLIDER
1934 .tweak = ov7670_t_vcenter,
1935 .query = ov7670_q_vcenter,
1938 .qc = {
1939 .id = V4L2_CID_AUTOGAIN,
1940 .type = V4L2_CTRL_TYPE_BOOLEAN,
1941 .name = "AGC Enable",
1942 .minimum = 0,
1943 .maximum = 1,
1944 .step = 1,
1945 .default_value = 0x00,
1947 .tweak = ov7670_t_agc,
1948 .query = ov7670_q_agc,
1951 .qc = {
1952 .id = V4L2_CID_AUTO_WHITE_BALANCE,
1953 .type = V4L2_CTRL_TYPE_BOOLEAN,
1954 .name = "AWB Enable",
1955 .minimum = 0,
1956 .maximum = 1,
1957 .step = 1,
1958 .default_value = 0x00,
1960 .tweak = ov7670_t_awb,
1961 .query = ov7670_q_awb,
1964 .qc = {
1965 .id = V4L2_CID_AUTOEXPOSURE,
1966 .type = V4L2_CTRL_TYPE_BOOLEAN,
1967 .name = "AEC Enable",
1968 .minimum = 0,
1969 .maximum = 1,
1970 .step = 1,
1971 .default_value = 0x00,
1973 .tweak = ov7670_t_aec,
1974 .query = ov7670_q_aec,
1977 .qc = {
1978 .id = V4L2_CID_EXPOSURE,
1979 .type = V4L2_CTRL_TYPE_INTEGER,
1980 .name = "Exposure",
1981 .minimum = 0,
1982 .maximum = 4096, //65535,
1983 .step = 1,
1984 .default_value = 60,
1985 .flags = V4L2_CTRL_FLAG_SLIDER
1987 .tweak = ov7670_t_expo,
1988 .query = ov7670_q_expo,
1991 .qc = {
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,
2003 .qc = {
2004 .id = V4L2_CID_AEC_ALGORITHM,
2005 .type = V4L2_CTRL_TYPE_BOOLEAN,
2006 .name = "AEC Algorithm",
2007 .minimum = 0,
2008 .maximum = 1,
2009 .step = 1,
2010 .default_value = 1,
2012 .tweak = ov7670_t_aec_algorithm,
2013 .query = ov7670_q_aec_algorithm,
2016 .qc = {
2017 .id = V4L2_CID_EXPOSURE_CORRECTION,
2018 .type = V4L2_CTRL_TYPE_INTEGER,
2019 .name = "Exposure Correction",
2020 .minimum = -6,
2021 .maximum = 6,
2022 .step = 1,
2023 .default_value = 0,
2024 .flags = V4L2_CTRL_FLAG_SLIDER
2026 .tweak = ov7670_t_expo_correction,
2027 .query = ov7670_q_expo_correction,
2030 .qc = {
2031 .id = V4L2_CID_SHARPNESS,
2032 .type = V4L2_CTRL_TYPE_INTEGER,
2033 .name = "Sharpness:[5]auto,[0-4]magnitude",
2034 .minimum = 0,
2035 .maximum = 63,
2036 .step = 1,
2037 .default_value = 0,
2038 .flags = V4L2_CTRL_FLAG_SLIDER
2040 .tweak = ov7670_t_sharpness,
2041 .query = ov7670_q_sharpness,
2042 },*/
2044 .qc = {
2045 .id = V4L2_CID_SHARPNESS,
2046 .type = V4L2_CTRL_TYPE_INTEGER,
2047 .name = "Sharpness",
2048 .minimum = 0,
2049 .maximum = 31,
2050 .step = 1,
2051 .default_value = 2,
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)
2064 int i;
2066 for (i = 0; i < N_CONTROLS; i++)
2067 if (ov7670_controls[i].qc.id == id)
2068 return ov7670_controls + i;
2069 return NULL;
2073 static int ov7670_queryctrl(struct i2c_client *client,
2074 struct v4l2_queryctrl *qc)
2076 struct ov7670_control *ctrl = ov7670_find_control(qc->id);
2078 if (ctrl == NULL)
2079 return -EINVAL;
2080 *qc = ctrl->qc;
2081 return 0;
2084 static int ov7670_g_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
2086 struct ov7670_control *octrl = ov7670_find_control(ctrl->id);
2087 int ret;
2089 if (octrl == NULL)
2090 return -EINVAL;
2091 ret = octrl->query(client, &ctrl->value);
2092 if (ret >= 0)
2093 return 0;
2094 return ret;
2097 static int ov7670_s_ctrl(struct i2c_client *client, struct v4l2_control *ctrl)
2099 struct ov7670_control *octrl = ov7670_find_control(ctrl->id);
2100 int ret;
2102 if (octrl == NULL)
2103 return -EINVAL;
2104 ret = octrl->tweak(client, ctrl->value);
2105 if (ret >= 0)
2106 return 0;
2107 return ret;
2112 * Basic i2c stuff.
2114 static struct i2c_driver ov7670_driver;
2116 static int ov7670_attach(struct i2c_adapter *adapter)
2118 int ret;
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)
2128 return -ENODEV;
2130 client = kzalloc(sizeof (struct i2c_client), GFP_KERNEL);
2131 if (! client)
2132 return -ENOMEM;
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);
2141 if (! info) {
2142 ret = -ENOMEM;
2143 goto out_free;
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);
2153 if (ret)
2154 goto out_free_info;
2155 ret = i2c_attach_client(client);
2156 if (ret)
2157 goto out_free_info;
2159 ov7670_i2c_client = client;
2160 return 0;
2162 out_free_info:
2163 kfree(info);
2164 out_free:
2165 kfree(client);
2166 return ret;
2170 static int ov7670_detach(struct i2c_client *client)
2172 #ifdef REGDUMP
2173 ov7670_read_dump(client);
2174 #endif
2175 i2c_detach_client(client);
2176 kfree(i2c_get_clientdata(client));
2177 kfree(client);
2178 return 0;
2182 int ov7670_command(struct i2c_client *client, unsigned int cmd,
2183 void *arg)
2185 struct v4l2_capability *cap;
2186 #if 0
2187 printk(KERN_INFO "Executing command %x\n", (int)cmd);
2188 #endif
2189 switch (cmd) {
2191 case VIDIOC_QUERYCAP:
2192 cap = arg;
2193 strcpy(cap->driver, "OV7670");
2194 strcpy(cap->card, "OV7670");
2195 cap->version = 2;
2196 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
2197 V4L2_CAP_STREAMING |
2198 V4L2_CAP_READWRITE;
2199 return 0;
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);
2206 return 0;
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);
2215 case VIDIOC_S_FMT:
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);
2225 case VIDIOC_S_CTRL:
2226 return ov7670_s_ctrl(client, (struct v4l2_control *) arg);
2227 case VIDIOC_G_CTRL:
2228 return ov7670_g_ctrl(client, (struct v4l2_control *) arg);
2229 case VIDIOC_S_PARM:
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);
2236 case VIDIOC_G_PARM:
2237 return ov7670_g_parm(client, (struct v4l2_streamparm *) arg);
2238 default:
2239 return -EINVAL;
2241 return -EINVAL;
2245 static struct i2c_driver ov7670_driver = {
2246 .driver = {
2247 .name = "ov7670",
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);