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 * P/N 861037: Sensor HDCS1000 ASIC STV0600
19 * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
20 * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
21 * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
22 * P/N 861075-0040: Sensor HDCS1000 ASIC
23 * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
24 * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include "stv06xx_hdcs.h"
31 static struct v4l2_pix_format hdcs1x00_mode[] = {
38 HDCS_1X00_DEF_WIDTH * HDCS_1X00_DEF_HEIGHT,
39 .bytesperline = HDCS_1X00_DEF_WIDTH,
40 .colorspace = V4L2_COLORSPACE_SRGB,
45 static struct v4l2_pix_format hdcs1020_mode[] = {
52 HDCS_1020_DEF_WIDTH * HDCS_1020_DEF_HEIGHT,
53 .bytesperline = HDCS_1020_DEF_WIDTH,
54 .colorspace = V4L2_COLORSPACE_SRGB,
59 enum hdcs_power_state {
67 enum hdcs_power_state state;
70 /* visible area of the sensor array */
78 /* Column timing overhead */
80 /* Column processing overhead */
82 /* Row sample period constant */
84 /* Exposure reset duration */
91 static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)
93 u8 regs[I2C_MAX_BYTES * 2];
96 if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||
100 for (i = 0; i < len; i++) {
102 regs[2 * i + 1] = vals[i];
103 /* All addresses are shifted left one bit
104 * as bit 0 toggles r/w */
108 return stv06xx_write_sensor_bytes(sd, regs, len);
111 static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
113 struct hdcs *hdcs = sd->sensor_priv;
117 if (hdcs->state == state)
120 /* we need to go idle before running or sleeping */
121 if (hdcs->state != HDCS_STATE_IDLE) {
122 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
127 hdcs->state = HDCS_STATE_IDLE;
129 if (state == HDCS_STATE_IDLE)
133 case HDCS_STATE_SLEEP:
134 val = HDCS_SLEEP_MODE;
138 val = HDCS_RUN_ENABLE;
145 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
147 /* Update the state if the write succeeded */
154 static int hdcs_reset(struct sd *sd)
156 struct hdcs *hdcs = sd->sensor_priv;
159 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
163 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
165 hdcs->state = HDCS_STATE_IDLE;
170 static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
172 struct sd *sd = (struct sd *) gspca_dev;
173 struct hdcs *hdcs = sd->sensor_priv;
176 /* Column time period */
178 /* Column processing period */
180 /* Row processing period */
182 /* Minimum number of column timing periods
183 within the column processing period */
188 cycles = val * HDCS_CLK_FREQ_MHZ * 257;
190 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
191 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
193 /* the cycles one row takes */
194 rp = hdcs->exp.rs + cp;
196 rowexp = cycles / rp;
198 /* the remaining cycles */
199 cycles -= rowexp * rp;
201 /* calculate sub-row exposure */
203 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
204 srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
206 mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
207 max_srowexp = hdcs->w - mnct;
209 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
210 srowexp = cp - hdcs->exp.er - 6 - cycles;
212 mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
213 max_srowexp = cp - mnct * ct - 1;
218 else if (srowexp > max_srowexp)
219 srowexp = max_srowexp;
222 exp[0] = HDCS20_CONTROL;
223 exp[1] = 0x00; /* Stop streaming */
224 exp[2] = HDCS_ROWEXPL;
225 exp[3] = rowexp & 0xff;
226 exp[4] = HDCS_ROWEXPH;
227 exp[5] = rowexp >> 8;
228 exp[6] = HDCS20_SROWEXP;
229 exp[7] = (srowexp >> 2) & 0xff;
230 exp[8] = HDCS20_ERROR;
231 exp[9] = 0x10; /* Clear exposure error flag*/
232 exp[10] = HDCS20_CONTROL;
233 exp[11] = 0x04; /* Restart streaming */
234 err = stv06xx_write_sensor_bytes(sd, exp, 6);
236 exp[0] = HDCS00_CONTROL;
237 exp[1] = 0x00; /* Stop streaming */
238 exp[2] = HDCS_ROWEXPL;
239 exp[3] = rowexp & 0xff;
240 exp[4] = HDCS_ROWEXPH;
241 exp[5] = rowexp >> 8;
242 exp[6] = HDCS00_SROWEXPL;
243 exp[7] = srowexp & 0xff;
244 exp[8] = HDCS00_SROWEXPH;
245 exp[9] = srowexp >> 8;
246 exp[10] = HDCS_STATUS;
247 exp[11] = 0x10; /* Clear exposure error flag*/
248 exp[12] = HDCS00_CONTROL;
249 exp[13] = 0x04; /* Restart streaming */
250 err = stv06xx_write_sensor_bytes(sd, exp, 7);
254 PDEBUG(D_CONF, "Writing exposure %d, rowexp %d, srowexp %d",
255 val, rowexp, srowexp);
259 static int hdcs_set_gains(struct sd *sd, u8 g)
264 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
273 err = hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
277 static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
279 PDEBUG(D_CONF, "Writing gain %d", val);
280 return hdcs_set_gains((struct sd *) gspca_dev,
284 static int hdcs_set_size(struct sd *sd,
285 unsigned int width, unsigned int height)
287 struct hdcs *hdcs = sd->sensor_priv;
292 /* must be multiple of 4 */
293 width = (width + 3) & ~0x3;
294 height = (height + 3) & ~0x3;
296 if (width > hdcs->array.width)
297 width = hdcs->array.width;
300 /* the borders are also invalid */
301 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
302 > hdcs->array.height)
303 height = hdcs->array.height - 2 * hdcs->array.border -
304 HDCS_1020_BOTTOM_Y_SKIP;
306 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
309 if (height > hdcs->array.height)
310 height = hdcs->array.height;
312 y = hdcs->array.top + (hdcs->array.height - height) / 2;
315 x = hdcs->array.left + (hdcs->array.width - width) / 2;
319 win[2] = (y + height) / 4 - 1;
320 win[3] = (x + width) / 4 - 1;
322 err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
326 /* Update the current width and height */
332 static int hdcs_s_ctrl(struct v4l2_ctrl *ctrl)
334 struct gspca_dev *gspca_dev =
335 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
340 err = hdcs_set_gain(gspca_dev, ctrl->val);
342 case V4L2_CID_EXPOSURE:
343 err = hdcs_set_exposure(gspca_dev, ctrl->val);
349 static const struct v4l2_ctrl_ops hdcs_ctrl_ops = {
350 .s_ctrl = hdcs_s_ctrl,
353 static int hdcs_init_controls(struct sd *sd)
355 struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
357 v4l2_ctrl_handler_init(hdl, 2);
358 v4l2_ctrl_new_std(hdl, &hdcs_ctrl_ops,
359 V4L2_CID_EXPOSURE, 0, 0xff, 1, HDCS_DEFAULT_EXPOSURE);
360 v4l2_ctrl_new_std(hdl, &hdcs_ctrl_ops,
361 V4L2_CID_GAIN, 0, 0xff, 1, HDCS_DEFAULT_GAIN);
365 static int hdcs_probe_1x00(struct sd *sd)
371 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
372 if (ret < 0 || sensor != 0x08)
375 pr_info("HDCS-1000/1100 sensor detected\n");
377 sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
378 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
380 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
384 hdcs->array.left = 8;
386 hdcs->array.width = HDCS_1X00_DEF_WIDTH;
387 hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
388 hdcs->array.border = 4;
396 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
397 * 4 = doesn't work at all
405 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
406 * 15 = doesn't work at all
407 * 18 = doesn't work at all
415 hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
417 sd->sensor_priv = hdcs;
422 static int hdcs_probe_1020(struct sd *sd)
428 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
429 if (ret < 0 || sensor != 0x10)
432 pr_info("HDCS-1020 sensor detected\n");
434 sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
435 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
437 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
442 * From Andrey's test image: looks like HDCS-1020 upper-left
443 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
444 * visible pixel at 375,299 (x maybe even larger?)
446 hdcs->array.left = 24;
448 hdcs->array.width = HDCS_1020_DEF_WIDTH;
449 hdcs->array.height = 304;
450 hdcs->array.border = 4;
459 sd->sensor_priv = hdcs;
464 static int hdcs_start(struct sd *sd)
466 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
468 PDEBUG(D_STREAM, "Starting stream");
470 return hdcs_set_state(sd, HDCS_STATE_RUN);
473 static int hdcs_stop(struct sd *sd)
475 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
477 PDEBUG(D_STREAM, "Halting stream");
479 return hdcs_set_state(sd, HDCS_STATE_SLEEP);
482 static int hdcs_init(struct sd *sd)
484 struct hdcs *hdcs = sd->sensor_priv;
487 /* Set the STV0602AA in STV0600 emulation mode */
488 if (sd->bridge == BRIDGE_STV602)
489 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
491 /* Execute the bridge init */
492 for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
493 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
494 stv_bridge_init[i][1]);
499 /* sensor soft reset */
502 /* Execute the sensor init */
503 for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
504 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
505 stv_sensor_init[i][1]);
510 /* Enable continuous frame capture, bit 2: stop when frame complete */
511 err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
515 /* Set PGA sample duration
516 (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
518 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
519 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
521 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
522 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
526 return hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
529 static int hdcs_dump(struct sd *sd)
533 pr_info("Dumping sensor registers:\n");
535 for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
536 stv06xx_read_sensor(sd, reg, &val);
537 pr_info("reg 0x%02x = 0x%02x\n", reg, val);