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