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++, reg++) {
136 regs[2*i+1] = vals[i];
139 return stv06xx_write_sensor_bytes(sd, regs, len);
142 static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
144 struct hdcs *hdcs = sd->sensor_priv;
148 if (hdcs->state == state)
151 /* we need to go idle before running or sleeping */
152 if (hdcs->state != HDCS_STATE_IDLE) {
153 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
158 hdcs->state = HDCS_STATE_IDLE;
160 if (state == HDCS_STATE_IDLE)
164 case HDCS_STATE_SLEEP:
165 val = HDCS_SLEEP_MODE;
169 val = HDCS_RUN_ENABLE;
176 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
183 static int hdcs_reset(struct sd *sd)
185 struct hdcs *hdcs = sd->sensor_priv;
188 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
192 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
194 hdcs->state = HDCS_STATE_IDLE;
199 static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
201 struct sd *sd = (struct sd *) gspca_dev;
202 struct hdcs *hdcs = sd->sensor_priv;
204 /* Column time period */
206 /* Column processing period */
208 /* Row processing period */
215 err = stv06xx_read_sensor(sd, HDCS_ROWEXPL, &data[0]);
219 err = stv06xx_read_sensor(sd, HDCS_ROWEXPH, &data[1]);
223 rowexp = (data[1] << 8) | data[0];
225 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
226 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
227 rp = hdcs->exp.rs + cp;
229 cycles = rp * rowexp;
230 *val = cycles / HDCS_CLK_FREQ_MHZ;
231 PDEBUG(D_V4L2, "Read exposure %d", *val);
235 static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
237 struct sd *sd = (struct sd *) gspca_dev;
238 struct hdcs *hdcs = sd->sensor_priv;
241 /* Column time period */
243 /* Column processing period */
245 /* Row processing period */
247 /* Minimum number of column timing periods
248 within the column processing period */
253 cycles = val * HDCS_CLK_FREQ_MHZ;
255 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
256 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
258 /* the cycles one row takes */
259 rp = hdcs->exp.rs + cp;
261 rowexp = cycles / rp;
263 /* the remaining cycles */
264 cycles -= rowexp * rp;
266 /* calculate sub-row exposure */
268 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
269 srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
271 mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
272 max_srowexp = hdcs->w - mnct;
274 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
275 srowexp = cp - hdcs->exp.er - 6 - cycles;
277 mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
278 max_srowexp = cp - mnct * ct - 1;
283 else if (srowexp > max_srowexp)
284 srowexp = max_srowexp;
287 exp[0] = rowexp & 0xff;
288 exp[1] = rowexp >> 8;
289 exp[2] = (srowexp >> 2) & 0xff;
290 /* this clears exposure error flag */
292 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
294 exp[0] = rowexp & 0xff;
295 exp[1] = rowexp >> 8;
296 exp[2] = srowexp & 0xff;
297 exp[3] = srowexp >> 8;
298 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
302 /* clear exposure error flag */
303 err = stv06xx_write_sensor(sd,
304 HDCS_STATUS, BIT(4));
306 PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
307 val, rowexp, srowexp);
311 static int hdcs_set_gains(struct sd *sd, u8 r, u8 g, u8 b)
315 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
328 return hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
331 static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
333 struct sd *sd = (struct sd *) gspca_dev;
337 err = stv06xx_read_sensor(sd, HDCS_ERECPGA, &data);
339 /* Bit 7 doubles the gain */
341 *val = (data & 0x7f) * 2;
345 PDEBUG(D_V4L2, "Read gain %d", *val);
349 static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
351 PDEBUG(D_V4L2, "Writing gain %d", val);
352 return hdcs_set_gains((struct sd *) gspca_dev,
353 val & 0xff, val & 0xff, val & 0xff);
356 static int hdcs_set_size(struct sd *sd,
357 unsigned int width, unsigned int height)
359 struct hdcs *hdcs = sd->sensor_priv;
364 /* must be multiple of 4 */
365 width = (width + 3) & ~0x3;
366 height = (height + 3) & ~0x3;
368 if (width > hdcs->array.width)
369 width = hdcs->array.width;
372 /* the borders are also invalid */
373 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
374 > hdcs->array.height)
375 height = hdcs->array.height - 2 * hdcs->array.border -
376 HDCS_1020_BOTTOM_Y_SKIP;
378 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
381 if (height > hdcs->array.height)
382 height = hdcs->array.height;
384 y = hdcs->array.top + (hdcs->array.height - height) / 2;
387 x = hdcs->array.left + (hdcs->array.width - width) / 2;
391 win[2] = (y + height) / 4 - 1;
392 win[3] = (x + width) / 4 - 1;
394 err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
398 /* Update the current width and height */
404 static int hdcs_probe_1x00(struct sd *sd)
410 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
411 if (ret < 0 || sensor != 0x08)
414 info("HDCS-1000/1100 sensor detected");
416 sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
417 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
418 sd->desc.ctrls = hdcs1x00_ctrl;
419 sd->desc.nctrls = ARRAY_SIZE(hdcs1x00_ctrl);
421 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
425 hdcs->array.left = 8;
427 hdcs->array.width = HDCS_1X00_DEF_WIDTH;
428 hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
429 hdcs->array.border = 4;
437 * Frame rate on HDCS-1000 0x46D:0x840 depends on PSMP:
438 * 4 = doesn't work at all
446 * Frame rate on HDCS-1000 0x46D:0x870 depends on PSMP:
447 * 15 = doesn't work at all
448 * 18 = doesn't work at all
456 hdcs->psmp = IS_870(sd) ? 20 : 5;
458 sd->sensor_priv = hdcs;
463 static int hdcs_probe_1020(struct sd *sd)
469 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
470 if (ret < 0 || sensor != 0x10)
473 info("HDCS-1020 sensor detected");
475 sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
476 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
477 sd->desc.ctrls = hdcs1020_ctrl;
478 sd->desc.nctrls = ARRAY_SIZE(hdcs1020_ctrl);
480 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
485 * From Andrey's test image: looks like HDCS-1020 upper-left
486 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
487 * visible pixel at 375,299 (x maybe even larger?)
489 hdcs->array.left = 24;
491 hdcs->array.width = HDCS_1020_DEF_WIDTH;
492 hdcs->array.height = 304;
493 hdcs->array.border = 4;
502 sd->sensor_priv = hdcs;
507 static int hdcs_start(struct sd *sd)
509 PDEBUG(D_STREAM, "Starting stream");
511 return hdcs_set_state(sd, HDCS_STATE_RUN);
514 static int hdcs_stop(struct sd *sd)
516 PDEBUG(D_STREAM, "Halting stream");
518 return hdcs_set_state(sd, HDCS_STATE_SLEEP);
521 static void hdcs_disconnect(struct sd *sd)
523 PDEBUG(D_PROBE, "Disconnecting the sensor");
524 kfree(sd->sensor_priv);
527 static int hdcs_init(struct sd *sd)
529 struct hdcs *hdcs = sd->sensor_priv;
532 /* Set the STV0602AA in STV0600 emulation mode */
534 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
536 /* Execute the bridge init */
537 for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
538 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
539 stv_bridge_init[i][1]);
544 /* sensor soft reset */
547 /* Execute the sensor init */
548 for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
549 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
550 stv_sensor_init[i][1]);
555 /* Enable continous frame capture, bit 2: stop when frame complete */
556 err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
560 /* Set PGA sample duration
561 (was 0x7E for IS_870, but caused slow framerate with HDCS-1020) */
563 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
564 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
566 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
567 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
571 err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN, HDCS_DEFAULT_GAIN,
576 err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
580 err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
584 static int hdcs_dump(struct sd *sd)
588 info("Dumping sensor registers:");
590 for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
591 stv06xx_read_sensor(sd, reg, &val);
592 info("reg 0x%02x = 0x%02x", reg, val);