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
6 * Copyright (c) 2008 Chia-I Wu
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * P/N 861037: Sensor HDCS1000 ASIC STV0600
23 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
24 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
25 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
26 * P/N 861075-0040: Sensor HDCS1000 ASIC
27 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
28 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
31 #include "stv06xx_hdcs.h"
33 static const struct ctrl hdcs1x00_ctrl
[] = {
36 .id
= V4L2_CID_EXPOSURE
,
37 .type
= V4L2_CTRL_TYPE_INTEGER
,
42 .default_value
= HDCS_DEFAULT_EXPOSURE
,
43 .flags
= V4L2_CTRL_FLAG_SLIDER
45 .set
= hdcs_set_exposure
,
46 .get
= hdcs_get_exposure
50 .type
= V4L2_CTRL_TYPE_INTEGER
,
55 .default_value
= HDCS_DEFAULT_GAIN
,
56 .flags
= V4L2_CTRL_FLAG_SLIDER
63 static struct v4l2_pix_format hdcs1x00_mode
[] = {
70 HDCS_1X00_DEF_WIDTH
* HDCS_1X00_DEF_HEIGHT
,
71 .bytesperline
= HDCS_1X00_DEF_WIDTH
,
72 .colorspace
= V4L2_COLORSPACE_SRGB
,
77 static const struct ctrl hdcs1020_ctrl
[] = {};
79 static struct v4l2_pix_format hdcs1020_mode
[] = {
86 HDCS_1020_DEF_WIDTH
* HDCS_1020_DEF_HEIGHT
,
87 .bytesperline
= HDCS_1020_DEF_WIDTH
,
88 .colorspace
= V4L2_COLORSPACE_SRGB
,
93 enum hdcs_power_state
{
101 enum hdcs_power_state state
;
104 /* visible area of the sensor array */
112 /* Column timing overhead */
114 /* Column processing overhead */
116 /* Row sample period constant */
118 /* Exposure reset duration */
125 static int hdcs_reg_write_seq(struct sd
*sd
, u8 reg
, u8
*vals
, u8 len
)
127 u8 regs
[I2C_MAX_BYTES
* 2];
130 if (unlikely((len
<= 0) || (len
>= I2C_MAX_BYTES
) ||
134 for (i
= 0; i
< len
; i
++) {
136 regs
[2 * i
+ 1] = vals
[i
];
137 /* All addresses are shifted left one bit as bit 0 toggles r/w */
141 return stv06xx_write_sensor_bytes(sd
, regs
, len
);
144 static int hdcs_set_state(struct sd
*sd
, enum hdcs_power_state state
)
146 struct hdcs
*hdcs
= sd
->sensor_priv
;
150 if (hdcs
->state
== state
)
153 /* we need to go idle before running or sleeping */
154 if (hdcs
->state
!= HDCS_STATE_IDLE
) {
155 ret
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 0);
160 hdcs
->state
= HDCS_STATE_IDLE
;
162 if (state
== HDCS_STATE_IDLE
)
166 case HDCS_STATE_SLEEP
:
167 val
= HDCS_SLEEP_MODE
;
171 val
= HDCS_RUN_ENABLE
;
178 ret
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), val
);
180 /* Update the state if the write succeeded */
187 static int hdcs_reset(struct sd
*sd
)
189 struct hdcs
*hdcs
= sd
->sensor_priv
;
192 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 1);
196 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 0);
198 hdcs
->state
= HDCS_STATE_IDLE
;
203 static int hdcs_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
205 struct sd
*sd
= (struct sd
*) gspca_dev
;
206 struct hdcs
*hdcs
= sd
->sensor_priv
;
208 /* Column time period */
210 /* Column processing period */
212 /* Row processing period */
219 err
= stv06xx_read_sensor(sd
, HDCS_ROWEXPL
, &data
[0]);
223 err
= stv06xx_read_sensor(sd
, HDCS_ROWEXPH
, &data
[1]);
227 rowexp
= (data
[1] << 8) | data
[0];
229 ct
= hdcs
->exp
.cto
+ hdcs
->psmp
+ (HDCS_ADC_START_SIG_DUR
+ 2);
230 cp
= hdcs
->exp
.cto
+ (hdcs
->w
* ct
/ 2);
231 rp
= hdcs
->exp
.rs
+ cp
;
233 cycles
= rp
* rowexp
;
234 *val
= cycles
/ HDCS_CLK_FREQ_MHZ
;
235 PDEBUG(D_V4L2
, "Read exposure %d", *val
);
239 static int hdcs_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
241 struct sd
*sd
= (struct sd
*) gspca_dev
;
242 struct hdcs
*hdcs
= sd
->sensor_priv
;
245 /* Column time period */
247 /* Column processing period */
249 /* Row processing period */
251 /* Minimum number of column timing periods
252 within the column processing period */
257 cycles
= val
* HDCS_CLK_FREQ_MHZ
;
259 ct
= hdcs
->exp
.cto
+ hdcs
->psmp
+ (HDCS_ADC_START_SIG_DUR
+ 2);
260 cp
= hdcs
->exp
.cto
+ (hdcs
->w
* ct
/ 2);
262 /* the cycles one row takes */
263 rp
= hdcs
->exp
.rs
+ cp
;
265 rowexp
= cycles
/ rp
;
267 /* the remaining cycles */
268 cycles
-= rowexp
* rp
;
270 /* calculate sub-row exposure */
272 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
273 srowexp
= hdcs
->w
- (cycles
+ hdcs
->exp
.er
+ 13) / ct
;
275 mnct
= (hdcs
->exp
.er
+ 12 + ct
- 1) / ct
;
276 max_srowexp
= hdcs
->w
- mnct
;
278 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
279 srowexp
= cp
- hdcs
->exp
.er
- 6 - cycles
;
281 mnct
= (hdcs
->exp
.er
+ 5 + ct
- 1) / ct
;
282 max_srowexp
= cp
- mnct
* ct
- 1;
287 else if (srowexp
> max_srowexp
)
288 srowexp
= max_srowexp
;
291 exp
[0] = rowexp
& 0xff;
292 exp
[1] = rowexp
>> 8;
293 exp
[2] = (srowexp
>> 2) & 0xff;
294 /* this clears exposure error flag */
296 err
= hdcs_reg_write_seq(sd
, HDCS_ROWEXPL
, exp
, 4);
298 exp
[0] = rowexp
& 0xff;
299 exp
[1] = rowexp
>> 8;
300 exp
[2] = srowexp
& 0xff;
301 exp
[3] = srowexp
>> 8;
302 err
= hdcs_reg_write_seq(sd
, HDCS_ROWEXPL
, exp
, 4);
306 /* clear exposure error flag */
307 err
= stv06xx_write_sensor(sd
,
308 HDCS_STATUS
, BIT(4));
310 PDEBUG(D_V4L2
, "Writing exposure %d, rowexp %d, srowexp %d",
311 val
, rowexp
, srowexp
);
315 static int hdcs_set_gains(struct sd
*sd
, u8 r
, u8 g
, u8 b
)
319 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
332 return hdcs_reg_write_seq(sd
, HDCS_ERECPGA
, gains
, 4);
335 static int hdcs_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
337 struct sd
*sd
= (struct sd
*) gspca_dev
;
341 err
= stv06xx_read_sensor(sd
, HDCS_ERECPGA
, &data
);
343 /* Bit 7 doubles the gain */
345 *val
= (data
& 0x7f) * 2;
349 PDEBUG(D_V4L2
, "Read gain %d", *val
);
353 static int hdcs_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
355 PDEBUG(D_V4L2
, "Writing gain %d", val
);
356 return hdcs_set_gains((struct sd
*) gspca_dev
,
357 val
& 0xff, val
& 0xff, val
& 0xff);
360 static int hdcs_set_size(struct sd
*sd
,
361 unsigned int width
, unsigned int height
)
363 struct hdcs
*hdcs
= sd
->sensor_priv
;
368 /* must be multiple of 4 */
369 width
= (width
+ 3) & ~0x3;
370 height
= (height
+ 3) & ~0x3;
372 if (width
> hdcs
->array
.width
)
373 width
= hdcs
->array
.width
;
376 /* the borders are also invalid */
377 if (height
+ 2 * hdcs
->array
.border
+ HDCS_1020_BOTTOM_Y_SKIP
378 > hdcs
->array
.height
)
379 height
= hdcs
->array
.height
- 2 * hdcs
->array
.border
-
380 HDCS_1020_BOTTOM_Y_SKIP
;
382 y
= (hdcs
->array
.height
- HDCS_1020_BOTTOM_Y_SKIP
- height
) / 2
385 if (height
> hdcs
->array
.height
)
386 height
= hdcs
->array
.height
;
388 y
= hdcs
->array
.top
+ (hdcs
->array
.height
- height
) / 2;
391 x
= hdcs
->array
.left
+ (hdcs
->array
.width
- width
) / 2;
395 win
[2] = (y
+ height
) / 4 - 1;
396 win
[3] = (x
+ width
) / 4 - 1;
398 err
= hdcs_reg_write_seq(sd
, HDCS_FWROW
, win
, 4);
402 /* Update the current width and height */
408 static int hdcs_probe_1x00(struct sd
*sd
)
414 ret
= stv06xx_read_sensor(sd
, HDCS_IDENT
, &sensor
);
415 if (ret
< 0 || sensor
!= 0x08)
418 info("HDCS-1000/1100 sensor detected");
420 sd
->gspca_dev
.cam
.cam_mode
= hdcs1x00_mode
;
421 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(hdcs1x00_mode
);
422 sd
->desc
.ctrls
= hdcs1x00_ctrl
;
423 sd
->desc
.nctrls
= ARRAY_SIZE(hdcs1x00_ctrl
);
425 hdcs
= kmalloc(sizeof(struct hdcs
), GFP_KERNEL
);
429 hdcs
->array
.left
= 8;
431 hdcs
->array
.width
= HDCS_1X00_DEF_WIDTH
;
432 hdcs
->array
.height
= HDCS_1X00_DEF_HEIGHT
;
433 hdcs
->array
.border
= 4;
441 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
442 * 4 = doesn't work at all
450 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
451 * 15 = doesn't work at all
452 * 18 = doesn't work at all
460 hdcs
->psmp
= (sd
->bridge
== BRIDGE_STV602
) ? 20 : 5;
462 sd
->sensor_priv
= hdcs
;
467 static int hdcs_probe_1020(struct sd
*sd
)
473 ret
= stv06xx_read_sensor(sd
, HDCS_IDENT
, &sensor
);
474 if (ret
< 0 || sensor
!= 0x10)
477 info("HDCS-1020 sensor detected");
479 sd
->gspca_dev
.cam
.cam_mode
= hdcs1020_mode
;
480 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(hdcs1020_mode
);
481 sd
->desc
.ctrls
= hdcs1020_ctrl
;
482 sd
->desc
.nctrls
= ARRAY_SIZE(hdcs1020_ctrl
);
484 hdcs
= kmalloc(sizeof(struct hdcs
), GFP_KERNEL
);
489 * From Andrey's test image: looks like HDCS-1020 upper-left
490 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
491 * visible pixel at 375,299 (x maybe even larger?)
493 hdcs
->array
.left
= 24;
495 hdcs
->array
.width
= HDCS_1020_DEF_WIDTH
;
496 hdcs
->array
.height
= 304;
497 hdcs
->array
.border
= 4;
506 sd
->sensor_priv
= hdcs
;
511 static int hdcs_start(struct sd
*sd
)
513 PDEBUG(D_STREAM
, "Starting stream");
515 return hdcs_set_state(sd
, HDCS_STATE_RUN
);
518 static int hdcs_stop(struct sd
*sd
)
520 PDEBUG(D_STREAM
, "Halting stream");
522 return hdcs_set_state(sd
, HDCS_STATE_SLEEP
);
525 static void hdcs_disconnect(struct sd
*sd
)
527 PDEBUG(D_PROBE
, "Disconnecting the sensor");
528 kfree(sd
->sensor_priv
);
531 static int hdcs_init(struct sd
*sd
)
533 struct hdcs
*hdcs
= sd
->sensor_priv
;
536 /* Set the STV0602AA in STV0600 emulation mode */
537 if (sd
->bridge
== BRIDGE_STV602
)
538 stv06xx_write_bridge(sd
, STV_STV0600_EMULATION
, 1);
540 /* Execute the bridge init */
541 for (i
= 0; i
< ARRAY_SIZE(stv_bridge_init
) && !err
; i
++) {
542 err
= stv06xx_write_bridge(sd
, stv_bridge_init
[i
][0],
543 stv_bridge_init
[i
][1]);
548 /* sensor soft reset */
551 /* Execute the sensor init */
552 for (i
= 0; i
< ARRAY_SIZE(stv_sensor_init
) && !err
; i
++) {
553 err
= stv06xx_write_sensor(sd
, stv_sensor_init
[i
][0],
554 stv_sensor_init
[i
][1]);
559 /* Enable continous frame capture, bit 2: stop when frame complete */
560 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONFIG(sd
), BIT(3));
564 /* Set PGA sample duration
565 (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
567 err
= stv06xx_write_sensor(sd
, HDCS_TCTRL
,
568 (HDCS_ADC_START_SIG_DUR
<< 6) | hdcs
->psmp
);
570 err
= stv06xx_write_sensor(sd
, HDCS_TCTRL
,
571 (HDCS_ADC_START_SIG_DUR
<< 5) | hdcs
->psmp
);
575 err
= hdcs_set_gains(sd
, HDCS_DEFAULT_GAIN
, HDCS_DEFAULT_GAIN
,
580 err
= hdcs_set_exposure(&sd
->gspca_dev
, HDCS_DEFAULT_EXPOSURE
);
584 err
= hdcs_set_size(sd
, hdcs
->array
.width
, hdcs
->array
.height
);
588 static int hdcs_dump(struct sd
*sd
)
592 info("Dumping sensor registers:");
594 for (reg
= HDCS_IDENT
; reg
<= HDCS_ROWEXPH
; reg
++) {
595 stv06xx_read_sensor(sd
, reg
, &val
);
596 info("reg 0x%02x = 0x%02x", reg
, val
);