]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/video/gspca/ov519.c
V4L/DVB (8818): gspca: Reinitialize the device on resume.
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / gspca / ov519.c
CommitLineData
6a7eba24
JFM
1/**
2 * OV519 driver
3 *
4 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
5 *
6 * (This module is adapted from the ov51x-jpeg package)
7 *
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
11 * any later version.
12 *
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.
17 *
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
21 *
22 */
23#define MODULE_NAME "ov519"
24
25#include "gspca.h"
26
6a7eba24
JFM
27MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
28MODULE_DESCRIPTION("OV519 USB Camera Driver");
29MODULE_LICENSE("GPL");
30
31/* global parameters */
32static int frame_rate;
33
34/* Number of times to retry a failed I2C transaction. Increase this if you
35 * are getting "Failed to read sensor ID..." */
36static int i2c_detect_tries = 10;
37
38/* ov519 device descriptor */
39struct sd {
40 struct gspca_dev gspca_dev; /* !! must be the first item */
41
42 /* Determined by sensor type */
594f5b8b 43 char sif;
6a7eba24
JFM
44
45 unsigned char primary_i2c_slave; /* I2C write id of sensor */
46
47 unsigned char brightness;
48 unsigned char contrast;
49 unsigned char colors;
0cd6759d
JFM
50 __u8 hflip;
51 __u8 vflip;
6a7eba24
JFM
52
53 char compress; /* Should the next frame be compressed? */
54 char compress_inited; /* Are compression params uploaded? */
55 char stopped; /* Streaming is temporarily paused */
56
57 char frame_rate; /* current Framerate (OV519 only) */
58 char clockdiv; /* clockdiv override for OV519 only */
59
60 char sensor; /* Type of image sensor chip (SEN_*) */
61#define SEN_UNKNOWN 0
62#define SEN_OV6620 1
63#define SEN_OV6630 2
64#define SEN_OV7610 3
65#define SEN_OV7620 4
66#define SEN_OV7630 5
67#define SEN_OV7640 6
68#define SEN_OV7670 7
69#define SEN_OV76BE 8
70#define SEN_OV8610 9
71
72};
73
74/* V4L2 controls supported by the driver */
75static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
76static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
77static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
78static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
79static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
80static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
0cd6759d
JFM
81static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
82static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
83static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
84static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
6a7eba24
JFM
85
86static struct ctrl sd_ctrls[] = {
6a7eba24
JFM
87 {
88 {
89 .id = V4L2_CID_BRIGHTNESS,
90 .type = V4L2_CTRL_TYPE_INTEGER,
91 .name = "Brightness",
92 .minimum = 0,
93 .maximum = 255,
94 .step = 1,
594f5b8b
JFM
95#define BRIGHTNESS_DEF 127
96 .default_value = BRIGHTNESS_DEF,
6a7eba24
JFM
97 },
98 .set = sd_setbrightness,
99 .get = sd_getbrightness,
100 },
6a7eba24
JFM
101 {
102 {
103 .id = V4L2_CID_CONTRAST,
104 .type = V4L2_CTRL_TYPE_INTEGER,
105 .name = "Contrast",
106 .minimum = 0,
107 .maximum = 255,
108 .step = 1,
594f5b8b
JFM
109#define CONTRAST_DEF 127
110 .default_value = CONTRAST_DEF,
6a7eba24
JFM
111 },
112 .set = sd_setcontrast,
113 .get = sd_getcontrast,
114 },
6a7eba24
JFM
115 {
116 {
117 .id = V4L2_CID_SATURATION,
118 .type = V4L2_CTRL_TYPE_INTEGER,
594f5b8b 119 .name = "Color",
6a7eba24
JFM
120 .minimum = 0,
121 .maximum = 255,
122 .step = 1,
594f5b8b
JFM
123#define COLOR_DEF 127
124 .default_value = COLOR_DEF,
6a7eba24
JFM
125 },
126 .set = sd_setcolors,
127 .get = sd_getcolors,
128 },
0cd6759d
JFM
129/* next controls work with ov7670 only */
130 {
131 {
132 .id = V4L2_CID_HFLIP,
133 .type = V4L2_CTRL_TYPE_BOOLEAN,
134 .name = "Mirror",
135 .minimum = 0,
136 .maximum = 1,
137 .step = 1,
138#define HFLIP_DEF 0
139 .default_value = HFLIP_DEF,
140 },
141 .set = sd_sethflip,
142 .get = sd_gethflip,
143 },
144 {
145 {
146 .id = V4L2_CID_VFLIP,
147 .type = V4L2_CTRL_TYPE_BOOLEAN,
148 .name = "Vflip",
149 .minimum = 0,
150 .maximum = 1,
151 .step = 1,
152#define VFLIP_DEF 0
153 .default_value = VFLIP_DEF,
154 },
155 .set = sd_setvflip,
156 .get = sd_getvflip,
157 },
6a7eba24
JFM
158};
159
c2446b3e
JFM
160static struct v4l2_pix_format vga_mode[] = {
161 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
162 .bytesperline = 320,
594f5b8b 163 .sizeimage = 320 * 240 * 3 / 8 + 590,
c2446b3e
JFM
164 .colorspace = V4L2_COLORSPACE_JPEG,
165 .priv = 1},
166 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
167 .bytesperline = 640,
168 .sizeimage = 640 * 480 * 3 / 8 + 590,
169 .colorspace = V4L2_COLORSPACE_JPEG,
170 .priv = 0},
6a7eba24 171};
c2446b3e
JFM
172static struct v4l2_pix_format sif_mode[] = {
173 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
174 .bytesperline = 176,
594f5b8b 175 .sizeimage = 176 * 144 * 3 / 8 + 590,
c2446b3e
JFM
176 .colorspace = V4L2_COLORSPACE_JPEG,
177 .priv = 1},
178 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
179 .bytesperline = 352,
594f5b8b 180 .sizeimage = 352 * 288 * 3 / 8 + 590,
c2446b3e
JFM
181 .colorspace = V4L2_COLORSPACE_JPEG,
182 .priv = 0},
6a7eba24
JFM
183};
184
185/* OV519 Camera interface register numbers */
186#define OV519_CAM_H_SIZE 0x10
187#define OV519_CAM_V_SIZE 0x11
188#define OV519_CAM_X_OFFSETL 0x12
189#define OV519_CAM_X_OFFSETH 0x13
190#define OV519_CAM_Y_OFFSETL 0x14
191#define OV519_CAM_Y_OFFSETH 0x15
192#define OV519_CAM_DIVIDER 0x16
193#define OV519_CAM_DFR 0x20
194#define OV519_CAM_FORMAT 0x25
195
196/* OV519 System Controller register numbers */
197#define OV519_SYS_RESET1 0x51
198#define OV519_SYS_EN_CLK1 0x54
199
200#define OV519_GPIO_DATA_OUT0 0x71
201#define OV519_GPIO_IO_CTRL0 0x72
202
203#define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
204
205/* I2C registers */
206#define R51x_I2C_W_SID 0x41
207#define R51x_I2C_SADDR_3 0x42
208#define R51x_I2C_SADDR_2 0x43
209#define R51x_I2C_R_SID 0x44
210#define R51x_I2C_DATA 0x45
211#define R518_I2C_CTL 0x47 /* OV518(+) only */
212
213/* I2C ADDRESSES */
214#define OV7xx0_SID 0x42
215#define OV8xx0_SID 0xa0
216#define OV6xx0_SID 0xc0
217
218/* OV7610 registers */
219#define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
220#define OV7610_REG_SAT 0x03 /* saturation */
221#define OV8610_REG_HUE 0x04 /* 04 reserved */
222#define OV7610_REG_CNT 0x05 /* Y contrast */
223#define OV7610_REG_BRT 0x06 /* Y brightness */
224#define OV7610_REG_COM_C 0x14 /* misc common regs */
225#define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
226#define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
227#define OV7610_REG_COM_I 0x29 /* misc settings */
228
229/* OV7670 registers */
230#define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
231#define OV7670_REG_BLUE 0x01 /* blue gain */
232#define OV7670_REG_RED 0x02 /* red gain */
233#define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
234#define OV7670_REG_COM1 0x04 /* Control 1 */
235#define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
236#define OV7670_REG_COM3 0x0c /* Control 3 */
237#define OV7670_REG_COM4 0x0d /* Control 4 */
238#define OV7670_REG_COM5 0x0e /* All "reserved" */
239#define OV7670_REG_COM6 0x0f /* Control 6 */
240#define OV7670_REG_AECH 0x10 /* More bits of AEC value */
241#define OV7670_REG_CLKRC 0x11 /* Clock control */
242#define OV7670_REG_COM7 0x12 /* Control 7 */
243#define OV7670_COM7_FMT_VGA 0x00
244#define OV7670_COM7_YUV 0x00 /* YUV */
245#define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
246#define OV7670_COM7_FMT_MASK 0x38
247#define OV7670_COM7_RESET 0x80 /* Register reset */
248#define OV7670_REG_COM8 0x13 /* Control 8 */
249#define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
250#define OV7670_COM8_AWB 0x02 /* White balance enable */
251#define OV7670_COM8_AGC 0x04 /* Auto gain enable */
252#define OV7670_COM8_BFILT 0x20 /* Band filter enable */
253#define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
254#define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
255#define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
256#define OV7670_REG_COM10 0x15 /* Control 10 */
257#define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
258#define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
259#define OV7670_REG_VSTART 0x19 /* Vert start high bits */
260#define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
261#define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
0cd6759d 262#define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
6a7eba24
JFM
263#define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
264#define OV7670_REG_AEW 0x24 /* AGC upper limit */
265#define OV7670_REG_AEB 0x25 /* AGC lower limit */
266#define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
267#define OV7670_REG_HREF 0x32 /* HREF pieces */
268#define OV7670_REG_TSLB 0x3a /* lots of stuff */
269#define OV7670_REG_COM11 0x3b /* Control 11 */
270#define OV7670_COM11_EXP 0x02
271#define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
272#define OV7670_REG_COM12 0x3c /* Control 12 */
273#define OV7670_REG_COM13 0x3d /* Control 13 */
274#define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
275#define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
276#define OV7670_REG_COM14 0x3e /* Control 14 */
277#define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
278#define OV7670_REG_COM15 0x40 /* Control 15 */
279#define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
280#define OV7670_REG_COM16 0x41 /* Control 16 */
281#define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
282#define OV7670_REG_BRIGHT 0x55 /* Brightness */
283#define OV7670_REG_CONTRAS 0x56 /* Contrast control */
284#define OV7670_REG_GFIX 0x69 /* Fix gain control */
285#define OV7670_REG_RGB444 0x8c /* RGB 444 control */
286#define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
287#define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
288#define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
289#define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
290#define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
291#define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
292#define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
293#define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
294#define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
295
6a7eba24
JFM
296static unsigned char ov7670_abs_to_sm(unsigned char v)
297{
298 if (v > 127)
299 return v & 0x7f;
300 return (128 - v) | 0x80;
301}
302
303/* Write a OV519 register */
304static int reg_w(struct sd *sd, __u16 index, __u8 value)
305{
306 int ret;
6a7eba24 307
739570bb 308 sd->gspca_dev.usb_buf[0] = value;
6a7eba24
JFM
309 ret = usb_control_msg(sd->gspca_dev.dev,
310 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
311 1, /* REQ_IO (ov518/519) */
312 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
313 0, index,
739570bb 314 sd->gspca_dev.usb_buf, 1, 500);
6a7eba24
JFM
315 if (ret < 0)
316 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
317 return ret;
318}
319
320/* Read from a OV519 register */
321/* returns: negative is error, pos or zero is data */
322static int reg_r(struct sd *sd, __u16 index)
323{
324 int ret;
6a7eba24
JFM
325
326 ret = usb_control_msg(sd->gspca_dev.dev,
327 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
328 1, /* REQ_IO */
329 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
739570bb 330 0, index, sd->gspca_dev.usb_buf, 1, 500);
6a7eba24
JFM
331
332 if (ret >= 0)
739570bb 333 ret = sd->gspca_dev.usb_buf[0];
6a7eba24
JFM
334 else
335 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
336 return ret;
337}
338
339/* Read 8 values from a OV519 register */
340static int reg_r8(struct sd *sd,
a5ae2062 341 __u16 index)
6a7eba24
JFM
342{
343 int ret;
6a7eba24
JFM
344
345 ret = usb_control_msg(sd->gspca_dev.dev,
346 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
347 1, /* REQ_IO */
348 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
739570bb 349 0, index, sd->gspca_dev.usb_buf, 8, 500);
6a7eba24
JFM
350
351 if (ret >= 0)
739570bb 352 ret = sd->gspca_dev.usb_buf[0];
6a7eba24
JFM
353 else
354 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
355 return ret;
356}
357
358/*
359 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
360 * the same position as 1's in "mask" are cleared and set to "value". Bits
361 * that are in the same position as 0's in "mask" are preserved, regardless
362 * of their respective state in "value".
363 */
364static int reg_w_mask(struct sd *sd,
365 __u16 index,
366 __u8 value,
367 __u8 mask)
368{
369 int ret;
370 __u8 oldval;
371
372 if (mask != 0xff) {
373 value &= mask; /* Enforce mask on value */
374 ret = reg_r(sd, index);
375 if (ret < 0)
376 return ret;
377
378 oldval = ret & ~mask; /* Clear the masked bits */
379 value |= oldval; /* Set the desired bits */
380 }
381 return reg_w(sd, index, value);
382}
383
384/*
385 * The OV518 I2C I/O procedure is different, hence, this function.
386 * This is normally only called from i2c_w(). Note that this function
387 * always succeeds regardless of whether the sensor is present and working.
388 */
389static int i2c_w(struct sd *sd,
390 __u8 reg,
391 __u8 value)
392{
393 int rc;
394
395 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
396
397 /* Select camera register */
398 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
399 if (rc < 0)
400 return rc;
401
402 /* Write "value" to I2C data port of OV511 */
403 rc = reg_w(sd, R51x_I2C_DATA, value);
404 if (rc < 0)
405 return rc;
406
407 /* Initiate 3-byte write cycle */
408 rc = reg_w(sd, R518_I2C_CTL, 0x01);
409
410 /* wait for write complete */
411 msleep(4);
412 if (rc < 0)
413 return rc;
414 return reg_r8(sd, R518_I2C_CTL);
415}
416
417/*
418 * returns: negative is error, pos or zero is data
419 *
420 * The OV518 I2C I/O procedure is different, hence, this function.
421 * This is normally only called from i2c_r(). Note that this function
422 * always succeeds regardless of whether the sensor is present and working.
423 */
424static int i2c_r(struct sd *sd, __u8 reg)
425{
426 int rc, value;
427
428 /* Select camera register */
429 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
430 if (rc < 0)
431 return rc;
432
433 /* Initiate 2-byte write cycle */
434 rc = reg_w(sd, R518_I2C_CTL, 0x03);
435 if (rc < 0)
436 return rc;
437
438 /* Initiate 2-byte read cycle */
439 rc = reg_w(sd, R518_I2C_CTL, 0x05);
440 if (rc < 0)
441 return rc;
442 value = reg_r(sd, R51x_I2C_DATA);
443 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
444 return value;
445}
446
447/* Writes bits at positions specified by mask to an I2C reg. Bits that are in
448 * the same position as 1's in "mask" are cleared and set to "value". Bits
449 * that are in the same position as 0's in "mask" are preserved, regardless
450 * of their respective state in "value".
451 */
452static int i2c_w_mask(struct sd *sd,
453 __u8 reg,
454 __u8 value,
455 __u8 mask)
456{
457 int rc;
458 __u8 oldval;
459
460 value &= mask; /* Enforce mask on value */
461 rc = i2c_r(sd, reg);
462 if (rc < 0)
463 return rc;
464 oldval = rc & ~mask; /* Clear the masked bits */
465 value |= oldval; /* Set the desired bits */
466 return i2c_w(sd, reg, value);
467}
468
469/* Temporarily stops OV511 from functioning. Must do this before changing
470 * registers while the camera is streaming */
471static inline int ov51x_stop(struct sd *sd)
472{
473 PDEBUG(D_STREAM, "stopping");
474 sd->stopped = 1;
475 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
476}
477
478/* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
479 * actually stopped (for performance). */
480static inline int ov51x_restart(struct sd *sd)
481{
482 PDEBUG(D_STREAM, "restarting");
483 if (!sd->stopped)
484 return 0;
485 sd->stopped = 0;
486
487 /* Reinitialize the stream */
488 return reg_w(sd, OV519_SYS_RESET1, 0x00);
489}
490
491/* This does an initial reset of an OmniVision sensor and ensures that I2C
492 * is synchronized. Returns <0 on failure.
493 */
494static int init_ov_sensor(struct sd *sd)
495{
496 int i, success;
497
498 /* Reset the sensor */
499 if (i2c_w(sd, 0x12, 0x80) < 0)
500 return -EIO;
501
502 /* Wait for it to initialize */
503 msleep(150);
504
505 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
506 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
507 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
508 success = 1;
509 continue;
510 }
511
512 /* Reset the sensor */
513 if (i2c_w(sd, 0x12, 0x80) < 0)
514 return -EIO;
515 /* Wait for it to initialize */
516 msleep(150);
517 /* Dummy read to sync I2C */
518 if (i2c_r(sd, 0x00) < 0)
519 return -EIO;
520 }
521 if (!success)
522 return -EIO;
523 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
524 return 0;
525}
526
6a7eba24
JFM
527/* Set the read and write slave IDs. The "slave" argument is the write slave,
528 * and the read slave will be set to (slave + 1).
529 * This should not be called from outside the i2c I/O functions.
530 * Sets I2C read and write slave IDs. Returns <0 for error
531 */
532static int ov51x_set_slave_ids(struct sd *sd,
533 __u8 slave)
534{
535 int rc;
536
537 rc = reg_w(sd, R51x_I2C_W_SID, slave);
538 if (rc < 0)
539 return rc;
540 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
541}
542
543struct ov_regvals {
544 __u8 reg;
545 __u8 val;
546};
547struct ov_i2c_regvals {
548 __u8 reg;
549 __u8 val;
550};
551
552static int write_regvals(struct sd *sd,
a5ae2062 553 const struct ov_regvals *regvals,
6a7eba24
JFM
554 int n)
555{
556 int rc;
557
558 while (--n >= 0) {
559 rc = reg_w(sd, regvals->reg, regvals->val);
560 if (rc < 0)
561 return rc;
562 regvals++;
563 }
564 return 0;
565}
566
567static int write_i2c_regvals(struct sd *sd,
a5ae2062 568 const struct ov_i2c_regvals *regvals,
6a7eba24
JFM
569 int n)
570{
571 int rc;
572
573 while (--n >= 0) {
574 rc = i2c_w(sd, regvals->reg, regvals->val);
575 if (rc < 0)
576 return rc;
577 regvals++;
578 }
579 return 0;
580}
581
582/****************************************************************************
583 *
584 * OV511 and sensor configuration
585 *
586 ***************************************************************************/
587
588/* This initializes the OV8110, OV8610 sensor. The OV8110 uses
589 * the same register settings as the OV8610, since they are very similar.
590 */
591static int ov8xx0_configure(struct sd *sd)
592{
593 int rc;
a5ae2062 594 static const struct ov_i2c_regvals norm_8610[] = {
6a7eba24
JFM
595 { 0x12, 0x80 },
596 { 0x00, 0x00 },
597 { 0x01, 0x80 },
598 { 0x02, 0x80 },
599 { 0x03, 0xc0 },
600 { 0x04, 0x30 },
601 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
602 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
603 { 0x0a, 0x86 },
604 { 0x0b, 0xb0 },
605 { 0x0c, 0x20 },
606 { 0x0d, 0x20 },
607 { 0x11, 0x01 },
608 { 0x12, 0x25 },
609 { 0x13, 0x01 },
610 { 0x14, 0x04 },
611 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
612 { 0x16, 0x03 },
613 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
614 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
615 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
616 { 0x1a, 0xf5 },
617 { 0x1b, 0x00 },
618 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
619 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
620 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
621 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
622 { 0x26, 0xa2 },
623 { 0x27, 0xea },
624 { 0x28, 0x00 },
625 { 0x29, 0x00 },
626 { 0x2a, 0x80 },
627 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
628 { 0x2c, 0xac },
629 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
630 { 0x2e, 0x80 },
631 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
632 { 0x4c, 0x00 },
633 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
634 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
635 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
636 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
637 { 0x63, 0xff },
638 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
639 * maybe thats wrong */
640 { 0x65, 0x00 },
641 { 0x66, 0x55 },
642 { 0x67, 0xb0 },
643 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
644 { 0x69, 0x02 },
645 { 0x6a, 0x22 },
646 { 0x6b, 0x00 },
647 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
648 deleting bit7 colors the first images red */
649 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
650 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
651 { 0x6f, 0x01 },
652 { 0x70, 0x8b },
653 { 0x71, 0x00 },
654 { 0x72, 0x14 },
655 { 0x73, 0x54 },
656 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
657 { 0x75, 0x0e },
658 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
659 { 0x77, 0xff },
660 { 0x78, 0x80 },
661 { 0x79, 0x80 },
662 { 0x7a, 0x80 },
663 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
664 { 0x7c, 0x00 },
665 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
666 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
667 { 0x7f, 0xfb },
668 { 0x80, 0x28 },
669 { 0x81, 0x00 },
670 { 0x82, 0x23 },
671 { 0x83, 0x0b },
672 { 0x84, 0x00 },
673 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
674 { 0x86, 0xc9 },
675 { 0x87, 0x00 },
676 { 0x88, 0x00 },
677 { 0x89, 0x01 },
678 { 0x12, 0x20 },
679 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
680 };
681
682 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
683
684 if (init_ov_sensor(sd) < 0)
685 PDEBUG(D_ERR|D_PROBE, "Failed to read sensor ID");
686 else
687 PDEBUG(D_PROBE, "OV86x0 initialized");
688
689 /* Detect sensor (sub)type */
690 rc = i2c_r(sd, OV7610_REG_COM_I);
691 if (rc < 0) {
692 PDEBUG(D_ERR, "Error detecting sensor type");
693 return -1;
694 }
695 if ((rc & 3) == 1) {
6a7eba24
JFM
696 sd->sensor = SEN_OV8610;
697 } else {
698 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
699 return -1;
700 }
701 PDEBUG(D_PROBE, "Writing 8610 registers");
594f5b8b 702 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
6a7eba24
JFM
703 return -1;
704
705 /* Set sensor-specific vars */
594f5b8b 706/* sd->sif = 0; already done */
6a7eba24
JFM
707 return 0;
708}
709
710/* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
711 * the same register settings as the OV7610, since they are very similar.
712 */
713static int ov7xx0_configure(struct sd *sd)
714{
715 int rc, high, low;
716
717 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
718 *
719 * Register 0x0f in the 7610 has the following effects:
720 *
721 * 0x85 (AEC method 1): Best overall, good contrast range
722 * 0x45 (AEC method 2): Very overexposed
723 * 0xa5 (spec sheet default): Ok, but the black level is
724 * shifted resulting in loss of contrast
725 * 0x05 (old driver setting): very overexposed, too much
726 * contrast
727 */
a5ae2062 728 static const struct ov_i2c_regvals norm_7610[] = {
6a7eba24
JFM
729 { 0x10, 0xff },
730 { 0x16, 0x06 },
731 { 0x28, 0x24 },
732 { 0x2b, 0xac },
733 { 0x12, 0x00 },
734 { 0x38, 0x81 },
735 { 0x28, 0x24 }, /* 0c */
736 { 0x0f, 0x85 }, /* lg's setting */
737 { 0x15, 0x01 },
738 { 0x20, 0x1c },
739 { 0x23, 0x2a },
740 { 0x24, 0x10 },
741 { 0x25, 0x8a },
742 { 0x26, 0xa2 },
743 { 0x27, 0xc2 },
744 { 0x2a, 0x04 },
745 { 0x2c, 0xfe },
746 { 0x2d, 0x93 },
747 { 0x30, 0x71 },
748 { 0x31, 0x60 },
749 { 0x32, 0x26 },
750 { 0x33, 0x20 },
751 { 0x34, 0x48 },
752 { 0x12, 0x24 },
753 { 0x11, 0x01 },
754 { 0x0c, 0x24 },
755 { 0x0d, 0x24 },
756 };
757
a5ae2062 758 static const struct ov_i2c_regvals norm_7620[] = {
6a7eba24
JFM
759 { 0x00, 0x00 }, /* gain */
760 { 0x01, 0x80 }, /* blue gain */
761 { 0x02, 0x80 }, /* red gain */
762 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
763 { 0x06, 0x60 },
764 { 0x07, 0x00 },
765 { 0x0c, 0x24 },
766 { 0x0c, 0x24 },
767 { 0x0d, 0x24 },
768 { 0x11, 0x01 },
769 { 0x12, 0x24 },
770 { 0x13, 0x01 },
771 { 0x14, 0x84 },
772 { 0x15, 0x01 },
773 { 0x16, 0x03 },
774 { 0x17, 0x2f },
775 { 0x18, 0xcf },
776 { 0x19, 0x06 },
777 { 0x1a, 0xf5 },
778 { 0x1b, 0x00 },
779 { 0x20, 0x18 },
780 { 0x21, 0x80 },
781 { 0x22, 0x80 },
782 { 0x23, 0x00 },
783 { 0x26, 0xa2 },
784 { 0x27, 0xea },
785 { 0x28, 0x20 },
786 { 0x29, 0x00 },
787 { 0x2a, 0x10 },
788 { 0x2b, 0x00 },
789 { 0x2c, 0x88 },
790 { 0x2d, 0x91 },
791 { 0x2e, 0x80 },
792 { 0x2f, 0x44 },
793 { 0x60, 0x27 },
794 { 0x61, 0x02 },
795 { 0x62, 0x5f },
796 { 0x63, 0xd5 },
797 { 0x64, 0x57 },
798 { 0x65, 0x83 },
799 { 0x66, 0x55 },
800 { 0x67, 0x92 },
801 { 0x68, 0xcf },
802 { 0x69, 0x76 },
803 { 0x6a, 0x22 },
804 { 0x6b, 0x00 },
805 { 0x6c, 0x02 },
806 { 0x6d, 0x44 },
807 { 0x6e, 0x80 },
808 { 0x6f, 0x1d },
809 { 0x70, 0x8b },
810 { 0x71, 0x00 },
811 { 0x72, 0x14 },
812 { 0x73, 0x54 },
813 { 0x74, 0x00 },
814 { 0x75, 0x8e },
815 { 0x76, 0x00 },
816 { 0x77, 0xff },
817 { 0x78, 0x80 },
818 { 0x79, 0x80 },
819 { 0x7a, 0x80 },
820 { 0x7b, 0xe2 },
821 { 0x7c, 0x00 },
822 };
823
824 /* 7640 and 7648. The defaults should be OK for most registers. */
a5ae2062 825 static const struct ov_i2c_regvals norm_7640[] = {
6a7eba24
JFM
826 { 0x12, 0x80 },
827 { 0x12, 0x14 },
828 };
829
830 /* 7670. Defaults taken from OmniVision provided data,
831 * as provided by Jonathan Corbet of OLPC */
a5ae2062 832 static const struct ov_i2c_regvals norm_7670[] = {
6a7eba24
JFM
833 { OV7670_REG_COM7, OV7670_COM7_RESET },
834 { OV7670_REG_TSLB, 0x04 }, /* OV */
835 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
594f5b8b 836 { OV7670_REG_CLKRC, 0x01 },
6a7eba24
JFM
837 /*
838 * Set the hardware window. These values from OV don't entirely
839 * make sense - hstop is less than hstart. But they work...
840 */
841 { OV7670_REG_HSTART, 0x13 }, { OV7670_REG_HSTOP, 0x01 },
842 { OV7670_REG_HREF, 0xb6 }, { OV7670_REG_VSTART, 0x02 },
843 { OV7670_REG_VSTOP, 0x7a }, { OV7670_REG_VREF, 0x0a },
844
845 { OV7670_REG_COM3, 0 }, { OV7670_REG_COM14, 0 },
846 /* Mystery scaling numbers */
847 { 0x70, 0x3a }, { 0x71, 0x35 },
848 { 0x72, 0x11 }, { 0x73, 0xf0 },
849 { 0xa2, 0x02 },
594f5b8b 850/* { OV7670_REG_COM10, 0x0 }, */
6a7eba24
JFM
851
852 /* Gamma curve values */
853 { 0x7a, 0x20 },
6a7eba24 854 { 0x7b, 0x10 },
6a7eba24 855 { 0x7c, 0x1e },
6a7eba24
JFM
856 { 0x7d, 0x35 },
857 { 0x7e, 0x5a }, { 0x7f, 0x69 },
858 { 0x80, 0x76 }, { 0x81, 0x80 },
859 { 0x82, 0x88 }, { 0x83, 0x8f },
860 { 0x84, 0x96 }, { 0x85, 0xa3 },
861 { 0x86, 0xaf }, { 0x87, 0xc4 },
862 { 0x88, 0xd7 }, { 0x89, 0xe8 },
863
864 /* AGC and AEC parameters. Note we start by disabling those features,
865 then turn them only after tweaking the values. */
866 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
867 | OV7670_COM8_AECSTEP
868 | OV7670_COM8_BFILT },
869 { OV7670_REG_GAIN, 0 }, { OV7670_REG_AECH, 0 },
870 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
6a7eba24
JFM
871 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
872 { OV7670_REG_BD50MAX, 0x05 }, { OV7670_REG_BD60MAX, 0x07 },
873 { OV7670_REG_AEW, 0x95 }, { OV7670_REG_AEB, 0x33 },
874 { OV7670_REG_VPT, 0xe3 }, { OV7670_REG_HAECC1, 0x78 },
875 { OV7670_REG_HAECC2, 0x68 },
6a7eba24
JFM
876 { 0xa1, 0x03 }, /* magic */
877 { OV7670_REG_HAECC3, 0xd8 }, { OV7670_REG_HAECC4, 0xd8 },
878 { OV7670_REG_HAECC5, 0xf0 }, { OV7670_REG_HAECC6, 0x90 },
879 { OV7670_REG_HAECC7, 0x94 },
880 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
881 | OV7670_COM8_AECSTEP
882 | OV7670_COM8_BFILT
883 | OV7670_COM8_AGC
884 | OV7670_COM8_AEC },
885
886 /* Almost all of these are magic "reserved" values. */
887 { OV7670_REG_COM5, 0x61 }, { OV7670_REG_COM6, 0x4b },
888 { 0x16, 0x02 },
6a7eba24
JFM
889 { OV7670_REG_MVFP, 0x07 },
890 { 0x21, 0x02 }, { 0x22, 0x91 },
891 { 0x29, 0x07 }, { 0x33, 0x0b },
892 { 0x35, 0x0b }, { 0x37, 0x1d },
893 { 0x38, 0x71 }, { 0x39, 0x2a },
894 { OV7670_REG_COM12, 0x78 }, { 0x4d, 0x40 },
895 { 0x4e, 0x20 }, { OV7670_REG_GFIX, 0 },
896 { 0x6b, 0x4a }, { 0x74, 0x10 },
897 { 0x8d, 0x4f }, { 0x8e, 0 },
898 { 0x8f, 0 }, { 0x90, 0 },
899 { 0x91, 0 }, { 0x96, 0 },
900 { 0x9a, 0 }, { 0xb0, 0x84 },
901 { 0xb1, 0x0c }, { 0xb2, 0x0e },
902 { 0xb3, 0x82 }, { 0xb8, 0x0a },
903
904 /* More reserved magic, some of which tweaks white balance */
905 { 0x43, 0x0a }, { 0x44, 0xf0 },
906 { 0x45, 0x34 }, { 0x46, 0x58 },
907 { 0x47, 0x28 }, { 0x48, 0x3a },
908 { 0x59, 0x88 }, { 0x5a, 0x88 },
909 { 0x5b, 0x44 }, { 0x5c, 0x67 },
910 { 0x5d, 0x49 }, { 0x5e, 0x0e },
911 { 0x6c, 0x0a }, { 0x6d, 0x55 },
912 { 0x6e, 0x11 }, { 0x6f, 0x9f },
913 /* "9e for advance AWB" */
914 { 0x6a, 0x40 }, { OV7670_REG_BLUE, 0x40 },
915 { OV7670_REG_RED, 0x60 },
916 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
917 | OV7670_COM8_AECSTEP
918 | OV7670_COM8_BFILT
919 | OV7670_COM8_AGC
920 | OV7670_COM8_AEC
921 | OV7670_COM8_AWB },
922
923 /* Matrix coefficients */
924 { 0x4f, 0x80 }, { 0x50, 0x80 },
925 { 0x51, 0 }, { 0x52, 0x22 },
926 { 0x53, 0x5e }, { 0x54, 0x80 },
927 { 0x58, 0x9e },
928
929 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
930 { OV7670_REG_EDGE, 0 },
931 { 0x75, 0x05 }, { 0x76, 0xe1 },
932 { 0x4c, 0 }, { 0x77, 0x01 },
0cd6759d
JFM
933 { OV7670_REG_COM13, OV7670_COM13_GAMMA
934 | OV7670_COM13_UVSAT
935 | 2}, /* was 3 */
936 { 0x4b, 0x09 },
6a7eba24
JFM
937 { 0xc9, 0x60 }, { OV7670_REG_COM16, 0x38 },
938 { 0x56, 0x40 },
939
940 { 0x34, 0x11 },
941 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
942 { 0xa4, 0x88 }, { 0x96, 0 },
943 { 0x97, 0x30 }, { 0x98, 0x20 },
944 { 0x99, 0x30 }, { 0x9a, 0x84 },
945 { 0x9b, 0x29 }, { 0x9c, 0x03 },
946 { 0x9d, 0x4c }, { 0x9e, 0x3f },
947 { 0x78, 0x04 },
948
949 /* Extra-weird stuff. Some sort of multiplexor register */
950 { 0x79, 0x01 }, { 0xc8, 0xf0 },
951 { 0x79, 0x0f }, { 0xc8, 0x00 },
952 { 0x79, 0x10 }, { 0xc8, 0x7e },
953 { 0x79, 0x0a }, { 0xc8, 0x80 },
954 { 0x79, 0x0b }, { 0xc8, 0x01 },
955 { 0x79, 0x0c }, { 0xc8, 0x0f },
956 { 0x79, 0x0d }, { 0xc8, 0x20 },
957 { 0x79, 0x09 }, { 0xc8, 0x80 },
958 { 0x79, 0x02 }, { 0xc8, 0xc0 },
959 { 0x79, 0x03 }, { 0xc8, 0x40 },
960 { 0x79, 0x05 }, { 0xc8, 0x30 },
961 { 0x79, 0x26 },
594f5b8b 962 };
6a7eba24
JFM
963
964 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
965
6a7eba24
JFM
966 /* Detect sensor (sub)type */
967 rc = i2c_r(sd, OV7610_REG_COM_I);
968
969 /* add OV7670 here
970 * it appears to be wrongly detected as a 7610 by default */
971 if (rc < 0) {
972 PDEBUG(D_ERR, "Error detecting sensor type");
973 return -1;
974 }
975 if ((rc & 3) == 3) {
976 /* quick hack to make OV7670s work */
977 high = i2c_r(sd, 0x0a);
978 low = i2c_r(sd, 0x0b);
979 /* info("%x, %x", high, low); */
980 if (high == 0x76 && low == 0x73) {
981 PDEBUG(D_PROBE, "Sensor is an OV7670");
982 sd->sensor = SEN_OV7670;
983 } else {
984 PDEBUG(D_PROBE, "Sensor is an OV7610");
985 sd->sensor = SEN_OV7610;
986 }
987 } else if ((rc & 3) == 1) {
988 /* I don't know what's different about the 76BE yet. */
989 if (i2c_r(sd, 0x15) & 1)
990 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
991 else
992 PDEBUG(D_PROBE, "Sensor is an OV76BE");
993
994 /* OV511+ will return all zero isoc data unless we
995 * configure the sensor as a 7620. Someone needs to
996 * find the exact reg. setting that causes this. */
997 sd->sensor = SEN_OV76BE;
998 } else if ((rc & 3) == 0) {
999 /* try to read product id registers */
1000 high = i2c_r(sd, 0x0a);
1001 if (high < 0) {
1002 PDEBUG(D_ERR, "Error detecting camera chip PID");
1003 return high;
1004 }
1005 low = i2c_r(sd, 0x0b);
1006 if (low < 0) {
1007 PDEBUG(D_ERR, "Error detecting camera chip VER");
1008 return low;
1009 }
1010 if (high == 0x76) {
594f5b8b
JFM
1011 switch (low) {
1012 case 0x30:
6a7eba24
JFM
1013 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
1014 sd->sensor = SEN_OV7630;
594f5b8b
JFM
1015 break;
1016 case 0x40:
6a7eba24
JFM
1017 PDEBUG(D_PROBE, "Sensor is an OV7645");
1018 sd->sensor = SEN_OV7640; /* FIXME */
594f5b8b
JFM
1019 break;
1020 case 0x45:
6a7eba24
JFM
1021 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1022 sd->sensor = SEN_OV7640; /* FIXME */
594f5b8b
JFM
1023 break;
1024 case 0x48:
6a7eba24
JFM
1025 PDEBUG(D_PROBE, "Sensor is an OV7648");
1026 sd->sensor = SEN_OV7640; /* FIXME */
594f5b8b
JFM
1027 break;
1028 default:
1029 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
6a7eba24
JFM
1030 return -1;
1031 }
1032 } else {
1033 PDEBUG(D_PROBE, "Sensor is an OV7620");
1034 sd->sensor = SEN_OV7620;
1035 }
1036 } else {
1037 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1038 return -1;
1039 }
1040
594f5b8b
JFM
1041 switch (sd->sensor) {
1042 case SEN_OV7620:
6a7eba24 1043 PDEBUG(D_PROBE, "Writing 7620 registers");
594f5b8b 1044 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
6a7eba24 1045 return -1;
594f5b8b
JFM
1046 break;
1047 case SEN_OV7630:
6a7eba24
JFM
1048 PDEBUG(D_ERR, "7630 is not supported by this driver version");
1049 return -1;
594f5b8b 1050 case SEN_OV7640:
6a7eba24 1051 PDEBUG(D_PROBE, "Writing 7640 registers");
594f5b8b 1052 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
6a7eba24 1053 return -1;
594f5b8b
JFM
1054 break;
1055 case SEN_OV7670:
6a7eba24 1056 PDEBUG(D_PROBE, "Writing 7670 registers");
594f5b8b 1057 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
6a7eba24 1058 return -1;
594f5b8b
JFM
1059 break;
1060 default:
6a7eba24 1061 PDEBUG(D_PROBE, "Writing 7610 registers");
594f5b8b 1062 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
6a7eba24 1063 return -1;
594f5b8b 1064 break;
6a7eba24
JFM
1065 }
1066
1067 /* Set sensor-specific vars */
594f5b8b 1068/* sd->sif = 0; already done */
6a7eba24
JFM
1069 return 0;
1070}
1071
1072/* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1073static int ov6xx0_configure(struct sd *sd)
1074{
1075 int rc;
a5ae2062 1076 static const struct ov_i2c_regvals norm_6x20[] = {
6a7eba24
JFM
1077 { 0x12, 0x80 }, /* reset */
1078 { 0x11, 0x01 },
1079 { 0x03, 0x60 },
1080 { 0x05, 0x7f }, /* For when autoadjust is off */
1081 { 0x07, 0xa8 },
1082 /* The ratio of 0x0c and 0x0d controls the white point */
1083 { 0x0c, 0x24 },
1084 { 0x0d, 0x24 },
1085 { 0x0f, 0x15 }, /* COMS */
1086 { 0x10, 0x75 }, /* AEC Exposure time */
1087 { 0x12, 0x24 }, /* Enable AGC */
1088 { 0x14, 0x04 },
1089 /* 0x16: 0x06 helps frame stability with moving objects */
1090 { 0x16, 0x06 },
1091/* { 0x20, 0x30 }, * Aperture correction enable */
1092 { 0x26, 0xb2 }, /* BLC enable */
1093 /* 0x28: 0x05 Selects RGB format if RGB on */
1094 { 0x28, 0x05 },
1095 { 0x2a, 0x04 }, /* Disable framerate adjust */
1096/* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
1097 { 0x2d, 0x99 },
1098 { 0x33, 0xa0 }, /* Color Processing Parameter */
1099 { 0x34, 0xd2 }, /* Max A/D range */
1100 { 0x38, 0x8b },
1101 { 0x39, 0x40 },
1102
1103 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1104 { 0x3c, 0x3c }, /* Change AEC mode */
1105 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1106
1107 { 0x3d, 0x80 },
1108 /* These next two registers (0x4a, 0x4b) are undocumented.
1109 * They control the color balance */
1110 { 0x4a, 0x80 },
1111 { 0x4b, 0x80 },
1112 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1113 { 0x4e, 0xc1 },
1114 { 0x4f, 0x04 },
1115/* Do 50-53 have any effect? */
1116/* Toggle 0x12[2] off and on here? */
1117 };
1118
a5ae2062 1119 static const struct ov_i2c_regvals norm_6x30[] = {
6a7eba24
JFM
1120 { 0x12, 0x80 }, /* Reset */
1121 { 0x00, 0x1f }, /* Gain */
1122 { 0x01, 0x99 }, /* Blue gain */
1123 { 0x02, 0x7c }, /* Red gain */
1124 { 0x03, 0xc0 }, /* Saturation */
1125 { 0x05, 0x0a }, /* Contrast */
1126 { 0x06, 0x95 }, /* Brightness */
1127 { 0x07, 0x2d }, /* Sharpness */
1128 { 0x0c, 0x20 },
1129 { 0x0d, 0x20 },
1130 { 0x0e, 0x20 },
1131 { 0x0f, 0x05 },
1132 { 0x10, 0x9a },
1133 { 0x11, 0x00 }, /* Pixel clock = fastest */
1134 { 0x12, 0x24 }, /* Enable AGC and AWB */
1135 { 0x13, 0x21 },
1136 { 0x14, 0x80 },
1137 { 0x15, 0x01 },
1138 { 0x16, 0x03 },
1139 { 0x17, 0x38 },
1140 { 0x18, 0xea },
1141 { 0x19, 0x04 },
1142 { 0x1a, 0x93 },
1143 { 0x1b, 0x00 },
1144 { 0x1e, 0xc4 },
1145 { 0x1f, 0x04 },
1146 { 0x20, 0x20 },
1147 { 0x21, 0x10 },
1148 { 0x22, 0x88 },
1149 { 0x23, 0xc0 }, /* Crystal circuit power level */
1150 { 0x25, 0x9a }, /* Increase AEC black ratio */
1151 { 0x26, 0xb2 }, /* BLC enable */
1152 { 0x27, 0xa2 },
1153 { 0x28, 0x00 },
1154 { 0x29, 0x00 },
1155 { 0x2a, 0x84 }, /* 60 Hz power */
1156 { 0x2b, 0xa8 }, /* 60 Hz power */
1157 { 0x2c, 0xa0 },
1158 { 0x2d, 0x95 }, /* Enable auto-brightness */
1159 { 0x2e, 0x88 },
1160 { 0x33, 0x26 },
1161 { 0x34, 0x03 },
1162 { 0x36, 0x8f },
1163 { 0x37, 0x80 },
1164 { 0x38, 0x83 },
1165 { 0x39, 0x80 },
1166 { 0x3a, 0x0f },
1167 { 0x3b, 0x3c },
1168 { 0x3c, 0x1a },
1169 { 0x3d, 0x80 },
1170 { 0x3e, 0x80 },
1171 { 0x3f, 0x0e },
1172 { 0x40, 0x00 }, /* White bal */
1173 { 0x41, 0x00 }, /* White bal */
1174 { 0x42, 0x80 },
1175 { 0x43, 0x3f }, /* White bal */
1176 { 0x44, 0x80 },
1177 { 0x45, 0x20 },
1178 { 0x46, 0x20 },
1179 { 0x47, 0x80 },
1180 { 0x48, 0x7f },
1181 { 0x49, 0x00 },
1182 { 0x4a, 0x00 },
1183 { 0x4b, 0x80 },
1184 { 0x4c, 0xd0 },
1185 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1186 { 0x4e, 0x40 },
1187 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1188 { 0x50, 0xff },
1189 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1190 { 0x55, 0xff },
1191 { 0x56, 0x12 },
1192 { 0x57, 0x81 },
1193 { 0x58, 0x75 },
1194 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1195 { 0x5a, 0x2c },
1196 { 0x5b, 0x0f }, /* AWB chrominance levels */
1197 { 0x5c, 0x10 },
1198 { 0x3d, 0x80 },
1199 { 0x27, 0xa6 },
1200 { 0x12, 0x20 }, /* Toggle AWB */
1201 { 0x12, 0x24 },
1202 };
1203
1204 PDEBUG(D_PROBE, "starting sensor configuration");
1205
1206 if (init_ov_sensor(sd) < 0) {
1207 PDEBUG(D_ERR, "Failed to read sensor ID.");
1208 return -1;
1209 }
1210 PDEBUG(D_PROBE, "OV6xx0 sensor detected");
1211
1212 /* Detect sensor (sub)type */
1213 rc = i2c_r(sd, OV7610_REG_COM_I);
1214 if (rc < 0) {
1215 PDEBUG(D_ERR, "Error detecting sensor type");
1216 return -1;
1217 }
1218
1219 /* Ugh. The first two bits are the version bits, but
1220 * the entire register value must be used. I guess OVT
1221 * underestimated how many variants they would make. */
594f5b8b
JFM
1222 switch (rc) {
1223 case 0x00:
6a7eba24
JFM
1224 sd->sensor = SEN_OV6630;
1225 PDEBUG(D_ERR,
1226 "WARNING: Sensor is an OV66308. Your camera may have");
1227 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
594f5b8b
JFM
1228 break;
1229 case 0x01:
6a7eba24 1230 sd->sensor = SEN_OV6620;
594f5b8b
JFM
1231 break;
1232 case 0x02:
6a7eba24
JFM
1233 sd->sensor = SEN_OV6630;
1234 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
594f5b8b
JFM
1235 break;
1236 case 0x03:
6a7eba24
JFM
1237 sd->sensor = SEN_OV6630;
1238 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
594f5b8b
JFM
1239 break;
1240 case 0x90:
6a7eba24
JFM
1241 sd->sensor = SEN_OV6630;
1242 PDEBUG(D_ERR,
1243 "WARNING: Sensor is an OV66307. Your camera may have");
1244 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
594f5b8b
JFM
1245 break;
1246 default:
6a7eba24
JFM
1247 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1248 return -1;
1249 }
1250
1251 /* Set sensor-specific vars */
594f5b8b 1252 sd->sif = 1;
6a7eba24
JFM
1253
1254 if (sd->sensor == SEN_OV6620) {
1255 PDEBUG(D_PROBE, "Writing 6x20 registers");
594f5b8b 1256 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
6a7eba24
JFM
1257 return -1;
1258 } else {
1259 PDEBUG(D_PROBE, "Writing 6x30 registers");
594f5b8b 1260 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
6a7eba24
JFM
1261 return -1;
1262 }
1263 return 0;
1264}
1265
1266/* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1267static void ov51x_led_control(struct sd *sd, int on)
1268{
594f5b8b
JFM
1269/* PDEBUG(D_STREAM, "LED (%s)", on ? "on" : "off"); */
1270 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
6a7eba24
JFM
1271}
1272
1273/* this function is called at probe time */
1274static int sd_config(struct gspca_dev *gspca_dev,
1275 const struct usb_device_id *id)
1276{
1277 struct sd *sd = (struct sd *) gspca_dev;
1278 struct cam *cam;
1279
a5ae2062 1280 static const struct ov_regvals init_519[] = {
6a7eba24 1281 { 0x5a, 0x6d }, /* EnableSystem */
6a7eba24
JFM
1282 { 0x53, 0x9b },
1283 { 0x54, 0xff }, /* set bit2 to enable jpeg */
1284 { 0x5d, 0x03 },
1285 { 0x49, 0x01 },
1286 { 0x48, 0x00 },
1287 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1288 * detection will fail. This deserves further investigation. */
1289 { OV519_GPIO_IO_CTRL0, 0xee },
1290 { 0x51, 0x0f }, /* SetUsbInit */
1291 { 0x51, 0x00 },
1292 { 0x22, 0x00 },
1293 /* windows reads 0x55 at this point*/
1294 };
1295
a5ae2062 1296 if (write_regvals(sd, init_519, ARRAY_SIZE(init_519)))
6a7eba24 1297 goto error;
6a7eba24
JFM
1298 ov51x_led_control(sd, 0); /* turn LED off */
1299
1300 /* Test for 76xx */
1301 sd->primary_i2c_slave = OV7xx0_SID;
1302 if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1303 goto error;
1304
1305 /* The OV519 must be more aggressive about sensor detection since
1306 * I2C write will never fail if the sensor is not present. We have
1307 * to try to initialize the sensor to detect its presence */
1308 if (init_ov_sensor(sd) < 0) {
1309 /* Test for 6xx0 */
1310 sd->primary_i2c_slave = OV6xx0_SID;
1311 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1312 goto error;
1313
1314 if (init_ov_sensor(sd) < 0) {
1315 /* Test for 8xx0 */
1316 sd->primary_i2c_slave = OV8xx0_SID;
1317 if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1318 goto error;
1319
1320 if (init_ov_sensor(sd) < 0) {
1321 PDEBUG(D_ERR,
1322 "Can't determine sensor slave IDs");
1323 goto error;
1324 } else {
1325 if (ov8xx0_configure(sd) < 0) {
1326 PDEBUG(D_ERR,
1327 "Failed to configure OV8xx0 sensor");
1328 goto error;
1329 }
1330 }
1331 } else {
1332 if (ov6xx0_configure(sd) < 0) {
1333 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1334 goto error;
1335 }
1336 }
1337 } else {
1338 if (ov7xx0_configure(sd) < 0) {
1339 PDEBUG(D_ERR, "Failed to configure OV7xx0");
1340 goto error;
1341 }
1342 }
1343
1344 cam = &gspca_dev->cam;
1345 cam->epaddr = OV511_ENDPOINT_ADDRESS;
594f5b8b 1346 if (!sd->sif) {
6a7eba24 1347 cam->cam_mode = vga_mode;
594f5b8b 1348 cam->nmodes = ARRAY_SIZE(vga_mode);
6a7eba24
JFM
1349 } else {
1350 cam->cam_mode = sif_mode;
594f5b8b 1351 cam->nmodes = ARRAY_SIZE(sif_mode);
6a7eba24 1352 }
594f5b8b
JFM
1353 sd->brightness = BRIGHTNESS_DEF;
1354 sd->contrast = CONTRAST_DEF;
1355 sd->colors = COLOR_DEF;
0cd6759d
JFM
1356 sd->hflip = HFLIP_DEF;
1357 sd->vflip = VFLIP_DEF;
6a7eba24
JFM
1358 return 0;
1359error:
1360 PDEBUG(D_ERR, "OV519 Config failed");
1361 return -EBUSY;
1362}
1363
1364/* this function is called at open time */
1365static int sd_open(struct gspca_dev *gspca_dev)
1366{
1367 return 0;
1368}
1369
1370/* Sets up the OV519 with the given image parameters
1371 *
1372 * OV519 needs a completely different approach, until we can figure out what
1373 * the individual registers do.
1374 *
1375 * Do not put any sensor-specific code in here (including I2C I/O functions)
1376 */
594f5b8b 1377static int ov519_mode_init_regs(struct sd *sd)
6a7eba24 1378{
a5ae2062 1379 static const struct ov_regvals mode_init_519_ov7670[] = {
6a7eba24
JFM
1380 { 0x5d, 0x03 }, /* Turn off suspend mode */
1381 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1382 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1383 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1384 { 0xa3, 0x18 },
1385 { 0xa4, 0x04 },
1386 { 0xa5, 0x28 },
1387 { 0x37, 0x00 }, /* SetUsbInit */
1388 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1389 /* Enable both fields, YUV Input, disable defect comp (why?) */
1390 { 0x20, 0x0c },
1391 { 0x21, 0x38 },
1392 { 0x22, 0x1d },
1393 { 0x17, 0x50 }, /* undocumented */
1394 { 0x37, 0x00 }, /* undocumented */
1395 { 0x40, 0xff }, /* I2C timeout counter */
1396 { 0x46, 0x00 }, /* I2C clock prescaler */
1397 { 0x59, 0x04 }, /* new from windrv 090403 */
1398 { 0xff, 0x00 }, /* undocumented */
1399 /* windows reads 0x55 at this point, why? */
1400 };
1401
a5ae2062 1402 static const struct ov_regvals mode_init_519[] = {
6a7eba24
JFM
1403 { 0x5d, 0x03 }, /* Turn off suspend mode */
1404 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1405 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1406 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1407 { 0xa3, 0x18 },
1408 { 0xa4, 0x04 },
1409 { 0xa5, 0x28 },
1410 { 0x37, 0x00 }, /* SetUsbInit */
1411 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1412 /* Enable both fields, YUV Input, disable defect comp (why?) */
1413 { 0x22, 0x1d },
1414 { 0x17, 0x50 }, /* undocumented */
1415 { 0x37, 0x00 }, /* undocumented */
1416 { 0x40, 0xff }, /* I2C timeout counter */
1417 { 0x46, 0x00 }, /* I2C clock prescaler */
1418 { 0x59, 0x04 }, /* new from windrv 090403 */
1419 { 0xff, 0x00 }, /* undocumented */
1420 /* windows reads 0x55 at this point, why? */
1421 };
1422
6a7eba24
JFM
1423 /******** Set the mode ********/
1424 if (sd->sensor != SEN_OV7670) {
1425 if (write_regvals(sd, mode_init_519,
a5ae2062 1426 ARRAY_SIZE(mode_init_519)))
6a7eba24 1427 return -EIO;
594f5b8b
JFM
1428 if (sd->sensor == SEN_OV7640) {
1429 /* Select 8-bit input mode */
1430 reg_w_mask(sd, OV519_CAM_DFR, 0x10, 0x10);
1431 }
6a7eba24
JFM
1432 } else {
1433 if (write_regvals(sd, mode_init_519_ov7670,
a5ae2062 1434 ARRAY_SIZE(mode_init_519_ov7670)))
6a7eba24
JFM
1435 return -EIO;
1436 }
1437
594f5b8b
JFM
1438 reg_w(sd, OV519_CAM_H_SIZE, sd->gspca_dev.width >> 4);
1439 reg_w(sd, OV519_CAM_V_SIZE, sd->gspca_dev.height >> 3);
6a7eba24
JFM
1440 reg_w(sd, OV519_CAM_X_OFFSETL, 0x00);
1441 reg_w(sd, OV519_CAM_X_OFFSETH, 0x00);
1442 reg_w(sd, OV519_CAM_Y_OFFSETL, 0x00);
1443 reg_w(sd, OV519_CAM_Y_OFFSETH, 0x00);
1444 reg_w(sd, OV519_CAM_DIVIDER, 0x00);
1445 reg_w(sd, OV519_CAM_FORMAT, 0x03); /* YUV422 */
1446 reg_w(sd, 0x26, 0x00); /* Undocumented */
1447
1448 /******** Set the framerate ********/
1449 if (frame_rate > 0)
1450 sd->frame_rate = frame_rate;
1451
1452/* FIXME: These are only valid at the max resolution. */
1453 sd->clockdiv = 0;
594f5b8b
JFM
1454 switch (sd->sensor) {
1455 case SEN_OV7640:
6a7eba24 1456 switch (sd->frame_rate) {
594f5b8b 1457/*fixme: default was 30 fps */
6a7eba24
JFM
1458 case 30:
1459 reg_w(sd, 0xa4, 0x0c);
1460 reg_w(sd, 0x23, 0xff);
1461 break;
1462 case 25:
1463 reg_w(sd, 0xa4, 0x0c);
1464 reg_w(sd, 0x23, 0x1f);
1465 break;
1466 case 20:
1467 reg_w(sd, 0xa4, 0x0c);
1468 reg_w(sd, 0x23, 0x1b);
1469 break;
1470 default:
1471/* case 15: */
1472 reg_w(sd, 0xa4, 0x04);
1473 reg_w(sd, 0x23, 0xff);
1474 sd->clockdiv = 1;
1475 break;
1476 case 10:
1477 reg_w(sd, 0xa4, 0x04);
1478 reg_w(sd, 0x23, 0x1f);
1479 sd->clockdiv = 1;
1480 break;
1481 case 5:
1482 reg_w(sd, 0xa4, 0x04);
1483 reg_w(sd, 0x23, 0x1b);
1484 sd->clockdiv = 1;
1485 break;
1486 }
594f5b8b
JFM
1487 break;
1488 case SEN_OV8610:
6a7eba24
JFM
1489 switch (sd->frame_rate) {
1490 default: /* 15 fps */
1491/* case 15: */
1492 reg_w(sd, 0xa4, 0x06);
1493 reg_w(sd, 0x23, 0xff);
1494 break;
1495 case 10:
1496 reg_w(sd, 0xa4, 0x06);
1497 reg_w(sd, 0x23, 0x1f);
1498 break;
1499 case 5:
1500 reg_w(sd, 0xa4, 0x06);
1501 reg_w(sd, 0x23, 0x1b);
1502 break;
1503 }
594f5b8b
JFM
1504 break;
1505 case SEN_OV7670: /* guesses, based on 7640 */
6a7eba24
JFM
1506 PDEBUG(D_STREAM, "Setting framerate to %d fps",
1507 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
594f5b8b 1508 reg_w(sd, 0xa4, 0x10);
6a7eba24
JFM
1509 switch (sd->frame_rate) {
1510 case 30:
6a7eba24
JFM
1511 reg_w(sd, 0x23, 0xff);
1512 break;
1513 case 20:
6a7eba24
JFM
1514 reg_w(sd, 0x23, 0x1b);
1515 break;
594f5b8b
JFM
1516 default:
1517/* case 15: */
6a7eba24
JFM
1518 reg_w(sd, 0x23, 0xff);
1519 sd->clockdiv = 1;
1520 break;
1521 }
594f5b8b 1522 break;
6a7eba24
JFM
1523 }
1524
6a7eba24
JFM
1525 return 0;
1526}
1527
594f5b8b 1528static int mode_init_ov_sensor_regs(struct sd *sd)
6a7eba24 1529{
594f5b8b
JFM
1530 struct gspca_dev *gspca_dev;
1531 int qvga;
1532
1533 gspca_dev = &sd->gspca_dev;
1534 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
6a7eba24
JFM
1535
1536 /******** Mode (VGA/QVGA) and sensor specific regs ********/
1537 switch (sd->sensor) {
1538 case SEN_OV8610:
1539 /* For OV8610 qvga means qsvga */
1540 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
1541 break;
1542 case SEN_OV7610:
1543 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1544 break;
1545 case SEN_OV7620:
1546/* i2c_w(sd, 0x2b, 0x00); */
1547 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1548 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1549 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
1550 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
1551 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
1552 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
1553 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
1554 break;
1555 case SEN_OV76BE:
1556/* i2c_w(sd, 0x2b, 0x00); */
1557 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1558 break;
1559 case SEN_OV7640:
1560/* i2c_w(sd, 0x2b, 0x00); */
1561 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1562 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
1563/* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
1564/* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
1565/* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
1566/* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
1567/* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
1568 break;
1569 case SEN_OV7670:
1570 /* set COM7_FMT_VGA or COM7_FMT_QVGA
1571 * do we need to set anything else?
1572 * HSTART etc are set in set_ov_sensor_window itself */
1573 i2c_w_mask(sd, OV7670_REG_COM7,
1574 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
1575 OV7670_COM7_FMT_MASK);
1576 break;
1577 case SEN_OV6620:
6a7eba24
JFM
1578 case SEN_OV6630:
1579 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
1580 break;
1581 default:
1582 return -EINVAL;
1583 }
1584
1585 /******** Palette-specific regs ********/
594f5b8b
JFM
1586 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
1587 /* not valid on the OV6620/OV7620/6630? */
1588 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
1589 }
6a7eba24 1590
594f5b8b
JFM
1591 /* The OV518 needs special treatment. Although both the OV518
1592 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
1593 * bus is actually used. The UV bus is tied to ground.
1594 * Therefore, the OV6630 needs to be in 8-bit multiplexed
1595 * output mode */
6a7eba24 1596
594f5b8b 1597 /* OV7640 is 8-bit only */
6a7eba24 1598
594f5b8b
JFM
1599 if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV7640)
1600 i2c_w_mask(sd, 0x13, 0x00, 0x20);
6a7eba24
JFM
1601
1602 /******** Clock programming ********/
1603 /* The OV6620 needs special handling. This prevents the
1604 * severe banding that normally occurs */
1605 if (sd->sensor == SEN_OV6620) {
1606
1607 /* Clock down */
1608 i2c_w(sd, 0x2a, 0x04);
594f5b8b 1609 i2c_w(sd, 0x11, sd->clockdiv);
6a7eba24
JFM
1610 i2c_w(sd, 0x2a, 0x84);
1611 /* This next setting is critical. It seems to improve
1612 * the gain or the contrast. The "reserved" bits seem
1613 * to have some effect in this case. */
1614 i2c_w(sd, 0x2d, 0x85);
594f5b8b
JFM
1615 } else if (sd->clockdiv >= 0) {
1616 i2c_w(sd, 0x11, sd->clockdiv);
6a7eba24
JFM
1617 }
1618
1619 /******** Special Features ********/
1620/* no evidence this is possible with OV7670, either */
1621 /* Test Pattern */
1622 if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
1623 i2c_w_mask(sd, 0x12, 0x00, 0x02);
1624
1625 /* Enable auto white balance */
1626 if (sd->sensor == SEN_OV7670)
1627 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
1628 OV7670_COM8_AWB);
1629 else
1630 i2c_w_mask(sd, 0x12, 0x04, 0x04);
1631
1632 /* This will go away as soon as ov51x_mode_init_sensor_regs() */
1633 /* is fully tested. */
1634 /* 7620/6620/6630? don't have register 0x35, so play it safe */
1635 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
594f5b8b 1636 if (!qvga)
6a7eba24
JFM
1637 i2c_w(sd, 0x35, 0x9e);
1638 else
1639 i2c_w(sd, 0x35, 0x1e);
1640 }
1641 return 0;
1642}
1643
594f5b8b 1644static void sethvflip(struct sd *sd)
0cd6759d 1645{
594f5b8b
JFM
1646 if (sd->sensor != SEN_OV7670)
1647 return;
0cd6759d
JFM
1648 if (sd->gspca_dev.streaming)
1649 ov51x_stop(sd);
1650 i2c_w_mask(sd, OV7670_REG_MVFP,
594f5b8b
JFM
1651 OV7670_MVFP_MIRROR * sd->hflip
1652 | OV7670_MVFP_VFLIP * sd->vflip,
1653 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
0cd6759d
JFM
1654 if (sd->gspca_dev.streaming)
1655 ov51x_restart(sd);
1656}
1657
594f5b8b 1658static int set_ov_sensor_window(struct sd *sd)
6a7eba24 1659{
594f5b8b
JFM
1660 struct gspca_dev *gspca_dev;
1661 int qvga;
6a7eba24
JFM
1662 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
1663 int ret, hstart, hstop, vstop, vstart;
1664 __u8 v;
1665
594f5b8b
JFM
1666 gspca_dev = &sd->gspca_dev;
1667 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
1668
6a7eba24
JFM
1669 /* The different sensor ICs handle setting up of window differently.
1670 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
1671 switch (sd->sensor) {
1672 case SEN_OV8610:
1673 hwsbase = 0x1e;
1674 hwebase = 0x1e;
1675 vwsbase = 0x02;
1676 vwebase = 0x02;
1677 break;
1678 case SEN_OV7610:
1679 case SEN_OV76BE:
1680 hwsbase = 0x38;
1681 hwebase = 0x3a;
1682 vwsbase = vwebase = 0x05;
1683 break;
1684 case SEN_OV6620:
1685 case SEN_OV6630:
1686 hwsbase = 0x38;
1687 hwebase = 0x3a;
1688 vwsbase = 0x05;
1689 vwebase = 0x06;
1690 break;
1691 case SEN_OV7620:
1692 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
1693 hwebase = 0x2f;
1694 vwsbase = vwebase = 0x05;
1695 break;
1696 case SEN_OV7640:
1697 hwsbase = 0x1a;
1698 hwebase = 0x1a;
1699 vwsbase = vwebase = 0x03;
1700 break;
1701 case SEN_OV7670:
1702 /*handling of OV7670 hardware sensor start and stop values
1703 * is very odd, compared to the other OV sensors */
1704 vwsbase = vwebase = hwebase = hwsbase = 0x00;
1705 break;
1706 default:
1707 return -EINVAL;
1708 }
1709
1710 switch (sd->sensor) {
1711 case SEN_OV6620:
1712 case SEN_OV6630:
594f5b8b 1713 if (qvga) { /* QCIF */
6a7eba24
JFM
1714 hwscale = 0;
1715 vwscale = 0;
1716 } else { /* CIF */
1717 hwscale = 1;
1718 vwscale = 1; /* The datasheet says 0;
1719 * it's wrong */
1720 }
1721 break;
1722 case SEN_OV8610:
594f5b8b 1723 if (qvga) { /* QSVGA */
6a7eba24
JFM
1724 hwscale = 1;
1725 vwscale = 1;
1726 } else { /* SVGA */
1727 hwscale = 2;
1728 vwscale = 2;
1729 }
1730 break;
1731 default: /* SEN_OV7xx0 */
594f5b8b 1732 if (qvga) { /* QVGA */
6a7eba24
JFM
1733 hwscale = 1;
1734 vwscale = 0;
1735 } else { /* VGA */
1736 hwscale = 2;
1737 vwscale = 1;
1738 }
1739 }
1740
594f5b8b 1741 ret = mode_init_ov_sensor_regs(sd);
6a7eba24
JFM
1742 if (ret < 0)
1743 return ret;
1744
1745 if (sd->sensor == SEN_OV8610) {
1746 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
1747 /* old 0x95, new 0x05 from windrv 090403 */
1748 /* bits 5-7: reserved */
1749 i2c_w_mask(sd, 0x28, 0x20, 0x20);
1750 /* bit 5: progressive mode on */
1751 }
1752
1753 /* The below is wrong for OV7670s because their window registers
1754 * only store the high bits in 0x17 to 0x1a */
1755
1756 /* SRH Use sd->max values instead of requested win values */
1757 /* SCS Since we're sticking with only the max hardware widths
1758 * for a given mode */
1759 /* I can hard code this for OV7670s */
1760 /* Yes, these numbers do look odd, but they're tested and work! */
1761 if (sd->sensor == SEN_OV7670) {
594f5b8b 1762 if (qvga) { /* QVGA from ov7670.c by
6a7eba24
JFM
1763 * Jonathan Corbet */
1764 hstart = 164;
1765 hstop = 20;
1766 vstart = 14;
1767 vstop = 494;
1768 } else { /* VGA */
1769 hstart = 158;
1770 hstop = 14;
1771 vstart = 10;
1772 vstop = 490;
1773 }
1774 /* OV7670 hardware window registers are split across
1775 * multiple locations */
594f5b8b
JFM
1776 i2c_w(sd, OV7670_REG_HSTART, hstart >> 3);
1777 i2c_w(sd, OV7670_REG_HSTOP, hstop >> 3);
6a7eba24
JFM
1778 v = i2c_r(sd, OV7670_REG_HREF);
1779 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
1780 msleep(10); /* need to sleep between read and write to
1781 * same reg! */
1782 i2c_w(sd, OV7670_REG_HREF, v);
1783
594f5b8b
JFM
1784 i2c_w(sd, OV7670_REG_VSTART, vstart >> 2);
1785 i2c_w(sd, OV7670_REG_VSTOP, vstop >> 2);
6a7eba24
JFM
1786 v = i2c_r(sd, OV7670_REG_VREF);
1787 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
1788 msleep(10); /* need to sleep between read and write to
1789 * same reg! */
1790 i2c_w(sd, OV7670_REG_VREF, v);
594f5b8b 1791 sethvflip(sd);
6a7eba24 1792 } else {
594f5b8b
JFM
1793 i2c_w(sd, 0x17, hwsbase);
1794 i2c_w(sd, 0x18, hwebase + (sd->gspca_dev.width >> hwscale));
1795 i2c_w(sd, 0x19, vwsbase);
1796 i2c_w(sd, 0x1a, vwebase + (sd->gspca_dev.height >> vwscale));
6a7eba24
JFM
1797 }
1798 return 0;
1799}
1800
6a7eba24
JFM
1801/* -- start the camera -- */
1802static void sd_start(struct gspca_dev *gspca_dev)
1803{
1804 struct sd *sd = (struct sd *) gspca_dev;
1805 int ret;
1806
594f5b8b 1807 ret = ov519_mode_init_regs(sd);
6a7eba24
JFM
1808 if (ret < 0)
1809 goto out;
594f5b8b 1810 ret = set_ov_sensor_window(sd);
6a7eba24
JFM
1811 if (ret < 0)
1812 goto out;
1813
594f5b8b 1814 ret = ov51x_restart(sd);
6a7eba24
JFM
1815 if (ret < 0)
1816 goto out;
1817 PDEBUG(D_STREAM, "camera started alt: 0x%02x", gspca_dev->alt);
1818 ov51x_led_control(sd, 1);
1819 return;
1820out:
1821 PDEBUG(D_ERR, "camera start error:%d", ret);
1822}
1823
1824static void sd_stopN(struct gspca_dev *gspca_dev)
1825{
1826 ov51x_stop((struct sd *) gspca_dev);
1827 ov51x_led_control((struct sd *) gspca_dev, 0);
1828}
1829
1830static void sd_stop0(struct gspca_dev *gspca_dev)
1831{
1832}
1833
1834static void sd_close(struct gspca_dev *gspca_dev)
1835{
1836}
1837
1838static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1839 struct gspca_frame *frame, /* target */
a5ae2062 1840 __u8 *data, /* isoc packet */
6a7eba24
JFM
1841 int len) /* iso packet length */
1842{
1843 /* Header of ov519 is 16 bytes:
1844 * Byte Value Description
1845 * 0 0xff magic
1846 * 1 0xff magic
1847 * 2 0xff magic
1848 * 3 0xXX 0x50 = SOF, 0x51 = EOF
1849 * 9 0xXX 0x01 initial frame without data,
1850 * 0x00 standard frame with image
1851 * 14 Lo in EOF: length of image data / 8
1852 * 15 Hi
1853 */
1854
1855 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
1856 switch (data[3]) {
1857 case 0x50: /* start of frame */
1858#define HDRSZ 16
1859 data += HDRSZ;
1860 len -= HDRSZ;
1861#undef HDRSZ
1862 if (data[0] == 0xff || data[1] == 0xd8)
1863 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
1864 data, len);
1865 else
1866 gspca_dev->last_packet_type = DISCARD_PACKET;
1867 return;
1868 case 0x51: /* end of frame */
1869 if (data[9] != 0)
1870 gspca_dev->last_packet_type = DISCARD_PACKET;
1871 gspca_frame_add(gspca_dev, LAST_PACKET, frame,
1872 data, 0);
1873 return;
1874 }
1875 }
1876
1877 /* intermediate packet */
1878 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
1879 data, len);
1880}
1881
1882/* -- management routines -- */
1883
1884static void setbrightness(struct gspca_dev *gspca_dev)
1885{
1886 struct sd *sd = (struct sd *) gspca_dev;
1887 int val;
6a7eba24
JFM
1888
1889 val = sd->brightness;
1890 PDEBUG(D_CONF, "brightness:%d", val);
594f5b8b 1891/* if (gspca_dev->streaming)
6a7eba24
JFM
1892 * ov51x_stop(sd); */
1893 switch (sd->sensor) {
1894 case SEN_OV8610:
1895 case SEN_OV7610:
1896 case SEN_OV76BE:
1897 case SEN_OV6620:
1898 case SEN_OV6630:
1899 case SEN_OV7640:
1900 i2c_w(sd, OV7610_REG_BRT, val);
1901 break;
1902 case SEN_OV7620:
1903 /* 7620 doesn't like manual changes when in auto mode */
1904/*fixme
1905 * if (!sd->auto_brt) */
1906 i2c_w(sd, OV7610_REG_BRT, val);
1907 break;
1908 case SEN_OV7670:
594f5b8b 1909/*win trace
6a7eba24
JFM
1910 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
1911 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
1912 break;
1913 }
594f5b8b 1914/* if (gspca_dev->streaming)
6a7eba24
JFM
1915 * ov51x_restart(sd); */
1916}
1917
1918static void setcontrast(struct gspca_dev *gspca_dev)
1919{
1920 struct sd *sd = (struct sd *) gspca_dev;
1921 int val;
6a7eba24
JFM
1922
1923 val = sd->contrast;
1924 PDEBUG(D_CONF, "contrast:%d", val);
594f5b8b 1925/* if (gspca_dev->streaming)
6a7eba24
JFM
1926 ov51x_stop(sd); */
1927 switch (sd->sensor) {
1928 case SEN_OV7610:
1929 case SEN_OV6620:
1930 i2c_w(sd, OV7610_REG_CNT, val);
1931 break;
1932 case SEN_OV6630:
1933 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
1934 case SEN_OV8610: {
a5ae2062 1935 static const __u8 ctab[] = {
6a7eba24
JFM
1936 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
1937 };
1938
1939 /* Use Y gamma control instead. Bit 0 enables it. */
1940 i2c_w(sd, 0x64, ctab[val >> 5]);
1941 break;
1942 }
1943 case SEN_OV7620: {
a5ae2062 1944 static const __u8 ctab[] = {
6a7eba24
JFM
1945 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1946 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1947 };
1948
1949 /* Use Y gamma control instead. Bit 0 enables it. */
1950 i2c_w(sd, 0x64, ctab[val >> 4]);
1951 break;
1952 }
1953 case SEN_OV7640:
1954 /* Use gain control instead. */
1955 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
1956 break;
1957 case SEN_OV7670:
1958 /* check that this isn't just the same as ov7610 */
1959 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
1960 break;
1961 }
594f5b8b 1962/* if (gspca_dev->streaming)
6a7eba24
JFM
1963 ov51x_restart(sd); */
1964}
1965
1966static void setcolors(struct gspca_dev *gspca_dev)
1967{
1968 struct sd *sd = (struct sd *) gspca_dev;
1969 int val;
6a7eba24
JFM
1970
1971 val = sd->colors;
1972 PDEBUG(D_CONF, "saturation:%d", val);
594f5b8b 1973/* if (gspca_dev->streaming)
6a7eba24
JFM
1974 ov51x_stop(sd); */
1975 switch (sd->sensor) {
1976 case SEN_OV8610:
1977 case SEN_OV7610:
1978 case SEN_OV76BE:
1979 case SEN_OV6620:
1980 case SEN_OV6630:
1981 i2c_w(sd, OV7610_REG_SAT, val);
1982 break;
1983 case SEN_OV7620:
1984 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
1985/* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
1986 if (rc < 0)
1987 goto out; */
1988 i2c_w(sd, OV7610_REG_SAT, val);
1989 break;
1990 case SEN_OV7640:
1991 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
1992 break;
1993 case SEN_OV7670:
1994 /* supported later once I work out how to do it
1995 * transparently fail now! */
1996 /* set REG_COM13 values for UV sat auto mode */
1997 break;
1998 }
594f5b8b 1999/* if (gspca_dev->streaming)
6a7eba24
JFM
2000 ov51x_restart(sd); */
2001}
2002
2003static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2004{
2005 struct sd *sd = (struct sd *) gspca_dev;
2006
2007 sd->brightness = val;
2008 setbrightness(gspca_dev);
2009 return 0;
2010}
2011
2012static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2013{
2014 struct sd *sd = (struct sd *) gspca_dev;
2015
2016 *val = sd->brightness;
2017 return 0;
2018}
2019
2020static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2021{
2022 struct sd *sd = (struct sd *) gspca_dev;
2023
2024 sd->contrast = val;
2025 setcontrast(gspca_dev);
2026 return 0;
2027}
2028
2029static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2030{
2031 struct sd *sd = (struct sd *) gspca_dev;
2032
2033 *val = sd->contrast;
2034 return 0;
2035}
2036
2037static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2038{
2039 struct sd *sd = (struct sd *) gspca_dev;
2040
2041 sd->colors = val;
2042 setcolors(gspca_dev);
2043 return 0;
2044}
2045
2046static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2047{
2048 struct sd *sd = (struct sd *) gspca_dev;
2049
2050 *val = sd->colors;
2051 return 0;
2052}
2053
0cd6759d
JFM
2054static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
2055{
2056 struct sd *sd = (struct sd *) gspca_dev;
2057
2058 sd->hflip = val;
594f5b8b 2059 sethvflip(sd);
0cd6759d
JFM
2060 return 0;
2061}
2062
2063static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
2064{
2065 struct sd *sd = (struct sd *) gspca_dev;
2066
2067 *val = sd->hflip;
2068 return 0;
2069}
2070
2071static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2072{
2073 struct sd *sd = (struct sd *) gspca_dev;
2074
2075 sd->vflip = val;
594f5b8b 2076 sethvflip(sd);
0cd6759d
JFM
2077 return 0;
2078}
2079
2080static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2081{
2082 struct sd *sd = (struct sd *) gspca_dev;
2083
2084 *val = sd->vflip;
2085 return 0;
2086}
2087
6a7eba24 2088/* sub-driver description */
a5ae2062 2089static const struct sd_desc sd_desc = {
6a7eba24
JFM
2090 .name = MODULE_NAME,
2091 .ctrls = sd_ctrls,
2092 .nctrls = ARRAY_SIZE(sd_ctrls),
2093 .config = sd_config,
2094 .open = sd_open,
2095 .start = sd_start,
2096 .stopN = sd_stopN,
2097 .stop0 = sd_stop0,
2098 .close = sd_close,
2099 .pkt_scan = sd_pkt_scan,
2100};
2101
2102/* -- module initialisation -- */
a5ae2062 2103static const __devinitdata struct usb_device_id device_table[] = {
9d64fdb1
JFM
2104 {USB_DEVICE(0x041e, 0x4052)},
2105 {USB_DEVICE(0x041e, 0x405f)},
2106 {USB_DEVICE(0x041e, 0x4060)},
2107 {USB_DEVICE(0x041e, 0x4061)},
2108 {USB_DEVICE(0x041e, 0x4064)},
2109 {USB_DEVICE(0x041e, 0x4068)},
2110 {USB_DEVICE(0x045e, 0x028c)},
2111 {USB_DEVICE(0x054c, 0x0154)},
2112 {USB_DEVICE(0x054c, 0x0155)},
2113 {USB_DEVICE(0x05a9, 0x0519)},
2114 {USB_DEVICE(0x05a9, 0x0530)},
2115 {USB_DEVICE(0x05a9, 0x4519)},
2116 {USB_DEVICE(0x05a9, 0x8519)},
6a7eba24
JFM
2117 {}
2118};
2119#undef DVNAME
2120MODULE_DEVICE_TABLE(usb, device_table);
2121
2122/* -- device connect -- */
2123static int sd_probe(struct usb_interface *intf,
2124 const struct usb_device_id *id)
2125{
2126 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2127 THIS_MODULE);
2128}
2129
2130static struct usb_driver sd_driver = {
2131 .name = MODULE_NAME,
2132 .id_table = device_table,
2133 .probe = sd_probe,
2134 .disconnect = gspca_disconnect,
6a709749
JFM
2135#ifdef CONFIG_PM
2136 .suspend = gspca_suspend,
2137 .resume = gspca_resume,
2138#endif
6a7eba24
JFM
2139};
2140
2141/* -- module insert / remove -- */
2142static int __init sd_mod_init(void)
2143{
2144 if (usb_register(&sd_driver) < 0)
2145 return -1;
10b0e96e 2146 PDEBUG(D_PROBE, "registered");
6a7eba24
JFM
2147 return 0;
2148}
2149static void __exit sd_mod_exit(void)
2150{
2151 usb_deregister(&sd_driver);
2152 PDEBUG(D_PROBE, "deregistered");
2153}
2154
2155module_init(sd_mod_init);
2156module_exit(sd_mod_exit);
2157
2158module_param(frame_rate, int, 0644);
2159MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");