Commit | Line | Data |
---|---|---|
111f3356 JC |
1 | /* |
2 | * A V4L2 driver for OmniVision OV7670 cameras. | |
3 | * | |
4 | * Copyright 2006 One Laptop Per Child Association, Inc. Written | |
5 | * by Jonathan Corbet with substantial inspiration from Mark | |
6 | * McClelland's ovcamchip code. | |
7 | * | |
8 | * This file may be distributed under the terms of the GNU General | |
9 | * Public License, version 2. | |
10 | */ | |
11 | #include <linux/init.h> | |
12 | #include <linux/module.h> | |
13 | #include <linux/moduleparam.h> | |
14 | #include <linux/slab.h> | |
15 | #include <linux/delay.h> | |
16 | #include <linux/videodev.h> | |
17 | #include <media/v4l2-common.h> | |
18 | #include <linux/i2c.h> | |
19 | ||
20 | ||
21 | MODULE_AUTHOR("Jonathan Corbet <corbet@lwn.net."); | |
22 | MODULE_DESCRIPTION("A low-level driver for OmniVision ov7670 sensors"); | |
23 | MODULE_LICENSE("GPL"); | |
24 | ||
25 | /* | |
26 | * Basic window sizes. These probably belong somewhere more globally | |
27 | * useful. | |
28 | */ | |
29 | #define VGA_WIDTH 640 | |
30 | #define VGA_HEIGHT 480 | |
31 | #define QVGA_WIDTH 320 | |
32 | #define QVGA_HEIGHT 240 | |
33 | #define CIF_WIDTH 352 | |
34 | #define CIF_HEIGHT 288 | |
35 | #define QCIF_WIDTH 176 | |
36 | #define QCIF_HEIGHT 144 | |
37 | ||
38 | /* | |
39 | * The 7670 sits on i2c with ID 0x42 | |
40 | */ | |
41 | #define OV7670_I2C_ADDR 0x42 | |
42 | ||
43 | /* Registers */ | |
44 | #define REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */ | |
45 | #define REG_BLUE 0x01 /* blue gain */ | |
46 | #define REG_RED 0x02 /* red gain */ | |
47 | #define REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */ | |
48 | #define REG_COM1 0x04 /* Control 1 */ | |
49 | #define COM1_CCIR656 0x40 /* CCIR656 enable */ | |
50 | #define REG_BAVE 0x05 /* U/B Average level */ | |
51 | #define REG_GbAVE 0x06 /* Y/Gb Average level */ | |
52 | #define REG_AECHH 0x07 /* AEC MS 5 bits */ | |
53 | #define REG_RAVE 0x08 /* V/R Average level */ | |
54 | #define REG_COM2 0x09 /* Control 2 */ | |
55 | #define COM2_SSLEEP 0x10 /* Soft sleep mode */ | |
56 | #define REG_PID 0x0a /* Product ID MSB */ | |
57 | #define REG_VER 0x0b /* Product ID LSB */ | |
58 | #define REG_COM3 0x0c /* Control 3 */ | |
59 | #define COM3_SWAP 0x40 /* Byte swap */ | |
60 | #define COM3_SCALEEN 0x08 /* Enable scaling */ | |
61 | #define COM3_DCWEN 0x04 /* Enable downsamp/crop/window */ | |
62 | #define REG_COM4 0x0d /* Control 4 */ | |
63 | #define REG_COM5 0x0e /* All "reserved" */ | |
64 | #define REG_COM6 0x0f /* Control 6 */ | |
65 | #define REG_AECH 0x10 /* More bits of AEC value */ | |
66 | #define REG_CLKRC 0x11 /* Clocl control */ | |
67 | #define CLK_EXT 0x40 /* Use external clock directly */ | |
68 | #define CLK_SCALE 0x3f /* Mask for internal clock scale */ | |
69 | #define REG_COM7 0x12 /* Control 7 */ | |
70 | #define COM7_RESET 0x80 /* Register reset */ | |
71 | #define COM7_FMT_MASK 0x38 | |
72 | #define COM7_FMT_VGA 0x00 | |
73 | #define COM7_FMT_CIF 0x20 /* CIF format */ | |
74 | #define COM7_FMT_QVGA 0x10 /* QVGA format */ | |
75 | #define COM7_FMT_QCIF 0x08 /* QCIF format */ | |
76 | #define COM7_RGB 0x04 /* bits 0 and 2 - RGB format */ | |
77 | #define COM7_YUV 0x00 /* YUV */ | |
78 | #define COM7_BAYER 0x01 /* Bayer format */ | |
79 | #define COM7_PBAYER 0x05 /* "Processed bayer" */ | |
80 | #define REG_COM8 0x13 /* Control 8 */ | |
81 | #define COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */ | |
82 | #define COM8_AECSTEP 0x40 /* Unlimited AEC step size */ | |
83 | #define COM8_BFILT 0x20 /* Band filter enable */ | |
84 | #define COM8_AGC 0x04 /* Auto gain enable */ | |
85 | #define COM8_AWB 0x02 /* White balance enable */ | |
86 | #define COM8_AEC 0x01 /* Auto exposure enable */ | |
87 | #define REG_COM9 0x14 /* Control 9 - gain ceiling */ | |
88 | #define REG_COM10 0x15 /* Control 10 */ | |
89 | #define COM10_HSYNC 0x40 /* HSYNC instead of HREF */ | |
90 | #define COM10_PCLK_HB 0x20 /* Suppress PCLK on horiz blank */ | |
91 | #define COM10_HREF_REV 0x08 /* Reverse HREF */ | |
92 | #define COM10_VS_LEAD 0x04 /* VSYNC on clock leading edge */ | |
93 | #define COM10_VS_NEG 0x02 /* VSYNC negative */ | |
94 | #define COM10_HS_NEG 0x01 /* HSYNC negative */ | |
95 | #define REG_HSTART 0x17 /* Horiz start high bits */ | |
96 | #define REG_HSTOP 0x18 /* Horiz stop high bits */ | |
97 | #define REG_VSTART 0x19 /* Vert start high bits */ | |
98 | #define REG_VSTOP 0x1a /* Vert stop high bits */ | |
99 | #define REG_PSHFT 0x1b /* Pixel delay after HREF */ | |
100 | #define REG_MIDH 0x1c /* Manuf. ID high */ | |
101 | #define REG_MIDL 0x1d /* Manuf. ID low */ | |
102 | #define REG_MVFP 0x1e /* Mirror / vflip */ | |
103 | #define MVFP_MIRROR 0x20 /* Mirror image */ | |
104 | #define MVFP_FLIP 0x10 /* Vertical flip */ | |
105 | ||
106 | #define REG_AEW 0x24 /* AGC upper limit */ | |
107 | #define REG_AEB 0x25 /* AGC lower limit */ | |
108 | #define REG_VPT 0x26 /* AGC/AEC fast mode op region */ | |
109 | #define REG_HSYST 0x30 /* HSYNC rising edge delay */ | |
110 | #define REG_HSYEN 0x31 /* HSYNC falling edge delay */ | |
111 | #define REG_HREF 0x32 /* HREF pieces */ | |
112 | #define REG_TSLB 0x3a /* lots of stuff */ | |
113 | #define TSLB_YLAST 0x04 /* UYVY or VYUY - see com13 */ | |
114 | #define REG_COM11 0x3b /* Control 11 */ | |
115 | #define COM11_NIGHT 0x80 /* NIght mode enable */ | |
116 | #define COM11_NMFR 0x60 /* Two bit NM frame rate */ | |
117 | #define COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */ | |
118 | #define COM11_50HZ 0x08 /* Manual 50Hz select */ | |
119 | #define COM11_EXP 0x02 | |
120 | #define REG_COM12 0x3c /* Control 12 */ | |
121 | #define COM12_HREF 0x80 /* HREF always */ | |
122 | #define REG_COM13 0x3d /* Control 13 */ | |
123 | #define COM13_GAMMA 0x80 /* Gamma enable */ | |
124 | #define COM13_UVSAT 0x40 /* UV saturation auto adjustment */ | |
125 | #define COM13_UVSWAP 0x01 /* V before U - w/TSLB */ | |
126 | #define REG_COM14 0x3e /* Control 14 */ | |
127 | #define COM14_DCWEN 0x10 /* DCW/PCLK-scale enable */ | |
128 | #define REG_EDGE 0x3f /* Edge enhancement factor */ | |
129 | #define REG_COM15 0x40 /* Control 15 */ | |
130 | #define COM15_R10F0 0x00 /* Data range 10 to F0 */ | |
131 | #define COM15_R01FE 0x80 /* 01 to FE */ | |
132 | #define COM15_R00FF 0xc0 /* 00 to FF */ | |
133 | #define COM15_RGB565 0x10 /* RGB565 output */ | |
134 | #define COM15_RGB555 0x30 /* RGB555 output */ | |
135 | #define REG_COM16 0x41 /* Control 16 */ | |
136 | #define COM16_AWBGAIN 0x08 /* AWB gain enable */ | |
137 | #define REG_COM17 0x42 /* Control 17 */ | |
138 | #define COM17_AECWIN 0xc0 /* AEC window - must match COM4 */ | |
139 | #define COM17_CBAR 0x08 /* DSP Color bar */ | |
140 | ||
f9a76156 JC |
141 | /* |
142 | * This matrix defines how the colors are generated, must be | |
143 | * tweaked to adjust hue and saturation. | |
144 | * | |
145 | * Order: v-red, v-green, v-blue, u-red, u-green, u-blue | |
146 | * | |
147 | * They are nine-bit signed quantities, with the sign bit | |
148 | * stored in 0x58. Sign for v-red is bit 0, and up from there. | |
149 | */ | |
150 | #define REG_CMATRIX_BASE 0x4f | |
151 | #define CMATRIX_LEN 6 | |
152 | #define REG_CMATRIX_SIGN 0x58 | |
153 | ||
154 | ||
111f3356 JC |
155 | #define REG_BRIGHT 0x55 /* Brightness */ |
156 | #define REG_CONTRAS 0x56 /* Contrast control */ | |
157 | ||
158 | #define REG_GFIX 0x69 /* Fix gain control */ | |
159 | ||
160 | #define REG_RGB444 0x8c /* RGB 444 control */ | |
161 | #define R444_ENABLE 0x02 /* Turn on RGB444, overrides 5x5 */ | |
162 | #define R444_RGBX 0x01 /* Empty nibble at end */ | |
163 | ||
164 | #define REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */ | |
165 | #define REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */ | |
166 | ||
167 | #define REG_BD50MAX 0xa5 /* 50hz banding step limit */ | |
168 | #define REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */ | |
169 | #define REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */ | |
170 | #define REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */ | |
171 | #define REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */ | |
172 | #define REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */ | |
173 | #define REG_BD60MAX 0xab /* 60hz banding step limit */ | |
174 | ||
175 | ||
f9a76156 JC |
176 | /* |
177 | * Information we maintain about a known sensor. | |
178 | */ | |
179 | struct ov7670_format_struct; /* coming later */ | |
180 | struct ov7670_info { | |
181 | struct ov7670_format_struct *fmt; /* Current format */ | |
182 | unsigned char sat; /* Saturation value */ | |
183 | int hue; /* Hue value */ | |
184 | }; | |
185 | ||
186 | ||
187 | ||
188 | ||
111f3356 JC |
189 | /* |
190 | * The default register settings, as obtained from OmniVision. There | |
191 | * is really no making sense of most of these - lots of "reserved" values | |
192 | * and such. | |
193 | * | |
194 | * These settings give VGA YUYV. | |
195 | */ | |
196 | ||
197 | struct regval_list { | |
198 | unsigned char reg_num; | |
199 | unsigned char value; | |
200 | }; | |
201 | ||
202 | static struct regval_list ov7670_default_regs[] = { | |
203 | { REG_COM7, COM7_RESET }, | |
204 | /* | |
205 | * Clock scale: 3 = 15fps | |
206 | * 2 = 20fps | |
207 | * 1 = 30fps | |
208 | */ | |
f9a76156 | 209 | { REG_CLKRC, 0x1 }, /* OV: clock scale (30 fps) */ |
111f3356 JC |
210 | { REG_TSLB, 0x04 }, /* OV */ |
211 | { REG_COM7, 0 }, /* VGA */ | |
212 | /* | |
213 | * Set the hardware window. These values from OV don't entirely | |
214 | * make sense - hstop is less than hstart. But they work... | |
215 | */ | |
216 | { REG_HSTART, 0x13 }, { REG_HSTOP, 0x01 }, | |
217 | { REG_HREF, 0xb6 }, { REG_VSTART, 0x02 }, | |
218 | { REG_VSTOP, 0x7a }, { REG_VREF, 0x0a }, | |
219 | ||
220 | { REG_COM3, 0 }, { REG_COM14, 0 }, | |
221 | /* Mystery scaling numbers */ | |
222 | { 0x70, 0x3a }, { 0x71, 0x35 }, | |
223 | { 0x72, 0x11 }, { 0x73, 0xf0 }, | |
224 | { 0xa2, 0x02 }, { REG_COM10, 0x0 }, | |
225 | ||
226 | /* Gamma curve values */ | |
227 | { 0x7a, 0x20 }, { 0x7b, 0x10 }, | |
228 | { 0x7c, 0x1e }, { 0x7d, 0x35 }, | |
229 | { 0x7e, 0x5a }, { 0x7f, 0x69 }, | |
230 | { 0x80, 0x76 }, { 0x81, 0x80 }, | |
231 | { 0x82, 0x88 }, { 0x83, 0x8f }, | |
232 | { 0x84, 0x96 }, { 0x85, 0xa3 }, | |
233 | { 0x86, 0xaf }, { 0x87, 0xc4 }, | |
234 | { 0x88, 0xd7 }, { 0x89, 0xe8 }, | |
235 | ||
236 | /* AGC and AEC parameters. Note we start by disabling those features, | |
237 | then turn them only after tweaking the values. */ | |
238 | { REG_COM8, COM8_FASTAEC | COM8_AECSTEP | COM8_BFILT }, | |
239 | { REG_GAIN, 0 }, { REG_AECH, 0 }, | |
240 | { REG_COM4, 0x40 }, /* magic reserved bit */ | |
241 | { REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */ | |
242 | { REG_BD50MAX, 0x05 }, { REG_BD60MAX, 0x07 }, | |
243 | { REG_AEW, 0x95 }, { REG_AEB, 0x33 }, | |
244 | { REG_VPT, 0xe3 }, { REG_HAECC1, 0x78 }, | |
245 | { REG_HAECC2, 0x68 }, { 0xa1, 0x03 }, /* magic */ | |
246 | { REG_HAECC3, 0xd8 }, { REG_HAECC4, 0xd8 }, | |
247 | { REG_HAECC5, 0xf0 }, { REG_HAECC6, 0x90 }, | |
248 | { REG_HAECC7, 0x94 }, | |
249 | { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC }, | |
250 | ||
251 | /* Almost all of these are magic "reserved" values. */ | |
252 | { REG_COM5, 0x61 }, { REG_COM6, 0x4b }, | |
253 | { 0x16, 0x02 }, { REG_MVFP, 0x07|MVFP_MIRROR }, | |
254 | { 0x21, 0x02 }, { 0x22, 0x91 }, | |
255 | { 0x29, 0x07 }, { 0x33, 0x0b }, | |
256 | { 0x35, 0x0b }, { 0x37, 0x1d }, | |
257 | { 0x38, 0x71 }, { 0x39, 0x2a }, | |
258 | { REG_COM12, 0x78 }, { 0x4d, 0x40 }, | |
259 | { 0x4e, 0x20 }, { REG_GFIX, 0 }, | |
260 | { 0x6b, 0x4a }, { 0x74, 0x10 }, | |
261 | { 0x8d, 0x4f }, { 0x8e, 0 }, | |
262 | { 0x8f, 0 }, { 0x90, 0 }, | |
263 | { 0x91, 0 }, { 0x96, 0 }, | |
264 | { 0x9a, 0 }, { 0xb0, 0x84 }, | |
265 | { 0xb1, 0x0c }, { 0xb2, 0x0e }, | |
266 | { 0xb3, 0x82 }, { 0xb8, 0x0a }, | |
267 | ||
268 | /* More reserved magic, some of which tweaks white balance */ | |
269 | { 0x43, 0x0a }, { 0x44, 0xf0 }, | |
270 | { 0x45, 0x34 }, { 0x46, 0x58 }, | |
271 | { 0x47, 0x28 }, { 0x48, 0x3a }, | |
272 | { 0x59, 0x88 }, { 0x5a, 0x88 }, | |
273 | { 0x5b, 0x44 }, { 0x5c, 0x67 }, | |
274 | { 0x5d, 0x49 }, { 0x5e, 0x0e }, | |
275 | { 0x6c, 0x0a }, { 0x6d, 0x55 }, | |
276 | { 0x6e, 0x11 }, { 0x6f, 0x9f }, /* "9e for advance AWB" */ | |
277 | { 0x6a, 0x40 }, { REG_BLUE, 0x40 }, | |
278 | { REG_RED, 0x60 }, | |
279 | { REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC|COM8_AWB }, | |
280 | ||
281 | /* Matrix coefficients */ | |
282 | { 0x4f, 0x80 }, { 0x50, 0x80 }, | |
283 | { 0x51, 0 }, { 0x52, 0x22 }, | |
284 | { 0x53, 0x5e }, { 0x54, 0x80 }, | |
285 | { 0x58, 0x9e }, | |
286 | ||
287 | { REG_COM16, COM16_AWBGAIN }, { REG_EDGE, 0 }, | |
288 | { 0x75, 0x05 }, { 0x76, 0xe1 }, | |
289 | { 0x4c, 0 }, { 0x77, 0x01 }, | |
290 | { REG_COM13, 0xc3 }, { 0x4b, 0x09 }, | |
291 | { 0xc9, 0x60 }, { REG_COM16, 0x38 }, | |
292 | { 0x56, 0x40 }, | |
293 | ||
294 | { 0x34, 0x11 }, { REG_COM11, COM11_EXP }, | |
295 | { 0xa4, 0x88 }, { 0x96, 0 }, | |
296 | { 0x97, 0x30 }, { 0x98, 0x20 }, | |
297 | { 0x99, 0x30 }, { 0x9a, 0x84 }, | |
298 | { 0x9b, 0x29 }, { 0x9c, 0x03 }, | |
299 | { 0x9d, 0x4c }, { 0x9e, 0x3f }, | |
300 | { 0x78, 0x04 }, | |
301 | ||
302 | /* Extra-weird stuff. Some sort of multiplexor register */ | |
303 | { 0x79, 0x01 }, { 0xc8, 0xf0 }, | |
304 | { 0x79, 0x0f }, { 0xc8, 0x00 }, | |
305 | { 0x79, 0x10 }, { 0xc8, 0x7e }, | |
306 | { 0x79, 0x0a }, { 0xc8, 0x80 }, | |
307 | { 0x79, 0x0b }, { 0xc8, 0x01 }, | |
308 | { 0x79, 0x0c }, { 0xc8, 0x0f }, | |
309 | { 0x79, 0x0d }, { 0xc8, 0x20 }, | |
310 | { 0x79, 0x09 }, { 0xc8, 0x80 }, | |
311 | { 0x79, 0x02 }, { 0xc8, 0xc0 }, | |
312 | { 0x79, 0x03 }, { 0xc8, 0x40 }, | |
313 | { 0x79, 0x05 }, { 0xc8, 0x30 }, | |
314 | { 0x79, 0x26 }, | |
315 | ||
111f3356 JC |
316 | { 0xff, 0xff }, /* END MARKER */ |
317 | }; | |
318 | ||
319 | ||
320 | /* | |
321 | * Here we'll try to encapsulate the changes for just the output | |
322 | * video format. | |
323 | * | |
324 | * RGB656 and YUV422 come from OV; RGB444 is homebrewed. | |
325 | * | |
326 | * IMPORTANT RULE: the first entry must be for COM7, see ov7670_s_fmt for why. | |
327 | */ | |
328 | ||
329 | ||
330 | static struct regval_list ov7670_fmt_yuv422[] = { | |
331 | { REG_COM7, 0x0 }, /* Selects YUV mode */ | |
332 | { REG_RGB444, 0 }, /* No RGB444 please */ | |
333 | { REG_COM1, 0 }, | |
334 | { REG_COM15, COM15_R00FF }, | |
335 | { REG_COM9, 0x18 }, /* 4x gain ceiling; 0x8 is reserved bit */ | |
336 | { 0x4f, 0x80 }, /* "matrix coefficient 1" */ | |
337 | { 0x50, 0x80 }, /* "matrix coefficient 2" */ | |
f9a76156 | 338 | { 0x51, 0 }, /* vb */ |
111f3356 JC |
339 | { 0x52, 0x22 }, /* "matrix coefficient 4" */ |
340 | { 0x53, 0x5e }, /* "matrix coefficient 5" */ | |
341 | { 0x54, 0x80 }, /* "matrix coefficient 6" */ | |
342 | { REG_COM13, COM13_GAMMA|COM13_UVSAT }, | |
343 | { 0xff, 0xff }, | |
344 | }; | |
345 | ||
346 | static struct regval_list ov7670_fmt_rgb565[] = { | |
347 | { REG_COM7, COM7_RGB }, /* Selects RGB mode */ | |
348 | { REG_RGB444, 0 }, /* No RGB444 please */ | |
349 | { REG_COM1, 0x0 }, | |
350 | { REG_COM15, COM15_RGB565 }, | |
351 | { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */ | |
352 | { 0x4f, 0xb3 }, /* "matrix coefficient 1" */ | |
353 | { 0x50, 0xb3 }, /* "matrix coefficient 2" */ | |
f9a76156 | 354 | { 0x51, 0 }, /* vb */ |
111f3356 JC |
355 | { 0x52, 0x3d }, /* "matrix coefficient 4" */ |
356 | { 0x53, 0xa7 }, /* "matrix coefficient 5" */ | |
357 | { 0x54, 0xe4 }, /* "matrix coefficient 6" */ | |
358 | { REG_COM13, COM13_GAMMA|COM13_UVSAT }, | |
359 | { 0xff, 0xff }, | |
360 | }; | |
361 | ||
362 | static struct regval_list ov7670_fmt_rgb444[] = { | |
363 | { REG_COM7, COM7_RGB }, /* Selects RGB mode */ | |
364 | { REG_RGB444, R444_ENABLE }, /* Enable xxxxrrrr ggggbbbb */ | |
365 | { REG_COM1, 0x40 }, /* Magic reserved bit */ | |
366 | { REG_COM15, COM15_R01FE|COM15_RGB565 }, /* Data range needed? */ | |
367 | { REG_COM9, 0x38 }, /* 16x gain ceiling; 0x8 is reserved bit */ | |
368 | { 0x4f, 0xb3 }, /* "matrix coefficient 1" */ | |
369 | { 0x50, 0xb3 }, /* "matrix coefficient 2" */ | |
f9a76156 | 370 | { 0x51, 0 }, /* vb */ |
111f3356 JC |
371 | { 0x52, 0x3d }, /* "matrix coefficient 4" */ |
372 | { 0x53, 0xa7 }, /* "matrix coefficient 5" */ | |
373 | { 0x54, 0xe4 }, /* "matrix coefficient 6" */ | |
374 | { REG_COM13, COM13_GAMMA|COM13_UVSAT|0x2 }, /* Magic rsvd bit */ | |
375 | { 0xff, 0xff }, | |
376 | }; | |
377 | ||
378 | ||
379 | ||
380 | ||
381 | /* | |
382 | * Low-level register I/O. | |
383 | */ | |
384 | ||
385 | static int ov7670_read(struct i2c_client *c, unsigned char reg, | |
386 | unsigned char *value) | |
387 | { | |
388 | int ret; | |
389 | ||
390 | ret = i2c_smbus_read_byte_data(c, reg); | |
f9a76156 | 391 | if (ret >= 0) |
111f3356 JC |
392 | *value = (unsigned char) ret; |
393 | return ret; | |
394 | } | |
395 | ||
396 | ||
397 | static int ov7670_write(struct i2c_client *c, unsigned char reg, | |
398 | unsigned char value) | |
399 | { | |
400 | return i2c_smbus_write_byte_data(c, reg, value); | |
401 | } | |
402 | ||
403 | ||
404 | /* | |
405 | * Write a list of register settings; ff/ff stops the process. | |
406 | */ | |
407 | static int ov7670_write_array(struct i2c_client *c, struct regval_list *vals) | |
408 | { | |
409 | while (vals->reg_num != 0xff || vals->value != 0xff) { | |
410 | int ret = ov7670_write(c, vals->reg_num, vals->value); | |
411 | if (ret < 0) | |
412 | return ret; | |
413 | vals++; | |
414 | } | |
415 | return 0; | |
416 | } | |
417 | ||
418 | ||
419 | /* | |
420 | * Stuff that knows about the sensor. | |
421 | */ | |
422 | static void ov7670_reset(struct i2c_client *client) | |
423 | { | |
424 | ov7670_write(client, REG_COM7, COM7_RESET); | |
425 | msleep(1); | |
426 | } | |
427 | ||
428 | ||
429 | static int ov7670_init(struct i2c_client *client) | |
430 | { | |
431 | return ov7670_write_array(client, ov7670_default_regs); | |
432 | } | |
433 | ||
434 | ||
435 | ||
436 | static int ov7670_detect(struct i2c_client *client) | |
437 | { | |
438 | unsigned char v; | |
439 | int ret; | |
440 | ||
441 | ret = ov7670_init(client); | |
442 | if (ret < 0) | |
443 | return ret; | |
444 | ret = ov7670_read(client, REG_MIDH, &v); | |
445 | if (ret < 0) | |
446 | return ret; | |
447 | if (v != 0x7f) /* OV manuf. id. */ | |
448 | return -ENODEV; | |
449 | ret = ov7670_read(client, REG_MIDL, &v); | |
450 | if (ret < 0) | |
451 | return ret; | |
452 | if (v != 0xa2) | |
453 | return -ENODEV; | |
454 | /* | |
455 | * OK, we know we have an OmniVision chip...but which one? | |
456 | */ | |
457 | ret = ov7670_read(client, REG_PID, &v); | |
458 | if (ret < 0) | |
459 | return ret; | |
460 | if (v != 0x76) /* PID + VER = 0x76 / 0x73 */ | |
461 | return -ENODEV; | |
462 | ret = ov7670_read(client, REG_VER, &v); | |
463 | if (ret < 0) | |
464 | return ret; | |
465 | if (v != 0x73) /* PID + VER = 0x76 / 0x73 */ | |
466 | return -ENODEV; | |
467 | return 0; | |
468 | } | |
469 | ||
470 | ||
f9a76156 JC |
471 | /* |
472 | * Store information about the video data format. The color matrix | |
473 | * is deeply tied into the format, so keep the relevant values here. | |
474 | * The magic matrix nubmers come from OmniVision. | |
475 | */ | |
111f3356 JC |
476 | static struct ov7670_format_struct { |
477 | __u8 *desc; | |
478 | __u32 pixelformat; | |
479 | struct regval_list *regs; | |
f9a76156 | 480 | int cmatrix[CMATRIX_LEN]; |
111f3356 JC |
481 | } ov7670_formats[] = { |
482 | { | |
483 | .desc = "YUYV 4:2:2", | |
484 | .pixelformat = V4L2_PIX_FMT_YUYV, | |
485 | .regs = ov7670_fmt_yuv422, | |
f9a76156 | 486 | .cmatrix = { 128, -128, 0, -34, -94, 128 }, |
111f3356 JC |
487 | }, |
488 | { | |
489 | .desc = "RGB 444", | |
490 | .pixelformat = V4L2_PIX_FMT_RGB444, | |
491 | .regs = ov7670_fmt_rgb444, | |
f9a76156 | 492 | .cmatrix = { 179, -179, 0, -61, -176, 228 }, |
111f3356 JC |
493 | }, |
494 | { | |
495 | .desc = "RGB 565", | |
496 | .pixelformat = V4L2_PIX_FMT_RGB565, | |
497 | .regs = ov7670_fmt_rgb565, | |
f9a76156 | 498 | .cmatrix = { 179, -179, 0, -61, -176, 228 }, |
111f3356 | 499 | }, |
111f3356 JC |
500 | }; |
501 | #define N_OV7670_FMTS (sizeof(ov7670_formats)/sizeof(ov7670_formats[0])) | |
502 | ||
503 | /* | |
504 | * All formats we support are 2 bytes/pixel. | |
505 | */ | |
506 | #define BYTES_PER_PIXEL 2 | |
507 | ||
508 | /* | |
509 | * Then there is the issue of window sizes. Try to capture the info here. | |
510 | */ | |
f9a76156 JC |
511 | |
512 | /* | |
513 | * QCIF mode is done (by OV) in a very strange way - it actually looks like | |
514 | * VGA with weird scaling options - they do *not* use the canned QCIF mode | |
515 | * which is allegedly provided by the sensor. So here's the weird register | |
516 | * settings. | |
517 | */ | |
518 | static struct regval_list ov7670_qcif_regs[] = { | |
519 | { REG_COM3, COM3_SCALEEN|COM3_DCWEN }, | |
520 | { REG_COM3, COM3_DCWEN }, | |
521 | { REG_COM14, COM14_DCWEN | 0x01}, | |
522 | { 0x73, 0xf1 }, | |
523 | { 0xa2, 0x52 }, | |
524 | { 0x7b, 0x1c }, | |
525 | { 0x7c, 0x28 }, | |
526 | { 0x7d, 0x3c }, | |
527 | { 0x7f, 0x69 }, | |
528 | { REG_COM9, 0x38 }, | |
529 | { 0xa1, 0x0b }, | |
530 | { 0x74, 0x19 }, | |
531 | { 0x9a, 0x80 }, | |
532 | { 0x43, 0x14 }, | |
533 | { REG_COM13, 0xc0 }, | |
534 | { 0xff, 0xff }, | |
535 | }; | |
536 | ||
111f3356 JC |
537 | static struct ov7670_win_size { |
538 | int width; | |
539 | int height; | |
540 | unsigned char com7_bit; | |
541 | int hstart; /* Start/stop values for the camera. Note */ | |
542 | int hstop; /* that they do not always make complete */ | |
543 | int vstart; /* sense to humans, but evidently the sensor */ | |
544 | int vstop; /* will do the right thing... */ | |
f9a76156 | 545 | struct regval_list *regs; /* Regs to tweak */ |
111f3356 JC |
546 | /* h/vref stuff */ |
547 | } ov7670_win_sizes[] = { | |
548 | /* VGA */ | |
549 | { | |
550 | .width = VGA_WIDTH, | |
551 | .height = VGA_HEIGHT, | |
552 | .com7_bit = COM7_FMT_VGA, | |
553 | .hstart = 158, /* These values from */ | |
554 | .hstop = 14, /* Omnivision */ | |
555 | .vstart = 10, | |
556 | .vstop = 490, | |
f9a76156 | 557 | .regs = NULL, |
111f3356 JC |
558 | }, |
559 | /* CIF */ | |
560 | { | |
561 | .width = CIF_WIDTH, | |
562 | .height = CIF_HEIGHT, | |
563 | .com7_bit = COM7_FMT_CIF, | |
564 | .hstart = 170, /* Empirically determined */ | |
565 | .hstop = 90, | |
566 | .vstart = 14, | |
567 | .vstop = 494, | |
f9a76156 | 568 | .regs = NULL, |
111f3356 JC |
569 | }, |
570 | /* QVGA */ | |
571 | { | |
572 | .width = QVGA_WIDTH, | |
573 | .height = QVGA_HEIGHT, | |
574 | .com7_bit = COM7_FMT_QVGA, | |
575 | .hstart = 164, /* Empirically determined */ | |
576 | .hstop = 20, | |
577 | .vstart = 14, | |
578 | .vstop = 494, | |
f9a76156 JC |
579 | .regs = NULL, |
580 | }, | |
581 | /* QCIF */ | |
582 | { | |
583 | .width = QCIF_WIDTH, | |
584 | .height = QCIF_HEIGHT, | |
585 | .com7_bit = COM7_FMT_VGA, /* see comment above */ | |
586 | .hstart = 456, /* Empirically determined */ | |
587 | .hstop = 24, | |
588 | .vstart = 14, | |
589 | .vstop = 494, | |
590 | .regs = ov7670_qcif_regs, | |
111f3356 JC |
591 | }, |
592 | }; | |
593 | ||
594 | #define N_WIN_SIZES (sizeof(ov7670_win_sizes)/sizeof(ov7670_win_sizes[0])) | |
595 | ||
596 | ||
597 | /* | |
598 | * Store a set of start/stop values into the camera. | |
599 | */ | |
600 | static int ov7670_set_hw(struct i2c_client *client, int hstart, int hstop, | |
601 | int vstart, int vstop) | |
602 | { | |
603 | int ret; | |
604 | unsigned char v; | |
605 | /* | |
606 | * Horizontal: 11 bits, top 8 live in hstart and hstop. Bottom 3 of | |
607 | * hstart are in href[2:0], bottom 3 of hstop in href[5:3]. There is | |
608 | * a mystery "edge offset" value in the top two bits of href. | |
609 | */ | |
610 | ret = ov7670_write(client, REG_HSTART, (hstart >> 3) & 0xff); | |
611 | ret += ov7670_write(client, REG_HSTOP, (hstop >> 3) & 0xff); | |
612 | ret += ov7670_read(client, REG_HREF, &v); | |
613 | v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7); | |
614 | msleep(10); | |
615 | ret += ov7670_write(client, REG_HREF, v); | |
616 | /* | |
617 | * Vertical: similar arrangement, but only 10 bits. | |
618 | */ | |
619 | ret += ov7670_write(client, REG_VSTART, (vstart >> 2) & 0xff); | |
620 | ret += ov7670_write(client, REG_VSTOP, (vstop >> 2) & 0xff); | |
621 | ret += ov7670_read(client, REG_VREF, &v); | |
622 | v = (v & 0xf0) | ((vstop & 0x3) << 2) | (vstart & 0x3); | |
623 | msleep(10); | |
624 | ret += ov7670_write(client, REG_VREF, v); | |
625 | return ret; | |
626 | } | |
627 | ||
628 | ||
629 | static int ov7670_enum_fmt(struct i2c_client *c, struct v4l2_fmtdesc *fmt) | |
630 | { | |
631 | struct ov7670_format_struct *ofmt; | |
632 | ||
633 | if (fmt->index >= N_OV7670_FMTS) | |
634 | return -EINVAL; | |
635 | ||
636 | ofmt = ov7670_formats + fmt->index; | |
637 | fmt->flags = 0; | |
638 | strcpy(fmt->description, ofmt->desc); | |
639 | fmt->pixelformat = ofmt->pixelformat; | |
640 | return 0; | |
641 | } | |
642 | ||
643 | ||
644 | static int ov7670_try_fmt(struct i2c_client *c, struct v4l2_format *fmt, | |
645 | struct ov7670_format_struct **ret_fmt, | |
646 | struct ov7670_win_size **ret_wsize) | |
647 | { | |
648 | int index; | |
649 | struct ov7670_win_size *wsize; | |
650 | struct v4l2_pix_format *pix = &fmt->fmt.pix; | |
651 | ||
652 | for (index = 0; index < N_OV7670_FMTS; index++) | |
653 | if (ov7670_formats[index].pixelformat == pix->pixelformat) | |
654 | break; | |
655 | if (index >= N_OV7670_FMTS) | |
656 | return -EINVAL; | |
657 | if (ret_fmt != NULL) | |
658 | *ret_fmt = ov7670_formats + index; | |
659 | /* | |
660 | * Fields: the OV devices claim to be progressive. | |
661 | */ | |
662 | if (pix->field == V4L2_FIELD_ANY) | |
663 | pix->field = V4L2_FIELD_NONE; | |
664 | else if (pix->field != V4L2_FIELD_NONE) | |
665 | return -EINVAL; | |
666 | /* | |
667 | * Round requested image size down to the nearest | |
668 | * we support, but not below the smallest. | |
669 | */ | |
670 | for (wsize = ov7670_win_sizes; wsize < ov7670_win_sizes + N_WIN_SIZES; | |
671 | wsize++) | |
672 | if (pix->width >= wsize->width && pix->height >= wsize->height) | |
673 | break; | |
f9a76156 | 674 | if (wsize >= ov7670_win_sizes + N_WIN_SIZES) |
111f3356 JC |
675 | wsize--; /* Take the smallest one */ |
676 | if (ret_wsize != NULL) | |
677 | *ret_wsize = wsize; | |
678 | /* | |
679 | * Note the size we'll actually handle. | |
680 | */ | |
681 | pix->width = wsize->width; | |
682 | pix->height = wsize->height; | |
683 | pix->bytesperline = pix->width*BYTES_PER_PIXEL; | |
111f3356 JC |
684 | pix->sizeimage = pix->height*pix->bytesperline; |
685 | return 0; | |
111f3356 JC |
686 | } |
687 | ||
688 | /* | |
689 | * Set a format. | |
690 | */ | |
691 | static int ov7670_s_fmt(struct i2c_client *c, struct v4l2_format *fmt) | |
692 | { | |
693 | int ret; | |
694 | struct ov7670_format_struct *ovfmt; | |
695 | struct ov7670_win_size *wsize; | |
f9a76156 | 696 | struct ov7670_info *info = i2c_get_clientdata(c); |
111f3356 JC |
697 | unsigned char com7; |
698 | ||
699 | ret = ov7670_try_fmt(c, fmt, &ovfmt, &wsize); | |
700 | if (ret) | |
701 | return ret; | |
702 | /* | |
703 | * COM7 is a pain in the ass, it doesn't like to be read then | |
704 | * quickly written afterward. But we have everything we need | |
705 | * to set it absolutely here, as long as the format-specific | |
706 | * register sets list it first. | |
707 | */ | |
708 | com7 = ovfmt->regs[0].value; | |
709 | com7 |= wsize->com7_bit; | |
710 | ov7670_write(c, REG_COM7, com7); | |
711 | /* | |
712 | * Now write the rest of the array. Also store start/stops | |
713 | */ | |
714 | ov7670_write_array(c, ovfmt->regs + 1); | |
715 | ov7670_set_hw(c, wsize->hstart, wsize->hstop, wsize->vstart, | |
716 | wsize->vstop); | |
f9a76156 JC |
717 | ret = 0; |
718 | if (wsize->regs) | |
719 | ret = ov7670_write_array(c, wsize->regs); | |
720 | info->fmt = ovfmt; | |
111f3356 JC |
721 | return 0; |
722 | } | |
723 | ||
724 | /* | |
725 | * Code for dealing with controls. | |
726 | */ | |
727 | ||
f9a76156 JC |
728 | |
729 | ||
730 | ||
731 | ||
732 | static int ov7670_store_cmatrix(struct i2c_client *client, | |
733 | int matrix[CMATRIX_LEN]) | |
734 | { | |
735 | int i, ret; | |
736 | unsigned char signbits; | |
737 | ||
738 | /* | |
739 | * Weird crap seems to exist in the upper part of | |
740 | * the sign bits register, so let's preserve it. | |
741 | */ | |
742 | ret = ov7670_read(client, REG_CMATRIX_SIGN, &signbits); | |
743 | signbits &= 0xc0; | |
744 | ||
745 | for (i = 0; i < CMATRIX_LEN; i++) { | |
746 | unsigned char raw; | |
747 | ||
748 | if (matrix[i] < 0) { | |
749 | signbits |= (1 << i); | |
750 | if (matrix[i] < -255) | |
751 | raw = 0xff; | |
752 | else | |
753 | raw = (-1 * matrix[i]) & 0xff; | |
754 | } | |
755 | else { | |
756 | if (matrix[i] > 255) | |
757 | raw = 0xff; | |
758 | else | |
759 | raw = matrix[i] & 0xff; | |
760 | } | |
761 | ret += ov7670_write(client, REG_CMATRIX_BASE + i, raw); | |
762 | } | |
763 | ret += ov7670_write(client, REG_CMATRIX_SIGN, signbits); | |
764 | return ret; | |
765 | } | |
766 | ||
767 | ||
768 | /* | |
769 | * Hue also requires messing with the color matrix. It also requires | |
770 | * trig functions, which tend not to be well supported in the kernel. | |
771 | * So here is a simple table of sine values, 0-90 degrees, in steps | |
772 | * of five degrees. Values are multiplied by 1000. | |
773 | * | |
774 | * The following naive approximate trig functions require an argument | |
775 | * carefully limited to -180 <= theta <= 180. | |
776 | */ | |
777 | #define SIN_STEP 5 | |
778 | static const int ov7670_sin_table[] = { | |
779 | 0, 87, 173, 258, 342, 422, | |
780 | 499, 573, 642, 707, 766, 819, | |
781 | 866, 906, 939, 965, 984, 996, | |
782 | 1000 | |
783 | }; | |
784 | ||
785 | static int ov7670_sine(int theta) | |
786 | { | |
787 | int chs = 1; | |
788 | int sine; | |
789 | ||
790 | if (theta < 0) { | |
791 | theta = -theta; | |
792 | chs = -1; | |
793 | } | |
794 | if (theta <= 90) | |
795 | sine = ov7670_sin_table[theta/SIN_STEP]; | |
796 | else { | |
797 | theta -= 90; | |
798 | sine = 1000 - ov7670_sin_table[theta/SIN_STEP]; | |
799 | } | |
800 | return sine*chs; | |
801 | } | |
802 | ||
803 | static int ov7670_cosine(int theta) | |
804 | { | |
805 | theta = 90 - theta; | |
806 | if (theta > 180) | |
807 | theta -= 360; | |
808 | else if (theta < -180) | |
809 | theta += 360; | |
810 | return ov7670_sine(theta); | |
811 | } | |
812 | ||
813 | ||
814 | ||
815 | ||
816 | static void ov7670_calc_cmatrix(struct ov7670_info *info, | |
817 | int matrix[CMATRIX_LEN]) | |
818 | { | |
819 | int i; | |
820 | /* | |
821 | * Apply the current saturation setting first. | |
822 | */ | |
823 | for (i = 0; i < CMATRIX_LEN; i++) | |
824 | matrix[i] = (info->fmt->cmatrix[i]*info->sat) >> 7; | |
825 | /* | |
826 | * Then, if need be, rotate the hue value. | |
827 | */ | |
828 | if (info->hue != 0) { | |
829 | int sinth, costh, tmpmatrix[CMATRIX_LEN]; | |
830 | ||
831 | memcpy(tmpmatrix, matrix, CMATRIX_LEN*sizeof(int)); | |
832 | sinth = ov7670_sine(info->hue); | |
833 | costh = ov7670_cosine(info->hue); | |
834 | ||
835 | matrix[0] = (matrix[3]*sinth + matrix[0]*costh)/1000; | |
836 | matrix[1] = (matrix[4]*sinth + matrix[1]*costh)/1000; | |
837 | matrix[2] = (matrix[5]*sinth + matrix[2]*costh)/1000; | |
838 | matrix[3] = (matrix[3]*costh - matrix[0]*sinth)/1000; | |
839 | matrix[4] = (matrix[4]*costh - matrix[1]*sinth)/1000; | |
840 | matrix[5] = (matrix[5]*costh - matrix[2]*sinth)/1000; | |
841 | } | |
842 | } | |
843 | ||
844 | ||
845 | ||
846 | static int ov7670_t_sat(struct i2c_client *client, int value) | |
847 | { | |
848 | struct ov7670_info *info = i2c_get_clientdata(client); | |
849 | int matrix[CMATRIX_LEN]; | |
850 | int ret; | |
851 | ||
852 | info->sat = value; | |
853 | ov7670_calc_cmatrix(info, matrix); | |
854 | ret = ov7670_store_cmatrix(client, matrix); | |
855 | return ret; | |
856 | } | |
857 | ||
858 | static int ov7670_q_sat(struct i2c_client *client, __s32 *value) | |
859 | { | |
860 | struct ov7670_info *info = i2c_get_clientdata(client); | |
861 | ||
862 | *value = info->sat; | |
863 | return 0; | |
864 | } | |
865 | ||
866 | static int ov7670_t_hue(struct i2c_client *client, int value) | |
867 | { | |
868 | struct ov7670_info *info = i2c_get_clientdata(client); | |
869 | int matrix[CMATRIX_LEN]; | |
870 | int ret; | |
871 | ||
872 | if (value < -180 || value > 180) | |
873 | return -EINVAL; | |
874 | info->hue = value; | |
875 | ov7670_calc_cmatrix(info, matrix); | |
876 | ret = ov7670_store_cmatrix(client, matrix); | |
877 | return ret; | |
878 | } | |
879 | ||
880 | ||
881 | static int ov7670_q_hue(struct i2c_client *client, __s32 *value) | |
882 | { | |
883 | struct ov7670_info *info = i2c_get_clientdata(client); | |
884 | ||
885 | *value = info->hue; | |
886 | return 0; | |
887 | } | |
888 | ||
889 | ||
111f3356 JC |
890 | /* |
891 | * Some weird registers seem to store values in a sign/magnitude format! | |
892 | */ | |
893 | static unsigned char ov7670_sm_to_abs(unsigned char v) | |
894 | { | |
895 | if ((v & 0x80) == 0) | |
896 | return v + 128; | |
897 | else | |
898 | return 128 - (v & 0x7f); | |
899 | } | |
900 | ||
901 | ||
902 | static unsigned char ov7670_abs_to_sm(unsigned char v) | |
903 | { | |
904 | if (v > 127) | |
905 | return v & 0x7f; | |
906 | else | |
907 | return (128 - v) | 0x80; | |
908 | } | |
909 | ||
f9a76156 | 910 | static int ov7670_t_brightness(struct i2c_client *client, int value) |
111f3356 | 911 | { |
f9a76156 | 912 | unsigned char com8, v; |
111f3356 JC |
913 | int ret; |
914 | ||
915 | ov7670_read(client, REG_COM8, &com8); | |
916 | com8 &= ~COM8_AEC; | |
917 | ov7670_write(client, REG_COM8, com8); | |
f9a76156 JC |
918 | v = ov7670_abs_to_sm(value); |
919 | ret = ov7670_write(client, REG_BRIGHT, v); | |
111f3356 JC |
920 | return ret; |
921 | } | |
922 | ||
f9a76156 | 923 | static int ov7670_q_brightness(struct i2c_client *client, __s32 *value) |
111f3356 | 924 | { |
f9a76156 JC |
925 | unsigned char v; |
926 | int ret = ov7670_read(client, REG_BRIGHT, &v); | |
927 | ||
928 | *value = ov7670_sm_to_abs(v); | |
111f3356 JC |
929 | return ret; |
930 | } | |
931 | ||
f9a76156 | 932 | static int ov7670_t_contrast(struct i2c_client *client, int value) |
111f3356 | 933 | { |
f9a76156 | 934 | return ov7670_write(client, REG_CONTRAS, (unsigned char) value); |
111f3356 JC |
935 | } |
936 | ||
f9a76156 | 937 | static int ov7670_q_contrast(struct i2c_client *client, __s32 *value) |
111f3356 | 938 | { |
f9a76156 JC |
939 | unsigned char v; |
940 | int ret = ov7670_read(client, REG_CONTRAS, &v); | |
941 | ||
942 | *value = v; | |
943 | return ret; | |
111f3356 JC |
944 | } |
945 | ||
f9a76156 | 946 | static int ov7670_q_hflip(struct i2c_client *client, __s32 *value) |
111f3356 JC |
947 | { |
948 | int ret; | |
949 | unsigned char v; | |
950 | ||
951 | ret = ov7670_read(client, REG_MVFP, &v); | |
952 | *value = (v & MVFP_MIRROR) == MVFP_MIRROR; | |
953 | return ret; | |
954 | } | |
955 | ||
956 | ||
f9a76156 | 957 | static int ov7670_t_hflip(struct i2c_client *client, int value) |
111f3356 JC |
958 | { |
959 | unsigned char v; | |
960 | int ret; | |
961 | ||
962 | ret = ov7670_read(client, REG_MVFP, &v); | |
963 | if (value) | |
964 | v |= MVFP_MIRROR; | |
965 | else | |
966 | v &= ~MVFP_MIRROR; | |
967 | msleep(10); /* FIXME */ | |
968 | ret += ov7670_write(client, REG_MVFP, v); | |
969 | return ret; | |
970 | } | |
971 | ||
972 | ||
973 | ||
f9a76156 | 974 | static int ov7670_q_vflip(struct i2c_client *client, __s32 *value) |
111f3356 JC |
975 | { |
976 | int ret; | |
977 | unsigned char v; | |
978 | ||
979 | ret = ov7670_read(client, REG_MVFP, &v); | |
980 | *value = (v & MVFP_FLIP) == MVFP_FLIP; | |
981 | return ret; | |
982 | } | |
983 | ||
984 | ||
f9a76156 | 985 | static int ov7670_t_vflip(struct i2c_client *client, int value) |
111f3356 JC |
986 | { |
987 | unsigned char v; | |
988 | int ret; | |
989 | ||
990 | ret = ov7670_read(client, REG_MVFP, &v); | |
991 | if (value) | |
992 | v |= MVFP_FLIP; | |
993 | else | |
994 | v &= ~MVFP_FLIP; | |
995 | msleep(10); /* FIXME */ | |
996 | ret += ov7670_write(client, REG_MVFP, v); | |
997 | return ret; | |
998 | } | |
999 | ||
1000 | ||
1001 | static struct ov7670_control { | |
1002 | struct v4l2_queryctrl qc; | |
f9a76156 JC |
1003 | int (*query)(struct i2c_client *c, __s32 *value); |
1004 | int (*tweak)(struct i2c_client *c, int value); | |
111f3356 JC |
1005 | } ov7670_controls[] = |
1006 | { | |
1007 | { | |
1008 | .qc = { | |
1009 | .id = V4L2_CID_BRIGHTNESS, | |
1010 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1011 | .name = "Brightness", | |
1012 | .minimum = 0, | |
1013 | .maximum = 255, | |
1014 | .step = 1, | |
1015 | .default_value = 0x80, | |
1016 | .flags = V4L2_CTRL_FLAG_SLIDER | |
1017 | }, | |
1018 | .tweak = ov7670_t_brightness, | |
1019 | .query = ov7670_q_brightness, | |
1020 | }, | |
1021 | { | |
1022 | .qc = { | |
1023 | .id = V4L2_CID_CONTRAST, | |
1024 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1025 | .name = "Contrast", | |
1026 | .minimum = 0, | |
1027 | .maximum = 127, | |
1028 | .step = 1, | |
1029 | .default_value = 0x40, /* XXX ov7670 spec */ | |
1030 | .flags = V4L2_CTRL_FLAG_SLIDER | |
1031 | }, | |
1032 | .tweak = ov7670_t_contrast, | |
1033 | .query = ov7670_q_contrast, | |
1034 | }, | |
f9a76156 JC |
1035 | { |
1036 | .qc = { | |
1037 | .id = V4L2_CID_SATURATION, | |
1038 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1039 | .name = "Saturation", | |
1040 | .minimum = 0, | |
1041 | .maximum = 256, | |
1042 | .step = 1, | |
1043 | .default_value = 0x80, | |
1044 | .flags = V4L2_CTRL_FLAG_SLIDER | |
1045 | }, | |
1046 | .tweak = ov7670_t_sat, | |
1047 | .query = ov7670_q_sat, | |
1048 | }, | |
1049 | { | |
1050 | .qc = { | |
1051 | .id = V4L2_CID_HUE, | |
1052 | .type = V4L2_CTRL_TYPE_INTEGER, | |
1053 | .name = "HUE", | |
1054 | .minimum = -180, | |
1055 | .maximum = 180, | |
1056 | .step = 5, | |
1057 | .default_value = 0, | |
1058 | .flags = V4L2_CTRL_FLAG_SLIDER | |
1059 | }, | |
1060 | .tweak = ov7670_t_hue, | |
1061 | .query = ov7670_q_hue, | |
1062 | }, | |
111f3356 JC |
1063 | { |
1064 | .qc = { | |
1065 | .id = V4L2_CID_VFLIP, | |
1066 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
1067 | .name = "Vertical flip", | |
1068 | .minimum = 0, | |
1069 | .maximum = 1, | |
1070 | .step = 1, | |
1071 | .default_value = 0, | |
1072 | }, | |
1073 | .tweak = ov7670_t_vflip, | |
1074 | .query = ov7670_q_vflip, | |
1075 | }, | |
1076 | { | |
1077 | .qc = { | |
1078 | .id = V4L2_CID_HFLIP, | |
1079 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
1080 | .name = "Horizontal mirror", | |
1081 | .minimum = 0, | |
1082 | .maximum = 1, | |
1083 | .step = 1, | |
1084 | .default_value = 0, | |
1085 | }, | |
1086 | .tweak = ov7670_t_hflip, | |
1087 | .query = ov7670_q_hflip, | |
1088 | }, | |
1089 | }; | |
1090 | #define N_CONTROLS (sizeof(ov7670_controls)/sizeof(ov7670_controls[0])) | |
1091 | ||
1092 | static struct ov7670_control *ov7670_find_control(__u32 id) | |
1093 | { | |
1094 | int i; | |
1095 | ||
1096 | for (i = 0; i < N_CONTROLS; i++) | |
1097 | if (ov7670_controls[i].qc.id == id) | |
1098 | return ov7670_controls + i; | |
1099 | return NULL; | |
1100 | } | |
1101 | ||
1102 | ||
1103 | static int ov7670_queryctrl(struct i2c_client *client, | |
1104 | struct v4l2_queryctrl *qc) | |
1105 | { | |
1106 | struct ov7670_control *ctrl = ov7670_find_control(qc->id); | |
1107 | ||
1108 | if (ctrl == NULL) | |
1109 | return -EINVAL; | |
1110 | *qc = ctrl->qc; | |
1111 | return 0; | |
1112 | } | |
1113 | ||
1114 | static int ov7670_g_ctrl(struct i2c_client *client, struct v4l2_control *ctrl) | |
1115 | { | |
1116 | struct ov7670_control *octrl = ov7670_find_control(ctrl->id); | |
1117 | int ret; | |
111f3356 JC |
1118 | |
1119 | if (octrl == NULL) | |
1120 | return -EINVAL; | |
f9a76156 JC |
1121 | ret = octrl->query(client, &ctrl->value); |
1122 | if (ret >= 0) | |
111f3356 | 1123 | return 0; |
111f3356 JC |
1124 | return ret; |
1125 | } | |
1126 | ||
1127 | static int ov7670_s_ctrl(struct i2c_client *client, struct v4l2_control *ctrl) | |
1128 | { | |
1129 | struct ov7670_control *octrl = ov7670_find_control(ctrl->id); | |
f9a76156 | 1130 | int ret; |
111f3356 JC |
1131 | |
1132 | if (octrl == NULL) | |
1133 | return -EINVAL; | |
f9a76156 JC |
1134 | ret = octrl->tweak(client, ctrl->value); |
1135 | if (ret >= 0) | |
1136 | return 0; | |
1137 | return ret; | |
111f3356 JC |
1138 | } |
1139 | ||
1140 | ||
1141 | ||
1142 | ||
1143 | ||
1144 | ||
111f3356 JC |
1145 | /* |
1146 | * Basic i2c stuff. | |
1147 | */ | |
1148 | static struct i2c_driver ov7670_driver; | |
1149 | ||
1150 | static int ov7670_attach(struct i2c_adapter *adapter) | |
1151 | { | |
1152 | int ret; | |
1153 | struct i2c_client *client; | |
f9a76156 | 1154 | struct ov7670_info *info; |
111f3356 | 1155 | |
111f3356 JC |
1156 | /* |
1157 | * For now: only deal with adapters we recognize. | |
1158 | */ | |
1159 | if (adapter->id != I2C_HW_SMBUS_CAFE) | |
1160 | return -ENODEV; | |
1161 | ||
111f3356 JC |
1162 | client = kzalloc(sizeof (struct i2c_client), GFP_KERNEL); |
1163 | if (! client) | |
1164 | return -ENOMEM; | |
1165 | client->adapter = adapter; | |
1166 | client->addr = OV7670_I2C_ADDR; | |
1167 | client->driver = &ov7670_driver, | |
1168 | strcpy(client->name, "OV7670"); | |
f9a76156 JC |
1169 | /* |
1170 | * Set up our info structure. | |
1171 | */ | |
1172 | info = kzalloc(sizeof (struct ov7670_info), GFP_KERNEL); | |
1173 | if (! info) { | |
1174 | ret = -ENOMEM; | |
1175 | goto out_free; | |
1176 | } | |
1177 | info->fmt = &ov7670_formats[0]; | |
1178 | info->sat = 128; /* Review this */ | |
1179 | i2c_set_clientdata(client, info); | |
111f3356 JC |
1180 | |
1181 | /* | |
1182 | * Make sure it's an ov7670 | |
1183 | */ | |
1184 | ret = ov7670_detect(client); | |
111f3356 | 1185 | if (ret) |
f9a76156 | 1186 | goto out_free_info; |
111f3356 JC |
1187 | i2c_attach_client(client); |
1188 | return 0; | |
1189 | ||
f9a76156 JC |
1190 | out_free_info: |
1191 | kfree(info); | |
111f3356 JC |
1192 | out_free: |
1193 | kfree(client); | |
1194 | return ret; | |
1195 | } | |
1196 | ||
1197 | ||
1198 | static int ov7670_detach(struct i2c_client *client) | |
1199 | { | |
1200 | i2c_detach_client(client); | |
f9a76156 | 1201 | kfree(i2c_get_clientdata(client)); |
111f3356 JC |
1202 | kfree(client); |
1203 | return 0; | |
1204 | } | |
1205 | ||
1206 | ||
1207 | static int ov7670_command(struct i2c_client *client, unsigned int cmd, | |
1208 | void *arg) | |
1209 | { | |
1210 | switch (cmd) { | |
1211 | case VIDIOC_INT_G_CHIP_IDENT: | |
1212 | * (enum v4l2_chip_ident *) arg = V4L2_IDENT_OV7670; | |
1213 | return 0; | |
1214 | ||
1215 | case VIDIOC_INT_RESET: | |
1216 | ov7670_reset(client); | |
1217 | return 0; | |
1218 | ||
1219 | case VIDIOC_INT_INIT: | |
1220 | return ov7670_init(client); | |
1221 | ||
1222 | case VIDIOC_ENUM_FMT: | |
1223 | return ov7670_enum_fmt(client, (struct v4l2_fmtdesc *) arg); | |
1224 | case VIDIOC_TRY_FMT: | |
1225 | return ov7670_try_fmt(client, (struct v4l2_format *) arg, NULL, NULL); | |
1226 | case VIDIOC_S_FMT: | |
1227 | return ov7670_s_fmt(client, (struct v4l2_format *) arg); | |
1228 | case VIDIOC_QUERYCTRL: | |
1229 | return ov7670_queryctrl(client, (struct v4l2_queryctrl *) arg); | |
1230 | case VIDIOC_S_CTRL: | |
1231 | return ov7670_s_ctrl(client, (struct v4l2_control *) arg); | |
1232 | case VIDIOC_G_CTRL: | |
1233 | return ov7670_g_ctrl(client, (struct v4l2_control *) arg); | |
1234 | /* Todo: | |
1235 | g/s_parm | |
1236 | initialization | |
1237 | */ | |
1238 | } | |
1239 | return -EINVAL; | |
1240 | } | |
1241 | ||
1242 | ||
1243 | ||
1244 | static struct i2c_driver ov7670_driver = { | |
1245 | .driver = { | |
1246 | .name = "ov7670", | |
1247 | }, | |
1248 | .id = I2C_DRIVERID_OV7670, | |
1249 | .class = I2C_CLASS_CAM_DIGITAL, | |
1250 | .attach_adapter = ov7670_attach, | |
1251 | .detach_client = ov7670_detach, | |
1252 | .command = ov7670_command, | |
1253 | }; | |
1254 | ||
1255 | ||
1256 | /* | |
1257 | * Module initialization | |
1258 | */ | |
1259 | static int __init ov7670_mod_init(void) | |
1260 | { | |
1261 | printk(KERN_NOTICE "OmniVision ov7670 sensor driver, at your service\n"); | |
1262 | return i2c_add_driver(&ov7670_driver); | |
1263 | } | |
1264 | ||
1265 | static void __exit ov7670_mod_exit(void) | |
1266 | { | |
1267 | i2c_del_driver(&ov7670_driver); | |
1268 | } | |
1269 | ||
1270 | module_init(ov7670_mod_init); | |
1271 | module_exit(ov7670_mod_exit); |