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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include "stv06xx_hdcs.h"
35 static const struct ctrl hdcs1x00_ctrl
[] = {
38 .id
= V4L2_CID_EXPOSURE
,
39 .type
= V4L2_CTRL_TYPE_INTEGER
,
44 .default_value
= HDCS_DEFAULT_EXPOSURE
,
45 .flags
= V4L2_CTRL_FLAG_SLIDER
47 .set
= hdcs_set_exposure
,
48 .get
= hdcs_get_exposure
52 .type
= V4L2_CTRL_TYPE_INTEGER
,
57 .default_value
= HDCS_DEFAULT_GAIN
,
58 .flags
= V4L2_CTRL_FLAG_SLIDER
65 static struct v4l2_pix_format hdcs1x00_mode
[] = {
72 HDCS_1X00_DEF_WIDTH
* HDCS_1X00_DEF_HEIGHT
,
73 .bytesperline
= HDCS_1X00_DEF_WIDTH
,
74 .colorspace
= V4L2_COLORSPACE_SRGB
,
79 static const struct ctrl hdcs1020_ctrl
[] = {
82 .id
= V4L2_CID_EXPOSURE
,
83 .type
= V4L2_CTRL_TYPE_INTEGER
,
88 .default_value
= HDCS_DEFAULT_EXPOSURE
,
89 .flags
= V4L2_CTRL_FLAG_SLIDER
91 .set
= hdcs_set_exposure
,
92 .get
= hdcs_get_exposure
96 .type
= V4L2_CTRL_TYPE_INTEGER
,
101 .default_value
= HDCS_DEFAULT_GAIN
,
102 .flags
= V4L2_CTRL_FLAG_SLIDER
104 .set
= hdcs_set_gain
,
109 static struct v4l2_pix_format hdcs1020_mode
[] = {
112 HDCS_1020_DEF_HEIGHT
,
116 HDCS_1020_DEF_WIDTH
* HDCS_1020_DEF_HEIGHT
,
117 .bytesperline
= HDCS_1020_DEF_WIDTH
,
118 .colorspace
= V4L2_COLORSPACE_SRGB
,
123 enum hdcs_power_state
{
131 enum hdcs_power_state state
;
134 /* visible area of the sensor array */
142 /* Column timing overhead */
144 /* Column processing overhead */
146 /* Row sample period constant */
148 /* Exposure reset duration */
153 u8 exp_cache
, gain_cache
;
156 static int hdcs_reg_write_seq(struct sd
*sd
, u8 reg
, u8
*vals
, u8 len
)
158 u8 regs
[I2C_MAX_BYTES
* 2];
161 if (unlikely((len
<= 0) || (len
>= I2C_MAX_BYTES
) ||
165 for (i
= 0; i
< len
; i
++) {
167 regs
[2 * i
+ 1] = vals
[i
];
168 /* All addresses are shifted left one bit
169 * as bit 0 toggles r/w */
173 return stv06xx_write_sensor_bytes(sd
, regs
, len
);
176 static int hdcs_set_state(struct sd
*sd
, enum hdcs_power_state state
)
178 struct hdcs
*hdcs
= sd
->sensor_priv
;
182 if (hdcs
->state
== state
)
185 /* we need to go idle before running or sleeping */
186 if (hdcs
->state
!= HDCS_STATE_IDLE
) {
187 ret
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 0);
192 hdcs
->state
= HDCS_STATE_IDLE
;
194 if (state
== HDCS_STATE_IDLE
)
198 case HDCS_STATE_SLEEP
:
199 val
= HDCS_SLEEP_MODE
;
203 val
= HDCS_RUN_ENABLE
;
210 ret
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), val
);
212 /* Update the state if the write succeeded */
219 static int hdcs_reset(struct sd
*sd
)
221 struct hdcs
*hdcs
= sd
->sensor_priv
;
224 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 1);
228 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 0);
230 hdcs
->state
= HDCS_STATE_IDLE
;
235 static int hdcs_get_exposure(struct gspca_dev
*gspca_dev
, __s32
*val
)
237 struct sd
*sd
= (struct sd
*) gspca_dev
;
238 struct hdcs
*hdcs
= sd
->sensor_priv
;
240 *val
= hdcs
->exp_cache
;
245 static int hdcs_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
247 struct sd
*sd
= (struct sd
*) gspca_dev
;
248 struct hdcs
*hdcs
= sd
->sensor_priv
;
251 /* Column time period */
253 /* Column processing period */
255 /* Row processing period */
257 /* Minimum number of column timing periods
258 within the column processing period */
264 hdcs
->exp_cache
= val
;
266 cycles
= val
* HDCS_CLK_FREQ_MHZ
* 257;
268 ct
= hdcs
->exp
.cto
+ hdcs
->psmp
+ (HDCS_ADC_START_SIG_DUR
+ 2);
269 cp
= hdcs
->exp
.cto
+ (hdcs
->w
* ct
/ 2);
271 /* the cycles one row takes */
272 rp
= hdcs
->exp
.rs
+ cp
;
274 rowexp
= cycles
/ rp
;
276 /* the remaining cycles */
277 cycles
-= rowexp
* rp
;
279 /* calculate sub-row exposure */
281 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
282 srowexp
= hdcs
->w
- (cycles
+ hdcs
->exp
.er
+ 13) / ct
;
284 mnct
= (hdcs
->exp
.er
+ 12 + ct
- 1) / ct
;
285 max_srowexp
= hdcs
->w
- mnct
;
287 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
288 srowexp
= cp
- hdcs
->exp
.er
- 6 - cycles
;
290 mnct
= (hdcs
->exp
.er
+ 5 + ct
- 1) / ct
;
291 max_srowexp
= cp
- mnct
* ct
- 1;
296 else if (srowexp
> max_srowexp
)
297 srowexp
= max_srowexp
;
300 exp
[0] = HDCS20_CONTROL
;
301 exp
[1] = 0x00; /* Stop streaming */
302 exp
[2] = HDCS_ROWEXPL
;
303 exp
[3] = rowexp
& 0xff;
304 exp
[4] = HDCS_ROWEXPH
;
305 exp
[5] = rowexp
>> 8;
306 exp
[6] = HDCS20_SROWEXP
;
307 exp
[7] = (srowexp
>> 2) & 0xff;
308 exp
[8] = HDCS20_ERROR
;
309 exp
[9] = 0x10; /* Clear exposure error flag*/
310 exp
[10] = HDCS20_CONTROL
;
311 exp
[11] = 0x04; /* Restart streaming */
312 err
= stv06xx_write_sensor_bytes(sd
, exp
, 6);
314 exp
[0] = HDCS00_CONTROL
;
315 exp
[1] = 0x00; /* Stop streaming */
316 exp
[2] = HDCS_ROWEXPL
;
317 exp
[3] = rowexp
& 0xff;
318 exp
[4] = HDCS_ROWEXPH
;
319 exp
[5] = rowexp
>> 8;
320 exp
[6] = HDCS00_SROWEXPL
;
321 exp
[7] = srowexp
& 0xff;
322 exp
[8] = HDCS00_SROWEXPH
;
323 exp
[9] = srowexp
>> 8;
324 exp
[10] = HDCS_STATUS
;
325 exp
[11] = 0x10; /* Clear exposure error flag*/
326 exp
[12] = HDCS00_CONTROL
;
327 exp
[13] = 0x04; /* Restart streaming */
328 err
= stv06xx_write_sensor_bytes(sd
, exp
, 7);
332 PDEBUG(D_V4L2
, "Writing exposure %d, rowexp %d, srowexp %d",
333 val
, rowexp
, srowexp
);
337 static int hdcs_set_gains(struct sd
*sd
, u8 g
)
339 struct hdcs
*hdcs
= sd
->sensor_priv
;
343 hdcs
->gain_cache
= g
;
345 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
354 err
= hdcs_reg_write_seq(sd
, HDCS_ERECPGA
, gains
, 4);
358 static int hdcs_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
360 struct sd
*sd
= (struct sd
*) gspca_dev
;
361 struct hdcs
*hdcs
= sd
->sensor_priv
;
363 *val
= hdcs
->gain_cache
;
368 static int hdcs_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
370 PDEBUG(D_V4L2
, "Writing gain %d", val
);
371 return hdcs_set_gains((struct sd
*) gspca_dev
,
375 static int hdcs_set_size(struct sd
*sd
,
376 unsigned int width
, unsigned int height
)
378 struct hdcs
*hdcs
= sd
->sensor_priv
;
383 /* must be multiple of 4 */
384 width
= (width
+ 3) & ~0x3;
385 height
= (height
+ 3) & ~0x3;
387 if (width
> hdcs
->array
.width
)
388 width
= hdcs
->array
.width
;
391 /* the borders are also invalid */
392 if (height
+ 2 * hdcs
->array
.border
+ HDCS_1020_BOTTOM_Y_SKIP
393 > hdcs
->array
.height
)
394 height
= hdcs
->array
.height
- 2 * hdcs
->array
.border
-
395 HDCS_1020_BOTTOM_Y_SKIP
;
397 y
= (hdcs
->array
.height
- HDCS_1020_BOTTOM_Y_SKIP
- height
) / 2
400 if (height
> hdcs
->array
.height
)
401 height
= hdcs
->array
.height
;
403 y
= hdcs
->array
.top
+ (hdcs
->array
.height
- height
) / 2;
406 x
= hdcs
->array
.left
+ (hdcs
->array
.width
- width
) / 2;
410 win
[2] = (y
+ height
) / 4 - 1;
411 win
[3] = (x
+ width
) / 4 - 1;
413 err
= hdcs_reg_write_seq(sd
, HDCS_FWROW
, win
, 4);
417 /* Update the current width and height */
423 static int hdcs_probe_1x00(struct sd
*sd
)
429 ret
= stv06xx_read_sensor(sd
, HDCS_IDENT
, &sensor
);
430 if (ret
< 0 || sensor
!= 0x08)
433 pr_info("HDCS-1000/1100 sensor detected\n");
435 sd
->gspca_dev
.cam
.cam_mode
= hdcs1x00_mode
;
436 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(hdcs1x00_mode
);
437 sd
->desc
.ctrls
= hdcs1x00_ctrl
;
438 sd
->desc
.nctrls
= ARRAY_SIZE(hdcs1x00_ctrl
);
440 hdcs
= kmalloc(sizeof(struct hdcs
), GFP_KERNEL
);
444 hdcs
->array
.left
= 8;
446 hdcs
->array
.width
= HDCS_1X00_DEF_WIDTH
;
447 hdcs
->array
.height
= HDCS_1X00_DEF_HEIGHT
;
448 hdcs
->array
.border
= 4;
456 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
457 * 4 = doesn't work at all
465 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
466 * 15 = doesn't work at all
467 * 18 = doesn't work at all
475 hdcs
->psmp
= (sd
->bridge
== BRIDGE_STV602
) ? 20 : 5;
477 sd
->sensor_priv
= hdcs
;
482 static int hdcs_probe_1020(struct sd
*sd
)
488 ret
= stv06xx_read_sensor(sd
, HDCS_IDENT
, &sensor
);
489 if (ret
< 0 || sensor
!= 0x10)
492 pr_info("HDCS-1020 sensor detected\n");
494 sd
->gspca_dev
.cam
.cam_mode
= hdcs1020_mode
;
495 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(hdcs1020_mode
);
496 sd
->desc
.ctrls
= hdcs1020_ctrl
;
497 sd
->desc
.nctrls
= ARRAY_SIZE(hdcs1020_ctrl
);
499 hdcs
= kmalloc(sizeof(struct hdcs
), GFP_KERNEL
);
504 * From Andrey's test image: looks like HDCS-1020 upper-left
505 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
506 * visible pixel at 375,299 (x maybe even larger?)
508 hdcs
->array
.left
= 24;
510 hdcs
->array
.width
= HDCS_1020_DEF_WIDTH
;
511 hdcs
->array
.height
= 304;
512 hdcs
->array
.border
= 4;
521 sd
->sensor_priv
= hdcs
;
526 static int hdcs_start(struct sd
*sd
)
528 PDEBUG(D_STREAM
, "Starting stream");
530 return hdcs_set_state(sd
, HDCS_STATE_RUN
);
533 static int hdcs_stop(struct sd
*sd
)
535 PDEBUG(D_STREAM
, "Halting stream");
537 return hdcs_set_state(sd
, HDCS_STATE_SLEEP
);
540 static void hdcs_disconnect(struct sd
*sd
)
542 PDEBUG(D_PROBE
, "Disconnecting the sensor");
543 kfree(sd
->sensor_priv
);
546 static int hdcs_init(struct sd
*sd
)
548 struct hdcs
*hdcs
= sd
->sensor_priv
;
551 /* Set the STV0602AA in STV0600 emulation mode */
552 if (sd
->bridge
== BRIDGE_STV602
)
553 stv06xx_write_bridge(sd
, STV_STV0600_EMULATION
, 1);
555 /* Execute the bridge init */
556 for (i
= 0; i
< ARRAY_SIZE(stv_bridge_init
) && !err
; i
++) {
557 err
= stv06xx_write_bridge(sd
, stv_bridge_init
[i
][0],
558 stv_bridge_init
[i
][1]);
563 /* sensor soft reset */
566 /* Execute the sensor init */
567 for (i
= 0; i
< ARRAY_SIZE(stv_sensor_init
) && !err
; i
++) {
568 err
= stv06xx_write_sensor(sd
, stv_sensor_init
[i
][0],
569 stv_sensor_init
[i
][1]);
574 /* Enable continuous frame capture, bit 2: stop when frame complete */
575 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONFIG(sd
), BIT(3));
579 /* Set PGA sample duration
580 (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
582 err
= stv06xx_write_sensor(sd
, HDCS_TCTRL
,
583 (HDCS_ADC_START_SIG_DUR
<< 6) | hdcs
->psmp
);
585 err
= stv06xx_write_sensor(sd
, HDCS_TCTRL
,
586 (HDCS_ADC_START_SIG_DUR
<< 5) | hdcs
->psmp
);
590 err
= hdcs_set_gains(sd
, HDCS_DEFAULT_GAIN
);
594 err
= hdcs_set_size(sd
, hdcs
->array
.width
, hdcs
->array
.height
);
598 err
= hdcs_set_exposure(&sd
->gspca_dev
, HDCS_DEFAULT_EXPOSURE
);
602 static int hdcs_dump(struct sd
*sd
)
606 pr_info("Dumping sensor registers:\n");
608 for (reg
= HDCS_IDENT
; reg
<= HDCS_ROWEXPH
; reg
++) {
609 stv06xx_read_sensor(sd
, reg
, &val
);
610 pr_info("reg 0x%02x = 0x%02x\n", reg
, val
);