xref: /OK3568_Linux_fs/kernel/drivers/media/usb/gspca/sonixb.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  *		sonix sn9c102 (bayer) library
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2009-2011 Jean-François Moine <http://moinejf.free.fr>
6*4882a593Smuzhiyun  * Copyright (C) 2003 2004 Michel Xhaard mxhaard@magic.fr
7*4882a593Smuzhiyun  * Add Pas106 Stefano Mozzi (C) 2004
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun /* Some documentation on known sonixb registers:
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun Reg	Use
13*4882a593Smuzhiyun sn9c101 / sn9c102:
14*4882a593Smuzhiyun 0x10	high nibble red gain low nibble blue gain
15*4882a593Smuzhiyun 0x11	low nibble green gain
16*4882a593Smuzhiyun sn9c103:
17*4882a593Smuzhiyun 0x05	red gain 0-127
18*4882a593Smuzhiyun 0x06	blue gain 0-127
19*4882a593Smuzhiyun 0x07	green gain 0-127
20*4882a593Smuzhiyun all:
21*4882a593Smuzhiyun 0x08-0x0f i2c / 3wire registers
22*4882a593Smuzhiyun 0x12	hstart
23*4882a593Smuzhiyun 0x13	vstart
24*4882a593Smuzhiyun 0x15	hsize (hsize = register-value * 16)
25*4882a593Smuzhiyun 0x16	vsize (vsize = register-value * 16)
26*4882a593Smuzhiyun 0x17	bit 0 toggle compression quality (according to sn9c102 driver)
27*4882a593Smuzhiyun 0x18	bit 7 enables compression, bit 4-5 set image down scaling:
28*4882a593Smuzhiyun 	00 scale 1, 01 scale 1/2, 10, scale 1/4
29*4882a593Smuzhiyun 0x19	high-nibble is sensor clock divider, changes exposure on sensors which
30*4882a593Smuzhiyun 	use a clock generated by the bridge. Some sensors have their own clock.
31*4882a593Smuzhiyun 0x1c	auto_exposure area (for avg_lum) startx (startx = register-value * 32)
32*4882a593Smuzhiyun 0x1d	auto_exposure area (for avg_lum) starty (starty = register-value * 32)
33*4882a593Smuzhiyun 0x1e	auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32)
34*4882a593Smuzhiyun 0x1f	auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)
35*4882a593Smuzhiyun */
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #define MODULE_NAME "sonixb"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include <linux/input.h>
40*4882a593Smuzhiyun #include "gspca.h"
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
43*4882a593Smuzhiyun MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
44*4882a593Smuzhiyun MODULE_LICENSE("GPL");
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /* specific webcam descriptor */
47*4882a593Smuzhiyun struct sd {
48*4882a593Smuzhiyun 	struct gspca_dev gspca_dev;	/* !! must be the first item */
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	struct v4l2_ctrl *brightness;
51*4882a593Smuzhiyun 	struct v4l2_ctrl *plfreq;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	atomic_t avg_lum;
54*4882a593Smuzhiyun 	int prev_avg_lum;
55*4882a593Smuzhiyun 	int exposure_knee;
56*4882a593Smuzhiyun 	int header_read;
57*4882a593Smuzhiyun 	u8 header[12]; /* Header without sof marker */
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	unsigned char autogain_ignore_frames;
60*4882a593Smuzhiyun 	unsigned char frames_to_drop;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	__u8 bridge;			/* Type of bridge */
63*4882a593Smuzhiyun #define BRIDGE_101 0
64*4882a593Smuzhiyun #define BRIDGE_102 0 /* We make no difference between 101 and 102 */
65*4882a593Smuzhiyun #define BRIDGE_103 1
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	__u8 sensor;			/* Type of image sensor chip */
68*4882a593Smuzhiyun #define SENSOR_HV7131D 0
69*4882a593Smuzhiyun #define SENSOR_HV7131R 1
70*4882a593Smuzhiyun #define SENSOR_OV6650 2
71*4882a593Smuzhiyun #define SENSOR_OV7630 3
72*4882a593Smuzhiyun #define SENSOR_PAS106 4
73*4882a593Smuzhiyun #define SENSOR_PAS202 5
74*4882a593Smuzhiyun #define SENSOR_TAS5110C 6
75*4882a593Smuzhiyun #define SENSOR_TAS5110D 7
76*4882a593Smuzhiyun #define SENSOR_TAS5130CXX 8
77*4882a593Smuzhiyun 	__u8 reg11;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun typedef const __u8 sensor_init_t[8];
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun struct sensor_data {
83*4882a593Smuzhiyun 	const __u8 *bridge_init;
84*4882a593Smuzhiyun 	sensor_init_t *sensor_init;
85*4882a593Smuzhiyun 	int sensor_init_size;
86*4882a593Smuzhiyun 	int flags;
87*4882a593Smuzhiyun 	__u8 sensor_addr;
88*4882a593Smuzhiyun };
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun /* sensor_data flags */
91*4882a593Smuzhiyun #define F_SIF		0x01	/* sif or vga */
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun /* priv field of struct v4l2_pix_format flags (do not use low nibble!) */
94*4882a593Smuzhiyun #define MODE_RAW 0x10		/* raw bayer mode */
95*4882a593Smuzhiyun #define MODE_REDUCED_SIF 0x20	/* vga mode (320x240 / 160x120) on sif cam */
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define COMP 0xc7		/* 0x87 //0x07 */
98*4882a593Smuzhiyun #define COMP1 0xc9		/* 0x89 //0x09 */
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun #define MCK_INIT 0x63
101*4882a593Smuzhiyun #define MCK_INIT1 0x20		/*fixme: Bayer - 0x50 for JPEG ??*/
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun #define SYS_CLK 0x04
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #define SENS(bridge, sensor, _flags, _sensor_addr) \
106*4882a593Smuzhiyun { \
107*4882a593Smuzhiyun 	.bridge_init = bridge, \
108*4882a593Smuzhiyun 	.sensor_init = sensor, \
109*4882a593Smuzhiyun 	.sensor_init_size = sizeof(sensor), \
110*4882a593Smuzhiyun 	.flags = _flags, .sensor_addr = _sensor_addr \
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun /* We calculate the autogain at the end of the transfer of a frame, at this
114*4882a593Smuzhiyun    moment a frame with the old settings is being captured and transmitted. So
115*4882a593Smuzhiyun    if we adjust the gain or exposure we must ignore at least the next frame for
116*4882a593Smuzhiyun    the new settings to come into effect before doing any other adjustments. */
117*4882a593Smuzhiyun #define AUTOGAIN_IGNORE_FRAMES 1
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun static const struct v4l2_pix_format vga_mode[] = {
120*4882a593Smuzhiyun 	{160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
121*4882a593Smuzhiyun 		.bytesperline = 160,
122*4882a593Smuzhiyun 		.sizeimage = 160 * 120,
123*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
124*4882a593Smuzhiyun 		.priv = 2 | MODE_RAW},
125*4882a593Smuzhiyun 	{160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
126*4882a593Smuzhiyun 		.bytesperline = 160,
127*4882a593Smuzhiyun 		.sizeimage = 160 * 120 * 5 / 4,
128*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
129*4882a593Smuzhiyun 		.priv = 2},
130*4882a593Smuzhiyun 	{320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
131*4882a593Smuzhiyun 		.bytesperline = 320,
132*4882a593Smuzhiyun 		.sizeimage = 320 * 240 * 5 / 4,
133*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
134*4882a593Smuzhiyun 		.priv = 1},
135*4882a593Smuzhiyun 	{640, 480, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
136*4882a593Smuzhiyun 		.bytesperline = 640,
137*4882a593Smuzhiyun 		.sizeimage = 640 * 480 * 5 / 4,
138*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
139*4882a593Smuzhiyun 		.priv = 0},
140*4882a593Smuzhiyun };
141*4882a593Smuzhiyun static const struct v4l2_pix_format sif_mode[] = {
142*4882a593Smuzhiyun 	{160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
143*4882a593Smuzhiyun 		.bytesperline = 160,
144*4882a593Smuzhiyun 		.sizeimage = 160 * 120,
145*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
146*4882a593Smuzhiyun 		.priv = 1 | MODE_RAW | MODE_REDUCED_SIF},
147*4882a593Smuzhiyun 	{160, 120, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
148*4882a593Smuzhiyun 		.bytesperline = 160,
149*4882a593Smuzhiyun 		.sizeimage = 160 * 120 * 5 / 4,
150*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
151*4882a593Smuzhiyun 		.priv = 1 | MODE_REDUCED_SIF},
152*4882a593Smuzhiyun 	{176, 144, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
153*4882a593Smuzhiyun 		.bytesperline = 176,
154*4882a593Smuzhiyun 		.sizeimage = 176 * 144,
155*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
156*4882a593Smuzhiyun 		.priv = 1 | MODE_RAW},
157*4882a593Smuzhiyun 	{176, 144, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
158*4882a593Smuzhiyun 		.bytesperline = 176,
159*4882a593Smuzhiyun 		.sizeimage = 176 * 144 * 5 / 4,
160*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
161*4882a593Smuzhiyun 		.priv = 1},
162*4882a593Smuzhiyun 	{320, 240, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
163*4882a593Smuzhiyun 		.bytesperline = 320,
164*4882a593Smuzhiyun 		.sizeimage = 320 * 240 * 5 / 4,
165*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
166*4882a593Smuzhiyun 		.priv = 0 | MODE_REDUCED_SIF},
167*4882a593Smuzhiyun 	{352, 288, V4L2_PIX_FMT_SN9C10X, V4L2_FIELD_NONE,
168*4882a593Smuzhiyun 		.bytesperline = 352,
169*4882a593Smuzhiyun 		.sizeimage = 352 * 288 * 5 / 4,
170*4882a593Smuzhiyun 		.colorspace = V4L2_COLORSPACE_SRGB,
171*4882a593Smuzhiyun 		.priv = 0},
172*4882a593Smuzhiyun };
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun static const __u8 initHv7131d[] = {
175*4882a593Smuzhiyun 	0x04, 0x03, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
176*4882a593Smuzhiyun 	0x00, 0x00,
177*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x02, 0x02, 0x00,
178*4882a593Smuzhiyun 	0x28, 0x1e, 0x60, 0x8e, 0x42,
179*4882a593Smuzhiyun };
180*4882a593Smuzhiyun static const __u8 hv7131d_sensor_init[][8] = {
181*4882a593Smuzhiyun 	{0xa0, 0x11, 0x01, 0x04, 0x00, 0x00, 0x00, 0x17},
182*4882a593Smuzhiyun 	{0xa0, 0x11, 0x02, 0x00, 0x00, 0x00, 0x00, 0x17},
183*4882a593Smuzhiyun 	{0xa0, 0x11, 0x28, 0x00, 0x00, 0x00, 0x00, 0x17},
184*4882a593Smuzhiyun 	{0xa0, 0x11, 0x30, 0x30, 0x00, 0x00, 0x00, 0x17}, /* reset level */
185*4882a593Smuzhiyun 	{0xa0, 0x11, 0x34, 0x02, 0x00, 0x00, 0x00, 0x17}, /* pixel bias volt */
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun static const __u8 initHv7131r[] = {
189*4882a593Smuzhiyun 	0x46, 0x77, 0x00, 0x04, 0x00, 0x00, 0x00, 0x80, 0x11, 0x00, 0x00, 0x00,
190*4882a593Smuzhiyun 	0x00, 0x00,
191*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x02, 0x01, 0x00,
192*4882a593Smuzhiyun 	0x28, 0x1e, 0x60, 0x8a, 0x20,
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun static const __u8 hv7131r_sensor_init[][8] = {
195*4882a593Smuzhiyun 	{0xc0, 0x11, 0x31, 0x38, 0x2a, 0x2e, 0x00, 0x10},
196*4882a593Smuzhiyun 	{0xa0, 0x11, 0x01, 0x08, 0x2a, 0x2e, 0x00, 0x10},
197*4882a593Smuzhiyun 	{0xb0, 0x11, 0x20, 0x00, 0xd0, 0x2e, 0x00, 0x10},
198*4882a593Smuzhiyun 	{0xc0, 0x11, 0x25, 0x03, 0x0e, 0x28, 0x00, 0x16},
199*4882a593Smuzhiyun 	{0xa0, 0x11, 0x30, 0x10, 0x0e, 0x28, 0x00, 0x15},
200*4882a593Smuzhiyun };
201*4882a593Smuzhiyun static const __u8 initOv6650[] = {
202*4882a593Smuzhiyun 	0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
203*4882a593Smuzhiyun 	0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204*4882a593Smuzhiyun 	0x00, 0x01, 0x01, 0x0a, 0x16, 0x12, 0x68, 0x8b,
205*4882a593Smuzhiyun 	0x10,
206*4882a593Smuzhiyun };
207*4882a593Smuzhiyun static const __u8 ov6650_sensor_init[][8] = {
208*4882a593Smuzhiyun 	/* Bright, contrast, etc are set through SCBB interface.
209*4882a593Smuzhiyun 	 * AVCAP on win2 do not send any data on this controls. */
210*4882a593Smuzhiyun 	/* Anyway, some registers appears to alter bright and constrat */
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	/* Reset sensor */
213*4882a593Smuzhiyun 	{0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
214*4882a593Smuzhiyun 	/* Set clock register 0x11 low nibble is clock divider */
215*4882a593Smuzhiyun 	{0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10},
216*4882a593Smuzhiyun 	/* Next some unknown stuff */
217*4882a593Smuzhiyun 	{0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10},
218*4882a593Smuzhiyun /*	{0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10},
219*4882a593Smuzhiyun 		 * THIS SET GREEN SCREEN
220*4882a593Smuzhiyun 		 * (pixels could be innverted in decode kind of "brg",
221*4882a593Smuzhiyun 		 * but blue wont be there. Avoid this data ... */
222*4882a593Smuzhiyun 	{0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
223*4882a593Smuzhiyun 	{0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
224*4882a593Smuzhiyun 	{0xa0, 0x60, 0x30, 0x3d, 0x0a, 0xd8, 0xa4, 0x10},
225*4882a593Smuzhiyun 	/* Enable rgb brightness control */
226*4882a593Smuzhiyun 	{0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10},
227*4882a593Smuzhiyun 	/* HDG: Note windows uses the line below, which sets both register 0x60
228*4882a593Smuzhiyun 	   and 0x61 I believe these registers of the ov6650 are identical as
229*4882a593Smuzhiyun 	   those of the ov7630, because if this is true the windows settings
230*4882a593Smuzhiyun 	   add a bit additional red gain and a lot additional blue gain, which
231*4882a593Smuzhiyun 	   matches my findings that the windows settings make blue much too
232*4882a593Smuzhiyun 	   blue and red a little too red.
233*4882a593Smuzhiyun 	{0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */
234*4882a593Smuzhiyun 	/* Some more unknown stuff */
235*4882a593Smuzhiyun 	{0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
236*4882a593Smuzhiyun 	{0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
237*4882a593Smuzhiyun };
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun static const __u8 initOv7630[] = {
240*4882a593Smuzhiyun 	0x04, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,	/* r01 .. r08 */
241*4882a593Smuzhiyun 	0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	/* r09 .. r10 */
242*4882a593Smuzhiyun 	0x00, 0x01, 0x01, 0x0a,				/* r11 .. r14 */
243*4882a593Smuzhiyun 	0x28, 0x1e,			/* H & V sizes     r15 .. r16 */
244*4882a593Smuzhiyun 	0x68, 0x8f, MCK_INIT1,				/* r17 .. r19 */
245*4882a593Smuzhiyun };
246*4882a593Smuzhiyun static const __u8 ov7630_sensor_init[][8] = {
247*4882a593Smuzhiyun 	{0xa0, 0x21, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10},
248*4882a593Smuzhiyun 	{0xb0, 0x21, 0x01, 0x77, 0x3a, 0x00, 0x00, 0x10},
249*4882a593Smuzhiyun /*	{0xd0, 0x21, 0x12, 0x7c, 0x01, 0x80, 0x34, 0x10},	   jfm */
250*4882a593Smuzhiyun 	{0xd0, 0x21, 0x12, 0x5c, 0x00, 0x80, 0x34, 0x10},	/* jfm */
251*4882a593Smuzhiyun 	{0xa0, 0x21, 0x1b, 0x04, 0x00, 0x80, 0x34, 0x10},
252*4882a593Smuzhiyun 	{0xa0, 0x21, 0x20, 0x44, 0x00, 0x80, 0x34, 0x10},
253*4882a593Smuzhiyun 	{0xa0, 0x21, 0x23, 0xee, 0x00, 0x80, 0x34, 0x10},
254*4882a593Smuzhiyun 	{0xd0, 0x21, 0x26, 0xa0, 0x9a, 0xa0, 0x30, 0x10},
255*4882a593Smuzhiyun 	{0xb0, 0x21, 0x2a, 0x80, 0x00, 0xa0, 0x30, 0x10},
256*4882a593Smuzhiyun 	{0xb0, 0x21, 0x2f, 0x3d, 0x24, 0xa0, 0x30, 0x10},
257*4882a593Smuzhiyun 	{0xa0, 0x21, 0x32, 0x86, 0x24, 0xa0, 0x30, 0x10},
258*4882a593Smuzhiyun 	{0xb0, 0x21, 0x60, 0xa9, 0x4a, 0xa0, 0x30, 0x10},
259*4882a593Smuzhiyun /*	{0xb0, 0x21, 0x60, 0xa9, 0x42, 0xa0, 0x30, 0x10},	 * jfm */
260*4882a593Smuzhiyun 	{0xa0, 0x21, 0x65, 0x00, 0x42, 0xa0, 0x30, 0x10},
261*4882a593Smuzhiyun 	{0xa0, 0x21, 0x69, 0x38, 0x42, 0xa0, 0x30, 0x10},
262*4882a593Smuzhiyun 	{0xc0, 0x21, 0x6f, 0x88, 0x0b, 0x00, 0x30, 0x10},
263*4882a593Smuzhiyun 	{0xc0, 0x21, 0x74, 0x21, 0x8e, 0x00, 0x30, 0x10},
264*4882a593Smuzhiyun 	{0xa0, 0x21, 0x7d, 0xf7, 0x8e, 0x00, 0x30, 0x10},
265*4882a593Smuzhiyun 	{0xd0, 0x21, 0x17, 0x1c, 0xbd, 0x06, 0xf6, 0x10},
266*4882a593Smuzhiyun };
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun static const __u8 initPas106[] = {
269*4882a593Smuzhiyun 	0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81, 0x40, 0x00, 0x00, 0x00,
270*4882a593Smuzhiyun 	0x00, 0x00,
271*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x04, 0x01, 0x00,
272*4882a593Smuzhiyun 	0x16, 0x12, 0x24, COMP1, MCK_INIT1,
273*4882a593Smuzhiyun };
274*4882a593Smuzhiyun /* compression 0x86 mckinit1 0x2b */
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun /* "Known" PAS106B registers:
277*4882a593Smuzhiyun   0x02 clock divider
278*4882a593Smuzhiyun   0x03 Variable framerate bits 4-11
279*4882a593Smuzhiyun   0x04 Var framerate bits 0-3, one must leave the 4 msb's at 0 !!
280*4882a593Smuzhiyun        The variable framerate control must never be set lower then 300,
281*4882a593Smuzhiyun        which sets the framerate at 90 / reg02, otherwise vsync is lost.
282*4882a593Smuzhiyun   0x05 Shutter Time Line Offset, this can be used as an exposure control:
283*4882a593Smuzhiyun        0 = use full frame time, 255 = no exposure at all
284*4882a593Smuzhiyun        Note this may never be larger then "var-framerate control" / 2 - 2.
285*4882a593Smuzhiyun        When var-framerate control is < 514, no exposure is reached at the max
286*4882a593Smuzhiyun        allowed value for the framerate control value, rather then at 255.
287*4882a593Smuzhiyun   0x06 Shutter Time Pixel Offset, like reg05 this influences exposure, but
288*4882a593Smuzhiyun        only a very little bit, leave at 0xcd
289*4882a593Smuzhiyun   0x07 offset sign bit (bit0 1 > negative offset)
290*4882a593Smuzhiyun   0x08 offset
291*4882a593Smuzhiyun   0x09 Blue Gain
292*4882a593Smuzhiyun   0x0a Green1 Gain
293*4882a593Smuzhiyun   0x0b Green2 Gain
294*4882a593Smuzhiyun   0x0c Red Gain
295*4882a593Smuzhiyun   0x0e Global gain
296*4882a593Smuzhiyun   0x13 Write 1 to commit settings to sensor
297*4882a593Smuzhiyun */
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun static const __u8 pas106_sensor_init[][8] = {
300*4882a593Smuzhiyun 	/* Pixel Clock Divider 6 */
301*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x14 },
302*4882a593Smuzhiyun 	/* Frame Time MSB (also seen as 0x12) */
303*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x03, 0x13, 0x00, 0x00, 0x00, 0x14 },
304*4882a593Smuzhiyun 	/* Frame Time LSB (also seen as 0x05) */
305*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x04, 0x06, 0x00, 0x00, 0x00, 0x14 },
306*4882a593Smuzhiyun 	/* Shutter Time Line Offset (also seen as 0x6d) */
307*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x05, 0x65, 0x00, 0x00, 0x00, 0x14 },
308*4882a593Smuzhiyun 	/* Shutter Time Pixel Offset (also seen as 0xb1) */
309*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x06, 0xcd, 0x00, 0x00, 0x00, 0x14 },
310*4882a593Smuzhiyun 	/* Black Level Subtract Sign (also seen 0x00) */
311*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x07, 0xc1, 0x00, 0x00, 0x00, 0x14 },
312*4882a593Smuzhiyun 	/* Black Level Subtract Level (also seen 0x01) */
313*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
314*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x08, 0x06, 0x00, 0x00, 0x00, 0x14 },
315*4882a593Smuzhiyun 	/* Color Gain B Pixel 5 a */
316*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x09, 0x05, 0x00, 0x00, 0x00, 0x14 },
317*4882a593Smuzhiyun 	/* Color Gain G1 Pixel 1 5 */
318*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x14 },
319*4882a593Smuzhiyun 	/* Color Gain G2 Pixel 1 0 5 */
320*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x0b, 0x04, 0x00, 0x00, 0x00, 0x14 },
321*4882a593Smuzhiyun 	/* Color Gain R Pixel 3 1 */
322*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x0c, 0x05, 0x00, 0x00, 0x00, 0x14 },
323*4882a593Smuzhiyun 	/* Color GainH  Pixel */
324*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x14 },
325*4882a593Smuzhiyun 	/* Global Gain */
326*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x0e, 0x0e, 0x00, 0x00, 0x00, 0x14 },
327*4882a593Smuzhiyun 	/* Contrast */
328*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x14 },
329*4882a593Smuzhiyun 	/* H&V synchro polarity */
330*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x10, 0x06, 0x00, 0x00, 0x00, 0x14 },
331*4882a593Smuzhiyun 	/* ?default */
332*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x11, 0x06, 0x00, 0x00, 0x00, 0x14 },
333*4882a593Smuzhiyun 	/* DAC scale */
334*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x12, 0x06, 0x00, 0x00, 0x00, 0x14 },
335*4882a593Smuzhiyun 	/* ?default */
336*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x14, 0x02, 0x00, 0x00, 0x00, 0x14 },
337*4882a593Smuzhiyun 	/* Validate Settings */
338*4882a593Smuzhiyun 	{ 0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14 },
339*4882a593Smuzhiyun };
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun static const __u8 initPas202[] = {
342*4882a593Smuzhiyun 	0x44, 0x44, 0x21, 0x30, 0x00, 0x00, 0x00, 0x80, 0x40, 0x00, 0x00, 0x00,
343*4882a593Smuzhiyun 	0x00, 0x00,
344*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x06, 0x03, 0x0a,
345*4882a593Smuzhiyun 	0x28, 0x1e, 0x20, 0x89, 0x20,
346*4882a593Smuzhiyun };
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun /* "Known" PAS202BCB registers:
349*4882a593Smuzhiyun   0x02 clock divider
350*4882a593Smuzhiyun   0x04 Variable framerate bits 6-11 (*)
351*4882a593Smuzhiyun   0x05 Var framerate  bits 0-5, one must leave the 2 msb's at 0 !!
352*4882a593Smuzhiyun   0x07 Blue Gain
353*4882a593Smuzhiyun   0x08 Green Gain
354*4882a593Smuzhiyun   0x09 Red Gain
355*4882a593Smuzhiyun   0x0b offset sign bit (bit0 1 > negative offset)
356*4882a593Smuzhiyun   0x0c offset
357*4882a593Smuzhiyun   0x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too,
358*4882a593Smuzhiyun        leave at 1 otherwise we get a jump in our exposure control
359*4882a593Smuzhiyun   0x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all
360*4882a593Smuzhiyun   0x10 Master gain 0 - 31
361*4882a593Smuzhiyun   0x11 write 1 to apply changes
362*4882a593Smuzhiyun   (*) The variable framerate control must never be set lower then 500
363*4882a593Smuzhiyun       which sets the framerate at 30 / reg02, otherwise vsync is lost.
364*4882a593Smuzhiyun */
365*4882a593Smuzhiyun static const __u8 pas202_sensor_init[][8] = {
366*4882a593Smuzhiyun 	/* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like
367*4882a593Smuzhiyun 	   to set it lower, but for some reason the bridge starts missing
368*4882a593Smuzhiyun 	   vsync's then */
369*4882a593Smuzhiyun 	{0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},
370*4882a593Smuzhiyun 	{0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
371*4882a593Smuzhiyun 	{0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
372*4882a593Smuzhiyun 	{0xd0, 0x40, 0x0c, 0x00, 0x0c, 0x01, 0x32, 0x10},
373*4882a593Smuzhiyun 	{0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
374*4882a593Smuzhiyun 	{0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
375*4882a593Smuzhiyun 	{0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
376*4882a593Smuzhiyun 	{0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
377*4882a593Smuzhiyun 	{0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
378*4882a593Smuzhiyun 	{0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
379*4882a593Smuzhiyun };
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun static const __u8 initTas5110c[] = {
382*4882a593Smuzhiyun 	0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
383*4882a593Smuzhiyun 	0x00, 0x00,
384*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x45, 0x09, 0x0a,
385*4882a593Smuzhiyun 	0x16, 0x12, 0x60, 0x86, 0x2b,
386*4882a593Smuzhiyun };
387*4882a593Smuzhiyun /* Same as above, except a different hstart */
388*4882a593Smuzhiyun static const __u8 initTas5110d[] = {
389*4882a593Smuzhiyun 	0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
390*4882a593Smuzhiyun 	0x00, 0x00,
391*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x41, 0x09, 0x0a,
392*4882a593Smuzhiyun 	0x16, 0x12, 0x60, 0x86, 0x2b,
393*4882a593Smuzhiyun };
394*4882a593Smuzhiyun /* tas5110c is 3 wire, tas5110d is 2 wire (regular i2c) */
395*4882a593Smuzhiyun static const __u8 tas5110c_sensor_init[][8] = {
396*4882a593Smuzhiyun 	{0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
397*4882a593Smuzhiyun 	{0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
398*4882a593Smuzhiyun };
399*4882a593Smuzhiyun /* Known TAS5110D registers
400*4882a593Smuzhiyun  * reg02: gain, bit order reversed!! 0 == max gain, 255 == min gain
401*4882a593Smuzhiyun  * reg03: bit3: vflip, bit4: ~hflip, bit7: ~gainboost (~ == inverted)
402*4882a593Smuzhiyun  *        Note: writing reg03 seems to only work when written together with 02
403*4882a593Smuzhiyun  */
404*4882a593Smuzhiyun static const __u8 tas5110d_sensor_init[][8] = {
405*4882a593Smuzhiyun 	{0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17}, /* reset */
406*4882a593Smuzhiyun };
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun static const __u8 initTas5130[] = {
409*4882a593Smuzhiyun 	0x04, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00,
410*4882a593Smuzhiyun 	0x00, 0x00,
411*4882a593Smuzhiyun 	0x00, 0x00, 0x00, 0x68, 0x0c, 0x0a,
412*4882a593Smuzhiyun 	0x28, 0x1e, 0x60, COMP, MCK_INIT,
413*4882a593Smuzhiyun };
414*4882a593Smuzhiyun static const __u8 tas5130_sensor_init[][8] = {
415*4882a593Smuzhiyun /*	{0x30, 0x11, 0x00, 0x40, 0x47, 0x00, 0x00, 0x10},
416*4882a593Smuzhiyun 					* shutter 0x47 short exposure? */
417*4882a593Smuzhiyun 	{0x30, 0x11, 0x00, 0x40, 0x01, 0x00, 0x00, 0x10},
418*4882a593Smuzhiyun 					/* shutter 0x01 long exposure */
419*4882a593Smuzhiyun 	{0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10},
420*4882a593Smuzhiyun };
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun static const struct sensor_data sensor_data[] = {
423*4882a593Smuzhiyun 	SENS(initHv7131d, hv7131d_sensor_init, 0, 0),
424*4882a593Smuzhiyun 	SENS(initHv7131r, hv7131r_sensor_init, 0, 0),
425*4882a593Smuzhiyun 	SENS(initOv6650, ov6650_sensor_init, F_SIF, 0x60),
426*4882a593Smuzhiyun 	SENS(initOv7630, ov7630_sensor_init, 0, 0x21),
427*4882a593Smuzhiyun 	SENS(initPas106, pas106_sensor_init, F_SIF, 0),
428*4882a593Smuzhiyun 	SENS(initPas202, pas202_sensor_init, 0, 0),
429*4882a593Smuzhiyun 	SENS(initTas5110c, tas5110c_sensor_init, F_SIF, 0),
430*4882a593Smuzhiyun 	SENS(initTas5110d, tas5110d_sensor_init, F_SIF, 0),
431*4882a593Smuzhiyun 	SENS(initTas5130, tas5130_sensor_init, 0, 0),
432*4882a593Smuzhiyun };
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun /* get one byte in gspca_dev->usb_buf */
reg_r(struct gspca_dev * gspca_dev,__u16 value)435*4882a593Smuzhiyun static void reg_r(struct gspca_dev *gspca_dev,
436*4882a593Smuzhiyun 		  __u16 value)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	int res;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
441*4882a593Smuzhiyun 		return;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	res = usb_control_msg(gspca_dev->dev,
444*4882a593Smuzhiyun 			usb_rcvctrlpipe(gspca_dev->dev, 0),
445*4882a593Smuzhiyun 			0,			/* request */
446*4882a593Smuzhiyun 			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
447*4882a593Smuzhiyun 			value,
448*4882a593Smuzhiyun 			0,			/* index */
449*4882a593Smuzhiyun 			gspca_dev->usb_buf, 1,
450*4882a593Smuzhiyun 			500);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	if (res < 0) {
453*4882a593Smuzhiyun 		dev_err(gspca_dev->v4l2_dev.dev,
454*4882a593Smuzhiyun 			"Error reading register %02x: %d\n", value, res);
455*4882a593Smuzhiyun 		gspca_dev->usb_err = res;
456*4882a593Smuzhiyun 		/*
457*4882a593Smuzhiyun 		 * Make sure the result is zeroed to avoid uninitialized
458*4882a593Smuzhiyun 		 * values.
459*4882a593Smuzhiyun 		 */
460*4882a593Smuzhiyun 		gspca_dev->usb_buf[0] = 0;
461*4882a593Smuzhiyun 	}
462*4882a593Smuzhiyun }
463*4882a593Smuzhiyun 
reg_w(struct gspca_dev * gspca_dev,__u16 value,const __u8 * buffer,int len)464*4882a593Smuzhiyun static void reg_w(struct gspca_dev *gspca_dev,
465*4882a593Smuzhiyun 		  __u16 value,
466*4882a593Smuzhiyun 		  const __u8 *buffer,
467*4882a593Smuzhiyun 		  int len)
468*4882a593Smuzhiyun {
469*4882a593Smuzhiyun 	int res;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
472*4882a593Smuzhiyun 		return;
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	memcpy(gspca_dev->usb_buf, buffer, len);
475*4882a593Smuzhiyun 	res = usb_control_msg(gspca_dev->dev,
476*4882a593Smuzhiyun 			usb_sndctrlpipe(gspca_dev->dev, 0),
477*4882a593Smuzhiyun 			0x08,			/* request */
478*4882a593Smuzhiyun 			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE,
479*4882a593Smuzhiyun 			value,
480*4882a593Smuzhiyun 			0,			/* index */
481*4882a593Smuzhiyun 			gspca_dev->usb_buf, len,
482*4882a593Smuzhiyun 			500);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	if (res < 0) {
485*4882a593Smuzhiyun 		dev_err(gspca_dev->v4l2_dev.dev,
486*4882a593Smuzhiyun 			"Error writing register %02x: %d\n", value, res);
487*4882a593Smuzhiyun 		gspca_dev->usb_err = res;
488*4882a593Smuzhiyun 	}
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
i2c_w(struct gspca_dev * gspca_dev,const u8 * buf)491*4882a593Smuzhiyun static void i2c_w(struct gspca_dev *gspca_dev, const u8 *buf)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	int retry = 60;
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun 	if (gspca_dev->usb_err < 0)
496*4882a593Smuzhiyun 		return;
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	/* is i2c ready */
499*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x08, buf, 8);
500*4882a593Smuzhiyun 	while (retry--) {
501*4882a593Smuzhiyun 		if (gspca_dev->usb_err < 0)
502*4882a593Smuzhiyun 			return;
503*4882a593Smuzhiyun 		msleep(1);
504*4882a593Smuzhiyun 		reg_r(gspca_dev, 0x08);
505*4882a593Smuzhiyun 		if (gspca_dev->usb_buf[0] & 0x04) {
506*4882a593Smuzhiyun 			if (gspca_dev->usb_buf[0] & 0x08) {
507*4882a593Smuzhiyun 				dev_err(gspca_dev->v4l2_dev.dev,
508*4882a593Smuzhiyun 					"i2c error writing %8ph\n", buf);
509*4882a593Smuzhiyun 				gspca_dev->usb_err = -EIO;
510*4882a593Smuzhiyun 			}
511*4882a593Smuzhiyun 			return;
512*4882a593Smuzhiyun 		}
513*4882a593Smuzhiyun 	}
514*4882a593Smuzhiyun 
515*4882a593Smuzhiyun 	dev_err(gspca_dev->v4l2_dev.dev, "i2c write timeout\n");
516*4882a593Smuzhiyun 	gspca_dev->usb_err = -EIO;
517*4882a593Smuzhiyun }
518*4882a593Smuzhiyun 
i2c_w_vector(struct gspca_dev * gspca_dev,const __u8 buffer[][8],int len)519*4882a593Smuzhiyun static void i2c_w_vector(struct gspca_dev *gspca_dev,
520*4882a593Smuzhiyun 			const __u8 buffer[][8], int len)
521*4882a593Smuzhiyun {
522*4882a593Smuzhiyun 	for (;;) {
523*4882a593Smuzhiyun 		if (gspca_dev->usb_err < 0)
524*4882a593Smuzhiyun 			return;
525*4882a593Smuzhiyun 		i2c_w(gspca_dev, *buffer);
526*4882a593Smuzhiyun 		len -= 8;
527*4882a593Smuzhiyun 		if (len <= 0)
528*4882a593Smuzhiyun 			break;
529*4882a593Smuzhiyun 		buffer++;
530*4882a593Smuzhiyun 	}
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
setbrightness(struct gspca_dev * gspca_dev)533*4882a593Smuzhiyun static void setbrightness(struct gspca_dev *gspca_dev)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 	switch (sd->sensor) {
538*4882a593Smuzhiyun 	case  SENSOR_OV6650:
539*4882a593Smuzhiyun 	case  SENSOR_OV7630: {
540*4882a593Smuzhiyun 		__u8 i2cOV[] =
541*4882a593Smuzhiyun 			{0xa0, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x10};
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun 		/* change reg 0x06 */
544*4882a593Smuzhiyun 		i2cOV[1] = sensor_data[sd->sensor].sensor_addr;
545*4882a593Smuzhiyun 		i2cOV[3] = sd->brightness->val;
546*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cOV);
547*4882a593Smuzhiyun 		break;
548*4882a593Smuzhiyun 	}
549*4882a593Smuzhiyun 	case SENSOR_PAS106:
550*4882a593Smuzhiyun 	case SENSOR_PAS202: {
551*4882a593Smuzhiyun 		__u8 i2cpbright[] =
552*4882a593Smuzhiyun 			{0xb0, 0x40, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x16};
553*4882a593Smuzhiyun 		__u8 i2cpdoit[] =
554*4882a593Smuzhiyun 			{0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 		/* PAS106 uses reg 7 and 8 instead of b and c */
557*4882a593Smuzhiyun 		if (sd->sensor == SENSOR_PAS106) {
558*4882a593Smuzhiyun 			i2cpbright[2] = 7;
559*4882a593Smuzhiyun 			i2cpdoit[2] = 0x13;
560*4882a593Smuzhiyun 		}
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 		if (sd->brightness->val < 127) {
563*4882a593Smuzhiyun 			/* change reg 0x0b, signreg */
564*4882a593Smuzhiyun 			i2cpbright[3] = 0x01;
565*4882a593Smuzhiyun 			/* set reg 0x0c, offset */
566*4882a593Smuzhiyun 			i2cpbright[4] = 127 - sd->brightness->val;
567*4882a593Smuzhiyun 		} else
568*4882a593Smuzhiyun 			i2cpbright[4] = sd->brightness->val - 127;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpbright);
571*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpdoit);
572*4882a593Smuzhiyun 		break;
573*4882a593Smuzhiyun 	}
574*4882a593Smuzhiyun 	default:
575*4882a593Smuzhiyun 		break;
576*4882a593Smuzhiyun 	}
577*4882a593Smuzhiyun }
578*4882a593Smuzhiyun 
setgain(struct gspca_dev * gspca_dev)579*4882a593Smuzhiyun static void setgain(struct gspca_dev *gspca_dev)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
582*4882a593Smuzhiyun 	u8 gain = gspca_dev->gain->val;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	switch (sd->sensor) {
585*4882a593Smuzhiyun 	case SENSOR_HV7131D: {
586*4882a593Smuzhiyun 		__u8 i2c[] =
587*4882a593Smuzhiyun 			{0xc0, 0x11, 0x31, 0x00, 0x00, 0x00, 0x00, 0x17};
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 		i2c[3] = 0x3f - gain;
590*4882a593Smuzhiyun 		i2c[4] = 0x3f - gain;
591*4882a593Smuzhiyun 		i2c[5] = 0x3f - gain;
592*4882a593Smuzhiyun 
593*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2c);
594*4882a593Smuzhiyun 		break;
595*4882a593Smuzhiyun 	}
596*4882a593Smuzhiyun 	case SENSOR_TAS5110C:
597*4882a593Smuzhiyun 	case SENSOR_TAS5130CXX: {
598*4882a593Smuzhiyun 		__u8 i2c[] =
599*4882a593Smuzhiyun 			{0x30, 0x11, 0x02, 0x20, 0x70, 0x00, 0x00, 0x10};
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		i2c[4] = 255 - gain;
602*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2c);
603*4882a593Smuzhiyun 		break;
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 	case SENSOR_TAS5110D: {
606*4882a593Smuzhiyun 		__u8 i2c[] = {
607*4882a593Smuzhiyun 			0xb0, 0x61, 0x02, 0x00, 0x10, 0x00, 0x00, 0x17 };
608*4882a593Smuzhiyun 		gain = 255 - gain;
609*4882a593Smuzhiyun 		/* The bits in the register are the wrong way around!! */
610*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x80) >> 7;
611*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x40) >> 5;
612*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x20) >> 3;
613*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x10) >> 1;
614*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x08) << 1;
615*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x04) << 3;
616*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x02) << 5;
617*4882a593Smuzhiyun 		i2c[3] |= (gain & 0x01) << 7;
618*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2c);
619*4882a593Smuzhiyun 		break;
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun 	case SENSOR_OV6650:
622*4882a593Smuzhiyun 	case SENSOR_OV7630: {
623*4882a593Smuzhiyun 		__u8 i2c[] = {0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10};
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 		/*
626*4882a593Smuzhiyun 		 * The ov7630's gain is weird, at 32 the gain drops to the
627*4882a593Smuzhiyun 		 * same level as at 16, so skip 32-47 (of the 0-63 scale).
628*4882a593Smuzhiyun 		 */
629*4882a593Smuzhiyun 		if (sd->sensor == SENSOR_OV7630 && gain >= 32)
630*4882a593Smuzhiyun 			gain += 16;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 		i2c[1] = sensor_data[sd->sensor].sensor_addr;
633*4882a593Smuzhiyun 		i2c[3] = gain;
634*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2c);
635*4882a593Smuzhiyun 		break;
636*4882a593Smuzhiyun 	}
637*4882a593Smuzhiyun 	case SENSOR_PAS106:
638*4882a593Smuzhiyun 	case SENSOR_PAS202: {
639*4882a593Smuzhiyun 		__u8 i2cpgain[] =
640*4882a593Smuzhiyun 			{0xa0, 0x40, 0x10, 0x00, 0x00, 0x00, 0x00, 0x15};
641*4882a593Smuzhiyun 		__u8 i2cpcolorgain[] =
642*4882a593Smuzhiyun 			{0xc0, 0x40, 0x07, 0x00, 0x00, 0x00, 0x00, 0x15};
643*4882a593Smuzhiyun 		__u8 i2cpdoit[] =
644*4882a593Smuzhiyun 			{0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
645*4882a593Smuzhiyun 
646*4882a593Smuzhiyun 		/* PAS106 uses different regs (and has split green gains) */
647*4882a593Smuzhiyun 		if (sd->sensor == SENSOR_PAS106) {
648*4882a593Smuzhiyun 			i2cpgain[2] = 0x0e;
649*4882a593Smuzhiyun 			i2cpcolorgain[0] = 0xd0;
650*4882a593Smuzhiyun 			i2cpcolorgain[2] = 0x09;
651*4882a593Smuzhiyun 			i2cpdoit[2] = 0x13;
652*4882a593Smuzhiyun 		}
653*4882a593Smuzhiyun 
654*4882a593Smuzhiyun 		i2cpgain[3] = gain;
655*4882a593Smuzhiyun 		i2cpcolorgain[3] = gain >> 1;
656*4882a593Smuzhiyun 		i2cpcolorgain[4] = gain >> 1;
657*4882a593Smuzhiyun 		i2cpcolorgain[5] = gain >> 1;
658*4882a593Smuzhiyun 		i2cpcolorgain[6] = gain >> 1;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpgain);
661*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpcolorgain);
662*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpdoit);
663*4882a593Smuzhiyun 		break;
664*4882a593Smuzhiyun 	}
665*4882a593Smuzhiyun 	default:
666*4882a593Smuzhiyun 		if (sd->bridge == BRIDGE_103) {
667*4882a593Smuzhiyun 			u8 buf[3] = { gain, gain, gain }; /* R, G, B */
668*4882a593Smuzhiyun 			reg_w(gspca_dev, 0x05, buf, 3);
669*4882a593Smuzhiyun 		} else {
670*4882a593Smuzhiyun 			u8 buf[2];
671*4882a593Smuzhiyun 			buf[0] = gain << 4 | gain; /* Red and blue */
672*4882a593Smuzhiyun 			buf[1] = gain; /* Green */
673*4882a593Smuzhiyun 			reg_w(gspca_dev, 0x10, buf, 2);
674*4882a593Smuzhiyun 		}
675*4882a593Smuzhiyun 	}
676*4882a593Smuzhiyun }
677*4882a593Smuzhiyun 
setexposure(struct gspca_dev * gspca_dev)678*4882a593Smuzhiyun static void setexposure(struct gspca_dev *gspca_dev)
679*4882a593Smuzhiyun {
680*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	switch (sd->sensor) {
683*4882a593Smuzhiyun 	case SENSOR_HV7131D: {
684*4882a593Smuzhiyun 		/* Note the datasheet wrongly says line mode exposure uses reg
685*4882a593Smuzhiyun 		   0x26 and 0x27, testing has shown 0x25 + 0x26 */
686*4882a593Smuzhiyun 		__u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};
687*4882a593Smuzhiyun 		u16 reg = gspca_dev->exposure->val;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 		i2c[3] = reg >> 8;
690*4882a593Smuzhiyun 		i2c[4] = reg & 0xff;
691*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2c);
692*4882a593Smuzhiyun 		break;
693*4882a593Smuzhiyun 	}
694*4882a593Smuzhiyun 	case SENSOR_TAS5110C:
695*4882a593Smuzhiyun 	case SENSOR_TAS5110D: {
696*4882a593Smuzhiyun 		/* register 19's high nibble contains the sn9c10x clock divider
697*4882a593Smuzhiyun 		   The high nibble configures the no fps according to the
698*4882a593Smuzhiyun 		   formula: 60 / high_nibble. With a maximum of 30 fps */
699*4882a593Smuzhiyun 		u8 reg = gspca_dev->exposure->val;
700*4882a593Smuzhiyun 
701*4882a593Smuzhiyun 		reg = (reg << 4) | 0x0b;
702*4882a593Smuzhiyun 		reg_w(gspca_dev, 0x19, &reg, 1);
703*4882a593Smuzhiyun 		break;
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun 	case SENSOR_OV6650:
706*4882a593Smuzhiyun 	case SENSOR_OV7630: {
707*4882a593Smuzhiyun 		/* The ov6650 / ov7630 have 2 registers which both influence
708*4882a593Smuzhiyun 		   exposure, register 11, whose low nibble sets the nr off fps
709*4882a593Smuzhiyun 		   according to: fps = 30 / (low_nibble + 1)
710*4882a593Smuzhiyun 
711*4882a593Smuzhiyun 		   The fps configures the maximum exposure setting, but it is
712*4882a593Smuzhiyun 		   possible to use less exposure then what the fps maximum
713*4882a593Smuzhiyun 		   allows by setting register 10. register 10 configures the
714*4882a593Smuzhiyun 		   actual exposure as quotient of the full exposure, with 0
715*4882a593Smuzhiyun 		   being no exposure at all (not very useful) and reg10_max
716*4882a593Smuzhiyun 		   being max exposure possible at that framerate.
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 		   The code maps our 0 - 510 ms exposure ctrl to these 2
719*4882a593Smuzhiyun 		   registers, trying to keep fps as high as possible.
720*4882a593Smuzhiyun 		*/
721*4882a593Smuzhiyun 		__u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10};
722*4882a593Smuzhiyun 		int reg10, reg11, reg10_max;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 		/* ov6645 datasheet says reg10_max is 9a, but that uses
725*4882a593Smuzhiyun 		   tline * 2 * reg10 as formula for calculating texpo, the
726*4882a593Smuzhiyun 		   ov6650 probably uses the same formula as the 7730 which uses
727*4882a593Smuzhiyun 		   tline * 4 * reg10, which explains why the reg10max we've
728*4882a593Smuzhiyun 		   found experimentally for the ov6650 is exactly half that of
729*4882a593Smuzhiyun 		   the ov6645. The ov7630 datasheet says the max is 0x41. */
730*4882a593Smuzhiyun 		if (sd->sensor == SENSOR_OV6650) {
731*4882a593Smuzhiyun 			reg10_max = 0x4d;
732*4882a593Smuzhiyun 			i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
733*4882a593Smuzhiyun 		} else
734*4882a593Smuzhiyun 			reg10_max = 0x41;
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 		reg11 = (15 * gspca_dev->exposure->val + 999) / 1000;
737*4882a593Smuzhiyun 		if (reg11 < 1)
738*4882a593Smuzhiyun 			reg11 = 1;
739*4882a593Smuzhiyun 		else if (reg11 > 16)
740*4882a593Smuzhiyun 			reg11 = 16;
741*4882a593Smuzhiyun 
742*4882a593Smuzhiyun 		/* In 640x480, if the reg11 has less than 4, the image is
743*4882a593Smuzhiyun 		   unstable (the bridge goes into a higher compression mode
744*4882a593Smuzhiyun 		   which we have not reverse engineered yet). */
745*4882a593Smuzhiyun 		if (gspca_dev->pixfmt.width == 640 && reg11 < 4)
746*4882a593Smuzhiyun 			reg11 = 4;
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 		/* frame exposure time in ms = 1000 * reg11 / 30    ->
749*4882a593Smuzhiyun 		reg10 = (gspca_dev->exposure->val / 2) * reg10_max
750*4882a593Smuzhiyun 				/ (1000 * reg11 / 30) */
751*4882a593Smuzhiyun 		reg10 = (gspca_dev->exposure->val * 15 * reg10_max)
752*4882a593Smuzhiyun 				/ (1000 * reg11);
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 		/* Don't allow this to get below 10 when using autogain, the
755*4882a593Smuzhiyun 		   steps become very large (relatively) when below 10 causing
756*4882a593Smuzhiyun 		   the image to oscillate from much too dark, to much too bright
757*4882a593Smuzhiyun 		   and back again. */
758*4882a593Smuzhiyun 		if (gspca_dev->autogain->val && reg10 < 10)
759*4882a593Smuzhiyun 			reg10 = 10;
760*4882a593Smuzhiyun 		else if (reg10 > reg10_max)
761*4882a593Smuzhiyun 			reg10 = reg10_max;
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 		/* Write reg 10 and reg11 low nibble */
764*4882a593Smuzhiyun 		i2c[1] = sensor_data[sd->sensor].sensor_addr;
765*4882a593Smuzhiyun 		i2c[3] = reg10;
766*4882a593Smuzhiyun 		i2c[4] |= reg11 - 1;
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 		/* If register 11 didn't change, don't change it */
769*4882a593Smuzhiyun 		if (sd->reg11 == reg11)
770*4882a593Smuzhiyun 			i2c[0] = 0xa0;
771*4882a593Smuzhiyun 
772*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2c);
773*4882a593Smuzhiyun 		if (gspca_dev->usb_err == 0)
774*4882a593Smuzhiyun 			sd->reg11 = reg11;
775*4882a593Smuzhiyun 		break;
776*4882a593Smuzhiyun 	}
777*4882a593Smuzhiyun 	case SENSOR_PAS202: {
778*4882a593Smuzhiyun 		__u8 i2cpframerate[] =
779*4882a593Smuzhiyun 			{0xb0, 0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x16};
780*4882a593Smuzhiyun 		__u8 i2cpexpo[] =
781*4882a593Smuzhiyun 			{0xa0, 0x40, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x16};
782*4882a593Smuzhiyun 		const __u8 i2cpdoit[] =
783*4882a593Smuzhiyun 			{0xa0, 0x40, 0x11, 0x01, 0x00, 0x00, 0x00, 0x16};
784*4882a593Smuzhiyun 		int framerate_ctrl;
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 		/* The exposure knee for the autogain algorithm is 200
787*4882a593Smuzhiyun 		   (100 ms / 10 fps on other sensors), for values below this
788*4882a593Smuzhiyun 		   use the control for setting the partial frame expose time,
789*4882a593Smuzhiyun 		   above that use variable framerate. This way we run at max
790*4882a593Smuzhiyun 		   framerate (640x480@7.5 fps, 320x240@10fps) until the knee
791*4882a593Smuzhiyun 		   is reached. Using the variable framerate control above 200
792*4882a593Smuzhiyun 		   is better then playing around with both clockdiv + partial
793*4882a593Smuzhiyun 		   frame exposure times (like we are doing with the ov chips),
794*4882a593Smuzhiyun 		   as that sometimes leads to jumps in the exposure control,
795*4882a593Smuzhiyun 		   which are bad for auto exposure. */
796*4882a593Smuzhiyun 		if (gspca_dev->exposure->val < 200) {
797*4882a593Smuzhiyun 			i2cpexpo[3] = 255 - (gspca_dev->exposure->val * 255)
798*4882a593Smuzhiyun 						/ 200;
799*4882a593Smuzhiyun 			framerate_ctrl = 500;
800*4882a593Smuzhiyun 		} else {
801*4882a593Smuzhiyun 			/* The PAS202's exposure control goes from 0 - 4095,
802*4882a593Smuzhiyun 			   but anything below 500 causes vsync issues, so scale
803*4882a593Smuzhiyun 			   our 200-1023 to 500-4095 */
804*4882a593Smuzhiyun 			framerate_ctrl = (gspca_dev->exposure->val - 200)
805*4882a593Smuzhiyun 							* 1000 / 229 +  500;
806*4882a593Smuzhiyun 		}
807*4882a593Smuzhiyun 
808*4882a593Smuzhiyun 		i2cpframerate[3] = framerate_ctrl >> 6;
809*4882a593Smuzhiyun 		i2cpframerate[4] = framerate_ctrl & 0x3f;
810*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpframerate);
811*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpexpo);
812*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpdoit);
813*4882a593Smuzhiyun 		break;
814*4882a593Smuzhiyun 	}
815*4882a593Smuzhiyun 	case SENSOR_PAS106: {
816*4882a593Smuzhiyun 		__u8 i2cpframerate[] =
817*4882a593Smuzhiyun 			{0xb1, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x14};
818*4882a593Smuzhiyun 		__u8 i2cpexpo[] =
819*4882a593Smuzhiyun 			{0xa1, 0x40, 0x05, 0x00, 0x00, 0x00, 0x00, 0x14};
820*4882a593Smuzhiyun 		const __u8 i2cpdoit[] =
821*4882a593Smuzhiyun 			{0xa1, 0x40, 0x13, 0x01, 0x00, 0x00, 0x00, 0x14};
822*4882a593Smuzhiyun 		int framerate_ctrl;
823*4882a593Smuzhiyun 
824*4882a593Smuzhiyun 		/* For values below 150 use partial frame exposure, above
825*4882a593Smuzhiyun 		   that use framerate ctrl */
826*4882a593Smuzhiyun 		if (gspca_dev->exposure->val < 150) {
827*4882a593Smuzhiyun 			i2cpexpo[3] = 150 - gspca_dev->exposure->val;
828*4882a593Smuzhiyun 			framerate_ctrl = 300;
829*4882a593Smuzhiyun 		} else {
830*4882a593Smuzhiyun 			/* The PAS106's exposure control goes from 0 - 4095,
831*4882a593Smuzhiyun 			   but anything below 300 causes vsync issues, so scale
832*4882a593Smuzhiyun 			   our 150-1023 to 300-4095 */
833*4882a593Smuzhiyun 			framerate_ctrl = (gspca_dev->exposure->val - 150)
834*4882a593Smuzhiyun 						* 1000 / 230 + 300;
835*4882a593Smuzhiyun 		}
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun 		i2cpframerate[3] = framerate_ctrl >> 4;
838*4882a593Smuzhiyun 		i2cpframerate[4] = framerate_ctrl & 0x0f;
839*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpframerate);
840*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpexpo);
841*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2cpdoit);
842*4882a593Smuzhiyun 		break;
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 	default:
845*4882a593Smuzhiyun 		break;
846*4882a593Smuzhiyun 	}
847*4882a593Smuzhiyun }
848*4882a593Smuzhiyun 
setfreq(struct gspca_dev * gspca_dev)849*4882a593Smuzhiyun static void setfreq(struct gspca_dev *gspca_dev)
850*4882a593Smuzhiyun {
851*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
852*4882a593Smuzhiyun 
853*4882a593Smuzhiyun 	if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630) {
854*4882a593Smuzhiyun 		/* Framerate adjust register for artificial light 50 hz flicker
855*4882a593Smuzhiyun 		   compensation, for the ov6650 this is identical to ov6630
856*4882a593Smuzhiyun 		   0x2b register, see ov6630 datasheet.
857*4882a593Smuzhiyun 		   0x4f / 0x8a -> (30 fps -> 25 fps), 0x00 -> no adjustment */
858*4882a593Smuzhiyun 		__u8 i2c[] = {0xa0, 0x00, 0x2b, 0x00, 0x00, 0x00, 0x00, 0x10};
859*4882a593Smuzhiyun 		switch (sd->plfreq->val) {
860*4882a593Smuzhiyun 		default:
861*4882a593Smuzhiyun /*		case 0:			 * no filter*/
862*4882a593Smuzhiyun /*		case 2:			 * 60 hz */
863*4882a593Smuzhiyun 			i2c[3] = 0;
864*4882a593Smuzhiyun 			break;
865*4882a593Smuzhiyun 		case 1:			/* 50 hz */
866*4882a593Smuzhiyun 			i2c[3] = (sd->sensor == SENSOR_OV6650)
867*4882a593Smuzhiyun 					? 0x4f : 0x8a;
868*4882a593Smuzhiyun 			break;
869*4882a593Smuzhiyun 		}
870*4882a593Smuzhiyun 		i2c[1] = sensor_data[sd->sensor].sensor_addr;
871*4882a593Smuzhiyun 		i2c_w(gspca_dev, i2c);
872*4882a593Smuzhiyun 	}
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun 
do_autogain(struct gspca_dev * gspca_dev)875*4882a593Smuzhiyun static void do_autogain(struct gspca_dev *gspca_dev)
876*4882a593Smuzhiyun {
877*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
878*4882a593Smuzhiyun 	int deadzone, desired_avg_lum, avg_lum;
879*4882a593Smuzhiyun 
880*4882a593Smuzhiyun 	avg_lum = atomic_read(&sd->avg_lum);
881*4882a593Smuzhiyun 	if (avg_lum == -1)
882*4882a593Smuzhiyun 		return;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	if (sd->autogain_ignore_frames > 0) {
885*4882a593Smuzhiyun 		sd->autogain_ignore_frames--;
886*4882a593Smuzhiyun 		return;
887*4882a593Smuzhiyun 	}
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	/* SIF / VGA sensors have a different autoexposure area and thus
890*4882a593Smuzhiyun 	   different avg_lum values for the same picture brightness */
891*4882a593Smuzhiyun 	if (sensor_data[sd->sensor].flags & F_SIF) {
892*4882a593Smuzhiyun 		deadzone = 500;
893*4882a593Smuzhiyun 		/* SIF sensors tend to overexpose, so keep this small */
894*4882a593Smuzhiyun 		desired_avg_lum = 5000;
895*4882a593Smuzhiyun 	} else {
896*4882a593Smuzhiyun 		deadzone = 1500;
897*4882a593Smuzhiyun 		desired_avg_lum = 13000;
898*4882a593Smuzhiyun 	}
899*4882a593Smuzhiyun 
900*4882a593Smuzhiyun 	if (sd->brightness)
901*4882a593Smuzhiyun 		desired_avg_lum = sd->brightness->val * desired_avg_lum / 127;
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	if (gspca_dev->exposure->maximum < 500) {
904*4882a593Smuzhiyun 		if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
905*4882a593Smuzhiyun 				desired_avg_lum, deadzone))
906*4882a593Smuzhiyun 			sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
907*4882a593Smuzhiyun 	} else {
908*4882a593Smuzhiyun 		int gain_knee = (s32)gspca_dev->gain->maximum * 9 / 10;
909*4882a593Smuzhiyun 		if (gspca_expo_autogain(gspca_dev, avg_lum, desired_avg_lum,
910*4882a593Smuzhiyun 				deadzone, gain_knee, sd->exposure_knee))
911*4882a593Smuzhiyun 			sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
912*4882a593Smuzhiyun 	}
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun /* this function is called at probe time */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)916*4882a593Smuzhiyun static int sd_config(struct gspca_dev *gspca_dev,
917*4882a593Smuzhiyun 			const struct usb_device_id *id)
918*4882a593Smuzhiyun {
919*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
920*4882a593Smuzhiyun 	struct cam *cam;
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	reg_r(gspca_dev, 0x00);
923*4882a593Smuzhiyun 	if (gspca_dev->usb_buf[0] != 0x10)
924*4882a593Smuzhiyun 		return -ENODEV;
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	/* copy the webcam info from the device id */
927*4882a593Smuzhiyun 	sd->sensor = id->driver_info >> 8;
928*4882a593Smuzhiyun 	sd->bridge = id->driver_info & 0xff;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	cam = &gspca_dev->cam;
931*4882a593Smuzhiyun 	if (!(sensor_data[sd->sensor].flags & F_SIF)) {
932*4882a593Smuzhiyun 		cam->cam_mode = vga_mode;
933*4882a593Smuzhiyun 		cam->nmodes = ARRAY_SIZE(vga_mode);
934*4882a593Smuzhiyun 	} else {
935*4882a593Smuzhiyun 		cam->cam_mode = sif_mode;
936*4882a593Smuzhiyun 		cam->nmodes = ARRAY_SIZE(sif_mode);
937*4882a593Smuzhiyun 	}
938*4882a593Smuzhiyun 	cam->npkt = 36;			/* 36 packets per ISOC message */
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	return 0;
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun 
943*4882a593Smuzhiyun /* this function is called at probe and resume time */
sd_init(struct gspca_dev * gspca_dev)944*4882a593Smuzhiyun static int sd_init(struct gspca_dev *gspca_dev)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun 	const __u8 stop = 0x09; /* Disable stream turn of LED */
947*4882a593Smuzhiyun 
948*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x01, &stop, 1);
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	return gspca_dev->usb_err;
951*4882a593Smuzhiyun }
952*4882a593Smuzhiyun 
sd_s_ctrl(struct v4l2_ctrl * ctrl)953*4882a593Smuzhiyun static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
954*4882a593Smuzhiyun {
955*4882a593Smuzhiyun 	struct gspca_dev *gspca_dev =
956*4882a593Smuzhiyun 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
957*4882a593Smuzhiyun 	struct sd *sd = (struct sd *)gspca_dev;
958*4882a593Smuzhiyun 
959*4882a593Smuzhiyun 	gspca_dev->usb_err = 0;
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) {
962*4882a593Smuzhiyun 		/* when switching to autogain set defaults to make sure
963*4882a593Smuzhiyun 		   we are on a valid point of the autogain gain /
964*4882a593Smuzhiyun 		   exposure knee graph, and give this change time to
965*4882a593Smuzhiyun 		   take effect before doing autogain. */
966*4882a593Smuzhiyun 		gspca_dev->gain->val = gspca_dev->gain->default_value;
967*4882a593Smuzhiyun 		gspca_dev->exposure->val = gspca_dev->exposure->default_value;
968*4882a593Smuzhiyun 		sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
969*4882a593Smuzhiyun 	}
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun 	if (!gspca_dev->streaming)
972*4882a593Smuzhiyun 		return 0;
973*4882a593Smuzhiyun 
974*4882a593Smuzhiyun 	switch (ctrl->id) {
975*4882a593Smuzhiyun 	case V4L2_CID_BRIGHTNESS:
976*4882a593Smuzhiyun 		setbrightness(gspca_dev);
977*4882a593Smuzhiyun 		break;
978*4882a593Smuzhiyun 	case V4L2_CID_AUTOGAIN:
979*4882a593Smuzhiyun 		if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
980*4882a593Smuzhiyun 			setexposure(gspca_dev);
981*4882a593Smuzhiyun 		if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
982*4882a593Smuzhiyun 			setgain(gspca_dev);
983*4882a593Smuzhiyun 		break;
984*4882a593Smuzhiyun 	case V4L2_CID_POWER_LINE_FREQUENCY:
985*4882a593Smuzhiyun 		setfreq(gspca_dev);
986*4882a593Smuzhiyun 		break;
987*4882a593Smuzhiyun 	default:
988*4882a593Smuzhiyun 		return -EINVAL;
989*4882a593Smuzhiyun 	}
990*4882a593Smuzhiyun 	return gspca_dev->usb_err;
991*4882a593Smuzhiyun }
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun static const struct v4l2_ctrl_ops sd_ctrl_ops = {
994*4882a593Smuzhiyun 	.s_ctrl = sd_s_ctrl,
995*4882a593Smuzhiyun };
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun /* this function is called at probe time */
sd_init_controls(struct gspca_dev * gspca_dev)998*4882a593Smuzhiyun static int sd_init_controls(struct gspca_dev *gspca_dev)
999*4882a593Smuzhiyun {
1000*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1001*4882a593Smuzhiyun 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun 	gspca_dev->vdev.ctrl_handler = hdl;
1004*4882a593Smuzhiyun 	v4l2_ctrl_handler_init(hdl, 5);
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630 ||
1007*4882a593Smuzhiyun 	    sd->sensor == SENSOR_PAS106 || sd->sensor == SENSOR_PAS202)
1008*4882a593Smuzhiyun 		sd->brightness = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1009*4882a593Smuzhiyun 					V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
1010*4882a593Smuzhiyun 
1011*4882a593Smuzhiyun 	/* Gain range is sensor dependent */
1012*4882a593Smuzhiyun 	switch (sd->sensor) {
1013*4882a593Smuzhiyun 	case SENSOR_OV6650:
1014*4882a593Smuzhiyun 	case SENSOR_PAS106:
1015*4882a593Smuzhiyun 	case SENSOR_PAS202:
1016*4882a593Smuzhiyun 		gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1017*4882a593Smuzhiyun 					V4L2_CID_GAIN, 0, 31, 1, 15);
1018*4882a593Smuzhiyun 		break;
1019*4882a593Smuzhiyun 	case SENSOR_OV7630:
1020*4882a593Smuzhiyun 		gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1021*4882a593Smuzhiyun 					V4L2_CID_GAIN, 0, 47, 1, 31);
1022*4882a593Smuzhiyun 		break;
1023*4882a593Smuzhiyun 	case SENSOR_HV7131D:
1024*4882a593Smuzhiyun 		gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1025*4882a593Smuzhiyun 					V4L2_CID_GAIN, 0, 63, 1, 31);
1026*4882a593Smuzhiyun 		break;
1027*4882a593Smuzhiyun 	case SENSOR_TAS5110C:
1028*4882a593Smuzhiyun 	case SENSOR_TAS5110D:
1029*4882a593Smuzhiyun 	case SENSOR_TAS5130CXX:
1030*4882a593Smuzhiyun 		gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1031*4882a593Smuzhiyun 					V4L2_CID_GAIN, 0, 255, 1, 127);
1032*4882a593Smuzhiyun 		break;
1033*4882a593Smuzhiyun 	default:
1034*4882a593Smuzhiyun 		if (sd->bridge == BRIDGE_103) {
1035*4882a593Smuzhiyun 			gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1036*4882a593Smuzhiyun 						V4L2_CID_GAIN, 0, 127, 1, 63);
1037*4882a593Smuzhiyun 		} else {
1038*4882a593Smuzhiyun 			gspca_dev->gain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1039*4882a593Smuzhiyun 						V4L2_CID_GAIN, 0, 15, 1, 7);
1040*4882a593Smuzhiyun 		}
1041*4882a593Smuzhiyun 	}
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	/* Exposure range is sensor dependent, and not all have exposure */
1044*4882a593Smuzhiyun 	switch (sd->sensor) {
1045*4882a593Smuzhiyun 	case SENSOR_HV7131D:
1046*4882a593Smuzhiyun 		gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1047*4882a593Smuzhiyun 					V4L2_CID_EXPOSURE, 0, 8191, 1, 482);
1048*4882a593Smuzhiyun 		sd->exposure_knee = 964;
1049*4882a593Smuzhiyun 		break;
1050*4882a593Smuzhiyun 	case SENSOR_OV6650:
1051*4882a593Smuzhiyun 	case SENSOR_OV7630:
1052*4882a593Smuzhiyun 	case SENSOR_PAS106:
1053*4882a593Smuzhiyun 	case SENSOR_PAS202:
1054*4882a593Smuzhiyun 		gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1055*4882a593Smuzhiyun 					V4L2_CID_EXPOSURE, 0, 1023, 1, 66);
1056*4882a593Smuzhiyun 		sd->exposure_knee = 200;
1057*4882a593Smuzhiyun 		break;
1058*4882a593Smuzhiyun 	case SENSOR_TAS5110C:
1059*4882a593Smuzhiyun 	case SENSOR_TAS5110D:
1060*4882a593Smuzhiyun 		gspca_dev->exposure = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1061*4882a593Smuzhiyun 					V4L2_CID_EXPOSURE, 2, 15, 1, 2);
1062*4882a593Smuzhiyun 		break;
1063*4882a593Smuzhiyun 	}
1064*4882a593Smuzhiyun 
1065*4882a593Smuzhiyun 	if (gspca_dev->exposure) {
1066*4882a593Smuzhiyun 		gspca_dev->autogain = v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
1067*4882a593Smuzhiyun 						V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
1068*4882a593Smuzhiyun 	}
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	if (sd->sensor == SENSOR_OV6650 || sd->sensor == SENSOR_OV7630)
1071*4882a593Smuzhiyun 		sd->plfreq = v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
1072*4882a593Smuzhiyun 			V4L2_CID_POWER_LINE_FREQUENCY,
1073*4882a593Smuzhiyun 			V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0,
1074*4882a593Smuzhiyun 			V4L2_CID_POWER_LINE_FREQUENCY_DISABLED);
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	if (hdl->error) {
1077*4882a593Smuzhiyun 		pr_err("Could not initialize controls\n");
1078*4882a593Smuzhiyun 		return hdl->error;
1079*4882a593Smuzhiyun 	}
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	if (gspca_dev->autogain)
1082*4882a593Smuzhiyun 		v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
1083*4882a593Smuzhiyun 
1084*4882a593Smuzhiyun 	return 0;
1085*4882a593Smuzhiyun }
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun /* -- start the camera -- */
sd_start(struct gspca_dev * gspca_dev)1088*4882a593Smuzhiyun static int sd_start(struct gspca_dev *gspca_dev)
1089*4882a593Smuzhiyun {
1090*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1091*4882a593Smuzhiyun 	struct cam *cam = &gspca_dev->cam;
1092*4882a593Smuzhiyun 	int i, mode;
1093*4882a593Smuzhiyun 	__u8 regs[0x31];
1094*4882a593Smuzhiyun 
1095*4882a593Smuzhiyun 	mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07;
1096*4882a593Smuzhiyun 	/* Copy registers 0x01 - 0x19 from the template */
1097*4882a593Smuzhiyun 	memcpy(&regs[0x01], sensor_data[sd->sensor].bridge_init, 0x19);
1098*4882a593Smuzhiyun 	/* Set the mode */
1099*4882a593Smuzhiyun 	regs[0x18] |= mode << 4;
1100*4882a593Smuzhiyun 
1101*4882a593Smuzhiyun 	/* Set bridge gain to 1.0 */
1102*4882a593Smuzhiyun 	if (sd->bridge == BRIDGE_103) {
1103*4882a593Smuzhiyun 		regs[0x05] = 0x20; /* Red */
1104*4882a593Smuzhiyun 		regs[0x06] = 0x20; /* Green */
1105*4882a593Smuzhiyun 		regs[0x07] = 0x20; /* Blue */
1106*4882a593Smuzhiyun 	} else {
1107*4882a593Smuzhiyun 		regs[0x10] = 0x00; /* Red and blue */
1108*4882a593Smuzhiyun 		regs[0x11] = 0x00; /* Green */
1109*4882a593Smuzhiyun 	}
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	/* Setup pixel numbers and auto exposure window */
1112*4882a593Smuzhiyun 	if (sensor_data[sd->sensor].flags & F_SIF) {
1113*4882a593Smuzhiyun 		regs[0x1a] = 0x14; /* HO_SIZE 640, makes no sense */
1114*4882a593Smuzhiyun 		regs[0x1b] = 0x0a; /* VO_SIZE 320, makes no sense */
1115*4882a593Smuzhiyun 		regs[0x1c] = 0x02; /* AE H-start 64 */
1116*4882a593Smuzhiyun 		regs[0x1d] = 0x02; /* AE V-start 64 */
1117*4882a593Smuzhiyun 		regs[0x1e] = 0x09; /* AE H-end 288 */
1118*4882a593Smuzhiyun 		regs[0x1f] = 0x07; /* AE V-end 224 */
1119*4882a593Smuzhiyun 	} else {
1120*4882a593Smuzhiyun 		regs[0x1a] = 0x1d; /* HO_SIZE 960, makes no sense */
1121*4882a593Smuzhiyun 		regs[0x1b] = 0x10; /* VO_SIZE 512, makes no sense */
1122*4882a593Smuzhiyun 		regs[0x1c] = 0x05; /* AE H-start 160 */
1123*4882a593Smuzhiyun 		regs[0x1d] = 0x03; /* AE V-start 96 */
1124*4882a593Smuzhiyun 		regs[0x1e] = 0x0f; /* AE H-end 480 */
1125*4882a593Smuzhiyun 		regs[0x1f] = 0x0c; /* AE V-end 384 */
1126*4882a593Smuzhiyun 	}
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	/* Setup the gamma table (only used with the sn9c103 bridge) */
1129*4882a593Smuzhiyun 	for (i = 0; i < 16; i++)
1130*4882a593Smuzhiyun 		regs[0x20 + i] = i * 16;
1131*4882a593Smuzhiyun 	regs[0x20 + i] = 255;
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun 	/* Special cases where some regs depend on mode or bridge */
1134*4882a593Smuzhiyun 	switch (sd->sensor) {
1135*4882a593Smuzhiyun 	case SENSOR_TAS5130CXX:
1136*4882a593Smuzhiyun 		/* FIXME / TESTME
1137*4882a593Smuzhiyun 		   probably not mode specific at all most likely the upper
1138*4882a593Smuzhiyun 		   nibble of 0x19 is exposure (clock divider) just as with
1139*4882a593Smuzhiyun 		   the tas5110, we need someone to test this. */
1140*4882a593Smuzhiyun 		regs[0x19] = mode ? 0x23 : 0x43;
1141*4882a593Smuzhiyun 		break;
1142*4882a593Smuzhiyun 	case SENSOR_OV7630:
1143*4882a593Smuzhiyun 		/* FIXME / TESTME for some reason with the 101/102 bridge the
1144*4882a593Smuzhiyun 		   clock is set to 12 Mhz (reg1 == 0x04), rather then 24.
1145*4882a593Smuzhiyun 		   Also the hstart needs to go from 1 to 2 when using a 103,
1146*4882a593Smuzhiyun 		   which is likely related. This does not seem right. */
1147*4882a593Smuzhiyun 		if (sd->bridge == BRIDGE_103) {
1148*4882a593Smuzhiyun 			regs[0x01] = 0x44; /* Select 24 Mhz clock */
1149*4882a593Smuzhiyun 			regs[0x12] = 0x02; /* Set hstart to 2 */
1150*4882a593Smuzhiyun 		}
1151*4882a593Smuzhiyun 		break;
1152*4882a593Smuzhiyun 	case SENSOR_PAS202:
1153*4882a593Smuzhiyun 		/* For some unknown reason we need to increase hstart by 1 on
1154*4882a593Smuzhiyun 		   the sn9c103, otherwise we get wrong colors (bayer shift). */
1155*4882a593Smuzhiyun 		if (sd->bridge == BRIDGE_103)
1156*4882a593Smuzhiyun 			regs[0x12] += 1;
1157*4882a593Smuzhiyun 		break;
1158*4882a593Smuzhiyun 	}
1159*4882a593Smuzhiyun 	/* Disable compression when the raw bayer format has been selected */
1160*4882a593Smuzhiyun 	if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
1161*4882a593Smuzhiyun 		regs[0x18] &= ~0x80;
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	/* Vga mode emulation on SIF sensor? */
1164*4882a593Smuzhiyun 	if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_REDUCED_SIF) {
1165*4882a593Smuzhiyun 		regs[0x12] += 16;	/* hstart adjust */
1166*4882a593Smuzhiyun 		regs[0x13] += 24;	/* vstart adjust */
1167*4882a593Smuzhiyun 		regs[0x15]  = 320 / 16; /* hsize */
1168*4882a593Smuzhiyun 		regs[0x16]  = 240 / 16; /* vsize */
1169*4882a593Smuzhiyun 	}
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	/* reg 0x01 bit 2 video transfert on */
1172*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1173*4882a593Smuzhiyun 	/* reg 0x17 SensorClk enable inv Clk 0x60 */
1174*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1175*4882a593Smuzhiyun 	/* Set the registers from the template */
1176*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x01, &regs[0x01],
1177*4882a593Smuzhiyun 	      (sd->bridge == BRIDGE_103) ? 0x30 : 0x1f);
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun 	/* Init the sensor */
1180*4882a593Smuzhiyun 	i2c_w_vector(gspca_dev, sensor_data[sd->sensor].sensor_init,
1181*4882a593Smuzhiyun 			sensor_data[sd->sensor].sensor_init_size);
1182*4882a593Smuzhiyun 
1183*4882a593Smuzhiyun 	/* Mode / bridge specific sensor setup */
1184*4882a593Smuzhiyun 	switch (sd->sensor) {
1185*4882a593Smuzhiyun 	case SENSOR_PAS202: {
1186*4882a593Smuzhiyun 		const __u8 i2cpclockdiv[] =
1187*4882a593Smuzhiyun 			{0xa0, 0x40, 0x02, 0x03, 0x00, 0x00, 0x00, 0x10};
1188*4882a593Smuzhiyun 		/* clockdiv from 4 to 3 (7.5 -> 10 fps) when in low res mode */
1189*4882a593Smuzhiyun 		if (mode)
1190*4882a593Smuzhiyun 			i2c_w(gspca_dev, i2cpclockdiv);
1191*4882a593Smuzhiyun 		break;
1192*4882a593Smuzhiyun 	    }
1193*4882a593Smuzhiyun 	case SENSOR_OV7630:
1194*4882a593Smuzhiyun 		/* FIXME / TESTME We should be able to handle this identical
1195*4882a593Smuzhiyun 		   for the 101/102 and the 103 case */
1196*4882a593Smuzhiyun 		if (sd->bridge == BRIDGE_103) {
1197*4882a593Smuzhiyun 			const __u8 i2c[] = { 0xa0, 0x21, 0x13,
1198*4882a593Smuzhiyun 					     0x80, 0x00, 0x00, 0x00, 0x10 };
1199*4882a593Smuzhiyun 			i2c_w(gspca_dev, i2c);
1200*4882a593Smuzhiyun 		}
1201*4882a593Smuzhiyun 		break;
1202*4882a593Smuzhiyun 	}
1203*4882a593Smuzhiyun 	/* H_size V_size 0x28, 0x1e -> 640x480. 0x16, 0x12 -> 352x288 */
1204*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x15, &regs[0x15], 2);
1205*4882a593Smuzhiyun 	/* compression register */
1206*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x18, &regs[0x18], 1);
1207*4882a593Smuzhiyun 	/* H_start */
1208*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x12, &regs[0x12], 1);
1209*4882a593Smuzhiyun 	/* V_START */
1210*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x13, &regs[0x13], 1);
1211*4882a593Smuzhiyun 	/* reset 0x17 SensorClk enable inv Clk 0x60 */
1212*4882a593Smuzhiyun 				/*fixme: ov7630 [17]=68 8f (+20 if 102)*/
1213*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x17, &regs[0x17], 1);
1214*4882a593Smuzhiyun 	/*MCKSIZE ->3 */	/*fixme: not ov7630*/
1215*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x19, &regs[0x19], 1);
1216*4882a593Smuzhiyun 	/* AE_STRX AE_STRY AE_ENDX AE_ENDY */
1217*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x1c, &regs[0x1c], 4);
1218*4882a593Smuzhiyun 	/* Enable video transfert */
1219*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x01, &regs[0x01], 1);
1220*4882a593Smuzhiyun 	/* Compression */
1221*4882a593Smuzhiyun 	reg_w(gspca_dev, 0x18, &regs[0x18], 2);
1222*4882a593Smuzhiyun 	msleep(20);
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 	sd->reg11 = -1;
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	setgain(gspca_dev);
1227*4882a593Smuzhiyun 	setbrightness(gspca_dev);
1228*4882a593Smuzhiyun 	setexposure(gspca_dev);
1229*4882a593Smuzhiyun 	setfreq(gspca_dev);
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 	sd->frames_to_drop = 0;
1232*4882a593Smuzhiyun 	sd->autogain_ignore_frames = 0;
1233*4882a593Smuzhiyun 	gspca_dev->exp_too_high_cnt = 0;
1234*4882a593Smuzhiyun 	gspca_dev->exp_too_low_cnt = 0;
1235*4882a593Smuzhiyun 	atomic_set(&sd->avg_lum, -1);
1236*4882a593Smuzhiyun 	return gspca_dev->usb_err;
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun 
sd_stopN(struct gspca_dev * gspca_dev)1239*4882a593Smuzhiyun static void sd_stopN(struct gspca_dev *gspca_dev)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun 	sd_init(gspca_dev);
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun 
find_sof(struct gspca_dev * gspca_dev,u8 * data,int len)1244*4882a593Smuzhiyun static u8* find_sof(struct gspca_dev *gspca_dev, u8 *data, int len)
1245*4882a593Smuzhiyun {
1246*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1247*4882a593Smuzhiyun 	int i, header_size = (sd->bridge == BRIDGE_103) ? 18 : 12;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	/* frames start with:
1250*4882a593Smuzhiyun 	 *	ff ff 00 c4 c4 96	synchro
1251*4882a593Smuzhiyun 	 *	00		(unknown)
1252*4882a593Smuzhiyun 	 *	xx		(frame sequence / size / compression)
1253*4882a593Smuzhiyun 	 *	(xx)		(idem - extra byte for sn9c103)
1254*4882a593Smuzhiyun 	 *	ll mm		brightness sum inside auto exposure
1255*4882a593Smuzhiyun 	 *	ll mm		brightness sum outside auto exposure
1256*4882a593Smuzhiyun 	 *	(xx xx xx xx xx)	audio values for snc103
1257*4882a593Smuzhiyun 	 */
1258*4882a593Smuzhiyun 	for (i = 0; i < len; i++) {
1259*4882a593Smuzhiyun 		switch (sd->header_read) {
1260*4882a593Smuzhiyun 		case 0:
1261*4882a593Smuzhiyun 			if (data[i] == 0xff)
1262*4882a593Smuzhiyun 				sd->header_read++;
1263*4882a593Smuzhiyun 			break;
1264*4882a593Smuzhiyun 		case 1:
1265*4882a593Smuzhiyun 			if (data[i] == 0xff)
1266*4882a593Smuzhiyun 				sd->header_read++;
1267*4882a593Smuzhiyun 			else
1268*4882a593Smuzhiyun 				sd->header_read = 0;
1269*4882a593Smuzhiyun 			break;
1270*4882a593Smuzhiyun 		case 2:
1271*4882a593Smuzhiyun 			if (data[i] == 0x00)
1272*4882a593Smuzhiyun 				sd->header_read++;
1273*4882a593Smuzhiyun 			else if (data[i] != 0xff)
1274*4882a593Smuzhiyun 				sd->header_read = 0;
1275*4882a593Smuzhiyun 			break;
1276*4882a593Smuzhiyun 		case 3:
1277*4882a593Smuzhiyun 			if (data[i] == 0xc4)
1278*4882a593Smuzhiyun 				sd->header_read++;
1279*4882a593Smuzhiyun 			else if (data[i] == 0xff)
1280*4882a593Smuzhiyun 				sd->header_read = 1;
1281*4882a593Smuzhiyun 			else
1282*4882a593Smuzhiyun 				sd->header_read = 0;
1283*4882a593Smuzhiyun 			break;
1284*4882a593Smuzhiyun 		case 4:
1285*4882a593Smuzhiyun 			if (data[i] == 0xc4)
1286*4882a593Smuzhiyun 				sd->header_read++;
1287*4882a593Smuzhiyun 			else if (data[i] == 0xff)
1288*4882a593Smuzhiyun 				sd->header_read = 1;
1289*4882a593Smuzhiyun 			else
1290*4882a593Smuzhiyun 				sd->header_read = 0;
1291*4882a593Smuzhiyun 			break;
1292*4882a593Smuzhiyun 		case 5:
1293*4882a593Smuzhiyun 			if (data[i] == 0x96)
1294*4882a593Smuzhiyun 				sd->header_read++;
1295*4882a593Smuzhiyun 			else if (data[i] == 0xff)
1296*4882a593Smuzhiyun 				sd->header_read = 1;
1297*4882a593Smuzhiyun 			else
1298*4882a593Smuzhiyun 				sd->header_read = 0;
1299*4882a593Smuzhiyun 			break;
1300*4882a593Smuzhiyun 		default:
1301*4882a593Smuzhiyun 			sd->header[sd->header_read - 6] = data[i];
1302*4882a593Smuzhiyun 			sd->header_read++;
1303*4882a593Smuzhiyun 			if (sd->header_read == header_size) {
1304*4882a593Smuzhiyun 				sd->header_read = 0;
1305*4882a593Smuzhiyun 				return data + i + 1;
1306*4882a593Smuzhiyun 			}
1307*4882a593Smuzhiyun 		}
1308*4882a593Smuzhiyun 	}
1309*4882a593Smuzhiyun 	return NULL;
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun 
sd_pkt_scan(struct gspca_dev * gspca_dev,u8 * data,int len)1312*4882a593Smuzhiyun static void sd_pkt_scan(struct gspca_dev *gspca_dev,
1313*4882a593Smuzhiyun 			u8 *data,			/* isoc packet */
1314*4882a593Smuzhiyun 			int len)			/* iso packet length */
1315*4882a593Smuzhiyun {
1316*4882a593Smuzhiyun 	int fr_h_sz = 0, lum_offset = 0, len_after_sof = 0;
1317*4882a593Smuzhiyun 	struct sd *sd = (struct sd *) gspca_dev;
1318*4882a593Smuzhiyun 	struct cam *cam = &gspca_dev->cam;
1319*4882a593Smuzhiyun 	u8 *sof;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	sof = find_sof(gspca_dev, data, len);
1322*4882a593Smuzhiyun 	if (sof) {
1323*4882a593Smuzhiyun 		if (sd->bridge == BRIDGE_103) {
1324*4882a593Smuzhiyun 			fr_h_sz = 18;
1325*4882a593Smuzhiyun 			lum_offset = 3;
1326*4882a593Smuzhiyun 		} else {
1327*4882a593Smuzhiyun 			fr_h_sz = 12;
1328*4882a593Smuzhiyun 			lum_offset = 2;
1329*4882a593Smuzhiyun 		}
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 		len_after_sof = len - (sof - data);
1332*4882a593Smuzhiyun 		len = (sof - data) - fr_h_sz;
1333*4882a593Smuzhiyun 		if (len < 0)
1334*4882a593Smuzhiyun 			len = 0;
1335*4882a593Smuzhiyun 	}
1336*4882a593Smuzhiyun 
1337*4882a593Smuzhiyun 	if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) {
1338*4882a593Smuzhiyun 		/* In raw mode we sometimes get some garbage after the frame
1339*4882a593Smuzhiyun 		   ignore this */
1340*4882a593Smuzhiyun 		int used;
1341*4882a593Smuzhiyun 		int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 		used = gspca_dev->image_len;
1344*4882a593Smuzhiyun 		if (used + len > size)
1345*4882a593Smuzhiyun 			len = size - used;
1346*4882a593Smuzhiyun 	}
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
1349*4882a593Smuzhiyun 
1350*4882a593Smuzhiyun 	if (sof) {
1351*4882a593Smuzhiyun 		int  lum = sd->header[lum_offset] +
1352*4882a593Smuzhiyun 			  (sd->header[lum_offset + 1] << 8);
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 		/* When exposure changes midway a frame we
1355*4882a593Smuzhiyun 		   get a lum of 0 in this case drop 2 frames
1356*4882a593Smuzhiyun 		   as the frames directly after an exposure
1357*4882a593Smuzhiyun 		   change have an unstable image. Sometimes lum
1358*4882a593Smuzhiyun 		   *really* is 0 (cam used in low light with
1359*4882a593Smuzhiyun 		   low exposure setting), so do not drop frames
1360*4882a593Smuzhiyun 		   if the previous lum was 0 too. */
1361*4882a593Smuzhiyun 		if (lum == 0 && sd->prev_avg_lum != 0) {
1362*4882a593Smuzhiyun 			lum = -1;
1363*4882a593Smuzhiyun 			sd->frames_to_drop = 2;
1364*4882a593Smuzhiyun 			sd->prev_avg_lum = 0;
1365*4882a593Smuzhiyun 		} else
1366*4882a593Smuzhiyun 			sd->prev_avg_lum = lum;
1367*4882a593Smuzhiyun 		atomic_set(&sd->avg_lum, lum);
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 		if (sd->frames_to_drop)
1370*4882a593Smuzhiyun 			sd->frames_to_drop--;
1371*4882a593Smuzhiyun 		else
1372*4882a593Smuzhiyun 			gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
1373*4882a593Smuzhiyun 
1374*4882a593Smuzhiyun 		gspca_frame_add(gspca_dev, FIRST_PACKET, sof, len_after_sof);
1375*4882a593Smuzhiyun 	}
1376*4882a593Smuzhiyun }
1377*4882a593Smuzhiyun 
1378*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_INPUT)
sd_int_pkt_scan(struct gspca_dev * gspca_dev,u8 * data,int len)1379*4882a593Smuzhiyun static int sd_int_pkt_scan(struct gspca_dev *gspca_dev,
1380*4882a593Smuzhiyun 			u8 *data,		/* interrupt packet data */
1381*4882a593Smuzhiyun 			int len)		/* interrupt packet length */
1382*4882a593Smuzhiyun {
1383*4882a593Smuzhiyun 	int ret = -EINVAL;
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun 	if (len == 1 && data[0] == 1) {
1386*4882a593Smuzhiyun 		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 1);
1387*4882a593Smuzhiyun 		input_sync(gspca_dev->input_dev);
1388*4882a593Smuzhiyun 		input_report_key(gspca_dev->input_dev, KEY_CAMERA, 0);
1389*4882a593Smuzhiyun 		input_sync(gspca_dev->input_dev);
1390*4882a593Smuzhiyun 		ret = 0;
1391*4882a593Smuzhiyun 	}
1392*4882a593Smuzhiyun 
1393*4882a593Smuzhiyun 	return ret;
1394*4882a593Smuzhiyun }
1395*4882a593Smuzhiyun #endif
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun /* sub-driver description */
1398*4882a593Smuzhiyun static const struct sd_desc sd_desc = {
1399*4882a593Smuzhiyun 	.name = MODULE_NAME,
1400*4882a593Smuzhiyun 	.config = sd_config,
1401*4882a593Smuzhiyun 	.init = sd_init,
1402*4882a593Smuzhiyun 	.init_controls = sd_init_controls,
1403*4882a593Smuzhiyun 	.start = sd_start,
1404*4882a593Smuzhiyun 	.stopN = sd_stopN,
1405*4882a593Smuzhiyun 	.pkt_scan = sd_pkt_scan,
1406*4882a593Smuzhiyun 	.dq_callback = do_autogain,
1407*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_INPUT)
1408*4882a593Smuzhiyun 	.int_pkt_scan = sd_int_pkt_scan,
1409*4882a593Smuzhiyun #endif
1410*4882a593Smuzhiyun };
1411*4882a593Smuzhiyun 
1412*4882a593Smuzhiyun /* -- module initialisation -- */
1413*4882a593Smuzhiyun #define SB(sensor, bridge) \
1414*4882a593Smuzhiyun 	.driver_info = (SENSOR_ ## sensor << 8) | BRIDGE_ ## bridge
1415*4882a593Smuzhiyun 
1416*4882a593Smuzhiyun 
1417*4882a593Smuzhiyun static const struct usb_device_id device_table[] = {
1418*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6001), SB(TAS5110C, 102)}, /* TAS5110C1B */
1419*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6005), SB(TAS5110C, 101)}, /* TAS5110C1B */
1420*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6007), SB(TAS5110D, 101)}, /* TAS5110D */
1421*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6009), SB(PAS106, 101)},
1422*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x600d), SB(PAS106, 101)},
1423*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6011), SB(OV6650, 101)},
1424*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6019), SB(OV7630, 101)},
1425*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6024), SB(TAS5130CXX, 102)},
1426*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6025), SB(TAS5130CXX, 102)},
1427*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6027), SB(OV7630, 101)}, /* Genius Eye 310 */
1428*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6028), SB(PAS202, 102)},
1429*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6029), SB(PAS106, 102)},
1430*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x602a), SB(HV7131D, 102)},
1431*4882a593Smuzhiyun 	/* {USB_DEVICE(0x0c45, 0x602b), SB(MI0343, 102)}, */
1432*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x602c), SB(OV7630, 102)},
1433*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x602d), SB(HV7131R, 102)},
1434*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x602e), SB(OV7630, 102)},
1435*4882a593Smuzhiyun 	/* {USB_DEVICE(0x0c45, 0x6030), SB(MI03XX, 102)}, */ /* MI0343 MI0360 MI0330 */
1436*4882a593Smuzhiyun 	/* {USB_DEVICE(0x0c45, 0x6082), SB(MI03XX, 103)}, */ /* MI0343 MI0360 */
1437*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x6083), SB(HV7131D, 103)},
1438*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x608c), SB(HV7131R, 103)},
1439*4882a593Smuzhiyun 	/* {USB_DEVICE(0x0c45, 0x608e), SB(CISVF10, 103)}, */
1440*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x608f), SB(OV7630, 103)},
1441*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x60a8), SB(PAS106, 103)},
1442*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x60aa), SB(TAS5130CXX, 103)},
1443*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x60af), SB(PAS202, 103)},
1444*4882a593Smuzhiyun 	{USB_DEVICE(0x0c45, 0x60b0), SB(OV7630, 103)},
1445*4882a593Smuzhiyun 	{}
1446*4882a593Smuzhiyun };
1447*4882a593Smuzhiyun MODULE_DEVICE_TABLE(usb, device_table);
1448*4882a593Smuzhiyun 
1449*4882a593Smuzhiyun /* -- device connect -- */
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)1450*4882a593Smuzhiyun static int sd_probe(struct usb_interface *intf,
1451*4882a593Smuzhiyun 			const struct usb_device_id *id)
1452*4882a593Smuzhiyun {
1453*4882a593Smuzhiyun 	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
1454*4882a593Smuzhiyun 				THIS_MODULE);
1455*4882a593Smuzhiyun }
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun static struct usb_driver sd_driver = {
1458*4882a593Smuzhiyun 	.name = MODULE_NAME,
1459*4882a593Smuzhiyun 	.id_table = device_table,
1460*4882a593Smuzhiyun 	.probe = sd_probe,
1461*4882a593Smuzhiyun 	.disconnect = gspca_disconnect,
1462*4882a593Smuzhiyun #ifdef CONFIG_PM
1463*4882a593Smuzhiyun 	.suspend = gspca_suspend,
1464*4882a593Smuzhiyun 	.resume = gspca_resume,
1465*4882a593Smuzhiyun 	.reset_resume = gspca_resume,
1466*4882a593Smuzhiyun #endif
1467*4882a593Smuzhiyun };
1468*4882a593Smuzhiyun 
1469*4882a593Smuzhiyun module_usb_driver(sd_driver);
1470