4 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
5 * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
7 * This module is adapted from the ov51x-jpeg package, which itself
8 * was adapted from the ov511 driver.
10 * Original copyright for the ov511 driver is:
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>
19 * ov51x-jpeg original copyright is:
21 * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
22 * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
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
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.
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
39 #define MODULE_NAME "ov519"
41 #include <linux/input.h>
44 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
45 MODULE_DESCRIPTION("OV519 USB Camera Driver");
46 MODULE_LICENSE("GPL");
48 /* global parameters */
49 static int frame_rate;
51 /* Number of times to retry a failed I2C transaction. Increase this if you
52 * are getting "Failed to read sensor ID..." */
53 static int i2c_detect_tries = 10;
55 /* ov519 device descriptor */
57 struct gspca_dev gspca_dev; /* !! must be the first item */
62 #define BRIDGE_OV511 0
63 #define BRIDGE_OV511PLUS 1
64 #define BRIDGE_OV518 2
65 #define BRIDGE_OV518PLUS 3
66 #define BRIDGE_OV519 4
67 #define BRIDGE_OVFX2 5
68 #define BRIDGE_W9968CF 6
72 #define BRIDGE_INVERT_LED 8
74 char snapshot_pressed;
75 char snapshot_needs_reset;
77 /* Determined by sensor type */
88 #define QUALITY_MIN 50
89 #define QUALITY_MAX 70
90 #define QUALITY_DEF 50
92 __u8 stopped; /* Streaming is temporarily paused */
95 __u8 frame_rate; /* current Framerate */
96 __u8 clockdiv; /* clockdiv override */
98 char sensor; /* Type of image sensor chip (SEN_*) */
104 #define SEN_OV66308AF 5
107 #define SEN_OV7620AE 8
109 #define SEN_OV7648 10
110 #define SEN_OV7670 11
111 #define SEN_OV76BE 12
112 #define SEN_OV8610 13
117 int sensor_reg_cache[256];
122 /* Note this is a bit of a hack, but the w9968cf driver needs the code for all
123 the ov sensors which is already present here. When we have the time we
124 really should move the sensor drivers to v4l2 sub drivers. */
127 /* V4L2 controls supported by the driver */
128 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
129 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
130 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
131 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
132 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
133 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
134 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
135 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
136 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
137 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
138 static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val);
139 static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val);
140 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
141 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
142 static void setbrightness(struct gspca_dev *gspca_dev);
143 static void setcontrast(struct gspca_dev *gspca_dev);
144 static void setcolors(struct gspca_dev *gspca_dev);
145 static void setautobrightness(struct sd *sd);
146 static void setfreq(struct sd *sd);
148 static const struct ctrl sd_ctrls[] = {
149 #define BRIGHTNESS_IDX 0
152 .id = V4L2_CID_BRIGHTNESS,
153 .type = V4L2_CTRL_TYPE_INTEGER,
154 .name = "Brightness",
158 #define BRIGHTNESS_DEF 127
159 .default_value = BRIGHTNESS_DEF,
161 .set = sd_setbrightness,
162 .get = sd_getbrightness,
164 #define CONTRAST_IDX 1
167 .id = V4L2_CID_CONTRAST,
168 .type = V4L2_CTRL_TYPE_INTEGER,
173 #define CONTRAST_DEF 127
174 .default_value = CONTRAST_DEF,
176 .set = sd_setcontrast,
177 .get = sd_getcontrast,
182 .id = V4L2_CID_SATURATION,
183 .type = V4L2_CTRL_TYPE_INTEGER,
188 #define COLOR_DEF 127
189 .default_value = COLOR_DEF,
194 /* The flip controls work with ov7670 only */
198 .id = V4L2_CID_HFLIP,
199 .type = V4L2_CTRL_TYPE_BOOLEAN,
205 .default_value = HFLIP_DEF,
213 .id = V4L2_CID_VFLIP,
214 .type = V4L2_CTRL_TYPE_BOOLEAN,
220 .default_value = VFLIP_DEF,
225 #define AUTOBRIGHT_IDX 5
228 .id = V4L2_CID_AUTOBRIGHTNESS,
229 .type = V4L2_CTRL_TYPE_BOOLEAN,
230 .name = "Auto Brightness",
234 #define AUTOBRIGHT_DEF 1
235 .default_value = AUTOBRIGHT_DEF,
237 .set = sd_setautobrightness,
238 .get = sd_getautobrightness,
243 .id = V4L2_CID_POWER_LINE_FREQUENCY,
244 .type = V4L2_CTRL_TYPE_MENU,
245 .name = "Light frequency filter",
247 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
250 .default_value = FREQ_DEF,
255 #define OV7670_FREQ_IDX 7
258 .id = V4L2_CID_POWER_LINE_FREQUENCY,
259 .type = V4L2_CTRL_TYPE_MENU,
260 .name = "Light frequency filter",
262 .maximum = 3, /* 0: 0, 1: 50Hz, 2:60Hz 3: Auto Hz */
264 #define OV7670_FREQ_DEF 3
265 .default_value = OV7670_FREQ_DEF,
272 static const struct v4l2_pix_format ov519_vga_mode[] = {
273 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
275 .sizeimage = 320 * 240 * 3 / 8 + 590,
276 .colorspace = V4L2_COLORSPACE_JPEG,
278 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
280 .sizeimage = 640 * 480 * 3 / 8 + 590,
281 .colorspace = V4L2_COLORSPACE_JPEG,
284 static const struct v4l2_pix_format ov519_sif_mode[] = {
285 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
287 .sizeimage = 160 * 120 * 3 / 8 + 590,
288 .colorspace = V4L2_COLORSPACE_JPEG,
290 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
292 .sizeimage = 176 * 144 * 3 / 8 + 590,
293 .colorspace = V4L2_COLORSPACE_JPEG,
295 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
297 .sizeimage = 320 * 240 * 3 / 8 + 590,
298 .colorspace = V4L2_COLORSPACE_JPEG,
300 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
302 .sizeimage = 352 * 288 * 3 / 8 + 590,
303 .colorspace = V4L2_COLORSPACE_JPEG,
307 /* Note some of the sizeimage values for the ov511 / ov518 may seem
308 larger then necessary, however they need to be this big as the ov511 /
309 ov518 always fills the entire isoc frame, using 0 padding bytes when
310 it doesn't have any data. So with low framerates the amount of data
311 transfered can become quite large (libv4l will remove all the 0 padding
313 static const struct v4l2_pix_format ov518_vga_mode[] = {
314 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
316 .sizeimage = 320 * 240 * 3,
317 .colorspace = V4L2_COLORSPACE_JPEG,
319 {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
321 .sizeimage = 640 * 480 * 2,
322 .colorspace = V4L2_COLORSPACE_JPEG,
325 static const struct v4l2_pix_format ov518_sif_mode[] = {
326 {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
329 .colorspace = V4L2_COLORSPACE_JPEG,
331 {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
334 .colorspace = V4L2_COLORSPACE_JPEG,
336 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
338 .sizeimage = 320 * 240 * 3,
339 .colorspace = V4L2_COLORSPACE_JPEG,
341 {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
343 .sizeimage = 352 * 288 * 3,
344 .colorspace = V4L2_COLORSPACE_JPEG,
348 static const struct v4l2_pix_format ov511_vga_mode[] = {
349 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
351 .sizeimage = 320 * 240 * 3,
352 .colorspace = V4L2_COLORSPACE_JPEG,
354 {640, 480, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
356 .sizeimage = 640 * 480 * 2,
357 .colorspace = V4L2_COLORSPACE_JPEG,
360 static const struct v4l2_pix_format ov511_sif_mode[] = {
361 {160, 120, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
364 .colorspace = V4L2_COLORSPACE_JPEG,
366 {176, 144, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
369 .colorspace = V4L2_COLORSPACE_JPEG,
371 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
373 .sizeimage = 320 * 240 * 3,
374 .colorspace = V4L2_COLORSPACE_JPEG,
376 {352, 288, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
378 .sizeimage = 352 * 288 * 3,
379 .colorspace = V4L2_COLORSPACE_JPEG,
383 static const struct v4l2_pix_format ovfx2_vga_mode[] = {
384 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
386 .sizeimage = 320 * 240,
387 .colorspace = V4L2_COLORSPACE_SRGB,
389 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
391 .sizeimage = 640 * 480,
392 .colorspace = V4L2_COLORSPACE_SRGB,
395 static const struct v4l2_pix_format ovfx2_cif_mode[] = {
396 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
398 .sizeimage = 160 * 120,
399 .colorspace = V4L2_COLORSPACE_SRGB,
401 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
403 .sizeimage = 176 * 144,
404 .colorspace = V4L2_COLORSPACE_SRGB,
406 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
408 .sizeimage = 320 * 240,
409 .colorspace = V4L2_COLORSPACE_SRGB,
411 {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
413 .sizeimage = 352 * 288,
414 .colorspace = V4L2_COLORSPACE_SRGB,
417 static const struct v4l2_pix_format ovfx2_ov2610_mode[] = {
418 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
419 .bytesperline = 1600,
420 .sizeimage = 1600 * 1200,
421 .colorspace = V4L2_COLORSPACE_SRGB},
423 static const struct v4l2_pix_format ovfx2_ov3610_mode[] = {
424 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
426 .sizeimage = 640 * 480,
427 .colorspace = V4L2_COLORSPACE_SRGB,
429 {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
431 .sizeimage = 800 * 600,
432 .colorspace = V4L2_COLORSPACE_SRGB,
434 {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
435 .bytesperline = 1024,
436 .sizeimage = 1024 * 768,
437 .colorspace = V4L2_COLORSPACE_SRGB,
439 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
440 .bytesperline = 1600,
441 .sizeimage = 1600 * 1200,
442 .colorspace = V4L2_COLORSPACE_SRGB,
444 {2048, 1536, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
445 .bytesperline = 2048,
446 .sizeimage = 2048 * 1536,
447 .colorspace = V4L2_COLORSPACE_SRGB,
452 /* Registers common to OV511 / OV518 */
453 #define R51x_FIFO_PSIZE 0x30 /* 2 bytes wide w/ OV518(+) */
454 #define R51x_SYS_RESET 0x50
455 /* Reset type flags */
456 #define OV511_RESET_OMNICE 0x08
457 #define R51x_SYS_INIT 0x53
458 #define R51x_SYS_SNAP 0x52
459 #define R51x_SYS_CUST_ID 0x5F
460 #define R51x_COMP_LUT_BEGIN 0x80
462 /* OV511 Camera interface register numbers */
463 #define R511_CAM_DELAY 0x10
464 #define R511_CAM_EDGE 0x11
465 #define R511_CAM_PXCNT 0x12
466 #define R511_CAM_LNCNT 0x13
467 #define R511_CAM_PXDIV 0x14
468 #define R511_CAM_LNDIV 0x15
469 #define R511_CAM_UV_EN 0x16
470 #define R511_CAM_LINE_MODE 0x17
471 #define R511_CAM_OPTS 0x18
473 #define R511_SNAP_FRAME 0x19
474 #define R511_SNAP_PXCNT 0x1A
475 #define R511_SNAP_LNCNT 0x1B
476 #define R511_SNAP_PXDIV 0x1C
477 #define R511_SNAP_LNDIV 0x1D
478 #define R511_SNAP_UV_EN 0x1E
479 #define R511_SNAP_UV_EN 0x1E
480 #define R511_SNAP_OPTS 0x1F
482 #define R511_DRAM_FLOW_CTL 0x20
483 #define R511_FIFO_OPTS 0x31
484 #define R511_I2C_CTL 0x40
485 #define R511_SYS_LED_CTL 0x55 /* OV511+ only */
486 #define R511_COMP_EN 0x78
487 #define R511_COMP_LUT_EN 0x79
489 /* OV518 Camera interface register numbers */
490 #define R518_GPIO_OUT 0x56 /* OV518(+) only */
491 #define R518_GPIO_CTL 0x57 /* OV518(+) only */
493 /* OV519 Camera interface register numbers */
494 #define OV519_R10_H_SIZE 0x10
495 #define OV519_R11_V_SIZE 0x11
496 #define OV519_R12_X_OFFSETL 0x12
497 #define OV519_R13_X_OFFSETH 0x13
498 #define OV519_R14_Y_OFFSETL 0x14
499 #define OV519_R15_Y_OFFSETH 0x15
500 #define OV519_R16_DIVIDER 0x16
501 #define OV519_R20_DFR 0x20
502 #define OV519_R25_FORMAT 0x25
504 /* OV519 System Controller register numbers */
505 #define OV519_SYS_RESET1 0x51
506 #define OV519_SYS_EN_CLK1 0x54
508 #define OV519_GPIO_DATA_OUT0 0x71
509 #define OV519_GPIO_IO_CTRL0 0x72
511 #define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
514 * The FX2 chip does not give us a zero length read at end of frame.
515 * It does, however, give a short read at the end of a frame, if
516 * necessary, rather than run two frames together.
518 * By choosing the right bulk transfer size, we are guaranteed to always
519 * get a short read for the last read of each frame. Frame sizes are
520 * always a composite number (width * height, or a multiple) so if we
521 * choose a prime number, we are guaranteed that the last read of a
522 * frame will be short.
524 * But it isn't that easy: the 2.6 kernel requires a multiple of 4KB,
525 * otherwise EOVERFLOW "babbling" errors occur. I have not been able
526 * to figure out why. [PMiller]
528 * The constant (13 * 4096) is the largest "prime enough" number less than 64KB.
530 * It isn't enough to know the number of bytes per frame, in case we
531 * have data dropouts or buffer overruns (even though the FX2 double
532 * buffers, there are some pretty strict real time constraints for
533 * isochronous transfer for larger frame sizes).
535 #define OVFX2_BULK_SIZE (13 * 4096)
538 #define R51x_I2C_W_SID 0x41
539 #define R51x_I2C_SADDR_3 0x42
540 #define R51x_I2C_SADDR_2 0x43
541 #define R51x_I2C_R_SID 0x44
542 #define R51x_I2C_DATA 0x45
543 #define R518_I2C_CTL 0x47 /* OV518(+) only */
544 #define OVFX2_I2C_ADDR 0x00
547 #define OV7xx0_SID 0x42
548 #define OV_HIRES_SID 0x60 /* OV9xxx / OV2xxx / OV3xxx */
549 #define OV8xx0_SID 0xa0
550 #define OV6xx0_SID 0xc0
552 /* OV7610 registers */
553 #define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
554 #define OV7610_REG_BLUE 0x01 /* blue channel balance */
555 #define OV7610_REG_RED 0x02 /* red channel balance */
556 #define OV7610_REG_SAT 0x03 /* saturation */
557 #define OV8610_REG_HUE 0x04 /* 04 reserved */
558 #define OV7610_REG_CNT 0x05 /* Y contrast */
559 #define OV7610_REG_BRT 0x06 /* Y brightness */
560 #define OV7610_REG_COM_C 0x14 /* misc common regs */
561 #define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
562 #define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
563 #define OV7610_REG_COM_I 0x29 /* misc settings */
565 /* OV7670 registers */
566 #define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
567 #define OV7670_REG_BLUE 0x01 /* blue gain */
568 #define OV7670_REG_RED 0x02 /* red gain */
569 #define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
570 #define OV7670_REG_COM1 0x04 /* Control 1 */
571 #define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
572 #define OV7670_REG_COM3 0x0c /* Control 3 */
573 #define OV7670_REG_COM4 0x0d /* Control 4 */
574 #define OV7670_REG_COM5 0x0e /* All "reserved" */
575 #define OV7670_REG_COM6 0x0f /* Control 6 */
576 #define OV7670_REG_AECH 0x10 /* More bits of AEC value */
577 #define OV7670_REG_CLKRC 0x11 /* Clock control */
578 #define OV7670_REG_COM7 0x12 /* Control 7 */
579 #define OV7670_COM7_FMT_VGA 0x00
580 #define OV7670_COM7_YUV 0x00 /* YUV */
581 #define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
582 #define OV7670_COM7_FMT_MASK 0x38
583 #define OV7670_COM7_RESET 0x80 /* Register reset */
584 #define OV7670_REG_COM8 0x13 /* Control 8 */
585 #define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
586 #define OV7670_COM8_AWB 0x02 /* White balance enable */
587 #define OV7670_COM8_AGC 0x04 /* Auto gain enable */
588 #define OV7670_COM8_BFILT 0x20 /* Band filter enable */
589 #define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
590 #define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
591 #define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
592 #define OV7670_REG_COM10 0x15 /* Control 10 */
593 #define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
594 #define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
595 #define OV7670_REG_VSTART 0x19 /* Vert start high bits */
596 #define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
597 #define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
598 #define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
599 #define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
600 #define OV7670_REG_AEW 0x24 /* AGC upper limit */
601 #define OV7670_REG_AEB 0x25 /* AGC lower limit */
602 #define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
603 #define OV7670_REG_HREF 0x32 /* HREF pieces */
604 #define OV7670_REG_TSLB 0x3a /* lots of stuff */
605 #define OV7670_REG_COM11 0x3b /* Control 11 */
606 #define OV7670_COM11_EXP 0x02
607 #define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
608 #define OV7670_REG_COM12 0x3c /* Control 12 */
609 #define OV7670_REG_COM13 0x3d /* Control 13 */
610 #define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
611 #define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
612 #define OV7670_REG_COM14 0x3e /* Control 14 */
613 #define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
614 #define OV7670_REG_COM15 0x40 /* Control 15 */
615 #define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
616 #define OV7670_REG_COM16 0x41 /* Control 16 */
617 #define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
618 #define OV7670_REG_BRIGHT 0x55 /* Brightness */
619 #define OV7670_REG_CONTRAS 0x56 /* Contrast control */
620 #define OV7670_REG_GFIX 0x69 /* Fix gain control */
621 #define OV7670_REG_RGB444 0x8c /* RGB 444 control */
622 #define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
623 #define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
624 #define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
625 #define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
626 #define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
627 #define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
628 #define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
629 #define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
630 #define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
636 struct ov_i2c_regvals {
641 /* Settings for OV2610 camera chip */
642 static const struct ov_i2c_regvals norm_2610[] =
644 { 0x12, 0x80 }, /* reset */
647 static const struct ov_i2c_regvals norm_3620b[] =
650 * From the datasheet: "Note that after writing to register COMH
651 * (0x12) to change the sensor mode, registers related to the
652 * sensor’s cropping window will be reset back to their default
655 * "wait 4096 external clock ... to make sure the sensor is
656 * stable and ready to access registers" i.e. 160us at 24MHz
659 { 0x12, 0x80 }, /* COMH reset */
660 { 0x12, 0x00 }, /* QXGA, master */
663 * 11 CLKRC "Clock Rate Control"
664 * [7] internal frequency doublers: on
665 * [6] video port mode: master
666 * [5:0] clock divider: 1
671 * 13 COMI "Common Control I"
672 * = 192 (0xC0) 11000000
673 * COMI[7] "AEC speed selection"
674 * = 1 (0x01) 1....... "Faster AEC correction"
675 * COMI[6] "AEC speed step selection"
676 * = 1 (0x01) .1...... "Big steps, fast"
677 * COMI[5] "Banding filter on off"
678 * = 0 (0x00) ..0..... "Off"
679 * COMI[4] "Banding filter option"
680 * = 0 (0x00) ...0.... "Main clock is 48 MHz and
683 * = 0 (0x00) ....0...
684 * COMI[2] "AGC auto manual control selection"
685 * = 0 (0x00) .....0.. "Manual"
686 * COMI[1] "AWB auto manual control selection"
687 * = 0 (0x00) ......0. "Manual"
688 * COMI[0] "Exposure control"
689 * = 0 (0x00) .......0 "Manual"
694 * 09 COMC "Common Control C"
695 * = 8 (0x08) 00001000
696 * COMC[7:5] "Reserved"
697 * = 0 (0x00) 000.....
698 * COMC[4] "Sleep Mode Enable"
699 * = 0 (0x00) ...0.... "Normal mode"
700 * COMC[3:2] "Sensor sampling reset timing selection"
701 * = 2 (0x02) ....10.. "Longer reset time"
702 * COMC[1:0] "Output drive current select"
703 * = 0 (0x00) ......00 "Weakest"
708 * 0C COMD "Common Control D"
709 * = 8 (0x08) 00001000
711 * = 0 (0x00) 0.......
712 * COMD[6] "Swap MSB and LSB at the output port"
713 * = 0 (0x00) .0...... "False"
714 * COMD[5:3] "Reserved"
715 * = 1 (0x01) ..001...
716 * COMD[2] "Output Average On Off"
717 * = 0 (0x00) .....0.. "Output Normal"
718 * COMD[1] "Sensor precharge voltage selection"
719 * = 0 (0x00) ......0. "Selects internal
720 * reference precharge
722 * COMD[0] "Snapshot option"
723 * = 0 (0x00) .......0 "Enable live video output
724 * after snapshot sequence"
729 * 0D COME "Common Control E"
730 * = 161 (0xA1) 10100001
731 * COME[7] "Output average option"
732 * = 1 (0x01) 1....... "Output average of 4 pixels"
733 * COME[6] "Anti-blooming control"
734 * = 0 (0x00) .0...... "Off"
735 * COME[5:3] "Reserved"
736 * = 4 (0x04) ..100...
737 * COME[2] "Clock output power down pin status"
738 * = 0 (0x00) .....0.. "Tri-state data output pin
740 * COME[1] "Data output pin status selection at power down"
741 * = 0 (0x00) ......0. "Tri-state VSYNC, PCLK,
742 * HREF, and CHSYNC pins on
744 * COME[0] "Auto zero circuit select"
745 * = 1 (0x01) .......1 "On"
750 * 0E COMF "Common Control F"
751 * = 112 (0x70) 01110000
752 * COMF[7] "System clock selection"
753 * = 0 (0x00) 0....... "Use 24 MHz system clock"
754 * COMF[6:4] "Reserved"
755 * = 7 (0x07) .111....
756 * COMF[3] "Manual auto negative offset canceling selection"
757 * = 0 (0x00) ....0... "Auto detect negative
758 * offset and cancel it"
759 * COMF[2:0] "Reserved"
760 * = 0 (0x00) .....000
765 * 0F COMG "Common Control G"
766 * = 66 (0x42) 01000010
767 * COMG[7] "Optical black output selection"
768 * = 0 (0x00) 0....... "Disable"
769 * COMG[6] "Black level calibrate selection"
770 * = 1 (0x01) .1...... "Use optical black pixels
772 * COMG[5:4] "Reserved"
773 * = 0 (0x00) ..00....
774 * COMG[3] "Channel offset adjustment"
775 * = 0 (0x00) ....0... "Disable offset adjustment"
776 * COMG[2] "ADC black level calibration option"
777 * = 0 (0x00) .....0.. "Use B/G line and G/R
778 * line to calibrate each
779 * channel's black level"
781 * = 1 (0x01) ......1.
782 * COMG[0] "ADC black level calibration enable"
783 * = 0 (0x00) .......0 "Disable"
788 * 14 COMJ "Common Control J"
789 * = 198 (0xC6) 11000110
790 * COMJ[7:6] "AGC gain ceiling"
791 * = 3 (0x03) 11...... "8x"
792 * COMJ[5:4] "Reserved"
793 * = 0 (0x00) ..00....
794 * COMJ[3] "Auto banding filter"
795 * = 0 (0x00) ....0... "Banding filter is always
796 * on off depending on
798 * COMJ[2] "VSYNC drop option"
799 * = 1 (0x01) .....1.. "SYNC is dropped if frame
801 * COMJ[1] "Frame data drop"
802 * = 1 (0x01) ......1. "Drop frame data if
803 * exposure is not within
804 * tolerance. In AEC mode,
805 * data is normally dropped
806 * when data is out of
809 * = 0 (0x00) .......0
814 * 15 COMK "Common Control K"
815 * = 2 (0x02) 00000010
816 * COMK[7] "CHSYNC pin output swap"
817 * = 0 (0x00) 0....... "CHSYNC"
818 * COMK[6] "HREF pin output swap"
819 * = 0 (0x00) .0...... "HREF"
820 * COMK[5] "PCLK output selection"
821 * = 0 (0x00) ..0..... "PCLK always output"
822 * COMK[4] "PCLK edge selection"
823 * = 0 (0x00) ...0.... "Data valid on falling edge"
824 * COMK[3] "HREF output polarity"
825 * = 0 (0x00) ....0... "positive"
827 * = 0 (0x00) .....0..
828 * COMK[1] "VSYNC polarity"
829 * = 1 (0x01) ......1. "negative"
830 * COMK[0] "HSYNC polarity"
831 * = 0 (0x00) .......0 "positive"
836 * 33 CHLF "Current Control"
837 * = 9 (0x09) 00001001
838 * CHLF[7:6] "Sensor current control"
839 * = 0 (0x00) 00......
840 * CHLF[5] "Sensor current range control"
841 * = 0 (0x00) ..0..... "normal range"
842 * CHLF[4] "Sensor current"
843 * = 0 (0x00) ...0.... "normal current"
844 * CHLF[3] "Sensor buffer current control"
845 * = 1 (0x01) ....1... "half current"
846 * CHLF[2] "Column buffer current control"
847 * = 0 (0x00) .....0.. "normal current"
848 * CHLF[1] "Analog DSP current control"
849 * = 0 (0x00) ......0. "normal current"
850 * CHLF[1] "ADC current control"
851 * = 0 (0x00) ......0. "normal current"
856 * 34 VBLM "Blooming Control"
857 * = 80 (0x50) 01010000
858 * VBLM[7] "Hard soft reset switch"
859 * = 0 (0x00) 0....... "Hard reset"
860 * VBLM[6:4] "Blooming voltage selection"
861 * = 5 (0x05) .101....
862 * VBLM[3:0] "Sensor current control"
863 * = 0 (0x00) ....0000
868 * 36 VCHG "Sensor Precharge Voltage Control"
869 * = 0 (0x00) 00000000
871 * = 0 (0x00) 0.......
872 * VCHG[6:4] "Sensor precharge voltage control"
873 * = 0 (0x00) .000....
874 * VCHG[3:0] "Sensor array common reference"
875 * = 0 (0x00) ....0000
880 * 37 ADC "ADC Reference Control"
881 * = 4 (0x04) 00000100
882 * ADC[7:4] "Reserved"
883 * = 0 (0x00) 0000....
884 * ADC[3] "ADC input signal range"
885 * = 0 (0x00) ....0... "Input signal 1.0x"
886 * ADC[2:0] "ADC range control"
887 * = 4 (0x04) .....100
892 * 38 ACOM "Analog Common Ground"
893 * = 82 (0x52) 01010010
894 * ACOM[7] "Analog gain control"
895 * = 0 (0x00) 0....... "Gain 1x"
896 * ACOM[6] "Analog black level calibration"
897 * = 1 (0x01) .1...... "On"
898 * ACOM[5:0] "Reserved"
899 * = 18 (0x12) ..010010
904 * 3A FREFA "Internal Reference Adjustment"
905 * = 0 (0x00) 00000000
907 * = 0 (0x00) 00000000
912 * 3C FVOPT "Internal Reference Adjustment"
913 * = 31 (0x1F) 00011111
915 * = 31 (0x1F) 00011111
920 * 44 Undocumented = 0 (0x00) 00000000
921 * 44[7:0] "It's a secret"
922 * = 0 (0x00) 00000000
927 * 40 Undocumented = 0 (0x00) 00000000
928 * 40[7:0] "It's a secret"
929 * = 0 (0x00) 00000000
934 * 41 Undocumented = 0 (0x00) 00000000
935 * 41[7:0] "It's a secret"
936 * = 0 (0x00) 00000000
941 * 42 Undocumented = 0 (0x00) 00000000
942 * 42[7:0] "It's a secret"
943 * = 0 (0x00) 00000000
948 * 43 Undocumented = 0 (0x00) 00000000
949 * 43[7:0] "It's a secret"
950 * = 0 (0x00) 00000000
955 * 45 Undocumented = 128 (0x80) 10000000
956 * 45[7:0] "It's a secret"
957 * = 128 (0x80) 10000000
962 * 48 Undocumented = 192 (0xC0) 11000000
963 * 48[7:0] "It's a secret"
964 * = 192 (0xC0) 11000000
969 * 49 Undocumented = 25 (0x19) 00011001
970 * 49[7:0] "It's a secret"
971 * = 25 (0x19) 00011001
976 * 4B Undocumented = 128 (0x80) 10000000
977 * 4B[7:0] "It's a secret"
978 * = 128 (0x80) 10000000
983 * 4D Undocumented = 196 (0xC4) 11000100
984 * 4D[7:0] "It's a secret"
985 * = 196 (0xC4) 11000100
990 * 35 VREF "Reference Voltage Control"
991 * = 76 (0x4C) 01001100
992 * VREF[7:5] "Column high reference control"
993 * = 2 (0x02) 010..... "higher voltage"
994 * VREF[4:2] "Column low reference control"
995 * = 3 (0x03) ...011.. "Highest voltage"
996 * VREF[1:0] "Reserved"
997 * = 0 (0x00) ......00
1002 * 3D Undocumented = 0 (0x00) 00000000
1003 * 3D[7:0] "It's a secret"
1004 * = 0 (0x00) 00000000
1009 * 3E Undocumented = 0 (0x00) 00000000
1010 * 3E[7:0] "It's a secret"
1011 * = 0 (0x00) 00000000
1016 * 3B FREFB "Internal Reference Adjustment"
1017 * = 24 (0x18) 00011000
1018 * FREFB[7:0] "Range"
1019 * = 24 (0x18) 00011000
1024 * 33 CHLF "Current Control"
1025 * = 25 (0x19) 00011001
1026 * CHLF[7:6] "Sensor current control"
1027 * = 0 (0x00) 00......
1028 * CHLF[5] "Sensor current range control"
1029 * = 0 (0x00) ..0..... "normal range"
1030 * CHLF[4] "Sensor current"
1031 * = 1 (0x01) ...1.... "double current"
1032 * CHLF[3] "Sensor buffer current control"
1033 * = 1 (0x01) ....1... "half current"
1034 * CHLF[2] "Column buffer current control"
1035 * = 0 (0x00) .....0.. "normal current"
1036 * CHLF[1] "Analog DSP current control"
1037 * = 0 (0x00) ......0. "normal current"
1038 * CHLF[1] "ADC current control"
1039 * = 0 (0x00) ......0. "normal current"
1044 * 34 VBLM "Blooming Control"
1045 * = 90 (0x5A) 01011010
1046 * VBLM[7] "Hard soft reset switch"
1047 * = 0 (0x00) 0....... "Hard reset"
1048 * VBLM[6:4] "Blooming voltage selection"
1049 * = 5 (0x05) .101....
1050 * VBLM[3:0] "Sensor current control"
1051 * = 10 (0x0A) ....1010
1056 * 3B FREFB "Internal Reference Adjustment"
1057 * = 0 (0x00) 00000000
1058 * FREFB[7:0] "Range"
1059 * = 0 (0x00) 00000000
1064 * 33 CHLF "Current Control"
1065 * = 9 (0x09) 00001001
1066 * CHLF[7:6] "Sensor current control"
1067 * = 0 (0x00) 00......
1068 * CHLF[5] "Sensor current range control"
1069 * = 0 (0x00) ..0..... "normal range"
1070 * CHLF[4] "Sensor current"
1071 * = 0 (0x00) ...0.... "normal current"
1072 * CHLF[3] "Sensor buffer current control"
1073 * = 1 (0x01) ....1... "half current"
1074 * CHLF[2] "Column buffer current control"
1075 * = 0 (0x00) .....0.. "normal current"
1076 * CHLF[1] "Analog DSP current control"
1077 * = 0 (0x00) ......0. "normal current"
1078 * CHLF[1] "ADC current control"
1079 * = 0 (0x00) ......0. "normal current"
1084 * 34 VBLM "Blooming Control"
1085 * = 80 (0x50) 01010000
1086 * VBLM[7] "Hard soft reset switch"
1087 * = 0 (0x00) 0....... "Hard reset"
1088 * VBLM[6:4] "Blooming voltage selection"
1089 * = 5 (0x05) .101....
1090 * VBLM[3:0] "Sensor current control"
1091 * = 0 (0x00) ....0000
1096 * 12 COMH "Common Control H"
1097 * = 64 (0x40) 01000000
1099 * = 0 (0x00) 0....... "No-op"
1100 * COMH[6:4] "Resolution selection"
1101 * = 4 (0x04) .100.... "XGA"
1102 * COMH[3] "Master slave selection"
1103 * = 0 (0x00) ....0... "Master mode"
1104 * COMH[2] "Internal B/R channel option"
1105 * = 0 (0x00) .....0.. "B/R use same channel"
1106 * COMH[1] "Color bar test pattern"
1107 * = 0 (0x00) ......0. "Off"
1108 * COMH[0] "Reserved"
1109 * = 0 (0x00) .......0
1114 * 17 HREFST "Horizontal window start"
1115 * = 31 (0x1F) 00011111
1116 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1117 * = 31 (0x1F) 00011111
1122 * 18 HREFEND "Horizontal window end"
1123 * = 95 (0x5F) 01011111
1124 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1125 * = 95 (0x5F) 01011111
1130 * 19 VSTRT "Vertical window start"
1131 * = 0 (0x00) 00000000
1132 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1133 * = 0 (0x00) 00000000
1138 * 1A VEND "Vertical window end"
1139 * = 96 (0x60) 01100000
1140 * VEND[7:0] "Vertical Window End, 8 MSBs"
1141 * = 96 (0x60) 01100000
1146 * 32 COMM "Common Control M"
1147 * = 18 (0x12) 00010010
1148 * COMM[7:6] "Pixel clock divide option"
1149 * = 0 (0x00) 00...... "/1"
1150 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1151 * = 2 (0x02) ..010...
1152 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1153 * = 2 (0x02) .....010
1158 * 03 COMA "Common Control A"
1159 * = 74 (0x4A) 01001010
1160 * COMA[7:4] "AWB Update Threshold"
1161 * = 4 (0x04) 0100....
1162 * COMA[3:2] "Vertical window end line control 2 LSBs"
1163 * = 2 (0x02) ....10..
1164 * COMA[1:0] "Vertical window start line control 2 LSBs"
1165 * = 2 (0x02) ......10
1170 * 11 CLKRC "Clock Rate Control"
1171 * = 128 (0x80) 10000000
1172 * CLKRC[7] "Internal frequency doublers on off seclection"
1173 * = 1 (0x01) 1....... "On"
1174 * CLKRC[6] "Digital video master slave selection"
1175 * = 0 (0x00) .0...... "Master mode, sensor
1177 * CLKRC[5:0] "Clock divider { CLK = PCLK/(1+CLKRC[5:0]) }"
1178 * = 0 (0x00) ..000000
1183 * 12 COMH "Common Control H"
1184 * = 0 (0x00) 00000000
1186 * = 0 (0x00) 0....... "No-op"
1187 * COMH[6:4] "Resolution selection"
1188 * = 0 (0x00) .000.... "QXGA"
1189 * COMH[3] "Master slave selection"
1190 * = 0 (0x00) ....0... "Master mode"
1191 * COMH[2] "Internal B/R channel option"
1192 * = 0 (0x00) .....0.. "B/R use same channel"
1193 * COMH[1] "Color bar test pattern"
1194 * = 0 (0x00) ......0. "Off"
1195 * COMH[0] "Reserved"
1196 * = 0 (0x00) .......0
1201 * 12 COMH "Common Control H"
1202 * = 64 (0x40) 01000000
1204 * = 0 (0x00) 0....... "No-op"
1205 * COMH[6:4] "Resolution selection"
1206 * = 4 (0x04) .100.... "XGA"
1207 * COMH[3] "Master slave selection"
1208 * = 0 (0x00) ....0... "Master mode"
1209 * COMH[2] "Internal B/R channel option"
1210 * = 0 (0x00) .....0.. "B/R use same channel"
1211 * COMH[1] "Color bar test pattern"
1212 * = 0 (0x00) ......0. "Off"
1213 * COMH[0] "Reserved"
1214 * = 0 (0x00) .......0
1219 * 17 HREFST "Horizontal window start"
1220 * = 31 (0x1F) 00011111
1221 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1222 * = 31 (0x1F) 00011111
1227 * 18 HREFEND "Horizontal window end"
1228 * = 95 (0x5F) 01011111
1229 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1230 * = 95 (0x5F) 01011111
1235 * 19 VSTRT "Vertical window start"
1236 * = 0 (0x00) 00000000
1237 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1238 * = 0 (0x00) 00000000
1243 * 1A VEND "Vertical window end"
1244 * = 96 (0x60) 01100000
1245 * VEND[7:0] "Vertical Window End, 8 MSBs"
1246 * = 96 (0x60) 01100000
1251 * 32 COMM "Common Control M"
1252 * = 18 (0x12) 00010010
1253 * COMM[7:6] "Pixel clock divide option"
1254 * = 0 (0x00) 00...... "/1"
1255 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1256 * = 2 (0x02) ..010...
1257 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1258 * = 2 (0x02) .....010
1263 * 03 COMA "Common Control A"
1264 * = 74 (0x4A) 01001010
1265 * COMA[7:4] "AWB Update Threshold"
1266 * = 4 (0x04) 0100....
1267 * COMA[3:2] "Vertical window end line control 2 LSBs"
1268 * = 2 (0x02) ....10..
1269 * COMA[1:0] "Vertical window start line control 2 LSBs"
1270 * = 2 (0x02) ......10
1275 * 02 RED "Red Gain Control"
1276 * = 175 (0xAF) 10101111
1278 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1280 * = 47 (0x2F) .0101111
1285 * 2D ADDVSL "VSYNC Pulse Width"
1286 * = 210 (0xD2) 11010010
1287 * ADDVSL[7:0] "VSYNC pulse width, LSB"
1288 * = 210 (0xD2) 11010010
1293 * 00 GAIN = 24 (0x18) 00011000
1294 * GAIN[7:6] "Reserved"
1295 * = 0 (0x00) 00......
1297 * = 0 (0x00) ..0..... "False"
1299 * = 1 (0x01) ...1.... "True"
1301 * = 8 (0x08) ....1000
1306 * 01 BLUE "Blue Gain Control"
1307 * = 240 (0xF0) 11110000
1309 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1311 * = 112 (0x70) .1110000
1316 * 10 AEC "Automatic Exposure Control"
1317 * = 10 (0x0A) 00001010
1318 * AEC[7:0] "Automatic Exposure Control, 8 MSBs"
1319 * = 10 (0x0A) 00001010
1331 static const struct ov_i2c_regvals norm_6x20[] = {
1332 { 0x12, 0x80 }, /* reset */
1335 { 0x05, 0x7f }, /* For when autoadjust is off */
1337 /* The ratio of 0x0c and 0x0d controls the white point */
1340 { 0x0f, 0x15 }, /* COMS */
1341 { 0x10, 0x75 }, /* AEC Exposure time */
1342 { 0x12, 0x24 }, /* Enable AGC */
1344 /* 0x16: 0x06 helps frame stability with moving objects */
1346 /* { 0x20, 0x30 }, * Aperture correction enable */
1347 { 0x26, 0xb2 }, /* BLC enable */
1348 /* 0x28: 0x05 Selects RGB format if RGB on */
1350 { 0x2a, 0x04 }, /* Disable framerate adjust */
1351 /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
1353 { 0x33, 0xa0 }, /* Color Processing Parameter */
1354 { 0x34, 0xd2 }, /* Max A/D range */
1358 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1359 { 0x3c, 0x3c }, /* Change AEC mode */
1360 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1363 /* These next two registers (0x4a, 0x4b) are undocumented.
1364 * They control the color balance */
1367 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1370 /* Do 50-53 have any effect? */
1371 /* Toggle 0x12[2] off and on here? */
1374 static const struct ov_i2c_regvals norm_6x30[] = {
1375 { 0x12, 0x80 }, /* Reset */
1376 { 0x00, 0x1f }, /* Gain */
1377 { 0x01, 0x99 }, /* Blue gain */
1378 { 0x02, 0x7c }, /* Red gain */
1379 { 0x03, 0xc0 }, /* Saturation */
1380 { 0x05, 0x0a }, /* Contrast */
1381 { 0x06, 0x95 }, /* Brightness */
1382 { 0x07, 0x2d }, /* Sharpness */
1385 { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
1388 { 0x11, 0x00 }, /* Pixel clock = fastest */
1389 { 0x12, 0x24 }, /* Enable AGC and AWB */
1404 { 0x23, 0xc0 }, /* Crystal circuit power level */
1405 { 0x25, 0x9a }, /* Increase AEC black ratio */
1406 { 0x26, 0xb2 }, /* BLC enable */
1410 { 0x2a, 0x84 }, /* 60 Hz power */
1411 { 0x2b, 0xa8 }, /* 60 Hz power */
1413 { 0x2d, 0x95 }, /* Enable auto-brightness */
1427 { 0x40, 0x00 }, /* White bal */
1428 { 0x41, 0x00 }, /* White bal */
1430 { 0x43, 0x3f }, /* White bal */
1440 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1442 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1444 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1449 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1451 { 0x5b, 0x0f }, /* AWB chrominance levels */
1455 { 0x12, 0x20 }, /* Toggle AWB */
1459 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
1461 * Register 0x0f in the 7610 has the following effects:
1463 * 0x85 (AEC method 1): Best overall, good contrast range
1464 * 0x45 (AEC method 2): Very overexposed
1465 * 0xa5 (spec sheet default): Ok, but the black level is
1466 * shifted resulting in loss of contrast
1467 * 0x05 (old driver setting): very overexposed, too much
1470 static const struct ov_i2c_regvals norm_7610[] = {
1477 { 0x28, 0x24 }, /* 0c */
1478 { 0x0f, 0x85 }, /* lg's setting */
1500 static const struct ov_i2c_regvals norm_7620[] = {
1501 { 0x12, 0x80 }, /* reset */
1502 { 0x00, 0x00 }, /* gain */
1503 { 0x01, 0x80 }, /* blue gain */
1504 { 0x02, 0x80 }, /* red gain */
1505 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
1528 { 0x28, 0x22 }, /* Was 0x20, bit1 enables a 2x gain which we need */
1567 /* 7640 and 7648. The defaults should be OK for most registers. */
1568 static const struct ov_i2c_regvals norm_7640[] = {
1573 /* 7670. Defaults taken from OmniVision provided data,
1574 * as provided by Jonathan Corbet of OLPC */
1575 static const struct ov_i2c_regvals norm_7670[] = {
1576 { OV7670_REG_COM7, OV7670_COM7_RESET },
1577 { OV7670_REG_TSLB, 0x04 }, /* OV */
1578 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
1579 { OV7670_REG_CLKRC, 0x01 },
1581 * Set the hardware window. These values from OV don't entirely
1582 * make sense - hstop is less than hstart. But they work...
1584 { OV7670_REG_HSTART, 0x13 },
1585 { OV7670_REG_HSTOP, 0x01 },
1586 { OV7670_REG_HREF, 0xb6 },
1587 { OV7670_REG_VSTART, 0x02 },
1588 { OV7670_REG_VSTOP, 0x7a },
1589 { OV7670_REG_VREF, 0x0a },
1591 { OV7670_REG_COM3, 0x00 },
1592 { OV7670_REG_COM14, 0x00 },
1593 /* Mystery scaling numbers */
1599 /* { OV7670_REG_COM10, 0x0 }, */
1601 /* Gamma curve values */
1619 /* AGC and AEC parameters. Note we start by disabling those features,
1620 then turn them only after tweaking the values. */
1621 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1622 | OV7670_COM8_AECSTEP
1623 | OV7670_COM8_BFILT },
1624 { OV7670_REG_GAIN, 0x00 },
1625 { OV7670_REG_AECH, 0x00 },
1626 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
1627 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
1628 { OV7670_REG_BD50MAX, 0x05 },
1629 { OV7670_REG_BD60MAX, 0x07 },
1630 { OV7670_REG_AEW, 0x95 },
1631 { OV7670_REG_AEB, 0x33 },
1632 { OV7670_REG_VPT, 0xe3 },
1633 { OV7670_REG_HAECC1, 0x78 },
1634 { OV7670_REG_HAECC2, 0x68 },
1635 { 0xa1, 0x03 }, /* magic */
1636 { OV7670_REG_HAECC3, 0xd8 },
1637 { OV7670_REG_HAECC4, 0xd8 },
1638 { OV7670_REG_HAECC5, 0xf0 },
1639 { OV7670_REG_HAECC6, 0x90 },
1640 { OV7670_REG_HAECC7, 0x94 },
1641 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1642 | OV7670_COM8_AECSTEP
1645 | OV7670_COM8_AEC },
1647 /* Almost all of these are magic "reserved" values. */
1648 { OV7670_REG_COM5, 0x61 },
1649 { OV7670_REG_COM6, 0x4b },
1651 { OV7670_REG_MVFP, 0x07 },
1660 { OV7670_REG_COM12, 0x78 },
1663 { OV7670_REG_GFIX, 0x00 },
1679 /* More reserved magic, some of which tweaks white balance */
1696 /* "9e for advance AWB" */
1698 { OV7670_REG_BLUE, 0x40 },
1699 { OV7670_REG_RED, 0x60 },
1700 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1701 | OV7670_COM8_AECSTEP
1705 | OV7670_COM8_AWB },
1707 /* Matrix coefficients */
1716 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
1717 { OV7670_REG_EDGE, 0x00 },
1722 { OV7670_REG_COM13, OV7670_COM13_GAMMA
1723 | OV7670_COM13_UVSAT
1727 { OV7670_REG_COM16, 0x38 },
1731 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
1744 /* Extra-weird stuff. Some sort of multiplexor register */
1770 static const struct ov_i2c_regvals norm_8610[] = {
1777 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
1778 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
1787 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
1789 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
1790 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
1791 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
1794 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
1795 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
1796 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
1797 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
1803 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
1805 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
1807 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
1809 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
1810 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
1811 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
1812 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
1814 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
1815 * maybe thats wrong */
1819 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
1823 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
1824 * deleting bit7 colors the first images red */
1825 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
1826 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
1832 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
1834 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
1839 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
1841 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
1842 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
1849 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
1855 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
1858 static unsigned char ov7670_abs_to_sm(unsigned char v)
1862 return (128 - v) | 0x80;
1865 /* Write a OV519 register */
1866 static int reg_w(struct sd *sd, __u16 index, __u16 value)
1870 switch (sd->bridge) {
1872 case BRIDGE_OV511PLUS:
1878 case BRIDGE_W9968CF:
1879 ret = usb_control_msg(sd->gspca_dev.dev,
1880 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1882 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1883 value, index, NULL, 0, 500);
1889 sd->gspca_dev.usb_buf[0] = value;
1890 ret = usb_control_msg(sd->gspca_dev.dev,
1891 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1893 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1895 sd->gspca_dev.usb_buf, 1, 500);
1898 PDEBUG(D_ERR, "Write reg 0x%04x -> [0x%02x] failed",
1903 PDEBUG(D_USBO, "Write reg 0x%04x -> [0x%02x]", value, index);
1907 /* Read from a OV519 register, note not valid for the w9968cf!! */
1908 /* returns: negative is error, pos or zero is data */
1909 static int reg_r(struct sd *sd, __u16 index)
1914 switch (sd->bridge) {
1916 case BRIDGE_OV511PLUS:
1926 ret = usb_control_msg(sd->gspca_dev.dev,
1927 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1929 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1930 0, index, sd->gspca_dev.usb_buf, 1, 500);
1933 ret = sd->gspca_dev.usb_buf[0];
1934 PDEBUG(D_USBI, "Read reg [0x%02X] -> 0x%04X", index, ret);
1936 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
1941 /* Read 8 values from a OV519 register */
1942 static int reg_r8(struct sd *sd,
1947 ret = usb_control_msg(sd->gspca_dev.dev,
1948 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1950 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1951 0, index, sd->gspca_dev.usb_buf, 8, 500);
1954 ret = sd->gspca_dev.usb_buf[0];
1956 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
1962 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1963 * the same position as 1's in "mask" are cleared and set to "value". Bits
1964 * that are in the same position as 0's in "mask" are preserved, regardless
1965 * of their respective state in "value".
1967 static int reg_w_mask(struct sd *sd,
1976 value &= mask; /* Enforce mask on value */
1977 ret = reg_r(sd, index);
1981 oldval = ret & ~mask; /* Clear the masked bits */
1982 value |= oldval; /* Set the desired bits */
1984 return reg_w(sd, index, value);
1988 * Writes multiple (n) byte value to a single register. Only valid with certain
1989 * registers (0x30 and 0xc4 - 0xce).
1991 static int ov518_reg_w32(struct sd *sd, __u16 index, u32 value, int n)
1995 *((__le32 *) sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
1997 ret = usb_control_msg(sd->gspca_dev.dev,
1998 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2000 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2002 sd->gspca_dev.usb_buf, n, 500);
2004 PDEBUG(D_ERR, "Write reg32 [%02x] %08x failed", index, value);
2011 static int ov511_i2c_w(struct sd *sd, __u8 reg, __u8 value)
2015 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
2017 /* Three byte write cycle */
2018 for (retries = 6; ; ) {
2019 /* Select camera register */
2020 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
2024 /* Write "value" to I2C data port of OV511 */
2025 rc = reg_w(sd, R51x_I2C_DATA, value);
2029 /* Initiate 3-byte write cycle */
2030 rc = reg_w(sd, R511_I2C_CTL, 0x01);
2035 rc = reg_r(sd, R511_I2C_CTL);
2036 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2041 if ((rc & 2) == 0) /* Ack? */
2043 if (--retries < 0) {
2044 PDEBUG(D_USBO, "i2c write retries exhausted");
2052 static int ov511_i2c_r(struct sd *sd, __u8 reg)
2054 int rc, value, retries;
2056 /* Two byte write cycle */
2057 for (retries = 6; ; ) {
2058 /* Select camera register */
2059 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
2063 /* Initiate 2-byte write cycle */
2064 rc = reg_w(sd, R511_I2C_CTL, 0x03);
2069 rc = reg_r(sd, R511_I2C_CTL);
2070 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2075 if ((rc & 2) == 0) /* Ack? */
2079 reg_w(sd, R511_I2C_CTL, 0x10);
2081 if (--retries < 0) {
2082 PDEBUG(D_USBI, "i2c write retries exhausted");
2087 /* Two byte read cycle */
2088 for (retries = 6; ; ) {
2089 /* Initiate 2-byte read cycle */
2090 rc = reg_w(sd, R511_I2C_CTL, 0x05);
2095 rc = reg_r(sd, R511_I2C_CTL);
2096 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2101 if ((rc & 2) == 0) /* Ack? */
2105 rc = reg_w(sd, R511_I2C_CTL, 0x10);
2109 if (--retries < 0) {
2110 PDEBUG(D_USBI, "i2c read retries exhausted");
2115 value = reg_r(sd, R51x_I2C_DATA);
2117 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
2119 /* This is needed to make i2c_w() work */
2120 rc = reg_w(sd, R511_I2C_CTL, 0x05);
2128 * The OV518 I2C I/O procedure is different, hence, this function.
2129 * This is normally only called from i2c_w(). Note that this function
2130 * always succeeds regardless of whether the sensor is present and working.
2132 static int ov518_i2c_w(struct sd *sd,
2138 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
2140 /* Select camera register */
2141 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
2145 /* Write "value" to I2C data port of OV511 */
2146 rc = reg_w(sd, R51x_I2C_DATA, value);
2150 /* Initiate 3-byte write cycle */
2151 rc = reg_w(sd, R518_I2C_CTL, 0x01);
2155 /* wait for write complete */
2157 return reg_r8(sd, R518_I2C_CTL);
2161 * returns: negative is error, pos or zero is data
2163 * The OV518 I2C I/O procedure is different, hence, this function.
2164 * This is normally only called from i2c_r(). Note that this function
2165 * always succeeds regardless of whether the sensor is present and working.
2167 static int ov518_i2c_r(struct sd *sd, __u8 reg)
2171 /* Select camera register */
2172 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
2176 /* Initiate 2-byte write cycle */
2177 rc = reg_w(sd, R518_I2C_CTL, 0x03);
2181 /* Initiate 2-byte read cycle */
2182 rc = reg_w(sd, R518_I2C_CTL, 0x05);
2185 value = reg_r(sd, R51x_I2C_DATA);
2186 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
2190 static int ovfx2_i2c_w(struct sd *sd, __u8 reg, __u8 value)
2194 ret = usb_control_msg(sd->gspca_dev.dev,
2195 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2197 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2198 (__u16)value, (__u16)reg, NULL, 0, 500);
2201 PDEBUG(D_ERR, "i2c 0x%02x -> [0x%02x] failed", value, reg);
2205 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
2209 static int ovfx2_i2c_r(struct sd *sd, __u8 reg)
2213 ret = usb_control_msg(sd->gspca_dev.dev,
2214 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
2216 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2217 0, (__u16)reg, sd->gspca_dev.usb_buf, 1, 500);
2220 ret = sd->gspca_dev.usb_buf[0];
2221 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, ret);
2223 PDEBUG(D_ERR, "i2c read [0x%02x] failed", reg);
2228 static int i2c_w(struct sd *sd, __u8 reg, __u8 value)
2232 if (sd->sensor_reg_cache[reg] == value)
2235 switch (sd->bridge) {
2237 case BRIDGE_OV511PLUS:
2238 ret = ov511_i2c_w(sd, reg, value);
2241 case BRIDGE_OV518PLUS:
2243 ret = ov518_i2c_w(sd, reg, value);
2246 ret = ovfx2_i2c_w(sd, reg, value);
2248 case BRIDGE_W9968CF:
2249 ret = w9968cf_i2c_w(sd, reg, value);
2254 /* Up on sensor reset empty the register cache */
2255 if (reg == 0x12 && (value & 0x80))
2256 memset(sd->sensor_reg_cache, -1,
2257 sizeof(sd->sensor_reg_cache));
2259 sd->sensor_reg_cache[reg] = value;
2265 static int i2c_r(struct sd *sd, __u8 reg)
2269 if (sd->sensor_reg_cache[reg] != -1)
2270 return sd->sensor_reg_cache[reg];
2272 switch (sd->bridge) {
2274 case BRIDGE_OV511PLUS:
2275 ret = ov511_i2c_r(sd, reg);
2278 case BRIDGE_OV518PLUS:
2280 ret = ov518_i2c_r(sd, reg);
2283 ret = ovfx2_i2c_r(sd, reg);
2285 case BRIDGE_W9968CF:
2286 ret = w9968cf_i2c_r(sd, reg);
2291 sd->sensor_reg_cache[reg] = ret;
2296 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
2297 * the same position as 1's in "mask" are cleared and set to "value". Bits
2298 * that are in the same position as 0's in "mask" are preserved, regardless
2299 * of their respective state in "value".
2301 static int i2c_w_mask(struct sd *sd,
2309 value &= mask; /* Enforce mask on value */
2310 rc = i2c_r(sd, reg);
2313 oldval = rc & ~mask; /* Clear the masked bits */
2314 value |= oldval; /* Set the desired bits */
2315 return i2c_w(sd, reg, value);
2318 /* Temporarily stops OV511 from functioning. Must do this before changing
2319 * registers while the camera is streaming */
2320 static inline int ov51x_stop(struct sd *sd)
2322 PDEBUG(D_STREAM, "stopping");
2324 switch (sd->bridge) {
2326 case BRIDGE_OV511PLUS:
2327 return reg_w(sd, R51x_SYS_RESET, 0x3d);
2329 case BRIDGE_OV518PLUS:
2330 return reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
2332 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
2334 return reg_w_mask(sd, 0x0f, 0x00, 0x02);
2335 case BRIDGE_W9968CF:
2336 return reg_w(sd, 0x3c, 0x0a05); /* stop USB transfer */
2342 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
2343 * actually stopped (for performance). */
2344 static inline int ov51x_restart(struct sd *sd)
2348 PDEBUG(D_STREAM, "restarting");
2353 /* Reinitialize the stream */
2354 switch (sd->bridge) {
2356 case BRIDGE_OV511PLUS:
2357 return reg_w(sd, R51x_SYS_RESET, 0x00);
2359 case BRIDGE_OV518PLUS:
2360 rc = reg_w(sd, 0x2f, 0x80);
2363 return reg_w(sd, R51x_SYS_RESET, 0x00);
2365 return reg_w(sd, OV519_SYS_RESET1, 0x00);
2367 return reg_w_mask(sd, 0x0f, 0x02, 0x02);
2368 case BRIDGE_W9968CF:
2369 return reg_w(sd, 0x3c, 0x8a05); /* USB FIFO enable */
2375 static int ov51x_set_slave_ids(struct sd *sd, __u8 slave);
2377 /* This does an initial reset of an OmniVision sensor and ensures that I2C
2378 * is synchronized. Returns <0 on failure.
2380 static int init_ov_sensor(struct sd *sd, __u8 slave)
2384 if (ov51x_set_slave_ids(sd, slave) < 0)
2387 /* Reset the sensor */
2388 if (i2c_w(sd, 0x12, 0x80) < 0)
2391 /* Wait for it to initialize */
2394 for (i = 0; i < i2c_detect_tries; i++) {
2395 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
2396 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
2397 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
2401 /* Reset the sensor */
2402 if (i2c_w(sd, 0x12, 0x80) < 0)
2404 /* Wait for it to initialize */
2406 /* Dummy read to sync I2C */
2407 if (i2c_r(sd, 0x00) < 0)
2413 /* Set the read and write slave IDs. The "slave" argument is the write slave,
2414 * and the read slave will be set to (slave + 1).
2415 * This should not be called from outside the i2c I/O functions.
2416 * Sets I2C read and write slave IDs. Returns <0 for error
2418 static int ov51x_set_slave_ids(struct sd *sd,
2423 switch (sd->bridge) {
2425 return reg_w(sd, OVFX2_I2C_ADDR, slave);
2426 case BRIDGE_W9968CF:
2427 sd->sensor_addr = slave;
2431 rc = reg_w(sd, R51x_I2C_W_SID, slave);
2434 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
2437 static int write_regvals(struct sd *sd,
2438 const struct ov_regvals *regvals,
2444 rc = reg_w(sd, regvals->reg, regvals->val);
2452 static int write_i2c_regvals(struct sd *sd,
2453 const struct ov_i2c_regvals *regvals,
2459 rc = i2c_w(sd, regvals->reg, regvals->val);
2467 /****************************************************************************
2469 * OV511 and sensor configuration
2471 ***************************************************************************/
2473 /* This initializes the OV2x10 / OV3610 / OV3620 */
2474 static int ov_hires_configure(struct sd *sd)
2478 if (sd->bridge != BRIDGE_OVFX2) {
2479 PDEBUG(D_ERR, "error hires sensors only supported with ovfx2");
2483 PDEBUG(D_PROBE, "starting ov hires configuration");
2485 /* Detect sensor (sub)type */
2486 high = i2c_r(sd, 0x0a);
2487 low = i2c_r(sd, 0x0b);
2488 /* info("%x, %x", high, low); */
2489 if (high == 0x96 && low == 0x40) {
2490 PDEBUG(D_PROBE, "Sensor is an OV2610");
2491 sd->sensor = SEN_OV2610;
2492 } else if (high == 0x36 && (low & 0x0f) == 0x00) {
2493 PDEBUG(D_PROBE, "Sensor is an OV3610");
2494 sd->sensor = SEN_OV3610;
2496 PDEBUG(D_ERR, "Error unknown sensor type: 0x%02x%02x",
2501 /* Set sensor-specific vars */
2506 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
2507 * the same register settings as the OV8610, since they are very similar.
2509 static int ov8xx0_configure(struct sd *sd)
2513 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
2515 /* Detect sensor (sub)type */
2516 rc = i2c_r(sd, OV7610_REG_COM_I);
2518 PDEBUG(D_ERR, "Error detecting sensor type");
2521 if ((rc & 3) == 1) {
2522 sd->sensor = SEN_OV8610;
2524 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
2528 /* Set sensor-specific vars */
2532 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
2533 * the same register settings as the OV7610, since they are very similar.
2535 static int ov7xx0_configure(struct sd *sd)
2540 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
2542 /* Detect sensor (sub)type */
2543 rc = i2c_r(sd, OV7610_REG_COM_I);
2546 * it appears to be wrongly detected as a 7610 by default */
2548 PDEBUG(D_ERR, "Error detecting sensor type");
2551 if ((rc & 3) == 3) {
2552 /* quick hack to make OV7670s work */
2553 high = i2c_r(sd, 0x0a);
2554 low = i2c_r(sd, 0x0b);
2555 /* info("%x, %x", high, low); */
2556 if (high == 0x76 && low == 0x73) {
2557 PDEBUG(D_PROBE, "Sensor is an OV7670");
2558 sd->sensor = SEN_OV7670;
2560 PDEBUG(D_PROBE, "Sensor is an OV7610");
2561 sd->sensor = SEN_OV7610;
2563 } else if ((rc & 3) == 1) {
2564 /* I don't know what's different about the 76BE yet. */
2565 if (i2c_r(sd, 0x15) & 1) {
2566 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
2567 sd->sensor = SEN_OV7620AE;
2569 PDEBUG(D_PROBE, "Sensor is an OV76BE");
2570 sd->sensor = SEN_OV76BE;
2572 } else if ((rc & 3) == 0) {
2573 /* try to read product id registers */
2574 high = i2c_r(sd, 0x0a);
2576 PDEBUG(D_ERR, "Error detecting camera chip PID");
2579 low = i2c_r(sd, 0x0b);
2581 PDEBUG(D_ERR, "Error detecting camera chip VER");
2587 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
2589 "7630 is not supported by this driver");
2592 PDEBUG(D_PROBE, "Sensor is an OV7645");
2593 sd->sensor = SEN_OV7640; /* FIXME */
2596 PDEBUG(D_PROBE, "Sensor is an OV7645B");
2597 sd->sensor = SEN_OV7640; /* FIXME */
2600 PDEBUG(D_PROBE, "Sensor is an OV7648");
2601 sd->sensor = SEN_OV7648;
2604 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
2608 PDEBUG(D_PROBE, "Sensor is an OV7620");
2609 sd->sensor = SEN_OV7620;
2612 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
2616 /* Set sensor-specific vars */
2620 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
2621 static int ov6xx0_configure(struct sd *sd)
2624 PDEBUG(D_PROBE, "starting OV6xx0 configuration");
2626 /* Detect sensor (sub)type */
2627 rc = i2c_r(sd, OV7610_REG_COM_I);
2629 PDEBUG(D_ERR, "Error detecting sensor type");
2633 /* Ugh. The first two bits are the version bits, but
2634 * the entire register value must be used. I guess OVT
2635 * underestimated how many variants they would make. */
2638 sd->sensor = SEN_OV6630;
2640 "WARNING: Sensor is an OV66308. Your camera may have");
2641 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
2644 sd->sensor = SEN_OV6620;
2645 PDEBUG(D_PROBE, "Sensor is an OV6620");
2648 sd->sensor = SEN_OV6630;
2649 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
2652 sd->sensor = SEN_OV66308AF;
2653 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
2656 sd->sensor = SEN_OV6630;
2658 "WARNING: Sensor is an OV66307. Your camera may have");
2659 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
2662 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
2666 /* Set sensor-specific vars */
2672 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
2673 static void ov51x_led_control(struct sd *sd, int on)
2678 switch (sd->bridge) {
2679 /* OV511 has no LED control */
2680 case BRIDGE_OV511PLUS:
2681 reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
2684 case BRIDGE_OV518PLUS:
2685 reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
2688 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
2693 static void sd_reset_snapshot(struct gspca_dev *gspca_dev)
2695 struct sd *sd = (struct sd *) gspca_dev;
2697 if (!sd->snapshot_needs_reset)
2700 /* Note it is important that we clear sd->snapshot_needs_reset,
2701 before actually clearing the snapshot state in the bridge
2702 otherwise we might race with the pkt_scan interrupt handler */
2703 sd->snapshot_needs_reset = 0;
2705 switch (sd->bridge) {
2707 case BRIDGE_OV511PLUS:
2708 reg_w(sd, R51x_SYS_SNAP, 0x02);
2709 reg_w(sd, R51x_SYS_SNAP, 0x00);
2712 case BRIDGE_OV518PLUS:
2713 reg_w(sd, R51x_SYS_SNAP, 0x02); /* Reset */
2714 reg_w(sd, R51x_SYS_SNAP, 0x01); /* Enable */
2717 reg_w(sd, R51x_SYS_RESET, 0x40);
2718 reg_w(sd, R51x_SYS_RESET, 0x00);
2723 static int ov51x_upload_quan_tables(struct sd *sd)
2725 const unsigned char yQuanTable511[] = {
2726 0, 1, 1, 2, 2, 3, 3, 4,
2727 1, 1, 1, 2, 2, 3, 4, 4,
2728 1, 1, 2, 2, 3, 4, 4, 4,
2729 2, 2, 2, 3, 4, 4, 4, 4,
2730 2, 2, 3, 4, 4, 5, 5, 5,
2731 3, 3, 4, 4, 5, 5, 5, 5,
2732 3, 4, 4, 4, 5, 5, 5, 5,
2733 4, 4, 4, 4, 5, 5, 5, 5
2736 const unsigned char uvQuanTable511[] = {
2737 0, 2, 2, 3, 4, 4, 4, 4,
2738 2, 2, 2, 4, 4, 4, 4, 4,
2739 2, 2, 3, 4, 4, 4, 4, 4,
2740 3, 4, 4, 4, 4, 4, 4, 4,
2741 4, 4, 4, 4, 4, 4, 4, 4,
2742 4, 4, 4, 4, 4, 4, 4, 4,
2743 4, 4, 4, 4, 4, 4, 4, 4,
2744 4, 4, 4, 4, 4, 4, 4, 4
2747 /* OV518 quantization tables are 8x4 (instead of 8x8) */
2748 const unsigned char yQuanTable518[] = {
2749 5, 4, 5, 6, 6, 7, 7, 7,
2750 5, 5, 5, 5, 6, 7, 7, 7,
2751 6, 6, 6, 6, 7, 7, 7, 8,
2752 7, 7, 6, 7, 7, 7, 8, 8
2755 const unsigned char uvQuanTable518[] = {
2756 6, 6, 6, 7, 7, 7, 7, 7,
2757 6, 6, 6, 7, 7, 7, 7, 7,
2758 6, 6, 6, 7, 7, 7, 7, 8,
2759 7, 7, 7, 7, 7, 7, 8, 8
2762 const unsigned char *pYTable, *pUVTable;
2763 unsigned char val0, val1;
2764 int i, size, rc, reg = R51x_COMP_LUT_BEGIN;
2766 PDEBUG(D_PROBE, "Uploading quantization tables");
2768 if (sd->bridge == BRIDGE_OV511 || sd->bridge == BRIDGE_OV511PLUS) {
2769 pYTable = yQuanTable511;
2770 pUVTable = uvQuanTable511;
2773 pYTable = yQuanTable518;
2774 pUVTable = uvQuanTable518;
2778 for (i = 0; i < size; i++) {
2784 rc = reg_w(sd, reg, val0);
2793 rc = reg_w(sd, reg + size, val0);
2803 /* This initializes the OV511/OV511+ and the sensor */
2804 static int ov511_configure(struct gspca_dev *gspca_dev)
2806 struct sd *sd = (struct sd *) gspca_dev;
2809 /* For 511 and 511+ */
2810 const struct ov_regvals init_511[] = {
2811 { R51x_SYS_RESET, 0x7f },
2812 { R51x_SYS_INIT, 0x01 },
2813 { R51x_SYS_RESET, 0x7f },
2814 { R51x_SYS_INIT, 0x01 },
2815 { R51x_SYS_RESET, 0x3f },
2816 { R51x_SYS_INIT, 0x01 },
2817 { R51x_SYS_RESET, 0x3d },
2820 const struct ov_regvals norm_511[] = {
2821 { R511_DRAM_FLOW_CTL, 0x01 },
2822 { R51x_SYS_SNAP, 0x00 },
2823 { R51x_SYS_SNAP, 0x02 },
2824 { R51x_SYS_SNAP, 0x00 },
2825 { R511_FIFO_OPTS, 0x1f },
2826 { R511_COMP_EN, 0x00 },
2827 { R511_COMP_LUT_EN, 0x03 },
2830 const struct ov_regvals norm_511_p[] = {
2831 { R511_DRAM_FLOW_CTL, 0xff },
2832 { R51x_SYS_SNAP, 0x00 },
2833 { R51x_SYS_SNAP, 0x02 },
2834 { R51x_SYS_SNAP, 0x00 },
2835 { R511_FIFO_OPTS, 0xff },
2836 { R511_COMP_EN, 0x00 },
2837 { R511_COMP_LUT_EN, 0x03 },
2840 const struct ov_regvals compress_511[] = {
2851 PDEBUG(D_PROBE, "Device custom id %x", reg_r(sd, R51x_SYS_CUST_ID));
2853 rc = write_regvals(sd, init_511, ARRAY_SIZE(init_511));
2857 switch (sd->bridge) {
2859 rc = write_regvals(sd, norm_511, ARRAY_SIZE(norm_511));
2863 case BRIDGE_OV511PLUS:
2864 rc = write_regvals(sd, norm_511_p, ARRAY_SIZE(norm_511_p));
2870 /* Init compression */
2871 rc = write_regvals(sd, compress_511, ARRAY_SIZE(compress_511));
2875 rc = ov51x_upload_quan_tables(sd);
2877 PDEBUG(D_ERR, "Error uploading quantization tables");
2884 /* This initializes the OV518/OV518+ and the sensor */
2885 static int ov518_configure(struct gspca_dev *gspca_dev)
2887 struct sd *sd = (struct sd *) gspca_dev;
2890 /* For 518 and 518+ */
2891 const struct ov_regvals init_518[] = {
2892 { R51x_SYS_RESET, 0x40 },
2893 { R51x_SYS_INIT, 0xe1 },
2894 { R51x_SYS_RESET, 0x3e },
2895 { R51x_SYS_INIT, 0xe1 },
2896 { R51x_SYS_RESET, 0x00 },
2897 { R51x_SYS_INIT, 0xe1 },
2902 const struct ov_regvals norm_518[] = {
2903 { R51x_SYS_SNAP, 0x02 }, /* Reset */
2904 { R51x_SYS_SNAP, 0x01 }, /* Enable */
2915 const struct ov_regvals norm_518_p[] = {
2916 { R51x_SYS_SNAP, 0x02 }, /* Reset */
2917 { R51x_SYS_SNAP, 0x01 }, /* Enable */
2934 /* First 5 bits of custom ID reg are a revision ID on OV518 */
2935 PDEBUG(D_PROBE, "Device revision %d",
2936 0x1F & reg_r(sd, R51x_SYS_CUST_ID));
2938 rc = write_regvals(sd, init_518, ARRAY_SIZE(init_518));
2942 /* Set LED GPIO pin to output mode */
2943 rc = reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
2947 switch (sd->bridge) {
2949 rc = write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
2953 case BRIDGE_OV518PLUS:
2954 rc = write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
2960 rc = ov51x_upload_quan_tables(sd);
2962 PDEBUG(D_ERR, "Error uploading quantization tables");
2966 rc = reg_w(sd, 0x2f, 0x80);
2973 static int ov519_configure(struct sd *sd)
2975 static const struct ov_regvals init_519[] = {
2976 { 0x5a, 0x6d }, /* EnableSystem */
2978 { 0x54, 0xff }, /* set bit2 to enable jpeg */
2982 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
2983 * detection will fail. This deserves further investigation. */
2984 { OV519_GPIO_IO_CTRL0, 0xee },
2985 { 0x51, 0x0f }, /* SetUsbInit */
2988 /* windows reads 0x55 at this point*/
2991 return write_regvals(sd, init_519, ARRAY_SIZE(init_519));
2994 static int ovfx2_configure(struct sd *sd)
2996 static const struct ov_regvals init_fx2[] = {
3008 return write_regvals(sd, init_fx2, ARRAY_SIZE(init_fx2));
3011 /* this function is called at probe time */
3012 static int sd_config(struct gspca_dev *gspca_dev,
3013 const struct usb_device_id *id)
3015 struct sd *sd = (struct sd *) gspca_dev;
3016 struct cam *cam = &gspca_dev->cam;
3019 sd->bridge = id->driver_info & BRIDGE_MASK;
3020 sd->invert_led = id->driver_info & BRIDGE_INVERT_LED;
3022 switch (sd->bridge) {
3024 case BRIDGE_OV511PLUS:
3025 ret = ov511_configure(gspca_dev);
3028 case BRIDGE_OV518PLUS:
3029 ret = ov518_configure(gspca_dev);
3032 ret = ov519_configure(sd);
3035 ret = ovfx2_configure(sd);
3036 cam->bulk_size = OVFX2_BULK_SIZE;
3037 cam->bulk_nurbs = MAX_NURBS;
3040 case BRIDGE_W9968CF:
3041 ret = w9968cf_configure(sd);
3042 cam->reverse_alts = 1;
3049 ov51x_led_control(sd, 0); /* turn LED off */
3051 /* The OV519 must be more aggressive about sensor detection since
3052 * I2C write will never fail if the sensor is not present. We have
3053 * to try to initialize the sensor to detect its presence */
3056 if (init_ov_sensor(sd, OV7xx0_SID) >= 0) {
3057 if (ov7xx0_configure(sd) < 0) {
3058 PDEBUG(D_ERR, "Failed to configure OV7xx0");
3062 } else if (init_ov_sensor(sd, OV6xx0_SID) >= 0) {
3063 if (ov6xx0_configure(sd) < 0) {
3064 PDEBUG(D_ERR, "Failed to configure OV6xx0");
3068 } else if (init_ov_sensor(sd, OV8xx0_SID) >= 0) {
3069 if (ov8xx0_configure(sd) < 0) {
3070 PDEBUG(D_ERR, "Failed to configure OV8xx0");
3073 /* Test for 3xxx / 2xxx */
3074 } else if (init_ov_sensor(sd, OV_HIRES_SID) >= 0) {
3075 if (ov_hires_configure(sd) < 0) {
3076 PDEBUG(D_ERR, "Failed to configure high res OV");
3080 PDEBUG(D_ERR, "Can't determine sensor slave IDs");
3084 switch (sd->bridge) {
3086 case BRIDGE_OV511PLUS:
3088 cam->cam_mode = ov511_vga_mode;
3089 cam->nmodes = ARRAY_SIZE(ov511_vga_mode);
3091 cam->cam_mode = ov511_sif_mode;
3092 cam->nmodes = ARRAY_SIZE(ov511_sif_mode);
3096 case BRIDGE_OV518PLUS:
3098 cam->cam_mode = ov518_vga_mode;
3099 cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
3101 cam->cam_mode = ov518_sif_mode;
3102 cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
3107 cam->cam_mode = ov519_vga_mode;
3108 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3110 cam->cam_mode = ov519_sif_mode;
3111 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3115 if (sd->sensor == SEN_OV2610) {
3116 cam->cam_mode = ovfx2_ov2610_mode;
3117 cam->nmodes = ARRAY_SIZE(ovfx2_ov2610_mode);
3118 } else if (sd->sensor == SEN_OV3610) {
3119 cam->cam_mode = ovfx2_ov3610_mode;
3120 cam->nmodes = ARRAY_SIZE(ovfx2_ov3610_mode);
3121 } else if (!sd->sif) {
3122 cam->cam_mode = ov519_vga_mode;
3123 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3125 cam->cam_mode = ov519_sif_mode;
3126 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3129 case BRIDGE_W9968CF:
3130 cam->cam_mode = w9968cf_vga_mode;
3131 cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode);
3135 /* w9968cf needs initialisation once the sensor is known */
3136 if (w9968cf_init(sd) < 0)
3140 sd->brightness = BRIGHTNESS_DEF;
3141 if (sd->sensor == SEN_OV6630 || sd->sensor == SEN_OV66308AF)
3142 sd->contrast = 200; /* The default is too low for the ov6630 */
3144 sd->contrast = CONTRAST_DEF;
3145 sd->colors = COLOR_DEF;
3146 sd->hflip = HFLIP_DEF;
3147 sd->vflip = VFLIP_DEF;
3148 sd->autobrightness = AUTOBRIGHT_DEF;
3149 if (sd->sensor == SEN_OV7670) {
3150 sd->freq = OV7670_FREQ_DEF;
3151 gspca_dev->ctrl_dis = (1 << FREQ_IDX) | (1 << COLOR_IDX);
3153 sd->freq = FREQ_DEF;
3154 gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX) |
3155 (1 << OV7670_FREQ_IDX);
3157 sd->quality = QUALITY_DEF;
3158 if (sd->sensor == SEN_OV7640 ||
3159 sd->sensor == SEN_OV7648)
3160 gspca_dev->ctrl_dis |= (1 << AUTOBRIGHT_IDX) |
3161 (1 << CONTRAST_IDX);
3162 if (sd->sensor == SEN_OV7670)
3163 gspca_dev->ctrl_dis |= 1 << AUTOBRIGHT_IDX;
3164 /* OV8610 Frequency filter control should work but needs testing */
3165 if (sd->sensor == SEN_OV8610)
3166 gspca_dev->ctrl_dis |= 1 << FREQ_IDX;
3167 /* No controls for the OV2610/OV3610 */
3168 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
3169 gspca_dev->ctrl_dis |= 0xFF;
3173 PDEBUG(D_ERR, "OV519 Config failed");
3177 /* this function is called at probe and resume time */
3178 static int sd_init(struct gspca_dev *gspca_dev)
3180 struct sd *sd = (struct sd *) gspca_dev;
3182 /* initialize the sensor */
3183 switch (sd->sensor) {
3185 if (write_i2c_regvals(sd, norm_2610, ARRAY_SIZE(norm_2610)))
3187 /* Enable autogain, autoexpo, awb, bandfilter */
3188 if (i2c_w_mask(sd, 0x13, 0x27, 0x27) < 0)
3192 if (write_i2c_regvals(sd, norm_3620b, ARRAY_SIZE(norm_3620b)))
3194 /* Enable autogain, autoexpo, awb, bandfilter */
3195 if (i2c_w_mask(sd, 0x13, 0x27, 0x27) < 0)
3199 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
3204 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
3208 /* case SEN_OV7610: */
3209 /* case SEN_OV76BE: */
3210 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
3212 if (i2c_w_mask(sd, 0x0e, 0x00, 0x40))
3217 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
3222 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
3226 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
3230 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
3237 /* Set up the OV511/OV511+ with the given image parameters.
3239 * Do not put any sensor-specific code in here (including I2C I/O functions)
3241 static int ov511_mode_init_regs(struct sd *sd)
3243 int hsegs, vsegs, packet_size, fps, needed;
3245 struct usb_host_interface *alt;
3246 struct usb_interface *intf;
3248 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3249 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3251 PDEBUG(D_ERR, "Couldn't get altsetting");
3255 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3256 reg_w(sd, R51x_FIFO_PSIZE, packet_size >> 5);
3258 reg_w(sd, R511_CAM_UV_EN, 0x01);
3259 reg_w(sd, R511_SNAP_UV_EN, 0x01);
3260 reg_w(sd, R511_SNAP_OPTS, 0x03);
3262 /* Here I'm assuming that snapshot size == image size.
3263 * I hope that's always true. --claudio
3265 hsegs = (sd->gspca_dev.width >> 3) - 1;
3266 vsegs = (sd->gspca_dev.height >> 3) - 1;
3268 reg_w(sd, R511_CAM_PXCNT, hsegs);
3269 reg_w(sd, R511_CAM_LNCNT, vsegs);
3270 reg_w(sd, R511_CAM_PXDIV, 0x00);
3271 reg_w(sd, R511_CAM_LNDIV, 0x00);
3273 /* YUV420, low pass filter on */
3274 reg_w(sd, R511_CAM_OPTS, 0x03);
3276 /* Snapshot additions */
3277 reg_w(sd, R511_SNAP_PXCNT, hsegs);
3278 reg_w(sd, R511_SNAP_LNCNT, vsegs);
3279 reg_w(sd, R511_SNAP_PXDIV, 0x00);
3280 reg_w(sd, R511_SNAP_LNDIV, 0x00);
3282 /******** Set the framerate ********/
3284 sd->frame_rate = frame_rate;
3286 switch (sd->sensor) {
3288 /* No framerate control, doesn't like higher rates yet */
3292 /* Note once the FIXME's in mode_init_ov_sensor_regs() are fixed
3293 for more sensors we need to do this for them too */
3299 if (sd->gspca_dev.width == 320)
3305 switch (sd->frame_rate) {
3308 /* Not enough bandwidth to do 640x480 @ 30 fps */
3309 if (sd->gspca_dev.width != 640) {
3313 /* Fall through for 640x480 case */
3327 sd->clockdiv = (sd->clockdiv + 1) * 2 - 1;
3328 /* Higher then 10 does not work */
3329 if (sd->clockdiv > 10)
3335 /* No framerate control ?? */
3340 /* Check if we have enough bandwidth to disable compression */
3341 fps = (interlaced ? 60 : 30) / (sd->clockdiv + 1) + 1;
3342 needed = fps * sd->gspca_dev.width * sd->gspca_dev.height * 3 / 2;
3343 /* 1400 is a conservative estimate of the max nr of isoc packets/sec */
3344 if (needed > 1400 * packet_size) {
3345 /* Enable Y and UV quantization and compression */
3346 reg_w(sd, R511_COMP_EN, 0x07);
3347 reg_w(sd, R511_COMP_LUT_EN, 0x03);
3349 reg_w(sd, R511_COMP_EN, 0x06);
3350 reg_w(sd, R511_COMP_LUT_EN, 0x00);
3353 reg_w(sd, R51x_SYS_RESET, OV511_RESET_OMNICE);
3354 reg_w(sd, R51x_SYS_RESET, 0);
3359 /* Sets up the OV518/OV518+ with the given image parameters
3361 * OV518 needs a completely different approach, until we can figure out what
3362 * the individual registers do. Also, only 15 FPS is supported now.
3364 * Do not put any sensor-specific code in here (including I2C I/O functions)
3366 static int ov518_mode_init_regs(struct sd *sd)
3368 int hsegs, vsegs, packet_size;
3369 struct usb_host_interface *alt;
3370 struct usb_interface *intf;
3372 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3373 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3375 PDEBUG(D_ERR, "Couldn't get altsetting");
3379 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3380 ov518_reg_w32(sd, R51x_FIFO_PSIZE, packet_size & ~7, 2);
3382 /******** Set the mode ********/
3393 if (sd->bridge == BRIDGE_OV518) {
3394 /* Set 8-bit (YVYU) input format */
3395 reg_w_mask(sd, 0x20, 0x08, 0x08);
3397 /* Set 12-bit (4:2:0) output format */
3398 reg_w_mask(sd, 0x28, 0x80, 0xf0);
3399 reg_w_mask(sd, 0x38, 0x80, 0xf0);
3401 reg_w(sd, 0x28, 0x80);
3402 reg_w(sd, 0x38, 0x80);
3405 hsegs = sd->gspca_dev.width / 16;
3406 vsegs = sd->gspca_dev.height / 4;
3408 reg_w(sd, 0x29, hsegs);
3409 reg_w(sd, 0x2a, vsegs);
3411 reg_w(sd, 0x39, hsegs);
3412 reg_w(sd, 0x3a, vsegs);
3414 /* Windows driver does this here; who knows why */
3415 reg_w(sd, 0x2f, 0x80);
3417 /******** Set the framerate ********/
3420 /* Mode independent, but framerate dependent, regs */
3421 /* 0x51: Clock divider; Only works on some cams which use 2 crystals */
3422 reg_w(sd, 0x51, 0x04);
3423 reg_w(sd, 0x22, 0x18);
3424 reg_w(sd, 0x23, 0xff);
3426 if (sd->bridge == BRIDGE_OV518PLUS) {
3427 switch (sd->sensor) {
3429 if (sd->gspca_dev.width == 320) {
3430 reg_w(sd, 0x20, 0x00);
3431 reg_w(sd, 0x21, 0x19);
3433 reg_w(sd, 0x20, 0x60);
3434 reg_w(sd, 0x21, 0x1f);
3438 reg_w(sd, 0x20, 0x00);
3439 reg_w(sd, 0x21, 0x19);
3442 reg_w(sd, 0x21, 0x19);
3445 reg_w(sd, 0x71, 0x17); /* Compression-related? */
3447 /* FIXME: Sensor-specific */
3448 /* Bit 5 is what matters here. Of course, it is "reserved" */
3449 i2c_w(sd, 0x54, 0x23);
3451 reg_w(sd, 0x2f, 0x80);
3453 if (sd->bridge == BRIDGE_OV518PLUS) {
3454 reg_w(sd, 0x24, 0x94);
3455 reg_w(sd, 0x25, 0x90);
3456 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3457 ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */
3458 ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */
3459 ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */
3460 ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */
3461 ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */
3462 ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */
3463 ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */
3464 ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */
3466 reg_w(sd, 0x24, 0x9f);
3467 reg_w(sd, 0x25, 0x90);
3468 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3469 ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */
3470 ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */
3471 ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */
3472 ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */
3473 ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */
3474 ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */
3475 ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */
3476 ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */
3479 reg_w(sd, 0x2f, 0x80);
3485 /* Sets up the OV519 with the given image parameters
3487 * OV519 needs a completely different approach, until we can figure out what
3488 * the individual registers do.
3490 * Do not put any sensor-specific code in here (including I2C I/O functions)
3492 static int ov519_mode_init_regs(struct sd *sd)
3494 static const struct ov_regvals mode_init_519_ov7670[] = {
3495 { 0x5d, 0x03 }, /* Turn off suspend mode */
3496 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3497 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
3498 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3502 { 0x37, 0x00 }, /* SetUsbInit */
3503 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3504 /* Enable both fields, YUV Input, disable defect comp (why?) */
3508 { 0x17, 0x50 }, /* undocumented */
3509 { 0x37, 0x00 }, /* undocumented */
3510 { 0x40, 0xff }, /* I2C timeout counter */
3511 { 0x46, 0x00 }, /* I2C clock prescaler */
3512 { 0x59, 0x04 }, /* new from windrv 090403 */
3513 { 0xff, 0x00 }, /* undocumented */
3514 /* windows reads 0x55 at this point, why? */
3517 static const struct ov_regvals mode_init_519[] = {
3518 { 0x5d, 0x03 }, /* Turn off suspend mode */
3519 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3520 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
3521 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3525 { 0x37, 0x00 }, /* SetUsbInit */
3526 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3527 /* Enable both fields, YUV Input, disable defect comp (why?) */
3529 { 0x17, 0x50 }, /* undocumented */
3530 { 0x37, 0x00 }, /* undocumented */
3531 { 0x40, 0xff }, /* I2C timeout counter */
3532 { 0x46, 0x00 }, /* I2C clock prescaler */
3533 { 0x59, 0x04 }, /* new from windrv 090403 */
3534 { 0xff, 0x00 }, /* undocumented */
3535 /* windows reads 0x55 at this point, why? */
3538 /******** Set the mode ********/
3539 if (sd->sensor != SEN_OV7670) {
3540 if (write_regvals(sd, mode_init_519,
3541 ARRAY_SIZE(mode_init_519)))
3543 if (sd->sensor == SEN_OV7640 ||
3544 sd->sensor == SEN_OV7648) {
3545 /* Select 8-bit input mode */
3546 reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
3549 if (write_regvals(sd, mode_init_519_ov7670,
3550 ARRAY_SIZE(mode_init_519_ov7670)))
3554 reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);
3555 reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);
3556 if (sd->sensor == SEN_OV7670 &&
3557 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3558 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
3559 else if (sd->sensor == SEN_OV7648 &&
3560 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3561 reg_w(sd, OV519_R12_X_OFFSETL, 0x01);
3563 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
3564 reg_w(sd, OV519_R13_X_OFFSETH, 0x00);
3565 reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);
3566 reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);
3567 reg_w(sd, OV519_R16_DIVIDER, 0x00);
3568 reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */
3569 reg_w(sd, 0x26, 0x00); /* Undocumented */
3571 /******** Set the framerate ********/
3573 sd->frame_rate = frame_rate;
3575 /* FIXME: These are only valid at the max resolution. */
3577 switch (sd->sensor) {
3580 switch (sd->frame_rate) {
3583 reg_w(sd, 0xa4, 0x0c);
3584 reg_w(sd, 0x23, 0xff);
3587 reg_w(sd, 0xa4, 0x0c);
3588 reg_w(sd, 0x23, 0x1f);
3591 reg_w(sd, 0xa4, 0x0c);
3592 reg_w(sd, 0x23, 0x1b);
3595 reg_w(sd, 0xa4, 0x04);
3596 reg_w(sd, 0x23, 0xff);
3600 reg_w(sd, 0xa4, 0x04);
3601 reg_w(sd, 0x23, 0x1f);
3605 reg_w(sd, 0xa4, 0x04);
3606 reg_w(sd, 0x23, 0x1b);
3612 switch (sd->frame_rate) {
3613 default: /* 15 fps */
3615 reg_w(sd, 0xa4, 0x06);
3616 reg_w(sd, 0x23, 0xff);
3619 reg_w(sd, 0xa4, 0x06);
3620 reg_w(sd, 0x23, 0x1f);
3623 reg_w(sd, 0xa4, 0x06);
3624 reg_w(sd, 0x23, 0x1b);
3628 case SEN_OV7670: /* guesses, based on 7640 */
3629 PDEBUG(D_STREAM, "Setting framerate to %d fps",
3630 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
3631 reg_w(sd, 0xa4, 0x10);
3632 switch (sd->frame_rate) {
3634 reg_w(sd, 0x23, 0xff);
3637 reg_w(sd, 0x23, 0x1b);
3641 reg_w(sd, 0x23, 0xff);
3650 static int mode_init_ov_sensor_regs(struct sd *sd)
3652 struct gspca_dev *gspca_dev;
3653 int qvga, xstart, xend, ystart, yend;
3656 gspca_dev = &sd->gspca_dev;
3657 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
3659 /******** Mode (VGA/QVGA) and sensor specific regs ********/
3660 switch (sd->sensor) {
3662 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3663 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3664 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3665 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3666 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3667 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3668 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3672 xstart = (1040 - gspca_dev->width) / 2 + (0x1f << 4);
3673 ystart = (776 - gspca_dev->height) / 2;
3675 xstart = (2076 - gspca_dev->width) / 2 + (0x10 << 4);
3676 ystart = (1544 - gspca_dev->height) / 2;
3678 xend = xstart + gspca_dev->width;
3679 yend = ystart + gspca_dev->height;
3680 /* Writing to the COMH register resets the other windowing regs
3681 to their default values, so we must do this first. */
3682 i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0xf0);
3683 i2c_w_mask(sd, 0x32,
3684 (((xend >> 1) & 7) << 3) | ((xstart >> 1) & 7),
3686 i2c_w_mask(sd, 0x03,
3687 (((yend >> 1) & 3) << 2) | ((ystart >> 1) & 3),
3689 i2c_w(sd, 0x17, xstart >> 4);
3690 i2c_w(sd, 0x18, xend >> 4);
3691 i2c_w(sd, 0x19, ystart >> 3);
3692 i2c_w(sd, 0x1a, yend >> 3);
3695 /* For OV8610 qvga means qsvga */
3696 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
3697 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3698 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3699 i2c_w_mask(sd, 0x2d, 0x00, 0x40); /* from windrv 090403 */
3700 i2c_w_mask(sd, 0x28, 0x20, 0x20); /* progressive mode on */
3703 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3704 i2c_w(sd, 0x35, qvga?0x1e:0x9e);
3705 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3706 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3711 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3712 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3713 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3714 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3715 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3716 i2c_w_mask(sd, 0x67, qvga ? 0xb0 : 0x90, 0xf0);
3717 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3718 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3719 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3720 if (sd->sensor == SEN_OV76BE)
3721 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
3725 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3726 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3727 /* Setting this undocumented bit in qvga mode removes a very
3728 annoying vertical shaking of the image */
3729 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3731 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3732 /* Allow higher automatic gain (to allow higher framerates) */
3733 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3734 i2c_w_mask(sd, 0x12, 0x04, 0x04); /* AWB: 1 */
3737 /* set COM7_FMT_VGA or COM7_FMT_QVGA
3738 * do we need to set anything else?
3739 * HSTART etc are set in set_ov_sensor_window itself */
3740 i2c_w_mask(sd, OV7670_REG_COM7,
3741 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
3742 OV7670_COM7_FMT_MASK);
3743 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3744 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
3746 if (qvga) { /* QVGA from ov7670.c by
3747 * Jonathan Corbet */
3758 /* OV7670 hardware window registers are split across
3759 * multiple locations */
3760 i2c_w(sd, OV7670_REG_HSTART, xstart >> 3);
3761 i2c_w(sd, OV7670_REG_HSTOP, xend >> 3);
3762 v = i2c_r(sd, OV7670_REG_HREF);
3763 v = (v & 0xc0) | ((xend & 0x7) << 3) | (xstart & 0x07);
3764 msleep(10); /* need to sleep between read and write to
3766 i2c_w(sd, OV7670_REG_HREF, v);
3768 i2c_w(sd, OV7670_REG_VSTART, ystart >> 2);
3769 i2c_w(sd, OV7670_REG_VSTOP, yend >> 2);
3770 v = i2c_r(sd, OV7670_REG_VREF);
3771 v = (v & 0xc0) | ((yend & 0x3) << 2) | (ystart & 0x03);
3772 msleep(10); /* need to sleep between read and write to
3774 i2c_w(sd, OV7670_REG_VREF, v);
3777 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3778 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3779 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3783 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3784 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3790 /******** Clock programming ********/
3791 i2c_w(sd, 0x11, sd->clockdiv);
3796 static void sethvflip(struct sd *sd)
3798 if (sd->sensor != SEN_OV7670)
3800 if (sd->gspca_dev.streaming)
3802 i2c_w_mask(sd, OV7670_REG_MVFP,
3803 OV7670_MVFP_MIRROR * sd->hflip
3804 | OV7670_MVFP_VFLIP * sd->vflip,
3805 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
3806 if (sd->gspca_dev.streaming)
3810 static int set_ov_sensor_window(struct sd *sd)
3812 struct gspca_dev *gspca_dev;
3814 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
3817 /* mode setup is fully handled in mode_init_ov_sensor_regs for these */
3818 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610 ||
3819 sd->sensor == SEN_OV7670)
3820 return mode_init_ov_sensor_regs(sd);
3822 gspca_dev = &sd->gspca_dev;
3823 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
3824 crop = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 2;
3826 /* The different sensor ICs handle setting up of window differently.
3827 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
3828 switch (sd->sensor) {
3839 vwsbase = vwebase = 0x05;
3848 if (sd->sensor == SEN_OV66308AF && qvga)
3849 /* HDG: this fixes U and V getting swapped */
3860 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
3862 vwsbase = vwebase = 0x05;
3868 vwsbase = vwebase = 0x03;
3874 switch (sd->sensor) {
3878 if (qvga) { /* QCIF */
3883 vwscale = 1; /* The datasheet says 0;
3888 if (qvga) { /* QSVGA */
3896 default: /* SEN_OV7xx0 */
3897 if (qvga) { /* QVGA */
3906 ret = mode_init_ov_sensor_regs(sd);
3910 i2c_w(sd, 0x17, hwsbase);
3911 i2c_w(sd, 0x18, hwebase + (sd->sensor_width >> hwscale));
3912 i2c_w(sd, 0x19, vwsbase);
3913 i2c_w(sd, 0x1a, vwebase + (sd->sensor_height >> vwscale));
3918 /* -- start the camera -- */
3919 static int sd_start(struct gspca_dev *gspca_dev)
3921 struct sd *sd = (struct sd *) gspca_dev;
3924 /* Default for most bridges, allow bridge_mode_init_regs to override */
3925 sd->sensor_width = sd->gspca_dev.width;
3926 sd->sensor_height = sd->gspca_dev.height;
3928 switch (sd->bridge) {
3930 case BRIDGE_OV511PLUS:
3931 ret = ov511_mode_init_regs(sd);
3934 case BRIDGE_OV518PLUS:
3935 ret = ov518_mode_init_regs(sd);
3938 ret = ov519_mode_init_regs(sd);
3940 /* case BRIDGE_OVFX2: nothing to do */
3941 case BRIDGE_W9968CF:
3942 ret = w9968cf_mode_init_regs(sd);
3948 ret = set_ov_sensor_window(sd);
3952 setcontrast(gspca_dev);
3953 setbrightness(gspca_dev);
3954 setcolors(gspca_dev);
3956 setautobrightness(sd);
3959 /* Force clear snapshot state in case the snapshot button was
3960 pressed while we weren't streaming */
3961 sd->snapshot_needs_reset = 1;
3962 sd_reset_snapshot(gspca_dev);
3963 sd->snapshot_pressed = 0;
3965 sd->first_frame = 3;
3967 ret = ov51x_restart(sd);
3970 ov51x_led_control(sd, 1);
3973 PDEBUG(D_ERR, "camera start error:%d", ret);
3977 static void sd_stopN(struct gspca_dev *gspca_dev)
3979 struct sd *sd = (struct sd *) gspca_dev;
3982 ov51x_led_control(sd, 0);
3985 static void sd_stop0(struct gspca_dev *gspca_dev)
3987 struct sd *sd = (struct sd *) gspca_dev;
3989 if (sd->bridge == BRIDGE_W9968CF)
3993 static void ov51x_handle_button(struct gspca_dev *gspca_dev, u8 state)
3995 struct sd *sd = (struct sd *) gspca_dev;
3997 if (sd->snapshot_pressed != state) {
3999 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
4000 input_sync(gspca_dev->input_dev);
4003 sd->snapshot_needs_reset = 1;
4005 sd->snapshot_pressed = state;
4007 /* On the ov511 / ov519 we need to reset the button state
4008 multiple times, as resetting does not work as long as the
4009 button stays pressed */
4010 switch (sd->bridge) {
4012 case BRIDGE_OV511PLUS:
4015 sd->snapshot_needs_reset = 1;
4021 static void ov511_pkt_scan(struct gspca_dev *gspca_dev,
4022 u8 *in, /* isoc packet */
4023 int len) /* iso packet length */
4025 struct sd *sd = (struct sd *) gspca_dev;
4027 /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
4028 * byte non-zero. The EOF packet has image width/height in the
4029 * 10th and 11th bytes. The 9th byte is given as follows:
4032 * 6: compression enabled
4033 * 5: 422/420/400 modes
4034 * 4: 422/420/400 modes
4036 * 2: snapshot button on
4040 if (!(in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) &&
4042 ov51x_handle_button(gspca_dev, (in[8] >> 2) & 1);
4045 if ((in[9] + 1) * 8 != gspca_dev->width ||
4046 (in[10] + 1) * 8 != gspca_dev->height) {
4047 PDEBUG(D_ERR, "Invalid frame size, got: %dx%d,"
4048 " requested: %dx%d\n",
4049 (in[9] + 1) * 8, (in[10] + 1) * 8,
4050 gspca_dev->width, gspca_dev->height);
4051 gspca_dev->last_packet_type = DISCARD_PACKET;
4054 /* Add 11 byte footer to frame, might be usefull */
4055 gspca_frame_add(gspca_dev, LAST_PACKET, in, 11);
4059 gspca_frame_add(gspca_dev, FIRST_PACKET, in, 0);
4064 /* Ignore the packet number */
4067 /* intermediate packet */
4068 gspca_frame_add(gspca_dev, INTER_PACKET, in, len);
4071 static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
4072 u8 *data, /* isoc packet */
4073 int len) /* iso packet length */
4075 struct sd *sd = (struct sd *) gspca_dev;
4077 /* A false positive here is likely, until OVT gives me
4078 * the definitive SOF/EOF format */
4079 if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
4080 ov51x_handle_button(gspca_dev, (data[6] >> 1) & 1);
4081 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4082 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
4086 if (gspca_dev->last_packet_type == DISCARD_PACKET)
4089 /* Does this device use packet numbers ? */
4092 if (sd->packet_nr == data[len])
4094 /* The last few packets of the frame (which are all 0's
4095 except that they may contain part of the footer), are
4097 else if (sd->packet_nr == 0 || data[len]) {
4098 PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
4099 (int)data[len], (int)sd->packet_nr);
4100 gspca_dev->last_packet_type = DISCARD_PACKET;
4105 /* intermediate packet */
4106 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4109 static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
4110 u8 *data, /* isoc packet */
4111 int len) /* iso packet length */
4113 /* Header of ov519 is 16 bytes:
4114 * Byte Value Description
4118 * 3 0xXX 0x50 = SOF, 0x51 = EOF
4119 * 9 0xXX 0x01 initial frame without data,
4120 * 0x00 standard frame with image
4121 * 14 Lo in EOF: length of image data / 8
4125 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
4127 case 0x50: /* start of frame */
4128 /* Don't check the button state here, as the state
4129 usually (always ?) changes at EOF and checking it
4130 here leads to unnecessary snapshot state resets. */
4135 if (data[0] == 0xff || data[1] == 0xd8)
4136 gspca_frame_add(gspca_dev, FIRST_PACKET,
4139 gspca_dev->last_packet_type = DISCARD_PACKET;
4141 case 0x51: /* end of frame */
4142 ov51x_handle_button(gspca_dev, data[11] & 1);
4144 gspca_dev->last_packet_type = DISCARD_PACKET;
4145 gspca_frame_add(gspca_dev, LAST_PACKET,
4151 /* intermediate packet */
4152 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4155 static void ovfx2_pkt_scan(struct gspca_dev *gspca_dev,
4156 u8 *data, /* isoc packet */
4157 int len) /* iso packet length */
4159 struct sd *sd = (struct sd *) gspca_dev;
4160 struct gspca_frame *frame;
4162 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4164 /* A short read signals EOF */
4165 if (len < OVFX2_BULK_SIZE) {
4166 /* If the frame is short, and it is one of the first ones
4167 the sensor and bridge are still syncing, so drop it. */
4168 if (sd->first_frame) {
4170 frame = gspca_get_i_frame(gspca_dev);
4171 if (!frame || (frame->data_end - frame->data) <
4172 (sd->gspca_dev.width * sd->gspca_dev.height))
4173 gspca_dev->last_packet_type = DISCARD_PACKET;
4175 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4176 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
4180 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
4181 u8 *data, /* isoc packet */
4182 int len) /* iso packet length */
4184 struct sd *sd = (struct sd *) gspca_dev;
4186 switch (sd->bridge) {
4188 case BRIDGE_OV511PLUS:
4189 ov511_pkt_scan(gspca_dev, data, len);
4192 case BRIDGE_OV518PLUS:
4193 ov518_pkt_scan(gspca_dev, data, len);
4196 ov519_pkt_scan(gspca_dev, data, len);
4199 ovfx2_pkt_scan(gspca_dev, data, len);
4201 case BRIDGE_W9968CF:
4202 w9968cf_pkt_scan(gspca_dev, data, len);
4207 /* -- management routines -- */
4209 static void setbrightness(struct gspca_dev *gspca_dev)
4211 struct sd *sd = (struct sd *) gspca_dev;
4214 val = sd->brightness;
4215 switch (sd->sensor) {
4224 i2c_w(sd, OV7610_REG_BRT, val);
4228 /* 7620 doesn't like manual changes when in auto mode */
4229 if (!sd->autobrightness)
4230 i2c_w(sd, OV7610_REG_BRT, val);
4234 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
4235 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
4240 static void setcontrast(struct gspca_dev *gspca_dev)
4242 struct sd *sd = (struct sd *) gspca_dev;
4246 switch (sd->sensor) {
4249 i2c_w(sd, OV7610_REG_CNT, val);
4253 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
4256 static const __u8 ctab[] = {
4257 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
4260 /* Use Y gamma control instead. Bit 0 enables it. */
4261 i2c_w(sd, 0x64, ctab[val >> 5]);
4265 case SEN_OV7620AE: {
4266 static const __u8 ctab[] = {
4267 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
4268 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
4271 /* Use Y gamma control instead. Bit 0 enables it. */
4272 i2c_w(sd, 0x64, ctab[val >> 4]);
4276 /* check that this isn't just the same as ov7610 */
4277 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
4282 static void setcolors(struct gspca_dev *gspca_dev)
4284 struct sd *sd = (struct sd *) gspca_dev;
4288 switch (sd->sensor) {
4295 i2c_w(sd, OV7610_REG_SAT, val);
4299 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
4300 /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
4303 i2c_w(sd, OV7610_REG_SAT, val);
4307 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
4310 /* supported later once I work out how to do it
4311 * transparently fail now! */
4312 /* set REG_COM13 values for UV sat auto mode */
4317 static void setautobrightness(struct sd *sd)
4319 if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7648 ||
4320 sd->sensor == SEN_OV7670 ||
4321 sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
4324 i2c_w_mask(sd, 0x2d, sd->autobrightness ? 0x10 : 0x00, 0x10);
4327 static void setfreq(struct sd *sd)
4329 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
4332 if (sd->sensor == SEN_OV7670) {
4334 case 0: /* Banding filter disabled */
4335 i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_BFILT);
4338 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4340 i2c_w_mask(sd, OV7670_REG_COM11, 0x08, 0x18);
4343 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4345 i2c_w_mask(sd, OV7670_REG_COM11, 0x00, 0x18);
4347 case 3: /* Auto hz */
4348 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4350 i2c_w_mask(sd, OV7670_REG_COM11, OV7670_COM11_HZAUTO,
4356 case 0: /* Banding filter disabled */
4357 i2c_w_mask(sd, 0x2d, 0x00, 0x04);
4358 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4360 case 1: /* 50 hz (filter on and framerate adj) */
4361 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4362 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4363 /* 20 fps -> 16.667 fps */
4364 if (sd->sensor == SEN_OV6620 ||
4365 sd->sensor == SEN_OV6630 ||
4366 sd->sensor == SEN_OV66308AF)
4367 i2c_w(sd, 0x2b, 0x5e);
4369 i2c_w(sd, 0x2b, 0xac);
4371 case 2: /* 60 hz (filter on, ...) */
4372 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4373 if (sd->sensor == SEN_OV6620 ||
4374 sd->sensor == SEN_OV6630 ||
4375 sd->sensor == SEN_OV66308AF) {
4376 /* 20 fps -> 15 fps */
4377 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4378 i2c_w(sd, 0x2b, 0xa8);
4380 /* no framerate adj. */
4381 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4388 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
4390 struct sd *sd = (struct sd *) gspca_dev;
4392 sd->brightness = val;
4393 if (gspca_dev->streaming)
4394 setbrightness(gspca_dev);
4398 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
4400 struct sd *sd = (struct sd *) gspca_dev;
4402 *val = sd->brightness;
4406 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
4408 struct sd *sd = (struct sd *) gspca_dev;
4411 if (gspca_dev->streaming)
4412 setcontrast(gspca_dev);
4416 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
4418 struct sd *sd = (struct sd *) gspca_dev;
4420 *val = sd->contrast;
4424 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
4426 struct sd *sd = (struct sd *) gspca_dev;
4429 if (gspca_dev->streaming)
4430 setcolors(gspca_dev);
4434 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
4436 struct sd *sd = (struct sd *) gspca_dev;
4442 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
4444 struct sd *sd = (struct sd *) gspca_dev;
4447 if (gspca_dev->streaming)
4452 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
4454 struct sd *sd = (struct sd *) gspca_dev;
4460 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
4462 struct sd *sd = (struct sd *) gspca_dev;
4465 if (gspca_dev->streaming)
4470 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
4472 struct sd *sd = (struct sd *) gspca_dev;
4478 static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val)
4480 struct sd *sd = (struct sd *) gspca_dev;
4482 sd->autobrightness = val;
4483 if (gspca_dev->streaming)
4484 setautobrightness(sd);
4488 static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val)
4490 struct sd *sd = (struct sd *) gspca_dev;
4492 *val = sd->autobrightness;
4496 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
4498 struct sd *sd = (struct sd *) gspca_dev;
4501 if (gspca_dev->streaming) {
4503 /* Ugly but necessary */
4504 if (sd->bridge == BRIDGE_W9968CF)
4505 w9968cf_set_crop_window(sd);
4510 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
4512 struct sd *sd = (struct sd *) gspca_dev;
4518 static int sd_querymenu(struct gspca_dev *gspca_dev,
4519 struct v4l2_querymenu *menu)
4521 struct sd *sd = (struct sd *) gspca_dev;
4524 case V4L2_CID_POWER_LINE_FREQUENCY:
4525 switch (menu->index) {
4526 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
4527 strcpy((char *) menu->name, "NoFliker");
4529 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
4530 strcpy((char *) menu->name, "50 Hz");
4532 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
4533 strcpy((char *) menu->name, "60 Hz");
4536 if (sd->sensor != SEN_OV7670)
4539 strcpy((char *) menu->name, "Automatic");
4547 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4548 struct v4l2_jpegcompression *jcomp)
4550 struct sd *sd = (struct sd *) gspca_dev;
4552 if (sd->bridge != BRIDGE_W9968CF)
4555 memset(jcomp, 0, sizeof *jcomp);
4556 jcomp->quality = sd->quality;
4557 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |
4558 V4L2_JPEG_MARKER_DRI;
4562 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4563 struct v4l2_jpegcompression *jcomp)
4565 struct sd *sd = (struct sd *) gspca_dev;
4567 if (sd->bridge != BRIDGE_W9968CF)
4570 if (gspca_dev->streaming)
4573 if (jcomp->quality < QUALITY_MIN)
4574 sd->quality = QUALITY_MIN;
4575 else if (jcomp->quality > QUALITY_MAX)
4576 sd->quality = QUALITY_MAX;
4578 sd->quality = jcomp->quality;
4580 /* Return resulting jcomp params to app */
4581 sd_get_jcomp(gspca_dev, jcomp);
4586 /* sub-driver description */
4587 static const struct sd_desc sd_desc = {
4588 .name = MODULE_NAME,
4590 .nctrls = ARRAY_SIZE(sd_ctrls),
4591 .config = sd_config,
4596 .pkt_scan = sd_pkt_scan,
4597 .dq_callback = sd_reset_snapshot,
4598 .querymenu = sd_querymenu,
4599 .get_jcomp = sd_get_jcomp,
4600 .set_jcomp = sd_set_jcomp,
4606 /* -- module initialisation -- */
4607 static const __devinitdata struct usb_device_id device_table[] = {
4608 {USB_DEVICE(0x041e, 0x4003), .driver_info = BRIDGE_W9968CF },
4609 {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
4610 {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
4611 {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
4612 {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
4613 {USB_DEVICE(0x041e, 0x4064),
4614 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4615 {USB_DEVICE(0x041e, 0x4067), .driver_info = BRIDGE_OV519 },
4616 {USB_DEVICE(0x041e, 0x4068),
4617 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4618 {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
4619 {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
4620 {USB_DEVICE(0x054c, 0x0155),
4621 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4622 {USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },
4623 {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
4624 {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
4625 {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
4626 {USB_DEVICE(0x05a9, 0x2800), .driver_info = BRIDGE_OVFX2 },
4627 {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
4628 {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
4629 {USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },
4630 {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
4631 {USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },
4632 {USB_DEVICE(0x0b62, 0x0059), .driver_info = BRIDGE_OVFX2 },
4633 {USB_DEVICE(0x0e96, 0xc001), .driver_info = BRIDGE_OVFX2 },
4634 {USB_DEVICE(0x1046, 0x9967), .driver_info = BRIDGE_W9968CF },
4635 {USB_DEVICE(0x8020, 0xEF04), .driver_info = BRIDGE_OVFX2 },
4639 MODULE_DEVICE_TABLE(usb, device_table);
4641 /* -- device connect -- */
4642 static int sd_probe(struct usb_interface *intf,
4643 const struct usb_device_id *id)
4645 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
4649 static struct usb_driver sd_driver = {
4650 .name = MODULE_NAME,
4651 .id_table = device_table,
4653 .disconnect = gspca_disconnect,
4655 .suspend = gspca_suspend,
4656 .resume = gspca_resume,
4660 /* -- module insert / remove -- */
4661 static int __init sd_mod_init(void)
4664 ret = usb_register(&sd_driver);
4667 PDEBUG(D_PROBE, "registered");
4670 static void __exit sd_mod_exit(void)
4672 usb_deregister(&sd_driver);
4673 PDEBUG(D_PROBE, "deregistered");
4676 module_init(sd_mod_init);
4677 module_exit(sd_mod_exit);
4679 module_param(frame_rate, int, 0644);
4680 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");