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 /* The jpeg_hdr is used by w996Xcf only */
45 /* The CONEX_CAM define for jpeg.h needs renaming, now its used here too */
49 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
50 MODULE_DESCRIPTION("OV519 USB Camera Driver");
51 MODULE_LICENSE("GPL");
53 /* global parameters */
54 static int frame_rate;
56 /* Number of times to retry a failed I2C transaction. Increase this if you
57 * are getting "Failed to read sensor ID..." */
58 static int i2c_detect_tries = 10;
69 NCTRL /* number of controls */
72 /* ov519 device descriptor */
74 struct gspca_dev gspca_dev; /* !! must be the first item */
76 struct gspca_ctrl ctrls[NCTRL];
81 #define BRIDGE_OV511 0
82 #define BRIDGE_OV511PLUS 1
83 #define BRIDGE_OV518 2
84 #define BRIDGE_OV518PLUS 3
85 #define BRIDGE_OV519 4
86 #define BRIDGE_OVFX2 5
87 #define BRIDGE_W9968CF 6
91 #define BRIDGE_INVERT_LED 8
93 char snapshot_pressed;
94 char snapshot_needs_reset;
96 /* Determined by sensor type */
100 #define QUALITY_MIN 50
101 #define QUALITY_MAX 70
102 #define QUALITY_DEF 50
104 __u8 stopped; /* Streaming is temporarily paused */
107 __u8 frame_rate; /* current Framerate */
108 __u8 clockdiv; /* clockdiv override */
110 char sensor; /* Type of image sensor chip (SEN_*) */
111 #define SEN_UNKNOWN 0
116 #define SEN_OV66308AF 5
119 #define SEN_OV7620AE 8
121 #define SEN_OV7648 10
122 #define SEN_OV7670 11
123 #define SEN_OV76BE 12
124 #define SEN_OV8610 13
129 int sensor_reg_cache[256];
131 u8 jpeg_hdr[JPEG_HDR_SZ];
134 /* Note this is a bit of a hack, but the w9968cf driver needs the code for all
135 the ov sensors which is already present here. When we have the time we
136 really should move the sensor drivers to v4l2 sub drivers. */
139 /* V4L2 controls supported by the driver */
140 static void setbrightness(struct gspca_dev *gspca_dev);
141 static void setcontrast(struct gspca_dev *gspca_dev);
142 static void setcolors(struct gspca_dev *gspca_dev);
143 static void sethvflip(struct gspca_dev *gspca_dev);
144 static void setautobright(struct gspca_dev *gspca_dev);
145 static void setfreq(struct gspca_dev *gspca_dev);
146 static void setfreq_i(struct sd *sd);
148 static const struct ctrl sd_ctrls[] = {
151 .id = V4L2_CID_BRIGHTNESS,
152 .type = V4L2_CTRL_TYPE_INTEGER,
153 .name = "Brightness",
157 .default_value = 127,
159 .set_control = setbrightness,
163 .id = V4L2_CID_CONTRAST,
164 .type = V4L2_CTRL_TYPE_INTEGER,
169 .default_value = 127,
171 .set_control = setcontrast,
175 .id = V4L2_CID_SATURATION,
176 .type = V4L2_CTRL_TYPE_INTEGER,
181 .default_value = 127,
183 .set_control = setcolors,
185 /* The flip controls work with ov7670 only */
188 .id = V4L2_CID_HFLIP,
189 .type = V4L2_CTRL_TYPE_BOOLEAN,
196 .set_control = sethvflip,
200 .id = V4L2_CID_VFLIP,
201 .type = V4L2_CTRL_TYPE_BOOLEAN,
208 .set_control = sethvflip,
212 .id = V4L2_CID_AUTOBRIGHTNESS,
213 .type = V4L2_CTRL_TYPE_BOOLEAN,
214 .name = "Auto Brightness",
220 .set_control = setautobright,
224 .id = V4L2_CID_POWER_LINE_FREQUENCY,
225 .type = V4L2_CTRL_TYPE_MENU,
226 .name = "Light frequency filter",
228 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
232 .set_control = setfreq,
236 static const struct v4l2_pix_format ov519_vga_mode[] = {
237 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
239 .sizeimage = 320 * 240 * 3 / 8 + 590,
240 .colorspace = V4L2_COLORSPACE_JPEG,
242 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
244 .sizeimage = 640 * 480 * 3 / 8 + 590,
245 .colorspace = V4L2_COLORSPACE_JPEG,
248 static const struct v4l2_pix_format ov519_sif_mode[] = {
249 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
251 .sizeimage = 160 * 120 * 3 / 8 + 590,
252 .colorspace = V4L2_COLORSPACE_JPEG,
254 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
256 .sizeimage = 176 * 144 * 3 / 8 + 590,
257 .colorspace = V4L2_COLORSPACE_JPEG,
259 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
261 .sizeimage = 320 * 240 * 3 / 8 + 590,
262 .colorspace = V4L2_COLORSPACE_JPEG,
264 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
266 .sizeimage = 352 * 288 * 3 / 8 + 590,
267 .colorspace = V4L2_COLORSPACE_JPEG,
271 /* Note some of the sizeimage values for the ov511 / ov518 may seem
272 larger then necessary, however they need to be this big as the ov511 /
273 ov518 always fills the entire isoc frame, using 0 padding bytes when
274 it doesn't have any data. So with low framerates the amount of data
275 transfered can become quite large (libv4l will remove all the 0 padding
277 static const struct v4l2_pix_format ov518_vga_mode[] = {
278 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
280 .sizeimage = 320 * 240 * 3,
281 .colorspace = V4L2_COLORSPACE_JPEG,
283 {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
285 .sizeimage = 640 * 480 * 2,
286 .colorspace = V4L2_COLORSPACE_JPEG,
289 static const struct v4l2_pix_format ov518_sif_mode[] = {
290 {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
293 .colorspace = V4L2_COLORSPACE_JPEG,
295 {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
298 .colorspace = V4L2_COLORSPACE_JPEG,
300 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
302 .sizeimage = 320 * 240 * 3,
303 .colorspace = V4L2_COLORSPACE_JPEG,
305 {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
307 .sizeimage = 352 * 288 * 3,
308 .colorspace = V4L2_COLORSPACE_JPEG,
312 static const struct v4l2_pix_format ov511_vga_mode[] = {
313 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
315 .sizeimage = 320 * 240 * 3,
316 .colorspace = V4L2_COLORSPACE_JPEG,
318 {640, 480, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
320 .sizeimage = 640 * 480 * 2,
321 .colorspace = V4L2_COLORSPACE_JPEG,
324 static const struct v4l2_pix_format ov511_sif_mode[] = {
325 {160, 120, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
328 .colorspace = V4L2_COLORSPACE_JPEG,
330 {176, 144, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
333 .colorspace = V4L2_COLORSPACE_JPEG,
335 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
337 .sizeimage = 320 * 240 * 3,
338 .colorspace = V4L2_COLORSPACE_JPEG,
340 {352, 288, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
342 .sizeimage = 352 * 288 * 3,
343 .colorspace = V4L2_COLORSPACE_JPEG,
347 static const struct v4l2_pix_format ovfx2_vga_mode[] = {
348 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
350 .sizeimage = 320 * 240,
351 .colorspace = V4L2_COLORSPACE_SRGB,
353 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
355 .sizeimage = 640 * 480,
356 .colorspace = V4L2_COLORSPACE_SRGB,
359 static const struct v4l2_pix_format ovfx2_cif_mode[] = {
360 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
362 .sizeimage = 160 * 120,
363 .colorspace = V4L2_COLORSPACE_SRGB,
365 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
367 .sizeimage = 176 * 144,
368 .colorspace = V4L2_COLORSPACE_SRGB,
370 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
372 .sizeimage = 320 * 240,
373 .colorspace = V4L2_COLORSPACE_SRGB,
375 {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
377 .sizeimage = 352 * 288,
378 .colorspace = V4L2_COLORSPACE_SRGB,
381 static const struct v4l2_pix_format ovfx2_ov2610_mode[] = {
382 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
383 .bytesperline = 1600,
384 .sizeimage = 1600 * 1200,
385 .colorspace = V4L2_COLORSPACE_SRGB},
387 static const struct v4l2_pix_format ovfx2_ov3610_mode[] = {
388 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
390 .sizeimage = 640 * 480,
391 .colorspace = V4L2_COLORSPACE_SRGB,
393 {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
395 .sizeimage = 800 * 600,
396 .colorspace = V4L2_COLORSPACE_SRGB,
398 {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
399 .bytesperline = 1024,
400 .sizeimage = 1024 * 768,
401 .colorspace = V4L2_COLORSPACE_SRGB,
403 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
404 .bytesperline = 1600,
405 .sizeimage = 1600 * 1200,
406 .colorspace = V4L2_COLORSPACE_SRGB,
408 {2048, 1536, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
409 .bytesperline = 2048,
410 .sizeimage = 2048 * 1536,
411 .colorspace = V4L2_COLORSPACE_SRGB,
416 /* Registers common to OV511 / OV518 */
417 #define R51x_FIFO_PSIZE 0x30 /* 2 bytes wide w/ OV518(+) */
418 #define R51x_SYS_RESET 0x50
419 /* Reset type flags */
420 #define OV511_RESET_OMNICE 0x08
421 #define R51x_SYS_INIT 0x53
422 #define R51x_SYS_SNAP 0x52
423 #define R51x_SYS_CUST_ID 0x5F
424 #define R51x_COMP_LUT_BEGIN 0x80
426 /* OV511 Camera interface register numbers */
427 #define R511_CAM_DELAY 0x10
428 #define R511_CAM_EDGE 0x11
429 #define R511_CAM_PXCNT 0x12
430 #define R511_CAM_LNCNT 0x13
431 #define R511_CAM_PXDIV 0x14
432 #define R511_CAM_LNDIV 0x15
433 #define R511_CAM_UV_EN 0x16
434 #define R511_CAM_LINE_MODE 0x17
435 #define R511_CAM_OPTS 0x18
437 #define R511_SNAP_FRAME 0x19
438 #define R511_SNAP_PXCNT 0x1A
439 #define R511_SNAP_LNCNT 0x1B
440 #define R511_SNAP_PXDIV 0x1C
441 #define R511_SNAP_LNDIV 0x1D
442 #define R511_SNAP_UV_EN 0x1E
443 #define R511_SNAP_UV_EN 0x1E
444 #define R511_SNAP_OPTS 0x1F
446 #define R511_DRAM_FLOW_CTL 0x20
447 #define R511_FIFO_OPTS 0x31
448 #define R511_I2C_CTL 0x40
449 #define R511_SYS_LED_CTL 0x55 /* OV511+ only */
450 #define R511_COMP_EN 0x78
451 #define R511_COMP_LUT_EN 0x79
453 /* OV518 Camera interface register numbers */
454 #define R518_GPIO_OUT 0x56 /* OV518(+) only */
455 #define R518_GPIO_CTL 0x57 /* OV518(+) only */
457 /* OV519 Camera interface register numbers */
458 #define OV519_R10_H_SIZE 0x10
459 #define OV519_R11_V_SIZE 0x11
460 #define OV519_R12_X_OFFSETL 0x12
461 #define OV519_R13_X_OFFSETH 0x13
462 #define OV519_R14_Y_OFFSETL 0x14
463 #define OV519_R15_Y_OFFSETH 0x15
464 #define OV519_R16_DIVIDER 0x16
465 #define OV519_R20_DFR 0x20
466 #define OV519_R25_FORMAT 0x25
468 /* OV519 System Controller register numbers */
469 #define OV519_SYS_RESET1 0x51
470 #define OV519_SYS_EN_CLK1 0x54
472 #define OV519_GPIO_DATA_OUT0 0x71
473 #define OV519_GPIO_IO_CTRL0 0x72
475 #define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
478 * The FX2 chip does not give us a zero length read at end of frame.
479 * It does, however, give a short read at the end of a frame, if
480 * necessary, rather than run two frames together.
482 * By choosing the right bulk transfer size, we are guaranteed to always
483 * get a short read for the last read of each frame. Frame sizes are
484 * always a composite number (width * height, or a multiple) so if we
485 * choose a prime number, we are guaranteed that the last read of a
486 * frame will be short.
488 * But it isn't that easy: the 2.6 kernel requires a multiple of 4KB,
489 * otherwise EOVERFLOW "babbling" errors occur. I have not been able
490 * to figure out why. [PMiller]
492 * The constant (13 * 4096) is the largest "prime enough" number less than 64KB.
494 * It isn't enough to know the number of bytes per frame, in case we
495 * have data dropouts or buffer overruns (even though the FX2 double
496 * buffers, there are some pretty strict real time constraints for
497 * isochronous transfer for larger frame sizes).
499 #define OVFX2_BULK_SIZE (13 * 4096)
502 #define R51x_I2C_W_SID 0x41
503 #define R51x_I2C_SADDR_3 0x42
504 #define R51x_I2C_SADDR_2 0x43
505 #define R51x_I2C_R_SID 0x44
506 #define R51x_I2C_DATA 0x45
507 #define R518_I2C_CTL 0x47 /* OV518(+) only */
508 #define OVFX2_I2C_ADDR 0x00
511 #define OV7xx0_SID 0x42
512 #define OV_HIRES_SID 0x60 /* OV9xxx / OV2xxx / OV3xxx */
513 #define OV8xx0_SID 0xa0
514 #define OV6xx0_SID 0xc0
516 /* OV7610 registers */
517 #define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
518 #define OV7610_REG_BLUE 0x01 /* blue channel balance */
519 #define OV7610_REG_RED 0x02 /* red channel balance */
520 #define OV7610_REG_SAT 0x03 /* saturation */
521 #define OV8610_REG_HUE 0x04 /* 04 reserved */
522 #define OV7610_REG_CNT 0x05 /* Y contrast */
523 #define OV7610_REG_BRT 0x06 /* Y brightness */
524 #define OV7610_REG_COM_C 0x14 /* misc common regs */
525 #define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
526 #define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
527 #define OV7610_REG_COM_I 0x29 /* misc settings */
529 /* OV7670 registers */
530 #define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
531 #define OV7670_REG_BLUE 0x01 /* blue gain */
532 #define OV7670_REG_RED 0x02 /* red gain */
533 #define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
534 #define OV7670_REG_COM1 0x04 /* Control 1 */
535 #define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
536 #define OV7670_REG_COM3 0x0c /* Control 3 */
537 #define OV7670_REG_COM4 0x0d /* Control 4 */
538 #define OV7670_REG_COM5 0x0e /* All "reserved" */
539 #define OV7670_REG_COM6 0x0f /* Control 6 */
540 #define OV7670_REG_AECH 0x10 /* More bits of AEC value */
541 #define OV7670_REG_CLKRC 0x11 /* Clock control */
542 #define OV7670_REG_COM7 0x12 /* Control 7 */
543 #define OV7670_COM7_FMT_VGA 0x00
544 #define OV7670_COM7_YUV 0x00 /* YUV */
545 #define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
546 #define OV7670_COM7_FMT_MASK 0x38
547 #define OV7670_COM7_RESET 0x80 /* Register reset */
548 #define OV7670_REG_COM8 0x13 /* Control 8 */
549 #define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
550 #define OV7670_COM8_AWB 0x02 /* White balance enable */
551 #define OV7670_COM8_AGC 0x04 /* Auto gain enable */
552 #define OV7670_COM8_BFILT 0x20 /* Band filter enable */
553 #define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
554 #define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
555 #define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
556 #define OV7670_REG_COM10 0x15 /* Control 10 */
557 #define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
558 #define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
559 #define OV7670_REG_VSTART 0x19 /* Vert start high bits */
560 #define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
561 #define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
562 #define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
563 #define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
564 #define OV7670_REG_AEW 0x24 /* AGC upper limit */
565 #define OV7670_REG_AEB 0x25 /* AGC lower limit */
566 #define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
567 #define OV7670_REG_HREF 0x32 /* HREF pieces */
568 #define OV7670_REG_TSLB 0x3a /* lots of stuff */
569 #define OV7670_REG_COM11 0x3b /* Control 11 */
570 #define OV7670_COM11_EXP 0x02
571 #define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
572 #define OV7670_REG_COM12 0x3c /* Control 12 */
573 #define OV7670_REG_COM13 0x3d /* Control 13 */
574 #define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
575 #define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
576 #define OV7670_REG_COM14 0x3e /* Control 14 */
577 #define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
578 #define OV7670_REG_COM15 0x40 /* Control 15 */
579 #define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
580 #define OV7670_REG_COM16 0x41 /* Control 16 */
581 #define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
582 #define OV7670_REG_BRIGHT 0x55 /* Brightness */
583 #define OV7670_REG_CONTRAS 0x56 /* Contrast control */
584 #define OV7670_REG_GFIX 0x69 /* Fix gain control */
585 #define OV7670_REG_RGB444 0x8c /* RGB 444 control */
586 #define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
587 #define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
588 #define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
589 #define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
590 #define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
591 #define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
592 #define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
593 #define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
594 #define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
600 struct ov_i2c_regvals {
605 /* Settings for OV2610 camera chip */
606 static const struct ov_i2c_regvals norm_2610[] = {
607 { 0x12, 0x80 }, /* reset */
610 static const struct ov_i2c_regvals norm_3620b[] = {
612 * From the datasheet: "Note that after writing to register COMH
613 * (0x12) to change the sensor mode, registers related to the
614 * sensor’s cropping window will be reset back to their default
617 * "wait 4096 external clock ... to make sure the sensor is
618 * stable and ready to access registers" i.e. 160us at 24MHz
621 { 0x12, 0x80 }, /* COMH reset */
622 { 0x12, 0x00 }, /* QXGA, master */
625 * 11 CLKRC "Clock Rate Control"
626 * [7] internal frequency doublers: on
627 * [6] video port mode: master
628 * [5:0] clock divider: 1
633 * 13 COMI "Common Control I"
634 * = 192 (0xC0) 11000000
635 * COMI[7] "AEC speed selection"
636 * = 1 (0x01) 1....... "Faster AEC correction"
637 * COMI[6] "AEC speed step selection"
638 * = 1 (0x01) .1...... "Big steps, fast"
639 * COMI[5] "Banding filter on off"
640 * = 0 (0x00) ..0..... "Off"
641 * COMI[4] "Banding filter option"
642 * = 0 (0x00) ...0.... "Main clock is 48 MHz and
645 * = 0 (0x00) ....0...
646 * COMI[2] "AGC auto manual control selection"
647 * = 0 (0x00) .....0.. "Manual"
648 * COMI[1] "AWB auto manual control selection"
649 * = 0 (0x00) ......0. "Manual"
650 * COMI[0] "Exposure control"
651 * = 0 (0x00) .......0 "Manual"
656 * 09 COMC "Common Control C"
657 * = 8 (0x08) 00001000
658 * COMC[7:5] "Reserved"
659 * = 0 (0x00) 000.....
660 * COMC[4] "Sleep Mode Enable"
661 * = 0 (0x00) ...0.... "Normal mode"
662 * COMC[3:2] "Sensor sampling reset timing selection"
663 * = 2 (0x02) ....10.. "Longer reset time"
664 * COMC[1:0] "Output drive current select"
665 * = 0 (0x00) ......00 "Weakest"
670 * 0C COMD "Common Control D"
671 * = 8 (0x08) 00001000
673 * = 0 (0x00) 0.......
674 * COMD[6] "Swap MSB and LSB at the output port"
675 * = 0 (0x00) .0...... "False"
676 * COMD[5:3] "Reserved"
677 * = 1 (0x01) ..001...
678 * COMD[2] "Output Average On Off"
679 * = 0 (0x00) .....0.. "Output Normal"
680 * COMD[1] "Sensor precharge voltage selection"
681 * = 0 (0x00) ......0. "Selects internal
682 * reference precharge
684 * COMD[0] "Snapshot option"
685 * = 0 (0x00) .......0 "Enable live video output
686 * after snapshot sequence"
691 * 0D COME "Common Control E"
692 * = 161 (0xA1) 10100001
693 * COME[7] "Output average option"
694 * = 1 (0x01) 1....... "Output average of 4 pixels"
695 * COME[6] "Anti-blooming control"
696 * = 0 (0x00) .0...... "Off"
697 * COME[5:3] "Reserved"
698 * = 4 (0x04) ..100...
699 * COME[2] "Clock output power down pin status"
700 * = 0 (0x00) .....0.. "Tri-state data output pin
702 * COME[1] "Data output pin status selection at power down"
703 * = 0 (0x00) ......0. "Tri-state VSYNC, PCLK,
704 * HREF, and CHSYNC pins on
706 * COME[0] "Auto zero circuit select"
707 * = 1 (0x01) .......1 "On"
712 * 0E COMF "Common Control F"
713 * = 112 (0x70) 01110000
714 * COMF[7] "System clock selection"
715 * = 0 (0x00) 0....... "Use 24 MHz system clock"
716 * COMF[6:4] "Reserved"
717 * = 7 (0x07) .111....
718 * COMF[3] "Manual auto negative offset canceling selection"
719 * = 0 (0x00) ....0... "Auto detect negative
720 * offset and cancel it"
721 * COMF[2:0] "Reserved"
722 * = 0 (0x00) .....000
727 * 0F COMG "Common Control G"
728 * = 66 (0x42) 01000010
729 * COMG[7] "Optical black output selection"
730 * = 0 (0x00) 0....... "Disable"
731 * COMG[6] "Black level calibrate selection"
732 * = 1 (0x01) .1...... "Use optical black pixels
734 * COMG[5:4] "Reserved"
735 * = 0 (0x00) ..00....
736 * COMG[3] "Channel offset adjustment"
737 * = 0 (0x00) ....0... "Disable offset adjustment"
738 * COMG[2] "ADC black level calibration option"
739 * = 0 (0x00) .....0.. "Use B/G line and G/R
740 * line to calibrate each
741 * channel's black level"
743 * = 1 (0x01) ......1.
744 * COMG[0] "ADC black level calibration enable"
745 * = 0 (0x00) .......0 "Disable"
750 * 14 COMJ "Common Control J"
751 * = 198 (0xC6) 11000110
752 * COMJ[7:6] "AGC gain ceiling"
753 * = 3 (0x03) 11...... "8x"
754 * COMJ[5:4] "Reserved"
755 * = 0 (0x00) ..00....
756 * COMJ[3] "Auto banding filter"
757 * = 0 (0x00) ....0... "Banding filter is always
758 * on off depending on
760 * COMJ[2] "VSYNC drop option"
761 * = 1 (0x01) .....1.. "SYNC is dropped if frame
763 * COMJ[1] "Frame data drop"
764 * = 1 (0x01) ......1. "Drop frame data if
765 * exposure is not within
766 * tolerance. In AEC mode,
767 * data is normally dropped
768 * when data is out of
771 * = 0 (0x00) .......0
776 * 15 COMK "Common Control K"
777 * = 2 (0x02) 00000010
778 * COMK[7] "CHSYNC pin output swap"
779 * = 0 (0x00) 0....... "CHSYNC"
780 * COMK[6] "HREF pin output swap"
781 * = 0 (0x00) .0...... "HREF"
782 * COMK[5] "PCLK output selection"
783 * = 0 (0x00) ..0..... "PCLK always output"
784 * COMK[4] "PCLK edge selection"
785 * = 0 (0x00) ...0.... "Data valid on falling edge"
786 * COMK[3] "HREF output polarity"
787 * = 0 (0x00) ....0... "positive"
789 * = 0 (0x00) .....0..
790 * COMK[1] "VSYNC polarity"
791 * = 1 (0x01) ......1. "negative"
792 * COMK[0] "HSYNC polarity"
793 * = 0 (0x00) .......0 "positive"
798 * 33 CHLF "Current Control"
799 * = 9 (0x09) 00001001
800 * CHLF[7:6] "Sensor current control"
801 * = 0 (0x00) 00......
802 * CHLF[5] "Sensor current range control"
803 * = 0 (0x00) ..0..... "normal range"
804 * CHLF[4] "Sensor current"
805 * = 0 (0x00) ...0.... "normal current"
806 * CHLF[3] "Sensor buffer current control"
807 * = 1 (0x01) ....1... "half current"
808 * CHLF[2] "Column buffer current control"
809 * = 0 (0x00) .....0.. "normal current"
810 * CHLF[1] "Analog DSP current control"
811 * = 0 (0x00) ......0. "normal current"
812 * CHLF[1] "ADC current control"
813 * = 0 (0x00) ......0. "normal current"
818 * 34 VBLM "Blooming Control"
819 * = 80 (0x50) 01010000
820 * VBLM[7] "Hard soft reset switch"
821 * = 0 (0x00) 0....... "Hard reset"
822 * VBLM[6:4] "Blooming voltage selection"
823 * = 5 (0x05) .101....
824 * VBLM[3:0] "Sensor current control"
825 * = 0 (0x00) ....0000
830 * 36 VCHG "Sensor Precharge Voltage Control"
831 * = 0 (0x00) 00000000
833 * = 0 (0x00) 0.......
834 * VCHG[6:4] "Sensor precharge voltage control"
835 * = 0 (0x00) .000....
836 * VCHG[3:0] "Sensor array common reference"
837 * = 0 (0x00) ....0000
842 * 37 ADC "ADC Reference Control"
843 * = 4 (0x04) 00000100
844 * ADC[7:4] "Reserved"
845 * = 0 (0x00) 0000....
846 * ADC[3] "ADC input signal range"
847 * = 0 (0x00) ....0... "Input signal 1.0x"
848 * ADC[2:0] "ADC range control"
849 * = 4 (0x04) .....100
854 * 38 ACOM "Analog Common Ground"
855 * = 82 (0x52) 01010010
856 * ACOM[7] "Analog gain control"
857 * = 0 (0x00) 0....... "Gain 1x"
858 * ACOM[6] "Analog black level calibration"
859 * = 1 (0x01) .1...... "On"
860 * ACOM[5:0] "Reserved"
861 * = 18 (0x12) ..010010
866 * 3A FREFA "Internal Reference Adjustment"
867 * = 0 (0x00) 00000000
869 * = 0 (0x00) 00000000
874 * 3C FVOPT "Internal Reference Adjustment"
875 * = 31 (0x1F) 00011111
877 * = 31 (0x1F) 00011111
882 * 44 Undocumented = 0 (0x00) 00000000
883 * 44[7:0] "It's a secret"
884 * = 0 (0x00) 00000000
889 * 40 Undocumented = 0 (0x00) 00000000
890 * 40[7:0] "It's a secret"
891 * = 0 (0x00) 00000000
896 * 41 Undocumented = 0 (0x00) 00000000
897 * 41[7:0] "It's a secret"
898 * = 0 (0x00) 00000000
903 * 42 Undocumented = 0 (0x00) 00000000
904 * 42[7:0] "It's a secret"
905 * = 0 (0x00) 00000000
910 * 43 Undocumented = 0 (0x00) 00000000
911 * 43[7:0] "It's a secret"
912 * = 0 (0x00) 00000000
917 * 45 Undocumented = 128 (0x80) 10000000
918 * 45[7:0] "It's a secret"
919 * = 128 (0x80) 10000000
924 * 48 Undocumented = 192 (0xC0) 11000000
925 * 48[7:0] "It's a secret"
926 * = 192 (0xC0) 11000000
931 * 49 Undocumented = 25 (0x19) 00011001
932 * 49[7:0] "It's a secret"
933 * = 25 (0x19) 00011001
938 * 4B Undocumented = 128 (0x80) 10000000
939 * 4B[7:0] "It's a secret"
940 * = 128 (0x80) 10000000
945 * 4D Undocumented = 196 (0xC4) 11000100
946 * 4D[7:0] "It's a secret"
947 * = 196 (0xC4) 11000100
952 * 35 VREF "Reference Voltage Control"
953 * = 76 (0x4C) 01001100
954 * VREF[7:5] "Column high reference control"
955 * = 2 (0x02) 010..... "higher voltage"
956 * VREF[4:2] "Column low reference control"
957 * = 3 (0x03) ...011.. "Highest voltage"
958 * VREF[1:0] "Reserved"
959 * = 0 (0x00) ......00
964 * 3D Undocumented = 0 (0x00) 00000000
965 * 3D[7:0] "It's a secret"
966 * = 0 (0x00) 00000000
971 * 3E Undocumented = 0 (0x00) 00000000
972 * 3E[7:0] "It's a secret"
973 * = 0 (0x00) 00000000
978 * 3B FREFB "Internal Reference Adjustment"
979 * = 24 (0x18) 00011000
981 * = 24 (0x18) 00011000
986 * 33 CHLF "Current Control"
987 * = 25 (0x19) 00011001
988 * CHLF[7:6] "Sensor current control"
989 * = 0 (0x00) 00......
990 * CHLF[5] "Sensor current range control"
991 * = 0 (0x00) ..0..... "normal range"
992 * CHLF[4] "Sensor current"
993 * = 1 (0x01) ...1.... "double current"
994 * CHLF[3] "Sensor buffer current control"
995 * = 1 (0x01) ....1... "half current"
996 * CHLF[2] "Column buffer current control"
997 * = 0 (0x00) .....0.. "normal current"
998 * CHLF[1] "Analog DSP current control"
999 * = 0 (0x00) ......0. "normal current"
1000 * CHLF[1] "ADC current control"
1001 * = 0 (0x00) ......0. "normal current"
1006 * 34 VBLM "Blooming Control"
1007 * = 90 (0x5A) 01011010
1008 * VBLM[7] "Hard soft reset switch"
1009 * = 0 (0x00) 0....... "Hard reset"
1010 * VBLM[6:4] "Blooming voltage selection"
1011 * = 5 (0x05) .101....
1012 * VBLM[3:0] "Sensor current control"
1013 * = 10 (0x0A) ....1010
1018 * 3B FREFB "Internal Reference Adjustment"
1019 * = 0 (0x00) 00000000
1020 * FREFB[7:0] "Range"
1021 * = 0 (0x00) 00000000
1026 * 33 CHLF "Current Control"
1027 * = 9 (0x09) 00001001
1028 * CHLF[7:6] "Sensor current control"
1029 * = 0 (0x00) 00......
1030 * CHLF[5] "Sensor current range control"
1031 * = 0 (0x00) ..0..... "normal range"
1032 * CHLF[4] "Sensor current"
1033 * = 0 (0x00) ...0.... "normal current"
1034 * CHLF[3] "Sensor buffer current control"
1035 * = 1 (0x01) ....1... "half current"
1036 * CHLF[2] "Column buffer current control"
1037 * = 0 (0x00) .....0.. "normal current"
1038 * CHLF[1] "Analog DSP current control"
1039 * = 0 (0x00) ......0. "normal current"
1040 * CHLF[1] "ADC current control"
1041 * = 0 (0x00) ......0. "normal current"
1046 * 34 VBLM "Blooming Control"
1047 * = 80 (0x50) 01010000
1048 * VBLM[7] "Hard soft reset switch"
1049 * = 0 (0x00) 0....... "Hard reset"
1050 * VBLM[6:4] "Blooming voltage selection"
1051 * = 5 (0x05) .101....
1052 * VBLM[3:0] "Sensor current control"
1053 * = 0 (0x00) ....0000
1058 * 12 COMH "Common Control H"
1059 * = 64 (0x40) 01000000
1061 * = 0 (0x00) 0....... "No-op"
1062 * COMH[6:4] "Resolution selection"
1063 * = 4 (0x04) .100.... "XGA"
1064 * COMH[3] "Master slave selection"
1065 * = 0 (0x00) ....0... "Master mode"
1066 * COMH[2] "Internal B/R channel option"
1067 * = 0 (0x00) .....0.. "B/R use same channel"
1068 * COMH[1] "Color bar test pattern"
1069 * = 0 (0x00) ......0. "Off"
1070 * COMH[0] "Reserved"
1071 * = 0 (0x00) .......0
1076 * 17 HREFST "Horizontal window start"
1077 * = 31 (0x1F) 00011111
1078 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1079 * = 31 (0x1F) 00011111
1084 * 18 HREFEND "Horizontal window end"
1085 * = 95 (0x5F) 01011111
1086 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1087 * = 95 (0x5F) 01011111
1092 * 19 VSTRT "Vertical window start"
1093 * = 0 (0x00) 00000000
1094 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1095 * = 0 (0x00) 00000000
1100 * 1A VEND "Vertical window end"
1101 * = 96 (0x60) 01100000
1102 * VEND[7:0] "Vertical Window End, 8 MSBs"
1103 * = 96 (0x60) 01100000
1108 * 32 COMM "Common Control M"
1109 * = 18 (0x12) 00010010
1110 * COMM[7:6] "Pixel clock divide option"
1111 * = 0 (0x00) 00...... "/1"
1112 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1113 * = 2 (0x02) ..010...
1114 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1115 * = 2 (0x02) .....010
1120 * 03 COMA "Common Control A"
1121 * = 74 (0x4A) 01001010
1122 * COMA[7:4] "AWB Update Threshold"
1123 * = 4 (0x04) 0100....
1124 * COMA[3:2] "Vertical window end line control 2 LSBs"
1125 * = 2 (0x02) ....10..
1126 * COMA[1:0] "Vertical window start line control 2 LSBs"
1127 * = 2 (0x02) ......10
1132 * 11 CLKRC "Clock Rate Control"
1133 * = 128 (0x80) 10000000
1134 * CLKRC[7] "Internal frequency doublers on off seclection"
1135 * = 1 (0x01) 1....... "On"
1136 * CLKRC[6] "Digital video master slave selection"
1137 * = 0 (0x00) .0...... "Master mode, sensor
1139 * CLKRC[5:0] "Clock divider { CLK = PCLK/(1+CLKRC[5:0]) }"
1140 * = 0 (0x00) ..000000
1145 * 12 COMH "Common Control H"
1146 * = 0 (0x00) 00000000
1148 * = 0 (0x00) 0....... "No-op"
1149 * COMH[6:4] "Resolution selection"
1150 * = 0 (0x00) .000.... "QXGA"
1151 * COMH[3] "Master slave selection"
1152 * = 0 (0x00) ....0... "Master mode"
1153 * COMH[2] "Internal B/R channel option"
1154 * = 0 (0x00) .....0.. "B/R use same channel"
1155 * COMH[1] "Color bar test pattern"
1156 * = 0 (0x00) ......0. "Off"
1157 * COMH[0] "Reserved"
1158 * = 0 (0x00) .......0
1163 * 12 COMH "Common Control H"
1164 * = 64 (0x40) 01000000
1166 * = 0 (0x00) 0....... "No-op"
1167 * COMH[6:4] "Resolution selection"
1168 * = 4 (0x04) .100.... "XGA"
1169 * COMH[3] "Master slave selection"
1170 * = 0 (0x00) ....0... "Master mode"
1171 * COMH[2] "Internal B/R channel option"
1172 * = 0 (0x00) .....0.. "B/R use same channel"
1173 * COMH[1] "Color bar test pattern"
1174 * = 0 (0x00) ......0. "Off"
1175 * COMH[0] "Reserved"
1176 * = 0 (0x00) .......0
1181 * 17 HREFST "Horizontal window start"
1182 * = 31 (0x1F) 00011111
1183 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1184 * = 31 (0x1F) 00011111
1189 * 18 HREFEND "Horizontal window end"
1190 * = 95 (0x5F) 01011111
1191 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1192 * = 95 (0x5F) 01011111
1197 * 19 VSTRT "Vertical window start"
1198 * = 0 (0x00) 00000000
1199 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1200 * = 0 (0x00) 00000000
1205 * 1A VEND "Vertical window end"
1206 * = 96 (0x60) 01100000
1207 * VEND[7:0] "Vertical Window End, 8 MSBs"
1208 * = 96 (0x60) 01100000
1213 * 32 COMM "Common Control M"
1214 * = 18 (0x12) 00010010
1215 * COMM[7:6] "Pixel clock divide option"
1216 * = 0 (0x00) 00...... "/1"
1217 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1218 * = 2 (0x02) ..010...
1219 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1220 * = 2 (0x02) .....010
1225 * 03 COMA "Common Control A"
1226 * = 74 (0x4A) 01001010
1227 * COMA[7:4] "AWB Update Threshold"
1228 * = 4 (0x04) 0100....
1229 * COMA[3:2] "Vertical window end line control 2 LSBs"
1230 * = 2 (0x02) ....10..
1231 * COMA[1:0] "Vertical window start line control 2 LSBs"
1232 * = 2 (0x02) ......10
1237 * 02 RED "Red Gain Control"
1238 * = 175 (0xAF) 10101111
1240 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1242 * = 47 (0x2F) .0101111
1247 * 2D ADDVSL "VSYNC Pulse Width"
1248 * = 210 (0xD2) 11010010
1249 * ADDVSL[7:0] "VSYNC pulse width, LSB"
1250 * = 210 (0xD2) 11010010
1255 * 00 GAIN = 24 (0x18) 00011000
1256 * GAIN[7:6] "Reserved"
1257 * = 0 (0x00) 00......
1259 * = 0 (0x00) ..0..... "False"
1261 * = 1 (0x01) ...1.... "True"
1263 * = 8 (0x08) ....1000
1268 * 01 BLUE "Blue Gain Control"
1269 * = 240 (0xF0) 11110000
1271 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1273 * = 112 (0x70) .1110000
1278 * 10 AEC "Automatic Exposure Control"
1279 * = 10 (0x0A) 00001010
1280 * AEC[7:0] "Automatic Exposure Control, 8 MSBs"
1281 * = 10 (0x0A) 00001010
1293 static const struct ov_i2c_regvals norm_6x20[] = {
1294 { 0x12, 0x80 }, /* reset */
1297 { 0x05, 0x7f }, /* For when autoadjust is off */
1299 /* The ratio of 0x0c and 0x0d controls the white point */
1302 { 0x0f, 0x15 }, /* COMS */
1303 { 0x10, 0x75 }, /* AEC Exposure time */
1304 { 0x12, 0x24 }, /* Enable AGC */
1306 /* 0x16: 0x06 helps frame stability with moving objects */
1308 /* { 0x20, 0x30 }, * Aperture correction enable */
1309 { 0x26, 0xb2 }, /* BLC enable */
1310 /* 0x28: 0x05 Selects RGB format if RGB on */
1312 { 0x2a, 0x04 }, /* Disable framerate adjust */
1313 /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
1315 { 0x33, 0xa0 }, /* Color Processing Parameter */
1316 { 0x34, 0xd2 }, /* Max A/D range */
1320 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1321 { 0x3c, 0x3c }, /* Change AEC mode */
1322 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1325 /* These next two registers (0x4a, 0x4b) are undocumented.
1326 * They control the color balance */
1329 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1332 /* Do 50-53 have any effect? */
1333 /* Toggle 0x12[2] off and on here? */
1336 static const struct ov_i2c_regvals norm_6x30[] = {
1337 { 0x12, 0x80 }, /* Reset */
1338 { 0x00, 0x1f }, /* Gain */
1339 { 0x01, 0x99 }, /* Blue gain */
1340 { 0x02, 0x7c }, /* Red gain */
1341 { 0x03, 0xc0 }, /* Saturation */
1342 { 0x05, 0x0a }, /* Contrast */
1343 { 0x06, 0x95 }, /* Brightness */
1344 { 0x07, 0x2d }, /* Sharpness */
1347 { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
1350 { 0x11, 0x00 }, /* Pixel clock = fastest */
1351 { 0x12, 0x24 }, /* Enable AGC and AWB */
1366 { 0x23, 0xc0 }, /* Crystal circuit power level */
1367 { 0x25, 0x9a }, /* Increase AEC black ratio */
1368 { 0x26, 0xb2 }, /* BLC enable */
1372 { 0x2a, 0x84 }, /* 60 Hz power */
1373 { 0x2b, 0xa8 }, /* 60 Hz power */
1375 { 0x2d, 0x95 }, /* Enable auto-brightness */
1389 { 0x40, 0x00 }, /* White bal */
1390 { 0x41, 0x00 }, /* White bal */
1392 { 0x43, 0x3f }, /* White bal */
1402 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1404 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1406 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1411 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1413 { 0x5b, 0x0f }, /* AWB chrominance levels */
1417 { 0x12, 0x20 }, /* Toggle AWB */
1421 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
1423 * Register 0x0f in the 7610 has the following effects:
1425 * 0x85 (AEC method 1): Best overall, good contrast range
1426 * 0x45 (AEC method 2): Very overexposed
1427 * 0xa5 (spec sheet default): Ok, but the black level is
1428 * shifted resulting in loss of contrast
1429 * 0x05 (old driver setting): very overexposed, too much
1432 static const struct ov_i2c_regvals norm_7610[] = {
1439 { 0x28, 0x24 }, /* 0c */
1440 { 0x0f, 0x85 }, /* lg's setting */
1462 static const struct ov_i2c_regvals norm_7620[] = {
1463 { 0x12, 0x80 }, /* reset */
1464 { 0x00, 0x00 }, /* gain */
1465 { 0x01, 0x80 }, /* blue gain */
1466 { 0x02, 0x80 }, /* red gain */
1467 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
1490 { 0x28, 0x22 }, /* Was 0x20, bit1 enables a 2x gain which we need */
1529 /* 7640 and 7648. The defaults should be OK for most registers. */
1530 static const struct ov_i2c_regvals norm_7640[] = {
1535 /* 7670. Defaults taken from OmniVision provided data,
1536 * as provided by Jonathan Corbet of OLPC */
1537 static const struct ov_i2c_regvals norm_7670[] = {
1538 { OV7670_REG_COM7, OV7670_COM7_RESET },
1539 { OV7670_REG_TSLB, 0x04 }, /* OV */
1540 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
1541 { OV7670_REG_CLKRC, 0x01 },
1543 * Set the hardware window. These values from OV don't entirely
1544 * make sense - hstop is less than hstart. But they work...
1546 { OV7670_REG_HSTART, 0x13 },
1547 { OV7670_REG_HSTOP, 0x01 },
1548 { OV7670_REG_HREF, 0xb6 },
1549 { OV7670_REG_VSTART, 0x02 },
1550 { OV7670_REG_VSTOP, 0x7a },
1551 { OV7670_REG_VREF, 0x0a },
1553 { OV7670_REG_COM3, 0x00 },
1554 { OV7670_REG_COM14, 0x00 },
1555 /* Mystery scaling numbers */
1561 /* { OV7670_REG_COM10, 0x0 }, */
1563 /* Gamma curve values */
1581 /* AGC and AEC parameters. Note we start by disabling those features,
1582 then turn them only after tweaking the values. */
1583 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1584 | OV7670_COM8_AECSTEP
1585 | OV7670_COM8_BFILT },
1586 { OV7670_REG_GAIN, 0x00 },
1587 { OV7670_REG_AECH, 0x00 },
1588 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
1589 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
1590 { OV7670_REG_BD50MAX, 0x05 },
1591 { OV7670_REG_BD60MAX, 0x07 },
1592 { OV7670_REG_AEW, 0x95 },
1593 { OV7670_REG_AEB, 0x33 },
1594 { OV7670_REG_VPT, 0xe3 },
1595 { OV7670_REG_HAECC1, 0x78 },
1596 { OV7670_REG_HAECC2, 0x68 },
1597 { 0xa1, 0x03 }, /* magic */
1598 { OV7670_REG_HAECC3, 0xd8 },
1599 { OV7670_REG_HAECC4, 0xd8 },
1600 { OV7670_REG_HAECC5, 0xf0 },
1601 { OV7670_REG_HAECC6, 0x90 },
1602 { OV7670_REG_HAECC7, 0x94 },
1603 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1604 | OV7670_COM8_AECSTEP
1607 | OV7670_COM8_AEC },
1609 /* Almost all of these are magic "reserved" values. */
1610 { OV7670_REG_COM5, 0x61 },
1611 { OV7670_REG_COM6, 0x4b },
1613 { OV7670_REG_MVFP, 0x07 },
1622 { OV7670_REG_COM12, 0x78 },
1625 { OV7670_REG_GFIX, 0x00 },
1641 /* More reserved magic, some of which tweaks white balance */
1658 /* "9e for advance AWB" */
1660 { OV7670_REG_BLUE, 0x40 },
1661 { OV7670_REG_RED, 0x60 },
1662 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1663 | OV7670_COM8_AECSTEP
1667 | OV7670_COM8_AWB },
1669 /* Matrix coefficients */
1678 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
1679 { OV7670_REG_EDGE, 0x00 },
1684 { OV7670_REG_COM13, OV7670_COM13_GAMMA
1685 | OV7670_COM13_UVSAT
1689 { OV7670_REG_COM16, 0x38 },
1693 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
1706 /* Extra-weird stuff. Some sort of multiplexor register */
1732 static const struct ov_i2c_regvals norm_8610[] = {
1739 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
1740 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
1749 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
1751 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
1752 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
1753 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
1756 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
1757 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
1758 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
1759 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
1765 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
1767 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
1769 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
1771 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
1772 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
1773 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
1774 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
1776 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
1777 * maybe thats wrong */
1781 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
1785 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
1786 * deleting bit7 colors the first images red */
1787 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
1788 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
1794 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
1796 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
1801 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
1803 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
1804 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
1811 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
1817 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
1820 static unsigned char ov7670_abs_to_sm(unsigned char v)
1824 return (128 - v) | 0x80;
1827 /* Write a OV519 register */
1828 static int reg_w(struct sd *sd, __u16 index, __u16 value)
1832 switch (sd->bridge) {
1834 case BRIDGE_OV511PLUS:
1840 case BRIDGE_W9968CF:
1841 ret = usb_control_msg(sd->gspca_dev.dev,
1842 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1844 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1845 value, index, NULL, 0, 500);
1851 sd->gspca_dev.usb_buf[0] = value;
1852 ret = usb_control_msg(sd->gspca_dev.dev,
1853 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1855 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1857 sd->gspca_dev.usb_buf, 1, 500);
1860 err("Write reg 0x%04x -> [0x%02x] failed",
1865 PDEBUG(D_USBO, "Write reg 0x%04x -> [0x%02x]", value, index);
1869 /* Read from a OV519 register, note not valid for the w9968cf!! */
1870 /* returns: negative is error, pos or zero is data */
1871 static int reg_r(struct sd *sd, __u16 index)
1876 switch (sd->bridge) {
1878 case BRIDGE_OV511PLUS:
1888 ret = usb_control_msg(sd->gspca_dev.dev,
1889 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1891 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1892 0, index, sd->gspca_dev.usb_buf, 1, 500);
1895 ret = sd->gspca_dev.usb_buf[0];
1896 PDEBUG(D_USBI, "Read reg [0x%02X] -> 0x%04X", index, ret);
1898 err("Read reg [0x%02x] failed", index);
1903 /* Read 8 values from a OV519 register */
1904 static int reg_r8(struct sd *sd,
1909 ret = usb_control_msg(sd->gspca_dev.dev,
1910 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1912 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1913 0, index, sd->gspca_dev.usb_buf, 8, 500);
1916 ret = sd->gspca_dev.usb_buf[0];
1918 err("Read reg 8 [0x%02x] failed", index);
1924 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1925 * the same position as 1's in "mask" are cleared and set to "value". Bits
1926 * that are in the same position as 0's in "mask" are preserved, regardless
1927 * of their respective state in "value".
1929 static int reg_w_mask(struct sd *sd,
1938 value &= mask; /* Enforce mask on value */
1939 ret = reg_r(sd, index);
1943 oldval = ret & ~mask; /* Clear the masked bits */
1944 value |= oldval; /* Set the desired bits */
1946 return reg_w(sd, index, value);
1950 * Writes multiple (n) byte value to a single register. Only valid with certain
1951 * registers (0x30 and 0xc4 - 0xce).
1953 static int ov518_reg_w32(struct sd *sd, __u16 index, u32 value, int n)
1957 *((__le32 *) sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
1959 ret = usb_control_msg(sd->gspca_dev.dev,
1960 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1962 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1964 sd->gspca_dev.usb_buf, n, 500);
1966 err("Write reg32 [%02x] %08x failed", index, value);
1973 static int ov511_i2c_w(struct sd *sd, __u8 reg, __u8 value)
1977 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
1979 /* Three byte write cycle */
1980 for (retries = 6; ; ) {
1981 /* Select camera register */
1982 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
1986 /* Write "value" to I2C data port of OV511 */
1987 rc = reg_w(sd, R51x_I2C_DATA, value);
1991 /* Initiate 3-byte write cycle */
1992 rc = reg_w(sd, R511_I2C_CTL, 0x01);
1997 rc = reg_r(sd, R511_I2C_CTL);
1998 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2003 if ((rc & 2) == 0) /* Ack? */
2005 if (--retries < 0) {
2006 PDEBUG(D_USBO, "i2c write retries exhausted");
2014 static int ov511_i2c_r(struct sd *sd, __u8 reg)
2016 int rc, value, retries;
2018 /* Two byte write cycle */
2019 for (retries = 6; ; ) {
2020 /* Select camera register */
2021 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
2025 /* Initiate 2-byte write cycle */
2026 rc = reg_w(sd, R511_I2C_CTL, 0x03);
2031 rc = reg_r(sd, R511_I2C_CTL);
2032 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2037 if ((rc & 2) == 0) /* Ack? */
2041 reg_w(sd, R511_I2C_CTL, 0x10);
2043 if (--retries < 0) {
2044 PDEBUG(D_USBI, "i2c write retries exhausted");
2049 /* Two byte read cycle */
2050 for (retries = 6; ; ) {
2051 /* Initiate 2-byte read cycle */
2052 rc = reg_w(sd, R511_I2C_CTL, 0x05);
2057 rc = reg_r(sd, R511_I2C_CTL);
2058 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
2063 if ((rc & 2) == 0) /* Ack? */
2067 rc = reg_w(sd, R511_I2C_CTL, 0x10);
2071 if (--retries < 0) {
2072 PDEBUG(D_USBI, "i2c read retries exhausted");
2077 value = reg_r(sd, R51x_I2C_DATA);
2079 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
2081 /* This is needed to make i2c_w() work */
2082 rc = reg_w(sd, R511_I2C_CTL, 0x05);
2090 * The OV518 I2C I/O procedure is different, hence, this function.
2091 * This is normally only called from i2c_w(). Note that this function
2092 * always succeeds regardless of whether the sensor is present and working.
2094 static int ov518_i2c_w(struct sd *sd,
2100 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
2102 /* Select camera register */
2103 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
2107 /* Write "value" to I2C data port of OV511 */
2108 rc = reg_w(sd, R51x_I2C_DATA, value);
2112 /* Initiate 3-byte write cycle */
2113 rc = reg_w(sd, R518_I2C_CTL, 0x01);
2117 /* wait for write complete */
2119 return reg_r8(sd, R518_I2C_CTL);
2123 * returns: negative is error, pos or zero is data
2125 * The OV518 I2C I/O procedure is different, hence, this function.
2126 * This is normally only called from i2c_r(). Note that this function
2127 * always succeeds regardless of whether the sensor is present and working.
2129 static int ov518_i2c_r(struct sd *sd, __u8 reg)
2133 /* Select camera register */
2134 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
2138 /* Initiate 2-byte write cycle */
2139 rc = reg_w(sd, R518_I2C_CTL, 0x03);
2143 /* Initiate 2-byte read cycle */
2144 rc = reg_w(sd, R518_I2C_CTL, 0x05);
2147 value = reg_r(sd, R51x_I2C_DATA);
2148 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
2152 static int ovfx2_i2c_w(struct sd *sd, __u8 reg, __u8 value)
2156 ret = usb_control_msg(sd->gspca_dev.dev,
2157 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2159 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2160 (__u16)value, (__u16)reg, NULL, 0, 500);
2163 err("i2c 0x%02x -> [0x%02x] failed", value, reg);
2167 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
2171 static int ovfx2_i2c_r(struct sd *sd, __u8 reg)
2175 ret = usb_control_msg(sd->gspca_dev.dev,
2176 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
2178 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2179 0, (__u16)reg, sd->gspca_dev.usb_buf, 1, 500);
2182 ret = sd->gspca_dev.usb_buf[0];
2183 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, ret);
2185 err("i2c read [0x%02x] failed", reg);
2190 static int i2c_w(struct sd *sd, __u8 reg, __u8 value)
2194 if (sd->sensor_reg_cache[reg] == value)
2197 switch (sd->bridge) {
2199 case BRIDGE_OV511PLUS:
2200 ret = ov511_i2c_w(sd, reg, value);
2203 case BRIDGE_OV518PLUS:
2205 ret = ov518_i2c_w(sd, reg, value);
2208 ret = ovfx2_i2c_w(sd, reg, value);
2210 case BRIDGE_W9968CF:
2211 ret = w9968cf_i2c_w(sd, reg, value);
2216 /* Up on sensor reset empty the register cache */
2217 if (reg == 0x12 && (value & 0x80))
2218 memset(sd->sensor_reg_cache, -1,
2219 sizeof(sd->sensor_reg_cache));
2221 sd->sensor_reg_cache[reg] = value;
2227 static int i2c_r(struct sd *sd, __u8 reg)
2231 if (sd->sensor_reg_cache[reg] != -1)
2232 return sd->sensor_reg_cache[reg];
2234 switch (sd->bridge) {
2236 case BRIDGE_OV511PLUS:
2237 ret = ov511_i2c_r(sd, reg);
2240 case BRIDGE_OV518PLUS:
2242 ret = ov518_i2c_r(sd, reg);
2245 ret = ovfx2_i2c_r(sd, reg);
2247 case BRIDGE_W9968CF:
2248 ret = w9968cf_i2c_r(sd, reg);
2253 sd->sensor_reg_cache[reg] = ret;
2258 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
2259 * the same position as 1's in "mask" are cleared and set to "value". Bits
2260 * that are in the same position as 0's in "mask" are preserved, regardless
2261 * of their respective state in "value".
2263 static int i2c_w_mask(struct sd *sd,
2271 value &= mask; /* Enforce mask on value */
2272 rc = i2c_r(sd, reg);
2275 oldval = rc & ~mask; /* Clear the masked bits */
2276 value |= oldval; /* Set the desired bits */
2277 return i2c_w(sd, reg, value);
2280 /* Temporarily stops OV511 from functioning. Must do this before changing
2281 * registers while the camera is streaming */
2282 static inline int ov51x_stop(struct sd *sd)
2284 PDEBUG(D_STREAM, "stopping");
2286 switch (sd->bridge) {
2288 case BRIDGE_OV511PLUS:
2289 return reg_w(sd, R51x_SYS_RESET, 0x3d);
2291 case BRIDGE_OV518PLUS:
2292 return reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
2294 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
2296 return reg_w_mask(sd, 0x0f, 0x00, 0x02);
2297 case BRIDGE_W9968CF:
2298 return reg_w(sd, 0x3c, 0x0a05); /* stop USB transfer */
2304 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
2305 * actually stopped (for performance). */
2306 static inline int ov51x_restart(struct sd *sd)
2310 PDEBUG(D_STREAM, "restarting");
2315 /* Reinitialize the stream */
2316 switch (sd->bridge) {
2318 case BRIDGE_OV511PLUS:
2319 return reg_w(sd, R51x_SYS_RESET, 0x00);
2321 case BRIDGE_OV518PLUS:
2322 rc = reg_w(sd, 0x2f, 0x80);
2325 return reg_w(sd, R51x_SYS_RESET, 0x00);
2327 return reg_w(sd, OV519_SYS_RESET1, 0x00);
2329 return reg_w_mask(sd, 0x0f, 0x02, 0x02);
2330 case BRIDGE_W9968CF:
2331 return reg_w(sd, 0x3c, 0x8a05); /* USB FIFO enable */
2337 static int ov51x_set_slave_ids(struct sd *sd, __u8 slave);
2339 /* This does an initial reset of an OmniVision sensor and ensures that I2C
2340 * is synchronized. Returns <0 on failure.
2342 static int init_ov_sensor(struct sd *sd, __u8 slave)
2346 if (ov51x_set_slave_ids(sd, slave) < 0)
2349 /* Reset the sensor */
2350 if (i2c_w(sd, 0x12, 0x80) < 0)
2353 /* Wait for it to initialize */
2356 for (i = 0; i < i2c_detect_tries; i++) {
2357 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
2358 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
2359 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
2363 /* Reset the sensor */
2364 if (i2c_w(sd, 0x12, 0x80) < 0)
2366 /* Wait for it to initialize */
2368 /* Dummy read to sync I2C */
2369 if (i2c_r(sd, 0x00) < 0)
2375 /* Set the read and write slave IDs. The "slave" argument is the write slave,
2376 * and the read slave will be set to (slave + 1).
2377 * This should not be called from outside the i2c I/O functions.
2378 * Sets I2C read and write slave IDs. Returns <0 for error
2380 static int ov51x_set_slave_ids(struct sd *sd,
2385 switch (sd->bridge) {
2387 return reg_w(sd, OVFX2_I2C_ADDR, slave);
2388 case BRIDGE_W9968CF:
2389 sd->sensor_addr = slave;
2393 rc = reg_w(sd, R51x_I2C_W_SID, slave);
2396 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
2399 static int write_regvals(struct sd *sd,
2400 const struct ov_regvals *regvals,
2406 rc = reg_w(sd, regvals->reg, regvals->val);
2414 static int write_i2c_regvals(struct sd *sd,
2415 const struct ov_i2c_regvals *regvals,
2421 rc = i2c_w(sd, regvals->reg, regvals->val);
2429 /****************************************************************************
2431 * OV511 and sensor configuration
2433 ***************************************************************************/
2435 /* This initializes the OV2x10 / OV3610 / OV3620 */
2436 static int ov_hires_configure(struct sd *sd)
2440 if (sd->bridge != BRIDGE_OVFX2) {
2441 err("error hires sensors only supported with ovfx2");
2445 PDEBUG(D_PROBE, "starting ov hires configuration");
2447 /* Detect sensor (sub)type */
2448 high = i2c_r(sd, 0x0a);
2449 low = i2c_r(sd, 0x0b);
2450 /* info("%x, %x", high, low); */
2451 if (high == 0x96 && low == 0x40) {
2452 PDEBUG(D_PROBE, "Sensor is an OV2610");
2453 sd->sensor = SEN_OV2610;
2454 } else if (high == 0x36 && (low & 0x0f) == 0x00) {
2455 PDEBUG(D_PROBE, "Sensor is an OV3610");
2456 sd->sensor = SEN_OV3610;
2458 err("Error unknown sensor type: 0x%02x%02x",
2463 /* Set sensor-specific vars */
2468 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
2469 * the same register settings as the OV8610, since they are very similar.
2471 static int ov8xx0_configure(struct sd *sd)
2475 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
2477 /* Detect sensor (sub)type */
2478 rc = i2c_r(sd, OV7610_REG_COM_I);
2480 PDEBUG(D_ERR, "Error detecting sensor type");
2483 if ((rc & 3) == 1) {
2484 sd->sensor = SEN_OV8610;
2486 err("Unknown image sensor version: %d", rc & 3);
2490 /* Set sensor-specific vars */
2494 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
2495 * the same register settings as the OV7610, since they are very similar.
2497 static int ov7xx0_configure(struct sd *sd)
2502 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
2504 /* Detect sensor (sub)type */
2505 rc = i2c_r(sd, OV7610_REG_COM_I);
2508 * it appears to be wrongly detected as a 7610 by default */
2510 PDEBUG(D_ERR, "Error detecting sensor type");
2513 if ((rc & 3) == 3) {
2514 /* quick hack to make OV7670s work */
2515 high = i2c_r(sd, 0x0a);
2516 low = i2c_r(sd, 0x0b);
2517 /* info("%x, %x", high, low); */
2518 if (high == 0x76 && low == 0x73) {
2519 PDEBUG(D_PROBE, "Sensor is an OV7670");
2520 sd->sensor = SEN_OV7670;
2522 PDEBUG(D_PROBE, "Sensor is an OV7610");
2523 sd->sensor = SEN_OV7610;
2525 } else if ((rc & 3) == 1) {
2526 /* I don't know what's different about the 76BE yet. */
2527 if (i2c_r(sd, 0x15) & 1) {
2528 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
2529 sd->sensor = SEN_OV7620AE;
2531 PDEBUG(D_PROBE, "Sensor is an OV76BE");
2532 sd->sensor = SEN_OV76BE;
2534 } else if ((rc & 3) == 0) {
2535 /* try to read product id registers */
2536 high = i2c_r(sd, 0x0a);
2538 PDEBUG(D_ERR, "Error detecting camera chip PID");
2541 low = i2c_r(sd, 0x0b);
2543 PDEBUG(D_ERR, "Error detecting camera chip VER");
2549 err("Sensor is an OV7630/OV7635");
2550 err("7630 is not supported by this driver");
2553 PDEBUG(D_PROBE, "Sensor is an OV7645");
2554 sd->sensor = SEN_OV7640; /* FIXME */
2557 PDEBUG(D_PROBE, "Sensor is an OV7645B");
2558 sd->sensor = SEN_OV7640; /* FIXME */
2561 PDEBUG(D_PROBE, "Sensor is an OV7648");
2562 sd->sensor = SEN_OV7648;
2565 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
2569 PDEBUG(D_PROBE, "Sensor is an OV7620");
2570 sd->sensor = SEN_OV7620;
2573 err("Unknown image sensor version: %d", rc & 3);
2577 /* Set sensor-specific vars */
2581 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
2582 static int ov6xx0_configure(struct sd *sd)
2585 PDEBUG(D_PROBE, "starting OV6xx0 configuration");
2587 /* Detect sensor (sub)type */
2588 rc = i2c_r(sd, OV7610_REG_COM_I);
2590 PDEBUG(D_ERR, "Error detecting sensor type");
2594 /* Ugh. The first two bits are the version bits, but
2595 * the entire register value must be used. I guess OVT
2596 * underestimated how many variants they would make. */
2599 sd->sensor = SEN_OV6630;
2600 warn("WARNING: Sensor is an OV66308. Your camera may have");
2601 warn("been misdetected in previous driver versions.");
2604 sd->sensor = SEN_OV6620;
2605 PDEBUG(D_PROBE, "Sensor is an OV6620");
2608 sd->sensor = SEN_OV6630;
2609 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
2612 sd->sensor = SEN_OV66308AF;
2613 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
2616 sd->sensor = SEN_OV6630;
2617 warn("WARNING: Sensor is an OV66307. Your camera may have");
2618 warn("been misdetected in previous driver versions.");
2621 err("FATAL: Unknown sensor version: 0x%02x", rc);
2625 /* Set sensor-specific vars */
2631 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
2632 static void ov51x_led_control(struct sd *sd, int on)
2637 switch (sd->bridge) {
2638 /* OV511 has no LED control */
2639 case BRIDGE_OV511PLUS:
2640 reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
2643 case BRIDGE_OV518PLUS:
2644 reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
2647 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
2652 static void sd_reset_snapshot(struct gspca_dev *gspca_dev)
2654 struct sd *sd = (struct sd *) gspca_dev;
2656 if (!sd->snapshot_needs_reset)
2659 /* Note it is important that we clear sd->snapshot_needs_reset,
2660 before actually clearing the snapshot state in the bridge
2661 otherwise we might race with the pkt_scan interrupt handler */
2662 sd->snapshot_needs_reset = 0;
2664 switch (sd->bridge) {
2666 case BRIDGE_OV511PLUS:
2667 reg_w(sd, R51x_SYS_SNAP, 0x02);
2668 reg_w(sd, R51x_SYS_SNAP, 0x00);
2671 case BRIDGE_OV518PLUS:
2672 reg_w(sd, R51x_SYS_SNAP, 0x02); /* Reset */
2673 reg_w(sd, R51x_SYS_SNAP, 0x01); /* Enable */
2676 reg_w(sd, R51x_SYS_RESET, 0x40);
2677 reg_w(sd, R51x_SYS_RESET, 0x00);
2682 static int ov51x_upload_quan_tables(struct sd *sd)
2684 const unsigned char yQuanTable511[] = {
2685 0, 1, 1, 2, 2, 3, 3, 4,
2686 1, 1, 1, 2, 2, 3, 4, 4,
2687 1, 1, 2, 2, 3, 4, 4, 4,
2688 2, 2, 2, 3, 4, 4, 4, 4,
2689 2, 2, 3, 4, 4, 5, 5, 5,
2690 3, 3, 4, 4, 5, 5, 5, 5,
2691 3, 4, 4, 4, 5, 5, 5, 5,
2692 4, 4, 4, 4, 5, 5, 5, 5
2695 const unsigned char uvQuanTable511[] = {
2696 0, 2, 2, 3, 4, 4, 4, 4,
2697 2, 2, 2, 4, 4, 4, 4, 4,
2698 2, 2, 3, 4, 4, 4, 4, 4,
2699 3, 4, 4, 4, 4, 4, 4, 4,
2700 4, 4, 4, 4, 4, 4, 4, 4,
2701 4, 4, 4, 4, 4, 4, 4, 4,
2702 4, 4, 4, 4, 4, 4, 4, 4,
2703 4, 4, 4, 4, 4, 4, 4, 4
2706 /* OV518 quantization tables are 8x4 (instead of 8x8) */
2707 const unsigned char yQuanTable518[] = {
2708 5, 4, 5, 6, 6, 7, 7, 7,
2709 5, 5, 5, 5, 6, 7, 7, 7,
2710 6, 6, 6, 6, 7, 7, 7, 8,
2711 7, 7, 6, 7, 7, 7, 8, 8
2714 const unsigned char uvQuanTable518[] = {
2715 6, 6, 6, 7, 7, 7, 7, 7,
2716 6, 6, 6, 7, 7, 7, 7, 7,
2717 6, 6, 6, 7, 7, 7, 7, 8,
2718 7, 7, 7, 7, 7, 7, 8, 8
2721 const unsigned char *pYTable, *pUVTable;
2722 unsigned char val0, val1;
2723 int i, size, rc, reg = R51x_COMP_LUT_BEGIN;
2725 PDEBUG(D_PROBE, "Uploading quantization tables");
2727 if (sd->bridge == BRIDGE_OV511 || sd->bridge == BRIDGE_OV511PLUS) {
2728 pYTable = yQuanTable511;
2729 pUVTable = uvQuanTable511;
2732 pYTable = yQuanTable518;
2733 pUVTable = uvQuanTable518;
2737 for (i = 0; i < size; i++) {
2743 rc = reg_w(sd, reg, val0);
2752 rc = reg_w(sd, reg + size, val0);
2762 /* This initializes the OV511/OV511+ and the sensor */
2763 static int ov511_configure(struct gspca_dev *gspca_dev)
2765 struct sd *sd = (struct sd *) gspca_dev;
2768 /* For 511 and 511+ */
2769 const struct ov_regvals init_511[] = {
2770 { R51x_SYS_RESET, 0x7f },
2771 { R51x_SYS_INIT, 0x01 },
2772 { R51x_SYS_RESET, 0x7f },
2773 { R51x_SYS_INIT, 0x01 },
2774 { R51x_SYS_RESET, 0x3f },
2775 { R51x_SYS_INIT, 0x01 },
2776 { R51x_SYS_RESET, 0x3d },
2779 const struct ov_regvals norm_511[] = {
2780 { R511_DRAM_FLOW_CTL, 0x01 },
2781 { R51x_SYS_SNAP, 0x00 },
2782 { R51x_SYS_SNAP, 0x02 },
2783 { R51x_SYS_SNAP, 0x00 },
2784 { R511_FIFO_OPTS, 0x1f },
2785 { R511_COMP_EN, 0x00 },
2786 { R511_COMP_LUT_EN, 0x03 },
2789 const struct ov_regvals norm_511_p[] = {
2790 { R511_DRAM_FLOW_CTL, 0xff },
2791 { R51x_SYS_SNAP, 0x00 },
2792 { R51x_SYS_SNAP, 0x02 },
2793 { R51x_SYS_SNAP, 0x00 },
2794 { R511_FIFO_OPTS, 0xff },
2795 { R511_COMP_EN, 0x00 },
2796 { R511_COMP_LUT_EN, 0x03 },
2799 const struct ov_regvals compress_511[] = {
2810 PDEBUG(D_PROBE, "Device custom id %x", reg_r(sd, R51x_SYS_CUST_ID));
2812 rc = write_regvals(sd, init_511, ARRAY_SIZE(init_511));
2816 switch (sd->bridge) {
2818 rc = write_regvals(sd, norm_511, ARRAY_SIZE(norm_511));
2822 case BRIDGE_OV511PLUS:
2823 rc = write_regvals(sd, norm_511_p, ARRAY_SIZE(norm_511_p));
2829 /* Init compression */
2830 rc = write_regvals(sd, compress_511, ARRAY_SIZE(compress_511));
2834 rc = ov51x_upload_quan_tables(sd);
2836 PDEBUG(D_ERR, "Error uploading quantization tables");
2843 /* This initializes the OV518/OV518+ and the sensor */
2844 static int ov518_configure(struct gspca_dev *gspca_dev)
2846 struct sd *sd = (struct sd *) gspca_dev;
2849 /* For 518 and 518+ */
2850 const struct ov_regvals init_518[] = {
2851 { R51x_SYS_RESET, 0x40 },
2852 { R51x_SYS_INIT, 0xe1 },
2853 { R51x_SYS_RESET, 0x3e },
2854 { R51x_SYS_INIT, 0xe1 },
2855 { R51x_SYS_RESET, 0x00 },
2856 { R51x_SYS_INIT, 0xe1 },
2861 const struct ov_regvals norm_518[] = {
2862 { R51x_SYS_SNAP, 0x02 }, /* Reset */
2863 { R51x_SYS_SNAP, 0x01 }, /* Enable */
2874 const struct ov_regvals norm_518_p[] = {
2875 { R51x_SYS_SNAP, 0x02 }, /* Reset */
2876 { R51x_SYS_SNAP, 0x01 }, /* Enable */
2893 /* First 5 bits of custom ID reg are a revision ID on OV518 */
2894 PDEBUG(D_PROBE, "Device revision %d",
2895 0x1F & reg_r(sd, R51x_SYS_CUST_ID));
2897 rc = write_regvals(sd, init_518, ARRAY_SIZE(init_518));
2901 /* Set LED GPIO pin to output mode */
2902 rc = reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
2906 switch (sd->bridge) {
2908 rc = write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
2912 case BRIDGE_OV518PLUS:
2913 rc = write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
2919 rc = ov51x_upload_quan_tables(sd);
2921 PDEBUG(D_ERR, "Error uploading quantization tables");
2925 rc = reg_w(sd, 0x2f, 0x80);
2932 static int ov519_configure(struct sd *sd)
2934 static const struct ov_regvals init_519[] = {
2935 { 0x5a, 0x6d }, /* EnableSystem */
2937 { 0x54, 0xff }, /* set bit2 to enable jpeg */
2941 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
2942 * detection will fail. This deserves further investigation. */
2943 { OV519_GPIO_IO_CTRL0, 0xee },
2944 { 0x51, 0x0f }, /* SetUsbInit */
2947 /* windows reads 0x55 at this point*/
2950 return write_regvals(sd, init_519, ARRAY_SIZE(init_519));
2953 static int ovfx2_configure(struct sd *sd)
2955 static const struct ov_regvals init_fx2[] = {
2967 return write_regvals(sd, init_fx2, ARRAY_SIZE(init_fx2));
2970 /* this function is called at probe time */
2971 static int sd_config(struct gspca_dev *gspca_dev,
2972 const struct usb_device_id *id)
2974 struct sd *sd = (struct sd *) gspca_dev;
2975 struct cam *cam = &gspca_dev->cam;
2978 sd->bridge = id->driver_info & BRIDGE_MASK;
2979 sd->invert_led = id->driver_info & BRIDGE_INVERT_LED;
2981 switch (sd->bridge) {
2983 case BRIDGE_OV511PLUS:
2984 ret = ov511_configure(gspca_dev);
2987 case BRIDGE_OV518PLUS:
2988 ret = ov518_configure(gspca_dev);
2991 ret = ov519_configure(sd);
2994 ret = ovfx2_configure(sd);
2995 cam->bulk_size = OVFX2_BULK_SIZE;
2996 cam->bulk_nurbs = MAX_NURBS;
2999 case BRIDGE_W9968CF:
3000 ret = w9968cf_configure(sd);
3001 cam->reverse_alts = 1;
3008 ov51x_led_control(sd, 0); /* turn LED off */
3010 /* The OV519 must be more aggressive about sensor detection since
3011 * I2C write will never fail if the sensor is not present. We have
3012 * to try to initialize the sensor to detect its presence */
3015 if (init_ov_sensor(sd, OV7xx0_SID) >= 0) {
3016 if (ov7xx0_configure(sd) < 0) {
3017 PDEBUG(D_ERR, "Failed to configure OV7xx0");
3021 } else if (init_ov_sensor(sd, OV6xx0_SID) >= 0) {
3022 if (ov6xx0_configure(sd) < 0) {
3023 PDEBUG(D_ERR, "Failed to configure OV6xx0");
3027 } else if (init_ov_sensor(sd, OV8xx0_SID) >= 0) {
3028 if (ov8xx0_configure(sd) < 0) {
3029 PDEBUG(D_ERR, "Failed to configure OV8xx0");
3032 /* Test for 3xxx / 2xxx */
3033 } else if (init_ov_sensor(sd, OV_HIRES_SID) >= 0) {
3034 if (ov_hires_configure(sd) < 0) {
3035 PDEBUG(D_ERR, "Failed to configure high res OV");
3039 err("Can't determine sensor slave IDs");
3043 switch (sd->bridge) {
3045 case BRIDGE_OV511PLUS:
3047 cam->cam_mode = ov511_vga_mode;
3048 cam->nmodes = ARRAY_SIZE(ov511_vga_mode);
3050 cam->cam_mode = ov511_sif_mode;
3051 cam->nmodes = ARRAY_SIZE(ov511_sif_mode);
3055 case BRIDGE_OV518PLUS:
3057 cam->cam_mode = ov518_vga_mode;
3058 cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
3060 cam->cam_mode = ov518_sif_mode;
3061 cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
3066 cam->cam_mode = ov519_vga_mode;
3067 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3069 cam->cam_mode = ov519_sif_mode;
3070 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3074 if (sd->sensor == SEN_OV2610) {
3075 cam->cam_mode = ovfx2_ov2610_mode;
3076 cam->nmodes = ARRAY_SIZE(ovfx2_ov2610_mode);
3077 } else if (sd->sensor == SEN_OV3610) {
3078 cam->cam_mode = ovfx2_ov3610_mode;
3079 cam->nmodes = ARRAY_SIZE(ovfx2_ov3610_mode);
3080 } else if (!sd->sif) {
3081 cam->cam_mode = ov519_vga_mode;
3082 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3084 cam->cam_mode = ov519_sif_mode;
3085 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3088 case BRIDGE_W9968CF:
3089 cam->cam_mode = w9968cf_vga_mode;
3090 cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode);
3094 /* w9968cf needs initialisation once the sensor is known */
3095 if (w9968cf_init(sd) < 0)
3099 gspca_dev->cam.ctrls = sd->ctrls;
3100 if (sd->sensor == SEN_OV7670)
3101 gspca_dev->ctrl_dis = 1 << COLORS;
3103 gspca_dev->ctrl_dis = (1 << HFLIP) | (1 << VFLIP);
3104 sd->quality = QUALITY_DEF;
3105 if (sd->sensor == SEN_OV7640 ||
3106 sd->sensor == SEN_OV7648)
3107 gspca_dev->ctrl_dis |= (1 << AUTOBRIGHT) | (1 << CONTRAST);
3108 if (sd->sensor == SEN_OV7670)
3109 gspca_dev->ctrl_dis |= 1 << AUTOBRIGHT;
3110 /* OV8610 Frequency filter control should work but needs testing */
3111 if (sd->sensor == SEN_OV8610)
3112 gspca_dev->ctrl_dis |= 1 << FREQ;
3113 /* No controls for the OV2610/OV3610 */
3114 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
3115 gspca_dev->ctrl_dis |= (1 << NCTRL) - 1;
3119 PDEBUG(D_ERR, "OV519 Config failed");
3123 /* this function is called at probe and resume time */
3124 static int sd_init(struct gspca_dev *gspca_dev)
3126 struct sd *sd = (struct sd *) gspca_dev;
3128 /* initialize the sensor */
3129 switch (sd->sensor) {
3131 if (write_i2c_regvals(sd, norm_2610, ARRAY_SIZE(norm_2610)))
3133 /* Enable autogain, autoexpo, awb, bandfilter */
3134 if (i2c_w_mask(sd, 0x13, 0x27, 0x27) < 0)
3138 if (write_i2c_regvals(sd, norm_3620b, ARRAY_SIZE(norm_3620b)))
3140 /* Enable autogain, autoexpo, awb, bandfilter */
3141 if (i2c_w_mask(sd, 0x13, 0x27, 0x27) < 0)
3145 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
3150 sd->ctrls[CONTRAST].def = 200;
3151 /* The default is too low for the ov6630 */
3152 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
3156 /* case SEN_OV7610: */
3157 /* case SEN_OV76BE: */
3158 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
3160 if (i2c_w_mask(sd, 0x0e, 0x00, 0x40))
3165 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
3170 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
3174 sd->ctrls[FREQ].max = 3; /* auto */
3175 sd->ctrls[FREQ].def = 3;
3176 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
3180 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
3187 /* Set up the OV511/OV511+ with the given image parameters.
3189 * Do not put any sensor-specific code in here (including I2C I/O functions)
3191 static int ov511_mode_init_regs(struct sd *sd)
3193 int hsegs, vsegs, packet_size, fps, needed;
3195 struct usb_host_interface *alt;
3196 struct usb_interface *intf;
3198 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3199 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3201 err("Couldn't get altsetting");
3205 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3206 reg_w(sd, R51x_FIFO_PSIZE, packet_size >> 5);
3208 reg_w(sd, R511_CAM_UV_EN, 0x01);
3209 reg_w(sd, R511_SNAP_UV_EN, 0x01);
3210 reg_w(sd, R511_SNAP_OPTS, 0x03);
3212 /* Here I'm assuming that snapshot size == image size.
3213 * I hope that's always true. --claudio
3215 hsegs = (sd->gspca_dev.width >> 3) - 1;
3216 vsegs = (sd->gspca_dev.height >> 3) - 1;
3218 reg_w(sd, R511_CAM_PXCNT, hsegs);
3219 reg_w(sd, R511_CAM_LNCNT, vsegs);
3220 reg_w(sd, R511_CAM_PXDIV, 0x00);
3221 reg_w(sd, R511_CAM_LNDIV, 0x00);
3223 /* YUV420, low pass filter on */
3224 reg_w(sd, R511_CAM_OPTS, 0x03);
3226 /* Snapshot additions */
3227 reg_w(sd, R511_SNAP_PXCNT, hsegs);
3228 reg_w(sd, R511_SNAP_LNCNT, vsegs);
3229 reg_w(sd, R511_SNAP_PXDIV, 0x00);
3230 reg_w(sd, R511_SNAP_LNDIV, 0x00);
3232 /******** Set the framerate ********/
3234 sd->frame_rate = frame_rate;
3236 switch (sd->sensor) {
3238 /* No framerate control, doesn't like higher rates yet */
3242 /* Note once the FIXME's in mode_init_ov_sensor_regs() are fixed
3243 for more sensors we need to do this for them too */
3249 if (sd->gspca_dev.width == 320)
3255 switch (sd->frame_rate) {
3258 /* Not enough bandwidth to do 640x480 @ 30 fps */
3259 if (sd->gspca_dev.width != 640) {
3263 /* Fall through for 640x480 case */
3277 sd->clockdiv = (sd->clockdiv + 1) * 2 - 1;
3278 /* Higher then 10 does not work */
3279 if (sd->clockdiv > 10)
3285 /* No framerate control ?? */
3290 /* Check if we have enough bandwidth to disable compression */
3291 fps = (interlaced ? 60 : 30) / (sd->clockdiv + 1) + 1;
3292 needed = fps * sd->gspca_dev.width * sd->gspca_dev.height * 3 / 2;
3293 /* 1400 is a conservative estimate of the max nr of isoc packets/sec */
3294 if (needed > 1400 * packet_size) {
3295 /* Enable Y and UV quantization and compression */
3296 reg_w(sd, R511_COMP_EN, 0x07);
3297 reg_w(sd, R511_COMP_LUT_EN, 0x03);
3299 reg_w(sd, R511_COMP_EN, 0x06);
3300 reg_w(sd, R511_COMP_LUT_EN, 0x00);
3303 reg_w(sd, R51x_SYS_RESET, OV511_RESET_OMNICE);
3304 reg_w(sd, R51x_SYS_RESET, 0);
3309 /* Sets up the OV518/OV518+ with the given image parameters
3311 * OV518 needs a completely different approach, until we can figure out what
3312 * the individual registers do. Also, only 15 FPS is supported now.
3314 * Do not put any sensor-specific code in here (including I2C I/O functions)
3316 static int ov518_mode_init_regs(struct sd *sd)
3318 int hsegs, vsegs, packet_size;
3319 struct usb_host_interface *alt;
3320 struct usb_interface *intf;
3322 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3323 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3325 err("Couldn't get altsetting");
3329 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3330 ov518_reg_w32(sd, R51x_FIFO_PSIZE, packet_size & ~7, 2);
3332 /******** Set the mode ********/
3343 if (sd->bridge == BRIDGE_OV518) {
3344 /* Set 8-bit (YVYU) input format */
3345 reg_w_mask(sd, 0x20, 0x08, 0x08);
3347 /* Set 12-bit (4:2:0) output format */
3348 reg_w_mask(sd, 0x28, 0x80, 0xf0);
3349 reg_w_mask(sd, 0x38, 0x80, 0xf0);
3351 reg_w(sd, 0x28, 0x80);
3352 reg_w(sd, 0x38, 0x80);
3355 hsegs = sd->gspca_dev.width / 16;
3356 vsegs = sd->gspca_dev.height / 4;
3358 reg_w(sd, 0x29, hsegs);
3359 reg_w(sd, 0x2a, vsegs);
3361 reg_w(sd, 0x39, hsegs);
3362 reg_w(sd, 0x3a, vsegs);
3364 /* Windows driver does this here; who knows why */
3365 reg_w(sd, 0x2f, 0x80);
3367 /******** Set the framerate ********/
3370 /* Mode independent, but framerate dependent, regs */
3371 /* 0x51: Clock divider; Only works on some cams which use 2 crystals */
3372 reg_w(sd, 0x51, 0x04);
3373 reg_w(sd, 0x22, 0x18);
3374 reg_w(sd, 0x23, 0xff);
3376 if (sd->bridge == BRIDGE_OV518PLUS) {
3377 switch (sd->sensor) {
3379 if (sd->gspca_dev.width == 320) {
3380 reg_w(sd, 0x20, 0x00);
3381 reg_w(sd, 0x21, 0x19);
3383 reg_w(sd, 0x20, 0x60);
3384 reg_w(sd, 0x21, 0x1f);
3388 reg_w(sd, 0x20, 0x00);
3389 reg_w(sd, 0x21, 0x19);
3392 reg_w(sd, 0x21, 0x19);
3395 reg_w(sd, 0x71, 0x17); /* Compression-related? */
3397 /* FIXME: Sensor-specific */
3398 /* Bit 5 is what matters here. Of course, it is "reserved" */
3399 i2c_w(sd, 0x54, 0x23);
3401 reg_w(sd, 0x2f, 0x80);
3403 if (sd->bridge == BRIDGE_OV518PLUS) {
3404 reg_w(sd, 0x24, 0x94);
3405 reg_w(sd, 0x25, 0x90);
3406 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3407 ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */
3408 ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */
3409 ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */
3410 ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */
3411 ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */
3412 ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */
3413 ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */
3414 ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */
3416 reg_w(sd, 0x24, 0x9f);
3417 reg_w(sd, 0x25, 0x90);
3418 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3419 ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */
3420 ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */
3421 ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */
3422 ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */
3423 ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */
3424 ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */
3425 ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */
3426 ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */
3429 reg_w(sd, 0x2f, 0x80);
3435 /* Sets up the OV519 with the given image parameters
3437 * OV519 needs a completely different approach, until we can figure out what
3438 * the individual registers do.
3440 * Do not put any sensor-specific code in here (including I2C I/O functions)
3442 static int ov519_mode_init_regs(struct sd *sd)
3444 static const struct ov_regvals mode_init_519_ov7670[] = {
3445 { 0x5d, 0x03 }, /* Turn off suspend mode */
3446 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3447 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
3448 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3452 { 0x37, 0x00 }, /* SetUsbInit */
3453 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3454 /* Enable both fields, YUV Input, disable defect comp (why?) */
3458 { 0x17, 0x50 }, /* undocumented */
3459 { 0x37, 0x00 }, /* undocumented */
3460 { 0x40, 0xff }, /* I2C timeout counter */
3461 { 0x46, 0x00 }, /* I2C clock prescaler */
3462 { 0x59, 0x04 }, /* new from windrv 090403 */
3463 { 0xff, 0x00 }, /* undocumented */
3464 /* windows reads 0x55 at this point, why? */
3467 static const struct ov_regvals mode_init_519[] = {
3468 { 0x5d, 0x03 }, /* Turn off suspend mode */
3469 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3470 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
3471 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3475 { 0x37, 0x00 }, /* SetUsbInit */
3476 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3477 /* Enable both fields, YUV Input, disable defect comp (why?) */
3479 { 0x17, 0x50 }, /* undocumented */
3480 { 0x37, 0x00 }, /* undocumented */
3481 { 0x40, 0xff }, /* I2C timeout counter */
3482 { 0x46, 0x00 }, /* I2C clock prescaler */
3483 { 0x59, 0x04 }, /* new from windrv 090403 */
3484 { 0xff, 0x00 }, /* undocumented */
3485 /* windows reads 0x55 at this point, why? */
3488 /******** Set the mode ********/
3489 if (sd->sensor != SEN_OV7670) {
3490 if (write_regvals(sd, mode_init_519,
3491 ARRAY_SIZE(mode_init_519)))
3493 if (sd->sensor == SEN_OV7640 ||
3494 sd->sensor == SEN_OV7648) {
3495 /* Select 8-bit input mode */
3496 reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
3499 if (write_regvals(sd, mode_init_519_ov7670,
3500 ARRAY_SIZE(mode_init_519_ov7670)))
3504 reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);
3505 reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);
3506 if (sd->sensor == SEN_OV7670 &&
3507 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3508 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
3509 else if (sd->sensor == SEN_OV7648 &&
3510 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3511 reg_w(sd, OV519_R12_X_OFFSETL, 0x01);
3513 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
3514 reg_w(sd, OV519_R13_X_OFFSETH, 0x00);
3515 reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);
3516 reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);
3517 reg_w(sd, OV519_R16_DIVIDER, 0x00);
3518 reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */
3519 reg_w(sd, 0x26, 0x00); /* Undocumented */
3521 /******** Set the framerate ********/
3523 sd->frame_rate = frame_rate;
3525 /* FIXME: These are only valid at the max resolution. */
3527 switch (sd->sensor) {
3530 switch (sd->frame_rate) {
3533 reg_w(sd, 0xa4, 0x0c);
3534 reg_w(sd, 0x23, 0xff);
3537 reg_w(sd, 0xa4, 0x0c);
3538 reg_w(sd, 0x23, 0x1f);
3541 reg_w(sd, 0xa4, 0x0c);
3542 reg_w(sd, 0x23, 0x1b);
3545 reg_w(sd, 0xa4, 0x04);
3546 reg_w(sd, 0x23, 0xff);
3550 reg_w(sd, 0xa4, 0x04);
3551 reg_w(sd, 0x23, 0x1f);
3555 reg_w(sd, 0xa4, 0x04);
3556 reg_w(sd, 0x23, 0x1b);
3562 switch (sd->frame_rate) {
3563 default: /* 15 fps */
3565 reg_w(sd, 0xa4, 0x06);
3566 reg_w(sd, 0x23, 0xff);
3569 reg_w(sd, 0xa4, 0x06);
3570 reg_w(sd, 0x23, 0x1f);
3573 reg_w(sd, 0xa4, 0x06);
3574 reg_w(sd, 0x23, 0x1b);
3578 case SEN_OV7670: /* guesses, based on 7640 */
3579 PDEBUG(D_STREAM, "Setting framerate to %d fps",
3580 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
3581 reg_w(sd, 0xa4, 0x10);
3582 switch (sd->frame_rate) {
3584 reg_w(sd, 0x23, 0xff);
3587 reg_w(sd, 0x23, 0x1b);
3591 reg_w(sd, 0x23, 0xff);
3600 static int mode_init_ov_sensor_regs(struct sd *sd)
3602 struct gspca_dev *gspca_dev;
3603 int qvga, xstart, xend, ystart, yend;
3606 gspca_dev = &sd->gspca_dev;
3607 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
3609 /******** Mode (VGA/QVGA) and sensor specific regs ********/
3610 switch (sd->sensor) {
3612 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3613 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3614 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3615 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3616 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3617 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3618 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3622 xstart = (1040 - gspca_dev->width) / 2 + (0x1f << 4);
3623 ystart = (776 - gspca_dev->height) / 2;
3625 xstart = (2076 - gspca_dev->width) / 2 + (0x10 << 4);
3626 ystart = (1544 - gspca_dev->height) / 2;
3628 xend = xstart + gspca_dev->width;
3629 yend = ystart + gspca_dev->height;
3630 /* Writing to the COMH register resets the other windowing regs
3631 to their default values, so we must do this first. */
3632 i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0xf0);
3633 i2c_w_mask(sd, 0x32,
3634 (((xend >> 1) & 7) << 3) | ((xstart >> 1) & 7),
3636 i2c_w_mask(sd, 0x03,
3637 (((yend >> 1) & 3) << 2) | ((ystart >> 1) & 3),
3639 i2c_w(sd, 0x17, xstart >> 4);
3640 i2c_w(sd, 0x18, xend >> 4);
3641 i2c_w(sd, 0x19, ystart >> 3);
3642 i2c_w(sd, 0x1a, yend >> 3);
3645 /* For OV8610 qvga means qsvga */
3646 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
3647 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3648 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3649 i2c_w_mask(sd, 0x2d, 0x00, 0x40); /* from windrv 090403 */
3650 i2c_w_mask(sd, 0x28, 0x20, 0x20); /* progressive mode on */
3653 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3654 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
3655 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3656 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3661 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3662 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3663 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3664 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3665 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3666 i2c_w_mask(sd, 0x67, qvga ? 0xb0 : 0x90, 0xf0);
3667 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3668 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3669 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3670 if (sd->sensor == SEN_OV76BE)
3671 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
3675 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3676 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3677 /* Setting this undocumented bit in qvga mode removes a very
3678 annoying vertical shaking of the image */
3679 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3681 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3682 /* Allow higher automatic gain (to allow higher framerates) */
3683 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3684 i2c_w_mask(sd, 0x12, 0x04, 0x04); /* AWB: 1 */
3687 /* set COM7_FMT_VGA or COM7_FMT_QVGA
3688 * do we need to set anything else?
3689 * HSTART etc are set in set_ov_sensor_window itself */
3690 i2c_w_mask(sd, OV7670_REG_COM7,
3691 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
3692 OV7670_COM7_FMT_MASK);
3693 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3694 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
3696 if (qvga) { /* QVGA from ov7670.c by
3697 * Jonathan Corbet */
3708 /* OV7670 hardware window registers are split across
3709 * multiple locations */
3710 i2c_w(sd, OV7670_REG_HSTART, xstart >> 3);
3711 i2c_w(sd, OV7670_REG_HSTOP, xend >> 3);
3712 v = i2c_r(sd, OV7670_REG_HREF);
3713 v = (v & 0xc0) | ((xend & 0x7) << 3) | (xstart & 0x07);
3714 msleep(10); /* need to sleep between read and write to
3716 i2c_w(sd, OV7670_REG_HREF, v);
3718 i2c_w(sd, OV7670_REG_VSTART, ystart >> 2);
3719 i2c_w(sd, OV7670_REG_VSTOP, yend >> 2);
3720 v = i2c_r(sd, OV7670_REG_VREF);
3721 v = (v & 0xc0) | ((yend & 0x3) << 2) | (ystart & 0x03);
3722 msleep(10); /* need to sleep between read and write to
3724 i2c_w(sd, OV7670_REG_VREF, v);
3727 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3728 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3729 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3733 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3734 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3740 /******** Clock programming ********/
3741 i2c_w(sd, 0x11, sd->clockdiv);
3746 static void sethvflip(struct gspca_dev *gspca_dev)
3748 struct sd *sd = (struct sd *) gspca_dev;
3750 if (sd->sensor != SEN_OV7670)
3752 if (sd->gspca_dev.streaming)
3754 i2c_w_mask(sd, OV7670_REG_MVFP,
3755 OV7670_MVFP_MIRROR * sd->ctrls[HFLIP].val
3756 | OV7670_MVFP_VFLIP * sd->ctrls[VFLIP].val,
3757 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
3758 if (sd->gspca_dev.streaming)
3762 static int set_ov_sensor_window(struct sd *sd)
3764 struct gspca_dev *gspca_dev;
3766 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
3769 /* mode setup is fully handled in mode_init_ov_sensor_regs for these */
3770 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610 ||
3771 sd->sensor == SEN_OV7670)
3772 return mode_init_ov_sensor_regs(sd);
3774 gspca_dev = &sd->gspca_dev;
3775 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
3776 crop = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 2;
3778 /* The different sensor ICs handle setting up of window differently.
3779 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
3780 switch (sd->sensor) {
3791 vwsbase = vwebase = 0x05;
3800 if (sd->sensor == SEN_OV66308AF && qvga)
3801 /* HDG: this fixes U and V getting swapped */
3812 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
3814 vwsbase = vwebase = 0x05;
3820 vwsbase = vwebase = 0x03;
3826 switch (sd->sensor) {
3830 if (qvga) { /* QCIF */
3835 vwscale = 1; /* The datasheet says 0;
3840 if (qvga) { /* QSVGA */
3848 default: /* SEN_OV7xx0 */
3849 if (qvga) { /* QVGA */
3858 ret = mode_init_ov_sensor_regs(sd);
3862 i2c_w(sd, 0x17, hwsbase);
3863 i2c_w(sd, 0x18, hwebase + (sd->sensor_width >> hwscale));
3864 i2c_w(sd, 0x19, vwsbase);
3865 i2c_w(sd, 0x1a, vwebase + (sd->sensor_height >> vwscale));
3870 /* -- start the camera -- */
3871 static int sd_start(struct gspca_dev *gspca_dev)
3873 struct sd *sd = (struct sd *) gspca_dev;
3876 /* Default for most bridges, allow bridge_mode_init_regs to override */
3877 sd->sensor_width = sd->gspca_dev.width;
3878 sd->sensor_height = sd->gspca_dev.height;
3880 switch (sd->bridge) {
3882 case BRIDGE_OV511PLUS:
3883 ret = ov511_mode_init_regs(sd);
3886 case BRIDGE_OV518PLUS:
3887 ret = ov518_mode_init_regs(sd);
3890 ret = ov519_mode_init_regs(sd);
3892 /* case BRIDGE_OVFX2: nothing to do */
3893 case BRIDGE_W9968CF:
3894 ret = w9968cf_mode_init_regs(sd);
3900 ret = set_ov_sensor_window(sd);
3904 setcontrast(gspca_dev);
3905 setbrightness(gspca_dev);
3906 setcolors(gspca_dev);
3907 sethvflip(gspca_dev);
3908 setautobright(gspca_dev);
3911 /* Force clear snapshot state in case the snapshot button was
3912 pressed while we weren't streaming */
3913 sd->snapshot_needs_reset = 1;
3914 sd_reset_snapshot(gspca_dev);
3916 sd->first_frame = 3;
3918 ret = ov51x_restart(sd);
3921 ov51x_led_control(sd, 1);
3924 PDEBUG(D_ERR, "camera start error:%d", ret);
3928 static void sd_stopN(struct gspca_dev *gspca_dev)
3930 struct sd *sd = (struct sd *) gspca_dev;
3933 ov51x_led_control(sd, 0);
3936 static void sd_stop0(struct gspca_dev *gspca_dev)
3938 struct sd *sd = (struct sd *) gspca_dev;
3940 if (sd->bridge == BRIDGE_W9968CF)
3943 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3944 /* If the last button state is pressed, release it now! */
3945 if (sd->snapshot_pressed) {
3946 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
3947 input_sync(gspca_dev->input_dev);
3948 sd->snapshot_pressed = 0;
3953 static void ov51x_handle_button(struct gspca_dev *gspca_dev, u8 state)
3955 struct sd *sd = (struct sd *) gspca_dev;
3957 if (sd->snapshot_pressed != state) {
3958 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
3959 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
3960 input_sync(gspca_dev->input_dev);
3963 sd->snapshot_needs_reset = 1;
3965 sd->snapshot_pressed = state;
3967 /* On the ov511 / ov519 we need to reset the button state
3968 multiple times, as resetting does not work as long as the
3969 button stays pressed */
3970 switch (sd->bridge) {
3972 case BRIDGE_OV511PLUS:
3975 sd->snapshot_needs_reset = 1;
3981 static void ov511_pkt_scan(struct gspca_dev *gspca_dev,
3982 u8 *in, /* isoc packet */
3983 int len) /* iso packet length */
3985 struct sd *sd = (struct sd *) gspca_dev;
3987 /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
3988 * byte non-zero. The EOF packet has image width/height in the
3989 * 10th and 11th bytes. The 9th byte is given as follows:
3992 * 6: compression enabled
3993 * 5: 422/420/400 modes
3994 * 4: 422/420/400 modes
3996 * 2: snapshot button on
4000 if (!(in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) &&
4002 ov51x_handle_button(gspca_dev, (in[8] >> 2) & 1);
4005 if ((in[9] + 1) * 8 != gspca_dev->width ||
4006 (in[10] + 1) * 8 != gspca_dev->height) {
4007 PDEBUG(D_ERR, "Invalid frame size, got: %dx%d,"
4008 " requested: %dx%d\n",
4009 (in[9] + 1) * 8, (in[10] + 1) * 8,
4010 gspca_dev->width, gspca_dev->height);
4011 gspca_dev->last_packet_type = DISCARD_PACKET;
4014 /* Add 11 byte footer to frame, might be usefull */
4015 gspca_frame_add(gspca_dev, LAST_PACKET, in, 11);
4019 gspca_frame_add(gspca_dev, FIRST_PACKET, in, 0);
4024 /* Ignore the packet number */
4027 /* intermediate packet */
4028 gspca_frame_add(gspca_dev, INTER_PACKET, in, len);
4031 static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
4032 u8 *data, /* isoc packet */
4033 int len) /* iso packet length */
4035 struct sd *sd = (struct sd *) gspca_dev;
4037 /* A false positive here is likely, until OVT gives me
4038 * the definitive SOF/EOF format */
4039 if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
4040 ov51x_handle_button(gspca_dev, (data[6] >> 1) & 1);
4041 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4042 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
4046 if (gspca_dev->last_packet_type == DISCARD_PACKET)
4049 /* Does this device use packet numbers ? */
4052 if (sd->packet_nr == data[len])
4054 /* The last few packets of the frame (which are all 0's
4055 except that they may contain part of the footer), are
4057 else if (sd->packet_nr == 0 || data[len]) {
4058 PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
4059 (int)data[len], (int)sd->packet_nr);
4060 gspca_dev->last_packet_type = DISCARD_PACKET;
4065 /* intermediate packet */
4066 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4069 static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
4070 u8 *data, /* isoc packet */
4071 int len) /* iso packet length */
4073 /* Header of ov519 is 16 bytes:
4074 * Byte Value Description
4078 * 3 0xXX 0x50 = SOF, 0x51 = EOF
4079 * 9 0xXX 0x01 initial frame without data,
4080 * 0x00 standard frame with image
4081 * 14 Lo in EOF: length of image data / 8
4085 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
4087 case 0x50: /* start of frame */
4088 /* Don't check the button state here, as the state
4089 usually (always ?) changes at EOF and checking it
4090 here leads to unnecessary snapshot state resets. */
4095 if (data[0] == 0xff || data[1] == 0xd8)
4096 gspca_frame_add(gspca_dev, FIRST_PACKET,
4099 gspca_dev->last_packet_type = DISCARD_PACKET;
4101 case 0x51: /* end of frame */
4102 ov51x_handle_button(gspca_dev, data[11] & 1);
4104 gspca_dev->last_packet_type = DISCARD_PACKET;
4105 gspca_frame_add(gspca_dev, LAST_PACKET,
4111 /* intermediate packet */
4112 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4115 static void ovfx2_pkt_scan(struct gspca_dev *gspca_dev,
4116 u8 *data, /* isoc packet */
4117 int len) /* iso packet length */
4119 struct sd *sd = (struct sd *) gspca_dev;
4121 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4123 /* A short read signals EOF */
4124 if (len < OVFX2_BULK_SIZE) {
4125 /* If the frame is short, and it is one of the first ones
4126 the sensor and bridge are still syncing, so drop it. */
4127 if (sd->first_frame) {
4129 if (gspca_dev->image_len <
4130 sd->gspca_dev.width * sd->gspca_dev.height)
4131 gspca_dev->last_packet_type = DISCARD_PACKET;
4133 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4134 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
4138 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
4139 u8 *data, /* isoc packet */
4140 int len) /* iso packet length */
4142 struct sd *sd = (struct sd *) gspca_dev;
4144 switch (sd->bridge) {
4146 case BRIDGE_OV511PLUS:
4147 ov511_pkt_scan(gspca_dev, data, len);
4150 case BRIDGE_OV518PLUS:
4151 ov518_pkt_scan(gspca_dev, data, len);
4154 ov519_pkt_scan(gspca_dev, data, len);
4157 ovfx2_pkt_scan(gspca_dev, data, len);
4159 case BRIDGE_W9968CF:
4160 w9968cf_pkt_scan(gspca_dev, data, len);
4165 /* -- management routines -- */
4167 static void setbrightness(struct gspca_dev *gspca_dev)
4169 struct sd *sd = (struct sd *) gspca_dev;
4172 val = sd->ctrls[BRIGHTNESS].val;
4173 switch (sd->sensor) {
4182 i2c_w(sd, OV7610_REG_BRT, val);
4186 /* 7620 doesn't like manual changes when in auto mode */
4187 if (!sd->ctrls[AUTOBRIGHT].val)
4188 i2c_w(sd, OV7610_REG_BRT, val);
4192 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
4193 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
4198 static void setcontrast(struct gspca_dev *gspca_dev)
4200 struct sd *sd = (struct sd *) gspca_dev;
4203 val = sd->ctrls[CONTRAST].val;
4204 switch (sd->sensor) {
4207 i2c_w(sd, OV7610_REG_CNT, val);
4211 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
4214 static const __u8 ctab[] = {
4215 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
4218 /* Use Y gamma control instead. Bit 0 enables it. */
4219 i2c_w(sd, 0x64, ctab[val >> 5]);
4223 case SEN_OV7620AE: {
4224 static const __u8 ctab[] = {
4225 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
4226 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
4229 /* Use Y gamma control instead. Bit 0 enables it. */
4230 i2c_w(sd, 0x64, ctab[val >> 4]);
4234 /* check that this isn't just the same as ov7610 */
4235 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
4240 static void setcolors(struct gspca_dev *gspca_dev)
4242 struct sd *sd = (struct sd *) gspca_dev;
4245 val = sd->ctrls[COLORS].val;
4246 switch (sd->sensor) {
4253 i2c_w(sd, OV7610_REG_SAT, val);
4257 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
4258 /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
4261 i2c_w(sd, OV7610_REG_SAT, val);
4265 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
4268 /* supported later once I work out how to do it
4269 * transparently fail now! */
4270 /* set REG_COM13 values for UV sat auto mode */
4275 static void setautobright(struct gspca_dev *gspca_dev)
4277 struct sd *sd = (struct sd *) gspca_dev;
4279 if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7648 ||
4280 sd->sensor == SEN_OV7670 ||
4281 sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
4284 i2c_w_mask(sd, 0x2d, sd->ctrls[AUTOBRIGHT].val ? 0x10 : 0x00, 0x10);
4287 static void setfreq_i(struct sd *sd)
4289 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
4292 if (sd->sensor == SEN_OV7670) {
4293 switch (sd->ctrls[FREQ].val) {
4294 case 0: /* Banding filter disabled */
4295 i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_BFILT);
4298 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4300 i2c_w_mask(sd, OV7670_REG_COM11, 0x08, 0x18);
4303 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4305 i2c_w_mask(sd, OV7670_REG_COM11, 0x00, 0x18);
4307 case 3: /* Auto hz */
4308 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4310 i2c_w_mask(sd, OV7670_REG_COM11, OV7670_COM11_HZAUTO,
4315 switch (sd->ctrls[FREQ].val) {
4316 case 0: /* Banding filter disabled */
4317 i2c_w_mask(sd, 0x2d, 0x00, 0x04);
4318 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4320 case 1: /* 50 hz (filter on and framerate adj) */
4321 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4322 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4323 /* 20 fps -> 16.667 fps */
4324 if (sd->sensor == SEN_OV6620 ||
4325 sd->sensor == SEN_OV6630 ||
4326 sd->sensor == SEN_OV66308AF)
4327 i2c_w(sd, 0x2b, 0x5e);
4329 i2c_w(sd, 0x2b, 0xac);
4331 case 2: /* 60 hz (filter on, ...) */
4332 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4333 if (sd->sensor == SEN_OV6620 ||
4334 sd->sensor == SEN_OV6630 ||
4335 sd->sensor == SEN_OV66308AF) {
4336 /* 20 fps -> 15 fps */
4337 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4338 i2c_w(sd, 0x2b, 0xa8);
4340 /* no framerate adj. */
4341 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4347 static void setfreq(struct gspca_dev *gspca_dev)
4349 struct sd *sd = (struct sd *) gspca_dev;
4353 /* Ugly but necessary */
4354 if (sd->bridge == BRIDGE_W9968CF)
4355 w9968cf_set_crop_window(sd);
4358 static int sd_querymenu(struct gspca_dev *gspca_dev,
4359 struct v4l2_querymenu *menu)
4361 struct sd *sd = (struct sd *) gspca_dev;
4364 case V4L2_CID_POWER_LINE_FREQUENCY:
4365 switch (menu->index) {
4366 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
4367 strcpy((char *) menu->name, "NoFliker");
4369 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
4370 strcpy((char *) menu->name, "50 Hz");
4372 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
4373 strcpy((char *) menu->name, "60 Hz");
4376 if (sd->sensor != SEN_OV7670)
4379 strcpy((char *) menu->name, "Automatic");
4387 static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4388 struct v4l2_jpegcompression *jcomp)
4390 struct sd *sd = (struct sd *) gspca_dev;
4392 if (sd->bridge != BRIDGE_W9968CF)
4395 memset(jcomp, 0, sizeof *jcomp);
4396 jcomp->quality = sd->quality;
4397 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |
4398 V4L2_JPEG_MARKER_DRI;
4402 static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4403 struct v4l2_jpegcompression *jcomp)
4405 struct sd *sd = (struct sd *) gspca_dev;
4407 if (sd->bridge != BRIDGE_W9968CF)
4410 if (gspca_dev->streaming)
4413 if (jcomp->quality < QUALITY_MIN)
4414 sd->quality = QUALITY_MIN;
4415 else if (jcomp->quality > QUALITY_MAX)
4416 sd->quality = QUALITY_MAX;
4418 sd->quality = jcomp->quality;
4420 /* Return resulting jcomp params to app */
4421 sd_get_jcomp(gspca_dev, jcomp);
4426 /* sub-driver description */
4427 static const struct sd_desc sd_desc = {
4428 .name = MODULE_NAME,
4430 .nctrls = ARRAY_SIZE(sd_ctrls),
4431 .config = sd_config,
4436 .pkt_scan = sd_pkt_scan,
4437 .dq_callback = sd_reset_snapshot,
4438 .querymenu = sd_querymenu,
4439 .get_jcomp = sd_get_jcomp,
4440 .set_jcomp = sd_set_jcomp,
4441 #if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
4446 /* -- module initialisation -- */
4447 static const __devinitdata struct usb_device_id device_table[] = {
4448 {USB_DEVICE(0x041e, 0x4003), .driver_info = BRIDGE_W9968CF },
4449 {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
4450 {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
4451 {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
4452 {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
4453 {USB_DEVICE(0x041e, 0x4064),
4454 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4455 {USB_DEVICE(0x041e, 0x4067), .driver_info = BRIDGE_OV519 },
4456 {USB_DEVICE(0x041e, 0x4068),
4457 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4458 {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
4459 {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
4460 {USB_DEVICE(0x054c, 0x0155),
4461 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
4462 {USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },
4463 {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
4464 {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
4465 {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
4466 {USB_DEVICE(0x05a9, 0x2800), .driver_info = BRIDGE_OVFX2 },
4467 {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
4468 {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
4469 {USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },
4470 {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
4471 {USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },
4472 {USB_DEVICE(0x0b62, 0x0059), .driver_info = BRIDGE_OVFX2 },
4473 {USB_DEVICE(0x0e96, 0xc001), .driver_info = BRIDGE_OVFX2 },
4474 {USB_DEVICE(0x1046, 0x9967), .driver_info = BRIDGE_W9968CF },
4475 {USB_DEVICE(0x8020, 0xEF04), .driver_info = BRIDGE_OVFX2 },
4479 MODULE_DEVICE_TABLE(usb, device_table);
4481 /* -- device connect -- */
4482 static int sd_probe(struct usb_interface *intf,
4483 const struct usb_device_id *id)
4485 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
4489 static struct usb_driver sd_driver = {
4490 .name = MODULE_NAME,
4491 .id_table = device_table,
4493 .disconnect = gspca_disconnect,
4495 .suspend = gspca_suspend,
4496 .resume = gspca_resume,
4500 /* -- module insert / remove -- */
4501 static int __init sd_mod_init(void)
4503 return usb_register(&sd_driver);
4505 static void __exit sd_mod_exit(void)
4507 usb_deregister(&sd_driver);
4510 module_init(sd_mod_init);
4511 module_exit(sd_mod_exit);
4513 module_param(frame_rate, int, 0644);
4514 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");