2 * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
3 * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
4 * Copyright (c) 2002, 2003 Tuukka Toivonen
5 * Copyright (c) 2008 Erik Andrén
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * P/N 861037: Sensor HDCS1000 ASIC STV0600
18 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
19 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
20 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
21 * P/N 861075-0040: Sensor HDCS1000 ASIC
22 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
23 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
27 * The spec file for the PB-0100 suggests the following for best quality
28 * images after the sensor has been reset :
30 * PB_ADCGAINL = R60 = 0x03 (3 dec) : sets low reference of ADC
31 to produce good black level
32 * PB_PREADCTRL = R32 = 0x1400 (5120 dec) : Enables global gain changes
34 * PB_ADCMINGAIN = R52 = 0x10 (16 dec) : Sets the minimum gain for
36 * PB_ADCGLOBALGAIN = R53 = 0x10 (16 dec) : Sets the global gain
37 * PB_EXPGAIN = R14 = 0x11 (17 dec) : Sets the auto-exposure value
38 * PB_UPDATEINT = R23 = 0x02 (2 dec) : Sets the speed on
40 * PB_CFILLIN = R5 = 0x0E (14 dec) : Sets the frame rate
43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #include "stv06xx_pb0100.h"
48 struct { /* one big happy control cluster... */
49 struct v4l2_ctrl
*autogain
;
50 struct v4l2_ctrl
*gain
;
51 struct v4l2_ctrl
*exposure
;
52 struct v4l2_ctrl
*red
;
53 struct v4l2_ctrl
*blue
;
54 struct v4l2_ctrl
*natural
;
56 struct v4l2_ctrl
*target
;
59 static struct v4l2_pix_format pb0100_mode
[] = {
60 /* low res / subsample modes disabled as they are only half res horizontal,
61 halving the vertical resolution does not seem to work */
67 .sizeimage
= 320 * 240,
69 .colorspace
= V4L2_COLORSPACE_SRGB
,
70 .priv
= PB0100_CROP_TO_VGA
77 .sizeimage
= 352 * 288,
79 .colorspace
= V4L2_COLORSPACE_SRGB
,
84 static int pb0100_s_ctrl(struct v4l2_ctrl
*ctrl
)
86 struct gspca_dev
*gspca_dev
=
87 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
88 struct sd
*sd
= (struct sd
*)gspca_dev
;
89 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
93 case V4L2_CID_AUTOGAIN
:
94 err
= pb0100_set_autogain(gspca_dev
, ctrl
->val
);
99 err
= pb0100_set_gain(gspca_dev
, ctrls
->gain
->val
);
102 err
= pb0100_set_exposure(gspca_dev
, ctrls
->exposure
->val
);
104 case V4L2_CTRL_CLASS_USER
+ 0x1001:
105 err
= pb0100_set_autogain_target(gspca_dev
, ctrl
->val
);
111 static const struct v4l2_ctrl_ops pb0100_ctrl_ops
= {
112 .s_ctrl
= pb0100_s_ctrl
,
115 static int pb0100_init_controls(struct sd
*sd
)
117 struct v4l2_ctrl_handler
*hdl
= &sd
->gspca_dev
.ctrl_handler
;
118 struct pb0100_ctrls
*ctrls
;
119 static const struct v4l2_ctrl_config autogain_target
= {
120 .ops
= &pb0100_ctrl_ops
,
121 .id
= V4L2_CTRL_CLASS_USER
+ 0x1000,
122 .type
= V4L2_CTRL_TYPE_INTEGER
,
123 .name
= "Automatic Gain Target",
128 static const struct v4l2_ctrl_config natural_light
= {
129 .ops
= &pb0100_ctrl_ops
,
130 .id
= V4L2_CTRL_CLASS_USER
+ 0x1001,
131 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
132 .name
= "Natural Light Source",
138 ctrls
= kzalloc(sizeof(*ctrls
), GFP_KERNEL
);
142 v4l2_ctrl_handler_init(hdl
, 6);
143 ctrls
->autogain
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
144 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
145 ctrls
->exposure
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
146 V4L2_CID_EXPOSURE
, 0, 511, 1, 12);
147 ctrls
->gain
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
148 V4L2_CID_GAIN
, 0, 255, 1, 128);
149 ctrls
->red
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
150 V4L2_CID_RED_BALANCE
, -255, 255, 1, 0);
151 ctrls
->blue
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
152 V4L2_CID_BLUE_BALANCE
, -255, 255, 1, 0);
153 ctrls
->natural
= v4l2_ctrl_new_custom(hdl
, &natural_light
, NULL
);
154 ctrls
->target
= v4l2_ctrl_new_custom(hdl
, &autogain_target
, NULL
);
159 sd
->sensor_priv
= ctrls
;
160 v4l2_ctrl_auto_cluster(5, &ctrls
->autogain
, 0, false);
164 static int pb0100_probe(struct sd
*sd
)
169 err
= stv06xx_read_sensor(sd
, PB_IDENT
, &sensor
);
173 if ((sensor
>> 8) != 0x64)
176 pr_info("Photobit pb0100 sensor detected\n");
178 sd
->gspca_dev
.cam
.cam_mode
= pb0100_mode
;
179 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(pb0100_mode
);
184 static int pb0100_start(struct sd
*sd
)
186 int err
, packet_size
, max_packet_size
;
187 struct usb_host_interface
*alt
;
188 struct usb_interface
*intf
;
189 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
190 struct cam
*cam
= &sd
->gspca_dev
.cam
;
191 u32 mode
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].priv
;
193 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
194 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
197 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
199 /* If we don't have enough bandwidth use a lower framerate */
200 max_packet_size
= sd
->sensor
->max_packet_size
[sd
->gspca_dev
.curr_mode
];
201 if (packet_size
< max_packet_size
)
202 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(4)|BIT(3)|BIT(1));
204 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(5)|BIT(3)|BIT(1));
206 /* Setup sensor window */
207 if (mode
& PB0100_CROP_TO_VGA
) {
208 stv06xx_write_sensor(sd
, PB_RSTART
, 30);
209 stv06xx_write_sensor(sd
, PB_CSTART
, 20);
210 stv06xx_write_sensor(sd
, PB_RWSIZE
, 240 - 1);
211 stv06xx_write_sensor(sd
, PB_CWSIZE
, 320 - 1);
213 stv06xx_write_sensor(sd
, PB_RSTART
, 8);
214 stv06xx_write_sensor(sd
, PB_CSTART
, 4);
215 stv06xx_write_sensor(sd
, PB_RWSIZE
, 288 - 1);
216 stv06xx_write_sensor(sd
, PB_CWSIZE
, 352 - 1);
219 if (mode
& PB0100_SUBSAMPLE
) {
220 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x02); /* Wrong, FIXME */
221 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x06);
223 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x10);
225 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x01);
226 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x0a);
227 /* larger -> slower */
228 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x20);
231 err
= stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3)|BIT(1));
232 gspca_dbg(gspca_dev
, D_STREAM
, "Started stream, status: %d\n", err
);
234 return (err
< 0) ? err
: 0;
237 static int pb0100_stop(struct sd
*sd
)
239 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
242 err
= stv06xx_write_sensor(sd
, PB_ABORTFRAME
, 1);
247 /* Set bit 1 to zero */
248 err
= stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3));
250 gspca_dbg(gspca_dev
, D_STREAM
, "Halting stream\n");
252 return (err
< 0) ? err
: 0;
255 /* FIXME: Sort the init commands out and put them into tables,
256 this is only for getting the camera to work */
257 /* FIXME: No error handling for now,
258 add this once the init has been converted to proper tables */
259 static int pb0100_init(struct sd
*sd
)
261 stv06xx_write_bridge(sd
, STV_REG00
, 1);
262 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0);
265 stv06xx_write_sensor(sd
, PB_RESET
, 1);
266 stv06xx_write_sensor(sd
, PB_RESET
, 0);
269 stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3));
272 stv06xx_write_sensor(sd
, PB_PREADCTRL
, BIT(12)|BIT(10)|BIT(6));
273 stv06xx_write_sensor(sd
, PB_ADCGLOBALGAIN
, 12);
275 /* Set up auto-exposure */
276 /* ADC VREF_HI new setting for a transition
277 from the Expose1 to the Expose2 setting */
278 stv06xx_write_sensor(sd
, PB_R28
, 12);
279 /* gain max for autoexposure */
280 stv06xx_write_sensor(sd
, PB_ADCMAXGAIN
, 180);
281 /* gain min for autoexposure */
282 stv06xx_write_sensor(sd
, PB_ADCMINGAIN
, 12);
283 /* Maximum frame integration time (programmed into R8)
284 allowed for auto-exposure routine */
285 stv06xx_write_sensor(sd
, PB_R54
, 3);
286 /* Minimum frame integration time (programmed into R8)
287 allowed for auto-exposure routine */
288 stv06xx_write_sensor(sd
, PB_R55
, 0);
289 stv06xx_write_sensor(sd
, PB_UPDATEINT
, 1);
290 /* R15 Expose0 (maximum that auto-exposure may use) */
291 stv06xx_write_sensor(sd
, PB_R15
, 800);
292 /* R17 Expose2 (minimum that auto-exposure may use) */
293 stv06xx_write_sensor(sd
, PB_R17
, 10);
295 stv06xx_write_sensor(sd
, PB_EXPGAIN
, 0);
298 stv06xx_write_sensor(sd
, PB_VOFFSET
, 0);
300 stv06xx_write_sensor(sd
, PB_ADCGAINH
, 11);
301 /* Set black level (important!) */
302 stv06xx_write_sensor(sd
, PB_ADCGAINL
, 0);
305 stv06xx_write_bridge(sd
, STV_REG00
, 0x11);
306 stv06xx_write_bridge(sd
, STV_REG03
, 0x45);
307 stv06xx_write_bridge(sd
, STV_REG04
, 0x07);
309 /* Scan/timing for the sensor */
310 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(4)|BIT(3)|BIT(1));
311 stv06xx_write_sensor(sd
, PB_CFILLIN
, 14);
312 stv06xx_write_sensor(sd
, PB_VBL
, 0);
313 stv06xx_write_sensor(sd
, PB_FINTTIME
, 0);
314 stv06xx_write_sensor(sd
, PB_RINTTIME
, 123);
316 stv06xx_write_bridge(sd
, STV_REG01
, 0xc2);
317 stv06xx_write_bridge(sd
, STV_REG02
, 0xb0);
321 static int pb0100_dump(struct sd
*sd
)
326 static int pb0100_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
329 struct sd
*sd
= (struct sd
*) gspca_dev
;
330 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
332 err
= stv06xx_write_sensor(sd
, PB_G1GAIN
, val
);
334 err
= stv06xx_write_sensor(sd
, PB_G2GAIN
, val
);
335 gspca_dbg(gspca_dev
, D_CONF
, "Set green gain to %d, status: %d\n",
339 err
= pb0100_set_red_balance(gspca_dev
, ctrls
->red
->val
);
341 err
= pb0100_set_blue_balance(gspca_dev
, ctrls
->blue
->val
);
346 static int pb0100_set_red_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
349 struct sd
*sd
= (struct sd
*) gspca_dev
;
350 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
352 val
+= ctrls
->gain
->val
;
358 err
= stv06xx_write_sensor(sd
, PB_RGAIN
, val
);
359 gspca_dbg(gspca_dev
, D_CONF
, "Set red gain to %d, status: %d\n",
365 static int pb0100_set_blue_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
368 struct sd
*sd
= (struct sd
*) gspca_dev
;
369 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
371 val
+= ctrls
->gain
->val
;
377 err
= stv06xx_write_sensor(sd
, PB_BGAIN
, val
);
378 gspca_dbg(gspca_dev
, D_CONF
, "Set blue gain to %d, status: %d\n",
384 static int pb0100_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
386 struct sd
*sd
= (struct sd
*) gspca_dev
;
389 err
= stv06xx_write_sensor(sd
, PB_RINTTIME
, val
);
390 gspca_dbg(gspca_dev
, D_CONF
, "Set exposure to %d, status: %d\n",
396 static int pb0100_set_autogain(struct gspca_dev
*gspca_dev
, __s32 val
)
399 struct sd
*sd
= (struct sd
*) gspca_dev
;
400 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
403 if (ctrls
->natural
->val
)
404 val
= BIT(6)|BIT(4)|BIT(0);
410 err
= stv06xx_write_sensor(sd
, PB_EXPGAIN
, val
);
411 gspca_dbg(gspca_dev
, D_CONF
, "Set autogain to %d (natural: %d), status: %d\n",
412 val
, ctrls
->natural
->val
, err
);
417 static int pb0100_set_autogain_target(struct gspca_dev
*gspca_dev
, __s32 val
)
419 int err
, totalpixels
, brightpixels
, darkpixels
;
420 struct sd
*sd
= (struct sd
*) gspca_dev
;
422 /* Number of pixels counted by the sensor when subsampling the pixels.
423 * Slightly larger than the real value to avoid oscillation */
424 totalpixels
= gspca_dev
->pixfmt
.width
* gspca_dev
->pixfmt
.height
;
425 totalpixels
= totalpixels
/(8*8) + totalpixels
/(64*64);
427 brightpixels
= (totalpixels
* val
) >> 8;
428 darkpixels
= totalpixels
- brightpixels
;
429 err
= stv06xx_write_sensor(sd
, PB_R21
, brightpixels
);
431 err
= stv06xx_write_sensor(sd
, PB_R22
, darkpixels
);
433 gspca_dbg(gspca_dev
, D_CONF
, "Set autogain target to %d, status: %d\n",