1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
4 * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
5 * Copyright (c) 2002, 2003 Tuukka Toivonen
6 * Copyright (c) 2008 Erik Andrén
8 * P/N 861037: Sensor HDCS1000 ASIC STV0600
9 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
10 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
11 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
12 * P/N 861075-0040: Sensor HDCS1000 ASIC
13 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
14 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
18 * The spec file for the PB-0100 suggests the following for best quality
19 * images after the sensor has been reset :
21 * PB_ADCGAINL = R60 = 0x03 (3 dec) : sets low reference of ADC
22 to produce good black level
23 * PB_PREADCTRL = R32 = 0x1400 (5120 dec) : Enables global gain changes
25 * PB_ADCMINGAIN = R52 = 0x10 (16 dec) : Sets the minimum gain for
27 * PB_ADCGLOBALGAIN = R53 = 0x10 (16 dec) : Sets the global gain
28 * PB_EXPGAIN = R14 = 0x11 (17 dec) : Sets the auto-exposure value
29 * PB_UPDATEINT = R23 = 0x02 (2 dec) : Sets the speed on
31 * PB_CFILLIN = R5 = 0x0E (14 dec) : Sets the frame rate
34 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36 #include "stv06xx_pb0100.h"
39 struct { /* one big happy control cluster... */
40 struct v4l2_ctrl
*autogain
;
41 struct v4l2_ctrl
*gain
;
42 struct v4l2_ctrl
*exposure
;
43 struct v4l2_ctrl
*red
;
44 struct v4l2_ctrl
*blue
;
45 struct v4l2_ctrl
*natural
;
47 struct v4l2_ctrl
*target
;
50 static struct v4l2_pix_format pb0100_mode
[] = {
51 /* low res / subsample modes disabled as they are only half res horizontal,
52 halving the vertical resolution does not seem to work */
58 .sizeimage
= 320 * 240,
60 .colorspace
= V4L2_COLORSPACE_SRGB
,
61 .priv
= PB0100_CROP_TO_VGA
68 .sizeimage
= 352 * 288,
70 .colorspace
= V4L2_COLORSPACE_SRGB
,
75 static int pb0100_s_ctrl(struct v4l2_ctrl
*ctrl
)
77 struct gspca_dev
*gspca_dev
=
78 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
79 struct sd
*sd
= (struct sd
*)gspca_dev
;
80 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
84 case V4L2_CID_AUTOGAIN
:
85 err
= pb0100_set_autogain(gspca_dev
, ctrl
->val
);
90 err
= pb0100_set_gain(gspca_dev
, ctrls
->gain
->val
);
93 err
= pb0100_set_exposure(gspca_dev
, ctrls
->exposure
->val
);
95 case V4L2_CTRL_CLASS_USER
+ 0x1001:
96 err
= pb0100_set_autogain_target(gspca_dev
, ctrl
->val
);
102 static const struct v4l2_ctrl_ops pb0100_ctrl_ops
= {
103 .s_ctrl
= pb0100_s_ctrl
,
106 static int pb0100_init_controls(struct sd
*sd
)
108 struct v4l2_ctrl_handler
*hdl
= &sd
->gspca_dev
.ctrl_handler
;
109 struct pb0100_ctrls
*ctrls
;
110 static const struct v4l2_ctrl_config autogain_target
= {
111 .ops
= &pb0100_ctrl_ops
,
112 .id
= V4L2_CTRL_CLASS_USER
+ 0x1000,
113 .type
= V4L2_CTRL_TYPE_INTEGER
,
114 .name
= "Automatic Gain Target",
119 static const struct v4l2_ctrl_config natural_light
= {
120 .ops
= &pb0100_ctrl_ops
,
121 .id
= V4L2_CTRL_CLASS_USER
+ 0x1001,
122 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
123 .name
= "Natural Light Source",
129 ctrls
= kzalloc(sizeof(*ctrls
), GFP_KERNEL
);
133 v4l2_ctrl_handler_init(hdl
, 6);
134 ctrls
->autogain
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
135 V4L2_CID_AUTOGAIN
, 0, 1, 1, 1);
136 ctrls
->exposure
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
137 V4L2_CID_EXPOSURE
, 0, 511, 1, 12);
138 ctrls
->gain
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
139 V4L2_CID_GAIN
, 0, 255, 1, 128);
140 ctrls
->red
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
141 V4L2_CID_RED_BALANCE
, -255, 255, 1, 0);
142 ctrls
->blue
= v4l2_ctrl_new_std(hdl
, &pb0100_ctrl_ops
,
143 V4L2_CID_BLUE_BALANCE
, -255, 255, 1, 0);
144 ctrls
->natural
= v4l2_ctrl_new_custom(hdl
, &natural_light
, NULL
);
145 ctrls
->target
= v4l2_ctrl_new_custom(hdl
, &autogain_target
, NULL
);
150 sd
->sensor_priv
= ctrls
;
151 v4l2_ctrl_auto_cluster(5, &ctrls
->autogain
, 0, false);
155 static int pb0100_probe(struct sd
*sd
)
160 err
= stv06xx_read_sensor(sd
, PB_IDENT
, &sensor
);
164 if ((sensor
>> 8) != 0x64)
167 pr_info("Photobit pb0100 sensor detected\n");
169 sd
->gspca_dev
.cam
.cam_mode
= pb0100_mode
;
170 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(pb0100_mode
);
175 static int pb0100_start(struct sd
*sd
)
177 int err
, packet_size
, max_packet_size
;
178 struct usb_host_interface
*alt
;
179 struct usb_interface
*intf
;
180 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
181 struct cam
*cam
= &sd
->gspca_dev
.cam
;
182 u32 mode
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].priv
;
184 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
185 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
189 if (alt
->desc
.bNumEndpoints
< 1)
192 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
194 /* If we don't have enough bandwidth use a lower framerate */
195 max_packet_size
= sd
->sensor
->max_packet_size
[sd
->gspca_dev
.curr_mode
];
196 if (packet_size
< max_packet_size
)
197 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(4)|BIT(3)|BIT(1));
199 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(5)|BIT(3)|BIT(1));
201 /* Setup sensor window */
202 if (mode
& PB0100_CROP_TO_VGA
) {
203 stv06xx_write_sensor(sd
, PB_RSTART
, 30);
204 stv06xx_write_sensor(sd
, PB_CSTART
, 20);
205 stv06xx_write_sensor(sd
, PB_RWSIZE
, 240 - 1);
206 stv06xx_write_sensor(sd
, PB_CWSIZE
, 320 - 1);
208 stv06xx_write_sensor(sd
, PB_RSTART
, 8);
209 stv06xx_write_sensor(sd
, PB_CSTART
, 4);
210 stv06xx_write_sensor(sd
, PB_RWSIZE
, 288 - 1);
211 stv06xx_write_sensor(sd
, PB_CWSIZE
, 352 - 1);
214 if (mode
& PB0100_SUBSAMPLE
) {
215 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x02); /* Wrong, FIXME */
216 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x06);
218 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x10);
220 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x01);
221 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x0a);
222 /* larger -> slower */
223 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x20);
226 err
= stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3)|BIT(1));
227 gspca_dbg(gspca_dev
, D_STREAM
, "Started stream, status: %d\n", err
);
229 return (err
< 0) ? err
: 0;
232 static int pb0100_stop(struct sd
*sd
)
234 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
237 err
= stv06xx_write_sensor(sd
, PB_ABORTFRAME
, 1);
242 /* Set bit 1 to zero */
243 err
= stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3));
245 gspca_dbg(gspca_dev
, D_STREAM
, "Halting stream\n");
247 return (err
< 0) ? err
: 0;
250 /* FIXME: Sort the init commands out and put them into tables,
251 this is only for getting the camera to work */
252 /* FIXME: No error handling for now,
253 add this once the init has been converted to proper tables */
254 static int pb0100_init(struct sd
*sd
)
256 stv06xx_write_bridge(sd
, STV_REG00
, 1);
257 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0);
260 stv06xx_write_sensor(sd
, PB_RESET
, 1);
261 stv06xx_write_sensor(sd
, PB_RESET
, 0);
264 stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3));
267 stv06xx_write_sensor(sd
, PB_PREADCTRL
, BIT(12)|BIT(10)|BIT(6));
268 stv06xx_write_sensor(sd
, PB_ADCGLOBALGAIN
, 12);
270 /* Set up auto-exposure */
271 /* ADC VREF_HI new setting for a transition
272 from the Expose1 to the Expose2 setting */
273 stv06xx_write_sensor(sd
, PB_R28
, 12);
274 /* gain max for autoexposure */
275 stv06xx_write_sensor(sd
, PB_ADCMAXGAIN
, 180);
276 /* gain min for autoexposure */
277 stv06xx_write_sensor(sd
, PB_ADCMINGAIN
, 12);
278 /* Maximum frame integration time (programmed into R8)
279 allowed for auto-exposure routine */
280 stv06xx_write_sensor(sd
, PB_R54
, 3);
281 /* Minimum frame integration time (programmed into R8)
282 allowed for auto-exposure routine */
283 stv06xx_write_sensor(sd
, PB_R55
, 0);
284 stv06xx_write_sensor(sd
, PB_UPDATEINT
, 1);
285 /* R15 Expose0 (maximum that auto-exposure may use) */
286 stv06xx_write_sensor(sd
, PB_R15
, 800);
287 /* R17 Expose2 (minimum that auto-exposure may use) */
288 stv06xx_write_sensor(sd
, PB_R17
, 10);
290 stv06xx_write_sensor(sd
, PB_EXPGAIN
, 0);
293 stv06xx_write_sensor(sd
, PB_VOFFSET
, 0);
295 stv06xx_write_sensor(sd
, PB_ADCGAINH
, 11);
296 /* Set black level (important!) */
297 stv06xx_write_sensor(sd
, PB_ADCGAINL
, 0);
300 stv06xx_write_bridge(sd
, STV_REG00
, 0x11);
301 stv06xx_write_bridge(sd
, STV_REG03
, 0x45);
302 stv06xx_write_bridge(sd
, STV_REG04
, 0x07);
304 /* Scan/timing for the sensor */
305 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(4)|BIT(3)|BIT(1));
306 stv06xx_write_sensor(sd
, PB_CFILLIN
, 14);
307 stv06xx_write_sensor(sd
, PB_VBL
, 0);
308 stv06xx_write_sensor(sd
, PB_FINTTIME
, 0);
309 stv06xx_write_sensor(sd
, PB_RINTTIME
, 123);
311 stv06xx_write_bridge(sd
, STV_REG01
, 0xc2);
312 stv06xx_write_bridge(sd
, STV_REG02
, 0xb0);
316 static int pb0100_dump(struct sd
*sd
)
321 static int pb0100_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
324 struct sd
*sd
= (struct sd
*) gspca_dev
;
325 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
327 err
= stv06xx_write_sensor(sd
, PB_G1GAIN
, val
);
329 err
= stv06xx_write_sensor(sd
, PB_G2GAIN
, val
);
330 gspca_dbg(gspca_dev
, D_CONF
, "Set green gain to %d, status: %d\n",
334 err
= pb0100_set_red_balance(gspca_dev
, ctrls
->red
->val
);
336 err
= pb0100_set_blue_balance(gspca_dev
, ctrls
->blue
->val
);
341 static int pb0100_set_red_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
344 struct sd
*sd
= (struct sd
*) gspca_dev
;
345 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
347 val
+= ctrls
->gain
->val
;
353 err
= stv06xx_write_sensor(sd
, PB_RGAIN
, val
);
354 gspca_dbg(gspca_dev
, D_CONF
, "Set red gain to %d, status: %d\n",
360 static int pb0100_set_blue_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
363 struct sd
*sd
= (struct sd
*) gspca_dev
;
364 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
366 val
+= ctrls
->gain
->val
;
372 err
= stv06xx_write_sensor(sd
, PB_BGAIN
, val
);
373 gspca_dbg(gspca_dev
, D_CONF
, "Set blue gain to %d, status: %d\n",
379 static int pb0100_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
381 struct sd
*sd
= (struct sd
*) gspca_dev
;
384 err
= stv06xx_write_sensor(sd
, PB_RINTTIME
, val
);
385 gspca_dbg(gspca_dev
, D_CONF
, "Set exposure to %d, status: %d\n",
391 static int pb0100_set_autogain(struct gspca_dev
*gspca_dev
, __s32 val
)
394 struct sd
*sd
= (struct sd
*) gspca_dev
;
395 struct pb0100_ctrls
*ctrls
= sd
->sensor_priv
;
398 if (ctrls
->natural
->val
)
399 val
= BIT(6)|BIT(4)|BIT(0);
405 err
= stv06xx_write_sensor(sd
, PB_EXPGAIN
, val
);
406 gspca_dbg(gspca_dev
, D_CONF
, "Set autogain to %d (natural: %d), status: %d\n",
407 val
, ctrls
->natural
->val
, err
);
412 static int pb0100_set_autogain_target(struct gspca_dev
*gspca_dev
, __s32 val
)
414 int err
, totalpixels
, brightpixels
, darkpixels
;
415 struct sd
*sd
= (struct sd
*) gspca_dev
;
417 /* Number of pixels counted by the sensor when subsampling the pixels.
418 * Slightly larger than the real value to avoid oscillation */
419 totalpixels
= gspca_dev
->pixfmt
.width
* gspca_dev
->pixfmt
.height
;
420 totalpixels
= totalpixels
/(8*8) + totalpixels
/(64*64);
422 brightpixels
= (totalpixels
* val
) >> 8;
423 darkpixels
= totalpixels
- brightpixels
;
424 err
= stv06xx_write_sensor(sd
, PB_R21
, brightpixels
);
426 err
= stv06xx_write_sensor(sd
, PB_R22
, darkpixels
);
428 gspca_dbg(gspca_dev
, D_CONF
, "Set autogain target to %d, status: %d\n",