blob: 17d0d56fc1262939191984e0f8b248832393f19d [file] [log] [blame]
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001/**
2 * OV519 driver
3 *
Jean-François Moine58c92d32011-03-13 16:36:49 -03004 * Copyright (C) 2008-2011 Jean-François Moine <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 Beauxis2961e8752008-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 Beauxis2961e8752008-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 Beauxis2961e8752008-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 */
Joe Perches133a9fe2011-08-21 19:56:57 -030039
40#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030042#define MODULE_NAME "ov519"
43
Hans de Goede417a4d22010-02-19 07:37:08 -030044#include <linux/input.h>
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030045#include "gspca.h"
46
Jean-François Moine9a731a32010-06-04 05:26:42 -030047/* The jpeg_hdr is used by w996Xcf only */
48/* The CONEX_CAM define for jpeg.h needs renaming, now its used here too */
49#define CONEX_CAM
50#include "jpeg.h"
51
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030052MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
53MODULE_DESCRIPTION("OV519 USB Camera Driver");
54MODULE_LICENSE("GPL");
55
56/* global parameters */
57static int frame_rate;
58
59/* Number of times to retry a failed I2C transaction. Increase this if you
60 * are getting "Failed to read sensor ID..." */
61static int i2c_detect_tries = 10;
62
Jean-François Moine62833ac2010-10-02 04:27:02 -030063/* controls */
64enum e_ctrl {
65 BRIGHTNESS,
66 CONTRAST,
Jean-François Moine58c92d32011-03-13 16:36:49 -030067 EXPOSURE,
Jean-François Moine62833ac2010-10-02 04:27:02 -030068 COLORS,
69 HFLIP,
70 VFLIP,
71 AUTOBRIGHT,
Jean-François Moine58c92d32011-03-13 16:36:49 -030072 AUTOGAIN,
Jean-François Moine62833ac2010-10-02 04:27:02 -030073 FREQ,
74 NCTRL /* number of controls */
75};
76
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030077/* ov519 device descriptor */
78struct sd {
79 struct gspca_dev gspca_dev; /* !! must be the first item */
80
Jean-François Moine62833ac2010-10-02 04:27:02 -030081 struct gspca_ctrl ctrls[NCTRL];
82
Jean-François Moine9d1593a2010-11-11 08:04:06 -030083 u8 packet_nr;
Hans de Goede92918a52009-06-14 06:21:35 -030084
Hans de Goede49809d62009-06-07 12:10:39 -030085 char bridge;
86#define BRIDGE_OV511 0
87#define BRIDGE_OV511PLUS 1
88#define BRIDGE_OV518 2
89#define BRIDGE_OV518PLUS 3
Jean-François Moine42e142f2010-11-13 05:10:27 -030090#define BRIDGE_OV519 4 /* = ov530 */
Hans de Goede635118d2009-10-11 09:49:03 -030091#define BRIDGE_OVFX2 5
Hans de Goedea511ba92009-10-16 07:13:07 -030092#define BRIDGE_W9968CF 6
Hans de Goede9e4d8252009-06-14 06:25:06 -030093#define BRIDGE_MASK 7
94
95 char invert_led;
96#define BRIDGE_INVERT_LED 8
Hans de Goede49809d62009-06-07 12:10:39 -030097
Hans de Goede417a4d22010-02-19 07:37:08 -030098 char snapshot_pressed;
99 char snapshot_needs_reset;
100
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300101 /* Determined by sensor type */
Jean-François Moine9d1593a2010-11-11 08:04:06 -0300102 u8 sif;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300103
Jean-François Moine9d1593a2010-11-11 08:04:06 -0300104 u8 quality;
Hans de Goede79b35902009-10-19 06:08:01 -0300105#define QUALITY_MIN 50
106#define QUALITY_MAX 70
107#define QUALITY_DEF 50
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300108
Jean-François Moine9d1593a2010-11-11 08:04:06 -0300109 u8 stopped; /* Streaming is temporarily paused */
110 u8 first_frame;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300111
Jean-François Moine9d1593a2010-11-11 08:04:06 -0300112 u8 frame_rate; /* current Framerate */
113 u8 clockdiv; /* clockdiv override */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300114
Jean-François Moine7bbe6b82010-11-11 08:27:24 -0300115 s8 sensor; /* Type of image sensor chip (SEN_*) */
Hans de Goedea511ba92009-10-16 07:13:07 -0300116
117 u8 sensor_addr;
Jean-François Moined6fa6632010-11-11 08:05:50 -0300118 u16 sensor_width;
119 u16 sensor_height;
120 s16 sensor_reg_cache[256];
Hans de Goede79b35902009-10-19 06:08:01 -0300121
Jean-François Moine9a731a32010-06-04 05:26:42 -0300122 u8 jpeg_hdr[JPEG_HDR_SZ];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300123};
Jean-François Moine7bbe6b82010-11-11 08:27:24 -0300124enum sensors {
125 SEN_OV2610,
Jean-François Moine07c6c9c2011-02-10 13:32:22 -0300126 SEN_OV2610AE,
Jean-François Moine7bbe6b82010-11-11 08:27:24 -0300127 SEN_OV3610,
128 SEN_OV6620,
129 SEN_OV6630,
130 SEN_OV66308AF,
131 SEN_OV7610,
132 SEN_OV7620,
133 SEN_OV7620AE,
134 SEN_OV7640,
135 SEN_OV7648,
Jean-François Moine42e142f2010-11-13 05:10:27 -0300136 SEN_OV7660,
Jean-François Moine7bbe6b82010-11-11 08:27:24 -0300137 SEN_OV7670,
138 SEN_OV76BE,
139 SEN_OV8610,
Jean-François Moine798ae152011-05-23 05:38:10 -0300140 SEN_OV9600,
Jean-François Moine7bbe6b82010-11-11 08:27:24 -0300141};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300142
Hans de Goedea511ba92009-10-16 07:13:07 -0300143/* Note this is a bit of a hack, but the w9968cf driver needs the code for all
144 the ov sensors which is already present here. When we have the time we
145 really should move the sensor drivers to v4l2 sub drivers. */
146#include "w996Xcf.c"
147
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300148/* V4L2 controls supported by the driver */
Hans de Goede49809d62009-06-07 12:10:39 -0300149static void setbrightness(struct gspca_dev *gspca_dev);
150static void setcontrast(struct gspca_dev *gspca_dev);
Jean-François Moine58c92d32011-03-13 16:36:49 -0300151static void setexposure(struct gspca_dev *gspca_dev);
Hans de Goede49809d62009-06-07 12:10:39 -0300152static void setcolors(struct gspca_dev *gspca_dev);
Jean-François Moine62833ac2010-10-02 04:27:02 -0300153static void sethvflip(struct gspca_dev *gspca_dev);
154static void setautobright(struct gspca_dev *gspca_dev);
Jean-François Moine58c92d32011-03-13 16:36:49 -0300155static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
Jean-François Moine62833ac2010-10-02 04:27:02 -0300156static void setfreq(struct gspca_dev *gspca_dev);
157static void setfreq_i(struct sd *sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300158
Hans de Goede02ab18b2009-06-14 04:32:04 -0300159static const struct ctrl sd_ctrls[] = {
Jean-François Moine62833ac2010-10-02 04:27:02 -0300160[BRIGHTNESS] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300161 {
162 .id = V4L2_CID_BRIGHTNESS,
163 .type = V4L2_CTRL_TYPE_INTEGER,
164 .name = "Brightness",
165 .minimum = 0,
166 .maximum = 255,
167 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300168 .default_value = 127,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300169 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300170 .set_control = setbrightness,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300171 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300172[CONTRAST] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300173 {
174 .id = V4L2_CID_CONTRAST,
175 .type = V4L2_CTRL_TYPE_INTEGER,
176 .name = "Contrast",
177 .minimum = 0,
178 .maximum = 255,
179 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300180 .default_value = 127,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300181 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300182 .set_control = setcontrast,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300183 },
Jean-François Moine58c92d32011-03-13 16:36:49 -0300184[EXPOSURE] = {
185 {
186 .id = V4L2_CID_EXPOSURE,
187 .type = V4L2_CTRL_TYPE_INTEGER,
188 .name = "Exposure",
189 .minimum = 0,
190 .maximum = 255,
191 .step = 1,
192 .default_value = 127,
193 },
194 .set_control = setexposure,
195 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300196[COLORS] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300197 {
198 .id = V4L2_CID_SATURATION,
199 .type = V4L2_CTRL_TYPE_INTEGER,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300200 .name = "Color",
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300201 .minimum = 0,
202 .maximum = 255,
203 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300204 .default_value = 127,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300205 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300206 .set_control = setcolors,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300207 },
Jean-François Moine42e142f2010-11-13 05:10:27 -0300208/* The flip controls work for sensors ov7660 and ov7670 only */
Jean-François Moine62833ac2010-10-02 04:27:02 -0300209[HFLIP] = {
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300210 {
211 .id = V4L2_CID_HFLIP,
212 .type = V4L2_CTRL_TYPE_BOOLEAN,
213 .name = "Mirror",
214 .minimum = 0,
215 .maximum = 1,
216 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300217 .default_value = 0,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300218 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300219 .set_control = sethvflip,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300220 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300221[VFLIP] = {
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300222 {
223 .id = V4L2_CID_VFLIP,
224 .type = V4L2_CTRL_TYPE_BOOLEAN,
225 .name = "Vflip",
226 .minimum = 0,
227 .maximum = 1,
228 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300229 .default_value = 0,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300230 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300231 .set_control = sethvflip,
Jean-Francois Moine0cd67592008-07-29 05:25:28 -0300232 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300233[AUTOBRIGHT] = {
Hans de Goede02ab18b2009-06-14 04:32:04 -0300234 {
235 .id = V4L2_CID_AUTOBRIGHTNESS,
236 .type = V4L2_CTRL_TYPE_BOOLEAN,
237 .name = "Auto Brightness",
238 .minimum = 0,
239 .maximum = 1,
240 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300241 .default_value = 1,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300242 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300243 .set_control = setautobright,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300244 },
Jean-François Moine58c92d32011-03-13 16:36:49 -0300245[AUTOGAIN] = {
246 {
247 .id = V4L2_CID_AUTOGAIN,
248 .type = V4L2_CTRL_TYPE_BOOLEAN,
249 .name = "Auto Gain",
250 .minimum = 0,
251 .maximum = 1,
252 .step = 1,
253 .default_value = 1,
254 .flags = V4L2_CTRL_FLAG_UPDATE
255 },
256 .set = sd_setautogain,
257 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300258[FREQ] = {
Hans de Goede02ab18b2009-06-14 04:32:04 -0300259 {
260 .id = V4L2_CID_POWER_LINE_FREQUENCY,
261 .type = V4L2_CTRL_TYPE_MENU,
262 .name = "Light frequency filter",
263 .minimum = 0,
Jean-François Moine87bae742010-11-12 05:31:34 -0300264 .maximum = 2, /* 0: no flicker, 1: 50Hz, 2:60Hz, 3: auto */
Hans de Goede02ab18b2009-06-14 04:32:04 -0300265 .step = 1,
Jean-François Moine62833ac2010-10-02 04:27:02 -0300266 .default_value = 0,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300267 },
Jean-François Moine62833ac2010-10-02 04:27:02 -0300268 .set_control = setfreq,
Hans de Goede02ab18b2009-06-14 04:32:04 -0300269 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300270};
271
Jean-François Moine83db7682010-11-12 07:14:08 -0300272/* table of the disabled controls */
273static const unsigned ctrl_dis[] = {
Jean-François Moine58c92d32011-03-13 16:36:49 -0300274[SEN_OV2610] = ((1 << NCTRL) - 1) /* no control */
275 ^ ((1 << EXPOSURE) /* but exposure */
276 | (1 << AUTOGAIN)), /* and autogain */
Jean-François Moine83db7682010-11-12 07:14:08 -0300277
Jean-François Moine58c92d32011-03-13 16:36:49 -0300278[SEN_OV2610AE] = ((1 << NCTRL) - 1) /* no control */
279 ^ ((1 << EXPOSURE) /* but exposure */
280 | (1 << AUTOGAIN)), /* and autogain */
Jean-François Moine07c6c9c2011-02-10 13:32:22 -0300281
Jean-François Moine83db7682010-11-12 07:14:08 -0300282[SEN_OV3610] = (1 << NCTRL) - 1, /* no control */
283
284[SEN_OV6620] = (1 << HFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300285 (1 << VFLIP) |
286 (1 << EXPOSURE) |
287 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300288
289[SEN_OV6630] = (1 << HFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300290 (1 << VFLIP) |
291 (1 << EXPOSURE) |
292 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300293
294[SEN_OV66308AF] = (1 << HFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300295 (1 << VFLIP) |
296 (1 << EXPOSURE) |
297 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300298
299[SEN_OV7610] = (1 << HFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300300 (1 << VFLIP) |
301 (1 << EXPOSURE) |
302 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300303
304[SEN_OV7620] = (1 << HFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300305 (1 << VFLIP) |
306 (1 << EXPOSURE) |
307 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300308
309[SEN_OV7620AE] = (1 << HFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300310 (1 << VFLIP) |
311 (1 << EXPOSURE) |
312 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300313
314[SEN_OV7640] = (1 << HFLIP) |
315 (1 << VFLIP) |
316 (1 << AUTOBRIGHT) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300317 (1 << CONTRAST) |
318 (1 << EXPOSURE) |
319 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300320
321[SEN_OV7648] = (1 << HFLIP) |
322 (1 << VFLIP) |
323 (1 << AUTOBRIGHT) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300324 (1 << CONTRAST) |
325 (1 << EXPOSURE) |
326 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300327
Jean-François Moine58c92d32011-03-13 16:36:49 -0300328[SEN_OV7660] = (1 << AUTOBRIGHT) |
329 (1 << EXPOSURE) |
330 (1 << AUTOGAIN),
Jean-François Moine42e142f2010-11-13 05:10:27 -0300331
Jean-François Moine83db7682010-11-12 07:14:08 -0300332[SEN_OV7670] = (1 << COLORS) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300333 (1 << AUTOBRIGHT) |
334 (1 << EXPOSURE) |
335 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300336
337[SEN_OV76BE] = (1 << HFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300338 (1 << VFLIP) |
339 (1 << EXPOSURE) |
340 (1 << AUTOGAIN),
Jean-François Moine83db7682010-11-12 07:14:08 -0300341
342[SEN_OV8610] = (1 << HFLIP) |
343 (1 << VFLIP) |
Jean-François Moine58c92d32011-03-13 16:36:49 -0300344 (1 << EXPOSURE) |
345 (1 << AUTOGAIN) |
Jean-François Moine83db7682010-11-12 07:14:08 -0300346 (1 << FREQ),
Jean-François Moine798ae152011-05-23 05:38:10 -0300347[SEN_OV9600] = ((1 << NCTRL) - 1) /* no control */
348 ^ ((1 << EXPOSURE) /* but exposure */
349 | (1 << AUTOGAIN)), /* and autogain */
350
Jean-François Moine83db7682010-11-12 07:14:08 -0300351};
352
Hans de Goede49809d62009-06-07 12:10:39 -0300353static const struct v4l2_pix_format ov519_vga_mode[] = {
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300354 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
355 .bytesperline = 320,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300356 .sizeimage = 320 * 240 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300357 .colorspace = V4L2_COLORSPACE_JPEG,
358 .priv = 1},
359 {640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
360 .bytesperline = 640,
361 .sizeimage = 640 * 480 * 3 / 8 + 590,
362 .colorspace = V4L2_COLORSPACE_JPEG,
363 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300364};
Hans de Goede49809d62009-06-07 12:10:39 -0300365static const struct v4l2_pix_format ov519_sif_mode[] = {
Hans de Goede124cc9c2009-06-14 05:48:00 -0300366 {160, 120, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
367 .bytesperline = 160,
368 .sizeimage = 160 * 120 * 3 / 8 + 590,
369 .colorspace = V4L2_COLORSPACE_JPEG,
370 .priv = 3},
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300371 {176, 144, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
372 .bytesperline = 176,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300373 .sizeimage = 176 * 144 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300374 .colorspace = V4L2_COLORSPACE_JPEG,
375 .priv = 1},
Hans de Goede124cc9c2009-06-14 05:48:00 -0300376 {320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
377 .bytesperline = 320,
378 .sizeimage = 320 * 240 * 3 / 8 + 590,
379 .colorspace = V4L2_COLORSPACE_JPEG,
380 .priv = 2},
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300381 {352, 288, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
382 .bytesperline = 352,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -0300383 .sizeimage = 352 * 288 * 3 / 8 + 590,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300384 .colorspace = V4L2_COLORSPACE_JPEG,
385 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300386};
387
Hans de Goedeb282d872009-06-14 19:10:40 -0300388/* Note some of the sizeimage values for the ov511 / ov518 may seem
389 larger then necessary, however they need to be this big as the ov511 /
390 ov518 always fills the entire isoc frame, using 0 padding bytes when
391 it doesn't have any data. So with low framerates the amount of data
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300392 transferred can become quite large (libv4l will remove all the 0 padding
Hans de Goedeb282d872009-06-14 19:10:40 -0300393 in userspace). */
Hans de Goede49809d62009-06-07 12:10:39 -0300394static const struct v4l2_pix_format ov518_vga_mode[] = {
395 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
396 .bytesperline = 320,
Hans de Goedeb282d872009-06-14 19:10:40 -0300397 .sizeimage = 320 * 240 * 3,
Hans de Goede49809d62009-06-07 12:10:39 -0300398 .colorspace = V4L2_COLORSPACE_JPEG,
399 .priv = 1},
400 {640, 480, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
401 .bytesperline = 640,
Hans de Goedeb282d872009-06-14 19:10:40 -0300402 .sizeimage = 640 * 480 * 2,
Hans de Goede49809d62009-06-07 12:10:39 -0300403 .colorspace = V4L2_COLORSPACE_JPEG,
404 .priv = 0},
405};
406static const struct v4l2_pix_format ov518_sif_mode[] = {
Hans de Goede124cc9c2009-06-14 05:48:00 -0300407 {160, 120, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
408 .bytesperline = 160,
Hans de Goedeb282d872009-06-14 19:10:40 -0300409 .sizeimage = 70000,
Hans de Goede124cc9c2009-06-14 05:48:00 -0300410 .colorspace = V4L2_COLORSPACE_JPEG,
411 .priv = 3},
Hans de Goede49809d62009-06-07 12:10:39 -0300412 {176, 144, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
413 .bytesperline = 176,
Hans de Goedeb282d872009-06-14 19:10:40 -0300414 .sizeimage = 70000,
Hans de Goede49809d62009-06-07 12:10:39 -0300415 .colorspace = V4L2_COLORSPACE_JPEG,
416 .priv = 1},
Hans de Goede124cc9c2009-06-14 05:48:00 -0300417 {320, 240, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
418 .bytesperline = 320,
Hans de Goedeb282d872009-06-14 19:10:40 -0300419 .sizeimage = 320 * 240 * 3,
Hans de Goede124cc9c2009-06-14 05:48:00 -0300420 .colorspace = V4L2_COLORSPACE_JPEG,
421 .priv = 2},
Hans de Goede49809d62009-06-07 12:10:39 -0300422 {352, 288, V4L2_PIX_FMT_OV518, V4L2_FIELD_NONE,
423 .bytesperline = 352,
Hans de Goedeb282d872009-06-14 19:10:40 -0300424 .sizeimage = 352 * 288 * 3,
Hans de Goede49809d62009-06-07 12:10:39 -0300425 .colorspace = V4L2_COLORSPACE_JPEG,
426 .priv = 0},
427};
428
Hans de Goede1876bb92009-06-14 06:45:50 -0300429static const struct v4l2_pix_format ov511_vga_mode[] = {
430 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
431 .bytesperline = 320,
432 .sizeimage = 320 * 240 * 3,
433 .colorspace = V4L2_COLORSPACE_JPEG,
434 .priv = 1},
435 {640, 480, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
436 .bytesperline = 640,
437 .sizeimage = 640 * 480 * 2,
438 .colorspace = V4L2_COLORSPACE_JPEG,
439 .priv = 0},
440};
441static const struct v4l2_pix_format ov511_sif_mode[] = {
442 {160, 120, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
443 .bytesperline = 160,
Hans de Goedeb282d872009-06-14 19:10:40 -0300444 .sizeimage = 70000,
Hans de Goede1876bb92009-06-14 06:45:50 -0300445 .colorspace = V4L2_COLORSPACE_JPEG,
446 .priv = 3},
447 {176, 144, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
448 .bytesperline = 176,
Hans de Goedeb282d872009-06-14 19:10:40 -0300449 .sizeimage = 70000,
Hans de Goede1876bb92009-06-14 06:45:50 -0300450 .colorspace = V4L2_COLORSPACE_JPEG,
451 .priv = 1},
452 {320, 240, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
453 .bytesperline = 320,
454 .sizeimage = 320 * 240 * 3,
455 .colorspace = V4L2_COLORSPACE_JPEG,
456 .priv = 2},
457 {352, 288, V4L2_PIX_FMT_OV511, V4L2_FIELD_NONE,
458 .bytesperline = 352,
459 .sizeimage = 352 * 288 * 3,
460 .colorspace = V4L2_COLORSPACE_JPEG,
461 .priv = 0},
462};
Hans de Goede49809d62009-06-07 12:10:39 -0300463
Hans de Goede635118d2009-10-11 09:49:03 -0300464static const struct v4l2_pix_format ovfx2_vga_mode[] = {
465 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
466 .bytesperline = 320,
467 .sizeimage = 320 * 240,
468 .colorspace = V4L2_COLORSPACE_SRGB,
469 .priv = 1},
470 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
471 .bytesperline = 640,
472 .sizeimage = 640 * 480,
473 .colorspace = V4L2_COLORSPACE_SRGB,
474 .priv = 0},
475};
476static const struct v4l2_pix_format ovfx2_cif_mode[] = {
477 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
478 .bytesperline = 160,
479 .sizeimage = 160 * 120,
480 .colorspace = V4L2_COLORSPACE_SRGB,
481 .priv = 3},
482 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
483 .bytesperline = 176,
484 .sizeimage = 176 * 144,
485 .colorspace = V4L2_COLORSPACE_SRGB,
486 .priv = 1},
487 {320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
488 .bytesperline = 320,
489 .sizeimage = 320 * 240,
490 .colorspace = V4L2_COLORSPACE_SRGB,
491 .priv = 2},
492 {352, 288, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
493 .bytesperline = 352,
494 .sizeimage = 352 * 288,
495 .colorspace = V4L2_COLORSPACE_SRGB,
496 .priv = 0},
497};
498static const struct v4l2_pix_format ovfx2_ov2610_mode[] = {
Jean-François Moinec42cedb2011-02-10 13:37:48 -0300499 {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
500 .bytesperline = 800,
501 .sizeimage = 800 * 600,
502 .colorspace = V4L2_COLORSPACE_SRGB,
503 .priv = 1},
Hans de Goede635118d2009-10-11 09:49:03 -0300504 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
505 .bytesperline = 1600,
506 .sizeimage = 1600 * 1200,
507 .colorspace = V4L2_COLORSPACE_SRGB},
508};
509static const struct v4l2_pix_format ovfx2_ov3610_mode[] = {
Hans de Goede635118d2009-10-11 09:49:03 -0300510 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
511 .bytesperline = 640,
512 .sizeimage = 640 * 480,
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300513 .colorspace = V4L2_COLORSPACE_SRGB,
514 .priv = 1},
515 {800, 600, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
516 .bytesperline = 800,
517 .sizeimage = 800 * 600,
518 .colorspace = V4L2_COLORSPACE_SRGB,
519 .priv = 1},
520 {1024, 768, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
521 .bytesperline = 1024,
522 .sizeimage = 1024 * 768,
523 .colorspace = V4L2_COLORSPACE_SRGB,
524 .priv = 1},
525 {1600, 1200, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
526 .bytesperline = 1600,
527 .sizeimage = 1600 * 1200,
528 .colorspace = V4L2_COLORSPACE_SRGB,
529 .priv = 0},
530 {2048, 1536, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
531 .bytesperline = 2048,
532 .sizeimage = 2048 * 1536,
533 .colorspace = V4L2_COLORSPACE_SRGB,
534 .priv = 0},
Hans de Goede635118d2009-10-11 09:49:03 -0300535};
Jean-François Moine798ae152011-05-23 05:38:10 -0300536static const struct v4l2_pix_format ovfx2_ov9600_mode[] = {
537 {640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
538 .bytesperline = 640,
539 .sizeimage = 640 * 480,
540 .colorspace = V4L2_COLORSPACE_SRGB,
541 .priv = 1},
542 {1280, 1024, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
543 .bytesperline = 1280,
544 .sizeimage = 1280 * 1024,
545 .colorspace = V4L2_COLORSPACE_SRGB},
546};
Hans de Goede635118d2009-10-11 09:49:03 -0300547
Hans de Goede49809d62009-06-07 12:10:39 -0300548/* Registers common to OV511 / OV518 */
Hans de Goede1876bb92009-06-14 06:45:50 -0300549#define R51x_FIFO_PSIZE 0x30 /* 2 bytes wide w/ OV518(+) */
Jean-François Moine780e3122010-10-19 04:29:10 -0300550#define R51x_SYS_RESET 0x50
Hans de Goede1876bb92009-06-14 06:45:50 -0300551 /* Reset type flags */
552 #define OV511_RESET_OMNICE 0x08
Jean-François Moine780e3122010-10-19 04:29:10 -0300553#define R51x_SYS_INIT 0x53
Hans de Goede49809d62009-06-07 12:10:39 -0300554#define R51x_SYS_SNAP 0x52
Jean-François Moine87bae742010-11-12 05:31:34 -0300555#define R51x_SYS_CUST_ID 0x5f
Hans de Goede49809d62009-06-07 12:10:39 -0300556#define R51x_COMP_LUT_BEGIN 0x80
557
558/* OV511 Camera interface register numbers */
Hans de Goede1876bb92009-06-14 06:45:50 -0300559#define R511_CAM_DELAY 0x10
560#define R511_CAM_EDGE 0x11
561#define R511_CAM_PXCNT 0x12
562#define R511_CAM_LNCNT 0x13
563#define R511_CAM_PXDIV 0x14
564#define R511_CAM_LNDIV 0x15
565#define R511_CAM_UV_EN 0x16
566#define R511_CAM_LINE_MODE 0x17
567#define R511_CAM_OPTS 0x18
568
569#define R511_SNAP_FRAME 0x19
Jean-François Moine87bae742010-11-12 05:31:34 -0300570#define R511_SNAP_PXCNT 0x1a
571#define R511_SNAP_LNCNT 0x1b
572#define R511_SNAP_PXDIV 0x1c
573#define R511_SNAP_LNDIV 0x1d
574#define R511_SNAP_UV_EN 0x1e
Jean-François Moine87bae742010-11-12 05:31:34 -0300575#define R511_SNAP_OPTS 0x1f
Hans de Goede1876bb92009-06-14 06:45:50 -0300576
577#define R511_DRAM_FLOW_CTL 0x20
578#define R511_FIFO_OPTS 0x31
579#define R511_I2C_CTL 0x40
Hans de Goede49809d62009-06-07 12:10:39 -0300580#define R511_SYS_LED_CTL 0x55 /* OV511+ only */
Hans de Goede1876bb92009-06-14 06:45:50 -0300581#define R511_COMP_EN 0x78
582#define R511_COMP_LUT_EN 0x79
Hans de Goede49809d62009-06-07 12:10:39 -0300583
584/* OV518 Camera interface register numbers */
585#define R518_GPIO_OUT 0x56 /* OV518(+) only */
586#define R518_GPIO_CTL 0x57 /* OV518(+) only */
587
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300588/* OV519 Camera interface register numbers */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -0300589#define OV519_R10_H_SIZE 0x10
590#define OV519_R11_V_SIZE 0x11
591#define OV519_R12_X_OFFSETL 0x12
592#define OV519_R13_X_OFFSETH 0x13
593#define OV519_R14_Y_OFFSETL 0x14
594#define OV519_R15_Y_OFFSETH 0x15
595#define OV519_R16_DIVIDER 0x16
596#define OV519_R20_DFR 0x20
597#define OV519_R25_FORMAT 0x25
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300598
599/* OV519 System Controller register numbers */
Jean-François Moine21867802010-11-12 06:12:09 -0300600#define OV519_R51_RESET1 0x51
601#define OV519_R54_EN_CLK1 0x54
Jean-François Moineb4e96ea2010-11-12 16:13:17 -0300602#define OV519_R57_SNAPSHOT 0x57
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300603
604#define OV519_GPIO_DATA_OUT0 0x71
605#define OV519_GPIO_IO_CTRL0 0x72
606
Jean-François Moine87bae742010-11-12 05:31:34 -0300607/*#define OV511_ENDPOINT_ADDRESS 1 * Isoc endpoint number */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300608
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300609/*
610 * The FX2 chip does not give us a zero length read at end of frame.
611 * It does, however, give a short read at the end of a frame, if
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800612 * necessary, rather than run two frames together.
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300613 *
614 * By choosing the right bulk transfer size, we are guaranteed to always
615 * get a short read for the last read of each frame. Frame sizes are
616 * always a composite number (width * height, or a multiple) so if we
617 * choose a prime number, we are guaranteed that the last read of a
618 * frame will be short.
619 *
620 * But it isn't that easy: the 2.6 kernel requires a multiple of 4KB,
621 * otherwise EOVERFLOW "babbling" errors occur. I have not been able
622 * to figure out why. [PMiller]
623 *
624 * The constant (13 * 4096) is the largest "prime enough" number less than 64KB.
625 *
626 * It isn't enough to know the number of bytes per frame, in case we
627 * have data dropouts or buffer overruns (even though the FX2 double
628 * buffers, there are some pretty strict real time constraints for
629 * isochronous transfer for larger frame sizes).
630 */
Jean-François Moine9cf208e2011-05-22 05:05:11 -0300631/*jfm: this value does not work for 800x600 - see isoc_init */
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300632#define OVFX2_BULK_SIZE (13 * 4096)
633
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300634/* I2C registers */
635#define R51x_I2C_W_SID 0x41
636#define R51x_I2C_SADDR_3 0x42
637#define R51x_I2C_SADDR_2 0x43
638#define R51x_I2C_R_SID 0x44
639#define R51x_I2C_DATA 0x45
640#define R518_I2C_CTL 0x47 /* OV518(+) only */
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300641#define OVFX2_I2C_ADDR 0x00
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300642
643/* I2C ADDRESSES */
644#define OV7xx0_SID 0x42
Hans de Goede229bb7d2009-10-11 07:41:46 -0300645#define OV_HIRES_SID 0x60 /* OV9xxx / OV2xxx / OV3xxx */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300646#define OV8xx0_SID 0xa0
647#define OV6xx0_SID 0xc0
648
649/* OV7610 registers */
650#define OV7610_REG_GAIN 0x00 /* gain setting (5:0) */
Hans de Goede49809d62009-06-07 12:10:39 -0300651#define OV7610_REG_BLUE 0x01 /* blue channel balance */
652#define OV7610_REG_RED 0x02 /* red channel balance */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300653#define OV7610_REG_SAT 0x03 /* saturation */
654#define OV8610_REG_HUE 0x04 /* 04 reserved */
655#define OV7610_REG_CNT 0x05 /* Y contrast */
656#define OV7610_REG_BRT 0x06 /* Y brightness */
657#define OV7610_REG_COM_C 0x14 /* misc common regs */
658#define OV7610_REG_ID_HIGH 0x1c /* manufacturer ID MSB */
659#define OV7610_REG_ID_LOW 0x1d /* manufacturer ID LSB */
660#define OV7610_REG_COM_I 0x29 /* misc settings */
661
Jean-François Moine42e142f2010-11-13 05:10:27 -0300662/* OV7660 and OV7670 registers */
Jean-François Moine21867802010-11-12 06:12:09 -0300663#define OV7670_R00_GAIN 0x00 /* Gain lower 8 bits (rest in vref) */
664#define OV7670_R01_BLUE 0x01 /* blue gain */
665#define OV7670_R02_RED 0x02 /* red gain */
666#define OV7670_R03_VREF 0x03 /* Pieces of GAIN, VSTART, VSTOP */
667#define OV7670_R04_COM1 0x04 /* Control 1 */
668/*#define OV7670_R07_AECHH 0x07 * AEC MS 5 bits */
669#define OV7670_R0C_COM3 0x0c /* Control 3 */
670#define OV7670_R0D_COM4 0x0d /* Control 4 */
671#define OV7670_R0E_COM5 0x0e /* All "reserved" */
672#define OV7670_R0F_COM6 0x0f /* Control 6 */
673#define OV7670_R10_AECH 0x10 /* More bits of AEC value */
674#define OV7670_R11_CLKRC 0x11 /* Clock control */
675#define OV7670_R12_COM7 0x12 /* Control 7 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300676#define OV7670_COM7_FMT_VGA 0x00
677/*#define OV7670_COM7_YUV 0x00 * YUV */
678#define OV7670_COM7_FMT_QVGA 0x10 /* QVGA format */
679#define OV7670_COM7_FMT_MASK 0x38
680#define OV7670_COM7_RESET 0x80 /* Register reset */
Jean-François Moine21867802010-11-12 06:12:09 -0300681#define OV7670_R13_COM8 0x13 /* Control 8 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300682#define OV7670_COM8_AEC 0x01 /* Auto exposure enable */
683#define OV7670_COM8_AWB 0x02 /* White balance enable */
684#define OV7670_COM8_AGC 0x04 /* Auto gain enable */
685#define OV7670_COM8_BFILT 0x20 /* Band filter enable */
686#define OV7670_COM8_AECSTEP 0x40 /* Unlimited AEC step size */
687#define OV7670_COM8_FASTAEC 0x80 /* Enable fast AGC/AEC */
Jean-François Moine21867802010-11-12 06:12:09 -0300688#define OV7670_R14_COM9 0x14 /* Control 9 - gain ceiling */
689#define OV7670_R15_COM10 0x15 /* Control 10 */
690#define OV7670_R17_HSTART 0x17 /* Horiz start high bits */
691#define OV7670_R18_HSTOP 0x18 /* Horiz stop high bits */
692#define OV7670_R19_VSTART 0x19 /* Vert start high bits */
693#define OV7670_R1A_VSTOP 0x1a /* Vert stop high bits */
694#define OV7670_R1E_MVFP 0x1e /* Mirror / vflip */
Jean-François Moine87bae742010-11-12 05:31:34 -0300695#define OV7670_MVFP_VFLIP 0x10 /* vertical flip */
696#define OV7670_MVFP_MIRROR 0x20 /* Mirror image */
Jean-François Moine21867802010-11-12 06:12:09 -0300697#define OV7670_R24_AEW 0x24 /* AGC upper limit */
698#define OV7670_R25_AEB 0x25 /* AGC lower limit */
699#define OV7670_R26_VPT 0x26 /* AGC/AEC fast mode op region */
700#define OV7670_R32_HREF 0x32 /* HREF pieces */
701#define OV7670_R3A_TSLB 0x3a /* lots of stuff */
702#define OV7670_R3B_COM11 0x3b /* Control 11 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300703#define OV7670_COM11_EXP 0x02
704#define OV7670_COM11_HZAUTO 0x10 /* Auto detect 50/60 Hz */
Jean-François Moine21867802010-11-12 06:12:09 -0300705#define OV7670_R3C_COM12 0x3c /* Control 12 */
706#define OV7670_R3D_COM13 0x3d /* Control 13 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300707#define OV7670_COM13_GAMMA 0x80 /* Gamma enable */
708#define OV7670_COM13_UVSAT 0x40 /* UV saturation auto adjustment */
Jean-François Moine21867802010-11-12 06:12:09 -0300709#define OV7670_R3E_COM14 0x3e /* Control 14 */
710#define OV7670_R3F_EDGE 0x3f /* Edge enhancement factor */
711#define OV7670_R40_COM15 0x40 /* Control 15 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300712/*#define OV7670_COM15_R00FF 0xc0 * 00 to FF */
Jean-François Moine21867802010-11-12 06:12:09 -0300713#define OV7670_R41_COM16 0x41 /* Control 16 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300714#define OV7670_COM16_AWBGAIN 0x08 /* AWB gain enable */
Jean-François Moine42e142f2010-11-13 05:10:27 -0300715/* end of ov7660 common registers */
Jean-François Moine21867802010-11-12 06:12:09 -0300716#define OV7670_R55_BRIGHT 0x55 /* Brightness */
717#define OV7670_R56_CONTRAS 0x56 /* Contrast control */
718#define OV7670_R69_GFIX 0x69 /* Fix gain control */
719/*#define OV7670_R8C_RGB444 0x8c * RGB 444 control */
720#define OV7670_R9F_HAECC1 0x9f /* Hist AEC/AGC control 1 */
721#define OV7670_RA0_HAECC2 0xa0 /* Hist AEC/AGC control 2 */
722#define OV7670_RA5_BD50MAX 0xa5 /* 50hz banding step limit */
723#define OV7670_RA6_HAECC3 0xa6 /* Hist AEC/AGC control 3 */
724#define OV7670_RA7_HAECC4 0xa7 /* Hist AEC/AGC control 4 */
725#define OV7670_RA8_HAECC5 0xa8 /* Hist AEC/AGC control 5 */
726#define OV7670_RA9_HAECC6 0xa9 /* Hist AEC/AGC control 6 */
727#define OV7670_RAA_HAECC7 0xaa /* Hist AEC/AGC control 7 */
728#define OV7670_RAB_BD60MAX 0xab /* 60hz banding step limit */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300729
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300730struct ov_regvals {
Jean-François Moine9d1593a2010-11-11 08:04:06 -0300731 u8 reg;
732 u8 val;
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300733};
734struct ov_i2c_regvals {
Jean-François Moine9d1593a2010-11-11 08:04:06 -0300735 u8 reg;
736 u8 val;
Jean-Francois Moine4202f712008-09-03 17:12:15 -0300737};
738
Hans de Goede635118d2009-10-11 09:49:03 -0300739/* Settings for OV2610 camera chip */
Jean-François Moine780e3122010-10-19 04:29:10 -0300740static const struct ov_i2c_regvals norm_2610[] = {
Hans de Goedeb46aaa02009-10-12 10:07:57 -0300741 { 0x12, 0x80 }, /* reset */
Hans de Goede635118d2009-10-11 09:49:03 -0300742};
743
Jean-François Moine07c6c9c2011-02-10 13:32:22 -0300744static const struct ov_i2c_regvals norm_2610ae[] = {
745 {0x12, 0x80}, /* reset */
746 {0x13, 0xcd},
747 {0x09, 0x01},
748 {0x0d, 0x00},
749 {0x11, 0x80},
750 {0x12, 0x20}, /* 1600x1200 */
751 {0x33, 0x0c},
752 {0x35, 0x90},
753 {0x36, 0x37},
754/* ms-win traces */
755 {0x11, 0x83}, /* clock / 3 ? */
756 {0x2d, 0x00}, /* 60 Hz filter */
757 {0x24, 0xb0}, /* normal colors */
758 {0x25, 0x90},
759 {0x10, 0x43},
760};
761
Jean-François Moine780e3122010-10-19 04:29:10 -0300762static const struct ov_i2c_regvals norm_3620b[] = {
Hans de Goede635118d2009-10-11 09:49:03 -0300763 /*
764 * From the datasheet: "Note that after writing to register COMH
765 * (0x12) to change the sensor mode, registers related to the
766 * sensor’s cropping window will be reset back to their default
767 * values."
768 *
769 * "wait 4096 external clock ... to make sure the sensor is
770 * stable and ready to access registers" i.e. 160us at 24MHz
771 */
Hans de Goede635118d2009-10-11 09:49:03 -0300772 { 0x12, 0x80 }, /* COMH reset */
773 { 0x12, 0x00 }, /* QXGA, master */
774
775 /*
776 * 11 CLKRC "Clock Rate Control"
777 * [7] internal frequency doublers: on
778 * [6] video port mode: master
779 * [5:0] clock divider: 1
780 */
781 { 0x11, 0x80 },
782
783 /*
784 * 13 COMI "Common Control I"
785 * = 192 (0xC0) 11000000
786 * COMI[7] "AEC speed selection"
787 * = 1 (0x01) 1....... "Faster AEC correction"
788 * COMI[6] "AEC speed step selection"
789 * = 1 (0x01) .1...... "Big steps, fast"
790 * COMI[5] "Banding filter on off"
791 * = 0 (0x00) ..0..... "Off"
792 * COMI[4] "Banding filter option"
793 * = 0 (0x00) ...0.... "Main clock is 48 MHz and
794 * the PLL is ON"
795 * COMI[3] "Reserved"
796 * = 0 (0x00) ....0...
797 * COMI[2] "AGC auto manual control selection"
798 * = 0 (0x00) .....0.. "Manual"
799 * COMI[1] "AWB auto manual control selection"
800 * = 0 (0x00) ......0. "Manual"
801 * COMI[0] "Exposure control"
802 * = 0 (0x00) .......0 "Manual"
803 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300804 { 0x13, 0xc0 },
Hans de Goede635118d2009-10-11 09:49:03 -0300805
806 /*
807 * 09 COMC "Common Control C"
808 * = 8 (0x08) 00001000
809 * COMC[7:5] "Reserved"
810 * = 0 (0x00) 000.....
811 * COMC[4] "Sleep Mode Enable"
812 * = 0 (0x00) ...0.... "Normal mode"
813 * COMC[3:2] "Sensor sampling reset timing selection"
814 * = 2 (0x02) ....10.. "Longer reset time"
815 * COMC[1:0] "Output drive current select"
816 * = 0 (0x00) ......00 "Weakest"
817 */
818 { 0x09, 0x08 },
819
820 /*
821 * 0C COMD "Common Control D"
822 * = 8 (0x08) 00001000
823 * COMD[7] "Reserved"
824 * = 0 (0x00) 0.......
825 * COMD[6] "Swap MSB and LSB at the output port"
826 * = 0 (0x00) .0...... "False"
827 * COMD[5:3] "Reserved"
828 * = 1 (0x01) ..001...
829 * COMD[2] "Output Average On Off"
830 * = 0 (0x00) .....0.. "Output Normal"
831 * COMD[1] "Sensor precharge voltage selection"
832 * = 0 (0x00) ......0. "Selects internal
833 * reference precharge
834 * voltage"
835 * COMD[0] "Snapshot option"
836 * = 0 (0x00) .......0 "Enable live video output
837 * after snapshot sequence"
838 */
839 { 0x0c, 0x08 },
840
841 /*
842 * 0D COME "Common Control E"
843 * = 161 (0xA1) 10100001
844 * COME[7] "Output average option"
845 * = 1 (0x01) 1....... "Output average of 4 pixels"
846 * COME[6] "Anti-blooming control"
847 * = 0 (0x00) .0...... "Off"
848 * COME[5:3] "Reserved"
849 * = 4 (0x04) ..100...
850 * COME[2] "Clock output power down pin status"
851 * = 0 (0x00) .....0.. "Tri-state data output pin
852 * on power down"
853 * COME[1] "Data output pin status selection at power down"
854 * = 0 (0x00) ......0. "Tri-state VSYNC, PCLK,
855 * HREF, and CHSYNC pins on
856 * power down"
857 * COME[0] "Auto zero circuit select"
858 * = 1 (0x01) .......1 "On"
859 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300860 { 0x0d, 0xa1 },
Hans de Goede635118d2009-10-11 09:49:03 -0300861
862 /*
863 * 0E COMF "Common Control F"
864 * = 112 (0x70) 01110000
865 * COMF[7] "System clock selection"
866 * = 0 (0x00) 0....... "Use 24 MHz system clock"
867 * COMF[6:4] "Reserved"
868 * = 7 (0x07) .111....
869 * COMF[3] "Manual auto negative offset canceling selection"
870 * = 0 (0x00) ....0... "Auto detect negative
871 * offset and cancel it"
872 * COMF[2:0] "Reserved"
873 * = 0 (0x00) .....000
874 */
875 { 0x0e, 0x70 },
876
877 /*
878 * 0F COMG "Common Control G"
879 * = 66 (0x42) 01000010
880 * COMG[7] "Optical black output selection"
881 * = 0 (0x00) 0....... "Disable"
882 * COMG[6] "Black level calibrate selection"
883 * = 1 (0x01) .1...... "Use optical black pixels
884 * to calibrate"
885 * COMG[5:4] "Reserved"
886 * = 0 (0x00) ..00....
887 * COMG[3] "Channel offset adjustment"
888 * = 0 (0x00) ....0... "Disable offset adjustment"
889 * COMG[2] "ADC black level calibration option"
890 * = 0 (0x00) .....0.. "Use B/G line and G/R
891 * line to calibrate each
892 * channel's black level"
893 * COMG[1] "Reserved"
894 * = 1 (0x01) ......1.
895 * COMG[0] "ADC black level calibration enable"
896 * = 0 (0x00) .......0 "Disable"
897 */
898 { 0x0f, 0x42 },
899
900 /*
901 * 14 COMJ "Common Control J"
902 * = 198 (0xC6) 11000110
903 * COMJ[7:6] "AGC gain ceiling"
904 * = 3 (0x03) 11...... "8x"
905 * COMJ[5:4] "Reserved"
906 * = 0 (0x00) ..00....
907 * COMJ[3] "Auto banding filter"
908 * = 0 (0x00) ....0... "Banding filter is always
909 * on off depending on
910 * COMI[5] setting"
911 * COMJ[2] "VSYNC drop option"
912 * = 1 (0x01) .....1.. "SYNC is dropped if frame
913 * data is dropped"
914 * COMJ[1] "Frame data drop"
915 * = 1 (0x01) ......1. "Drop frame data if
916 * exposure is not within
917 * tolerance. In AEC mode,
918 * data is normally dropped
919 * when data is out of
920 * range."
921 * COMJ[0] "Reserved"
922 * = 0 (0x00) .......0
923 */
Jean-François Moine87bae742010-11-12 05:31:34 -0300924 { 0x14, 0xc6 },
Hans de Goede635118d2009-10-11 09:49:03 -0300925
926 /*
927 * 15 COMK "Common Control K"
928 * = 2 (0x02) 00000010
929 * COMK[7] "CHSYNC pin output swap"
930 * = 0 (0x00) 0....... "CHSYNC"
931 * COMK[6] "HREF pin output swap"
932 * = 0 (0x00) .0...... "HREF"
933 * COMK[5] "PCLK output selection"
934 * = 0 (0x00) ..0..... "PCLK always output"
935 * COMK[4] "PCLK edge selection"
936 * = 0 (0x00) ...0.... "Data valid on falling edge"
937 * COMK[3] "HREF output polarity"
938 * = 0 (0x00) ....0... "positive"
939 * COMK[2] "Reserved"
940 * = 0 (0x00) .....0..
941 * COMK[1] "VSYNC polarity"
942 * = 1 (0x01) ......1. "negative"
943 * COMK[0] "HSYNC polarity"
944 * = 0 (0x00) .......0 "positive"
945 */
946 { 0x15, 0x02 },
947
948 /*
949 * 33 CHLF "Current Control"
950 * = 9 (0x09) 00001001
951 * CHLF[7:6] "Sensor current control"
952 * = 0 (0x00) 00......
953 * CHLF[5] "Sensor current range control"
954 * = 0 (0x00) ..0..... "normal range"
955 * CHLF[4] "Sensor current"
956 * = 0 (0x00) ...0.... "normal current"
957 * CHLF[3] "Sensor buffer current control"
958 * = 1 (0x01) ....1... "half current"
959 * CHLF[2] "Column buffer current control"
960 * = 0 (0x00) .....0.. "normal current"
961 * CHLF[1] "Analog DSP current control"
962 * = 0 (0x00) ......0. "normal current"
963 * CHLF[1] "ADC current control"
964 * = 0 (0x00) ......0. "normal current"
965 */
966 { 0x33, 0x09 },
967
968 /*
969 * 34 VBLM "Blooming Control"
970 * = 80 (0x50) 01010000
971 * VBLM[7] "Hard soft reset switch"
972 * = 0 (0x00) 0....... "Hard reset"
973 * VBLM[6:4] "Blooming voltage selection"
974 * = 5 (0x05) .101....
975 * VBLM[3:0] "Sensor current control"
976 * = 0 (0x00) ....0000
977 */
978 { 0x34, 0x50 },
979
980 /*
981 * 36 VCHG "Sensor Precharge Voltage Control"
982 * = 0 (0x00) 00000000
983 * VCHG[7] "Reserved"
984 * = 0 (0x00) 0.......
985 * VCHG[6:4] "Sensor precharge voltage control"
986 * = 0 (0x00) .000....
987 * VCHG[3:0] "Sensor array common reference"
988 * = 0 (0x00) ....0000
989 */
990 { 0x36, 0x00 },
991
992 /*
993 * 37 ADC "ADC Reference Control"
994 * = 4 (0x04) 00000100
995 * ADC[7:4] "Reserved"
996 * = 0 (0x00) 0000....
997 * ADC[3] "ADC input signal range"
998 * = 0 (0x00) ....0... "Input signal 1.0x"
999 * ADC[2:0] "ADC range control"
1000 * = 4 (0x04) .....100
1001 */
1002 { 0x37, 0x04 },
1003
1004 /*
1005 * 38 ACOM "Analog Common Ground"
1006 * = 82 (0x52) 01010010
1007 * ACOM[7] "Analog gain control"
1008 * = 0 (0x00) 0....... "Gain 1x"
1009 * ACOM[6] "Analog black level calibration"
1010 * = 1 (0x01) .1...... "On"
1011 * ACOM[5:0] "Reserved"
1012 * = 18 (0x12) ..010010
1013 */
1014 { 0x38, 0x52 },
1015
1016 /*
1017 * 3A FREFA "Internal Reference Adjustment"
1018 * = 0 (0x00) 00000000
1019 * FREFA[7:0] "Range"
1020 * = 0 (0x00) 00000000
1021 */
1022 { 0x3a, 0x00 },
1023
1024 /*
1025 * 3C FVOPT "Internal Reference Adjustment"
1026 * = 31 (0x1F) 00011111
1027 * FVOPT[7:0] "Range"
1028 * = 31 (0x1F) 00011111
1029 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001030 { 0x3c, 0x1f },
Hans de Goede635118d2009-10-11 09:49:03 -03001031
1032 /*
1033 * 44 Undocumented = 0 (0x00) 00000000
1034 * 44[7:0] "It's a secret"
1035 * = 0 (0x00) 00000000
1036 */
1037 { 0x44, 0x00 },
1038
1039 /*
1040 * 40 Undocumented = 0 (0x00) 00000000
1041 * 40[7:0] "It's a secret"
1042 * = 0 (0x00) 00000000
1043 */
1044 { 0x40, 0x00 },
1045
1046 /*
1047 * 41 Undocumented = 0 (0x00) 00000000
1048 * 41[7:0] "It's a secret"
1049 * = 0 (0x00) 00000000
1050 */
1051 { 0x41, 0x00 },
1052
1053 /*
1054 * 42 Undocumented = 0 (0x00) 00000000
1055 * 42[7:0] "It's a secret"
1056 * = 0 (0x00) 00000000
1057 */
1058 { 0x42, 0x00 },
1059
1060 /*
1061 * 43 Undocumented = 0 (0x00) 00000000
1062 * 43[7:0] "It's a secret"
1063 * = 0 (0x00) 00000000
1064 */
1065 { 0x43, 0x00 },
1066
1067 /*
1068 * 45 Undocumented = 128 (0x80) 10000000
1069 * 45[7:0] "It's a secret"
1070 * = 128 (0x80) 10000000
1071 */
1072 { 0x45, 0x80 },
1073
1074 /*
1075 * 48 Undocumented = 192 (0xC0) 11000000
1076 * 48[7:0] "It's a secret"
1077 * = 192 (0xC0) 11000000
1078 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001079 { 0x48, 0xc0 },
Hans de Goede635118d2009-10-11 09:49:03 -03001080
1081 /*
1082 * 49 Undocumented = 25 (0x19) 00011001
1083 * 49[7:0] "It's a secret"
1084 * = 25 (0x19) 00011001
1085 */
1086 { 0x49, 0x19 },
1087
1088 /*
1089 * 4B Undocumented = 128 (0x80) 10000000
1090 * 4B[7:0] "It's a secret"
1091 * = 128 (0x80) 10000000
1092 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001093 { 0x4b, 0x80 },
Hans de Goede635118d2009-10-11 09:49:03 -03001094
1095 /*
1096 * 4D Undocumented = 196 (0xC4) 11000100
1097 * 4D[7:0] "It's a secret"
1098 * = 196 (0xC4) 11000100
1099 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001100 { 0x4d, 0xc4 },
Hans de Goede635118d2009-10-11 09:49:03 -03001101
1102 /*
1103 * 35 VREF "Reference Voltage Control"
Jean-François Moine87bae742010-11-12 05:31:34 -03001104 * = 76 (0x4c) 01001100
Hans de Goede635118d2009-10-11 09:49:03 -03001105 * VREF[7:5] "Column high reference control"
1106 * = 2 (0x02) 010..... "higher voltage"
1107 * VREF[4:2] "Column low reference control"
1108 * = 3 (0x03) ...011.. "Highest voltage"
1109 * VREF[1:0] "Reserved"
1110 * = 0 (0x00) ......00
1111 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001112 { 0x35, 0x4c },
Hans de Goede635118d2009-10-11 09:49:03 -03001113
1114 /*
1115 * 3D Undocumented = 0 (0x00) 00000000
1116 * 3D[7:0] "It's a secret"
1117 * = 0 (0x00) 00000000
1118 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001119 { 0x3d, 0x00 },
Hans de Goede635118d2009-10-11 09:49:03 -03001120
1121 /*
1122 * 3E Undocumented = 0 (0x00) 00000000
1123 * 3E[7:0] "It's a secret"
1124 * = 0 (0x00) 00000000
1125 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001126 { 0x3e, 0x00 },
Hans de Goede635118d2009-10-11 09:49:03 -03001127
1128 /*
1129 * 3B FREFB "Internal Reference Adjustment"
1130 * = 24 (0x18) 00011000
1131 * FREFB[7:0] "Range"
1132 * = 24 (0x18) 00011000
1133 */
1134 { 0x3b, 0x18 },
1135
1136 /*
1137 * 33 CHLF "Current Control"
1138 * = 25 (0x19) 00011001
1139 * CHLF[7:6] "Sensor current control"
1140 * = 0 (0x00) 00......
1141 * CHLF[5] "Sensor current range control"
1142 * = 0 (0x00) ..0..... "normal range"
1143 * CHLF[4] "Sensor current"
1144 * = 1 (0x01) ...1.... "double current"
1145 * CHLF[3] "Sensor buffer current control"
1146 * = 1 (0x01) ....1... "half current"
1147 * CHLF[2] "Column buffer current control"
1148 * = 0 (0x00) .....0.. "normal current"
1149 * CHLF[1] "Analog DSP current control"
1150 * = 0 (0x00) ......0. "normal current"
1151 * CHLF[1] "ADC current control"
1152 * = 0 (0x00) ......0. "normal current"
1153 */
1154 { 0x33, 0x19 },
1155
1156 /*
1157 * 34 VBLM "Blooming Control"
1158 * = 90 (0x5A) 01011010
1159 * VBLM[7] "Hard soft reset switch"
1160 * = 0 (0x00) 0....... "Hard reset"
1161 * VBLM[6:4] "Blooming voltage selection"
1162 * = 5 (0x05) .101....
1163 * VBLM[3:0] "Sensor current control"
1164 * = 10 (0x0A) ....1010
1165 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001166 { 0x34, 0x5a },
Hans de Goede635118d2009-10-11 09:49:03 -03001167
1168 /*
1169 * 3B FREFB "Internal Reference Adjustment"
1170 * = 0 (0x00) 00000000
1171 * FREFB[7:0] "Range"
1172 * = 0 (0x00) 00000000
1173 */
1174 { 0x3b, 0x00 },
1175
1176 /*
1177 * 33 CHLF "Current Control"
1178 * = 9 (0x09) 00001001
1179 * CHLF[7:6] "Sensor current control"
1180 * = 0 (0x00) 00......
1181 * CHLF[5] "Sensor current range control"
1182 * = 0 (0x00) ..0..... "normal range"
1183 * CHLF[4] "Sensor current"
1184 * = 0 (0x00) ...0.... "normal current"
1185 * CHLF[3] "Sensor buffer current control"
1186 * = 1 (0x01) ....1... "half current"
1187 * CHLF[2] "Column buffer current control"
1188 * = 0 (0x00) .....0.. "normal current"
1189 * CHLF[1] "Analog DSP current control"
1190 * = 0 (0x00) ......0. "normal current"
1191 * CHLF[1] "ADC current control"
1192 * = 0 (0x00) ......0. "normal current"
1193 */
1194 { 0x33, 0x09 },
1195
1196 /*
1197 * 34 VBLM "Blooming Control"
1198 * = 80 (0x50) 01010000
1199 * VBLM[7] "Hard soft reset switch"
1200 * = 0 (0x00) 0....... "Hard reset"
1201 * VBLM[6:4] "Blooming voltage selection"
1202 * = 5 (0x05) .101....
1203 * VBLM[3:0] "Sensor current control"
1204 * = 0 (0x00) ....0000
1205 */
1206 { 0x34, 0x50 },
1207
1208 /*
1209 * 12 COMH "Common Control H"
1210 * = 64 (0x40) 01000000
1211 * COMH[7] "SRST"
1212 * = 0 (0x00) 0....... "No-op"
1213 * COMH[6:4] "Resolution selection"
1214 * = 4 (0x04) .100.... "XGA"
1215 * COMH[3] "Master slave selection"
1216 * = 0 (0x00) ....0... "Master mode"
1217 * COMH[2] "Internal B/R channel option"
1218 * = 0 (0x00) .....0.. "B/R use same channel"
1219 * COMH[1] "Color bar test pattern"
1220 * = 0 (0x00) ......0. "Off"
1221 * COMH[0] "Reserved"
1222 * = 0 (0x00) .......0
1223 */
1224 { 0x12, 0x40 },
1225
1226 /*
1227 * 17 HREFST "Horizontal window start"
1228 * = 31 (0x1F) 00011111
1229 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1230 * = 31 (0x1F) 00011111
1231 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001232 { 0x17, 0x1f },
Hans de Goede635118d2009-10-11 09:49:03 -03001233
1234 /*
1235 * 18 HREFEND "Horizontal window end"
1236 * = 95 (0x5F) 01011111
1237 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1238 * = 95 (0x5F) 01011111
1239 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001240 { 0x18, 0x5f },
Hans de Goede635118d2009-10-11 09:49:03 -03001241
1242 /*
1243 * 19 VSTRT "Vertical window start"
1244 * = 0 (0x00) 00000000
1245 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1246 * = 0 (0x00) 00000000
1247 */
1248 { 0x19, 0x00 },
1249
1250 /*
1251 * 1A VEND "Vertical window end"
1252 * = 96 (0x60) 01100000
1253 * VEND[7:0] "Vertical Window End, 8 MSBs"
1254 * = 96 (0x60) 01100000
1255 */
1256 { 0x1a, 0x60 },
1257
1258 /*
1259 * 32 COMM "Common Control M"
1260 * = 18 (0x12) 00010010
1261 * COMM[7:6] "Pixel clock divide option"
1262 * = 0 (0x00) 00...... "/1"
1263 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1264 * = 2 (0x02) ..010...
1265 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1266 * = 2 (0x02) .....010
1267 */
1268 { 0x32, 0x12 },
1269
1270 /*
1271 * 03 COMA "Common Control A"
1272 * = 74 (0x4A) 01001010
1273 * COMA[7:4] "AWB Update Threshold"
1274 * = 4 (0x04) 0100....
1275 * COMA[3:2] "Vertical window end line control 2 LSBs"
1276 * = 2 (0x02) ....10..
1277 * COMA[1:0] "Vertical window start line control 2 LSBs"
1278 * = 2 (0x02) ......10
1279 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001280 { 0x03, 0x4a },
Hans de Goede635118d2009-10-11 09:49:03 -03001281
1282 /*
1283 * 11 CLKRC "Clock Rate Control"
1284 * = 128 (0x80) 10000000
1285 * CLKRC[7] "Internal frequency doublers on off seclection"
1286 * = 1 (0x01) 1....... "On"
1287 * CLKRC[6] "Digital video master slave selection"
1288 * = 0 (0x00) .0...... "Master mode, sensor
1289 * provides PCLK"
1290 * CLKRC[5:0] "Clock divider { CLK = PCLK/(1+CLKRC[5:0]) }"
1291 * = 0 (0x00) ..000000
1292 */
1293 { 0x11, 0x80 },
1294
1295 /*
1296 * 12 COMH "Common Control H"
1297 * = 0 (0x00) 00000000
1298 * COMH[7] "SRST"
1299 * = 0 (0x00) 0....... "No-op"
1300 * COMH[6:4] "Resolution selection"
1301 * = 0 (0x00) .000.... "QXGA"
1302 * COMH[3] "Master slave selection"
1303 * = 0 (0x00) ....0... "Master mode"
1304 * COMH[2] "Internal B/R channel option"
1305 * = 0 (0x00) .....0.. "B/R use same channel"
1306 * COMH[1] "Color bar test pattern"
1307 * = 0 (0x00) ......0. "Off"
1308 * COMH[0] "Reserved"
1309 * = 0 (0x00) .......0
1310 */
1311 { 0x12, 0x00 },
1312
1313 /*
1314 * 12 COMH "Common Control H"
1315 * = 64 (0x40) 01000000
1316 * COMH[7] "SRST"
1317 * = 0 (0x00) 0....... "No-op"
1318 * COMH[6:4] "Resolution selection"
1319 * = 4 (0x04) .100.... "XGA"
1320 * COMH[3] "Master slave selection"
1321 * = 0 (0x00) ....0... "Master mode"
1322 * COMH[2] "Internal B/R channel option"
1323 * = 0 (0x00) .....0.. "B/R use same channel"
1324 * COMH[1] "Color bar test pattern"
1325 * = 0 (0x00) ......0. "Off"
1326 * COMH[0] "Reserved"
1327 * = 0 (0x00) .......0
1328 */
1329 { 0x12, 0x40 },
1330
1331 /*
1332 * 17 HREFST "Horizontal window start"
1333 * = 31 (0x1F) 00011111
1334 * HREFST[7:0] "Horizontal window start, 8 MSBs"
1335 * = 31 (0x1F) 00011111
1336 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001337 { 0x17, 0x1f },
Hans de Goede635118d2009-10-11 09:49:03 -03001338
1339 /*
1340 * 18 HREFEND "Horizontal window end"
1341 * = 95 (0x5F) 01011111
1342 * HREFEND[7:0] "Horizontal Window End, 8 MSBs"
1343 * = 95 (0x5F) 01011111
1344 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001345 { 0x18, 0x5f },
Hans de Goede635118d2009-10-11 09:49:03 -03001346
1347 /*
1348 * 19 VSTRT "Vertical window start"
1349 * = 0 (0x00) 00000000
1350 * VSTRT[7:0] "Vertical Window Start, 8 MSBs"
1351 * = 0 (0x00) 00000000
1352 */
1353 { 0x19, 0x00 },
1354
1355 /*
1356 * 1A VEND "Vertical window end"
1357 * = 96 (0x60) 01100000
1358 * VEND[7:0] "Vertical Window End, 8 MSBs"
1359 * = 96 (0x60) 01100000
1360 */
1361 { 0x1a, 0x60 },
1362
1363 /*
1364 * 32 COMM "Common Control M"
1365 * = 18 (0x12) 00010010
1366 * COMM[7:6] "Pixel clock divide option"
1367 * = 0 (0x00) 00...... "/1"
1368 * COMM[5:3] "Horizontal window end position, 3 LSBs"
1369 * = 2 (0x02) ..010...
1370 * COMM[2:0] "Horizontal window start position, 3 LSBs"
1371 * = 2 (0x02) .....010
1372 */
1373 { 0x32, 0x12 },
1374
1375 /*
1376 * 03 COMA "Common Control A"
1377 * = 74 (0x4A) 01001010
1378 * COMA[7:4] "AWB Update Threshold"
1379 * = 4 (0x04) 0100....
1380 * COMA[3:2] "Vertical window end line control 2 LSBs"
1381 * = 2 (0x02) ....10..
1382 * COMA[1:0] "Vertical window start line control 2 LSBs"
1383 * = 2 (0x02) ......10
1384 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001385 { 0x03, 0x4a },
Hans de Goede635118d2009-10-11 09:49:03 -03001386
1387 /*
1388 * 02 RED "Red Gain Control"
1389 * = 175 (0xAF) 10101111
1390 * RED[7] "Action"
1391 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1392 * RED[6:0] "Value"
1393 * = 47 (0x2F) .0101111
1394 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001395 { 0x02, 0xaf },
Hans de Goede635118d2009-10-11 09:49:03 -03001396
1397 /*
1398 * 2D ADDVSL "VSYNC Pulse Width"
1399 * = 210 (0xD2) 11010010
1400 * ADDVSL[7:0] "VSYNC pulse width, LSB"
1401 * = 210 (0xD2) 11010010
1402 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001403 { 0x2d, 0xd2 },
Hans de Goede635118d2009-10-11 09:49:03 -03001404
1405 /*
1406 * 00 GAIN = 24 (0x18) 00011000
1407 * GAIN[7:6] "Reserved"
1408 * = 0 (0x00) 00......
1409 * GAIN[5] "Double"
1410 * = 0 (0x00) ..0..... "False"
1411 * GAIN[4] "Double"
1412 * = 1 (0x01) ...1.... "True"
1413 * GAIN[3:0] "Range"
1414 * = 8 (0x08) ....1000
1415 */
1416 { 0x00, 0x18 },
1417
1418 /*
1419 * 01 BLUE "Blue Gain Control"
1420 * = 240 (0xF0) 11110000
1421 * BLUE[7] "Action"
1422 * = 1 (0x01) 1....... "gain = 1/(1+bitrev([6:0]))"
1423 * BLUE[6:0] "Value"
1424 * = 112 (0x70) .1110000
1425 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001426 { 0x01, 0xf0 },
Hans de Goede635118d2009-10-11 09:49:03 -03001427
1428 /*
1429 * 10 AEC "Automatic Exposure Control"
1430 * = 10 (0x0A) 00001010
1431 * AEC[7:0] "Automatic Exposure Control, 8 MSBs"
1432 * = 10 (0x0A) 00001010
1433 */
Jean-François Moine87bae742010-11-12 05:31:34 -03001434 { 0x10, 0x0a },
Hans de Goede635118d2009-10-11 09:49:03 -03001435
Jean-François Moine87bae742010-11-12 05:31:34 -03001436 { 0xe1, 0x67 },
1437 { 0xe3, 0x03 },
1438 { 0xe4, 0x26 },
1439 { 0xe5, 0x3e },
1440 { 0xf8, 0x01 },
1441 { 0xff, 0x01 },
Hans de Goede635118d2009-10-11 09:49:03 -03001442};
1443
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001444static const struct ov_i2c_regvals norm_6x20[] = {
1445 { 0x12, 0x80 }, /* reset */
1446 { 0x11, 0x01 },
1447 { 0x03, 0x60 },
1448 { 0x05, 0x7f }, /* For when autoadjust is off */
1449 { 0x07, 0xa8 },
Jean-François Moine87bae742010-11-12 05:31:34 -03001450 /* The ratio of 0x0c and 0x0d controls the white point */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001451 { 0x0c, 0x24 },
1452 { 0x0d, 0x24 },
1453 { 0x0f, 0x15 }, /* COMS */
1454 { 0x10, 0x75 }, /* AEC Exposure time */
1455 { 0x12, 0x24 }, /* Enable AGC */
1456 { 0x14, 0x04 },
1457 /* 0x16: 0x06 helps frame stability with moving objects */
1458 { 0x16, 0x06 },
1459/* { 0x20, 0x30 }, * Aperture correction enable */
1460 { 0x26, 0xb2 }, /* BLC enable */
1461 /* 0x28: 0x05 Selects RGB format if RGB on */
1462 { 0x28, 0x05 },
1463 { 0x2a, 0x04 }, /* Disable framerate adjust */
1464/* { 0x2b, 0xac }, * Framerate; Set 2a[7] first */
Hans de Goedeae49c402009-06-14 19:15:07 -03001465 { 0x2d, 0x85 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001466 { 0x33, 0xa0 }, /* Color Processing Parameter */
1467 { 0x34, 0xd2 }, /* Max A/D range */
1468 { 0x38, 0x8b },
1469 { 0x39, 0x40 },
1470
1471 { 0x3c, 0x39 }, /* Enable AEC mode changing */
1472 { 0x3c, 0x3c }, /* Change AEC mode */
1473 { 0x3c, 0x24 }, /* Disable AEC mode changing */
1474
1475 { 0x3d, 0x80 },
1476 /* These next two registers (0x4a, 0x4b) are undocumented.
1477 * They control the color balance */
1478 { 0x4a, 0x80 },
1479 { 0x4b, 0x80 },
1480 { 0x4d, 0xd2 }, /* This reduces noise a bit */
1481 { 0x4e, 0xc1 },
1482 { 0x4f, 0x04 },
1483/* Do 50-53 have any effect? */
1484/* Toggle 0x12[2] off and on here? */
1485};
1486
1487static const struct ov_i2c_regvals norm_6x30[] = {
1488 { 0x12, 0x80 }, /* Reset */
1489 { 0x00, 0x1f }, /* Gain */
1490 { 0x01, 0x99 }, /* Blue gain */
1491 { 0x02, 0x7c }, /* Red gain */
1492 { 0x03, 0xc0 }, /* Saturation */
1493 { 0x05, 0x0a }, /* Contrast */
1494 { 0x06, 0x95 }, /* Brightness */
1495 { 0x07, 0x2d }, /* Sharpness */
1496 { 0x0c, 0x20 },
1497 { 0x0d, 0x20 },
Hans de Goede02ab18b2009-06-14 04:32:04 -03001498 { 0x0e, 0xa0 }, /* Was 0x20, bit7 enables a 2x gain which we need */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001499 { 0x0f, 0x05 },
1500 { 0x10, 0x9a },
1501 { 0x11, 0x00 }, /* Pixel clock = fastest */
1502 { 0x12, 0x24 }, /* Enable AGC and AWB */
1503 { 0x13, 0x21 },
1504 { 0x14, 0x80 },
1505 { 0x15, 0x01 },
1506 { 0x16, 0x03 },
1507 { 0x17, 0x38 },
1508 { 0x18, 0xea },
1509 { 0x19, 0x04 },
1510 { 0x1a, 0x93 },
1511 { 0x1b, 0x00 },
1512 { 0x1e, 0xc4 },
1513 { 0x1f, 0x04 },
1514 { 0x20, 0x20 },
1515 { 0x21, 0x10 },
1516 { 0x22, 0x88 },
1517 { 0x23, 0xc0 }, /* Crystal circuit power level */
1518 { 0x25, 0x9a }, /* Increase AEC black ratio */
1519 { 0x26, 0xb2 }, /* BLC enable */
1520 { 0x27, 0xa2 },
1521 { 0x28, 0x00 },
1522 { 0x29, 0x00 },
1523 { 0x2a, 0x84 }, /* 60 Hz power */
1524 { 0x2b, 0xa8 }, /* 60 Hz power */
1525 { 0x2c, 0xa0 },
1526 { 0x2d, 0x95 }, /* Enable auto-brightness */
1527 { 0x2e, 0x88 },
1528 { 0x33, 0x26 },
1529 { 0x34, 0x03 },
1530 { 0x36, 0x8f },
1531 { 0x37, 0x80 },
1532 { 0x38, 0x83 },
1533 { 0x39, 0x80 },
1534 { 0x3a, 0x0f },
1535 { 0x3b, 0x3c },
1536 { 0x3c, 0x1a },
1537 { 0x3d, 0x80 },
1538 { 0x3e, 0x80 },
1539 { 0x3f, 0x0e },
1540 { 0x40, 0x00 }, /* White bal */
1541 { 0x41, 0x00 }, /* White bal */
1542 { 0x42, 0x80 },
1543 { 0x43, 0x3f }, /* White bal */
1544 { 0x44, 0x80 },
1545 { 0x45, 0x20 },
1546 { 0x46, 0x20 },
1547 { 0x47, 0x80 },
1548 { 0x48, 0x7f },
1549 { 0x49, 0x00 },
1550 { 0x4a, 0x00 },
1551 { 0x4b, 0x80 },
1552 { 0x4c, 0xd0 },
1553 { 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
1554 { 0x4e, 0x40 },
1555 { 0x4f, 0x07 }, /* UV avg., col. killer: max */
1556 { 0x50, 0xff },
1557 { 0x54, 0x23 }, /* Max AGC gain: 18dB */
1558 { 0x55, 0xff },
1559 { 0x56, 0x12 },
1560 { 0x57, 0x81 },
1561 { 0x58, 0x75 },
1562 { 0x59, 0x01 }, /* AGC dark current comp.: +1 */
1563 { 0x5a, 0x2c },
1564 { 0x5b, 0x0f }, /* AWB chrominance levels */
1565 { 0x5c, 0x10 },
1566 { 0x3d, 0x80 },
1567 { 0x27, 0xa6 },
1568 { 0x12, 0x20 }, /* Toggle AWB */
1569 { 0x12, 0x24 },
1570};
1571
1572/* Lawrence Glaister <lg@jfm.bc.ca> reports:
1573 *
1574 * Register 0x0f in the 7610 has the following effects:
1575 *
1576 * 0x85 (AEC method 1): Best overall, good contrast range
1577 * 0x45 (AEC method 2): Very overexposed
1578 * 0xa5 (spec sheet default): Ok, but the black level is
1579 * shifted resulting in loss of contrast
1580 * 0x05 (old driver setting): very overexposed, too much
1581 * contrast
1582 */
1583static const struct ov_i2c_regvals norm_7610[] = {
1584 { 0x10, 0xff },
1585 { 0x16, 0x06 },
1586 { 0x28, 0x24 },
1587 { 0x2b, 0xac },
1588 { 0x12, 0x00 },
1589 { 0x38, 0x81 },
1590 { 0x28, 0x24 }, /* 0c */
1591 { 0x0f, 0x85 }, /* lg's setting */
1592 { 0x15, 0x01 },
1593 { 0x20, 0x1c },
1594 { 0x23, 0x2a },
1595 { 0x24, 0x10 },
1596 { 0x25, 0x8a },
1597 { 0x26, 0xa2 },
1598 { 0x27, 0xc2 },
1599 { 0x2a, 0x04 },
1600 { 0x2c, 0xfe },
1601 { 0x2d, 0x93 },
1602 { 0x30, 0x71 },
1603 { 0x31, 0x60 },
1604 { 0x32, 0x26 },
1605 { 0x33, 0x20 },
1606 { 0x34, 0x48 },
1607 { 0x12, 0x24 },
1608 { 0x11, 0x01 },
1609 { 0x0c, 0x24 },
1610 { 0x0d, 0x24 },
1611};
1612
1613static const struct ov_i2c_regvals norm_7620[] = {
Hans de Goedea511ba92009-10-16 07:13:07 -03001614 { 0x12, 0x80 }, /* reset */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001615 { 0x00, 0x00 }, /* gain */
1616 { 0x01, 0x80 }, /* blue gain */
1617 { 0x02, 0x80 }, /* red gain */
Jean-François Moine21867802010-11-12 06:12:09 -03001618 { 0x03, 0xc0 }, /* OV7670_R03_VREF */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001619 { 0x06, 0x60 },
1620 { 0x07, 0x00 },
1621 { 0x0c, 0x24 },
1622 { 0x0c, 0x24 },
1623 { 0x0d, 0x24 },
1624 { 0x11, 0x01 },
1625 { 0x12, 0x24 },
1626 { 0x13, 0x01 },
1627 { 0x14, 0x84 },
1628 { 0x15, 0x01 },
1629 { 0x16, 0x03 },
1630 { 0x17, 0x2f },
1631 { 0x18, 0xcf },
1632 { 0x19, 0x06 },
1633 { 0x1a, 0xf5 },
1634 { 0x1b, 0x00 },
1635 { 0x20, 0x18 },
1636 { 0x21, 0x80 },
1637 { 0x22, 0x80 },
1638 { 0x23, 0x00 },
1639 { 0x26, 0xa2 },
1640 { 0x27, 0xea },
Hans de Goedeb282d872009-06-14 19:10:40 -03001641 { 0x28, 0x22 }, /* Was 0x20, bit1 enables a 2x gain which we need */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001642 { 0x29, 0x00 },
1643 { 0x2a, 0x10 },
1644 { 0x2b, 0x00 },
1645 { 0x2c, 0x88 },
1646 { 0x2d, 0x91 },
1647 { 0x2e, 0x80 },
1648 { 0x2f, 0x44 },
1649 { 0x60, 0x27 },
1650 { 0x61, 0x02 },
1651 { 0x62, 0x5f },
1652 { 0x63, 0xd5 },
1653 { 0x64, 0x57 },
1654 { 0x65, 0x83 },
1655 { 0x66, 0x55 },
1656 { 0x67, 0x92 },
1657 { 0x68, 0xcf },
1658 { 0x69, 0x76 },
1659 { 0x6a, 0x22 },
1660 { 0x6b, 0x00 },
1661 { 0x6c, 0x02 },
1662 { 0x6d, 0x44 },
1663 { 0x6e, 0x80 },
1664 { 0x6f, 0x1d },
1665 { 0x70, 0x8b },
1666 { 0x71, 0x00 },
1667 { 0x72, 0x14 },
1668 { 0x73, 0x54 },
1669 { 0x74, 0x00 },
1670 { 0x75, 0x8e },
1671 { 0x76, 0x00 },
1672 { 0x77, 0xff },
1673 { 0x78, 0x80 },
1674 { 0x79, 0x80 },
1675 { 0x7a, 0x80 },
1676 { 0x7b, 0xe2 },
1677 { 0x7c, 0x00 },
1678};
1679
1680/* 7640 and 7648. The defaults should be OK for most registers. */
1681static const struct ov_i2c_regvals norm_7640[] = {
1682 { 0x12, 0x80 },
1683 { 0x12, 0x14 },
1684};
1685
Jean-François Moine42e142f2010-11-13 05:10:27 -03001686static const struct ov_regvals init_519_ov7660[] = {
1687 { 0x5d, 0x03 }, /* Turn off suspend mode */
1688 { 0x53, 0x9b }, /* 0x9f enables the (unused) microcontroller */
1689 { 0x54, 0x0f }, /* bit2 (jpeg enable) */
1690 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
1691 { 0xa3, 0x18 },
1692 { 0xa4, 0x04 },
1693 { 0xa5, 0x28 },
1694 { 0x37, 0x00 }, /* SetUsbInit */
1695 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
1696 /* Enable both fields, YUV Input, disable defect comp (why?) */
1697 { 0x20, 0x0c }, /* 0x0d does U <-> V swap */
1698 { 0x21, 0x38 },
1699 { 0x22, 0x1d },
1700 { 0x17, 0x50 }, /* undocumented */
1701 { 0x37, 0x00 }, /* undocumented */
1702 { 0x40, 0xff }, /* I2C timeout counter */
1703 { 0x46, 0x00 }, /* I2C clock prescaler */
1704};
1705static const struct ov_i2c_regvals norm_7660[] = {
1706 {OV7670_R12_COM7, OV7670_COM7_RESET},
1707 {OV7670_R11_CLKRC, 0x81},
1708 {0x92, 0x00}, /* DM_LNL */
1709 {0x93, 0x00}, /* DM_LNH */
1710 {0x9d, 0x4c}, /* BD50ST */
1711 {0x9e, 0x3f}, /* BD60ST */
1712 {OV7670_R3B_COM11, 0x02},
1713 {OV7670_R13_COM8, 0xf5},
1714 {OV7670_R10_AECH, 0x00},
1715 {OV7670_R00_GAIN, 0x00},
1716 {OV7670_R01_BLUE, 0x7c},
1717 {OV7670_R02_RED, 0x9d},
1718 {OV7670_R12_COM7, 0x00},
1719 {OV7670_R04_COM1, 00},
1720 {OV7670_R18_HSTOP, 0x01},
1721 {OV7670_R17_HSTART, 0x13},
1722 {OV7670_R32_HREF, 0x92},
1723 {OV7670_R19_VSTART, 0x02},
1724 {OV7670_R1A_VSTOP, 0x7a},
1725 {OV7670_R03_VREF, 0x00},
1726 {OV7670_R0E_COM5, 0x04},
1727 {OV7670_R0F_COM6, 0x62},
1728 {OV7670_R15_COM10, 0x00},
1729 {0x16, 0x02}, /* RSVD */
1730 {0x1b, 0x00}, /* PSHFT */
1731 {OV7670_R1E_MVFP, 0x01},
1732 {0x29, 0x3c}, /* RSVD */
1733 {0x33, 0x00}, /* CHLF */
1734 {0x34, 0x07}, /* ARBLM */
1735 {0x35, 0x84}, /* RSVD */
1736 {0x36, 0x00}, /* RSVD */
1737 {0x37, 0x04}, /* ADC */
1738 {0x39, 0x43}, /* OFON */
1739 {OV7670_R3A_TSLB, 0x00},
1740 {OV7670_R3C_COM12, 0x6c},
1741 {OV7670_R3D_COM13, 0x98},
1742 {OV7670_R3F_EDGE, 0x23},
1743 {OV7670_R40_COM15, 0xc1},
1744 {OV7670_R41_COM16, 0x22},
1745 {0x6b, 0x0a}, /* DBLV */
1746 {0xa1, 0x08}, /* RSVD */
1747 {0x69, 0x80}, /* HV */
1748 {0x43, 0xf0}, /* RSVD.. */
1749 {0x44, 0x10},
1750 {0x45, 0x78},
1751 {0x46, 0xa8},
1752 {0x47, 0x60},
1753 {0x48, 0x80},
1754 {0x59, 0xba},
1755 {0x5a, 0x9a},
1756 {0x5b, 0x22},
1757 {0x5c, 0xb9},
1758 {0x5d, 0x9b},
1759 {0x5e, 0x10},
1760 {0x5f, 0xe0},
1761 {0x60, 0x85},
1762 {0x61, 0x60},
1763 {0x9f, 0x9d}, /* RSVD */
1764 {0xa0, 0xa0}, /* DSPC2 */
1765 {0x4f, 0x60}, /* matrix */
1766 {0x50, 0x64},
1767 {0x51, 0x04},
1768 {0x52, 0x18},
1769 {0x53, 0x3c},
1770 {0x54, 0x54},
1771 {0x55, 0x40},
1772 {0x56, 0x40},
1773 {0x57, 0x40},
1774 {0x58, 0x0d}, /* matrix sign */
1775 {0x8b, 0xcc}, /* RSVD */
1776 {0x8c, 0xcc},
1777 {0x8d, 0xcf},
1778 {0x6c, 0x40}, /* gamma curve */
1779 {0x6d, 0xe0},
1780 {0x6e, 0xa0},
1781 {0x6f, 0x80},
1782 {0x70, 0x70},
1783 {0x71, 0x80},
1784 {0x72, 0x60},
1785 {0x73, 0x60},
1786 {0x74, 0x50},
1787 {0x75, 0x40},
1788 {0x76, 0x38},
1789 {0x77, 0x3c},
1790 {0x78, 0x32},
1791 {0x79, 0x1a},
1792 {0x7a, 0x28},
1793 {0x7b, 0x24},
1794 {0x7c, 0x04}, /* gamma curve */
1795 {0x7d, 0x12},
1796 {0x7e, 0x26},
1797 {0x7f, 0x46},
1798 {0x80, 0x54},
1799 {0x81, 0x64},
1800 {0x82, 0x70},
1801 {0x83, 0x7c},
1802 {0x84, 0x86},
1803 {0x85, 0x8e},
1804 {0x86, 0x9c},
1805 {0x87, 0xab},
1806 {0x88, 0xc4},
1807 {0x89, 0xd1},
1808 {0x8a, 0xe5},
1809 {OV7670_R14_COM9, 0x1e},
1810 {OV7670_R24_AEW, 0x80},
1811 {OV7670_R25_AEB, 0x72},
1812 {OV7670_R26_VPT, 0xb3},
1813 {0x62, 0x80}, /* LCC1 */
1814 {0x63, 0x80}, /* LCC2 */
1815 {0x64, 0x06}, /* LCC3 */
1816 {0x65, 0x00}, /* LCC4 */
1817 {0x66, 0x01}, /* LCC5 */
1818 {0x94, 0x0e}, /* RSVD.. */
1819 {0x95, 0x14},
1820 {OV7670_R13_COM8, OV7670_COM8_FASTAEC
1821 | OV7670_COM8_AECSTEP
1822 | OV7670_COM8_BFILT
1823 | 0x10
1824 | OV7670_COM8_AGC
1825 | OV7670_COM8_AWB
1826 | OV7670_COM8_AEC},
1827 {0xa1, 0xc8}
1828};
Jean-François Moine798ae152011-05-23 05:38:10 -03001829static const struct ov_i2c_regvals norm_9600[] = {
1830 {0x12, 0x80},
1831 {0x0c, 0x28},
1832 {0x11, 0x80},
1833 {0x13, 0xb5},
1834 {0x14, 0x3e},
1835 {0x1b, 0x04},
1836 {0x24, 0xb0},
1837 {0x25, 0x90},
1838 {0x26, 0x94},
1839 {0x35, 0x90},
1840 {0x37, 0x07},
1841 {0x38, 0x08},
1842 {0x01, 0x8e},
1843 {0x02, 0x85}
1844};
Jean-François Moine42e142f2010-11-13 05:10:27 -03001845
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001846/* 7670. Defaults taken from OmniVision provided data,
1847* as provided by Jonathan Corbet of OLPC */
1848static const struct ov_i2c_regvals norm_7670[] = {
Jean-François Moine21867802010-11-12 06:12:09 -03001849 { OV7670_R12_COM7, OV7670_COM7_RESET },
1850 { OV7670_R3A_TSLB, 0x04 }, /* OV */
1851 { OV7670_R12_COM7, OV7670_COM7_FMT_VGA }, /* VGA */
1852 { OV7670_R11_CLKRC, 0x01 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001853/*
1854 * Set the hardware window. These values from OV don't entirely
1855 * make sense - hstop is less than hstart. But they work...
1856 */
Jean-François Moine21867802010-11-12 06:12:09 -03001857 { OV7670_R17_HSTART, 0x13 },
1858 { OV7670_R18_HSTOP, 0x01 },
1859 { OV7670_R32_HREF, 0xb6 },
1860 { OV7670_R19_VSTART, 0x02 },
1861 { OV7670_R1A_VSTOP, 0x7a },
1862 { OV7670_R03_VREF, 0x0a },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001863
Jean-François Moine21867802010-11-12 06:12:09 -03001864 { OV7670_R0C_COM3, 0x00 },
1865 { OV7670_R3E_COM14, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001866/* Mystery scaling numbers */
1867 { 0x70, 0x3a },
1868 { 0x71, 0x35 },
1869 { 0x72, 0x11 },
1870 { 0x73, 0xf0 },
1871 { 0xa2, 0x02 },
Jean-François Moine21867802010-11-12 06:12:09 -03001872/* { OV7670_R15_COM10, 0x0 }, */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001873
1874/* Gamma curve values */
1875 { 0x7a, 0x20 },
1876 { 0x7b, 0x10 },
1877 { 0x7c, 0x1e },
1878 { 0x7d, 0x35 },
1879 { 0x7e, 0x5a },
1880 { 0x7f, 0x69 },
1881 { 0x80, 0x76 },
1882 { 0x81, 0x80 },
1883 { 0x82, 0x88 },
1884 { 0x83, 0x8f },
1885 { 0x84, 0x96 },
1886 { 0x85, 0xa3 },
1887 { 0x86, 0xaf },
1888 { 0x87, 0xc4 },
1889 { 0x88, 0xd7 },
1890 { 0x89, 0xe8 },
1891
1892/* AGC and AEC parameters. Note we start by disabling those features,
1893 then turn them only after tweaking the values. */
Jean-François Moine21867802010-11-12 06:12:09 -03001894 { OV7670_R13_COM8, OV7670_COM8_FASTAEC
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001895 | OV7670_COM8_AECSTEP
1896 | OV7670_COM8_BFILT },
Jean-François Moine21867802010-11-12 06:12:09 -03001897 { OV7670_R00_GAIN, 0x00 },
1898 { OV7670_R10_AECH, 0x00 },
1899 { OV7670_R0D_COM4, 0x40 }, /* magic reserved bit */
1900 { OV7670_R14_COM9, 0x18 }, /* 4x gain + magic rsvd bit */
1901 { OV7670_RA5_BD50MAX, 0x05 },
1902 { OV7670_RAB_BD60MAX, 0x07 },
1903 { OV7670_R24_AEW, 0x95 },
1904 { OV7670_R25_AEB, 0x33 },
1905 { OV7670_R26_VPT, 0xe3 },
1906 { OV7670_R9F_HAECC1, 0x78 },
1907 { OV7670_RA0_HAECC2, 0x68 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001908 { 0xa1, 0x03 }, /* magic */
Jean-François Moine21867802010-11-12 06:12:09 -03001909 { OV7670_RA6_HAECC3, 0xd8 },
1910 { OV7670_RA7_HAECC4, 0xd8 },
1911 { OV7670_RA8_HAECC5, 0xf0 },
1912 { OV7670_RA9_HAECC6, 0x90 },
1913 { OV7670_RAA_HAECC7, 0x94 },
1914 { OV7670_R13_COM8, OV7670_COM8_FASTAEC
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001915 | OV7670_COM8_AECSTEP
1916 | OV7670_COM8_BFILT
1917 | OV7670_COM8_AGC
1918 | OV7670_COM8_AEC },
1919
1920/* Almost all of these are magic "reserved" values. */
Jean-François Moine21867802010-11-12 06:12:09 -03001921 { OV7670_R0E_COM5, 0x61 },
1922 { OV7670_R0F_COM6, 0x4b },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001923 { 0x16, 0x02 },
Jean-François Moine21867802010-11-12 06:12:09 -03001924 { OV7670_R1E_MVFP, 0x07 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001925 { 0x21, 0x02 },
1926 { 0x22, 0x91 },
1927 { 0x29, 0x07 },
1928 { 0x33, 0x0b },
1929 { 0x35, 0x0b },
1930 { 0x37, 0x1d },
1931 { 0x38, 0x71 },
1932 { 0x39, 0x2a },
Jean-François Moine21867802010-11-12 06:12:09 -03001933 { OV7670_R3C_COM12, 0x78 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001934 { 0x4d, 0x40 },
1935 { 0x4e, 0x20 },
Jean-François Moine21867802010-11-12 06:12:09 -03001936 { OV7670_R69_GFIX, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001937 { 0x6b, 0x4a },
1938 { 0x74, 0x10 },
1939 { 0x8d, 0x4f },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001940 { 0x8e, 0x00 },
1941 { 0x8f, 0x00 },
1942 { 0x90, 0x00 },
1943 { 0x91, 0x00 },
1944 { 0x96, 0x00 },
1945 { 0x9a, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001946 { 0xb0, 0x84 },
1947 { 0xb1, 0x0c },
1948 { 0xb2, 0x0e },
1949 { 0xb3, 0x82 },
1950 { 0xb8, 0x0a },
1951
1952/* More reserved magic, some of which tweaks white balance */
1953 { 0x43, 0x0a },
1954 { 0x44, 0xf0 },
1955 { 0x45, 0x34 },
1956 { 0x46, 0x58 },
1957 { 0x47, 0x28 },
1958 { 0x48, 0x3a },
1959 { 0x59, 0x88 },
1960 { 0x5a, 0x88 },
1961 { 0x5b, 0x44 },
1962 { 0x5c, 0x67 },
1963 { 0x5d, 0x49 },
1964 { 0x5e, 0x0e },
1965 { 0x6c, 0x0a },
1966 { 0x6d, 0x55 },
1967 { 0x6e, 0x11 },
Jean-François Moinefc63de882011-01-13 05:35:18 -03001968 { 0x6f, 0x9f }, /* "9e for advance AWB" */
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001969 { 0x6a, 0x40 },
Jean-François Moine21867802010-11-12 06:12:09 -03001970 { OV7670_R01_BLUE, 0x40 },
1971 { OV7670_R02_RED, 0x60 },
1972 { OV7670_R13_COM8, OV7670_COM8_FASTAEC
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001973 | OV7670_COM8_AECSTEP
1974 | OV7670_COM8_BFILT
1975 | OV7670_COM8_AGC
1976 | OV7670_COM8_AEC
1977 | OV7670_COM8_AWB },
1978
1979/* Matrix coefficients */
1980 { 0x4f, 0x80 },
1981 { 0x50, 0x80 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001982 { 0x51, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001983 { 0x52, 0x22 },
1984 { 0x53, 0x5e },
1985 { 0x54, 0x80 },
1986 { 0x58, 0x9e },
1987
Jean-François Moine21867802010-11-12 06:12:09 -03001988 { OV7670_R41_COM16, OV7670_COM16_AWBGAIN },
1989 { OV7670_R3F_EDGE, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001990 { 0x75, 0x05 },
1991 { 0x76, 0xe1 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03001992 { 0x4c, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001993 { 0x77, 0x01 },
Jean-François Moine21867802010-11-12 06:12:09 -03001994 { OV7670_R3D_COM13, OV7670_COM13_GAMMA
Jean-Francois Moine4202f712008-09-03 17:12:15 -03001995 | OV7670_COM13_UVSAT
1996 | 2}, /* was 3 */
1997 { 0x4b, 0x09 },
1998 { 0xc9, 0x60 },
Jean-François Moine21867802010-11-12 06:12:09 -03001999 { OV7670_R41_COM16, 0x38 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03002000 { 0x56, 0x40 },
2001
2002 { 0x34, 0x11 },
Jean-François Moine21867802010-11-12 06:12:09 -03002003 { OV7670_R3B_COM11, OV7670_COM11_EXP|OV7670_COM11_HZAUTO },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03002004 { 0xa4, 0x88 },
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002005 { 0x96, 0x00 },
Jean-Francois Moine4202f712008-09-03 17:12:15 -03002006 { 0x97, 0x30 },
2007 { 0x98, 0x20 },
2008 { 0x99, 0x30 },
2009 { 0x9a, 0x84 },
2010 { 0x9b, 0x29 },
2011 { 0x9c, 0x03 },
2012 { 0x9d, 0x4c },
2013 { 0x9e, 0x3f },
2014 { 0x78, 0x04 },
2015
2016/* Extra-weird stuff. Some sort of multiplexor register */
2017 { 0x79, 0x01 },
2018 { 0xc8, 0xf0 },
2019 { 0x79, 0x0f },
2020 { 0xc8, 0x00 },
2021 { 0x79, 0x10 },
2022 { 0xc8, 0x7e },
2023 { 0x79, 0x0a },
2024 { 0xc8, 0x80 },
2025 { 0x79, 0x0b },
2026 { 0xc8, 0x01 },
2027 { 0x79, 0x0c },
2028 { 0xc8, 0x0f },
2029 { 0x79, 0x0d },
2030 { 0xc8, 0x20 },
2031 { 0x79, 0x09 },
2032 { 0xc8, 0x80 },
2033 { 0x79, 0x02 },
2034 { 0xc8, 0xc0 },
2035 { 0x79, 0x03 },
2036 { 0xc8, 0x40 },
2037 { 0x79, 0x05 },
2038 { 0xc8, 0x30 },
2039 { 0x79, 0x26 },
2040};
2041
2042static const struct ov_i2c_regvals norm_8610[] = {
2043 { 0x12, 0x80 },
2044 { 0x00, 0x00 },
2045 { 0x01, 0x80 },
2046 { 0x02, 0x80 },
2047 { 0x03, 0xc0 },
2048 { 0x04, 0x30 },
2049 { 0x05, 0x30 }, /* was 0x10, new from windrv 090403 */
2050 { 0x06, 0x70 }, /* was 0x80, new from windrv 090403 */
2051 { 0x0a, 0x86 },
2052 { 0x0b, 0xb0 },
2053 { 0x0c, 0x20 },
2054 { 0x0d, 0x20 },
2055 { 0x11, 0x01 },
2056 { 0x12, 0x25 },
2057 { 0x13, 0x01 },
2058 { 0x14, 0x04 },
2059 { 0x15, 0x01 }, /* Lin and Win think different about UV order */
2060 { 0x16, 0x03 },
2061 { 0x17, 0x38 }, /* was 0x2f, new from windrv 090403 */
2062 { 0x18, 0xea }, /* was 0xcf, new from windrv 090403 */
2063 { 0x19, 0x02 }, /* was 0x06, new from windrv 090403 */
2064 { 0x1a, 0xf5 },
2065 { 0x1b, 0x00 },
2066 { 0x20, 0xd0 }, /* was 0x90, new from windrv 090403 */
2067 { 0x23, 0xc0 }, /* was 0x00, new from windrv 090403 */
2068 { 0x24, 0x30 }, /* was 0x1d, new from windrv 090403 */
2069 { 0x25, 0x50 }, /* was 0x57, new from windrv 090403 */
2070 { 0x26, 0xa2 },
2071 { 0x27, 0xea },
2072 { 0x28, 0x00 },
2073 { 0x29, 0x00 },
2074 { 0x2a, 0x80 },
2075 { 0x2b, 0xc8 }, /* was 0xcc, new from windrv 090403 */
2076 { 0x2c, 0xac },
2077 { 0x2d, 0x45 }, /* was 0xd5, new from windrv 090403 */
2078 { 0x2e, 0x80 },
2079 { 0x2f, 0x14 }, /* was 0x01, new from windrv 090403 */
2080 { 0x4c, 0x00 },
2081 { 0x4d, 0x30 }, /* was 0x10, new from windrv 090403 */
2082 { 0x60, 0x02 }, /* was 0x01, new from windrv 090403 */
2083 { 0x61, 0x00 }, /* was 0x09, new from windrv 090403 */
2084 { 0x62, 0x5f }, /* was 0xd7, new from windrv 090403 */
2085 { 0x63, 0xff },
2086 { 0x64, 0x53 }, /* new windrv 090403 says 0x57,
2087 * maybe thats wrong */
2088 { 0x65, 0x00 },
2089 { 0x66, 0x55 },
2090 { 0x67, 0xb0 },
2091 { 0x68, 0xc0 }, /* was 0xaf, new from windrv 090403 */
2092 { 0x69, 0x02 },
2093 { 0x6a, 0x22 },
2094 { 0x6b, 0x00 },
2095 { 0x6c, 0x99 }, /* was 0x80, old windrv says 0x00, but
2096 * deleting bit7 colors the first images red */
2097 { 0x6d, 0x11 }, /* was 0x00, new from windrv 090403 */
2098 { 0x6e, 0x11 }, /* was 0x00, new from windrv 090403 */
2099 { 0x6f, 0x01 },
2100 { 0x70, 0x8b },
2101 { 0x71, 0x00 },
2102 { 0x72, 0x14 },
2103 { 0x73, 0x54 },
2104 { 0x74, 0x00 },/* 0x60? - was 0x00, new from windrv 090403 */
2105 { 0x75, 0x0e },
2106 { 0x76, 0x02 }, /* was 0x02, new from windrv 090403 */
2107 { 0x77, 0xff },
2108 { 0x78, 0x80 },
2109 { 0x79, 0x80 },
2110 { 0x7a, 0x80 },
2111 { 0x7b, 0x10 }, /* was 0x13, new from windrv 090403 */
2112 { 0x7c, 0x00 },
2113 { 0x7d, 0x08 }, /* was 0x09, new from windrv 090403 */
2114 { 0x7e, 0x08 }, /* was 0xc0, new from windrv 090403 */
2115 { 0x7f, 0xfb },
2116 { 0x80, 0x28 },
2117 { 0x81, 0x00 },
2118 { 0x82, 0x23 },
2119 { 0x83, 0x0b },
2120 { 0x84, 0x00 },
2121 { 0x85, 0x62 }, /* was 0x61, new from windrv 090403 */
2122 { 0x86, 0xc9 },
2123 { 0x87, 0x00 },
2124 { 0x88, 0x00 },
2125 { 0x89, 0x01 },
2126 { 0x12, 0x20 },
2127 { 0x12, 0x25 }, /* was 0x24, new from windrv 090403 */
2128};
2129
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002130static unsigned char ov7670_abs_to_sm(unsigned char v)
2131{
2132 if (v > 127)
2133 return v & 0x7f;
2134 return (128 - v) | 0x80;
2135}
2136
2137/* Write a OV519 register */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002138static void reg_w(struct sd *sd, u16 index, u16 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002139{
Hans de Goedea511ba92009-10-16 07:13:07 -03002140 int ret, req = 0;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002141
Jean-François Moinef8f20182010-11-12 07:54:02 -03002142 if (sd->gspca_dev.usb_err < 0)
2143 return;
2144
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002145 switch (sd->bridge) {
2146 case BRIDGE_OV511:
2147 case BRIDGE_OV511PLUS:
2148 req = 2;
2149 break;
2150 case BRIDGE_OVFX2:
Hans de Goedea511ba92009-10-16 07:13:07 -03002151 req = 0x0a;
2152 /* fall through */
2153 case BRIDGE_W9968CF:
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002154 PDEBUG(D_USBO, "SET %02x %04x %04x",
2155 req, value, index);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002156 ret = usb_control_msg(sd->gspca_dev.dev,
2157 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goedea511ba92009-10-16 07:13:07 -03002158 req,
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002159 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Hans de Goedea511ba92009-10-16 07:13:07 -03002160 value, index, NULL, 0, 500);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002161 goto leave;
2162 default:
2163 req = 1;
2164 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002165
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002166 PDEBUG(D_USBO, "SET %02x 0000 %04x %02x",
2167 req, index, value);
Jean-Francois Moine739570b2008-07-14 09:38:29 -03002168 sd->gspca_dev.usb_buf[0] = value;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002169 ret = usb_control_msg(sd->gspca_dev.dev,
2170 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goede49809d62009-06-07 12:10:39 -03002171 req,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002172 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2173 0, index,
Jean-Francois Moine739570b2008-07-14 09:38:29 -03002174 sd->gspca_dev.usb_buf, 1, 500);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002175leave:
Hans de Goedea511ba92009-10-16 07:13:07 -03002176 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002177 pr_err("reg_w %02x failed %d\n", index, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002178 sd->gspca_dev.usb_err = ret;
2179 return;
Hans de Goedea511ba92009-10-16 07:13:07 -03002180 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002181}
2182
Hans de Goedea511ba92009-10-16 07:13:07 -03002183/* Read from a OV519 register, note not valid for the w9968cf!! */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002184/* returns: negative is error, pos or zero is data */
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002185static int reg_r(struct sd *sd, u16 index)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002186{
2187 int ret;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002188 int req;
2189
Jean-François Moinef8f20182010-11-12 07:54:02 -03002190 if (sd->gspca_dev.usb_err < 0)
2191 return -1;
2192
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002193 switch (sd->bridge) {
2194 case BRIDGE_OV511:
2195 case BRIDGE_OV511PLUS:
2196 req = 3;
2197 break;
2198 case BRIDGE_OVFX2:
2199 req = 0x0b;
2200 break;
2201 default:
2202 req = 1;
2203 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002204
2205 ret = usb_control_msg(sd->gspca_dev.dev,
2206 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
Hans de Goede49809d62009-06-07 12:10:39 -03002207 req,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002208 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-Francois Moine739570b2008-07-14 09:38:29 -03002209 0, index, sd->gspca_dev.usb_buf, 1, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002210
Hans de Goedea511ba92009-10-16 07:13:07 -03002211 if (ret >= 0) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -03002212 ret = sd->gspca_dev.usb_buf[0];
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002213 PDEBUG(D_USBI, "GET %02x 0000 %04x %02x",
2214 req, index, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002215 } else {
Joe Perches133a9fe2011-08-21 19:56:57 -03002216 pr_err("reg_r %02x failed %d\n", index, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002217 sd->gspca_dev.usb_err = ret;
2218 }
Hans de Goedea511ba92009-10-16 07:13:07 -03002219
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002220 return ret;
2221}
2222
2223/* Read 8 values from a OV519 register */
2224static int reg_r8(struct sd *sd,
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002225 u16 index)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002226{
2227 int ret;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002228
Jean-François Moinef8f20182010-11-12 07:54:02 -03002229 if (sd->gspca_dev.usb_err < 0)
2230 return -1;
2231
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002232 ret = usb_control_msg(sd->gspca_dev.dev,
2233 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
2234 1, /* REQ_IO */
2235 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-Francois Moine739570b2008-07-14 09:38:29 -03002236 0, index, sd->gspca_dev.usb_buf, 8, 500);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002237
Jean-François Moinef8f20182010-11-12 07:54:02 -03002238 if (ret >= 0) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -03002239 ret = sd->gspca_dev.usb_buf[0];
Jean-François Moinef8f20182010-11-12 07:54:02 -03002240 } else {
Joe Perches133a9fe2011-08-21 19:56:57 -03002241 pr_err("reg_r8 %02x failed %d\n", index, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002242 sd->gspca_dev.usb_err = ret;
2243 }
Hans de Goedea511ba92009-10-16 07:13:07 -03002244
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002245 return ret;
2246}
2247
2248/*
2249 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
2250 * the same position as 1's in "mask" are cleared and set to "value". Bits
2251 * that are in the same position as 0's in "mask" are preserved, regardless
2252 * of their respective state in "value".
2253 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002254static void reg_w_mask(struct sd *sd,
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002255 u16 index,
2256 u8 value,
2257 u8 mask)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002258{
2259 int ret;
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002260 u8 oldval;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002261
2262 if (mask != 0xff) {
2263 value &= mask; /* Enforce mask on value */
2264 ret = reg_r(sd, index);
2265 if (ret < 0)
Jean-François Moinef8f20182010-11-12 07:54:02 -03002266 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002267
2268 oldval = ret & ~mask; /* Clear the masked bits */
2269 value |= oldval; /* Set the desired bits */
2270 }
Jean-François Moinef8f20182010-11-12 07:54:02 -03002271 reg_w(sd, index, value);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002272}
2273
2274/*
Hans de Goede49809d62009-06-07 12:10:39 -03002275 * Writes multiple (n) byte value to a single register. Only valid with certain
2276 * registers (0x30 and 0xc4 - 0xce).
2277 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002278static void ov518_reg_w32(struct sd *sd, u16 index, u32 value, int n)
Hans de Goede49809d62009-06-07 12:10:39 -03002279{
2280 int ret;
2281
Jean-François Moinef8f20182010-11-12 07:54:02 -03002282 if (sd->gspca_dev.usb_err < 0)
2283 return;
2284
Jean-Francois Moine83955552009-12-12 06:58:01 -03002285 *((__le32 *) sd->gspca_dev.usb_buf) = __cpu_to_le32(value);
Hans de Goede49809d62009-06-07 12:10:39 -03002286
2287 ret = usb_control_msg(sd->gspca_dev.dev,
2288 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2289 1 /* REG_IO */,
2290 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2291 0, index,
2292 sd->gspca_dev.usb_buf, n, 500);
Hans de Goedea511ba92009-10-16 07:13:07 -03002293 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002294 pr_err("reg_w32 %02x failed %d\n", index, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002295 sd->gspca_dev.usb_err = ret;
Hans de Goedea511ba92009-10-16 07:13:07 -03002296 }
Hans de Goede49809d62009-06-07 12:10:39 -03002297}
2298
Jean-François Moinef8f20182010-11-12 07:54:02 -03002299static void ov511_i2c_w(struct sd *sd, u8 reg, u8 value)
Hans de Goede1876bb92009-06-14 06:45:50 -03002300{
2301 int rc, retries;
2302
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002303 PDEBUG(D_USBO, "ov511_i2c_w %02x %02x", reg, value);
Hans de Goede1876bb92009-06-14 06:45:50 -03002304
2305 /* Three byte write cycle */
2306 for (retries = 6; ; ) {
2307 /* Select camera register */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002308 reg_w(sd, R51x_I2C_SADDR_3, reg);
Hans de Goede1876bb92009-06-14 06:45:50 -03002309
2310 /* Write "value" to I2C data port of OV511 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002311 reg_w(sd, R51x_I2C_DATA, value);
Hans de Goede1876bb92009-06-14 06:45:50 -03002312
2313 /* Initiate 3-byte write cycle */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002314 reg_w(sd, R511_I2C_CTL, 0x01);
Hans de Goede1876bb92009-06-14 06:45:50 -03002315
Jean-Francois Moine83955552009-12-12 06:58:01 -03002316 do {
Hans de Goede1876bb92009-06-14 06:45:50 -03002317 rc = reg_r(sd, R511_I2C_CTL);
Jean-Francois Moine83955552009-12-12 06:58:01 -03002318 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
Hans de Goede1876bb92009-06-14 06:45:50 -03002319
2320 if (rc < 0)
Jean-François Moinef8f20182010-11-12 07:54:02 -03002321 return;
Hans de Goede1876bb92009-06-14 06:45:50 -03002322
2323 if ((rc & 2) == 0) /* Ack? */
2324 break;
2325 if (--retries < 0) {
2326 PDEBUG(D_USBO, "i2c write retries exhausted");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002327 return;
Hans de Goede1876bb92009-06-14 06:45:50 -03002328 }
2329 }
Hans de Goede1876bb92009-06-14 06:45:50 -03002330}
2331
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002332static int ov511_i2c_r(struct sd *sd, u8 reg)
Hans de Goede1876bb92009-06-14 06:45:50 -03002333{
2334 int rc, value, retries;
2335
2336 /* Two byte write cycle */
2337 for (retries = 6; ; ) {
2338 /* Select camera register */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002339 reg_w(sd, R51x_I2C_SADDR_2, reg);
Hans de Goede1876bb92009-06-14 06:45:50 -03002340
2341 /* Initiate 2-byte write cycle */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002342 reg_w(sd, R511_I2C_CTL, 0x03);
Hans de Goede1876bb92009-06-14 06:45:50 -03002343
Jean-Francois Moine83955552009-12-12 06:58:01 -03002344 do {
Hans de Goede1876bb92009-06-14 06:45:50 -03002345 rc = reg_r(sd, R511_I2C_CTL);
Jean-Francois Moine83955552009-12-12 06:58:01 -03002346 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
Hans de Goede1876bb92009-06-14 06:45:50 -03002347
2348 if (rc < 0)
2349 return rc;
2350
2351 if ((rc & 2) == 0) /* Ack? */
2352 break;
2353
2354 /* I2C abort */
2355 reg_w(sd, R511_I2C_CTL, 0x10);
2356
2357 if (--retries < 0) {
2358 PDEBUG(D_USBI, "i2c write retries exhausted");
2359 return -1;
2360 }
2361 }
2362
2363 /* Two byte read cycle */
2364 for (retries = 6; ; ) {
2365 /* Initiate 2-byte read cycle */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002366 reg_w(sd, R511_I2C_CTL, 0x05);
Hans de Goede1876bb92009-06-14 06:45:50 -03002367
Jean-Francois Moine83955552009-12-12 06:58:01 -03002368 do {
Hans de Goede1876bb92009-06-14 06:45:50 -03002369 rc = reg_r(sd, R511_I2C_CTL);
Jean-Francois Moine83955552009-12-12 06:58:01 -03002370 } while (rc > 0 && ((rc & 1) == 0)); /* Retry until idle */
Hans de Goede1876bb92009-06-14 06:45:50 -03002371
2372 if (rc < 0)
2373 return rc;
2374
2375 if ((rc & 2) == 0) /* Ack? */
2376 break;
2377
2378 /* I2C abort */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002379 reg_w(sd, R511_I2C_CTL, 0x10);
Hans de Goede1876bb92009-06-14 06:45:50 -03002380
2381 if (--retries < 0) {
2382 PDEBUG(D_USBI, "i2c read retries exhausted");
2383 return -1;
2384 }
2385 }
2386
2387 value = reg_r(sd, R51x_I2C_DATA);
2388
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002389 PDEBUG(D_USBI, "ov511_i2c_r %02x %02x", reg, value);
Hans de Goede1876bb92009-06-14 06:45:50 -03002390
2391 /* This is needed to make i2c_w() work */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002392 reg_w(sd, R511_I2C_CTL, 0x05);
Hans de Goede1876bb92009-06-14 06:45:50 -03002393
2394 return value;
2395}
Hans de Goede49809d62009-06-07 12:10:39 -03002396
2397/*
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002398 * The OV518 I2C I/O procedure is different, hence, this function.
2399 * This is normally only called from i2c_w(). Note that this function
2400 * always succeeds regardless of whether the sensor is present and working.
2401 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002402static void ov518_i2c_w(struct sd *sd,
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002403 u8 reg,
2404 u8 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002405{
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002406 PDEBUG(D_USBO, "ov518_i2c_w %02x %02x", reg, value);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002407
2408 /* Select camera register */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002409 reg_w(sd, R51x_I2C_SADDR_3, reg);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002410
2411 /* Write "value" to I2C data port of OV511 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002412 reg_w(sd, R51x_I2C_DATA, value);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002413
2414 /* Initiate 3-byte write cycle */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002415 reg_w(sd, R518_I2C_CTL, 0x01);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002416
2417 /* wait for write complete */
2418 msleep(4);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002419 reg_r8(sd, R518_I2C_CTL);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002420}
2421
2422/*
2423 * returns: negative is error, pos or zero is data
2424 *
2425 * The OV518 I2C I/O procedure is different, hence, this function.
2426 * This is normally only called from i2c_r(). Note that this function
2427 * always succeeds regardless of whether the sensor is present and working.
2428 */
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002429static int ov518_i2c_r(struct sd *sd, u8 reg)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002430{
Jean-François Moinef8f20182010-11-12 07:54:02 -03002431 int value;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002432
2433 /* Select camera register */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002434 reg_w(sd, R51x_I2C_SADDR_2, reg);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002435
2436 /* Initiate 2-byte write cycle */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002437 reg_w(sd, R518_I2C_CTL, 0x03);
Jean-François Moineb877a9a2011-07-03 05:17:27 -03002438 reg_r8(sd, R518_I2C_CTL);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002439
2440 /* Initiate 2-byte read cycle */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002441 reg_w(sd, R518_I2C_CTL, 0x05);
Jean-François Moineb877a9a2011-07-03 05:17:27 -03002442 reg_r8(sd, R518_I2C_CTL);
2443
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002444 value = reg_r(sd, R51x_I2C_DATA);
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002445 PDEBUG(D_USBI, "ov518_i2c_r %02x %02x", reg, value);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002446 return value;
2447}
2448
Jean-François Moinef8f20182010-11-12 07:54:02 -03002449static void ovfx2_i2c_w(struct sd *sd, u8 reg, u8 value)
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002450{
2451 int ret;
2452
Jean-François Moinef8f20182010-11-12 07:54:02 -03002453 if (sd->gspca_dev.usb_err < 0)
2454 return;
2455
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002456 ret = usb_control_msg(sd->gspca_dev.dev,
2457 usb_sndctrlpipe(sd->gspca_dev.dev, 0),
2458 0x02,
2459 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002460 (u16) value, (u16) reg, NULL, 0, 500);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002461
Hans de Goedea511ba92009-10-16 07:13:07 -03002462 if (ret < 0) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002463 pr_err("ovfx2_i2c_w %02x failed %d\n", reg, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002464 sd->gspca_dev.usb_err = ret;
Hans de Goedea511ba92009-10-16 07:13:07 -03002465 }
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002466
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002467 PDEBUG(D_USBO, "ovfx2_i2c_w %02x %02x", reg, value);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002468}
2469
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002470static int ovfx2_i2c_r(struct sd *sd, u8 reg)
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002471{
2472 int ret;
2473
Jean-François Moinef8f20182010-11-12 07:54:02 -03002474 if (sd->gspca_dev.usb_err < 0)
2475 return -1;
2476
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002477 ret = usb_control_msg(sd->gspca_dev.dev,
2478 usb_rcvctrlpipe(sd->gspca_dev.dev, 0),
2479 0x03,
2480 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002481 0, (u16) reg, sd->gspca_dev.usb_buf, 1, 500);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002482
2483 if (ret >= 0) {
2484 ret = sd->gspca_dev.usb_buf[0];
Jean-François Moine858ea5e2010-11-12 13:53:10 -03002485 PDEBUG(D_USBI, "ovfx2_i2c_r %02x %02x", reg, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002486 } else {
Joe Perches133a9fe2011-08-21 19:56:57 -03002487 pr_err("ovfx2_i2c_r %02x failed %d\n", reg, ret);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002488 sd->gspca_dev.usb_err = ret;
2489 }
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002490
2491 return ret;
2492}
2493
Jean-François Moinef8f20182010-11-12 07:54:02 -03002494static void i2c_w(struct sd *sd, u8 reg, u8 value)
Hans de Goede1876bb92009-06-14 06:45:50 -03002495{
Hans de Goedefb1f9022009-10-16 07:42:53 -03002496 if (sd->sensor_reg_cache[reg] == value)
Jean-François Moinef8f20182010-11-12 07:54:02 -03002497 return;
Hans de Goedefb1f9022009-10-16 07:42:53 -03002498
Hans de Goede1876bb92009-06-14 06:45:50 -03002499 switch (sd->bridge) {
2500 case BRIDGE_OV511:
2501 case BRIDGE_OV511PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002502 ov511_i2c_w(sd, reg, value);
Hans de Goedefb1f9022009-10-16 07:42:53 -03002503 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002504 case BRIDGE_OV518:
2505 case BRIDGE_OV518PLUS:
2506 case BRIDGE_OV519:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002507 ov518_i2c_w(sd, reg, value);
Hans de Goedefb1f9022009-10-16 07:42:53 -03002508 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002509 case BRIDGE_OVFX2:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002510 ovfx2_i2c_w(sd, reg, value);
Hans de Goedefb1f9022009-10-16 07:42:53 -03002511 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03002512 case BRIDGE_W9968CF:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002513 w9968cf_i2c_w(sd, reg, value);
Hans de Goedefb1f9022009-10-16 07:42:53 -03002514 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002515 }
Hans de Goedefb1f9022009-10-16 07:42:53 -03002516
Jean-François Moinef8f20182010-11-12 07:54:02 -03002517 if (sd->gspca_dev.usb_err >= 0) {
Hans de Goedefb1f9022009-10-16 07:42:53 -03002518 /* Up on sensor reset empty the register cache */
2519 if (reg == 0x12 && (value & 0x80))
2520 memset(sd->sensor_reg_cache, -1,
Jean-François Moine87bae742010-11-12 05:31:34 -03002521 sizeof(sd->sensor_reg_cache));
Hans de Goedefb1f9022009-10-16 07:42:53 -03002522 else
2523 sd->sensor_reg_cache[reg] = value;
2524 }
Hans de Goede1876bb92009-06-14 06:45:50 -03002525}
2526
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002527static int i2c_r(struct sd *sd, u8 reg)
Hans de Goede1876bb92009-06-14 06:45:50 -03002528{
Hans de Goede8394bcf2009-10-16 11:26:22 -03002529 int ret = -1;
Hans de Goedefb1f9022009-10-16 07:42:53 -03002530
2531 if (sd->sensor_reg_cache[reg] != -1)
2532 return sd->sensor_reg_cache[reg];
2533
Hans de Goede1876bb92009-06-14 06:45:50 -03002534 switch (sd->bridge) {
2535 case BRIDGE_OV511:
2536 case BRIDGE_OV511PLUS:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002537 ret = ov511_i2c_r(sd, reg);
2538 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002539 case BRIDGE_OV518:
2540 case BRIDGE_OV518PLUS:
2541 case BRIDGE_OV519:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002542 ret = ov518_i2c_r(sd, reg);
2543 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002544 case BRIDGE_OVFX2:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002545 ret = ovfx2_i2c_r(sd, reg);
2546 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03002547 case BRIDGE_W9968CF:
Hans de Goedefb1f9022009-10-16 07:42:53 -03002548 ret = w9968cf_i2c_r(sd, reg);
2549 break;
Hans de Goede1876bb92009-06-14 06:45:50 -03002550 }
Hans de Goedefb1f9022009-10-16 07:42:53 -03002551
2552 if (ret >= 0)
2553 sd->sensor_reg_cache[reg] = ret;
2554
2555 return ret;
Hans de Goede1876bb92009-06-14 06:45:50 -03002556}
2557
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002558/* Writes bits at positions specified by mask to an I2C reg. Bits that are in
2559 * the same position as 1's in "mask" are cleared and set to "value". Bits
2560 * that are in the same position as 0's in "mask" are preserved, regardless
2561 * of their respective state in "value".
2562 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002563static void i2c_w_mask(struct sd *sd,
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002564 u8 reg,
2565 u8 value,
2566 u8 mask)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002567{
2568 int rc;
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002569 u8 oldval;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002570
2571 value &= mask; /* Enforce mask on value */
2572 rc = i2c_r(sd, reg);
2573 if (rc < 0)
Jean-François Moinef8f20182010-11-12 07:54:02 -03002574 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002575 oldval = rc & ~mask; /* Clear the masked bits */
2576 value |= oldval; /* Set the desired bits */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002577 i2c_w(sd, reg, value);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002578}
2579
2580/* Temporarily stops OV511 from functioning. Must do this before changing
2581 * registers while the camera is streaming */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002582static inline void ov51x_stop(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002583{
2584 PDEBUG(D_STREAM, "stopping");
2585 sd->stopped = 1;
Hans de Goede49809d62009-06-07 12:10:39 -03002586 switch (sd->bridge) {
2587 case BRIDGE_OV511:
2588 case BRIDGE_OV511PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002589 reg_w(sd, R51x_SYS_RESET, 0x3d);
2590 break;
Hans de Goede49809d62009-06-07 12:10:39 -03002591 case BRIDGE_OV518:
2592 case BRIDGE_OV518PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002593 reg_w_mask(sd, R51x_SYS_RESET, 0x3a, 0x3a);
2594 break;
Hans de Goede49809d62009-06-07 12:10:39 -03002595 case BRIDGE_OV519:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002596 reg_w(sd, OV519_R51_RESET1, 0x0f);
Jean-François Moine5927abc2010-11-12 15:32:29 -03002597 reg_w(sd, OV519_R51_RESET1, 0x00);
2598 reg_w(sd, 0x22, 0x00); /* FRAR */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002599 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002600 case BRIDGE_OVFX2:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002601 reg_w_mask(sd, 0x0f, 0x00, 0x02);
2602 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03002603 case BRIDGE_W9968CF:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002604 reg_w(sd, 0x3c, 0x0a05); /* stop USB transfer */
2605 break;
Hans de Goede49809d62009-06-07 12:10:39 -03002606 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002607}
2608
2609/* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
2610 * actually stopped (for performance). */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002611static inline void ov51x_restart(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002612{
2613 PDEBUG(D_STREAM, "restarting");
2614 if (!sd->stopped)
Jean-François Moinef8f20182010-11-12 07:54:02 -03002615 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002616 sd->stopped = 0;
2617
2618 /* Reinitialize the stream */
Hans de Goede49809d62009-06-07 12:10:39 -03002619 switch (sd->bridge) {
2620 case BRIDGE_OV511:
2621 case BRIDGE_OV511PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002622 reg_w(sd, R51x_SYS_RESET, 0x00);
2623 break;
Hans de Goede49809d62009-06-07 12:10:39 -03002624 case BRIDGE_OV518:
2625 case BRIDGE_OV518PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002626 reg_w(sd, 0x2f, 0x80);
2627 reg_w(sd, R51x_SYS_RESET, 0x00);
2628 break;
Hans de Goede49809d62009-06-07 12:10:39 -03002629 case BRIDGE_OV519:
Jean-François Moine5927abc2010-11-12 15:32:29 -03002630 reg_w(sd, OV519_R51_RESET1, 0x0f);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002631 reg_w(sd, OV519_R51_RESET1, 0x00);
Jean-François Moine5927abc2010-11-12 15:32:29 -03002632 reg_w(sd, 0x22, 0x1d); /* FRAR */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002633 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002634 case BRIDGE_OVFX2:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002635 reg_w_mask(sd, 0x0f, 0x02, 0x02);
2636 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03002637 case BRIDGE_W9968CF:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002638 reg_w(sd, 0x3c, 0x8a05); /* USB FIFO enable */
2639 break;
Hans de Goede49809d62009-06-07 12:10:39 -03002640 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002641}
2642
Jean-François Moinef8f20182010-11-12 07:54:02 -03002643static void ov51x_set_slave_ids(struct sd *sd, u8 slave);
Hans de Goede229bb7d2009-10-11 07:41:46 -03002644
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002645/* This does an initial reset of an OmniVision sensor and ensures that I2C
2646 * is synchronized. Returns <0 on failure.
2647 */
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002648static int init_ov_sensor(struct sd *sd, u8 slave)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002649{
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002650 int i;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002651
Jean-François Moinef8f20182010-11-12 07:54:02 -03002652 ov51x_set_slave_ids(sd, slave);
Hans de Goede229bb7d2009-10-11 07:41:46 -03002653
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002654 /* Reset the sensor */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002655 i2c_w(sd, 0x12, 0x80);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002656
2657 /* Wait for it to initialize */
2658 msleep(150);
2659
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002660 for (i = 0; i < i2c_detect_tries; i++) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002661 if (i2c_r(sd, OV7610_REG_ID_HIGH) == 0x7f &&
2662 i2c_r(sd, OV7610_REG_ID_LOW) == 0xa2) {
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03002663 PDEBUG(D_PROBE, "I2C synced in %d attempt(s)", i);
2664 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002665 }
2666
2667 /* Reset the sensor */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002668 i2c_w(sd, 0x12, 0x80);
2669
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002670 /* Wait for it to initialize */
2671 msleep(150);
Jean-François Moine87bae742010-11-12 05:31:34 -03002672
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002673 /* Dummy read to sync I2C */
2674 if (i2c_r(sd, 0x00) < 0)
Jean-François Moinef8f20182010-11-12 07:54:02 -03002675 return -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002676 }
Jean-François Moinef8f20182010-11-12 07:54:02 -03002677 return -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002678}
2679
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002680/* Set the read and write slave IDs. The "slave" argument is the write slave,
2681 * and the read slave will be set to (slave + 1).
2682 * This should not be called from outside the i2c I/O functions.
2683 * Sets I2C read and write slave IDs. Returns <0 for error
2684 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002685static void ov51x_set_slave_ids(struct sd *sd,
Jean-François Moine9d1593a2010-11-11 08:04:06 -03002686 u8 slave)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002687{
Hans de Goedea511ba92009-10-16 07:13:07 -03002688 switch (sd->bridge) {
2689 case BRIDGE_OVFX2:
Jean-François Moinef8f20182010-11-12 07:54:02 -03002690 reg_w(sd, OVFX2_I2C_ADDR, slave);
2691 return;
Hans de Goedea511ba92009-10-16 07:13:07 -03002692 case BRIDGE_W9968CF:
2693 sd->sensor_addr = slave;
Jean-François Moinef8f20182010-11-12 07:54:02 -03002694 return;
Hans de Goedea511ba92009-10-16 07:13:07 -03002695 }
Hans de Goedeb46aaa02009-10-12 10:07:57 -03002696
Jean-François Moinef8f20182010-11-12 07:54:02 -03002697 reg_w(sd, R51x_I2C_W_SID, slave);
2698 reg_w(sd, R51x_I2C_R_SID, slave + 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002699}
2700
Jean-François Moinef8f20182010-11-12 07:54:02 -03002701static void write_regvals(struct sd *sd,
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03002702 const struct ov_regvals *regvals,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002703 int n)
2704{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002705 while (--n >= 0) {
Jean-François Moinef8f20182010-11-12 07:54:02 -03002706 reg_w(sd, regvals->reg, regvals->val);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002707 regvals++;
2708 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002709}
2710
Jean-François Moinef8f20182010-11-12 07:54:02 -03002711static void write_i2c_regvals(struct sd *sd,
2712 const struct ov_i2c_regvals *regvals,
2713 int n)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002714{
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002715 while (--n >= 0) {
Jean-François Moinef8f20182010-11-12 07:54:02 -03002716 i2c_w(sd, regvals->reg, regvals->val);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002717 regvals++;
2718 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002719}
2720
2721/****************************************************************************
2722 *
2723 * OV511 and sensor configuration
2724 *
2725 ***************************************************************************/
2726
Jean-François Moine798ae152011-05-23 05:38:10 -03002727/* This initializes the OV2x10 / OV3610 / OV3620 / OV9600 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002728static void ov_hires_configure(struct sd *sd)
Hans de Goede635118d2009-10-11 09:49:03 -03002729{
2730 int high, low;
2731
2732 if (sd->bridge != BRIDGE_OVFX2) {
Joe Perches133a9fe2011-08-21 19:56:57 -03002733 pr_err("error hires sensors only supported with ovfx2\n");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002734 return;
Hans de Goede635118d2009-10-11 09:49:03 -03002735 }
2736
2737 PDEBUG(D_PROBE, "starting ov hires configuration");
2738
2739 /* Detect sensor (sub)type */
2740 high = i2c_r(sd, 0x0a);
2741 low = i2c_r(sd, 0x0b);
2742 /* info("%x, %x", high, low); */
Jean-François Moine798ae152011-05-23 05:38:10 -03002743 switch (high) {
2744 case 0x96:
2745 switch (low) {
2746 case 0x40:
2747 PDEBUG(D_PROBE, "Sensor is a OV2610");
2748 sd->sensor = SEN_OV2610;
2749 return;
2750 case 0x41:
2751 PDEBUG(D_PROBE, "Sensor is a OV2610AE");
2752 sd->sensor = SEN_OV2610AE;
2753 return;
2754 case 0xb1:
2755 PDEBUG(D_PROBE, "Sensor is a OV9600");
2756 sd->sensor = SEN_OV9600;
2757 return;
2758 }
2759 break;
2760 case 0x36:
2761 if ((low & 0x0f) == 0x00) {
2762 PDEBUG(D_PROBE, "Sensor is a OV3610");
2763 sd->sensor = SEN_OV3610;
2764 return;
2765 }
2766 break;
Hans de Goede635118d2009-10-11 09:49:03 -03002767 }
Joe Perches133a9fe2011-08-21 19:56:57 -03002768 pr_err("Error unknown sensor type: %02x%02x\n", high, low);
Hans de Goede635118d2009-10-11 09:49:03 -03002769}
2770
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002771/* This initializes the OV8110, OV8610 sensor. The OV8110 uses
2772 * the same register settings as the OV8610, since they are very similar.
2773 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002774static void ov8xx0_configure(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002775{
2776 int rc;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002777
2778 PDEBUG(D_PROBE, "starting ov8xx0 configuration");
2779
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002780 /* Detect sensor (sub)type */
2781 rc = i2c_r(sd, OV7610_REG_COM_I);
2782 if (rc < 0) {
2783 PDEBUG(D_ERR, "Error detecting sensor type");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002784 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002785 }
Jean-François Moinef8f20182010-11-12 07:54:02 -03002786 if ((rc & 3) == 1)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002787 sd->sensor = SEN_OV8610;
Jean-François Moinef8f20182010-11-12 07:54:02 -03002788 else
Joe Perches133a9fe2011-08-21 19:56:57 -03002789 pr_err("Unknown image sensor version: %d\n", rc & 3);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002790}
2791
2792/* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
2793 * the same register settings as the OV7610, since they are very similar.
2794 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002795static void ov7xx0_configure(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002796{
2797 int rc, high, low;
2798
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002799 PDEBUG(D_PROBE, "starting OV7xx0 configuration");
2800
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002801 /* Detect sensor (sub)type */
2802 rc = i2c_r(sd, OV7610_REG_COM_I);
2803
2804 /* add OV7670 here
2805 * it appears to be wrongly detected as a 7610 by default */
2806 if (rc < 0) {
2807 PDEBUG(D_ERR, "Error detecting sensor type");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002808 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002809 }
2810 if ((rc & 3) == 3) {
2811 /* quick hack to make OV7670s work */
2812 high = i2c_r(sd, 0x0a);
2813 low = i2c_r(sd, 0x0b);
2814 /* info("%x, %x", high, low); */
Jean-François Moine7a5a4142010-12-25 13:58:45 -03002815 if (high == 0x76 && (low & 0xf0) == 0x70) {
2816 PDEBUG(D_PROBE, "Sensor is an OV76%02x", low);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002817 sd->sensor = SEN_OV7670;
2818 } else {
2819 PDEBUG(D_PROBE, "Sensor is an OV7610");
2820 sd->sensor = SEN_OV7610;
2821 }
2822 } else if ((rc & 3) == 1) {
2823 /* I don't know what's different about the 76BE yet. */
Hans de Goedeb282d872009-06-14 19:10:40 -03002824 if (i2c_r(sd, 0x15) & 1) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002825 PDEBUG(D_PROBE, "Sensor is an OV7620AE");
Hans de Goede859cc472010-01-07 15:42:35 -03002826 sd->sensor = SEN_OV7620AE;
Hans de Goedeb282d872009-06-14 19:10:40 -03002827 } else {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002828 PDEBUG(D_PROBE, "Sensor is an OV76BE");
Hans de Goedeb282d872009-06-14 19:10:40 -03002829 sd->sensor = SEN_OV76BE;
2830 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002831 } else if ((rc & 3) == 0) {
2832 /* try to read product id registers */
2833 high = i2c_r(sd, 0x0a);
2834 if (high < 0) {
2835 PDEBUG(D_ERR, "Error detecting camera chip PID");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002836 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002837 }
2838 low = i2c_r(sd, 0x0b);
2839 if (low < 0) {
2840 PDEBUG(D_ERR, "Error detecting camera chip VER");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002841 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002842 }
2843 if (high == 0x76) {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002844 switch (low) {
2845 case 0x30:
Joe Perches133a9fe2011-08-21 19:56:57 -03002846 pr_err("Sensor is an OV7630/OV7635\n");
2847 pr_err("7630 is not supported by this driver\n");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002848 return;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002849 case 0x40:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002850 PDEBUG(D_PROBE, "Sensor is an OV7645");
2851 sd->sensor = SEN_OV7640; /* FIXME */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002852 break;
2853 case 0x45:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002854 PDEBUG(D_PROBE, "Sensor is an OV7645B");
2855 sd->sensor = SEN_OV7640; /* FIXME */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002856 break;
2857 case 0x48:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002858 PDEBUG(D_PROBE, "Sensor is an OV7648");
Hans de Goede035d3a32010-01-09 08:14:43 -03002859 sd->sensor = SEN_OV7648;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002860 break;
Jean-François Moine42e142f2010-11-13 05:10:27 -03002861 case 0x60:
2862 PDEBUG(D_PROBE, "Sensor is a OV7660");
2863 sd->sensor = SEN_OV7660;
2864 sd->invert_led = 0;
2865 break;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002866 default:
2867 PDEBUG(D_PROBE, "Unknown sensor: 0x76%x", low);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002868 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002869 }
2870 } else {
2871 PDEBUG(D_PROBE, "Sensor is an OV7620");
2872 sd->sensor = SEN_OV7620;
2873 }
2874 } else {
Joe Perches133a9fe2011-08-21 19:56:57 -03002875 pr_err("Unknown image sensor version: %d\n", rc & 3);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002876 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002877}
2878
2879/* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
Jean-François Moinef8f20182010-11-12 07:54:02 -03002880static void ov6xx0_configure(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002881{
2882 int rc;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03002883 PDEBUG(D_PROBE, "starting OV6xx0 configuration");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002884
2885 /* Detect sensor (sub)type */
2886 rc = i2c_r(sd, OV7610_REG_COM_I);
2887 if (rc < 0) {
2888 PDEBUG(D_ERR, "Error detecting sensor type");
Jean-François Moinef8f20182010-11-12 07:54:02 -03002889 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002890 }
2891
2892 /* Ugh. The first two bits are the version bits, but
2893 * the entire register value must be used. I guess OVT
2894 * underestimated how many variants they would make. */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002895 switch (rc) {
2896 case 0x00:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002897 sd->sensor = SEN_OV6630;
Joe Perches133a9fe2011-08-21 19:56:57 -03002898 pr_warn("WARNING: Sensor is an OV66308. Your camera may have been misdetected in previous driver versions.\n");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002899 break;
2900 case 0x01:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002901 sd->sensor = SEN_OV6620;
Hans de Goede7d971372009-06-14 05:28:17 -03002902 PDEBUG(D_PROBE, "Sensor is an OV6620");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002903 break;
2904 case 0x02:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002905 sd->sensor = SEN_OV6630;
2906 PDEBUG(D_PROBE, "Sensor is an OV66308AE");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002907 break;
2908 case 0x03:
Hans de Goede7d971372009-06-14 05:28:17 -03002909 sd->sensor = SEN_OV66308AF;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002910 PDEBUG(D_PROBE, "Sensor is an OV66308AF");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002911 break;
2912 case 0x90:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002913 sd->sensor = SEN_OV6630;
Joe Perches133a9fe2011-08-21 19:56:57 -03002914 pr_warn("WARNING: Sensor is an OV66307. Your camera may have been misdetected in previous driver versions.\n");
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002915 break;
2916 default:
Joe Perches133a9fe2011-08-21 19:56:57 -03002917 pr_err("FATAL: Unknown sensor version: 0x%02x\n", rc);
Jean-François Moinef8f20182010-11-12 07:54:02 -03002918 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002919 }
2920
2921 /* Set sensor-specific vars */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03002922 sd->sif = 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002923}
2924
2925/* Turns on or off the LED. Only has an effect with OV511+/OV518(+)/OV519 */
2926static void ov51x_led_control(struct sd *sd, int on)
2927{
Hans de Goede9e4d8252009-06-14 06:25:06 -03002928 if (sd->invert_led)
2929 on = !on;
2930
Hans de Goede49809d62009-06-07 12:10:39 -03002931 switch (sd->bridge) {
2932 /* OV511 has no LED control */
2933 case BRIDGE_OV511PLUS:
Jean-François Moinea23acec42010-11-12 15:07:35 -03002934 reg_w(sd, R511_SYS_LED_CTL, on);
Hans de Goede49809d62009-06-07 12:10:39 -03002935 break;
2936 case BRIDGE_OV518:
2937 case BRIDGE_OV518PLUS:
Jean-François Moinea23acec42010-11-12 15:07:35 -03002938 reg_w_mask(sd, R518_GPIO_OUT, 0x02 * on, 0x02);
Hans de Goede49809d62009-06-07 12:10:39 -03002939 break;
2940 case BRIDGE_OV519:
Jean-François Moinea23acec42010-11-12 15:07:35 -03002941 reg_w_mask(sd, OV519_GPIO_DATA_OUT0, on, 1);
Hans de Goede49809d62009-06-07 12:10:39 -03002942 break;
2943 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03002944}
2945
Hans de Goede417a4d22010-02-19 07:37:08 -03002946static void sd_reset_snapshot(struct gspca_dev *gspca_dev)
2947{
2948 struct sd *sd = (struct sd *) gspca_dev;
2949
2950 if (!sd->snapshot_needs_reset)
2951 return;
2952
2953 /* Note it is important that we clear sd->snapshot_needs_reset,
2954 before actually clearing the snapshot state in the bridge
2955 otherwise we might race with the pkt_scan interrupt handler */
2956 sd->snapshot_needs_reset = 0;
2957
2958 switch (sd->bridge) {
Hans de Goede88e8d202010-02-20 04:45:49 -03002959 case BRIDGE_OV511:
2960 case BRIDGE_OV511PLUS:
2961 reg_w(sd, R51x_SYS_SNAP, 0x02);
2962 reg_w(sd, R51x_SYS_SNAP, 0x00);
2963 break;
Hans de Goede92e232a2010-02-20 04:30:45 -03002964 case BRIDGE_OV518:
2965 case BRIDGE_OV518PLUS:
2966 reg_w(sd, R51x_SYS_SNAP, 0x02); /* Reset */
2967 reg_w(sd, R51x_SYS_SNAP, 0x01); /* Enable */
2968 break;
Hans de Goede417a4d22010-02-19 07:37:08 -03002969 case BRIDGE_OV519:
2970 reg_w(sd, R51x_SYS_RESET, 0x40);
2971 reg_w(sd, R51x_SYS_RESET, 0x00);
2972 break;
2973 }
2974}
2975
Jean-François Moinef8f20182010-11-12 07:54:02 -03002976static void ov51x_upload_quan_tables(struct sd *sd)
Hans de Goede49809d62009-06-07 12:10:39 -03002977{
Hans de Goede1876bb92009-06-14 06:45:50 -03002978 const unsigned char yQuanTable511[] = {
2979 0, 1, 1, 2, 2, 3, 3, 4,
2980 1, 1, 1, 2, 2, 3, 4, 4,
2981 1, 1, 2, 2, 3, 4, 4, 4,
2982 2, 2, 2, 3, 4, 4, 4, 4,
2983 2, 2, 3, 4, 4, 5, 5, 5,
2984 3, 3, 4, 4, 5, 5, 5, 5,
2985 3, 4, 4, 4, 5, 5, 5, 5,
2986 4, 4, 4, 4, 5, 5, 5, 5
2987 };
2988
2989 const unsigned char uvQuanTable511[] = {
2990 0, 2, 2, 3, 4, 4, 4, 4,
2991 2, 2, 2, 4, 4, 4, 4, 4,
2992 2, 2, 3, 4, 4, 4, 4, 4,
2993 3, 4, 4, 4, 4, 4, 4, 4,
2994 4, 4, 4, 4, 4, 4, 4, 4,
2995 4, 4, 4, 4, 4, 4, 4, 4,
2996 4, 4, 4, 4, 4, 4, 4, 4,
2997 4, 4, 4, 4, 4, 4, 4, 4
2998 };
2999
3000 /* OV518 quantization tables are 8x4 (instead of 8x8) */
Hans de Goede49809d62009-06-07 12:10:39 -03003001 const unsigned char yQuanTable518[] = {
3002 5, 4, 5, 6, 6, 7, 7, 7,
3003 5, 5, 5, 5, 6, 7, 7, 7,
3004 6, 6, 6, 6, 7, 7, 7, 8,
3005 7, 7, 6, 7, 7, 7, 8, 8
3006 };
Hans de Goede49809d62009-06-07 12:10:39 -03003007 const unsigned char uvQuanTable518[] = {
3008 6, 6, 6, 7, 7, 7, 7, 7,
3009 6, 6, 6, 7, 7, 7, 7, 7,
3010 6, 6, 6, 7, 7, 7, 7, 8,
3011 7, 7, 7, 7, 7, 7, 8, 8
3012 };
3013
Hans de Goede1876bb92009-06-14 06:45:50 -03003014 const unsigned char *pYTable, *pUVTable;
Hans de Goede49809d62009-06-07 12:10:39 -03003015 unsigned char val0, val1;
Jean-François Moinef8f20182010-11-12 07:54:02 -03003016 int i, size, reg = R51x_COMP_LUT_BEGIN;
Hans de Goede49809d62009-06-07 12:10:39 -03003017
3018 PDEBUG(D_PROBE, "Uploading quantization tables");
3019
Hans de Goede1876bb92009-06-14 06:45:50 -03003020 if (sd->bridge == BRIDGE_OV511 || sd->bridge == BRIDGE_OV511PLUS) {
3021 pYTable = yQuanTable511;
3022 pUVTable = uvQuanTable511;
Jean-François Moine87bae742010-11-12 05:31:34 -03003023 size = 32;
Hans de Goede1876bb92009-06-14 06:45:50 -03003024 } else {
3025 pYTable = yQuanTable518;
3026 pUVTable = uvQuanTable518;
Jean-François Moine87bae742010-11-12 05:31:34 -03003027 size = 16;
Hans de Goede1876bb92009-06-14 06:45:50 -03003028 }
3029
3030 for (i = 0; i < size; i++) {
Hans de Goede49809d62009-06-07 12:10:39 -03003031 val0 = *pYTable++;
3032 val1 = *pYTable++;
3033 val0 &= 0x0f;
3034 val1 &= 0x0f;
3035 val0 |= val1 << 4;
Jean-François Moinef8f20182010-11-12 07:54:02 -03003036 reg_w(sd, reg, val0);
Hans de Goede49809d62009-06-07 12:10:39 -03003037
3038 val0 = *pUVTable++;
3039 val1 = *pUVTable++;
3040 val0 &= 0x0f;
3041 val1 &= 0x0f;
3042 val0 |= val1 << 4;
Jean-François Moinef8f20182010-11-12 07:54:02 -03003043 reg_w(sd, reg + size, val0);
Hans de Goede49809d62009-06-07 12:10:39 -03003044
3045 reg++;
3046 }
Hans de Goede49809d62009-06-07 12:10:39 -03003047}
3048
Hans de Goede1876bb92009-06-14 06:45:50 -03003049/* This initializes the OV511/OV511+ and the sensor */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003050static void ov511_configure(struct gspca_dev *gspca_dev)
Hans de Goede1876bb92009-06-14 06:45:50 -03003051{
3052 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede1876bb92009-06-14 06:45:50 -03003053
3054 /* For 511 and 511+ */
3055 const struct ov_regvals init_511[] = {
3056 { R51x_SYS_RESET, 0x7f },
3057 { R51x_SYS_INIT, 0x01 },
3058 { R51x_SYS_RESET, 0x7f },
3059 { R51x_SYS_INIT, 0x01 },
3060 { R51x_SYS_RESET, 0x3f },
3061 { R51x_SYS_INIT, 0x01 },
3062 { R51x_SYS_RESET, 0x3d },
3063 };
3064
3065 const struct ov_regvals norm_511[] = {
Jean-François Moine780e3122010-10-19 04:29:10 -03003066 { R511_DRAM_FLOW_CTL, 0x01 },
Hans de Goede1876bb92009-06-14 06:45:50 -03003067 { R51x_SYS_SNAP, 0x00 },
3068 { R51x_SYS_SNAP, 0x02 },
3069 { R51x_SYS_SNAP, 0x00 },
3070 { R511_FIFO_OPTS, 0x1f },
3071 { R511_COMP_EN, 0x00 },
3072 { R511_COMP_LUT_EN, 0x03 },
3073 };
3074
3075 const struct ov_regvals norm_511_p[] = {
3076 { R511_DRAM_FLOW_CTL, 0xff },
3077 { R51x_SYS_SNAP, 0x00 },
3078 { R51x_SYS_SNAP, 0x02 },
3079 { R51x_SYS_SNAP, 0x00 },
3080 { R511_FIFO_OPTS, 0xff },
3081 { R511_COMP_EN, 0x00 },
3082 { R511_COMP_LUT_EN, 0x03 },
3083 };
3084
3085 const struct ov_regvals compress_511[] = {
3086 { 0x70, 0x1f },
3087 { 0x71, 0x05 },
3088 { 0x72, 0x06 },
3089 { 0x73, 0x06 },
3090 { 0x74, 0x14 },
3091 { 0x75, 0x03 },
3092 { 0x76, 0x04 },
3093 { 0x77, 0x04 },
3094 };
3095
3096 PDEBUG(D_PROBE, "Device custom id %x", reg_r(sd, R51x_SYS_CUST_ID));
3097
Jean-François Moinef8f20182010-11-12 07:54:02 -03003098 write_regvals(sd, init_511, ARRAY_SIZE(init_511));
Hans de Goede1876bb92009-06-14 06:45:50 -03003099
3100 switch (sd->bridge) {
3101 case BRIDGE_OV511:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003102 write_regvals(sd, norm_511, ARRAY_SIZE(norm_511));
Hans de Goede1876bb92009-06-14 06:45:50 -03003103 break;
3104 case BRIDGE_OV511PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003105 write_regvals(sd, norm_511_p, ARRAY_SIZE(norm_511_p));
Hans de Goede1876bb92009-06-14 06:45:50 -03003106 break;
3107 }
3108
3109 /* Init compression */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003110 write_regvals(sd, compress_511, ARRAY_SIZE(compress_511));
Hans de Goede1876bb92009-06-14 06:45:50 -03003111
Jean-François Moinef8f20182010-11-12 07:54:02 -03003112 ov51x_upload_quan_tables(sd);
Hans de Goede1876bb92009-06-14 06:45:50 -03003113}
3114
Hans de Goede49809d62009-06-07 12:10:39 -03003115/* This initializes the OV518/OV518+ and the sensor */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003116static void ov518_configure(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003117{
3118 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003119
Hans de Goede49809d62009-06-07 12:10:39 -03003120 /* For 518 and 518+ */
Hans de Goedee080fcd2009-06-18 05:03:16 -03003121 const struct ov_regvals init_518[] = {
Hans de Goede49809d62009-06-07 12:10:39 -03003122 { R51x_SYS_RESET, 0x40 },
3123 { R51x_SYS_INIT, 0xe1 },
3124 { R51x_SYS_RESET, 0x3e },
3125 { R51x_SYS_INIT, 0xe1 },
3126 { R51x_SYS_RESET, 0x00 },
3127 { R51x_SYS_INIT, 0xe1 },
3128 { 0x46, 0x00 },
3129 { 0x5d, 0x03 },
3130 };
3131
Hans de Goedee080fcd2009-06-18 05:03:16 -03003132 const struct ov_regvals norm_518[] = {
Hans de Goede49809d62009-06-07 12:10:39 -03003133 { R51x_SYS_SNAP, 0x02 }, /* Reset */
3134 { R51x_SYS_SNAP, 0x01 }, /* Enable */
Jean-François Moine780e3122010-10-19 04:29:10 -03003135 { 0x31, 0x0f },
Hans de Goede49809d62009-06-07 12:10:39 -03003136 { 0x5d, 0x03 },
3137 { 0x24, 0x9f },
3138 { 0x25, 0x90 },
3139 { 0x20, 0x00 },
3140 { 0x51, 0x04 },
3141 { 0x71, 0x19 },
3142 { 0x2f, 0x80 },
3143 };
3144
Hans de Goedee080fcd2009-06-18 05:03:16 -03003145 const struct ov_regvals norm_518_p[] = {
Hans de Goede49809d62009-06-07 12:10:39 -03003146 { R51x_SYS_SNAP, 0x02 }, /* Reset */
3147 { R51x_SYS_SNAP, 0x01 }, /* Enable */
Jean-François Moine780e3122010-10-19 04:29:10 -03003148 { 0x31, 0x0f },
Hans de Goede49809d62009-06-07 12:10:39 -03003149 { 0x5d, 0x03 },
3150 { 0x24, 0x9f },
3151 { 0x25, 0x90 },
3152 { 0x20, 0x60 },
3153 { 0x51, 0x02 },
3154 { 0x71, 0x19 },
3155 { 0x40, 0xff },
3156 { 0x41, 0x42 },
3157 { 0x46, 0x00 },
3158 { 0x33, 0x04 },
3159 { 0x21, 0x19 },
3160 { 0x3f, 0x10 },
3161 { 0x2f, 0x80 },
3162 };
3163
3164 /* First 5 bits of custom ID reg are a revision ID on OV518 */
3165 PDEBUG(D_PROBE, "Device revision %d",
Jean-François Moine87bae742010-11-12 05:31:34 -03003166 0x1f & reg_r(sd, R51x_SYS_CUST_ID));
Hans de Goede49809d62009-06-07 12:10:39 -03003167
Jean-François Moinef8f20182010-11-12 07:54:02 -03003168 write_regvals(sd, init_518, ARRAY_SIZE(init_518));
Hans de Goede49809d62009-06-07 12:10:39 -03003169
3170 /* Set LED GPIO pin to output mode */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003171 reg_w_mask(sd, R518_GPIO_CTL, 0x00, 0x02);
Hans de Goede49809d62009-06-07 12:10:39 -03003172
3173 switch (sd->bridge) {
3174 case BRIDGE_OV518:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003175 write_regvals(sd, norm_518, ARRAY_SIZE(norm_518));
Hans de Goede49809d62009-06-07 12:10:39 -03003176 break;
3177 case BRIDGE_OV518PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003178 write_regvals(sd, norm_518_p, ARRAY_SIZE(norm_518_p));
Hans de Goede49809d62009-06-07 12:10:39 -03003179 break;
3180 }
3181
Jean-François Moinef8f20182010-11-12 07:54:02 -03003182 ov51x_upload_quan_tables(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03003183
Jean-François Moinef8f20182010-11-12 07:54:02 -03003184 reg_w(sd, 0x2f, 0x80);
Hans de Goede49809d62009-06-07 12:10:39 -03003185}
3186
Jean-François Moinef8f20182010-11-12 07:54:02 -03003187static void ov519_configure(struct sd *sd)
Hans de Goede49809d62009-06-07 12:10:39 -03003188{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03003189 static const struct ov_regvals init_519[] = {
Jean-François Moine87bae742010-11-12 05:31:34 -03003190 { 0x5a, 0x6d }, /* EnableSystem */
Jean-François Moinefc63de882011-01-13 05:35:18 -03003191 { 0x53, 0x9b }, /* don't enable the microcontroller */
Jean-François Moine21867802010-11-12 06:12:09 -03003192 { OV519_R54_EN_CLK1, 0xff }, /* set bit2 to enable jpeg */
Jean-François Moine87bae742010-11-12 05:31:34 -03003193 { 0x5d, 0x03 },
3194 { 0x49, 0x01 },
3195 { 0x48, 0x00 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003196 /* Set LED pin to output mode. Bit 4 must be cleared or sensor
3197 * detection will fail. This deserves further investigation. */
3198 { OV519_GPIO_IO_CTRL0, 0xee },
Jean-François Moine21867802010-11-12 06:12:09 -03003199 { OV519_R51_RESET1, 0x0f },
3200 { OV519_R51_RESET1, 0x00 },
Jean-François Moine87bae742010-11-12 05:31:34 -03003201 { 0x22, 0x00 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003202 /* windows reads 0x55 at this point*/
3203 };
3204
Jean-François Moinef8f20182010-11-12 07:54:02 -03003205 write_regvals(sd, init_519, ARRAY_SIZE(init_519));
Hans de Goede49809d62009-06-07 12:10:39 -03003206}
3207
Jean-François Moinef8f20182010-11-12 07:54:02 -03003208static void ovfx2_configure(struct sd *sd)
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003209{
3210 static const struct ov_regvals init_fx2[] = {
3211 { 0x00, 0x60 },
3212 { 0x02, 0x01 },
3213 { 0x0f, 0x1d },
3214 { 0xe9, 0x82 },
3215 { 0xea, 0xc7 },
3216 { 0xeb, 0x10 },
3217 { 0xec, 0xf6 },
3218 };
3219
3220 sd->stopped = 1;
3221
Jean-François Moinef8f20182010-11-12 07:54:02 -03003222 write_regvals(sd, init_fx2, ARRAY_SIZE(init_fx2));
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003223}
3224
Jean-François Moine42e142f2010-11-13 05:10:27 -03003225/* set the mode */
3226/* This function works for ov7660 only */
3227static void ov519_set_mode(struct sd *sd)
3228{
3229 static const struct ov_regvals bridge_ov7660[2][10] = {
3230 {{0x10, 0x14}, {0x11, 0x1e}, {0x12, 0x00}, {0x13, 0x00},
3231 {0x14, 0x00}, {0x15, 0x00}, {0x16, 0x00}, {0x20, 0x0c},
3232 {0x25, 0x01}, {0x26, 0x00}},
3233 {{0x10, 0x28}, {0x11, 0x3c}, {0x12, 0x00}, {0x13, 0x00},
3234 {0x14, 0x00}, {0x15, 0x00}, {0x16, 0x00}, {0x20, 0x0c},
3235 {0x25, 0x03}, {0x26, 0x00}}
3236 };
3237 static const struct ov_i2c_regvals sensor_ov7660[2][3] = {
3238 {{0x12, 0x00}, {0x24, 0x00}, {0x0c, 0x0c}},
3239 {{0x12, 0x00}, {0x04, 0x00}, {0x0c, 0x00}}
3240 };
3241 static const struct ov_i2c_regvals sensor_ov7660_2[] = {
3242 {OV7670_R17_HSTART, 0x13},
3243 {OV7670_R18_HSTOP, 0x01},
3244 {OV7670_R32_HREF, 0x92},
3245 {OV7670_R19_VSTART, 0x02},
3246 {OV7670_R1A_VSTOP, 0x7a},
3247 {OV7670_R03_VREF, 0x00},
3248/* {0x33, 0x00}, */
3249/* {0x34, 0x07}, */
3250/* {0x36, 0x00}, */
3251/* {0x6b, 0x0a}, */
3252 };
3253
3254 write_regvals(sd, bridge_ov7660[sd->gspca_dev.curr_mode],
3255 ARRAY_SIZE(bridge_ov7660[0]));
3256 write_i2c_regvals(sd, sensor_ov7660[sd->gspca_dev.curr_mode],
3257 ARRAY_SIZE(sensor_ov7660[0]));
3258 write_i2c_regvals(sd, sensor_ov7660_2,
3259 ARRAY_SIZE(sensor_ov7660_2));
3260}
3261
3262/* set the frame rate */
3263/* This function works for sensors ov7640, ov7648 ov7660 and ov7670 only */
3264static void ov519_set_fr(struct sd *sd)
3265{
3266 int fr;
3267 u8 clock;
3268 /* frame rate table with indices:
3269 * - mode = 0: 320x240, 1: 640x480
3270 * - fr rate = 0: 30, 1: 25, 2: 20, 3: 15, 4: 10, 5: 5
3271 * - reg = 0: bridge a4, 1: bridge 23, 2: sensor 11 (clock)
3272 */
3273 static const u8 fr_tb[2][6][3] = {
3274 {{0x04, 0xff, 0x00},
3275 {0x04, 0x1f, 0x00},
3276 {0x04, 0x1b, 0x00},
3277 {0x04, 0x15, 0x00},
3278 {0x04, 0x09, 0x00},
3279 {0x04, 0x01, 0x00}},
3280 {{0x0c, 0xff, 0x00},
3281 {0x0c, 0x1f, 0x00},
3282 {0x0c, 0x1b, 0x00},
3283 {0x04, 0xff, 0x01},
3284 {0x04, 0x1f, 0x01},
3285 {0x04, 0x1b, 0x01}},
3286 };
3287
3288 if (frame_rate > 0)
3289 sd->frame_rate = frame_rate;
3290 if (sd->frame_rate >= 30)
3291 fr = 0;
3292 else if (sd->frame_rate >= 25)
3293 fr = 1;
3294 else if (sd->frame_rate >= 20)
3295 fr = 2;
3296 else if (sd->frame_rate >= 15)
3297 fr = 3;
3298 else if (sd->frame_rate >= 10)
3299 fr = 4;
3300 else
3301 fr = 5;
3302 reg_w(sd, 0xa4, fr_tb[sd->gspca_dev.curr_mode][fr][0]);
3303 reg_w(sd, 0x23, fr_tb[sd->gspca_dev.curr_mode][fr][1]);
3304 clock = fr_tb[sd->gspca_dev.curr_mode][fr][2];
3305 if (sd->sensor == SEN_OV7660)
3306 clock |= 0x80; /* enable double clock */
3307 ov518_i2c_w(sd, OV7670_R11_CLKRC, clock);
3308}
3309
Jean-François Moine58c92d32011-03-13 16:36:49 -03003310static void setautogain(struct gspca_dev *gspca_dev)
3311{
3312 struct sd *sd = (struct sd *) gspca_dev;
3313
3314 i2c_w_mask(sd, 0x13, sd->ctrls[AUTOGAIN].val ? 0x05 : 0x00, 0x05);
3315}
3316
Hans de Goede49809d62009-06-07 12:10:39 -03003317/* this function is called at probe time */
3318static int sd_config(struct gspca_dev *gspca_dev,
3319 const struct usb_device_id *id)
3320{
3321 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003322 struct cam *cam = &gspca_dev->cam;
Hans de Goede49809d62009-06-07 12:10:39 -03003323
Hans de Goede9e4d8252009-06-14 06:25:06 -03003324 sd->bridge = id->driver_info & BRIDGE_MASK;
Jean-François Moinea23acec42010-11-12 15:07:35 -03003325 sd->invert_led = (id->driver_info & BRIDGE_INVERT_LED) != 0;
Hans de Goede49809d62009-06-07 12:10:39 -03003326
3327 switch (sd->bridge) {
Hans de Goede1876bb92009-06-14 06:45:50 -03003328 case BRIDGE_OV511:
3329 case BRIDGE_OV511PLUS:
Jean-François Moine7491f782010-11-13 03:56:41 -03003330 cam->cam_mode = ov511_vga_mode;
3331 cam->nmodes = ARRAY_SIZE(ov511_vga_mode);
3332 break;
3333 case BRIDGE_OV518:
3334 case BRIDGE_OV518PLUS:
3335 cam->cam_mode = ov518_vga_mode;
3336 cam->nmodes = ARRAY_SIZE(ov518_vga_mode);
3337 break;
3338 case BRIDGE_OV519:
3339 cam->cam_mode = ov519_vga_mode;
3340 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3341 sd->invert_led = !sd->invert_led;
3342 break;
3343 case BRIDGE_OVFX2:
3344 cam->cam_mode = ov519_vga_mode;
3345 cam->nmodes = ARRAY_SIZE(ov519_vga_mode);
3346 cam->bulk_size = OVFX2_BULK_SIZE;
3347 cam->bulk_nurbs = MAX_NURBS;
3348 cam->bulk = 1;
3349 break;
3350 case BRIDGE_W9968CF:
3351 cam->cam_mode = w9968cf_vga_mode;
3352 cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode);
3353 cam->reverse_alts = 1;
3354 break;
3355 }
3356
3357 gspca_dev->cam.ctrls = sd->ctrls;
3358 sd->quality = QUALITY_DEF;
Jean-François Moinec8ef0a52011-05-23 06:08:49 -03003359 sd->frame_rate = 15;
Jean-François Moine7491f782010-11-13 03:56:41 -03003360
3361 return 0;
3362}
3363
3364/* this function is called at probe and resume time */
3365static int sd_init(struct gspca_dev *gspca_dev)
3366{
3367 struct sd *sd = (struct sd *) gspca_dev;
3368 struct cam *cam = &gspca_dev->cam;
3369
3370 switch (sd->bridge) {
3371 case BRIDGE_OV511:
3372 case BRIDGE_OV511PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003373 ov511_configure(gspca_dev);
Hans de Goede1876bb92009-06-14 06:45:50 -03003374 break;
Hans de Goede49809d62009-06-07 12:10:39 -03003375 case BRIDGE_OV518:
3376 case BRIDGE_OV518PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003377 ov518_configure(gspca_dev);
Hans de Goede49809d62009-06-07 12:10:39 -03003378 break;
3379 case BRIDGE_OV519:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003380 ov519_configure(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03003381 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003382 case BRIDGE_OVFX2:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003383 ovfx2_configure(sd);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03003384 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03003385 case BRIDGE_W9968CF:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003386 w9968cf_configure(sd);
Hans de Goedea511ba92009-10-16 07:13:07 -03003387 break;
Hans de Goede49809d62009-06-07 12:10:39 -03003388 }
3389
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003390 /* The OV519 must be more aggressive about sensor detection since
3391 * I2C write will never fail if the sensor is not present. We have
3392 * to try to initialize the sensor to detect its presence */
Jean-François Moine7bbe6b82010-11-11 08:27:24 -03003393 sd->sensor = -1;
Hans de Goede229bb7d2009-10-11 07:41:46 -03003394
3395 /* Test for 76xx */
3396 if (init_ov_sensor(sd, OV7xx0_SID) >= 0) {
Jean-François Moinef8f20182010-11-12 07:54:02 -03003397 ov7xx0_configure(sd);
3398
Hans de Goede229bb7d2009-10-11 07:41:46 -03003399 /* Test for 6xx0 */
3400 } else if (init_ov_sensor(sd, OV6xx0_SID) >= 0) {
Jean-François Moinef8f20182010-11-12 07:54:02 -03003401 ov6xx0_configure(sd);
3402
Hans de Goede229bb7d2009-10-11 07:41:46 -03003403 /* Test for 8xx0 */
3404 } else if (init_ov_sensor(sd, OV8xx0_SID) >= 0) {
Jean-François Moinef8f20182010-11-12 07:54:02 -03003405 ov8xx0_configure(sd);
3406
Hans de Goede635118d2009-10-11 09:49:03 -03003407 /* Test for 3xxx / 2xxx */
3408 } else if (init_ov_sensor(sd, OV_HIRES_SID) >= 0) {
Jean-François Moinef8f20182010-11-12 07:54:02 -03003409 ov_hires_configure(sd);
Hans de Goede229bb7d2009-10-11 07:41:46 -03003410 } else {
Joe Perches133a9fe2011-08-21 19:56:57 -03003411 pr_err("Can't determine sensor slave IDs\n");
Hans de Goede229bb7d2009-10-11 07:41:46 -03003412 goto error;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003413 }
3414
Jean-François Moine7bbe6b82010-11-11 08:27:24 -03003415 if (sd->sensor < 0)
3416 goto error;
3417
Jean-François Moine7491f782010-11-13 03:56:41 -03003418 ov51x_led_control(sd, 0); /* turn LED off */
3419
Hans de Goede49809d62009-06-07 12:10:39 -03003420 switch (sd->bridge) {
Hans de Goede1876bb92009-06-14 06:45:50 -03003421 case BRIDGE_OV511:
3422 case BRIDGE_OV511PLUS:
Jean-François Moine7491f782010-11-13 03:56:41 -03003423 if (sd->sif) {
Hans de Goede1876bb92009-06-14 06:45:50 -03003424 cam->cam_mode = ov511_sif_mode;
3425 cam->nmodes = ARRAY_SIZE(ov511_sif_mode);
3426 }
3427 break;
Hans de Goede49809d62009-06-07 12:10:39 -03003428 case BRIDGE_OV518:
3429 case BRIDGE_OV518PLUS:
Jean-François Moine7491f782010-11-13 03:56:41 -03003430 if (sd->sif) {
Hans de Goede49809d62009-06-07 12:10:39 -03003431 cam->cam_mode = ov518_sif_mode;
3432 cam->nmodes = ARRAY_SIZE(ov518_sif_mode);
3433 }
3434 break;
3435 case BRIDGE_OV519:
Jean-François Moine7491f782010-11-13 03:56:41 -03003436 if (sd->sif) {
Hans de Goede49809d62009-06-07 12:10:39 -03003437 cam->cam_mode = ov519_sif_mode;
3438 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3439 }
3440 break;
Hans de Goede635118d2009-10-11 09:49:03 -03003441 case BRIDGE_OVFX2:
Jean-François Moine07c6c9c2011-02-10 13:32:22 -03003442 switch (sd->sensor) {
3443 case SEN_OV2610:
3444 case SEN_OV2610AE:
Hans de Goede635118d2009-10-11 09:49:03 -03003445 cam->cam_mode = ovfx2_ov2610_mode;
3446 cam->nmodes = ARRAY_SIZE(ovfx2_ov2610_mode);
Jean-François Moine07c6c9c2011-02-10 13:32:22 -03003447 break;
3448 case SEN_OV3610:
Hans de Goede635118d2009-10-11 09:49:03 -03003449 cam->cam_mode = ovfx2_ov3610_mode;
3450 cam->nmodes = ARRAY_SIZE(ovfx2_ov3610_mode);
Jean-François Moine07c6c9c2011-02-10 13:32:22 -03003451 break;
Jean-François Moine798ae152011-05-23 05:38:10 -03003452 case SEN_OV9600:
3453 cam->cam_mode = ovfx2_ov9600_mode;
3454 cam->nmodes = ARRAY_SIZE(ovfx2_ov9600_mode);
3455 break;
Jean-François Moine07c6c9c2011-02-10 13:32:22 -03003456 default:
3457 if (sd->sif) {
3458 cam->cam_mode = ov519_sif_mode;
3459 cam->nmodes = ARRAY_SIZE(ov519_sif_mode);
3460 }
3461 break;
Hans de Goede635118d2009-10-11 09:49:03 -03003462 }
3463 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03003464 case BRIDGE_W9968CF:
Hans de Goede79b35902009-10-19 06:08:01 -03003465 if (sd->sif)
Jean-François Moine7491f782010-11-13 03:56:41 -03003466 cam->nmodes = ARRAY_SIZE(w9968cf_vga_mode) - 1;
Hans de Goedea511ba92009-10-16 07:13:07 -03003467
3468 /* w9968cf needs initialisation once the sensor is known */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003469 w9968cf_init(sd);
Hans de Goedea511ba92009-10-16 07:13:07 -03003470 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003471 }
Jean-François Moine83db7682010-11-12 07:14:08 -03003472
3473 gspca_dev->ctrl_dis = ctrl_dis[sd->sensor];
Hans de Goede02ab18b2009-06-14 04:32:04 -03003474
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003475 /* initialize the sensor */
3476 switch (sd->sensor) {
Hans de Goede635118d2009-10-11 09:49:03 -03003477 case SEN_OV2610:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003478 write_i2c_regvals(sd, norm_2610, ARRAY_SIZE(norm_2610));
3479
Hans de Goede635118d2009-10-11 09:49:03 -03003480 /* Enable autogain, autoexpo, awb, bandfilter */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003481 i2c_w_mask(sd, 0x13, 0x27, 0x27);
Hans de Goede635118d2009-10-11 09:49:03 -03003482 break;
Jean-François Moine07c6c9c2011-02-10 13:32:22 -03003483 case SEN_OV2610AE:
3484 write_i2c_regvals(sd, norm_2610ae, ARRAY_SIZE(norm_2610ae));
3485
3486 /* enable autoexpo */
3487 i2c_w_mask(sd, 0x13, 0x05, 0x05);
3488 break;
Hans de Goede635118d2009-10-11 09:49:03 -03003489 case SEN_OV3610:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003490 write_i2c_regvals(sd, norm_3620b, ARRAY_SIZE(norm_3620b));
3491
Hans de Goede635118d2009-10-11 09:49:03 -03003492 /* Enable autogain, autoexpo, awb, bandfilter */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003493 i2c_w_mask(sd, 0x13, 0x27, 0x27);
Hans de Goede635118d2009-10-11 09:49:03 -03003494 break;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003495 case SEN_OV6620:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003496 write_i2c_regvals(sd, norm_6x20, ARRAY_SIZE(norm_6x20));
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003497 break;
3498 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03003499 case SEN_OV66308AF:
Jean-François Moine62833ac2010-10-02 04:27:02 -03003500 sd->ctrls[CONTRAST].def = 200;
3501 /* The default is too low for the ov6630 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003502 write_i2c_regvals(sd, norm_6x30, ARRAY_SIZE(norm_6x30));
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003503 break;
3504 default:
3505/* case SEN_OV7610: */
3506/* case SEN_OV76BE: */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003507 write_i2c_regvals(sd, norm_7610, ARRAY_SIZE(norm_7610));
3508 i2c_w_mask(sd, 0x0e, 0x00, 0x40);
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003509 break;
3510 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03003511 case SEN_OV7620AE:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003512 write_i2c_regvals(sd, norm_7620, ARRAY_SIZE(norm_7620));
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003513 break;
3514 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003515 case SEN_OV7648:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003516 write_i2c_regvals(sd, norm_7640, ARRAY_SIZE(norm_7640));
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003517 break;
Jean-François Moine42e142f2010-11-13 05:10:27 -03003518 case SEN_OV7660:
3519 i2c_w(sd, OV7670_R12_COM7, OV7670_COM7_RESET);
3520 msleep(14);
3521 reg_w(sd, OV519_R57_SNAPSHOT, 0x23);
3522 write_regvals(sd, init_519_ov7660,
3523 ARRAY_SIZE(init_519_ov7660));
3524 write_i2c_regvals(sd, norm_7660, ARRAY_SIZE(norm_7660));
3525 sd->gspca_dev.curr_mode = 1; /* 640x480 */
Jean-François Moine42e142f2010-11-13 05:10:27 -03003526 ov519_set_mode(sd);
3527 ov519_set_fr(sd);
3528 sd->ctrls[COLORS].max = 4; /* 0..4 */
3529 sd->ctrls[COLORS].val =
3530 sd->ctrls[COLORS].def = 2;
3531 setcolors(gspca_dev);
3532 sd->ctrls[CONTRAST].max = 6; /* 0..6 */
3533 sd->ctrls[CONTRAST].val =
3534 sd->ctrls[CONTRAST].def = 3;
3535 setcontrast(gspca_dev);
3536 sd->ctrls[BRIGHTNESS].max = 6; /* 0..6 */
3537 sd->ctrls[BRIGHTNESS].val =
3538 sd->ctrls[BRIGHTNESS].def = 3;
3539 setbrightness(gspca_dev);
3540 sd_reset_snapshot(gspca_dev);
3541 ov51x_restart(sd);
3542 ov51x_stop(sd); /* not in win traces */
3543 ov51x_led_control(sd, 0);
3544 break;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003545 case SEN_OV7670:
Jean-François Moine62833ac2010-10-02 04:27:02 -03003546 sd->ctrls[FREQ].max = 3; /* auto */
3547 sd->ctrls[FREQ].def = 3;
Jean-François Moinef8f20182010-11-12 07:54:02 -03003548 write_i2c_regvals(sd, norm_7670, ARRAY_SIZE(norm_7670));
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003549 break;
3550 case SEN_OV8610:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003551 write_i2c_regvals(sd, norm_8610, ARRAY_SIZE(norm_8610));
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003552 break;
Jean-François Moine798ae152011-05-23 05:38:10 -03003553 case SEN_OV9600:
3554 write_i2c_regvals(sd, norm_9600, ARRAY_SIZE(norm_9600));
3555
3556 /* enable autoexpo */
3557/* i2c_w_mask(sd, 0x13, 0x05, 0x05); */
3558 break;
Jean-Francois Moine4202f712008-09-03 17:12:15 -03003559 }
Jean-François Moinef8f20182010-11-12 07:54:02 -03003560 return gspca_dev->usb_err;
Jean-François Moine7491f782010-11-13 03:56:41 -03003561error:
3562 PDEBUG(D_ERR, "OV519 Config failed");
3563 return -EINVAL;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003564}
3565
Jean-François Moinec42cedb2011-02-10 13:37:48 -03003566/* function called at start time before URB creation */
3567static int sd_isoc_init(struct gspca_dev *gspca_dev)
3568{
3569 struct sd *sd = (struct sd *) gspca_dev;
3570
3571 switch (sd->bridge) {
3572 case BRIDGE_OVFX2:
Jean-François Moine9cf208e2011-05-22 05:05:11 -03003573 if (gspca_dev->width != 800)
Jean-François Moinec42cedb2011-02-10 13:37:48 -03003574 gspca_dev->cam.bulk_size = OVFX2_BULK_SIZE;
3575 else
3576 gspca_dev->cam.bulk_size = 7 * 4096;
3577 break;
3578 }
3579 return 0;
3580}
3581
Hans de Goede1876bb92009-06-14 06:45:50 -03003582/* Set up the OV511/OV511+ with the given image parameters.
3583 *
3584 * Do not put any sensor-specific code in here (including I2C I/O functions)
3585 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003586static void ov511_mode_init_regs(struct sd *sd)
Hans de Goede1876bb92009-06-14 06:45:50 -03003587{
3588 int hsegs, vsegs, packet_size, fps, needed;
3589 int interlaced = 0;
3590 struct usb_host_interface *alt;
3591 struct usb_interface *intf;
3592
3593 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3594 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3595 if (!alt) {
Joe Perches133a9fe2011-08-21 19:56:57 -03003596 pr_err("Couldn't get altsetting\n");
Jean-François Moinef8f20182010-11-12 07:54:02 -03003597 sd->gspca_dev.usb_err = -EIO;
3598 return;
Hans de Goede1876bb92009-06-14 06:45:50 -03003599 }
3600
3601 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3602 reg_w(sd, R51x_FIFO_PSIZE, packet_size >> 5);
3603
3604 reg_w(sd, R511_CAM_UV_EN, 0x01);
3605 reg_w(sd, R511_SNAP_UV_EN, 0x01);
3606 reg_w(sd, R511_SNAP_OPTS, 0x03);
3607
3608 /* Here I'm assuming that snapshot size == image size.
3609 * I hope that's always true. --claudio
3610 */
3611 hsegs = (sd->gspca_dev.width >> 3) - 1;
3612 vsegs = (sd->gspca_dev.height >> 3) - 1;
3613
3614 reg_w(sd, R511_CAM_PXCNT, hsegs);
3615 reg_w(sd, R511_CAM_LNCNT, vsegs);
3616 reg_w(sd, R511_CAM_PXDIV, 0x00);
3617 reg_w(sd, R511_CAM_LNDIV, 0x00);
3618
3619 /* YUV420, low pass filter on */
3620 reg_w(sd, R511_CAM_OPTS, 0x03);
3621
3622 /* Snapshot additions */
3623 reg_w(sd, R511_SNAP_PXCNT, hsegs);
3624 reg_w(sd, R511_SNAP_LNCNT, vsegs);
3625 reg_w(sd, R511_SNAP_PXDIV, 0x00);
3626 reg_w(sd, R511_SNAP_LNDIV, 0x00);
3627
3628 /******** Set the framerate ********/
3629 if (frame_rate > 0)
3630 sd->frame_rate = frame_rate;
3631
3632 switch (sd->sensor) {
3633 case SEN_OV6620:
3634 /* No framerate control, doesn't like higher rates yet */
3635 sd->clockdiv = 3;
3636 break;
3637
3638 /* Note once the FIXME's in mode_init_ov_sensor_regs() are fixed
3639 for more sensors we need to do this for them too */
3640 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03003641 case SEN_OV7620AE:
Hans de Goede1876bb92009-06-14 06:45:50 -03003642 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003643 case SEN_OV7648:
Hans de Goedeb282d872009-06-14 19:10:40 -03003644 case SEN_OV76BE:
Hans de Goede1876bb92009-06-14 06:45:50 -03003645 if (sd->gspca_dev.width == 320)
3646 interlaced = 1;
3647 /* Fall through */
3648 case SEN_OV6630:
Hans de Goede1876bb92009-06-14 06:45:50 -03003649 case SEN_OV7610:
3650 case SEN_OV7670:
3651 switch (sd->frame_rate) {
3652 case 30:
3653 case 25:
3654 /* Not enough bandwidth to do 640x480 @ 30 fps */
3655 if (sd->gspca_dev.width != 640) {
3656 sd->clockdiv = 0;
3657 break;
3658 }
3659 /* Fall through for 640x480 case */
3660 default:
3661/* case 20: */
3662/* case 15: */
3663 sd->clockdiv = 1;
3664 break;
3665 case 10:
3666 sd->clockdiv = 2;
3667 break;
3668 case 5:
3669 sd->clockdiv = 5;
3670 break;
3671 }
3672 if (interlaced) {
3673 sd->clockdiv = (sd->clockdiv + 1) * 2 - 1;
3674 /* Higher then 10 does not work */
3675 if (sd->clockdiv > 10)
3676 sd->clockdiv = 10;
3677 }
3678 break;
3679
3680 case SEN_OV8610:
3681 /* No framerate control ?? */
3682 sd->clockdiv = 0;
3683 break;
3684 }
3685
3686 /* Check if we have enough bandwidth to disable compression */
3687 fps = (interlaced ? 60 : 30) / (sd->clockdiv + 1) + 1;
3688 needed = fps * sd->gspca_dev.width * sd->gspca_dev.height * 3 / 2;
3689 /* 1400 is a conservative estimate of the max nr of isoc packets/sec */
3690 if (needed > 1400 * packet_size) {
3691 /* Enable Y and UV quantization and compression */
3692 reg_w(sd, R511_COMP_EN, 0x07);
3693 reg_w(sd, R511_COMP_LUT_EN, 0x03);
3694 } else {
3695 reg_w(sd, R511_COMP_EN, 0x06);
3696 reg_w(sd, R511_COMP_LUT_EN, 0x00);
3697 }
3698
3699 reg_w(sd, R51x_SYS_RESET, OV511_RESET_OMNICE);
3700 reg_w(sd, R51x_SYS_RESET, 0);
Hans de Goede1876bb92009-06-14 06:45:50 -03003701}
3702
Hans de Goede49809d62009-06-07 12:10:39 -03003703/* Sets up the OV518/OV518+ with the given image parameters
3704 *
3705 * OV518 needs a completely different approach, until we can figure out what
3706 * the individual registers do. Also, only 15 FPS is supported now.
3707 *
3708 * Do not put any sensor-specific code in here (including I2C I/O functions)
3709 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003710static void ov518_mode_init_regs(struct sd *sd)
Hans de Goede49809d62009-06-07 12:10:39 -03003711{
Hans de Goedeb282d872009-06-14 19:10:40 -03003712 int hsegs, vsegs, packet_size;
3713 struct usb_host_interface *alt;
3714 struct usb_interface *intf;
3715
3716 intf = usb_ifnum_to_if(sd->gspca_dev.dev, sd->gspca_dev.iface);
3717 alt = usb_altnum_to_altsetting(intf, sd->gspca_dev.alt);
3718 if (!alt) {
Joe Perches133a9fe2011-08-21 19:56:57 -03003719 pr_err("Couldn't get altsetting\n");
Jean-François Moinef8f20182010-11-12 07:54:02 -03003720 sd->gspca_dev.usb_err = -EIO;
3721 return;
Hans de Goedeb282d872009-06-14 19:10:40 -03003722 }
3723
3724 packet_size = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
3725 ov518_reg_w32(sd, R51x_FIFO_PSIZE, packet_size & ~7, 2);
Hans de Goede49809d62009-06-07 12:10:39 -03003726
3727 /******** Set the mode ********/
Hans de Goede49809d62009-06-07 12:10:39 -03003728 reg_w(sd, 0x2b, 0);
3729 reg_w(sd, 0x2c, 0);
3730 reg_w(sd, 0x2d, 0);
3731 reg_w(sd, 0x2e, 0);
3732 reg_w(sd, 0x3b, 0);
3733 reg_w(sd, 0x3c, 0);
3734 reg_w(sd, 0x3d, 0);
3735 reg_w(sd, 0x3e, 0);
3736
3737 if (sd->bridge == BRIDGE_OV518) {
3738 /* Set 8-bit (YVYU) input format */
3739 reg_w_mask(sd, 0x20, 0x08, 0x08);
3740
3741 /* Set 12-bit (4:2:0) output format */
3742 reg_w_mask(sd, 0x28, 0x80, 0xf0);
3743 reg_w_mask(sd, 0x38, 0x80, 0xf0);
3744 } else {
3745 reg_w(sd, 0x28, 0x80);
3746 reg_w(sd, 0x38, 0x80);
3747 }
3748
3749 hsegs = sd->gspca_dev.width / 16;
3750 vsegs = sd->gspca_dev.height / 4;
3751
3752 reg_w(sd, 0x29, hsegs);
3753 reg_w(sd, 0x2a, vsegs);
3754
3755 reg_w(sd, 0x39, hsegs);
3756 reg_w(sd, 0x3a, vsegs);
3757
3758 /* Windows driver does this here; who knows why */
3759 reg_w(sd, 0x2f, 0x80);
3760
Jean-François Moine87bae742010-11-12 05:31:34 -03003761 /******** Set the framerate ********/
Hans de Goedeb282d872009-06-14 19:10:40 -03003762 sd->clockdiv = 1;
Hans de Goede49809d62009-06-07 12:10:39 -03003763
3764 /* Mode independent, but framerate dependent, regs */
Hans de Goedeb282d872009-06-14 19:10:40 -03003765 /* 0x51: Clock divider; Only works on some cams which use 2 crystals */
3766 reg_w(sd, 0x51, 0x04);
Hans de Goede49809d62009-06-07 12:10:39 -03003767 reg_w(sd, 0x22, 0x18);
3768 reg_w(sd, 0x23, 0xff);
3769
Hans de Goedeb282d872009-06-14 19:10:40 -03003770 if (sd->bridge == BRIDGE_OV518PLUS) {
3771 switch (sd->sensor) {
Hans de Goede859cc472010-01-07 15:42:35 -03003772 case SEN_OV7620AE:
Hans de Goedeb282d872009-06-14 19:10:40 -03003773 if (sd->gspca_dev.width == 320) {
3774 reg_w(sd, 0x20, 0x00);
3775 reg_w(sd, 0x21, 0x19);
3776 } else {
3777 reg_w(sd, 0x20, 0x60);
3778 reg_w(sd, 0x21, 0x1f);
3779 }
3780 break;
Hans de Goede859cc472010-01-07 15:42:35 -03003781 case SEN_OV7620:
3782 reg_w(sd, 0x20, 0x00);
3783 reg_w(sd, 0x21, 0x19);
3784 break;
Hans de Goedeb282d872009-06-14 19:10:40 -03003785 default:
3786 reg_w(sd, 0x21, 0x19);
3787 }
3788 } else
Hans de Goede49809d62009-06-07 12:10:39 -03003789 reg_w(sd, 0x71, 0x17); /* Compression-related? */
3790
3791 /* FIXME: Sensor-specific */
3792 /* Bit 5 is what matters here. Of course, it is "reserved" */
3793 i2c_w(sd, 0x54, 0x23);
3794
3795 reg_w(sd, 0x2f, 0x80);
3796
3797 if (sd->bridge == BRIDGE_OV518PLUS) {
3798 reg_w(sd, 0x24, 0x94);
3799 reg_w(sd, 0x25, 0x90);
3800 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3801 ov518_reg_w32(sd, 0xc6, 540, 2); /* 21ch */
3802 ov518_reg_w32(sd, 0xc7, 540, 2); /* 21ch */
3803 ov518_reg_w32(sd, 0xc8, 108, 2); /* 6ch */
3804 ov518_reg_w32(sd, 0xca, 131098, 3); /* 2001ah */
3805 ov518_reg_w32(sd, 0xcb, 532, 2); /* 214h */
3806 ov518_reg_w32(sd, 0xcc, 2400, 2); /* 960h */
3807 ov518_reg_w32(sd, 0xcd, 32, 2); /* 20h */
3808 ov518_reg_w32(sd, 0xce, 608, 2); /* 260h */
3809 } else {
3810 reg_w(sd, 0x24, 0x9f);
3811 reg_w(sd, 0x25, 0x90);
3812 ov518_reg_w32(sd, 0xc4, 400, 2); /* 190h */
3813 ov518_reg_w32(sd, 0xc6, 381, 2); /* 17dh */
3814 ov518_reg_w32(sd, 0xc7, 381, 2); /* 17dh */
3815 ov518_reg_w32(sd, 0xc8, 128, 2); /* 80h */
3816 ov518_reg_w32(sd, 0xca, 183331, 3); /* 2cc23h */
3817 ov518_reg_w32(sd, 0xcb, 746, 2); /* 2eah */
3818 ov518_reg_w32(sd, 0xcc, 1750, 2); /* 6d6h */
3819 ov518_reg_w32(sd, 0xcd, 45, 2); /* 2dh */
3820 ov518_reg_w32(sd, 0xce, 851, 2); /* 353h */
3821 }
3822
3823 reg_w(sd, 0x2f, 0x80);
Hans de Goede49809d62009-06-07 12:10:39 -03003824}
3825
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003826/* Sets up the OV519 with the given image parameters
3827 *
3828 * OV519 needs a completely different approach, until we can figure out what
3829 * the individual registers do.
3830 *
3831 * Do not put any sensor-specific code in here (including I2C I/O functions)
3832 */
Jean-François Moinef8f20182010-11-12 07:54:02 -03003833static void ov519_mode_init_regs(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003834{
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03003835 static const struct ov_regvals mode_init_519_ov7670[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003836 { 0x5d, 0x03 }, /* Turn off suspend mode */
3837 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
Jean-François Moine21867802010-11-12 06:12:09 -03003838 { OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003839 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3840 { 0xa3, 0x18 },
3841 { 0xa4, 0x04 },
3842 { 0xa5, 0x28 },
3843 { 0x37, 0x00 }, /* SetUsbInit */
3844 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3845 /* Enable both fields, YUV Input, disable defect comp (why?) */
3846 { 0x20, 0x0c },
3847 { 0x21, 0x38 },
3848 { 0x22, 0x1d },
3849 { 0x17, 0x50 }, /* undocumented */
3850 { 0x37, 0x00 }, /* undocumented */
3851 { 0x40, 0xff }, /* I2C timeout counter */
3852 { 0x46, 0x00 }, /* I2C clock prescaler */
3853 { 0x59, 0x04 }, /* new from windrv 090403 */
3854 { 0xff, 0x00 }, /* undocumented */
3855 /* windows reads 0x55 at this point, why? */
3856 };
3857
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03003858 static const struct ov_regvals mode_init_519[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003859 { 0x5d, 0x03 }, /* Turn off suspend mode */
3860 { 0x53, 0x9f }, /* was 9b in 1.65-1.08 */
Jean-François Moine21867802010-11-12 06:12:09 -03003861 { OV519_R54_EN_CLK1, 0x0f }, /* bit2 (jpeg enable) */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003862 { 0xa2, 0x20 }, /* a2-a5 are undocumented */
3863 { 0xa3, 0x18 },
3864 { 0xa4, 0x04 },
3865 { 0xa5, 0x28 },
3866 { 0x37, 0x00 }, /* SetUsbInit */
3867 { 0x55, 0x02 }, /* 4.096 Mhz audio clock */
3868 /* Enable both fields, YUV Input, disable defect comp (why?) */
3869 { 0x22, 0x1d },
3870 { 0x17, 0x50 }, /* undocumented */
3871 { 0x37, 0x00 }, /* undocumented */
3872 { 0x40, 0xff }, /* I2C timeout counter */
3873 { 0x46, 0x00 }, /* I2C clock prescaler */
3874 { 0x59, 0x04 }, /* new from windrv 090403 */
3875 { 0xff, 0x00 }, /* undocumented */
3876 /* windows reads 0x55 at this point, why? */
3877 };
3878
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003879 /******** Set the mode ********/
Jean-François Moine42e142f2010-11-13 05:10:27 -03003880 switch (sd->sensor) {
3881 default:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003882 write_regvals(sd, mode_init_519, ARRAY_SIZE(mode_init_519));
Hans de Goede035d3a32010-01-09 08:14:43 -03003883 if (sd->sensor == SEN_OV7640 ||
3884 sd->sensor == SEN_OV7648) {
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003885 /* Select 8-bit input mode */
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03003886 reg_w_mask(sd, OV519_R20_DFR, 0x10, 0x10);
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003887 }
Jean-François Moine42e142f2010-11-13 05:10:27 -03003888 break;
3889 case SEN_OV7660:
3890 return; /* done by ov519_set_mode/fr() */
3891 case SEN_OV7670:
Jean-François Moinef8f20182010-11-12 07:54:02 -03003892 write_regvals(sd, mode_init_519_ov7670,
3893 ARRAY_SIZE(mode_init_519_ov7670));
Jean-François Moine42e142f2010-11-13 05:10:27 -03003894 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003895 }
3896
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03003897 reg_w(sd, OV519_R10_H_SIZE, sd->gspca_dev.width >> 4);
3898 reg_w(sd, OV519_R11_V_SIZE, sd->gspca_dev.height >> 3);
Hans de Goede80142ef2009-06-14 06:26:49 -03003899 if (sd->sensor == SEN_OV7670 &&
3900 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3901 reg_w(sd, OV519_R12_X_OFFSETL, 0x04);
Hans de Goede035d3a32010-01-09 08:14:43 -03003902 else if (sd->sensor == SEN_OV7648 &&
3903 sd->gspca_dev.cam.cam_mode[sd->gspca_dev.curr_mode].priv)
3904 reg_w(sd, OV519_R12_X_OFFSETL, 0x01);
Hans de Goede80142ef2009-06-14 06:26:49 -03003905 else
3906 reg_w(sd, OV519_R12_X_OFFSETL, 0x00);
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03003907 reg_w(sd, OV519_R13_X_OFFSETH, 0x00);
3908 reg_w(sd, OV519_R14_Y_OFFSETL, 0x00);
3909 reg_w(sd, OV519_R15_Y_OFFSETH, 0x00);
3910 reg_w(sd, OV519_R16_DIVIDER, 0x00);
3911 reg_w(sd, OV519_R25_FORMAT, 0x03); /* YUV422 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003912 reg_w(sd, 0x26, 0x00); /* Undocumented */
3913
3914 /******** Set the framerate ********/
3915 if (frame_rate > 0)
3916 sd->frame_rate = frame_rate;
3917
3918/* FIXME: These are only valid at the max resolution. */
3919 sd->clockdiv = 0;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003920 switch (sd->sensor) {
3921 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03003922 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003923 switch (sd->frame_rate) {
Jean-Francois Moine53e74512008-11-08 06:10:19 -03003924 default:
3925/* case 30: */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003926 reg_w(sd, 0xa4, 0x0c);
3927 reg_w(sd, 0x23, 0xff);
3928 break;
3929 case 25:
3930 reg_w(sd, 0xa4, 0x0c);
3931 reg_w(sd, 0x23, 0x1f);
3932 break;
3933 case 20:
3934 reg_w(sd, 0xa4, 0x0c);
3935 reg_w(sd, 0x23, 0x1b);
3936 break;
Jean-Francois Moine53e74512008-11-08 06:10:19 -03003937 case 15:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003938 reg_w(sd, 0xa4, 0x04);
3939 reg_w(sd, 0x23, 0xff);
3940 sd->clockdiv = 1;
3941 break;
3942 case 10:
3943 reg_w(sd, 0xa4, 0x04);
3944 reg_w(sd, 0x23, 0x1f);
3945 sd->clockdiv = 1;
3946 break;
3947 case 5:
3948 reg_w(sd, 0xa4, 0x04);
3949 reg_w(sd, 0x23, 0x1b);
3950 sd->clockdiv = 1;
3951 break;
3952 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003953 break;
3954 case SEN_OV8610:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003955 switch (sd->frame_rate) {
3956 default: /* 15 fps */
3957/* case 15: */
3958 reg_w(sd, 0xa4, 0x06);
3959 reg_w(sd, 0x23, 0xff);
3960 break;
3961 case 10:
3962 reg_w(sd, 0xa4, 0x06);
3963 reg_w(sd, 0x23, 0x1f);
3964 break;
3965 case 5:
3966 reg_w(sd, 0xa4, 0x06);
3967 reg_w(sd, 0x23, 0x1b);
3968 break;
3969 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003970 break;
3971 case SEN_OV7670: /* guesses, based on 7640 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003972 PDEBUG(D_STREAM, "Setting framerate to %d fps",
3973 (sd->frame_rate == 0) ? 15 : sd->frame_rate);
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003974 reg_w(sd, 0xa4, 0x10);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003975 switch (sd->frame_rate) {
3976 case 30:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003977 reg_w(sd, 0x23, 0xff);
3978 break;
3979 case 20:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003980 reg_w(sd, 0x23, 0x1b);
3981 break;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003982 default:
3983/* case 15: */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003984 reg_w(sd, 0x23, 0xff);
3985 sd->clockdiv = 1;
3986 break;
3987 }
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003988 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003989 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003990}
3991
Jean-François Moinef8f20182010-11-12 07:54:02 -03003992static void mode_init_ov_sensor_regs(struct sd *sd)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03003993{
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003994 struct gspca_dev *gspca_dev;
Hans de Goedeebbb5c32009-10-12 11:32:44 -03003995 int qvga, xstart, xend, ystart, yend;
Jean-François Moine9d1593a2010-11-11 08:04:06 -03003996 u8 v;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03003997
3998 gspca_dev = &sd->gspca_dev;
Jean-François Moine87bae742010-11-12 05:31:34 -03003999 qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004000
4001 /******** Mode (VGA/QVGA) and sensor specific regs ********/
4002 switch (sd->sensor) {
Hans de Goede635118d2009-10-11 09:49:03 -03004003 case SEN_OV2610:
4004 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
4005 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
4006 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
4007 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
4008 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
4009 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
4010 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
Jean-François Moinef8f20182010-11-12 07:54:02 -03004011 return;
Jean-François Moine07c6c9c2011-02-10 13:32:22 -03004012 case SEN_OV2610AE: {
4013 u8 v;
4014
4015 /* frame rates:
4016 * 10fps / 5 fps for 1600x1200
4017 * 40fps / 20fps for 800x600
4018 */
4019 v = 80;
4020 if (qvga) {
4021 if (sd->frame_rate < 25)
4022 v = 0x81;
4023 } else {
4024 if (sd->frame_rate < 10)
4025 v = 0x81;
4026 }
4027 i2c_w(sd, 0x11, v);
4028 i2c_w(sd, 0x12, qvga ? 0x60 : 0x20);
4029 return;
4030 }
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004031 case SEN_OV3610:
Hans de Goede635118d2009-10-11 09:49:03 -03004032 if (qvga) {
4033 xstart = (1040 - gspca_dev->width) / 2 + (0x1f << 4);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004034 ystart = (776 - gspca_dev->height) / 2;
Hans de Goede635118d2009-10-11 09:49:03 -03004035 } else {
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004036 xstart = (2076 - gspca_dev->width) / 2 + (0x10 << 4);
Hans de Goede635118d2009-10-11 09:49:03 -03004037 ystart = (1544 - gspca_dev->height) / 2;
4038 }
4039 xend = xstart + gspca_dev->width;
4040 yend = ystart + gspca_dev->height;
4041 /* Writing to the COMH register resets the other windowing regs
4042 to their default values, so we must do this first. */
4043 i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0xf0);
4044 i2c_w_mask(sd, 0x32,
4045 (((xend >> 1) & 7) << 3) | ((xstart >> 1) & 7),
4046 0x3f);
4047 i2c_w_mask(sd, 0x03,
4048 (((yend >> 1) & 3) << 2) | ((ystart >> 1) & 3),
4049 0x0f);
4050 i2c_w(sd, 0x17, xstart >> 4);
4051 i2c_w(sd, 0x18, xend >> 4);
4052 i2c_w(sd, 0x19, ystart >> 3);
4053 i2c_w(sd, 0x1a, yend >> 3);
Jean-François Moinef8f20182010-11-12 07:54:02 -03004054 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004055 case SEN_OV8610:
4056 /* For OV8610 qvga means qsvga */
4057 i2c_w_mask(sd, OV7610_REG_COM_C, qvga ? (1 << 5) : 0, 1 << 5);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004058 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
4059 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
4060 i2c_w_mask(sd, 0x2d, 0x00, 0x40); /* from windrv 090403 */
4061 i2c_w_mask(sd, 0x28, 0x20, 0x20); /* progressive mode on */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004062 break;
4063 case SEN_OV7610:
4064 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
Jean-François Moine780e3122010-10-19 04:29:10 -03004065 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004066 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
4067 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004068 break;
4069 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03004070 case SEN_OV7620AE:
Hans de Goedeb282d872009-06-14 19:10:40 -03004071 case SEN_OV76BE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004072 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
4073 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
4074 i2c_w(sd, 0x24, qvga ? 0x20 : 0x3a);
4075 i2c_w(sd, 0x25, qvga ? 0x30 : 0x60);
4076 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
Hans de Goedeb282d872009-06-14 19:10:40 -03004077 i2c_w_mask(sd, 0x67, qvga ? 0xb0 : 0x90, 0xf0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004078 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004079 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
4080 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
4081 if (sd->sensor == SEN_OV76BE)
4082 i2c_w(sd, 0x35, qvga ? 0x1e : 0x9e);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004083 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004084 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03004085 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004086 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
4087 i2c_w_mask(sd, 0x28, qvga ? 0x00 : 0x20, 0x20);
Hans de Goede8d0082f2010-01-09 19:45:44 -03004088 /* Setting this undocumented bit in qvga mode removes a very
4089 annoying vertical shaking of the image */
Hans de Goede035d3a32010-01-09 08:14:43 -03004090 i2c_w_mask(sd, 0x2d, qvga ? 0x40 : 0x00, 0x40);
Hans de Goede8d0082f2010-01-09 19:45:44 -03004091 /* Unknown */
Hans de Goede035d3a32010-01-09 08:14:43 -03004092 i2c_w_mask(sd, 0x67, qvga ? 0xf0 : 0x90, 0xf0);
Hans de Goede8d0082f2010-01-09 19:45:44 -03004093 /* Allow higher automatic gain (to allow higher framerates) */
Hans de Goede035d3a32010-01-09 08:14:43 -03004094 i2c_w_mask(sd, 0x74, qvga ? 0x20 : 0x00, 0x20);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004095 i2c_w_mask(sd, 0x12, 0x04, 0x04); /* AWB: 1 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004096 break;
4097 case SEN_OV7670:
4098 /* set COM7_FMT_VGA or COM7_FMT_QVGA
4099 * do we need to set anything else?
4100 * HSTART etc are set in set_ov_sensor_window itself */
Jean-François Moine21867802010-11-12 06:12:09 -03004101 i2c_w_mask(sd, OV7670_R12_COM7,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004102 qvga ? OV7670_COM7_FMT_QVGA : OV7670_COM7_FMT_VGA,
4103 OV7670_COM7_FMT_MASK);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004104 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
Jean-François Moine21867802010-11-12 06:12:09 -03004105 i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_AWB,
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004106 OV7670_COM8_AWB);
4107 if (qvga) { /* QVGA from ov7670.c by
4108 * Jonathan Corbet */
4109 xstart = 164;
4110 xend = 28;
4111 ystart = 14;
4112 yend = 494;
4113 } else { /* VGA */
4114 xstart = 158;
4115 xend = 14;
4116 ystart = 10;
4117 yend = 490;
4118 }
4119 /* OV7670 hardware window registers are split across
4120 * multiple locations */
Jean-François Moine21867802010-11-12 06:12:09 -03004121 i2c_w(sd, OV7670_R17_HSTART, xstart >> 3);
4122 i2c_w(sd, OV7670_R18_HSTOP, xend >> 3);
4123 v = i2c_r(sd, OV7670_R32_HREF);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004124 v = (v & 0xc0) | ((xend & 0x7) << 3) | (xstart & 0x07);
4125 msleep(10); /* need to sleep between read and write to
4126 * same reg! */
Jean-François Moine21867802010-11-12 06:12:09 -03004127 i2c_w(sd, OV7670_R32_HREF, v);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004128
Jean-François Moine21867802010-11-12 06:12:09 -03004129 i2c_w(sd, OV7670_R19_VSTART, ystart >> 2);
4130 i2c_w(sd, OV7670_R1A_VSTOP, yend >> 2);
4131 v = i2c_r(sd, OV7670_R03_VREF);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004132 v = (v & 0xc0) | ((yend & 0x3) << 2) | (ystart & 0x03);
4133 msleep(10); /* need to sleep between read and write to
4134 * same reg! */
Jean-François Moine21867802010-11-12 06:12:09 -03004135 i2c_w(sd, OV7670_R03_VREF, v);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004136 break;
4137 case SEN_OV6620:
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004138 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
4139 i2c_w_mask(sd, 0x13, 0x00, 0x20); /* Select 16 bit data bus */
4140 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
4141 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004142 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004143 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004144 i2c_w_mask(sd, 0x14, qvga ? 0x20 : 0x00, 0x20);
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004145 i2c_w_mask(sd, 0x12, 0x04, 0x06); /* AWB: 1 Test pattern: 0 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004146 break;
Jean-François Moine798ae152011-05-23 05:38:10 -03004147 case SEN_OV9600: {
4148 const struct ov_i2c_regvals *vals;
4149 static const struct ov_i2c_regvals sxga_15[] = {
4150 {0x11, 0x80}, {0x14, 0x3e}, {0x24, 0x85}, {0x25, 0x75}
4151 };
4152 static const struct ov_i2c_regvals sxga_7_5[] = {
4153 {0x11, 0x81}, {0x14, 0x3e}, {0x24, 0x85}, {0x25, 0x75}
4154 };
4155 static const struct ov_i2c_regvals vga_30[] = {
4156 {0x11, 0x81}, {0x14, 0x7e}, {0x24, 0x70}, {0x25, 0x60}
4157 };
4158 static const struct ov_i2c_regvals vga_15[] = {
4159 {0x11, 0x83}, {0x14, 0x3e}, {0x24, 0x80}, {0x25, 0x70}
4160 };
4161
4162 /* frame rates:
4163 * 15fps / 7.5 fps for 1280x1024
4164 * 30fps / 15fps for 640x480
4165 */
4166 i2c_w_mask(sd, 0x12, qvga ? 0x40 : 0x00, 0x40);
4167 if (qvga)
4168 vals = sd->frame_rate < 30 ? vga_15 : vga_30;
4169 else
4170 vals = sd->frame_rate < 15 ? sxga_7_5 : sxga_15;
4171 write_i2c_regvals(sd, vals, ARRAY_SIZE(sxga_15));
4172 return;
4173 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004174 default:
Jean-François Moinef8f20182010-11-12 07:54:02 -03004175 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004176 }
4177
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004178 /******** Clock programming ********/
Hans de Goedeae49c402009-06-14 19:15:07 -03004179 i2c_w(sd, 0x11, sd->clockdiv);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004180}
4181
Jean-François Moine42e142f2010-11-13 05:10:27 -03004182/* this function works for bridge ov519 and sensors ov7660 and ov7670 only */
Jean-François Moine62833ac2010-10-02 04:27:02 -03004183static void sethvflip(struct gspca_dev *gspca_dev)
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03004184{
Jean-François Moine62833ac2010-10-02 04:27:02 -03004185 struct sd *sd = (struct sd *) gspca_dev;
4186
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03004187 if (sd->gspca_dev.streaming)
Jean-François Moine5927abc2010-11-12 15:32:29 -03004188 reg_w(sd, OV519_R51_RESET1, 0x0f); /* block stream */
Jean-François Moine21867802010-11-12 06:12:09 -03004189 i2c_w_mask(sd, OV7670_R1E_MVFP,
Jean-François Moine62833ac2010-10-02 04:27:02 -03004190 OV7670_MVFP_MIRROR * sd->ctrls[HFLIP].val
4191 | OV7670_MVFP_VFLIP * sd->ctrls[VFLIP].val,
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004192 OV7670_MVFP_MIRROR | OV7670_MVFP_VFLIP);
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03004193 if (sd->gspca_dev.streaming)
Jean-François Moine5927abc2010-11-12 15:32:29 -03004194 reg_w(sd, OV519_R51_RESET1, 0x00); /* restart stream */
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03004195}
4196
Jean-François Moinef8f20182010-11-12 07:54:02 -03004197static void set_ov_sensor_window(struct sd *sd)
Jean-Francois Moine0cd67592008-07-29 05:25:28 -03004198{
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004199 struct gspca_dev *gspca_dev;
Hans de Goede124cc9c2009-06-14 05:48:00 -03004200 int qvga, crop;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004201 int hwsbase, hwebase, vwsbase, vwebase, hwscale, vwscale;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004202
Hans de Goede635118d2009-10-11 09:49:03 -03004203 /* mode setup is fully handled in mode_init_ov_sensor_regs for these */
Jean-François Moine42e142f2010-11-13 05:10:27 -03004204 switch (sd->sensor) {
4205 case SEN_OV2610:
Jean-François Moine07c6c9c2011-02-10 13:32:22 -03004206 case SEN_OV2610AE:
Jean-François Moine42e142f2010-11-13 05:10:27 -03004207 case SEN_OV3610:
4208 case SEN_OV7670:
Jean-François Moine798ae152011-05-23 05:38:10 -03004209 case SEN_OV9600:
Jean-François Moinef8f20182010-11-12 07:54:02 -03004210 mode_init_ov_sensor_regs(sd);
4211 return;
Jean-François Moine42e142f2010-11-13 05:10:27 -03004212 case SEN_OV7660:
4213 ov519_set_mode(sd);
4214 ov519_set_fr(sd);
4215 return;
Jean-François Moinef8f20182010-11-12 07:54:02 -03004216 }
Jean-François Moine42e142f2010-11-13 05:10:27 -03004217
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004218 gspca_dev = &sd->gspca_dev;
Jean-François Moine87bae742010-11-12 05:31:34 -03004219 qvga = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 1;
4220 crop = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].priv & 2;
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004221
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004222 /* The different sensor ICs handle setting up of window differently.
4223 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!! */
4224 switch (sd->sensor) {
4225 case SEN_OV8610:
4226 hwsbase = 0x1e;
4227 hwebase = 0x1e;
4228 vwsbase = 0x02;
4229 vwebase = 0x02;
4230 break;
4231 case SEN_OV7610:
4232 case SEN_OV76BE:
4233 hwsbase = 0x38;
4234 hwebase = 0x3a;
4235 vwsbase = vwebase = 0x05;
4236 break;
4237 case SEN_OV6620:
4238 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004239 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004240 hwsbase = 0x38;
4241 hwebase = 0x3a;
4242 vwsbase = 0x05;
4243 vwebase = 0x06;
Hans de Goede7d971372009-06-14 05:28:17 -03004244 if (sd->sensor == SEN_OV66308AF && qvga)
Hans de Goede49809d62009-06-07 12:10:39 -03004245 /* HDG: this fixes U and V getting swapped */
Hans de Goede7d971372009-06-14 05:28:17 -03004246 hwsbase++;
Hans de Goede124cc9c2009-06-14 05:48:00 -03004247 if (crop) {
4248 hwsbase += 8;
4249 hwebase += 8;
4250 vwsbase += 11;
4251 vwebase += 11;
4252 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004253 break;
4254 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03004255 case SEN_OV7620AE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004256 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
4257 hwebase = 0x2f;
4258 vwsbase = vwebase = 0x05;
4259 break;
4260 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03004261 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004262 hwsbase = 0x1a;
4263 hwebase = 0x1a;
4264 vwsbase = vwebase = 0x03;
4265 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004266 default:
Jean-François Moinef8f20182010-11-12 07:54:02 -03004267 return;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004268 }
4269
4270 switch (sd->sensor) {
4271 case SEN_OV6620:
4272 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004273 case SEN_OV66308AF:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004274 if (qvga) { /* QCIF */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004275 hwscale = 0;
4276 vwscale = 0;
4277 } else { /* CIF */
4278 hwscale = 1;
4279 vwscale = 1; /* The datasheet says 0;
4280 * it's wrong */
4281 }
4282 break;
4283 case SEN_OV8610:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004284 if (qvga) { /* QSVGA */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004285 hwscale = 1;
4286 vwscale = 1;
4287 } else { /* SVGA */
4288 hwscale = 2;
4289 vwscale = 2;
4290 }
4291 break;
4292 default: /* SEN_OV7xx0 */
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004293 if (qvga) { /* QVGA */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004294 hwscale = 1;
4295 vwscale = 0;
4296 } else { /* VGA */
4297 hwscale = 2;
4298 vwscale = 1;
4299 }
4300 }
4301
Jean-François Moinef8f20182010-11-12 07:54:02 -03004302 mode_init_ov_sensor_regs(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004303
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004304 i2c_w(sd, 0x17, hwsbase);
Hans de Goedea511ba92009-10-16 07:13:07 -03004305 i2c_w(sd, 0x18, hwebase + (sd->sensor_width >> hwscale));
Hans de Goedeebbb5c32009-10-12 11:32:44 -03004306 i2c_w(sd, 0x19, vwsbase);
Hans de Goedea511ba92009-10-16 07:13:07 -03004307 i2c_w(sd, 0x1a, vwebase + (sd->sensor_height >> vwscale));
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004308}
4309
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004310/* -- start the camera -- */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03004311static int sd_start(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004312{
4313 struct sd *sd = (struct sd *) gspca_dev;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004314
Hans de Goedea511ba92009-10-16 07:13:07 -03004315 /* Default for most bridges, allow bridge_mode_init_regs to override */
4316 sd->sensor_width = sd->gspca_dev.width;
4317 sd->sensor_height = sd->gspca_dev.height;
4318
Hans de Goede49809d62009-06-07 12:10:39 -03004319 switch (sd->bridge) {
Hans de Goede1876bb92009-06-14 06:45:50 -03004320 case BRIDGE_OV511:
4321 case BRIDGE_OV511PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03004322 ov511_mode_init_regs(sd);
Hans de Goede1876bb92009-06-14 06:45:50 -03004323 break;
Hans de Goede49809d62009-06-07 12:10:39 -03004324 case BRIDGE_OV518:
4325 case BRIDGE_OV518PLUS:
Jean-François Moinef8f20182010-11-12 07:54:02 -03004326 ov518_mode_init_regs(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03004327 break;
4328 case BRIDGE_OV519:
Jean-François Moinef8f20182010-11-12 07:54:02 -03004329 ov519_mode_init_regs(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03004330 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004331 /* case BRIDGE_OVFX2: nothing to do */
Hans de Goedea511ba92009-10-16 07:13:07 -03004332 case BRIDGE_W9968CF:
Jean-François Moinef8f20182010-11-12 07:54:02 -03004333 w9968cf_mode_init_regs(sd);
Hans de Goedea511ba92009-10-16 07:13:07 -03004334 break;
Hans de Goede49809d62009-06-07 12:10:39 -03004335 }
Hans de Goede49809d62009-06-07 12:10:39 -03004336
Jean-François Moinef8f20182010-11-12 07:54:02 -03004337 set_ov_sensor_window(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004338
Jean-François Moinee2817022010-11-12 13:59:48 -03004339 if (!(sd->gspca_dev.ctrl_dis & (1 << CONTRAST)))
4340 setcontrast(gspca_dev);
4341 if (!(sd->gspca_dev.ctrl_dis & (1 << BRIGHTNESS)))
4342 setbrightness(gspca_dev);
Jean-François Moine58c92d32011-03-13 16:36:49 -03004343 if (!(sd->gspca_dev.ctrl_dis & (1 << EXPOSURE)))
4344 setexposure(gspca_dev);
Jean-François Moinee2817022010-11-12 13:59:48 -03004345 if (!(sd->gspca_dev.ctrl_dis & (1 << COLORS)))
4346 setcolors(gspca_dev);
4347 if (!(sd->gspca_dev.ctrl_dis & ((1 << HFLIP) | (1 << VFLIP))))
4348 sethvflip(gspca_dev);
4349 if (!(sd->gspca_dev.ctrl_dis & (1 << AUTOBRIGHT)))
4350 setautobright(gspca_dev);
Jean-François Moine58c92d32011-03-13 16:36:49 -03004351 if (!(sd->gspca_dev.ctrl_dis & (1 << AUTOGAIN)))
4352 setautogain(gspca_dev);
Jean-François Moinee2817022010-11-12 13:59:48 -03004353 if (!(sd->gspca_dev.ctrl_dis & (1 << FREQ)))
4354 setfreq_i(sd);
Hans de Goede49809d62009-06-07 12:10:39 -03004355
Hans de Goede417a4d22010-02-19 07:37:08 -03004356 /* Force clear snapshot state in case the snapshot button was
4357 pressed while we weren't streaming */
4358 sd->snapshot_needs_reset = 1;
4359 sd_reset_snapshot(gspca_dev);
Hans de Goede417a4d22010-02-19 07:37:08 -03004360
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004361 sd->first_frame = 3;
4362
Jean-François Moinef8f20182010-11-12 07:54:02 -03004363 ov51x_restart(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004364 ov51x_led_control(sd, 1);
Jean-François Moinef8f20182010-11-12 07:54:02 -03004365 return gspca_dev->usb_err;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004366}
4367
4368static void sd_stopN(struct gspca_dev *gspca_dev)
4369{
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03004370 struct sd *sd = (struct sd *) gspca_dev;
4371
4372 ov51x_stop(sd);
4373 ov51x_led_control(sd, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004374}
4375
Hans de Goede79b35902009-10-19 06:08:01 -03004376static void sd_stop0(struct gspca_dev *gspca_dev)
4377{
4378 struct sd *sd = (struct sd *) gspca_dev;
4379
Jean-François Moined65174c2010-11-11 06:20:42 -03004380 if (!sd->gspca_dev.present)
4381 return;
Hans de Goede79b35902009-10-19 06:08:01 -03004382 if (sd->bridge == BRIDGE_W9968CF)
4383 w9968cf_stop0(sd);
Hans de Goede614d0692010-10-27 07:42:28 -03004384
Jean-François Moine14653e62010-11-11 06:17:01 -03004385#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede614d0692010-10-27 07:42:28 -03004386 /* If the last button state is pressed, release it now! */
4387 if (sd->snapshot_pressed) {
4388 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
4389 input_sync(gspca_dev->input_dev);
4390 sd->snapshot_pressed = 0;
4391 }
4392#endif
Jean-François Moineb4e96ea2010-11-12 16:13:17 -03004393 if (sd->bridge == BRIDGE_OV519)
4394 reg_w(sd, OV519_R57_SNAPSHOT, 0x23);
Hans de Goede79b35902009-10-19 06:08:01 -03004395}
4396
Hans de Goede92e232a2010-02-20 04:30:45 -03004397static void ov51x_handle_button(struct gspca_dev *gspca_dev, u8 state)
4398{
4399 struct sd *sd = (struct sd *) gspca_dev;
4400
4401 if (sd->snapshot_pressed != state) {
Jean-François Moine28566432010-10-01 07:33:26 -03004402#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede92e232a2010-02-20 04:30:45 -03004403 input_report_key(gspca_dev->input_dev, KEY_CAMERA, state);
4404 input_sync(gspca_dev->input_dev);
4405#endif
4406 if (state)
4407 sd->snapshot_needs_reset = 1;
4408
4409 sd->snapshot_pressed = state;
4410 } else {
Hans de Goede88e8d202010-02-20 04:45:49 -03004411 /* On the ov511 / ov519 we need to reset the button state
4412 multiple times, as resetting does not work as long as the
4413 button stays pressed */
4414 switch (sd->bridge) {
4415 case BRIDGE_OV511:
4416 case BRIDGE_OV511PLUS:
4417 case BRIDGE_OV519:
4418 if (state)
4419 sd->snapshot_needs_reset = 1;
4420 break;
4421 }
Hans de Goede92e232a2010-02-20 04:30:45 -03004422 }
4423}
4424
Hans de Goede1876bb92009-06-14 06:45:50 -03004425static void ov511_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004426 u8 *in, /* isoc packet */
4427 int len) /* iso packet length */
Hans de Goede1876bb92009-06-14 06:45:50 -03004428{
4429 struct sd *sd = (struct sd *) gspca_dev;
4430
4431 /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
4432 * byte non-zero. The EOF packet has image width/height in the
4433 * 10th and 11th bytes. The 9th byte is given as follows:
4434 *
4435 * bit 7: EOF
4436 * 6: compression enabled
4437 * 5: 422/420/400 modes
4438 * 4: 422/420/400 modes
4439 * 3: 1
4440 * 2: snapshot button on
4441 * 1: snapshot frame
4442 * 0: even/odd field
4443 */
4444 if (!(in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) &&
4445 (in[8] & 0x08)) {
Hans de Goede88e8d202010-02-20 04:45:49 -03004446 ov51x_handle_button(gspca_dev, (in[8] >> 2) & 1);
Hans de Goede1876bb92009-06-14 06:45:50 -03004447 if (in[8] & 0x80) {
4448 /* Frame end */
4449 if ((in[9] + 1) * 8 != gspca_dev->width ||
4450 (in[10] + 1) * 8 != gspca_dev->height) {
4451 PDEBUG(D_ERR, "Invalid frame size, got: %dx%d,"
4452 " requested: %dx%d\n",
4453 (in[9] + 1) * 8, (in[10] + 1) * 8,
4454 gspca_dev->width, gspca_dev->height);
4455 gspca_dev->last_packet_type = DISCARD_PACKET;
4456 return;
4457 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004458 /* Add 11 byte footer to frame, might be useful */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004459 gspca_frame_add(gspca_dev, LAST_PACKET, in, 11);
Hans de Goede1876bb92009-06-14 06:45:50 -03004460 return;
4461 } else {
4462 /* Frame start */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004463 gspca_frame_add(gspca_dev, FIRST_PACKET, in, 0);
Hans de Goede1876bb92009-06-14 06:45:50 -03004464 sd->packet_nr = 0;
4465 }
4466 }
4467
4468 /* Ignore the packet number */
4469 len--;
4470
4471 /* intermediate packet */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004472 gspca_frame_add(gspca_dev, INTER_PACKET, in, len);
Hans de Goede1876bb92009-06-14 06:45:50 -03004473}
4474
Hans de Goede49809d62009-06-07 12:10:39 -03004475static void ov518_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004476 u8 *data, /* isoc packet */
Hans de Goede49809d62009-06-07 12:10:39 -03004477 int len) /* iso packet length */
4478{
Hans de Goede92918a52009-06-14 06:21:35 -03004479 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede49809d62009-06-07 12:10:39 -03004480
4481 /* A false positive here is likely, until OVT gives me
4482 * the definitive SOF/EOF format */
4483 if ((!(data[0] | data[1] | data[2] | data[3] | data[5])) && data[6]) {
Hans de Goede92e232a2010-02-20 04:30:45 -03004484 ov51x_handle_button(gspca_dev, (data[6] >> 1) & 1);
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004485 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4486 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
Hans de Goede92918a52009-06-14 06:21:35 -03004487 sd->packet_nr = 0;
4488 }
4489
4490 if (gspca_dev->last_packet_type == DISCARD_PACKET)
4491 return;
4492
4493 /* Does this device use packet numbers ? */
4494 if (len & 7) {
4495 len--;
4496 if (sd->packet_nr == data[len])
4497 sd->packet_nr++;
4498 /* The last few packets of the frame (which are all 0's
4499 except that they may contain part of the footer), are
4500 numbered 0 */
4501 else if (sd->packet_nr == 0 || data[len]) {
4502 PDEBUG(D_ERR, "Invalid packet nr: %d (expect: %d)",
4503 (int)data[len], (int)sd->packet_nr);
4504 gspca_dev->last_packet_type = DISCARD_PACKET;
4505 return;
4506 }
Hans de Goede49809d62009-06-07 12:10:39 -03004507 }
4508
4509 /* intermediate packet */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004510 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004511}
4512
4513static void ov519_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004514 u8 *data, /* isoc packet */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004515 int len) /* iso packet length */
4516{
4517 /* Header of ov519 is 16 bytes:
4518 * Byte Value Description
4519 * 0 0xff magic
4520 * 1 0xff magic
4521 * 2 0xff magic
4522 * 3 0xXX 0x50 = SOF, 0x51 = EOF
4523 * 9 0xXX 0x01 initial frame without data,
4524 * 0x00 standard frame with image
4525 * 14 Lo in EOF: length of image data / 8
4526 * 15 Hi
4527 */
4528
4529 if (data[0] == 0xff && data[1] == 0xff && data[2] == 0xff) {
4530 switch (data[3]) {
4531 case 0x50: /* start of frame */
Hans de Goede417a4d22010-02-19 07:37:08 -03004532 /* Don't check the button state here, as the state
4533 usually (always ?) changes at EOF and checking it
4534 here leads to unnecessary snapshot state resets. */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004535#define HDRSZ 16
4536 data += HDRSZ;
4537 len -= HDRSZ;
4538#undef HDRSZ
4539 if (data[0] == 0xff || data[1] == 0xd8)
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004540 gspca_frame_add(gspca_dev, FIRST_PACKET,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004541 data, len);
4542 else
4543 gspca_dev->last_packet_type = DISCARD_PACKET;
4544 return;
4545 case 0x51: /* end of frame */
Hans de Goede92e232a2010-02-20 04:30:45 -03004546 ov51x_handle_button(gspca_dev, data[11] & 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004547 if (data[9] != 0)
4548 gspca_dev->last_packet_type = DISCARD_PACKET;
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004549 gspca_frame_add(gspca_dev, LAST_PACKET,
4550 NULL, 0);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004551 return;
4552 }
4553 }
4554
4555 /* intermediate packet */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004556 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004557}
4558
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004559static void ovfx2_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004560 u8 *data, /* isoc packet */
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004561 int len) /* iso packet length */
4562{
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004563 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004564
4565 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
4566
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004567 /* A short read signals EOF */
Jean-François Moine9d2b9092011-05-22 04:56:32 -03004568 if (len < gspca_dev->cam.bulk_size) {
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004569 /* If the frame is short, and it is one of the first ones
4570 the sensor and bridge are still syncing, so drop it. */
4571 if (sd->first_frame) {
4572 sd->first_frame--;
Jean-François Moineb192ca92010-06-27 03:08:19 -03004573 if (gspca_dev->image_len <
4574 sd->gspca_dev.width * sd->gspca_dev.height)
Hans de Goeded6b6d7a2010-05-08 08:55:42 -03004575 gspca_dev->last_packet_type = DISCARD_PACKET;
4576 }
4577 gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004578 gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004579 }
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004580}
4581
Hans de Goede49809d62009-06-07 12:10:39 -03004582static void sd_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004583 u8 *data, /* isoc packet */
Hans de Goede49809d62009-06-07 12:10:39 -03004584 int len) /* iso packet length */
4585{
4586 struct sd *sd = (struct sd *) gspca_dev;
4587
4588 switch (sd->bridge) {
4589 case BRIDGE_OV511:
4590 case BRIDGE_OV511PLUS:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004591 ov511_pkt_scan(gspca_dev, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004592 break;
4593 case BRIDGE_OV518:
4594 case BRIDGE_OV518PLUS:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004595 ov518_pkt_scan(gspca_dev, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004596 break;
4597 case BRIDGE_OV519:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004598 ov519_pkt_scan(gspca_dev, data, len);
Hans de Goede49809d62009-06-07 12:10:39 -03004599 break;
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004600 case BRIDGE_OVFX2:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004601 ovfx2_pkt_scan(gspca_dev, data, len);
Hans de Goedeb46aaa02009-10-12 10:07:57 -03004602 break;
Hans de Goedea511ba92009-10-16 07:13:07 -03004603 case BRIDGE_W9968CF:
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03004604 w9968cf_pkt_scan(gspca_dev, data, len);
Hans de Goedea511ba92009-10-16 07:13:07 -03004605 break;
Hans de Goede49809d62009-06-07 12:10:39 -03004606 }
4607}
4608
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004609/* -- management routines -- */
4610
4611static void setbrightness(struct gspca_dev *gspca_dev)
4612{
4613 struct sd *sd = (struct sd *) gspca_dev;
4614 int val;
Jean-François Moine42e142f2010-11-13 05:10:27 -03004615 static const struct ov_i2c_regvals brit_7660[][7] = {
4616 {{0x0f, 0x6a}, {0x24, 0x40}, {0x25, 0x2b}, {0x26, 0x90},
4617 {0x27, 0xe0}, {0x28, 0xe0}, {0x2c, 0xe0}},
4618 {{0x0f, 0x6a}, {0x24, 0x50}, {0x25, 0x40}, {0x26, 0xa1},
4619 {0x27, 0xc0}, {0x28, 0xc0}, {0x2c, 0xc0}},
4620 {{0x0f, 0x6a}, {0x24, 0x68}, {0x25, 0x58}, {0x26, 0xc2},
4621 {0x27, 0xa0}, {0x28, 0xa0}, {0x2c, 0xa0}},
4622 {{0x0f, 0x6a}, {0x24, 0x70}, {0x25, 0x68}, {0x26, 0xd3},
4623 {0x27, 0x80}, {0x28, 0x80}, {0x2c, 0x80}},
4624 {{0x0f, 0x6a}, {0x24, 0x80}, {0x25, 0x70}, {0x26, 0xd3},
4625 {0x27, 0x20}, {0x28, 0x20}, {0x2c, 0x20}},
4626 {{0x0f, 0x6a}, {0x24, 0x88}, {0x25, 0x78}, {0x26, 0xd3},
4627 {0x27, 0x40}, {0x28, 0x40}, {0x2c, 0x40}},
4628 {{0x0f, 0x6a}, {0x24, 0x90}, {0x25, 0x80}, {0x26, 0xd4},
4629 {0x27, 0x60}, {0x28, 0x60}, {0x2c, 0x60}}
4630 };
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004631
Jean-François Moine62833ac2010-10-02 04:27:02 -03004632 val = sd->ctrls[BRIGHTNESS].val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004633 switch (sd->sensor) {
4634 case SEN_OV8610:
4635 case SEN_OV7610:
4636 case SEN_OV76BE:
4637 case SEN_OV6620:
4638 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004639 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004640 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03004641 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004642 i2c_w(sd, OV7610_REG_BRT, val);
4643 break;
4644 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03004645 case SEN_OV7620AE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004646 /* 7620 doesn't like manual changes when in auto mode */
Jean-François Moine62833ac2010-10-02 04:27:02 -03004647 if (!sd->ctrls[AUTOBRIGHT].val)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004648 i2c_w(sd, OV7610_REG_BRT, val);
4649 break;
Jean-François Moine42e142f2010-11-13 05:10:27 -03004650 case SEN_OV7660:
4651 write_i2c_regvals(sd, brit_7660[val],
4652 ARRAY_SIZE(brit_7660[0]));
4653 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004654 case SEN_OV7670:
Jean-Francois Moine594f5b82008-08-01 06:37:51 -03004655/*win trace
Jean-François Moine21867802010-11-12 06:12:09 -03004656 * i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_AEC); */
4657 i2c_w(sd, OV7670_R55_BRIGHT, ov7670_abs_to_sm(val));
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004658 break;
4659 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004660}
4661
4662static void setcontrast(struct gspca_dev *gspca_dev)
4663{
4664 struct sd *sd = (struct sd *) gspca_dev;
4665 int val;
Jean-François Moine42e142f2010-11-13 05:10:27 -03004666 static const struct ov_i2c_regvals contrast_7660[][31] = {
4667 {{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf8}, {0x6f, 0xa0},
4668 {0x70, 0x58}, {0x71, 0x38}, {0x72, 0x30}, {0x73, 0x30},
4669 {0x74, 0x28}, {0x75, 0x28}, {0x76, 0x24}, {0x77, 0x24},
4670 {0x78, 0x22}, {0x79, 0x28}, {0x7a, 0x2a}, {0x7b, 0x34},
4671 {0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3d}, {0x7f, 0x65},
4672 {0x80, 0x70}, {0x81, 0x77}, {0x82, 0x7d}, {0x83, 0x83},
4673 {0x84, 0x88}, {0x85, 0x8d}, {0x86, 0x96}, {0x87, 0x9f},
4674 {0x88, 0xb0}, {0x89, 0xc4}, {0x8a, 0xd9}},
4675 {{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf8}, {0x6f, 0x94},
4676 {0x70, 0x58}, {0x71, 0x40}, {0x72, 0x30}, {0x73, 0x30},
4677 {0x74, 0x30}, {0x75, 0x30}, {0x76, 0x2c}, {0x77, 0x24},
4678 {0x78, 0x22}, {0x79, 0x28}, {0x7a, 0x2a}, {0x7b, 0x31},
4679 {0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3d}, {0x7f, 0x62},
4680 {0x80, 0x6d}, {0x81, 0x75}, {0x82, 0x7b}, {0x83, 0x81},
4681 {0x84, 0x87}, {0x85, 0x8d}, {0x86, 0x98}, {0x87, 0xa1},
4682 {0x88, 0xb2}, {0x89, 0xc6}, {0x8a, 0xdb}},
4683 {{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf0}, {0x6f, 0x84},
4684 {0x70, 0x58}, {0x71, 0x48}, {0x72, 0x40}, {0x73, 0x40},
4685 {0x74, 0x28}, {0x75, 0x28}, {0x76, 0x28}, {0x77, 0x24},
4686 {0x78, 0x26}, {0x79, 0x28}, {0x7a, 0x28}, {0x7b, 0x34},
4687 {0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3c}, {0x7f, 0x5d},
4688 {0x80, 0x68}, {0x81, 0x71}, {0x82, 0x79}, {0x83, 0x81},
4689 {0x84, 0x86}, {0x85, 0x8b}, {0x86, 0x95}, {0x87, 0x9e},
4690 {0x88, 0xb1}, {0x89, 0xc5}, {0x8a, 0xd9}},
4691 {{0x6c, 0xf0}, {0x6d, 0xf0}, {0x6e, 0xf0}, {0x6f, 0x70},
4692 {0x70, 0x58}, {0x71, 0x58}, {0x72, 0x48}, {0x73, 0x48},
4693 {0x74, 0x38}, {0x75, 0x40}, {0x76, 0x34}, {0x77, 0x34},
4694 {0x78, 0x2e}, {0x79, 0x28}, {0x7a, 0x24}, {0x7b, 0x22},
4695 {0x7c, 0x0f}, {0x7d, 0x1e}, {0x7e, 0x3c}, {0x7f, 0x58},
4696 {0x80, 0x63}, {0x81, 0x6e}, {0x82, 0x77}, {0x83, 0x80},
4697 {0x84, 0x87}, {0x85, 0x8f}, {0x86, 0x9c}, {0x87, 0xa9},
4698 {0x88, 0xc0}, {0x89, 0xd4}, {0x8a, 0xe6}},
4699 {{0x6c, 0xa0}, {0x6d, 0xf0}, {0x6e, 0x90}, {0x6f, 0x80},
4700 {0x70, 0x70}, {0x71, 0x80}, {0x72, 0x60}, {0x73, 0x60},
4701 {0x74, 0x58}, {0x75, 0x60}, {0x76, 0x4c}, {0x77, 0x38},
4702 {0x78, 0x38}, {0x79, 0x2a}, {0x7a, 0x20}, {0x7b, 0x0e},
4703 {0x7c, 0x0a}, {0x7d, 0x14}, {0x7e, 0x26}, {0x7f, 0x46},
4704 {0x80, 0x54}, {0x81, 0x64}, {0x82, 0x70}, {0x83, 0x7c},
4705 {0x84, 0x87}, {0x85, 0x93}, {0x86, 0xa6}, {0x87, 0xb4},
4706 {0x88, 0xd0}, {0x89, 0xe5}, {0x8a, 0xf5}},
4707 {{0x6c, 0x60}, {0x6d, 0x80}, {0x6e, 0x60}, {0x6f, 0x80},
4708 {0x70, 0x80}, {0x71, 0x80}, {0x72, 0x88}, {0x73, 0x30},
4709 {0x74, 0x70}, {0x75, 0x68}, {0x76, 0x64}, {0x77, 0x50},
4710 {0x78, 0x3c}, {0x79, 0x22}, {0x7a, 0x10}, {0x7b, 0x08},
4711 {0x7c, 0x06}, {0x7d, 0x0e}, {0x7e, 0x1a}, {0x7f, 0x3a},
4712 {0x80, 0x4a}, {0x81, 0x5a}, {0x82, 0x6b}, {0x83, 0x7b},
4713 {0x84, 0x89}, {0x85, 0x96}, {0x86, 0xaf}, {0x87, 0xc3},
4714 {0x88, 0xe1}, {0x89, 0xf2}, {0x8a, 0xfa}},
4715 {{0x6c, 0x20}, {0x6d, 0x40}, {0x6e, 0x20}, {0x6f, 0x60},
4716 {0x70, 0x88}, {0x71, 0xc8}, {0x72, 0xc0}, {0x73, 0xb8},
4717 {0x74, 0xa8}, {0x75, 0xb8}, {0x76, 0x80}, {0x77, 0x5c},
4718 {0x78, 0x26}, {0x79, 0x10}, {0x7a, 0x08}, {0x7b, 0x04},
4719 {0x7c, 0x02}, {0x7d, 0x06}, {0x7e, 0x0a}, {0x7f, 0x22},
4720 {0x80, 0x33}, {0x81, 0x4c}, {0x82, 0x64}, {0x83, 0x7b},
4721 {0x84, 0x90}, {0x85, 0xa7}, {0x86, 0xc7}, {0x87, 0xde},
4722 {0x88, 0xf1}, {0x89, 0xf9}, {0x8a, 0xfd}},
4723 };
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004724
Jean-François Moine62833ac2010-10-02 04:27:02 -03004725 val = sd->ctrls[CONTRAST].val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004726 switch (sd->sensor) {
4727 case SEN_OV7610:
4728 case SEN_OV6620:
4729 i2c_w(sd, OV7610_REG_CNT, val);
4730 break;
4731 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004732 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004733 i2c_w_mask(sd, OV7610_REG_CNT, val >> 4, 0x0f);
Hans de Goede49809d62009-06-07 12:10:39 -03004734 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004735 case SEN_OV8610: {
Jean-François Moine9d1593a2010-11-11 08:04:06 -03004736 static const u8 ctab[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004737 0x03, 0x09, 0x0b, 0x0f, 0x53, 0x6f, 0x35, 0x7f
4738 };
4739
4740 /* Use Y gamma control instead. Bit 0 enables it. */
4741 i2c_w(sd, 0x64, ctab[val >> 5]);
4742 break;
4743 }
Hans de Goede859cc472010-01-07 15:42:35 -03004744 case SEN_OV7620:
4745 case SEN_OV7620AE: {
Jean-François Moine9d1593a2010-11-11 08:04:06 -03004746 static const u8 ctab[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004747 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
4748 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
4749 };
4750
4751 /* Use Y gamma control instead. Bit 0 enables it. */
4752 i2c_w(sd, 0x64, ctab[val >> 4]);
4753 break;
4754 }
Jean-François Moine42e142f2010-11-13 05:10:27 -03004755 case SEN_OV7660:
4756 write_i2c_regvals(sd, contrast_7660[val],
4757 ARRAY_SIZE(contrast_7660[0]));
4758 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004759 case SEN_OV7670:
4760 /* check that this isn't just the same as ov7610 */
Jean-François Moine21867802010-11-12 06:12:09 -03004761 i2c_w(sd, OV7670_R56_CONTRAS, val >> 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004762 break;
4763 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004764}
4765
Jean-François Moine58c92d32011-03-13 16:36:49 -03004766static void setexposure(struct gspca_dev *gspca_dev)
4767{
4768 struct sd *sd = (struct sd *) gspca_dev;
4769
4770 if (!sd->ctrls[AUTOGAIN].val)
4771 i2c_w(sd, 0x10, sd->ctrls[EXPOSURE].val);
4772}
4773
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004774static void setcolors(struct gspca_dev *gspca_dev)
4775{
4776 struct sd *sd = (struct sd *) gspca_dev;
4777 int val;
Jean-François Moine42e142f2010-11-13 05:10:27 -03004778 static const struct ov_i2c_regvals colors_7660[][6] = {
4779 {{0x4f, 0x28}, {0x50, 0x2a}, {0x51, 0x02}, {0x52, 0x0a},
4780 {0x53, 0x19}, {0x54, 0x23}},
4781 {{0x4f, 0x47}, {0x50, 0x4a}, {0x51, 0x03}, {0x52, 0x11},
4782 {0x53, 0x2c}, {0x54, 0x3e}},
4783 {{0x4f, 0x66}, {0x50, 0x6b}, {0x51, 0x05}, {0x52, 0x19},
4784 {0x53, 0x40}, {0x54, 0x59}},
4785 {{0x4f, 0x84}, {0x50, 0x8b}, {0x51, 0x06}, {0x52, 0x20},
4786 {0x53, 0x53}, {0x54, 0x73}},
4787 {{0x4f, 0xa3}, {0x50, 0xab}, {0x51, 0x08}, {0x52, 0x28},
4788 {0x53, 0x66}, {0x54, 0x8e}},
4789 };
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004790
Jean-François Moine62833ac2010-10-02 04:27:02 -03004791 val = sd->ctrls[COLORS].val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004792 switch (sd->sensor) {
4793 case SEN_OV8610:
4794 case SEN_OV7610:
4795 case SEN_OV76BE:
4796 case SEN_OV6620:
4797 case SEN_OV6630:
Hans de Goede7d971372009-06-14 05:28:17 -03004798 case SEN_OV66308AF:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004799 i2c_w(sd, OV7610_REG_SAT, val);
4800 break;
4801 case SEN_OV7620:
Hans de Goede859cc472010-01-07 15:42:35 -03004802 case SEN_OV7620AE:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004803 /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
4804/* rc = ov_i2c_write(sd->dev, 0x62, (val >> 9) & 0x7e);
4805 if (rc < 0)
4806 goto out; */
4807 i2c_w(sd, OV7610_REG_SAT, val);
4808 break;
4809 case SEN_OV7640:
Hans de Goede035d3a32010-01-09 08:14:43 -03004810 case SEN_OV7648:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004811 i2c_w(sd, OV7610_REG_SAT, val & 0xf0);
4812 break;
Jean-François Moine42e142f2010-11-13 05:10:27 -03004813 case SEN_OV7660:
4814 write_i2c_regvals(sd, colors_7660[val],
4815 ARRAY_SIZE(colors_7660[0]));
4816 break;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004817 case SEN_OV7670:
4818 /* supported later once I work out how to do it
4819 * transparently fail now! */
4820 /* set REG_COM13 values for UV sat auto mode */
4821 break;
4822 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004823}
4824
Jean-François Moine62833ac2010-10-02 04:27:02 -03004825static void setautobright(struct gspca_dev *gspca_dev)
Hans de Goede02ab18b2009-06-14 04:32:04 -03004826{
Jean-François Moine62833ac2010-10-02 04:27:02 -03004827 struct sd *sd = (struct sd *) gspca_dev;
4828
Jean-François Moine62833ac2010-10-02 04:27:02 -03004829 i2c_w_mask(sd, 0x2d, sd->ctrls[AUTOBRIGHT].val ? 0x10 : 0x00, 0x10);
Hans de Goede02ab18b2009-06-14 04:32:04 -03004830}
4831
Jean-François Moine58c92d32011-03-13 16:36:49 -03004832static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
4833{
4834 struct sd *sd = (struct sd *) gspca_dev;
4835
4836 sd->ctrls[AUTOGAIN].val = val;
4837 if (val) {
4838 gspca_dev->ctrl_inac |= (1 << EXPOSURE);
4839 } else {
4840 gspca_dev->ctrl_inac &= ~(1 << EXPOSURE);
4841 sd->ctrls[EXPOSURE].val = i2c_r(sd, 0x10);
4842 }
4843 if (gspca_dev->streaming)
4844 setautogain(gspca_dev);
4845 return gspca_dev->usb_err;
4846}
4847
Jean-François Moine62833ac2010-10-02 04:27:02 -03004848static void setfreq_i(struct sd *sd)
Hans de Goede02ab18b2009-06-14 04:32:04 -03004849{
Jean-François Moine42e142f2010-11-13 05:10:27 -03004850 if (sd->sensor == SEN_OV7660
4851 || sd->sensor == SEN_OV7670) {
Jean-François Moine62833ac2010-10-02 04:27:02 -03004852 switch (sd->ctrls[FREQ].val) {
Hans de Goede02ab18b2009-06-14 04:32:04 -03004853 case 0: /* Banding filter disabled */
Jean-François Moine21867802010-11-12 06:12:09 -03004854 i2c_w_mask(sd, OV7670_R13_COM8, 0, OV7670_COM8_BFILT);
Hans de Goede02ab18b2009-06-14 04:32:04 -03004855 break;
4856 case 1: /* 50 hz */
Jean-François Moine21867802010-11-12 06:12:09 -03004857 i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
Hans de Goede02ab18b2009-06-14 04:32:04 -03004858 OV7670_COM8_BFILT);
Jean-François Moine21867802010-11-12 06:12:09 -03004859 i2c_w_mask(sd, OV7670_R3B_COM11, 0x08, 0x18);
Hans de Goede02ab18b2009-06-14 04:32:04 -03004860 break;
4861 case 2: /* 60 hz */
Jean-François Moine21867802010-11-12 06:12:09 -03004862 i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
Hans de Goede02ab18b2009-06-14 04:32:04 -03004863 OV7670_COM8_BFILT);
Jean-François Moine21867802010-11-12 06:12:09 -03004864 i2c_w_mask(sd, OV7670_R3B_COM11, 0x00, 0x18);
Hans de Goede02ab18b2009-06-14 04:32:04 -03004865 break;
Jean-François Moine21867802010-11-12 06:12:09 -03004866 case 3: /* Auto hz - ov7670 only */
4867 i2c_w_mask(sd, OV7670_R13_COM8, OV7670_COM8_BFILT,
Hans de Goede02ab18b2009-06-14 04:32:04 -03004868 OV7670_COM8_BFILT);
Jean-François Moine21867802010-11-12 06:12:09 -03004869 i2c_w_mask(sd, OV7670_R3B_COM11, OV7670_COM11_HZAUTO,
Hans de Goede02ab18b2009-06-14 04:32:04 -03004870 0x18);
4871 break;
4872 }
4873 } else {
Jean-François Moine62833ac2010-10-02 04:27:02 -03004874 switch (sd->ctrls[FREQ].val) {
Hans de Goede02ab18b2009-06-14 04:32:04 -03004875 case 0: /* Banding filter disabled */
4876 i2c_w_mask(sd, 0x2d, 0x00, 0x04);
4877 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4878 break;
4879 case 1: /* 50 hz (filter on and framerate adj) */
4880 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4881 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4882 /* 20 fps -> 16.667 fps */
4883 if (sd->sensor == SEN_OV6620 ||
Hans de Goede7d971372009-06-14 05:28:17 -03004884 sd->sensor == SEN_OV6630 ||
4885 sd->sensor == SEN_OV66308AF)
Hans de Goede02ab18b2009-06-14 04:32:04 -03004886 i2c_w(sd, 0x2b, 0x5e);
4887 else
4888 i2c_w(sd, 0x2b, 0xac);
4889 break;
4890 case 2: /* 60 hz (filter on, ...) */
4891 i2c_w_mask(sd, 0x2d, 0x04, 0x04);
4892 if (sd->sensor == SEN_OV6620 ||
Hans de Goede7d971372009-06-14 05:28:17 -03004893 sd->sensor == SEN_OV6630 ||
4894 sd->sensor == SEN_OV66308AF) {
Hans de Goede02ab18b2009-06-14 04:32:04 -03004895 /* 20 fps -> 15 fps */
4896 i2c_w_mask(sd, 0x2a, 0x80, 0x80);
4897 i2c_w(sd, 0x2b, 0xa8);
4898 } else {
4899 /* no framerate adj. */
4900 i2c_w_mask(sd, 0x2a, 0x00, 0x80);
4901 }
4902 break;
4903 }
4904 }
4905}
Jean-François Moine62833ac2010-10-02 04:27:02 -03004906static void setfreq(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004907{
4908 struct sd *sd = (struct sd *) gspca_dev;
4909
Jean-François Moine62833ac2010-10-02 04:27:02 -03004910 setfreq_i(sd);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004911
Jean-François Moine62833ac2010-10-02 04:27:02 -03004912 /* Ugly but necessary */
4913 if (sd->bridge == BRIDGE_W9968CF)
4914 w9968cf_set_crop_window(sd);
Hans de Goede02ab18b2009-06-14 04:32:04 -03004915}
4916
4917static int sd_querymenu(struct gspca_dev *gspca_dev,
4918 struct v4l2_querymenu *menu)
4919{
4920 struct sd *sd = (struct sd *) gspca_dev;
4921
4922 switch (menu->id) {
4923 case V4L2_CID_POWER_LINE_FREQUENCY:
4924 switch (menu->index) {
4925 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
4926 strcpy((char *) menu->name, "NoFliker");
4927 return 0;
4928 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
4929 strcpy((char *) menu->name, "50 Hz");
4930 return 0;
4931 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
4932 strcpy((char *) menu->name, "60 Hz");
4933 return 0;
4934 case 3:
4935 if (sd->sensor != SEN_OV7670)
4936 return -EINVAL;
4937
4938 strcpy((char *) menu->name, "Automatic");
4939 return 0;
4940 }
4941 break;
4942 }
4943 return -EINVAL;
4944}
4945
Hans de Goede79b35902009-10-19 06:08:01 -03004946static int sd_get_jcomp(struct gspca_dev *gspca_dev,
4947 struct v4l2_jpegcompression *jcomp)
4948{
4949 struct sd *sd = (struct sd *) gspca_dev;
4950
4951 if (sd->bridge != BRIDGE_W9968CF)
4952 return -EINVAL;
4953
4954 memset(jcomp, 0, sizeof *jcomp);
4955 jcomp->quality = sd->quality;
4956 jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT |
4957 V4L2_JPEG_MARKER_DRI;
4958 return 0;
4959}
4960
4961static int sd_set_jcomp(struct gspca_dev *gspca_dev,
4962 struct v4l2_jpegcompression *jcomp)
4963{
4964 struct sd *sd = (struct sd *) gspca_dev;
4965
4966 if (sd->bridge != BRIDGE_W9968CF)
4967 return -EINVAL;
4968
4969 if (gspca_dev->streaming)
4970 return -EBUSY;
4971
4972 if (jcomp->quality < QUALITY_MIN)
4973 sd->quality = QUALITY_MIN;
4974 else if (jcomp->quality > QUALITY_MAX)
4975 sd->quality = QUALITY_MAX;
4976 else
4977 sd->quality = jcomp->quality;
4978
4979 /* Return resulting jcomp params to app */
4980 sd_get_jcomp(gspca_dev, jcomp);
4981
4982 return 0;
4983}
4984
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004985/* sub-driver description */
Jean-Francois Moinea5ae2062008-07-04 11:16:16 -03004986static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004987 .name = MODULE_NAME,
4988 .ctrls = sd_ctrls,
4989 .nctrls = ARRAY_SIZE(sd_ctrls),
4990 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03004991 .init = sd_init,
Jean-François Moinec42cedb2011-02-10 13:37:48 -03004992 .isoc_init = sd_isoc_init,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004993 .start = sd_start,
4994 .stopN = sd_stopN,
Hans de Goede79b35902009-10-19 06:08:01 -03004995 .stop0 = sd_stop0,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03004996 .pkt_scan = sd_pkt_scan,
Hans de Goede417a4d22010-02-19 07:37:08 -03004997 .dq_callback = sd_reset_snapshot,
Hans de Goede02ab18b2009-06-14 04:32:04 -03004998 .querymenu = sd_querymenu,
Hans de Goede79b35902009-10-19 06:08:01 -03004999 .get_jcomp = sd_get_jcomp,
5000 .set_jcomp = sd_set_jcomp,
Jean-François Moine28566432010-10-01 07:33:26 -03005001#if defined(CONFIG_INPUT) || defined(CONFIG_INPUT_MODULE)
Hans de Goede417a4d22010-02-19 07:37:08 -03005002 .other_input = 1,
5003#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03005004};
5005
5006/* -- module initialisation -- */
Jean-François Moine95c967c2011-01-13 05:20:29 -03005007static const struct usb_device_id device_table[] = {
Hans de Goedea511ba92009-10-16 07:13:07 -03005008 {USB_DEVICE(0x041e, 0x4003), .driver_info = BRIDGE_W9968CF },
Hans de Goede49809d62009-06-07 12:10:39 -03005009 {USB_DEVICE(0x041e, 0x4052), .driver_info = BRIDGE_OV519 },
Jean-François Moine0fdee88e2011-07-03 05:24:05 -03005010 {USB_DEVICE(0x041e, 0x405f),
5011 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
Hans de Goede49809d62009-06-07 12:10:39 -03005012 {USB_DEVICE(0x041e, 0x4060), .driver_info = BRIDGE_OV519 },
5013 {USB_DEVICE(0x041e, 0x4061), .driver_info = BRIDGE_OV519 },
Hans de Goede9e4d8252009-06-14 06:25:06 -03005014 {USB_DEVICE(0x041e, 0x4064),
Jean-François Moine87bae742010-11-12 05:31:34 -03005015 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
Rafal Milecki518c8df2009-10-02 03:54:44 -03005016 {USB_DEVICE(0x041e, 0x4067), .driver_info = BRIDGE_OV519 },
Hans de Goede9e4d8252009-06-14 06:25:06 -03005017 {USB_DEVICE(0x041e, 0x4068),
Jean-François Moine87bae742010-11-12 05:31:34 -03005018 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
Hans de Goede49809d62009-06-07 12:10:39 -03005019 {USB_DEVICE(0x045e, 0x028c), .driver_info = BRIDGE_OV519 },
5020 {USB_DEVICE(0x054c, 0x0154), .driver_info = BRIDGE_OV519 },
Hans de Goede98184f72010-01-07 12:06:00 -03005021 {USB_DEVICE(0x054c, 0x0155),
Jean-François Moine87bae742010-11-12 05:31:34 -03005022 .driver_info = BRIDGE_OV519 | BRIDGE_INVERT_LED },
Hans de Goede1876bb92009-06-14 06:45:50 -03005023 {USB_DEVICE(0x05a9, 0x0511), .driver_info = BRIDGE_OV511 },
Hans de Goede49809d62009-06-07 12:10:39 -03005024 {USB_DEVICE(0x05a9, 0x0518), .driver_info = BRIDGE_OV518 },
5025 {USB_DEVICE(0x05a9, 0x0519), .driver_info = BRIDGE_OV519 },
5026 {USB_DEVICE(0x05a9, 0x0530), .driver_info = BRIDGE_OV519 },
Hans de Goedeb46aaa02009-10-12 10:07:57 -03005027 {USB_DEVICE(0x05a9, 0x2800), .driver_info = BRIDGE_OVFX2 },
Hans de Goede49809d62009-06-07 12:10:39 -03005028 {USB_DEVICE(0x05a9, 0x4519), .driver_info = BRIDGE_OV519 },
5029 {USB_DEVICE(0x05a9, 0x8519), .driver_info = BRIDGE_OV519 },
Hans de Goede1876bb92009-06-14 06:45:50 -03005030 {USB_DEVICE(0x05a9, 0xa511), .driver_info = BRIDGE_OV511PLUS },
Hans de Goede49809d62009-06-07 12:10:39 -03005031 {USB_DEVICE(0x05a9, 0xa518), .driver_info = BRIDGE_OV518PLUS },
Hans de Goede1876bb92009-06-14 06:45:50 -03005032 {USB_DEVICE(0x0813, 0x0002), .driver_info = BRIDGE_OV511PLUS },
Hans de Goedeb46aaa02009-10-12 10:07:57 -03005033 {USB_DEVICE(0x0b62, 0x0059), .driver_info = BRIDGE_OVFX2 },
5034 {USB_DEVICE(0x0e96, 0xc001), .driver_info = BRIDGE_OVFX2 },
Hans de Goedea511ba92009-10-16 07:13:07 -03005035 {USB_DEVICE(0x1046, 0x9967), .driver_info = BRIDGE_W9968CF },
Jean-François Moine87bae742010-11-12 05:31:34 -03005036 {USB_DEVICE(0x8020, 0xef04), .driver_info = BRIDGE_OVFX2 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03005037 {}
5038};
Jean-Francois Moineac40b1f2008-11-08 06:03:37 -03005039
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03005040MODULE_DEVICE_TABLE(usb, device_table);
5041
5042/* -- device connect -- */
5043static int sd_probe(struct usb_interface *intf,
5044 const struct usb_device_id *id)
5045{
5046 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
5047 THIS_MODULE);
5048}
5049
5050static struct usb_driver sd_driver = {
5051 .name = MODULE_NAME,
5052 .id_table = device_table,
5053 .probe = sd_probe,
5054 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03005055#ifdef CONFIG_PM
5056 .suspend = gspca_suspend,
5057 .resume = gspca_resume,
5058#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03005059};
5060
5061/* -- module insert / remove -- */
5062static int __init sd_mod_init(void)
5063{
Jean-François Moine54826432010-09-13 04:53:03 -03005064 return usb_register(&sd_driver);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03005065}
5066static void __exit sd_mod_exit(void)
5067{
5068 usb_deregister(&sd_driver);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03005069}
5070
5071module_init(sd_mod_init);
5072module_exit(sd_mod_exit);
5073
5074module_param(frame_rate, int, 0644);
5075MODULE_PARM_DESC(frame_rate, "Frame rate (5, 10, 15, 20 or 30 fps)");