blob: 2d5d95f027793529a6b169100f4455d36405dff7 [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 *
Romain Beauxis2961e872008-12-05 06:25:26 -03006 * This module is adapted from the ov51x-jpeg package, which itself
7 * was adapted from the ov511 driver.
8 *
9 * Original copyright for the ov511 driver is:
10 *
11 * Copyright (c) 1999-2004 Mark W. McClelland
12 * Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach
13 *
14 * ov51x-jpeg original copyright is:
15 *
16 * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
17 * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030018 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 *
33 */
34#define MODULE_NAME "ov519"
35
36#include "gspca.h"
37
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030038MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
39MODULE_DESCRIPTION("OV519 USB Camera Driver");
40MODULE_LICENSE("GPL");
41
42/* global parameters */
43static int frame_rate;
44
45/* Number of times to retry a failed I2C transaction. Increase this if you
46 * are getting "Failed to read sensor ID..." */
47static int i2c_detect_tries = 10;
48
49/* ov519 device descriptor */
50struct sd {
51 struct gspca_dev gspca_dev; /* !! must be the first item */
52
Hans de Goede49809d62009-06-07 12:10:39 -030053 char bridge;
54#define BRIDGE_OV511 0
55#define BRIDGE_OV511PLUS 1
56#define BRIDGE_OV518 2
57#define BRIDGE_OV518PLUS 3
58#define BRIDGE_OV519 4
59
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030060 /* Determined by sensor type */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -030061 __u8 sif;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030062
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -030063 __u8 brightness;
64 __u8 contrast;
65 __u8 colors;
Jean-Francois Moine0cd67592008-07-29 05:25:28 -030066 __u8 hflip;
67 __u8 vflip;
Hans de Goede02ab18b2009-06-14 04:32:04 -030068 __u8 autobrightness;
69 __u8 freq;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030070
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -030071 __u8 stopped; /* Streaming is temporarily paused */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030072
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -030073 __u8 frame_rate; /* current Framerate (OV519 only) */
74 __u8 clockdiv; /* clockdiv override for OV519 only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030075
76 char sensor; /* Type of image sensor chip (SEN_*) */
77#define SEN_UNKNOWN 0
78#define SEN_OV6620 1
79#define SEN_OV6630 2
Hans de Goede7d971372009-06-14 05:28:17 -030080#define SEN_OV66308AF 3
81#define SEN_OV7610 4
82#define SEN_OV7620 5
83#define SEN_OV7640 6
84#define SEN_OV7670 7
85#define SEN_OV76BE 8
86#define SEN_OV8610 9
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030087};
88
89/* V4L2 controls supported by the driver */
90static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
91static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
92static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
93static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
94static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
95static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
Jean-Francois Moine0cd67592008-07-29 05:25:28 -030096static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val);
97static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val);
98static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val);
99static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goede02ab18b2009-06-14 04:32:04 -0300100static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val);
101static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val);
102static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
103static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goede49809d62009-06-07 12:10:39 -0300104static void setbrightness(struct gspca_dev *gspca_dev);
105static void setcontrast(struct gspca_dev *gspca_dev);
106static void setcolors(struct gspca_dev *gspca_dev);
Hans de Goede02ab18b2009-06-14 04:32:04 -0300107static void setautobrightness(struct sd *sd);
108static void setfreq(struct sd *sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300109
Hans de Goede02ab18b2009-06-14 04:32:04 -0300110static const struct ctrl sd_ctrls[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300111 {
112 {
113 .id = V4L2_CID_BRIGHTNESS,
114 .type = V4L2_CTRL_TYPE_INTEGER,
115 .name = "Brightness",
116 .minimum = 0,
117 .maximum = 255,
118 .step = 1,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300119#define BRIGHTNESS_DEF 127
120 .default_value = BRIGHTNESS_DEF,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300121 },
122 .set = sd_setbrightness,
123 .get = sd_getbrightness,
124 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300125 {
126 {
127 .id = V4L2_CID_CONTRAST,
128 .type = V4L2_CTRL_TYPE_INTEGER,
129 .name = "Contrast",
130 .minimum = 0,
131 .maximum = 255,
132 .step = 1,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300133#define CONTRAST_DEF 127
134 .default_value = CONTRAST_DEF,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300135 },
136 .set = sd_setcontrast,
137 .get = sd_getcontrast,
138 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300139 {
140 {
141 .id = V4L2_CID_SATURATION,
142 .type = V4L2_CTRL_TYPE_INTEGER,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300143 .name = "Color",
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300144 .minimum = 0,
145 .maximum = 255,
146 .step = 1,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300147#define COLOR_DEF 127
148 .default_value = COLOR_DEF,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300149 },
150 .set = sd_setcolors,
151 .get = sd_getcolors,
152 },
Hans de Goede02ab18b2009-06-14 04:32:04 -0300153/* The flip controls work with ov7670 only */
Jean-Francois Moinede004482008-09-03 17:12:16 -0300154#define HFLIP_IDX 3
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300155 {
156 {
157 .id = V4L2_CID_HFLIP,
158 .type = V4L2_CTRL_TYPE_BOOLEAN,
159 .name = "Mirror",
160 .minimum = 0,
161 .maximum = 1,
162 .step = 1,
163#define HFLIP_DEF 0
164 .default_value = HFLIP_DEF,
165 },
166 .set = sd_sethflip,
167 .get = sd_gethflip,
168 },
Jean-Francois Moinede004482008-09-03 17:12:16 -0300169#define VFLIP_IDX 4
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300170 {
171 {
172 .id = V4L2_CID_VFLIP,
173 .type = V4L2_CTRL_TYPE_BOOLEAN,
174 .name = "Vflip",
175 .minimum = 0,
176 .maximum = 1,
177 .step = 1,
178#define VFLIP_DEF 0
179 .default_value = VFLIP_DEF,
180 },
181 .set = sd_setvflip,
182 .get = sd_getvflip,
183 },
Hans de Goede02ab18b2009-06-14 04:32:04 -0300184#define AUTOBRIGHT_IDX 5
185 {
186 {
187 .id = V4L2_CID_AUTOBRIGHTNESS,
188 .type = V4L2_CTRL_TYPE_BOOLEAN,
189 .name = "Auto Brightness",
190 .minimum = 0,
191 .maximum = 1,
192 .step = 1,
193#define AUTOBRIGHT_DEF 1
194 .default_value = AUTOBRIGHT_DEF,
195 },
196 .set = sd_setautobrightness,
197 .get = sd_getautobrightness,
198 },
199#define FREQ_IDX 6
200 {
201 {
202 .id = V4L2_CID_POWER_LINE_FREQUENCY,
203 .type = V4L2_CTRL_TYPE_MENU,
204 .name = "Light frequency filter",
205 .minimum = 0,
206 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
207 .step = 1,
208#define FREQ_DEF 0
209 .default_value = FREQ_DEF,
210 },
211 .set = sd_setfreq,
212 .get = sd_getfreq,
213 },
214#define OV7670_FREQ_IDX 7
215 {
216 {
217 .id = V4L2_CID_POWER_LINE_FREQUENCY,
218 .type = V4L2_CTRL_TYPE_MENU,
219 .name = "Light frequency filter",
220 .minimum = 0,
221 .maximum = 3, /* 0: 0, 1: 50Hz, 2:60Hz 3: Auto Hz */
222 .step = 1,
223#define OV7670_FREQ_DEF 3
224 .default_value = OV7670_FREQ_DEF,
225 },
226 .set = sd_setfreq,
227 .get = sd_getfreq,
228 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300229};
230
Hans de Goede49809d62009-06-07 12:10:39 -0300231static const struct v4l2_pix_format ov519_vga_mode[] = {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300232 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
233 .bytesperline = 320,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300234 .sizeimage = 320 * 240 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300235 .colorspace = V4L2_COLORSPACE_JPEG,
236 .priv = 1},
237 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
238 .bytesperline = 640,
239 .sizeimage = 640 * 480 * 3 / 8 + 590,
240 .colorspace = V4L2_COLORSPACE_JPEG,
241 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300242};
Hans de Goede49809d62009-06-07 12:10:39 -0300243static const struct v4l2_pix_format ov519_sif_mode[] = {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300244 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
245 .bytesperline = 176,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300246 .sizeimage = 176 * 144 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300247 .colorspace = V4L2_COLORSPACE_JPEG,
248 .priv = 1},
249 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
250 .bytesperline = 352,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300251 .sizeimage = 352 * 288 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300252 .colorspace = V4L2_COLORSPACE_JPEG,
253 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300254};
255
Hans de Goede49809d62009-06-07 12:10:39 -0300256static const struct v4l2_pix_format ov518_vga_mode[] = {
257 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
258 .bytesperline = 320,
259 .sizeimage = 320 * 240 * 3 / 8 + 590,
260 .colorspace = V4L2_COLORSPACE_JPEG,
261 .priv = 1},
262 {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
263 .bytesperline = 640,
264 .sizeimage = 640 * 480 * 3 / 8 + 590,
265 .colorspace = V4L2_COLORSPACE_JPEG,
266 .priv = 0},
267};
268static const struct v4l2_pix_format ov518_sif_mode[] = {
269 {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
270 .bytesperline = 176,
271 .sizeimage = 40000,
272 .colorspace = V4L2_COLORSPACE_JPEG,
273 .priv = 1},
274 {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
275 .bytesperline = 352,
276 .sizeimage = 352 * 288 * 3 / 8 + 590,
277 .colorspace = V4L2_COLORSPACE_JPEG,
278 .priv = 0},
279};
280
281
282/* Registers common to OV511 / OV518 */
283#define R51x_SYS_RESET 0x50
284#define R51x_SYS_INIT 0x53
285#define R51x_SYS_SNAP 0x52
286#define R51x_SYS_CUST_ID 0x5F
287#define R51x_COMP_LUT_BEGIN 0x80
288
289/* OV511 Camera interface register numbers */
290#define R511_SYS_LED_CTL 0x55 /* OV511+ only */
291#define OV511_RESET_NOREGS 0x3F /* All but OV511 & regs */
292
293/* OV518 Camera interface register numbers */
294#define R518_GPIO_OUT 0x56 /* OV518(+) only */
295#define R518_GPIO_CTL 0x57 /* OV518(+) only */
296
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300297/* OV519 Camera interface register numbers */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300298#define OV519_R10_H_SIZE 0x10
299#define OV519_R11_V_SIZE 0x11
300#define OV519_R12_X_OFFSETL 0x12
301#define OV519_R13_X_OFFSETH 0x13
302#define OV519_R14_Y_OFFSETL 0x14
303#define OV519_R15_Y_OFFSETH 0x15
304#define OV519_R16_DIVIDER 0x16
305#define OV519_R20_DFR 0x20
306#define OV519_R25_FORMAT 0x25
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300307
308/* OV519 System Controller register numbers */
309#define OV519_SYS_RESET1 0x51
310#define OV519_SYS_EN_CLK1 0x54
311
312#define OV519_GPIO_DATA_OUT0 0x71
313#define OV519_GPIO_IO_CTRL0 0x72
314
315#define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
316
317/* I2C registers */
318#define R51x_I2C_W_SID 0x41
319#define R51x_I2C_SADDR_3 0x42
320#define R51x_I2C_SADDR_2 0x43
321#define R51x_I2C_R_SID 0x44
322#define R51x_I2C_DATA 0x45
323#define R518_I2C_CTL 0x47 /* OV518(+) only */
324
325/* I2C ADDRESSES */
326#define OV7xx0_SID 0x42
327#define OV8xx0_SID 0xa0
328#define OV6xx0_SID 0xc0
329
330/* OV7610 registers */
331#define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
Hans de Goede49809d62009-06-07 12:10:39 -0300332#define OV7610_REG_BLUE 0x01 /* blue channel balance */
333#define OV7610_REG_RED 0x02 /* red channel balance */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300334#define OV7610_REG_SAT 0x03 /* saturation */
335#define OV8610_REG_HUE 0x04 /* 04 reserved */
336#define OV7610_REG_CNT 0x05 /* Y contrast */
337#define OV7610_REG_BRT 0x06 /* Y brightness */
338#define OV7610_REG_COM_C 0x14 /* misc common regs */
339#define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
340#define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
341#define OV7610_REG_COM_I 0x29 /* misc settings */
342
343/* OV7670 registers */
344#define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
345#define OV7670_REG_BLUE 0x01 /* blue gain */
346#define OV7670_REG_RED 0x02 /* red gain */
347#define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
348#define OV7670_REG_COM1 0x04 /* Control 1 */
349#define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
350#define OV7670_REG_COM3 0x0c /* Control 3 */
351#define OV7670_REG_COM4 0x0d /* Control 4 */
352#define OV7670_REG_COM5 0x0e /* All "reserved" */
353#define OV7670_REG_COM6 0x0f /* Control 6 */
354#define OV7670_REG_AECH 0x10 /* More bits of AEC value */
355#define OV7670_REG_CLKRC 0x11 /* Clock control */
356#define OV7670_REG_COM7 0x12 /* Control 7 */
357#define OV7670_COM7_FMT_VGA 0x00
358#define OV7670_COM7_YUV 0x00 /* YUV */
359#define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
360#define OV7670_COM7_FMT_MASK 0x38
361#define OV7670_COM7_RESET 0x80 /* Register reset */
362#define OV7670_REG_COM8 0x13 /* Control 8 */
363#define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
364#define OV7670_COM8_AWB 0x02 /* White balance enable */
365#define OV7670_COM8_AGC 0x04 /* Auto gain enable */
366#define OV7670_COM8_BFILT 0x20 /* Band filter enable */
367#define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
368#define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
369#define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
370#define OV7670_REG_COM10 0x15 /* Control 10 */
371#define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
372#define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
373#define OV7670_REG_VSTART 0x19 /* Vert start high bits */
374#define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
375#define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300376#define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300377#define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
378#define OV7670_REG_AEW 0x24 /* AGC upper limit */
379#define OV7670_REG_AEB 0x25 /* AGC lower limit */
380#define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
381#define OV7670_REG_HREF 0x32 /* HREF pieces */
382#define OV7670_REG_TSLB 0x3a /* lots of stuff */
383#define OV7670_REG_COM11 0x3b /* Control 11 */
384#define OV7670_COM11_EXP 0x02
385#define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
386#define OV7670_REG_COM12 0x3c /* Control 12 */
387#define OV7670_REG_COM13 0x3d /* Control 13 */
388#define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
389#define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
390#define OV7670_REG_COM14 0x3e /* Control 14 */
391#define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
392#define OV7670_REG_COM15 0x40 /* Control 15 */
393#define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
394#define OV7670_REG_COM16 0x41 /* Control 16 */
395#define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
396#define OV7670_REG_BRIGHT 0x55 /* Brightness */
397#define OV7670_REG_CONTRAS 0x56 /* Contrast control */
398#define OV7670_REG_GFIX 0x69 /* Fix gain control */
399#define OV7670_REG_RGB444 0x8c /* RGB 444 control */
400#define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
401#define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
402#define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
403#define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
404#define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
405#define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
406#define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
407#define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
408#define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
409
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300410struct ov_regvals {
411 __u8 reg;
412 __u8 val;
413};
414struct ov_i2c_regvals {
415 __u8 reg;
416 __u8 val;
417};
418
419static const struct ov_i2c_regvals norm_6x20[] = {
420 { 0x12, 0x80 }, /* reset */
421 { 0x11, 0x01 },
422 { 0x03, 0x60 },
423 { 0x05, 0x7f }, /* For when autoadjust is off */
424 { 0x07, 0xa8 },
425 /* The ratio of 0x0c and 0x0d controls the white point */
426 { 0x0c, 0x24 },
427 { 0x0d, 0x24 },
428 { 0x0f, 0x15 }, /* COMS */
429 { 0x10, 0x75 }, /* AEC Exposure time */
430 { 0x12, 0x24 }, /* Enable AGC */
431 { 0x14, 0x04 },
432 /* 0x16: 0x06 helps frame stability with moving objects */
433 { 0x16, 0x06 },
434/* { 0x20, 0x30 }, * Aperture correction enable */
435 { 0x26, 0xb2 }, /* BLC enable */
436 /* 0x28: 0x05 Selects RGB format if RGB on */
437 { 0x28, 0x05 },
438 { 0x2a, 0x04 }, /* Disable framerate adjust */
439/* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
440 { 0x2d, 0x99 },
441 { 0x33, 0xa0 }, /* Color Processing Parameter */
442 { 0x34, 0xd2 }, /* Max A/D range */
443 { 0x38, 0x8b },
444 { 0x39, 0x40 },
445
446 { 0x3c, 0x39 }, /* Enable AEC mode changing */
447 { 0x3c, 0x3c }, /* Change AEC mode */
448 { 0x3c, 0x24 }, /* Disable AEC mode changing */
449
450 { 0x3d, 0x80 },
451 /* These next two registers (0x4a, 0x4b) are undocumented.
452 * They control the color balance */
453 { 0x4a, 0x80 },
454 { 0x4b, 0x80 },
455 { 0x4d, 0xd2 }, /* This reduces noise a bit */
456 { 0x4e, 0xc1 },
457 { 0x4f, 0x04 },
458/* Do 50-53 have any effect? */
459/* Toggle 0x12[2] off and on here? */
460};
461
462static const struct ov_i2c_regvals norm_6x30[] = {
463 { 0x12, 0x80 }, /* Reset */
464 { 0x00, 0x1f }, /* Gain */
465 { 0x01, 0x99 }, /* Blue gain */
466 { 0x02, 0x7c }, /* Red gain */
467 { 0x03, 0xc0 }, /* Saturation */
468 { 0x05, 0x0a }, /* Contrast */
469 { 0x06, 0x95 }, /* Brightness */
470 { 0x07, 0x2d }, /* Sharpness */
471 { 0x0c, 0x20 },
472 { 0x0d, 0x20 },
Hans de Goede02ab18b2009-06-14 04:32:04 -0300473 { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300474 { 0x0f, 0x05 },
475 { 0x10, 0x9a },
476 { 0x11, 0x00 }, /* Pixel clock = fastest */
477 { 0x12, 0x24 }, /* Enable AGC and AWB */
478 { 0x13, 0x21 },
479 { 0x14, 0x80 },
480 { 0x15, 0x01 },
481 { 0x16, 0x03 },
482 { 0x17, 0x38 },
483 { 0x18, 0xea },
484 { 0x19, 0x04 },
485 { 0x1a, 0x93 },
486 { 0x1b, 0x00 },
487 { 0x1e, 0xc4 },
488 { 0x1f, 0x04 },
489 { 0x20, 0x20 },
490 { 0x21, 0x10 },
491 { 0x22, 0x88 },
492 { 0x23, 0xc0 }, /* Crystal circuit power level */
493 { 0x25, 0x9a }, /* Increase AEC black ratio */
494 { 0x26, 0xb2 }, /* BLC enable */
495 { 0x27, 0xa2 },
496 { 0x28, 0x00 },
497 { 0x29, 0x00 },
498 { 0x2a, 0x84 }, /* 60 Hz power */
499 { 0x2b, 0xa8 }, /* 60 Hz power */
500 { 0x2c, 0xa0 },
501 { 0x2d, 0x95 }, /* Enable auto-brightness */
502 { 0x2e, 0x88 },
503 { 0x33, 0x26 },
504 { 0x34, 0x03 },
505 { 0x36, 0x8f },
506 { 0x37, 0x80 },
507 { 0x38, 0x83 },
508 { 0x39, 0x80 },
509 { 0x3a, 0x0f },
510 { 0x3b, 0x3c },
511 { 0x3c, 0x1a },
512 { 0x3d, 0x80 },
513 { 0x3e, 0x80 },
514 { 0x3f, 0x0e },
515 { 0x40, 0x00 }, /* White bal */
516 { 0x41, 0x00 }, /* White bal */
517 { 0x42, 0x80 },
518 { 0x43, 0x3f }, /* White bal */
519 { 0x44, 0x80 },
520 { 0x45, 0x20 },
521 { 0x46, 0x20 },
522 { 0x47, 0x80 },
523 { 0x48, 0x7f },
524 { 0x49, 0x00 },
525 { 0x4a, 0x00 },
526 { 0x4b, 0x80 },
527 { 0x4c, 0xd0 },
528 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
529 { 0x4e, 0x40 },
530 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
531 { 0x50, 0xff },
532 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
533 { 0x55, 0xff },
534 { 0x56, 0x12 },
535 { 0x57, 0x81 },
536 { 0x58, 0x75 },
537 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
538 { 0x5a, 0x2c },
539 { 0x5b, 0x0f }, /* AWB chrominance levels */
540 { 0x5c, 0x10 },
541 { 0x3d, 0x80 },
542 { 0x27, 0xa6 },
543 { 0x12, 0x20 }, /* Toggle AWB */
544 { 0x12, 0x24 },
545};
546
547/* Lawrence Glaister <lg@jfm.bc.ca> reports:
548 *
549 * Register 0x0f in the 7610 has the following effects:
550 *
551 * 0x85 (AEC method 1): Best overall, good contrast range
552 * 0x45 (AEC method 2): Very overexposed
553 * 0xa5 (spec sheet default): Ok, but the black level is
554 * shifted resulting in loss of contrast
555 * 0x05 (old driver setting): very overexposed, too much
556 * contrast
557 */
558static const struct ov_i2c_regvals norm_7610[] = {
559 { 0x10, 0xff },
560 { 0x16, 0x06 },
561 { 0x28, 0x24 },
562 { 0x2b, 0xac },
563 { 0x12, 0x00 },
564 { 0x38, 0x81 },
565 { 0x28, 0x24 }, /* 0c */
566 { 0x0f, 0x85 }, /* lg's setting */
567 { 0x15, 0x01 },
568 { 0x20, 0x1c },
569 { 0x23, 0x2a },
570 { 0x24, 0x10 },
571 { 0x25, 0x8a },
572 { 0x26, 0xa2 },
573 { 0x27, 0xc2 },
574 { 0x2a, 0x04 },
575 { 0x2c, 0xfe },
576 { 0x2d, 0x93 },
577 { 0x30, 0x71 },
578 { 0x31, 0x60 },
579 { 0x32, 0x26 },
580 { 0x33, 0x20 },
581 { 0x34, 0x48 },
582 { 0x12, 0x24 },
583 { 0x11, 0x01 },
584 { 0x0c, 0x24 },
585 { 0x0d, 0x24 },
586};
587
588static const struct ov_i2c_regvals norm_7620[] = {
589 { 0x00, 0x00 }, /* gain */
590 { 0x01, 0x80 }, /* blue gain */
591 { 0x02, 0x80 }, /* red gain */
592 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
593 { 0x06, 0x60 },
594 { 0x07, 0x00 },
595 { 0x0c, 0x24 },
596 { 0x0c, 0x24 },
597 { 0x0d, 0x24 },
598 { 0x11, 0x01 },
599 { 0x12, 0x24 },
600 { 0x13, 0x01 },
601 { 0x14, 0x84 },
602 { 0x15, 0x01 },
603 { 0x16, 0x03 },
604 { 0x17, 0x2f },
605 { 0x18, 0xcf },
606 { 0x19, 0x06 },
607 { 0x1a, 0xf5 },
608 { 0x1b, 0x00 },
609 { 0x20, 0x18 },
610 { 0x21, 0x80 },
611 { 0x22, 0x80 },
612 { 0x23, 0x00 },
613 { 0x26, 0xa2 },
614 { 0x27, 0xea },
615 { 0x28, 0x20 },
616 { 0x29, 0x00 },
617 { 0x2a, 0x10 },
618 { 0x2b, 0x00 },
619 { 0x2c, 0x88 },
620 { 0x2d, 0x91 },
621 { 0x2e, 0x80 },
622 { 0x2f, 0x44 },
623 { 0x60, 0x27 },
624 { 0x61, 0x02 },
625 { 0x62, 0x5f },
626 { 0x63, 0xd5 },
627 { 0x64, 0x57 },
628 { 0x65, 0x83 },
629 { 0x66, 0x55 },
630 { 0x67, 0x92 },
631 { 0x68, 0xcf },
632 { 0x69, 0x76 },
633 { 0x6a, 0x22 },
634 { 0x6b, 0x00 },
635 { 0x6c, 0x02 },
636 { 0x6d, 0x44 },
637 { 0x6e, 0x80 },
638 { 0x6f, 0x1d },
639 { 0x70, 0x8b },
640 { 0x71, 0x00 },
641 { 0x72, 0x14 },
642 { 0x73, 0x54 },
643 { 0x74, 0x00 },
644 { 0x75, 0x8e },
645 { 0x76, 0x00 },
646 { 0x77, 0xff },
647 { 0x78, 0x80 },
648 { 0x79, 0x80 },
649 { 0x7a, 0x80 },
650 { 0x7b, 0xe2 },
651 { 0x7c, 0x00 },
652};
653
654/* 7640 and 7648. The defaults should be OK for most registers. */
655static const struct ov_i2c_regvals norm_7640[] = {
656 { 0x12, 0x80 },
657 { 0x12, 0x14 },
658};
659
660/* 7670. Defaults taken from OmniVision provided data,
661* as provided by Jonathan Corbet of OLPC */
662static const struct ov_i2c_regvals norm_7670[] = {
663 { OV7670_REG_COM7, OV7670_COM7_RESET },
664 { OV7670_REG_TSLB, 0x04 }, /* OV */
665 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
666 { OV7670_REG_CLKRC, 0x01 },
667/*
668 * Set the hardware window. These values from OV don't entirely
669 * make sense - hstop is less than hstart. But they work...
670 */
671 { OV7670_REG_HSTART, 0x13 },
672 { OV7670_REG_HSTOP, 0x01 },
673 { OV7670_REG_HREF, 0xb6 },
674 { OV7670_REG_VSTART, 0x02 },
675 { OV7670_REG_VSTOP, 0x7a },
676 { OV7670_REG_VREF, 0x0a },
677
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300678 { OV7670_REG_COM3, 0x00 },
679 { OV7670_REG_COM14, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300680/* Mystery scaling numbers */
681 { 0x70, 0x3a },
682 { 0x71, 0x35 },
683 { 0x72, 0x11 },
684 { 0x73, 0xf0 },
685 { 0xa2, 0x02 },
686/* { OV7670_REG_COM10, 0x0 }, */
687
688/* Gamma curve values */
689 { 0x7a, 0x20 },
690 { 0x7b, 0x10 },
691 { 0x7c, 0x1e },
692 { 0x7d, 0x35 },
693 { 0x7e, 0x5a },
694 { 0x7f, 0x69 },
695 { 0x80, 0x76 },
696 { 0x81, 0x80 },
697 { 0x82, 0x88 },
698 { 0x83, 0x8f },
699 { 0x84, 0x96 },
700 { 0x85, 0xa3 },
701 { 0x86, 0xaf },
702 { 0x87, 0xc4 },
703 { 0x88, 0xd7 },
704 { 0x89, 0xe8 },
705
706/* AGC and AEC parameters. Note we start by disabling those features,
707 then turn them only after tweaking the values. */
708 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
709 | OV7670_COM8_AECSTEP
710 | OV7670_COM8_BFILT },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300711 { OV7670_REG_GAIN, 0x00 },
712 { OV7670_REG_AECH, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300713 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
714 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
715 { OV7670_REG_BD50MAX, 0x05 },
716 { OV7670_REG_BD60MAX, 0x07 },
717 { OV7670_REG_AEW, 0x95 },
718 { OV7670_REG_AEB, 0x33 },
719 { OV7670_REG_VPT, 0xe3 },
720 { OV7670_REG_HAECC1, 0x78 },
721 { OV7670_REG_HAECC2, 0x68 },
722 { 0xa1, 0x03 }, /* magic */
723 { OV7670_REG_HAECC3, 0xd8 },
724 { OV7670_REG_HAECC4, 0xd8 },
725 { OV7670_REG_HAECC5, 0xf0 },
726 { OV7670_REG_HAECC6, 0x90 },
727 { OV7670_REG_HAECC7, 0x94 },
728 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
729 | OV7670_COM8_AECSTEP
730 | OV7670_COM8_BFILT
731 | OV7670_COM8_AGC
732 | OV7670_COM8_AEC },
733
734/* Almost all of these are magic "reserved" values. */
735 { OV7670_REG_COM5, 0x61 },
736 { OV7670_REG_COM6, 0x4b },
737 { 0x16, 0x02 },
738 { OV7670_REG_MVFP, 0x07 },
739 { 0x21, 0x02 },
740 { 0x22, 0x91 },
741 { 0x29, 0x07 },
742 { 0x33, 0x0b },
743 { 0x35, 0x0b },
744 { 0x37, 0x1d },
745 { 0x38, 0x71 },
746 { 0x39, 0x2a },
747 { OV7670_REG_COM12, 0x78 },
748 { 0x4d, 0x40 },
749 { 0x4e, 0x20 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300750 { OV7670_REG_GFIX, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300751 { 0x6b, 0x4a },
752 { 0x74, 0x10 },
753 { 0x8d, 0x4f },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300754 { 0x8e, 0x00 },
755 { 0x8f, 0x00 },
756 { 0x90, 0x00 },
757 { 0x91, 0x00 },
758 { 0x96, 0x00 },
759 { 0x9a, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300760 { 0xb0, 0x84 },
761 { 0xb1, 0x0c },
762 { 0xb2, 0x0e },
763 { 0xb3, 0x82 },
764 { 0xb8, 0x0a },
765
766/* More reserved magic, some of which tweaks white balance */
767 { 0x43, 0x0a },
768 { 0x44, 0xf0 },
769 { 0x45, 0x34 },
770 { 0x46, 0x58 },
771 { 0x47, 0x28 },
772 { 0x48, 0x3a },
773 { 0x59, 0x88 },
774 { 0x5a, 0x88 },
775 { 0x5b, 0x44 },
776 { 0x5c, 0x67 },
777 { 0x5d, 0x49 },
778 { 0x5e, 0x0e },
779 { 0x6c, 0x0a },
780 { 0x6d, 0x55 },
781 { 0x6e, 0x11 },
782 { 0x6f, 0x9f },
783 /* "9e for advance AWB" */
784 { 0x6a, 0x40 },
785 { OV7670_REG_BLUE, 0x40 },
786 { OV7670_REG_RED, 0x60 },
787 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
788 | OV7670_COM8_AECSTEP
789 | OV7670_COM8_BFILT
790 | OV7670_COM8_AGC
791 | OV7670_COM8_AEC
792 | OV7670_COM8_AWB },
793
794/* Matrix coefficients */
795 { 0x4f, 0x80 },
796 { 0x50, 0x80 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300797 { 0x51, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300798 { 0x52, 0x22 },
799 { 0x53, 0x5e },
800 { 0x54, 0x80 },
801 { 0x58, 0x9e },
802
803 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300804 { OV7670_REG_EDGE, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300805 { 0x75, 0x05 },
806 { 0x76, 0xe1 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300807 { 0x4c, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300808 { 0x77, 0x01 },
809 { OV7670_REG_COM13, OV7670_COM13_GAMMA
810 | OV7670_COM13_UVSAT
811 | 2}, /* was 3 */
812 { 0x4b, 0x09 },
813 { 0xc9, 0x60 },
814 { OV7670_REG_COM16, 0x38 },
815 { 0x56, 0x40 },
816
817 { 0x34, 0x11 },
818 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
819 { 0xa4, 0x88 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300820 { 0x96, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300821 { 0x97, 0x30 },
822 { 0x98, 0x20 },
823 { 0x99, 0x30 },
824 { 0x9a, 0x84 },
825 { 0x9b, 0x29 },
826 { 0x9c, 0x03 },
827 { 0x9d, 0x4c },
828 { 0x9e, 0x3f },
829 { 0x78, 0x04 },
830
831/* Extra-weird stuff. Some sort of multiplexor register */
832 { 0x79, 0x01 },
833 { 0xc8, 0xf0 },
834 { 0x79, 0x0f },
835 { 0xc8, 0x00 },
836 { 0x79, 0x10 },
837 { 0xc8, 0x7e },
838 { 0x79, 0x0a },
839 { 0xc8, 0x80 },
840 { 0x79, 0x0b },
841 { 0xc8, 0x01 },
842 { 0x79, 0x0c },
843 { 0xc8, 0x0f },
844 { 0x79, 0x0d },
845 { 0xc8, 0x20 },
846 { 0x79, 0x09 },
847 { 0xc8, 0x80 },
848 { 0x79, 0x02 },
849 { 0xc8, 0xc0 },
850 { 0x79, 0x03 },
851 { 0xc8, 0x40 },
852 { 0x79, 0x05 },
853 { 0xc8, 0x30 },
854 { 0x79, 0x26 },
855};
856
857static const struct ov_i2c_regvals norm_8610[] = {
858 { 0x12, 0x80 },
859 { 0x00, 0x00 },
860 { 0x01, 0x80 },
861 { 0x02, 0x80 },
862 { 0x03, 0xc0 },
863 { 0x04, 0x30 },
864 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
865 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
866 { 0x0a, 0x86 },
867 { 0x0b, 0xb0 },
868 { 0x0c, 0x20 },
869 { 0x0d, 0x20 },
870 { 0x11, 0x01 },
871 { 0x12, 0x25 },
872 { 0x13, 0x01 },
873 { 0x14, 0x04 },
874 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
875 { 0x16, 0x03 },
876 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
877 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
878 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
879 { 0x1a, 0xf5 },
880 { 0x1b, 0x00 },
881 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
882 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
883 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
884 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
885 { 0x26, 0xa2 },
886 { 0x27, 0xea },
887 { 0x28, 0x00 },
888 { 0x29, 0x00 },
889 { 0x2a, 0x80 },
890 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
891 { 0x2c, 0xac },
892 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
893 { 0x2e, 0x80 },
894 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
895 { 0x4c, 0x00 },
896 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
897 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
898 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
899 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
900 { 0x63, 0xff },
901 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
902 * maybe thats wrong */
903 { 0x65, 0x00 },
904 { 0x66, 0x55 },
905 { 0x67, 0xb0 },
906 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
907 { 0x69, 0x02 },
908 { 0x6a, 0x22 },
909 { 0x6b, 0x00 },
910 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
911 * deleting bit7 colors the first images red */
912 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
913 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
914 { 0x6f, 0x01 },
915 { 0x70, 0x8b },
916 { 0x71, 0x00 },
917 { 0x72, 0x14 },
918 { 0x73, 0x54 },
919 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
920 { 0x75, 0x0e },
921 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
922 { 0x77, 0xff },
923 { 0x78, 0x80 },
924 { 0x79, 0x80 },
925 { 0x7a, 0x80 },
926 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
927 { 0x7c, 0x00 },
928 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
929 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
930 { 0x7f, 0xfb },
931 { 0x80, 0x28 },
932 { 0x81, 0x00 },
933 { 0x82, 0x23 },
934 { 0x83, 0x0b },
935 { 0x84, 0x00 },
936 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
937 { 0x86, 0xc9 },
938 { 0x87, 0x00 },
939 { 0x88, 0x00 },
940 { 0x89, 0x01 },
941 { 0x12, 0x20 },
942 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
943};
944
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300945static unsigned char ov7670_abs_to_sm(unsigned char v)
946{
947 if (v > 127)
948 return v & 0x7f;
949 return (128 - v) | 0x80;
950}
951
952/* Write a OV519 register */
953static int reg_w(struct sd *sd, __u16 index, __u8 value)
954{
955 int ret;
Hans de Goede49809d62009-06-07 12:10:39 -0300956 int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 2 : 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300957
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300958 sd->gspca_dev.usb_buf[0] = value;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300959 ret = usb_control_msg(sd->gspca_dev.dev,
960 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goede49809d62009-06-07 12:10:39 -0300961 req,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300962 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
963 0, index,
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300964 sd->gspca_dev.usb_buf, 1, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300965 if (ret < 0)
966 PDEBUG(D_ERR, "Write reg [%02x] %02x failed", index, value);
967 return ret;
968}
969
970/* Read from a OV519 register */
971/* returns: negative is error, pos or zero is data */
972static int reg_r(struct sd *sd, __u16 index)
973{
974 int ret;
Hans de Goede49809d62009-06-07 12:10:39 -0300975 int req = (sd->bridge <= BRIDGE_OV511PLUS) ? 3 : 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300976
977 ret = usb_control_msg(sd->gspca_dev.dev,
978 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goede49809d62009-06-07 12:10:39 -0300979 req,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300980 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300981 0, index, sd->gspca_dev.usb_buf, 1, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300982
983 if (ret >= 0)
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300984 ret = sd->gspca_dev.usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300985 else
986 PDEBUG(D_ERR, "Read reg [0x%02x] failed", index);
987 return ret;
988}
989
990/* Read 8 values from a OV519 register */
991static int reg_r8(struct sd *sd,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -0300992 __u16 index)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300993{
994 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300995
996 ret = usb_control_msg(sd->gspca_dev.dev,
997 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
998 1, /* REQ_IO */
999 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001000 0, index, sd->gspca_dev.usb_buf, 8, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001001
1002 if (ret >= 0)
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001003 ret = sd->gspca_dev.usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001004 else
1005 PDEBUG(D_ERR, "Read reg 8 [0x%02x] failed", index);
1006 return ret;
1007}
1008
1009/*
1010 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1011 * the same position as 1's in "mask" are cleared and set to "value". Bits
1012 * that are in the same position as 0's in "mask" are preserved, regardless
1013 * of their respective state in "value".
1014 */
1015static int reg_w_mask(struct sd *sd,
1016 __u16 index,
1017 __u8 value,
1018 __u8 mask)
1019{
1020 int ret;
1021 __u8 oldval;
1022
1023 if (mask != 0xff) {
1024 value &= mask; /* Enforce mask on value */
1025 ret = reg_r(sd, index);
1026 if (ret < 0)
1027 return ret;
1028
1029 oldval = ret & ~mask; /* Clear the masked bits */
1030 value |= oldval; /* Set the desired bits */
1031 }
1032 return reg_w(sd, index, value);
1033}
1034
1035/*
Hans de Goede49809d62009-06-07 12:10:39 -03001036 * Writes multiple (n) byte value to a single register. Only valid with certain
1037 * registers (0x30 and 0xc4 - 0xce).
1038 */
1039static int ov518_reg_w32(struct sd *sd, __u16 index, u32 value, int n)
1040{
1041 int ret;
1042
1043 *((u32 *)sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
1044
1045 ret = usb_control_msg(sd->gspca_dev.dev,
1046 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1047 1 /* REG_IO */,
1048 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1049 0, index,
1050 sd->gspca_dev.usb_buf, n, 500);
1051 if (ret < 0)
1052 PDEBUG(D_ERR, "Write reg32 [%02x] %08x failed", index, value);
1053 return ret;
1054}
1055
1056
1057/*
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001058 * The OV518 I2C I/O procedure is different, hence, this function.
1059 * This is normally only called from i2c_w(). Note that this function
1060 * always succeeds regardless of whether the sensor is present and working.
1061 */
1062static int i2c_w(struct sd *sd,
1063 __u8 reg,
1064 __u8 value)
1065{
1066 int rc;
1067
1068 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
1069
1070 /* Select camera register */
1071 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
1072 if (rc < 0)
1073 return rc;
1074
1075 /* Write "value" to I2C data port of OV511 */
1076 rc = reg_w(sd, R51x_I2C_DATA, value);
1077 if (rc < 0)
1078 return rc;
1079
1080 /* Initiate 3-byte write cycle */
1081 rc = reg_w(sd, R518_I2C_CTL, 0x01);
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001082 if (rc < 0)
1083 return rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001084
1085 /* wait for write complete */
1086 msleep(4);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001087 return reg_r8(sd, R518_I2C_CTL);
1088}
1089
1090/*
1091 * returns: negative is error, pos or zero is data
1092 *
1093 * The OV518 I2C I/O procedure is different, hence, this function.
1094 * This is normally only called from i2c_r(). Note that this function
1095 * always succeeds regardless of whether the sensor is present and working.
1096 */
1097static int i2c_r(struct sd *sd, __u8 reg)
1098{
1099 int rc, value;
1100
1101 /* Select camera register */
1102 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
1103 if (rc < 0)
1104 return rc;
1105
1106 /* Initiate 2-byte write cycle */
1107 rc = reg_w(sd, R518_I2C_CTL, 0x03);
1108 if (rc < 0)
1109 return rc;
1110
1111 /* Initiate 2-byte read cycle */
1112 rc = reg_w(sd, R518_I2C_CTL, 0x05);
1113 if (rc < 0)
1114 return rc;
1115 value = reg_r(sd, R51x_I2C_DATA);
1116 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
1117 return value;
1118}
1119
1120/* Writes bits at positions specified by mask to an I2C reg. Bits that are in
1121 * the same position as 1's in "mask" are cleared and set to "value". Bits
1122 * that are in the same position as 0's in "mask" are preserved, regardless
1123 * of their respective state in "value".
1124 */
1125static int i2c_w_mask(struct sd *sd,
1126 __u8 reg,
1127 __u8 value,
1128 __u8 mask)
1129{
1130 int rc;
1131 __u8 oldval;
1132
1133 value &= mask; /* Enforce mask on value */
1134 rc = i2c_r(sd, reg);
1135 if (rc < 0)
1136 return rc;
1137 oldval = rc & ~mask; /* Clear the masked bits */
1138 value |= oldval; /* Set the desired bits */
1139 return i2c_w(sd, reg, value);
1140}
1141
1142/* Temporarily stops OV511 from functioning. Must do this before changing
1143 * registers while the camera is streaming */
1144static inline int ov51x_stop(struct sd *sd)
1145{
1146 PDEBUG(D_STREAM, "stopping");
1147 sd->stopped = 1;
Hans de Goede49809d62009-06-07 12:10:39 -03001148 switch (sd->bridge) {
1149 case BRIDGE_OV511:
1150 case BRIDGE_OV511PLUS:
1151 return reg_w(sd, R51x_SYS_RESET, 0x3d);
1152 case BRIDGE_OV518:
1153 case BRIDGE_OV518PLUS:
1154 return reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
1155 case BRIDGE_OV519:
1156 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
1157 }
1158
1159 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001160}
1161
1162/* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
1163 * actually stopped (for performance). */
1164static inline int ov51x_restart(struct sd *sd)
1165{
Hans de Goede49809d62009-06-07 12:10:39 -03001166 int rc;
1167
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001168 PDEBUG(D_STREAM, "restarting");
1169 if (!sd->stopped)
1170 return 0;
1171 sd->stopped = 0;
1172
1173 /* Reinitialize the stream */
Hans de Goede49809d62009-06-07 12:10:39 -03001174 switch (sd->bridge) {
1175 case BRIDGE_OV511:
1176 case BRIDGE_OV511PLUS:
1177 return reg_w(sd, R51x_SYS_RESET, 0x00);
1178 case BRIDGE_OV518:
1179 case BRIDGE_OV518PLUS:
1180 rc = reg_w(sd, 0x2f, 0x80);
1181 if (rc < 0)
1182 return rc;
1183 return reg_w(sd, R51x_SYS_RESET, 0x00);
1184 case BRIDGE_OV519:
1185 return reg_w(sd, OV519_SYS_RESET1, 0x00);
1186 }
1187
1188 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001189}
1190
1191/* This does an initial reset of an OmniVision sensor and ensures that I2C
1192 * is synchronized. Returns <0 on failure.
1193 */
1194static int init_ov_sensor(struct sd *sd)
1195{
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001196 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001197
1198 /* Reset the sensor */
1199 if (i2c_w(sd, 0x12, 0x80) < 0)
1200 return -EIO;
1201
1202 /* Wait for it to initialize */
1203 msleep(150);
1204
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001205 for (i = 0; i < i2c_detect_tries; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001206 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
1207 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001208 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
1209 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001210 }
1211
1212 /* Reset the sensor */
1213 if (i2c_w(sd, 0x12, 0x80) < 0)
1214 return -EIO;
1215 /* Wait for it to initialize */
1216 msleep(150);
1217 /* Dummy read to sync I2C */
1218 if (i2c_r(sd, 0x00) < 0)
1219 return -EIO;
1220 }
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001221 return -EIO;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001222}
1223
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001224/* Set the read and write slave IDs. The "slave" argument is the write slave,
1225 * and the read slave will be set to (slave + 1).
1226 * This should not be called from outside the i2c I/O functions.
1227 * Sets I2C read and write slave IDs. Returns <0 for error
1228 */
1229static int ov51x_set_slave_ids(struct sd *sd,
1230 __u8 slave)
1231{
1232 int rc;
1233
1234 rc = reg_w(sd, R51x_I2C_W_SID, slave);
1235 if (rc < 0)
1236 return rc;
1237 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
1238}
1239
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001240static int write_regvals(struct sd *sd,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001241 const struct ov_regvals *regvals,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001242 int n)
1243{
1244 int rc;
1245
1246 while (--n >= 0) {
1247 rc = reg_w(sd, regvals->reg, regvals->val);
1248 if (rc < 0)
1249 return rc;
1250 regvals++;
1251 }
1252 return 0;
1253}
1254
1255static int write_i2c_regvals(struct sd *sd,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001256 const struct ov_i2c_regvals *regvals,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001257 int n)
1258{
1259 int rc;
1260
1261 while (--n >= 0) {
1262 rc = i2c_w(sd, regvals->reg, regvals->val);
1263 if (rc < 0)
1264 return rc;
1265 regvals++;
1266 }
1267 return 0;
1268}
1269
1270/****************************************************************************
1271 *
1272 * OV511 and sensor configuration
1273 *
1274 ***************************************************************************/
1275
1276/* This initializes the OV8110, OV8610 sensor. The OV8110 uses
1277 * the same register settings as the OV8610, since they are very similar.
1278 */
1279static int ov8xx0_configure(struct sd *sd)
1280{
1281 int rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001282
1283 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
1284
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001285 /* Detect sensor (sub)type */
1286 rc = i2c_r(sd, OV7610_REG_COM_I);
1287 if (rc < 0) {
1288 PDEBUG(D_ERR, "Error detecting sensor type");
1289 return -1;
1290 }
1291 if ((rc & 3) == 1) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001292 sd->sensor = SEN_OV8610;
1293 } else {
1294 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1295 return -1;
1296 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001297
1298 /* Set sensor-specific vars */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001299/* sd->sif = 0; already done */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001300 return 0;
1301}
1302
1303/* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
1304 * the same register settings as the OV7610, since they are very similar.
1305 */
1306static int ov7xx0_configure(struct sd *sd)
1307{
1308 int rc, high, low;
1309
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001310
1311 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
1312
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001313 /* Detect sensor (sub)type */
1314 rc = i2c_r(sd, OV7610_REG_COM_I);
1315
1316 /* add OV7670 here
1317 * it appears to be wrongly detected as a 7610 by default */
1318 if (rc < 0) {
1319 PDEBUG(D_ERR, "Error detecting sensor type");
1320 return -1;
1321 }
1322 if ((rc & 3) == 3) {
1323 /* quick hack to make OV7670s work */
1324 high = i2c_r(sd, 0x0a);
1325 low = i2c_r(sd, 0x0b);
1326 /* info("%x, %x", high, low); */
1327 if (high == 0x76 && low == 0x73) {
1328 PDEBUG(D_PROBE, "Sensor is an OV7670");
1329 sd->sensor = SEN_OV7670;
1330 } else {
1331 PDEBUG(D_PROBE, "Sensor is an OV7610");
1332 sd->sensor = SEN_OV7610;
1333 }
1334 } else if ((rc & 3) == 1) {
1335 /* I don't know what's different about the 76BE yet. */
1336 if (i2c_r(sd, 0x15) & 1)
1337 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
1338 else
1339 PDEBUG(D_PROBE, "Sensor is an OV76BE");
1340
1341 /* OV511+ will return all zero isoc data unless we
1342 * configure the sensor as a 7620. Someone needs to
1343 * find the exact reg. setting that causes this. */
1344 sd->sensor = SEN_OV76BE;
1345 } else if ((rc & 3) == 0) {
1346 /* try to read product id registers */
1347 high = i2c_r(sd, 0x0a);
1348 if (high < 0) {
1349 PDEBUG(D_ERR, "Error detecting camera chip PID");
1350 return high;
1351 }
1352 low = i2c_r(sd, 0x0b);
1353 if (low < 0) {
1354 PDEBUG(D_ERR, "Error detecting camera chip VER");
1355 return low;
1356 }
1357 if (high == 0x76) {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001358 switch (low) {
1359 case 0x30:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001360 PDEBUG(D_PROBE, "Sensor is an OV7630/OV7635");
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001361 PDEBUG(D_ERR,
1362 "7630 is not supported by this driver");
1363 return -1;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001364 case 0x40:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001365 PDEBUG(D_PROBE, "Sensor is an OV7645");
1366 sd->sensor = SEN_OV7640; /* FIXME */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001367 break;
1368 case 0x45:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001369 PDEBUG(D_PROBE, "Sensor is an OV7645B");
1370 sd->sensor = SEN_OV7640; /* FIXME */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001371 break;
1372 case 0x48:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001373 PDEBUG(D_PROBE, "Sensor is an OV7648");
1374 sd->sensor = SEN_OV7640; /* FIXME */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001375 break;
1376 default:
1377 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001378 return -1;
1379 }
1380 } else {
1381 PDEBUG(D_PROBE, "Sensor is an OV7620");
1382 sd->sensor = SEN_OV7620;
1383 }
1384 } else {
1385 PDEBUG(D_ERR, "Unknown image sensor version: %d", rc & 3);
1386 return -1;
1387 }
1388
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001389 /* Set sensor-specific vars */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001390/* sd->sif = 0; already done */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001391 return 0;
1392}
1393
1394/* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
1395static int ov6xx0_configure(struct sd *sd)
1396{
1397 int rc;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001398 PDEBUG(D_PROBE, "starting OV6xx0 configuration");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001399
1400 /* Detect sensor (sub)type */
1401 rc = i2c_r(sd, OV7610_REG_COM_I);
1402 if (rc < 0) {
1403 PDEBUG(D_ERR, "Error detecting sensor type");
1404 return -1;
1405 }
1406
1407 /* Ugh. The first two bits are the version bits, but
1408 * the entire register value must be used. I guess OVT
1409 * underestimated how many variants they would make. */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001410 switch (rc) {
1411 case 0x00:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001412 sd->sensor = SEN_OV6630;
1413 PDEBUG(D_ERR,
1414 "WARNING: Sensor is an OV66308. Your camera may have");
1415 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001416 break;
1417 case 0x01:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001418 sd->sensor = SEN_OV6620;
Hans de Goede7d971372009-06-14 05:28:17 -03001419 PDEBUG(D_PROBE, "Sensor is an OV6620");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001420 break;
1421 case 0x02:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001422 sd->sensor = SEN_OV6630;
1423 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001424 break;
1425 case 0x03:
Hans de Goede7d971372009-06-14 05:28:17 -03001426 sd->sensor = SEN_OV66308AF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001427 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001428 break;
1429 case 0x90:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001430 sd->sensor = SEN_OV6630;
1431 PDEBUG(D_ERR,
1432 "WARNING: Sensor is an OV66307. Your camera may have");
1433 PDEBUG(D_ERR, "been misdetected in previous driver versions.");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001434 break;
1435 default:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001436 PDEBUG(D_ERR, "FATAL: Unknown sensor version: 0x%02x", rc);
1437 return -1;
1438 }
1439
1440 /* Set sensor-specific vars */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001441 sd->sif = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001442
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001443 return 0;
1444}
1445
1446/* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
1447static void ov51x_led_control(struct sd *sd, int on)
1448{
Hans de Goede49809d62009-06-07 12:10:39 -03001449 switch (sd->bridge) {
1450 /* OV511 has no LED control */
1451 case BRIDGE_OV511PLUS:
1452 reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
1453 break;
1454 case BRIDGE_OV518:
1455 case BRIDGE_OV518PLUS:
1456 reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
1457 break;
1458 case BRIDGE_OV519:
1459 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
1460 break;
1461 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001462}
1463
Hans de Goede49809d62009-06-07 12:10:39 -03001464/* OV518 quantization tables are 8x4 (instead of 8x8) */
1465static int ov518_upload_quan_tables(struct sd *sd)
1466{
1467 const unsigned char yQuanTable518[] = {
1468 5, 4, 5, 6, 6, 7, 7, 7,
1469 5, 5, 5, 5, 6, 7, 7, 7,
1470 6, 6, 6, 6, 7, 7, 7, 8,
1471 7, 7, 6, 7, 7, 7, 8, 8
1472 };
1473
1474 const unsigned char uvQuanTable518[] = {
1475 6, 6, 6, 7, 7, 7, 7, 7,
1476 6, 6, 6, 7, 7, 7, 7, 7,
1477 6, 6, 6, 7, 7, 7, 7, 8,
1478 7, 7, 7, 7, 7, 7, 8, 8
1479 };
1480
1481 const unsigned char *pYTable = yQuanTable518;
1482 const unsigned char *pUVTable = uvQuanTable518;
1483 unsigned char val0, val1;
1484 int i, rc, reg = R51x_COMP_LUT_BEGIN;
1485
1486 PDEBUG(D_PROBE, "Uploading quantization tables");
1487
1488 for (i = 0; i < 16; i++) {
1489 val0 = *pYTable++;
1490 val1 = *pYTable++;
1491 val0 &= 0x0f;
1492 val1 &= 0x0f;
1493 val0 |= val1 << 4;
1494 rc = reg_w(sd, reg, val0);
1495 if (rc < 0)
1496 return rc;
1497
1498 val0 = *pUVTable++;
1499 val1 = *pUVTable++;
1500 val0 &= 0x0f;
1501 val1 &= 0x0f;
1502 val0 |= val1 << 4;
1503 rc = reg_w(sd, reg + 16, val0);
1504 if (rc < 0)
1505 return rc;
1506
1507 reg++;
1508 }
1509
1510 return 0;
1511}
1512
1513/* This initializes the OV518/OV518+ and the sensor */
1514static int ov518_configure(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001515{
1516 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede49809d62009-06-07 12:10:39 -03001517 int rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001518
Hans de Goede49809d62009-06-07 12:10:39 -03001519 /* For 518 and 518+ */
1520 static struct ov_regvals init_518[] = {
1521 { R51x_SYS_RESET, 0x40 },
1522 { R51x_SYS_INIT, 0xe1 },
1523 { R51x_SYS_RESET, 0x3e },
1524 { R51x_SYS_INIT, 0xe1 },
1525 { R51x_SYS_RESET, 0x00 },
1526 { R51x_SYS_INIT, 0xe1 },
1527 { 0x46, 0x00 },
1528 { 0x5d, 0x03 },
1529 };
1530
1531 static struct ov_regvals norm_518[] = {
1532 { R51x_SYS_SNAP, 0x02 }, /* Reset */
1533 { R51x_SYS_SNAP, 0x01 }, /* Enable */
1534 { 0x31, 0x0f },
1535 { 0x5d, 0x03 },
1536 { 0x24, 0x9f },
1537 { 0x25, 0x90 },
1538 { 0x20, 0x00 },
1539 { 0x51, 0x04 },
1540 { 0x71, 0x19 },
1541 { 0x2f, 0x80 },
1542 };
1543
1544 static struct ov_regvals norm_518_p[] = {
1545 { R51x_SYS_SNAP, 0x02 }, /* Reset */
1546 { R51x_SYS_SNAP, 0x01 }, /* Enable */
1547 { 0x31, 0x0f },
1548 { 0x5d, 0x03 },
1549 { 0x24, 0x9f },
1550 { 0x25, 0x90 },
1551 { 0x20, 0x60 },
1552 { 0x51, 0x02 },
1553 { 0x71, 0x19 },
1554 { 0x40, 0xff },
1555 { 0x41, 0x42 },
1556 { 0x46, 0x00 },
1557 { 0x33, 0x04 },
1558 { 0x21, 0x19 },
1559 { 0x3f, 0x10 },
1560 { 0x2f, 0x80 },
1561 };
1562
1563 /* First 5 bits of custom ID reg are a revision ID on OV518 */
1564 PDEBUG(D_PROBE, "Device revision %d",
1565 0x1F & reg_r(sd, R51x_SYS_CUST_ID));
1566
1567 rc = write_regvals(sd, init_518, ARRAY_SIZE(init_518));
1568 if (rc < 0)
1569 return rc;
1570
1571 /* Set LED GPIO pin to output mode */
1572 rc = reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
1573 if (rc < 0)
1574 return rc;
1575
1576 switch (sd->bridge) {
1577 case BRIDGE_OV518:
1578 rc = write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
1579 if (rc < 0)
1580 return rc;
1581 break;
1582 case BRIDGE_OV518PLUS:
1583 rc = write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
1584 if (rc < 0)
1585 return rc;
1586 break;
1587 }
1588
1589 rc = ov518_upload_quan_tables(sd);
1590 if (rc < 0) {
1591 PDEBUG(D_ERR, "Error uploading quantization tables");
1592 return rc;
1593 }
1594
1595 rc = reg_w(sd, 0x2f, 0x80);
1596 if (rc < 0)
1597 return rc;
1598
1599 return 0;
1600}
1601
1602static int ov519_configure(struct sd *sd)
1603{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001604 static const struct ov_regvals init_519[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001605 { 0x5a, 0x6d }, /* EnableSystem */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001606 { 0x53, 0x9b },
1607 { 0x54, 0xff }, /* set bit2 to enable jpeg */
1608 { 0x5d, 0x03 },
1609 { 0x49, 0x01 },
1610 { 0x48, 0x00 },
1611 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
1612 * detection will fail. This deserves further investigation. */
1613 { OV519_GPIO_IO_CTRL0, 0xee },
1614 { 0x51, 0x0f }, /* SetUsbInit */
1615 { 0x51, 0x00 },
1616 { 0x22, 0x00 },
1617 /* windows reads 0x55 at this point*/
1618 };
1619
Hans de Goede49809d62009-06-07 12:10:39 -03001620 return write_regvals(sd, init_519, ARRAY_SIZE(init_519));
1621}
1622
1623/* this function is called at probe time */
1624static int sd_config(struct gspca_dev *gspca_dev,
1625 const struct usb_device_id *id)
1626{
1627 struct sd *sd = (struct sd *) gspca_dev;
1628 struct cam *cam;
1629 int ret = 0;
1630
1631 sd->bridge = id->driver_info;
1632
1633 switch (sd->bridge) {
1634 case BRIDGE_OV518:
1635 case BRIDGE_OV518PLUS:
1636 ret = ov518_configure(gspca_dev);
1637 break;
1638 case BRIDGE_OV519:
1639 ret = ov519_configure(sd);
1640 break;
1641 }
1642
1643 if (ret)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001644 goto error;
Hans de Goede49809d62009-06-07 12:10:39 -03001645
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001646 ov51x_led_control(sd, 0); /* turn LED off */
1647
1648 /* Test for 76xx */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001649 if (ov51x_set_slave_ids(sd, OV7xx0_SID) < 0)
1650 goto error;
1651
1652 /* The OV519 must be more aggressive about sensor detection since
1653 * I2C write will never fail if the sensor is not present. We have
1654 * to try to initialize the sensor to detect its presence */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001655 if (init_ov_sensor(sd) >= 0) {
1656 if (ov7xx0_configure(sd) < 0) {
1657 PDEBUG(D_ERR, "Failed to configure OV7xx0");
1658 goto error;
1659 }
1660 } else {
1661
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001662 /* Test for 6xx0 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001663 if (ov51x_set_slave_ids(sd, OV6xx0_SID) < 0)
1664 goto error;
1665
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001666 if (init_ov_sensor(sd) >= 0) {
1667 if (ov6xx0_configure(sd) < 0) {
1668 PDEBUG(D_ERR, "Failed to configure OV6xx0");
1669 goto error;
1670 }
1671 } else {
1672
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001673 /* Test for 8xx0 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001674 if (ov51x_set_slave_ids(sd, OV8xx0_SID) < 0)
1675 goto error;
1676
1677 if (init_ov_sensor(sd) < 0) {
1678 PDEBUG(D_ERR,
1679 "Can't determine sensor slave IDs");
1680 goto error;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001681 }
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001682 if (ov8xx0_configure(sd) < 0) {
1683 PDEBUG(D_ERR,
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001684 "Failed to configure OV8xx0 sensor");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001685 goto error;
1686 }
1687 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001688 }
1689
1690 cam = &gspca_dev->cam;
Hans de Goede49809d62009-06-07 12:10:39 -03001691 switch (sd->bridge) {
1692 case BRIDGE_OV518:
1693 case BRIDGE_OV518PLUS:
1694 if (!sd->sif) {
1695 cam->cam_mode = ov518_vga_mode;
1696 cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
1697 } else {
1698 cam->cam_mode = ov518_sif_mode;
1699 cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
1700 }
1701 break;
1702 case BRIDGE_OV519:
1703 if (!sd->sif) {
1704 cam->cam_mode = ov519_vga_mode;
1705 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
1706 } else {
1707 cam->cam_mode = ov519_sif_mode;
1708 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
1709 }
1710 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001711 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001712 sd->brightness = BRIGHTNESS_DEF;
1713 sd->contrast = CONTRAST_DEF;
1714 sd->colors = COLOR_DEF;
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03001715 sd->hflip = HFLIP_DEF;
1716 sd->vflip = VFLIP_DEF;
Hans de Goede02ab18b2009-06-14 04:32:04 -03001717 sd->autobrightness = AUTOBRIGHT_DEF;
1718 if (sd->sensor == SEN_OV7670) {
1719 sd->freq = OV7670_FREQ_DEF;
1720 gspca_dev->ctrl_dis = 1 << FREQ_IDX;
1721 } else {
1722 sd->freq = FREQ_DEF;
1723 gspca_dev->ctrl_dis = (1 << HFLIP_IDX) | (1 << VFLIP_IDX) |
1724 (1 << OV7670_FREQ_IDX);
1725 }
1726 if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
1727 gspca_dev->ctrl_dis |= 1 << AUTOBRIGHT_IDX;
1728 /* OV8610 Frequency filter control should work but needs testing */
1729 if (sd->sensor == SEN_OV8610)
1730 gspca_dev->ctrl_dis |= 1 << FREQ_IDX;
1731
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001732 return 0;
1733error:
1734 PDEBUG(D_ERR, "OV519 Config failed");
1735 return -EBUSY;
1736}
1737
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001738/* this function is called at probe and resume time */
1739static int sd_init(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001740{
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001741 struct sd *sd = (struct sd *) gspca_dev;
1742
1743 /* initialize the sensor */
1744 switch (sd->sensor) {
1745 case SEN_OV6620:
1746 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
1747 return -EIO;
1748 break;
1749 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03001750 case SEN_OV66308AF:
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001751 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
1752 return -EIO;
1753 break;
1754 default:
1755/* case SEN_OV7610: */
1756/* case SEN_OV76BE: */
1757 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
1758 return -EIO;
1759 break;
1760 case SEN_OV7620:
1761 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
1762 return -EIO;
1763 break;
1764 case SEN_OV7640:
1765 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
1766 return -EIO;
1767 break;
1768 case SEN_OV7670:
1769 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
1770 return -EIO;
1771 break;
1772 case SEN_OV8610:
1773 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
1774 return -EIO;
1775 break;
1776 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001777 return 0;
1778}
1779
Hans de Goede49809d62009-06-07 12:10:39 -03001780/* Sets up the OV518/OV518+ with the given image parameters
1781 *
1782 * OV518 needs a completely different approach, until we can figure out what
1783 * the individual registers do. Also, only 15 FPS is supported now.
1784 *
1785 * Do not put any sensor-specific code in here (including I2C I/O functions)
1786 */
1787static int ov518_mode_init_regs(struct sd *sd)
1788{
1789 int hsegs, vsegs;
1790
1791 /******** Set the mode ********/
1792
1793 reg_w(sd, 0x2b, 0);
1794 reg_w(sd, 0x2c, 0);
1795 reg_w(sd, 0x2d, 0);
1796 reg_w(sd, 0x2e, 0);
1797 reg_w(sd, 0x3b, 0);
1798 reg_w(sd, 0x3c, 0);
1799 reg_w(sd, 0x3d, 0);
1800 reg_w(sd, 0x3e, 0);
1801
1802 if (sd->bridge == BRIDGE_OV518) {
1803 /* Set 8-bit (YVYU) input format */
1804 reg_w_mask(sd, 0x20, 0x08, 0x08);
1805
1806 /* Set 12-bit (4:2:0) output format */
1807 reg_w_mask(sd, 0x28, 0x80, 0xf0);
1808 reg_w_mask(sd, 0x38, 0x80, 0xf0);
1809 } else {
1810 reg_w(sd, 0x28, 0x80);
1811 reg_w(sd, 0x38, 0x80);
1812 }
1813
1814 hsegs = sd->gspca_dev.width / 16;
1815 vsegs = sd->gspca_dev.height / 4;
1816
1817 reg_w(sd, 0x29, hsegs);
1818 reg_w(sd, 0x2a, vsegs);
1819
1820 reg_w(sd, 0x39, hsegs);
1821 reg_w(sd, 0x3a, vsegs);
1822
1823 /* Windows driver does this here; who knows why */
1824 reg_w(sd, 0x2f, 0x80);
1825
1826 /******** Set the framerate (to 30 FPS) ********/
1827 if (sd->bridge == BRIDGE_OV518PLUS)
1828 sd->clockdiv = 1;
1829 else
1830 sd->clockdiv = 0;
1831
1832 /* Mode independent, but framerate dependent, regs */
1833 reg_w(sd, 0x51, 0x04); /* Clock divider; lower==faster */
1834 reg_w(sd, 0x22, 0x18);
1835 reg_w(sd, 0x23, 0xff);
1836
1837 if (sd->bridge == BRIDGE_OV518PLUS)
1838 reg_w(sd, 0x21, 0x19);
1839 else
1840 reg_w(sd, 0x71, 0x17); /* Compression-related? */
1841
1842 /* FIXME: Sensor-specific */
1843 /* Bit 5 is what matters here. Of course, it is "reserved" */
1844 i2c_w(sd, 0x54, 0x23);
1845
1846 reg_w(sd, 0x2f, 0x80);
1847
1848 if (sd->bridge == BRIDGE_OV518PLUS) {
1849 reg_w(sd, 0x24, 0x94);
1850 reg_w(sd, 0x25, 0x90);
1851 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
1852 ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */
1853 ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */
1854 ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */
1855 ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */
1856 ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */
1857 ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */
1858 ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */
1859 ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */
1860 } else {
1861 reg_w(sd, 0x24, 0x9f);
1862 reg_w(sd, 0x25, 0x90);
1863 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
1864 ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */
1865 ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */
1866 ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */
1867 ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */
1868 ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */
1869 ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */
1870 ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */
1871 ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */
1872 }
1873
1874 reg_w(sd, 0x2f, 0x80);
1875
1876 return 0;
1877}
1878
1879
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001880/* Sets up the OV519 with the given image parameters
1881 *
1882 * OV519 needs a completely different approach, until we can figure out what
1883 * the individual registers do.
1884 *
1885 * Do not put any sensor-specific code in here (including I2C I/O functions)
1886 */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001887static int ov519_mode_init_regs(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001888{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001889 static const struct ov_regvals mode_init_519_ov7670[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001890 { 0x5d, 0x03 }, /* Turn off suspend mode */
1891 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1892 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1893 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1894 { 0xa3, 0x18 },
1895 { 0xa4, 0x04 },
1896 { 0xa5, 0x28 },
1897 { 0x37, 0x00 }, /* SetUsbInit */
1898 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1899 /* Enable both fields, YUV Input, disable defect comp (why?) */
1900 { 0x20, 0x0c },
1901 { 0x21, 0x38 },
1902 { 0x22, 0x1d },
1903 { 0x17, 0x50 }, /* undocumented */
1904 { 0x37, 0x00 }, /* undocumented */
1905 { 0x40, 0xff }, /* I2C timeout counter */
1906 { 0x46, 0x00 }, /* I2C clock prescaler */
1907 { 0x59, 0x04 }, /* new from windrv 090403 */
1908 { 0xff, 0x00 }, /* undocumented */
1909 /* windows reads 0x55 at this point, why? */
1910 };
1911
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001912 static const struct ov_regvals mode_init_519[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001913 { 0x5d, 0x03 }, /* Turn off suspend mode */
1914 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
1915 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1916 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1917 { 0xa3, 0x18 },
1918 { 0xa4, 0x04 },
1919 { 0xa5, 0x28 },
1920 { 0x37, 0x00 }, /* SetUsbInit */
1921 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1922 /* Enable both fields, YUV Input, disable defect comp (why?) */
1923 { 0x22, 0x1d },
1924 { 0x17, 0x50 }, /* undocumented */
1925 { 0x37, 0x00 }, /* undocumented */
1926 { 0x40, 0xff }, /* I2C timeout counter */
1927 { 0x46, 0x00 }, /* I2C clock prescaler */
1928 { 0x59, 0x04 }, /* new from windrv 090403 */
1929 { 0xff, 0x00 }, /* undocumented */
1930 /* windows reads 0x55 at this point, why? */
1931 };
1932
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001933 /******** Set the mode ********/
1934 if (sd->sensor != SEN_OV7670) {
1935 if (write_regvals(sd, mode_init_519,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001936 ARRAY_SIZE(mode_init_519)))
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001937 return -EIO;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001938 if (sd->sensor == SEN_OV7640) {
1939 /* Select 8-bit input mode */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001940 reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001941 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001942 } else {
1943 if (write_regvals(sd, mode_init_519_ov7670,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001944 ARRAY_SIZE(mode_init_519_ov7670)))
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001945 return -EIO;
1946 }
1947
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001948 reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);
1949 reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);
1950 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
1951 reg_w(sd, OV519_R13_X_OFFSETH, 0x00);
1952 reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);
1953 reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);
1954 reg_w(sd, OV519_R16_DIVIDER, 0x00);
1955 reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001956 reg_w(sd, 0x26, 0x00); /* Undocumented */
1957
1958 /******** Set the framerate ********/
1959 if (frame_rate > 0)
1960 sd->frame_rate = frame_rate;
1961
1962/* FIXME: These are only valid at the max resolution. */
1963 sd->clockdiv = 0;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001964 switch (sd->sensor) {
1965 case SEN_OV7640:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001966 switch (sd->frame_rate) {
Jean-Francois Moine53e74512008-11-08 06:10:19 -03001967 default:
1968/* case 30: */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001969 reg_w(sd, 0xa4, 0x0c);
1970 reg_w(sd, 0x23, 0xff);
1971 break;
1972 case 25:
1973 reg_w(sd, 0xa4, 0x0c);
1974 reg_w(sd, 0x23, 0x1f);
1975 break;
1976 case 20:
1977 reg_w(sd, 0xa4, 0x0c);
1978 reg_w(sd, 0x23, 0x1b);
1979 break;
Jean-Francois Moine53e74512008-11-08 06:10:19 -03001980 case 15:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001981 reg_w(sd, 0xa4, 0x04);
1982 reg_w(sd, 0x23, 0xff);
1983 sd->clockdiv = 1;
1984 break;
1985 case 10:
1986 reg_w(sd, 0xa4, 0x04);
1987 reg_w(sd, 0x23, 0x1f);
1988 sd->clockdiv = 1;
1989 break;
1990 case 5:
1991 reg_w(sd, 0xa4, 0x04);
1992 reg_w(sd, 0x23, 0x1b);
1993 sd->clockdiv = 1;
1994 break;
1995 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03001996 break;
1997 case SEN_OV8610:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001998 switch (sd->frame_rate) {
1999 default: /* 15 fps */
2000/* case 15: */
2001 reg_w(sd, 0xa4, 0x06);
2002 reg_w(sd, 0x23, 0xff);
2003 break;
2004 case 10:
2005 reg_w(sd, 0xa4, 0x06);
2006 reg_w(sd, 0x23, 0x1f);
2007 break;
2008 case 5:
2009 reg_w(sd, 0xa4, 0x06);
2010 reg_w(sd, 0x23, 0x1b);
2011 break;
2012 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002013 break;
2014 case SEN_OV7670: /* guesses, based on 7640 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002015 PDEBUG(D_STREAM, "Setting framerate to %d fps",
2016 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002017 reg_w(sd, 0xa4, 0x10);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002018 switch (sd->frame_rate) {
2019 case 30:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002020 reg_w(sd, 0x23, 0xff);
2021 break;
2022 case 20:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002023 reg_w(sd, 0x23, 0x1b);
2024 break;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002025 default:
2026/* case 15: */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002027 reg_w(sd, 0x23, 0xff);
2028 sd->clockdiv = 1;
2029 break;
2030 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002031 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002032 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002033 return 0;
2034}
2035
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002036static int mode_init_ov_sensor_regs(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002037{
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002038 struct gspca_dev *gspca_dev;
2039 int qvga;
2040
2041 gspca_dev = &sd->gspca_dev;
2042 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002043
2044 /******** Mode (VGA/QVGA) and sensor specific regs ********/
2045 switch (sd->sensor) {
2046 case SEN_OV8610:
2047 /* For OV8610 qvga means qsvga */
2048 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
2049 break;
2050 case SEN_OV7610:
2051 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2052 break;
2053 case SEN_OV7620:
2054/* i2c_w(sd, 0x2b, 0x00); */
2055 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2056 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
2057 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
2058 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
2059 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
2060 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
2061 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
2062 break;
2063 case SEN_OV76BE:
2064/* i2c_w(sd, 0x2b, 0x00); */
2065 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2066 break;
2067 case SEN_OV7640:
2068/* i2c_w(sd, 0x2b, 0x00); */
2069 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2070 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
2071/* i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a); */
2072/* i2c_w(sd, 0x25, qvga ? 0x30 : 0x60); */
2073/* i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40); */
2074/* i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0); */
2075/* i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20); */
2076 break;
2077 case SEN_OV7670:
2078 /* set COM7_FMT_VGA or COM7_FMT_QVGA
2079 * do we need to set anything else?
2080 * HSTART etc are set in set_ov_sensor_window itself */
2081 i2c_w_mask(sd, OV7670_REG_COM7,
2082 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
2083 OV7670_COM7_FMT_MASK);
2084 break;
2085 case SEN_OV6620:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002086 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03002087 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002088 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
2089 break;
2090 default:
2091 return -EINVAL;
2092 }
2093
2094 /******** Palette-specific regs ********/
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002095 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
2096 /* not valid on the OV6620/OV7620/6630? */
2097 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
2098 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002099
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002100 /* The OV518 needs special treatment. Although both the OV518
2101 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
2102 * bus is actually used. The UV bus is tied to ground.
2103 * Therefore, the OV6630 needs to be in 8-bit multiplexed
2104 * output mode */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002105
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002106 /* OV7640 is 8-bit only */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002107
Hans de Goede7d971372009-06-14 05:28:17 -03002108 if (sd->sensor != SEN_OV6630 && sd->sensor != SEN_OV66308AF &&
2109 sd->sensor != SEN_OV7640)
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002110 i2c_w_mask(sd, 0x13, 0x00, 0x20);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002111
2112 /******** Clock programming ********/
2113 /* The OV6620 needs special handling. This prevents the
2114 * severe banding that normally occurs */
2115 if (sd->sensor == SEN_OV6620) {
2116
2117 /* Clock down */
2118 i2c_w(sd, 0x2a, 0x04);
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002119 i2c_w(sd, 0x11, sd->clockdiv);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002120 i2c_w(sd, 0x2a, 0x84);
2121 /* This next setting is critical. It seems to improve
2122 * the gain or the contrast. The "reserved" bits seem
2123 * to have some effect in this case. */
2124 i2c_w(sd, 0x2d, 0x85);
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002125 } else {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002126 i2c_w(sd, 0x11, sd->clockdiv);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002127 }
2128
2129 /******** Special Features ********/
2130/* no evidence this is possible with OV7670, either */
2131 /* Test Pattern */
2132 if (sd->sensor != SEN_OV7640 && sd->sensor != SEN_OV7670)
2133 i2c_w_mask(sd, 0x12, 0x00, 0x02);
2134
2135 /* Enable auto white balance */
2136 if (sd->sensor == SEN_OV7670)
2137 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
2138 OV7670_COM8_AWB);
2139 else
2140 i2c_w_mask(sd, 0x12, 0x04, 0x04);
2141
2142 /* This will go away as soon as ov51x_mode_init_sensor_regs() */
2143 /* is fully tested. */
2144 /* 7620/6620/6630? don't have register 0x35, so play it safe */
2145 if (sd->sensor == SEN_OV7610 || sd->sensor == SEN_OV76BE) {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002146 if (!qvga)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002147 i2c_w(sd, 0x35, 0x9e);
2148 else
2149 i2c_w(sd, 0x35, 0x1e);
2150 }
2151 return 0;
2152}
2153
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002154static void sethvflip(struct sd *sd)
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03002155{
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002156 if (sd->sensor != SEN_OV7670)
2157 return;
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03002158 if (sd->gspca_dev.streaming)
2159 ov51x_stop(sd);
2160 i2c_w_mask(sd, OV7670_REG_MVFP,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002161 OV7670_MVFP_MIRROR * sd->hflip
2162 | OV7670_MVFP_VFLIP * sd->vflip,
2163 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03002164 if (sd->gspca_dev.streaming)
2165 ov51x_restart(sd);
2166}
2167
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002168static int set_ov_sensor_window(struct sd *sd)
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03002169{
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002170 struct gspca_dev *gspca_dev;
2171 int qvga;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002172 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
2173 int ret, hstart, hstop, vstop, vstart;
2174 __u8 v;
2175
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002176 gspca_dev = &sd->gspca_dev;
2177 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;
2178
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002179 /* The different sensor ICs handle setting up of window differently.
2180 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
2181 switch (sd->sensor) {
2182 case SEN_OV8610:
2183 hwsbase = 0x1e;
2184 hwebase = 0x1e;
2185 vwsbase = 0x02;
2186 vwebase = 0x02;
2187 break;
2188 case SEN_OV7610:
2189 case SEN_OV76BE:
2190 hwsbase = 0x38;
2191 hwebase = 0x3a;
2192 vwsbase = vwebase = 0x05;
2193 break;
2194 case SEN_OV6620:
2195 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03002196 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002197 hwsbase = 0x38;
2198 hwebase = 0x3a;
2199 vwsbase = 0x05;
2200 vwebase = 0x06;
Hans de Goede7d971372009-06-14 05:28:17 -03002201 if (sd->sensor == SEN_OV66308AF && qvga)
Hans de Goede49809d62009-06-07 12:10:39 -03002202 /* HDG: this fixes U and V getting swapped */
Hans de Goede7d971372009-06-14 05:28:17 -03002203 hwsbase++;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002204 break;
2205 case SEN_OV7620:
2206 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
2207 hwebase = 0x2f;
2208 vwsbase = vwebase = 0x05;
2209 break;
2210 case SEN_OV7640:
2211 hwsbase = 0x1a;
2212 hwebase = 0x1a;
2213 vwsbase = vwebase = 0x03;
2214 break;
2215 case SEN_OV7670:
2216 /*handling of OV7670 hardware sensor start and stop values
2217 * is very odd, compared to the other OV sensors */
2218 vwsbase = vwebase = hwebase = hwsbase = 0x00;
2219 break;
2220 default:
2221 return -EINVAL;
2222 }
2223
2224 switch (sd->sensor) {
2225 case SEN_OV6620:
2226 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03002227 case SEN_OV66308AF:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002228 if (qvga) { /* QCIF */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002229 hwscale = 0;
2230 vwscale = 0;
2231 } else { /* CIF */
2232 hwscale = 1;
2233 vwscale = 1; /* The datasheet says 0;
2234 * it's wrong */
2235 }
2236 break;
2237 case SEN_OV8610:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002238 if (qvga) { /* QSVGA */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002239 hwscale = 1;
2240 vwscale = 1;
2241 } else { /* SVGA */
2242 hwscale = 2;
2243 vwscale = 2;
2244 }
2245 break;
2246 default: /* SEN_OV7xx0 */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002247 if (qvga) { /* QVGA */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002248 hwscale = 1;
2249 vwscale = 0;
2250 } else { /* VGA */
2251 hwscale = 2;
2252 vwscale = 1;
2253 }
2254 }
2255
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002256 ret = mode_init_ov_sensor_regs(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002257 if (ret < 0)
2258 return ret;
2259
2260 if (sd->sensor == SEN_OV8610) {
2261 i2c_w_mask(sd, 0x2d, 0x05, 0x40);
2262 /* old 0x95, new 0x05 from windrv 090403 */
2263 /* bits 5-7: reserved */
2264 i2c_w_mask(sd, 0x28, 0x20, 0x20);
2265 /* bit 5: progressive mode on */
2266 }
2267
2268 /* The below is wrong for OV7670s because their window registers
2269 * only store the high bits in 0x17 to 0x1a */
2270
2271 /* SRH Use sd->max values instead of requested win values */
2272 /* SCS Since we're sticking with only the max hardware widths
2273 * for a given mode */
2274 /* I can hard code this for OV7670s */
2275 /* Yes, these numbers do look odd, but they're tested and work! */
2276 if (sd->sensor == SEN_OV7670) {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002277 if (qvga) { /* QVGA from ov7670.c by
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002278 * Jonathan Corbet */
2279 hstart = 164;
2280 hstop = 20;
2281 vstart = 14;
2282 vstop = 494;
2283 } else { /* VGA */
2284 hstart = 158;
2285 hstop = 14;
2286 vstart = 10;
2287 vstop = 490;
2288 }
2289 /* OV7670 hardware window registers are split across
2290 * multiple locations */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002291 i2c_w(sd, OV7670_REG_HSTART, hstart >> 3);
2292 i2c_w(sd, OV7670_REG_HSTOP, hstop >> 3);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002293 v = i2c_r(sd, OV7670_REG_HREF);
2294 v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x07);
2295 msleep(10); /* need to sleep between read and write to
2296 * same reg! */
2297 i2c_w(sd, OV7670_REG_HREF, v);
2298
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002299 i2c_w(sd, OV7670_REG_VSTART, vstart >> 2);
2300 i2c_w(sd, OV7670_REG_VSTOP, vstop >> 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002301 v = i2c_r(sd, OV7670_REG_VREF);
2302 v = (v & 0xc0) | ((vstop & 0x3) << 2) | (vstart & 0x03);
2303 msleep(10); /* need to sleep between read and write to
2304 * same reg! */
2305 i2c_w(sd, OV7670_REG_VREF, v);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002306 } else {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002307 i2c_w(sd, 0x17, hwsbase);
2308 i2c_w(sd, 0x18, hwebase + (sd->gspca_dev.width >> hwscale));
2309 i2c_w(sd, 0x19, vwsbase);
2310 i2c_w(sd, 0x1a, vwebase + (sd->gspca_dev.height >> vwscale));
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002311 }
2312 return 0;
2313}
2314
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002315/* -- start the camera -- */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03002316static int sd_start(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002317{
2318 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede49809d62009-06-07 12:10:39 -03002319 int ret = 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002320
Hans de Goede49809d62009-06-07 12:10:39 -03002321 switch (sd->bridge) {
2322 case BRIDGE_OV518:
2323 case BRIDGE_OV518PLUS:
2324 ret = ov518_mode_init_regs(sd);
2325 break;
2326 case BRIDGE_OV519:
2327 ret = ov519_mode_init_regs(sd);
2328 break;
2329 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002330 if (ret < 0)
2331 goto out;
Hans de Goede49809d62009-06-07 12:10:39 -03002332
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002333 ret = set_ov_sensor_window(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002334 if (ret < 0)
2335 goto out;
2336
Hans de Goede49809d62009-06-07 12:10:39 -03002337 setcontrast(gspca_dev);
2338 setbrightness(gspca_dev);
2339 setcolors(gspca_dev);
Hans de Goede02ab18b2009-06-14 04:32:04 -03002340 sethvflip(sd);
2341 setautobrightness(sd);
2342 setfreq(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03002343
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002344 ret = ov51x_restart(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002345 if (ret < 0)
2346 goto out;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002347 ov51x_led_control(sd, 1);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03002348 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002349out:
2350 PDEBUG(D_ERR, "camera start error:%d", ret);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03002351 return ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002352}
2353
2354static void sd_stopN(struct gspca_dev *gspca_dev)
2355{
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002356 struct sd *sd = (struct sd *) gspca_dev;
2357
2358 ov51x_stop(sd);
2359 ov51x_led_control(sd, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002360}
2361
Hans de Goede49809d62009-06-07 12:10:39 -03002362static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
2363 struct gspca_frame *frame, /* target */
2364 __u8 *data, /* isoc packet */
2365 int len) /* iso packet length */
2366{
2367 PDEBUG(D_STREAM, "ov518_pkt_scan: %d bytes", len);
2368
2369 if (len & 7) {
2370 len--;
2371 PDEBUG(D_STREAM, "packet number: %d\n", (int)data[len]);
2372 }
2373
2374 /* A false positive here is likely, until OVT gives me
2375 * the definitive SOF/EOF format */
2376 if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
2377 gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
2378 gspca_frame_add(gspca_dev, FIRST_PACKET, frame, data, 0);
2379 }
2380
2381 /* intermediate packet */
2382 gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
2383}
2384
2385static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002386 struct gspca_frame *frame, /* target */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002387 __u8 *data, /* isoc packet */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002388 int len) /* iso packet length */
2389{
2390 /* Header of ov519 is 16 bytes:
2391 * Byte Value Description
2392 * 0 0xff magic
2393 * 1 0xff magic
2394 * 2 0xff magic
2395 * 3 0xXX 0x50 = SOF, 0x51 = EOF
2396 * 9 0xXX 0x01 initial frame without data,
2397 * 0x00 standard frame with image
2398 * 14 Lo in EOF: length of image data / 8
2399 * 15 Hi
2400 */
2401
2402 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
2403 switch (data[3]) {
2404 case 0x50: /* start of frame */
2405#define HDRSZ 16
2406 data += HDRSZ;
2407 len -= HDRSZ;
2408#undef HDRSZ
2409 if (data[0] == 0xff || data[1] == 0xd8)
2410 gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
2411 data, len);
2412 else
2413 gspca_dev->last_packet_type = DISCARD_PACKET;
2414 return;
2415 case 0x51: /* end of frame */
2416 if (data[9] != 0)
2417 gspca_dev->last_packet_type = DISCARD_PACKET;
2418 gspca_frame_add(gspca_dev, LAST_PACKET, frame,
2419 data, 0);
2420 return;
2421 }
2422 }
2423
2424 /* intermediate packet */
2425 gspca_frame_add(gspca_dev, INTER_PACKET, frame,
2426 data, len);
2427}
2428
Hans de Goede49809d62009-06-07 12:10:39 -03002429static void sd_pkt_scan(struct gspca_dev *gspca_dev,
2430 struct gspca_frame *frame, /* target */
2431 __u8 *data, /* isoc packet */
2432 int len) /* iso packet length */
2433{
2434 struct sd *sd = (struct sd *) gspca_dev;
2435
2436 switch (sd->bridge) {
2437 case BRIDGE_OV511:
2438 case BRIDGE_OV511PLUS:
2439 break;
2440 case BRIDGE_OV518:
2441 case BRIDGE_OV518PLUS:
2442 ov518_pkt_scan(gspca_dev, frame, data, len);
2443 break;
2444 case BRIDGE_OV519:
2445 ov519_pkt_scan(gspca_dev, frame, data, len);
2446 break;
2447 }
2448}
2449
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002450/* -- management routines -- */
2451
2452static void setbrightness(struct gspca_dev *gspca_dev)
2453{
2454 struct sd *sd = (struct sd *) gspca_dev;
2455 int val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002456
2457 val = sd->brightness;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002458 switch (sd->sensor) {
2459 case SEN_OV8610:
2460 case SEN_OV7610:
2461 case SEN_OV76BE:
2462 case SEN_OV6620:
2463 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03002464 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002465 case SEN_OV7640:
2466 i2c_w(sd, OV7610_REG_BRT, val);
2467 break;
2468 case SEN_OV7620:
2469 /* 7620 doesn't like manual changes when in auto mode */
Hans de Goede02ab18b2009-06-14 04:32:04 -03002470 if (!sd->autobrightness)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002471 i2c_w(sd, OV7610_REG_BRT, val);
2472 break;
2473 case SEN_OV7670:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002474/*win trace
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002475 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
2476 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
2477 break;
2478 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002479}
2480
2481static void setcontrast(struct gspca_dev *gspca_dev)
2482{
2483 struct sd *sd = (struct sd *) gspca_dev;
2484 int val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002485
2486 val = sd->contrast;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002487 switch (sd->sensor) {
2488 case SEN_OV7610:
2489 case SEN_OV6620:
2490 i2c_w(sd, OV7610_REG_CNT, val);
2491 break;
2492 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03002493 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002494 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
Hans de Goede49809d62009-06-07 12:10:39 -03002495 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002496 case SEN_OV8610: {
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002497 static const __u8 ctab[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002498 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
2499 };
2500
2501 /* Use Y gamma control instead. Bit 0 enables it. */
2502 i2c_w(sd, 0x64, ctab[val >> 5]);
2503 break;
2504 }
2505 case SEN_OV7620: {
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002506 static const __u8 ctab[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002507 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
2508 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
2509 };
2510
2511 /* Use Y gamma control instead. Bit 0 enables it. */
2512 i2c_w(sd, 0x64, ctab[val >> 4]);
2513 break;
2514 }
2515 case SEN_OV7640:
2516 /* Use gain control instead. */
2517 i2c_w(sd, OV7610_REG_GAIN, val >> 2);
2518 break;
2519 case SEN_OV7670:
2520 /* check that this isn't just the same as ov7610 */
2521 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
2522 break;
2523 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002524}
2525
2526static void setcolors(struct gspca_dev *gspca_dev)
2527{
2528 struct sd *sd = (struct sd *) gspca_dev;
2529 int val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002530
2531 val = sd->colors;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002532 switch (sd->sensor) {
2533 case SEN_OV8610:
2534 case SEN_OV7610:
2535 case SEN_OV76BE:
2536 case SEN_OV6620:
2537 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03002538 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002539 i2c_w(sd, OV7610_REG_SAT, val);
2540 break;
2541 case SEN_OV7620:
2542 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
2543/* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
2544 if (rc < 0)
2545 goto out; */
2546 i2c_w(sd, OV7610_REG_SAT, val);
2547 break;
2548 case SEN_OV7640:
2549 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
2550 break;
2551 case SEN_OV7670:
2552 /* supported later once I work out how to do it
2553 * transparently fail now! */
2554 /* set REG_COM13 values for UV sat auto mode */
2555 break;
2556 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002557}
2558
Hans de Goede02ab18b2009-06-14 04:32:04 -03002559static void setautobrightness(struct sd *sd)
2560{
2561 if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7670)
2562 return;
2563
2564 i2c_w_mask(sd, 0x2d, sd->autobrightness ? 0x10 : 0x00, 0x10);
2565}
2566
2567static void setfreq(struct sd *sd)
2568{
2569 if (sd->sensor == SEN_OV7670) {
2570 switch (sd->freq) {
2571 case 0: /* Banding filter disabled */
2572 i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_BFILT);
2573 break;
2574 case 1: /* 50 hz */
2575 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2576 OV7670_COM8_BFILT);
2577 i2c_w_mask(sd, OV7670_REG_COM11, 0x08, 0x18);
2578 break;
2579 case 2: /* 60 hz */
2580 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2581 OV7670_COM8_BFILT);
2582 i2c_w_mask(sd, OV7670_REG_COM11, 0x00, 0x18);
2583 break;
2584 case 3: /* Auto hz */
2585 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
2586 OV7670_COM8_BFILT);
2587 i2c_w_mask(sd, OV7670_REG_COM11, OV7670_COM11_HZAUTO,
2588 0x18);
2589 break;
2590 }
2591 } else {
2592 switch (sd->freq) {
2593 case 0: /* Banding filter disabled */
2594 i2c_w_mask(sd, 0x2d, 0x00, 0x04);
2595 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
2596 break;
2597 case 1: /* 50 hz (filter on and framerate adj) */
2598 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
2599 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
2600 /* 20 fps -> 16.667 fps */
2601 if (sd->sensor == SEN_OV6620 ||
Hans de Goede7d971372009-06-14 05:28:17 -03002602 sd->sensor == SEN_OV6630 ||
2603 sd->sensor == SEN_OV66308AF)
Hans de Goede02ab18b2009-06-14 04:32:04 -03002604 i2c_w(sd, 0x2b, 0x5e);
2605 else
2606 i2c_w(sd, 0x2b, 0xac);
2607 break;
2608 case 2: /* 60 hz (filter on, ...) */
2609 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
2610 if (sd->sensor == SEN_OV6620 ||
Hans de Goede7d971372009-06-14 05:28:17 -03002611 sd->sensor == SEN_OV6630 ||
2612 sd->sensor == SEN_OV66308AF) {
Hans de Goede02ab18b2009-06-14 04:32:04 -03002613 /* 20 fps -> 15 fps */
2614 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
2615 i2c_w(sd, 0x2b, 0xa8);
2616 } else {
2617 /* no framerate adj. */
2618 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
2619 }
2620 break;
2621 }
2622 }
2623}
2624
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002625static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
2626{
2627 struct sd *sd = (struct sd *) gspca_dev;
2628
2629 sd->brightness = val;
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002630 if (gspca_dev->streaming)
2631 setbrightness(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002632 return 0;
2633}
2634
2635static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
2636{
2637 struct sd *sd = (struct sd *) gspca_dev;
2638
2639 *val = sd->brightness;
2640 return 0;
2641}
2642
2643static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
2644{
2645 struct sd *sd = (struct sd *) gspca_dev;
2646
2647 sd->contrast = val;
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002648 if (gspca_dev->streaming)
2649 setcontrast(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002650 return 0;
2651}
2652
2653static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
2654{
2655 struct sd *sd = (struct sd *) gspca_dev;
2656
2657 *val = sd->contrast;
2658 return 0;
2659}
2660
2661static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
2662{
2663 struct sd *sd = (struct sd *) gspca_dev;
2664
2665 sd->colors = val;
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002666 if (gspca_dev->streaming)
2667 setcolors(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002668 return 0;
2669}
2670
2671static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
2672{
2673 struct sd *sd = (struct sd *) gspca_dev;
2674
2675 *val = sd->colors;
2676 return 0;
2677}
2678
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03002679static int sd_sethflip(struct gspca_dev *gspca_dev, __s32 val)
2680{
2681 struct sd *sd = (struct sd *) gspca_dev;
2682
2683 sd->hflip = val;
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002684 if (gspca_dev->streaming)
2685 sethvflip(sd);
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03002686 return 0;
2687}
2688
2689static int sd_gethflip(struct gspca_dev *gspca_dev, __s32 *val)
2690{
2691 struct sd *sd = (struct sd *) gspca_dev;
2692
2693 *val = sd->hflip;
2694 return 0;
2695}
2696
2697static int sd_setvflip(struct gspca_dev *gspca_dev, __s32 val)
2698{
2699 struct sd *sd = (struct sd *) gspca_dev;
2700
2701 sd->vflip = val;
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002702 if (gspca_dev->streaming)
2703 sethvflip(sd);
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03002704 return 0;
2705}
2706
2707static int sd_getvflip(struct gspca_dev *gspca_dev, __s32 *val)
2708{
2709 struct sd *sd = (struct sd *) gspca_dev;
2710
2711 *val = sd->vflip;
2712 return 0;
2713}
2714
Hans de Goede02ab18b2009-06-14 04:32:04 -03002715static int sd_setautobrightness(struct gspca_dev *gspca_dev, __s32 val)
2716{
2717 struct sd *sd = (struct sd *) gspca_dev;
2718
2719 sd->autobrightness = val;
2720 if (gspca_dev->streaming)
2721 setautobrightness(sd);
2722 return 0;
2723}
2724
2725static int sd_getautobrightness(struct gspca_dev *gspca_dev, __s32 *val)
2726{
2727 struct sd *sd = (struct sd *) gspca_dev;
2728
2729 *val = sd->autobrightness;
2730 return 0;
2731}
2732
2733static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
2734{
2735 struct sd *sd = (struct sd *) gspca_dev;
2736
2737 sd->freq = val;
2738 if (gspca_dev->streaming)
2739 setfreq(sd);
2740 return 0;
2741}
2742
2743static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
2744{
2745 struct sd *sd = (struct sd *) gspca_dev;
2746
2747 *val = sd->freq;
2748 return 0;
2749}
2750
2751static int sd_querymenu(struct gspca_dev *gspca_dev,
2752 struct v4l2_querymenu *menu)
2753{
2754 struct sd *sd = (struct sd *) gspca_dev;
2755
2756 switch (menu->id) {
2757 case V4L2_CID_POWER_LINE_FREQUENCY:
2758 switch (menu->index) {
2759 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
2760 strcpy((char *) menu->name, "NoFliker");
2761 return 0;
2762 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
2763 strcpy((char *) menu->name, "50 Hz");
2764 return 0;
2765 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
2766 strcpy((char *) menu->name, "60 Hz");
2767 return 0;
2768 case 3:
2769 if (sd->sensor != SEN_OV7670)
2770 return -EINVAL;
2771
2772 strcpy((char *) menu->name, "Automatic");
2773 return 0;
2774 }
2775 break;
2776 }
2777 return -EINVAL;
2778}
2779
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002780/* sub-driver description */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002781static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002782 .name = MODULE_NAME,
2783 .ctrls = sd_ctrls,
2784 .nctrls = ARRAY_SIZE(sd_ctrls),
2785 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03002786 .init = sd_init,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002787 .start = sd_start,
2788 .stopN = sd_stopN,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002789 .pkt_scan = sd_pkt_scan,
Hans de Goede02ab18b2009-06-14 04:32:04 -03002790 .querymenu = sd_querymenu,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002791};
2792
2793/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002794static const __devinitdata struct usb_device_id device_table[] = {
Hans de Goede49809d62009-06-07 12:10:39 -03002795 {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
2796 {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
2797 {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
2798 {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
2799 {USB_DEVICE(0x041e, 0x4064), .driver_info = BRIDGE_OV519 },
2800 {USB_DEVICE(0x041e, 0x4068), .driver_info = BRIDGE_OV519 },
2801 {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
2802 {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
2803 {USB_DEVICE(0x054c, 0x0155), .driver_info = BRIDGE_OV519 },
2804 {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
2805 {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
2806 {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
2807 {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
2808 {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
2809 {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002810 {}
2811};
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002812
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002813MODULE_DEVICE_TABLE(usb, device_table);
2814
2815/* -- device connect -- */
2816static int sd_probe(struct usb_interface *intf,
2817 const struct usb_device_id *id)
2818{
2819 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
2820 THIS_MODULE);
2821}
2822
2823static struct usb_driver sd_driver = {
2824 .name = MODULE_NAME,
2825 .id_table = device_table,
2826 .probe = sd_probe,
2827 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03002828#ifdef CONFIG_PM
2829 .suspend = gspca_suspend,
2830 .resume = gspca_resume,
2831#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002832};
2833
2834/* -- module insert / remove -- */
2835static int __init sd_mod_init(void)
2836{
Alexey Klimovf69e9522009-01-01 13:02:07 -03002837 int ret;
2838 ret = usb_register(&sd_driver);
2839 if (ret < 0)
Alexey Klimove6b14842009-01-01 13:04:58 -03002840 return ret;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -03002841 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002842 return 0;
2843}
2844static void __exit sd_mod_exit(void)
2845{
2846 usb_deregister(&sd_driver);
2847 PDEBUG(D_PROBE, "deregistered");
2848}
2849
2850module_init(sd_mod_init);
2851module_exit(sd_mod_exit);
2852
2853module_param(frame_rate, int, 0644);
2854MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");