blob: 2417a7e70a42cb51f67d2eb84113d3b7731d6ab2 [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)
Hans de Goedeb46aaa02009-10-12 10:07:57 -03005 * Copyright (C) 2009 Hans de Goede <hdegoede@redhat.com>
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03006 *
Romain Beauxis2961e872008-12-05 06:25:26 -03007 * This module is adapted from the ov51x-jpeg package, which itself
8 * was adapted from the ov511 driver.
9 *
10 * Original copyright for the ov511 driver is:
11 *
Hans de Goedeb46aaa02009-10-12 10:07:57 -030012 * Copyright (c) 1999-2006 Mark W. McClelland
Romain Beauxis2961e872008-12-05 06:25:26 -030013 * Support for OV519, OV8610 Copyright (c) 2003 Joerg Heckenbach
Hans de Goedeb46aaa02009-10-12 10:07:57 -030014 * Many improvements by Bret Wallach <bwallac1@san.rr.com>
15 * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
16 * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
17 * Changes by Claudio Matsuoka <claudio@conectiva.com>
Romain Beauxis2961e872008-12-05 06:25:26 -030018 *
19 * ov51x-jpeg original copyright is:
20 *
21 * Copyright (c) 2004-2007 Romain Beauxis <toots@rastageeks.org>
22 * Support for OV7670 sensors was contributed by Sam Skipsey <aoanla@yahoo.com>
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030023 *
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * any later version.
28 *
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
33 *
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 *
38 */
39#define MODULE_NAME "ov519"
40
Hans de Goede417a4d22010-02-19 07:37:08 -030041#include <linux/input.h>
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030042#include "gspca.h"
43
Jean-François Moine9a731a32010-06-04 05:26:42 -030044/* The jpeg_hdr is used by w996Xcf only */
45/* The CONEX_CAM define for jpeg.h needs renaming, now its used here too */
46#define CONEX_CAM
47#include "jpeg.h"
48
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030049MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
50MODULE_DESCRIPTION("OV519 USB Camera Driver");
51MODULE_LICENSE("GPL");
52
53/* global parameters */
54static int frame_rate;
55
56/* Number of times to retry a failed I2C transaction. Increase this if you
57 * are getting "Failed to read sensor ID..." */
58static int i2c_detect_tries = 10;
59
Jean-François Moine62833ac2010-10-02 04:27:02 -030060/* controls */
61enum e_ctrl {
62 BRIGHTNESS,
63 CONTRAST,
64 COLORS,
65 HFLIP,
66 VFLIP,
67 AUTOBRIGHT,
68 FREQ,
69 NCTRL /* number of controls */
70};
71
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030072/* ov519 device descriptor */
73struct sd {
74 struct gspca_dev gspca_dev; /* !! must be the first item */
75
Jean-François Moine62833ac2010-10-02 04:27:02 -030076 struct gspca_ctrl ctrls[NCTRL];
77
Hans de Goede92918a52009-06-14 06:21:35 -030078 __u8 packet_nr;
79
Hans de Goede49809d62009-06-07 12:10:39 -030080 char bridge;
81#define BRIDGE_OV511 0
82#define BRIDGE_OV511PLUS 1
83#define BRIDGE_OV518 2
84#define BRIDGE_OV518PLUS 3
85#define BRIDGE_OV519 4
Hans de Goede635118d2009-10-11 09:49:03 -030086#define BRIDGE_OVFX2 5
Hans de Goedea511ba92009-10-16 07:13:07 -030087#define BRIDGE_W9968CF 6
Hans de Goede9e4d8252009-06-14 06:25:06 -030088#define BRIDGE_MASK 7
89
90 char invert_led;
91#define BRIDGE_INVERT_LED 8
Hans de Goede49809d62009-06-07 12:10:39 -030092
Hans de Goede417a4d22010-02-19 07:37:08 -030093 char snapshot_pressed;
94 char snapshot_needs_reset;
95
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030096 /* Determined by sensor type */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -030097 __u8 sif;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030098
Hans de Goede79b35902009-10-19 06:08:01 -030099 __u8 quality;
100#define QUALITY_MIN 50
101#define QUALITY_MAX 70
102#define QUALITY_DEF 50
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300103
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300104 __u8 stopped; /* Streaming is temporarily paused */
Hans de Goeded6b6d7a2010-05-08 08:55:42 -0300105 __u8 first_frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300106
Hans de Goede1876bb92009-06-14 06:45:50 -0300107 __u8 frame_rate; /* current Framerate */
108 __u8 clockdiv; /* clockdiv override */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300109
110 char sensor; /* Type of image sensor chip (SEN_*) */
111#define SEN_UNKNOWN 0
Hans de Goede635118d2009-10-11 09:49:03 -0300112#define SEN_OV2610 1
113#define SEN_OV3610 2
114#define SEN_OV6620 3
115#define SEN_OV6630 4
116#define SEN_OV66308AF 5
117#define SEN_OV7610 6
118#define SEN_OV7620 7
Hans de Goede859cc472010-01-07 15:42:35 -0300119#define SEN_OV7620AE 8
120#define SEN_OV7640 9
Hans de Goede035d3a32010-01-09 08:14:43 -0300121#define SEN_OV7648 10
122#define SEN_OV7670 11
123#define SEN_OV76BE 12
124#define SEN_OV8610 13
Hans de Goedea511ba92009-10-16 07:13:07 -0300125
126 u8 sensor_addr;
127 int sensor_width;
128 int sensor_height;
Hans de Goedefb1f9022009-10-16 07:42:53 -0300129 int sensor_reg_cache[256];
Hans de Goede79b35902009-10-19 06:08:01 -0300130
Jean-François Moine9a731a32010-06-04 05:26:42 -0300131 u8 jpeg_hdr[JPEG_HDR_SZ];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300132};
133
Hans de Goedea511ba92009-10-16 07:13:07 -0300134/* Note this is a bit of a hack, but the w9968cf driver needs the code for all
135 the ov sensors which is already present here. When we have the time we
136 really should move the sensor drivers to v4l2 sub drivers. */
137#include "w996Xcf.c"
138
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300139/* V4L2 controls supported by the driver */
Hans de Goede49809d62009-06-07 12:10:39 -0300140static void setbrightness(struct gspca_dev *gspca_dev);
141static void setcontrast(struct gspca_dev *gspca_dev);
142static void setcolors(struct gspca_dev *gspca_dev);
Jean-François Moine62833ac2010-10-02 04:27:02 -0300143static void sethvflip(struct gspca_dev *gspca_dev);
144static void setautobright(struct gspca_dev *gspca_dev);
145static void setfreq(struct gspca_dev *gspca_dev);
146static void setfreq_i(struct sd *sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300147
Hans de Goede02ab18b2009-06-14 04:32:04 -0300148static const struct ctrl sd_ctrls[] = {
Jean-François Moine62833ac2010-10-02 04:27:02 -0300149[BRIGHTNESS] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300150 {
151 .id = V4L2_CID_BRIGHTNESS,
152 .type = V4L2_CTRL_TYPE_INTEGER,
153 .name = "Brightness",
154 .minimum = 0,
155 .maximum = 255,
156 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300157 .default_value = 127,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300158 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300159 .set_control = setbrightness,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300160 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300161[CONTRAST] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300162 {
163 .id = V4L2_CID_CONTRAST,
164 .type = V4L2_CTRL_TYPE_INTEGER,
165 .name = "Contrast",
166 .minimum = 0,
167 .maximum = 255,
168 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300169 .default_value = 127,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300170 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300171 .set_control = setcontrast,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300172 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300173[COLORS] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300174 {
175 .id = V4L2_CID_SATURATION,
176 .type = V4L2_CTRL_TYPE_INTEGER,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300177 .name = "Color",
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300178 .minimum = 0,
179 .maximum = 255,
180 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300181 .default_value = 127,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300182 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300183 .set_control = setcolors,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300184 },
Hans de Goede02ab18b2009-06-14 04:32:04 -0300185/* The flip controls work with ov7670 only */
Jean-François Moine62833ac2010-10-02 04:27:02 -0300186[HFLIP] = {
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300187 {
188 .id = V4L2_CID_HFLIP,
189 .type = V4L2_CTRL_TYPE_BOOLEAN,
190 .name = "Mirror",
191 .minimum = 0,
192 .maximum = 1,
193 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300194 .default_value = 0,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300195 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300196 .set_control = sethvflip,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300197 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300198[VFLIP] = {
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300199 {
200 .id = V4L2_CID_VFLIP,
201 .type = V4L2_CTRL_TYPE_BOOLEAN,
202 .name = "Vflip",
203 .minimum = 0,
204 .maximum = 1,
205 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300206 .default_value = 0,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300207 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300208 .set_control = sethvflip,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300209 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300210[AUTOBRIGHT] = {
Hans de Goede02ab18b2009-06-14 04:32:04 -0300211 {
212 .id = V4L2_CID_AUTOBRIGHTNESS,
213 .type = V4L2_CTRL_TYPE_BOOLEAN,
214 .name = "Auto Brightness",
215 .minimum = 0,
216 .maximum = 1,
217 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300218 .default_value = 1,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300219 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300220 .set_control = setautobright,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300221 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300222[FREQ] = {
Hans de Goede02ab18b2009-06-14 04:32:04 -0300223 {
224 .id = V4L2_CID_POWER_LINE_FREQUENCY,
225 .type = V4L2_CTRL_TYPE_MENU,
226 .name = "Light frequency filter",
227 .minimum = 0,
228 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
229 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300230 .default_value = 0,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300231 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300232 .set_control = setfreq,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300233 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300234};
235
Hans de Goede49809d62009-06-07 12:10:39 -0300236static const struct v4l2_pix_format ov519_vga_mode[] = {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300237 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
238 .bytesperline = 320,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300239 .sizeimage = 320 * 240 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300240 .colorspace = V4L2_COLORSPACE_JPEG,
241 .priv = 1},
242 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
243 .bytesperline = 640,
244 .sizeimage = 640 * 480 * 3 / 8 + 590,
245 .colorspace = V4L2_COLORSPACE_JPEG,
246 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300247};
Hans de Goede49809d62009-06-07 12:10:39 -0300248static const struct v4l2_pix_format ov519_sif_mode[] = {
Hans de Goede124cc9c2009-06-14 05:48:00 -0300249 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
250 .bytesperline = 160,
251 .sizeimage = 160 * 120 * 3 / 8 + 590,
252 .colorspace = V4L2_COLORSPACE_JPEG,
253 .priv = 3},
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300254 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
255 .bytesperline = 176,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300256 .sizeimage = 176 * 144 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300257 .colorspace = V4L2_COLORSPACE_JPEG,
258 .priv = 1},
Hans de Goede124cc9c2009-06-14 05:48:00 -0300259 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
260 .bytesperline = 320,
261 .sizeimage = 320 * 240 * 3 / 8 + 590,
262 .colorspace = V4L2_COLORSPACE_JPEG,
263 .priv = 2},
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300264 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
265 .bytesperline = 352,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300266 .sizeimage = 352 * 288 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300267 .colorspace = V4L2_COLORSPACE_JPEG,
268 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300269};
270
Hans de Goedeb282d872009-06-14 19:10:40 -0300271/* Note some of the sizeimage values for the ov511 / ov518 may seem
272 larger then necessary, however they need to be this big as the ov511 /
273 ov518 always fills the entire isoc frame, using 0 padding bytes when
274 it doesn't have any data. So with low framerates the amount of data
275 transfered can become quite large (libv4l will remove all the 0 padding
276 in userspace). */
Hans de Goede49809d62009-06-07 12:10:39 -0300277static const struct v4l2_pix_format ov518_vga_mode[] = {
278 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
279 .bytesperline = 320,
Hans de Goedeb282d872009-06-14 19:10:40 -0300280 .sizeimage = 320 * 240 * 3,
Hans de Goede49809d62009-06-07 12:10:39 -0300281 .colorspace = V4L2_COLORSPACE_JPEG,
282 .priv = 1},
283 {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
284 .bytesperline = 640,
Hans de Goedeb282d872009-06-14 19:10:40 -0300285 .sizeimage = 640 * 480 * 2,
Hans de Goede49809d62009-06-07 12:10:39 -0300286 .colorspace = V4L2_COLORSPACE_JPEG,
287 .priv = 0},
288};
289static const struct v4l2_pix_format ov518_sif_mode[] = {
Hans de Goede124cc9c2009-06-14 05:48:00 -0300290 {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
291 .bytesperline = 160,
Hans de Goedeb282d872009-06-14 19:10:40 -0300292 .sizeimage = 70000,
Hans de Goede124cc9c2009-06-14 05:48:00 -0300293 .colorspace = V4L2_COLORSPACE_JPEG,
294 .priv = 3},
Hans de Goede49809d62009-06-07 12:10:39 -0300295 {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
296 .bytesperline = 176,
Hans de Goedeb282d872009-06-14 19:10:40 -0300297 .sizeimage = 70000,
Hans de Goede49809d62009-06-07 12:10:39 -0300298 .colorspace = V4L2_COLORSPACE_JPEG,
299 .priv = 1},
Hans de Goede124cc9c2009-06-14 05:48:00 -0300300 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
301 .bytesperline = 320,
Hans de Goedeb282d872009-06-14 19:10:40 -0300302 .sizeimage = 320 * 240 * 3,
Hans de Goede124cc9c2009-06-14 05:48:00 -0300303 .colorspace = V4L2_COLORSPACE_JPEG,
304 .priv = 2},
Hans de Goede49809d62009-06-07 12:10:39 -0300305 {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
306 .bytesperline = 352,
Hans de Goedeb282d872009-06-14 19:10:40 -0300307 .sizeimage = 352 * 288 * 3,
Hans de Goede49809d62009-06-07 12:10:39 -0300308 .colorspace = V4L2_COLORSPACE_JPEG,
309 .priv = 0},
310};
311
Hans de Goede1876bb92009-06-14 06:45:50 -0300312static const struct v4l2_pix_format ov511_vga_mode[] = {
313 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
314 .bytesperline = 320,
315 .sizeimage = 320 * 240 * 3,
316 .colorspace = V4L2_COLORSPACE_JPEG,
317 .priv = 1},
318 {640, 480, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
319 .bytesperline = 640,
320 .sizeimage = 640 * 480 * 2,
321 .colorspace = V4L2_COLORSPACE_JPEG,
322 .priv = 0},
323};
324static const struct v4l2_pix_format ov511_sif_mode[] = {
325 {160, 120, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
326 .bytesperline = 160,
Hans de Goedeb282d872009-06-14 19:10:40 -0300327 .sizeimage = 70000,
Hans de Goede1876bb92009-06-14 06:45:50 -0300328 .colorspace = V4L2_COLORSPACE_JPEG,
329 .priv = 3},
330 {176, 144, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
331 .bytesperline = 176,
Hans de Goedeb282d872009-06-14 19:10:40 -0300332 .sizeimage = 70000,
Hans de Goede1876bb92009-06-14 06:45:50 -0300333 .colorspace = V4L2_COLORSPACE_JPEG,
334 .priv = 1},
335 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
336 .bytesperline = 320,
337 .sizeimage = 320 * 240 * 3,
338 .colorspace = V4L2_COLORSPACE_JPEG,
339 .priv = 2},
340 {352, 288, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
341 .bytesperline = 352,
342 .sizeimage = 352 * 288 * 3,
343 .colorspace = V4L2_COLORSPACE_JPEG,
344 .priv = 0},
345};
Hans de Goede49809d62009-06-07 12:10:39 -0300346
Hans de Goede635118d2009-10-11 09:49:03 -0300347static const struct v4l2_pix_format ovfx2_vga_mode[] = {
348 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
349 .bytesperline = 320,
350 .sizeimage = 320 * 240,
351 .colorspace = V4L2_COLORSPACE_SRGB,
352 .priv = 1},
353 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
354 .bytesperline = 640,
355 .sizeimage = 640 * 480,
356 .colorspace = V4L2_COLORSPACE_SRGB,
357 .priv = 0},
358};
359static const struct v4l2_pix_format ovfx2_cif_mode[] = {
360 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
361 .bytesperline = 160,
362 .sizeimage = 160 * 120,
363 .colorspace = V4L2_COLORSPACE_SRGB,
364 .priv = 3},
365 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
366 .bytesperline = 176,
367 .sizeimage = 176 * 144,
368 .colorspace = V4L2_COLORSPACE_SRGB,
369 .priv = 1},
370 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
371 .bytesperline = 320,
372 .sizeimage = 320 * 240,
373 .colorspace = V4L2_COLORSPACE_SRGB,
374 .priv = 2},
375 {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
376 .bytesperline = 352,
377 .sizeimage = 352 * 288,
378 .colorspace = V4L2_COLORSPACE_SRGB,
379 .priv = 0},
380};
381static const struct v4l2_pix_format ovfx2_ov2610_mode[] = {
382 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
383 .bytesperline = 1600,
384 .sizeimage = 1600 * 1200,
385 .colorspace = V4L2_COLORSPACE_SRGB},
386};
387static const struct v4l2_pix_format ovfx2_ov3610_mode[] = {
Hans de Goede635118d2009-10-11 09:49:03 -0300388 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
389 .bytesperline = 640,
390 .sizeimage = 640 * 480,
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300391 .colorspace = V4L2_COLORSPACE_SRGB,
392 .priv = 1},
393 {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
394 .bytesperline = 800,
395 .sizeimage = 800 * 600,
396 .colorspace = V4L2_COLORSPACE_SRGB,
397 .priv = 1},
398 {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
399 .bytesperline = 1024,
400 .sizeimage = 1024 * 768,
401 .colorspace = V4L2_COLORSPACE_SRGB,
402 .priv = 1},
403 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
404 .bytesperline = 1600,
405 .sizeimage = 1600 * 1200,
406 .colorspace = V4L2_COLORSPACE_SRGB,
407 .priv = 0},
408 {2048, 1536, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
409 .bytesperline = 2048,
410 .sizeimage = 2048 * 1536,
411 .colorspace = V4L2_COLORSPACE_SRGB,
412 .priv = 0},
Hans de Goede635118d2009-10-11 09:49:03 -0300413};
414
415
Hans de Goede49809d62009-06-07 12:10:39 -0300416/* Registers common to OV511 / OV518 */
Hans de Goede1876bb92009-06-14 06:45:50 -0300417#define R51x_FIFO_PSIZE 0x30 /* 2 bytes wide w/ OV518(+) */
Hans de Goede49809d62009-06-07 12:10:39 -0300418#define R51x_SYS_RESET 0x50
Hans de Goede1876bb92009-06-14 06:45:50 -0300419 /* Reset type flags */
420 #define OV511_RESET_OMNICE 0x08
Hans de Goede49809d62009-06-07 12:10:39 -0300421#define R51x_SYS_INIT 0x53
422#define R51x_SYS_SNAP 0x52
423#define R51x_SYS_CUST_ID 0x5F
424#define R51x_COMP_LUT_BEGIN 0x80
425
426/* OV511 Camera interface register numbers */
Hans de Goede1876bb92009-06-14 06:45:50 -0300427#define R511_CAM_DELAY 0x10
428#define R511_CAM_EDGE 0x11
429#define R511_CAM_PXCNT 0x12
430#define R511_CAM_LNCNT 0x13
431#define R511_CAM_PXDIV 0x14
432#define R511_CAM_LNDIV 0x15
433#define R511_CAM_UV_EN 0x16
434#define R511_CAM_LINE_MODE 0x17
435#define R511_CAM_OPTS 0x18
436
437#define R511_SNAP_FRAME 0x19
438#define R511_SNAP_PXCNT 0x1A
439#define R511_SNAP_LNCNT 0x1B
440#define R511_SNAP_PXDIV 0x1C
441#define R511_SNAP_LNDIV 0x1D
442#define R511_SNAP_UV_EN 0x1E
443#define R511_SNAP_UV_EN 0x1E
444#define R511_SNAP_OPTS 0x1F
445
446#define R511_DRAM_FLOW_CTL 0x20
447#define R511_FIFO_OPTS 0x31
448#define R511_I2C_CTL 0x40
Hans de Goede49809d62009-06-07 12:10:39 -0300449#define R511_SYS_LED_CTL 0x55 /* OV511+ only */
Hans de Goede1876bb92009-06-14 06:45:50 -0300450#define R511_COMP_EN 0x78
451#define R511_COMP_LUT_EN 0x79
Hans de Goede49809d62009-06-07 12:10:39 -0300452
453/* OV518 Camera interface register numbers */
454#define R518_GPIO_OUT 0x56 /* OV518(+) only */
455#define R518_GPIO_CTL 0x57 /* OV518(+) only */
456
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300457/* OV519 Camera interface register numbers */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300458#define OV519_R10_H_SIZE 0x10
459#define OV519_R11_V_SIZE 0x11
460#define OV519_R12_X_OFFSETL 0x12
461#define OV519_R13_X_OFFSETH 0x13
462#define OV519_R14_Y_OFFSETL 0x14
463#define OV519_R15_Y_OFFSETH 0x15
464#define OV519_R16_DIVIDER 0x16
465#define OV519_R20_DFR 0x20
466#define OV519_R25_FORMAT 0x25
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300467
468/* OV519 System Controller register numbers */
469#define OV519_SYS_RESET1 0x51
470#define OV519_SYS_EN_CLK1 0x54
471
472#define OV519_GPIO_DATA_OUT0 0x71
473#define OV519_GPIO_IO_CTRL0 0x72
474
475#define OV511_ENDPOINT_ADDRESS 1 /* Isoc endpoint number */
476
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300477/*
478 * The FX2 chip does not give us a zero length read at end of frame.
479 * It does, however, give a short read at the end of a frame, if
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800480 * necessary, rather than run two frames together.
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300481 *
482 * By choosing the right bulk transfer size, we are guaranteed to always
483 * get a short read for the last read of each frame. Frame sizes are
484 * always a composite number (width * height, or a multiple) so if we
485 * choose a prime number, we are guaranteed that the last read of a
486 * frame will be short.
487 *
488 * But it isn't that easy: the 2.6 kernel requires a multiple of 4KB,
489 * otherwise EOVERFLOW "babbling" errors occur. I have not been able
490 * to figure out why. [PMiller]
491 *
492 * The constant (13 * 4096) is the largest "prime enough" number less than 64KB.
493 *
494 * It isn't enough to know the number of bytes per frame, in case we
495 * have data dropouts or buffer overruns (even though the FX2 double
496 * buffers, there are some pretty strict real time constraints for
497 * isochronous transfer for larger frame sizes).
498 */
499#define OVFX2_BULK_SIZE (13 * 4096)
500
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300501/* I2C registers */
502#define R51x_I2C_W_SID 0x41
503#define R51x_I2C_SADDR_3 0x42
504#define R51x_I2C_SADDR_2 0x43
505#define R51x_I2C_R_SID 0x44
506#define R51x_I2C_DATA 0x45
507#define R518_I2C_CTL 0x47 /* OV518(+) only */
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300508#define OVFX2_I2C_ADDR 0x00
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300509
510/* I2C ADDRESSES */
511#define OV7xx0_SID 0x42
Hans de Goede229bb7d2009-10-11 07:41:46 -0300512#define OV_HIRES_SID 0x60 /* OV9xxx / OV2xxx / OV3xxx */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300513#define OV8xx0_SID 0xa0
514#define OV6xx0_SID 0xc0
515
516/* OV7610 registers */
517#define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
Hans de Goede49809d62009-06-07 12:10:39 -0300518#define OV7610_REG_BLUE 0x01 /* blue channel balance */
519#define OV7610_REG_RED 0x02 /* red channel balance */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300520#define OV7610_REG_SAT 0x03 /* saturation */
521#define OV8610_REG_HUE 0x04 /* 04 reserved */
522#define OV7610_REG_CNT 0x05 /* Y contrast */
523#define OV7610_REG_BRT 0x06 /* Y brightness */
524#define OV7610_REG_COM_C 0x14 /* misc common regs */
525#define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
526#define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
527#define OV7610_REG_COM_I 0x29 /* misc settings */
528
529/* OV7670 registers */
530#define OV7670_REG_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
531#define OV7670_REG_BLUE 0x01 /* blue gain */
532#define OV7670_REG_RED 0x02 /* red gain */
533#define OV7670_REG_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
534#define OV7670_REG_COM1 0x04 /* Control 1 */
535#define OV7670_REG_AECHH 0x07 /* AEC MS 5 bits */
536#define OV7670_REG_COM3 0x0c /* Control 3 */
537#define OV7670_REG_COM4 0x0d /* Control 4 */
538#define OV7670_REG_COM5 0x0e /* All "reserved" */
539#define OV7670_REG_COM6 0x0f /* Control 6 */
540#define OV7670_REG_AECH 0x10 /* More bits of AEC value */
541#define OV7670_REG_CLKRC 0x11 /* Clock control */
542#define OV7670_REG_COM7 0x12 /* Control 7 */
543#define OV7670_COM7_FMT_VGA 0x00
544#define OV7670_COM7_YUV 0x00 /* YUV */
545#define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
546#define OV7670_COM7_FMT_MASK 0x38
547#define OV7670_COM7_RESET 0x80 /* Register reset */
548#define OV7670_REG_COM8 0x13 /* Control 8 */
549#define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
550#define OV7670_COM8_AWB 0x02 /* White balance enable */
551#define OV7670_COM8_AGC 0x04 /* Auto gain enable */
552#define OV7670_COM8_BFILT 0x20 /* Band filter enable */
553#define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
554#define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
555#define OV7670_REG_COM9 0x14 /* Control 9 - gain ceiling */
556#define OV7670_REG_COM10 0x15 /* Control 10 */
557#define OV7670_REG_HSTART 0x17 /* Horiz start high bits */
558#define OV7670_REG_HSTOP 0x18 /* Horiz stop high bits */
559#define OV7670_REG_VSTART 0x19 /* Vert start high bits */
560#define OV7670_REG_VSTOP 0x1a /* Vert stop high bits */
561#define OV7670_REG_MVFP 0x1e /* Mirror / vflip */
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300562#define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300563#define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
564#define OV7670_REG_AEW 0x24 /* AGC upper limit */
565#define OV7670_REG_AEB 0x25 /* AGC lower limit */
566#define OV7670_REG_VPT 0x26 /* AGC/AEC fast mode op region */
567#define OV7670_REG_HREF 0x32 /* HREF pieces */
568#define OV7670_REG_TSLB 0x3a /* lots of stuff */
569#define OV7670_REG_COM11 0x3b /* Control 11 */
570#define OV7670_COM11_EXP 0x02
571#define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
572#define OV7670_REG_COM12 0x3c /* Control 12 */
573#define OV7670_REG_COM13 0x3d /* Control 13 */
574#define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
575#define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
576#define OV7670_REG_COM14 0x3e /* Control 14 */
577#define OV7670_REG_EDGE 0x3f /* Edge enhancement factor */
578#define OV7670_REG_COM15 0x40 /* Control 15 */
579#define OV7670_COM15_R00FF 0xc0 /* 00 to FF */
580#define OV7670_REG_COM16 0x41 /* Control 16 */
581#define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
582#define OV7670_REG_BRIGHT 0x55 /* Brightness */
583#define OV7670_REG_CONTRAS 0x56 /* Contrast control */
584#define OV7670_REG_GFIX 0x69 /* Fix gain control */
585#define OV7670_REG_RGB444 0x8c /* RGB 444 control */
586#define OV7670_REG_HAECC1 0x9f /* Hist AEC/AGC control 1 */
587#define OV7670_REG_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
588#define OV7670_REG_BD50MAX 0xa5 /* 50hz banding step limit */
589#define OV7670_REG_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
590#define OV7670_REG_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
591#define OV7670_REG_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
592#define OV7670_REG_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
593#define OV7670_REG_HAECC7 0xaa /* Hist AEC/AGC control 7 */
594#define OV7670_REG_BD60MAX 0xab /* 60hz banding step limit */
595
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300596struct ov_regvals {
597 __u8 reg;
598 __u8 val;
599};
600struct ov_i2c_regvals {
601 __u8 reg;
602 __u8 val;
603};
604
Hans de Goede635118d2009-10-11 09:49:03 -0300605/* Settings for OV2610 camera chip */
606static const struct ov_i2c_regvals norm_2610[] =
607{
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300608 { 0x12, 0x80 }, /* reset */
Hans de Goede635118d2009-10-11 09:49:03 -0300609};
610
611static const struct ov_i2c_regvals norm_3620b[] =
612{
613 /*
614 * From the datasheet: "Note that after writing to register COMH
615 * (0x12) to change the sensor mode, registers related to the
616 * sensor’s cropping window will be reset back to their default
617 * values."
618 *
619 * "wait 4096 external clock ... to make sure the sensor is
620 * stable and ready to access registers" i.e. 160us at 24MHz
621 */
622
623 { 0x12, 0x80 }, /* COMH reset */
624 { 0x12, 0x00 }, /* QXGA, master */
625
626 /*
627 * 11 CLKRC "Clock Rate Control"
628 * [7] internal frequency doublers: on
629 * [6] video port mode: master
630 * [5:0] clock divider: 1
631 */
632 { 0x11, 0x80 },
633
634 /*
635 * 13 COMI "Common Control I"
636 * = 192 (0xC0) 11000000
637 * COMI[7] "AEC speed selection"
638 * = 1 (0x01) 1....... "Faster AEC correction"
639 * COMI[6] "AEC speed step selection"
640 * = 1 (0x01) .1...... "Big steps, fast"
641 * COMI[5] "Banding filter on off"
642 * = 0 (0x00) ..0..... "Off"
643 * COMI[4] "Banding filter option"
644 * = 0 (0x00) ...0.... "Main clock is 48 MHz and
645 * the PLL is ON"
646 * COMI[3] "Reserved"
647 * = 0 (0x00) ....0...
648 * COMI[2] "AGC auto manual control selection"
649 * = 0 (0x00) .....0.. "Manual"
650 * COMI[1] "AWB auto manual control selection"
651 * = 0 (0x00) ......0. "Manual"
652 * COMI[0] "Exposure control"
653 * = 0 (0x00) .......0 "Manual"
654 */
655 { 0x13, 0xC0 },
656
657 /*
658 * 09 COMC "Common Control C"
659 * = 8 (0x08) 00001000
660 * COMC[7:5] "Reserved"
661 * = 0 (0x00) 000.....
662 * COMC[4] "Sleep Mode Enable"
663 * = 0 (0x00) ...0.... "Normal mode"
664 * COMC[3:2] "Sensor sampling reset timing selection"
665 * = 2 (0x02) ....10.. "Longer reset time"
666 * COMC[1:0] "Output drive current select"
667 * = 0 (0x00) ......00 "Weakest"
668 */
669 { 0x09, 0x08 },
670
671 /*
672 * 0C COMD "Common Control D"
673 * = 8 (0x08) 00001000
674 * COMD[7] "Reserved"
675 * = 0 (0x00) 0.......
676 * COMD[6] "Swap MSB and LSB at the output port"
677 * = 0 (0x00) .0...... "False"
678 * COMD[5:3] "Reserved"
679 * = 1 (0x01) ..001...
680 * COMD[2] "Output Average On Off"
681 * = 0 (0x00) .....0.. "Output Normal"
682 * COMD[1] "Sensor precharge voltage selection"
683 * = 0 (0x00) ......0. "Selects internal
684 * reference precharge
685 * voltage"
686 * COMD[0] "Snapshot option"
687 * = 0 (0x00) .......0 "Enable live video output
688 * after snapshot sequence"
689 */
690 { 0x0c, 0x08 },
691
692 /*
693 * 0D COME "Common Control E"
694 * = 161 (0xA1) 10100001
695 * COME[7] "Output average option"
696 * = 1 (0x01) 1....... "Output average of 4 pixels"
697 * COME[6] "Anti-blooming control"
698 * = 0 (0x00) .0...... "Off"
699 * COME[5:3] "Reserved"
700 * = 4 (0x04) ..100...
701 * COME[2] "Clock output power down pin status"
702 * = 0 (0x00) .....0.. "Tri-state data output pin
703 * on power down"
704 * COME[1] "Data output pin status selection at power down"
705 * = 0 (0x00) ......0. "Tri-state VSYNC, PCLK,
706 * HREF, and CHSYNC pins on
707 * power down"
708 * COME[0] "Auto zero circuit select"
709 * = 1 (0x01) .......1 "On"
710 */
711 { 0x0d, 0xA1 },
712
713 /*
714 * 0E COMF "Common Control F"
715 * = 112 (0x70) 01110000
716 * COMF[7] "System clock selection"
717 * = 0 (0x00) 0....... "Use 24 MHz system clock"
718 * COMF[6:4] "Reserved"
719 * = 7 (0x07) .111....
720 * COMF[3] "Manual auto negative offset canceling selection"
721 * = 0 (0x00) ....0... "Auto detect negative
722 * offset and cancel it"
723 * COMF[2:0] "Reserved"
724 * = 0 (0x00) .....000
725 */
726 { 0x0e, 0x70 },
727
728 /*
729 * 0F COMG "Common Control G"
730 * = 66 (0x42) 01000010
731 * COMG[7] "Optical black output selection"
732 * = 0 (0x00) 0....... "Disable"
733 * COMG[6] "Black level calibrate selection"
734 * = 1 (0x01) .1...... "Use optical black pixels
735 * to calibrate"
736 * COMG[5:4] "Reserved"
737 * = 0 (0x00) ..00....
738 * COMG[3] "Channel offset adjustment"
739 * = 0 (0x00) ....0... "Disable offset adjustment"
740 * COMG[2] "ADC black level calibration option"
741 * = 0 (0x00) .....0.. "Use B/G line and G/R
742 * line to calibrate each
743 * channel's black level"
744 * COMG[1] "Reserved"
745 * = 1 (0x01) ......1.
746 * COMG[0] "ADC black level calibration enable"
747 * = 0 (0x00) .......0 "Disable"
748 */
749 { 0x0f, 0x42 },
750
751 /*
752 * 14 COMJ "Common Control J"
753 * = 198 (0xC6) 11000110
754 * COMJ[7:6] "AGC gain ceiling"
755 * = 3 (0x03) 11...... "8x"
756 * COMJ[5:4] "Reserved"
757 * = 0 (0x00) ..00....
758 * COMJ[3] "Auto banding filter"
759 * = 0 (0x00) ....0... "Banding filter is always
760 * on off depending on
761 * COMI[5] setting"
762 * COMJ[2] "VSYNC drop option"
763 * = 1 (0x01) .....1.. "SYNC is dropped if frame
764 * data is dropped"
765 * COMJ[1] "Frame data drop"
766 * = 1 (0x01) ......1. "Drop frame data if
767 * exposure is not within
768 * tolerance. In AEC mode,
769 * data is normally dropped
770 * when data is out of
771 * range."
772 * COMJ[0] "Reserved"
773 * = 0 (0x00) .......0
774 */
775 { 0x14, 0xC6 },
776
777 /*
778 * 15 COMK "Common Control K"
779 * = 2 (0x02) 00000010
780 * COMK[7] "CHSYNC pin output swap"
781 * = 0 (0x00) 0....... "CHSYNC"
782 * COMK[6] "HREF pin output swap"
783 * = 0 (0x00) .0...... "HREF"
784 * COMK[5] "PCLK output selection"
785 * = 0 (0x00) ..0..... "PCLK always output"
786 * COMK[4] "PCLK edge selection"
787 * = 0 (0x00) ...0.... "Data valid on falling edge"
788 * COMK[3] "HREF output polarity"
789 * = 0 (0x00) ....0... "positive"
790 * COMK[2] "Reserved"
791 * = 0 (0x00) .....0..
792 * COMK[1] "VSYNC polarity"
793 * = 1 (0x01) ......1. "negative"
794 * COMK[0] "HSYNC polarity"
795 * = 0 (0x00) .......0 "positive"
796 */
797 { 0x15, 0x02 },
798
799 /*
800 * 33 CHLF "Current Control"
801 * = 9 (0x09) 00001001
802 * CHLF[7:6] "Sensor current control"
803 * = 0 (0x00) 00......
804 * CHLF[5] "Sensor current range control"
805 * = 0 (0x00) ..0..... "normal range"
806 * CHLF[4] "Sensor current"
807 * = 0 (0x00) ...0.... "normal current"
808 * CHLF[3] "Sensor buffer current control"
809 * = 1 (0x01) ....1... "half current"
810 * CHLF[2] "Column buffer current control"
811 * = 0 (0x00) .....0.. "normal current"
812 * CHLF[1] "Analog DSP current control"
813 * = 0 (0x00) ......0. "normal current"
814 * CHLF[1] "ADC current control"
815 * = 0 (0x00) ......0. "normal current"
816 */
817 { 0x33, 0x09 },
818
819 /*
820 * 34 VBLM "Blooming Control"
821 * = 80 (0x50) 01010000
822 * VBLM[7] "Hard soft reset switch"
823 * = 0 (0x00) 0....... "Hard reset"
824 * VBLM[6:4] "Blooming voltage selection"
825 * = 5 (0x05) .101....
826 * VBLM[3:0] "Sensor current control"
827 * = 0 (0x00) ....0000
828 */
829 { 0x34, 0x50 },
830
831 /*
832 * 36 VCHG "Sensor Precharge Voltage Control"
833 * = 0 (0x00) 00000000
834 * VCHG[7] "Reserved"
835 * = 0 (0x00) 0.......
836 * VCHG[6:4] "Sensor precharge voltage control"
837 * = 0 (0x00) .000....
838 * VCHG[3:0] "Sensor array common reference"
839 * = 0 (0x00) ....0000
840 */
841 { 0x36, 0x00 },
842
843 /*
844 * 37 ADC "ADC Reference Control"
845 * = 4 (0x04) 00000100
846 * ADC[7:4] "Reserved"
847 * = 0 (0x00) 0000....
848 * ADC[3] "ADC input signal range"
849 * = 0 (0x00) ....0... "Input signal 1.0x"
850 * ADC[2:0] "ADC range control"
851 * = 4 (0x04) .....100
852 */
853 { 0x37, 0x04 },
854
855 /*
856 * 38 ACOM "Analog Common Ground"
857 * = 82 (0x52) 01010010
858 * ACOM[7] "Analog gain control"
859 * = 0 (0x00) 0....... "Gain 1x"
860 * ACOM[6] "Analog black level calibration"
861 * = 1 (0x01) .1...... "On"
862 * ACOM[5:0] "Reserved"
863 * = 18 (0x12) ..010010
864 */
865 { 0x38, 0x52 },
866
867 /*
868 * 3A FREFA "Internal Reference Adjustment"
869 * = 0 (0x00) 00000000
870 * FREFA[7:0] "Range"
871 * = 0 (0x00) 00000000
872 */
873 { 0x3a, 0x00 },
874
875 /*
876 * 3C FVOPT "Internal Reference Adjustment"
877 * = 31 (0x1F) 00011111
878 * FVOPT[7:0] "Range"
879 * = 31 (0x1F) 00011111
880 */
881 { 0x3c, 0x1F },
882
883 /*
884 * 44 Undocumented = 0 (0x00) 00000000
885 * 44[7:0] "It's a secret"
886 * = 0 (0x00) 00000000
887 */
888 { 0x44, 0x00 },
889
890 /*
891 * 40 Undocumented = 0 (0x00) 00000000
892 * 40[7:0] "It's a secret"
893 * = 0 (0x00) 00000000
894 */
895 { 0x40, 0x00 },
896
897 /*
898 * 41 Undocumented = 0 (0x00) 00000000
899 * 41[7:0] "It's a secret"
900 * = 0 (0x00) 00000000
901 */
902 { 0x41, 0x00 },
903
904 /*
905 * 42 Undocumented = 0 (0x00) 00000000
906 * 42[7:0] "It's a secret"
907 * = 0 (0x00) 00000000
908 */
909 { 0x42, 0x00 },
910
911 /*
912 * 43 Undocumented = 0 (0x00) 00000000
913 * 43[7:0] "It's a secret"
914 * = 0 (0x00) 00000000
915 */
916 { 0x43, 0x00 },
917
918 /*
919 * 45 Undocumented = 128 (0x80) 10000000
920 * 45[7:0] "It's a secret"
921 * = 128 (0x80) 10000000
922 */
923 { 0x45, 0x80 },
924
925 /*
926 * 48 Undocumented = 192 (0xC0) 11000000
927 * 48[7:0] "It's a secret"
928 * = 192 (0xC0) 11000000
929 */
930 { 0x48, 0xC0 },
931
932 /*
933 * 49 Undocumented = 25 (0x19) 00011001
934 * 49[7:0] "It's a secret"
935 * = 25 (0x19) 00011001
936 */
937 { 0x49, 0x19 },
938
939 /*
940 * 4B Undocumented = 128 (0x80) 10000000
941 * 4B[7:0] "It's a secret"
942 * = 128 (0x80) 10000000
943 */
944 { 0x4B, 0x80 },
945
946 /*
947 * 4D Undocumented = 196 (0xC4) 11000100
948 * 4D[7:0] "It's a secret"
949 * = 196 (0xC4) 11000100
950 */
951 { 0x4D, 0xC4 },
952
953 /*
954 * 35 VREF "Reference Voltage Control"
955 * = 76 (0x4C) 01001100
956 * VREF[7:5] "Column high reference control"
957 * = 2 (0x02) 010..... "higher voltage"
958 * VREF[4:2] "Column low reference control"
959 * = 3 (0x03) ...011.. "Highest voltage"
960 * VREF[1:0] "Reserved"
961 * = 0 (0x00) ......00
962 */
963 { 0x35, 0x4C },
964
965 /*
966 * 3D Undocumented = 0 (0x00) 00000000
967 * 3D[7:0] "It's a secret"
968 * = 0 (0x00) 00000000
969 */
970 { 0x3D, 0x00 },
971
972 /*
973 * 3E Undocumented = 0 (0x00) 00000000
974 * 3E[7:0] "It's a secret"
975 * = 0 (0x00) 00000000
976 */
977 { 0x3E, 0x00 },
978
979 /*
980 * 3B FREFB "Internal Reference Adjustment"
981 * = 24 (0x18) 00011000
982 * FREFB[7:0] "Range"
983 * = 24 (0x18) 00011000
984 */
985 { 0x3b, 0x18 },
986
987 /*
988 * 33 CHLF "Current Control"
989 * = 25 (0x19) 00011001
990 * CHLF[7:6] "Sensor current control"
991 * = 0 (0x00) 00......
992 * CHLF[5] "Sensor current range control"
993 * = 0 (0x00) ..0..... "normal range"
994 * CHLF[4] "Sensor current"
995 * = 1 (0x01) ...1.... "double current"
996 * CHLF[3] "Sensor buffer current control"
997 * = 1 (0x01) ....1... "half current"
998 * CHLF[2] "Column buffer current control"
999 * = 0 (0x00) .....0.. "normal current"
1000 * CHLF[1] "Analog DSP current control"
1001 * = 0 (0x00) ......0. "normal current"
1002 * CHLF[1] "ADC current control"
1003 * = 0 (0x00) ......0. "normal current"
1004 */
1005 { 0x33, 0x19 },
1006
1007 /*
1008 * 34 VBLM "Blooming Control"
1009 * = 90 (0x5A) 01011010
1010 * VBLM[7] "Hard soft reset switch"
1011 * = 0 (0x00) 0....... "Hard reset"
1012 * VBLM[6:4] "Blooming voltage selection"
1013 * = 5 (0x05) .101....
1014 * VBLM[3:0] "Sensor current control"
1015 * = 10 (0x0A) ....1010
1016 */
1017 { 0x34, 0x5A },
1018
1019 /*
1020 * 3B FREFB "Internal Reference Adjustment"
1021 * = 0 (0x00) 00000000
1022 * FREFB[7:0] "Range"
1023 * = 0 (0x00) 00000000
1024 */
1025 { 0x3b, 0x00 },
1026
1027 /*
1028 * 33 CHLF "Current Control"
1029 * = 9 (0x09) 00001001
1030 * CHLF[7:6] "Sensor current control"
1031 * = 0 (0x00) 00......
1032 * CHLF[5] "Sensor current range control"
1033 * = 0 (0x00) ..0..... "normal range"
1034 * CHLF[4] "Sensor current"
1035 * = 0 (0x00) ...0.... "normal current"
1036 * CHLF[3] "Sensor buffer current control"
1037 * = 1 (0x01) ....1... "half current"
1038 * CHLF[2] "Column buffer current control"
1039 * = 0 (0x00) .....0.. "normal current"
1040 * CHLF[1] "Analog DSP current control"
1041 * = 0 (0x00) ......0. "normal current"
1042 * CHLF[1] "ADC current control"
1043 * = 0 (0x00) ......0. "normal current"
1044 */
1045 { 0x33, 0x09 },
1046
1047 /*
1048 * 34 VBLM "Blooming Control"
1049 * = 80 (0x50) 01010000
1050 * VBLM[7] "Hard soft reset switch"
1051 * = 0 (0x00) 0....... "Hard reset"
1052 * VBLM[6:4] "Blooming voltage selection"
1053 * = 5 (0x05) .101....
1054 * VBLM[3:0] "Sensor current control"
1055 * = 0 (0x00) ....0000
1056 */
1057 { 0x34, 0x50 },
1058
1059 /*
1060 * 12 COMH "Common Control H"
1061 * = 64 (0x40) 01000000
1062 * COMH[7] "SRST"
1063 * = 0 (0x00) 0....... "No-op"
1064 * COMH[6:4] "Resolution selection"
1065 * = 4 (0x04) .100.... "XGA"
1066 * COMH[3] "Master slave selection"
1067 * = 0 (0x00) ....0... "Master mode"
1068 * COMH[2] "Internal B/R channel option"
1069 * = 0 (0x00) .....0.. "B/R use same channel"
1070 * COMH[1] "Color bar test pattern"
1071 * = 0 (0x00) ......0. "Off"
1072 * COMH[0] "Reserved"
1073 * = 0 (0x00) .......0
1074 */
1075 { 0x12, 0x40 },
1076
1077 /*
1078 * 17 HREFST "Horizontal window start"
1079 * = 31 (0x1F) 00011111
1080 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1081 * = 31 (0x1F) 00011111
1082 */
1083 { 0x17, 0x1F },
1084
1085 /*
1086 * 18 HREFEND "Horizontal window end"
1087 * = 95 (0x5F) 01011111
1088 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1089 * = 95 (0x5F) 01011111
1090 */
1091 { 0x18, 0x5F },
1092
1093 /*
1094 * 19 VSTRT "Vertical window start"
1095 * = 0 (0x00) 00000000
1096 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1097 * = 0 (0x00) 00000000
1098 */
1099 { 0x19, 0x00 },
1100
1101 /*
1102 * 1A VEND "Vertical window end"
1103 * = 96 (0x60) 01100000
1104 * VEND[7:0] "Vertical Window End, 8 MSBs"
1105 * = 96 (0x60) 01100000
1106 */
1107 { 0x1a, 0x60 },
1108
1109 /*
1110 * 32 COMM "Common Control M"
1111 * = 18 (0x12) 00010010
1112 * COMM[7:6] "Pixel clock divide option"
1113 * = 0 (0x00) 00...... "/1"
1114 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1115 * = 2 (0x02) ..010...
1116 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1117 * = 2 (0x02) .....010
1118 */
1119 { 0x32, 0x12 },
1120
1121 /*
1122 * 03 COMA "Common Control A"
1123 * = 74 (0x4A) 01001010
1124 * COMA[7:4] "AWB Update Threshold"
1125 * = 4 (0x04) 0100....
1126 * COMA[3:2] "Vertical window end line control 2 LSBs"
1127 * = 2 (0x02) ....10..
1128 * COMA[1:0] "Vertical window start line control 2 LSBs"
1129 * = 2 (0x02) ......10
1130 */
1131 { 0x03, 0x4A },
1132
1133 /*
1134 * 11 CLKRC "Clock Rate Control"
1135 * = 128 (0x80) 10000000
1136 * CLKRC[7] "Internal frequency doublers on off seclection"
1137 * = 1 (0x01) 1....... "On"
1138 * CLKRC[6] "Digital video master slave selection"
1139 * = 0 (0x00) .0...... "Master mode, sensor
1140 * provides PCLK"
1141 * CLKRC[5:0] "Clock divider { CLK = PCLK/(1+CLKRC[5:0]) }"
1142 * = 0 (0x00) ..000000
1143 */
1144 { 0x11, 0x80 },
1145
1146 /*
1147 * 12 COMH "Common Control H"
1148 * = 0 (0x00) 00000000
1149 * COMH[7] "SRST"
1150 * = 0 (0x00) 0....... "No-op"
1151 * COMH[6:4] "Resolution selection"
1152 * = 0 (0x00) .000.... "QXGA"
1153 * COMH[3] "Master slave selection"
1154 * = 0 (0x00) ....0... "Master mode"
1155 * COMH[2] "Internal B/R channel option"
1156 * = 0 (0x00) .....0.. "B/R use same channel"
1157 * COMH[1] "Color bar test pattern"
1158 * = 0 (0x00) ......0. "Off"
1159 * COMH[0] "Reserved"
1160 * = 0 (0x00) .......0
1161 */
1162 { 0x12, 0x00 },
1163
1164 /*
1165 * 12 COMH "Common Control H"
1166 * = 64 (0x40) 01000000
1167 * COMH[7] "SRST"
1168 * = 0 (0x00) 0....... "No-op"
1169 * COMH[6:4] "Resolution selection"
1170 * = 4 (0x04) .100.... "XGA"
1171 * COMH[3] "Master slave selection"
1172 * = 0 (0x00) ....0... "Master mode"
1173 * COMH[2] "Internal B/R channel option"
1174 * = 0 (0x00) .....0.. "B/R use same channel"
1175 * COMH[1] "Color bar test pattern"
1176 * = 0 (0x00) ......0. "Off"
1177 * COMH[0] "Reserved"
1178 * = 0 (0x00) .......0
1179 */
1180 { 0x12, 0x40 },
1181
1182 /*
1183 * 17 HREFST "Horizontal window start"
1184 * = 31 (0x1F) 00011111
1185 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1186 * = 31 (0x1F) 00011111
1187 */
1188 { 0x17, 0x1F },
1189
1190 /*
1191 * 18 HREFEND "Horizontal window end"
1192 * = 95 (0x5F) 01011111
1193 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1194 * = 95 (0x5F) 01011111
1195 */
1196 { 0x18, 0x5F },
1197
1198 /*
1199 * 19 VSTRT "Vertical window start"
1200 * = 0 (0x00) 00000000
1201 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1202 * = 0 (0x00) 00000000
1203 */
1204 { 0x19, 0x00 },
1205
1206 /*
1207 * 1A VEND "Vertical window end"
1208 * = 96 (0x60) 01100000
1209 * VEND[7:0] "Vertical Window End, 8 MSBs"
1210 * = 96 (0x60) 01100000
1211 */
1212 { 0x1a, 0x60 },
1213
1214 /*
1215 * 32 COMM "Common Control M"
1216 * = 18 (0x12) 00010010
1217 * COMM[7:6] "Pixel clock divide option"
1218 * = 0 (0x00) 00...... "/1"
1219 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1220 * = 2 (0x02) ..010...
1221 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1222 * = 2 (0x02) .....010
1223 */
1224 { 0x32, 0x12 },
1225
1226 /*
1227 * 03 COMA "Common Control A"
1228 * = 74 (0x4A) 01001010
1229 * COMA[7:4] "AWB Update Threshold"
1230 * = 4 (0x04) 0100....
1231 * COMA[3:2] "Vertical window end line control 2 LSBs"
1232 * = 2 (0x02) ....10..
1233 * COMA[1:0] "Vertical window start line control 2 LSBs"
1234 * = 2 (0x02) ......10
1235 */
1236 { 0x03, 0x4A },
1237
1238 /*
1239 * 02 RED "Red Gain Control"
1240 * = 175 (0xAF) 10101111
1241 * RED[7] "Action"
1242 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1243 * RED[6:0] "Value"
1244 * = 47 (0x2F) .0101111
1245 */
1246 { 0x02, 0xAF },
1247
1248 /*
1249 * 2D ADDVSL "VSYNC Pulse Width"
1250 * = 210 (0xD2) 11010010
1251 * ADDVSL[7:0] "VSYNC pulse width, LSB"
1252 * = 210 (0xD2) 11010010
1253 */
1254 { 0x2d, 0xD2 },
1255
1256 /*
1257 * 00 GAIN = 24 (0x18) 00011000
1258 * GAIN[7:6] "Reserved"
1259 * = 0 (0x00) 00......
1260 * GAIN[5] "Double"
1261 * = 0 (0x00) ..0..... "False"
1262 * GAIN[4] "Double"
1263 * = 1 (0x01) ...1.... "True"
1264 * GAIN[3:0] "Range"
1265 * = 8 (0x08) ....1000
1266 */
1267 { 0x00, 0x18 },
1268
1269 /*
1270 * 01 BLUE "Blue Gain Control"
1271 * = 240 (0xF0) 11110000
1272 * BLUE[7] "Action"
1273 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1274 * BLUE[6:0] "Value"
1275 * = 112 (0x70) .1110000
1276 */
1277 { 0x01, 0xF0 },
1278
1279 /*
1280 * 10 AEC "Automatic Exposure Control"
1281 * = 10 (0x0A) 00001010
1282 * AEC[7:0] "Automatic Exposure Control, 8 MSBs"
1283 * = 10 (0x0A) 00001010
1284 */
1285 { 0x10, 0x0A },
1286
1287 { 0xE1, 0x67 },
1288 { 0xE3, 0x03 },
1289 { 0xE4, 0x26 },
1290 { 0xE5, 0x3E },
1291 { 0xF8, 0x01 },
1292 { 0xFF, 0x01 },
1293};
1294
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001295static const struct ov_i2c_regvals norm_6x20[] = {
1296 { 0x12, 0x80 }, /* reset */
1297 { 0x11, 0x01 },
1298 { 0x03, 0x60 },
1299 { 0x05, 0x7f }, /* For when autoadjust is off */
1300 { 0x07, 0xa8 },
1301 /* The ratio of 0x0c and 0x0d controls the white point */
1302 { 0x0c, 0x24 },
1303 { 0x0d, 0x24 },
1304 { 0x0f, 0x15 }, /* COMS */
1305 { 0x10, 0x75 }, /* AEC Exposure time */
1306 { 0x12, 0x24 }, /* Enable AGC */
1307 { 0x14, 0x04 },
1308 /* 0x16: 0x06 helps frame stability with moving objects */
1309 { 0x16, 0x06 },
1310/* { 0x20, 0x30 }, * Aperture correction enable */
1311 { 0x26, 0xb2 }, /* BLC enable */
1312 /* 0x28: 0x05 Selects RGB format if RGB on */
1313 { 0x28, 0x05 },
1314 { 0x2a, 0x04 }, /* Disable framerate adjust */
1315/* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
Hans de Goedeae49c402009-06-14 19:15:07 -03001316 { 0x2d, 0x85 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001317 { 0x33, 0xa0 }, /* Color Processing Parameter */
1318 { 0x34, 0xd2 }, /* Max A/D range */
1319 { 0x38, 0x8b },
1320 { 0x39, 0x40 },
1321
1322 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1323 { 0x3c, 0x3c }, /* Change AEC mode */
1324 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1325
1326 { 0x3d, 0x80 },
1327 /* These next two registers (0x4a, 0x4b) are undocumented.
1328 * They control the color balance */
1329 { 0x4a, 0x80 },
1330 { 0x4b, 0x80 },
1331 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1332 { 0x4e, 0xc1 },
1333 { 0x4f, 0x04 },
1334/* Do 50-53 have any effect? */
1335/* Toggle 0x12[2] off and on here? */
1336};
1337
1338static const struct ov_i2c_regvals norm_6x30[] = {
1339 { 0x12, 0x80 }, /* Reset */
1340 { 0x00, 0x1f }, /* Gain */
1341 { 0x01, 0x99 }, /* Blue gain */
1342 { 0x02, 0x7c }, /* Red gain */
1343 { 0x03, 0xc0 }, /* Saturation */
1344 { 0x05, 0x0a }, /* Contrast */
1345 { 0x06, 0x95 }, /* Brightness */
1346 { 0x07, 0x2d }, /* Sharpness */
1347 { 0x0c, 0x20 },
1348 { 0x0d, 0x20 },
Hans de Goede02ab18b2009-06-14 04:32:04 -03001349 { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001350 { 0x0f, 0x05 },
1351 { 0x10, 0x9a },
1352 { 0x11, 0x00 }, /* Pixel clock = fastest */
1353 { 0x12, 0x24 }, /* Enable AGC and AWB */
1354 { 0x13, 0x21 },
1355 { 0x14, 0x80 },
1356 { 0x15, 0x01 },
1357 { 0x16, 0x03 },
1358 { 0x17, 0x38 },
1359 { 0x18, 0xea },
1360 { 0x19, 0x04 },
1361 { 0x1a, 0x93 },
1362 { 0x1b, 0x00 },
1363 { 0x1e, 0xc4 },
1364 { 0x1f, 0x04 },
1365 { 0x20, 0x20 },
1366 { 0x21, 0x10 },
1367 { 0x22, 0x88 },
1368 { 0x23, 0xc0 }, /* Crystal circuit power level */
1369 { 0x25, 0x9a }, /* Increase AEC black ratio */
1370 { 0x26, 0xb2 }, /* BLC enable */
1371 { 0x27, 0xa2 },
1372 { 0x28, 0x00 },
1373 { 0x29, 0x00 },
1374 { 0x2a, 0x84 }, /* 60 Hz power */
1375 { 0x2b, 0xa8 }, /* 60 Hz power */
1376 { 0x2c, 0xa0 },
1377 { 0x2d, 0x95 }, /* Enable auto-brightness */
1378 { 0x2e, 0x88 },
1379 { 0x33, 0x26 },
1380 { 0x34, 0x03 },
1381 { 0x36, 0x8f },
1382 { 0x37, 0x80 },
1383 { 0x38, 0x83 },
1384 { 0x39, 0x80 },
1385 { 0x3a, 0x0f },
1386 { 0x3b, 0x3c },
1387 { 0x3c, 0x1a },
1388 { 0x3d, 0x80 },
1389 { 0x3e, 0x80 },
1390 { 0x3f, 0x0e },
1391 { 0x40, 0x00 }, /* White bal */
1392 { 0x41, 0x00 }, /* White bal */
1393 { 0x42, 0x80 },
1394 { 0x43, 0x3f }, /* White bal */
1395 { 0x44, 0x80 },
1396 { 0x45, 0x20 },
1397 { 0x46, 0x20 },
1398 { 0x47, 0x80 },
1399 { 0x48, 0x7f },
1400 { 0x49, 0x00 },
1401 { 0x4a, 0x00 },
1402 { 0x4b, 0x80 },
1403 { 0x4c, 0xd0 },
1404 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1405 { 0x4e, 0x40 },
1406 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1407 { 0x50, 0xff },
1408 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1409 { 0x55, 0xff },
1410 { 0x56, 0x12 },
1411 { 0x57, 0x81 },
1412 { 0x58, 0x75 },
1413 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1414 { 0x5a, 0x2c },
1415 { 0x5b, 0x0f }, /* AWB chrominance levels */
1416 { 0x5c, 0x10 },
1417 { 0x3d, 0x80 },
1418 { 0x27, 0xa6 },
1419 { 0x12, 0x20 }, /* Toggle AWB */
1420 { 0x12, 0x24 },
1421};
1422
1423/* Lawrence Glaister <lg@jfm.bc.ca> reports:
1424 *
1425 * Register 0x0f in the 7610 has the following effects:
1426 *
1427 * 0x85 (AEC method 1): Best overall, good contrast range
1428 * 0x45 (AEC method 2): Very overexposed
1429 * 0xa5 (spec sheet default): Ok, but the black level is
1430 * shifted resulting in loss of contrast
1431 * 0x05 (old driver setting): very overexposed, too much
1432 * contrast
1433 */
1434static const struct ov_i2c_regvals norm_7610[] = {
1435 { 0x10, 0xff },
1436 { 0x16, 0x06 },
1437 { 0x28, 0x24 },
1438 { 0x2b, 0xac },
1439 { 0x12, 0x00 },
1440 { 0x38, 0x81 },
1441 { 0x28, 0x24 }, /* 0c */
1442 { 0x0f, 0x85 }, /* lg's setting */
1443 { 0x15, 0x01 },
1444 { 0x20, 0x1c },
1445 { 0x23, 0x2a },
1446 { 0x24, 0x10 },
1447 { 0x25, 0x8a },
1448 { 0x26, 0xa2 },
1449 { 0x27, 0xc2 },
1450 { 0x2a, 0x04 },
1451 { 0x2c, 0xfe },
1452 { 0x2d, 0x93 },
1453 { 0x30, 0x71 },
1454 { 0x31, 0x60 },
1455 { 0x32, 0x26 },
1456 { 0x33, 0x20 },
1457 { 0x34, 0x48 },
1458 { 0x12, 0x24 },
1459 { 0x11, 0x01 },
1460 { 0x0c, 0x24 },
1461 { 0x0d, 0x24 },
1462};
1463
1464static const struct ov_i2c_regvals norm_7620[] = {
Hans de Goedea511ba92009-10-16 07:13:07 -03001465 { 0x12, 0x80 }, /* reset */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001466 { 0x00, 0x00 }, /* gain */
1467 { 0x01, 0x80 }, /* blue gain */
1468 { 0x02, 0x80 }, /* red gain */
1469 { 0x03, 0xc0 }, /* OV7670_REG_VREF */
1470 { 0x06, 0x60 },
1471 { 0x07, 0x00 },
1472 { 0x0c, 0x24 },
1473 { 0x0c, 0x24 },
1474 { 0x0d, 0x24 },
1475 { 0x11, 0x01 },
1476 { 0x12, 0x24 },
1477 { 0x13, 0x01 },
1478 { 0x14, 0x84 },
1479 { 0x15, 0x01 },
1480 { 0x16, 0x03 },
1481 { 0x17, 0x2f },
1482 { 0x18, 0xcf },
1483 { 0x19, 0x06 },
1484 { 0x1a, 0xf5 },
1485 { 0x1b, 0x00 },
1486 { 0x20, 0x18 },
1487 { 0x21, 0x80 },
1488 { 0x22, 0x80 },
1489 { 0x23, 0x00 },
1490 { 0x26, 0xa2 },
1491 { 0x27, 0xea },
Hans de Goedeb282d872009-06-14 19:10:40 -03001492 { 0x28, 0x22 }, /* Was 0x20, bit1 enables a 2x gain which we need */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001493 { 0x29, 0x00 },
1494 { 0x2a, 0x10 },
1495 { 0x2b, 0x00 },
1496 { 0x2c, 0x88 },
1497 { 0x2d, 0x91 },
1498 { 0x2e, 0x80 },
1499 { 0x2f, 0x44 },
1500 { 0x60, 0x27 },
1501 { 0x61, 0x02 },
1502 { 0x62, 0x5f },
1503 { 0x63, 0xd5 },
1504 { 0x64, 0x57 },
1505 { 0x65, 0x83 },
1506 { 0x66, 0x55 },
1507 { 0x67, 0x92 },
1508 { 0x68, 0xcf },
1509 { 0x69, 0x76 },
1510 { 0x6a, 0x22 },
1511 { 0x6b, 0x00 },
1512 { 0x6c, 0x02 },
1513 { 0x6d, 0x44 },
1514 { 0x6e, 0x80 },
1515 { 0x6f, 0x1d },
1516 { 0x70, 0x8b },
1517 { 0x71, 0x00 },
1518 { 0x72, 0x14 },
1519 { 0x73, 0x54 },
1520 { 0x74, 0x00 },
1521 { 0x75, 0x8e },
1522 { 0x76, 0x00 },
1523 { 0x77, 0xff },
1524 { 0x78, 0x80 },
1525 { 0x79, 0x80 },
1526 { 0x7a, 0x80 },
1527 { 0x7b, 0xe2 },
1528 { 0x7c, 0x00 },
1529};
1530
1531/* 7640 and 7648. The defaults should be OK for most registers. */
1532static const struct ov_i2c_regvals norm_7640[] = {
1533 { 0x12, 0x80 },
1534 { 0x12, 0x14 },
1535};
1536
1537/* 7670. Defaults taken from OmniVision provided data,
1538* as provided by Jonathan Corbet of OLPC */
1539static const struct ov_i2c_regvals norm_7670[] = {
1540 { OV7670_REG_COM7, OV7670_COM7_RESET },
1541 { OV7670_REG_TSLB, 0x04 }, /* OV */
1542 { OV7670_REG_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
1543 { OV7670_REG_CLKRC, 0x01 },
1544/*
1545 * Set the hardware window. These values from OV don't entirely
1546 * make sense - hstop is less than hstart. But they work...
1547 */
1548 { OV7670_REG_HSTART, 0x13 },
1549 { OV7670_REG_HSTOP, 0x01 },
1550 { OV7670_REG_HREF, 0xb6 },
1551 { OV7670_REG_VSTART, 0x02 },
1552 { OV7670_REG_VSTOP, 0x7a },
1553 { OV7670_REG_VREF, 0x0a },
1554
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001555 { OV7670_REG_COM3, 0x00 },
1556 { OV7670_REG_COM14, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001557/* Mystery scaling numbers */
1558 { 0x70, 0x3a },
1559 { 0x71, 0x35 },
1560 { 0x72, 0x11 },
1561 { 0x73, 0xf0 },
1562 { 0xa2, 0x02 },
1563/* { OV7670_REG_COM10, 0x0 }, */
1564
1565/* Gamma curve values */
1566 { 0x7a, 0x20 },
1567 { 0x7b, 0x10 },
1568 { 0x7c, 0x1e },
1569 { 0x7d, 0x35 },
1570 { 0x7e, 0x5a },
1571 { 0x7f, 0x69 },
1572 { 0x80, 0x76 },
1573 { 0x81, 0x80 },
1574 { 0x82, 0x88 },
1575 { 0x83, 0x8f },
1576 { 0x84, 0x96 },
1577 { 0x85, 0xa3 },
1578 { 0x86, 0xaf },
1579 { 0x87, 0xc4 },
1580 { 0x88, 0xd7 },
1581 { 0x89, 0xe8 },
1582
1583/* AGC and AEC parameters. Note we start by disabling those features,
1584 then turn them only after tweaking the values. */
1585 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1586 | OV7670_COM8_AECSTEP
1587 | OV7670_COM8_BFILT },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001588 { OV7670_REG_GAIN, 0x00 },
1589 { OV7670_REG_AECH, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001590 { OV7670_REG_COM4, 0x40 }, /* magic reserved bit */
1591 { OV7670_REG_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
1592 { OV7670_REG_BD50MAX, 0x05 },
1593 { OV7670_REG_BD60MAX, 0x07 },
1594 { OV7670_REG_AEW, 0x95 },
1595 { OV7670_REG_AEB, 0x33 },
1596 { OV7670_REG_VPT, 0xe3 },
1597 { OV7670_REG_HAECC1, 0x78 },
1598 { OV7670_REG_HAECC2, 0x68 },
1599 { 0xa1, 0x03 }, /* magic */
1600 { OV7670_REG_HAECC3, 0xd8 },
1601 { OV7670_REG_HAECC4, 0xd8 },
1602 { OV7670_REG_HAECC5, 0xf0 },
1603 { OV7670_REG_HAECC6, 0x90 },
1604 { OV7670_REG_HAECC7, 0x94 },
1605 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1606 | OV7670_COM8_AECSTEP
1607 | OV7670_COM8_BFILT
1608 | OV7670_COM8_AGC
1609 | OV7670_COM8_AEC },
1610
1611/* Almost all of these are magic "reserved" values. */
1612 { OV7670_REG_COM5, 0x61 },
1613 { OV7670_REG_COM6, 0x4b },
1614 { 0x16, 0x02 },
1615 { OV7670_REG_MVFP, 0x07 },
1616 { 0x21, 0x02 },
1617 { 0x22, 0x91 },
1618 { 0x29, 0x07 },
1619 { 0x33, 0x0b },
1620 { 0x35, 0x0b },
1621 { 0x37, 0x1d },
1622 { 0x38, 0x71 },
1623 { 0x39, 0x2a },
1624 { OV7670_REG_COM12, 0x78 },
1625 { 0x4d, 0x40 },
1626 { 0x4e, 0x20 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001627 { OV7670_REG_GFIX, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001628 { 0x6b, 0x4a },
1629 { 0x74, 0x10 },
1630 { 0x8d, 0x4f },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001631 { 0x8e, 0x00 },
1632 { 0x8f, 0x00 },
1633 { 0x90, 0x00 },
1634 { 0x91, 0x00 },
1635 { 0x96, 0x00 },
1636 { 0x9a, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001637 { 0xb0, 0x84 },
1638 { 0xb1, 0x0c },
1639 { 0xb2, 0x0e },
1640 { 0xb3, 0x82 },
1641 { 0xb8, 0x0a },
1642
1643/* More reserved magic, some of which tweaks white balance */
1644 { 0x43, 0x0a },
1645 { 0x44, 0xf0 },
1646 { 0x45, 0x34 },
1647 { 0x46, 0x58 },
1648 { 0x47, 0x28 },
1649 { 0x48, 0x3a },
1650 { 0x59, 0x88 },
1651 { 0x5a, 0x88 },
1652 { 0x5b, 0x44 },
1653 { 0x5c, 0x67 },
1654 { 0x5d, 0x49 },
1655 { 0x5e, 0x0e },
1656 { 0x6c, 0x0a },
1657 { 0x6d, 0x55 },
1658 { 0x6e, 0x11 },
1659 { 0x6f, 0x9f },
1660 /* "9e for advance AWB" */
1661 { 0x6a, 0x40 },
1662 { OV7670_REG_BLUE, 0x40 },
1663 { OV7670_REG_RED, 0x60 },
1664 { OV7670_REG_COM8, OV7670_COM8_FASTAEC
1665 | OV7670_COM8_AECSTEP
1666 | OV7670_COM8_BFILT
1667 | OV7670_COM8_AGC
1668 | OV7670_COM8_AEC
1669 | OV7670_COM8_AWB },
1670
1671/* Matrix coefficients */
1672 { 0x4f, 0x80 },
1673 { 0x50, 0x80 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001674 { 0x51, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001675 { 0x52, 0x22 },
1676 { 0x53, 0x5e },
1677 { 0x54, 0x80 },
1678 { 0x58, 0x9e },
1679
1680 { OV7670_REG_COM16, OV7670_COM16_AWBGAIN },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001681 { OV7670_REG_EDGE, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001682 { 0x75, 0x05 },
1683 { 0x76, 0xe1 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001684 { 0x4c, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001685 { 0x77, 0x01 },
1686 { OV7670_REG_COM13, OV7670_COM13_GAMMA
1687 | OV7670_COM13_UVSAT
1688 | 2}, /* was 3 */
1689 { 0x4b, 0x09 },
1690 { 0xc9, 0x60 },
1691 { OV7670_REG_COM16, 0x38 },
1692 { 0x56, 0x40 },
1693
1694 { 0x34, 0x11 },
1695 { OV7670_REG_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
1696 { 0xa4, 0x88 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001697 { 0x96, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001698 { 0x97, 0x30 },
1699 { 0x98, 0x20 },
1700 { 0x99, 0x30 },
1701 { 0x9a, 0x84 },
1702 { 0x9b, 0x29 },
1703 { 0x9c, 0x03 },
1704 { 0x9d, 0x4c },
1705 { 0x9e, 0x3f },
1706 { 0x78, 0x04 },
1707
1708/* Extra-weird stuff. Some sort of multiplexor register */
1709 { 0x79, 0x01 },
1710 { 0xc8, 0xf0 },
1711 { 0x79, 0x0f },
1712 { 0xc8, 0x00 },
1713 { 0x79, 0x10 },
1714 { 0xc8, 0x7e },
1715 { 0x79, 0x0a },
1716 { 0xc8, 0x80 },
1717 { 0x79, 0x0b },
1718 { 0xc8, 0x01 },
1719 { 0x79, 0x0c },
1720 { 0xc8, 0x0f },
1721 { 0x79, 0x0d },
1722 { 0xc8, 0x20 },
1723 { 0x79, 0x09 },
1724 { 0xc8, 0x80 },
1725 { 0x79, 0x02 },
1726 { 0xc8, 0xc0 },
1727 { 0x79, 0x03 },
1728 { 0xc8, 0x40 },
1729 { 0x79, 0x05 },
1730 { 0xc8, 0x30 },
1731 { 0x79, 0x26 },
1732};
1733
1734static const struct ov_i2c_regvals norm_8610[] = {
1735 { 0x12, 0x80 },
1736 { 0x00, 0x00 },
1737 { 0x01, 0x80 },
1738 { 0x02, 0x80 },
1739 { 0x03, 0xc0 },
1740 { 0x04, 0x30 },
1741 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
1742 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
1743 { 0x0a, 0x86 },
1744 { 0x0b, 0xb0 },
1745 { 0x0c, 0x20 },
1746 { 0x0d, 0x20 },
1747 { 0x11, 0x01 },
1748 { 0x12, 0x25 },
1749 { 0x13, 0x01 },
1750 { 0x14, 0x04 },
1751 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
1752 { 0x16, 0x03 },
1753 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
1754 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
1755 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
1756 { 0x1a, 0xf5 },
1757 { 0x1b, 0x00 },
1758 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
1759 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
1760 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
1761 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
1762 { 0x26, 0xa2 },
1763 { 0x27, 0xea },
1764 { 0x28, 0x00 },
1765 { 0x29, 0x00 },
1766 { 0x2a, 0x80 },
1767 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
1768 { 0x2c, 0xac },
1769 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
1770 { 0x2e, 0x80 },
1771 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
1772 { 0x4c, 0x00 },
1773 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
1774 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
1775 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
1776 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
1777 { 0x63, 0xff },
1778 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
1779 * maybe thats wrong */
1780 { 0x65, 0x00 },
1781 { 0x66, 0x55 },
1782 { 0x67, 0xb0 },
1783 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
1784 { 0x69, 0x02 },
1785 { 0x6a, 0x22 },
1786 { 0x6b, 0x00 },
1787 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
1788 * deleting bit7 colors the first images red */
1789 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
1790 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
1791 { 0x6f, 0x01 },
1792 { 0x70, 0x8b },
1793 { 0x71, 0x00 },
1794 { 0x72, 0x14 },
1795 { 0x73, 0x54 },
1796 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
1797 { 0x75, 0x0e },
1798 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
1799 { 0x77, 0xff },
1800 { 0x78, 0x80 },
1801 { 0x79, 0x80 },
1802 { 0x7a, 0x80 },
1803 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
1804 { 0x7c, 0x00 },
1805 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
1806 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
1807 { 0x7f, 0xfb },
1808 { 0x80, 0x28 },
1809 { 0x81, 0x00 },
1810 { 0x82, 0x23 },
1811 { 0x83, 0x0b },
1812 { 0x84, 0x00 },
1813 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
1814 { 0x86, 0xc9 },
1815 { 0x87, 0x00 },
1816 { 0x88, 0x00 },
1817 { 0x89, 0x01 },
1818 { 0x12, 0x20 },
1819 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
1820};
1821
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001822static unsigned char ov7670_abs_to_sm(unsigned char v)
1823{
1824 if (v > 127)
1825 return v & 0x7f;
1826 return (128 - v) | 0x80;
1827}
1828
1829/* Write a OV519 register */
Hans de Goedea511ba92009-10-16 07:13:07 -03001830static int reg_w(struct sd *sd, __u16 index, __u16 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001831{
Hans de Goedea511ba92009-10-16 07:13:07 -03001832 int ret, req = 0;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03001833
1834 switch (sd->bridge) {
1835 case BRIDGE_OV511:
1836 case BRIDGE_OV511PLUS:
1837 req = 2;
1838 break;
1839 case BRIDGE_OVFX2:
Hans de Goedea511ba92009-10-16 07:13:07 -03001840 req = 0x0a;
1841 /* fall through */
1842 case BRIDGE_W9968CF:
Hans de Goedeb46aaa02009-10-12 10:07:57 -03001843 ret = usb_control_msg(sd->gspca_dev.dev,
1844 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goedea511ba92009-10-16 07:13:07 -03001845 req,
Hans de Goedeb46aaa02009-10-12 10:07:57 -03001846 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Hans de Goedea511ba92009-10-16 07:13:07 -03001847 value, index, NULL, 0, 500);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03001848 goto leave;
1849 default:
1850 req = 1;
1851 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001852
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001853 sd->gspca_dev.usb_buf[0] = value;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001854 ret = usb_control_msg(sd->gspca_dev.dev,
1855 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goede49809d62009-06-07 12:10:39 -03001856 req,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001857 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1858 0, index,
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001859 sd->gspca_dev.usb_buf, 1, 500);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03001860leave:
Hans de Goedea511ba92009-10-16 07:13:07 -03001861 if (ret < 0) {
Jean-François Moine0b656322010-09-13 05:19:58 -03001862 err("Write reg 0x%04x -> [0x%02x] failed",
Hans de Goedea511ba92009-10-16 07:13:07 -03001863 value, index);
1864 return ret;
1865 }
1866
1867 PDEBUG(D_USBO, "Write reg 0x%04x -> [0x%02x]", value, index);
1868 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001869}
1870
Hans de Goedea511ba92009-10-16 07:13:07 -03001871/* Read from a OV519 register, note not valid for the w9968cf!! */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001872/* returns: negative is error, pos or zero is data */
1873static int reg_r(struct sd *sd, __u16 index)
1874{
1875 int ret;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03001876 int req;
1877
1878 switch (sd->bridge) {
1879 case BRIDGE_OV511:
1880 case BRIDGE_OV511PLUS:
1881 req = 3;
1882 break;
1883 case BRIDGE_OVFX2:
1884 req = 0x0b;
1885 break;
1886 default:
1887 req = 1;
1888 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001889
1890 ret = usb_control_msg(sd->gspca_dev.dev,
1891 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goede49809d62009-06-07 12:10:39 -03001892 req,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001893 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001894 0, index, sd->gspca_dev.usb_buf, 1, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001895
Hans de Goedea511ba92009-10-16 07:13:07 -03001896 if (ret >= 0) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001897 ret = sd->gspca_dev.usb_buf[0];
Hans de Goedea511ba92009-10-16 07:13:07 -03001898 PDEBUG(D_USBI, "Read reg [0x%02X] -> 0x%04X", index, ret);
1899 } else
Jean-François Moine0b656322010-09-13 05:19:58 -03001900 err("Read reg [0x%02x] failed", index);
Hans de Goedea511ba92009-10-16 07:13:07 -03001901
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001902 return ret;
1903}
1904
1905/* Read 8 values from a OV519 register */
1906static int reg_r8(struct sd *sd,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03001907 __u16 index)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001908{
1909 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001910
1911 ret = usb_control_msg(sd->gspca_dev.dev,
1912 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
1913 1, /* REQ_IO */
1914 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001915 0, index, sd->gspca_dev.usb_buf, 8, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001916
1917 if (ret >= 0)
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001918 ret = sd->gspca_dev.usb_buf[0];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001919 else
Jean-François Moine0b656322010-09-13 05:19:58 -03001920 err("Read reg 8 [0x%02x] failed", index);
Hans de Goedea511ba92009-10-16 07:13:07 -03001921
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001922 return ret;
1923}
1924
1925/*
1926 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
1927 * the same position as 1's in "mask" are cleared and set to "value". Bits
1928 * that are in the same position as 0's in "mask" are preserved, regardless
1929 * of their respective state in "value".
1930 */
1931static int reg_w_mask(struct sd *sd,
1932 __u16 index,
1933 __u8 value,
1934 __u8 mask)
1935{
1936 int ret;
1937 __u8 oldval;
1938
1939 if (mask != 0xff) {
1940 value &= mask; /* Enforce mask on value */
1941 ret = reg_r(sd, index);
1942 if (ret < 0)
1943 return ret;
1944
1945 oldval = ret & ~mask; /* Clear the masked bits */
1946 value |= oldval; /* Set the desired bits */
1947 }
1948 return reg_w(sd, index, value);
1949}
1950
1951/*
Hans de Goede49809d62009-06-07 12:10:39 -03001952 * Writes multiple (n) byte value to a single register. Only valid with certain
1953 * registers (0x30 and 0xc4 - 0xce).
1954 */
1955static int ov518_reg_w32(struct sd *sd, __u16 index, u32 value, int n)
1956{
1957 int ret;
1958
Jean-Francois Moine83955552009-12-12 06:58:01 -03001959 *((__le32 *) sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
Hans de Goede49809d62009-06-07 12:10:39 -03001960
1961 ret = usb_control_msg(sd->gspca_dev.dev,
1962 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
1963 1 /* REG_IO */,
1964 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
1965 0, index,
1966 sd->gspca_dev.usb_buf, n, 500);
Hans de Goedea511ba92009-10-16 07:13:07 -03001967 if (ret < 0) {
Jean-François Moine0b656322010-09-13 05:19:58 -03001968 err("Write reg32 [%02x] %08x failed", index, value);
Hans de Goedea511ba92009-10-16 07:13:07 -03001969 return ret;
1970 }
1971
1972 return 0;
Hans de Goede49809d62009-06-07 12:10:39 -03001973}
1974
Hans de Goede1876bb92009-06-14 06:45:50 -03001975static int ov511_i2c_w(struct sd *sd, __u8 reg, __u8 value)
1976{
1977 int rc, retries;
1978
1979 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
1980
1981 /* Three byte write cycle */
1982 for (retries = 6; ; ) {
1983 /* Select camera register */
1984 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
1985 if (rc < 0)
1986 return rc;
1987
1988 /* Write "value" to I2C data port of OV511 */
1989 rc = reg_w(sd, R51x_I2C_DATA, value);
1990 if (rc < 0)
1991 return rc;
1992
1993 /* Initiate 3-byte write cycle */
1994 rc = reg_w(sd, R511_I2C_CTL, 0x01);
1995 if (rc < 0)
1996 return rc;
1997
Jean-Francois Moine83955552009-12-12 06:58:01 -03001998 do {
Hans de Goede1876bb92009-06-14 06:45:50 -03001999 rc = reg_r(sd, R511_I2C_CTL);
Jean-Francois Moine83955552009-12-12 06:58:01 -03002000 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
Hans de Goede1876bb92009-06-14 06:45:50 -03002001
2002 if (rc < 0)
2003 return rc;
2004
2005 if ((rc & 2) == 0) /* Ack? */
2006 break;
2007 if (--retries < 0) {
2008 PDEBUG(D_USBO, "i2c write retries exhausted");
2009 return -1;
2010 }
2011 }
2012
2013 return 0;
2014}
2015
2016static int ov511_i2c_r(struct sd *sd, __u8 reg)
2017{
2018 int rc, value, retries;
2019
2020 /* Two byte write cycle */
2021 for (retries = 6; ; ) {
2022 /* Select camera register */
2023 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
2024 if (rc < 0)
2025 return rc;
2026
2027 /* Initiate 2-byte write cycle */
2028 rc = reg_w(sd, R511_I2C_CTL, 0x03);
2029 if (rc < 0)
2030 return rc;
2031
Jean-Francois Moine83955552009-12-12 06:58:01 -03002032 do {
Hans de Goede1876bb92009-06-14 06:45:50 -03002033 rc = reg_r(sd, R511_I2C_CTL);
Jean-Francois Moine83955552009-12-12 06:58:01 -03002034 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
Hans de Goede1876bb92009-06-14 06:45:50 -03002035
2036 if (rc < 0)
2037 return rc;
2038
2039 if ((rc & 2) == 0) /* Ack? */
2040 break;
2041
2042 /* I2C abort */
2043 reg_w(sd, R511_I2C_CTL, 0x10);
2044
2045 if (--retries < 0) {
2046 PDEBUG(D_USBI, "i2c write retries exhausted");
2047 return -1;
2048 }
2049 }
2050
2051 /* Two byte read cycle */
2052 for (retries = 6; ; ) {
2053 /* Initiate 2-byte read cycle */
2054 rc = reg_w(sd, R511_I2C_CTL, 0x05);
2055 if (rc < 0)
2056 return rc;
2057
Jean-Francois Moine83955552009-12-12 06:58:01 -03002058 do {
Hans de Goede1876bb92009-06-14 06:45:50 -03002059 rc = reg_r(sd, R511_I2C_CTL);
Jean-Francois Moine83955552009-12-12 06:58:01 -03002060 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
Hans de Goede1876bb92009-06-14 06:45:50 -03002061
2062 if (rc < 0)
2063 return rc;
2064
2065 if ((rc & 2) == 0) /* Ack? */
2066 break;
2067
2068 /* I2C abort */
2069 rc = reg_w(sd, R511_I2C_CTL, 0x10);
2070 if (rc < 0)
2071 return rc;
2072
2073 if (--retries < 0) {
2074 PDEBUG(D_USBI, "i2c read retries exhausted");
2075 return -1;
2076 }
2077 }
2078
2079 value = reg_r(sd, R51x_I2C_DATA);
2080
2081 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
2082
2083 /* This is needed to make i2c_w() work */
2084 rc = reg_w(sd, R511_I2C_CTL, 0x05);
2085 if (rc < 0)
2086 return rc;
2087
2088 return value;
2089}
Hans de Goede49809d62009-06-07 12:10:39 -03002090
2091/*
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002092 * The OV518 I2C I/O procedure is different, hence, this function.
2093 * This is normally only called from i2c_w(). Note that this function
2094 * always succeeds regardless of whether the sensor is present and working.
2095 */
Hans de Goede1876bb92009-06-14 06:45:50 -03002096static int ov518_i2c_w(struct sd *sd,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002097 __u8 reg,
2098 __u8 value)
2099{
2100 int rc;
2101
2102 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
2103
2104 /* Select camera register */
2105 rc = reg_w(sd, R51x_I2C_SADDR_3, reg);
2106 if (rc < 0)
2107 return rc;
2108
2109 /* Write "value" to I2C data port of OV511 */
2110 rc = reg_w(sd, R51x_I2C_DATA, value);
2111 if (rc < 0)
2112 return rc;
2113
2114 /* Initiate 3-byte write cycle */
2115 rc = reg_w(sd, R518_I2C_CTL, 0x01);
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002116 if (rc < 0)
2117 return rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002118
2119 /* wait for write complete */
2120 msleep(4);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002121 return reg_r8(sd, R518_I2C_CTL);
2122}
2123
2124/*
2125 * returns: negative is error, pos or zero is data
2126 *
2127 * The OV518 I2C I/O procedure is different, hence, this function.
2128 * This is normally only called from i2c_r(). Note that this function
2129 * always succeeds regardless of whether the sensor is present and working.
2130 */
Hans de Goede1876bb92009-06-14 06:45:50 -03002131static int ov518_i2c_r(struct sd *sd, __u8 reg)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002132{
2133 int rc, value;
2134
2135 /* Select camera register */
2136 rc = reg_w(sd, R51x_I2C_SADDR_2, reg);
2137 if (rc < 0)
2138 return rc;
2139
2140 /* Initiate 2-byte write cycle */
2141 rc = reg_w(sd, R518_I2C_CTL, 0x03);
2142 if (rc < 0)
2143 return rc;
2144
2145 /* Initiate 2-byte read cycle */
2146 rc = reg_w(sd, R518_I2C_CTL, 0x05);
2147 if (rc < 0)
2148 return rc;
2149 value = reg_r(sd, R51x_I2C_DATA);
2150 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, value);
2151 return value;
2152}
2153
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002154static int ovfx2_i2c_w(struct sd *sd, __u8 reg, __u8 value)
2155{
2156 int ret;
2157
2158 ret = usb_control_msg(sd->gspca_dev.dev,
2159 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2160 0x02,
2161 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2162 (__u16)value, (__u16)reg, NULL, 0, 500);
2163
Hans de Goedea511ba92009-10-16 07:13:07 -03002164 if (ret < 0) {
Jean-François Moine0b656322010-09-13 05:19:58 -03002165 err("i2c 0x%02x -> [0x%02x] failed", value, reg);
Hans de Goedea511ba92009-10-16 07:13:07 -03002166 return ret;
2167 }
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002168
Hans de Goedea511ba92009-10-16 07:13:07 -03002169 PDEBUG(D_USBO, "i2c 0x%02x -> [0x%02x]", value, reg);
2170 return 0;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002171}
2172
2173static int ovfx2_i2c_r(struct sd *sd, __u8 reg)
2174{
2175 int ret;
2176
2177 ret = usb_control_msg(sd->gspca_dev.dev,
2178 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
2179 0x03,
2180 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2181 0, (__u16)reg, sd->gspca_dev.usb_buf, 1, 500);
2182
2183 if (ret >= 0) {
2184 ret = sd->gspca_dev.usb_buf[0];
2185 PDEBUG(D_USBI, "i2c [0x%02X] -> 0x%02X", reg, ret);
2186 } else
Jean-François Moine0b656322010-09-13 05:19:58 -03002187 err("i2c read [0x%02x] failed", reg);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002188
2189 return ret;
2190}
2191
Hans de Goede1876bb92009-06-14 06:45:50 -03002192static int i2c_w(struct sd *sd, __u8 reg, __u8 value)
2193{
Hans de Goedefb1f9022009-10-16 07:42:53 -03002194 int ret = -1;
2195
2196 if (sd->sensor_reg_cache[reg] == value)
2197 return 0;
2198
Hans de Goede1876bb92009-06-14 06:45:50 -03002199 switch (sd->bridge) {
2200 case BRIDGE_OV511:
2201 case BRIDGE_OV511PLUS:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002202 ret = ov511_i2c_w(sd, reg, value);
2203 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002204 case BRIDGE_OV518:
2205 case BRIDGE_OV518PLUS:
2206 case BRIDGE_OV519:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002207 ret = ov518_i2c_w(sd, reg, value);
2208 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002209 case BRIDGE_OVFX2:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002210 ret = ovfx2_i2c_w(sd, reg, value);
2211 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03002212 case BRIDGE_W9968CF:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002213 ret = w9968cf_i2c_w(sd, reg, value);
2214 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002215 }
Hans de Goedefb1f9022009-10-16 07:42:53 -03002216
2217 if (ret >= 0) {
2218 /* Up on sensor reset empty the register cache */
2219 if (reg == 0x12 && (value & 0x80))
2220 memset(sd->sensor_reg_cache, -1,
2221 sizeof(sd->sensor_reg_cache));
2222 else
2223 sd->sensor_reg_cache[reg] = value;
2224 }
2225
2226 return ret;
Hans de Goede1876bb92009-06-14 06:45:50 -03002227}
2228
2229static int i2c_r(struct sd *sd, __u8 reg)
2230{
Hans de Goede8394bcf2009-10-16 11:26:22 -03002231 int ret = -1;
Hans de Goedefb1f9022009-10-16 07:42:53 -03002232
2233 if (sd->sensor_reg_cache[reg] != -1)
2234 return sd->sensor_reg_cache[reg];
2235
Hans de Goede1876bb92009-06-14 06:45:50 -03002236 switch (sd->bridge) {
2237 case BRIDGE_OV511:
2238 case BRIDGE_OV511PLUS:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002239 ret = ov511_i2c_r(sd, reg);
2240 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002241 case BRIDGE_OV518:
2242 case BRIDGE_OV518PLUS:
2243 case BRIDGE_OV519:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002244 ret = ov518_i2c_r(sd, reg);
2245 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002246 case BRIDGE_OVFX2:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002247 ret = ovfx2_i2c_r(sd, reg);
2248 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03002249 case BRIDGE_W9968CF:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002250 ret = w9968cf_i2c_r(sd, reg);
2251 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002252 }
Hans de Goedefb1f9022009-10-16 07:42:53 -03002253
2254 if (ret >= 0)
2255 sd->sensor_reg_cache[reg] = ret;
2256
2257 return ret;
Hans de Goede1876bb92009-06-14 06:45:50 -03002258}
2259
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002260/* Writes bits at positions specified by mask to an I2C reg. Bits that are in
2261 * the same position as 1's in "mask" are cleared and set to "value". Bits
2262 * that are in the same position as 0's in "mask" are preserved, regardless
2263 * of their respective state in "value".
2264 */
2265static int i2c_w_mask(struct sd *sd,
2266 __u8 reg,
2267 __u8 value,
2268 __u8 mask)
2269{
2270 int rc;
2271 __u8 oldval;
2272
2273 value &= mask; /* Enforce mask on value */
2274 rc = i2c_r(sd, reg);
2275 if (rc < 0)
2276 return rc;
2277 oldval = rc & ~mask; /* Clear the masked bits */
2278 value |= oldval; /* Set the desired bits */
2279 return i2c_w(sd, reg, value);
2280}
2281
2282/* Temporarily stops OV511 from functioning. Must do this before changing
2283 * registers while the camera is streaming */
2284static inline int ov51x_stop(struct sd *sd)
2285{
2286 PDEBUG(D_STREAM, "stopping");
2287 sd->stopped = 1;
Hans de Goede49809d62009-06-07 12:10:39 -03002288 switch (sd->bridge) {
2289 case BRIDGE_OV511:
2290 case BRIDGE_OV511PLUS:
2291 return reg_w(sd, R51x_SYS_RESET, 0x3d);
2292 case BRIDGE_OV518:
2293 case BRIDGE_OV518PLUS:
2294 return reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
2295 case BRIDGE_OV519:
2296 return reg_w(sd, OV519_SYS_RESET1, 0x0f);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002297 case BRIDGE_OVFX2:
2298 return reg_w_mask(sd, 0x0f, 0x00, 0x02);
Hans de Goedea511ba92009-10-16 07:13:07 -03002299 case BRIDGE_W9968CF:
Hans de Goede79b35902009-10-19 06:08:01 -03002300 return reg_w(sd, 0x3c, 0x0a05); /* stop USB transfer */
Hans de Goede49809d62009-06-07 12:10:39 -03002301 }
2302
2303 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002304}
2305
2306/* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
2307 * actually stopped (for performance). */
2308static inline int ov51x_restart(struct sd *sd)
2309{
Hans de Goede49809d62009-06-07 12:10:39 -03002310 int rc;
2311
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002312 PDEBUG(D_STREAM, "restarting");
2313 if (!sd->stopped)
2314 return 0;
2315 sd->stopped = 0;
2316
2317 /* Reinitialize the stream */
Hans de Goede49809d62009-06-07 12:10:39 -03002318 switch (sd->bridge) {
2319 case BRIDGE_OV511:
2320 case BRIDGE_OV511PLUS:
2321 return reg_w(sd, R51x_SYS_RESET, 0x00);
2322 case BRIDGE_OV518:
2323 case BRIDGE_OV518PLUS:
2324 rc = reg_w(sd, 0x2f, 0x80);
2325 if (rc < 0)
2326 return rc;
2327 return reg_w(sd, R51x_SYS_RESET, 0x00);
2328 case BRIDGE_OV519:
2329 return reg_w(sd, OV519_SYS_RESET1, 0x00);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002330 case BRIDGE_OVFX2:
2331 return reg_w_mask(sd, 0x0f, 0x02, 0x02);
Hans de Goedea511ba92009-10-16 07:13:07 -03002332 case BRIDGE_W9968CF:
2333 return reg_w(sd, 0x3c, 0x8a05); /* USB FIFO enable */
Hans de Goede49809d62009-06-07 12:10:39 -03002334 }
2335
2336 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002337}
2338
Hans de Goede229bb7d2009-10-11 07:41:46 -03002339static int ov51x_set_slave_ids(struct sd *sd, __u8 slave);
2340
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002341/* This does an initial reset of an OmniVision sensor and ensures that I2C
2342 * is synchronized. Returns <0 on failure.
2343 */
Hans de Goede229bb7d2009-10-11 07:41:46 -03002344static int init_ov_sensor(struct sd *sd, __u8 slave)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002345{
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002346 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002347
Hans de Goede229bb7d2009-10-11 07:41:46 -03002348 if (ov51x_set_slave_ids(sd, slave) < 0)
2349 return -EIO;
2350
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002351 /* Reset the sensor */
2352 if (i2c_w(sd, 0x12, 0x80) < 0)
2353 return -EIO;
2354
2355 /* Wait for it to initialize */
2356 msleep(150);
2357
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002358 for (i = 0; i < i2c_detect_tries; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002359 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
2360 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002361 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
2362 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002363 }
2364
2365 /* Reset the sensor */
2366 if (i2c_w(sd, 0x12, 0x80) < 0)
2367 return -EIO;
2368 /* Wait for it to initialize */
2369 msleep(150);
2370 /* Dummy read to sync I2C */
2371 if (i2c_r(sd, 0x00) < 0)
2372 return -EIO;
2373 }
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002374 return -EIO;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002375}
2376
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002377/* Set the read and write slave IDs. The "slave" argument is the write slave,
2378 * and the read slave will be set to (slave + 1).
2379 * This should not be called from outside the i2c I/O functions.
2380 * Sets I2C read and write slave IDs. Returns <0 for error
2381 */
2382static int ov51x_set_slave_ids(struct sd *sd,
2383 __u8 slave)
2384{
2385 int rc;
2386
Hans de Goedea511ba92009-10-16 07:13:07 -03002387 switch (sd->bridge) {
2388 case BRIDGE_OVFX2:
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002389 return reg_w(sd, OVFX2_I2C_ADDR, slave);
Hans de Goedea511ba92009-10-16 07:13:07 -03002390 case BRIDGE_W9968CF:
2391 sd->sensor_addr = slave;
2392 return 0;
2393 }
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002394
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002395 rc = reg_w(sd, R51x_I2C_W_SID, slave);
2396 if (rc < 0)
2397 return rc;
2398 return reg_w(sd, R51x_I2C_R_SID, slave + 1);
2399}
2400
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002401static int write_regvals(struct sd *sd,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002402 const struct ov_regvals *regvals,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002403 int n)
2404{
2405 int rc;
2406
2407 while (--n >= 0) {
2408 rc = reg_w(sd, regvals->reg, regvals->val);
2409 if (rc < 0)
2410 return rc;
2411 regvals++;
2412 }
2413 return 0;
2414}
2415
2416static int write_i2c_regvals(struct sd *sd,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002417 const struct ov_i2c_regvals *regvals,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002418 int n)
2419{
2420 int rc;
2421
2422 while (--n >= 0) {
2423 rc = i2c_w(sd, regvals->reg, regvals->val);
2424 if (rc < 0)
2425 return rc;
2426 regvals++;
2427 }
2428 return 0;
2429}
2430
2431/****************************************************************************
2432 *
2433 * OV511 and sensor configuration
2434 *
2435 ***************************************************************************/
2436
Hans de Goede635118d2009-10-11 09:49:03 -03002437/* This initializes the OV2x10 / OV3610 / OV3620 */
2438static int ov_hires_configure(struct sd *sd)
2439{
2440 int high, low;
2441
2442 if (sd->bridge != BRIDGE_OVFX2) {
Jean-François Moine0b656322010-09-13 05:19:58 -03002443 err("error hires sensors only supported with ovfx2");
Hans de Goede635118d2009-10-11 09:49:03 -03002444 return -1;
2445 }
2446
2447 PDEBUG(D_PROBE, "starting ov hires configuration");
2448
2449 /* Detect sensor (sub)type */
2450 high = i2c_r(sd, 0x0a);
2451 low = i2c_r(sd, 0x0b);
2452 /* info("%x, %x", high, low); */
2453 if (high == 0x96 && low == 0x40) {
2454 PDEBUG(D_PROBE, "Sensor is an OV2610");
2455 sd->sensor = SEN_OV2610;
2456 } else if (high == 0x36 && (low & 0x0f) == 0x00) {
2457 PDEBUG(D_PROBE, "Sensor is an OV3610");
2458 sd->sensor = SEN_OV3610;
2459 } else {
Jean-François Moine0b656322010-09-13 05:19:58 -03002460 err("Error unknown sensor type: 0x%02x%02x",
Hans de Goede635118d2009-10-11 09:49:03 -03002461 high, low);
2462 return -1;
2463 }
2464
2465 /* Set sensor-specific vars */
2466 return 0;
2467}
2468
2469
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002470/* This initializes the OV8110, OV8610 sensor. The OV8110 uses
2471 * the same register settings as the OV8610, since they are very similar.
2472 */
2473static int ov8xx0_configure(struct sd *sd)
2474{
2475 int rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002476
2477 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
2478
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002479 /* Detect sensor (sub)type */
2480 rc = i2c_r(sd, OV7610_REG_COM_I);
2481 if (rc < 0) {
2482 PDEBUG(D_ERR, "Error detecting sensor type");
2483 return -1;
2484 }
2485 if ((rc & 3) == 1) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002486 sd->sensor = SEN_OV8610;
2487 } else {
Jean-François Moine0b656322010-09-13 05:19:58 -03002488 err("Unknown image sensor version: %d", rc & 3);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002489 return -1;
2490 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002491
2492 /* Set sensor-specific vars */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002493 return 0;
2494}
2495
2496/* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
2497 * the same register settings as the OV7610, since they are very similar.
2498 */
2499static int ov7xx0_configure(struct sd *sd)
2500{
2501 int rc, high, low;
2502
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002503
2504 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
2505
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002506 /* Detect sensor (sub)type */
2507 rc = i2c_r(sd, OV7610_REG_COM_I);
2508
2509 /* add OV7670 here
2510 * it appears to be wrongly detected as a 7610 by default */
2511 if (rc < 0) {
2512 PDEBUG(D_ERR, "Error detecting sensor type");
2513 return -1;
2514 }
2515 if ((rc & 3) == 3) {
2516 /* quick hack to make OV7670s work */
2517 high = i2c_r(sd, 0x0a);
2518 low = i2c_r(sd, 0x0b);
2519 /* info("%x, %x", high, low); */
2520 if (high == 0x76 && low == 0x73) {
2521 PDEBUG(D_PROBE, "Sensor is an OV7670");
2522 sd->sensor = SEN_OV7670;
2523 } else {
2524 PDEBUG(D_PROBE, "Sensor is an OV7610");
2525 sd->sensor = SEN_OV7610;
2526 }
2527 } else if ((rc & 3) == 1) {
2528 /* I don't know what's different about the 76BE yet. */
Hans de Goedeb282d872009-06-14 19:10:40 -03002529 if (i2c_r(sd, 0x15) & 1) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002530 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
Hans de Goede859cc472010-01-07 15:42:35 -03002531 sd->sensor = SEN_OV7620AE;
Hans de Goedeb282d872009-06-14 19:10:40 -03002532 } else {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002533 PDEBUG(D_PROBE, "Sensor is an OV76BE");
Hans de Goedeb282d872009-06-14 19:10:40 -03002534 sd->sensor = SEN_OV76BE;
2535 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002536 } else if ((rc & 3) == 0) {
2537 /* try to read product id registers */
2538 high = i2c_r(sd, 0x0a);
2539 if (high < 0) {
2540 PDEBUG(D_ERR, "Error detecting camera chip PID");
2541 return high;
2542 }
2543 low = i2c_r(sd, 0x0b);
2544 if (low < 0) {
2545 PDEBUG(D_ERR, "Error detecting camera chip VER");
2546 return low;
2547 }
2548 if (high == 0x76) {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002549 switch (low) {
2550 case 0x30:
Jean-François Moine0b656322010-09-13 05:19:58 -03002551 err("Sensor is an OV7630/OV7635");
2552 err("7630 is not supported by this driver");
Jean-Francois Moine4202f712008-09-03 17:12:15 -03002553 return -1;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002554 case 0x40:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002555 PDEBUG(D_PROBE, "Sensor is an OV7645");
2556 sd->sensor = SEN_OV7640; /* FIXME */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002557 break;
2558 case 0x45:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002559 PDEBUG(D_PROBE, "Sensor is an OV7645B");
2560 sd->sensor = SEN_OV7640; /* FIXME */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002561 break;
2562 case 0x48:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002563 PDEBUG(D_PROBE, "Sensor is an OV7648");
Hans de Goede035d3a32010-01-09 08:14:43 -03002564 sd->sensor = SEN_OV7648;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002565 break;
2566 default:
2567 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002568 return -1;
2569 }
2570 } else {
2571 PDEBUG(D_PROBE, "Sensor is an OV7620");
2572 sd->sensor = SEN_OV7620;
2573 }
2574 } else {
Jean-François Moine0b656322010-09-13 05:19:58 -03002575 err("Unknown image sensor version: %d", rc & 3);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002576 return -1;
2577 }
2578
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002579 /* Set sensor-specific vars */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002580 return 0;
2581}
2582
2583/* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
2584static int ov6xx0_configure(struct sd *sd)
2585{
2586 int rc;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03002587 PDEBUG(D_PROBE, "starting OV6xx0 configuration");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002588
2589 /* Detect sensor (sub)type */
2590 rc = i2c_r(sd, OV7610_REG_COM_I);
2591 if (rc < 0) {
2592 PDEBUG(D_ERR, "Error detecting sensor type");
2593 return -1;
2594 }
2595
2596 /* Ugh. The first two bits are the version bits, but
2597 * the entire register value must be used. I guess OVT
2598 * underestimated how many variants they would make. */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002599 switch (rc) {
2600 case 0x00:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002601 sd->sensor = SEN_OV6630;
Jean-François Moine0b656322010-09-13 05:19:58 -03002602 warn("WARNING: Sensor is an OV66308. Your camera may have");
2603 warn("been misdetected in previous driver versions.");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002604 break;
2605 case 0x01:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002606 sd->sensor = SEN_OV6620;
Hans de Goede7d971372009-06-14 05:28:17 -03002607 PDEBUG(D_PROBE, "Sensor is an OV6620");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002608 break;
2609 case 0x02:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002610 sd->sensor = SEN_OV6630;
2611 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002612 break;
2613 case 0x03:
Hans de Goede7d971372009-06-14 05:28:17 -03002614 sd->sensor = SEN_OV66308AF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002615 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002616 break;
2617 case 0x90:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002618 sd->sensor = SEN_OV6630;
Jean-François Moine0b656322010-09-13 05:19:58 -03002619 warn("WARNING: Sensor is an OV66307. Your camera may have");
2620 warn("been misdetected in previous driver versions.");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002621 break;
2622 default:
Jean-François Moine0b656322010-09-13 05:19:58 -03002623 err("FATAL: Unknown sensor version: 0x%02x", rc);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002624 return -1;
2625 }
2626
2627 /* Set sensor-specific vars */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002628 sd->sif = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002629
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002630 return 0;
2631}
2632
2633/* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
2634static void ov51x_led_control(struct sd *sd, int on)
2635{
Hans de Goede9e4d8252009-06-14 06:25:06 -03002636 if (sd->invert_led)
2637 on = !on;
2638
Hans de Goede49809d62009-06-07 12:10:39 -03002639 switch (sd->bridge) {
2640 /* OV511 has no LED control */
2641 case BRIDGE_OV511PLUS:
2642 reg_w(sd, R511_SYS_LED_CTL, on ? 1 : 0);
2643 break;
2644 case BRIDGE_OV518:
2645 case BRIDGE_OV518PLUS:
2646 reg_w_mask(sd, R518_GPIO_OUT, on ? 0x02 : 0x00, 0x02);
2647 break;
2648 case BRIDGE_OV519:
2649 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, !on, 1); /* 0 / 1 */
2650 break;
2651 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002652}
2653
Hans de Goede417a4d22010-02-19 07:37:08 -03002654static void sd_reset_snapshot(struct gspca_dev *gspca_dev)
2655{
2656 struct sd *sd = (struct sd *) gspca_dev;
2657
2658 if (!sd->snapshot_needs_reset)
2659 return;
2660
2661 /* Note it is important that we clear sd->snapshot_needs_reset,
2662 before actually clearing the snapshot state in the bridge
2663 otherwise we might race with the pkt_scan interrupt handler */
2664 sd->snapshot_needs_reset = 0;
2665
2666 switch (sd->bridge) {
Hans de Goede88e8d202010-02-20 04:45:49 -03002667 case BRIDGE_OV511:
2668 case BRIDGE_OV511PLUS:
2669 reg_w(sd, R51x_SYS_SNAP, 0x02);
2670 reg_w(sd, R51x_SYS_SNAP, 0x00);
2671 break;
Hans de Goede92e232a2010-02-20 04:30:45 -03002672 case BRIDGE_OV518:
2673 case BRIDGE_OV518PLUS:
2674 reg_w(sd, R51x_SYS_SNAP, 0x02); /* Reset */
2675 reg_w(sd, R51x_SYS_SNAP, 0x01); /* Enable */
2676 break;
Hans de Goede417a4d22010-02-19 07:37:08 -03002677 case BRIDGE_OV519:
2678 reg_w(sd, R51x_SYS_RESET, 0x40);
2679 reg_w(sd, R51x_SYS_RESET, 0x00);
2680 break;
2681 }
2682}
2683
Hans de Goede1876bb92009-06-14 06:45:50 -03002684static int ov51x_upload_quan_tables(struct sd *sd)
Hans de Goede49809d62009-06-07 12:10:39 -03002685{
Hans de Goede1876bb92009-06-14 06:45:50 -03002686 const unsigned char yQuanTable511[] = {
2687 0, 1, 1, 2, 2, 3, 3, 4,
2688 1, 1, 1, 2, 2, 3, 4, 4,
2689 1, 1, 2, 2, 3, 4, 4, 4,
2690 2, 2, 2, 3, 4, 4, 4, 4,
2691 2, 2, 3, 4, 4, 5, 5, 5,
2692 3, 3, 4, 4, 5, 5, 5, 5,
2693 3, 4, 4, 4, 5, 5, 5, 5,
2694 4, 4, 4, 4, 5, 5, 5, 5
2695 };
2696
2697 const unsigned char uvQuanTable511[] = {
2698 0, 2, 2, 3, 4, 4, 4, 4,
2699 2, 2, 2, 4, 4, 4, 4, 4,
2700 2, 2, 3, 4, 4, 4, 4, 4,
2701 3, 4, 4, 4, 4, 4, 4, 4,
2702 4, 4, 4, 4, 4, 4, 4, 4,
2703 4, 4, 4, 4, 4, 4, 4, 4,
2704 4, 4, 4, 4, 4, 4, 4, 4,
2705 4, 4, 4, 4, 4, 4, 4, 4
2706 };
2707
2708 /* OV518 quantization tables are 8x4 (instead of 8x8) */
Hans de Goede49809d62009-06-07 12:10:39 -03002709 const unsigned char yQuanTable518[] = {
2710 5, 4, 5, 6, 6, 7, 7, 7,
2711 5, 5, 5, 5, 6, 7, 7, 7,
2712 6, 6, 6, 6, 7, 7, 7, 8,
2713 7, 7, 6, 7, 7, 7, 8, 8
2714 };
2715
2716 const unsigned char uvQuanTable518[] = {
2717 6, 6, 6, 7, 7, 7, 7, 7,
2718 6, 6, 6, 7, 7, 7, 7, 7,
2719 6, 6, 6, 7, 7, 7, 7, 8,
2720 7, 7, 7, 7, 7, 7, 8, 8
2721 };
2722
Hans de Goede1876bb92009-06-14 06:45:50 -03002723 const unsigned char *pYTable, *pUVTable;
Hans de Goede49809d62009-06-07 12:10:39 -03002724 unsigned char val0, val1;
Hans de Goede1876bb92009-06-14 06:45:50 -03002725 int i, size, rc, reg = R51x_COMP_LUT_BEGIN;
Hans de Goede49809d62009-06-07 12:10:39 -03002726
2727 PDEBUG(D_PROBE, "Uploading quantization tables");
2728
Hans de Goede1876bb92009-06-14 06:45:50 -03002729 if (sd->bridge == BRIDGE_OV511 || sd->bridge == BRIDGE_OV511PLUS) {
2730 pYTable = yQuanTable511;
2731 pUVTable = uvQuanTable511;
2732 size = 32;
2733 } else {
2734 pYTable = yQuanTable518;
2735 pUVTable = uvQuanTable518;
2736 size = 16;
2737 }
2738
2739 for (i = 0; i < size; i++) {
Hans de Goede49809d62009-06-07 12:10:39 -03002740 val0 = *pYTable++;
2741 val1 = *pYTable++;
2742 val0 &= 0x0f;
2743 val1 &= 0x0f;
2744 val0 |= val1 << 4;
2745 rc = reg_w(sd, reg, val0);
2746 if (rc < 0)
2747 return rc;
2748
2749 val0 = *pUVTable++;
2750 val1 = *pUVTable++;
2751 val0 &= 0x0f;
2752 val1 &= 0x0f;
2753 val0 |= val1 << 4;
Hans de Goede1876bb92009-06-14 06:45:50 -03002754 rc = reg_w(sd, reg + size, val0);
Hans de Goede49809d62009-06-07 12:10:39 -03002755 if (rc < 0)
2756 return rc;
2757
2758 reg++;
2759 }
2760
2761 return 0;
2762}
2763
Hans de Goede1876bb92009-06-14 06:45:50 -03002764/* This initializes the OV511/OV511+ and the sensor */
2765static int ov511_configure(struct gspca_dev *gspca_dev)
2766{
2767 struct sd *sd = (struct sd *) gspca_dev;
2768 int rc;
2769
2770 /* For 511 and 511+ */
2771 const struct ov_regvals init_511[] = {
2772 { R51x_SYS_RESET, 0x7f },
2773 { R51x_SYS_INIT, 0x01 },
2774 { R51x_SYS_RESET, 0x7f },
2775 { R51x_SYS_INIT, 0x01 },
2776 { R51x_SYS_RESET, 0x3f },
2777 { R51x_SYS_INIT, 0x01 },
2778 { R51x_SYS_RESET, 0x3d },
2779 };
2780
2781 const struct ov_regvals norm_511[] = {
2782 { R511_DRAM_FLOW_CTL, 0x01 },
2783 { R51x_SYS_SNAP, 0x00 },
2784 { R51x_SYS_SNAP, 0x02 },
2785 { R51x_SYS_SNAP, 0x00 },
2786 { R511_FIFO_OPTS, 0x1f },
2787 { R511_COMP_EN, 0x00 },
2788 { R511_COMP_LUT_EN, 0x03 },
2789 };
2790
2791 const struct ov_regvals norm_511_p[] = {
2792 { R511_DRAM_FLOW_CTL, 0xff },
2793 { R51x_SYS_SNAP, 0x00 },
2794 { R51x_SYS_SNAP, 0x02 },
2795 { R51x_SYS_SNAP, 0x00 },
2796 { R511_FIFO_OPTS, 0xff },
2797 { R511_COMP_EN, 0x00 },
2798 { R511_COMP_LUT_EN, 0x03 },
2799 };
2800
2801 const struct ov_regvals compress_511[] = {
2802 { 0x70, 0x1f },
2803 { 0x71, 0x05 },
2804 { 0x72, 0x06 },
2805 { 0x73, 0x06 },
2806 { 0x74, 0x14 },
2807 { 0x75, 0x03 },
2808 { 0x76, 0x04 },
2809 { 0x77, 0x04 },
2810 };
2811
2812 PDEBUG(D_PROBE, "Device custom id %x", reg_r(sd, R51x_SYS_CUST_ID));
2813
2814 rc = write_regvals(sd, init_511, ARRAY_SIZE(init_511));
2815 if (rc < 0)
2816 return rc;
2817
2818 switch (sd->bridge) {
2819 case BRIDGE_OV511:
2820 rc = write_regvals(sd, norm_511, ARRAY_SIZE(norm_511));
2821 if (rc < 0)
2822 return rc;
2823 break;
2824 case BRIDGE_OV511PLUS:
2825 rc = write_regvals(sd, norm_511_p, ARRAY_SIZE(norm_511_p));
2826 if (rc < 0)
2827 return rc;
2828 break;
2829 }
2830
2831 /* Init compression */
2832 rc = write_regvals(sd, compress_511, ARRAY_SIZE(compress_511));
2833 if (rc < 0)
2834 return rc;
2835
2836 rc = ov51x_upload_quan_tables(sd);
2837 if (rc < 0) {
2838 PDEBUG(D_ERR, "Error uploading quantization tables");
2839 return rc;
2840 }
2841
2842 return 0;
2843}
2844
Hans de Goede49809d62009-06-07 12:10:39 -03002845/* This initializes the OV518/OV518+ and the sensor */
2846static int ov518_configure(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002847{
2848 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede49809d62009-06-07 12:10:39 -03002849 int rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002850
Hans de Goede49809d62009-06-07 12:10:39 -03002851 /* For 518 and 518+ */
Hans de Goedee080fcd2009-06-18 05:03:16 -03002852 const struct ov_regvals init_518[] = {
Hans de Goede49809d62009-06-07 12:10:39 -03002853 { R51x_SYS_RESET, 0x40 },
2854 { R51x_SYS_INIT, 0xe1 },
2855 { R51x_SYS_RESET, 0x3e },
2856 { R51x_SYS_INIT, 0xe1 },
2857 { R51x_SYS_RESET, 0x00 },
2858 { R51x_SYS_INIT, 0xe1 },
2859 { 0x46, 0x00 },
2860 { 0x5d, 0x03 },
2861 };
2862
Hans de Goedee080fcd2009-06-18 05:03:16 -03002863 const struct ov_regvals norm_518[] = {
Hans de Goede49809d62009-06-07 12:10:39 -03002864 { R51x_SYS_SNAP, 0x02 }, /* Reset */
2865 { R51x_SYS_SNAP, 0x01 }, /* Enable */
2866 { 0x31, 0x0f },
2867 { 0x5d, 0x03 },
2868 { 0x24, 0x9f },
2869 { 0x25, 0x90 },
2870 { 0x20, 0x00 },
2871 { 0x51, 0x04 },
2872 { 0x71, 0x19 },
2873 { 0x2f, 0x80 },
2874 };
2875
Hans de Goedee080fcd2009-06-18 05:03:16 -03002876 const struct ov_regvals norm_518_p[] = {
Hans de Goede49809d62009-06-07 12:10:39 -03002877 { R51x_SYS_SNAP, 0x02 }, /* Reset */
2878 { R51x_SYS_SNAP, 0x01 }, /* Enable */
2879 { 0x31, 0x0f },
2880 { 0x5d, 0x03 },
2881 { 0x24, 0x9f },
2882 { 0x25, 0x90 },
2883 { 0x20, 0x60 },
2884 { 0x51, 0x02 },
2885 { 0x71, 0x19 },
2886 { 0x40, 0xff },
2887 { 0x41, 0x42 },
2888 { 0x46, 0x00 },
2889 { 0x33, 0x04 },
2890 { 0x21, 0x19 },
2891 { 0x3f, 0x10 },
2892 { 0x2f, 0x80 },
2893 };
2894
2895 /* First 5 bits of custom ID reg are a revision ID on OV518 */
2896 PDEBUG(D_PROBE, "Device revision %d",
2897 0x1F & reg_r(sd, R51x_SYS_CUST_ID));
2898
2899 rc = write_regvals(sd, init_518, ARRAY_SIZE(init_518));
2900 if (rc < 0)
2901 return rc;
2902
2903 /* Set LED GPIO pin to output mode */
2904 rc = reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
2905 if (rc < 0)
2906 return rc;
2907
2908 switch (sd->bridge) {
2909 case BRIDGE_OV518:
2910 rc = write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
2911 if (rc < 0)
2912 return rc;
2913 break;
2914 case BRIDGE_OV518PLUS:
2915 rc = write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
2916 if (rc < 0)
2917 return rc;
2918 break;
2919 }
2920
Hans de Goede1876bb92009-06-14 06:45:50 -03002921 rc = ov51x_upload_quan_tables(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03002922 if (rc < 0) {
2923 PDEBUG(D_ERR, "Error uploading quantization tables");
2924 return rc;
2925 }
2926
2927 rc = reg_w(sd, 0x2f, 0x80);
2928 if (rc < 0)
2929 return rc;
2930
2931 return 0;
2932}
2933
2934static int ov519_configure(struct sd *sd)
2935{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002936 static const struct ov_regvals init_519[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002937 { 0x5a, 0x6d }, /* EnableSystem */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002938 { 0x53, 0x9b },
2939 { 0x54, 0xff }, /* set bit2 to enable jpeg */
2940 { 0x5d, 0x03 },
2941 { 0x49, 0x01 },
2942 { 0x48, 0x00 },
2943 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
2944 * detection will fail. This deserves further investigation. */
2945 { OV519_GPIO_IO_CTRL0, 0xee },
2946 { 0x51, 0x0f }, /* SetUsbInit */
2947 { 0x51, 0x00 },
2948 { 0x22, 0x00 },
2949 /* windows reads 0x55 at this point*/
2950 };
2951
Hans de Goede49809d62009-06-07 12:10:39 -03002952 return write_regvals(sd, init_519, ARRAY_SIZE(init_519));
2953}
2954
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002955static int ovfx2_configure(struct sd *sd)
2956{
2957 static const struct ov_regvals init_fx2[] = {
2958 { 0x00, 0x60 },
2959 { 0x02, 0x01 },
2960 { 0x0f, 0x1d },
2961 { 0xe9, 0x82 },
2962 { 0xea, 0xc7 },
2963 { 0xeb, 0x10 },
2964 { 0xec, 0xf6 },
2965 };
2966
2967 sd->stopped = 1;
2968
2969 return write_regvals(sd, init_fx2, ARRAY_SIZE(init_fx2));
2970}
2971
Hans de Goede49809d62009-06-07 12:10:39 -03002972/* this function is called at probe time */
2973static int sd_config(struct gspca_dev *gspca_dev,
2974 const struct usb_device_id *id)
2975{
2976 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002977 struct cam *cam = &gspca_dev->cam;
Hans de Goede49809d62009-06-07 12:10:39 -03002978 int ret = 0;
2979
Hans de Goede9e4d8252009-06-14 06:25:06 -03002980 sd->bridge = id->driver_info & BRIDGE_MASK;
2981 sd->invert_led = id->driver_info & BRIDGE_INVERT_LED;
Hans de Goede49809d62009-06-07 12:10:39 -03002982
2983 switch (sd->bridge) {
Hans de Goede1876bb92009-06-14 06:45:50 -03002984 case BRIDGE_OV511:
2985 case BRIDGE_OV511PLUS:
2986 ret = ov511_configure(gspca_dev);
2987 break;
Hans de Goede49809d62009-06-07 12:10:39 -03002988 case BRIDGE_OV518:
2989 case BRIDGE_OV518PLUS:
2990 ret = ov518_configure(gspca_dev);
2991 break;
2992 case BRIDGE_OV519:
2993 ret = ov519_configure(sd);
2994 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002995 case BRIDGE_OVFX2:
2996 ret = ovfx2_configure(sd);
2997 cam->bulk_size = OVFX2_BULK_SIZE;
2998 cam->bulk_nurbs = MAX_NURBS;
2999 cam->bulk = 1;
3000 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03003001 case BRIDGE_W9968CF:
3002 ret = w9968cf_configure(sd);
3003 cam->reverse_alts = 1;
3004 break;
Hans de Goede49809d62009-06-07 12:10:39 -03003005 }
3006
3007 if (ret)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003008 goto error;
Hans de Goede49809d62009-06-07 12:10:39 -03003009
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003010 ov51x_led_control(sd, 0); /* turn LED off */
3011
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003012 /* The OV519 must be more aggressive about sensor detection since
3013 * I2C write will never fail if the sensor is not present. We have
3014 * to try to initialize the sensor to detect its presence */
Hans de Goede229bb7d2009-10-11 07:41:46 -03003015
3016 /* Test for 76xx */
3017 if (init_ov_sensor(sd, OV7xx0_SID) >= 0) {
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003018 if (ov7xx0_configure(sd) < 0) {
3019 PDEBUG(D_ERR, "Failed to configure OV7xx0");
3020 goto error;
3021 }
Hans de Goede229bb7d2009-10-11 07:41:46 -03003022 /* Test for 6xx0 */
3023 } else if (init_ov_sensor(sd, OV6xx0_SID) >= 0) {
3024 if (ov6xx0_configure(sd) < 0) {
3025 PDEBUG(D_ERR, "Failed to configure OV6xx0");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003026 goto error;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003027 }
Hans de Goede229bb7d2009-10-11 07:41:46 -03003028 /* Test for 8xx0 */
3029 } else if (init_ov_sensor(sd, OV8xx0_SID) >= 0) {
3030 if (ov8xx0_configure(sd) < 0) {
3031 PDEBUG(D_ERR, "Failed to configure OV8xx0");
3032 goto error;
3033 }
Hans de Goede635118d2009-10-11 09:49:03 -03003034 /* Test for 3xxx / 2xxx */
3035 } else if (init_ov_sensor(sd, OV_HIRES_SID) >= 0) {
3036 if (ov_hires_configure(sd) < 0) {
3037 PDEBUG(D_ERR, "Failed to configure high res OV");
3038 goto error;
3039 }
Hans de Goede229bb7d2009-10-11 07:41:46 -03003040 } else {
Jean-François Moine0b656322010-09-13 05:19:58 -03003041 err("Can't determine sensor slave IDs");
Hans de Goede229bb7d2009-10-11 07:41:46 -03003042 goto error;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003043 }
3044
Hans de Goede49809d62009-06-07 12:10:39 -03003045 switch (sd->bridge) {
Hans de Goede1876bb92009-06-14 06:45:50 -03003046 case BRIDGE_OV511:
3047 case BRIDGE_OV511PLUS:
3048 if (!sd->sif) {
3049 cam->cam_mode = ov511_vga_mode;
3050 cam->nmodes = ARRAY_SIZE(ov511_vga_mode);
3051 } else {
3052 cam->cam_mode = ov511_sif_mode;
3053 cam->nmodes = ARRAY_SIZE(ov511_sif_mode);
3054 }
3055 break;
Hans de Goede49809d62009-06-07 12:10:39 -03003056 case BRIDGE_OV518:
3057 case BRIDGE_OV518PLUS:
3058 if (!sd->sif) {
3059 cam->cam_mode = ov518_vga_mode;
3060 cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
3061 } else {
3062 cam->cam_mode = ov518_sif_mode;
3063 cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
3064 }
3065 break;
3066 case BRIDGE_OV519:
3067 if (!sd->sif) {
3068 cam->cam_mode = ov519_vga_mode;
3069 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3070 } else {
3071 cam->cam_mode = ov519_sif_mode;
3072 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3073 }
3074 break;
Hans de Goede635118d2009-10-11 09:49:03 -03003075 case BRIDGE_OVFX2:
3076 if (sd->sensor == SEN_OV2610) {
3077 cam->cam_mode = ovfx2_ov2610_mode;
3078 cam->nmodes = ARRAY_SIZE(ovfx2_ov2610_mode);
3079 } else if (sd->sensor == SEN_OV3610) {
3080 cam->cam_mode = ovfx2_ov3610_mode;
3081 cam->nmodes = ARRAY_SIZE(ovfx2_ov3610_mode);
3082 } else if (!sd->sif) {
3083 cam->cam_mode = ov519_vga_mode;
3084 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3085 } else {
3086 cam->cam_mode = ov519_sif_mode;
3087 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3088 }
3089 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03003090 case BRIDGE_W9968CF:
3091 cam->cam_mode = w9968cf_vga_mode;
3092 cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode);
Hans de Goede79b35902009-10-19 06:08:01 -03003093 if (sd->sif)
3094 cam->nmodes--;
Hans de Goedea511ba92009-10-16 07:13:07 -03003095
3096 /* w9968cf needs initialisation once the sensor is known */
3097 if (w9968cf_init(sd) < 0)
3098 goto error;
3099 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003100 }
Jean-François Moine62833ac2010-10-02 04:27:02 -03003101 gspca_dev->cam.ctrls = sd->ctrls;
3102 if (sd->sensor == SEN_OV7670)
3103 gspca_dev->ctrl_dis = 1 << COLORS;
Hans de Goedef5cee952009-06-14 06:32:52 -03003104 else
Jean-François Moine62833ac2010-10-02 04:27:02 -03003105 gspca_dev->ctrl_dis = (1 << HFLIP) | (1 << VFLIP);
Hans de Goede79b35902009-10-19 06:08:01 -03003106 sd->quality = QUALITY_DEF;
Hans de Goede035d3a32010-01-09 08:14:43 -03003107 if (sd->sensor == SEN_OV7640 ||
Hans de Goeded02134d2010-01-09 19:22:34 -03003108 sd->sensor == SEN_OV7648)
Jean-François Moine62833ac2010-10-02 04:27:02 -03003109 gspca_dev->ctrl_dis |= (1 << AUTOBRIGHT) | (1 << CONTRAST);
Hans de Goeded02134d2010-01-09 19:22:34 -03003110 if (sd->sensor == SEN_OV7670)
Jean-François Moine62833ac2010-10-02 04:27:02 -03003111 gspca_dev->ctrl_dis |= 1 << AUTOBRIGHT;
Hans de Goede02ab18b2009-06-14 04:32:04 -03003112 /* OV8610 Frequency filter control should work but needs testing */
3113 if (sd->sensor == SEN_OV8610)
Jean-François Moine62833ac2010-10-02 04:27:02 -03003114 gspca_dev->ctrl_dis |= 1 << FREQ;
Hans de Goede635118d2009-10-11 09:49:03 -03003115 /* No controls for the OV2610/OV3610 */
3116 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
Jean-François Moine62833ac2010-10-02 04:27:02 -03003117 gspca_dev->ctrl_dis |= (1 << NCTRL) - 1;
Hans de Goede02ab18b2009-06-14 04:32:04 -03003118
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003119 return 0;
3120error:
3121 PDEBUG(D_ERR, "OV519 Config failed");
3122 return -EBUSY;
3123}
3124
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03003125/* this function is called at probe and resume time */
3126static int sd_init(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003127{
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003128 struct sd *sd = (struct sd *) gspca_dev;
3129
3130 /* initialize the sensor */
3131 switch (sd->sensor) {
Hans de Goede635118d2009-10-11 09:49:03 -03003132 case SEN_OV2610:
3133 if (write_i2c_regvals(sd, norm_2610, ARRAY_SIZE(norm_2610)))
3134 return -EIO;
3135 /* Enable autogain, autoexpo, awb, bandfilter */
3136 if (i2c_w_mask(sd, 0x13, 0x27, 0x27) < 0)
3137 return -EIO;
3138 break;
3139 case SEN_OV3610:
3140 if (write_i2c_regvals(sd, norm_3620b, ARRAY_SIZE(norm_3620b)))
3141 return -EIO;
3142 /* Enable autogain, autoexpo, awb, bandfilter */
3143 if (i2c_w_mask(sd, 0x13, 0x27, 0x27) < 0)
3144 return -EIO;
3145 break;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003146 case SEN_OV6620:
3147 if (write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20)))
3148 return -EIO;
3149 break;
3150 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03003151 case SEN_OV66308AF:
Jean-François Moine62833ac2010-10-02 04:27:02 -03003152 sd->ctrls[CONTRAST].def = 200;
3153 /* The default is too low for the ov6630 */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003154 if (write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30)))
3155 return -EIO;
3156 break;
3157 default:
3158/* case SEN_OV7610: */
3159/* case SEN_OV76BE: */
3160 if (write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610)))
3161 return -EIO;
Hans de Goedeae49c402009-06-14 19:15:07 -03003162 if (i2c_w_mask(sd, 0x0e, 0x00, 0x40))
3163 return -EIO;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003164 break;
3165 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03003166 case SEN_OV7620AE:
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003167 if (write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620)))
3168 return -EIO;
3169 break;
3170 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003171 case SEN_OV7648:
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003172 if (write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640)))
3173 return -EIO;
3174 break;
3175 case SEN_OV7670:
Jean-François Moine62833ac2010-10-02 04:27:02 -03003176 sd->ctrls[FREQ].max = 3; /* auto */
3177 sd->ctrls[FREQ].def = 3;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003178 if (write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670)))
3179 return -EIO;
3180 break;
3181 case SEN_OV8610:
3182 if (write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610)))
3183 return -EIO;
3184 break;
3185 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003186 return 0;
3187}
3188
Hans de Goede1876bb92009-06-14 06:45:50 -03003189/* Set up the OV511/OV511+ with the given image parameters.
3190 *
3191 * Do not put any sensor-specific code in here (including I2C I/O functions)
3192 */
3193static int ov511_mode_init_regs(struct sd *sd)
3194{
3195 int hsegs, vsegs, packet_size, fps, needed;
3196 int interlaced = 0;
3197 struct usb_host_interface *alt;
3198 struct usb_interface *intf;
3199
3200 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3201 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3202 if (!alt) {
Jean-François Moine0b656322010-09-13 05:19:58 -03003203 err("Couldn't get altsetting");
Hans de Goede1876bb92009-06-14 06:45:50 -03003204 return -EIO;
3205 }
3206
3207 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3208 reg_w(sd, R51x_FIFO_PSIZE, packet_size >> 5);
3209
3210 reg_w(sd, R511_CAM_UV_EN, 0x01);
3211 reg_w(sd, R511_SNAP_UV_EN, 0x01);
3212 reg_w(sd, R511_SNAP_OPTS, 0x03);
3213
3214 /* Here I'm assuming that snapshot size == image size.
3215 * I hope that's always true. --claudio
3216 */
3217 hsegs = (sd->gspca_dev.width >> 3) - 1;
3218 vsegs = (sd->gspca_dev.height >> 3) - 1;
3219
3220 reg_w(sd, R511_CAM_PXCNT, hsegs);
3221 reg_w(sd, R511_CAM_LNCNT, vsegs);
3222 reg_w(sd, R511_CAM_PXDIV, 0x00);
3223 reg_w(sd, R511_CAM_LNDIV, 0x00);
3224
3225 /* YUV420, low pass filter on */
3226 reg_w(sd, R511_CAM_OPTS, 0x03);
3227
3228 /* Snapshot additions */
3229 reg_w(sd, R511_SNAP_PXCNT, hsegs);
3230 reg_w(sd, R511_SNAP_LNCNT, vsegs);
3231 reg_w(sd, R511_SNAP_PXDIV, 0x00);
3232 reg_w(sd, R511_SNAP_LNDIV, 0x00);
3233
3234 /******** Set the framerate ********/
3235 if (frame_rate > 0)
3236 sd->frame_rate = frame_rate;
3237
3238 switch (sd->sensor) {
3239 case SEN_OV6620:
3240 /* No framerate control, doesn't like higher rates yet */
3241 sd->clockdiv = 3;
3242 break;
3243
3244 /* Note once the FIXME's in mode_init_ov_sensor_regs() are fixed
3245 for more sensors we need to do this for them too */
3246 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03003247 case SEN_OV7620AE:
Hans de Goede1876bb92009-06-14 06:45:50 -03003248 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003249 case SEN_OV7648:
Hans de Goedeb282d872009-06-14 19:10:40 -03003250 case SEN_OV76BE:
Hans de Goede1876bb92009-06-14 06:45:50 -03003251 if (sd->gspca_dev.width == 320)
3252 interlaced = 1;
3253 /* Fall through */
3254 case SEN_OV6630:
Hans de Goede1876bb92009-06-14 06:45:50 -03003255 case SEN_OV7610:
3256 case SEN_OV7670:
3257 switch (sd->frame_rate) {
3258 case 30:
3259 case 25:
3260 /* Not enough bandwidth to do 640x480 @ 30 fps */
3261 if (sd->gspca_dev.width != 640) {
3262 sd->clockdiv = 0;
3263 break;
3264 }
3265 /* Fall through for 640x480 case */
3266 default:
3267/* case 20: */
3268/* case 15: */
3269 sd->clockdiv = 1;
3270 break;
3271 case 10:
3272 sd->clockdiv = 2;
3273 break;
3274 case 5:
3275 sd->clockdiv = 5;
3276 break;
3277 }
3278 if (interlaced) {
3279 sd->clockdiv = (sd->clockdiv + 1) * 2 - 1;
3280 /* Higher then 10 does not work */
3281 if (sd->clockdiv > 10)
3282 sd->clockdiv = 10;
3283 }
3284 break;
3285
3286 case SEN_OV8610:
3287 /* No framerate control ?? */
3288 sd->clockdiv = 0;
3289 break;
3290 }
3291
3292 /* Check if we have enough bandwidth to disable compression */
3293 fps = (interlaced ? 60 : 30) / (sd->clockdiv + 1) + 1;
3294 needed = fps * sd->gspca_dev.width * sd->gspca_dev.height * 3 / 2;
3295 /* 1400 is a conservative estimate of the max nr of isoc packets/sec */
3296 if (needed > 1400 * packet_size) {
3297 /* Enable Y and UV quantization and compression */
3298 reg_w(sd, R511_COMP_EN, 0x07);
3299 reg_w(sd, R511_COMP_LUT_EN, 0x03);
3300 } else {
3301 reg_w(sd, R511_COMP_EN, 0x06);
3302 reg_w(sd, R511_COMP_LUT_EN, 0x00);
3303 }
3304
3305 reg_w(sd, R51x_SYS_RESET, OV511_RESET_OMNICE);
3306 reg_w(sd, R51x_SYS_RESET, 0);
3307
3308 return 0;
3309}
3310
Hans de Goede49809d62009-06-07 12:10:39 -03003311/* Sets up the OV518/OV518+ with the given image parameters
3312 *
3313 * OV518 needs a completely different approach, until we can figure out what
3314 * the individual registers do. Also, only 15 FPS is supported now.
3315 *
3316 * Do not put any sensor-specific code in here (including I2C I/O functions)
3317 */
3318static int ov518_mode_init_regs(struct sd *sd)
3319{
Hans de Goedeb282d872009-06-14 19:10:40 -03003320 int hsegs, vsegs, packet_size;
3321 struct usb_host_interface *alt;
3322 struct usb_interface *intf;
3323
3324 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3325 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3326 if (!alt) {
Jean-François Moine0b656322010-09-13 05:19:58 -03003327 err("Couldn't get altsetting");
Hans de Goedeb282d872009-06-14 19:10:40 -03003328 return -EIO;
3329 }
3330
3331 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3332 ov518_reg_w32(sd, R51x_FIFO_PSIZE, packet_size & ~7, 2);
Hans de Goede49809d62009-06-07 12:10:39 -03003333
3334 /******** Set the mode ********/
3335
3336 reg_w(sd, 0x2b, 0);
3337 reg_w(sd, 0x2c, 0);
3338 reg_w(sd, 0x2d, 0);
3339 reg_w(sd, 0x2e, 0);
3340 reg_w(sd, 0x3b, 0);
3341 reg_w(sd, 0x3c, 0);
3342 reg_w(sd, 0x3d, 0);
3343 reg_w(sd, 0x3e, 0);
3344
3345 if (sd->bridge == BRIDGE_OV518) {
3346 /* Set 8-bit (YVYU) input format */
3347 reg_w_mask(sd, 0x20, 0x08, 0x08);
3348
3349 /* Set 12-bit (4:2:0) output format */
3350 reg_w_mask(sd, 0x28, 0x80, 0xf0);
3351 reg_w_mask(sd, 0x38, 0x80, 0xf0);
3352 } else {
3353 reg_w(sd, 0x28, 0x80);
3354 reg_w(sd, 0x38, 0x80);
3355 }
3356
3357 hsegs = sd->gspca_dev.width / 16;
3358 vsegs = sd->gspca_dev.height / 4;
3359
3360 reg_w(sd, 0x29, hsegs);
3361 reg_w(sd, 0x2a, vsegs);
3362
3363 reg_w(sd, 0x39, hsegs);
3364 reg_w(sd, 0x3a, vsegs);
3365
3366 /* Windows driver does this here; who knows why */
3367 reg_w(sd, 0x2f, 0x80);
3368
Hans de Goedeb282d872009-06-14 19:10:40 -03003369 /******** Set the framerate ********/
3370 sd->clockdiv = 1;
Hans de Goede49809d62009-06-07 12:10:39 -03003371
3372 /* Mode independent, but framerate dependent, regs */
Hans de Goedeb282d872009-06-14 19:10:40 -03003373 /* 0x51: Clock divider; Only works on some cams which use 2 crystals */
3374 reg_w(sd, 0x51, 0x04);
Hans de Goede49809d62009-06-07 12:10:39 -03003375 reg_w(sd, 0x22, 0x18);
3376 reg_w(sd, 0x23, 0xff);
3377
Hans de Goedeb282d872009-06-14 19:10:40 -03003378 if (sd->bridge == BRIDGE_OV518PLUS) {
3379 switch (sd->sensor) {
Hans de Goede859cc472010-01-07 15:42:35 -03003380 case SEN_OV7620AE:
Hans de Goedeb282d872009-06-14 19:10:40 -03003381 if (sd->gspca_dev.width == 320) {
3382 reg_w(sd, 0x20, 0x00);
3383 reg_w(sd, 0x21, 0x19);
3384 } else {
3385 reg_w(sd, 0x20, 0x60);
3386 reg_w(sd, 0x21, 0x1f);
3387 }
3388 break;
Hans de Goede859cc472010-01-07 15:42:35 -03003389 case SEN_OV7620:
3390 reg_w(sd, 0x20, 0x00);
3391 reg_w(sd, 0x21, 0x19);
3392 break;
Hans de Goedeb282d872009-06-14 19:10:40 -03003393 default:
3394 reg_w(sd, 0x21, 0x19);
3395 }
3396 } else
Hans de Goede49809d62009-06-07 12:10:39 -03003397 reg_w(sd, 0x71, 0x17); /* Compression-related? */
3398
3399 /* FIXME: Sensor-specific */
3400 /* Bit 5 is what matters here. Of course, it is "reserved" */
3401 i2c_w(sd, 0x54, 0x23);
3402
3403 reg_w(sd, 0x2f, 0x80);
3404
3405 if (sd->bridge == BRIDGE_OV518PLUS) {
3406 reg_w(sd, 0x24, 0x94);
3407 reg_w(sd, 0x25, 0x90);
3408 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3409 ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */
3410 ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */
3411 ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */
3412 ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */
3413 ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */
3414 ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */
3415 ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */
3416 ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */
3417 } else {
3418 reg_w(sd, 0x24, 0x9f);
3419 reg_w(sd, 0x25, 0x90);
3420 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3421 ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */
3422 ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */
3423 ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */
3424 ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */
3425 ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */
3426 ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */
3427 ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */
3428 ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */
3429 }
3430
3431 reg_w(sd, 0x2f, 0x80);
3432
3433 return 0;
3434}
3435
3436
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003437/* Sets up the OV519 with the given image parameters
3438 *
3439 * OV519 needs a completely different approach, until we can figure out what
3440 * the individual registers do.
3441 *
3442 * Do not put any sensor-specific code in here (including I2C I/O functions)
3443 */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003444static int ov519_mode_init_regs(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003445{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03003446 static const struct ov_regvals mode_init_519_ov7670[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003447 { 0x5d, 0x03 }, /* Turn off suspend mode */
3448 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3449 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
3450 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3451 { 0xa3, 0x18 },
3452 { 0xa4, 0x04 },
3453 { 0xa5, 0x28 },
3454 { 0x37, 0x00 }, /* SetUsbInit */
3455 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3456 /* Enable both fields, YUV Input, disable defect comp (why?) */
3457 { 0x20, 0x0c },
3458 { 0x21, 0x38 },
3459 { 0x22, 0x1d },
3460 { 0x17, 0x50 }, /* undocumented */
3461 { 0x37, 0x00 }, /* undocumented */
3462 { 0x40, 0xff }, /* I2C timeout counter */
3463 { 0x46, 0x00 }, /* I2C clock prescaler */
3464 { 0x59, 0x04 }, /* new from windrv 090403 */
3465 { 0xff, 0x00 }, /* undocumented */
3466 /* windows reads 0x55 at this point, why? */
3467 };
3468
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03003469 static const struct ov_regvals mode_init_519[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003470 { 0x5d, 0x03 }, /* Turn off suspend mode */
3471 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
3472 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
3473 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3474 { 0xa3, 0x18 },
3475 { 0xa4, 0x04 },
3476 { 0xa5, 0x28 },
3477 { 0x37, 0x00 }, /* SetUsbInit */
3478 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3479 /* Enable both fields, YUV Input, disable defect comp (why?) */
3480 { 0x22, 0x1d },
3481 { 0x17, 0x50 }, /* undocumented */
3482 { 0x37, 0x00 }, /* undocumented */
3483 { 0x40, 0xff }, /* I2C timeout counter */
3484 { 0x46, 0x00 }, /* I2C clock prescaler */
3485 { 0x59, 0x04 }, /* new from windrv 090403 */
3486 { 0xff, 0x00 }, /* undocumented */
3487 /* windows reads 0x55 at this point, why? */
3488 };
3489
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003490 /******** Set the mode ********/
3491 if (sd->sensor != SEN_OV7670) {
3492 if (write_regvals(sd, mode_init_519,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03003493 ARRAY_SIZE(mode_init_519)))
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003494 return -EIO;
Hans de Goede035d3a32010-01-09 08:14:43 -03003495 if (sd->sensor == SEN_OV7640 ||
3496 sd->sensor == SEN_OV7648) {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003497 /* Select 8-bit input mode */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03003498 reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003499 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003500 } else {
3501 if (write_regvals(sd, mode_init_519_ov7670,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03003502 ARRAY_SIZE(mode_init_519_ov7670)))
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003503 return -EIO;
3504 }
3505
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03003506 reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);
3507 reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);
Hans de Goede80142ef2009-06-14 06:26:49 -03003508 if (sd->sensor == SEN_OV7670 &&
3509 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3510 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
Hans de Goede035d3a32010-01-09 08:14:43 -03003511 else if (sd->sensor == SEN_OV7648 &&
3512 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3513 reg_w(sd, OV519_R12_X_OFFSETL, 0x01);
Hans de Goede80142ef2009-06-14 06:26:49 -03003514 else
3515 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03003516 reg_w(sd, OV519_R13_X_OFFSETH, 0x00);
3517 reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);
3518 reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);
3519 reg_w(sd, OV519_R16_DIVIDER, 0x00);
3520 reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003521 reg_w(sd, 0x26, 0x00); /* Undocumented */
3522
3523 /******** Set the framerate ********/
3524 if (frame_rate > 0)
3525 sd->frame_rate = frame_rate;
3526
3527/* FIXME: These are only valid at the max resolution. */
3528 sd->clockdiv = 0;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003529 switch (sd->sensor) {
3530 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003531 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003532 switch (sd->frame_rate) {
Jean-Francois Moine53e74512008-11-08 06:10:19 -03003533 default:
3534/* case 30: */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003535 reg_w(sd, 0xa4, 0x0c);
3536 reg_w(sd, 0x23, 0xff);
3537 break;
3538 case 25:
3539 reg_w(sd, 0xa4, 0x0c);
3540 reg_w(sd, 0x23, 0x1f);
3541 break;
3542 case 20:
3543 reg_w(sd, 0xa4, 0x0c);
3544 reg_w(sd, 0x23, 0x1b);
3545 break;
Jean-Francois Moine53e74512008-11-08 06:10:19 -03003546 case 15:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003547 reg_w(sd, 0xa4, 0x04);
3548 reg_w(sd, 0x23, 0xff);
3549 sd->clockdiv = 1;
3550 break;
3551 case 10:
3552 reg_w(sd, 0xa4, 0x04);
3553 reg_w(sd, 0x23, 0x1f);
3554 sd->clockdiv = 1;
3555 break;
3556 case 5:
3557 reg_w(sd, 0xa4, 0x04);
3558 reg_w(sd, 0x23, 0x1b);
3559 sd->clockdiv = 1;
3560 break;
3561 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003562 break;
3563 case SEN_OV8610:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003564 switch (sd->frame_rate) {
3565 default: /* 15 fps */
3566/* case 15: */
3567 reg_w(sd, 0xa4, 0x06);
3568 reg_w(sd, 0x23, 0xff);
3569 break;
3570 case 10:
3571 reg_w(sd, 0xa4, 0x06);
3572 reg_w(sd, 0x23, 0x1f);
3573 break;
3574 case 5:
3575 reg_w(sd, 0xa4, 0x06);
3576 reg_w(sd, 0x23, 0x1b);
3577 break;
3578 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003579 break;
3580 case SEN_OV7670: /* guesses, based on 7640 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003581 PDEBUG(D_STREAM, "Setting framerate to %d fps",
3582 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003583 reg_w(sd, 0xa4, 0x10);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003584 switch (sd->frame_rate) {
3585 case 30:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003586 reg_w(sd, 0x23, 0xff);
3587 break;
3588 case 20:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003589 reg_w(sd, 0x23, 0x1b);
3590 break;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003591 default:
3592/* case 15: */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003593 reg_w(sd, 0x23, 0xff);
3594 sd->clockdiv = 1;
3595 break;
3596 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003597 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003598 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003599 return 0;
3600}
3601
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003602static int mode_init_ov_sensor_regs(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003603{
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003604 struct gspca_dev *gspca_dev;
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003605 int qvga, xstart, xend, ystart, yend;
3606 __u8 v;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003607
3608 gspca_dev = &sd->gspca_dev;
Hans de Goede124cc9c2009-06-14 05:48:00 -03003609 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003610
3611 /******** Mode (VGA/QVGA) and sensor specific regs ********/
3612 switch (sd->sensor) {
Hans de Goede635118d2009-10-11 09:49:03 -03003613 case SEN_OV2610:
3614 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3615 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3616 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3617 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3618 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
3619 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
3620 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
3621 return 0;
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003622 case SEN_OV3610:
Hans de Goede635118d2009-10-11 09:49:03 -03003623 if (qvga) {
3624 xstart = (1040 - gspca_dev->width) / 2 + (0x1f << 4);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003625 ystart = (776 - gspca_dev->height) / 2;
Hans de Goede635118d2009-10-11 09:49:03 -03003626 } else {
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003627 xstart = (2076 - gspca_dev->width) / 2 + (0x10 << 4);
Hans de Goede635118d2009-10-11 09:49:03 -03003628 ystart = (1544 - gspca_dev->height) / 2;
3629 }
3630 xend = xstart + gspca_dev->width;
3631 yend = ystart + gspca_dev->height;
3632 /* Writing to the COMH register resets the other windowing regs
3633 to their default values, so we must do this first. */
3634 i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0xf0);
3635 i2c_w_mask(sd, 0x32,
3636 (((xend >> 1) & 7) << 3) | ((xstart >> 1) & 7),
3637 0x3f);
3638 i2c_w_mask(sd, 0x03,
3639 (((yend >> 1) & 3) << 2) | ((ystart >> 1) & 3),
3640 0x0f);
3641 i2c_w(sd, 0x17, xstart >> 4);
3642 i2c_w(sd, 0x18, xend >> 4);
3643 i2c_w(sd, 0x19, ystart >> 3);
3644 i2c_w(sd, 0x1a, yend >> 3);
3645 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003646 case SEN_OV8610:
3647 /* For OV8610 qvga means qsvga */
3648 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003649 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3650 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3651 i2c_w_mask(sd, 0x2d, 0x00, 0x40); /* from windrv 090403 */
3652 i2c_w_mask(sd, 0x28, 0x20, 0x20); /* progressive mode on */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003653 break;
3654 case SEN_OV7610:
3655 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003656 i2c_w(sd, 0x35, qvga?0x1e:0x9e);
3657 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3658 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003659 break;
3660 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03003661 case SEN_OV7620AE:
Hans de Goedeb282d872009-06-14 19:10:40 -03003662 case SEN_OV76BE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003663 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3664 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
3665 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
3666 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
3667 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
Hans de Goedeb282d872009-06-14 19:10:40 -03003668 i2c_w_mask(sd, 0x67, qvga ? 0xb0 : 0x90, 0xf0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003669 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003670 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3671 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3672 if (sd->sensor == SEN_OV76BE)
3673 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003674 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003675 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003676 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003677 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3678 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
Hans de Goede8d0082f2010-01-09 19:45:44 -03003679 /* Setting this undocumented bit in qvga mode removes a very
3680 annoying vertical shaking of the image */
Hans de Goede035d3a32010-01-09 08:14:43 -03003681 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
Hans de Goede8d0082f2010-01-09 19:45:44 -03003682 /* Unknown */
Hans de Goede035d3a32010-01-09 08:14:43 -03003683 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
Hans de Goede8d0082f2010-01-09 19:45:44 -03003684 /* Allow higher automatic gain (to allow higher framerates) */
Hans de Goede035d3a32010-01-09 08:14:43 -03003685 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003686 i2c_w_mask(sd, 0x12, 0x04, 0x04); /* AWB: 1 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003687 break;
3688 case SEN_OV7670:
3689 /* set COM7_FMT_VGA or COM7_FMT_QVGA
3690 * do we need to set anything else?
3691 * HSTART etc are set in set_ov_sensor_window itself */
3692 i2c_w_mask(sd, OV7670_REG_COM7,
3693 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
3694 OV7670_COM7_FMT_MASK);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003695 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3696 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_AWB,
3697 OV7670_COM8_AWB);
3698 if (qvga) { /* QVGA from ov7670.c by
3699 * Jonathan Corbet */
3700 xstart = 164;
3701 xend = 28;
3702 ystart = 14;
3703 yend = 494;
3704 } else { /* VGA */
3705 xstart = 158;
3706 xend = 14;
3707 ystart = 10;
3708 yend = 490;
3709 }
3710 /* OV7670 hardware window registers are split across
3711 * multiple locations */
3712 i2c_w(sd, OV7670_REG_HSTART, xstart >> 3);
3713 i2c_w(sd, OV7670_REG_HSTOP, xend >> 3);
3714 v = i2c_r(sd, OV7670_REG_HREF);
3715 v = (v & 0xc0) | ((xend & 0x7) << 3) | (xstart & 0x07);
3716 msleep(10); /* need to sleep between read and write to
3717 * same reg! */
3718 i2c_w(sd, OV7670_REG_HREF, v);
3719
3720 i2c_w(sd, OV7670_REG_VSTART, ystart >> 2);
3721 i2c_w(sd, OV7670_REG_VSTOP, yend >> 2);
3722 v = i2c_r(sd, OV7670_REG_VREF);
3723 v = (v & 0xc0) | ((yend & 0x3) << 2) | (ystart & 0x03);
3724 msleep(10); /* need to sleep between read and write to
3725 * same reg! */
3726 i2c_w(sd, OV7670_REG_VREF, v);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003727 break;
3728 case SEN_OV6620:
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003729 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
3730 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
3731 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
3732 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003733 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03003734 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003735 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003736 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003737 break;
3738 default:
3739 return -EINVAL;
3740 }
3741
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003742 /******** Clock programming ********/
Hans de Goedeae49c402009-06-14 19:15:07 -03003743 i2c_w(sd, 0x11, sd->clockdiv);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003744
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003745 return 0;
3746}
3747
Jean-François Moine62833ac2010-10-02 04:27:02 -03003748static void sethvflip(struct gspca_dev *gspca_dev)
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03003749{
Jean-François Moine62833ac2010-10-02 04:27:02 -03003750 struct sd *sd = (struct sd *) gspca_dev;
3751
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003752 if (sd->sensor != SEN_OV7670)
3753 return;
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03003754 if (sd->gspca_dev.streaming)
3755 ov51x_stop(sd);
3756 i2c_w_mask(sd, OV7670_REG_MVFP,
Jean-François Moine62833ac2010-10-02 04:27:02 -03003757 OV7670_MVFP_MIRROR * sd->ctrls[HFLIP].val
3758 | OV7670_MVFP_VFLIP * sd->ctrls[VFLIP].val,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003759 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03003760 if (sd->gspca_dev.streaming)
3761 ov51x_restart(sd);
3762}
3763
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003764static int set_ov_sensor_window(struct sd *sd)
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03003765{
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003766 struct gspca_dev *gspca_dev;
Hans de Goede124cc9c2009-06-14 05:48:00 -03003767 int qvga, crop;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003768 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003769 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003770
Hans de Goede635118d2009-10-11 09:49:03 -03003771 /* mode setup is fully handled in mode_init_ov_sensor_regs for these */
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003772 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610 ||
3773 sd->sensor == SEN_OV7670)
Hans de Goede635118d2009-10-11 09:49:03 -03003774 return mode_init_ov_sensor_regs(sd);
3775
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003776 gspca_dev = &sd->gspca_dev;
Hans de Goede124cc9c2009-06-14 05:48:00 -03003777 qvga = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 1;
3778 crop = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv & 2;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003779
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003780 /* The different sensor ICs handle setting up of window differently.
3781 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
3782 switch (sd->sensor) {
3783 case SEN_OV8610:
3784 hwsbase = 0x1e;
3785 hwebase = 0x1e;
3786 vwsbase = 0x02;
3787 vwebase = 0x02;
3788 break;
3789 case SEN_OV7610:
3790 case SEN_OV76BE:
3791 hwsbase = 0x38;
3792 hwebase = 0x3a;
3793 vwsbase = vwebase = 0x05;
3794 break;
3795 case SEN_OV6620:
3796 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03003797 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003798 hwsbase = 0x38;
3799 hwebase = 0x3a;
3800 vwsbase = 0x05;
3801 vwebase = 0x06;
Hans de Goede7d971372009-06-14 05:28:17 -03003802 if (sd->sensor == SEN_OV66308AF && qvga)
Hans de Goede49809d62009-06-07 12:10:39 -03003803 /* HDG: this fixes U and V getting swapped */
Hans de Goede7d971372009-06-14 05:28:17 -03003804 hwsbase++;
Hans de Goede124cc9c2009-06-14 05:48:00 -03003805 if (crop) {
3806 hwsbase += 8;
3807 hwebase += 8;
3808 vwsbase += 11;
3809 vwebase += 11;
3810 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003811 break;
3812 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03003813 case SEN_OV7620AE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003814 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
3815 hwebase = 0x2f;
3816 vwsbase = vwebase = 0x05;
3817 break;
3818 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003819 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003820 hwsbase = 0x1a;
3821 hwebase = 0x1a;
3822 vwsbase = vwebase = 0x03;
3823 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003824 default:
3825 return -EINVAL;
3826 }
3827
3828 switch (sd->sensor) {
3829 case SEN_OV6620:
3830 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03003831 case SEN_OV66308AF:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003832 if (qvga) { /* QCIF */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003833 hwscale = 0;
3834 vwscale = 0;
3835 } else { /* CIF */
3836 hwscale = 1;
3837 vwscale = 1; /* The datasheet says 0;
3838 * it's wrong */
3839 }
3840 break;
3841 case SEN_OV8610:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003842 if (qvga) { /* QSVGA */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003843 hwscale = 1;
3844 vwscale = 1;
3845 } else { /* SVGA */
3846 hwscale = 2;
3847 vwscale = 2;
3848 }
3849 break;
3850 default: /* SEN_OV7xx0 */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003851 if (qvga) { /* QVGA */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003852 hwscale = 1;
3853 vwscale = 0;
3854 } else { /* VGA */
3855 hwscale = 2;
3856 vwscale = 1;
3857 }
3858 }
3859
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003860 ret = mode_init_ov_sensor_regs(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003861 if (ret < 0)
3862 return ret;
3863
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003864 i2c_w(sd, 0x17, hwsbase);
Hans de Goedea511ba92009-10-16 07:13:07 -03003865 i2c_w(sd, 0x18, hwebase + (sd->sensor_width >> hwscale));
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003866 i2c_w(sd, 0x19, vwsbase);
Hans de Goedea511ba92009-10-16 07:13:07 -03003867 i2c_w(sd, 0x1a, vwebase + (sd->sensor_height >> vwscale));
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003868
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003869 return 0;
3870}
3871
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003872/* -- start the camera -- */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03003873static int sd_start(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003874{
3875 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede49809d62009-06-07 12:10:39 -03003876 int ret = 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003877
Hans de Goedea511ba92009-10-16 07:13:07 -03003878 /* Default for most bridges, allow bridge_mode_init_regs to override */
3879 sd->sensor_width = sd->gspca_dev.width;
3880 sd->sensor_height = sd->gspca_dev.height;
3881
Hans de Goede49809d62009-06-07 12:10:39 -03003882 switch (sd->bridge) {
Hans de Goede1876bb92009-06-14 06:45:50 -03003883 case BRIDGE_OV511:
3884 case BRIDGE_OV511PLUS:
3885 ret = ov511_mode_init_regs(sd);
3886 break;
Hans de Goede49809d62009-06-07 12:10:39 -03003887 case BRIDGE_OV518:
3888 case BRIDGE_OV518PLUS:
3889 ret = ov518_mode_init_regs(sd);
3890 break;
3891 case BRIDGE_OV519:
3892 ret = ov519_mode_init_regs(sd);
3893 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003894 /* case BRIDGE_OVFX2: nothing to do */
Hans de Goedea511ba92009-10-16 07:13:07 -03003895 case BRIDGE_W9968CF:
3896 ret = w9968cf_mode_init_regs(sd);
3897 break;
Hans de Goede49809d62009-06-07 12:10:39 -03003898 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003899 if (ret < 0)
3900 goto out;
Hans de Goede49809d62009-06-07 12:10:39 -03003901
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003902 ret = set_ov_sensor_window(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003903 if (ret < 0)
3904 goto out;
3905
Hans de Goede49809d62009-06-07 12:10:39 -03003906 setcontrast(gspca_dev);
3907 setbrightness(gspca_dev);
3908 setcolors(gspca_dev);
Jean-François Moine62833ac2010-10-02 04:27:02 -03003909 sethvflip(gspca_dev);
3910 setautobright(gspca_dev);
3911 setfreq_i(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03003912
Hans de Goede417a4d22010-02-19 07:37:08 -03003913 /* Force clear snapshot state in case the snapshot button was
3914 pressed while we weren't streaming */
3915 sd->snapshot_needs_reset = 1;
3916 sd_reset_snapshot(gspca_dev);
3917 sd->snapshot_pressed = 0;
3918
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03003919 sd->first_frame = 3;
3920
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003921 ret = ov51x_restart(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003922 if (ret < 0)
3923 goto out;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003924 ov51x_led_control(sd, 1);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03003925 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003926out:
3927 PDEBUG(D_ERR, "camera start error:%d", ret);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03003928 return ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003929}
3930
3931static void sd_stopN(struct gspca_dev *gspca_dev)
3932{
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03003933 struct sd *sd = (struct sd *) gspca_dev;
3934
3935 ov51x_stop(sd);
3936 ov51x_led_control(sd, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003937}
3938
Hans de Goede79b35902009-10-19 06:08:01 -03003939static void sd_stop0(struct gspca_dev *gspca_dev)
3940{
3941 struct sd *sd = (struct sd *) gspca_dev;
3942
3943 if (sd->bridge == BRIDGE_W9968CF)
3944 w9968cf_stop0(sd);
3945}
3946
Hans de Goede92e232a2010-02-20 04:30:45 -03003947static void ov51x_handle_button(struct gspca_dev *gspca_dev, u8 state)
3948{
3949 struct sd *sd = (struct sd *) gspca_dev;
3950
3951 if (sd->snapshot_pressed != state) {
Jean-François Moine28566432010-10-01 07:33:26 -03003952#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede92e232a2010-02-20 04:30:45 -03003953 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
3954 input_sync(gspca_dev->input_dev);
3955#endif
3956 if (state)
3957 sd->snapshot_needs_reset = 1;
3958
3959 sd->snapshot_pressed = state;
3960 } else {
Hans de Goede88e8d202010-02-20 04:45:49 -03003961 /* On the ov511 / ov519 we need to reset the button state
3962 multiple times, as resetting does not work as long as the
3963 button stays pressed */
3964 switch (sd->bridge) {
3965 case BRIDGE_OV511:
3966 case BRIDGE_OV511PLUS:
3967 case BRIDGE_OV519:
3968 if (state)
3969 sd->snapshot_needs_reset = 1;
3970 break;
3971 }
Hans de Goede92e232a2010-02-20 04:30:45 -03003972 }
3973}
3974
Hans de Goede1876bb92009-06-14 06:45:50 -03003975static void ov511_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03003976 u8 *in, /* isoc packet */
3977 int len) /* iso packet length */
Hans de Goede1876bb92009-06-14 06:45:50 -03003978{
3979 struct sd *sd = (struct sd *) gspca_dev;
3980
3981 /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
3982 * byte non-zero. The EOF packet has image width/height in the
3983 * 10th and 11th bytes. The 9th byte is given as follows:
3984 *
3985 * bit 7: EOF
3986 * 6: compression enabled
3987 * 5: 422/420/400 modes
3988 * 4: 422/420/400 modes
3989 * 3: 1
3990 * 2: snapshot button on
3991 * 1: snapshot frame
3992 * 0: even/odd field
3993 */
3994 if (!(in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) &&
3995 (in[8] & 0x08)) {
Hans de Goede88e8d202010-02-20 04:45:49 -03003996 ov51x_handle_button(gspca_dev, (in[8] >> 2) & 1);
Hans de Goede1876bb92009-06-14 06:45:50 -03003997 if (in[8] & 0x80) {
3998 /* Frame end */
3999 if ((in[9] + 1) * 8 != gspca_dev->width ||
4000 (in[10] + 1) * 8 != gspca_dev->height) {
4001 PDEBUG(D_ERR, "Invalid frame size, got: %dx%d,"
4002 " requested: %dx%d\n",
4003 (in[9] + 1) * 8, (in[10] + 1) * 8,
4004 gspca_dev->width, gspca_dev->height);
4005 gspca_dev->last_packet_type = DISCARD_PACKET;
4006 return;
4007 }
4008 /* Add 11 byte footer to frame, might be usefull */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004009 gspca_frame_add(gspca_dev, LAST_PACKET, in, 11);
Hans de Goede1876bb92009-06-14 06:45:50 -03004010 return;
4011 } else {
4012 /* Frame start */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004013 gspca_frame_add(gspca_dev, FIRST_PACKET, in, 0);
Hans de Goede1876bb92009-06-14 06:45:50 -03004014 sd->packet_nr = 0;
4015 }
4016 }
4017
4018 /* Ignore the packet number */
4019 len--;
4020
4021 /* intermediate packet */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004022 gspca_frame_add(gspca_dev, INTER_PACKET, in, len);
Hans de Goede1876bb92009-06-14 06:45:50 -03004023}
4024
Hans de Goede49809d62009-06-07 12:10:39 -03004025static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004026 u8 *data, /* isoc packet */
Hans de Goede49809d62009-06-07 12:10:39 -03004027 int len) /* iso packet length */
4028{
Hans de Goede92918a52009-06-14 06:21:35 -03004029 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede49809d62009-06-07 12:10:39 -03004030
4031 /* A false positive here is likely, until OVT gives me
4032 * the definitive SOF/EOF format */
4033 if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
Hans de Goede92e232a2010-02-20 04:30:45 -03004034 ov51x_handle_button(gspca_dev, (data[6] >> 1) & 1);
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004035 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4036 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
Hans de Goede92918a52009-06-14 06:21:35 -03004037 sd->packet_nr = 0;
4038 }
4039
4040 if (gspca_dev->last_packet_type == DISCARD_PACKET)
4041 return;
4042
4043 /* Does this device use packet numbers ? */
4044 if (len & 7) {
4045 len--;
4046 if (sd->packet_nr == data[len])
4047 sd->packet_nr++;
4048 /* The last few packets of the frame (which are all 0's
4049 except that they may contain part of the footer), are
4050 numbered 0 */
4051 else if (sd->packet_nr == 0 || data[len]) {
4052 PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
4053 (int)data[len], (int)sd->packet_nr);
4054 gspca_dev->last_packet_type = DISCARD_PACKET;
4055 return;
4056 }
Hans de Goede49809d62009-06-07 12:10:39 -03004057 }
4058
4059 /* intermediate packet */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004060 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004061}
4062
4063static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004064 u8 *data, /* isoc packet */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004065 int len) /* iso packet length */
4066{
4067 /* Header of ov519 is 16 bytes:
4068 * Byte Value Description
4069 * 0 0xff magic
4070 * 1 0xff magic
4071 * 2 0xff magic
4072 * 3 0xXX 0x50 = SOF, 0x51 = EOF
4073 * 9 0xXX 0x01 initial frame without data,
4074 * 0x00 standard frame with image
4075 * 14 Lo in EOF: length of image data / 8
4076 * 15 Hi
4077 */
4078
4079 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
4080 switch (data[3]) {
4081 case 0x50: /* start of frame */
Hans de Goede417a4d22010-02-19 07:37:08 -03004082 /* Don't check the button state here, as the state
4083 usually (always ?) changes at EOF and checking it
4084 here leads to unnecessary snapshot state resets. */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004085#define HDRSZ 16
4086 data += HDRSZ;
4087 len -= HDRSZ;
4088#undef HDRSZ
4089 if (data[0] == 0xff || data[1] == 0xd8)
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004090 gspca_frame_add(gspca_dev, FIRST_PACKET,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004091 data, len);
4092 else
4093 gspca_dev->last_packet_type = DISCARD_PACKET;
4094 return;
4095 case 0x51: /* end of frame */
Hans de Goede92e232a2010-02-20 04:30:45 -03004096 ov51x_handle_button(gspca_dev, data[11] & 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004097 if (data[9] != 0)
4098 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004099 gspca_frame_add(gspca_dev, LAST_PACKET,
4100 NULL, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004101 return;
4102 }
4103 }
4104
4105 /* intermediate packet */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004106 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004107}
4108
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004109static void ovfx2_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004110 u8 *data, /* isoc packet */
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004111 int len) /* iso packet length */
4112{
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004113 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004114
4115 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4116
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004117 /* A short read signals EOF */
4118 if (len < OVFX2_BULK_SIZE) {
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004119 /* If the frame is short, and it is one of the first ones
4120 the sensor and bridge are still syncing, so drop it. */
4121 if (sd->first_frame) {
4122 sd->first_frame--;
Jean-François Moineb192ca92010-06-27 03:08:19 -03004123 if (gspca_dev->image_len <
4124 sd->gspca_dev.width * sd->gspca_dev.height)
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004125 gspca_dev->last_packet_type = DISCARD_PACKET;
4126 }
4127 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004128 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004129 }
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004130}
4131
Hans de Goede49809d62009-06-07 12:10:39 -03004132static void sd_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004133 u8 *data, /* isoc packet */
Hans de Goede49809d62009-06-07 12:10:39 -03004134 int len) /* iso packet length */
4135{
4136 struct sd *sd = (struct sd *) gspca_dev;
4137
4138 switch (sd->bridge) {
4139 case BRIDGE_OV511:
4140 case BRIDGE_OV511PLUS:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004141 ov511_pkt_scan(gspca_dev, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004142 break;
4143 case BRIDGE_OV518:
4144 case BRIDGE_OV518PLUS:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004145 ov518_pkt_scan(gspca_dev, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004146 break;
4147 case BRIDGE_OV519:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004148 ov519_pkt_scan(gspca_dev, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004149 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004150 case BRIDGE_OVFX2:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004151 ovfx2_pkt_scan(gspca_dev, data, len);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004152 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03004153 case BRIDGE_W9968CF:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004154 w9968cf_pkt_scan(gspca_dev, data, len);
Hans de Goedea511ba92009-10-16 07:13:07 -03004155 break;
Hans de Goede49809d62009-06-07 12:10:39 -03004156 }
4157}
4158
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004159/* -- management routines -- */
4160
4161static void setbrightness(struct gspca_dev *gspca_dev)
4162{
4163 struct sd *sd = (struct sd *) gspca_dev;
4164 int val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004165
Jean-François Moine62833ac2010-10-02 04:27:02 -03004166 val = sd->ctrls[BRIGHTNESS].val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004167 switch (sd->sensor) {
4168 case SEN_OV8610:
4169 case SEN_OV7610:
4170 case SEN_OV76BE:
4171 case SEN_OV6620:
4172 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004173 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004174 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03004175 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004176 i2c_w(sd, OV7610_REG_BRT, val);
4177 break;
4178 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03004179 case SEN_OV7620AE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004180 /* 7620 doesn't like manual changes when in auto mode */
Jean-François Moine62833ac2010-10-02 04:27:02 -03004181 if (!sd->ctrls[AUTOBRIGHT].val)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004182 i2c_w(sd, OV7610_REG_BRT, val);
4183 break;
4184 case SEN_OV7670:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004185/*win trace
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004186 * i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_AEC); */
4187 i2c_w(sd, OV7670_REG_BRIGHT, ov7670_abs_to_sm(val));
4188 break;
4189 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004190}
4191
4192static void setcontrast(struct gspca_dev *gspca_dev)
4193{
4194 struct sd *sd = (struct sd *) gspca_dev;
4195 int val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004196
Jean-François Moine62833ac2010-10-02 04:27:02 -03004197 val = sd->ctrls[CONTRAST].val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004198 switch (sd->sensor) {
4199 case SEN_OV7610:
4200 case SEN_OV6620:
4201 i2c_w(sd, OV7610_REG_CNT, val);
4202 break;
4203 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004204 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004205 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
Hans de Goede49809d62009-06-07 12:10:39 -03004206 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004207 case SEN_OV8610: {
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03004208 static const __u8 ctab[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004209 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
4210 };
4211
4212 /* Use Y gamma control instead. Bit 0 enables it. */
4213 i2c_w(sd, 0x64, ctab[val >> 5]);
4214 break;
4215 }
Hans de Goede859cc472010-01-07 15:42:35 -03004216 case SEN_OV7620:
4217 case SEN_OV7620AE: {
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03004218 static const __u8 ctab[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004219 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
4220 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
4221 };
4222
4223 /* Use Y gamma control instead. Bit 0 enables it. */
4224 i2c_w(sd, 0x64, ctab[val >> 4]);
4225 break;
4226 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004227 case SEN_OV7670:
4228 /* check that this isn't just the same as ov7610 */
4229 i2c_w(sd, OV7670_REG_CONTRAS, val >> 1);
4230 break;
4231 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004232}
4233
4234static void setcolors(struct gspca_dev *gspca_dev)
4235{
4236 struct sd *sd = (struct sd *) gspca_dev;
4237 int val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004238
Jean-François Moine62833ac2010-10-02 04:27:02 -03004239 val = sd->ctrls[COLORS].val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004240 switch (sd->sensor) {
4241 case SEN_OV8610:
4242 case SEN_OV7610:
4243 case SEN_OV76BE:
4244 case SEN_OV6620:
4245 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004246 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004247 i2c_w(sd, OV7610_REG_SAT, val);
4248 break;
4249 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03004250 case SEN_OV7620AE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004251 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
4252/* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
4253 if (rc < 0)
4254 goto out; */
4255 i2c_w(sd, OV7610_REG_SAT, val);
4256 break;
4257 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03004258 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004259 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
4260 break;
4261 case SEN_OV7670:
4262 /* supported later once I work out how to do it
4263 * transparently fail now! */
4264 /* set REG_COM13 values for UV sat auto mode */
4265 break;
4266 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004267}
4268
Jean-François Moine62833ac2010-10-02 04:27:02 -03004269static void setautobright(struct gspca_dev *gspca_dev)
Hans de Goede02ab18b2009-06-14 04:32:04 -03004270{
Jean-François Moine62833ac2010-10-02 04:27:02 -03004271 struct sd *sd = (struct sd *) gspca_dev;
4272
Hans de Goede035d3a32010-01-09 08:14:43 -03004273 if (sd->sensor == SEN_OV7640 || sd->sensor == SEN_OV7648 ||
4274 sd->sensor == SEN_OV7670 ||
Hans de Goede635118d2009-10-11 09:49:03 -03004275 sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
Hans de Goede02ab18b2009-06-14 04:32:04 -03004276 return;
4277
Jean-François Moine62833ac2010-10-02 04:27:02 -03004278 i2c_w_mask(sd, 0x2d, sd->ctrls[AUTOBRIGHT].val ? 0x10 : 0x00, 0x10);
Hans de Goede02ab18b2009-06-14 04:32:04 -03004279}
4280
Jean-François Moine62833ac2010-10-02 04:27:02 -03004281static void setfreq_i(struct sd *sd)
Hans de Goede02ab18b2009-06-14 04:32:04 -03004282{
Hans de Goede635118d2009-10-11 09:49:03 -03004283 if (sd->sensor == SEN_OV2610 || sd->sensor == SEN_OV3610)
4284 return;
4285
Hans de Goede02ab18b2009-06-14 04:32:04 -03004286 if (sd->sensor == SEN_OV7670) {
Jean-François Moine62833ac2010-10-02 04:27:02 -03004287 switch (sd->ctrls[FREQ].val) {
Hans de Goede02ab18b2009-06-14 04:32:04 -03004288 case 0: /* Banding filter disabled */
4289 i2c_w_mask(sd, OV7670_REG_COM8, 0, OV7670_COM8_BFILT);
4290 break;
4291 case 1: /* 50 hz */
4292 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4293 OV7670_COM8_BFILT);
4294 i2c_w_mask(sd, OV7670_REG_COM11, 0x08, 0x18);
4295 break;
4296 case 2: /* 60 hz */
4297 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4298 OV7670_COM8_BFILT);
4299 i2c_w_mask(sd, OV7670_REG_COM11, 0x00, 0x18);
4300 break;
4301 case 3: /* Auto hz */
4302 i2c_w_mask(sd, OV7670_REG_COM8, OV7670_COM8_BFILT,
4303 OV7670_COM8_BFILT);
4304 i2c_w_mask(sd, OV7670_REG_COM11, OV7670_COM11_HZAUTO,
4305 0x18);
4306 break;
4307 }
4308 } else {
Jean-François Moine62833ac2010-10-02 04:27:02 -03004309 switch (sd->ctrls[FREQ].val) {
Hans de Goede02ab18b2009-06-14 04:32:04 -03004310 case 0: /* Banding filter disabled */
4311 i2c_w_mask(sd, 0x2d, 0x00, 0x04);
4312 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4313 break;
4314 case 1: /* 50 hz (filter on and framerate adj) */
4315 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4316 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4317 /* 20 fps -> 16.667 fps */
4318 if (sd->sensor == SEN_OV6620 ||
Hans de Goede7d971372009-06-14 05:28:17 -03004319 sd->sensor == SEN_OV6630 ||
4320 sd->sensor == SEN_OV66308AF)
Hans de Goede02ab18b2009-06-14 04:32:04 -03004321 i2c_w(sd, 0x2b, 0x5e);
4322 else
4323 i2c_w(sd, 0x2b, 0xac);
4324 break;
4325 case 2: /* 60 hz (filter on, ...) */
4326 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4327 if (sd->sensor == SEN_OV6620 ||
Hans de Goede7d971372009-06-14 05:28:17 -03004328 sd->sensor == SEN_OV6630 ||
4329 sd->sensor == SEN_OV66308AF) {
Hans de Goede02ab18b2009-06-14 04:32:04 -03004330 /* 20 fps -> 15 fps */
4331 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4332 i2c_w(sd, 0x2b, 0xa8);
4333 } else {
4334 /* no framerate adj. */
4335 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4336 }
4337 break;
4338 }
4339 }
4340}
Jean-François Moine62833ac2010-10-02 04:27:02 -03004341static void setfreq(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004342{
4343 struct sd *sd = (struct sd *) gspca_dev;
4344
Jean-François Moine62833ac2010-10-02 04:27:02 -03004345 setfreq_i(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004346
Jean-François Moine62833ac2010-10-02 04:27:02 -03004347 /* Ugly but necessary */
4348 if (sd->bridge == BRIDGE_W9968CF)
4349 w9968cf_set_crop_window(sd);
Hans de Goede02ab18b2009-06-14 04:32:04 -03004350}
4351
4352static int sd_querymenu(struct gspca_dev *gspca_dev,
4353 struct v4l2_querymenu *menu)
4354{
4355 struct sd *sd = (struct sd *) gspca_dev;
4356
4357 switch (menu->id) {
4358 case V4L2_CID_POWER_LINE_FREQUENCY:
4359 switch (menu->index) {
4360 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
4361 strcpy((char *) menu->name, "NoFliker");
4362 return 0;
4363 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
4364 strcpy((char *) menu->name, "50 Hz");
4365 return 0;
4366 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
4367 strcpy((char *) menu->name, "60 Hz");
4368 return 0;
4369 case 3:
4370 if (sd->sensor != SEN_OV7670)
4371 return -EINVAL;
4372
4373 strcpy((char *) menu->name, "Automatic");
4374 return 0;
4375 }
4376 break;
4377 }
4378 return -EINVAL;
4379}
4380
Hans de Goede79b35902009-10-19 06:08:01 -03004381static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4382 struct v4l2_jpegcompression *jcomp)
4383{
4384 struct sd *sd = (struct sd *) gspca_dev;
4385
4386 if (sd->bridge != BRIDGE_W9968CF)
4387 return -EINVAL;
4388
4389 memset(jcomp, 0, sizeof *jcomp);
4390 jcomp->quality = sd->quality;
4391 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |
4392 V4L2_JPEG_MARKER_DRI;
4393 return 0;
4394}
4395
4396static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4397 struct v4l2_jpegcompression *jcomp)
4398{
4399 struct sd *sd = (struct sd *) gspca_dev;
4400
4401 if (sd->bridge != BRIDGE_W9968CF)
4402 return -EINVAL;
4403
4404 if (gspca_dev->streaming)
4405 return -EBUSY;
4406
4407 if (jcomp->quality < QUALITY_MIN)
4408 sd->quality = QUALITY_MIN;
4409 else if (jcomp->quality > QUALITY_MAX)
4410 sd->quality = QUALITY_MAX;
4411 else
4412 sd->quality = jcomp->quality;
4413
4414 /* Return resulting jcomp params to app */
4415 sd_get_jcomp(gspca_dev, jcomp);
4416
4417 return 0;
4418}
4419
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004420/* sub-driver description */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03004421static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004422 .name = MODULE_NAME,
4423 .ctrls = sd_ctrls,
4424 .nctrls = ARRAY_SIZE(sd_ctrls),
4425 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03004426 .init = sd_init,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004427 .start = sd_start,
4428 .stopN = sd_stopN,
Hans de Goede79b35902009-10-19 06:08:01 -03004429 .stop0 = sd_stop0,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004430 .pkt_scan = sd_pkt_scan,
Hans de Goede417a4d22010-02-19 07:37:08 -03004431 .dq_callback = sd_reset_snapshot,
Hans de Goede02ab18b2009-06-14 04:32:04 -03004432 .querymenu = sd_querymenu,
Hans de Goede79b35902009-10-19 06:08:01 -03004433 .get_jcomp = sd_get_jcomp,
4434 .set_jcomp = sd_set_jcomp,
Jean-François Moine28566432010-10-01 07:33:26 -03004435#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede417a4d22010-02-19 07:37:08 -03004436 .other_input = 1,
4437#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004438};
4439
4440/* -- module initialisation -- */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03004441static const __devinitdata struct usb_device_id device_table[] = {
Hans de Goedea511ba92009-10-16 07:13:07 -03004442 {USB_DEVICE(0x041e, 0x4003), .driver_info = BRIDGE_W9968CF },
Hans de Goede49809d62009-06-07 12:10:39 -03004443 {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
4444 {USB_DEVICE(0x041e, 0x405f), .driver_info = BRIDGE_OV519 },
4445 {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
4446 {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
Hans de Goede9e4d8252009-06-14 06:25:06 -03004447 {USB_DEVICE(0x041e, 0x4064),
4448 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
Rafal Milecki518c8df2009-10-02 03:54:44 -03004449 {USB_DEVICE(0x041e, 0x4067), .driver_info = BRIDGE_OV519 },
Hans de Goede9e4d8252009-06-14 06:25:06 -03004450 {USB_DEVICE(0x041e, 0x4068),
4451 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
Hans de Goede49809d62009-06-07 12:10:39 -03004452 {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
4453 {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
Hans de Goede98184f72010-01-07 12:06:00 -03004454 {USB_DEVICE(0x054c, 0x0155),
4455 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
Hans de Goede1876bb92009-06-14 06:45:50 -03004456 {USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },
Hans de Goede49809d62009-06-07 12:10:39 -03004457 {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
4458 {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
4459 {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004460 {USB_DEVICE(0x05a9, 0x2800), .driver_info = BRIDGE_OVFX2 },
Hans de Goede49809d62009-06-07 12:10:39 -03004461 {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
4462 {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
Hans de Goede1876bb92009-06-14 06:45:50 -03004463 {USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },
Hans de Goede49809d62009-06-07 12:10:39 -03004464 {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
Hans de Goede1876bb92009-06-14 06:45:50 -03004465 {USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004466 {USB_DEVICE(0x0b62, 0x0059), .driver_info = BRIDGE_OVFX2 },
4467 {USB_DEVICE(0x0e96, 0xc001), .driver_info = BRIDGE_OVFX2 },
Hans de Goedea511ba92009-10-16 07:13:07 -03004468 {USB_DEVICE(0x1046, 0x9967), .driver_info = BRIDGE_W9968CF },
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004469 {USB_DEVICE(0x8020, 0xEF04), .driver_info = BRIDGE_OVFX2 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004470 {}
4471};
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03004472
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004473MODULE_DEVICE_TABLE(usb, device_table);
4474
4475/* -- device connect -- */
4476static int sd_probe(struct usb_interface *intf,
4477 const struct usb_device_id *id)
4478{
4479 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
4480 THIS_MODULE);
4481}
4482
4483static struct usb_driver sd_driver = {
4484 .name = MODULE_NAME,
4485 .id_table = device_table,
4486 .probe = sd_probe,
4487 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03004488#ifdef CONFIG_PM
4489 .suspend = gspca_suspend,
4490 .resume = gspca_resume,
4491#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004492};
4493
4494/* -- module insert / remove -- */
4495static int __init sd_mod_init(void)
4496{
Jean-François Moine54826432010-09-13 04:53:03 -03004497 return usb_register(&sd_driver);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004498}
4499static void __exit sd_mod_exit(void)
4500{
4501 usb_deregister(&sd_driver);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004502}
4503
4504module_init(sd_mod_init);
4505module_exit(sd_mod_exit);
4506
4507module_param(frame_rate, int, 0644);
4508MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");