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
7 * Copyright (c) 2008 Chia-I Wu
9 * P/N 861037: Sensor HDCS1000 ASIC STV0600
10 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
11 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
12 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
13 * P/N 861075-0040: Sensor HDCS1000 ASIC
14 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
15 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include "stv06xx_hdcs.h"
22 static struct v4l2_pix_format hdcs1x00_mode
[] = {
29 HDCS_1X00_DEF_WIDTH
* HDCS_1X00_DEF_HEIGHT
,
30 .bytesperline
= HDCS_1X00_DEF_WIDTH
,
31 .colorspace
= V4L2_COLORSPACE_SRGB
,
36 static struct v4l2_pix_format hdcs1020_mode
[] = {
43 HDCS_1020_DEF_WIDTH
* HDCS_1020_DEF_HEIGHT
,
44 .bytesperline
= HDCS_1020_DEF_WIDTH
,
45 .colorspace
= V4L2_COLORSPACE_SRGB
,
50 enum hdcs_power_state
{
58 enum hdcs_power_state state
;
61 /* visible area of the sensor array */
69 /* Column timing overhead */
71 /* Column processing overhead */
73 /* Row sample period constant */
75 /* Exposure reset duration */
82 static int hdcs_reg_write_seq(struct sd
*sd
, u8 reg
, u8
*vals
, u8 len
)
84 u8 regs
[I2C_MAX_BYTES
* 2];
87 if (unlikely((len
<= 0) || (len
>= I2C_MAX_BYTES
) ||
91 for (i
= 0; i
< len
; i
++) {
93 regs
[2 * i
+ 1] = vals
[i
];
94 /* All addresses are shifted left one bit
95 * as bit 0 toggles r/w */
99 return stv06xx_write_sensor_bytes(sd
, regs
, len
);
102 static int hdcs_set_state(struct sd
*sd
, enum hdcs_power_state state
)
104 struct hdcs
*hdcs
= sd
->sensor_priv
;
108 if (hdcs
->state
== state
)
111 /* we need to go idle before running or sleeping */
112 if (hdcs
->state
!= HDCS_STATE_IDLE
) {
113 ret
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 0);
118 hdcs
->state
= HDCS_STATE_IDLE
;
120 if (state
== HDCS_STATE_IDLE
)
124 case HDCS_STATE_SLEEP
:
125 val
= HDCS_SLEEP_MODE
;
129 val
= HDCS_RUN_ENABLE
;
136 ret
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), val
);
138 /* Update the state if the write succeeded */
145 static int hdcs_reset(struct sd
*sd
)
147 struct hdcs
*hdcs
= sd
->sensor_priv
;
150 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 1);
154 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONTROL(sd
), 0);
156 hdcs
->state
= HDCS_STATE_IDLE
;
161 static int hdcs_set_exposure(struct gspca_dev
*gspca_dev
, __s32 val
)
163 struct sd
*sd
= (struct sd
*) gspca_dev
;
164 struct hdcs
*hdcs
= sd
->sensor_priv
;
167 /* Column time period */
169 /* Column processing period */
171 /* Row processing period */
173 /* Minimum number of column timing periods
174 within the column processing period */
179 cycles
= val
* HDCS_CLK_FREQ_MHZ
* 257;
181 ct
= hdcs
->exp
.cto
+ hdcs
->psmp
+ (HDCS_ADC_START_SIG_DUR
+ 2);
182 cp
= hdcs
->exp
.cto
+ (hdcs
->w
* ct
/ 2);
184 /* the cycles one row takes */
185 rp
= hdcs
->exp
.rs
+ cp
;
187 rowexp
= cycles
/ rp
;
189 /* the remaining cycles */
190 cycles
-= rowexp
* rp
;
192 /* calculate sub-row exposure */
194 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
195 srowexp
= hdcs
->w
- (cycles
+ hdcs
->exp
.er
+ 13) / ct
;
197 mnct
= (hdcs
->exp
.er
+ 12 + ct
- 1) / ct
;
198 max_srowexp
= hdcs
->w
- mnct
;
200 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
201 srowexp
= cp
- hdcs
->exp
.er
- 6 - cycles
;
203 mnct
= (hdcs
->exp
.er
+ 5 + ct
- 1) / ct
;
204 max_srowexp
= cp
- mnct
* ct
- 1;
209 else if (srowexp
> max_srowexp
)
210 srowexp
= max_srowexp
;
213 exp
[0] = HDCS20_CONTROL
;
214 exp
[1] = 0x00; /* Stop streaming */
215 exp
[2] = HDCS_ROWEXPL
;
216 exp
[3] = rowexp
& 0xff;
217 exp
[4] = HDCS_ROWEXPH
;
218 exp
[5] = rowexp
>> 8;
219 exp
[6] = HDCS20_SROWEXP
;
220 exp
[7] = (srowexp
>> 2) & 0xff;
221 exp
[8] = HDCS20_ERROR
;
222 exp
[9] = 0x10; /* Clear exposure error flag*/
223 exp
[10] = HDCS20_CONTROL
;
224 exp
[11] = 0x04; /* Restart streaming */
225 err
= stv06xx_write_sensor_bytes(sd
, exp
, 6);
227 exp
[0] = HDCS00_CONTROL
;
228 exp
[1] = 0x00; /* Stop streaming */
229 exp
[2] = HDCS_ROWEXPL
;
230 exp
[3] = rowexp
& 0xff;
231 exp
[4] = HDCS_ROWEXPH
;
232 exp
[5] = rowexp
>> 8;
233 exp
[6] = HDCS00_SROWEXPL
;
234 exp
[7] = srowexp
& 0xff;
235 exp
[8] = HDCS00_SROWEXPH
;
236 exp
[9] = srowexp
>> 8;
237 exp
[10] = HDCS_STATUS
;
238 exp
[11] = 0x10; /* Clear exposure error flag*/
239 exp
[12] = HDCS00_CONTROL
;
240 exp
[13] = 0x04; /* Restart streaming */
241 err
= stv06xx_write_sensor_bytes(sd
, exp
, 7);
245 gspca_dbg(gspca_dev
, D_CONF
, "Writing exposure %d, rowexp %d, srowexp %d\n",
246 val
, rowexp
, srowexp
);
250 static int hdcs_set_gains(struct sd
*sd
, u8 g
)
255 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
264 err
= hdcs_reg_write_seq(sd
, HDCS_ERECPGA
, gains
, 4);
268 static int hdcs_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
270 gspca_dbg(gspca_dev
, D_CONF
, "Writing gain %d\n", val
);
271 return hdcs_set_gains((struct sd
*) gspca_dev
,
275 static int hdcs_set_size(struct sd
*sd
,
276 unsigned int width
, unsigned int height
)
278 struct hdcs
*hdcs
= sd
->sensor_priv
;
283 /* must be multiple of 4 */
284 width
= (width
+ 3) & ~0x3;
285 height
= (height
+ 3) & ~0x3;
287 if (width
> hdcs
->array
.width
)
288 width
= hdcs
->array
.width
;
291 /* the borders are also invalid */
292 if (height
+ 2 * hdcs
->array
.border
+ HDCS_1020_BOTTOM_Y_SKIP
293 > hdcs
->array
.height
)
294 height
= hdcs
->array
.height
- 2 * hdcs
->array
.border
-
295 HDCS_1020_BOTTOM_Y_SKIP
;
297 y
= (hdcs
->array
.height
- HDCS_1020_BOTTOM_Y_SKIP
- height
) / 2
300 if (height
> hdcs
->array
.height
)
301 height
= hdcs
->array
.height
;
303 y
= hdcs
->array
.top
+ (hdcs
->array
.height
- height
) / 2;
306 x
= hdcs
->array
.left
+ (hdcs
->array
.width
- width
) / 2;
310 win
[2] = (y
+ height
) / 4 - 1;
311 win
[3] = (x
+ width
) / 4 - 1;
313 err
= hdcs_reg_write_seq(sd
, HDCS_FWROW
, win
, 4);
317 /* Update the current width and height */
323 static int hdcs_s_ctrl(struct v4l2_ctrl
*ctrl
)
325 struct gspca_dev
*gspca_dev
=
326 container_of(ctrl
->handler
, struct gspca_dev
, ctrl_handler
);
331 err
= hdcs_set_gain(gspca_dev
, ctrl
->val
);
333 case V4L2_CID_EXPOSURE
:
334 err
= hdcs_set_exposure(gspca_dev
, ctrl
->val
);
340 static const struct v4l2_ctrl_ops hdcs_ctrl_ops
= {
341 .s_ctrl
= hdcs_s_ctrl
,
344 static int hdcs_init_controls(struct sd
*sd
)
346 struct v4l2_ctrl_handler
*hdl
= &sd
->gspca_dev
.ctrl_handler
;
348 v4l2_ctrl_handler_init(hdl
, 2);
349 v4l2_ctrl_new_std(hdl
, &hdcs_ctrl_ops
,
350 V4L2_CID_EXPOSURE
, 0, 0xff, 1, HDCS_DEFAULT_EXPOSURE
);
351 v4l2_ctrl_new_std(hdl
, &hdcs_ctrl_ops
,
352 V4L2_CID_GAIN
, 0, 0xff, 1, HDCS_DEFAULT_GAIN
);
356 static int hdcs_probe_1x00(struct sd
*sd
)
362 ret
= stv06xx_read_sensor(sd
, HDCS_IDENT
, &sensor
);
363 if (ret
< 0 || sensor
!= 0x08)
366 pr_info("HDCS-1000/1100 sensor detected\n");
368 sd
->gspca_dev
.cam
.cam_mode
= hdcs1x00_mode
;
369 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(hdcs1x00_mode
);
371 hdcs
= kmalloc(sizeof(struct hdcs
), GFP_KERNEL
);
375 hdcs
->array
.left
= 8;
377 hdcs
->array
.width
= HDCS_1X00_DEF_WIDTH
;
378 hdcs
->array
.height
= HDCS_1X00_DEF_HEIGHT
;
379 hdcs
->array
.border
= 4;
387 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
388 * 4 = doesn't work at all
396 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
397 * 15 = doesn't work at all
398 * 18 = doesn't work at all
406 hdcs
->psmp
= (sd
->bridge
== BRIDGE_STV602
) ? 20 : 5;
408 sd
->sensor_priv
= hdcs
;
413 static int hdcs_probe_1020(struct sd
*sd
)
419 ret
= stv06xx_read_sensor(sd
, HDCS_IDENT
, &sensor
);
420 if (ret
< 0 || sensor
!= 0x10)
423 pr_info("HDCS-1020 sensor detected\n");
425 sd
->gspca_dev
.cam
.cam_mode
= hdcs1020_mode
;
426 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(hdcs1020_mode
);
428 hdcs
= kmalloc(sizeof(struct hdcs
), GFP_KERNEL
);
433 * From Andrey's test image: looks like HDCS-1020 upper-left
434 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
435 * visible pixel at 375,299 (x maybe even larger?)
437 hdcs
->array
.left
= 24;
439 hdcs
->array
.width
= HDCS_1020_DEF_WIDTH
;
440 hdcs
->array
.height
= 304;
441 hdcs
->array
.border
= 4;
450 sd
->sensor_priv
= hdcs
;
455 static int hdcs_start(struct sd
*sd
)
457 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
459 gspca_dbg(gspca_dev
, D_STREAM
, "Starting stream\n");
461 return hdcs_set_state(sd
, HDCS_STATE_RUN
);
464 static int hdcs_stop(struct sd
*sd
)
466 struct gspca_dev
*gspca_dev
= (struct gspca_dev
*)sd
;
468 gspca_dbg(gspca_dev
, D_STREAM
, "Halting stream\n");
470 return hdcs_set_state(sd
, HDCS_STATE_SLEEP
);
473 static int hdcs_init(struct sd
*sd
)
475 struct hdcs
*hdcs
= sd
->sensor_priv
;
478 /* Set the STV0602AA in STV0600 emulation mode */
479 if (sd
->bridge
== BRIDGE_STV602
)
480 stv06xx_write_bridge(sd
, STV_STV0600_EMULATION
, 1);
482 /* Execute the bridge init */
483 for (i
= 0; i
< ARRAY_SIZE(stv_bridge_init
) && !err
; i
++) {
484 err
= stv06xx_write_bridge(sd
, stv_bridge_init
[i
][0],
485 stv_bridge_init
[i
][1]);
490 /* sensor soft reset */
493 /* Execute the sensor init */
494 for (i
= 0; i
< ARRAY_SIZE(stv_sensor_init
) && !err
; i
++) {
495 err
= stv06xx_write_sensor(sd
, stv_sensor_init
[i
][0],
496 stv_sensor_init
[i
][1]);
501 /* Enable continuous frame capture, bit 2: stop when frame complete */
502 err
= stv06xx_write_sensor(sd
, HDCS_REG_CONFIG(sd
), BIT(3));
506 /* Set PGA sample duration
507 (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
509 err
= stv06xx_write_sensor(sd
, HDCS_TCTRL
,
510 (HDCS_ADC_START_SIG_DUR
<< 6) | hdcs
->psmp
);
512 err
= stv06xx_write_sensor(sd
, HDCS_TCTRL
,
513 (HDCS_ADC_START_SIG_DUR
<< 5) | hdcs
->psmp
);
517 return hdcs_set_size(sd
, hdcs
->array
.width
, hdcs
->array
.height
);
520 static int hdcs_dump(struct sd
*sd
)
524 pr_info("Dumping sensor registers:\n");
526 for (reg
= HDCS_IDENT
; reg
<= HDCS_ROWEXPH
; reg
++) {
527 stv06xx_read_sensor(sd
, reg
, &val
);
528 pr_info("reg 0x%02x = 0x%02x\n", reg
, val
);