[media] gspca - ov519: Re-initialize the webcam at resume time
[pandora-kernel.git] / drivers / media / video / gspca / ov519.c
1 /**
2  * OV519 driver
3  *
4  * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
5  * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
6  *
7  * This module is adapted from the ov51x-jpeg package, which itself
8  * was adapted from the ov511 driver.
9  *
10  * Original copyright for the ov511 driver is:
11  *
12  * Copyright (c) 1999-2006 Mark W. McClelland
13  * Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach
14  * Many improvements by Bret Wallach <bwallac1@san.rr.com>
15  * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
16  * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
17  * Changes by Claudio Matsuoka <claudio@conectiva.com>
18  *
19  * ov51x-jpeg original copyright is:
20  *
21  * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
22  * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
23  *
24  * This program is free software; you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation; either version 2 of the License, or
27  * any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program; if not, write to the Free Software
36  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37  *
38  */
39 #define MODULE_NAME "ov519"
40
41 #include <linux/input.h>
42 #include "gspca.h"
43
44 /* The jpeg_hdr is used by w996Xcf only */
45 /* The CONEX_CAM define for jpeg.h needs renaming, now its used here too */
46 #define CONEX_CAM
47 #include "jpeg.h"
48
49 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
50 MODULE_DESCRIPTION("OV519 USB Camera Driver");
51 MODULE_LICENSE("GPL");
52
53 /* global parameters */
54 static int frame_rate;
55
56 /* Number of times to retry a failed I2C transaction. Increase this if you
57  * are getting "Failed to read sensor ID..." */
58 static int i2c_detect_tries = 10;
59
60 /* controls */
61 enum e_ctrl {
62         BRIGHTNESS,
63         CONTRAST,
64         COLORS,
65         HFLIP,
66         VFLIP,
67         AUTOBRIGHT,
68         FREQ,
69         NCTRL           /* number of controls */
70 };
71
72 /* ov519 device descriptor */
73 struct sd {
74         struct gspca_dev gspca_dev;             /* !! must be the first item */
75
76         struct gspca_ctrl ctrls[NCTRL];
77
78         u8 packet_nr;
79
80         char bridge;
81 #define BRIDGE_OV511            0
82 #define BRIDGE_OV511PLUS        1
83 #define BRIDGE_OV518            2
84 #define BRIDGE_OV518PLUS        3
85 #define BRIDGE_OV519            4
86 #define BRIDGE_OVFX2            5
87 #define BRIDGE_W9968CF          6
88 #define BRIDGE_MASK             7
89
90         char invert_led;
91 #define BRIDGE_INVERT_LED       8
92
93         char snapshot_pressed;
94         char snapshot_needs_reset;
95
96         /* Determined by sensor type */
97         u8 sif;
98
99         u8 quality;
100 #define QUALITY_MIN 50
101 #define QUALITY_MAX 70
102 #define QUALITY_DEF 50
103
104         u8 stopped;             /* Streaming is temporarily paused */
105         u8 first_frame;
106
107         u8 frame_rate;          /* current Framerate */
108         u8 clockdiv;            /* clockdiv override */
109
110         s8 sensor;              /* Type of image sensor chip (SEN_*) */
111
112         u8 sensor_addr;
113         u16 sensor_width;
114         u16 sensor_height;
115         s16 sensor_reg_cache[256];
116
117         u8 jpeg_hdr[JPEG_HDR_SZ];
118 };
119 enum sensors {
120         SEN_OV2610,
121         SEN_OV3610,
122         SEN_OV6620,
123         SEN_OV6630,
124         SEN_OV66308AF,
125         SEN_OV7610,
126         SEN_OV7620,
127         SEN_OV7620AE,
128         SEN_OV7640,
129         SEN_OV7648,
130         SEN_OV7670,
131         SEN_OV76BE,
132         SEN_OV8610,
133 };
134
135 /* Note this is a bit of a hack, but the w9968cf driver needs the code for all
136    the ov sensors which is already present here. When we have the time we
137    really should move the sensor drivers to v4l2 sub drivers. */
138 #include "w996Xcf.c"
139
140 /* V4L2 controls supported by the driver */
141 static void setbrightness(struct gspca_dev *gspca_dev);
142 static void setcontrast(struct gspca_dev *gspca_dev);
143 static void setcolors(struct gspca_dev *gspca_dev);
144 static void sethvflip(struct gspca_dev *gspca_dev);
145 static void setautobright(struct gspca_dev *gspca_dev);
146 static void setfreq(struct gspca_dev *gspca_dev);
147 static void setfreq_i(struct sd *sd);
148
149 static const struct ctrl sd_ctrls[] = {
150 [BRIGHTNESS] = {
151             {
152                 .id      = V4L2_CID_BRIGHTNESS,
153                 .type    = V4L2_CTRL_TYPE_INTEGER,
154                 .name    = "Brightness",
155                 .minimum = 0,
156                 .maximum = 255,
157                 .step    = 1,
158                 .default_value = 127,
159             },
160             .set_control = setbrightness,
161         },
162 [CONTRAST] = {
163             {
164                 .id      = V4L2_CID_CONTRAST,
165                 .type    = V4L2_CTRL_TYPE_INTEGER,
166                 .name    = "Contrast",
167                 .minimum = 0,
168                 .maximum = 255,
169                 .step    = 1,
170                 .default_value = 127,
171             },
172             .set_control = setcontrast,
173         },
174 [COLORS] = {
175             {
176                 .id      = V4L2_CID_SATURATION,
177                 .type    = V4L2_CTRL_TYPE_INTEGER,
178                 .name    = "Color",
179                 .minimum = 0,
180                 .maximum = 255,
181                 .step    = 1,
182                 .default_value = 127,
183             },
184             .set_control = setcolors,
185         },
186 /* The flip controls work with ov7670 only */
187 [HFLIP] = {
188             {
189                 .id      = V4L2_CID_HFLIP,
190                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
191                 .name    = "Mirror",
192                 .minimum = 0,
193                 .maximum = 1,
194                 .step    = 1,
195                 .default_value = 0,
196             },
197             .set_control = sethvflip,
198         },
199 [VFLIP] = {
200             {
201                 .id      = V4L2_CID_VFLIP,
202                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
203                 .name    = "Vflip",
204                 .minimum = 0,
205                 .maximum = 1,
206                 .step    = 1,
207                 .default_value = 0,
208             },
209             .set_control = sethvflip,
210         },
211 [AUTOBRIGHT] = {
212             {
213                 .id      = V4L2_CID_AUTOBRIGHTNESS,
214                 .type    = V4L2_CTRL_TYPE_BOOLEAN,
215                 .name    = "Auto Brightness",
216                 .minimum = 0,
217                 .maximum = 1,
218                 .step    = 1,
219                 .default_value = 1,
220             },
221             .set_control = setautobright,
222         },
223 [FREQ] = {
224             {
225                 .id      = V4L2_CID_POWER_LINE_FREQUENCY,
226                 .type    = V4L2_CTRL_TYPE_MENU,
227                 .name    = "Light frequency filter",
228                 .minimum = 0,
229                 .maximum = 2,   /* 0: no flicker, 1: 50Hz, 2:60Hz, 3: auto */
230                 .step    = 1,
231                 .default_value = 0,
232             },
233             .set_control = setfreq,
234         },
235 };
236
237 /* table of the disabled controls */
238 static const unsigned ctrl_dis[] = {
239 [SEN_OV2610] =          (1 << NCTRL) - 1,       /* no control */
240
241 [SEN_OV3610] =          (1 << NCTRL) - 1,       /* no control */
242
243 [SEN_OV6620] =          (1 << HFLIP) |
244                         (1 << VFLIP),
245
246 [SEN_OV6630] =          (1 << HFLIP) |
247                         (1 << VFLIP),
248
249 [SEN_OV66308AF] =       (1 << HFLIP) |
250                         (1 << VFLIP),
251
252 [SEN_OV7610] =          (1 << HFLIP) |
253                         (1 << VFLIP),
254
255 [SEN_OV7620] =          (1 << HFLIP) |
256                         (1 << VFLIP),
257
258 [SEN_OV7620AE] =        (1 << HFLIP) |
259                         (1 << VFLIP),
260
261 [SEN_OV7640] =          (1 << HFLIP) |
262                         (1 << VFLIP) |
263                         (1 << AUTOBRIGHT) |
264                         (1 << CONTRAST),
265
266 [SEN_OV7648] =          (1 << HFLIP) |
267                         (1 << VFLIP) |
268                         (1 << AUTOBRIGHT) |
269                         (1 << CONTRAST),
270
271 [SEN_OV7670] =          (1 << COLORS) |
272                         (1 << AUTOBRIGHT),
273
274 [SEN_OV76BE] =          (1 << HFLIP) |
275                         (1 << VFLIP),
276
277 [SEN_OV8610] =          (1 << HFLIP) |
278                         (1 << VFLIP) |
279                         (1 << FREQ),
280 };
281
282 static const struct v4l2_pix_format ov519_vga_mode[] = {
283         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
284                 .bytesperline = 320,
285                 .sizeimage = 320 * 240 * 3 / 8 + 590,
286                 .colorspace = V4L2_COLORSPACE_JPEG,
287                 .priv = 1},
288         {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
289                 .bytesperline = 640,
290                 .sizeimage = 640 * 480 * 3 / 8 + 590,
291                 .colorspace = V4L2_COLORSPACE_JPEG,
292                 .priv = 0},
293 };
294 static const struct v4l2_pix_format ov519_sif_mode[] = {
295         {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
296                 .bytesperline = 160,
297                 .sizeimage = 160 * 120 * 3 / 8 + 590,
298                 .colorspace = V4L2_COLORSPACE_JPEG,
299                 .priv = 3},
300         {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
301                 .bytesperline = 176,
302                 .sizeimage = 176 * 144 * 3 / 8 + 590,
303                 .colorspace = V4L2_COLORSPACE_JPEG,
304                 .priv = 1},
305         {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
306                 .bytesperline = 320,
307                 .sizeimage = 320 * 240 * 3 / 8 + 590,
308                 .colorspace = V4L2_COLORSPACE_JPEG,
309                 .priv = 2},
310         {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
311                 .bytesperline = 352,
312                 .sizeimage = 352 * 288 * 3 / 8 + 590,
313                 .colorspace = V4L2_COLORSPACE_JPEG,
314                 .priv = 0},
315 };
316
317 /* Note some of the sizeimage values for the ov511 / ov518 may seem
318    larger then necessary, however they need to be this big as the ov511 /
319    ov518 always fills the entire isoc frame, using 0 padding bytes when
320    it doesn't have any data. So with low framerates the amount of data
321    transfered can become quite large (libv4l will remove all the 0 padding
322    in userspace). */
323 static const struct v4l2_pix_format ov518_vga_mode[] = {
324         {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
325                 .bytesperline = 320,
326                 .sizeimage = 320 * 240 * 3,
327                 .colorspace = V4L2_COLORSPACE_JPEG,
328                 .priv = 1},
329         {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
330                 .bytesperline = 640,
331                 .sizeimage = 640 * 480 * 2,
332                 .colorspace = V4L2_COLORSPACE_JPEG,
333                 .priv = 0},
334 };
335 static const struct v4l2_pix_format ov518_sif_mode[] = {
336         {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
337                 .bytesperline = 160,
338                 .sizeimage = 70000,
339                 .colorspace = V4L2_COLORSPACE_JPEG,
340                 .priv = 3},
341         {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
342                 .bytesperline = 176,
343                 .sizeimage = 70000,
344                 .colorspace = V4L2_COLORSPACE_JPEG,
345                 .priv = 1},
346         {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
347                 .bytesperline = 320,
348                 .sizeimage = 320 * 240 * 3,
349                 .colorspace = V4L2_COLORSPACE_JPEG,
350                 .priv = 2},
351         {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
352                 .bytesperline = 352,
353                 .sizeimage = 352 * 288 * 3,
354                 .colorspace = V4L2_COLORSPACE_JPEG,
355                 .priv = 0},
356 };
357
358 static const struct v4l2_pix_format ov511_vga_mode[] = {
359         {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
360                 .bytesperline = 320,
361                 .sizeimage = 320 * 240 * 3,
362                 .colorspace = V4L2_COLORSPACE_JPEG,
363                 .priv = 1},
364         {640, 480, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
365                 .bytesperline = 640,
366                 .sizeimage = 640 * 480 * 2,
367                 .colorspace = V4L2_COLORSPACE_JPEG,
368                 .priv = 0},
369 };
370 static const struct v4l2_pix_format ov511_sif_mode[] = {
371         {160, 120, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
372                 .bytesperline = 160,
373                 .sizeimage = 70000,
374                 .colorspace = V4L2_COLORSPACE_JPEG,
375                 .priv = 3},
376         {176, 144, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
377                 .bytesperline = 176,
378                 .sizeimage = 70000,
379                 .colorspace = V4L2_COLORSPACE_JPEG,
380                 .priv = 1},
381         {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
382                 .bytesperline = 320,
383                 .sizeimage = 320 * 240 * 3,
384                 .colorspace = V4L2_COLORSPACE_JPEG,
385                 .priv = 2},
386         {352, 288, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
387                 .bytesperline = 352,
388                 .sizeimage = 352 * 288 * 3,
389                 .colorspace = V4L2_COLORSPACE_JPEG,
390                 .priv = 0},
391 };
392
393 static const struct v4l2_pix_format ovfx2_vga_mode[] = {
394         {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
395                 .bytesperline = 320,
396                 .sizeimage = 320 * 240,
397                 .colorspace = V4L2_COLORSPACE_SRGB,
398                 .priv = 1},
399         {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
400                 .bytesperline = 640,
401                 .sizeimage = 640 * 480,
402                 .colorspace = V4L2_COLORSPACE_SRGB,
403                 .priv = 0},
404 };
405 static const struct v4l2_pix_format ovfx2_cif_mode[] = {
406         {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
407                 .bytesperline = 160,
408                 .sizeimage = 160 * 120,
409                 .colorspace = V4L2_COLORSPACE_SRGB,
410                 .priv = 3},
411         {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
412                 .bytesperline = 176,
413                 .sizeimage = 176 * 144,
414                 .colorspace = V4L2_COLORSPACE_SRGB,
415                 .priv = 1},
416         {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
417                 .bytesperline = 320,
418                 .sizeimage = 320 * 240,
419                 .colorspace = V4L2_COLORSPACE_SRGB,
420                 .priv = 2},
421         {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
422                 .bytesperline = 352,
423                 .sizeimage = 352 * 288,
424                 .colorspace = V4L2_COLORSPACE_SRGB,
425                 .priv = 0},
426 };
427 static const struct v4l2_pix_format ovfx2_ov2610_mode[] = {
428         {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
429                 .bytesperline = 1600,
430                 .sizeimage = 1600 * 1200,
431                 .colorspace = V4L2_COLORSPACE_SRGB},
432 };
433 static const struct v4l2_pix_format ovfx2_ov3610_mode[] = {
434         {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
435                 .bytesperline = 640,
436                 .sizeimage = 640 * 480,
437                 .colorspace = V4L2_COLORSPACE_SRGB,
438                 .priv = 1},
439         {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
440                 .bytesperline = 800,
441                 .sizeimage = 800 * 600,
442                 .colorspace = V4L2_COLORSPACE_SRGB,
443                 .priv = 1},
444         {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
445                 .bytesperline = 1024,
446                 .sizeimage = 1024 * 768,
447                 .colorspace = V4L2_COLORSPACE_SRGB,
448                 .priv = 1},
449         {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
450                 .bytesperline = 1600,
451                 .sizeimage = 1600 * 1200,
452                 .colorspace = V4L2_COLORSPACE_SRGB,
453                 .priv = 0},
454         {2048, 1536, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
455                 .bytesperline = 2048,
456                 .sizeimage = 2048 * 1536,
457                 .colorspace = V4L2_COLORSPACE_SRGB,
458                 .priv = 0},
459 };
460
461 /* Registers common to OV511 / OV518 */
462 #define R51x_FIFO_PSIZE                 0x30    /* 2 bytes wide w/ OV518(+) */
463 #define R51x_SYS_RESET                  0x50
464         /* Reset type flags */
465         #define OV511_RESET_OMNICE      0x08
466 #define R51x_SYS_INIT                   0x53
467 #define R51x_SYS_SNAP                   0x52
468 #define R51x_SYS_CUST_ID                0x5f
469 #define R51x_COMP_LUT_BEGIN             0x80
470
471 /* OV511 Camera interface register numbers */
472 #define R511_CAM_DELAY                  0x10
473 #define R511_CAM_EDGE                   0x11
474 #define R511_CAM_PXCNT                  0x12
475 #define R511_CAM_LNCNT                  0x13
476 #define R511_CAM_PXDIV                  0x14
477 #define R511_CAM_LNDIV                  0x15
478 #define R511_CAM_UV_EN                  0x16
479 #define R511_CAM_LINE_MODE              0x17
480 #define R511_CAM_OPTS                   0x18
481
482 #define R511_SNAP_FRAME                 0x19
483 #define R511_SNAP_PXCNT                 0x1a
484 #define R511_SNAP_LNCNT                 0x1b
485 #define R511_SNAP_PXDIV                 0x1c
486 #define R511_SNAP_LNDIV                 0x1d
487 #define R511_SNAP_UV_EN                 0x1e
488 #define R511_SNAP_UV_EN                 0x1e
489 #define R511_SNAP_OPTS                  0x1f
490
491 #define R511_DRAM_FLOW_CTL              0x20
492 #define R511_FIFO_OPTS                  0x31
493 #define R511_I2C_CTL                    0x40
494 #define R511_SYS_LED_CTL                0x55    /* OV511+ only */
495 #define R511_COMP_EN                    0x78
496 #define R511_COMP_LUT_EN                0x79
497
498 /* OV518 Camera interface register numbers */
499 #define R518_GPIO_OUT                   0x56    /* OV518(+) only */
500 #define R518_GPIO_CTL                   0x57    /* OV518(+) only */
501
502 /* OV519 Camera interface register numbers */
503 #define OV519_R10_H_SIZE                0x10
504 #define OV519_R11_V_SIZE                0x11
505 #define OV519_R12_X_OFFSETL             0x12
506 #define OV519_R13_X_OFFSETH             0x13
507 #define OV519_R14_Y_OFFSETL             0x14
508 #define OV519_R15_Y_OFFSETH             0x15
509 #define OV519_R16_DIVIDER               0x16
510 #define OV519_R20_DFR                   0x20
511 #define OV519_R25_FORMAT                0x25
512
513 /* OV519 System Controller register numbers */
514 #define OV519_R51_RESET1                0x51
515 #define OV519_R54_EN_CLK1               0x54
516 #define OV519_R57_SNAPSHOT              0x57
517
518 #define OV519_GPIO_DATA_OUT0            0x71
519 #define OV519_GPIO_IO_CTRL0             0x72
520
521 /*#define OV511_ENDPOINT_ADDRESS 1       * Isoc endpoint number */
522
523 /*
524  * The FX2 chip does not give us a zero length read at end of frame.
525  * It does, however, give a short read at the end of a frame, if
526  * necessary, rather than run two frames together.
527  *
528  * By choosing the right bulk transfer size, we are guaranteed to always
529  * get a short read for the last read of each frame.  Frame sizes are
530  * always a composite number (width * height, or a multiple) so if we
531  * choose a prime number, we are guaranteed that the last read of a
532  * frame will be short.
533  *
534  * But it isn't that easy: the 2.6 kernel requires a multiple of 4KB,
535  * otherwise EOVERFLOW "babbling" errors occur.  I have not been able
536  * to figure out why.  [PMiller]
537  *
538  * The constant (13 * 4096) is the largest "prime enough" number less than 64KB.
539  *
540  * It isn't enough to know the number of bytes per frame, in case we
541  * have data dropouts or buffer overruns (even though the FX2 double
542  * buffers, there are some pretty strict real time constraints for
543  * isochronous transfer for larger frame sizes).
544  */
545 #define OVFX2_BULK_SIZE (13 * 4096)
546
547 /* I2C registers */
548 #define R51x_I2C_W_SID          0x41
549 #define R51x_I2C_SADDR_3        0x42
550 #define R51x_I2C_SADDR_2        0x43
551 #define R51x_I2C_R_SID          0x44
552 #define R51x_I2C_DATA           0x45
553 #define R518_I2C_CTL            0x47    /* OV518(+) only */
554 #define OVFX2_I2C_ADDR          0x00
555
556 /* I2C ADDRESSES */
557 #define OV7xx0_SID   0x42
558 #define OV_HIRES_SID 0x60               /* OV9xxx / OV2xxx / OV3xxx */
559 #define OV8xx0_SID   0xa0
560 #define OV6xx0_SID   0xc0
561
562 /* OV7610 registers */
563 #define OV7610_REG_GAIN         0x00    /* gain setting (5:0) */
564 #define OV7610_REG_BLUE         0x01    /* blue channel balance */
565 #define OV7610_REG_RED          0x02    /* red channel balance */
566 #define OV7610_REG_SAT          0x03    /* saturation */
567 #define OV8610_REG_HUE          0x04    /* 04 reserved */
568 #define OV7610_REG_CNT          0x05    /* Y contrast */
569 #define OV7610_REG_BRT          0x06    /* Y brightness */
570 #define OV7610_REG_COM_C        0x14    /* misc common regs */
571 #define OV7610_REG_ID_HIGH      0x1c    /* manufacturer ID MSB */
572 #define OV7610_REG_ID_LOW       0x1d    /* manufacturer ID LSB */
573 #define OV7610_REG_COM_I        0x29    /* misc settings */
574
575 /* OV7670 registers */
576 #define OV7670_R00_GAIN         0x00    /* Gain lower 8 bits (rest in vref) */
577 #define OV7670_R01_BLUE         0x01    /* blue gain */
578 #define OV7670_R02_RED          0x02    /* red gain */
579 #define OV7670_R03_VREF         0x03    /* Pieces of GAIN, VSTART, VSTOP */
580 #define OV7670_R04_COM1         0x04    /* Control 1 */
581 /*#define OV7670_R07_AECHH      0x07     * AEC MS 5 bits */
582 #define OV7670_R0C_COM3         0x0c    /* Control 3 */
583 #define OV7670_R0D_COM4         0x0d    /* Control 4 */
584 #define OV7670_R0E_COM5         0x0e    /* All "reserved" */
585 #define OV7670_R0F_COM6         0x0f    /* Control 6 */
586 #define OV7670_R10_AECH         0x10    /* More bits of AEC value */
587 #define OV7670_R11_CLKRC        0x11    /* Clock control */
588 #define OV7670_R12_COM7         0x12    /* Control 7 */
589 #define   OV7670_COM7_FMT_VGA    0x00
590 /*#define   OV7670_COM7_YUV      0x00    * YUV */
591 #define   OV7670_COM7_FMT_QVGA   0x10   /* QVGA format */
592 #define   OV7670_COM7_FMT_MASK   0x38
593 #define   OV7670_COM7_RESET      0x80   /* Register reset */
594 #define OV7670_R13_COM8         0x13    /* Control 8 */
595 #define   OV7670_COM8_AEC        0x01   /* Auto exposure enable */
596 #define   OV7670_COM8_AWB        0x02   /* White balance enable */
597 #define   OV7670_COM8_AGC        0x04   /* Auto gain enable */
598 #define   OV7670_COM8_BFILT      0x20   /* Band filter enable */
599 #define   OV7670_COM8_AECSTEP    0x40   /* Unlimited AEC step size */
600 #define   OV7670_COM8_FASTAEC    0x80   /* Enable fast AGC/AEC */
601 #define OV7670_R14_COM9         0x14    /* Control 9 - gain ceiling */
602 #define OV7670_R15_COM10        0x15    /* Control 10 */
603 #define OV7670_R17_HSTART       0x17    /* Horiz start high bits */
604 #define OV7670_R18_HSTOP        0x18    /* Horiz stop high bits */
605 #define OV7670_R19_VSTART       0x19    /* Vert start high bits */
606 #define OV7670_R1A_VSTOP        0x1a    /* Vert stop high bits */
607 #define OV7670_R1E_MVFP         0x1e    /* Mirror / vflip */
608 #define   OV7670_MVFP_VFLIP      0x10   /* vertical flip */
609 #define   OV7670_MVFP_MIRROR     0x20   /* Mirror image */
610 #define OV7670_R24_AEW          0x24    /* AGC upper limit */
611 #define OV7670_R25_AEB          0x25    /* AGC lower limit */
612 #define OV7670_R26_VPT          0x26    /* AGC/AEC fast mode op region */
613 #define OV7670_R32_HREF         0x32    /* HREF pieces */
614 #define OV7670_R3A_TSLB         0x3a    /* lots of stuff */
615 #define OV7670_R3B_COM11        0x3b    /* Control 11 */
616 #define   OV7670_COM11_EXP       0x02
617 #define   OV7670_COM11_HZAUTO    0x10   /* Auto detect 50/60 Hz */
618 #define OV7670_R3C_COM12        0x3c    /* Control 12 */
619 #define OV7670_R3D_COM13        0x3d    /* Control 13 */
620 #define   OV7670_COM13_GAMMA     0x80   /* Gamma enable */
621 #define   OV7670_COM13_UVSAT     0x40   /* UV saturation auto adjustment */
622 #define OV7670_R3E_COM14        0x3e    /* Control 14 */
623 #define OV7670_R3F_EDGE         0x3f    /* Edge enhancement factor */
624 #define OV7670_R40_COM15        0x40    /* Control 15 */
625 /*#define   OV7670_COM15_R00FF   0xc0    *      00 to FF */
626 #define OV7670_R41_COM16        0x41    /* Control 16 */
627 #define   OV7670_COM16_AWBGAIN   0x08   /* AWB gain enable */
628 #define OV7670_R55_BRIGHT       0x55    /* Brightness */
629 #define OV7670_R56_CONTRAS      0x56    /* Contrast control */
630 #define OV7670_R69_GFIX         0x69    /* Fix gain control */
631 /*#define OV7670_R8C_RGB444     0x8c     * RGB 444 control */
632 #define OV7670_R9F_HAECC1       0x9f    /* Hist AEC/AGC control 1 */
633 #define OV7670_RA0_HAECC2       0xa0    /* Hist AEC/AGC control 2 */
634 #define OV7670_RA5_BD50MAX      0xa5    /* 50hz banding step limit */
635 #define OV7670_RA6_HAECC3       0xa6    /* Hist AEC/AGC control 3 */
636 #define OV7670_RA7_HAECC4       0xa7    /* Hist AEC/AGC control 4 */
637 #define OV7670_RA8_HAECC5       0xa8    /* Hist AEC/AGC control 5 */
638 #define OV7670_RA9_HAECC6       0xa9    /* Hist AEC/AGC control 6 */
639 #define OV7670_RAA_HAECC7       0xaa    /* Hist AEC/AGC control 7 */
640 #define OV7670_RAB_BD60MAX      0xab    /* 60hz banding step limit */
641
642 struct ov_regvals {
643         u8 reg;
644         u8 val;
645 };
646 struct ov_i2c_regvals {
647         u8 reg;
648         u8 val;
649 };
650
651 /* Settings for OV2610 camera chip */
652 static const struct ov_i2c_regvals norm_2610[] = {
653         { 0x12, 0x80 }, /* reset */
654 };
655
656 static const struct ov_i2c_regvals norm_3620b[] = {
657         /*
658          * From the datasheet: "Note that after writing to register COMH
659          * (0x12) to change the sensor mode, registers related to the
660          * sensor’s cropping window will be reset back to their default
661          * values."
662          *
663          * "wait 4096 external clock ... to make sure the sensor is
664          * stable and ready to access registers" i.e. 160us at 24MHz
665          */
666         { 0x12, 0x80 }, /* COMH reset */
667         { 0x12, 0x00 }, /* QXGA, master */
668
669         /*
670          * 11 CLKRC "Clock Rate Control"
671          * [7] internal frequency doublers: on
672          * [6] video port mode: master
673          * [5:0] clock divider: 1
674          */
675         { 0x11, 0x80 },
676
677         /*
678          * 13 COMI "Common Control I"
679          *                  = 192 (0xC0) 11000000
680          *    COMI[7] "AEC speed selection"
681          *                  =   1 (0x01) 1....... "Faster AEC correction"
682          *    COMI[6] "AEC speed step selection"
683          *                  =   1 (0x01) .1...... "Big steps, fast"
684          *    COMI[5] "Banding filter on off"
685          *                  =   0 (0x00) ..0..... "Off"
686          *    COMI[4] "Banding filter option"
687          *                  =   0 (0x00) ...0.... "Main clock is 48 MHz and
688          *                                         the PLL is ON"
689          *    COMI[3] "Reserved"
690          *                  =   0 (0x00) ....0...
691          *    COMI[2] "AGC auto manual control selection"
692          *                  =   0 (0x00) .....0.. "Manual"
693          *    COMI[1] "AWB auto manual control selection"
694          *                  =   0 (0x00) ......0. "Manual"
695          *    COMI[0] "Exposure control"
696          *                  =   0 (0x00) .......0 "Manual"
697          */
698         { 0x13, 0xc0 },
699
700         /*
701          * 09 COMC "Common Control C"
702          *                  =   8 (0x08) 00001000
703          *    COMC[7:5] "Reserved"
704          *                  =   0 (0x00) 000.....
705          *    COMC[4] "Sleep Mode Enable"
706          *                  =   0 (0x00) ...0.... "Normal mode"
707          *    COMC[3:2] "Sensor sampling reset timing selection"
708          *                  =   2 (0x02) ....10.. "Longer reset time"
709          *    COMC[1:0] "Output drive current select"
710          *                  =   0 (0x00) ......00 "Weakest"
711          */
712         { 0x09, 0x08 },
713
714         /*
715          * 0C COMD "Common Control D"
716          *                  =   8 (0x08) 00001000
717          *    COMD[7] "Reserved"
718          *                  =   0 (0x00) 0.......
719          *    COMD[6] "Swap MSB and LSB at the output port"
720          *                  =   0 (0x00) .0...... "False"
721          *    COMD[5:3] "Reserved"
722          *                  =   1 (0x01) ..001...
723          *    COMD[2] "Output Average On Off"
724          *                  =   0 (0x00) .....0.. "Output Normal"
725          *    COMD[1] "Sensor precharge voltage selection"
726          *                  =   0 (0x00) ......0. "Selects internal
727          *                                         reference precharge
728          *                                         voltage"
729          *    COMD[0] "Snapshot option"
730          *                  =   0 (0x00) .......0 "Enable live video output
731          *                                         after snapshot sequence"
732          */
733         { 0x0c, 0x08 },
734
735         /*
736          * 0D COME "Common Control E"
737          *                  = 161 (0xA1) 10100001
738          *    COME[7] "Output average option"
739          *                  =   1 (0x01) 1....... "Output average of 4 pixels"
740          *    COME[6] "Anti-blooming control"
741          *                  =   0 (0x00) .0...... "Off"
742          *    COME[5:3] "Reserved"
743          *                  =   4 (0x04) ..100...
744          *    COME[2] "Clock output power down pin status"
745          *                  =   0 (0x00) .....0.. "Tri-state data output pin
746          *                                         on power down"
747          *    COME[1] "Data output pin status selection at power down"
748          *                  =   0 (0x00) ......0. "Tri-state VSYNC, PCLK,
749          *                                         HREF, and CHSYNC pins on
750          *                                         power down"
751          *    COME[0] "Auto zero circuit select"
752          *                  =   1 (0x01) .......1 "On"
753          */
754         { 0x0d, 0xa1 },
755
756         /*
757          * 0E COMF "Common Control F"
758          *                  = 112 (0x70) 01110000
759          *    COMF[7] "System clock selection"
760          *                  =   0 (0x00) 0....... "Use 24 MHz system clock"
761          *    COMF[6:4] "Reserved"
762          *                  =   7 (0x07) .111....
763          *    COMF[3] "Manual auto negative offset canceling selection"
764          *                  =   0 (0x00) ....0... "Auto detect negative
765          *                                         offset and cancel it"
766          *    COMF[2:0] "Reserved"
767          *                  =   0 (0x00) .....000
768          */
769         { 0x0e, 0x70 },
770
771         /*
772          * 0F COMG "Common Control G"
773          *                  =  66 (0x42) 01000010
774          *    COMG[7] "Optical black output selection"
775          *                  =   0 (0x00) 0....... "Disable"
776          *    COMG[6] "Black level calibrate selection"
777          *                  =   1 (0x01) .1...... "Use optical black pixels
778          *                                         to calibrate"
779          *    COMG[5:4] "Reserved"
780          *                  =   0 (0x00) ..00....
781          *    COMG[3] "Channel offset adjustment"
782          *                  =   0 (0x00) ....0... "Disable offset adjustment"
783          *    COMG[2] "ADC black level calibration option"
784          *                  =   0 (0x00) .....0.. "Use B/G line and G/R
785          *                                         line to calibrate each
786          *                                         channel's black level"
787          *    COMG[1] "Reserved"
788          *                  =   1 (0x01) ......1.
789          *    COMG[0] "ADC black level calibration enable"
790          *                  =   0 (0x00) .......0 "Disable"
791          */
792         { 0x0f, 0x42 },
793
794         /*
795          * 14 COMJ "Common Control J"
796          *                  = 198 (0xC6) 11000110
797          *    COMJ[7:6] "AGC gain ceiling"
798          *                  =   3 (0x03) 11...... "8x"
799          *    COMJ[5:4] "Reserved"
800          *                  =   0 (0x00) ..00....
801          *    COMJ[3] "Auto banding filter"
802          *                  =   0 (0x00) ....0... "Banding filter is always
803          *                                         on off depending on
804          *                                         COMI[5] setting"
805          *    COMJ[2] "VSYNC drop option"
806          *                  =   1 (0x01) .....1.. "SYNC is dropped if frame
807          *                                         data is dropped"
808          *    COMJ[1] "Frame data drop"
809          *                  =   1 (0x01) ......1. "Drop frame data if
810          *                                         exposure is not within
811          *                                         tolerance.  In AEC mode,
812          *                                         data is normally dropped
813          *                                         when data is out of
814          *                                         range."
815          *    COMJ[0] "Reserved"
816          *                  =   0 (0x00) .......0
817          */
818         { 0x14, 0xc6 },
819
820         /*
821          * 15 COMK "Common Control K"
822          *                  =   2 (0x02) 00000010
823          *    COMK[7] "CHSYNC pin output swap"
824          *                  =   0 (0x00) 0....... "CHSYNC"
825          *    COMK[6] "HREF pin output swap"
826          *                  =   0 (0x00) .0...... "HREF"
827          *    COMK[5] "PCLK output selection"
828          *                  =   0 (0x00) ..0..... "PCLK always output"
829          *    COMK[4] "PCLK edge selection"
830          *                  =   0 (0x00) ...0.... "Data valid on falling edge"
831          *    COMK[3] "HREF output polarity"
832          *                  =   0 (0x00) ....0... "positive"
833          *    COMK[2] "Reserved"
834          *                  =   0 (0x00) .....0..
835          *    COMK[1] "VSYNC polarity"
836          *                  =   1 (0x01) ......1. "negative"
837          *    COMK[0] "HSYNC polarity"
838          *                  =   0 (0x00) .......0 "positive"
839          */
840         { 0x15, 0x02 },
841
842         /*
843          * 33 CHLF "Current Control"
844          *                  =   9 (0x09) 00001001
845          *    CHLF[7:6] "Sensor current control"
846          *                  =   0 (0x00) 00......
847          *    CHLF[5] "Sensor current range control"
848          *                  =   0 (0x00) ..0..... "normal range"
849          *    CHLF[4] "Sensor current"
850          *                  =   0 (0x00) ...0.... "normal current"
851          *    CHLF[3] "Sensor buffer current control"
852          *                  =   1 (0x01) ....1... "half current"
853          *    CHLF[2] "Column buffer current control"
854          *                  =   0 (0x00) .....0.. "normal current"
855          *    CHLF[1] "Analog DSP current control"
856          *                  =   0 (0x00) ......0. "normal current"
857          *    CHLF[1] "ADC current control"
858          *                  =   0 (0x00) ......0. "normal current"
859          */
860         { 0x33, 0x09 },
861
862         /*
863          * 34 VBLM "Blooming Control"
864          *                  =  80 (0x50) 01010000
865          *    VBLM[7] "Hard soft reset switch"
866          *                  =   0 (0x00) 0....... "Hard reset"
867          *    VBLM[6:4] "Blooming voltage selection"
868          *                  =   5 (0x05) .101....
869          *    VBLM[3:0] "Sensor current control"
870          *                  =   0 (0x00) ....0000
871          */
872         { 0x34, 0x50 },
873
874         /*
875          * 36 VCHG "Sensor Precharge Voltage Control"
876          *                  =   0 (0x00) 00000000
877          *    VCHG[7] "Reserved"
878          *                  =   0 (0x00) 0.......
879          *    VCHG[6:4] "Sensor precharge voltage control"
880          *                  =   0 (0x00) .000....
881          *    VCHG[3:0] "Sensor array common reference"
882          *                  =   0 (0x00) ....0000
883          */
884         { 0x36, 0x00 },
885
886         /*
887          * 37 ADC "ADC Reference Control"
888          *                  =   4 (0x04) 00000100
889          *    ADC[7:4] "Reserved"
890          *                  =   0 (0x00) 0000....
891          *    ADC[3] "ADC input signal range"
892          *                  =   0 (0x00) ....0... "Input signal 1.0x"
893          *    ADC[2:0] "ADC range control"
894          *                  =   4 (0x04) .....100
895          */
896         { 0x37, 0x04 },
897
898         /*
899          * 38 ACOM "Analog Common Ground"
900          *                  =  82 (0x52) 01010010
901          *    ACOM[7] "Analog gain control"
902          *                  =   0 (0x00) 0....... "Gain 1x"
903          *    ACOM[6] "Analog black level calibration"
904          *                  =   1 (0x01) .1...... "On"
905          *    ACOM[5:0] "Reserved"
906          *                  =  18 (0x12) ..010010
907          */
908         { 0x38, 0x52 },
909
910         /*
911          * 3A FREFA "Internal Reference Adjustment"
912          *                  =   0 (0x00) 00000000
913          *    FREFA[7:0] "Range"
914          *                  =   0 (0x00) 00000000
915          */
916         { 0x3a, 0x00 },
917
918         /*
919          * 3C FVOPT "Internal Reference Adjustment"
920          *                  =  31 (0x1F) 00011111
921          *    FVOPT[7:0] "Range"
922          *                  =  31 (0x1F) 00011111
923          */
924         { 0x3c, 0x1f },
925
926         /*
927          * 44 Undocumented  =   0 (0x00) 00000000
928          *    44[7:0] "It's a secret"
929          *                  =   0 (0x00) 00000000
930          */
931         { 0x44, 0x00 },
932
933         /*
934          * 40 Undocumented  =   0 (0x00) 00000000
935          *    40[7:0] "It's a secret"
936          *                  =   0 (0x00) 00000000
937          */
938         { 0x40, 0x00 },
939
940         /*
941          * 41 Undocumented  =   0 (0x00) 00000000
942          *    41[7:0] "It's a secret"
943          *                  =   0 (0x00) 00000000
944          */
945         { 0x41, 0x00 },
946
947         /*
948          * 42 Undocumented  =   0 (0x00) 00000000
949          *    42[7:0] "It's a secret"
950          *                  =   0 (0x00) 00000000
951          */
952         { 0x42, 0x00 },
953
954         /*
955          * 43 Undocumented  =   0 (0x00) 00000000
956          *    43[7:0] "It's a secret"
957          *                  =   0 (0x00) 00000000
958          */
959         { 0x43, 0x00 },
960
961         /*
962          * 45 Undocumented  = 128 (0x80) 10000000
963          *    45[7:0] "It's a secret"
964          *                  = 128 (0x80) 10000000
965          */
966         { 0x45, 0x80 },
967
968         /*
969          * 48 Undocumented  = 192 (0xC0) 11000000
970          *    48[7:0] "It's a secret"
971          *                  = 192 (0xC0) 11000000
972          */
973         { 0x48, 0xc0 },
974
975         /*
976          * 49 Undocumented  =  25 (0x19) 00011001
977          *    49[7:0] "It's a secret"
978          *                  =  25 (0x19) 00011001
979          */
980         { 0x49, 0x19 },
981
982         /*
983          * 4B Undocumented  = 128 (0x80) 10000000
984          *    4B[7:0] "It's a secret"
985          *                  = 128 (0x80) 10000000
986          */
987         { 0x4b, 0x80 },
988
989         /*
990          * 4D Undocumented  = 196 (0xC4) 11000100
991          *    4D[7:0] "It's a secret"
992          *                  = 196 (0xC4) 11000100
993          */
994         { 0x4d, 0xc4 },
995
996         /*
997          * 35 VREF "Reference Voltage Control"
998          *                  =  76 (0x4c) 01001100
999          *    VREF[7:5] "Column high reference control"
1000          *                  =   2 (0x02) 010..... "higher voltage"
1001          *    VREF[4:2] "Column low reference control"
1002          *                  =   3 (0x03) ...011.. "Highest voltage"
1003          *    VREF[1:0] "Reserved"
1004          *                  =   0 (0x00) ......00
1005          */
1006         { 0x35, 0x4c },
1007
1008         /*
1009          * 3D Undocumented  =   0 (0x00) 00000000
1010          *    3D[7:0] "It's a secret"
1011          *                  =   0 (0x00) 00000000
1012          */
1013         { 0x3d, 0x00 },
1014
1015         /*
1016          * 3E Undocumented  =   0 (0x00) 00000000
1017          *    3E[7:0] "It's a secret"
1018          *                  =   0 (0x00) 00000000
1019          */
1020         { 0x3e, 0x00 },
1021
1022         /*
1023          * 3B FREFB "Internal Reference Adjustment"
1024          *                  =  24 (0x18) 00011000
1025          *    FREFB[7:0] "Range"
1026          *                  =  24 (0x18) 00011000
1027          */
1028         { 0x3b, 0x18 },
1029
1030         /*
1031          * 33 CHLF "Current Control"
1032          *                  =  25 (0x19) 00011001
1033          *    CHLF[7:6] "Sensor current control"
1034          *                  =   0 (0x00) 00......
1035          *    CHLF[5] "Sensor current range control"
1036          *                  =   0 (0x00) ..0..... "normal range"
1037          *    CHLF[4] "Sensor current"
1038          *                  =   1 (0x01) ...1.... "double current"
1039          *    CHLF[3] "Sensor buffer current control"
1040          *                  =   1 (0x01) ....1... "half current"
1041          *    CHLF[2] "Column buffer current control"
1042          *                  =   0 (0x00) .....0.. "normal current"
1043          *    CHLF[1] "Analog DSP current control"
1044          *                  =   0 (0x00) ......0. "normal current"
1045          *    CHLF[1] "ADC current control"
1046          *                  =   0 (0x00) ......0. "normal current"
1047          */
1048         { 0x33, 0x19 },
1049
1050         /*
1051          * 34 VBLM "Blooming Control"
1052          *                  =  90 (0x5A) 01011010
1053          *    VBLM[7] "Hard soft reset switch"
1054          *                  =   0 (0x00) 0....... "Hard reset"
1055          *    VBLM[6:4] "Blooming voltage selection"
1056          *                  =   5 (0x05) .101....
1057          *    VBLM[3:0] "Sensor current control"
1058          *                  =  10 (0x0A) ....1010
1059          */
1060         { 0x34, 0x5a },
1061
1062         /*
1063          * 3B FREFB "Internal Reference Adjustment"
1064          *                  =   0 (0x00) 00000000
1065          *    FREFB[7:0] "Range"
1066          *                  =   0 (0x00) 00000000
1067          */
1068         { 0x3b, 0x00 },
1069
1070         /*
1071          * 33 CHLF "Current Control"
1072          *                  =   9 (0x09) 00001001
1073          *    CHLF[7:6] "Sensor current control"
1074          *                  =   0 (0x00) 00......
1075          *    CHLF[5] "Sensor current range control"
1076          *                  =   0 (0x00) ..0..... "normal range"
1077          *    CHLF[4] "Sensor current"
1078          *                  =   0 (0x00) ...0.... "normal current"
1079          *    CHLF[3] "Sensor buffer current control"
1080          *                  =   1 (0x01) ....1... "half current"
1081          *    CHLF[2] "Column buffer current control"
1082          *                  =   0 (0x00) .....0.. "normal current"
1083          *    CHLF[1] "Analog DSP current control"
1084          *                  =   0 (0x00) ......0. "normal current"
1085          *    CHLF[1] "ADC current control"
1086          *                  =   0 (0x00) ......0. "normal current"
1087          */
1088         { 0x33, 0x09 },
1089
1090         /*
1091          * 34 VBLM "Blooming Control"
1092          *                  =  80 (0x50) 01010000
1093          *    VBLM[7] "Hard soft reset switch"
1094          *                  =   0 (0x00) 0....... "Hard reset"
1095          *    VBLM[6:4] "Blooming voltage selection"
1096          *                  =   5 (0x05) .101....
1097          *    VBLM[3:0] "Sensor current control"
1098          *                  =   0 (0x00) ....0000
1099          */
1100         { 0x34, 0x50 },
1101
1102         /*
1103          * 12 COMH "Common Control H"
1104          *                  =  64 (0x40) 01000000
1105          *    COMH[7] "SRST"
1106          *                  =   0 (0x00) 0....... "No-op"
1107          *    COMH[6:4] "Resolution selection"
1108          *                  =   4 (0x04) .100.... "XGA"
1109          *    COMH[3] "Master slave selection"
1110          *                  =   0 (0x00) ....0... "Master mode"
1111          *    COMH[2] "Internal B/R channel option"
1112          *                  =   0 (0x00) .....0.. "B/R use same channel"
1113          *    COMH[1] "Color bar test pattern"
1114          *                  =   0 (0x00) ......0. "Off"
1115          *    COMH[0] "Reserved"
1116          *                  =   0 (0x00) .......0
1117          */
1118         { 0x12, 0x40 },
1119
1120         /*
1121          * 17 HREFST "Horizontal window start"
1122          *                  =  31 (0x1F) 00011111
1123          *    HREFST[7:0] "Horizontal window start, 8 MSBs"
1124          *                  =  31 (0x1F) 00011111
1125          */
1126         { 0x17, 0x1f },
1127
1128         /*
1129          * 18 HREFEND "Horizontal window end"
1130          *                  =  95 (0x5F) 01011111
1131          *    HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1132          *                  =  95 (0x5F) 01011111
1133          */
1134         { 0x18, 0x5f },
1135
1136         /*
1137          * 19 VSTRT "Vertical window start"
1138          *                  =   0 (0x00) 00000000
1139          *    VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1140          *                  =   0 (0x00) 00000000
1141          */
1142         { 0x19, 0x00 },
1143
1144         /*
1145          * 1A VEND "Vertical window end"
1146          *                  =  96 (0x60) 01100000
1147          *    VEND[7:0] "Vertical Window End, 8 MSBs"
1148          *                  =  96 (0x60) 01100000
1149          */
1150         { 0x1a, 0x60 },
1151
1152         /*
1153          * 32 COMM "Common Control M"
1154          *                  =  18 (0x12) 00010010
1155          *    COMM[7:6] "Pixel clock divide option"
1156          *                  =   0 (0x00) 00...... "/1"
1157          *    COMM[5:3] "Horizontal window end position, 3 LSBs"
1158          *                  =   2 (0x02) ..010...
1159          *    COMM[2:0] "Horizontal window start position, 3 LSBs"
1160          *                  =   2 (0x02) .....010
1161          */
1162         { 0x32, 0x12 },
1163
1164         /*
1165          * 03 COMA "Common Control A"
1166          *                  =  74 (0x4A) 01001010
1167          *    COMA[7:4] "AWB Update Threshold"
1168          *                  =   4 (0x04) 0100....
1169          *    COMA[3:2] "Vertical window end line control 2 LSBs"
1170          *                  =   2 (0x02) ....10..
1171          *    COMA[1:0] "Vertical window start line control 2 LSBs"
1172          *                  =   2 (0x02) ......10
1173          */
1174         { 0x03, 0x4a },
1175
1176         /*
1177          * 11 CLKRC "Clock Rate Control"
1178          *                  = 128 (0x80) 10000000
1179          *    CLKRC[7] "Internal frequency doublers on off seclection"
1180          *                  =   1 (0x01) 1....... "On"
1181          *    CLKRC[6] "Digital video master slave selection"
1182          *                  =   0 (0x00) .0...... "Master mode, sensor
1183          *                                         provides PCLK"
1184          *    CLKRC[5:0] "Clock divider { CLK = PCLK/(1+CLKRC[5:0]) }"
1185          *                  =   0 (0x00) ..000000
1186          */
1187         { 0x11, 0x80 },
1188
1189         /*
1190          * 12 COMH "Common Control H"
1191          *                  =   0 (0x00) 00000000
1192          *    COMH[7] "SRST"
1193          *                  =   0 (0x00) 0....... "No-op"
1194          *    COMH[6:4] "Resolution selection"
1195          *                  =   0 (0x00) .000.... "QXGA"
1196          *    COMH[3] "Master slave selection"
1197          *                  =   0 (0x00) ....0... "Master mode"
1198          *    COMH[2] "Internal B/R channel option"
1199          *                  =   0 (0x00) .....0.. "B/R use same channel"
1200          *    COMH[1] "Color bar test pattern"
1201          *                  =   0 (0x00) ......0. "Off"
1202          *    COMH[0] "Reserved"
1203          *                  =   0 (0x00) .......0
1204          */
1205         { 0x12, 0x00 },
1206
1207         /*
1208          * 12 COMH "Common Control H"
1209          *                  =  64 (0x40) 01000000
1210          *    COMH[7] "SRST"
1211          *                  =   0 (0x00) 0....... "No-op"
1212          *    COMH[6:4] "Resolution selection"
1213          *                  =   4 (0x04) .100.... "XGA"
1214          *    COMH[3] "Master slave selection"
1215          *                  =   0 (0x00) ....0... "Master mode"
1216          *    COMH[2] "Internal B/R channel option"
1217          *                  =   0 (0x00) .....0.. "B/R use same channel"
1218          *    COMH[1] "Color bar test pattern"
1219          *                  =   0 (0x00) ......0. "Off"
1220          *    COMH[0] "Reserved"
1221          *                  =   0 (0x00) .......0
1222          */
1223         { 0x12, 0x40 },
1224
1225         /*
1226          * 17 HREFST "Horizontal window start"
1227          *                  =  31 (0x1F) 00011111
1228          *    HREFST[7:0] "Horizontal window start, 8 MSBs"
1229          *                  =  31 (0x1F) 00011111
1230          */
1231         { 0x17, 0x1f },
1232
1233         /*
1234          * 18 HREFEND "Horizontal window end"
1235          *                  =  95 (0x5F) 01011111
1236          *    HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1237          *                  =  95 (0x5F) 01011111
1238          */
1239         { 0x18, 0x5f },
1240
1241         /*
1242          * 19 VSTRT "Vertical window start"
1243          *                  =   0 (0x00) 00000000
1244          *    VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1245          *                  =   0 (0x00) 00000000
1246          */
1247         { 0x19, 0x00 },
1248
1249         /*
1250          * 1A VEND "Vertical window end"
1251          *                  =  96 (0x60) 01100000
1252          *    VEND[7:0] "Vertical Window End, 8 MSBs"
1253          *                  =  96 (0x60) 01100000
1254          */
1255         { 0x1a, 0x60 },
1256
1257         /*
1258          * 32 COMM "Common Control M"
1259          *                  =  18 (0x12) 00010010
1260          *    COMM[7:6] "Pixel clock divide option"
1261          *                  =   0 (0x00) 00...... "/1"
1262          *    COMM[5:3] "Horizontal window end position, 3 LSBs"
1263          *                  =   2 (0x02) ..010...
1264          *    COMM[2:0] "Horizontal window start position, 3 LSBs"
1265          *                  =   2 (0x02) .....010
1266          */
1267         { 0x32, 0x12 },
1268
1269         /*
1270          * 03 COMA "Common Control A"
1271          *                  =  74 (0x4A) 01001010
1272          *    COMA[7:4] "AWB Update Threshold"
1273          *                  =   4 (0x04) 0100....
1274          *    COMA[3:2] "Vertical window end line control 2 LSBs"
1275          *                  =   2 (0x02) ....10..
1276          *    COMA[1:0] "Vertical window start line control 2 LSBs"
1277          *                  =   2 (0x02) ......10
1278          */
1279         { 0x03, 0x4a },
1280
1281         /*
1282          * 02 RED "Red Gain Control"
1283          *                  = 175 (0xAF) 10101111
1284          *    RED[7] "Action"
1285          *                  =   1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1286          *    RED[6:0] "Value"
1287          *                  =  47 (0x2F) .0101111
1288          */
1289         { 0x02, 0xaf },
1290
1291         /*
1292          * 2D ADDVSL "VSYNC Pulse Width"
1293          *                  = 210 (0xD2) 11010010
1294          *    ADDVSL[7:0] "VSYNC pulse width, LSB"
1295          *                  = 210 (0xD2) 11010010
1296          */
1297         { 0x2d, 0xd2 },
1298
1299         /*
1300          * 00 GAIN          =  24 (0x18) 00011000
1301          *    GAIN[7:6] "Reserved"
1302          *                  =   0 (0x00) 00......
1303          *    GAIN[5] "Double"
1304          *                  =   0 (0x00) ..0..... "False"
1305          *    GAIN[4] "Double"
1306          *                  =   1 (0x01) ...1.... "True"
1307          *    GAIN[3:0] "Range"
1308          *                  =   8 (0x08) ....1000
1309          */
1310         { 0x00, 0x18 },
1311
1312         /*
1313          * 01 BLUE "Blue Gain Control"
1314          *                  = 240 (0xF0) 11110000
1315          *    BLUE[7] "Action"
1316          *                  =   1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1317          *    BLUE[6:0] "Value"
1318          *                  = 112 (0x70) .1110000
1319          */
1320         { 0x01, 0xf0 },
1321
1322         /*
1323          * 10 AEC "Automatic Exposure Control"
1324          *                  =  10 (0x0A) 00001010
1325          *    AEC[7:0] "Automatic Exposure Control, 8 MSBs"
1326          *                  =  10 (0x0A) 00001010
1327          */
1328         { 0x10, 0x0a },
1329
1330         { 0xe1, 0x67 },
1331         { 0xe3, 0x03 },
1332         { 0xe4, 0x26 },
1333         { 0xe5, 0x3e },
1334         { 0xf8, 0x01 },
1335         { 0xff, 0x01 },
1336 };
1337
1338 static const struct ov_i2c_regvals norm_6x20[] = {
1339         { 0x12, 0x80 }, /* reset */
1340         { 0x11, 0x01 },
1341         { 0x03, 0x60 },
1342         { 0x05, 0x7f }, /* For when autoadjust is off */
1343         { 0x07, 0xa8 },
1344         /* The ratio of 0x0c and 0x0d controls the white point */
1345         { 0x0c, 0x24 },
1346         { 0x0d, 0x24 },
1347         { 0x0f, 0x15 }, /* COMS */
1348         { 0x10, 0x75 }, /* AEC Exposure time */
1349         { 0x12, 0x24 }, /* Enable AGC */
1350         { 0x14, 0x04 },
1351         /* 0x16: 0x06 helps frame stability with moving objects */
1352         { 0x16, 0x06 },
1353 /*      { 0x20, 0x30 },  * Aperture correction enable */
1354         { 0x26, 0xb2 }, /* BLC enable */
1355         /* 0x28: 0x05 Selects RGB format if RGB on */
1356         { 0x28, 0x05 },
1357         { 0x2a, 0x04 }, /* Disable framerate adjust */
1358 /*      { 0x2b, 0xac },  * Framerate; Set 2a[7] first */
1359         { 0x2d, 0x85 },
1360         { 0x33, 0xa0 }, /* Color Processing Parameter */
1361         { 0x34, 0xd2 }, /* Max A/D range */
1362         { 0x38, 0x8b },
1363         { 0x39, 0x40 },
1364
1365         { 0x3c, 0x39 }, /* Enable AEC mode changing */
1366         { 0x3c, 0x3c }, /* Change AEC mode */
1367         { 0x3c, 0x24 }, /* Disable AEC mode changing */
1368
1369         { 0x3d, 0x80 },
1370         /* These next two registers (0x4a, 0x4b) are undocumented.
1371          * They control the color balance */
1372         { 0x4a, 0x80 },
1373         { 0x4b, 0x80 },
1374         { 0x4d, 0xd2 }, /* This reduces noise a bit */
1375         { 0x4e, 0xc1 },
1376         { 0x4f, 0x04 },
1377 /* Do 50-53 have any effect? */
1378 /* Toggle 0x12[2] off and on here? */
1379 };
1380
1381 static const struct ov_i2c_regvals norm_6x30[] = {
1382         { 0x12, 0x80 }, /* Reset */
1383         { 0x00, 0x1f }, /* Gain */
1384         { 0x01, 0x99 }, /* Blue gain */
1385         { 0x02, 0x7c }, /* Red gain */
1386         { 0x03, 0xc0 }, /* Saturation */
1387         { 0x05, 0x0a }, /* Contrast */
1388         { 0x06, 0x95 }, /* Brightness */
1389         { 0x07, 0x2d }, /* Sharpness */
1390         { 0x0c, 0x20 },
1391         { 0x0d, 0x20 },
1392         { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
1393         { 0x0f, 0x05 },
1394         { 0x10, 0x9a },
1395         { 0x11, 0x00 }, /* Pixel clock = fastest */
1396         { 0x12, 0x24 }, /* Enable AGC and AWB */
1397         { 0x13, 0x21 },
1398         { 0x14, 0x80 },
1399         { 0x15, 0x01 },
1400         { 0x16, 0x03 },
1401         { 0x17, 0x38 },
1402         { 0x18, 0xea },
1403         { 0x19, 0x04 },
1404         { 0x1a, 0x93 },
1405         { 0x1b, 0x00 },
1406         { 0x1e, 0xc4 },
1407         { 0x1f, 0x04 },
1408         { 0x20, 0x20 },
1409         { 0x21, 0x10 },
1410         { 0x22, 0x88 },
1411         { 0x23, 0xc0 }, /* Crystal circuit power level */
1412         { 0x25, 0x9a }, /* Increase AEC black ratio */
1413         { 0x26, 0xb2 }, /* BLC enable */
1414         { 0x27, 0xa2 },
1415         { 0x28, 0x00 },
1416         { 0x29, 0x00 },
1417         { 0x2a, 0x84 }, /* 60 Hz power */
1418         { 0x2b, 0xa8 }, /* 60 Hz power */
1419         { 0x2c, 0xa0 },
1420         { 0x2d, 0x95 }, /* Enable auto-brightness */
1421         { 0x2e, 0x88 },
1422         { 0x33, 0x26 },
1423         { 0x34, 0x03 },
1424         { 0x36, 0x8f },
1425         { 0x37, 0x80 },
1426         { 0x38, 0x83 },
1427         { 0x39, 0x80 },
1428         { 0x3a, 0x0f },
1429         { 0x3b, 0x3c },
1430         { 0x3c, 0x1a },
1431         { 0x3d, 0x80 },
1432         { 0x3e, 0x80 },
1433         { 0x3f, 0x0e },
1434         { 0x40, 0x00 }, /* White bal */
1435         { 0x41, 0x00 }, /* White bal */
1436         { 0x42, 0x80 },
1437         { 0x43, 0x3f }, /* White bal */
1438         { 0x44, 0x80 },
1439         { 0x45, 0x20 },
1440         { 0x46, 0x20 },
1441         { 0x47, 0x80 },
1442         { 0x48, 0x7f },
1443         { 0x49, 0x00 },
1444         { 0x4a, 0x00 },
1445         { 0x4b, 0x80 },
1446         { 0x4c, 0xd0 },
1447         { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1448         { 0x4e, 0x40 },
1449         { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1450         { 0x50, 0xff },
1451         { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1452         { 0x55, 0xff },
1453         { 0x56, 0x12 },
1454         { 0x57, 0x81 },
1455         { 0x58, 0x75 },
1456         { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1457         { 0x5a, 0x2c },
1458         { 0x5b, 0x0f }, /* AWB chrominance levels */
1459         { 0x5c, 0x10 },
1460         { 0x3d, 0x80 },
1461         { 0x27, 0xa6 },
1462         { 0x12, 0x20 }, /* Toggle AWB */
1463         { 0x12, 0x24 },
1464 };
1465
1466 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
1467  *
1468  * Register 0x0f in the 7610 has the following effects:
1469  *
1470  * 0x85 (AEC method 1): Best overall, good contrast range
1471  * 0x45 (AEC method 2): Very overexposed
1472  * 0xa5 (spec sheet default): Ok, but the black level is
1473  *      shifted resulting in loss of contrast
1474  * 0x05 (old driver setting): very overexposed, too much
1475  *      contrast
1476  */
1477 static const struct ov_i2c_regvals norm_7610[] = {
1478         { 0x10, 0xff },
1479         { 0x16, 0x06 },
1480         { 0x28, 0x24 },
1481         { 0x2b, 0xac },
1482         { 0x12, 0x00 },
1483         { 0x38, 0x81 },
1484         { 0x28, 0x24 }, /* 0c */
1485         { 0x0f, 0x85 }, /* lg's setting */
1486         { 0x15, 0x01 },
1487         { 0x20, 0x1c },
1488         { 0x23, 0x2a },
1489         { 0x24, 0x10 },
1490         { 0x25, 0x8a },
1491         { 0x26, 0xa2 },
1492         { 0x27, 0xc2 },
1493         { 0x2a, 0x04 },
1494         { 0x2c, 0xfe },
1495         { 0x2d, 0x93 },
1496         { 0x30, 0x71 },
1497         { 0x31, 0x60 },
1498         { 0x32, 0x26 },
1499         { 0x33, 0x20 },
1500         { 0x34, 0x48 },
1501         { 0x12, 0x24 },
1502         { 0x11, 0x01 },
1503         { 0x0c, 0x24 },
1504         { 0x0d, 0x24 },
1505 };
1506
1507 static const struct ov_i2c_regvals norm_7620[] = {
1508         { 0x12, 0x80 },         /* reset */
1509         { 0x00, 0x00 },         /* gain */
1510         { 0x01, 0x80 },         /* blue gain */
1511         { 0x02, 0x80 },         /* red gain */
1512         { 0x03, 0xc0 },         /* OV7670_R03_VREF */
1513         { 0x06, 0x60 },
1514         { 0x07, 0x00 },
1515         { 0x0c, 0x24 },
1516         { 0x0c, 0x24 },
1517         { 0x0d, 0x24 },
1518         { 0x11, 0x01 },
1519         { 0x12, 0x24 },
1520         { 0x13, 0x01 },
1521         { 0x14, 0x84 },
1522         { 0x15, 0x01 },
1523         { 0x16, 0x03 },
1524         { 0x17, 0x2f },
1525         { 0x18, 0xcf },
1526         { 0x19, 0x06 },
1527         { 0x1a, 0xf5 },
1528         { 0x1b, 0x00 },
1529         { 0x20, 0x18 },
1530         { 0x21, 0x80 },
1531         { 0x22, 0x80 },
1532         { 0x23, 0x00 },
1533         { 0x26, 0xa2 },
1534         { 0x27, 0xea },
1535         { 0x28, 0x22 }, /* Was 0x20, bit1 enables a 2x gain which we need */
1536         { 0x29, 0x00 },
1537         { 0x2a, 0x10 },
1538         { 0x2b, 0x00 },
1539         { 0x2c, 0x88 },
1540         { 0x2d, 0x91 },
1541         { 0x2e, 0x80 },
1542         { 0x2f, 0x44 },
1543         { 0x60, 0x27 },
1544         { 0x61, 0x02 },
1545         { 0x62, 0x5f },
1546         { 0x63, 0xd5 },
1547         { 0x64, 0x57 },
1548         { 0x65, 0x83 },
1549         { 0x66, 0x55 },
1550         { 0x67, 0x92 },
1551         { 0x68, 0xcf },
1552         { 0x69, 0x76 },
1553         { 0x6a, 0x22 },
1554         { 0x6b, 0x00 },
1555         { 0x6c, 0x02 },
1556         { 0x6d, 0x44 },
1557         { 0x6e, 0x80 },
1558         { 0x6f, 0x1d },
1559         { 0x70, 0x8b },
1560         { 0x71, 0x00 },
1561         { 0x72, 0x14 },
1562         { 0x73, 0x54 },
1563         { 0x74, 0x00 },
1564         { 0x75, 0x8e },
1565         { 0x76, 0x00 },
1566         { 0x77, 0xff },
1567         { 0x78, 0x80 },
1568         { 0x79, 0x80 },
1569         { 0x7a, 0x80 },
1570         { 0x7b, 0xe2 },
1571         { 0x7c, 0x00 },
1572 };
1573
1574 /* 7640 and 7648. The defaults should be OK for most registers. */
1575 static const struct ov_i2c_regvals norm_7640[] = {
1576         { 0x12, 0x80 },
1577         { 0x12, 0x14 },
1578 };
1579
1580 /* 7670. Defaults taken from OmniVision provided data,
1581 *  as provided by Jonathan Corbet of OLPC               */
1582 static const struct ov_i2c_regvals norm_7670[] = {
1583         { OV7670_R12_COM7, OV7670_COM7_RESET },
1584         { OV7670_R3A_TSLB, 0x04 },              /* OV */
1585         { OV7670_R12_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
1586         { OV7670_R11_CLKRC, 0x01 },
1587 /*
1588  * Set the hardware window.  These values from OV don't entirely
1589  * make sense - hstop is less than hstart.  But they work...
1590  */
1591         { OV7670_R17_HSTART, 0x13 },
1592         { OV7670_R18_HSTOP, 0x01 },
1593         { OV7670_R32_HREF, 0xb6 },
1594         { OV7670_R19_VSTART, 0x02 },
1595         { OV7670_R1A_VSTOP, 0x7a },
1596         { OV7670_R03_VREF, 0x0a },
1597
1598         { OV7670_R0C_COM3, 0x00 },
1599         { OV7670_R3E_COM14, 0x00 },
1600 /* Mystery scaling numbers */
1601         { 0x70, 0x3a },
1602         { 0x71, 0x35 },
1603         { 0x72, 0x11 },
1604         { 0x73, 0xf0 },
1605         { 0xa2, 0x02 },
1606 /*      { OV7670_R15_COM10, 0x0 }, */
1607
1608 /* Gamma curve values */
1609         { 0x7a, 0x20 },
1610         { 0x7b, 0x10 },
1611         { 0x7c, 0x1e },
1612         { 0x7d, 0x35 },
1613         { 0x7e, 0x5a },
1614         { 0x7f, 0x69 },
1615         { 0x80, 0x76 },
1616         { 0x81, 0x80 },
1617         { 0x82, 0x88 },
1618         { 0x83, 0x8f },
1619         { 0x84, 0x96 },
1620         { 0x85, 0xa3 },
1621         { 0x86, 0xaf },
1622         { 0x87, 0xc4 },
1623         { 0x88, 0xd7 },
1624         { 0x89, 0xe8 },
1625
1626 /* AGC and AEC parameters.  Note we start by disabling those features,
1627    then turn them only after tweaking the values. */
1628         { OV7670_R13_COM8, OV7670_COM8_FASTAEC
1629                          | OV7670_COM8_AECSTEP
1630                          | OV7670_COM8_BFILT },
1631         { OV7670_R00_GAIN, 0x00 },
1632         { OV7670_R10_AECH, 0x00 },
1633         { OV7670_R0D_COM4, 0x40 }, /* magic reserved bit */
1634         { OV7670_R14_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
1635         { OV7670_RA5_BD50MAX, 0x05 },
1636         { OV7670_RAB_BD60MAX, 0x07 },
1637         { OV7670_R24_AEW, 0x95 },
1638         { OV7670_R25_AEB, 0x33 },
1639         { OV7670_R26_VPT, 0xe3 },
1640         { OV7670_R9F_HAECC1, 0x78 },
1641         { OV7670_RA0_HAECC2, 0x68 },
1642         { 0xa1, 0x03 }, /* magic */
1643         { OV7670_RA6_HAECC3, 0xd8 },
1644         { OV7670_RA7_HAECC4, 0xd8 },
1645         { OV7670_RA8_HAECC5, 0xf0 },
1646         { OV7670_RA9_HAECC6, 0x90 },
1647         { OV7670_RAA_HAECC7, 0x94 },
1648         { OV7670_R13_COM8, OV7670_COM8_FASTAEC
1649                         | OV7670_COM8_AECSTEP
1650                         | OV7670_COM8_BFILT
1651                         | OV7670_COM8_AGC
1652                         | OV7670_COM8_AEC },
1653
1654 /* Almost all of these are magic "reserved" values.  */
1655         { OV7670_R0E_COM5, 0x61 },
1656         { OV7670_R0F_COM6, 0x4b },
1657         { 0x16, 0x02 },
1658         { OV7670_R1E_MVFP, 0x07 },
1659         { 0x21, 0x02 },
1660         { 0x22, 0x91 },
1661         { 0x29, 0x07 },
1662         { 0x33, 0x0b },
1663         { 0x35, 0x0b },
1664         { 0x37, 0x1d },
1665         { 0x38, 0x71 },
1666         { 0x39, 0x2a },
1667         { OV7670_R3C_COM12, 0x78 },
1668         { 0x4d, 0x40 },
1669         { 0x4e, 0x20 },
1670         { OV7670_R69_GFIX, 0x00 },
1671         { 0x6b, 0x4a },
1672         { 0x74, 0x10 },
1673         { 0x8d, 0x4f },
1674         { 0x8e, 0x00 },
1675         { 0x8f, 0x00 },
1676         { 0x90, 0x00 },
1677         { 0x91, 0x00 },
1678         { 0x96, 0x00 },
1679         { 0x9a, 0x00 },
1680         { 0xb0, 0x84 },
1681         { 0xb1, 0x0c },
1682         { 0xb2, 0x0e },
1683         { 0xb3, 0x82 },
1684         { 0xb8, 0x0a },
1685
1686 /* More reserved magic, some of which tweaks white balance */
1687         { 0x43, 0x0a },
1688         { 0x44, 0xf0 },
1689         { 0x45, 0x34 },
1690         { 0x46, 0x58 },
1691         { 0x47, 0x28 },
1692         { 0x48, 0x3a },
1693         { 0x59, 0x88 },
1694         { 0x5a, 0x88 },
1695         { 0x5b, 0x44 },
1696         { 0x5c, 0x67 },
1697         { 0x5d, 0x49 },
1698         { 0x5e, 0x0e },
1699         { 0x6c, 0x0a },
1700         { 0x6d, 0x55 },
1701         { 0x6e, 0x11 },
1702         { 0x6f, 0x9f },
1703                                         /* "9e for advance AWB" */
1704         { 0x6a, 0x40 },
1705         { OV7670_R01_BLUE, 0x40 },
1706         { OV7670_R02_RED, 0x60 },
1707         { OV7670_R13_COM8, OV7670_COM8_FASTAEC
1708                         | OV7670_COM8_AECSTEP
1709                         | OV7670_COM8_BFILT
1710                         | OV7670_COM8_AGC
1711                         | OV7670_COM8_AEC
1712                         | OV7670_COM8_AWB },
1713
1714 /* Matrix coefficients */
1715         { 0x4f, 0x80 },
1716         { 0x50, 0x80 },
1717         { 0x51, 0x00 },
1718         { 0x52, 0x22 },
1719         { 0x53, 0x5e },
1720         { 0x54, 0x80 },
1721         { 0x58, 0x9e },
1722
1723         { OV7670_R41_COM16, OV7670_COM16_AWBGAIN },
1724         { OV7670_R3F_EDGE, 0x00 },
1725         { 0x75, 0x05 },
1726         { 0x76, 0xe1 },
1727         { 0x4c, 0x00 },
1728         { 0x77, 0x01 },
1729         { OV7670_R3D_COM13, OV7670_COM13_GAMMA
1730                           | OV7670_COM13_UVSAT
1731                           | 2},         /* was 3 */
1732         { 0x4b, 0x09 },
1733         { 0xc9, 0x60 },
1734         { OV7670_R41_COM16, 0x38 },
1735         { 0x56, 0x40 },
1736
1737         { 0x34, 0x11 },
1738         { OV7670_R3B_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
1739         { 0xa4, 0x88 },
1740         { 0x96, 0x00 },
1741         { 0x97, 0x30 },
1742         { 0x98, 0x20 },
1743         { 0x99, 0x30 },
1744         { 0x9a, 0x84 },
1745         { 0x9b, 0x29 },
1746         { 0x9c, 0x03 },
1747         { 0x9d, 0x4c },
1748         { 0x9e, 0x3f },
1749         { 0x78, 0x04 },
1750
1751 /* Extra-weird stuff.  Some sort of multiplexor register */
1752         { 0x79, 0x01 },
1753         { 0xc8, 0xf0 },
1754         { 0x79, 0x0f },
1755         { 0xc8, 0x00 },
1756         { 0x79, 0x10 },
1757         { 0xc8, 0x7e },
1758         { 0x79, 0x0a },
1759         { 0xc8, 0x80 },
1760         { 0x79, 0x0b },
1761         { 0xc8, 0x01 },
1762         { 0x79, 0x0c },
1763         { 0xc8, 0x0f },
1764         { 0x79, 0x0d },
1765         { 0xc8, 0x20 },
1766         { 0x79, 0x09 },
1767         { 0xc8, 0x80 },
1768         { 0x79, 0x02 },
1769         { 0xc8, 0xc0 },
1770         { 0x79, 0x03 },
1771         { 0xc8, 0x40 },
1772         { 0x79, 0x05 },
1773         { 0xc8, 0x30 },
1774         { 0x79, 0x26 },
1775 };
1776
1777 static const struct ov_i2c_regvals norm_8610[] = {
1778         { 0x12, 0x80 },
1779         { 0x00, 0x00 },
1780         { 0x01, 0x80 },
1781         { 0x02, 0x80 },
1782         { 0x03, 0xc0 },
1783         { 0x04, 0x30 },
1784         { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
1785         { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
1786         { 0x0a, 0x86 },
1787         { 0x0b, 0xb0 },
1788         { 0x0c, 0x20 },
1789         { 0x0d, 0x20 },
1790         { 0x11, 0x01 },
1791         { 0x12, 0x25 },
1792         { 0x13, 0x01 },
1793         { 0x14, 0x04 },
1794         { 0x15, 0x01 }, /* Lin and Win think different about UV order */
1795         { 0x16, 0x03 },
1796         { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
1797         { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
1798         { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
1799         { 0x1a, 0xf5 },
1800         { 0x1b, 0x00 },
1801         { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
1802         { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
1803         { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
1804         { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
1805         { 0x26, 0xa2 },
1806         { 0x27, 0xea },
1807         { 0x28, 0x00 },
1808         { 0x29, 0x00 },
1809         { 0x2a, 0x80 },
1810         { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
1811         { 0x2c, 0xac },
1812         { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
1813         { 0x2e, 0x80 },
1814         { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
1815         { 0x4c, 0x00 },
1816         { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
1817         { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
1818         { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
1819         { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
1820         { 0x63, 0xff },
1821         { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
1822                          * maybe thats wrong */
1823         { 0x65, 0x00 },
1824         { 0x66, 0x55 },
1825         { 0x67, 0xb0 },
1826         { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
1827         { 0x69, 0x02 },
1828         { 0x6a, 0x22 },
1829         { 0x6b, 0x00 },
1830         { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
1831                          * deleting bit7 colors the first images red */
1832         { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
1833         { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
1834         { 0x6f, 0x01 },
1835         { 0x70, 0x8b },
1836         { 0x71, 0x00 },
1837         { 0x72, 0x14 },
1838         { 0x73, 0x54 },
1839         { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
1840         { 0x75, 0x0e },
1841         { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
1842         { 0x77, 0xff },
1843         { 0x78, 0x80 },
1844         { 0x79, 0x80 },
1845         { 0x7a, 0x80 },
1846         { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
1847         { 0x7c, 0x00 },
1848         { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
1849         { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
1850         { 0x7f, 0xfb },
1851         { 0x80, 0x28 },
1852         { 0x81, 0x00 },
1853         { 0x82, 0x23 },
1854         { 0x83, 0x0b },
1855         { 0x84, 0x00 },
1856         { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
1857         { 0x86, 0xc9 },
1858         { 0x87, 0x00 },
1859         { 0x88, 0x00 },
1860         { 0x89, 0x01 },
1861         { 0x12, 0x20 },
1862         { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
1863 };
1864
1865 static unsigned char ov7670_abs_to_sm(unsigned char v)
1866 {
1867         if (v > 127)
1868                 return v & 0x7f;
1869         return (128 - v) | 0x80;
1870 }
1871
1872 /* Write a OV519 register */
1873 static void reg_w(struct sd *sd, u16 index, u16 value)
1874 {
1875         int ret, req = 0;
1876
1877         if (sd->gspca_dev.usb_err < 0)
1878                 return;
1879
1880         switch (sd->bridge) {
1881         case BRIDGE_OV511:
1882         case BRIDGE_OV511PLUS:
1883                 req = 2;
1884                 break;
1885         case BRIDGE_OVFX2:
1886                 req = 0x0a;
1887                 /* fall through */
1888         case BRIDGE_W9968CF:
1889                 PDEBUG(D_USBO, "SET %02x %04x %04x",
1890                                 req, value, index);
1891                 ret = usb_control_msg(sd->gspca_dev.dev,
1892                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1893                         req,
1894                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1895                         value, index, NULL, 0, 500);
1896                 goto leave;
1897         default:
1898                 req = 1;
1899         }
1900
1901         PDEBUG(D_USBO, "SET %02x 0000 %04x %02x",
1902                         req, index, value);
1903         sd->gspca_dev.usb_buf[0] = value;
1904         ret = usb_control_msg(sd->gspca_dev.dev,
1905                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1906                         req,
1907                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1908                         0, index,
1909                         sd->gspca_dev.usb_buf, 1, 500);
1910 leave:
1911         if (ret < 0) {
1912                 err("reg_w %02x failed %d", index, ret);
1913                 sd->gspca_dev.usb_err = ret;
1914                 return;
1915         }
1916 }
1917
1918 /* Read from a OV519 register, note not valid for the w9968cf!! */
1919 /* returns: negative is error, pos or zero is data */
1920 static int reg_r(struct sd *sd, u16 index)
1921 {
1922         int ret;
1923         int req;
1924
1925         if (sd->gspca_dev.usb_err < 0)
1926                 return -1;
1927
1928         switch (sd->bridge) {
1929         case BRIDGE_OV511:
1930         case BRIDGE_OV511PLUS:
1931                 req = 3;
1932                 break;
1933         case BRIDGE_OVFX2:
1934                 req = 0x0b;
1935                 break;
1936         default:
1937                 req = 1;
1938         }
1939
1940         ret = usb_control_msg(sd->gspca_dev.dev,
1941                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1942                         req,
1943                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1944                         0, index, sd->gspca_dev.usb_buf, 1, 500);
1945
1946         if (ret >= 0) {
1947                 ret = sd->gspca_dev.usb_buf[0];
1948                 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x",
1949                         req, index, ret);
1950         } else {
1951                 err("reg_r %02x failed %d", index, ret);
1952                 sd->gspca_dev.usb_err = ret;
1953         }
1954
1955         return ret;
1956 }
1957
1958 /* Read 8 values from a OV519 register */
1959 static int reg_r8(struct sd *sd,
1960                   u16 index)
1961 {
1962         int ret;
1963
1964         if (sd->gspca_dev.usb_err < 0)
1965                 return -1;
1966
1967         ret = usb_control_msg(sd->gspca_dev.dev,
1968                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1969                         1,                      /* REQ_IO */
1970                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1971                         0, index, sd->gspca_dev.usb_buf, 8, 500);
1972
1973         if (ret >= 0) {
1974                 ret = sd->gspca_dev.usb_buf[0];
1975         } else {
1976                 err("reg_r8 %02x failed %d", index, ret);
1977                 sd->gspca_dev.usb_err = ret;
1978         }
1979
1980         return ret;
1981 }
1982
1983 /*
1984  * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1985  * the same position as 1's in "mask" are cleared and set to "value". Bits
1986  * that are in the same position as 0's in "mask" are preserved, regardless
1987  * of their respective state in "value".
1988  */
1989 static void reg_w_mask(struct sd *sd,
1990                         u16 index,
1991                         u8 value,
1992                         u8 mask)
1993 {
1994         int ret;
1995         u8 oldval;
1996
1997         if (mask != 0xff) {
1998                 value &= mask;                  /* Enforce mask on value */
1999                 ret = reg_r(sd, index);
2000                 if (ret < 0)
2001                         return;
2002
2003                 oldval = ret & ~mask;           /* Clear the masked bits */
2004                 value |= oldval;                /* Set the desired bits */
2005         }
2006         reg_w(sd, index, value);
2007 }
2008
2009 /*
2010  * Writes multiple (n) byte value to a single register. Only valid with certain
2011  * registers (0x30 and 0xc4 - 0xce).
2012  */
2013 static void ov518_reg_w32(struct sd *sd, u16 index, u32 value, int n)
2014 {
2015         int ret;
2016
2017         if (sd->gspca_dev.usb_err < 0)
2018                 return;
2019
2020         *((__le32 *) sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
2021
2022         ret = usb_control_msg(sd->gspca_dev.dev,
2023                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2024                         1 /* REG_IO */,
2025                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2026                         0, index,
2027                         sd->gspca_dev.usb_buf, n, 500);
2028         if (ret < 0) {
2029                 err("reg_w32 %02x failed %d", index, ret);
2030                 sd->gspca_dev.usb_err = ret;
2031         }
2032 }
2033
2034 static void ov511_i2c_w(struct sd *sd, u8 reg, u8 value)
2035 {
2036         int rc, retries;
2037
2038         PDEBUG(D_USBO, "ov511_i2c_w %02x %02x", reg, value);
2039
2040         /* Three byte write cycle */
2041         for (retries = 6; ; ) {
2042                 /* Select camera register */
2043                 reg_w(sd, R51x_I2C_SADDR_3, reg);
2044
2045                 /* Write "value" to I2C data port of OV511 */
2046                 reg_w(sd, R51x_I2C_DATA, value);
2047
2048                 /* Initiate 3-byte write cycle */
2049                 reg_w(sd, R511_I2C_CTL, 0x01);
2050
2051                 do {
2052                         rc = reg_r(sd, R511_I2C_CTL);
2053                 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2054
2055                 if (rc < 0)
2056                         return;
2057
2058                 if ((rc & 2) == 0) /* Ack? */
2059                         break;
2060                 if (--retries < 0) {
2061                         PDEBUG(D_USBO, "i2c write retries exhausted");
2062                         return;
2063                 }
2064         }
2065 }
2066
2067 static int ov511_i2c_r(struct sd *sd, u8 reg)
2068 {
2069         int rc, value, retries;
2070
2071         /* Two byte write cycle */
2072         for (retries = 6; ; ) {
2073                 /* Select camera register */
2074                 reg_w(sd, R51x_I2C_SADDR_2, reg);
2075
2076                 /* Initiate 2-byte write cycle */
2077                 reg_w(sd, R511_I2C_CTL, 0x03);
2078
2079                 do {
2080                         rc = reg_r(sd, R511_I2C_CTL);
2081                 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2082
2083                 if (rc < 0)
2084                         return rc;
2085
2086                 if ((rc & 2) == 0) /* Ack? */
2087                         break;
2088
2089                 /* I2C abort */
2090                 reg_w(sd, R511_I2C_CTL, 0x10);
2091
2092                 if (--retries < 0) {
2093                         PDEBUG(D_USBI, "i2c write retries exhausted");
2094                         return -1;
2095                 }
2096         }
2097
2098         /* Two byte read cycle */
2099         for (retries = 6; ; ) {
2100                 /* Initiate 2-byte read cycle */
2101                 reg_w(sd, R511_I2C_CTL, 0x05);
2102
2103                 do {
2104                         rc = reg_r(sd, R511_I2C_CTL);
2105                 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2106
2107                 if (rc < 0)
2108                         return rc;
2109
2110                 if ((rc & 2) == 0) /* Ack? */
2111                         break;
2112
2113                 /* I2C abort */
2114                 reg_w(sd, R511_I2C_CTL, 0x10);
2115
2116                 if (--retries < 0) {
2117                         PDEBUG(D_USBI, "i2c read retries exhausted");
2118                         return -1;
2119                 }
2120         }
2121
2122         value = reg_r(sd, R51x_I2C_DATA);
2123
2124         PDEBUG(D_USBI, "ov511_i2c_r %02x %02x", reg, value);
2125
2126         /* This is needed to make i2c_w() work */
2127         reg_w(sd, R511_I2C_CTL, 0x05);
2128
2129         return value;
2130 }
2131
2132 /*
2133  * The OV518 I2C I/O procedure is different, hence, this function.
2134  * This is normally only called from i2c_w(). Note that this function
2135  * always succeeds regardless of whether the sensor is present and working.
2136  */
2137 static void ov518_i2c_w(struct sd *sd,
2138                 u8 reg,
2139                 u8 value)
2140 {
2141         PDEBUG(D_USBO, "ov518_i2c_w %02x %02x", reg, value);
2142
2143         /* Select camera register */
2144         reg_w(sd, R51x_I2C_SADDR_3, reg);
2145
2146         /* Write "value" to I2C data port of OV511 */
2147         reg_w(sd, R51x_I2C_DATA, value);
2148
2149         /* Initiate 3-byte write cycle */
2150         reg_w(sd, R518_I2C_CTL, 0x01);
2151
2152         /* wait for write complete */
2153         msleep(4);
2154         reg_r8(sd, R518_I2C_CTL);
2155 }
2156
2157 /*
2158  * returns: negative is error, pos or zero is data
2159  *
2160  * The OV518 I2C I/O procedure is different, hence, this function.
2161  * This is normally only called from i2c_r(). Note that this function
2162  * always succeeds regardless of whether the sensor is present and working.
2163  */
2164 static int ov518_i2c_r(struct sd *sd, u8 reg)
2165 {
2166         int value;
2167
2168         /* Select camera register */
2169         reg_w(sd, R51x_I2C_SADDR_2, reg);
2170
2171         /* Initiate 2-byte write cycle */
2172         reg_w(sd, R518_I2C_CTL, 0x03);
2173
2174         /* Initiate 2-byte read cycle */
2175         reg_w(sd, R518_I2C_CTL, 0x05);
2176         value = reg_r(sd, R51x_I2C_DATA);
2177         PDEBUG(D_USBI, "ov518_i2c_r %02x %02x", reg, value);
2178         return value;
2179 }
2180
2181 static void ovfx2_i2c_w(struct sd *sd, u8 reg, u8 value)
2182 {
2183         int ret;
2184
2185         if (sd->gspca_dev.usb_err < 0)
2186                 return;
2187
2188         ret = usb_control_msg(sd->gspca_dev.dev,
2189                         usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2190                         0x02,
2191                         USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2192                         (u16) value, (u16) reg, NULL, 0, 500);
2193
2194         if (ret < 0) {
2195                 err("ovfx2_i2c_w %02x failed %d", reg, ret);
2196                 sd->gspca_dev.usb_err = ret;
2197         }
2198
2199         PDEBUG(D_USBO, "ovfx2_i2c_w %02x %02x", reg, value);
2200 }
2201
2202 static int ovfx2_i2c_r(struct sd *sd, u8 reg)
2203 {
2204         int ret;
2205
2206         if (sd->gspca_dev.usb_err < 0)
2207                 return -1;
2208
2209         ret = usb_control_msg(sd->gspca_dev.dev,
2210                         usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
2211                         0x03,
2212                         USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2213                         0, (u16) reg, sd->gspca_dev.usb_buf, 1, 500);
2214
2215         if (ret >= 0) {
2216                 ret = sd->gspca_dev.usb_buf[0];
2217                 PDEBUG(D_USBI, "ovfx2_i2c_r %02x %02x", reg, ret);
2218         } else {
2219                 err("ovfx2_i2c_r %02x failed %d", reg, ret);
2220                 sd->gspca_dev.usb_err = ret;
2221         }
2222
2223         return ret;
2224 }
2225
2226 static void i2c_w(struct sd *sd, u8 reg, u8 value)
2227 {
2228         if (sd->sensor_reg_cache[reg] == value)
2229                 return;
2230
2231         switch (sd->bridge) {
2232         case BRIDGE_OV511:
2233         case BRIDGE_OV511PLUS:
2234                 ov511_i2c_w(sd, reg, value);
2235                 break;
2236         case BRIDGE_OV518:
2237         case BRIDGE_OV518PLUS:
2238         case BRIDGE_OV519:
2239                 ov518_i2c_w(sd, reg, value);
2240                 break;
2241         case BRIDGE_OVFX2:
2242                 ovfx2_i2c_w(sd, reg, value);
2243                 break;
2244         case BRIDGE_W9968CF:
2245                 w9968cf_i2c_w(sd, reg, value);
2246                 break;
2247         }
2248
2249         if (sd->gspca_dev.usb_err >= 0) {
2250                 /* Up on sensor reset empty the register cache */
2251                 if (reg == 0x12 && (value & 0x80))
2252                         memset(sd->sensor_reg_cache, -1,
2253                                 sizeof(sd->sensor_reg_cache));
2254                 else
2255                         sd->sensor_reg_cache[reg] = value;
2256         }
2257 }
2258
2259 static int i2c_r(struct sd *sd, u8 reg)
2260 {
2261         int ret = -1;
2262
2263         if (sd->sensor_reg_cache[reg] != -1)
2264                 return sd->sensor_reg_cache[reg];
2265
2266         switch (sd->bridge) {
2267         case BRIDGE_OV511:
2268         case BRIDGE_OV511PLUS:
2269                 ret = ov511_i2c_r(sd, reg);
2270                 break;
2271         case BRIDGE_OV518:
2272         case BRIDGE_OV518PLUS:
2273         case BRIDGE_OV519:
2274                 ret = ov518_i2c_r(sd, reg);
2275                 break;
2276         case BRIDGE_OVFX2:
2277                 ret = ovfx2_i2c_r(sd, reg);
2278                 break;
2279         case BRIDGE_W9968CF:
2280                 ret = w9968cf_i2c_r(sd, reg);
2281                 break;
2282         }
2283
2284         if (ret >= 0)
2285                 sd->sensor_reg_cache[reg] = ret;
2286
2287         return ret;
2288 }
2289
2290 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
2291  * the same position as 1's in "mask" are cleared and set to "value". Bits
2292  * that are in the same position as 0's in "mask" are preserved, regardless
2293  * of their respective state in "value".
2294  */
2295 static void i2c_w_mask(struct sd *sd,
2296                         u8 reg,
2297                         u8 value,
2298                         u8 mask)
2299 {
2300         int rc;
2301         u8 oldval;
2302
2303         value &= mask;                  /* Enforce mask on value */
2304         rc = i2c_r(sd, reg);
2305         if (rc < 0)
2306                 return;
2307         oldval = rc & ~mask;            /* Clear the masked bits */
2308         value |= oldval;                /* Set the desired bits */
2309         i2c_w(sd, reg, value);
2310 }
2311
2312 /* Temporarily stops OV511 from functioning. Must do this before changing
2313  * registers while the camera is streaming */
2314 static inline void ov51x_stop(struct sd *sd)
2315 {
2316         PDEBUG(D_STREAM, "stopping");
2317         sd->stopped = 1;
2318         switch (sd->bridge) {
2319         case BRIDGE_OV511:
2320         case BRIDGE_OV511PLUS:
2321                 reg_w(sd, R51x_SYS_RESET, 0x3d);
2322                 break;
2323         case BRIDGE_OV518:
2324         case BRIDGE_OV518PLUS:
2325                 reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
2326                 break;
2327         case BRIDGE_OV519:
2328                 reg_w(sd, OV519_R51_RESET1, 0x0f);
2329                 reg_w(sd, OV519_R51_RESET1, 0x00);
2330                 reg_w(sd, 0x22, 0x00);          /* FRAR */
2331                 break;
2332         case BRIDGE_OVFX2:
2333                 reg_w_mask(sd, 0x0f, 0x00, 0x02);
2334                 break;
2335         case BRIDGE_W9968CF:
2336                 reg_w(sd, 0x3c, 0x0a05); /* stop USB transfer */
2337                 break;
2338         }
2339 }
2340
2341 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
2342  * actually stopped (for performance). */
2343 static inline void ov51x_restart(struct sd *sd)
2344 {
2345         PDEBUG(D_STREAM, "restarting");
2346         if (!sd->stopped)
2347                 return;
2348         sd->stopped = 0;
2349
2350         /* Reinitialize the stream */
2351         switch (sd->bridge) {
2352         case BRIDGE_OV511:
2353         case BRIDGE_OV511PLUS:
2354                 reg_w(sd, R51x_SYS_RESET, 0x00);
2355                 break;
2356         case BRIDGE_OV518:
2357         case BRIDGE_OV518PLUS:
2358                 reg_w(sd, 0x2f, 0x80);
2359                 reg_w(sd, R51x_SYS_RESET, 0x00);
2360                 break;
2361         case BRIDGE_OV519:
2362                 reg_w(sd, OV519_R51_RESET1, 0x0f);
2363                 reg_w(sd, OV519_R51_RESET1, 0x00);
2364                 reg_w(sd, 0x22, 0x1d);          /* FRAR */
2365                 break;
2366         case BRIDGE_OVFX2:
2367                 reg_w_mask(sd, 0x0f, 0x02, 0x02);
2368                 break;
2369         case BRIDGE_W9968CF:
2370                 reg_w(sd, 0x3c, 0x8a05); /* USB FIFO enable */
2371                 break;
2372         }
2373 }
2374
2375 static void ov51x_set_slave_ids(struct sd *sd, u8 slave);
2376
2377 /* This does an initial reset of an OmniVision sensor and ensures that I2C
2378  * is synchronized. Returns <0 on failure.
2379  */
2380 static int init_ov_sensor(struct sd *sd, u8 slave)
2381 {
2382         int i;
2383
2384         ov51x_set_slave_ids(sd, slave);
2385
2386         /* Reset the sensor */
2387         i2c_w(sd, 0x12, 0x80);
2388
2389         /* Wait for it to initialize */
2390         msleep(150);
2391
2392         for (i = 0; i < i2c_detect_tries; i++) {
2393                 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
2394                     i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
2395                         PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
2396                         return 0;
2397                 }
2398
2399                 /* Reset the sensor */
2400                 i2c_w(sd, 0x12, 0x80);
2401
2402                 /* Wait for it to initialize */
2403                 msleep(150);
2404
2405                 /* Dummy read to sync I2C */
2406                 if (i2c_r(sd, 0x00) < 0)
2407                         return -1;
2408         }
2409         return -1;
2410 }
2411
2412 /* Set the read and write slave IDs. The "slave" argument is the write slave,
2413  * and the read slave will be set to (slave + 1).
2414  * This should not be called from outside the i2c I/O functions.
2415  * Sets I2C read and write slave IDs. Returns <0 for error
2416  */
2417 static void ov51x_set_slave_ids(struct sd *sd,
2418                                 u8 slave)
2419 {
2420         switch (sd->bridge) {
2421         case BRIDGE_OVFX2:
2422                 reg_w(sd, OVFX2_I2C_ADDR, slave);
2423                 return;
2424         case BRIDGE_W9968CF:
2425                 sd->sensor_addr = slave;
2426                 return;
2427         }
2428
2429         reg_w(sd, R51x_I2C_W_SID, slave);
2430         reg_w(sd, R51x_I2C_R_SID, slave + 1);
2431 }
2432
2433 static void write_regvals(struct sd *sd,
2434                          const struct ov_regvals *regvals,
2435                          int n)
2436 {
2437         while (--n >= 0) {
2438                 reg_w(sd, regvals->reg, regvals->val);
2439                 regvals++;
2440         }
2441 }
2442
2443 static void write_i2c_regvals(struct sd *sd,
2444                         const struct ov_i2c_regvals *regvals,
2445                         int n)
2446 {
2447         while (--n >= 0) {
2448                 i2c_w(sd, regvals->reg, regvals->val);
2449                 regvals++;
2450         }
2451 }
2452
2453 /****************************************************************************
2454  *
2455  * OV511 and sensor configuration
2456  *
2457  ***************************************************************************/
2458
2459 /* This initializes the OV2x10 / OV3610 / OV3620 */
2460 static void ov_hires_configure(struct sd *sd)
2461 {
2462         int high, low;
2463
2464         if (sd->bridge != BRIDGE_OVFX2) {
2465                 err("error hires sensors only supported with ovfx2");
2466                 return;
2467         }
2468
2469         PDEBUG(D_PROBE, "starting ov hires configuration");
2470
2471         /* Detect sensor (sub)type */
2472         high = i2c_r(sd, 0x0a);
2473         low = i2c_r(sd, 0x0b);
2474         /* info("%x, %x", high, low); */
2475         if (high == 0x96 && low == 0x40) {
2476                 PDEBUG(D_PROBE, "Sensor is an OV2610");
2477                 sd->sensor = SEN_OV2610;
2478         } else if (high == 0x36 && (low & 0x0f) == 0x00) {
2479                 PDEBUG(D_PROBE, "Sensor is an OV3610");
2480                 sd->sensor = SEN_OV3610;
2481         } else {
2482                 err("Error unknown sensor type: %02x%02x",
2483                         high, low);
2484         }
2485 }
2486
2487 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
2488  * the same register settings as the OV8610, since they are very similar.
2489  */
2490 static void ov8xx0_configure(struct sd *sd)
2491 {
2492         int rc;
2493
2494         PDEBUG(D_PROBE, "starting ov8xx0 configuration");
2495
2496         /* Detect sensor (sub)type */
2497         rc = i2c_r(sd, OV7610_REG_COM_I);
2498         if (rc < 0) {
2499                 PDEBUG(D_ERR, "Error detecting sensor type");
2500                 return;
2501         }
2502         if ((rc & 3) == 1)
2503                 sd->sensor = SEN_OV8610;
2504         else
2505                 err("Unknown image sensor version: %d", rc & 3);
2506 }
2507
2508 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
2509  * the same register settings as the OV7610, since they are very similar.
2510  */
2511 static void ov7xx0_configure(struct sd *sd)
2512 {
2513         int rc, high, low;
2514
2515         PDEBUG(D_PROBE, "starting OV7xx0 configuration");
2516
2517         /* Detect sensor (sub)type */
2518         rc = i2c_r(sd, OV7610_REG_COM_I);
2519
2520         /* add OV7670 here
2521          * it appears to be wrongly detected as a 7610 by default */
2522         if (rc < 0) {
2523                 PDEBUG(D_ERR, "Error detecting sensor type");
2524                 return;
2525         }
2526         if ((rc & 3) == 3) {
2527                 /* quick hack to make OV7670s work */
2528                 high = i2c_r(sd, 0x0a);
2529                 low = i2c_r(sd, 0x0b);
2530                 /* info("%x, %x", high, low); */
2531                 if (high == 0x76 && low == 0x73) {
2532                         PDEBUG(D_PROBE, "Sensor is an OV7670");
2533                         sd->sensor = SEN_OV7670;
2534                 } else {
2535                         PDEBUG(D_PROBE, "Sensor is an OV7610");
2536                         sd->sensor = SEN_OV7610;
2537                 }
2538         } else if ((rc & 3) == 1) {
2539                 /* I don't know what's different about the 76BE yet. */
2540                 if (i2c_r(sd, 0x15) & 1) {
2541                         PDEBUG(D_PROBE, "Sensor is an OV7620AE");
2542                         sd->sensor = SEN_OV7620AE;
2543                 } else {
2544                         PDEBUG(D_PROBE, "Sensor is an OV76BE");
2545                         sd->sensor = SEN_OV76BE;
2546                 }
2547         } else if ((rc & 3) == 0) {
2548                 /* try to read product id registers */
2549                 high = i2c_r(sd, 0x0a);
2550                 if (high < 0) {
2551                         PDEBUG(D_ERR, "Error detecting camera chip PID");
2552                         return;
2553                 }
2554                 low = i2c_r(sd, 0x0b);
2555                 if (low < 0) {
2556                         PDEBUG(D_ERR, "Error detecting camera chip VER");
2557                         return;
2558                 }
2559                 if (high == 0x76) {
2560                         switch (low) {
2561                         case 0x30:
2562                                 err("Sensor is an OV7630/OV7635");
2563                                 err("7630 is not supported by this driver");
2564                                 return;
2565                         case 0x40:
2566                                 PDEBUG(D_PROBE, "Sensor is an OV7645");
2567                                 sd->sensor = SEN_OV7640; /* FIXME */
2568                                 break;
2569                         case 0x45:
2570                                 PDEBUG(D_PROBE, "Sensor is an OV7645B");
2571                                 sd->sensor = SEN_OV7640; /* FIXME */
2572                                 break;
2573                         case 0x48:
2574                                 PDEBUG(D_PROBE, "Sensor is an OV7648");
2575                                 sd->sensor = SEN_OV7648;
2576                                 break;
2577                         default:
2578                                 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
2579                                 return;
2580                         }
2581                 } else {
2582                         PDEBUG(D_PROBE, "Sensor is an OV7620");
2583                         sd->sensor = SEN_OV7620;
2584                 }
2585         } else {
2586                 err("Unknown image sensor version: %d", rc & 3);
2587         }
2588 }
2589
2590 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
2591 static void ov6xx0_configure(struct sd *sd)
2592 {
2593         int rc;
2594         PDEBUG(D_PROBE, "starting OV6xx0 configuration");
2595
2596         /* Detect sensor (sub)type */
2597         rc = i2c_r(sd, OV7610_REG_COM_I);
2598         if (rc < 0) {
2599                 PDEBUG(D_ERR, "Error detecting sensor type");
2600                 return;
2601         }
2602
2603         /* Ugh. The first two bits are the version bits, but
2604          * the entire register value must be used. I guess OVT
2605          * underestimated how many variants they would make. */
2606         switch (rc) {
2607         case 0x00:
2608                 sd->sensor = SEN_OV6630;
2609                 warn("WARNING: Sensor is an OV66308. Your camera may have");
2610                 warn("been misdetected in previous driver versions.");
2611                 break;
2612         case 0x01:
2613                 sd->sensor = SEN_OV6620;
2614                 PDEBUG(D_PROBE, "Sensor is an OV6620");
2615                 break;
2616         case 0x02:
2617                 sd->sensor = SEN_OV6630;
2618                 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
2619                 break;
2620         case 0x03:
2621                 sd->sensor = SEN_OV66308AF;
2622                 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
2623                 break;
2624         case 0x90:
2625                 sd->sensor = SEN_OV6630;
2626                 warn("WARNING: Sensor is an OV66307. Your camera may have");
2627                 warn("been misdetected in previous driver versions.");
2628                 break;
2629         default:
2630                 err("FATAL: Unknown sensor version: 0x%02x", rc);
2631                 return;
2632         }
2633
2634         /* Set sensor-specific vars */
2635         sd->sif = 1;
2636 }
2637
2638 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
2639 static void ov51x_led_control(struct sd *sd, int on)
2640 {
2641         if (sd->invert_led)
2642                 on = !on;
2643
2644         switch (sd->bridge) {
2645         /* OV511 has no LED control */
2646         case BRIDGE_OV511PLUS:
2647                 reg_w(sd, R511_SYS_LED_CTL, on);
2648                 break;
2649         case BRIDGE_OV518:
2650         case BRIDGE_OV518PLUS:
2651                 reg_w_mask(sd, R518_GPIO_OUT, 0x02 * on, 0x02);
2652                 break;
2653         case BRIDGE_OV519:
2654                 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, on, 1);
2655                 break;
2656         }
2657 }
2658
2659 static void sd_reset_snapshot(struct gspca_dev *gspca_dev)
2660 {
2661         struct sd *sd = (struct sd *) gspca_dev;
2662
2663         if (!sd->snapshot_needs_reset)
2664                 return;
2665
2666         /* Note it is important that we clear sd->snapshot_needs_reset,
2667            before actually clearing the snapshot state in the bridge
2668            otherwise we might race with the pkt_scan interrupt handler */
2669         sd->snapshot_needs_reset = 0;
2670
2671         switch (sd->bridge) {
2672         case BRIDGE_OV511:
2673         case BRIDGE_OV511PLUS:
2674                 reg_w(sd, R51x_SYS_SNAP, 0x02);
2675                 reg_w(sd, R51x_SYS_SNAP, 0x00);
2676                 break;
2677         case BRIDGE_OV518:
2678         case BRIDGE_OV518PLUS:
2679                 reg_w(sd, R51x_SYS_SNAP, 0x02); /* Reset */
2680                 reg_w(sd, R51x_SYS_SNAP, 0x01); /* Enable */
2681                 break;
2682         case BRIDGE_OV519:
2683                 reg_w(sd, R51x_SYS_RESET, 0x40);
2684                 reg_w(sd, R51x_SYS_RESET, 0x00);
2685                 break;
2686         }
2687 }
2688
2689 static void ov51x_upload_quan_tables(struct sd *sd)
2690 {
2691         const unsigned char yQuanTable511[] = {
2692                 0, 1, 1, 2, 2, 3, 3, 4,
2693                 1, 1, 1, 2, 2, 3, 4, 4,
2694                 1, 1, 2, 2, 3, 4, 4, 4,
2695                 2, 2, 2, 3, 4, 4, 4, 4,
2696                 2, 2, 3, 4, 4, 5, 5, 5,
2697                 3, 3, 4, 4, 5, 5, 5, 5,
2698                 3, 4, 4, 4, 5, 5, 5, 5,
2699                 4, 4, 4, 4, 5, 5, 5, 5
2700         };
2701
2702         const unsigned char uvQuanTable511[] = {
2703                 0, 2, 2, 3, 4, 4, 4, 4,
2704                 2, 2, 2, 4, 4, 4, 4, 4,
2705                 2, 2, 3, 4, 4, 4, 4, 4,
2706                 3, 4, 4, 4, 4, 4, 4, 4,
2707                 4, 4, 4, 4, 4, 4, 4, 4,
2708                 4, 4, 4, 4, 4, 4, 4, 4,
2709                 4, 4, 4, 4, 4, 4, 4, 4,
2710                 4, 4, 4, 4, 4, 4, 4, 4
2711         };
2712
2713         /* OV518 quantization tables are 8x4 (instead of 8x8) */
2714         const unsigned char yQuanTable518[] = {
2715                 5, 4, 5, 6, 6, 7, 7, 7,
2716                 5, 5, 5, 5, 6, 7, 7, 7,
2717                 6, 6, 6, 6, 7, 7, 7, 8,
2718                 7, 7, 6, 7, 7, 7, 8, 8
2719         };
2720         const unsigned char uvQuanTable518[] = {
2721                 6, 6, 6, 7, 7, 7, 7, 7,
2722                 6, 6, 6, 7, 7, 7, 7, 7,
2723                 6, 6, 6, 7, 7, 7, 7, 8,
2724                 7, 7, 7, 7, 7, 7, 8, 8
2725         };
2726
2727         const unsigned char *pYTable, *pUVTable;
2728         unsigned char val0, val1;
2729         int i, size, reg = R51x_COMP_LUT_BEGIN;
2730
2731         PDEBUG(D_PROBE, "Uploading quantization tables");
2732
2733         if (sd->bridge == BRIDGE_OV511 || sd->bridge == BRIDGE_OV511PLUS) {
2734                 pYTable = yQuanTable511;
2735                 pUVTable = uvQuanTable511;
2736                 size = 32;
2737         } else {
2738                 pYTable = yQuanTable518;
2739                 pUVTable = uvQuanTable518;
2740                 size = 16;
2741         }
2742
2743         for (i = 0; i < size; i++) {
2744                 val0 = *pYTable++;
2745                 val1 = *pYTable++;
2746                 val0 &= 0x0f;
2747                 val1 &= 0x0f;
2748                 val0 |= val1 << 4;
2749                 reg_w(sd, reg, val0);
2750
2751                 val0 = *pUVTable++;
2752                 val1 = *pUVTable++;
2753                 val0 &= 0x0f;
2754                 val1 &= 0x0f;
2755                 val0 |= val1 << 4;
2756                 reg_w(sd, reg + size, val0);
2757
2758                 reg++;
2759         }
2760 }
2761
2762 /* This initializes the OV511/OV511+ and the sensor */
2763 static void ov511_configure(struct gspca_dev *gspca_dev)
2764 {
2765         struct sd *sd = (struct sd *) gspca_dev;
2766
2767         /* For 511 and 511+ */
2768         const struct ov_regvals init_511[] = {
2769                 { R51x_SYS_RESET,       0x7f },
2770                 { R51x_SYS_INIT,        0x01 },
2771                 { R51x_SYS_RESET,       0x7f },
2772                 { R51x_SYS_INIT,        0x01 },
2773                 { R51x_SYS_RESET,       0x3f },
2774                 { R51x_SYS_INIT,        0x01 },
2775                 { R51x_SYS_RESET,       0x3d },
2776         };
2777
2778         const struct ov_regvals norm_511[] = {
2779                 { R511_DRAM_FLOW_CTL,   0x01 },
2780                 { R51x_SYS_SNAP,        0x00 },
2781                 { R51x_SYS_SNAP,        0x02 },
2782                 { R51x_SYS_SNAP,        0x00 },
2783                 { R511_FIFO_OPTS,       0x1f },
2784                 { R511_COMP_EN,         0x00 },
2785                 { R511_COMP_LUT_EN,     0x03 },
2786         };
2787
2788         const struct ov_regvals norm_511_p[] = {
2789                 { R511_DRAM_FLOW_CTL,   0xff },
2790                 { R51x_SYS_SNAP,        0x00 },
2791                 { R51x_SYS_SNAP,        0x02 },
2792                 { R51x_SYS_SNAP,        0x00 },
2793                 { R511_FIFO_OPTS,       0xff },
2794                 { R511_COMP_EN,         0x00 },
2795                 { R511_COMP_LUT_EN,     0x03 },
2796         };
2797
2798         const struct ov_regvals compress_511[] = {
2799                 { 0x70, 0x1f },
2800                 { 0x71, 0x05 },
2801                 { 0x72, 0x06 },
2802                 { 0x73, 0x06 },
2803                 { 0x74, 0x14 },
2804                 { 0x75, 0x03 },
2805                 { 0x76, 0x04 },
2806                 { 0x77, 0x04 },
2807         };
2808
2809         PDEBUG(D_PROBE, "Device custom id %x", reg_r(sd, R51x_SYS_CUST_ID));
2810
2811         write_regvals(sd, init_511, ARRAY_SIZE(init_511));
2812
2813         switch (sd->bridge) {
2814         case BRIDGE_OV511:
2815                 write_regvals(sd, norm_511, ARRAY_SIZE(norm_511));
2816                 break;
2817         case BRIDGE_OV511PLUS:
2818                 write_regvals(sd, norm_511_p, ARRAY_SIZE(norm_511_p));
2819                 break;
2820         }
2821
2822         /* Init compression */
2823         write_regvals(sd, compress_511, ARRAY_SIZE(compress_511));
2824
2825         ov51x_upload_quan_tables(sd);
2826 }
2827
2828 /* This initializes the OV518/OV518+ and the sensor */
2829 static void ov518_configure(struct gspca_dev *gspca_dev)
2830 {
2831         struct sd *sd = (struct sd *) gspca_dev;
2832
2833         /* For 518 and 518+ */
2834         const struct ov_regvals init_518[] = {
2835                 { R51x_SYS_RESET,       0x40 },
2836                 { R51x_SYS_INIT,        0xe1 },
2837                 { R51x_SYS_RESET,       0x3e },
2838                 { R51x_SYS_INIT,        0xe1 },
2839                 { R51x_SYS_RESET,       0x00 },
2840                 { R51x_SYS_INIT,        0xe1 },
2841                 { 0x46,                 0x00 },
2842                 { 0x5d,                 0x03 },
2843         };
2844
2845         const struct ov_regvals norm_518[] = {
2846                 { R51x_SYS_SNAP,        0x02 }, /* Reset */
2847                 { R51x_SYS_SNAP,        0x01 }, /* Enable */
2848                 { 0x31,                 0x0f },
2849                 { 0x5d,                 0x03 },
2850                 { 0x24,                 0x9f },
2851                 { 0x25,                 0x90 },
2852                 { 0x20,                 0x00 },
2853                 { 0x51,                 0x04 },
2854                 { 0x71,                 0x19 },
2855                 { 0x2f,                 0x80 },
2856         };
2857
2858         const struct ov_regvals norm_518_p[] = {
2859                 { R51x_SYS_SNAP,        0x02 }, /* Reset */
2860                 { R51x_SYS_SNAP,        0x01 }, /* Enable */
2861                 { 0x31,                 0x0f },
2862                 { 0x5d,                 0x03 },
2863                 { 0x24,                 0x9f },
2864                 { 0x25,                 0x90 },
2865                 { 0x20,                 0x60 },
2866                 { 0x51,                 0x02 },
2867                 { 0x71,                 0x19 },
2868                 { 0x40,                 0xff },
2869                 { 0x41,                 0x42 },
2870                 { 0x46,                 0x00 },
2871                 { 0x33,                 0x04 },
2872                 { 0x21,                 0x19 },
2873                 { 0x3f,                 0x10 },
2874                 { 0x2f,                 0x80 },
2875         };
2876
2877         /* First 5 bits of custom ID reg are a revision ID on OV518 */
2878         PDEBUG(D_PROBE, "Device revision %d",
2879                 0x1f & reg_r(sd, R51x_SYS_CUST_ID));
2880
2881         write_regvals(sd, init_518, ARRAY_SIZE(init_518));
2882
2883         /* Set LED GPIO pin to output mode */
2884         reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
2885
2886         switch (sd->bridge) {
2887         case BRIDGE_OV518:
2888                 write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
2889                 break;
2890         case BRIDGE_OV518PLUS:
2891                 write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
2892                 break;
2893         }
2894
2895         ov51x_upload_quan_tables(sd);
2896
2897         reg_w(sd, 0x2f, 0x80);
2898 }
2899
2900 static void ov519_configure(struct sd *sd)
2901 {
2902         static const struct ov_regvals init_519[] = {
2903                 { 0x5a, 0x6d }, /* EnableSystem */
2904                 { 0x53, 0x9b },
2905                 { OV519_R54_EN_CLK1, 0xff }, /* set bit2 to enable jpeg */
2906                 { 0x5d, 0x03 },
2907                 { 0x49, 0x01 },
2908                 { 0x48, 0x00 },
2909                 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
2910                  * detection will fail. This deserves further investigation. */
2911                 { OV519_GPIO_IO_CTRL0,   0xee },
2912                 { OV519_R51_RESET1, 0x0f },
2913                 { OV519_R51_RESET1, 0x00 },
2914                 { 0x22, 0x00 },
2915                 /* windows reads 0x55 at this point*/
2916         };
2917
2918         write_regvals(sd, init_519, ARRAY_SIZE(init_519));
2919 }
2920
2921 static void ovfx2_configure(struct sd *sd)
2922 {
2923         static const struct ov_regvals init_fx2[] = {
2924                 { 0x00, 0x60 },
2925                 { 0x02, 0x01 },
2926                 { 0x0f, 0x1d },
2927                 { 0xe9, 0x82 },
2928                 { 0xea, 0xc7 },
2929                 { 0xeb, 0x10 },
2930                 { 0xec, 0xf6 },
2931         };
2932
2933         sd->stopped = 1;
2934
2935         write_regvals(sd, init_fx2, ARRAY_SIZE(init_fx2));
2936 }
2937
2938 /* this function is called at probe time */
2939 static int sd_config(struct gspca_dev *gspca_dev,
2940                         const struct usb_device_id *id)
2941 {
2942         struct sd *sd = (struct sd *) gspca_dev;
2943         struct cam *cam = &gspca_dev->cam;
2944
2945         sd->bridge = id->driver_info & BRIDGE_MASK;
2946         sd->invert_led = (id->driver_info & BRIDGE_INVERT_LED) != 0;
2947
2948         switch (sd->bridge) {
2949         case BRIDGE_OV511:
2950         case BRIDGE_OV511PLUS:
2951                 cam->cam_mode = ov511_vga_mode;
2952                 cam->nmodes = ARRAY_SIZE(ov511_vga_mode);
2953                 break;
2954         case BRIDGE_OV518:
2955         case BRIDGE_OV518PLUS:
2956                 cam->cam_mode = ov518_vga_mode;
2957                 cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
2958                 break;
2959         case BRIDGE_OV519:
2960                 cam->cam_mode = ov519_vga_mode;
2961                 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
2962                 sd->invert_led = !sd->invert_led;
2963                 break;
2964         case BRIDGE_OVFX2:
2965                 cam->cam_mode = ov519_vga_mode;
2966                 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
2967                 cam->bulk_size = OVFX2_BULK_SIZE;
2968                 cam->bulk_nurbs = MAX_NURBS;
2969                 cam->bulk = 1;
2970                 break;
2971         case BRIDGE_W9968CF:
2972                 cam->cam_mode = w9968cf_vga_mode;
2973                 cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode);
2974                 cam->reverse_alts = 1;
2975                 break;
2976         }
2977
2978         gspca_dev->cam.ctrls = sd->ctrls;
2979         sd->quality = QUALITY_DEF;
2980
2981         return 0;
2982 }
2983
2984 /* this function is called at probe and resume time */
2985 static int sd_init(struct gspca_dev *gspca_dev)
2986 {
2987         struct sd *sd = (struct sd *) gspca_dev;
2988         struct cam *cam = &gspca_dev->cam;
2989
2990         switch (sd->bridge) {
2991         case BRIDGE_OV511:
2992         case BRIDGE_OV511PLUS:
2993                 ov511_configure(gspca_dev);
2994                 break;
2995         case BRIDGE_OV518:
2996         case BRIDGE_OV518PLUS:
2997                 ov518_configure(gspca_dev);
2998                 break;
2999         case BRIDGE_OV519:
3000                 ov519_configure(sd);
3001                 break;
3002         case BRIDGE_OVFX2:
3003                 ovfx2_configure(sd);
3004                 break;
3005         case BRIDGE_W9968CF:
3006                 w9968cf_configure(sd);
3007                 break;
3008         }
3009
3010         /* The OV519 must be more aggressive about sensor detection since
3011          * I2C write will never fail if the sensor is not present. We have
3012          * to try to initialize the sensor to detect its presence */
3013         sd->sensor = -1;
3014
3015         /* Test for 76xx */
3016         if (init_ov_sensor(sd, OV7xx0_SID) >= 0) {
3017                 ov7xx0_configure(sd);
3018
3019         /* Test for 6xx0 */
3020         } else if (init_ov_sensor(sd, OV6xx0_SID) >= 0) {
3021                 ov6xx0_configure(sd);
3022
3023         /* Test for 8xx0 */
3024         } else if (init_ov_sensor(sd, OV8xx0_SID) >= 0) {
3025                 ov8xx0_configure(sd);
3026
3027         /* Test for 3xxx / 2xxx */
3028         } else if (init_ov_sensor(sd, OV_HIRES_SID) >= 0) {
3029                 ov_hires_configure(sd);
3030         } else {
3031                 err("Can't determine sensor slave IDs");
3032                 goto error;
3033         }
3034
3035         if (sd->sensor < 0)
3036                 goto error;
3037
3038         ov51x_led_control(sd, 0);       /* turn LED off */
3039
3040         switch (sd->bridge) {
3041         case BRIDGE_OV511:
3042         case BRIDGE_OV511PLUS:
3043                 if (sd->sif) {
3044                         cam->cam_mode = ov511_sif_mode;
3045                         cam->nmodes = ARRAY_SIZE(ov511_sif_mode);
3046                 }
3047                 break;
3048         case BRIDGE_OV518:
3049         case BRIDGE_OV518PLUS:
3050                 if (sd->sif) {
3051                         cam->cam_mode = ov518_sif_mode;
3052                         cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
3053                 }
3054                 break;
3055         case BRIDGE_OV519:
3056                 if (sd->sif) {
3057                         cam->cam_mode = ov519_sif_mode;
3058                         cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3059                 }
3060                 break;
3061         case BRIDGE_OVFX2:
3062                 if (sd->sensor == SEN_OV2610) {
3063                         cam->cam_mode = ovfx2_ov2610_mode;
3064                         cam->nmodes = ARRAY_SIZE(ovfx2_ov2610_mode);
3065                 } else if (sd->sensor == SEN_OV3610) {
3066                         cam->cam_mode = ovfx2_ov3610_mode;
3067                         cam->nmodes = ARRAY_SIZE(ovfx2_ov3610_mode);
3068                 } else if (sd->sif) {
3069                         cam->cam_mode = ov519_sif_mode;
3070                         cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3071                 }
3072                 break;
3073         case BRIDGE_W9968CF:
3074                 if (sd->sif)
3075                         cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode) - 1;
3076
3077                 /* w9968cf needs initialisation once the sensor is known */
3078                 w9968cf_init(sd);
3079                 break;
3080         }
3081
3082         gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
3083
3084         /* initialize the sensor */
3085         switch (sd->sensor) {
3086         case SEN_OV2610:
3087                 write_i2c_regvals(sd, norm_2610, ARRAY_SIZE(norm_2610));
3088
3089                 /* Enable autogain, autoexpo, awb, bandfilter */
3090                 i2c_w_mask(sd, 0x13, 0x27, 0x27);
3091                 break;
3092         case SEN_OV3610:
3093                 write_i2c_regvals(sd, norm_3620b, ARRAY_SIZE(norm_3620b));
3094
3095                 /* Enable autogain, autoexpo, awb, bandfilter */
3096                 i2c_w_mask(sd, 0x13, 0x27, 0x27);
3097                 break;
3098         case SEN_OV6620:
3099                 write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20));
3100                 break;
3101         case SEN_OV6630:
3102         case SEN_OV66308AF:
3103                 sd->ctrls[CONTRAST].def = 200;
3104                                  /* The default is too low for the ov6630 */
3105                 write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30));
3106                 break;
3107         default:
3108 /*      case SEN_OV7610: */
3109 /*      case SEN_OV76BE: */
3110                 write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610));
3111                 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
3112                 break;
3113         case SEN_OV7620:
3114         case SEN_OV7620AE:
3115                 write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620));
3116                 break;
3117         case SEN_OV7640:
3118         case SEN_OV7648:
3119                 write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640));
3120                 break;
3121         case SEN_OV7670:
3122                 sd->ctrls[FREQ].max = 3;        /* auto */
3123                 sd->ctrls[FREQ].def = 3;
3124                 write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670));
3125                 break;
3126         case SEN_OV8610:
3127                 write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610));
3128                 break;
3129         }
3130         return gspca_dev->usb_err;
3131 error:
3132         PDEBUG(D_ERR, "OV519 Config failed");
3133         return -EINVAL;
3134 }
3135
3136 /* Set up the OV511/OV511+ with the given image parameters.
3137  *
3138  * Do not put any sensor-specific code in here (including I2C I/O functions)
3139  */
3140 static void ov511_mode_init_regs(struct sd *sd)
3141 {
3142         int hsegs, vsegs, packet_size, fps, needed;
3143         int interlaced = 0;
3144         struct usb_host_interface *alt;
3145         struct usb_interface *intf;
3146
3147         intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3148         alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3149         if (!alt) {
3150                 err("Couldn't get altsetting");
3151                 sd->gspca_dev.usb_err = -EIO;
3152                 return;
3153         }
3154
3155         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3156         reg_w(sd, R51x_FIFO_PSIZE, packet_size >> 5);
3157
3158         reg_w(sd, R511_CAM_UV_EN, 0x01);
3159         reg_w(sd, R511_SNAP_UV_EN, 0x01);
3160         reg_w(sd, R511_SNAP_OPTS, 0x03);
3161
3162         /* Here I'm assuming that snapshot size == image size.
3163          * I hope that's always true. --claudio
3164          */
3165         hsegs = (sd->gspca_dev.width >> 3) - 1;
3166         vsegs = (sd->gspca_dev.height >> 3) - 1;
3167
3168         reg_w(sd, R511_CAM_PXCNT, hsegs);
3169         reg_w(sd, R511_CAM_LNCNT, vsegs);
3170         reg_w(sd, R511_CAM_PXDIV, 0x00);
3171         reg_w(sd, R511_CAM_LNDIV, 0x00);
3172
3173         /* YUV420, low pass filter on */
3174         reg_w(sd, R511_CAM_OPTS, 0x03);
3175
3176         /* Snapshot additions */
3177         reg_w(sd, R511_SNAP_PXCNT, hsegs);
3178         reg_w(sd, R511_SNAP_LNCNT, vsegs);
3179         reg_w(sd, R511_SNAP_PXDIV, 0x00);
3180         reg_w(sd, R511_SNAP_LNDIV, 0x00);
3181
3182         /******** Set the framerate ********/
3183         if (frame_rate > 0)
3184                 sd->frame_rate = frame_rate;
3185
3186         switch (sd->sensor) {
3187         case SEN_OV6620:
3188                 /* No framerate control, doesn't like higher rates yet */
3189                 sd->clockdiv = 3;
3190                 break;
3191
3192         /* Note once the FIXME's in mode_init_ov_sensor_regs() are fixed
3193            for more sensors we need to do this for them too */
3194         case SEN_OV7620:
3195         case SEN_OV7620AE:
3196         case SEN_OV7640:
3197         case SEN_OV7648:
3198         case SEN_OV76BE:
3199                 if (sd->gspca_dev.width == 320)
3200                         interlaced = 1;
3201                 /* Fall through */
3202         case SEN_OV6630:
3203         case SEN_OV7610:
3204         case SEN_OV7670:
3205                 switch (sd->frame_rate) {
3206                 case 30:
3207                 case 25:
3208                         /* Not enough bandwidth to do 640x480 @ 30 fps */
3209                         if (sd->gspca_dev.width != 640) {
3210                                 sd->clockdiv = 0;
3211                                 break;
3212                         }
3213                         /* Fall through for 640x480 case */
3214                 default:
3215 /*              case 20: */
3216 /*              case 15: */
3217                         sd->clockdiv = 1;
3218                         break;
3219                 case 10:
3220                         sd->clockdiv = 2;
3221                         break;
3222                 case 5:
3223                         sd->clockdiv = 5;
3224                         break;
3225                 }
3226                 if (interlaced) {
3227                         sd->clockdiv = (sd->clockdiv + 1) * 2 - 1;
3228                         /* Higher then 10 does not work */
3229                         if (sd->clockdiv > 10)
3230                                 sd->clockdiv = 10;
3231                 }
3232                 break;
3233
3234         case SEN_OV8610:
3235                 /* No framerate control ?? */
3236                 sd->clockdiv = 0;
3237                 break;
3238         }
3239
3240         /* Check if we have enough bandwidth to disable compression */
3241         fps = (interlaced ? 60 : 30) / (sd->clockdiv + 1) + 1;
3242         needed = fps * sd->gspca_dev.width * sd->gspca_dev.height * 3 / 2;
3243         /* 1400 is a conservative estimate of the max nr of isoc packets/sec */
3244         if (needed > 1400 * packet_size) {
3245                 /* Enable Y and UV quantization and compression */
3246                 reg_w(sd, R511_COMP_EN, 0x07);
3247                 reg_w(sd, R511_COMP_LUT_EN, 0x03);
3248         } else {
3249                 reg_w(sd, R511_COMP_EN, 0x06);
3250                 reg_w(sd, R511_COMP_LUT_EN, 0x00);
3251         }
3252
3253         reg_w(sd, R51x_SYS_RESET, OV511_RESET_OMNICE);
3254         reg_w(sd, R51x_SYS_RESET, 0);
3255 }
3256
3257 /* Sets up the OV518/OV518+ with the given image parameters
3258  *
3259  * OV518 needs a completely different approach, until we can figure out what
3260  * the individual registers do. Also, only 15 FPS is supported now.
3261  *
3262  * Do not put any sensor-specific code in here (including I2C I/O functions)
3263  */
3264 static void ov518_mode_init_regs(struct sd *sd)
3265 {
3266         int hsegs, vsegs, packet_size;
3267         struct usb_host_interface *alt;
3268         struct usb_interface *intf;
3269
3270         intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3271         alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3272         if (!alt) {
3273                 err("Couldn't get altsetting");
3274                 sd->gspca_dev.usb_err = -EIO;
3275                 return;
3276         }
3277
3278         packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3279         ov518_reg_w32(sd, R51x_FIFO_PSIZE, packet_size & ~7, 2);
3280
3281         /******** Set the mode ********/
3282         reg_w(sd, 0x2b, 0);
3283         reg_w(sd, 0x2c, 0);
3284         reg_w(sd, 0x2d, 0);
3285         reg_w(sd, 0x2e, 0);
3286         reg_w(sd, 0x3b, 0);
3287         reg_w(sd, 0x3c, 0);
3288         reg_w(sd, 0x3d, 0);
3289         reg_w(sd, 0x3e, 0);
3290
3291         if (sd->bridge == BRIDGE_OV518) {
3292                 /* Set 8-bit (YVYU) input format */
3293                 reg_w_mask(sd, 0x20, 0x08, 0x08);
3294
3295                 /* Set 12-bit (4:2:0) output format */
3296                 reg_w_mask(sd, 0x28, 0x80, 0xf0);
3297                 reg_w_mask(sd, 0x38, 0x80, 0xf0);
3298         } else {
3299                 reg_w(sd, 0x28, 0x80);
3300                 reg_w(sd, 0x38, 0x80);
3301         }
3302
3303         hsegs = sd->gspca_dev.width / 16;
3304         vsegs = sd->gspca_dev.height / 4;
3305
3306         reg_w(sd, 0x29, hsegs);
3307         reg_w(sd, 0x2a, vsegs);
3308
3309         reg_w(sd, 0x39, hsegs);
3310         reg_w(sd, 0x3a, vsegs);
3311
3312         /* Windows driver does this here; who knows why */
3313         reg_w(sd, 0x2f, 0x80);
3314
3315         /******** Set the framerate ********/
3316         sd->clockdiv = 1;
3317
3318         /* Mode independent, but framerate dependent, regs */
3319         /* 0x51: Clock divider; Only works on some cams which use 2 crystals */
3320         reg_w(sd, 0x51, 0x04);
3321         reg_w(sd, 0x22, 0x18);
3322         reg_w(sd, 0x23, 0xff);
3323
3324         if (sd->bridge == BRIDGE_OV518PLUS) {
3325                 switch (sd->sensor) {
3326                 case SEN_OV7620AE:
3327                         if (sd->gspca_dev.width == 320) {
3328                                 reg_w(sd, 0x20, 0x00);
3329                                 reg_w(sd, 0x21, 0x19);
3330                         } else {
3331                                 reg_w(sd, 0x20, 0x60);
3332                                 reg_w(sd, 0x21, 0x1f);
3333                         }
3334                         break;
3335                 case SEN_OV7620:
3336                         reg_w(sd, 0x20, 0x00);
3337                         reg_w(sd, 0x21, 0x19);
3338                         break;
3339                 default:
3340                         reg_w(sd, 0x21, 0x19);
3341                 }
3342         } else
3343                 reg_w(sd, 0x71, 0x17);  /* Compression-related? */
3344
3345         /* FIXME: Sensor-specific */
3346         /* Bit 5 is what matters here. Of course, it is "reserved" */
3347         i2c_w(sd, 0x54, 0x23);
3348
3349         reg_w(sd, 0x2f, 0x80);
3350
3351         if (sd->bridge == BRIDGE_OV518PLUS) {
3352                 reg_w(sd, 0x24, 0x94);
3353                 reg_w(sd, 0x25, 0x90);
3354                 ov518_reg_w32(sd, 0xc4,    400, 2);     /* 190h   */
3355                 ov518_reg_w32(sd, 0xc6,    540, 2);     /* 21ch   */
3356                 ov518_reg_w32(sd, 0xc7,    540, 2);     /* 21ch   */
3357                 ov518_reg_w32(sd, 0xc8,    108, 2);     /* 6ch    */
3358                 ov518_reg_w32(sd, 0xca, 131098, 3);     /* 2001ah */
3359                 ov518_reg_w32(sd, 0xcb,    532, 2);     /* 214h   */
3360                 ov518_reg_w32(sd, 0xcc,   2400, 2);     /* 960h   */
3361                 ov518_reg_w32(sd, 0xcd,     32, 2);     /* 20h    */
3362                 ov518_reg_w32(sd, 0xce,    608, 2);     /* 260h   */
3363         } else {
3364                 reg_w(sd, 0x24, 0x9f);
3365                 reg_w(sd, 0x25, 0x90);
3366                 ov518_reg_w32(sd, 0xc4,    400, 2);     /* 190h   */
3367                 ov518_reg_w32(sd, 0xc6,    381, 2);     /* 17dh   */
3368                 ov518_reg_w32(sd, 0xc7,    381, 2);     /* 17dh   */
3369                 ov518_reg_w32(sd, 0xc8,    128, 2);     /* 80h    */
3370                 ov518_reg_w32(sd, 0xca, 183331, 3);     /* 2cc23h */
3371                 ov518_reg_w32(sd, 0xcb,    746, 2);     /* 2eah   */
3372                 ov518_reg_w32(sd, 0xcc,   1750, 2);     /* 6d6h   */
3373                 ov518_reg_w32(sd, 0xcd,     45, 2);     /* 2dh    */
3374                 ov518_reg_w32(sd, 0xce,    851, 2);     /* 353h   */
3375         }
3376
3377         reg_w(sd, 0x2f, 0x80);
3378 }
3379
3380 /* Sets up the OV519 with the given image parameters
3381  *
3382  * OV519 needs a completely different approach, until we can figure out what
3383  * the individual registers do.
3384  *
3385  * Do not put any sensor-specific code in here (including I2C I/O functions)
3386  */
3387 static void ov519_mode_init_regs(struct sd *sd)
3388 {
3389         static const struct ov_regvals mode_init_519_ov7670[] = {
3390                 { 0x5d, 0x03 }, /* Turn off suspend mode */
3391                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3392                 { OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */
3393                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3394                 { 0xa3, 0x18 },
3395                 { 0xa4, 0x04 },
3396                 { 0xa5, 0x28 },
3397                 { 0x37, 0x00 }, /* SetUsbInit */
3398                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3399                 /* Enable both fields, YUV Input, disable defect comp (why?) */
3400                 { 0x20, 0x0c },
3401                 { 0x21, 0x38 },
3402                 { 0x22, 0x1d },
3403                 { 0x17, 0x50 }, /* undocumented */
3404                 { 0x37, 0x00 }, /* undocumented */
3405                 { 0x40, 0xff }, /* I2C timeout counter */
3406                 { 0x46, 0x00 }, /* I2C clock prescaler */
3407                 { 0x59, 0x04 }, /* new from windrv 090403 */
3408                 { 0xff, 0x00 }, /* undocumented */
3409                 /* windows reads 0x55 at this point, why? */
3410         };
3411
3412         static const struct ov_regvals mode_init_519[] = {
3413                 { 0x5d, 0x03 }, /* Turn off suspend mode */
3414                 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3415                 { OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */
3416                 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3417                 { 0xa3, 0x18 },
3418                 { 0xa4, 0x04 },
3419                 { 0xa5, 0x28 },
3420                 { 0x37, 0x00 }, /* SetUsbInit */
3421                 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3422                 /* Enable both fields, YUV Input, disable defect comp (why?) */
3423                 { 0x22, 0x1d },
3424                 { 0x17, 0x50 }, /* undocumented */
3425                 { 0x37, 0x00 }, /* undocumented */
3426                 { 0x40, 0xff }, /* I2C timeout counter */
3427                 { 0x46, 0x00 }, /* I2C clock prescaler */
3428                 { 0x59, 0x04 }, /* new from windrv 090403 */
3429                 { 0xff, 0x00 }, /* undocumented */
3430                 /* windows reads 0x55 at this point, why? */
3431         };
3432
3433         /******** Set the mode ********/
3434         if (sd->sensor != SEN_OV7670) {
3435                 write_regvals(sd, mode_init_519, ARRAY_SIZE(mode_init_519));
3436                 if (sd->sensor == SEN_OV7640 ||
3437                     sd->sensor == SEN_OV7648) {
3438                         /* Select 8-bit input mode */
3439                         reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
3440                 }
3441         } else {
3442                 write_regvals(sd, mode_init_519_ov7670,
3443                                 ARRAY_SIZE(mode_init_519_ov7670));
3444         }
3445
3446         reg_w(sd, OV519_R10_H_SIZE,     sd->gspca_dev.width >> 4);
3447         reg_w(sd, OV519_R11_V_SIZE,     sd->gspca_dev.height >> 3);
3448         if (sd->sensor == SEN_OV7670 &&
3449             sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3450                 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
3451         else if (sd->sensor == SEN_OV7648 &&
3452             sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3453                 reg_w(sd, OV519_R12_X_OFFSETL, 0x01);
3454         else
3455                 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
3456         reg_w(sd, OV519_R13_X_OFFSETH,  0x00);
3457         reg_w(sd, OV519_R14_Y_OFFSETL,  0x00);
3458         reg_w(sd, OV519_R15_Y_OFFSETH,  0x00);
3459         reg_w(sd, OV519_R16_DIVIDER,    0x00);
3460         reg_w(sd, OV519_R25_FORMAT,     0x03); /* YUV422 */
3461         reg_w(sd, 0x26,                 0x00); /* Undocumented */
3462
3463         /******** Set the framerate ********/
3464         if (frame_rate > 0)
3465                 sd->frame_rate = frame_rate;
3466
3467 /* FIXME: These are only valid at the max resolution. */
3468         sd->clockdiv = 0;
3469         switch (sd->sensor) {
3470         case SEN_OV7640:
3471         case SEN_OV7648:
3472                 switch (sd->frame_rate) {
3473                 default:
3474 /*              case 30: */
3475                         reg_w(sd, 0xa4, 0x0c);
3476                         reg_w(sd, 0x23, 0xff);
3477                         break;
3478                 case 25:
3479                         reg_w(sd, 0xa4, 0x0c);
3480                         reg_w(sd, 0x23, 0x1f);
3481                         break;
3482                 case 20:
3483                         reg_w(sd, 0xa4, 0x0c);
3484                         reg_w(sd, 0x23, 0x1b);
3485                         break;
3486                 case 15:
3487                         reg_w(sd, 0xa4, 0x04);
3488                         reg_w(sd, 0x23, 0xff);
3489                         sd->clockdiv = 1;
3490                         break;
3491                 case 10:
3492                         reg_w(sd, 0xa4, 0x04);
3493                         reg_w(sd, 0x23, 0x1f);
3494                         sd->clockdiv = 1;
3495                         break;
3496                 case 5:
3497                         reg_w(sd, 0xa4, 0x04);
3498                         reg_w(sd, 0x23, 0x1b);
3499                         sd->clockdiv = 1;
3500                         break;
3501                 }
3502                 break;
3503         case SEN_OV8610:
3504                 switch (sd->frame_rate) {
3505                 default:        /* 15 fps */
3506 /*              case 15: */
3507                         reg_w(sd, 0xa4, 0x06);
3508                         reg_w(sd, 0x23, 0xff);
3509                         break;
3510                 case 10:
3511                         reg_w(sd, 0xa4, 0x06);
3512                         reg_w(sd, 0x23, 0x1f);
3513                         break;
3514                 case 5:
3515                         reg_w(sd, 0xa4, 0x06);
3516                         reg_w(sd, 0x23, 0x1b);
3517                         break;
3518                 }
3519                 break;
3520         case SEN_OV7670:                /* guesses, based on 7640 */
3521                 PDEBUG(D_STREAM, "Setting framerate to %d fps",
3522                                  (sd->frame_rate == 0) ? 15 : sd->frame_rate);
3523                 reg_w(sd, 0xa4, 0x10);
3524                 switch (sd->frame_rate) {
3525                 case 30:
3526                         reg_w(sd, 0x23, 0xff);
3527                         break;
3528                 case 20:
3529                         reg_w(sd, 0x23, 0x1b);
3530                         break;
3531                 default:
3532 /*              case 15: */
3533                         reg_w(sd, 0x23, 0xff);
3534                         sd->clockdiv = 1;
3535                         break;
3536                 }
3537                 break;
3538         }
3539 }
3540
3541 static void mode_init_ov_sensor_regs(struct sd *sd)
3542 {
3543         struct gspca_dev *gspca_dev;
3544         int qvga, xstart, xend, ystart, yend;
3545         u8 v;
3546
3547         gspca_dev = &sd->gspca_dev;
3548         qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;
3549
3550         /******** Mode (VGA/QVGA) and sensor specific regs ********/
3551         switch (sd->sensor) {
3552         case SEN_OV2610:
3553                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3554                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3555                 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3556                 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3557                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3558                 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3559                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3560                 return;
3561         case SEN_OV3610:
3562                 if (qvga) {
3563                         xstart = (1040 - gspca_dev->width) / 2 + (0x1f << 4);
3564                         ystart = (776 - gspca_dev->height) / 2;
3565                 } else {
3566                         xstart = (2076 - gspca_dev->width) / 2 + (0x10 << 4);
3567                         ystart = (1544 - gspca_dev->height) / 2;
3568                 }
3569                 xend = xstart + gspca_dev->width;
3570                 yend = ystart + gspca_dev->height;
3571                 /* Writing to the COMH register resets the other windowing regs
3572                    to their default values, so we must do this first. */
3573                 i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0xf0);
3574                 i2c_w_mask(sd, 0x32,
3575                            (((xend >> 1) & 7) << 3) | ((xstart >> 1) & 7),
3576                            0x3f);
3577                 i2c_w_mask(sd, 0x03,
3578                            (((yend >> 1) & 3) << 2) | ((ystart >> 1) & 3),
3579                            0x0f);
3580                 i2c_w(sd, 0x17, xstart >> 4);
3581                 i2c_w(sd, 0x18, xend >> 4);
3582                 i2c_w(sd, 0x19, ystart >> 3);
3583                 i2c_w(sd, 0x1a, yend >> 3);
3584                 return;
3585         case SEN_OV8610:
3586                 /* For OV8610 qvga means qsvga */
3587                 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
3588                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3589                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3590                 i2c_w_mask(sd, 0x2d, 0x00, 0x40); /* from windrv 090403 */
3591                 i2c_w_mask(sd, 0x28, 0x20, 0x20); /* progressive mode on */
3592                 break;
3593         case SEN_OV7610:
3594                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3595                 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
3596                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3597                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3598                 break;
3599         case SEN_OV7620:
3600         case SEN_OV7620AE:
3601         case SEN_OV76BE:
3602                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3603                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3604                 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3605                 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3606                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3607                 i2c_w_mask(sd, 0x67, qvga ? 0xb0 : 0x90, 0xf0);
3608                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3609                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3610                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3611                 if (sd->sensor == SEN_OV76BE)
3612                         i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
3613                 break;
3614         case SEN_OV7640:
3615         case SEN_OV7648:
3616                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3617                 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3618                 /* Setting this undocumented bit in qvga mode removes a very
3619                    annoying vertical shaking of the image */
3620                 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3621                 /* Unknown */
3622                 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3623                 /* Allow higher automatic gain (to allow higher framerates) */
3624                 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3625                 i2c_w_mask(sd, 0x12, 0x04, 0x04); /* AWB: 1 */
3626                 break;
3627         case SEN_OV7670:
3628                 /* set COM7_FMT_VGA or COM7_FMT_QVGA
3629                  * do we need to set anything else?
3630                  *      HSTART etc are set in set_ov_sensor_window itself */
3631                 i2c_w_mask(sd, OV7670_R12_COM7,
3632                          qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
3633                          OV7670_COM7_FMT_MASK);
3634                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3635                 i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_AWB,
3636                                 OV7670_COM8_AWB);
3637                 if (qvga) {             /* QVGA from ov7670.c by
3638                                          * Jonathan Corbet */
3639                         xstart = 164;
3640                         xend = 28;
3641                         ystart = 14;
3642                         yend = 494;
3643                 } else {                /* VGA */
3644                         xstart = 158;
3645                         xend = 14;
3646                         ystart = 10;
3647                         yend = 490;
3648                 }
3649                 /* OV7670 hardware window registers are split across
3650                  * multiple locations */
3651                 i2c_w(sd, OV7670_R17_HSTART, xstart >> 3);
3652                 i2c_w(sd, OV7670_R18_HSTOP, xend >> 3);
3653                 v = i2c_r(sd, OV7670_R32_HREF);
3654                 v = (v & 0xc0) | ((xend & 0x7) << 3) | (xstart & 0x07);
3655                 msleep(10);     /* need to sleep between read and write to
3656                                  * same reg! */
3657                 i2c_w(sd, OV7670_R32_HREF, v);
3658
3659                 i2c_w(sd, OV7670_R19_VSTART, ystart >> 2);
3660                 i2c_w(sd, OV7670_R1A_VSTOP, yend >> 2);
3661                 v = i2c_r(sd, OV7670_R03_VREF);
3662                 v = (v & 0xc0) | ((yend & 0x3) << 2) | (ystart & 0x03);
3663                 msleep(10);     /* need to sleep between read and write to
3664                                  * same reg! */
3665                 i2c_w(sd, OV7670_R03_VREF, v);
3666                 break;
3667         case SEN_OV6620:
3668                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3669                 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3670                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3671                 break;
3672         case SEN_OV6630:
3673         case SEN_OV66308AF:
3674                 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3675                 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3676                 break;
3677         default:
3678                 return;
3679         }
3680
3681         /******** Clock programming ********/
3682         i2c_w(sd, 0x11, sd->clockdiv);
3683 }
3684
3685 static void sethvflip(struct gspca_dev *gspca_dev)
3686 {
3687         struct sd *sd = (struct sd *) gspca_dev;
3688
3689         if (sd->gspca_dev.streaming)
3690                 reg_w(sd, OV519_R51_RESET1, 0x0f);      /* block stream */
3691         i2c_w_mask(sd, OV7670_R1E_MVFP,
3692                 OV7670_MVFP_MIRROR * sd->ctrls[HFLIP].val
3693                         | OV7670_MVFP_VFLIP * sd->ctrls[VFLIP].val,
3694                 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
3695         if (sd->gspca_dev.streaming)
3696                 reg_w(sd, OV519_R51_RESET1, 0x00);      /* restart stream */
3697 }
3698
3699 static void set_ov_sensor_window(struct sd *sd)
3700 {
3701         struct gspca_dev *gspca_dev;
3702         int qvga, crop;
3703         int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
3704
3705         /* mode setup is fully handled in mode_init_ov_sensor_regs for these */
3706         if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610 ||
3707             sd->sensor == SEN_OV7670) {
3708                 mode_init_ov_sensor_regs(sd);
3709                 return;
3710         }
3711         gspca_dev = &sd->gspca_dev;
3712         qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;
3713         crop = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 2;
3714
3715         /* The different sensor ICs handle setting up of window differently.
3716          * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
3717         switch (sd->sensor) {
3718         case SEN_OV8610:
3719                 hwsbase = 0x1e;
3720                 hwebase = 0x1e;
3721                 vwsbase = 0x02;
3722                 vwebase = 0x02;
3723                 break;
3724         case SEN_OV7610:
3725         case SEN_OV76BE:
3726                 hwsbase = 0x38;
3727                 hwebase = 0x3a;
3728                 vwsbase = vwebase = 0x05;
3729                 break;
3730         case SEN_OV6620:
3731         case SEN_OV6630:
3732         case SEN_OV66308AF:
3733                 hwsbase = 0x38;
3734                 hwebase = 0x3a;
3735                 vwsbase = 0x05;
3736                 vwebase = 0x06;
3737                 if (sd->sensor == SEN_OV66308AF && qvga)
3738                         /* HDG: this fixes U and V getting swapped */
3739                         hwsbase++;
3740                 if (crop) {
3741                         hwsbase += 8;
3742                         hwebase += 8;
3743                         vwsbase += 11;
3744                         vwebase += 11;
3745                 }
3746                 break;
3747         case SEN_OV7620:
3748         case SEN_OV7620AE:
3749                 hwsbase = 0x2f;         /* From 7620.SET (spec is wrong) */
3750                 hwebase = 0x2f;
3751                 vwsbase = vwebase = 0x05;
3752                 break;
3753         case SEN_OV7640:
3754         case SEN_OV7648:
3755                 hwsbase = 0x1a;
3756                 hwebase = 0x1a;
3757                 vwsbase = vwebase = 0x03;
3758                 break;
3759         default:
3760                 return;
3761         }
3762
3763         switch (sd->sensor) {
3764         case SEN_OV6620:
3765         case SEN_OV6630:
3766         case SEN_OV66308AF:
3767                 if (qvga) {             /* QCIF */
3768                         hwscale = 0;
3769                         vwscale = 0;
3770                 } else {                /* CIF */
3771                         hwscale = 1;
3772                         vwscale = 1;    /* The datasheet says 0;
3773                                          * it's wrong */
3774                 }
3775                 break;
3776         case SEN_OV8610:
3777                 if (qvga) {             /* QSVGA */
3778                         hwscale = 1;
3779                         vwscale = 1;
3780                 } else {                /* SVGA */
3781                         hwscale = 2;
3782                         vwscale = 2;
3783                 }
3784                 break;
3785         default:                        /* SEN_OV7xx0 */
3786                 if (qvga) {             /* QVGA */
3787                         hwscale = 1;
3788                         vwscale = 0;
3789                 } else {                /* VGA */
3790                         hwscale = 2;
3791                         vwscale = 1;
3792                 }
3793         }
3794
3795         mode_init_ov_sensor_regs(sd);
3796
3797         i2c_w(sd, 0x17, hwsbase);
3798         i2c_w(sd, 0x18, hwebase + (sd->sensor_width >> hwscale));
3799         i2c_w(sd, 0x19, vwsbase);
3800         i2c_w(sd, 0x1a, vwebase + (sd->sensor_height >> vwscale));
3801 }
3802
3803 /* -- start the camera -- */
3804 static int sd_start(struct gspca_dev *gspca_dev)
3805 {
3806         struct sd *sd = (struct sd *) gspca_dev;
3807
3808         /* Default for most bridges, allow bridge_mode_init_regs to override */
3809         sd->sensor_width = sd->gspca_dev.width;
3810         sd->sensor_height = sd->gspca_dev.height;
3811
3812         switch (sd->bridge) {
3813         case BRIDGE_OV511:
3814         case BRIDGE_OV511PLUS:
3815                 ov511_mode_init_regs(sd);
3816                 break;
3817         case BRIDGE_OV518:
3818         case BRIDGE_OV518PLUS:
3819                 ov518_mode_init_regs(sd);
3820                 break;
3821         case BRIDGE_OV519:
3822                 ov519_mode_init_regs(sd);
3823                 break;
3824         /* case BRIDGE_OVFX2: nothing to do */
3825         case BRIDGE_W9968CF:
3826                 w9968cf_mode_init_regs(sd);
3827                 break;
3828         }
3829
3830         set_ov_sensor_window(sd);
3831
3832         if (!(sd->gspca_dev.ctrl_dis & (1 << CONTRAST)))
3833                 setcontrast(gspca_dev);
3834         if (!(sd->gspca_dev.ctrl_dis & (1 << BRIGHTNESS)))
3835                 setbrightness(gspca_dev);
3836         if (!(sd->gspca_dev.ctrl_dis & (1 << COLORS)))
3837                 setcolors(gspca_dev);
3838         if (!(sd->gspca_dev.ctrl_dis & ((1 << HFLIP) | (1 << VFLIP))))
3839                 sethvflip(gspca_dev);
3840         if (!(sd->gspca_dev.ctrl_dis & (1 << AUTOBRIGHT)))
3841                 setautobright(gspca_dev);
3842         if (!(sd->gspca_dev.ctrl_dis & (1 << FREQ)))
3843                 setfreq_i(sd);
3844
3845         /* Force clear snapshot state in case the snapshot button was
3846            pressed while we weren't streaming */
3847         sd->snapshot_needs_reset = 1;
3848         sd_reset_snapshot(gspca_dev);
3849
3850         sd->first_frame = 3;
3851
3852         ov51x_restart(sd);
3853         ov51x_led_control(sd, 1);
3854         return gspca_dev->usb_err;
3855 }
3856
3857 static void sd_stopN(struct gspca_dev *gspca_dev)
3858 {
3859         struct sd *sd = (struct sd *) gspca_dev;
3860
3861         ov51x_stop(sd);
3862         ov51x_led_control(sd, 0);
3863 }
3864
3865 static void sd_stop0(struct gspca_dev *gspca_dev)
3866 {
3867         struct sd *sd = (struct sd *) gspca_dev;
3868
3869         if (!sd->gspca_dev.present)
3870                 return;
3871         if (sd->bridge == BRIDGE_W9968CF)
3872                 w9968cf_stop0(sd);
3873
3874 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3875         /* If the last button state is pressed, release it now! */
3876         if (sd->snapshot_pressed) {
3877                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
3878                 input_sync(gspca_dev->input_dev);
3879                 sd->snapshot_pressed = 0;
3880         }
3881 #endif
3882         if (sd->bridge == BRIDGE_OV519)
3883                 reg_w(sd, OV519_R57_SNAPSHOT, 0x23);
3884 }
3885
3886 static void ov51x_handle_button(struct gspca_dev *gspca_dev, u8 state)
3887 {
3888         struct sd *sd = (struct sd *) gspca_dev;
3889
3890         if (sd->snapshot_pressed != state) {
3891 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3892                 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
3893                 input_sync(gspca_dev->input_dev);
3894 #endif
3895                 if (state)
3896                         sd->snapshot_needs_reset = 1;
3897
3898                 sd->snapshot_pressed = state;
3899         } else {
3900                 /* On the ov511 / ov519 we need to reset the button state
3901                    multiple times, as resetting does not work as long as the
3902                    button stays pressed */
3903                 switch (sd->bridge) {
3904                 case BRIDGE_OV511:
3905                 case BRIDGE_OV511PLUS:
3906                 case BRIDGE_OV519:
3907                         if (state)
3908                                 sd->snapshot_needs_reset = 1;
3909                         break;
3910                 }
3911         }
3912 }
3913
3914 static void ov511_pkt_scan(struct gspca_dev *gspca_dev,
3915                         u8 *in,                 /* isoc packet */
3916                         int len)                /* iso packet length */
3917 {
3918         struct sd *sd = (struct sd *) gspca_dev;
3919
3920         /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
3921          * byte non-zero. The EOF packet has image width/height in the
3922          * 10th and 11th bytes. The 9th byte is given as follows:
3923          *
3924          * bit 7: EOF
3925          *     6: compression enabled
3926          *     5: 422/420/400 modes
3927          *     4: 422/420/400 modes
3928          *     3: 1
3929          *     2: snapshot button on
3930          *     1: snapshot frame
3931          *     0: even/odd field
3932          */
3933         if (!(in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) &&
3934             (in[8] & 0x08)) {
3935                 ov51x_handle_button(gspca_dev, (in[8] >> 2) & 1);
3936                 if (in[8] & 0x80) {
3937                         /* Frame end */
3938                         if ((in[9] + 1) * 8 != gspca_dev->width ||
3939                             (in[10] + 1) * 8 != gspca_dev->height) {
3940                                 PDEBUG(D_ERR, "Invalid frame size, got: %dx%d,"
3941                                         " requested: %dx%d\n",
3942                                         (in[9] + 1) * 8, (in[10] + 1) * 8,
3943                                         gspca_dev->width, gspca_dev->height);
3944                                 gspca_dev->last_packet_type = DISCARD_PACKET;
3945                                 return;
3946                         }
3947                         /* Add 11 byte footer to frame, might be usefull */
3948                         gspca_frame_add(gspca_dev, LAST_PACKET, in, 11);
3949                         return;
3950                 } else {
3951                         /* Frame start */
3952                         gspca_frame_add(gspca_dev, FIRST_PACKET, in, 0);
3953                         sd->packet_nr = 0;
3954                 }
3955         }
3956
3957         /* Ignore the packet number */
3958         len--;
3959
3960         /* intermediate packet */
3961         gspca_frame_add(gspca_dev, INTER_PACKET, in, len);
3962 }
3963
3964 static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
3965                         u8 *data,                       /* isoc packet */
3966                         int len)                        /* iso packet length */
3967 {
3968         struct sd *sd = (struct sd *) gspca_dev;
3969
3970         /* A false positive here is likely, until OVT gives me
3971          * the definitive SOF/EOF format */
3972         if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
3973                 ov51x_handle_button(gspca_dev, (data[6] >> 1) & 1);
3974                 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
3975                 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
3976                 sd->packet_nr = 0;
3977         }
3978
3979         if (gspca_dev->last_packet_type == DISCARD_PACKET)
3980                 return;
3981
3982         /* Does this device use packet numbers ? */
3983         if (len & 7) {
3984                 len--;
3985                 if (sd->packet_nr == data[len])
3986                         sd->packet_nr++;
3987                 /* The last few packets of the frame (which are all 0's
3988                    except that they may contain part of the footer), are
3989                    numbered 0 */
3990                 else if (sd->packet_nr == 0 || data[len]) {
3991                         PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
3992                                 (int)data[len], (int)sd->packet_nr);
3993                         gspca_dev->last_packet_type = DISCARD_PACKET;
3994                         return;
3995                 }
3996         }
3997
3998         /* intermediate packet */
3999         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4000 }
4001
4002 static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
4003                         u8 *data,                       /* isoc packet */
4004                         int len)                        /* iso packet length */
4005 {
4006         /* Header of ov519 is 16 bytes:
4007          *     Byte     Value      Description
4008          *      0       0xff    magic
4009          *      1       0xff    magic
4010          *      2       0xff    magic
4011          *      3       0xXX    0x50 = SOF, 0x51 = EOF
4012          *      9       0xXX    0x01 initial frame without data,
4013          *                      0x00 standard frame with image
4014          *      14      Lo      in EOF: length of image data / 8
4015          *      15      Hi
4016          */
4017
4018         if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
4019                 switch (data[3]) {
4020                 case 0x50:              /* start of frame */
4021                         /* Don't check the button state here, as the state
4022                            usually (always ?) changes at EOF and checking it
4023                            here leads to unnecessary snapshot state resets. */
4024 #define HDRSZ 16
4025                         data += HDRSZ;
4026                         len -= HDRSZ;
4027 #undef HDRSZ
4028                         if (data[0] == 0xff || data[1] == 0xd8)
4029                                 gspca_frame_add(gspca_dev, FIRST_PACKET,
4030                                                 data, len);
4031                         else
4032                                 gspca_dev->last_packet_type = DISCARD_PACKET;
4033                         return;
4034                 case 0x51:              /* end of frame */
4035                         ov51x_handle_button(gspca_dev, data[11] & 1);
4036                         if (data[9] != 0)
4037                                 gspca_dev->last_packet_type = DISCARD_PACKET;
4038                         gspca_frame_add(gspca_dev, LAST_PACKET,
4039                                         NULL, 0);
4040                         return;
4041                 }
4042         }
4043
4044         /* intermediate packet */
4045         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4046 }
4047
4048 static void ovfx2_pkt_scan(struct gspca_dev *gspca_dev,
4049                         u8 *data,                       /* isoc packet */
4050                         int len)                        /* iso packet length */
4051 {
4052         struct sd *sd = (struct sd *) gspca_dev;
4053
4054         gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4055
4056         /* A short read signals EOF */
4057         if (len < OVFX2_BULK_SIZE) {
4058                 /* If the frame is short, and it is one of the first ones
4059                    the sensor and bridge are still syncing, so drop it. */
4060                 if (sd->first_frame) {
4061                         sd->first_frame--;
4062                         if (gspca_dev->image_len <
4063                                   sd->gspca_dev.width * sd->gspca_dev.height)
4064                                 gspca_dev->last_packet_type = DISCARD_PACKET;
4065                 }
4066                 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4067                 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
4068         }
4069 }
4070
4071 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
4072                         u8 *data,                       /* isoc packet */
4073                         int len)                        /* iso packet length */
4074 {
4075         struct sd *sd = (struct sd *) gspca_dev;
4076
4077         switch (sd->bridge) {
4078         case BRIDGE_OV511:
4079         case BRIDGE_OV511PLUS:
4080                 ov511_pkt_scan(gspca_dev, data, len);
4081                 break;
4082         case BRIDGE_OV518:
4083         case BRIDGE_OV518PLUS:
4084                 ov518_pkt_scan(gspca_dev, data, len);
4085                 break;
4086         case BRIDGE_OV519:
4087                 ov519_pkt_scan(gspca_dev, data, len);
4088                 break;
4089         case BRIDGE_OVFX2:
4090                 ovfx2_pkt_scan(gspca_dev, data, len);
4091                 break;
4092         case BRIDGE_W9968CF:
4093                 w9968cf_pkt_scan(gspca_dev, data, len);
4094                 break;
4095         }
4096 }
4097
4098 /* -- management routines -- */
4099
4100 static void setbrightness(struct gspca_dev *gspca_dev)
4101 {
4102         struct sd *sd = (struct sd *) gspca_dev;
4103         int val;
4104
4105         val = sd->ctrls[BRIGHTNESS].val;
4106         switch (sd->sensor) {
4107         case SEN_OV8610:
4108         case SEN_OV7610:
4109         case SEN_OV76BE:
4110         case SEN_OV6620:
4111         case SEN_OV6630:
4112         case SEN_OV66308AF:
4113         case SEN_OV7640:
4114         case SEN_OV7648:
4115                 i2c_w(sd, OV7610_REG_BRT, val);
4116                 break;
4117         case SEN_OV7620:
4118         case SEN_OV7620AE:
4119                 /* 7620 doesn't like manual changes when in auto mode */
4120                 if (!sd->ctrls[AUTOBRIGHT].val)
4121                         i2c_w(sd, OV7610_REG_BRT, val);
4122                 break;
4123         case SEN_OV7670:
4124 /*win trace
4125  *              i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_AEC); */
4126                 i2c_w(sd, OV7670_R55_BRIGHT, ov7670_abs_to_sm(val));
4127                 break;
4128         }
4129 }
4130
4131 static void setcontrast(struct gspca_dev *gspca_dev)
4132 {
4133         struct sd *sd = (struct sd *) gspca_dev;
4134         int val;
4135
4136         val = sd->ctrls[CONTRAST].val;
4137         switch (sd->sensor) {
4138         case SEN_OV7610:
4139         case SEN_OV6620:
4140                 i2c_w(sd, OV7610_REG_CNT, val);
4141                 break;
4142         case SEN_OV6630:
4143         case SEN_OV66308AF:
4144                 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
4145                 break;
4146         case SEN_OV8610: {
4147                 static const u8 ctab[] = {
4148                         0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
4149                 };
4150
4151                 /* Use Y gamma control instead. Bit 0 enables it. */
4152                 i2c_w(sd, 0x64, ctab[val >> 5]);
4153                 break;
4154             }
4155         case SEN_OV7620:
4156         case SEN_OV7620AE: {
4157                 static const u8 ctab[] = {
4158                         0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
4159                         0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
4160                 };
4161
4162                 /* Use Y gamma control instead. Bit 0 enables it. */
4163                 i2c_w(sd, 0x64, ctab[val >> 4]);
4164                 break;
4165             }
4166         case SEN_OV7670:
4167                 /* check that this isn't just the same as ov7610 */
4168                 i2c_w(sd, OV7670_R56_CONTRAS, val >> 1);
4169                 break;
4170         }
4171 }
4172
4173 static void setcolors(struct gspca_dev *gspca_dev)
4174 {
4175         struct sd *sd = (struct sd *) gspca_dev;
4176         int val;
4177
4178         val = sd->ctrls[COLORS].val;
4179         switch (sd->sensor) {
4180         case SEN_OV8610:
4181         case SEN_OV7610:
4182         case SEN_OV76BE:
4183         case SEN_OV6620:
4184         case SEN_OV6630:
4185         case SEN_OV66308AF:
4186                 i2c_w(sd, OV7610_REG_SAT, val);
4187                 break;
4188         case SEN_OV7620:
4189         case SEN_OV7620AE:
4190                 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
4191 /*              rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
4192                 if (rc < 0)
4193                         goto out; */
4194                 i2c_w(sd, OV7610_REG_SAT, val);
4195                 break;
4196         case SEN_OV7640:
4197         case SEN_OV7648:
4198                 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
4199                 break;
4200         case SEN_OV7670:
4201                 /* supported later once I work out how to do it
4202                  * transparently fail now! */
4203                 /* set REG_COM13 values for UV sat auto mode */
4204                 break;
4205         }
4206 }
4207
4208 static void setautobright(struct gspca_dev *gspca_dev)
4209 {
4210         struct sd *sd = (struct sd *) gspca_dev;
4211
4212         i2c_w_mask(sd, 0x2d, sd->ctrls[AUTOBRIGHT].val ? 0x10 : 0x00, 0x10);
4213 }
4214
4215 static void setfreq_i(struct sd *sd)
4216 {
4217         if (sd->sensor == SEN_OV7670) {
4218                 switch (sd->ctrls[FREQ].val) {
4219                 case 0: /* Banding filter disabled */
4220                         i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_BFILT);
4221                         break;
4222                 case 1: /* 50 hz */
4223                         i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
4224                                    OV7670_COM8_BFILT);
4225                         i2c_w_mask(sd, OV7670_R3B_COM11, 0x08, 0x18);
4226                         break;
4227                 case 2: /* 60 hz */
4228                         i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
4229                                    OV7670_COM8_BFILT);
4230                         i2c_w_mask(sd, OV7670_R3B_COM11, 0x00, 0x18);
4231                         break;
4232                 case 3: /* Auto hz - ov7670 only */
4233                         i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
4234                                    OV7670_COM8_BFILT);
4235                         i2c_w_mask(sd, OV7670_R3B_COM11, OV7670_COM11_HZAUTO,
4236                                    0x18);
4237                         break;
4238                 }
4239         } else {
4240                 switch (sd->ctrls[FREQ].val) {
4241                 case 0: /* Banding filter disabled */
4242                         i2c_w_mask(sd, 0x2d, 0x00, 0x04);
4243                         i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4244                         break;
4245                 case 1: /* 50 hz (filter on and framerate adj) */
4246                         i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4247                         i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4248                         /* 20 fps -> 16.667 fps */
4249                         if (sd->sensor == SEN_OV6620 ||
4250                             sd->sensor == SEN_OV6630 ||
4251                             sd->sensor == SEN_OV66308AF)
4252                                 i2c_w(sd, 0x2b, 0x5e);
4253                         else
4254                                 i2c_w(sd, 0x2b, 0xac);
4255                         break;
4256                 case 2: /* 60 hz (filter on, ...) */
4257                         i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4258                         if (sd->sensor == SEN_OV6620 ||
4259                             sd->sensor == SEN_OV6630 ||
4260                             sd->sensor == SEN_OV66308AF) {
4261                                 /* 20 fps -> 15 fps */
4262                                 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4263                                 i2c_w(sd, 0x2b, 0xa8);
4264                         } else {
4265                                 /* no framerate adj. */
4266                                 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4267                         }
4268                         break;
4269                 }
4270         }
4271 }
4272 static void setfreq(struct gspca_dev *gspca_dev)
4273 {
4274         struct sd *sd = (struct sd *) gspca_dev;
4275
4276         setfreq_i(sd);
4277
4278         /* Ugly but necessary */
4279         if (sd->bridge == BRIDGE_W9968CF)
4280                 w9968cf_set_crop_window(sd);
4281 }
4282
4283 static int sd_querymenu(struct gspca_dev *gspca_dev,
4284                         struct v4l2_querymenu *menu)
4285 {
4286         struct sd *sd = (struct sd *) gspca_dev;
4287
4288         switch (menu->id) {
4289         case V4L2_CID_POWER_LINE_FREQUENCY:
4290                 switch (menu->index) {
4291                 case 0:         /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
4292                         strcpy((char *) menu->name, "NoFliker");
4293                         return 0;
4294                 case 1:         /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
4295                         strcpy((char *) menu->name, "50 Hz");
4296                         return 0;
4297                 case 2:         /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
4298                         strcpy((char *) menu->name, "60 Hz");
4299                         return 0;
4300                 case 3:
4301                         if (sd->sensor != SEN_OV7670)
4302                                 return -EINVAL;
4303
4304                         strcpy((char *) menu->name, "Automatic");
4305                         return 0;
4306                 }
4307                 break;
4308         }
4309         return -EINVAL;
4310 }
4311
4312 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4313                         struct v4l2_jpegcompression *jcomp)
4314 {
4315         struct sd *sd = (struct sd *) gspca_dev;
4316
4317         if (sd->bridge != BRIDGE_W9968CF)
4318                 return -EINVAL;
4319
4320         memset(jcomp, 0, sizeof *jcomp);
4321         jcomp->quality = sd->quality;
4322         jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |
4323                               V4L2_JPEG_MARKER_DRI;
4324         return 0;
4325 }
4326
4327 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4328                         struct v4l2_jpegcompression *jcomp)
4329 {
4330         struct sd *sd = (struct sd *) gspca_dev;
4331
4332         if (sd->bridge != BRIDGE_W9968CF)
4333                 return -EINVAL;
4334
4335         if (gspca_dev->streaming)
4336                 return -EBUSY;
4337
4338         if (jcomp->quality < QUALITY_MIN)
4339                 sd->quality = QUALITY_MIN;
4340         else if (jcomp->quality > QUALITY_MAX)
4341                 sd->quality = QUALITY_MAX;
4342         else
4343                 sd->quality = jcomp->quality;
4344
4345         /* Return resulting jcomp params to app */
4346         sd_get_jcomp(gspca_dev, jcomp);
4347
4348         return 0;
4349 }
4350
4351 /* sub-driver description */
4352 static const struct sd_desc sd_desc = {
4353         .name = MODULE_NAME,
4354         .ctrls = sd_ctrls,
4355         .nctrls = ARRAY_SIZE(sd_ctrls),
4356         .config = sd_config,
4357         .init = sd_init,
4358         .start = sd_start,
4359         .stopN = sd_stopN,
4360         .stop0 = sd_stop0,
4361         .pkt_scan = sd_pkt_scan,
4362         .dq_callback = sd_reset_snapshot,
4363         .querymenu = sd_querymenu,
4364         .get_jcomp = sd_get_jcomp,
4365         .set_jcomp = sd_set_jcomp,
4366 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
4367         .other_input = 1,
4368 #endif
4369 };
4370
4371 /* -- module initialisation -- */
4372 static const __devinitdata struct usb_device_id device_table[] = {
4373         {USB_DEVICE(0x041e, 0x4003), .driver_info = BRIDGE_W9968CF },
4374         {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
4375         {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
4376         {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
4377         {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
4378         {USB_DEVICE(0x041e, 0x4064),
4379                 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4380         {USB_DEVICE(0x041e, 0x4067), .driver_info = BRIDGE_OV519 },
4381         {USB_DEVICE(0x041e, 0x4068),
4382                 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4383         {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
4384         {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
4385         {USB_DEVICE(0x054c, 0x0155),
4386                 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4387         {USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },
4388         {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
4389         {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
4390         {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
4391         {USB_DEVICE(0x05a9, 0x2800), .driver_info = BRIDGE_OVFX2 },
4392         {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
4393         {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
4394         {USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },
4395         {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
4396         {USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },
4397         {USB_DEVICE(0x0b62, 0x0059), .driver_info = BRIDGE_OVFX2 },
4398         {USB_DEVICE(0x0e96, 0xc001), .driver_info = BRIDGE_OVFX2 },
4399         {USB_DEVICE(0x1046, 0x9967), .driver_info = BRIDGE_W9968CF },
4400         {USB_DEVICE(0x8020, 0xef04), .driver_info = BRIDGE_OVFX2 },
4401         {}
4402 };
4403
4404 MODULE_DEVICE_TABLE(usb, device_table);
4405
4406 /* -- device connect -- */
4407 static int sd_probe(struct usb_interface *intf,
4408                         const struct usb_device_id *id)
4409 {
4410         return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
4411                                 THIS_MODULE);
4412 }
4413
4414 static struct usb_driver sd_driver = {
4415         .name = MODULE_NAME,
4416         .id_table = device_table,
4417         .probe = sd_probe,
4418         .disconnect = gspca_disconnect,
4419 #ifdef CONFIG_PM
4420         .suspend = gspca_suspend,
4421         .resume = gspca_resume,
4422 #endif
4423 };
4424
4425 /* -- module insert / remove -- */
4426 static int __init sd_mod_init(void)
4427 {
4428         return usb_register(&sd_driver);
4429 }
4430 static void __exit sd_mod_exit(void)
4431 {
4432         usb_deregister(&sd_driver);
4433 }
4434
4435 module_init(sd_mod_init);
4436 module_exit(sd_mod_exit);
4437
4438 module_param(frame_rate, int, 0644);
4439 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");