blob: dc2d88cdd4a2cb8531d0589165d119e579c1c0e5 [file] [log] [blame]
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001/*
2 * sonix sn9c102 (bayer) library
3 * Copyright (C) 2003 2004 Michel Xhaard mxhaard@magic.fr
4 * Add Pas106 Stefano Mozzi (C) 2004
5 *
6 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
Hans de Goede93627732008-09-04 16:20:12 -030023/* Some documentation on known sonixb registers:
24
25Reg Use
260x10 high nibble red gain low nibble blue gain
270x11 low nibble green gain
280x12 hstart
290x13 vstart
300x15 hsize (hsize = register-value * 16)
310x16 vsize (vsize = register-value * 16)
320x17 bit 0 toggle compression quality (according to sn9c102 driver)
330x18 bit 7 enables compression, bit 4-5 set image down scaling:
34 00 scale 1, 01 scale 1/2, 10, scale 1/4
350x19 high-nibble is sensor clock divider, changes exposure on sensors which
36 use a clock generated by the bridge. Some sensors have their own clock.
370x1c auto_exposure area (for avg_lum) startx (startx = register-value * 32)
380x1d auto_exposure area (for avg_lum) starty (starty = register-value * 32)
390x1e auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32)
400x1f auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)
41*/
42
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030043#define MODULE_NAME "sonixb"
44
Hans de Goedef65e93d2010-01-31 10:35:15 -030045#include <linux/input.h>
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030046#include "gspca.h"
47
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030048MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
49MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
50MODULE_LICENSE("GPL");
51
52/* specific webcam descriptor */
53struct sd {
54 struct gspca_dev gspca_dev; /* !! must be the first item */
Hans de Goededcef3232008-07-10 10:40:53 -030055 atomic_t avg_lum;
Hans de Goedebf2a2202008-09-04 16:22:56 -030056 int prev_avg_lum;
Hans de Goede26984b02010-02-01 13:18:37 -030057 int exp_too_low_cnt;
58 int exp_too_high_cnt;
Hans de Goededcef3232008-07-10 10:40:53 -030059
Hans de Goede26984b02010-02-01 13:18:37 -030060 unsigned short exposure;
Hans de Goedead5ef80d2008-07-14 10:11:42 -030061 unsigned char gain;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030062 unsigned char brightness;
Hans de Goededcef3232008-07-10 10:40:53 -030063 unsigned char autogain;
64 unsigned char autogain_ignore_frames;
Hans de Goede6af492e2008-07-22 07:09:33 -030065 unsigned char frames_to_drop;
Hans de Goede66f35822008-07-16 10:16:28 -030066 unsigned char freq; /* light freq filter setting */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030067
Hans de Goedef45f06b2008-09-03 17:12:21 -030068 __u8 bridge; /* Type of bridge */
69#define BRIDGE_101 0
70#define BRIDGE_102 0 /* We make no difference between 101 and 102 */
71#define BRIDGE_103 1
72
73 __u8 sensor; /* Type of image sensor chip */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030074#define SENSOR_HV7131R 0
75#define SENSOR_OV6650 1
76#define SENSOR_OV7630 2
Hans de Goede6af492e2008-07-22 07:09:33 -030077#define SENSOR_PAS106 3
78#define SENSOR_PAS202 4
Hans de Goedeb10af3f2010-01-10 19:31:34 -030079#define SENSOR_TAS5110C 5
80#define SENSOR_TAS5110D 6
81#define SENSOR_TAS5130CXX 7
Hans de Goede6af492e2008-07-22 07:09:33 -030082 __u8 reg11;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -030083};
84
Hans de Goedef45f06b2008-09-03 17:12:21 -030085typedef const __u8 sensor_init_t[8];
86
87struct sensor_data {
88 const __u8 *bridge_init[2];
89 int bridge_init_size[2];
90 sensor_init_t *sensor_init;
91 int sensor_init_size;
92 sensor_init_t *sensor_bridge_init[2];
93 int sensor_bridge_init_size[2];
94 int flags;
95 unsigned ctrl_dis;
96 __u8 sensor_addr;
97};
98
99/* sensor_data flags */
Jean-Francois Moine5da162e2008-07-26 14:17:23 -0300100#define F_GAIN 0x01 /* has gain */
Hans de Goedee2ad2a52008-09-03 17:12:15 -0300101#define F_SIF 0x02 /* sif or vga */
Hans de Goede26984b02010-02-01 13:18:37 -0300102#define F_COARSE_EXPO 0x04 /* exposure control is coarse */
Hans de Goedec437d652008-09-03 17:12:22 -0300103
104/* priv field of struct v4l2_pix_format flags (do not use low nibble!) */
105#define MODE_RAW 0x10 /* raw bayer mode */
Hans de Goede93627732008-09-04 16:20:12 -0300106#define MODE_REDUCED_SIF 0x20 /* vga mode (320x240 / 160x120) on sif cam */
Hans de Goedef45f06b2008-09-03 17:12:21 -0300107
108/* ctrl_dis helper macros */
Hans de Goede26984b02010-02-01 13:18:37 -0300109#define NO_EXPO ((1 << EXPOSURE_IDX) | (1 << COARSE_EXPOSURE_IDX) | \
110 (1 << AUTOGAIN_IDX))
Hans de Goedef45f06b2008-09-03 17:12:21 -0300111#define NO_FREQ (1 << FREQ_IDX)
112#define NO_BRIGHTNESS (1 << BRIGHTNESS_IDX)
Jean-Francois Moine5da162e2008-07-26 14:17:23 -0300113
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300114#define COMP2 0x8f
115#define COMP 0xc7 /* 0x87 //0x07 */
116#define COMP1 0xc9 /* 0x89 //0x09 */
117
118#define MCK_INIT 0x63
119#define MCK_INIT1 0x20 /*fixme: Bayer - 0x50 for JPEG ??*/
120
121#define SYS_CLK 0x04
122
Hans de Goedef45f06b2008-09-03 17:12:21 -0300123#define SENS(bridge_1, bridge_3, sensor, sensor_1, \
124 sensor_3, _flags, _ctrl_dis, _sensor_addr) \
125{ \
126 .bridge_init = { bridge_1, bridge_3 }, \
127 .bridge_init_size = { sizeof(bridge_1), sizeof(bridge_3) }, \
128 .sensor_init = sensor, \
129 .sensor_init_size = sizeof(sensor), \
130 .sensor_bridge_init = { sensor_1, sensor_3,}, \
131 .sensor_bridge_init_size = { sizeof(sensor_1), sizeof(sensor_3)}, \
132 .flags = _flags, .ctrl_dis = _ctrl_dis, .sensor_addr = _sensor_addr \
133}
134
Hans de Goededcef3232008-07-10 10:40:53 -0300135/* We calculate the autogain at the end of the transfer of a frame, at this
Hans de Goede26984b02010-02-01 13:18:37 -0300136 moment a frame with the old settings is being captured and transmitted. So
137 if we adjust the gain or exposure we must ignore atleast the next frame for
138 the new settings to come into effect before doing any other adjustments. */
139#define AUTOGAIN_IGNORE_FRAMES 1
Hans de Goededcef3232008-07-10 10:40:53 -0300140
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300141/* V4L2 controls supported by the driver */
142static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
143static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goededcef3232008-07-10 10:40:53 -0300144static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val);
145static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val);
146static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val);
147static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val);
148static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val);
149static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val);
Hans de Goede66f35822008-07-16 10:16:28 -0300150static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
151static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300152
Marton Nemeth7e64dc42009-12-30 09:12:41 -0300153static const struct ctrl sd_ctrls[] = {
Hans de Goedee2ad2a52008-09-03 17:12:15 -0300154#define BRIGHTNESS_IDX 0
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300155 {
156 {
157 .id = V4L2_CID_BRIGHTNESS,
158 .type = V4L2_CTRL_TYPE_INTEGER,
159 .name = "Brightness",
160 .minimum = 0,
161 .maximum = 255,
162 .step = 1,
Hans de Goededcef3232008-07-10 10:40:53 -0300163#define BRIGHTNESS_DEF 127
164 .default_value = BRIGHTNESS_DEF,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300165 },
166 .set = sd_setbrightness,
167 .get = sd_getbrightness,
168 },
Hans de Goedee2ad2a52008-09-03 17:12:15 -0300169#define GAIN_IDX 1
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300170 {
171 {
Hans de Goededcef3232008-07-10 10:40:53 -0300172 .id = V4L2_CID_GAIN,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300173 .type = V4L2_CTRL_TYPE_INTEGER,
Hans de Goededcef3232008-07-10 10:40:53 -0300174 .name = "Gain",
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300175 .minimum = 0,
Hans de Goedead5ef80d2008-07-14 10:11:42 -0300176 .maximum = 255,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300177 .step = 1,
Hans de Goedead5ef80d2008-07-14 10:11:42 -0300178#define GAIN_DEF 127
179#define GAIN_KNEE 200
Hans de Goededcef3232008-07-10 10:40:53 -0300180 .default_value = GAIN_DEF,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300181 },
Hans de Goededcef3232008-07-10 10:40:53 -0300182 .set = sd_setgain,
183 .get = sd_getgain,
184 },
Hans de Goedee2ad2a52008-09-03 17:12:15 -0300185#define EXPOSURE_IDX 2
Hans de Goededcef3232008-07-10 10:40:53 -0300186 {
187 {
188 .id = V4L2_CID_EXPOSURE,
189 .type = V4L2_CTRL_TYPE_INTEGER,
190 .name = "Exposure",
Hans de Goede26984b02010-02-01 13:18:37 -0300191#define EXPOSURE_DEF 33 /* 33 ms / 30 fps */
192#define EXPOSURE_KNEE 100 /* 100 ms / 10 fps */
Hans de Goededcef3232008-07-10 10:40:53 -0300193 .minimum = 0,
Hans de Goede26984b02010-02-01 13:18:37 -0300194 .maximum = 511,
Hans de Goededcef3232008-07-10 10:40:53 -0300195 .step = 1,
196 .default_value = EXPOSURE_DEF,
197 .flags = 0,
198 },
199 .set = sd_setexposure,
200 .get = sd_getexposure,
201 },
Hans de Goede26984b02010-02-01 13:18:37 -0300202#define COARSE_EXPOSURE_IDX 3
203 {
204 {
205 .id = V4L2_CID_EXPOSURE,
206 .type = V4L2_CTRL_TYPE_INTEGER,
207 .name = "Exposure",
208#define COARSE_EXPOSURE_DEF 2 /* 30 fps */
209 .minimum = 2,
210 .maximum = 15,
211 .step = 1,
212 .default_value = COARSE_EXPOSURE_DEF,
213 .flags = 0,
214 },
215 .set = sd_setexposure,
216 .get = sd_getexposure,
217 },
218#define AUTOGAIN_IDX 4
Hans de Goededcef3232008-07-10 10:40:53 -0300219 {
220 {
221 .id = V4L2_CID_AUTOGAIN,
222 .type = V4L2_CTRL_TYPE_BOOLEAN,
223 .name = "Automatic Gain (and Exposure)",
224 .minimum = 0,
225 .maximum = 1,
226 .step = 1,
Andoni Zubimendi51fc8e32008-07-10 11:12:24 -0300227#define AUTOGAIN_DEF 1
228 .default_value = AUTOGAIN_DEF,
Hans de Goededcef3232008-07-10 10:40:53 -0300229 .flags = 0,
230 },
231 .set = sd_setautogain,
232 .get = sd_getautogain,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300233 },
Hans de Goede26984b02010-02-01 13:18:37 -0300234#define FREQ_IDX 5
Hans de Goede66f35822008-07-16 10:16:28 -0300235 {
236 {
237 .id = V4L2_CID_POWER_LINE_FREQUENCY,
238 .type = V4L2_CTRL_TYPE_MENU,
239 .name = "Light frequency filter",
240 .minimum = 0,
241 .maximum = 2, /* 0: 0, 1: 50Hz, 2:60Hz */
242 .step = 1,
243#define FREQ_DEF 1
244 .default_value = FREQ_DEF,
245 },
246 .set = sd_setfreq,
247 .get = sd_getfreq,
248 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300249};
250
Jean-Francois Moinecc611b82008-12-29 07:49:41 -0300251static const struct v4l2_pix_format vga_mode[] = {
Hans de Goedec437d652008-09-03 17:12:22 -0300252 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
253 .bytesperline = 160,
Mauro Carvalho Chehab2389b362008-08-06 20:13:46 -0300254 .sizeimage = 160 * 120,
Hans de Goedec437d652008-09-03 17:12:22 -0300255 .colorspace = V4L2_COLORSPACE_SRGB,
256 .priv = 2 | MODE_RAW},
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300257 {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
258 .bytesperline = 160,
Hans de Goede5c51518d2008-09-03 17:12:22 -0300259 .sizeimage = 160 * 120 * 5 / 4,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300260 .colorspace = V4L2_COLORSPACE_SRGB,
261 .priv = 2},
262 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
263 .bytesperline = 320,
Hans de Goede5c51518d2008-09-03 17:12:22 -0300264 .sizeimage = 320 * 240 * 5 / 4,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300265 .colorspace = V4L2_COLORSPACE_SRGB,
266 .priv = 1},
267 {640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
268 .bytesperline = 640,
Hans de Goede5c51518d2008-09-03 17:12:22 -0300269 .sizeimage = 640 * 480 * 5 / 4,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300270 .colorspace = V4L2_COLORSPACE_SRGB,
271 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300272};
Jean-Francois Moinecc611b82008-12-29 07:49:41 -0300273static const struct v4l2_pix_format sif_mode[] = {
Hans de Goede93627732008-09-04 16:20:12 -0300274 {160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
275 .bytesperline = 160,
276 .sizeimage = 160 * 120,
277 .colorspace = V4L2_COLORSPACE_SRGB,
278 .priv = 1 | MODE_RAW | MODE_REDUCED_SIF},
279 {160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
280 .bytesperline = 160,
281 .sizeimage = 160 * 120 * 5 / 4,
282 .colorspace = V4L2_COLORSPACE_SRGB,
283 .priv = 1 | MODE_REDUCED_SIF},
Hans de Goedec437d652008-09-03 17:12:22 -0300284 {176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
285 .bytesperline = 176,
Mauro Carvalho Chehab2389b362008-08-06 20:13:46 -0300286 .sizeimage = 176 * 144,
Hans de Goedec437d652008-09-03 17:12:22 -0300287 .colorspace = V4L2_COLORSPACE_SRGB,
288 .priv = 1 | MODE_RAW},
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300289 {176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
290 .bytesperline = 176,
Hans de Goede5c51518d2008-09-03 17:12:22 -0300291 .sizeimage = 176 * 144 * 5 / 4,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300292 .colorspace = V4L2_COLORSPACE_SRGB,
293 .priv = 1},
Hans de Goede93627732008-09-04 16:20:12 -0300294 {320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
295 .bytesperline = 320,
296 .sizeimage = 320 * 240 * 5 / 4,
297 .colorspace = V4L2_COLORSPACE_SRGB,
298 .priv = 0 | MODE_REDUCED_SIF},
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300299 {352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
300 .bytesperline = 352,
Hans de Goede5c51518d2008-09-03 17:12:22 -0300301 .sizeimage = 352 * 288 * 5 / 4,
Jean-Francois Moinec2446b32008-07-05 11:49:20 -0300302 .colorspace = V4L2_COLORSPACE_SRGB,
303 .priv = 0},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300304};
305
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300306static const __u8 initHv7131[] = {
307 0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
308 0x00, 0x00,
Hans de Goedec437d652008-09-03 17:12:22 -0300309 0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300310 0x28, 0x1e, 0x60, 0x8a, 0x20,
311 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c
312};
313static const __u8 hv7131_sensor_init[][8] = {
314 {0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},
315 {0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},
316 {0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},
317 {0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},
318 {0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},
319};
320static const __u8 initOv6650[] = {
321 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
322 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Hans de Goedec437d652008-09-03 17:12:22 -0300323 0x00, 0x01, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x8b,
Hans de Goede93627732008-09-04 16:20:12 -0300324 0x10, 0x1d, 0x10, 0x02, 0x02, 0x09, 0x07
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300325};
326static const __u8 ov6650_sensor_init[][8] =
327{
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200328 /* Bright, contrast, etc are set through SCBB interface.
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300329 * AVCAP on win2 do not send any data on this controls. */
330 /* Anyway, some registers appears to alter bright and constrat */
Hans de Goededcef3232008-07-10 10:40:53 -0300331
332 /* Reset sensor */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300333 {0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
Hans de Goededcef3232008-07-10 10:40:53 -0300334 /* Set clock register 0x11 low nibble is clock divider */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300335 {0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
Hans de Goededcef3232008-07-10 10:40:53 -0300336 /* Next some unknown stuff */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300337 {0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
338/* {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
339 * THIS SET GREEN SCREEN
340 * (pixels could be innverted in decode kind of "brg",
341 * but blue wont be there. Avoid this data ... */
342 {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
343 {0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
344 {0xa0, 0x60, 0x30, 0x3d, 0x0A, 0xd8, 0xa4, 0x10},
Hans de Goede722103e2008-07-17 10:24:47 -0300345 /* Enable rgb brightness control */
346 {0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
347 /* HDG: Note windows uses the line below, which sets both register 0x60
348 and 0x61 I believe these registers of the ov6650 are identical as
349 those of the ov7630, because if this is true the windows settings
350 add a bit additional red gain and a lot additional blue gain, which
351 matches my findings that the windows settings make blue much too
352 blue and red a little too red.
353 {0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
Hans de Goededcef3232008-07-10 10:40:53 -0300354 /* Some more unknown stuff */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300355 {0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
356 {0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300357};
Hans de Goededcef3232008-07-10 10:40:53 -0300358
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300359static const __u8 initOv7630[] = {
360 0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, /* r01 .. r08 */
361 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
Hans de Goedec437d652008-09-03 17:12:22 -0300362 0x00, 0x01, 0x01, 0x0a, /* r11 .. r14 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300363 0x28, 0x1e, /* H & V sizes r15 .. r16 */
Hans de Goedef45f06b2008-09-03 17:12:21 -0300364 0x68, COMP2, MCK_INIT1, /* r17 .. r19 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300365 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c /* r1a .. r1f */
366};
367static const __u8 initOv7630_3[] = {
368 0x44, 0x44, 0x00, 0x1a, 0x20, 0x20, 0x20, 0x80, /* r01 .. r08 */
Hans de Goede4efcfa02010-02-01 07:48:17 -0300369 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* r09 .. r10 */
Hans de Goede4cce1652008-09-04 16:22:57 -0300370 0x00, 0x02, 0x01, 0x0a, /* r11 .. r14 */
Hans de Goede3647fea2008-07-15 05:36:30 -0300371 0x28, 0x1e, /* H & V sizes r15 .. r16 */
Andoni Zubimendi51fc8e32008-07-10 11:12:24 -0300372 0x68, 0x8f, MCK_INIT1, /* r17 .. r19 */
373 0x1d, 0x10, 0x02, 0x03, 0x0f, 0x0c, 0x00, /* r1a .. r20 */
374 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80, /* r21 .. r28 */
375 0x90, 0xa0, 0xb0, 0xc0, 0xd0, 0xe0, 0xf0, 0xff /* r29 .. r30 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300376};
Hans de Goede6af492e2008-07-22 07:09:33 -0300377static const __u8 ov7630_sensor_init[][8] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300378 {0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
379 {0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
380/* {0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10}, jfm */
Andoni Zubimendi794af522008-07-16 08:33:14 -0300381 {0xd0, 0x21, 0x12, 0x1c, 0x00, 0x80, 0x34, 0x10}, /* jfm */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300382 {0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
383 {0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
384 {0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
385 {0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
386 {0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
387 {0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
388 {0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
Andoni Zubimendi794af522008-07-16 08:33:14 -0300389 {0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
390/* {0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10}, * jfm */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300391 {0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
392 {0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
393 {0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
394 {0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
395 {0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
396 {0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
397};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300398
Hans de Goedef45f06b2008-09-03 17:12:21 -0300399static const __u8 ov7630_sensor_init_3[][8] = {
400 {0xa0, 0x21, 0x13, 0x80, 0x00, 0x00, 0x00, 0x10},
401};
402
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300403static const __u8 initPas106[] = {
404 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
405 0x00, 0x00,
Hans de Goedec437d652008-09-03 17:12:22 -0300406 0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
Hans de Goedef45f06b2008-09-03 17:12:21 -0300407 0x16, 0x12, 0x24, COMP1, MCK_INIT1,
Hans de Goede93627732008-09-04 16:20:12 -0300408 0x18, 0x10, 0x02, 0x02, 0x09, 0x07
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300409};
410/* compression 0x86 mckinit1 0x2b */
Hans de Goedef45f06b2008-09-03 17:12:21 -0300411static const __u8 pas106_sensor_init[][8] = {
412 /* Pixel Clock Divider 6 */
413 { 0xa1, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x14 },
414 /* Frame Time MSB (also seen as 0x12) */
415 { 0xa1, 0x40, 0x03, 0x13, 0x00, 0x00, 0x00, 0x14 },
416 /* Frame Time LSB (also seen as 0x05) */
417 { 0xa1, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x14 },
418 /* Shutter Time Line Offset (also seen as 0x6d) */
419 { 0xa1, 0x40, 0x05, 0x65, 0x00, 0x00, 0x00, 0x14 },
420 /* Shutter Time Pixel Offset (also seen as 0xb1) */
421 { 0xa1, 0x40, 0x06, 0xcd, 0x00, 0x00, 0x00, 0x14 },
422 /* Black Level Subtract Sign (also seen 0x00) */
423 { 0xa1, 0x40, 0x07, 0xc1, 0x00, 0x00, 0x00, 0x14 },
424 /* Black Level Subtract Level (also seen 0x01) */
425 { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
426 { 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
427 /* Color Gain B Pixel 5 a */
428 { 0xa1, 0x40, 0x09, 0x05, 0x00, 0x00, 0x00, 0x14 },
429 /* Color Gain G1 Pixel 1 5 */
430 { 0xa1, 0x40, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x14 },
431 /* Color Gain G2 Pixel 1 0 5 */
432 { 0xa1, 0x40, 0x0b, 0x04, 0x00, 0x00, 0x00, 0x14 },
433 /* Color Gain R Pixel 3 1 */
434 { 0xa1, 0x40, 0x0c, 0x05, 0x00, 0x00, 0x00, 0x14 },
435 /* Color GainH Pixel */
436 { 0xa1, 0x40, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x14 },
437 /* Global Gain */
438 { 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },
439 /* Contrast */
440 { 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },
441 /* H&V synchro polarity */
442 { 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },
443 /* ?default */
444 { 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },
445 /* DAC scale */
446 { 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },
447 /* ?default */
448 { 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },
449 /* Validate Settings */
450 { 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300451};
Hans de Goedef45f06b2008-09-03 17:12:21 -0300452
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300453static const __u8 initPas202[] = {
454 0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
455 0x00, 0x00,
Hans de Goedec437d652008-09-03 17:12:22 -0300456 0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
Hans de Goedef45f06b2008-09-03 17:12:21 -0300457 0x28, 0x1e, 0x28, 0x89, 0x20,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300458 0x00, 0x00, 0x02, 0x03, 0x0f, 0x0c
459};
460static const __u8 pas202_sensor_init[][8] = {
461 {0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10},
462 {0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
463 {0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
464 {0xd0, 0x40, 0x0C, 0x00, 0x0C, 0x00, 0x32, 0x10},
465 {0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
466 {0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
467 {0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
468 {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
469 {0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
470 {0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
471 {0xb0, 0x40, 0x04, 0x07, 0x2a, 0x00, 0x63, 0x10},
472 {0xb0, 0x40, 0x0e, 0x00, 0x3d, 0x00, 0x63, 0x10},
473
474 {0xa0, 0x40, 0x11, 0x01, 0x3d, 0x00, 0x63, 0x16},
475 {0xa0, 0x40, 0x10, 0x08, 0x3d, 0x00, 0x63, 0x15},
476 {0xa0, 0x40, 0x02, 0x04, 0x3d, 0x00, 0x63, 0x16},
477 {0xa0, 0x40, 0x11, 0x01, 0x3d, 0x00, 0x63, 0x16},
478 {0xb0, 0x40, 0x0e, 0x00, 0x31, 0x00, 0x63, 0x16},
479 {0xa0, 0x40, 0x11, 0x01, 0x31, 0x00, 0x63, 0x16},
480 {0xa0, 0x40, 0x10, 0x0e, 0x31, 0x00, 0x63, 0x15},
481 {0xa0, 0x40, 0x11, 0x01, 0x31, 0x00, 0x63, 0x16},
482};
483
Hans de Goedeb10af3f2010-01-10 19:31:34 -0300484static const __u8 initTas5110c[] = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300485 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
486 0x00, 0x00,
Hans de Goede4efcfa02010-02-01 07:48:17 -0300487 0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300488 0x16, 0x12, 0x60, 0x86, 0x2b,
489 0x14, 0x0a, 0x02, 0x02, 0x09, 0x07
490};
Hans de Goedeb10af3f2010-01-10 19:31:34 -0300491/* Same as above, except a different hstart */
492static const __u8 initTas5110d[] = {
493 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
494 0x00, 0x00,
Hans de Goede4efcfa02010-02-01 07:48:17 -0300495 0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
Hans de Goedeb10af3f2010-01-10 19:31:34 -0300496 0x16, 0x12, 0x60, 0x86, 0x2b,
497 0x14, 0x0a, 0x02, 0x02, 0x09, 0x07
498};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300499static const __u8 tas5110_sensor_init[][8] = {
500 {0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
501 {0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
502 {0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17},
503};
504
505static const __u8 initTas5130[] = {
506 0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
507 0x00, 0x00,
Hans de Goede4efcfa02010-02-01 07:48:17 -0300508 0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300509 0x28, 0x1e, 0x60, COMP, MCK_INIT,
510 0x18, 0x10, 0x04, 0x03, 0x11, 0x0c
511};
512static const __u8 tas5130_sensor_init[][8] = {
513/* {0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
514 * shutter 0x47 short exposure? */
515 {0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
516 /* shutter 0x01 long exposure */
517 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
518};
519
Hans Verkuild45b9b82008-09-04 03:33:43 -0300520static struct sensor_data sensor_data[] = {
Hans de Goedef45f06b2008-09-03 17:12:21 -0300521SENS(initHv7131, NULL, hv7131_sensor_init, NULL, NULL, 0, NO_EXPO|NO_FREQ, 0),
Hans de Goede93627732008-09-04 16:20:12 -0300522SENS(initOv6650, NULL, ov6650_sensor_init, NULL, NULL, F_GAIN|F_SIF, 0, 0x60),
Hans de Goedef45f06b2008-09-03 17:12:21 -0300523SENS(initOv7630, initOv7630_3, ov7630_sensor_init, NULL, ov7630_sensor_init_3,
524 F_GAIN, 0, 0x21),
525SENS(initPas106, NULL, pas106_sensor_init, NULL, NULL, F_SIF, NO_EXPO|NO_FREQ,
526 0),
Hans de Goede93627732008-09-04 16:20:12 -0300527SENS(initPas202, initPas202, pas202_sensor_init, NULL, NULL, 0,
Hans de Goedef45f06b2008-09-03 17:12:21 -0300528 NO_EXPO|NO_FREQ, 0),
Hans de Goede26984b02010-02-01 13:18:37 -0300529SENS(initTas5110c, NULL, tas5110_sensor_init, NULL, NULL,
530 F_GAIN|F_SIF|F_COARSE_EXPO, NO_BRIGHTNESS|NO_FREQ, 0),
531SENS(initTas5110d, NULL, tas5110_sensor_init, NULL, NULL,
532 F_GAIN|F_SIF|F_COARSE_EXPO, NO_BRIGHTNESS|NO_FREQ, 0),
Hans de Goedef45f06b2008-09-03 17:12:21 -0300533SENS(initTas5130, NULL, tas5130_sensor_init, NULL, NULL, 0, NO_EXPO|NO_FREQ,
534 0),
535};
536
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300537/* get one byte in gspca_dev->usb_buf */
538static void reg_r(struct gspca_dev *gspca_dev,
539 __u16 value)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300540{
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300541 usb_control_msg(gspca_dev->dev,
542 usb_rcvctrlpipe(gspca_dev->dev, 0),
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300543 0, /* request */
544 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
545 value,
546 0, /* index */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300547 gspca_dev->usb_buf, 1,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300548 500);
549}
550
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300551static void reg_w(struct gspca_dev *gspca_dev,
552 __u16 value,
553 const __u8 *buffer,
554 int len)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300555{
Jean-Francois Moine335b3f82008-07-30 04:53:02 -0300556#ifdef GSPCA_DEBUG
Jean-Francois Moine8295d992008-09-03 17:12:19 -0300557 if (len > USB_BUF_SZ) {
Hans de Goede0d2a7222008-07-03 08:15:22 -0300558 PDEBUG(D_ERR|D_PACK, "reg_w: buffer overflow");
559 return;
560 }
561#endif
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300562 memcpy(gspca_dev->usb_buf, buffer, len);
563 usb_control_msg(gspca_dev->dev,
564 usb_sndctrlpipe(gspca_dev->dev, 0),
565 0x08, /* request */
566 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
567 value,
568 0, /* index */
569 gspca_dev->usb_buf, len,
570 500);
571}
572
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300573static int i2c_w(struct gspca_dev *gspca_dev, const __u8 *buffer)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300574{
575 int retry = 60;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300576
577 /* is i2c ready */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300578 reg_w(gspca_dev, 0x08, buffer, 8);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300579 while (retry--) {
580 msleep(10);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300581 reg_r(gspca_dev, 0x08);
Andoni Zubimendib7474cf92008-07-16 08:40:30 -0300582 if (gspca_dev->usb_buf[0] & 0x04) {
583 if (gspca_dev->usb_buf[0] & 0x08)
584 return -1;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300585 return 0;
Andoni Zubimendib7474cf92008-07-16 08:40:30 -0300586 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300587 }
588 return -1;
589}
590
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300591static void i2c_w_vector(struct gspca_dev *gspca_dev,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300592 const __u8 buffer[][8], int len)
593{
594 for (;;) {
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300595 reg_w(gspca_dev, 0x08, *buffer, 8);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300596 len -= 8;
597 if (len <= 0)
598 break;
599 buffer++;
600 }
601}
602
603static void setbrightness(struct gspca_dev *gspca_dev)
604{
605 struct sd *sd = (struct sd *) gspca_dev;
606 __u8 value;
607
608 switch (sd->sensor) {
Hans de Goedea975a522008-07-16 15:29:11 -0300609 case SENSOR_OV6650:
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300610 case SENSOR_OV7630: {
611 __u8 i2cOV[] =
Hans de Goedea975a522008-07-16 15:29:11 -0300612 {0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300613
614 /* change reg 0x06 */
Hans de Goedef45f06b2008-09-03 17:12:21 -0300615 i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300616 i2cOV[3] = sd->brightness;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300617 if (i2c_w(gspca_dev, i2cOV) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300618 goto err;
619 break;
620 }
621 case SENSOR_PAS106: {
622 __u8 i2c1[] =
623 {0xa1, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14};
624
625 i2c1[3] = sd->brightness >> 3;
626 i2c1[2] = 0x0e;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300627 if (i2c_w(gspca_dev, i2c1) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300628 goto err;
629 i2c1[3] = 0x01;
630 i2c1[2] = 0x13;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300631 if (i2c_w(gspca_dev, i2c1) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300632 goto err;
633 break;
634 }
635 case SENSOR_PAS202: {
636 /* __u8 i2cpexpo1[] =
637 {0xb0, 0x40, 0x04, 0x07, 0x2a, 0x00, 0x63, 0x16}; */
638 __u8 i2cpexpo[] =
639 {0xb0, 0x40, 0x0e, 0x01, 0xab, 0x00, 0x63, 0x16};
640 __u8 i2cp202[] =
641 {0xa0, 0x40, 0x10, 0x0e, 0x31, 0x00, 0x63, 0x15};
642 static __u8 i2cpdoit[] =
643 {0xa0, 0x40, 0x11, 0x01, 0x31, 0x00, 0x63, 0x16};
644
645 /* change reg 0x10 */
646 i2cpexpo[4] = 0xff - sd->brightness;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300647/* if(i2c_w(gspca_dev,i2cpexpo1) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300648 goto err; */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300649/* if(i2c_w(gspca_dev,i2cpdoit) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300650 goto err; */
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300651 if (i2c_w(gspca_dev, i2cpexpo) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300652 goto err;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300653 if (i2c_w(gspca_dev, i2cpdoit) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300654 goto err;
655 i2cp202[3] = sd->brightness >> 3;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300656 if (i2c_w(gspca_dev, i2cp202) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300657 goto err;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300658 if (i2c_w(gspca_dev, i2cpdoit) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300659 goto err;
660 break;
661 }
Hans de Goededcef3232008-07-10 10:40:53 -0300662 case SENSOR_TAS5130CXX: {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300663 __u8 i2c[] =
664 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
665
666 value = 0xff - sd->brightness;
667 i2c[4] = value;
668 PDEBUG(D_CONF, "brightness %d : %d", value, i2c[4]);
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300669 if (i2c_w(gspca_dev, i2c) < 0)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300670 goto err;
671 break;
672 }
673 }
674 return;
675err:
676 PDEBUG(D_ERR, "i2c error brightness");
677}
Hans de Goededcef3232008-07-10 10:40:53 -0300678
679static void setsensorgain(struct gspca_dev *gspca_dev)
680{
681 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goedea975a522008-07-16 15:29:11 -0300682 unsigned char gain = sd->gain;
Hans de Goededcef3232008-07-10 10:40:53 -0300683
684 switch (sd->sensor) {
685
Hans de Goedeb10af3f2010-01-10 19:31:34 -0300686 case SENSOR_TAS5110C:
687 case SENSOR_TAS5110D: {
Hans de Goededcef3232008-07-10 10:40:53 -0300688 __u8 i2c[] =
689 {0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
690
Hans de Goedea975a522008-07-16 15:29:11 -0300691 i2c[4] = 255 - gain;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300692 if (i2c_w(gspca_dev, i2c) < 0)
Hans de Goededcef3232008-07-10 10:40:53 -0300693 goto err;
Andoni Zubimendi51fc8e32008-07-10 11:12:24 -0300694 break;
695 }
Hans de Goedead5ef80d2008-07-14 10:11:42 -0300696
Hans de Goedea975a522008-07-16 15:29:11 -0300697 case SENSOR_OV6650:
698 gain >>= 1;
699 /* fall thru */
Hans de Goede6af492e2008-07-22 07:09:33 -0300700 case SENSOR_OV7630: {
Hans de Goedea975a522008-07-16 15:29:11 -0300701 __u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
Andoni Zubimendi794af522008-07-16 08:33:14 -0300702
Hans de Goedef45f06b2008-09-03 17:12:21 -0300703 i2c[1] = sensor_data[sd->sensor].sensor_addr;
Hans de Goedea975a522008-07-16 15:29:11 -0300704 i2c[3] = gain >> 2;
Andoni Zubimendi794af522008-07-16 08:33:14 -0300705 if (i2c_w(gspca_dev, i2c) < 0)
706 goto err;
707 break;
708 }
Hans de Goededcef3232008-07-10 10:40:53 -0300709 }
710 return;
711err:
712 PDEBUG(D_ERR, "i2c error gain");
713}
714
715static void setgain(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300716{
717 struct sd *sd = (struct sd *) gspca_dev;
718 __u8 gain;
Hans de Goede4efcfa02010-02-01 07:48:17 -0300719 __u8 buf[2] = { 0, 0 };
720
721 if (sensor_data[sd->sensor].flags & F_GAIN) {
722 /* Use the sensor gain to do the actual gain */
723 setsensorgain(gspca_dev);
724 return;
725 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300726
Hans de Goedead5ef80d2008-07-14 10:11:42 -0300727 gain = sd->gain >> 4;
Hans de Goededcef3232008-07-10 10:40:53 -0300728
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300729 /* red and blue gain */
Hans de Goede4efcfa02010-02-01 07:48:17 -0300730 buf[0] = gain << 4 | gain;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300731 /* green gain */
Hans de Goede4efcfa02010-02-01 07:48:17 -0300732 buf[1] = gain;
733 reg_w(gspca_dev, 0x10, buf, 2);
Hans de Goededcef3232008-07-10 10:40:53 -0300734}
735
736static void setexposure(struct gspca_dev *gspca_dev)
737{
738 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goededcef3232008-07-10 10:40:53 -0300739
740 switch (sd->sensor) {
Hans de Goedeb10af3f2010-01-10 19:31:34 -0300741 case SENSOR_TAS5110C:
742 case SENSOR_TAS5110D: {
Hans de Goededcef3232008-07-10 10:40:53 -0300743 /* register 19's high nibble contains the sn9c10x clock divider
744 The high nibble configures the no fps according to the
745 formula: 60 / high_nibble. With a maximum of 30 fps */
Hans de Goede26984b02010-02-01 13:18:37 -0300746 __u8 reg = sd->exposure;
Hans de Goededcef3232008-07-10 10:40:53 -0300747 reg = (reg << 4) | 0x0b;
Jean-Francois Moine739570b2008-07-14 09:38:29 -0300748 reg_w(gspca_dev, 0x19, &reg, 1);
Andoni Zubimendi51fc8e32008-07-10 11:12:24 -0300749 break;
750 }
Hans de Goedea975a522008-07-16 15:29:11 -0300751 case SENSOR_OV6650:
Hans de Goede6af492e2008-07-22 07:09:33 -0300752 case SENSOR_OV7630: {
Hans de Goedea975a522008-07-16 15:29:11 -0300753 /* The ov6650 / ov7630 have 2 registers which both influence
754 exposure, register 11, whose low nibble sets the nr off fps
Hans de Goedef4d52022008-07-15 09:36:42 -0300755 according to: fps = 30 / (low_nibble + 1)
756
757 The fps configures the maximum exposure setting, but it is
758 possible to use less exposure then what the fps maximum
759 allows by setting register 10. register 10 configures the
760 actual exposure as quotient of the full exposure, with 0
761 being no exposure at all (not very usefull) and reg10_max
762 being max exposure possible at that framerate.
763
764 The code maps our 0 - 510 ms exposure ctrl to these 2
765 registers, trying to keep fps as high as possible.
766 */
Hans de Goede6af492e2008-07-22 07:09:33 -0300767 __u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
768 int reg10, reg11, reg10_max;
769
Hans de Goede66f35822008-07-16 10:16:28 -0300770 /* ov6645 datasheet says reg10_max is 9a, but that uses
771 tline * 2 * reg10 as formula for calculating texpo, the
772 ov6650 probably uses the same formula as the 7730 which uses
773 tline * 4 * reg10, which explains why the reg10max we've
774 found experimentally for the ov6650 is exactly half that of
Hans de Goedea975a522008-07-16 15:29:11 -0300775 the ov6645. The ov7630 datasheet says the max is 0x41. */
Hans de Goede6af492e2008-07-22 07:09:33 -0300776 if (sd->sensor == SENSOR_OV6650) {
777 reg10_max = 0x4d;
778 i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
779 } else
780 reg10_max = 0x41;
Hans de Goedef4d52022008-07-15 09:36:42 -0300781
Hans de Goede26984b02010-02-01 13:18:37 -0300782 reg11 = (30 * sd->exposure + 999) / 1000;
Hans de Goedef4d52022008-07-15 09:36:42 -0300783 if (reg11 < 1)
784 reg11 = 1;
785 else if (reg11 > 16)
786 reg11 = 16;
787
Hans de Goedee2ad2a52008-09-03 17:12:15 -0300788 /* In 640x480, if the reg11 has less than 3, the image is
789 unstable (not enough bandwidth). */
790 if (gspca_dev->width == 640 && reg11 < 3)
791 reg11 = 3;
792
Hans de Goedef4d52022008-07-15 09:36:42 -0300793 /* frame exposure time in ms = 1000 * reg11 / 30 ->
Hans de Goede26984b02010-02-01 13:18:37 -0300794 reg10 = sd->exposure * reg10_max / (1000 * reg11 / 30) */
795 reg10 = (sd->exposure * 30 * reg10_max) / (1000 * reg11);
Hans de Goedea975a522008-07-16 15:29:11 -0300796
797 /* Don't allow this to get below 10 when using autogain, the
798 steps become very large (relatively) when below 10 causing
799 the image to oscilate from much too dark, to much too bright
800 and back again. */
801 if (sd->autogain && reg10 < 10)
802 reg10 = 10;
Hans de Goedef4d52022008-07-15 09:36:42 -0300803 else if (reg10 > reg10_max)
804 reg10 = reg10_max;
805
806 /* Write reg 10 and reg11 low nibble */
Hans de Goedef45f06b2008-09-03 17:12:21 -0300807 i2c[1] = sensor_data[sd->sensor].sensor_addr;
Andoni Zubimendi794af522008-07-16 08:33:14 -0300808 i2c[3] = reg10;
809 i2c[4] |= reg11 - 1;
Hans de Goede6af492e2008-07-22 07:09:33 -0300810
811 /* If register 11 didn't change, don't change it */
812 if (sd->reg11 == reg11 )
813 i2c[0] = 0xa0;
814
815 if (i2c_w(gspca_dev, i2c) == 0)
816 sd->reg11 = reg11;
817 else
Andoni Zubimendi794af522008-07-16 08:33:14 -0300818 PDEBUG(D_ERR, "i2c error exposure");
819 break;
820 }
Hans de Goededcef3232008-07-10 10:40:53 -0300821 }
822}
823
Hans de Goede66f35822008-07-16 10:16:28 -0300824static void setfreq(struct gspca_dev *gspca_dev)
825{
826 struct sd *sd = (struct sd *) gspca_dev;
827
828 switch (sd->sensor) {
Andoni Zubimendid87616f2008-07-17 05:35:52 -0300829 case SENSOR_OV6650:
Hans de Goede6af492e2008-07-22 07:09:33 -0300830 case SENSOR_OV7630: {
Hans de Goede66f35822008-07-16 10:16:28 -0300831 /* Framerate adjust register for artificial light 50 hz flicker
Hans de Goede6af492e2008-07-22 07:09:33 -0300832 compensation, for the ov6650 this is identical to ov6630
833 0x2b register, see ov6630 datasheet.
834 0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
Andoni Zubimendid87616f2008-07-17 05:35:52 -0300835 __u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
Hans de Goede66f35822008-07-16 10:16:28 -0300836 switch (sd->freq) {
837 default:
838/* case 0: * no filter*/
839/* case 2: * 60 hz */
840 i2c[3] = 0;
841 break;
842 case 1: /* 50 hz */
Hans de Goede722103e2008-07-17 10:24:47 -0300843 i2c[3] = (sd->sensor == SENSOR_OV6650)
844 ? 0x4f : 0x8a;
Hans de Goede66f35822008-07-16 10:16:28 -0300845 break;
846 }
Hans de Goedef45f06b2008-09-03 17:12:21 -0300847 i2c[1] = sensor_data[sd->sensor].sensor_addr;
Hans de Goede66f35822008-07-16 10:16:28 -0300848 if (i2c_w(gspca_dev, i2c) < 0)
849 PDEBUG(D_ERR, "i2c error setfreq");
850 break;
851 }
852 }
853}
854
Hans de Goede26984b02010-02-01 13:18:37 -0300855#include "coarse_expo_autogain.h"
856
Hans de Goededcef3232008-07-10 10:40:53 -0300857static void do_autogain(struct gspca_dev *gspca_dev)
858{
Hans de Goede26984b02010-02-01 13:18:37 -0300859 int deadzone, desired_avg_lum, result;
Hans de Goededcef3232008-07-10 10:40:53 -0300860 struct sd *sd = (struct sd *) gspca_dev;
861 int avg_lum = atomic_read(&sd->avg_lum);
862
Hans de Goede26984b02010-02-01 13:18:37 -0300863 if (avg_lum == -1 || !sd->autogain)
Hans de Goededcef3232008-07-10 10:40:53 -0300864 return;
865
Hans de Goede26984b02010-02-01 13:18:37 -0300866 if (sd->autogain_ignore_frames > 0) {
867 sd->autogain_ignore_frames--;
868 return;
869 }
870
Hans de Goede5017c7b2008-10-22 04:59:29 -0300871 /* SIF / VGA sensors have a different autoexposure area and thus
872 different avg_lum values for the same picture brightness */
873 if (sensor_data[sd->sensor].flags & F_SIF) {
Hans de Goede26984b02010-02-01 13:18:37 -0300874 deadzone = 500;
875 /* SIF sensors tend to overexpose, so keep this small */
876 desired_avg_lum = 5000;
Hans de Goede5017c7b2008-10-22 04:59:29 -0300877 } else {
Hans de Goede26984b02010-02-01 13:18:37 -0300878 deadzone = 1500;
Hans de Goede5017c7b2008-10-22 04:59:29 -0300879 desired_avg_lum = 23000;
880 }
881
Hans de Goede26984b02010-02-01 13:18:37 -0300882 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO)
883 result = gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
884 sd->brightness * desired_avg_lum / 127,
885 deadzone);
886 else
887 result = gspca_auto_gain_n_exposure(gspca_dev, avg_lum,
888 sd->brightness * desired_avg_lum / 127,
889 deadzone, GAIN_KNEE, EXPOSURE_KNEE);
890
891 if (result) {
Jean-Francois Moine1c44d812008-11-10 05:56:55 -0300892 PDEBUG(D_FRAM, "autogain: gain changed: gain: %d expo: %d",
Hans de Goedea975a522008-07-16 15:29:11 -0300893 (int)sd->gain, (int)sd->exposure);
Hans de Goededcef3232008-07-10 10:40:53 -0300894 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
Hans de Goedea975a522008-07-16 15:29:11 -0300895 }
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300896}
897
898/* this function is called at probe time */
899static int sd_config(struct gspca_dev *gspca_dev,
900 const struct usb_device_id *id)
901{
902 struct sd *sd = (struct sd *) gspca_dev;
903 struct cam *cam;
Hans de Goede65f33392008-09-03 17:12:15 -0300904
905 reg_r(gspca_dev, 0x00);
906 if (gspca_dev->usb_buf[0] != 0x10)
907 return -ENODEV;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300908
Jean-Francois Moine5da162e2008-07-26 14:17:23 -0300909 /* copy the webcam info from the device id */
Hans de Goedef45f06b2008-09-03 17:12:21 -0300910 sd->sensor = id->driver_info >> 8;
911 sd->bridge = id->driver_info & 0xff;
912 gspca_dev->ctrl_dis = sensor_data[sd->sensor].ctrl_dis;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300913
914 cam = &gspca_dev->cam;
Hans de Goedef45f06b2008-09-03 17:12:21 -0300915 if (!(sensor_data[sd->sensor].flags & F_SIF)) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300916 cam->cam_mode = vga_mode;
Andoni Zubimendi51fc8e32008-07-10 11:12:24 -0300917 cam->nmodes = ARRAY_SIZE(vga_mode);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300918 } else {
919 cam->cam_mode = sif_mode;
Andoni Zubimendi51fc8e32008-07-10 11:12:24 -0300920 cam->nmodes = ARRAY_SIZE(sif_mode);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300921 }
Jean-Francois Moine49cb6b02009-04-25 13:29:01 -0300922 cam->npkt = 36; /* 36 packets per ISOC message */
923
Hans de Goededcef3232008-07-10 10:40:53 -0300924 sd->brightness = BRIGHTNESS_DEF;
925 sd->gain = GAIN_DEF;
Hans de Goede26984b02010-02-01 13:18:37 -0300926 if (sensor_data[sd->sensor].flags & F_COARSE_EXPO) {
927 sd->exposure = COARSE_EXPOSURE_DEF;
928 gspca_dev->ctrl_dis |= (1 << EXPOSURE_IDX);
929 } else {
930 sd->exposure = EXPOSURE_DEF;
931 gspca_dev->ctrl_dis |= (1 << COARSE_EXPOSURE_IDX);
932 }
Hans de Goedee2ad2a52008-09-03 17:12:15 -0300933 if (gspca_dev->ctrl_dis & (1 << AUTOGAIN_IDX))
934 sd->autogain = 0; /* Disable do_autogain callback */
935 else
936 sd->autogain = AUTOGAIN_DEF;
Hans de Goede12ff9122008-07-17 10:30:56 -0300937 sd->freq = FREQ_DEF;
Hans de Goede6af492e2008-07-22 07:09:33 -0300938
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300939 return 0;
940}
941
Jean-Francois Moine012d6b02008-09-03 17:12:16 -0300942/* this function is called at probe and resume time */
943static int sd_init(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300944{
Hans de Goede271315a2008-09-03 17:12:19 -0300945 const __u8 stop = 0x09; /* Disable stream turn of LED */
946
947 reg_w(gspca_dev, 0x01, &stop, 1);
948
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300949 return 0;
950}
951
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300952/* -- start the camera -- */
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -0300953static int sd_start(struct gspca_dev *gspca_dev)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300954{
955 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goede93627732008-09-04 16:20:12 -0300956 struct cam *cam = &gspca_dev->cam;
Hans de Goedef45f06b2008-09-03 17:12:21 -0300957 int mode, l;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300958 const __u8 *sn9c10x;
Hans de Goede93627732008-09-04 16:20:12 -0300959 __u8 reg12_19[8];
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300960
Hans de Goede93627732008-09-04 16:20:12 -0300961 mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
Hans de Goedef45f06b2008-09-03 17:12:21 -0300962 sn9c10x = sensor_data[sd->sensor].bridge_init[sd->bridge];
963 l = sensor_data[sd->sensor].bridge_init_size[sd->bridge];
Hans de Goede93627732008-09-04 16:20:12 -0300964 memcpy(reg12_19, &sn9c10x[0x12 - 1], 8);
965 reg12_19[6] = sn9c10x[0x18 - 1] | (mode << 4);
Hans de Goedef45f06b2008-09-03 17:12:21 -0300966 /* Special cases where reg 17 and or 19 value depends on mode */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300967 switch (sd->sensor) {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300968 case SENSOR_PAS202:
Hans de Goede93627732008-09-04 16:20:12 -0300969 reg12_19[5] = mode ? 0x24 : 0x20;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300970 break;
Hans de Goedef45f06b2008-09-03 17:12:21 -0300971 case SENSOR_TAS5130CXX:
972 /* probably not mode specific at all most likely the upper
973 nibble of 0x19 is exposure (clock divider) just as with
974 the tas5110, we need someone to test this. */
Hans de Goede93627732008-09-04 16:20:12 -0300975 reg12_19[7] = mode ? 0x23 : 0x43;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300976 break;
977 }
Hans de Goedec437d652008-09-03 17:12:22 -0300978 /* Disable compression when the raw bayer format has been selected */
Hans de Goede93627732008-09-04 16:20:12 -0300979 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
980 reg12_19[6] &= ~0x80;
981
982 /* Vga mode emulation on SIF sensor? */
983 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
984 reg12_19[0] += 16; /* 0x12: hstart adjust */
985 reg12_19[1] += 24; /* 0x13: vstart adjust */
986 reg12_19[3] = 320 / 16; /* 0x15: hsize */
987 reg12_19[4] = 240 / 16; /* 0x16: vsize */
988 }
Hans de Goede6af492e2008-07-22 07:09:33 -0300989
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300990 /* reg 0x01 bit 2 video transfert on */
Hans de Goedefff42052008-07-23 07:04:39 -0300991 reg_w(gspca_dev, 0x01, &sn9c10x[0x01 - 1], 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300992 /* reg 0x17 SensorClk enable inv Clk 0x60 */
Hans de Goedefff42052008-07-23 07:04:39 -0300993 reg_w(gspca_dev, 0x17, &sn9c10x[0x17 - 1], 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -0300994 /* Set the registers from the template */
Jean-Francois Moine8295d992008-09-03 17:12:19 -0300995 reg_w(gspca_dev, 0x01, sn9c10x, l);
Hans de Goedef45f06b2008-09-03 17:12:21 -0300996
997 /* Init the sensor */
998 i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
999 sensor_data[sd->sensor].sensor_init_size);
1000 if (sensor_data[sd->sensor].sensor_bridge_init[sd->bridge])
1001 i2c_w_vector(gspca_dev,
1002 sensor_data[sd->sensor].sensor_bridge_init[sd->bridge],
1003 sensor_data[sd->sensor].sensor_bridge_init_size[
1004 sd->bridge]);
1005
Hans de Goede3647fea2008-07-15 05:36:30 -03001006 /* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
Hans de Goede93627732008-09-04 16:20:12 -03001007 reg_w(gspca_dev, 0x15, &reg12_19[3], 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001008 /* compression register */
Hans de Goede93627732008-09-04 16:20:12 -03001009 reg_w(gspca_dev, 0x18, &reg12_19[6], 1);
Andoni Zubimendi794af522008-07-16 08:33:14 -03001010 /* H_start */
Hans de Goede93627732008-09-04 16:20:12 -03001011 reg_w(gspca_dev, 0x12, &reg12_19[0], 1);
Andoni Zubimendi794af522008-07-16 08:33:14 -03001012 /* V_START */
Hans de Goede93627732008-09-04 16:20:12 -03001013 reg_w(gspca_dev, 0x13, &reg12_19[1], 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001014 /* reset 0x17 SensorClk enable inv Clk 0x60 */
1015 /*fixme: ov7630 [17]=68 8f (+20 if 102)*/
Hans de Goede93627732008-09-04 16:20:12 -03001016 reg_w(gspca_dev, 0x17, &reg12_19[5], 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001017 /*MCKSIZE ->3 */ /*fixme: not ov7630*/
Hans de Goede93627732008-09-04 16:20:12 -03001018 reg_w(gspca_dev, 0x19, &reg12_19[7], 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001019 /* AE_STRX AE_STRY AE_ENDX AE_ENDY */
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001020 reg_w(gspca_dev, 0x1c, &sn9c10x[0x1c - 1], 4);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001021 /* Enable video transfert */
Jean-Francois Moine739570b2008-07-14 09:38:29 -03001022 reg_w(gspca_dev, 0x01, &sn9c10x[0], 1);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001023 /* Compression */
Hans de Goede93627732008-09-04 16:20:12 -03001024 reg_w(gspca_dev, 0x18, &reg12_19[6], 2);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001025 msleep(20);
1026
Hans de Goede6af492e2008-07-22 07:09:33 -03001027 sd->reg11 = -1;
1028
Hans de Goededcef3232008-07-10 10:40:53 -03001029 setgain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001030 setbrightness(gspca_dev);
Hans de Goededcef3232008-07-10 10:40:53 -03001031 setexposure(gspca_dev);
Hans de Goede66f35822008-07-16 10:16:28 -03001032 setfreq(gspca_dev);
Hans de Goededcef3232008-07-10 10:40:53 -03001033
Hans de Goede6af492e2008-07-22 07:09:33 -03001034 sd->frames_to_drop = 0;
Hans de Goededcef3232008-07-10 10:40:53 -03001035 sd->autogain_ignore_frames = 0;
Hans de Goede26984b02010-02-01 13:18:37 -03001036 sd->exp_too_high_cnt = 0;
1037 sd->exp_too_low_cnt = 0;
Hans de Goededcef3232008-07-10 10:40:53 -03001038 atomic_set(&sd->avg_lum, -1);
Jean-Francois Moine72ab97c2008-09-20 06:39:08 -03001039 return 0;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001040}
1041
1042static void sd_stopN(struct gspca_dev *gspca_dev)
1043{
Hans de Goedef45f06b2008-09-03 17:12:21 -03001044 sd_init(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001045}
1046
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001047static void sd_pkt_scan(struct gspca_dev *gspca_dev,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001048 u8 *data, /* isoc packet */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001049 int len) /* iso packet length */
1050{
Hans de Goede0d2a7222008-07-03 08:15:22 -03001051 int i;
Hans de Goededcef3232008-07-10 10:40:53 -03001052 struct sd *sd = (struct sd *) gspca_dev;
Hans de Goedec437d652008-09-03 17:12:22 -03001053 struct cam *cam = &gspca_dev->cam;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001054
Hans de Goedec36260ee2008-07-16 09:56:07 -03001055 /* frames start with:
1056 * ff ff 00 c4 c4 96 synchro
1057 * 00 (unknown)
1058 * xx (frame sequence / size / compression)
1059 * (xx) (idem - extra byte for sn9c103)
1060 * ll mm brightness sum inside auto exposure
1061 * ll mm brightness sum outside auto exposure
1062 * (xx xx xx xx xx) audio values for snc103
1063 */
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001064 if (len > 6 && len < 24) {
Hans de Goede0d2a7222008-07-03 08:15:22 -03001065 for (i = 0; i < len - 6; i++) {
1066 if (data[0 + i] == 0xff
1067 && data[1 + i] == 0xff
1068 && data[2 + i] == 0x00
1069 && data[3 + i] == 0xc4
1070 && data[4 + i] == 0xc4
1071 && data[5 + i] == 0x96) { /* start of frame */
Hans de Goede6af492e2008-07-22 07:09:33 -03001072 int lum = -1;
1073 int pkt_type = LAST_PACKET;
Hans de Goedef45f06b2008-09-03 17:12:21 -03001074 int fr_h_sz = (sd->bridge == BRIDGE_103) ?
1075 18 : 12;
Hans de Goede6af492e2008-07-22 07:09:33 -03001076
Hans de Goedef45f06b2008-09-03 17:12:21 -03001077 if (len - i < fr_h_sz) {
Hans de Goedec36260ee2008-07-16 09:56:07 -03001078 PDEBUG(D_STREAM, "packet too short to"
1079 " get avg brightness");
Hans de Goedef45f06b2008-09-03 17:12:21 -03001080 } else if (sd->bridge == BRIDGE_103) {
Hans de Goede6af492e2008-07-22 07:09:33 -03001081 lum = data[i + 9] +
1082 (data[i + 10] << 8);
Hans de Goedef45f06b2008-09-03 17:12:21 -03001083 } else {
1084 lum = data[i + 8] + (data[i + 9] << 8);
Hans de Goededcef3232008-07-10 10:40:53 -03001085 }
Hans de Goedebf2a2202008-09-04 16:22:56 -03001086 /* When exposure changes midway a frame we
1087 get a lum of 0 in this case drop 2 frames
1088 as the frames directly after an exposure
1089 change have an unstable image. Sometimes lum
1090 *really* is 0 (cam used in low light with
1091 low exposure setting), so do not drop frames
1092 if the previous lum was 0 too. */
1093 if (lum == 0 && sd->prev_avg_lum != 0) {
Hans de Goede6af492e2008-07-22 07:09:33 -03001094 lum = -1;
1095 sd->frames_to_drop = 2;
Hans de Goedebf2a2202008-09-04 16:22:56 -03001096 sd->prev_avg_lum = 0;
1097 } else
1098 sd->prev_avg_lum = lum;
Hans de Goede6af492e2008-07-22 07:09:33 -03001099 atomic_set(&sd->avg_lum, lum);
1100
1101 if (sd->frames_to_drop) {
1102 sd->frames_to_drop--;
1103 pkt_type = DISCARD_PACKET;
1104 }
1105
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001106 gspca_frame_add(gspca_dev, pkt_type,
1107 NULL, 0);
Hans de Goedef45f06b2008-09-03 17:12:21 -03001108 data += i + fr_h_sz;
1109 len -= i + fr_h_sz;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001110 gspca_frame_add(gspca_dev, FIRST_PACKET,
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001111 data, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001112 return;
1113 }
1114 }
1115 }
Hans de Goedec437d652008-09-03 17:12:22 -03001116
1117 if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1118 /* In raw mode we sometimes get some garbage after the frame
1119 ignore this */
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001120 struct gspca_frame *frame;
1121 int used;
Hans de Goedec437d652008-09-03 17:12:22 -03001122 int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1123
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001124 frame = gspca_get_i_frame(gspca_dev);
1125 if (frame == NULL) {
1126 gspca_dev->last_packet_type = DISCARD_PACKET;
1127 return;
1128 }
1129 used = frame->data_end - frame->data;
Hans de Goedec437d652008-09-03 17:12:22 -03001130 if (used + len > size)
1131 len = size - used;
1132 }
1133
Jean-Francois Moine76dd2722009-11-13 09:21:03 -03001134 gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001135}
1136
1137static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
1138{
1139 struct sd *sd = (struct sd *) gspca_dev;
1140
1141 sd->brightness = val;
1142 if (gspca_dev->streaming)
1143 setbrightness(gspca_dev);
1144 return 0;
1145}
1146
1147static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
1148{
1149 struct sd *sd = (struct sd *) gspca_dev;
1150
1151 *val = sd->brightness;
1152 return 0;
1153}
1154
Hans de Goededcef3232008-07-10 10:40:53 -03001155static int sd_setgain(struct gspca_dev *gspca_dev, __s32 val)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001156{
1157 struct sd *sd = (struct sd *) gspca_dev;
1158
Hans de Goededcef3232008-07-10 10:40:53 -03001159 sd->gain = val;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001160 if (gspca_dev->streaming)
Hans de Goededcef3232008-07-10 10:40:53 -03001161 setgain(gspca_dev);
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001162 return 0;
1163}
1164
Hans de Goededcef3232008-07-10 10:40:53 -03001165static int sd_getgain(struct gspca_dev *gspca_dev, __s32 *val)
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001166{
1167 struct sd *sd = (struct sd *) gspca_dev;
1168
Hans de Goededcef3232008-07-10 10:40:53 -03001169 *val = sd->gain;
1170 return 0;
1171}
1172
1173static int sd_setexposure(struct gspca_dev *gspca_dev, __s32 val)
1174{
1175 struct sd *sd = (struct sd *) gspca_dev;
1176
1177 sd->exposure = val;
1178 if (gspca_dev->streaming)
1179 setexposure(gspca_dev);
1180 return 0;
1181}
1182
1183static int sd_getexposure(struct gspca_dev *gspca_dev, __s32 *val)
1184{
1185 struct sd *sd = (struct sd *) gspca_dev;
1186
1187 *val = sd->exposure;
1188 return 0;
1189}
1190
1191static int sd_setautogain(struct gspca_dev *gspca_dev, __s32 val)
1192{
1193 struct sd *sd = (struct sd *) gspca_dev;
1194
1195 sd->autogain = val;
Hans de Goede26984b02010-02-01 13:18:37 -03001196 sd->exp_too_high_cnt = 0;
1197 sd->exp_too_low_cnt = 0;
1198
Hans de Goededcef3232008-07-10 10:40:53 -03001199 /* when switching to autogain set defaults to make sure
1200 we are on a valid point of the autogain gain /
1201 exposure knee graph, and give this change time to
1202 take effect before doing autogain. */
Hans de Goede26984b02010-02-01 13:18:37 -03001203 if (sd->autogain && !(sensor_data[sd->sensor].flags & F_COARSE_EXPO)) {
Hans de Goededcef3232008-07-10 10:40:53 -03001204 sd->exposure = EXPOSURE_DEF;
1205 sd->gain = GAIN_DEF;
1206 if (gspca_dev->streaming) {
1207 sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
1208 setexposure(gspca_dev);
1209 setgain(gspca_dev);
1210 }
1211 }
1212
1213 return 0;
1214}
1215
1216static int sd_getautogain(struct gspca_dev *gspca_dev, __s32 *val)
1217{
1218 struct sd *sd = (struct sd *) gspca_dev;
1219
1220 *val = sd->autogain;
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001221 return 0;
1222}
1223
Hans de Goede66f35822008-07-16 10:16:28 -03001224static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
1225{
1226 struct sd *sd = (struct sd *) gspca_dev;
1227
1228 sd->freq = val;
1229 if (gspca_dev->streaming)
1230 setfreq(gspca_dev);
1231 return 0;
1232}
1233
1234static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
1235{
1236 struct sd *sd = (struct sd *) gspca_dev;
1237
1238 *val = sd->freq;
1239 return 0;
1240}
1241
1242static int sd_querymenu(struct gspca_dev *gspca_dev,
1243 struct v4l2_querymenu *menu)
1244{
1245 switch (menu->id) {
1246 case V4L2_CID_POWER_LINE_FREQUENCY:
1247 switch (menu->index) {
1248 case 0: /* V4L2_CID_POWER_LINE_FREQUENCY_DISABLED */
1249 strcpy((char *) menu->name, "NoFliker");
1250 return 0;
1251 case 1: /* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
1252 strcpy((char *) menu->name, "50 Hz");
1253 return 0;
1254 case 2: /* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
1255 strcpy((char *) menu->name, "60 Hz");
1256 return 0;
1257 }
1258 break;
1259 }
1260 return -EINVAL;
1261}
1262
Hans de Goedef65e93d2010-01-31 10:35:15 -03001263#ifdef CONFIG_INPUT
1264static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1265 u8 *data, /* interrupt packet data */
1266 int len) /* interrupt packet length */
1267{
1268 int ret = -EINVAL;
1269
1270 if (len == 1 && data[0] == 1) {
1271 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1272 input_sync(gspca_dev->input_dev);
1273 input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1274 input_sync(gspca_dev->input_dev);
1275 ret = 0;
1276 }
1277
1278 return ret;
1279}
1280#endif
1281
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001282/* sub-driver description */
Hans de Goededcef3232008-07-10 10:40:53 -03001283static const struct sd_desc sd_desc = {
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001284 .name = MODULE_NAME,
1285 .ctrls = sd_ctrls,
1286 .nctrls = ARRAY_SIZE(sd_ctrls),
1287 .config = sd_config,
Jean-Francois Moine012d6b02008-09-03 17:12:16 -03001288 .init = sd_init,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001289 .start = sd_start,
1290 .stopN = sd_stopN,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001291 .pkt_scan = sd_pkt_scan,
Hans de Goede66f35822008-07-16 10:16:28 -03001292 .querymenu = sd_querymenu,
Hans de Goedee2ad2a52008-09-03 17:12:15 -03001293 .dq_callback = do_autogain,
Hans de Goedef65e93d2010-01-31 10:35:15 -03001294#ifdef CONFIG_INPUT
1295 .int_pkt_scan = sd_int_pkt_scan,
1296#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001297};
1298
1299/* -- module initialisation -- */
Hans de Goedef45f06b2008-09-03 17:12:21 -03001300#define SB(sensor, bridge) \
1301 .driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1302
Hans de Goedee2ad2a52008-09-03 17:12:15 -03001303
Márton Németh37b372e2009-12-10 11:31:09 -03001304static const struct usb_device_id device_table[] __devinitconst = {
Hans de Goedeb10af3f2010-01-10 19:31:34 -03001305 {USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1306 {USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
Hans de Goede222a07f2008-09-03 17:12:20 -03001307#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
Hans de Goedeb10af3f2010-01-10 19:31:34 -03001308 {USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
Hans de Goedef45f06b2008-09-03 17:12:21 -03001309 {USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1310 {USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
Hans de Goede5de39b22008-07-17 10:34:28 -03001311#endif
Hans de Goedef45f06b2008-09-03 17:12:21 -03001312 {USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
Hans de Goede222a07f2008-09-03 17:12:20 -03001313#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
Hans de Goedef45f06b2008-09-03 17:12:21 -03001314 {USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1315 {USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1316 {USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1317 {USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1318 {USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
Hans de Goede222a07f2008-09-03 17:12:20 -03001319#endif
Jean-Francois Moine29fbdf32008-11-07 04:53:28 -03001320 {USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
Hans de Goedef45f06b2008-09-03 17:12:21 -03001321 {USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
Hans de Goede222a07f2008-09-03 17:12:20 -03001322#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
Hans de Goedef45f06b2008-09-03 17:12:21 -03001323 {USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
Jean-Francois Moinec41492c2008-07-07 08:31:16 -03001324#endif
Hans de Goede4cce1652008-09-04 16:22:57 -03001325 {USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1326#if !defined CONFIG_USB_SN9C102 && !defined CONFIG_USB_SN9C102_MODULE
1327 {USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1328#endif
1329 {USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001330 {}
1331};
1332MODULE_DEVICE_TABLE(usb, device_table);
1333
1334/* -- device connect -- */
Márton Németh37b372e2009-12-10 11:31:09 -03001335static int __devinit sd_probe(struct usb_interface *intf,
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001336 const struct usb_device_id *id)
1337{
1338 return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1339 THIS_MODULE);
1340}
1341
1342static struct usb_driver sd_driver = {
1343 .name = MODULE_NAME,
1344 .id_table = device_table,
1345 .probe = sd_probe,
1346 .disconnect = gspca_disconnect,
Jean-Francois Moine6a709742008-09-03 16:48:10 -03001347#ifdef CONFIG_PM
1348 .suspend = gspca_suspend,
1349 .resume = gspca_resume,
1350#endif
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001351};
1352
1353/* -- module insert / remove -- */
1354static int __init sd_mod_init(void)
1355{
Alexey Klimovf69e9522009-01-01 13:02:07 -03001356 int ret;
1357 ret = usb_register(&sd_driver);
1358 if (ret < 0)
Alexey Klimove6b14842009-01-01 13:04:58 -03001359 return ret;
Jean-Francois Moine10b0e962008-07-22 05:35:10 -03001360 PDEBUG(D_PROBE, "registered");
Jean-Francois Moine6a7eba22008-06-30 15:50:11 -03001361 return 0;
1362}
1363static void __exit sd_mod_exit(void)
1364{
1365 usb_deregister(&sd_driver);
1366 PDEBUG(D_PROBE, "deregistered");
1367}
1368
1369module_init(sd_mod_init);
1370module_exit(sd_mod_exit);