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 */
45 unsigned char primary_i2c_slave; /* I2C write id of sensor */
47 unsigned char brightness;
48 unsigned char contrast;
53 char compress; /* Should the next frame be compressed? */
54 char compress_inited; /* Are compression params uploaded? */
55 char stopped; /* Streaming is temporarily paused */
57 char frame_rate; /* current Framerate (OV519 only) */
58 char clockdiv; /* clockdiv override for OV519 only */
60 char sensor; /* Type of image sensor chip (SEN_*) */
74 /* V4L2 controls supported by the driver */
75 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
76 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
77 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
78 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
79 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
80 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
81 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
82 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
83 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
84 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
86 static struct ctrl sd_ctrls[] = {
89 .id = V4L2_CID_BRIGHTNESS,
90 .type = V4L2_CTRL_TYPE_INTEGER,
95 #define BRIGHTNESS_DEF 127
96 .default_value = BRIGHTNESS_DEF,
98 .set = sd_setbrightness,
99 .get = sd_getbrightness,
103 .id = V4L2_CID_CONTRAST,
104 .type = V4L2_CTRL_TYPE_INTEGER,
109 #define CONTRAST_DEF 127
110 .default_value = CONTRAST_DEF,
112 .set = sd_setcontrast,
113 .get = sd_getcontrast,
117 .id = V4L2_CID_SATURATION,
118 .type = V4L2_CTRL_TYPE_INTEGER,
123 #define COLOR_DEF 127
124 .default_value = COLOR_DEF,
129 /* next controls work with ov7670 only */
132 .id = V4L2_CID_HFLIP,
133 .type = V4L2_CTRL_TYPE_BOOLEAN,
139 .default_value = HFLIP_DEF,
146 .id = V4L2_CID_VFLIP,
147 .type = V4L2_CTRL_TYPE_BOOLEAN,
153 .default_value = VFLIP_DEF,
160 static struct v4l2_pix_format vga_mode[] = {
161 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
163 .sizeimage = 320 * 240 * 3 / 8 + 590,
164 .colorspace = V4L2_COLORSPACE_JPEG,
166 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
168 .sizeimage = 640 * 480 * 3 / 8 + 590,
169 .colorspace = V4L2_COLORSPACE_JPEG,
172 static struct v4l2_pix_format sif_mode[] = {
173 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
175 .sizeimage = 176 * 144 * 3 / 8 + 590,
176 .colorspace = V4L2_COLORSPACE_JPEG,
178 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
180 .sizeimage = 352 * 288 * 3 / 8 + 590,
181 .colorspace = V4L2_COLORSPACE_JPEG,
185 /* OV519 Camera interface register numbers */
186 #define OV519_CAM_H_SIZE 0x10
187 #define OV519_CAM_V_SIZE 0x11
188 #define OV519_CAM_X_OFFSETL 0x12
189 #define OV519_CAM_X_OFFSETH 0x13
190 #define OV519_CAM_Y_OFFSETL 0x14
191 #define OV519_CAM_Y_OFFSETH 0x15
192 #define OV519_CAM_DIVIDER 0x16
193 #define OV519_CAM_DFR 0x20
194 #define OV519_CAM_FORMAT 0x25
196 /* OV519 System Controller register numbers */
197 #define OV519_SYS_RESET1 0x51
198 #define OV519_SYS_EN_CLK1 0x54
200 #define OV519_GPIO_DATA_OUT0 0x71
201 #define OV519_GPIO_IO_CTRL0 0x72
203 #define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
206 #define R51x_I2C_W_SID 0x41
207 #define R51x_I2C_SADDR_3 0x42
208 #define R51x_I2C_SADDR_2 0x43
209 #define R51x_I2C_R_SID 0x44
210 #define R51x_I2C_DATA 0x45
211 #define R518_I2C_CTL 0x47 /* OV518(+) only */
214 #define OV7xx0_SID 0x42
215 #define OV8xx0_SID 0xa0
216 #define OV6xx0_SID 0xc0
218 /* OV7610 registers */
219 #define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
220 #define OV7610_REG_SAT 0x03 /* saturation */
221 #define OV8610_REG_HUE 0x04 /* 04 reserved */
222 #define OV7610_REG_CNT 0x05 /* Y contrast */
223 #define OV7610_REG_BRT 0x06 /* Y brightness */
224 #define OV7610_REG_COM_C 0x14 /* misc common regs */
225 #define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
226 #define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
227 #define OV7610_REG_COM_I 0x29 /* misc settings */
229 /* OV7670 registers */
230 #define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
231 #define OV7670_REG_BLUE 0x01 /* blue gain */
232 #define OV7670_REG_RED 0x02 /* red gain */
233 #define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
234 #define OV7670_REG_COM1 0x04 /* Control 1 */
235 #define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
236 #define OV7670_REG_COM3 0x0c /* Control 3 */
237 #define OV7670_REG_COM4 0x0d /* Control 4 */
238 #define OV7670_REG_COM5 0x0e /* All "reserved" */
239 #define OV7670_REG_COM6 0x0f /* Control 6 */
240 #define OV7670_REG_AECH 0x10 /* More bits of AEC value */
241 #define OV7670_REG_CLKRC 0x11 /* Clock control */
242 #define OV7670_REG_COM7 0x12 /* Control 7 */
243 #define OV7670_COM7_FMT_VGA 0x00
244 #define OV7670_COM7_YUV 0x00 /* YUV */
245 #define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
246 #define OV7670_COM7_FMT_MASK 0x38
247 #define OV7670_COM7_RESET 0x80 /* Register reset */
248 #define OV7670_REG_COM8 0x13 /* Control 8 */
249 #define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
250 #define OV7670_COM8_AWB 0x02 /* White balance enable */
251 #define OV7670_COM8_AGC 0x04 /* Auto gain enable */
252 #define OV7670_COM8_BFILT 0x20 /* Band filter enable */
253 #define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
254 #define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
255 #define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
256 #define OV7670_REG_COM10 0x15 /* Control 10 */
257 #define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
258 #define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
259 #define OV7670_REG_VSTART 0x19 /* Vert start high bits */
260 #define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
261 #define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
262 #define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
263 #define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
264 #define OV7670_REG_AEW 0x24 /* AGC upper limit */
265 #define OV7670_REG_AEB 0x25 /* AGC lower limit */
266 #define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
267 #define OV7670_REG_HREF 0x32 /* HREF pieces */
268 #define OV7670_REG_TSLB 0x3a /* lots of stuff */
269 #define OV7670_REG_COM11 0x3b /* Control 11 */
270 #define OV7670_COM11_EXP 0x02
271 #define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
272 #define OV7670_REG_COM12 0x3c /* Control 12 */
273 #define OV7670_REG_COM13 0x3d /* Control 13 */
274 #define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
275 #define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
276 #define OV7670_REG_COM14 0x3e /* Control 14 */
277 #define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
278 #define OV7670_REG_COM15 0x40 /* Control 15 */
279 #define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
280 #define OV7670_REG_COM16 0x41 /* Control 16 */
281 #define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
282 #define OV7670_REG_BRIGHT 0x55 /* Brightness */
283 #define OV7670_REG_CONTRAS 0x56 /* Contrast control */
284 #define OV7670_REG_GFIX 0x69 /* Fix gain control */
285 #define OV7670_REG_RGB444 0x8c /* RGB 444 control */
286 #define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
287 #define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
288 #define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
289 #define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
290 #define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
291 #define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
292 #define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
293 #define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
294 #define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
296 static unsigned char ov7670_abs_to_sm(unsigned char v)
300 return (128 - v) | 0x80;
303 /* Write a OV519 register */
304 static int reg_w(struct sd *sd, __u16 index, __u8 value)
308 sd->gspca_dev.usb_buf[0] = value;
309 ret = usb_control_msg(sd->gspca_dev.dev,
310 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
311 1, /* REQ_IO (ov518/519) */
312 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
314 sd->gspca_dev.usb_buf, 1, 500);
316 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
320 /* Read from a OV519 register */
321 /* returns: negative is error, pos or zero is data */
322 static int reg_r(struct sd *sd, __u16 index)
326 ret = usb_control_msg(sd->gspca_dev.dev,
327 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
329 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
330 0, index, sd->gspca_dev.usb_buf, 1, 500);
333 ret = sd->gspca_dev.usb_buf[0];
335 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
339 /* Read 8 values from a OV519 register */
340 static int reg_r8(struct sd *sd,
345 ret = usb_control_msg(sd->gspca_dev.dev,
346 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
348 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
349 0, index, sd->gspca_dev.usb_buf, 8, 500);
352 ret = sd->gspca_dev.usb_buf[0];
354 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
359 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
360 * the same position as 1's in "mask" are cleared and set to "value". Bits
361 * that are in the same position as 0's in "mask" are preserved, regardless
362 * of their respective state in "value".
364 static int reg_w_mask(struct sd *sd,
373 value &= mask; /* Enforce mask on value */
374 ret = reg_r(sd, index);
378 oldval = ret & ~mask; /* Clear the masked bits */
379 value |= oldval; /* Set the desired bits */
381 return reg_w(sd, index, value);
385 * The OV518 I2C I/O procedure is different, hence, this function.
386 * This is normally only called from i2c_w(). Note that this function
387 * always succeeds regardless of whether the sensor is present and working.
389 static int i2c_w(struct sd *sd,
395 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
397 /* Select camera register */
398 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
402 /* Write "value" to I2C data port of OV511 */
403 rc = reg_w(sd, R51x_I2C_DATA, value);
407 /* Initiate 3-byte write cycle */
408 rc = reg_w(sd, R518_I2C_CTL, 0x01);
410 /* wait for write complete */
414 return reg_r8(sd, R518_I2C_CTL);
418 * returns: negative is error, pos or zero is data
420 * The OV518 I2C I/O procedure is different, hence, this function.
421 * This is normally only called from i2c_r(). Note that this function
422 * always succeeds regardless of whether the sensor is present and working.
424 static int i2c_r(struct sd *sd, __u8 reg)
428 /* Select camera register */
429 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
433 /* Initiate 2-byte write cycle */
434 rc = reg_w(sd, R518_I2C_CTL, 0x03);
438 /* Initiate 2-byte read cycle */
439 rc = reg_w(sd, R518_I2C_CTL, 0x05);
442 value = reg_r(sd, R51x_I2C_DATA);
443 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
447 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
448 * the same position as 1's in "mask" are cleared and set to "value". Bits
449 * that are in the same position as 0's in "mask" are preserved, regardless
450 * of their respective state in "value".
452 static int i2c_w_mask(struct sd *sd,
460 value &= mask; /* Enforce mask on value */
464 oldval = rc & ~mask; /* Clear the masked bits */
465 value |= oldval; /* Set the desired bits */
466 return i2c_w(sd, reg, value);
469 /* Temporarily stops OV511 from functioning. Must do this before changing
470 * registers while the camera is streaming */
471 static inline int ov51x_stop(struct sd *sd)
473 PDEBUG(D_STREAM, "stopping");
475 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
478 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
479 * actually stopped (for performance). */
480 static inline int ov51x_restart(struct sd *sd)
482 PDEBUG(D_STREAM, "restarting");
487 /* Reinitialize the stream */
488 return reg_w(sd, OV519_SYS_RESET1, 0x00);
491 /* This does an initial reset of an OmniVision sensor and ensures that I2C
492 * is synchronized. Returns <0 on failure.
494 static int init_ov_sensor(struct sd *sd)
498 /* Reset the sensor */
499 if (i2c_w(sd, 0x12, 0x80) < 0)
502 /* Wait for it to initialize */
505 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
506 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
507 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
512 /* Reset the sensor */
513 if (i2c_w(sd, 0x12, 0x80) < 0)
515 /* Wait for it to initialize */
517 /* Dummy read to sync I2C */
518 if (i2c_r(sd, 0x00) < 0)
523 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
527 /* Set the read and write slave IDs. The "slave" argument is the write slave,
528 * and the read slave will be set to (slave + 1).
529 * This should not be called from outside the i2c I/O functions.
530 * Sets I2C read and write slave IDs. Returns <0 for error
532 static int ov51x_set_slave_ids(struct sd *sd,
537 rc = reg_w(sd, R51x_I2C_W_SID, slave);
540 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
547 struct ov_i2c_regvals {
552 static int write_regvals(struct sd *sd,
553 const struct ov_regvals *regvals,
559 rc = reg_w(sd, regvals->reg, regvals->val);
567 static int write_i2c_regvals(struct sd *sd,
568 const struct ov_i2c_regvals *regvals,
574 rc = i2c_w(sd, regvals->reg, regvals->val);
582 /****************************************************************************
584 * OV511 and sensor configuration
586 ***************************************************************************/
588 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
589 * the same register settings as the OV8610, since they are very similar.
591 static int ov8xx0_configure(struct sd *sd)
594 static const struct ov_i2c_regvals norm_8610[] = {
601 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
602 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
611 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
613 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
614 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
615 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
618 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
619 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
620 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
621 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
627 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
629 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
631 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
633 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
634 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
635 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
636 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
638 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
639 * maybe thats wrong */
643 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
647 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
648 deleting bit7 colors the first images red */
649 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
650 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
656 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
658 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
663 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
665 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
666 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
673 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
679 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
682 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
684 if (init_ov_sensor(sd) < 0)
685 PDEBUG(D_ERR|D_PROBE, "Failed to read sensor ID");
687 PDEBUG(D_PROBE, "OV86x0 initialized");
689 /* Detect sensor (sub)type */
690 rc = i2c_r(sd, OV7610_REG_COM_I);
692 PDEBUG(D_ERR, "Error detecting sensor type");
696 sd->sensor = SEN_OV8610;
698 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
701 PDEBUG(D_PROBE, "Writing 8610 registers");
702 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
705 /* Set sensor-specific vars */
706 /* sd->sif = 0; already done */
710 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
711 * the same register settings as the OV7610, since they are very similar.
713 static int ov7xx0_configure(struct sd *sd)
717 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
719 * Register 0x0f in the 7610 has the following effects:
721 * 0x85 (AEC method 1): Best overall, good contrast range
722 * 0x45 (AEC method 2): Very overexposed
723 * 0xa5 (spec sheet default): Ok, but the black level is
724 * shifted resulting in loss of contrast
725 * 0x05 (old driver setting): very overexposed, too much
728 static const struct ov_i2c_regvals norm_7610[] = {
735 { 0x28, 0x24 }, /* 0c */
736 { 0x0f, 0x85 }, /* lg's setting */
758 static const struct ov_i2c_regvals norm_7620[] = {
759 { 0x00, 0x00 }, /* gain */
760 { 0x01, 0x80 }, /* blue gain */
761 { 0x02, 0x80 }, /* red gain */
762 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
824 /* 7640 and 7648. The defaults should be OK for most registers. */
825 static const struct ov_i2c_regvals norm_7640[] = {
830 /* 7670. Defaults taken from OmniVision provided data,
831 * as provided by Jonathan Corbet of OLPC */
832 static const struct ov_i2c_regvals norm_7670[] = {
833 { OV7670_REG_COM7, OV7670_COM7_RESET },
834 { OV7670_REG_TSLB, 0x04 }, /* OV */
835 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
836 { OV7670_REG_CLKRC, 0x01 },
838 * Set the hardware window. These values from OV don't entirely
839 * make sense - hstop is less than hstart. But they work...
841 { OV7670_REG_HSTART, 0x13 }, { OV7670_REG_HSTOP, 0x01 },
842 { OV7670_REG_HREF, 0xb6 }, { OV7670_REG_VSTART, 0x02 },
843 { OV7670_REG_VSTOP, 0x7a }, { OV7670_REG_VREF, 0x0a },
845 { OV7670_REG_COM3, 0 }, { OV7670_REG_COM14, 0 },
846 /* Mystery scaling numbers */
847 { 0x70, 0x3a }, { 0x71, 0x35 },
848 { 0x72, 0x11 }, { 0x73, 0xf0 },
850 /* { OV7670_REG_COM10, 0x0 }, */
852 /* Gamma curve values */
857 { 0x7e, 0x5a }, { 0x7f, 0x69 },
858 { 0x80, 0x76 }, { 0x81, 0x80 },
859 { 0x82, 0x88 }, { 0x83, 0x8f },
860 { 0x84, 0x96 }, { 0x85, 0xa3 },
861 { 0x86, 0xaf }, { 0x87, 0xc4 },
862 { 0x88, 0xd7 }, { 0x89, 0xe8 },
864 /* AGC and AEC parameters. Note we start by disabling those features,
865 then turn them only after tweaking the values. */
866 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
867 | OV7670_COM8_AECSTEP
868 | OV7670_COM8_BFILT },
869 { OV7670_REG_GAIN, 0 }, { OV7670_REG_AECH, 0 },
870 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
871 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
872 { OV7670_REG_BD50MAX, 0x05 }, { OV7670_REG_BD60MAX, 0x07 },
873 { OV7670_REG_AEW, 0x95 }, { OV7670_REG_AEB, 0x33 },
874 { OV7670_REG_VPT, 0xe3 }, { OV7670_REG_HAECC1, 0x78 },
875 { OV7670_REG_HAECC2, 0x68 },
876 { 0xa1, 0x03 }, /* magic */
877 { OV7670_REG_HAECC3, 0xd8 }, { OV7670_REG_HAECC4, 0xd8 },
878 { OV7670_REG_HAECC5, 0xf0 }, { OV7670_REG_HAECC6, 0x90 },
879 { OV7670_REG_HAECC7, 0x94 },
880 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
881 | OV7670_COM8_AECSTEP
886 /* Almost all of these are magic "reserved" values. */
887 { OV7670_REG_COM5, 0x61 }, { OV7670_REG_COM6, 0x4b },
889 { OV7670_REG_MVFP, 0x07 },
890 { 0x21, 0x02 }, { 0x22, 0x91 },
891 { 0x29, 0x07 }, { 0x33, 0x0b },
892 { 0x35, 0x0b }, { 0x37, 0x1d },
893 { 0x38, 0x71 }, { 0x39, 0x2a },
894 { OV7670_REG_COM12, 0x78 }, { 0x4d, 0x40 },
895 { 0x4e, 0x20 }, { OV7670_REG_GFIX, 0 },
896 { 0x6b, 0x4a }, { 0x74, 0x10 },
897 { 0x8d, 0x4f }, { 0x8e, 0 },
898 { 0x8f, 0 }, { 0x90, 0 },
899 { 0x91, 0 }, { 0x96, 0 },
900 { 0x9a, 0 }, { 0xb0, 0x84 },
901 { 0xb1, 0x0c }, { 0xb2, 0x0e },
902 { 0xb3, 0x82 }, { 0xb8, 0x0a },
904 /* More reserved magic, some of which tweaks white balance */
905 { 0x43, 0x0a }, { 0x44, 0xf0 },
906 { 0x45, 0x34 }, { 0x46, 0x58 },
907 { 0x47, 0x28 }, { 0x48, 0x3a },
908 { 0x59, 0x88 }, { 0x5a, 0x88 },
909 { 0x5b, 0x44 }, { 0x5c, 0x67 },
910 { 0x5d, 0x49 }, { 0x5e, 0x0e },
911 { 0x6c, 0x0a }, { 0x6d, 0x55 },
912 { 0x6e, 0x11 }, { 0x6f, 0x9f },
913 /* "9e for advance AWB" */
914 { 0x6a, 0x40 }, { OV7670_REG_BLUE, 0x40 },
915 { OV7670_REG_RED, 0x60 },
916 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
917 | OV7670_COM8_AECSTEP
923 /* Matrix coefficients */
924 { 0x4f, 0x80 }, { 0x50, 0x80 },
925 { 0x51, 0 }, { 0x52, 0x22 },
926 { 0x53, 0x5e }, { 0x54, 0x80 },
929 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
930 { OV7670_REG_EDGE, 0 },
931 { 0x75, 0x05 }, { 0x76, 0xe1 },
932 { 0x4c, 0 }, { 0x77, 0x01 },
933 { OV7670_REG_COM13, OV7670_COM13_GAMMA
937 { 0xc9, 0x60 }, { OV7670_REG_COM16, 0x38 },
941 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
942 { 0xa4, 0x88 }, { 0x96, 0 },
943 { 0x97, 0x30 }, { 0x98, 0x20 },
944 { 0x99, 0x30 }, { 0x9a, 0x84 },
945 { 0x9b, 0x29 }, { 0x9c, 0x03 },
946 { 0x9d, 0x4c }, { 0x9e, 0x3f },
949 /* Extra-weird stuff. Some sort of multiplexor register */
950 { 0x79, 0x01 }, { 0xc8, 0xf0 },
951 { 0x79, 0x0f }, { 0xc8, 0x00 },
952 { 0x79, 0x10 }, { 0xc8, 0x7e },
953 { 0x79, 0x0a }, { 0xc8, 0x80 },
954 { 0x79, 0x0b }, { 0xc8, 0x01 },
955 { 0x79, 0x0c }, { 0xc8, 0x0f },
956 { 0x79, 0x0d }, { 0xc8, 0x20 },
957 { 0x79, 0x09 }, { 0xc8, 0x80 },
958 { 0x79, 0x02 }, { 0xc8, 0xc0 },
959 { 0x79, 0x03 }, { 0xc8, 0x40 },
960 { 0x79, 0x05 }, { 0xc8, 0x30 },
964 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
966 /* Detect sensor (sub)type */
967 rc = i2c_r(sd, OV7610_REG_COM_I);
970 * it appears to be wrongly detected as a 7610 by default */
972 PDEBUG(D_ERR, "Error detecting sensor type");
976 /* quick hack to make OV7670s work */
977 high = i2c_r(sd, 0x0a);
978 low = i2c_r(sd, 0x0b);
979 /* info("%x, %x", high, low); */
980 if (high == 0x76 && low == 0x73) {
981 PDEBUG(D_PROBE, "Sensor is an OV7670");
982 sd->sensor = SEN_OV7670;
984 PDEBUG(D_PROBE, "Sensor is an OV7610");
985 sd->sensor = SEN_OV7610;
987 } else if ((rc & 3) == 1) {
988 /* I don't know what's different about the 76BE yet. */
989 if (i2c_r(sd, 0x15) & 1)
990 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
992 PDEBUG(D_PROBE, "Sensor is an OV76BE");
994 /* OV511+ will return all zero isoc data unless we
995 * configure the sensor as a 7620. Someone needs to
996 * find the exact reg. setting that causes this. */
997 sd->sensor = SEN_OV76BE;
998 } else if ((rc & 3) == 0) {
999 /* try to read product id registers */
1000 high = i2c_r(sd, 0x0a);
1002 PDEBUG(D_ERR, "Error detecting camera chip PID");
1005 low = i2c_r(sd, 0x0b);
1007 PDEBUG(D_ERR, "Error detecting camera chip VER");
1013 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
1014 sd->sensor = SEN_OV7630;
1017 PDEBUG(D_PROBE, "Sensor is an OV7645");
1018 sd->sensor = SEN_OV7640; /* FIXME */
1021 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1022 sd->sensor = SEN_OV7640; /* FIXME */
1025 PDEBUG(D_PROBE, "Sensor is an OV7648");
1026 sd->sensor = SEN_OV7640; /* FIXME */
1029 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
1033 PDEBUG(D_PROBE, "Sensor is an OV7620");
1034 sd->sensor = SEN_OV7620;
1037 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1041 switch (sd->sensor) {
1043 PDEBUG(D_PROBE, "Writing 7620 registers");
1044 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
1048 PDEBUG(D_ERR, "7630 is not supported by this driver version");
1051 PDEBUG(D_PROBE, "Writing 7640 registers");
1052 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
1056 PDEBUG(D_PROBE, "Writing 7670 registers");
1057 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
1061 PDEBUG(D_PROBE, "Writing 7610 registers");
1062 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
1067 /* Set sensor-specific vars */
1068 /* sd->sif = 0; already done */
1072 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1073 static int ov6xx0_configure(struct sd *sd)
1076 static const struct ov_i2c_regvals norm_6x20[] = {
1077 { 0x12, 0x80 }, /* reset */
1080 { 0x05, 0x7f }, /* For when autoadjust is off */
1082 /* The ratio of 0x0c and 0x0d controls the white point */
1085 { 0x0f, 0x15 }, /* COMS */
1086 { 0x10, 0x75 }, /* AEC Exposure time */
1087 { 0x12, 0x24 }, /* Enable AGC */
1089 /* 0x16: 0x06 helps frame stability with moving objects */
1091 /* { 0x20, 0x30 }, * Aperture correction enable */
1092 { 0x26, 0xb2 }, /* BLC enable */
1093 /* 0x28: 0x05 Selects RGB format if RGB on */
1095 { 0x2a, 0x04 }, /* Disable framerate adjust */
1096 /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
1098 { 0x33, 0xa0 }, /* Color Processing Parameter */
1099 { 0x34, 0xd2 }, /* Max A/D range */
1103 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1104 { 0x3c, 0x3c }, /* Change AEC mode */
1105 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1108 /* These next two registers (0x4a, 0x4b) are undocumented.
1109 * They control the color balance */
1112 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1115 /* Do 50-53 have any effect? */
1116 /* Toggle 0x12[2] off and on here? */
1119 static const struct ov_i2c_regvals norm_6x30[] = {
1120 { 0x12, 0x80 }, /* Reset */
1121 { 0x00, 0x1f }, /* Gain */
1122 { 0x01, 0x99 }, /* Blue gain */
1123 { 0x02, 0x7c }, /* Red gain */
1124 { 0x03, 0xc0 }, /* Saturation */
1125 { 0x05, 0x0a }, /* Contrast */
1126 { 0x06, 0x95 }, /* Brightness */
1127 { 0x07, 0x2d }, /* Sharpness */
1133 { 0x11, 0x00 }, /* Pixel clock = fastest */
1134 { 0x12, 0x24 }, /* Enable AGC and AWB */
1149 { 0x23, 0xc0 }, /* Crystal circuit power level */
1150 { 0x25, 0x9a }, /* Increase AEC black ratio */
1151 { 0x26, 0xb2 }, /* BLC enable */
1155 { 0x2a, 0x84 }, /* 60 Hz power */
1156 { 0x2b, 0xa8 }, /* 60 Hz power */
1158 { 0x2d, 0x95 }, /* Enable auto-brightness */
1172 { 0x40, 0x00 }, /* White bal */
1173 { 0x41, 0x00 }, /* White bal */
1175 { 0x43, 0x3f }, /* White bal */
1185 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1187 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1189 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1194 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1196 { 0x5b, 0x0f }, /* AWB chrominance levels */
1200 { 0x12, 0x20 }, /* Toggle AWB */
1204 PDEBUG(D_PROBE, "starting sensor configuration");
1206 if (init_ov_sensor(sd) < 0) {
1207 PDEBUG(D_ERR, "Failed to read sensor ID.");
1210 PDEBUG(D_PROBE, "OV6xx0 sensor detected");
1212 /* Detect sensor (sub)type */
1213 rc = i2c_r(sd, OV7610_REG_COM_I);
1215 PDEBUG(D_ERR, "Error detecting sensor type");
1219 /* Ugh. The first two bits are the version bits, but
1220 * the entire register value must be used. I guess OVT
1221 * underestimated how many variants they would make. */
1224 sd->sensor = SEN_OV6630;
1226 "WARNING: Sensor is an OV66308. Your camera may have");
1227 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1230 sd->sensor = SEN_OV6620;
1233 sd->sensor = SEN_OV6630;
1234 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
1237 sd->sensor = SEN_OV6630;
1238 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
1241 sd->sensor = SEN_OV6630;
1243 "WARNING: Sensor is an OV66307. Your camera may have");
1244 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1247 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1251 /* Set sensor-specific vars */
1254 if (sd->sensor == SEN_OV6620) {
1255 PDEBUG(D_PROBE, "Writing 6x20 registers");
1256 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
1259 PDEBUG(D_PROBE, "Writing 6x30 registers");
1260 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
1266 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1267 static void ov51x_led_control(struct sd *sd, int on)
1269 /* PDEBUG(D_STREAM, "LED (%s)", on ? "on" : "off"); */
1270 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
1273 /* this function is called at probe time */
1274 static int sd_config(struct gspca_dev *gspca_dev,
1275 const struct usb_device_id *id)
1277 struct sd *sd = (struct sd *) gspca_dev;
1280 static const struct ov_regvals init_519[] = {
1281 { 0x5a, 0x6d }, /* EnableSystem */
1283 { 0x54, 0xff }, /* set bit2 to enable jpeg */
1287 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1288 * detection will fail. This deserves further investigation. */
1289 { OV519_GPIO_IO_CTRL0, 0xee },
1290 { 0x51, 0x0f }, /* SetUsbInit */
1293 /* windows reads 0x55 at this point*/
1296 if (write_regvals(sd, init_519, ARRAY_SIZE(init_519)))
1298 ov51x_led_control(sd, 0); /* turn LED off */
1301 sd->primary_i2c_slave = OV7xx0_SID;
1302 if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1305 /* The OV519 must be more aggressive about sensor detection since
1306 * I2C write will never fail if the sensor is not present. We have
1307 * to try to initialize the sensor to detect its presence */
1308 if (init_ov_sensor(sd) < 0) {
1310 sd->primary_i2c_slave = OV6xx0_SID;
1311 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1314 if (init_ov_sensor(sd) < 0) {
1316 sd->primary_i2c_slave = OV8xx0_SID;
1317 if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1320 if (init_ov_sensor(sd) < 0) {
1322 "Can't determine sensor slave IDs");
1325 if (ov8xx0_configure(sd) < 0) {
1327 "Failed to configure OV8xx0 sensor");
1332 if (ov6xx0_configure(sd) < 0) {
1333 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1338 if (ov7xx0_configure(sd) < 0) {
1339 PDEBUG(D_ERR, "Failed to configure OV7xx0");
1344 cam = &gspca_dev->cam;
1345 cam->epaddr = OV511_ENDPOINT_ADDRESS;
1347 cam->cam_mode = vga_mode;
1348 cam->nmodes = ARRAY_SIZE(vga_mode);
1350 cam->cam_mode = sif_mode;
1351 cam->nmodes = ARRAY_SIZE(sif_mode);
1353 sd->brightness = BRIGHTNESS_DEF;
1354 sd->contrast = CONTRAST_DEF;
1355 sd->colors = COLOR_DEF;
1356 sd->hflip = HFLIP_DEF;
1357 sd->vflip = VFLIP_DEF;
1360 PDEBUG(D_ERR, "OV519 Config failed");
1364 /* this function is called at open time */
1365 static int sd_open(struct gspca_dev *gspca_dev)
1370 /* Sets up the OV519 with the given image parameters
1372 * OV519 needs a completely different approach, until we can figure out what
1373 * the individual registers do.
1375 * Do not put any sensor-specific code in here (including I2C I/O functions)
1377 static int ov519_mode_init_regs(struct sd *sd)
1379 static const struct ov_regvals mode_init_519_ov7670[] = {
1380 { 0x5d, 0x03 }, /* Turn off suspend mode */
1381 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1382 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1383 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1387 { 0x37, 0x00 }, /* SetUsbInit */
1388 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1389 /* Enable both fields, YUV Input, disable defect comp (why?) */
1393 { 0x17, 0x50 }, /* undocumented */
1394 { 0x37, 0x00 }, /* undocumented */
1395 { 0x40, 0xff }, /* I2C timeout counter */
1396 { 0x46, 0x00 }, /* I2C clock prescaler */
1397 { 0x59, 0x04 }, /* new from windrv 090403 */
1398 { 0xff, 0x00 }, /* undocumented */
1399 /* windows reads 0x55 at this point, why? */
1402 static const struct ov_regvals mode_init_519[] = {
1403 { 0x5d, 0x03 }, /* Turn off suspend mode */
1404 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1405 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1406 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1410 { 0x37, 0x00 }, /* SetUsbInit */
1411 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1412 /* Enable both fields, YUV Input, disable defect comp (why?) */
1414 { 0x17, 0x50 }, /* undocumented */
1415 { 0x37, 0x00 }, /* undocumented */
1416 { 0x40, 0xff }, /* I2C timeout counter */
1417 { 0x46, 0x00 }, /* I2C clock prescaler */
1418 { 0x59, 0x04 }, /* new from windrv 090403 */
1419 { 0xff, 0x00 }, /* undocumented */
1420 /* windows reads 0x55 at this point, why? */
1423 /******** Set the mode ********/
1424 if (sd->sensor != SEN_OV7670) {
1425 if (write_regvals(sd, mode_init_519,
1426 ARRAY_SIZE(mode_init_519)))
1428 if (sd->sensor == SEN_OV7640) {
1429 /* Select 8-bit input mode */
1430 reg_w_mask(sd, OV519_CAM_DFR, 0x10, 0x10);
1433 if (write_regvals(sd, mode_init_519_ov7670,
1434 ARRAY_SIZE(mode_init_519_ov7670)))
1438 reg_w(sd, OV519_CAM_H_SIZE, sd->gspca_dev.width >> 4);
1439 reg_w(sd, OV519_CAM_V_SIZE, sd->gspca_dev.height >> 3);
1440 reg_w(sd, OV519_CAM_X_OFFSETL, 0x00);
1441 reg_w(sd, OV519_CAM_X_OFFSETH, 0x00);
1442 reg_w(sd, OV519_CAM_Y_OFFSETL, 0x00);
1443 reg_w(sd, OV519_CAM_Y_OFFSETH, 0x00);
1444 reg_w(sd, OV519_CAM_DIVIDER, 0x00);
1445 reg_w(sd, OV519_CAM_FORMAT, 0x03); /* YUV422 */
1446 reg_w(sd, 0x26, 0x00); /* Undocumented */
1448 /******** Set the framerate ********/
1450 sd->frame_rate = frame_rate;
1452 /* FIXME: These are only valid at the max resolution. */
1454 switch (sd->sensor) {
1456 switch (sd->frame_rate) {
1457 /*fixme: default was 30 fps */
1459 reg_w(sd, 0xa4, 0x0c);
1460 reg_w(sd, 0x23, 0xff);
1463 reg_w(sd, 0xa4, 0x0c);
1464 reg_w(sd, 0x23, 0x1f);
1467 reg_w(sd, 0xa4, 0x0c);
1468 reg_w(sd, 0x23, 0x1b);
1472 reg_w(sd, 0xa4, 0x04);
1473 reg_w(sd, 0x23, 0xff);
1477 reg_w(sd, 0xa4, 0x04);
1478 reg_w(sd, 0x23, 0x1f);
1482 reg_w(sd, 0xa4, 0x04);
1483 reg_w(sd, 0x23, 0x1b);
1489 switch (sd->frame_rate) {
1490 default: /* 15 fps */
1492 reg_w(sd, 0xa4, 0x06);
1493 reg_w(sd, 0x23, 0xff);
1496 reg_w(sd, 0xa4, 0x06);
1497 reg_w(sd, 0x23, 0x1f);
1500 reg_w(sd, 0xa4, 0x06);
1501 reg_w(sd, 0x23, 0x1b);
1505 case SEN_OV7670: /* guesses, based on 7640 */
1506 PDEBUG(D_STREAM, "Setting framerate to %d fps",
1507 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
1508 reg_w(sd, 0xa4, 0x10);
1509 switch (sd->frame_rate) {
1511 reg_w(sd, 0x23, 0xff);
1514 reg_w(sd, 0x23, 0x1b);
1518 reg_w(sd, 0x23, 0xff);
1528 static int mode_init_ov_sensor_regs(struct sd *sd)
1530 struct gspca_dev *gspca_dev;
1533 gspca_dev = &sd->gspca_dev;
1534 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
1536 /******** Mode (VGA/QVGA) and sensor specific regs ********/
1537 switch (sd->sensor) {
1539 /* For OV8610 qvga means qsvga */
1540 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
1543 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1546 /* i2c_w(sd, 0x2b, 0x00); */
1547 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1548 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1549 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
1550 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
1551 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
1552 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
1553 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
1556 /* i2c_w(sd, 0x2b, 0x00); */
1557 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1560 /* i2c_w(sd, 0x2b, 0x00); */
1561 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1562 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1563 /* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
1564 /* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
1565 /* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
1566 /* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
1567 /* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
1570 /* set COM7_FMT_VGA or COM7_FMT_QVGA
1571 * do we need to set anything else?
1572 * HSTART etc are set in set_ov_sensor_window itself */
1573 i2c_w_mask(sd, OV7670_REG_COM7,
1574 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
1575 OV7670_COM7_FMT_MASK);
1579 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1585 /******** Palette-specific regs ********/
1586 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1587 /* not valid on the OV6620/OV7620/6630? */
1588 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
1591 /* The OV518 needs special treatment. Although both the OV518
1592 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
1593 * bus is actually used. The UV bus is tied to ground.
1594 * Therefore, the OV6630 needs to be in 8-bit multiplexed
1597 /* OV7640 is 8-bit only */
1599 if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV7640)
1600 i2c_w_mask(sd, 0x13, 0x00, 0x20);
1602 /******** Clock programming ********/
1603 /* The OV6620 needs special handling. This prevents the
1604 * severe banding that normally occurs */
1605 if (sd->sensor == SEN_OV6620) {
1608 i2c_w(sd, 0x2a, 0x04);
1609 i2c_w(sd, 0x11, sd->clockdiv);
1610 i2c_w(sd, 0x2a, 0x84);
1611 /* This next setting is critical. It seems to improve
1612 * the gain or the contrast. The "reserved" bits seem
1613 * to have some effect in this case. */
1614 i2c_w(sd, 0x2d, 0x85);
1615 } else if (sd->clockdiv >= 0) {
1616 i2c_w(sd, 0x11, sd->clockdiv);
1619 /******** Special Features ********/
1620 /* no evidence this is possible with OV7670, either */
1622 if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
1623 i2c_w_mask(sd, 0x12, 0x00, 0x02);
1625 /* Enable auto white balance */
1626 if (sd->sensor == SEN_OV7670)
1627 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
1630 i2c_w_mask(sd, 0x12, 0x04, 0x04);
1632 /* This will go away as soon as ov51x_mode_init_sensor_regs() */
1633 /* is fully tested. */
1634 /* 7620/6620/6630? don't have register 0x35, so play it safe */
1635 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1637 i2c_w(sd, 0x35, 0x9e);
1639 i2c_w(sd, 0x35, 0x1e);
1644 static void sethvflip(struct sd *sd)
1646 if (sd->sensor != SEN_OV7670)
1648 if (sd->gspca_dev.streaming)
1650 i2c_w_mask(sd, OV7670_REG_MVFP,
1651 OV7670_MVFP_MIRROR * sd->hflip
1652 | OV7670_MVFP_VFLIP * sd->vflip,
1653 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
1654 if (sd->gspca_dev.streaming)
1658 static int set_ov_sensor_window(struct sd *sd)
1660 struct gspca_dev *gspca_dev;
1662 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
1663 int ret, hstart, hstop, vstop, vstart;
1666 gspca_dev = &sd->gspca_dev;
1667 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
1669 /* The different sensor ICs handle setting up of window differently.
1670 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
1671 switch (sd->sensor) {
1682 vwsbase = vwebase = 0x05;
1692 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
1694 vwsbase = vwebase = 0x05;
1699 vwsbase = vwebase = 0x03;
1702 /*handling of OV7670 hardware sensor start and stop values
1703 * is very odd, compared to the other OV sensors */
1704 vwsbase = vwebase = hwebase = hwsbase = 0x00;
1710 switch (sd->sensor) {
1713 if (qvga) { /* QCIF */
1718 vwscale = 1; /* The datasheet says 0;
1723 if (qvga) { /* QSVGA */
1731 default: /* SEN_OV7xx0 */
1732 if (qvga) { /* QVGA */
1741 ret = mode_init_ov_sensor_regs(sd);
1745 if (sd->sensor == SEN_OV8610) {
1746 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
1747 /* old 0x95, new 0x05 from windrv 090403 */
1748 /* bits 5-7: reserved */
1749 i2c_w_mask(sd, 0x28, 0x20, 0x20);
1750 /* bit 5: progressive mode on */
1753 /* The below is wrong for OV7670s because their window registers
1754 * only store the high bits in 0x17 to 0x1a */
1756 /* SRH Use sd->max values instead of requested win values */
1757 /* SCS Since we're sticking with only the max hardware widths
1758 * for a given mode */
1759 /* I can hard code this for OV7670s */
1760 /* Yes, these numbers do look odd, but they're tested and work! */
1761 if (sd->sensor == SEN_OV7670) {
1762 if (qvga) { /* QVGA from ov7670.c by
1763 * Jonathan Corbet */
1774 /* OV7670 hardware window registers are split across
1775 * multiple locations */
1776 i2c_w(sd, OV7670_REG_HSTART, hstart >> 3);
1777 i2c_w(sd, OV7670_REG_HSTOP, hstop >> 3);
1778 v = i2c_r(sd, OV7670_REG_HREF);
1779 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
1780 msleep(10); /* need to sleep between read and write to
1782 i2c_w(sd, OV7670_REG_HREF, v);
1784 i2c_w(sd, OV7670_REG_VSTART, vstart >> 2);
1785 i2c_w(sd, OV7670_REG_VSTOP, vstop >> 2);
1786 v = i2c_r(sd, OV7670_REG_VREF);
1787 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
1788 msleep(10); /* need to sleep between read and write to
1790 i2c_w(sd, OV7670_REG_VREF, v);
1793 i2c_w(sd, 0x17, hwsbase);
1794 i2c_w(sd, 0x18, hwebase + (sd->gspca_dev.width >> hwscale));
1795 i2c_w(sd, 0x19, vwsbase);
1796 i2c_w(sd, 0x1a, vwebase + (sd->gspca_dev.height >> vwscale));
1801 /* -- start the camera -- */
1802 static void sd_start(struct gspca_dev *gspca_dev)
1804 struct sd *sd = (struct sd *) gspca_dev;
1807 ret = ov519_mode_init_regs(sd);
1810 ret = set_ov_sensor_window(sd);
1814 ret = ov51x_restart(sd);
1817 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
1818 ov51x_led_control(sd, 1);
1821 PDEBUG(D_ERR, "camera start error:%d", ret);
1824 static void sd_stopN(struct gspca_dev *gspca_dev)
1826 ov51x_stop((struct sd *) gspca_dev);
1827 ov51x_led_control((struct sd *) gspca_dev, 0);
1830 static void sd_stop0(struct gspca_dev *gspca_dev)
1834 static void sd_close(struct gspca_dev *gspca_dev)
1838 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1839 struct gspca_frame *frame, /* target */
1840 __u8 *data, /* isoc packet */
1841 int len) /* iso packet length */
1843 /* Header of ov519 is 16 bytes:
1844 * Byte Value Description
1848 * 3 0xXX 0x50 = SOF, 0x51 = EOF
1849 * 9 0xXX 0x01 initial frame without data,
1850 * 0x00 standard frame with image
1851 * 14 Lo in EOF: length of image data / 8
1855 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
1857 case 0x50: /* start of frame */
1862 if (data[0] == 0xff || data[1] == 0xd8)
1863 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
1866 gspca_dev->last_packet_type = DISCARD_PACKET;
1868 case 0x51: /* end of frame */
1870 gspca_dev->last_packet_type = DISCARD_PACKET;
1871 gspca_frame_add(gspca_dev, LAST_PACKET, frame,
1877 /* intermediate packet */
1878 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
1882 /* -- management routines -- */
1884 static void setbrightness(struct gspca_dev *gspca_dev)
1886 struct sd *sd = (struct sd *) gspca_dev;
1889 val = sd->brightness;
1890 PDEBUG(D_CONF, "brightness:%d", val);
1891 /* if (gspca_dev->streaming)
1892 * ov51x_stop(sd); */
1893 switch (sd->sensor) {
1900 i2c_w(sd, OV7610_REG_BRT, val);
1903 /* 7620 doesn't like manual changes when in auto mode */
1905 * if (!sd->auto_brt) */
1906 i2c_w(sd, OV7610_REG_BRT, val);
1910 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
1911 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
1914 /* if (gspca_dev->streaming)
1915 * ov51x_restart(sd); */
1918 static void setcontrast(struct gspca_dev *gspca_dev)
1920 struct sd *sd = (struct sd *) gspca_dev;
1924 PDEBUG(D_CONF, "contrast:%d", val);
1925 /* if (gspca_dev->streaming)
1927 switch (sd->sensor) {
1930 i2c_w(sd, OV7610_REG_CNT, val);
1933 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
1935 static const __u8 ctab[] = {
1936 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
1939 /* Use Y gamma control instead. Bit 0 enables it. */
1940 i2c_w(sd, 0x64, ctab[val >> 5]);
1944 static const __u8 ctab[] = {
1945 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1946 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1949 /* Use Y gamma control instead. Bit 0 enables it. */
1950 i2c_w(sd, 0x64, ctab[val >> 4]);
1954 /* Use gain control instead. */
1955 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
1958 /* check that this isn't just the same as ov7610 */
1959 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
1962 /* if (gspca_dev->streaming)
1963 ov51x_restart(sd); */
1966 static void setcolors(struct gspca_dev *gspca_dev)
1968 struct sd *sd = (struct sd *) gspca_dev;
1972 PDEBUG(D_CONF, "saturation:%d", val);
1973 /* if (gspca_dev->streaming)
1975 switch (sd->sensor) {
1981 i2c_w(sd, OV7610_REG_SAT, val);
1984 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
1985 /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
1988 i2c_w(sd, OV7610_REG_SAT, val);
1991 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
1994 /* supported later once I work out how to do it
1995 * transparently fail now! */
1996 /* set REG_COM13 values for UV sat auto mode */
1999 /* if (gspca_dev->streaming)
2000 ov51x_restart(sd); */
2003 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2005 struct sd *sd = (struct sd *) gspca_dev;
2007 sd->brightness = val;
2008 setbrightness(gspca_dev);
2012 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2014 struct sd *sd = (struct sd *) gspca_dev;
2016 *val = sd->brightness;
2020 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2022 struct sd *sd = (struct sd *) gspca_dev;
2025 setcontrast(gspca_dev);
2029 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2031 struct sd *sd = (struct sd *) gspca_dev;
2033 *val = sd->contrast;
2037 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2039 struct sd *sd = (struct sd *) gspca_dev;
2042 setcolors(gspca_dev);
2046 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2048 struct sd *sd = (struct sd *) gspca_dev;
2054 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
2056 struct sd *sd = (struct sd *) gspca_dev;
2063 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
2065 struct sd *sd = (struct sd *) gspca_dev;
2071 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2073 struct sd *sd = (struct sd *) gspca_dev;
2080 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2082 struct sd *sd = (struct sd *) gspca_dev;
2088 /* sub-driver description */
2089 static const struct sd_desc sd_desc = {
2090 .name = MODULE_NAME,
2092 .nctrls = ARRAY_SIZE(sd_ctrls),
2093 .config = sd_config,
2099 .pkt_scan = sd_pkt_scan,
2102 /* -- module initialisation -- */
2103 static const __devinitdata struct usb_device_id device_table[] = {
2104 {USB_DEVICE(0x041e, 0x4052)},
2105 {USB_DEVICE(0x041e, 0x405f)},
2106 {USB_DEVICE(0x041e, 0x4060)},
2107 {USB_DEVICE(0x041e, 0x4061)},
2108 {USB_DEVICE(0x041e, 0x4064)},
2109 {USB_DEVICE(0x041e, 0x4068)},
2110 {USB_DEVICE(0x045e, 0x028c)},
2111 {USB_DEVICE(0x054c, 0x0154)},
2112 {USB_DEVICE(0x054c, 0x0155)},
2113 {USB_DEVICE(0x05a9, 0x0519)},
2114 {USB_DEVICE(0x05a9, 0x0530)},
2115 {USB_DEVICE(0x05a9, 0x4519)},
2116 {USB_DEVICE(0x05a9, 0x8519)},
2120 MODULE_DEVICE_TABLE(usb, device_table);
2122 /* -- device connect -- */
2123 static int sd_probe(struct usb_interface *intf,
2124 const struct usb_device_id *id)
2126 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2130 static struct usb_driver sd_driver = {
2131 .name = MODULE_NAME,
2132 .id_table = device_table,
2134 .disconnect = gspca_disconnect,
2136 .suspend = gspca_suspend,
2137 .resume = gspca_resume,
2141 /* -- module insert / remove -- */
2142 static int __init sd_mod_init(void)
2144 if (usb_register(&sd_driver) < 0)
2146 PDEBUG(D_PROBE, "registered");
2149 static void __exit sd_mod_exit(void)
2151 usb_deregister(&sd_driver);
2152 PDEBUG(D_PROBE, "deregistered");
2155 module_init(sd_mod_init);
2156 module_exit(sd_mod_exit);
2158 module_param(frame_rate, int, 0644);
2159 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");