4 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
6 * (This module is adapted from the ov51x-jpeg package)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define MODULE_NAME "ov519"
27 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
28 MODULE_DESCRIPTION("OV519 USB Camera Driver");
29 MODULE_LICENSE("GPL");
31 /* global parameters */
32 static int frame_rate;
34 /* Number of times to retry a failed I2C transaction. Increase this if you
35 * are getting "Failed to read sensor ID..." */
36 static int i2c_detect_tries = 10;
38 /* ov519 device descriptor */
40 struct gspca_dev gspca_dev; /* !! must be the first item */
42 /* Determined by sensor type */
46 unsigned char primary_i2c_slave; /* I2C write id of sensor */
48 unsigned char brightness;
49 unsigned char contrast;
54 char compress; /* Should the next frame be compressed? */
55 char compress_inited; /* Are compression params uploaded? */
56 char stopped; /* Streaming is temporarily paused */
58 char frame_rate; /* current Framerate (OV519 only) */
59 char clockdiv; /* clockdiv override for OV519 only */
61 char sensor; /* Type of image sensor chip (SEN_*) */
75 /* V4L2 controls supported by the driver */
76 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
77 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
78 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
79 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
80 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
81 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
82 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
83 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
84 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
85 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
87 static struct ctrl sd_ctrls[] = {
88 #define SD_BRIGHTNESS 0
91 .id = V4L2_CID_BRIGHTNESS,
92 .type = V4L2_CTRL_TYPE_INTEGER,
99 .set = sd_setbrightness,
100 .get = sd_getbrightness,
102 #define SD_CONTRAST 1
105 .id = V4L2_CID_CONTRAST,
106 .type = V4L2_CTRL_TYPE_INTEGER,
111 .default_value = 127,
113 .set = sd_setcontrast,
114 .get = sd_getcontrast,
119 .id = V4L2_CID_SATURATION,
120 .type = V4L2_CTRL_TYPE_INTEGER,
121 .name = "Saturation",
125 .default_value = 127,
130 /* next controls work with ov7670 only */
133 .id = V4L2_CID_HFLIP,
134 .type = V4L2_CTRL_TYPE_BOOLEAN,
140 .default_value = HFLIP_DEF,
147 .id = V4L2_CID_VFLIP,
148 .type = V4L2_CTRL_TYPE_BOOLEAN,
154 .default_value = VFLIP_DEF,
161 static struct v4l2_pix_format vga_mode[] = {
162 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
164 .sizeimage = 320 * 240 * 3 / 8 + 589,
165 .colorspace = V4L2_COLORSPACE_JPEG,
167 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
169 .sizeimage = 640 * 480 * 3 / 8 + 590,
170 .colorspace = V4L2_COLORSPACE_JPEG,
173 static struct v4l2_pix_format sif_mode[] = {
174 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
176 .sizeimage = 176 * 144 * 3 / 8 + 589,
177 .colorspace = V4L2_COLORSPACE_JPEG,
179 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
181 .sizeimage = 352 * 288 * 3 / 8 + 589,
182 .colorspace = V4L2_COLORSPACE_JPEG,
186 /* OV519 Camera interface register numbers */
187 #define OV519_CAM_H_SIZE 0x10
188 #define OV519_CAM_V_SIZE 0x11
189 #define OV519_CAM_X_OFFSETL 0x12
190 #define OV519_CAM_X_OFFSETH 0x13
191 #define OV519_CAM_Y_OFFSETL 0x14
192 #define OV519_CAM_Y_OFFSETH 0x15
193 #define OV519_CAM_DIVIDER 0x16
194 #define OV519_CAM_DFR 0x20
195 #define OV519_CAM_FORMAT 0x25
197 /* OV519 System Controller register numbers */
198 #define OV519_SYS_RESET1 0x51
199 #define OV519_SYS_EN_CLK1 0x54
201 #define OV519_GPIO_DATA_OUT0 0x71
202 #define OV519_GPIO_IO_CTRL0 0x72
204 #define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
207 #define R51x_I2C_W_SID 0x41
208 #define R51x_I2C_SADDR_3 0x42
209 #define R51x_I2C_SADDR_2 0x43
210 #define R51x_I2C_R_SID 0x44
211 #define R51x_I2C_DATA 0x45
212 #define R518_I2C_CTL 0x47 /* OV518(+) only */
215 #define OV7xx0_SID 0x42
216 #define OV8xx0_SID 0xa0
217 #define OV6xx0_SID 0xc0
219 /* OV7610 registers */
220 #define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
221 #define OV7610_REG_SAT 0x03 /* saturation */
222 #define OV8610_REG_HUE 0x04 /* 04 reserved */
223 #define OV7610_REG_CNT 0x05 /* Y contrast */
224 #define OV7610_REG_BRT 0x06 /* Y brightness */
225 #define OV7610_REG_COM_C 0x14 /* misc common regs */
226 #define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
227 #define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
228 #define OV7610_REG_COM_I 0x29 /* misc settings */
230 /* OV7670 registers */
231 #define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
232 #define OV7670_REG_BLUE 0x01 /* blue gain */
233 #define OV7670_REG_RED 0x02 /* red gain */
234 #define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
235 #define OV7670_REG_COM1 0x04 /* Control 1 */
236 #define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
237 #define OV7670_REG_COM3 0x0c /* Control 3 */
238 #define OV7670_REG_COM4 0x0d /* Control 4 */
239 #define OV7670_REG_COM5 0x0e /* All "reserved" */
240 #define OV7670_REG_COM6 0x0f /* Control 6 */
241 #define OV7670_REG_AECH 0x10 /* More bits of AEC value */
242 #define OV7670_REG_CLKRC 0x11 /* Clock control */
243 #define OV7670_REG_COM7 0x12 /* Control 7 */
244 #define OV7670_COM7_FMT_VGA 0x00
245 #define OV7670_COM7_YUV 0x00 /* YUV */
246 #define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
247 #define OV7670_COM7_FMT_MASK 0x38
248 #define OV7670_COM7_RESET 0x80 /* Register reset */
249 #define OV7670_REG_COM8 0x13 /* Control 8 */
250 #define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
251 #define OV7670_COM8_AWB 0x02 /* White balance enable */
252 #define OV7670_COM8_AGC 0x04 /* Auto gain enable */
253 #define OV7670_COM8_BFILT 0x20 /* Band filter enable */
254 #define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
255 #define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
256 #define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
257 #define OV7670_REG_COM10 0x15 /* Control 10 */
258 #define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
259 #define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
260 #define OV7670_REG_VSTART 0x19 /* Vert start high bits */
261 #define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
262 #define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
263 #define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
264 #define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
265 #define OV7670_REG_AEW 0x24 /* AGC upper limit */
266 #define OV7670_REG_AEB 0x25 /* AGC lower limit */
267 #define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
268 #define OV7670_REG_HREF 0x32 /* HREF pieces */
269 #define OV7670_REG_TSLB 0x3a /* lots of stuff */
270 #define OV7670_REG_COM11 0x3b /* Control 11 */
271 #define OV7670_COM11_EXP 0x02
272 #define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
273 #define OV7670_REG_COM12 0x3c /* Control 12 */
274 #define OV7670_REG_COM13 0x3d /* Control 13 */
275 #define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
276 #define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
277 #define OV7670_REG_COM14 0x3e /* Control 14 */
278 #define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
279 #define OV7670_REG_COM15 0x40 /* Control 15 */
280 #define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
281 #define OV7670_REG_COM16 0x41 /* Control 16 */
282 #define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
283 #define OV7670_REG_BRIGHT 0x55 /* Brightness */
284 #define OV7670_REG_CONTRAS 0x56 /* Contrast control */
285 #define OV7670_REG_GFIX 0x69 /* Fix gain control */
286 #define OV7670_REG_RGB444 0x8c /* RGB 444 control */
287 #define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
288 #define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
289 #define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
290 #define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
291 #define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
292 #define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
293 #define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
294 #define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
295 #define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
297 struct ovsensor_window {
303 short quarter; /* Scale width and height down 2x */
304 short clockdiv; /* Clock divisor setting */
307 static unsigned char ov7670_abs_to_sm(unsigned char v)
311 return (128 - v) | 0x80;
314 /* Write a OV519 register */
315 static int reg_w(struct sd *sd, __u16 index, __u8 value)
319 sd->gspca_dev.usb_buf[0] = value;
320 ret = usb_control_msg(sd->gspca_dev.dev,
321 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
322 1, /* REQ_IO (ov518/519) */
323 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
325 sd->gspca_dev.usb_buf, 1, 500);
327 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
331 /* Read from a OV519 register */
332 /* returns: negative is error, pos or zero is data */
333 static int reg_r(struct sd *sd, __u16 index)
337 ret = usb_control_msg(sd->gspca_dev.dev,
338 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
340 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
341 0, index, sd->gspca_dev.usb_buf, 1, 500);
344 ret = sd->gspca_dev.usb_buf[0];
346 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
350 /* Read 8 values from a OV519 register */
351 static int reg_r8(struct sd *sd,
356 ret = usb_control_msg(sd->gspca_dev.dev,
357 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
359 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
360 0, index, sd->gspca_dev.usb_buf, 8, 500);
363 ret = sd->gspca_dev.usb_buf[0];
365 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
370 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
371 * the same position as 1's in "mask" are cleared and set to "value". Bits
372 * that are in the same position as 0's in "mask" are preserved, regardless
373 * of their respective state in "value".
375 static int reg_w_mask(struct sd *sd,
384 value &= mask; /* Enforce mask on value */
385 ret = reg_r(sd, index);
389 oldval = ret & ~mask; /* Clear the masked bits */
390 value |= oldval; /* Set the desired bits */
392 return reg_w(sd, index, value);
396 * The OV518 I2C I/O procedure is different, hence, this function.
397 * This is normally only called from i2c_w(). Note that this function
398 * always succeeds regardless of whether the sensor is present and working.
400 static int i2c_w(struct sd *sd,
406 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
408 /* Select camera register */
409 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
413 /* Write "value" to I2C data port of OV511 */
414 rc = reg_w(sd, R51x_I2C_DATA, value);
418 /* Initiate 3-byte write cycle */
419 rc = reg_w(sd, R518_I2C_CTL, 0x01);
421 /* wait for write complete */
425 return reg_r8(sd, R518_I2C_CTL);
429 * returns: negative is error, pos or zero is data
431 * The OV518 I2C I/O procedure is different, hence, this function.
432 * This is normally only called from i2c_r(). Note that this function
433 * always succeeds regardless of whether the sensor is present and working.
435 static int i2c_r(struct sd *sd, __u8 reg)
439 /* Select camera register */
440 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
444 /* Initiate 2-byte write cycle */
445 rc = reg_w(sd, R518_I2C_CTL, 0x03);
449 /* Initiate 2-byte read cycle */
450 rc = reg_w(sd, R518_I2C_CTL, 0x05);
453 value = reg_r(sd, R51x_I2C_DATA);
454 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
458 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
459 * the same position as 1's in "mask" are cleared and set to "value". Bits
460 * that are in the same position as 0's in "mask" are preserved, regardless
461 * of their respective state in "value".
463 static int i2c_w_mask(struct sd *sd,
471 value &= mask; /* Enforce mask on value */
475 oldval = rc & ~mask; /* Clear the masked bits */
476 value |= oldval; /* Set the desired bits */
477 return i2c_w(sd, reg, value);
480 /* Temporarily stops OV511 from functioning. Must do this before changing
481 * registers while the camera is streaming */
482 static inline int ov51x_stop(struct sd *sd)
484 PDEBUG(D_STREAM, "stopping");
486 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
489 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
490 * actually stopped (for performance). */
491 static inline int ov51x_restart(struct sd *sd)
493 PDEBUG(D_STREAM, "restarting");
498 /* Reinitialize the stream */
499 return reg_w(sd, OV519_SYS_RESET1, 0x00);
502 /* This does an initial reset of an OmniVision sensor and ensures that I2C
503 * is synchronized. Returns <0 on failure.
505 static int init_ov_sensor(struct sd *sd)
509 /* Reset the sensor */
510 if (i2c_w(sd, 0x12, 0x80) < 0)
513 /* Wait for it to initialize */
516 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
517 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
518 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
523 /* Reset the sensor */
524 if (i2c_w(sd, 0x12, 0x80) < 0)
526 /* Wait for it to initialize */
528 /* Dummy read to sync I2C */
529 if (i2c_r(sd, 0x00) < 0)
534 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
538 /* Switch on standard JPEG compression. Returns 0 for success. */
539 static int ov519_init_compression(struct sd *sd)
541 if (!sd->compress_inited) {
542 if (reg_w_mask(sd, OV519_SYS_EN_CLK1, 1 << 2, 1 << 2) < 0) {
543 PDEBUG(D_ERR, "Error switching to compressed mode");
546 sd->compress_inited = 1;
551 /* Set the read and write slave IDs. The "slave" argument is the write slave,
552 * and the read slave will be set to (slave + 1).
553 * This should not be called from outside the i2c I/O functions.
554 * Sets I2C read and write slave IDs. Returns <0 for error
556 static int ov51x_set_slave_ids(struct sd *sd,
561 rc = reg_w(sd, R51x_I2C_W_SID, slave);
564 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
571 struct ov_i2c_regvals {
576 static int write_regvals(struct sd *sd,
577 const struct ov_regvals *regvals,
583 rc = reg_w(sd, regvals->reg, regvals->val);
591 static int write_i2c_regvals(struct sd *sd,
592 const struct ov_i2c_regvals *regvals,
598 rc = i2c_w(sd, regvals->reg, regvals->val);
606 /****************************************************************************
608 * OV511 and sensor configuration
610 ***************************************************************************/
612 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
613 * the same register settings as the OV8610, since they are very similar.
615 static int ov8xx0_configure(struct sd *sd)
618 static const struct ov_i2c_regvals norm_8610[] = {
625 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
626 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
635 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
637 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
638 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
639 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
642 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
643 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
644 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
645 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
651 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
653 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
655 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
657 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
658 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
659 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
660 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
662 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
663 * maybe thats wrong */
667 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
671 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
672 deleting bit7 colors the first images red */
673 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
674 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
680 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
682 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
687 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
689 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
690 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
697 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
703 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
706 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
708 if (init_ov_sensor(sd) < 0)
709 PDEBUG(D_ERR|D_PROBE, "Failed to read sensor ID");
711 PDEBUG(D_PROBE, "OV86x0 initialized");
713 /* Detect sensor (sub)type */
714 rc = i2c_r(sd, OV7610_REG_COM_I);
716 PDEBUG(D_ERR, "Error detecting sensor type");
720 PDEBUG(D_PROBE, "Sensor is an OV8610");
721 sd->sensor = SEN_OV8610;
723 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
726 PDEBUG(D_PROBE, "Writing 8610 registers");
727 if (write_i2c_regvals(sd,
729 sizeof norm_8610 / sizeof norm_8610[0]))
732 /* Set sensor-specific vars */
738 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
739 * the same register settings as the OV7610, since they are very similar.
741 static int ov7xx0_configure(struct sd *sd)
745 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
747 * Register 0x0f in the 7610 has the following effects:
749 * 0x85 (AEC method 1): Best overall, good contrast range
750 * 0x45 (AEC method 2): Very overexposed
751 * 0xa5 (spec sheet default): Ok, but the black level is
752 * shifted resulting in loss of contrast
753 * 0x05 (old driver setting): very overexposed, too much
756 static const struct ov_i2c_regvals norm_7610[] = {
763 { 0x28, 0x24 }, /* 0c */
764 { 0x0f, 0x85 }, /* lg's setting */
786 static const struct ov_i2c_regvals norm_7620[] = {
787 { 0x00, 0x00 }, /* gain */
788 { 0x01, 0x80 }, /* blue gain */
789 { 0x02, 0x80 }, /* red gain */
790 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
852 /* 7640 and 7648. The defaults should be OK for most registers. */
853 static const struct ov_i2c_regvals norm_7640[] = {
858 /* 7670. Defaults taken from OmniVision provided data,
859 * as provided by Jonathan Corbet of OLPC */
860 static const struct ov_i2c_regvals norm_7670[] = {
861 { OV7670_REG_COM7, OV7670_COM7_RESET },
862 { OV7670_REG_TSLB, 0x04 }, /* OV */
863 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
864 { OV7670_REG_CLKRC, 0x1 },
866 * Set the hardware window. These values from OV don't entirely
867 * make sense - hstop is less than hstart. But they work...
869 { OV7670_REG_HSTART, 0x13 }, { OV7670_REG_HSTOP, 0x01 },
870 { OV7670_REG_HREF, 0xb6 }, { OV7670_REG_VSTART, 0x02 },
871 { OV7670_REG_VSTOP, 0x7a }, { OV7670_REG_VREF, 0x0a },
873 { OV7670_REG_COM3, 0 }, { OV7670_REG_COM14, 0 },
874 /* Mystery scaling numbers */
875 { 0x70, 0x3a }, { 0x71, 0x35 },
876 { 0x72, 0x11 }, { 0x73, 0xf0 },
879 /* { OV7670_REG_COM10, 0x0 }, */
881 /* Gamma curve values */
889 { 0x7e, 0x5a }, { 0x7f, 0x69 },
890 { 0x80, 0x76 }, { 0x81, 0x80 },
891 { 0x82, 0x88 }, { 0x83, 0x8f },
892 { 0x84, 0x96 }, { 0x85, 0xa3 },
893 { 0x86, 0xaf }, { 0x87, 0xc4 },
894 { 0x88, 0xd7 }, { 0x89, 0xe8 },
896 /* AGC and AEC parameters. Note we start by disabling those features,
897 then turn them only after tweaking the values. */
898 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
899 | OV7670_COM8_AECSTEP
900 | OV7670_COM8_BFILT },
901 { OV7670_REG_GAIN, 0 }, { OV7670_REG_AECH, 0 },
902 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
904 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
905 { OV7670_REG_BD50MAX, 0x05 }, { OV7670_REG_BD60MAX, 0x07 },
906 { OV7670_REG_AEW, 0x95 }, { OV7670_REG_AEB, 0x33 },
907 { OV7670_REG_VPT, 0xe3 }, { OV7670_REG_HAECC1, 0x78 },
908 { OV7670_REG_HAECC2, 0x68 },
910 { 0xa1, 0x03 }, /* magic */
911 { OV7670_REG_HAECC3, 0xd8 }, { OV7670_REG_HAECC4, 0xd8 },
912 { OV7670_REG_HAECC5, 0xf0 }, { OV7670_REG_HAECC6, 0x90 },
913 { OV7670_REG_HAECC7, 0x94 },
914 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
915 | OV7670_COM8_AECSTEP
920 /* Almost all of these are magic "reserved" values. */
921 { OV7670_REG_COM5, 0x61 }, { OV7670_REG_COM6, 0x4b },
924 /* { OV7670_REG_MVFP, 0x07|OV7670_MVFP_MIRROR }, */
925 { OV7670_REG_MVFP, 0x07 },
926 { 0x21, 0x02 }, { 0x22, 0x91 },
927 { 0x29, 0x07 }, { 0x33, 0x0b },
928 { 0x35, 0x0b }, { 0x37, 0x1d },
929 { 0x38, 0x71 }, { 0x39, 0x2a },
930 { OV7670_REG_COM12, 0x78 }, { 0x4d, 0x40 },
931 { 0x4e, 0x20 }, { OV7670_REG_GFIX, 0 },
932 { 0x6b, 0x4a }, { 0x74, 0x10 },
933 { 0x8d, 0x4f }, { 0x8e, 0 },
934 { 0x8f, 0 }, { 0x90, 0 },
935 { 0x91, 0 }, { 0x96, 0 },
936 { 0x9a, 0 }, { 0xb0, 0x84 },
937 { 0xb1, 0x0c }, { 0xb2, 0x0e },
938 { 0xb3, 0x82 }, { 0xb8, 0x0a },
940 /* More reserved magic, some of which tweaks white balance */
941 { 0x43, 0x0a }, { 0x44, 0xf0 },
942 { 0x45, 0x34 }, { 0x46, 0x58 },
943 { 0x47, 0x28 }, { 0x48, 0x3a },
944 { 0x59, 0x88 }, { 0x5a, 0x88 },
945 { 0x5b, 0x44 }, { 0x5c, 0x67 },
946 { 0x5d, 0x49 }, { 0x5e, 0x0e },
947 { 0x6c, 0x0a }, { 0x6d, 0x55 },
948 { 0x6e, 0x11 }, { 0x6f, 0x9f },
949 /* "9e for advance AWB" */
950 { 0x6a, 0x40 }, { OV7670_REG_BLUE, 0x40 },
951 { OV7670_REG_RED, 0x60 },
952 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
953 | OV7670_COM8_AECSTEP
959 /* Matrix coefficients */
960 { 0x4f, 0x80 }, { 0x50, 0x80 },
961 { 0x51, 0 }, { 0x52, 0x22 },
962 { 0x53, 0x5e }, { 0x54, 0x80 },
965 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
966 { OV7670_REG_EDGE, 0 },
967 { 0x75, 0x05 }, { 0x76, 0xe1 },
968 { 0x4c, 0 }, { 0x77, 0x01 },
969 { OV7670_REG_COM13, OV7670_COM13_GAMMA
973 { 0xc9, 0x60 }, { OV7670_REG_COM16, 0x38 },
977 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
978 { 0xa4, 0x88 }, { 0x96, 0 },
979 { 0x97, 0x30 }, { 0x98, 0x20 },
980 { 0x99, 0x30 }, { 0x9a, 0x84 },
981 { 0x9b, 0x29 }, { 0x9c, 0x03 },
982 { 0x9d, 0x4c }, { 0x9e, 0x3f },
985 /* Extra-weird stuff. Some sort of multiplexor register */
986 { 0x79, 0x01 }, { 0xc8, 0xf0 },
987 { 0x79, 0x0f }, { 0xc8, 0x00 },
988 { 0x79, 0x10 }, { 0xc8, 0x7e },
989 { 0x79, 0x0a }, { 0xc8, 0x80 },
990 { 0x79, 0x0b }, { 0xc8, 0x01 },
991 { 0x79, 0x0c }, { 0xc8, 0x0f },
992 { 0x79, 0x0d }, { 0xc8, 0x20 },
993 { 0x79, 0x09 }, { 0xc8, 0x80 },
994 { 0x79, 0x02 }, { 0xc8, 0xc0 },
995 { 0x79, 0x03 }, { 0xc8, 0x40 },
996 { 0x79, 0x05 }, { 0xc8, 0x30 },
1001 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
1003 /* jfm:already done? */
1004 if (init_ov_sensor(sd) < 0)
1005 PDEBUG(D_ERR, "Failed to read sensor ID");
1007 PDEBUG(D_PROBE, "OV7xx0 initialized");
1009 /* Detect sensor (sub)type */
1010 rc = i2c_r(sd, OV7610_REG_COM_I);
1013 * it appears to be wrongly detected as a 7610 by default */
1015 PDEBUG(D_ERR, "Error detecting sensor type");
1018 if ((rc & 3) == 3) {
1019 /* quick hack to make OV7670s work */
1020 high = i2c_r(sd, 0x0a);
1021 low = i2c_r(sd, 0x0b);
1022 /* info("%x, %x", high, low); */
1023 if (high == 0x76 && low == 0x73) {
1024 PDEBUG(D_PROBE, "Sensor is an OV7670");
1025 sd->sensor = SEN_OV7670;
1027 PDEBUG(D_PROBE, "Sensor is an OV7610");
1028 sd->sensor = SEN_OV7610;
1030 } else if ((rc & 3) == 1) {
1031 /* I don't know what's different about the 76BE yet. */
1032 if (i2c_r(sd, 0x15) & 1)
1033 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
1035 PDEBUG(D_PROBE, "Sensor is an OV76BE");
1037 /* OV511+ will return all zero isoc data unless we
1038 * configure the sensor as a 7620. Someone needs to
1039 * find the exact reg. setting that causes this. */
1040 sd->sensor = SEN_OV76BE;
1041 } else if ((rc & 3) == 0) {
1042 /* try to read product id registers */
1043 high = i2c_r(sd, 0x0a);
1045 PDEBUG(D_ERR, "Error detecting camera chip PID");
1048 low = i2c_r(sd, 0x0b);
1050 PDEBUG(D_ERR, "Error detecting camera chip VER");
1055 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
1056 sd->sensor = SEN_OV7630;
1057 } else if (low == 0x40) {
1058 PDEBUG(D_PROBE, "Sensor is an OV7645");
1059 sd->sensor = SEN_OV7640; /* FIXME */
1060 } else if (low == 0x45) {
1061 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1062 sd->sensor = SEN_OV7640; /* FIXME */
1063 } else if (low == 0x48) {
1064 PDEBUG(D_PROBE, "Sensor is an OV7648");
1065 sd->sensor = SEN_OV7640; /* FIXME */
1067 PDEBUG(D_PROBE, "Unknown sensor: 0x76%X", low);
1071 PDEBUG(D_PROBE, "Sensor is an OV7620");
1072 sd->sensor = SEN_OV7620;
1075 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1079 if (sd->sensor == SEN_OV7620) {
1080 PDEBUG(D_PROBE, "Writing 7620 registers");
1081 if (write_i2c_regvals(sd, norm_7620,
1082 sizeof norm_7620 / sizeof norm_7620[0]))
1084 } else if (sd->sensor == SEN_OV7630) {
1085 PDEBUG(D_ERR, "7630 is not supported by this driver version");
1087 } else if (sd->sensor == SEN_OV7640) {
1088 PDEBUG(D_PROBE, "Writing 7640 registers");
1089 if (write_i2c_regvals(sd, norm_7640,
1090 sizeof norm_7640 / sizeof norm_7640[0]))
1092 } else if (sd->sensor == SEN_OV7670) {
1093 PDEBUG(D_PROBE, "Writing 7670 registers");
1094 if (write_i2c_regvals(sd, norm_7670,
1095 sizeof norm_7670 / sizeof norm_7670[0]))
1098 PDEBUG(D_PROBE, "Writing 7610 registers");
1099 if (write_i2c_regvals(sd, norm_7610,
1100 sizeof norm_7610 / sizeof norm_7610[0]))
1104 /* Set sensor-specific vars */
1106 sd->maxheight = 480;
1110 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1111 static int ov6xx0_configure(struct sd *sd)
1114 static const struct ov_i2c_regvals norm_6x20[] = {
1115 { 0x12, 0x80 }, /* reset */
1118 { 0x05, 0x7f }, /* For when autoadjust is off */
1120 /* The ratio of 0x0c and 0x0d controls the white point */
1123 { 0x0f, 0x15 }, /* COMS */
1124 { 0x10, 0x75 }, /* AEC Exposure time */
1125 { 0x12, 0x24 }, /* Enable AGC */
1127 /* 0x16: 0x06 helps frame stability with moving objects */
1129 /* { 0x20, 0x30 }, * Aperture correction enable */
1130 { 0x26, 0xb2 }, /* BLC enable */
1131 /* 0x28: 0x05 Selects RGB format if RGB on */
1133 { 0x2a, 0x04 }, /* Disable framerate adjust */
1134 /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
1136 { 0x33, 0xa0 }, /* Color Processing Parameter */
1137 { 0x34, 0xd2 }, /* Max A/D range */
1141 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1142 { 0x3c, 0x3c }, /* Change AEC mode */
1143 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1146 /* These next two registers (0x4a, 0x4b) are undocumented.
1147 * They control the color balance */
1150 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1153 /* Do 50-53 have any effect? */
1154 /* Toggle 0x12[2] off and on here? */
1157 static const struct ov_i2c_regvals norm_6x30[] = {
1158 { 0x12, 0x80 }, /* Reset */
1159 { 0x00, 0x1f }, /* Gain */
1160 { 0x01, 0x99 }, /* Blue gain */
1161 { 0x02, 0x7c }, /* Red gain */
1162 { 0x03, 0xc0 }, /* Saturation */
1163 { 0x05, 0x0a }, /* Contrast */
1164 { 0x06, 0x95 }, /* Brightness */
1165 { 0x07, 0x2d }, /* Sharpness */
1171 { 0x11, 0x00 }, /* Pixel clock = fastest */
1172 { 0x12, 0x24 }, /* Enable AGC and AWB */
1187 { 0x23, 0xc0 }, /* Crystal circuit power level */
1188 { 0x25, 0x9a }, /* Increase AEC black ratio */
1189 { 0x26, 0xb2 }, /* BLC enable */
1193 { 0x2a, 0x84 }, /* 60 Hz power */
1194 { 0x2b, 0xa8 }, /* 60 Hz power */
1196 { 0x2d, 0x95 }, /* Enable auto-brightness */
1210 { 0x40, 0x00 }, /* White bal */
1211 { 0x41, 0x00 }, /* White bal */
1213 { 0x43, 0x3f }, /* White bal */
1223 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1225 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1227 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1232 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1234 { 0x5b, 0x0f }, /* AWB chrominance levels */
1238 { 0x12, 0x20 }, /* Toggle AWB */
1242 PDEBUG(D_PROBE, "starting sensor configuration");
1244 if (init_ov_sensor(sd) < 0) {
1245 PDEBUG(D_ERR, "Failed to read sensor ID.");
1248 PDEBUG(D_PROBE, "OV6xx0 sensor detected");
1250 /* Detect sensor (sub)type */
1251 rc = i2c_r(sd, OV7610_REG_COM_I);
1253 PDEBUG(D_ERR, "Error detecting sensor type");
1257 /* Ugh. The first two bits are the version bits, but
1258 * the entire register value must be used. I guess OVT
1259 * underestimated how many variants they would make. */
1261 sd->sensor = SEN_OV6630;
1263 "WARNING: Sensor is an OV66308. Your camera may have");
1264 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1265 } else if (rc == 0x01) {
1266 sd->sensor = SEN_OV6620;
1267 PDEBUG(D_PROBE, "Sensor is an OV6620");
1268 } else if (rc == 0x02) {
1269 sd->sensor = SEN_OV6630;
1270 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
1271 } else if (rc == 0x03) {
1272 sd->sensor = SEN_OV6630;
1273 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
1274 } else if (rc == 0x90) {
1275 sd->sensor = SEN_OV6630;
1277 "WARNING: Sensor is an OV66307. Your camera may have");
1278 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1280 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1284 /* Set sensor-specific vars */
1286 sd->maxheight = 288;
1288 if (sd->sensor == SEN_OV6620) {
1289 PDEBUG(D_PROBE, "Writing 6x20 registers");
1290 if (write_i2c_regvals(sd, norm_6x20,
1291 sizeof norm_6x20 / sizeof norm_6x20[0]))
1294 PDEBUG(D_PROBE, "Writing 6x30 registers");
1295 if (write_i2c_regvals(sd, norm_6x30,
1296 sizeof norm_6x30 / sizeof norm_6x30[0]))
1302 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1303 static void ov51x_led_control(struct sd *sd, int on)
1305 PDEBUG(D_STREAM, "LED (%s)", on ? "on" : "off");
1307 /* if (sd->bridge == BRG_OV511PLUS) */
1308 /* reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0); */
1309 /* else if (sd->bridge == BRG_OV519) */
1310 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
1311 /* else if (sd->bclass == BCL_OV518) */
1312 /* reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02); */
1315 /* this function is called at probe time */
1316 static int sd_config(struct gspca_dev *gspca_dev,
1317 const struct usb_device_id *id)
1319 struct sd *sd = (struct sd *) gspca_dev;
1322 /* (from ov519_configure) */
1323 static const struct ov_regvals init_519[] = {
1324 { 0x5a, 0x6d }, /* EnableSystem */
1325 /* jfm trace usbsnoop3-1.txt */
1328 { 0x54, 0xff }, /* set bit2 to enable jpeg */
1332 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1333 * detection will fail. This deserves further investigation. */
1334 { OV519_GPIO_IO_CTRL0, 0xee },
1335 { 0x51, 0x0f }, /* SetUsbInit */
1338 /* windows reads 0x55 at this point*/
1341 if (write_regvals(sd, init_519, ARRAY_SIZE(init_519)))
1343 /* jfm: not seen in windows trace */
1344 if (ov519_init_compression(sd))
1346 ov51x_led_control(sd, 0); /* turn LED off */
1349 sd->primary_i2c_slave = OV7xx0_SID;
1350 if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1353 /* The OV519 must be more aggressive about sensor detection since
1354 * I2C write will never fail if the sensor is not present. We have
1355 * to try to initialize the sensor to detect its presence */
1356 if (init_ov_sensor(sd) < 0) {
1358 sd->primary_i2c_slave = OV6xx0_SID;
1359 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1362 if (init_ov_sensor(sd) < 0) {
1364 sd->primary_i2c_slave = OV8xx0_SID;
1365 if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1368 if (init_ov_sensor(sd) < 0) {
1370 "Can't determine sensor slave IDs");
1373 if (ov8xx0_configure(sd) < 0) {
1375 "Failed to configure OV8xx0 sensor");
1380 if (ov6xx0_configure(sd) < 0) {
1381 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1386 if (ov7xx0_configure(sd) < 0) {
1387 PDEBUG(D_ERR, "Failed to configure OV7xx0");
1392 cam = &gspca_dev->cam;
1393 cam->epaddr = OV511_ENDPOINT_ADDRESS;
1394 if (sd->maxwidth == 640) {
1395 cam->cam_mode = vga_mode;
1396 cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
1398 cam->cam_mode = sif_mode;
1399 cam->nmodes = sizeof sif_mode / sizeof sif_mode[0];
1401 sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
1402 sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
1403 sd->colors = sd_ctrls[SD_COLOR].qctrl.default_value;
1404 sd->hflip = HFLIP_DEF;
1405 sd->vflip = VFLIP_DEF;
1408 PDEBUG(D_ERR, "OV519 Config failed");
1412 /* this function is called at open time */
1413 static int sd_open(struct gspca_dev *gspca_dev)
1418 /* Sets up the OV519 with the given image parameters
1420 * OV519 needs a completely different approach, until we can figure out what
1421 * the individual registers do.
1423 * Do not put any sensor-specific code in here (including I2C I/O functions)
1425 static int ov519_mode_init_regs(struct sd *sd,
1426 int width, int height)
1428 static const struct ov_regvals mode_init_519_ov7670[] = {
1429 { 0x5d, 0x03 }, /* Turn off suspend mode */
1430 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1431 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1432 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1436 { 0x37, 0x00 }, /* SetUsbInit */
1437 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1438 /* Enable both fields, YUV Input, disable defect comp (why?) */
1442 { 0x17, 0x50 }, /* undocumented */
1443 { 0x37, 0x00 }, /* undocumented */
1444 { 0x40, 0xff }, /* I2C timeout counter */
1445 { 0x46, 0x00 }, /* I2C clock prescaler */
1446 { 0x59, 0x04 }, /* new from windrv 090403 */
1447 { 0xff, 0x00 }, /* undocumented */
1448 /* windows reads 0x55 at this point, why? */
1451 static const struct ov_regvals mode_init_519[] = {
1452 { 0x5d, 0x03 }, /* Turn off suspend mode */
1453 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1454 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1455 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1459 { 0x37, 0x00 }, /* SetUsbInit */
1460 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1461 /* Enable both fields, YUV Input, disable defect comp (why?) */
1463 { 0x17, 0x50 }, /* undocumented */
1464 { 0x37, 0x00 }, /* undocumented */
1465 { 0x40, 0xff }, /* I2C timeout counter */
1466 { 0x46, 0x00 }, /* I2C clock prescaler */
1467 { 0x59, 0x04 }, /* new from windrv 090403 */
1468 { 0xff, 0x00 }, /* undocumented */
1469 /* windows reads 0x55 at this point, why? */
1474 PDEBUG(D_CONF, "mode init %dx%d", width, height);
1476 /* if (width >= 800 && height >= 600)
1481 /* if (ov51x_stop(sd) < 0)
1484 /******** Set the mode ********/
1485 if (sd->sensor != SEN_OV7670) {
1486 if (write_regvals(sd, mode_init_519,
1487 ARRAY_SIZE(mode_init_519)))
1490 if (write_regvals(sd, mode_init_519_ov7670,
1491 ARRAY_SIZE(mode_init_519_ov7670)))
1495 if (sd->sensor == SEN_OV7640) {
1496 /* Select 8-bit input mode */
1497 reg_w_mask(sd, OV519_CAM_DFR, 0x10, 0x10);
1500 reg_w(sd, OV519_CAM_H_SIZE, width >> 4);
1501 reg_w(sd, OV519_CAM_V_SIZE, height >> 3);
1502 reg_w(sd, OV519_CAM_X_OFFSETL, 0x00);
1503 reg_w(sd, OV519_CAM_X_OFFSETH, 0x00);
1504 reg_w(sd, OV519_CAM_Y_OFFSETL, 0x00);
1505 reg_w(sd, OV519_CAM_Y_OFFSETH, 0x00);
1506 reg_w(sd, OV519_CAM_DIVIDER, 0x00);
1507 reg_w(sd, OV519_CAM_FORMAT, 0x03); /* YUV422 */
1508 reg_w(sd, 0x26, 0x00); /* Undocumented */
1510 /******** Set the framerate ********/
1512 sd->frame_rate = frame_rate;
1514 /* FIXME: These are only valid at the max resolution. */
1516 if (sd->sensor == SEN_OV7640) {
1517 switch (sd->frame_rate) {
1518 /*jfm: default was 30 fps */
1520 reg_w(sd, 0xa4, 0x0c);
1521 reg_w(sd, 0x23, 0xff);
1524 reg_w(sd, 0xa4, 0x0c);
1525 reg_w(sd, 0x23, 0x1f);
1528 reg_w(sd, 0xa4, 0x0c);
1529 reg_w(sd, 0x23, 0x1b);
1533 reg_w(sd, 0xa4, 0x04);
1534 reg_w(sd, 0x23, 0xff);
1538 reg_w(sd, 0xa4, 0x04);
1539 reg_w(sd, 0x23, 0x1f);
1543 reg_w(sd, 0xa4, 0x04);
1544 reg_w(sd, 0x23, 0x1b);
1548 } else if (sd->sensor == SEN_OV8610) {
1549 switch (sd->frame_rate) {
1550 default: /* 15 fps */
1552 reg_w(sd, 0xa4, 0x06);
1553 reg_w(sd, 0x23, 0xff);
1556 reg_w(sd, 0xa4, 0x06);
1557 reg_w(sd, 0x23, 0x1f);
1560 reg_w(sd, 0xa4, 0x06);
1561 reg_w(sd, 0x23, 0x1b);
1565 } else if (sd->sensor == SEN_OV7670) { /* guesses, based on 7640 */
1566 PDEBUG(D_STREAM, "Setting framerate to %d fps",
1567 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
1568 switch (sd->frame_rate) {
1570 reg_w(sd, 0xa4, 0x10);
1571 reg_w(sd, 0x23, 0xff);
1574 reg_w(sd, 0xa4, 0x10);
1575 reg_w(sd, 0x23, 0x1b);
1577 default: /* 15 fps */
1579 reg_w(sd, 0xa4, 0x10);
1580 reg_w(sd, 0x23, 0xff);
1586 /* if (ov51x_restart(sd) < 0)
1589 /* Reset it just for good measure */
1590 /* if (ov51x_reset(sd, OV511_RESET_NOREGS) < 0)
1595 static int mode_init_ov_sensor_regs(struct sd *sd,
1596 struct ovsensor_window *win)
1598 int qvga = win->quarter;
1600 /******** Mode (VGA/QVGA) and sensor specific regs ********/
1601 switch (sd->sensor) {
1603 /* For OV8610 qvga means qsvga */
1604 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
1607 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1610 /* i2c_w(sd, 0x2b, 0x00); */
1611 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1612 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1613 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
1614 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
1615 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
1616 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
1617 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
1620 /* i2c_w(sd, 0x2b, 0x00); */
1621 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1624 /* i2c_w(sd, 0x2b, 0x00); */
1625 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1626 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1627 /* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
1628 /* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
1629 /* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
1630 /* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
1631 /* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
1634 /* set COM7_FMT_VGA or COM7_FMT_QVGA
1635 * do we need to set anything else?
1636 * HSTART etc are set in set_ov_sensor_window itself */
1637 i2c_w_mask(sd, OV7670_REG_COM7,
1638 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
1639 OV7670_COM7_FMT_MASK);
1642 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1645 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1651 /******** Palette-specific regs ********/
1652 /* Need to do work here for the OV7670 */
1654 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1655 /* not valid on the OV6620/OV7620/6630? */
1656 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
1659 /* The OV518 needs special treatment. Although both the OV518
1660 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
1661 * bus is actually used. The UV bus is tied to ground.
1662 * Therefore, the OV6630 needs to be in 8-bit multiplexed
1665 /* OV7640 is 8-bit only */
1667 if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV7640)
1668 i2c_w_mask(sd, 0x13, 0x00, 0x20);
1671 /******** Clock programming ********/
1672 /* The OV6620 needs special handling. This prevents the
1673 * severe banding that normally occurs */
1674 if (sd->sensor == SEN_OV6620) {
1677 i2c_w(sd, 0x2a, 0x04);
1678 i2c_w(sd, 0x11, win->clockdiv);
1679 i2c_w(sd, 0x2a, 0x84);
1680 /* This next setting is critical. It seems to improve
1681 * the gain or the contrast. The "reserved" bits seem
1682 * to have some effect in this case. */
1683 i2c_w(sd, 0x2d, 0x85);
1684 } else if (win->clockdiv >= 0) {
1685 i2c_w(sd, 0x11, win->clockdiv);
1688 /******** Special Features ********/
1689 /* no evidence this is possible with OV7670, either */
1691 if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
1692 i2c_w_mask(sd, 0x12, 0x00, 0x02);
1694 /* Enable auto white balance */
1695 if (sd->sensor == SEN_OV7670)
1696 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
1699 i2c_w_mask(sd, 0x12, 0x04, 0x04);
1701 /* This will go away as soon as ov51x_mode_init_sensor_regs() */
1702 /* is fully tested. */
1703 /* 7620/6620/6630? don't have register 0x35, so play it safe */
1704 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1705 if (win->width == 640 /*&& win->height == 480*/)
1706 i2c_w(sd, 0x35, 0x9e);
1708 i2c_w(sd, 0x35, 0x1e);
1713 static void sethflip(struct sd *sd)
1715 if (sd->gspca_dev.streaming)
1717 i2c_w_mask(sd, OV7670_REG_MVFP,
1718 OV7670_MVFP_MIRROR * sd->hflip, OV7670_MVFP_MIRROR);
1719 if (sd->gspca_dev.streaming)
1723 static void setvflip(struct sd *sd)
1725 if (sd->gspca_dev.streaming)
1727 i2c_w_mask(sd, OV7670_REG_MVFP,
1728 OV7670_MVFP_VFLIP * sd->vflip, OV7670_MVFP_VFLIP);
1729 if (sd->gspca_dev.streaming)
1733 static int set_ov_sensor_window(struct sd *sd,
1734 struct ovsensor_window *win)
1736 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
1737 int ret, hstart, hstop, vstop, vstart;
1740 /* The different sensor ICs handle setting up of window differently.
1741 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
1742 switch (sd->sensor) {
1753 vwsbase = vwebase = 0x05;
1763 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
1765 vwsbase = vwebase = 0x05;
1770 vwsbase = vwebase = 0x03;
1773 /*handling of OV7670 hardware sensor start and stop values
1774 * is very odd, compared to the other OV sensors */
1775 vwsbase = vwebase = hwebase = hwsbase = 0x00;
1781 switch (sd->sensor) {
1784 if (win->quarter) { /* QCIF */
1789 vwscale = 1; /* The datasheet says 0;
1794 if (win->quarter) { /* QSVGA */
1802 default: /* SEN_OV7xx0 */
1803 if (win->quarter) { /* QVGA */
1812 ret = mode_init_ov_sensor_regs(sd, win);
1816 if (sd->sensor == SEN_OV8610) {
1817 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
1818 /* old 0x95, new 0x05 from windrv 090403 */
1819 /* bits 5-7: reserved */
1820 i2c_w_mask(sd, 0x28, 0x20, 0x20);
1821 /* bit 5: progressive mode on */
1824 /* The below is wrong for OV7670s because their window registers
1825 * only store the high bits in 0x17 to 0x1a */
1827 /* SRH Use sd->max values instead of requested win values */
1828 /* SCS Since we're sticking with only the max hardware widths
1829 * for a given mode */
1830 /* I can hard code this for OV7670s */
1831 /* Yes, these numbers do look odd, but they're tested and work! */
1832 if (sd->sensor == SEN_OV7670) {
1833 if (win->quarter) { /* QVGA from ov7670.c by
1834 * Jonathan Corbet */
1845 /* OV7670 hardware window registers are split across
1846 * multiple locations */
1847 i2c_w(sd, OV7670_REG_HSTART, (hstart >> 3) & 0xff);
1848 i2c_w(sd, OV7670_REG_HSTOP, (hstop >> 3) & 0xff);
1849 v = i2c_r(sd, OV7670_REG_HREF);
1850 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
1851 msleep(10); /* need to sleep between read and write to
1853 i2c_w(sd, OV7670_REG_HREF, v);
1855 i2c_w(sd, OV7670_REG_VSTART, (vstart >> 2) & 0xff);
1856 i2c_w(sd, OV7670_REG_VSTOP, (vstop >> 2) & 0xff);
1857 v = i2c_r(sd, OV7670_REG_VREF);
1858 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
1859 msleep(10); /* need to sleep between read and write to
1861 i2c_w(sd, OV7670_REG_VREF, v);
1865 i2c_w(sd, 0x17, hwsbase + (win->x >> hwscale));
1866 i2c_w(sd, 0x18, hwebase + ((win->x + win->width) >> hwscale));
1867 i2c_w(sd, 0x19, vwsbase + (win->y >> vwscale));
1868 i2c_w(sd, 0x1a, vwebase + ((win->y + win->height) >> vwscale));
1873 static int ov_sensor_mode_setup(struct sd *sd,
1874 int width, int height)
1876 struct ovsensor_window win;
1878 /* win.format = mode; */
1880 /* Unless subcapture is enabled,
1881 * center the image window and downsample
1882 * if possible to increase the field of view */
1883 /* NOTE: OV518(+) and OV519 does downsampling on its own */
1885 win.height = height;
1886 if (width == sd->maxwidth)
1892 win.x = (win.width - width) / 2;
1893 win.y = (win.height - height) / 2;
1895 /* Clock is determined by OV519 frame rate code */
1896 win.clockdiv = sd->clockdiv;
1898 PDEBUG(D_CONF, "Setting clock divider to %d", win.clockdiv);
1899 return set_ov_sensor_window(sd, &win);
1902 /* -- start the camera -- */
1903 static void sd_start(struct gspca_dev *gspca_dev)
1905 struct sd *sd = (struct sd *) gspca_dev;
1909 ret = ov519_mode_init_regs(sd, gspca_dev->width, gspca_dev->height);
1912 ret = ov_sensor_mode_setup(sd, gspca_dev->width, gspca_dev->height);
1916 ret = ov51x_restart((struct sd *) gspca_dev);
1919 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
1920 ov51x_led_control(sd, 1);
1923 PDEBUG(D_ERR, "camera start error:%d", ret);
1926 static void sd_stopN(struct gspca_dev *gspca_dev)
1928 ov51x_stop((struct sd *) gspca_dev);
1929 ov51x_led_control((struct sd *) gspca_dev, 0);
1932 static void sd_stop0(struct gspca_dev *gspca_dev)
1936 static void sd_close(struct gspca_dev *gspca_dev)
1940 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1941 struct gspca_frame *frame, /* target */
1942 __u8 *data, /* isoc packet */
1943 int len) /* iso packet length */
1945 /* Header of ov519 is 16 bytes:
1946 * Byte Value Description
1950 * 3 0xXX 0x50 = SOF, 0x51 = EOF
1951 * 9 0xXX 0x01 initial frame without data,
1952 * 0x00 standard frame with image
1953 * 14 Lo in EOF: length of image data / 8
1957 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
1959 case 0x50: /* start of frame */
1964 if (data[0] == 0xff || data[1] == 0xd8)
1965 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
1968 gspca_dev->last_packet_type = DISCARD_PACKET;
1970 case 0x51: /* end of frame */
1972 gspca_dev->last_packet_type = DISCARD_PACKET;
1973 gspca_frame_add(gspca_dev, LAST_PACKET, frame,
1979 /* intermediate packet */
1980 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
1984 /* -- management routines -- */
1986 static void setbrightness(struct gspca_dev *gspca_dev)
1988 struct sd *sd = (struct sd *) gspca_dev;
1990 /* int was_streaming; */
1992 val = sd->brightness;
1993 PDEBUG(D_CONF, "brightness:%d", val);
1994 /* was_streaming = gspca_dev->streaming;
1995 * if (was_streaming)
1996 * ov51x_stop(sd); */
1997 switch (sd->sensor) {
2004 i2c_w(sd, OV7610_REG_BRT, val);
2007 /* 7620 doesn't like manual changes when in auto mode */
2009 * if (!sd->auto_brt) */
2010 i2c_w(sd, OV7610_REG_BRT, val);
2013 /*jfm - from windblows
2014 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
2015 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
2018 /* if (was_streaming)
2019 * ov51x_restart(sd); */
2022 static void setcontrast(struct gspca_dev *gspca_dev)
2024 struct sd *sd = (struct sd *) gspca_dev;
2026 /* int was_streaming; */
2029 PDEBUG(D_CONF, "contrast:%d", val);
2030 /* was_streaming = gspca_dev->streaming;
2033 switch (sd->sensor) {
2036 i2c_w(sd, OV7610_REG_CNT, val);
2039 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
2041 static const __u8 ctab[] = {
2042 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
2045 /* Use Y gamma control instead. Bit 0 enables it. */
2046 i2c_w(sd, 0x64, ctab[val >> 5]);
2050 static const __u8 ctab[] = {
2051 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
2052 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
2055 /* Use Y gamma control instead. Bit 0 enables it. */
2056 i2c_w(sd, 0x64, ctab[val >> 4]);
2060 /* Use gain control instead. */
2061 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
2064 /* check that this isn't just the same as ov7610 */
2065 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
2068 /* if (was_streaming)
2069 ov51x_restart(sd); */
2072 static void setcolors(struct gspca_dev *gspca_dev)
2074 struct sd *sd = (struct sd *) gspca_dev;
2076 /* int was_streaming; */
2079 PDEBUG(D_CONF, "saturation:%d", val);
2080 /* was_streaming = gspca_dev->streaming;
2083 switch (sd->sensor) {
2089 i2c_w(sd, OV7610_REG_SAT, val);
2092 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
2093 /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
2096 i2c_w(sd, OV7610_REG_SAT, val);
2099 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
2102 /* supported later once I work out how to do it
2103 * transparently fail now! */
2104 /* set REG_COM13 values for UV sat auto mode */
2107 /* if (was_streaming)
2108 ov51x_restart(sd); */
2111 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2113 struct sd *sd = (struct sd *) gspca_dev;
2115 sd->brightness = val;
2116 setbrightness(gspca_dev);
2120 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2122 struct sd *sd = (struct sd *) gspca_dev;
2124 *val = sd->brightness;
2128 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2130 struct sd *sd = (struct sd *) gspca_dev;
2133 setcontrast(gspca_dev);
2137 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2139 struct sd *sd = (struct sd *) gspca_dev;
2141 *val = sd->contrast;
2145 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2147 struct sd *sd = (struct sd *) gspca_dev;
2150 setcolors(gspca_dev);
2154 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2156 struct sd *sd = (struct sd *) gspca_dev;
2162 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
2164 struct sd *sd = (struct sd *) gspca_dev;
2171 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
2173 struct sd *sd = (struct sd *) gspca_dev;
2179 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2181 struct sd *sd = (struct sd *) gspca_dev;
2188 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2190 struct sd *sd = (struct sd *) gspca_dev;
2196 /* sub-driver description */
2197 static const struct sd_desc sd_desc = {
2198 .name = MODULE_NAME,
2200 .nctrls = ARRAY_SIZE(sd_ctrls),
2201 .config = sd_config,
2207 .pkt_scan = sd_pkt_scan,
2210 /* -- module initialisation -- */
2211 static const __devinitdata struct usb_device_id device_table[] = {
2212 {USB_DEVICE(0x041e, 0x4052)},
2213 {USB_DEVICE(0x041e, 0x405f)},
2214 {USB_DEVICE(0x041e, 0x4060)},
2215 {USB_DEVICE(0x041e, 0x4061)},
2216 {USB_DEVICE(0x041e, 0x4064)},
2217 {USB_DEVICE(0x041e, 0x4068)},
2218 {USB_DEVICE(0x045e, 0x028c)},
2219 {USB_DEVICE(0x054c, 0x0154)},
2220 {USB_DEVICE(0x054c, 0x0155)},
2221 {USB_DEVICE(0x05a9, 0x0519)},
2222 {USB_DEVICE(0x05a9, 0x0530)},
2223 {USB_DEVICE(0x05a9, 0x4519)},
2224 {USB_DEVICE(0x05a9, 0x8519)},
2228 MODULE_DEVICE_TABLE(usb, device_table);
2230 /* -- device connect -- */
2231 static int sd_probe(struct usb_interface *intf,
2232 const struct usb_device_id *id)
2234 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2238 static struct usb_driver sd_driver = {
2239 .name = MODULE_NAME,
2240 .id_table = device_table,
2242 .disconnect = gspca_disconnect,
2245 /* -- module insert / remove -- */
2246 static int __init sd_mod_init(void)
2248 if (usb_register(&sd_driver) < 0)
2250 PDEBUG(D_PROBE, "registered");
2253 static void __exit sd_mod_exit(void)
2255 usb_deregister(&sd_driver);
2256 PDEBUG(D_PROBE, "deregistered");
2259 module_init(sd_mod_init);
2260 module_exit(sd_mod_exit);
2262 module_param(frame_rate, int, 0644);
2263 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");