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 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * P/N 861037: Sensor HDCS1000 ASIC STV0600
22 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
23 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
24 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
25 * P/N 861075-0040: Sensor HDCS1000 ASIC
26 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
27 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
31 * The spec file for the PB-0100 suggests the following for best quality
32 * images after the sensor has been reset :
34 * PB_ADCGAINL = R60 = 0x03 (3 dec) : sets low reference of ADC
35 to produce good black level
36 * PB_PREADCTRL = R32 = 0x1400 (5120 dec) : Enables global gain changes
38 * PB_ADCMINGAIN = R52 = 0x10 (16 dec) : Sets the minimum gain for
40 * PB_ADCGLOBALGAIN = R53 = 0x10 (16 dec) : Sets the global gain
41 * PB_EXPGAIN = R14 = 0x11 (17 dec) : Sets the auto-exposure value
42 * PB_UPDATEINT = R23 = 0x02 (2 dec) : Sets the speed on
44 * PB_CFILLIN = R5 = 0x0E (14 dec) : Sets the frame rate
47 #include "stv06xx_pb0100.h"
49 static const struct ctrl pb0100_ctrl
[] = {
54 .type
= V4L2_CTRL_TYPE_INTEGER
,
61 .set
= pb0100_set_gain
,
62 .get
= pb0100_get_gain
64 #define RED_BALANCE_IDX 1
67 .id
= V4L2_CID_RED_BALANCE
,
68 .type
= V4L2_CTRL_TYPE_INTEGER
,
69 .name
= "Red Balance",
75 .set
= pb0100_set_red_balance
,
76 .get
= pb0100_get_red_balance
78 #define BLUE_BALANCE_IDX 2
81 .id
= V4L2_CID_BLUE_BALANCE
,
82 .type
= V4L2_CTRL_TYPE_INTEGER
,
83 .name
= "Blue Balance",
89 .set
= pb0100_set_blue_balance
,
90 .get
= pb0100_get_blue_balance
92 #define EXPOSURE_IDX 3
95 .id
= V4L2_CID_EXPOSURE
,
96 .type
= V4L2_CTRL_TYPE_INTEGER
,
103 .set
= pb0100_set_exposure
,
104 .get
= pb0100_get_exposure
106 #define AUTOGAIN_IDX 4
109 .id
= V4L2_CID_AUTOGAIN
,
110 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
111 .name
= "Automatic Gain and Exposure",
117 .set
= pb0100_set_autogain
,
118 .get
= pb0100_get_autogain
120 #define AUTOGAIN_TARGET_IDX 5
123 .id
= V4L2_CTRL_CLASS_USER
+ 0x1000,
124 .type
= V4L2_CTRL_TYPE_INTEGER
,
125 .name
= "Automatic Gain Target",
131 .set
= pb0100_set_autogain_target
,
132 .get
= pb0100_get_autogain_target
134 #define NATURAL_IDX 6
137 .id
= V4L2_CTRL_CLASS_USER
+ 0x1001,
138 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
139 .name
= "Natural Light Source",
145 .set
= pb0100_set_natural
,
146 .get
= pb0100_get_natural
150 static struct v4l2_pix_format pb0100_mode
[] = {
151 /* low res / subsample modes disabled as they are only half res horizontal,
152 halving the vertical resolution does not seem to work */
158 .sizeimage
= 320 * 240,
160 .colorspace
= V4L2_COLORSPACE_SRGB
,
161 .priv
= PB0100_CROP_TO_VGA
168 .sizeimage
= 352 * 288,
170 .colorspace
= V4L2_COLORSPACE_SRGB
,
175 static int pb0100_probe(struct sd
*sd
)
179 s32
*sensor_settings
;
181 err
= stv06xx_read_sensor(sd
, PB_IDENT
, &sensor
);
186 if ((sensor
>> 8) == 0x64) {
187 sensor_settings
= kmalloc(
188 ARRAY_SIZE(pb0100_ctrl
) * sizeof(s32
),
190 if (!sensor_settings
)
193 info("Photobit pb0100 sensor detected");
195 sd
->gspca_dev
.cam
.cam_mode
= pb0100_mode
;
196 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(pb0100_mode
);
197 sd
->desc
.ctrls
= pb0100_ctrl
;
198 sd
->desc
.nctrls
= ARRAY_SIZE(pb0100_ctrl
);
199 for (i
= 0; i
< sd
->desc
.nctrls
; i
++)
200 sensor_settings
[i
] = pb0100_ctrl
[i
].qctrl
.default_value
;
201 sd
->sensor_priv
= sensor_settings
;
209 static int pb0100_start(struct sd
*sd
)
211 int err
, packet_size
, max_packet_size
;
212 struct usb_host_interface
*alt
;
213 struct usb_interface
*intf
;
214 struct cam
*cam
= &sd
->gspca_dev
.cam
;
215 s32
*sensor_settings
= sd
->sensor_priv
;
216 u32 mode
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].priv
;
218 intf
= usb_ifnum_to_if(sd
->gspca_dev
.dev
, sd
->gspca_dev
.iface
);
219 alt
= usb_altnum_to_altsetting(intf
, sd
->gspca_dev
.alt
);
220 packet_size
= le16_to_cpu(alt
->endpoint
[0].desc
.wMaxPacketSize
);
222 /* If we don't have enough bandwidth use a lower framerate */
223 max_packet_size
= sd
->sensor
->max_packet_size
[sd
->gspca_dev
.curr_mode
];
224 if (packet_size
< max_packet_size
)
225 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(4)|BIT(3)|BIT(1));
227 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(5)|BIT(3)|BIT(1));
229 /* Setup sensor window */
230 if (mode
& PB0100_CROP_TO_VGA
) {
231 stv06xx_write_sensor(sd
, PB_RSTART
, 30);
232 stv06xx_write_sensor(sd
, PB_CSTART
, 20);
233 stv06xx_write_sensor(sd
, PB_RWSIZE
, 240 - 1);
234 stv06xx_write_sensor(sd
, PB_CWSIZE
, 320 - 1);
236 stv06xx_write_sensor(sd
, PB_RSTART
, 8);
237 stv06xx_write_sensor(sd
, PB_CSTART
, 4);
238 stv06xx_write_sensor(sd
, PB_RWSIZE
, 288 - 1);
239 stv06xx_write_sensor(sd
, PB_CWSIZE
, 352 - 1);
242 if (mode
& PB0100_SUBSAMPLE
) {
243 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x02); /* Wrong, FIXME */
244 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x06);
246 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x10);
248 stv06xx_write_bridge(sd
, STV_Y_CTRL
, 0x01);
249 stv06xx_write_bridge(sd
, STV_X_CTRL
, 0x0a);
250 /* larger -> slower */
251 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0x20);
254 /* set_gain also sets red and blue balance */
255 pb0100_set_gain(&sd
->gspca_dev
, sensor_settings
[GAIN_IDX
]);
256 pb0100_set_exposure(&sd
->gspca_dev
, sensor_settings
[EXPOSURE_IDX
]);
257 pb0100_set_autogain_target(&sd
->gspca_dev
,
258 sensor_settings
[AUTOGAIN_TARGET_IDX
]);
259 pb0100_set_autogain(&sd
->gspca_dev
, sensor_settings
[AUTOGAIN_IDX
]);
261 err
= stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3)|BIT(1));
262 PDEBUG(D_STREAM
, "Started stream, status: %d", err
);
264 return (err
< 0) ? err
: 0;
267 static int pb0100_stop(struct sd
*sd
)
271 err
= stv06xx_write_sensor(sd
, PB_ABORTFRAME
, 1);
276 /* Set bit 1 to zero */
277 err
= stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3));
279 PDEBUG(D_STREAM
, "Halting stream");
281 return (err
< 0) ? err
: 0;
284 static void pb0100_disconnect(struct sd
*sd
)
287 kfree(sd
->sensor_priv
);
290 /* FIXME: Sort the init commands out and put them into tables,
291 this is only for getting the camera to work */
292 /* FIXME: No error handling for now,
293 add this once the init has been converted to proper tables */
294 static int pb0100_init(struct sd
*sd
)
296 stv06xx_write_bridge(sd
, STV_REG00
, 1);
297 stv06xx_write_bridge(sd
, STV_SCAN_RATE
, 0);
300 stv06xx_write_sensor(sd
, PB_RESET
, 1);
301 stv06xx_write_sensor(sd
, PB_RESET
, 0);
304 stv06xx_write_sensor(sd
, PB_CONTROL
, BIT(5)|BIT(3));
307 stv06xx_write_sensor(sd
, PB_PREADCTRL
, BIT(12)|BIT(10)|BIT(6));
308 stv06xx_write_sensor(sd
, PB_ADCGLOBALGAIN
, 12);
310 /* Set up auto-exposure */
311 /* ADC VREF_HI new setting for a transition
312 from the Expose1 to the Expose2 setting */
313 stv06xx_write_sensor(sd
, PB_R28
, 12);
314 /* gain max for autoexposure */
315 stv06xx_write_sensor(sd
, PB_ADCMAXGAIN
, 180);
316 /* gain min for autoexposure */
317 stv06xx_write_sensor(sd
, PB_ADCMINGAIN
, 12);
318 /* Maximum frame integration time (programmed into R8)
319 allowed for auto-exposure routine */
320 stv06xx_write_sensor(sd
, PB_R54
, 3);
321 /* Minimum frame integration time (programmed into R8)
322 allowed for auto-exposure routine */
323 stv06xx_write_sensor(sd
, PB_R55
, 0);
324 stv06xx_write_sensor(sd
, PB_UPDATEINT
, 1);
325 /* R15 Expose0 (maximum that auto-exposure may use) */
326 stv06xx_write_sensor(sd
, PB_R15
, 800);
327 /* R17 Expose2 (minimum that auto-exposure may use) */
328 stv06xx_write_sensor(sd
, PB_R17
, 10);
330 stv06xx_write_sensor(sd
, PB_EXPGAIN
, 0);
333 stv06xx_write_sensor(sd
, PB_VOFFSET
, 0);
335 stv06xx_write_sensor(sd
, PB_ADCGAINH
, 11);
336 /* Set black level (important!) */
337 stv06xx_write_sensor(sd
, PB_ADCGAINL
, 0);
340 stv06xx_write_bridge(sd
, STV_REG00
, 0x11);
341 stv06xx_write_bridge(sd
, STV_REG03
, 0x45);
342 stv06xx_write_bridge(sd
, STV_REG04
, 0x07);
344 /* Scan/timing for the sensor */
345 stv06xx_write_sensor(sd
, PB_ROWSPEED
, BIT(4)|BIT(3)|BIT(1));
346 stv06xx_write_sensor(sd
, PB_CFILLIN
, 14);
347 stv06xx_write_sensor(sd
, PB_VBL
, 0);
348 stv06xx_write_sensor(sd
, PB_FINTTIME
, 0);
349 stv06xx_write_sensor(sd
, PB_RINTTIME
, 123);
351 stv06xx_write_bridge(sd
, STV_REG01
, 0xc2);
352 stv06xx_write_bridge(sd
, STV_REG02
, 0xb0);
356 static int pb0100_dump(struct sd
*sd
)
361 static int pb0100_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
363 struct sd
*sd
= (struct sd
*) gspca_dev
;
364 s32
*sensor_settings
= sd
->sensor_priv
;
366 *val
= sensor_settings
[GAIN_IDX
];
371 static int pb0100_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
374 struct sd
*sd
= (struct sd
*) gspca_dev
;
375 s32
*sensor_settings
= sd
->sensor_priv
;
377 if (sensor_settings
[AUTOGAIN_IDX
])
380 sensor_settings
[GAIN_IDX
] = val
;
381 err
= stv06xx_write_sensor(sd
, PB_G1GAIN
, val
);
383 err
= stv06xx_write_sensor(sd
, PB_G2GAIN
, val
);
384 PDEBUG(D_V4L2
, "Set green gain to %d, status: %d", val
, err
);
387 err
= pb0100_set_red_balance(gspca_dev
,
388 sensor_settings
[RED_BALANCE_IDX
]);
390 err
= pb0100_set_blue_balance(gspca_dev
,
391 sensor_settings
[BLUE_BALANCE_IDX
]);
396 static int pb0100_get_red_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
398 struct sd
*sd
= (struct sd
*) gspca_dev
;
399 s32
*sensor_settings
= sd
->sensor_priv
;
401 *val
= sensor_settings
[RED_BALANCE_IDX
];
406 static int pb0100_set_red_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
409 struct sd
*sd
= (struct sd
*) gspca_dev
;
410 s32
*sensor_settings
= sd
->sensor_priv
;
412 if (sensor_settings
[AUTOGAIN_IDX
])
415 sensor_settings
[RED_BALANCE_IDX
] = val
;
416 val
+= sensor_settings
[GAIN_IDX
];
422 err
= stv06xx_write_sensor(sd
, PB_RGAIN
, val
);
423 PDEBUG(D_V4L2
, "Set red gain to %d, status: %d", val
, err
);
428 static int pb0100_get_blue_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
430 struct sd
*sd
= (struct sd
*) gspca_dev
;
431 s32
*sensor_settings
= sd
->sensor_priv
;
433 *val
= sensor_settings
[BLUE_BALANCE_IDX
];
438 static int pb0100_set_blue_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
441 struct sd
*sd
= (struct sd
*) gspca_dev
;
442 s32
*sensor_settings
= sd
->sensor_priv
;
444 if (sensor_settings
[AUTOGAIN_IDX
])
447 sensor_settings
[BLUE_BALANCE_IDX
] = val
;
448 val
+= sensor_settings
[GAIN_IDX
];
454 err
= stv06xx_write_sensor(sd
, PB_BGAIN
, val
);
455 PDEBUG(D_V4L2
, "Set blue gain to %d, status: %d", val
, err
);
460 static int pb0100_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
462 struct sd
*sd
= (struct sd
*) gspca_dev
;
463 s32
*sensor_settings
= sd
->sensor_priv
;
465 *val
= sensor_settings
[EXPOSURE_IDX
];
470 static int pb0100_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
473 struct sd
*sd
= (struct sd
*) gspca_dev
;
474 s32
*sensor_settings
= sd
->sensor_priv
;
476 if (sensor_settings
[AUTOGAIN_IDX
])
479 sensor_settings
[EXPOSURE_IDX
] = val
;
480 err
= stv06xx_write_sensor(sd
, PB_RINTTIME
, val
);
481 PDEBUG(D_V4L2
, "Set exposure to %d, status: %d", val
, err
);
486 static int pb0100_get_autogain(struct gspca_dev
*gspca_dev
, __s32
*val
)
488 struct sd
*sd
= (struct sd
*) gspca_dev
;
489 s32
*sensor_settings
= sd
->sensor_priv
;
491 *val
= sensor_settings
[AUTOGAIN_IDX
];
496 static int pb0100_set_autogain(struct gspca_dev
*gspca_dev
, __s32 val
)
499 struct sd
*sd
= (struct sd
*) gspca_dev
;
500 s32
*sensor_settings
= sd
->sensor_priv
;
502 sensor_settings
[AUTOGAIN_IDX
] = val
;
503 if (sensor_settings
[AUTOGAIN_IDX
]) {
504 if (sensor_settings
[NATURAL_IDX
])
505 val
= BIT(6)|BIT(4)|BIT(0);
511 err
= stv06xx_write_sensor(sd
, PB_EXPGAIN
, val
);
512 PDEBUG(D_V4L2
, "Set autogain to %d (natural: %d), status: %d",
513 sensor_settings
[AUTOGAIN_IDX
], sensor_settings
[NATURAL_IDX
],
519 static int pb0100_get_autogain_target(struct gspca_dev
*gspca_dev
, __s32
*val
)
521 struct sd
*sd
= (struct sd
*) gspca_dev
;
522 s32
*sensor_settings
= sd
->sensor_priv
;
524 *val
= sensor_settings
[AUTOGAIN_TARGET_IDX
];
529 static int pb0100_set_autogain_target(struct gspca_dev
*gspca_dev
, __s32 val
)
531 int err
, totalpixels
, brightpixels
, darkpixels
;
532 struct sd
*sd
= (struct sd
*) gspca_dev
;
533 s32
*sensor_settings
= sd
->sensor_priv
;
535 sensor_settings
[AUTOGAIN_TARGET_IDX
] = val
;
537 /* Number of pixels counted by the sensor when subsampling the pixels.
538 * Slightly larger than the real value to avoid oscillation */
539 totalpixels
= gspca_dev
->width
* gspca_dev
->height
;
540 totalpixels
= totalpixels
/(8*8) + totalpixels
/(64*64);
542 brightpixels
= (totalpixels
* val
) >> 8;
543 darkpixels
= totalpixels
- brightpixels
;
544 err
= stv06xx_write_sensor(sd
, PB_R21
, brightpixels
);
546 err
= stv06xx_write_sensor(sd
, PB_R22
, darkpixels
);
548 PDEBUG(D_V4L2
, "Set autogain target to %d, status: %d", val
, err
);
553 static int pb0100_get_natural(struct gspca_dev
*gspca_dev
, __s32
*val
)
555 struct sd
*sd
= (struct sd
*) gspca_dev
;
556 s32
*sensor_settings
= sd
->sensor_priv
;
558 *val
= sensor_settings
[NATURAL_IDX
];
563 static int pb0100_set_natural(struct gspca_dev
*gspca_dev
, __s32 val
)
565 struct sd
*sd
= (struct sd
*) gspca_dev
;
566 s32
*sensor_settings
= sd
->sensor_priv
;
568 sensor_settings
[NATURAL_IDX
] = val
;
570 return pb0100_set_autogain(gspca_dev
, sensor_settings
[AUTOGAIN_IDX
]);