4 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
6 * This module is adapted from the ov51x-jpeg package, which itself
7 * was adapted from the ov511 driver.
9 * Original copyright for the ov511 driver is:
11 * Copyright (c) 1999-2004 Mark W. McClelland
12 * Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach
14 * ov51x-jpeg original copyright is:
16 * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
17 * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #define MODULE_NAME "ov519"
38 MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
39 MODULE_DESCRIPTION("OV519 USB Camera Driver");
40 MODULE_LICENSE("GPL");
42 /* global parameters */
43 static int frame_rate;
45 /* Number of times to retry a failed I2C transaction. Increase this if you
46 * are getting "Failed to read sensor ID..." */
47 static int i2c_detect_tries = 10;
49 /* ov519 device descriptor */
51 struct gspca_dev gspca_dev; /* !! must be the first item */
56 #define BRIDGE_OV511 0
57 #define BRIDGE_OV511PLUS 1
58 #define BRIDGE_OV518 2
59 #define BRIDGE_OV518PLUS 3
60 #define BRIDGE_OV519 4
64 #define BRIDGE_INVERT_LED 8
66 /* Determined by sensor type */
77 __u8 stopped; /* Streaming is temporarily paused */
79 __u8 frame_rate; /* current Framerate (OV519 only) */
80 __u8 clockdiv; /* clockdiv override for OV519 only */
82 char sensor; /* Type of image sensor chip (SEN_*) */
86 #define SEN_OV66308AF 3
95 /* V4L2 controls supported by the driver */
96 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
97 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
98 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
99 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
100 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
101 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
102 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
103 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
104 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
105 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
106 static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val);
107 static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val);
108 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
109 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
110 static void setbrightness(struct gspca_dev *gspca_dev);
111 static void setcontrast(struct gspca_dev *gspca_dev);
112 static void setcolors(struct gspca_dev *gspca_dev);
113 static void setautobrightness(struct sd *sd);
114 static void setfreq(struct sd *sd);
116 static const struct ctrl sd_ctrls[] = {
119 .id = V4L2_CID_BRIGHTNESS,
120 .type = V4L2_CTRL_TYPE_INTEGER,
121 .name = "Brightness",
125 #define BRIGHTNESS_DEF 127
126 .default_value = BRIGHTNESS_DEF,
128 .set = sd_setbrightness,
129 .get = sd_getbrightness,
133 .id = V4L2_CID_CONTRAST,
134 .type = V4L2_CTRL_TYPE_INTEGER,
139 #define CONTRAST_DEF 127
140 .default_value = CONTRAST_DEF,
142 .set = sd_setcontrast,
143 .get = sd_getcontrast,
147 .id = V4L2_CID_SATURATION,
148 .type = V4L2_CTRL_TYPE_INTEGER,
153 #define COLOR_DEF 127
154 .default_value = COLOR_DEF,
159 /* The flip controls work with ov7670 only */
163 .id = V4L2_CID_HFLIP,
164 .type = V4L2_CTRL_TYPE_BOOLEAN,
170 .default_value = HFLIP_DEF,
178 .id = V4L2_CID_VFLIP,
179 .type = V4L2_CTRL_TYPE_BOOLEAN,
185 .default_value = VFLIP_DEF,
190 #define AUTOBRIGHT_IDX 5
193 .id = V4L2_CID_AUTOBRIGHTNESS,
194 .type = V4L2_CTRL_TYPE_BOOLEAN,
195 .name = "Auto Brightness",
199 #define AUTOBRIGHT_DEF 1
200 .default_value = AUTOBRIGHT_DEF,
202 .set = sd_setautobrightness,
203 .get = sd_getautobrightness,
208 .id = V4L2_CID_POWER_LINE_FREQUENCY,
209 .type = V4L2_CTRL_TYPE_MENU,
210 .name = "Light frequency filter",
212 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
215 .default_value = FREQ_DEF,
220 #define OV7670_FREQ_IDX 7
223 .id = V4L2_CID_POWER_LINE_FREQUENCY,
224 .type = V4L2_CTRL_TYPE_MENU,
225 .name = "Light frequency filter",
227 .maximum = 3, /* 0: 0, 1: 50Hz, 2:60Hz 3: Auto Hz */
229 #define OV7670_FREQ_DEF 3
230 .default_value = OV7670_FREQ_DEF,
237 static const struct v4l2_pix_format ov519_vga_mode[] = {
238 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
240 .sizeimage = 320 * 240 * 3 / 8 + 590,
241 .colorspace = V4L2_COLORSPACE_JPEG,
243 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
245 .sizeimage = 640 * 480 * 3 / 8 + 590,
246 .colorspace = V4L2_COLORSPACE_JPEG,
249 static const struct v4l2_pix_format ov519_sif_mode[] = {
250 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
252 .sizeimage = 160 * 120 * 3 / 8 + 590,
253 .colorspace = V4L2_COLORSPACE_JPEG,
255 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
257 .sizeimage = 176 * 144 * 3 / 8 + 590,
258 .colorspace = V4L2_COLORSPACE_JPEG,
260 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
262 .sizeimage = 320 * 240 * 3 / 8 + 590,
263 .colorspace = V4L2_COLORSPACE_JPEG,
265 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
267 .sizeimage = 352 * 288 * 3 / 8 + 590,
268 .colorspace = V4L2_COLORSPACE_JPEG,
272 static const struct v4l2_pix_format ov518_vga_mode[] = {
273 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
275 .sizeimage = 320 * 240 * 3 / 8 + 590,
276 .colorspace = V4L2_COLORSPACE_JPEG,
278 {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
280 .sizeimage = 640 * 480 * 3 / 8 + 590,
281 .colorspace = V4L2_COLORSPACE_JPEG,
284 static const struct v4l2_pix_format ov518_sif_mode[] = {
285 {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
288 .colorspace = V4L2_COLORSPACE_JPEG,
290 {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
293 .colorspace = V4L2_COLORSPACE_JPEG,
295 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
297 .sizeimage = 320 * 240 * 3 / 8 + 590,
298 .colorspace = V4L2_COLORSPACE_JPEG,
300 {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
302 .sizeimage = 352 * 288 * 3 / 8 + 590,
303 .colorspace = V4L2_COLORSPACE_JPEG,
308 /* Registers common to OV511 / OV518 */
309 #define R51x_SYS_RESET 0x50
310 #define R51x_SYS_INIT 0x53
311 #define R51x_SYS_SNAP 0x52
312 #define R51x_SYS_CUST_ID 0x5F
313 #define R51x_COMP_LUT_BEGIN 0x80
315 /* OV511 Camera interface register numbers */
316 #define R511_SYS_LED_CTL 0x55 /* OV511+ only */
317 #define OV511_RESET_NOREGS 0x3F /* All but OV511 & regs */
319 /* OV518 Camera interface register numbers */
320 #define R518_GPIO_OUT 0x56 /* OV518(+) only */
321 #define R518_GPIO_CTL 0x57 /* OV518(+) only */
323 /* OV519 Camera interface register numbers */
324 #define OV519_R10_H_SIZE 0x10
325 #define OV519_R11_V_SIZE 0x11
326 #define OV519_R12_X_OFFSETL 0x12
327 #define OV519_R13_X_OFFSETH 0x13
328 #define OV519_R14_Y_OFFSETL 0x14
329 #define OV519_R15_Y_OFFSETH 0x15
330 #define OV519_R16_DIVIDER 0x16
331 #define OV519_R20_DFR 0x20
332 #define OV519_R25_FORMAT 0x25
334 /* OV519 System Controller register numbers */
335 #define OV519_SYS_RESET1 0x51
336 #define OV519_SYS_EN_CLK1 0x54
338 #define OV519_GPIO_DATA_OUT0 0x71
339 #define OV519_GPIO_IO_CTRL0 0x72
341 #define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
344 #define R51x_I2C_W_SID 0x41
345 #define R51x_I2C_SADDR_3 0x42
346 #define R51x_I2C_SADDR_2 0x43
347 #define R51x_I2C_R_SID 0x44
348 #define R51x_I2C_DATA 0x45
349 #define R518_I2C_CTL 0x47 /* OV518(+) only */
352 #define OV7xx0_SID 0x42
353 #define OV8xx0_SID 0xa0
354 #define OV6xx0_SID 0xc0
356 /* OV7610 registers */
357 #define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
358 #define OV7610_REG_BLUE 0x01 /* blue channel balance */
359 #define OV7610_REG_RED 0x02 /* red channel balance */
360 #define OV7610_REG_SAT 0x03 /* saturation */
361 #define OV8610_REG_HUE 0x04 /* 04 reserved */
362 #define OV7610_REG_CNT 0x05 /* Y contrast */
363 #define OV7610_REG_BRT 0x06 /* Y brightness */
364 #define OV7610_REG_COM_C 0x14 /* misc common regs */
365 #define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
366 #define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
367 #define OV7610_REG_COM_I 0x29 /* misc settings */
369 /* OV7670 registers */
370 #define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
371 #define OV7670_REG_BLUE 0x01 /* blue gain */
372 #define OV7670_REG_RED 0x02 /* red gain */
373 #define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
374 #define OV7670_REG_COM1 0x04 /* Control 1 */
375 #define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
376 #define OV7670_REG_COM3 0x0c /* Control 3 */
377 #define OV7670_REG_COM4 0x0d /* Control 4 */
378 #define OV7670_REG_COM5 0x0e /* All "reserved" */
379 #define OV7670_REG_COM6 0x0f /* Control 6 */
380 #define OV7670_REG_AECH 0x10 /* More bits of AEC value */
381 #define OV7670_REG_CLKRC 0x11 /* Clock control */
382 #define OV7670_REG_COM7 0x12 /* Control 7 */
383 #define OV7670_COM7_FMT_VGA 0x00
384 #define OV7670_COM7_YUV 0x00 /* YUV */
385 #define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
386 #define OV7670_COM7_FMT_MASK 0x38
387 #define OV7670_COM7_RESET 0x80 /* Register reset */
388 #define OV7670_REG_COM8 0x13 /* Control 8 */
389 #define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
390 #define OV7670_COM8_AWB 0x02 /* White balance enable */
391 #define OV7670_COM8_AGC 0x04 /* Auto gain enable */
392 #define OV7670_COM8_BFILT 0x20 /* Band filter enable */
393 #define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
394 #define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
395 #define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
396 #define OV7670_REG_COM10 0x15 /* Control 10 */
397 #define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
398 #define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
399 #define OV7670_REG_VSTART 0x19 /* Vert start high bits */
400 #define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
401 #define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
402 #define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
403 #define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
404 #define OV7670_REG_AEW 0x24 /* AGC upper limit */
405 #define OV7670_REG_AEB 0x25 /* AGC lower limit */
406 #define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
407 #define OV7670_REG_HREF 0x32 /* HREF pieces */
408 #define OV7670_REG_TSLB 0x3a /* lots of stuff */
409 #define OV7670_REG_COM11 0x3b /* Control 11 */
410 #define OV7670_COM11_EXP 0x02
411 #define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
412 #define OV7670_REG_COM12 0x3c /* Control 12 */
413 #define OV7670_REG_COM13 0x3d /* Control 13 */
414 #define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
415 #define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
416 #define OV7670_REG_COM14 0x3e /* Control 14 */
417 #define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
418 #define OV7670_REG_COM15 0x40 /* Control 15 */
419 #define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
420 #define OV7670_REG_COM16 0x41 /* Control 16 */
421 #define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
422 #define OV7670_REG_BRIGHT 0x55 /* Brightness */
423 #define OV7670_REG_CONTRAS 0x56 /* Contrast control */
424 #define OV7670_REG_GFIX 0x69 /* Fix gain control */
425 #define OV7670_REG_RGB444 0x8c /* RGB 444 control */
426 #define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
427 #define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
428 #define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
429 #define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
430 #define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
431 #define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
432 #define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
433 #define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
434 #define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
440 struct ov_i2c_regvals {
445 static const struct ov_i2c_regvals norm_6x20[] = {
446 { 0x12, 0x80 }, /* reset */
449 { 0x05, 0x7f }, /* For when autoadjust is off */
451 /* The ratio of 0x0c and 0x0d controls the white point */
454 { 0x0f, 0x15 }, /* COMS */
455 { 0x10, 0x75 }, /* AEC Exposure time */
456 { 0x12, 0x24 }, /* Enable AGC */
458 /* 0x16: 0x06 helps frame stability with moving objects */
460 /* { 0x20, 0x30 }, * Aperture correction enable */
461 { 0x26, 0xb2 }, /* BLC enable */
462 /* 0x28: 0x05 Selects RGB format if RGB on */
464 { 0x2a, 0x04 }, /* Disable framerate adjust */
465 /* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
467 { 0x33, 0xa0 }, /* Color Processing Parameter */
468 { 0x34, 0xd2 }, /* Max A/D range */
472 { 0x3c, 0x39 }, /* Enable AEC mode changing */
473 { 0x3c, 0x3c }, /* Change AEC mode */
474 { 0x3c, 0x24 }, /* Disable AEC mode changing */
477 /* These next two registers (0x4a, 0x4b) are undocumented.
478 * They control the color balance */
481 { 0x4d, 0xd2 }, /* This reduces noise a bit */
484 /* Do 50-53 have any effect? */
485 /* Toggle 0x12[2] off and on here? */
488 static const struct ov_i2c_regvals norm_6x30[] = {
489 { 0x12, 0x80 }, /* Reset */
490 { 0x00, 0x1f }, /* Gain */
491 { 0x01, 0x99 }, /* Blue gain */
492 { 0x02, 0x7c }, /* Red gain */
493 { 0x03, 0xc0 }, /* Saturation */
494 { 0x05, 0x0a }, /* Contrast */
495 { 0x06, 0x95 }, /* Brightness */
496 { 0x07, 0x2d }, /* Sharpness */
499 { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
502 { 0x11, 0x00 }, /* Pixel clock = fastest */
503 { 0x12, 0x24 }, /* Enable AGC and AWB */
518 { 0x23, 0xc0 }, /* Crystal circuit power level */
519 { 0x25, 0x9a }, /* Increase AEC black ratio */
520 { 0x26, 0xb2 }, /* BLC enable */
524 { 0x2a, 0x84 }, /* 60 Hz power */
525 { 0x2b, 0xa8 }, /* 60 Hz power */
527 { 0x2d, 0x95 }, /* Enable auto-brightness */
541 { 0x40, 0x00 }, /* White bal */
542 { 0x41, 0x00 }, /* White bal */
544 { 0x43, 0x3f }, /* White bal */
554 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
556 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
558 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
563 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
565 { 0x5b, 0x0f }, /* AWB chrominance levels */
569 { 0x12, 0x20 }, /* Toggle AWB */
573 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
575 * Register 0x0f in the 7610 has the following effects:
577 * 0x85 (AEC method 1): Best overall, good contrast range
578 * 0x45 (AEC method 2): Very overexposed
579 * 0xa5 (spec sheet default): Ok, but the black level is
580 * shifted resulting in loss of contrast
581 * 0x05 (old driver setting): very overexposed, too much
584 static const struct ov_i2c_regvals norm_7610[] = {
591 { 0x28, 0x24 }, /* 0c */
592 { 0x0f, 0x85 }, /* lg's setting */
614 static const struct ov_i2c_regvals norm_7620[] = {
615 { 0x00, 0x00 }, /* gain */
616 { 0x01, 0x80 }, /* blue gain */
617 { 0x02, 0x80 }, /* red gain */
618 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
680 /* 7640 and 7648. The defaults should be OK for most registers. */
681 static const struct ov_i2c_regvals norm_7640[] = {
686 /* 7670. Defaults taken from OmniVision provided data,
687 * as provided by Jonathan Corbet of OLPC */
688 static const struct ov_i2c_regvals norm_7670[] = {
689 { OV7670_REG_COM7, OV7670_COM7_RESET },
690 { OV7670_REG_TSLB, 0x04 }, /* OV */
691 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
692 { OV7670_REG_CLKRC, 0x01 },
694 * Set the hardware window. These values from OV don't entirely
695 * make sense - hstop is less than hstart. But they work...
697 { OV7670_REG_HSTART, 0x13 },
698 { OV7670_REG_HSTOP, 0x01 },
699 { OV7670_REG_HREF, 0xb6 },
700 { OV7670_REG_VSTART, 0x02 },
701 { OV7670_REG_VSTOP, 0x7a },
702 { OV7670_REG_VREF, 0x0a },
704 { OV7670_REG_COM3, 0x00 },
705 { OV7670_REG_COM14, 0x00 },
706 /* Mystery scaling numbers */
712 /* { OV7670_REG_COM10, 0x0 }, */
714 /* Gamma curve values */
732 /* AGC and AEC parameters. Note we start by disabling those features,
733 then turn them only after tweaking the values. */
734 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
735 | OV7670_COM8_AECSTEP
736 | OV7670_COM8_BFILT },
737 { OV7670_REG_GAIN, 0x00 },
738 { OV7670_REG_AECH, 0x00 },
739 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
740 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
741 { OV7670_REG_BD50MAX, 0x05 },
742 { OV7670_REG_BD60MAX, 0x07 },
743 { OV7670_REG_AEW, 0x95 },
744 { OV7670_REG_AEB, 0x33 },
745 { OV7670_REG_VPT, 0xe3 },
746 { OV7670_REG_HAECC1, 0x78 },
747 { OV7670_REG_HAECC2, 0x68 },
748 { 0xa1, 0x03 }, /* magic */
749 { OV7670_REG_HAECC3, 0xd8 },
750 { OV7670_REG_HAECC4, 0xd8 },
751 { OV7670_REG_HAECC5, 0xf0 },
752 { OV7670_REG_HAECC6, 0x90 },
753 { OV7670_REG_HAECC7, 0x94 },
754 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
755 | OV7670_COM8_AECSTEP
760 /* Almost all of these are magic "reserved" values. */
761 { OV7670_REG_COM5, 0x61 },
762 { OV7670_REG_COM6, 0x4b },
764 { OV7670_REG_MVFP, 0x07 },
773 { OV7670_REG_COM12, 0x78 },
776 { OV7670_REG_GFIX, 0x00 },
792 /* More reserved magic, some of which tweaks white balance */
809 /* "9e for advance AWB" */
811 { OV7670_REG_BLUE, 0x40 },
812 { OV7670_REG_RED, 0x60 },
813 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
814 | OV7670_COM8_AECSTEP
820 /* Matrix coefficients */
829 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
830 { OV7670_REG_EDGE, 0x00 },
835 { OV7670_REG_COM13, OV7670_COM13_GAMMA
840 { OV7670_REG_COM16, 0x38 },
844 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
857 /* Extra-weird stuff. Some sort of multiplexor register */
883 static const struct ov_i2c_regvals norm_8610[] = {
890 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
891 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
900 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
902 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
903 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
904 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
907 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
908 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
909 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
910 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
916 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
918 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
920 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
922 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
923 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
924 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
925 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
927 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
928 * maybe thats wrong */
932 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
936 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
937 * deleting bit7 colors the first images red */
938 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
939 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
945 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
947 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
952 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
954 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
955 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
962 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
968 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
971 static unsigned char ov7670_abs_to_sm(unsigned char v)
975 return (128 - v) | 0x80;
978 /* Write a OV519 register */
979 static int reg_w(struct sd *sd, __u16 index, __u8 value)
982 int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 2 : 1;
984 sd->gspca_dev.usb_buf[0] = value;
985 ret = usb_control_msg(sd->gspca_dev.dev,
986 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
988 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
990 sd->gspca_dev.usb_buf, 1, 500);
992 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
996 /* Read from a OV519 register */
997 /* returns: negative is error, pos or zero is data */
998 static int reg_r(struct sd *sd, __u16 index)
1001 int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 3 : 1;
1003 ret = usb_control_msg(sd->gspca_dev.dev,
1004 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1006 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1007 0, index, sd->gspca_dev.usb_buf, 1, 500);
1010 ret = sd->gspca_dev.usb_buf[0];
1012 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
1016 /* Read 8 values from a OV519 register */
1017 static int reg_r8(struct sd *sd,
1022 ret = usb_control_msg(sd->gspca_dev.dev,
1023 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1025 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1026 0, index, sd->gspca_dev.usb_buf, 8, 500);
1029 ret = sd->gspca_dev.usb_buf[0];
1031 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
1036 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1037 * the same position as 1's in "mask" are cleared and set to "value". Bits
1038 * that are in the same position as 0's in "mask" are preserved, regardless
1039 * of their respective state in "value".
1041 static int reg_w_mask(struct sd *sd,
1050 value &= mask; /* Enforce mask on value */
1051 ret = reg_r(sd, index);
1055 oldval = ret & ~mask; /* Clear the masked bits */
1056 value |= oldval; /* Set the desired bits */
1058 return reg_w(sd, index, value);
1062 * Writes multiple (n) byte value to a single register. Only valid with certain
1063 * registers (0x30 and 0xc4 - 0xce).
1065 static int ov518_reg_w32(struct sd *sd, __u16 index, u32 value, int n)
1069 *((u32 *)sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
1071 ret = usb_control_msg(sd->gspca_dev.dev,
1072 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1074 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1076 sd->gspca_dev.usb_buf, n, 500);
1078 PDEBUG(D_ERR, "Write reg32 [%02x] %08x failed", index, value);
1084 * The OV518 I2C I/O procedure is different, hence, this function.
1085 * This is normally only called from i2c_w(). Note that this function
1086 * always succeeds regardless of whether the sensor is present and working.
1088 static int i2c_w(struct sd *sd,
1094 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
1096 /* Select camera register */
1097 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
1101 /* Write "value" to I2C data port of OV511 */
1102 rc = reg_w(sd, R51x_I2C_DATA, value);
1106 /* Initiate 3-byte write cycle */
1107 rc = reg_w(sd, R518_I2C_CTL, 0x01);
1111 /* wait for write complete */
1113 return reg_r8(sd, R518_I2C_CTL);
1117 * returns: negative is error, pos or zero is data
1119 * The OV518 I2C I/O procedure is different, hence, this function.
1120 * This is normally only called from i2c_r(). Note that this function
1121 * always succeeds regardless of whether the sensor is present and working.
1123 static int i2c_r(struct sd *sd, __u8 reg)
1127 /* Select camera register */
1128 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
1132 /* Initiate 2-byte write cycle */
1133 rc = reg_w(sd, R518_I2C_CTL, 0x03);
1137 /* Initiate 2-byte read cycle */
1138 rc = reg_w(sd, R518_I2C_CTL, 0x05);
1141 value = reg_r(sd, R51x_I2C_DATA);
1142 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
1146 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
1147 * the same position as 1's in "mask" are cleared and set to "value". Bits
1148 * that are in the same position as 0's in "mask" are preserved, regardless
1149 * of their respective state in "value".
1151 static int i2c_w_mask(struct sd *sd,
1159 value &= mask; /* Enforce mask on value */
1160 rc = i2c_r(sd, reg);
1163 oldval = rc & ~mask; /* Clear the masked bits */
1164 value |= oldval; /* Set the desired bits */
1165 return i2c_w(sd, reg, value);
1168 /* Temporarily stops OV511 from functioning. Must do this before changing
1169 * registers while the camera is streaming */
1170 static inline int ov51x_stop(struct sd *sd)
1172 PDEBUG(D_STREAM, "stopping");
1174 switch (sd->bridge) {
1176 case BRIDGE_OV511PLUS:
1177 return reg_w(sd, R51x_SYS_RESET, 0x3d);
1179 case BRIDGE_OV518PLUS:
1180 return reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
1182 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
1188 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
1189 * actually stopped (for performance). */
1190 static inline int ov51x_restart(struct sd *sd)
1194 PDEBUG(D_STREAM, "restarting");
1199 /* Reinitialize the stream */
1200 switch (sd->bridge) {
1202 case BRIDGE_OV511PLUS:
1203 return reg_w(sd, R51x_SYS_RESET, 0x00);
1205 case BRIDGE_OV518PLUS:
1206 rc = reg_w(sd, 0x2f, 0x80);
1209 return reg_w(sd, R51x_SYS_RESET, 0x00);
1211 return reg_w(sd, OV519_SYS_RESET1, 0x00);
1217 /* This does an initial reset of an OmniVision sensor and ensures that I2C
1218 * is synchronized. Returns <0 on failure.
1220 static int init_ov_sensor(struct sd *sd)
1224 /* Reset the sensor */
1225 if (i2c_w(sd, 0x12, 0x80) < 0)
1228 /* Wait for it to initialize */
1231 for (i = 0; i < i2c_detect_tries; i++) {
1232 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
1233 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
1234 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
1238 /* Reset the sensor */
1239 if (i2c_w(sd, 0x12, 0x80) < 0)
1241 /* Wait for it to initialize */
1243 /* Dummy read to sync I2C */
1244 if (i2c_r(sd, 0x00) < 0)
1250 /* Set the read and write slave IDs. The "slave" argument is the write slave,
1251 * and the read slave will be set to (slave + 1).
1252 * This should not be called from outside the i2c I/O functions.
1253 * Sets I2C read and write slave IDs. Returns <0 for error
1255 static int ov51x_set_slave_ids(struct sd *sd,
1260 rc = reg_w(sd, R51x_I2C_W_SID, slave);
1263 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
1266 static int write_regvals(struct sd *sd,
1267 const struct ov_regvals *regvals,
1273 rc = reg_w(sd, regvals->reg, regvals->val);
1281 static int write_i2c_regvals(struct sd *sd,
1282 const struct ov_i2c_regvals *regvals,
1288 rc = i2c_w(sd, regvals->reg, regvals->val);
1296 /****************************************************************************
1298 * OV511 and sensor configuration
1300 ***************************************************************************/
1302 /* This initializes the OV8110, OV8610 sensor. The OV8110 uses
1303 * the same register settings as the OV8610, since they are very similar.
1305 static int ov8xx0_configure(struct sd *sd)
1309 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
1311 /* Detect sensor (sub)type */
1312 rc = i2c_r(sd, OV7610_REG_COM_I);
1314 PDEBUG(D_ERR, "Error detecting sensor type");
1317 if ((rc & 3) == 1) {
1318 sd->sensor = SEN_OV8610;
1320 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1324 /* Set sensor-specific vars */
1325 /* sd->sif = 0; already done */
1329 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
1330 * the same register settings as the OV7610, since they are very similar.
1332 static int ov7xx0_configure(struct sd *sd)
1337 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
1339 /* Detect sensor (sub)type */
1340 rc = i2c_r(sd, OV7610_REG_COM_I);
1343 * it appears to be wrongly detected as a 7610 by default */
1345 PDEBUG(D_ERR, "Error detecting sensor type");
1348 if ((rc & 3) == 3) {
1349 /* quick hack to make OV7670s work */
1350 high = i2c_r(sd, 0x0a);
1351 low = i2c_r(sd, 0x0b);
1352 /* info("%x, %x", high, low); */
1353 if (high == 0x76 && low == 0x73) {
1354 PDEBUG(D_PROBE, "Sensor is an OV7670");
1355 sd->sensor = SEN_OV7670;
1357 PDEBUG(D_PROBE, "Sensor is an OV7610");
1358 sd->sensor = SEN_OV7610;
1360 } else if ((rc & 3) == 1) {
1361 /* I don't know what's different about the 76BE yet. */
1362 if (i2c_r(sd, 0x15) & 1)
1363 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
1365 PDEBUG(D_PROBE, "Sensor is an OV76BE");
1367 /* OV511+ will return all zero isoc data unless we
1368 * configure the sensor as a 7620. Someone needs to
1369 * find the exact reg. setting that causes this. */
1370 sd->sensor = SEN_OV76BE;
1371 } else if ((rc & 3) == 0) {
1372 /* try to read product id registers */
1373 high = i2c_r(sd, 0x0a);
1375 PDEBUG(D_ERR, "Error detecting camera chip PID");
1378 low = i2c_r(sd, 0x0b);
1380 PDEBUG(D_ERR, "Error detecting camera chip VER");
1386 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
1388 "7630 is not supported by this driver");
1391 PDEBUG(D_PROBE, "Sensor is an OV7645");
1392 sd->sensor = SEN_OV7640; /* FIXME */
1395 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1396 sd->sensor = SEN_OV7640; /* FIXME */
1399 PDEBUG(D_PROBE, "Sensor is an OV7648");
1400 sd->sensor = SEN_OV7640; /* FIXME */
1403 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
1407 PDEBUG(D_PROBE, "Sensor is an OV7620");
1408 sd->sensor = SEN_OV7620;
1411 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1415 /* Set sensor-specific vars */
1416 /* sd->sif = 0; already done */
1420 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1421 static int ov6xx0_configure(struct sd *sd)
1424 PDEBUG(D_PROBE, "starting OV6xx0 configuration");
1426 /* Detect sensor (sub)type */
1427 rc = i2c_r(sd, OV7610_REG_COM_I);
1429 PDEBUG(D_ERR, "Error detecting sensor type");
1433 /* Ugh. The first two bits are the version bits, but
1434 * the entire register value must be used. I guess OVT
1435 * underestimated how many variants they would make. */
1438 sd->sensor = SEN_OV6630;
1440 "WARNING: Sensor is an OV66308. Your camera may have");
1441 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1444 sd->sensor = SEN_OV6620;
1445 PDEBUG(D_PROBE, "Sensor is an OV6620");
1448 sd->sensor = SEN_OV6630;
1449 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
1452 sd->sensor = SEN_OV66308AF;
1453 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
1456 sd->sensor = SEN_OV6630;
1458 "WARNING: Sensor is an OV66307. Your camera may have");
1459 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
1462 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1466 /* Set sensor-specific vars */
1472 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1473 static void ov51x_led_control(struct sd *sd, int on)
1478 switch (sd->bridge) {
1479 /* OV511 has no LED control */
1480 case BRIDGE_OV511PLUS:
1481 reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
1484 case BRIDGE_OV518PLUS:
1485 reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
1488 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
1493 /* OV518 quantization tables are 8x4 (instead of 8x8) */
1494 static int ov518_upload_quan_tables(struct sd *sd)
1496 const unsigned char yQuanTable518[] = {
1497 5, 4, 5, 6, 6, 7, 7, 7,
1498 5, 5, 5, 5, 6, 7, 7, 7,
1499 6, 6, 6, 6, 7, 7, 7, 8,
1500 7, 7, 6, 7, 7, 7, 8, 8
1503 const unsigned char uvQuanTable518[] = {
1504 6, 6, 6, 7, 7, 7, 7, 7,
1505 6, 6, 6, 7, 7, 7, 7, 7,
1506 6, 6, 6, 7, 7, 7, 7, 8,
1507 7, 7, 7, 7, 7, 7, 8, 8
1510 const unsigned char *pYTable = yQuanTable518;
1511 const unsigned char *pUVTable = uvQuanTable518;
1512 unsigned char val0, val1;
1513 int i, rc, reg = R51x_COMP_LUT_BEGIN;
1515 PDEBUG(D_PROBE, "Uploading quantization tables");
1517 for (i = 0; i < 16; i++) {
1523 rc = reg_w(sd, reg, val0);
1532 rc = reg_w(sd, reg + 16, val0);
1542 /* This initializes the OV518/OV518+ and the sensor */
1543 static int ov518_configure(struct gspca_dev *gspca_dev)
1545 struct sd *sd = (struct sd *) gspca_dev;
1548 /* For 518 and 518+ */
1549 static struct ov_regvals init_518[] = {
1550 { R51x_SYS_RESET, 0x40 },
1551 { R51x_SYS_INIT, 0xe1 },
1552 { R51x_SYS_RESET, 0x3e },
1553 { R51x_SYS_INIT, 0xe1 },
1554 { R51x_SYS_RESET, 0x00 },
1555 { R51x_SYS_INIT, 0xe1 },
1560 static struct ov_regvals norm_518[] = {
1561 { R51x_SYS_SNAP, 0x02 }, /* Reset */
1562 { R51x_SYS_SNAP, 0x01 }, /* Enable */
1573 static struct ov_regvals norm_518_p[] = {
1574 { R51x_SYS_SNAP, 0x02 }, /* Reset */
1575 { R51x_SYS_SNAP, 0x01 }, /* Enable */
1592 /* First 5 bits of custom ID reg are a revision ID on OV518 */
1593 PDEBUG(D_PROBE, "Device revision %d",
1594 0x1F & reg_r(sd, R51x_SYS_CUST_ID));
1596 rc = write_regvals(sd, init_518, ARRAY_SIZE(init_518));
1600 /* Set LED GPIO pin to output mode */
1601 rc = reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
1605 switch (sd->bridge) {
1607 rc = write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
1611 case BRIDGE_OV518PLUS:
1612 rc = write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
1618 rc = ov518_upload_quan_tables(sd);
1620 PDEBUG(D_ERR, "Error uploading quantization tables");
1624 rc = reg_w(sd, 0x2f, 0x80);
1631 static int ov519_configure(struct sd *sd)
1633 static const struct ov_regvals init_519[] = {
1634 { 0x5a, 0x6d }, /* EnableSystem */
1636 { 0x54, 0xff }, /* set bit2 to enable jpeg */
1640 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1641 * detection will fail. This deserves further investigation. */
1642 { OV519_GPIO_IO_CTRL0, 0xee },
1643 { 0x51, 0x0f }, /* SetUsbInit */
1646 /* windows reads 0x55 at this point*/
1649 return write_regvals(sd, init_519, ARRAY_SIZE(init_519));
1652 /* this function is called at probe time */
1653 static int sd_config(struct gspca_dev *gspca_dev,
1654 const struct usb_device_id *id)
1656 struct sd *sd = (struct sd *) gspca_dev;
1660 sd->bridge = id->driver_info & BRIDGE_MASK;
1661 sd->invert_led = id->driver_info & BRIDGE_INVERT_LED;
1663 switch (sd->bridge) {
1665 case BRIDGE_OV518PLUS:
1666 ret = ov518_configure(gspca_dev);
1669 ret = ov519_configure(sd);
1676 ov51x_led_control(sd, 0); /* turn LED off */
1679 if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1682 /* The OV519 must be more aggressive about sensor detection since
1683 * I2C write will never fail if the sensor is not present. We have
1684 * to try to initialize the sensor to detect its presence */
1685 if (init_ov_sensor(sd) >= 0) {
1686 if (ov7xx0_configure(sd) < 0) {
1687 PDEBUG(D_ERR, "Failed to configure OV7xx0");
1693 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1696 if (init_ov_sensor(sd) >= 0) {
1697 if (ov6xx0_configure(sd) < 0) {
1698 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1704 if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1707 if (init_ov_sensor(sd) < 0) {
1709 "Can't determine sensor slave IDs");
1712 if (ov8xx0_configure(sd) < 0) {
1714 "Failed to configure OV8xx0 sensor");
1720 cam = &gspca_dev->cam;
1721 switch (sd->bridge) {
1723 case BRIDGE_OV518PLUS:
1725 cam->cam_mode = ov518_vga_mode;
1726 cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
1728 cam->cam_mode = ov518_sif_mode;
1729 cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
1734 cam->cam_mode = ov519_vga_mode;
1735 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
1737 cam->cam_mode = ov519_sif_mode;
1738 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
1742 sd->brightness = BRIGHTNESS_DEF;
1743 sd->contrast = CONTRAST_DEF;
1744 sd->colors = COLOR_DEF;
1745 sd->hflip = HFLIP_DEF;
1746 sd->vflip = VFLIP_DEF;
1747 sd->autobrightness = AUTOBRIGHT_DEF;
1748 if (sd->sensor == SEN_OV7670) {
1749 sd->freq = OV7670_FREQ_DEF;
1750 gspca_dev->ctrl_dis = 1 << FREQ_IDX;
1752 sd->freq = FREQ_DEF;
1753 gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX) |
1754 (1 << OV7670_FREQ_IDX);
1756 if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
1757 gspca_dev->ctrl_dis |= 1 << AUTOBRIGHT_IDX;
1758 /* OV8610 Frequency filter control should work but needs testing */
1759 if (sd->sensor == SEN_OV8610)
1760 gspca_dev->ctrl_dis |= 1 << FREQ_IDX;
1764 PDEBUG(D_ERR, "OV519 Config failed");
1768 /* this function is called at probe and resume time */
1769 static int sd_init(struct gspca_dev *gspca_dev)
1771 struct sd *sd = (struct sd *) gspca_dev;
1773 /* initialize the sensor */
1774 switch (sd->sensor) {
1776 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
1781 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
1785 /* case SEN_OV7610: */
1786 /* case SEN_OV76BE: */
1787 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
1791 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
1795 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
1799 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
1803 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
1810 /* Sets up the OV518/OV518+ with the given image parameters
1812 * OV518 needs a completely different approach, until we can figure out what
1813 * the individual registers do. Also, only 15 FPS is supported now.
1815 * Do not put any sensor-specific code in here (including I2C I/O functions)
1817 static int ov518_mode_init_regs(struct sd *sd)
1821 /******** Set the mode ********/
1832 if (sd->bridge == BRIDGE_OV518) {
1833 /* Set 8-bit (YVYU) input format */
1834 reg_w_mask(sd, 0x20, 0x08, 0x08);
1836 /* Set 12-bit (4:2:0) output format */
1837 reg_w_mask(sd, 0x28, 0x80, 0xf0);
1838 reg_w_mask(sd, 0x38, 0x80, 0xf0);
1840 reg_w(sd, 0x28, 0x80);
1841 reg_w(sd, 0x38, 0x80);
1844 hsegs = sd->gspca_dev.width / 16;
1845 vsegs = sd->gspca_dev.height / 4;
1847 reg_w(sd, 0x29, hsegs);
1848 reg_w(sd, 0x2a, vsegs);
1850 reg_w(sd, 0x39, hsegs);
1851 reg_w(sd, 0x3a, vsegs);
1853 /* Windows driver does this here; who knows why */
1854 reg_w(sd, 0x2f, 0x80);
1856 /******** Set the framerate (to 30 FPS) ********/
1857 if (sd->bridge == BRIDGE_OV518PLUS)
1862 /* Mode independent, but framerate dependent, regs */
1863 reg_w(sd, 0x51, 0x04); /* Clock divider; lower==faster */
1864 reg_w(sd, 0x22, 0x18);
1865 reg_w(sd, 0x23, 0xff);
1867 if (sd->bridge == BRIDGE_OV518PLUS)
1868 reg_w(sd, 0x21, 0x19);
1870 reg_w(sd, 0x71, 0x17); /* Compression-related? */
1872 /* FIXME: Sensor-specific */
1873 /* Bit 5 is what matters here. Of course, it is "reserved" */
1874 i2c_w(sd, 0x54, 0x23);
1876 reg_w(sd, 0x2f, 0x80);
1878 if (sd->bridge == BRIDGE_OV518PLUS) {
1879 reg_w(sd, 0x24, 0x94);
1880 reg_w(sd, 0x25, 0x90);
1881 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
1882 ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */
1883 ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */
1884 ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */
1885 ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */
1886 ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */
1887 ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */
1888 ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */
1889 ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */
1891 reg_w(sd, 0x24, 0x9f);
1892 reg_w(sd, 0x25, 0x90);
1893 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
1894 ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */
1895 ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */
1896 ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */
1897 ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */
1898 ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */
1899 ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */
1900 ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */
1901 ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */
1904 reg_w(sd, 0x2f, 0x80);
1910 /* Sets up the OV519 with the given image parameters
1912 * OV519 needs a completely different approach, until we can figure out what
1913 * the individual registers do.
1915 * Do not put any sensor-specific code in here (including I2C I/O functions)
1917 static int ov519_mode_init_regs(struct sd *sd)
1919 static const struct ov_regvals mode_init_519_ov7670[] = {
1920 { 0x5d, 0x03 }, /* Turn off suspend mode */
1921 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1922 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1923 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1927 { 0x37, 0x00 }, /* SetUsbInit */
1928 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1929 /* Enable both fields, YUV Input, disable defect comp (why?) */
1933 { 0x17, 0x50 }, /* undocumented */
1934 { 0x37, 0x00 }, /* undocumented */
1935 { 0x40, 0xff }, /* I2C timeout counter */
1936 { 0x46, 0x00 }, /* I2C clock prescaler */
1937 { 0x59, 0x04 }, /* new from windrv 090403 */
1938 { 0xff, 0x00 }, /* undocumented */
1939 /* windows reads 0x55 at this point, why? */
1942 static const struct ov_regvals mode_init_519[] = {
1943 { 0x5d, 0x03 }, /* Turn off suspend mode */
1944 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1945 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1946 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1950 { 0x37, 0x00 }, /* SetUsbInit */
1951 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1952 /* Enable both fields, YUV Input, disable defect comp (why?) */
1954 { 0x17, 0x50 }, /* undocumented */
1955 { 0x37, 0x00 }, /* undocumented */
1956 { 0x40, 0xff }, /* I2C timeout counter */
1957 { 0x46, 0x00 }, /* I2C clock prescaler */
1958 { 0x59, 0x04 }, /* new from windrv 090403 */
1959 { 0xff, 0x00 }, /* undocumented */
1960 /* windows reads 0x55 at this point, why? */
1963 /******** Set the mode ********/
1964 if (sd->sensor != SEN_OV7670) {
1965 if (write_regvals(sd, mode_init_519,
1966 ARRAY_SIZE(mode_init_519)))
1968 if (sd->sensor == SEN_OV7640) {
1969 /* Select 8-bit input mode */
1970 reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
1973 if (write_regvals(sd, mode_init_519_ov7670,
1974 ARRAY_SIZE(mode_init_519_ov7670)))
1978 reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);
1979 reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);
1980 if (sd->sensor == SEN_OV7670 &&
1981 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
1982 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
1984 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
1985 reg_w(sd, OV519_R13_X_OFFSETH, 0x00);
1986 reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);
1987 reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);
1988 reg_w(sd, OV519_R16_DIVIDER, 0x00);
1989 reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */
1990 reg_w(sd, 0x26, 0x00); /* Undocumented */
1992 /******** Set the framerate ********/
1994 sd->frame_rate = frame_rate;
1996 /* FIXME: These are only valid at the max resolution. */
1998 switch (sd->sensor) {
2000 switch (sd->frame_rate) {
2003 reg_w(sd, 0xa4, 0x0c);
2004 reg_w(sd, 0x23, 0xff);
2007 reg_w(sd, 0xa4, 0x0c);
2008 reg_w(sd, 0x23, 0x1f);
2011 reg_w(sd, 0xa4, 0x0c);
2012 reg_w(sd, 0x23, 0x1b);
2015 reg_w(sd, 0xa4, 0x04);
2016 reg_w(sd, 0x23, 0xff);
2020 reg_w(sd, 0xa4, 0x04);
2021 reg_w(sd, 0x23, 0x1f);
2025 reg_w(sd, 0xa4, 0x04);
2026 reg_w(sd, 0x23, 0x1b);
2032 switch (sd->frame_rate) {
2033 default: /* 15 fps */
2035 reg_w(sd, 0xa4, 0x06);
2036 reg_w(sd, 0x23, 0xff);
2039 reg_w(sd, 0xa4, 0x06);
2040 reg_w(sd, 0x23, 0x1f);
2043 reg_w(sd, 0xa4, 0x06);
2044 reg_w(sd, 0x23, 0x1b);
2048 case SEN_OV7670: /* guesses, based on 7640 */
2049 PDEBUG(D_STREAM, "Setting framerate to %d fps",
2050 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
2051 reg_w(sd, 0xa4, 0x10);
2052 switch (sd->frame_rate) {
2054 reg_w(sd, 0x23, 0xff);
2057 reg_w(sd, 0x23, 0x1b);
2061 reg_w(sd, 0x23, 0xff);
2070 static int mode_init_ov_sensor_regs(struct sd *sd)
2072 struct gspca_dev *gspca_dev;
2075 gspca_dev = &sd->gspca_dev;
2076 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
2078 /******** Mode (VGA/QVGA) and sensor specific regs ********/
2079 switch (sd->sensor) {
2081 /* For OV8610 qvga means qsvga */
2082 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
2085 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2088 /* i2c_w(sd, 0x2b, 0x00); */
2089 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2090 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
2091 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
2092 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
2093 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
2094 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
2095 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
2098 /* i2c_w(sd, 0x2b, 0x00); */
2099 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2102 /* i2c_w(sd, 0x2b, 0x00); */
2103 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2104 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
2105 /* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
2106 /* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
2107 /* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
2108 /* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
2109 /* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
2112 /* set COM7_FMT_VGA or COM7_FMT_QVGA
2113 * do we need to set anything else?
2114 * HSTART etc are set in set_ov_sensor_window itself */
2115 i2c_w_mask(sd, OV7670_REG_COM7,
2116 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
2117 OV7670_COM7_FMT_MASK);
2122 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2128 /******** Palette-specific regs ********/
2129 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
2130 /* not valid on the OV6620/OV7620/6630? */
2131 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
2134 /* The OV518 needs special treatment. Although both the OV518
2135 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
2136 * bus is actually used. The UV bus is tied to ground.
2137 * Therefore, the OV6630 needs to be in 8-bit multiplexed
2140 /* OV7640 is 8-bit only */
2142 if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV66308AF &&
2143 sd->sensor != SEN_OV7640)
2144 i2c_w_mask(sd, 0x13, 0x00, 0x20);
2146 /******** Clock programming ********/
2147 /* The OV6620 needs special handling. This prevents the
2148 * severe banding that normally occurs */
2149 if (sd->sensor == SEN_OV6620) {
2152 i2c_w(sd, 0x2a, 0x04);
2153 i2c_w(sd, 0x11, sd->clockdiv);
2154 i2c_w(sd, 0x2a, 0x84);
2155 /* This next setting is critical. It seems to improve
2156 * the gain or the contrast. The "reserved" bits seem
2157 * to have some effect in this case. */
2158 i2c_w(sd, 0x2d, 0x85);
2160 i2c_w(sd, 0x11, sd->clockdiv);
2163 /******** Special Features ********/
2164 /* no evidence this is possible with OV7670, either */
2166 if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
2167 i2c_w_mask(sd, 0x12, 0x00, 0x02);
2169 /* Enable auto white balance */
2170 if (sd->sensor == SEN_OV7670)
2171 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
2174 i2c_w_mask(sd, 0x12, 0x04, 0x04);
2176 /* This will go away as soon as ov51x_mode_init_sensor_regs() */
2177 /* is fully tested. */
2178 /* 7620/6620/6630? don't have register 0x35, so play it safe */
2179 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
2181 i2c_w(sd, 0x35, 0x9e);
2183 i2c_w(sd, 0x35, 0x1e);
2188 static void sethvflip(struct sd *sd)
2190 if (sd->sensor != SEN_OV7670)
2192 if (sd->gspca_dev.streaming)
2194 i2c_w_mask(sd, OV7670_REG_MVFP,
2195 OV7670_MVFP_MIRROR * sd->hflip
2196 | OV7670_MVFP_VFLIP * sd->vflip,
2197 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
2198 if (sd->gspca_dev.streaming)
2202 static int set_ov_sensor_window(struct sd *sd)
2204 struct gspca_dev *gspca_dev;
2206 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
2207 int ret, hstart, hstop, vstop, vstart;
2210 gspca_dev = &sd->gspca_dev;
2211 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
2212 crop = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 2;
2214 /* The different sensor ICs handle setting up of window differently.
2215 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
2216 switch (sd->sensor) {
2227 vwsbase = vwebase = 0x05;
2236 if (sd->sensor == SEN_OV66308AF && qvga)
2237 /* HDG: this fixes U and V getting swapped */
2247 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
2249 vwsbase = vwebase = 0x05;
2254 vwsbase = vwebase = 0x03;
2257 /*handling of OV7670 hardware sensor start and stop values
2258 * is very odd, compared to the other OV sensors */
2259 vwsbase = vwebase = hwebase = hwsbase = 0x00;
2265 switch (sd->sensor) {
2269 if (qvga) { /* QCIF */
2274 vwscale = 1; /* The datasheet says 0;
2279 if (qvga) { /* QSVGA */
2287 default: /* SEN_OV7xx0 */
2288 if (qvga) { /* QVGA */
2297 ret = mode_init_ov_sensor_regs(sd);
2301 if (sd->sensor == SEN_OV8610) {
2302 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
2303 /* old 0x95, new 0x05 from windrv 090403 */
2304 /* bits 5-7: reserved */
2305 i2c_w_mask(sd, 0x28, 0x20, 0x20);
2306 /* bit 5: progressive mode on */
2309 /* The below is wrong for OV7670s because their window registers
2310 * only store the high bits in 0x17 to 0x1a */
2312 /* SRH Use sd->max values instead of requested win values */
2313 /* SCS Since we're sticking with only the max hardware widths
2314 * for a given mode */
2315 /* I can hard code this for OV7670s */
2316 /* Yes, these numbers do look odd, but they're tested and work! */
2317 if (sd->sensor == SEN_OV7670) {
2318 if (qvga) { /* QVGA from ov7670.c by
2319 * Jonathan Corbet */
2330 /* OV7670 hardware window registers are split across
2331 * multiple locations */
2332 i2c_w(sd, OV7670_REG_HSTART, hstart >> 3);
2333 i2c_w(sd, OV7670_REG_HSTOP, hstop >> 3);
2334 v = i2c_r(sd, OV7670_REG_HREF);
2335 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
2336 msleep(10); /* need to sleep between read and write to
2338 i2c_w(sd, OV7670_REG_HREF, v);
2340 i2c_w(sd, OV7670_REG_VSTART, vstart >> 2);
2341 i2c_w(sd, OV7670_REG_VSTOP, vstop >> 2);
2342 v = i2c_r(sd, OV7670_REG_VREF);
2343 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
2344 msleep(10); /* need to sleep between read and write to
2346 i2c_w(sd, OV7670_REG_VREF, v);
2348 i2c_w(sd, 0x17, hwsbase);
2349 i2c_w(sd, 0x18, hwebase + (sd->gspca_dev.width >> hwscale));
2350 i2c_w(sd, 0x19, vwsbase);
2351 i2c_w(sd, 0x1a, vwebase + (sd->gspca_dev.height >> vwscale));
2356 /* -- start the camera -- */
2357 static int sd_start(struct gspca_dev *gspca_dev)
2359 struct sd *sd = (struct sd *) gspca_dev;
2362 switch (sd->bridge) {
2364 case BRIDGE_OV518PLUS:
2365 ret = ov518_mode_init_regs(sd);
2368 ret = ov519_mode_init_regs(sd);
2374 ret = set_ov_sensor_window(sd);
2378 setcontrast(gspca_dev);
2379 setbrightness(gspca_dev);
2380 setcolors(gspca_dev);
2382 setautobrightness(sd);
2385 ret = ov51x_restart(sd);
2388 ov51x_led_control(sd, 1);
2391 PDEBUG(D_ERR, "camera start error:%d", ret);
2395 static void sd_stopN(struct gspca_dev *gspca_dev)
2397 struct sd *sd = (struct sd *) gspca_dev;
2400 ov51x_led_control(sd, 0);
2403 static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
2404 struct gspca_frame *frame, /* target */
2405 __u8 *data, /* isoc packet */
2406 int len) /* iso packet length */
2408 struct sd *sd = (struct sd *) gspca_dev;
2410 /* A false positive here is likely, until OVT gives me
2411 * the definitive SOF/EOF format */
2412 if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
2413 gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
2414 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, 0);
2418 if (gspca_dev->last_packet_type == DISCARD_PACKET)
2421 /* Does this device use packet numbers ? */
2424 if (sd->packet_nr == data[len])
2426 /* The last few packets of the frame (which are all 0's
2427 except that they may contain part of the footer), are
2429 else if (sd->packet_nr == 0 || data[len]) {
2430 PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
2431 (int)data[len], (int)sd->packet_nr);
2432 gspca_dev->last_packet_type = DISCARD_PACKET;
2437 /* intermediate packet */
2438 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
2441 static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
2442 struct gspca_frame *frame, /* target */
2443 __u8 *data, /* isoc packet */
2444 int len) /* iso packet length */
2446 /* Header of ov519 is 16 bytes:
2447 * Byte Value Description
2451 * 3 0xXX 0x50 = SOF, 0x51 = EOF
2452 * 9 0xXX 0x01 initial frame without data,
2453 * 0x00 standard frame with image
2454 * 14 Lo in EOF: length of image data / 8
2458 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
2460 case 0x50: /* start of frame */
2465 if (data[0] == 0xff || data[1] == 0xd8)
2466 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
2469 gspca_dev->last_packet_type = DISCARD_PACKET;
2471 case 0x51: /* end of frame */
2473 gspca_dev->last_packet_type = DISCARD_PACKET;
2474 gspca_frame_add(gspca_dev, LAST_PACKET, frame,
2480 /* intermediate packet */
2481 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
2485 static void sd_pkt_scan(struct gspca_dev *gspca_dev,
2486 struct gspca_frame *frame, /* target */
2487 __u8 *data, /* isoc packet */
2488 int len) /* iso packet length */
2490 struct sd *sd = (struct sd *) gspca_dev;
2492 switch (sd->bridge) {
2494 case BRIDGE_OV511PLUS:
2497 case BRIDGE_OV518PLUS:
2498 ov518_pkt_scan(gspca_dev, frame, data, len);
2501 ov519_pkt_scan(gspca_dev, frame, data, len);
2506 /* -- management routines -- */
2508 static void setbrightness(struct gspca_dev *gspca_dev)
2510 struct sd *sd = (struct sd *) gspca_dev;
2513 val = sd->brightness;
2514 switch (sd->sensor) {
2522 i2c_w(sd, OV7610_REG_BRT, val);
2525 /* 7620 doesn't like manual changes when in auto mode */
2526 if (!sd->autobrightness)
2527 i2c_w(sd, OV7610_REG_BRT, val);
2531 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
2532 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
2537 static void setcontrast(struct gspca_dev *gspca_dev)
2539 struct sd *sd = (struct sd *) gspca_dev;
2543 switch (sd->sensor) {
2546 i2c_w(sd, OV7610_REG_CNT, val);
2550 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
2553 static const __u8 ctab[] = {
2554 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
2557 /* Use Y gamma control instead. Bit 0 enables it. */
2558 i2c_w(sd, 0x64, ctab[val >> 5]);
2562 static const __u8 ctab[] = {
2563 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
2564 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
2567 /* Use Y gamma control instead. Bit 0 enables it. */
2568 i2c_w(sd, 0x64, ctab[val >> 4]);
2572 /* Use gain control instead. */
2573 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
2576 /* check that this isn't just the same as ov7610 */
2577 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
2582 static void setcolors(struct gspca_dev *gspca_dev)
2584 struct sd *sd = (struct sd *) gspca_dev;
2588 switch (sd->sensor) {
2595 i2c_w(sd, OV7610_REG_SAT, val);
2598 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
2599 /* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
2602 i2c_w(sd, OV7610_REG_SAT, val);
2605 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
2608 /* supported later once I work out how to do it
2609 * transparently fail now! */
2610 /* set REG_COM13 values for UV sat auto mode */
2615 static void setautobrightness(struct sd *sd)
2617 if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
2620 i2c_w_mask(sd, 0x2d, sd->autobrightness ? 0x10 : 0x00, 0x10);
2623 static void setfreq(struct sd *sd)
2625 if (sd->sensor == SEN_OV7670) {
2627 case 0: /* Banding filter disabled */
2628 i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_BFILT);
2631 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2633 i2c_w_mask(sd, OV7670_REG_COM11, 0x08, 0x18);
2636 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2638 i2c_w_mask(sd, OV7670_REG_COM11, 0x00, 0x18);
2640 case 3: /* Auto hz */
2641 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2643 i2c_w_mask(sd, OV7670_REG_COM11, OV7670_COM11_HZAUTO,
2649 case 0: /* Banding filter disabled */
2650 i2c_w_mask(sd, 0x2d, 0x00, 0x04);
2651 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
2653 case 1: /* 50 hz (filter on and framerate adj) */
2654 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
2655 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
2656 /* 20 fps -> 16.667 fps */
2657 if (sd->sensor == SEN_OV6620 ||
2658 sd->sensor == SEN_OV6630 ||
2659 sd->sensor == SEN_OV66308AF)
2660 i2c_w(sd, 0x2b, 0x5e);
2662 i2c_w(sd, 0x2b, 0xac);
2664 case 2: /* 60 hz (filter on, ...) */
2665 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
2666 if (sd->sensor == SEN_OV6620 ||
2667 sd->sensor == SEN_OV6630 ||
2668 sd->sensor == SEN_OV66308AF) {
2669 /* 20 fps -> 15 fps */
2670 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
2671 i2c_w(sd, 0x2b, 0xa8);
2673 /* no framerate adj. */
2674 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
2681 static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2683 struct sd *sd = (struct sd *) gspca_dev;
2685 sd->brightness = val;
2686 if (gspca_dev->streaming)
2687 setbrightness(gspca_dev);
2691 static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2693 struct sd *sd = (struct sd *) gspca_dev;
2695 *val = sd->brightness;
2699 static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2701 struct sd *sd = (struct sd *) gspca_dev;
2704 if (gspca_dev->streaming)
2705 setcontrast(gspca_dev);
2709 static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2711 struct sd *sd = (struct sd *) gspca_dev;
2713 *val = sd->contrast;
2717 static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2719 struct sd *sd = (struct sd *) gspca_dev;
2722 if (gspca_dev->streaming)
2723 setcolors(gspca_dev);
2727 static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2729 struct sd *sd = (struct sd *) gspca_dev;
2735 static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
2737 struct sd *sd = (struct sd *) gspca_dev;
2740 if (gspca_dev->streaming)
2745 static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
2747 struct sd *sd = (struct sd *) gspca_dev;
2753 static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2755 struct sd *sd = (struct sd *) gspca_dev;
2758 if (gspca_dev->streaming)
2763 static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2765 struct sd *sd = (struct sd *) gspca_dev;
2771 static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val)
2773 struct sd *sd = (struct sd *) gspca_dev;
2775 sd->autobrightness = val;
2776 if (gspca_dev->streaming)
2777 setautobrightness(sd);
2781 static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val)
2783 struct sd *sd = (struct sd *) gspca_dev;
2785 *val = sd->autobrightness;
2789 static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
2791 struct sd *sd = (struct sd *) gspca_dev;
2794 if (gspca_dev->streaming)
2799 static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
2801 struct sd *sd = (struct sd *) gspca_dev;
2807 static int sd_querymenu(struct gspca_dev *gspca_dev,
2808 struct v4l2_querymenu *menu)
2810 struct sd *sd = (struct sd *) gspca_dev;
2813 case V4L2_CID_POWER_LINE_FREQUENCY:
2814 switch (menu->index) {
2815 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
2816 strcpy((char *) menu->name, "NoFliker");
2818 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
2819 strcpy((char *) menu->name, "50 Hz");
2821 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
2822 strcpy((char *) menu->name, "60 Hz");
2825 if (sd->sensor != SEN_OV7670)
2828 strcpy((char *) menu->name, "Automatic");
2836 /* sub-driver description */
2837 static const struct sd_desc sd_desc = {
2838 .name = MODULE_NAME,
2840 .nctrls = ARRAY_SIZE(sd_ctrls),
2841 .config = sd_config,
2845 .pkt_scan = sd_pkt_scan,
2846 .querymenu = sd_querymenu,
2849 /* -- module initialisation -- */
2850 static const __devinitdata struct usb_device_id device_table[] = {
2851 {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
2852 {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
2853 {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
2854 {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
2855 {USB_DEVICE(0x041e, 0x4064),
2856 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
2857 {USB_DEVICE(0x041e, 0x4068),
2858 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
2859 {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
2860 {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
2861 {USB_DEVICE(0x054c, 0x0155), .driver_info = BRIDGE_OV519 },
2862 {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
2863 {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
2864 {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
2865 {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
2866 {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
2867 {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
2871 MODULE_DEVICE_TABLE(usb, device_table);
2873 /* -- device connect -- */
2874 static int sd_probe(struct usb_interface *intf,
2875 const struct usb_device_id *id)
2877 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2881 static struct usb_driver sd_driver = {
2882 .name = MODULE_NAME,
2883 .id_table = device_table,
2885 .disconnect = gspca_disconnect,
2887 .suspend = gspca_suspend,
2888 .resume = gspca_resume,
2892 /* -- module insert / remove -- */
2893 static int __init sd_mod_init(void)
2896 ret = usb_register(&sd_driver);
2899 PDEBUG(D_PROBE, "registered");
2902 static void __exit sd_mod_exit(void)
2904 usb_deregister(&sd_driver);
2905 PDEBUG(D_PROBE, "deregistered");
2908 module_init(sd_mod_init);
2909 module_exit(sd_mod_exit);
2911 module_param(frame_rate, int, 0644);
2912 MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");