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_*) */
73 /* V4L2 controls supported by the driver */
74 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
75 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
76 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
77 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
78 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
79 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
80 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
81 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
82 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
83 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
85 static struct ctrl sd_ctrls[] = {
88 .id = V4L2_CID_BRIGHTNESS,
89 .type = V4L2_CTRL_TYPE_INTEGER,
94 #define BRIGHTNESS_DEF 127
95 .default_value = BRIGHTNESS_DEF,
97 .set = sd_setbrightness,
98 .get = sd_getbrightness,
102 .id = V4L2_CID_CONTRAST,
103 .type = V4L2_CTRL_TYPE_INTEGER,
108 #define CONTRAST_DEF 127
109 .default_value = CONTRAST_DEF,
111 .set = sd_setcontrast,
112 .get = sd_getcontrast,
116 .id = V4L2_CID_SATURATION,
117 .type = V4L2_CTRL_TYPE_INTEGER,
122 #define COLOR_DEF 127
123 .default_value = COLOR_DEF,
128 /* next controls work with ov7670 only */
132 .id = V4L2_CID_HFLIP,
133 .type = V4L2_CTRL_TYPE_BOOLEAN,
139 .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 + 590,
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 + 590,
177 .colorspace = V4L2_COLORSPACE_JPEG,
179 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
181 .sizeimage = 352 * 288 * 3 / 8 + 590,
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 */
301 struct ov_i2c_regvals {
306 static const struct ov_i2c_regvals norm_6x20[] = {
307 { 0x12, 0x80 }, /* reset */
310 { 0x05, 0x7f }, /* For when autoadjust is off */
312 /* The ratio of 0x0c and 0x0d controls the white point */
315 { 0x0f, 0x15 }, /* COMS */
316 { 0x10, 0x75 }, /* AEC Exposure time */
317 { 0x12, 0x24 }, /* Enable AGC */
319 /* 0x16: 0x06 helps frame stability with moving objects */
321 /* { 0x20, 0x30 }, * Aperture correction enable */
322 { 0x26, 0xb2 }, /* BLC enable */
323 /* 0x28: 0x05 Selects RGB format if RGB on */
325 { 0x2a, 0x04 }, /* Disable framerate adjust */
326 /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
328 { 0x33, 0xa0 }, /* Color Processing Parameter */
329 { 0x34, 0xd2 }, /* Max A/D range */
333 { 0x3c, 0x39 }, /* Enable AEC mode changing */
334 { 0x3c, 0x3c }, /* Change AEC mode */
335 { 0x3c, 0x24 }, /* Disable AEC mode changing */
338 /* These next two registers (0x4a, 0x4b) are undocumented.
339 * They control the color balance */
342 { 0x4d, 0xd2 }, /* This reduces noise a bit */
345 /* Do 50-53 have any effect? */
346 /* Toggle 0x12[2] off and on here? */
349 static const struct ov_i2c_regvals norm_6x30[] = {
350 { 0x12, 0x80 }, /* Reset */
351 { 0x00, 0x1f }, /* Gain */
352 { 0x01, 0x99 }, /* Blue gain */
353 { 0x02, 0x7c }, /* Red gain */
354 { 0x03, 0xc0 }, /* Saturation */
355 { 0x05, 0x0a }, /* Contrast */
356 { 0x06, 0x95 }, /* Brightness */
357 { 0x07, 0x2d }, /* Sharpness */
363 { 0x11, 0x00 }, /* Pixel clock = fastest */
364 { 0x12, 0x24 }, /* Enable AGC and AWB */
379 { 0x23, 0xc0 }, /* Crystal circuit power level */
380 { 0x25, 0x9a }, /* Increase AEC black ratio */
381 { 0x26, 0xb2 }, /* BLC enable */
385 { 0x2a, 0x84 }, /* 60 Hz power */
386 { 0x2b, 0xa8 }, /* 60 Hz power */
388 { 0x2d, 0x95 }, /* Enable auto-brightness */
402 { 0x40, 0x00 }, /* White bal */
403 { 0x41, 0x00 }, /* White bal */
405 { 0x43, 0x3f }, /* White bal */
415 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
417 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
419 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
424 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
426 { 0x5b, 0x0f }, /* AWB chrominance levels */
430 { 0x12, 0x20 }, /* Toggle AWB */
434 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
436 * Register 0x0f in the 7610 has the following effects:
438 * 0x85 (AEC method 1): Best overall, good contrast range
439 * 0x45 (AEC method 2): Very overexposed
440 * 0xa5 (spec sheet default): Ok, but the black level is
441 * shifted resulting in loss of contrast
442 * 0x05 (old driver setting): very overexposed, too much
445 static const struct ov_i2c_regvals norm_7610[] = {
452 { 0x28, 0x24 }, /* 0c */
453 { 0x0f, 0x85 }, /* lg's setting */
475 static const struct ov_i2c_regvals norm_7620[] = {
476 { 0x00, 0x00 }, /* gain */
477 { 0x01, 0x80 }, /* blue gain */
478 { 0x02, 0x80 }, /* red gain */
479 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
541 /* 7640 and 7648. The defaults should be OK for most registers. */
542 static const struct ov_i2c_regvals norm_7640[] = {
547 /* 7670. Defaults taken from OmniVision provided data,
548 * as provided by Jonathan Corbet of OLPC */
549 static const struct ov_i2c_regvals norm_7670[] = {
550 { OV7670_REG_COM7, OV7670_COM7_RESET },
551 { OV7670_REG_TSLB, 0x04 }, /* OV */
552 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
553 { OV7670_REG_CLKRC, 0x01 },
555 * Set the hardware window. These values from OV don't entirely
556 * make sense - hstop is less than hstart. But they work...
558 { OV7670_REG_HSTART, 0x13 },
559 { OV7670_REG_HSTOP, 0x01 },
560 { OV7670_REG_HREF, 0xb6 },
561 { OV7670_REG_VSTART, 0x02 },
562 { OV7670_REG_VSTOP, 0x7a },
563 { OV7670_REG_VREF, 0x0a },
565 { OV7670_REG_COM3, 0 },
566 { OV7670_REG_COM14, 0 },
567 /* Mystery scaling numbers */
573 /* { OV7670_REG_COM10, 0x0 }, */
575 /* Gamma curve values */
593 /* AGC and AEC parameters. Note we start by disabling those features,
594 then turn them only after tweaking the values. */
595 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
596 | OV7670_COM8_AECSTEP
597 | OV7670_COM8_BFILT },
598 { OV7670_REG_GAIN, 0 },
599 { OV7670_REG_AECH, 0 },
600 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
601 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
602 { OV7670_REG_BD50MAX, 0x05 },
603 { OV7670_REG_BD60MAX, 0x07 },
604 { OV7670_REG_AEW, 0x95 },
605 { OV7670_REG_AEB, 0x33 },
606 { OV7670_REG_VPT, 0xe3 },
607 { OV7670_REG_HAECC1, 0x78 },
608 { OV7670_REG_HAECC2, 0x68 },
609 { 0xa1, 0x03 }, /* magic */
610 { OV7670_REG_HAECC3, 0xd8 },
611 { OV7670_REG_HAECC4, 0xd8 },
612 { OV7670_REG_HAECC5, 0xf0 },
613 { OV7670_REG_HAECC6, 0x90 },
614 { OV7670_REG_HAECC7, 0x94 },
615 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
616 | OV7670_COM8_AECSTEP
621 /* Almost all of these are magic "reserved" values. */
622 { OV7670_REG_COM5, 0x61 },
623 { OV7670_REG_COM6, 0x4b },
625 { OV7670_REG_MVFP, 0x07 },
634 { OV7670_REG_COM12, 0x78 },
637 { OV7670_REG_GFIX, 0 },
653 /* More reserved magic, some of which tweaks white balance */
670 /* "9e for advance AWB" */
672 { OV7670_REG_BLUE, 0x40 },
673 { OV7670_REG_RED, 0x60 },
674 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
675 | OV7670_COM8_AECSTEP
681 /* Matrix coefficients */
690 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
691 { OV7670_REG_EDGE, 0 },
696 { OV7670_REG_COM13, OV7670_COM13_GAMMA
701 { OV7670_REG_COM16, 0x38 },
705 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
718 /* Extra-weird stuff. Some sort of multiplexor register */
744 static const struct ov_i2c_regvals norm_8610[] = {
751 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
752 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
761 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
763 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
764 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
765 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
768 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
769 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
770 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
771 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
777 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
779 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
781 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
783 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
784 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
785 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
786 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
788 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
789 * maybe thats wrong */
793 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
797 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
798 * deleting bit7 colors the first images red */
799 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
800 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
806 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
808 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
813 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
815 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
816 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
823 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
829 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
832 static unsigned char ov7670_abs_to_sm(unsigned char v)
836 return (128 - v) | 0x80;
839 /* Write a OV519 register */
840 static int reg_w(struct sd *sd, __u16 index, __u8 value)
844 sd->gspca_dev.usb_buf[0] = value;
845 ret = usb_control_msg(sd->gspca_dev.dev,
846 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
847 1, /* REQ_IO (ov518/519) */
848 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
850 sd->gspca_dev.usb_buf, 1, 500);
852 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
856 /* Read from a OV519 register */
857 /* returns: negative is error, pos or zero is data */
858 static int reg_r(struct sd *sd, __u16 index)
862 ret = usb_control_msg(sd->gspca_dev.dev,
863 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
865 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
866 0, index, sd->gspca_dev.usb_buf, 1, 500);
869 ret = sd->gspca_dev.usb_buf[0];
871 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
875 /* Read 8 values from a OV519 register */
876 static int reg_r8(struct sd *sd,
881 ret = usb_control_msg(sd->gspca_dev.dev,
882 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
884 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
885 0, index, sd->gspca_dev.usb_buf, 8, 500);
888 ret = sd->gspca_dev.usb_buf[0];
890 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
895 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
896 * the same position as 1's in "mask" are cleared and set to "value". Bits
897 * that are in the same position as 0's in "mask" are preserved, regardless
898 * of their respective state in "value".
900 static int reg_w_mask(struct sd *sd,
909 value &= mask; /* Enforce mask on value */
910 ret = reg_r(sd, index);
914 oldval = ret & ~mask; /* Clear the masked bits */
915 value |= oldval; /* Set the desired bits */
917 return reg_w(sd, index, value);
921 * The OV518 I2C I/O procedure is different, hence, this function.
922 * This is normally only called from i2c_w(). Note that this function
923 * always succeeds regardless of whether the sensor is present and working.
925 static int i2c_w(struct sd *sd,
931 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
933 /* Select camera register */
934 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
938 /* Write "value" to I2C data port of OV511 */
939 rc = reg_w(sd, R51x_I2C_DATA, value);
943 /* Initiate 3-byte write cycle */
944 rc = reg_w(sd, R518_I2C_CTL, 0x01);
946 /* wait for write complete */
950 return reg_r8(sd, R518_I2C_CTL);
954 * returns: negative is error, pos or zero is data
956 * The OV518 I2C I/O procedure is different, hence, this function.
957 * This is normally only called from i2c_r(). Note that this function
958 * always succeeds regardless of whether the sensor is present and working.
960 static int i2c_r(struct sd *sd, __u8 reg)
964 /* Select camera register */
965 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
969 /* Initiate 2-byte write cycle */
970 rc = reg_w(sd, R518_I2C_CTL, 0x03);
974 /* Initiate 2-byte read cycle */
975 rc = reg_w(sd, R518_I2C_CTL, 0x05);
978 value = reg_r(sd, R51x_I2C_DATA);
979 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
983 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
984 * the same position as 1's in "mask" are cleared and set to "value". Bits
985 * that are in the same position as 0's in "mask" are preserved, regardless
986 * of their respective state in "value".
988 static int i2c_w_mask(struct sd *sd,
996 value &= mask; /* Enforce mask on value */
1000 oldval = rc & ~mask; /* Clear the masked bits */
1001 value |= oldval; /* Set the desired bits */
1002 return i2c_w(sd, reg, value);
1005 /* Temporarily stops OV511 from functioning. Must do this before changing
1006 * registers while the camera is streaming */
1007 static inline int ov51x_stop(struct sd *sd)
1009 PDEBUG(D_STREAM, "stopping");
1011 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
1014 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
1015 * actually stopped (for performance). */
1016 static inline int ov51x_restart(struct sd *sd)
1018 PDEBUG(D_STREAM, "restarting");
1023 /* Reinitialize the stream */
1024 return reg_w(sd, OV519_SYS_RESET1, 0x00);
1027 /* This does an initial reset of an OmniVision sensor and ensures that I2C
1028 * is synchronized. Returns <0 on failure.
1030 static int init_ov_sensor(struct sd *sd)
1034 /* Reset the sensor */
1035 if (i2c_w(sd, 0x12, 0x80) < 0)
1038 /* Wait for it to initialize */
1041 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
1042 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
1043 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
1048 /* Reset the sensor */
1049 if (i2c_w(sd, 0x12, 0x80) < 0)
1051 /* Wait for it to initialize */
1053 /* Dummy read to sync I2C */
1054 if (i2c_r(sd, 0x00) < 0)
1059 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
1063 /* Set the read and write slave IDs. The "slave" argument is the write slave,
1064 * and the read slave will be set to (slave + 1).
1065 * This should not be called from outside the i2c I/O functions.
1066 * Sets I2C read and write slave IDs. Returns <0 for error
1068 static int ov51x_set_slave_ids(struct sd *sd,
1073 rc = reg_w(sd, R51x_I2C_W_SID, slave);
1076 sd->primary_i2c_slave = slave;
1077 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
1080 static int write_regvals(struct sd *sd,
1081 const struct ov_regvals *regvals,
1087 rc = reg_w(sd, regvals->reg, regvals->val);
1095 static int write_i2c_regvals(struct sd *sd,
1096 const struct ov_i2c_regvals *regvals,
1102 rc = i2c_w(sd, regvals->reg, regvals->val);
1110 /****************************************************************************
1112 * OV511 and sensor configuration
1114 ***************************************************************************/
1116 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
1117 * the same register settings as the OV8610, since they are very similar.
1119 static int ov8xx0_configure(struct sd *sd)
1123 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
1125 /* Detect sensor (sub)type */
1126 rc = i2c_r(sd, OV7610_REG_COM_I);
1128 PDEBUG(D_ERR, "Error detecting sensor type");
1131 if ((rc & 3) == 1) {
1132 sd->sensor = SEN_OV8610;
1134 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1138 /* Set sensor-specific vars */
1139 /* sd->sif = 0; already done */
1143 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
1144 * the same register settings as the OV7610, since they are very similar.
1146 static int ov7xx0_configure(struct sd *sd)
1151 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
1153 /* Detect sensor (sub)type */
1154 rc = i2c_r(sd, OV7610_REG_COM_I);
1157 * it appears to be wrongly detected as a 7610 by default */
1159 PDEBUG(D_ERR, "Error detecting sensor type");
1162 if ((rc & 3) == 3) {
1163 /* quick hack to make OV7670s work */
1164 high = i2c_r(sd, 0x0a);
1165 low = i2c_r(sd, 0x0b);
1166 /* info("%x, %x", high, low); */
1167 if (high == 0x76 && low == 0x73) {
1168 PDEBUG(D_PROBE, "Sensor is an OV7670");
1169 sd->sensor = SEN_OV7670;
1171 PDEBUG(D_PROBE, "Sensor is an OV7610");
1172 sd->sensor = SEN_OV7610;
1174 } else if ((rc & 3) == 1) {
1175 /* I don't know what's different about the 76BE yet. */
1176 if (i2c_r(sd, 0x15) & 1)
1177 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
1179 PDEBUG(D_PROBE, "Sensor is an OV76BE");
1181 /* OV511+ will return all zero isoc data unless we
1182 * configure the sensor as a 7620. Someone needs to
1183 * find the exact reg. setting that causes this. */
1184 sd->sensor = SEN_OV76BE;
1185 } else if ((rc & 3) == 0) {
1186 /* try to read product id registers */
1187 high = i2c_r(sd, 0x0a);
1189 PDEBUG(D_ERR, "Error detecting camera chip PID");
1192 low = i2c_r(sd, 0x0b);
1194 PDEBUG(D_ERR, "Error detecting camera chip VER");
1200 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
1202 "7630 is not supported by this driver");
1205 PDEBUG(D_PROBE, "Sensor is an OV7645");
1206 sd->sensor = SEN_OV7640; /* FIXME */
1209 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1210 sd->sensor = SEN_OV7640; /* FIXME */
1213 PDEBUG(D_PROBE, "Sensor is an OV7648");
1214 sd->sensor = SEN_OV7640; /* FIXME */
1217 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
1221 PDEBUG(D_PROBE, "Sensor is an OV7620");
1222 sd->sensor = SEN_OV7620;
1225 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1229 /* Set sensor-specific vars */
1230 /* sd->sif = 0; already done */
1234 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1235 static int ov6xx0_configure(struct sd *sd)
1238 PDEBUG(D_PROBE, "starting OV6xx0 configuration");
1240 /* Detect sensor (sub)type */
1241 rc = i2c_r(sd, OV7610_REG_COM_I);
1243 PDEBUG(D_ERR, "Error detecting sensor type");
1247 /* Ugh. The first two bits are the version bits, but
1248 * the entire register value must be used. I guess OVT
1249 * underestimated how many variants they would make. */
1252 sd->sensor = SEN_OV6630;
1254 "WARNING: Sensor is an OV66308. Your camera may have");
1255 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1258 sd->sensor = SEN_OV6620;
1261 sd->sensor = SEN_OV6630;
1262 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
1265 sd->sensor = SEN_OV6630;
1266 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
1269 sd->sensor = SEN_OV6630;
1271 "WARNING: Sensor is an OV66307. Your camera may have");
1272 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1275 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1279 /* Set sensor-specific vars */
1285 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1286 static void ov51x_led_control(struct sd *sd, int on)
1288 /* PDEBUG(D_STREAM, "LED (%s)", on ? "on" : "off"); */
1289 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
1292 /* this function is called at probe time */
1293 static int sd_config(struct gspca_dev *gspca_dev,
1294 const struct usb_device_id *id)
1296 struct sd *sd = (struct sd *) gspca_dev;
1299 static const struct ov_regvals init_519[] = {
1300 { 0x5a, 0x6d }, /* EnableSystem */
1302 { 0x54, 0xff }, /* set bit2 to enable jpeg */
1306 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1307 * detection will fail. This deserves further investigation. */
1308 { OV519_GPIO_IO_CTRL0, 0xee },
1309 { 0x51, 0x0f }, /* SetUsbInit */
1312 /* windows reads 0x55 at this point*/
1315 if (write_regvals(sd, init_519, ARRAY_SIZE(init_519)))
1317 ov51x_led_control(sd, 0); /* turn LED off */
1320 if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1323 /* The OV519 must be more aggressive about sensor detection since
1324 * I2C write will never fail if the sensor is not present. We have
1325 * to try to initialize the sensor to detect its presence */
1326 if (init_ov_sensor(sd) >= 0) {
1327 if (ov7xx0_configure(sd) < 0) {
1328 PDEBUG(D_ERR, "Failed to configure OV7xx0");
1334 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1337 if (init_ov_sensor(sd) >= 0) {
1338 if (ov6xx0_configure(sd) < 0) {
1339 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1345 if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1348 if (init_ov_sensor(sd) < 0) {
1350 "Can't determine sensor slave IDs");
1353 if (ov8xx0_configure(sd) < 0) {
1355 "Failed to configure OV8xx0 sensor");
1361 cam = &gspca_dev->cam;
1362 cam->epaddr = OV511_ENDPOINT_ADDRESS;
1364 cam->cam_mode = vga_mode;
1365 cam->nmodes = ARRAY_SIZE(vga_mode);
1367 cam->cam_mode = sif_mode;
1368 cam->nmodes = ARRAY_SIZE(sif_mode);
1370 sd->brightness = BRIGHTNESS_DEF;
1371 sd->contrast = CONTRAST_DEF;
1372 sd->colors = COLOR_DEF;
1373 sd->hflip = HFLIP_DEF;
1374 sd->vflip = VFLIP_DEF;
1375 if (sd->sensor != SEN_OV7670)
1376 gspca_dev->ctrl_dis = (1 << HFLIP_IDX)
1380 PDEBUG(D_ERR, "OV519 Config failed");
1384 /* this function is called at probe and resume time */
1385 static int sd_init(struct gspca_dev *gspca_dev)
1387 struct sd *sd = (struct sd *) gspca_dev;
1389 /* initialize the sensor */
1390 switch (sd->sensor) {
1392 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
1396 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
1400 /* case SEN_OV7610: */
1401 /* case SEN_OV76BE: */
1402 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
1406 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
1410 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
1414 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
1418 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
1425 /* Sets up the OV519 with the given image parameters
1427 * OV519 needs a completely different approach, until we can figure out what
1428 * the individual registers do.
1430 * Do not put any sensor-specific code in here (including I2C I/O functions)
1432 static int ov519_mode_init_regs(struct sd *sd)
1434 static const struct ov_regvals mode_init_519_ov7670[] = {
1435 { 0x5d, 0x03 }, /* Turn off suspend mode */
1436 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1437 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1438 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1442 { 0x37, 0x00 }, /* SetUsbInit */
1443 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1444 /* Enable both fields, YUV Input, disable defect comp (why?) */
1448 { 0x17, 0x50 }, /* undocumented */
1449 { 0x37, 0x00 }, /* undocumented */
1450 { 0x40, 0xff }, /* I2C timeout counter */
1451 { 0x46, 0x00 }, /* I2C clock prescaler */
1452 { 0x59, 0x04 }, /* new from windrv 090403 */
1453 { 0xff, 0x00 }, /* undocumented */
1454 /* windows reads 0x55 at this point, why? */
1457 static const struct ov_regvals mode_init_519[] = {
1458 { 0x5d, 0x03 }, /* Turn off suspend mode */
1459 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1460 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1461 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1465 { 0x37, 0x00 }, /* SetUsbInit */
1466 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1467 /* Enable both fields, YUV Input, disable defect comp (why?) */
1469 { 0x17, 0x50 }, /* undocumented */
1470 { 0x37, 0x00 }, /* undocumented */
1471 { 0x40, 0xff }, /* I2C timeout counter */
1472 { 0x46, 0x00 }, /* I2C clock prescaler */
1473 { 0x59, 0x04 }, /* new from windrv 090403 */
1474 { 0xff, 0x00 }, /* undocumented */
1475 /* windows reads 0x55 at this point, why? */
1478 /******** Set the mode ********/
1479 if (sd->sensor != SEN_OV7670) {
1480 if (write_regvals(sd, mode_init_519,
1481 ARRAY_SIZE(mode_init_519)))
1483 if (sd->sensor == SEN_OV7640) {
1484 /* Select 8-bit input mode */
1485 reg_w_mask(sd, OV519_CAM_DFR, 0x10, 0x10);
1488 if (write_regvals(sd, mode_init_519_ov7670,
1489 ARRAY_SIZE(mode_init_519_ov7670)))
1493 reg_w(sd, OV519_CAM_H_SIZE, sd->gspca_dev.width >> 4);
1494 reg_w(sd, OV519_CAM_V_SIZE, sd->gspca_dev.height >> 3);
1495 reg_w(sd, OV519_CAM_X_OFFSETL, 0x00);
1496 reg_w(sd, OV519_CAM_X_OFFSETH, 0x00);
1497 reg_w(sd, OV519_CAM_Y_OFFSETL, 0x00);
1498 reg_w(sd, OV519_CAM_Y_OFFSETH, 0x00);
1499 reg_w(sd, OV519_CAM_DIVIDER, 0x00);
1500 reg_w(sd, OV519_CAM_FORMAT, 0x03); /* YUV422 */
1501 reg_w(sd, 0x26, 0x00); /* Undocumented */
1503 /******** Set the framerate ********/
1505 sd->frame_rate = frame_rate;
1507 /* FIXME: These are only valid at the max resolution. */
1509 switch (sd->sensor) {
1511 switch (sd->frame_rate) {
1512 /*fixme: default was 30 fps */
1514 reg_w(sd, 0xa4, 0x0c);
1515 reg_w(sd, 0x23, 0xff);
1518 reg_w(sd, 0xa4, 0x0c);
1519 reg_w(sd, 0x23, 0x1f);
1522 reg_w(sd, 0xa4, 0x0c);
1523 reg_w(sd, 0x23, 0x1b);
1527 reg_w(sd, 0xa4, 0x04);
1528 reg_w(sd, 0x23, 0xff);
1532 reg_w(sd, 0xa4, 0x04);
1533 reg_w(sd, 0x23, 0x1f);
1537 reg_w(sd, 0xa4, 0x04);
1538 reg_w(sd, 0x23, 0x1b);
1544 switch (sd->frame_rate) {
1545 default: /* 15 fps */
1547 reg_w(sd, 0xa4, 0x06);
1548 reg_w(sd, 0x23, 0xff);
1551 reg_w(sd, 0xa4, 0x06);
1552 reg_w(sd, 0x23, 0x1f);
1555 reg_w(sd, 0xa4, 0x06);
1556 reg_w(sd, 0x23, 0x1b);
1560 case SEN_OV7670: /* guesses, based on 7640 */
1561 PDEBUG(D_STREAM, "Setting framerate to %d fps",
1562 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
1563 reg_w(sd, 0xa4, 0x10);
1564 switch (sd->frame_rate) {
1566 reg_w(sd, 0x23, 0xff);
1569 reg_w(sd, 0x23, 0x1b);
1573 reg_w(sd, 0x23, 0xff);
1583 static int mode_init_ov_sensor_regs(struct sd *sd)
1585 struct gspca_dev *gspca_dev;
1588 gspca_dev = &sd->gspca_dev;
1589 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
1591 /******** Mode (VGA/QVGA) and sensor specific regs ********/
1592 switch (sd->sensor) {
1594 /* For OV8610 qvga means qsvga */
1595 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
1598 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1601 /* i2c_w(sd, 0x2b, 0x00); */
1602 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1603 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1604 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
1605 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
1606 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
1607 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
1608 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
1611 /* i2c_w(sd, 0x2b, 0x00); */
1612 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1615 /* i2c_w(sd, 0x2b, 0x00); */
1616 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1617 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1618 /* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
1619 /* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
1620 /* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
1621 /* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
1622 /* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
1625 /* set COM7_FMT_VGA or COM7_FMT_QVGA
1626 * do we need to set anything else?
1627 * HSTART etc are set in set_ov_sensor_window itself */
1628 i2c_w_mask(sd, OV7670_REG_COM7,
1629 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
1630 OV7670_COM7_FMT_MASK);
1634 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1640 /******** Palette-specific regs ********/
1641 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1642 /* not valid on the OV6620/OV7620/6630? */
1643 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
1646 /* The OV518 needs special treatment. Although both the OV518
1647 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
1648 * bus is actually used. The UV bus is tied to ground.
1649 * Therefore, the OV6630 needs to be in 8-bit multiplexed
1652 /* OV7640 is 8-bit only */
1654 if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV7640)
1655 i2c_w_mask(sd, 0x13, 0x00, 0x20);
1657 /******** Clock programming ********/
1658 /* The OV6620 needs special handling. This prevents the
1659 * severe banding that normally occurs */
1660 if (sd->sensor == SEN_OV6620) {
1663 i2c_w(sd, 0x2a, 0x04);
1664 i2c_w(sd, 0x11, sd->clockdiv);
1665 i2c_w(sd, 0x2a, 0x84);
1666 /* This next setting is critical. It seems to improve
1667 * the gain or the contrast. The "reserved" bits seem
1668 * to have some effect in this case. */
1669 i2c_w(sd, 0x2d, 0x85);
1670 } else if (sd->clockdiv >= 0) {
1671 i2c_w(sd, 0x11, sd->clockdiv);
1674 /******** Special Features ********/
1675 /* no evidence this is possible with OV7670, either */
1677 if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
1678 i2c_w_mask(sd, 0x12, 0x00, 0x02);
1680 /* Enable auto white balance */
1681 if (sd->sensor == SEN_OV7670)
1682 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
1685 i2c_w_mask(sd, 0x12, 0x04, 0x04);
1687 /* This will go away as soon as ov51x_mode_init_sensor_regs() */
1688 /* is fully tested. */
1689 /* 7620/6620/6630? don't have register 0x35, so play it safe */
1690 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1692 i2c_w(sd, 0x35, 0x9e);
1694 i2c_w(sd, 0x35, 0x1e);
1699 static void sethvflip(struct sd *sd)
1701 if (sd->sensor != SEN_OV7670)
1703 if (sd->gspca_dev.streaming)
1705 i2c_w_mask(sd, OV7670_REG_MVFP,
1706 OV7670_MVFP_MIRROR * sd->hflip
1707 | OV7670_MVFP_VFLIP * sd->vflip,
1708 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
1709 if (sd->gspca_dev.streaming)
1713 static int set_ov_sensor_window(struct sd *sd)
1715 struct gspca_dev *gspca_dev;
1717 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
1718 int ret, hstart, hstop, vstop, vstart;
1721 gspca_dev = &sd->gspca_dev;
1722 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
1724 /* The different sensor ICs handle setting up of window differently.
1725 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
1726 switch (sd->sensor) {
1737 vwsbase = vwebase = 0x05;
1747 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
1749 vwsbase = vwebase = 0x05;
1754 vwsbase = vwebase = 0x03;
1757 /*handling of OV7670 hardware sensor start and stop values
1758 * is very odd, compared to the other OV sensors */
1759 vwsbase = vwebase = hwebase = hwsbase = 0x00;
1765 switch (sd->sensor) {
1768 if (qvga) { /* QCIF */
1773 vwscale = 1; /* The datasheet says 0;
1778 if (qvga) { /* QSVGA */
1786 default: /* SEN_OV7xx0 */
1787 if (qvga) { /* QVGA */
1796 ret = mode_init_ov_sensor_regs(sd);
1800 if (sd->sensor == SEN_OV8610) {
1801 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
1802 /* old 0x95, new 0x05 from windrv 090403 */
1803 /* bits 5-7: reserved */
1804 i2c_w_mask(sd, 0x28, 0x20, 0x20);
1805 /* bit 5: progressive mode on */
1808 /* The below is wrong for OV7670s because their window registers
1809 * only store the high bits in 0x17 to 0x1a */
1811 /* SRH Use sd->max values instead of requested win values */
1812 /* SCS Since we're sticking with only the max hardware widths
1813 * for a given mode */
1814 /* I can hard code this for OV7670s */
1815 /* Yes, these numbers do look odd, but they're tested and work! */
1816 if (sd->sensor == SEN_OV7670) {
1817 if (qvga) { /* QVGA from ov7670.c by
1818 * Jonathan Corbet */
1829 /* OV7670 hardware window registers are split across
1830 * multiple locations */
1831 i2c_w(sd, OV7670_REG_HSTART, hstart >> 3);
1832 i2c_w(sd, OV7670_REG_HSTOP, hstop >> 3);
1833 v = i2c_r(sd, OV7670_REG_HREF);
1834 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
1835 msleep(10); /* need to sleep between read and write to
1837 i2c_w(sd, OV7670_REG_HREF, v);
1839 i2c_w(sd, OV7670_REG_VSTART, vstart >> 2);
1840 i2c_w(sd, OV7670_REG_VSTOP, vstop >> 2);
1841 v = i2c_r(sd, OV7670_REG_VREF);
1842 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
1843 msleep(10); /* need to sleep between read and write to
1845 i2c_w(sd, OV7670_REG_VREF, v);
1848 i2c_w(sd, 0x17, hwsbase);
1849 i2c_w(sd, 0x18, hwebase + (sd->gspca_dev.width >> hwscale));
1850 i2c_w(sd, 0x19, vwsbase);
1851 i2c_w(sd, 0x1a, vwebase + (sd->gspca_dev.height >> vwscale));
1856 /* -- start the camera -- */
1857 static int sd_start(struct gspca_dev *gspca_dev)
1859 struct sd *sd = (struct sd *) gspca_dev;
1862 ret = ov519_mode_init_regs(sd);
1865 ret = set_ov_sensor_window(sd);
1869 ret = ov51x_restart(sd);
1872 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
1873 ov51x_led_control(sd, 1);
1876 PDEBUG(D_ERR, "camera start error:%d", ret);
1880 static void sd_stopN(struct gspca_dev *gspca_dev)
1882 ov51x_stop((struct sd *) gspca_dev);
1883 ov51x_led_control((struct sd *) gspca_dev, 0);
1886 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1887 struct gspca_frame *frame, /* target */
1888 __u8 *data, /* isoc packet */
1889 int len) /* iso packet length */
1891 /* Header of ov519 is 16 bytes:
1892 * Byte Value Description
1896 * 3 0xXX 0x50 = SOF, 0x51 = EOF
1897 * 9 0xXX 0x01 initial frame without data,
1898 * 0x00 standard frame with image
1899 * 14 Lo in EOF: length of image data / 8
1903 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
1905 case 0x50: /* start of frame */
1910 if (data[0] == 0xff || data[1] == 0xd8)
1911 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
1914 gspca_dev->last_packet_type = DISCARD_PACKET;
1916 case 0x51: /* end of frame */
1918 gspca_dev->last_packet_type = DISCARD_PACKET;
1919 gspca_frame_add(gspca_dev, LAST_PACKET, frame,
1925 /* intermediate packet */
1926 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
1930 /* -- management routines -- */
1932 static void setbrightness(struct gspca_dev *gspca_dev)
1934 struct sd *sd = (struct sd *) gspca_dev;
1937 val = sd->brightness;
1938 PDEBUG(D_CONF, "brightness:%d", val);
1939 /* if (gspca_dev->streaming)
1940 * ov51x_stop(sd); */
1941 switch (sd->sensor) {
1948 i2c_w(sd, OV7610_REG_BRT, val);
1951 /* 7620 doesn't like manual changes when in auto mode */
1953 * if (!sd->auto_brt) */
1954 i2c_w(sd, OV7610_REG_BRT, val);
1958 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
1959 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
1962 /* if (gspca_dev->streaming)
1963 * ov51x_restart(sd); */
1966 static void setcontrast(struct gspca_dev *gspca_dev)
1968 struct sd *sd = (struct sd *) gspca_dev;
1972 PDEBUG(D_CONF, "contrast:%d", val);
1973 /* if (gspca_dev->streaming)
1975 switch (sd->sensor) {
1978 i2c_w(sd, OV7610_REG_CNT, val);
1981 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
1983 static const __u8 ctab[] = {
1984 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
1987 /* Use Y gamma control instead. Bit 0 enables it. */
1988 i2c_w(sd, 0x64, ctab[val >> 5]);
1992 static const __u8 ctab[] = {
1993 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1994 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1997 /* Use Y gamma control instead. Bit 0 enables it. */
1998 i2c_w(sd, 0x64, ctab[val >> 4]);
2002 /* Use gain control instead. */
2003 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
2006 /* check that this isn't just the same as ov7610 */
2007 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
2010 /* if (gspca_dev->streaming)
2011 ov51x_restart(sd); */
2014 static void setcolors(struct gspca_dev *gspca_dev)
2016 struct sd *sd = (struct sd *) gspca_dev;
2020 PDEBUG(D_CONF, "saturation:%d", val);
2021 /* if (gspca_dev->streaming)
2023 switch (sd->sensor) {
2029 i2c_w(sd, OV7610_REG_SAT, val);
2032 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
2033 /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
2036 i2c_w(sd, OV7610_REG_SAT, val);
2039 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
2042 /* supported later once I work out how to do it
2043 * transparently fail now! */
2044 /* set REG_COM13 values for UV sat auto mode */
2047 /* if (gspca_dev->streaming)
2048 ov51x_restart(sd); */
2051 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2053 struct sd *sd = (struct sd *) gspca_dev;
2055 sd->brightness = val;
2056 setbrightness(gspca_dev);
2060 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2062 struct sd *sd = (struct sd *) gspca_dev;
2064 *val = sd->brightness;
2068 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2070 struct sd *sd = (struct sd *) gspca_dev;
2073 setcontrast(gspca_dev);
2077 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2079 struct sd *sd = (struct sd *) gspca_dev;
2081 *val = sd->contrast;
2085 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2087 struct sd *sd = (struct sd *) gspca_dev;
2090 setcolors(gspca_dev);
2094 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2096 struct sd *sd = (struct sd *) gspca_dev;
2102 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
2104 struct sd *sd = (struct sd *) gspca_dev;
2111 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
2113 struct sd *sd = (struct sd *) gspca_dev;
2119 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2121 struct sd *sd = (struct sd *) gspca_dev;
2128 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2130 struct sd *sd = (struct sd *) gspca_dev;
2136 /* sub-driver description */
2137 static const struct sd_desc sd_desc = {
2138 .name = MODULE_NAME,
2140 .nctrls = ARRAY_SIZE(sd_ctrls),
2141 .config = sd_config,
2145 .pkt_scan = sd_pkt_scan,
2148 /* -- module initialisation -- */
2149 static const __devinitdata struct usb_device_id device_table[] = {
2150 {USB_DEVICE(0x041e, 0x4052)},
2151 {USB_DEVICE(0x041e, 0x405f)},
2152 {USB_DEVICE(0x041e, 0x4060)},
2153 {USB_DEVICE(0x041e, 0x4061)},
2154 {USB_DEVICE(0x041e, 0x4064)},
2155 {USB_DEVICE(0x041e, 0x4068)},
2156 {USB_DEVICE(0x045e, 0x028c)},
2157 {USB_DEVICE(0x054c, 0x0154)},
2158 {USB_DEVICE(0x054c, 0x0155)},
2159 {USB_DEVICE(0x05a9, 0x0519)},
2160 {USB_DEVICE(0x05a9, 0x0530)},
2161 {USB_DEVICE(0x05a9, 0x4519)},
2162 {USB_DEVICE(0x05a9, 0x8519)},
2166 MODULE_DEVICE_TABLE(usb, device_table);
2168 /* -- device connect -- */
2169 static int sd_probe(struct usb_interface *intf,
2170 const struct usb_device_id *id)
2172 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2176 static struct usb_driver sd_driver = {
2177 .name = MODULE_NAME,
2178 .id_table = device_table,
2180 .disconnect = gspca_disconnect,
2182 .suspend = gspca_suspend,
2183 .resume = gspca_resume,
2187 /* -- module insert / remove -- */
2188 static int __init sd_mod_init(void)
2190 if (usb_register(&sd_driver) < 0)
2192 PDEBUG(D_PROBE, "registered");
2195 static void __exit sd_mod_exit(void)
2197 usb_deregister(&sd_driver);
2198 PDEBUG(D_PROBE, "deregistered");
2201 module_init(sd_mod_init);
2202 module_exit(sd_mod_exit);
2204 module_param(frame_rate, int, 0644);
2205 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");