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 enum hdcs_power_state {
41 enum hdcs_power_state state;
44 /* visible area of the sensor array */
52 /* Column timing overhead */
54 /* Column processing overhead */
56 /* Row sample period constant */
58 /* Exposure reset duration */
65 static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)
67 u8 regs[I2C_MAX_BYTES * 2];
70 if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||
74 for (i = 0; i < len; i++, reg++) {
76 regs[2*i+1] = vals[i];
79 return stv06xx_write_sensor_bytes(sd, regs, len);
82 static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
84 struct hdcs *hdcs = sd->sensor_priv;
88 if (hdcs->state == state)
91 /* we need to go idle before running or sleeping */
92 if (hdcs->state != HDCS_STATE_IDLE) {
93 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
98 hdcs->state = HDCS_STATE_IDLE;
100 if (state == HDCS_STATE_IDLE)
104 case HDCS_STATE_SLEEP:
105 val = HDCS_SLEEP_MODE;
109 val = HDCS_RUN_ENABLE;
116 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
123 static int hdcs_reset(struct sd *sd)
125 struct hdcs *hdcs = sd->sensor_priv;
128 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
132 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
134 hdcs->state = HDCS_STATE_IDLE;
139 static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
141 struct sd *sd = (struct sd *) gspca_dev;
142 struct hdcs *hdcs = sd->sensor_priv;
144 /* Column time period */
146 /* Column processing period */
148 /* Row processing period */
155 err = stv06xx_read_sensor(sd, HDCS_ROWEXPL, &data[0]);
159 err = stv06xx_read_sensor(sd, HDCS_ROWEXPH, &data[1]);
163 rowexp = (data[1] << 8) | data[0];
165 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
166 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
167 rp = hdcs->exp.rs + cp;
169 cycles = rp * rowexp;
170 *val = cycles / HDCS_CLK_FREQ_MHZ;
171 PDEBUG(D_V4L2, "Read exposure %d", *val);
175 static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
177 struct sd *sd = (struct sd *) gspca_dev;
178 struct hdcs *hdcs = sd->sensor_priv;
181 /* Column time period */
183 /* Column processing period */
185 /* Row processing period */
187 /* Minimum number of column timing periods
188 within the column processing period */
193 cycles = val * HDCS_CLK_FREQ_MHZ;
195 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
196 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
198 /* the cycles one row takes */
199 rp = hdcs->exp.rs + cp;
201 rowexp = cycles / rp;
203 /* the remaining cycles */
204 cycles -= rowexp * rp;
206 /* calculate sub-row exposure */
208 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
209 srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
211 mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
212 max_srowexp = hdcs->w - mnct;
214 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
215 srowexp = cp - hdcs->exp.er - 6 - cycles;
217 mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
218 max_srowexp = cp - mnct * ct - 1;
223 else if (srowexp > max_srowexp)
224 srowexp = max_srowexp;
227 exp[0] = rowexp & 0xff;
228 exp[1] = rowexp >> 8;
229 exp[2] = (srowexp >> 2) & 0xff;
230 /* this clears exposure error flag */
232 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
234 exp[0] = rowexp & 0xff;
235 exp[1] = rowexp >> 8;
236 exp[2] = srowexp & 0xff;
237 exp[3] = srowexp >> 8;
238 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
242 /* clear exposure error flag */
243 err = stv06xx_write_sensor(sd,
244 HDCS_STATUS, BIT(4));
246 PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
247 val, rowexp, srowexp);
251 static int hdcs_set_gains(struct sd *sd, u8 r, u8 g, u8 b)
255 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
268 return hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
271 static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
273 struct sd *sd = (struct sd *) gspca_dev;
277 err = stv06xx_read_sensor(sd, HDCS_ERECPGA, &data);
279 /* Bit 7 doubles the gain */
281 *val = (data & 0x7f) * 2;
285 PDEBUG(D_V4L2, "Read gain %d", *val);
289 static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
291 PDEBUG(D_V4L2, "Writing gain %d", val);
292 return hdcs_set_gains((struct sd *) gspca_dev,
293 val & 0xff, val & 0xff, val & 0xff);
296 static int hdcs_set_size(struct sd *sd,
297 unsigned int width, unsigned int height)
299 struct hdcs *hdcs = sd->sensor_priv;
304 /* must be multiple of 4 */
305 width = (width + 3) & ~0x3;
306 height = (height + 3) & ~0x3;
308 if (width > hdcs->array.width)
309 width = hdcs->array.width;
312 /* the borders are also invalid */
313 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
314 > hdcs->array.height)
315 height = hdcs->array.height - 2 * hdcs->array.border -
316 HDCS_1020_BOTTOM_Y_SKIP;
318 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
321 if (height > hdcs->array.height)
322 height = hdcs->array.height;
324 y = hdcs->array.top + (hdcs->array.height - height) / 2;
327 x = hdcs->array.left + (hdcs->array.width - width) / 2;
331 win[2] = (y + height) / 4 - 1;
332 win[3] = (x + width) / 4 - 1;
334 err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
338 /* Update the current width and height */
344 static int hdcs_probe_1x00(struct sd *sd)
350 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
351 if (ret < 0 || sensor != 0x08)
354 info("HDCS-1000/1100 sensor detected");
356 sd->gspca_dev.cam.cam_mode = stv06xx_sensor_hdcs1x00.modes;
357 sd->gspca_dev.cam.nmodes = stv06xx_sensor_hdcs1x00.nmodes;
358 sd->desc.ctrls = stv06xx_sensor_hdcs1x00.ctrls;
359 sd->desc.nctrls = stv06xx_sensor_hdcs1x00.nctrls;
361 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
365 hdcs->array.left = 8;
367 hdcs->array.width = HDCS_1X00_DEF_WIDTH;
368 hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
369 hdcs->array.border = 4;
377 * Frame rate on HDCS-1000 0x46D:0x840 depends on PSMP:
378 * 4 = doesn't work at all
386 * Frame rate on HDCS-1000 0x46D:0x870 depends on PSMP:
387 * 15 = doesn't work at all
388 * 18 = doesn't work at all
396 hdcs->psmp = IS_870(sd) ? 20 : 5;
398 sd->sensor_priv = hdcs;
403 static int hdcs_probe_1020(struct sd *sd)
409 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
410 if (ret < 0 || sensor != 0x10)
413 info("HDCS-1020 sensor detected");
415 sd->gspca_dev.cam.cam_mode = stv06xx_sensor_hdcs1020.modes;
416 sd->gspca_dev.cam.nmodes = stv06xx_sensor_hdcs1020.nmodes;
417 sd->desc.ctrls = stv06xx_sensor_hdcs1020.ctrls;
418 sd->desc.nctrls = stv06xx_sensor_hdcs1020.nctrls;
420 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
425 * From Andrey's test image: looks like HDCS-1020 upper-left
426 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
427 * visible pixel at 375,299 (x maybe even larger?)
429 hdcs->array.left = 24;
431 hdcs->array.width = HDCS_1020_DEF_WIDTH;
432 hdcs->array.height = 304;
433 hdcs->array.border = 4;
442 sd->sensor_priv = hdcs;
447 static int hdcs_start(struct sd *sd)
449 PDEBUG(D_STREAM, "Starting stream");
451 return hdcs_set_state(sd, HDCS_STATE_RUN);
454 static int hdcs_stop(struct sd *sd)
456 PDEBUG(D_STREAM, "Halting stream");
458 return hdcs_set_state(sd, HDCS_STATE_SLEEP);
461 static void hdcs_disconnect(struct sd *sd)
463 PDEBUG(D_PROBE, "Disconnecting the sensor");
464 kfree(sd->sensor_priv);
467 static int hdcs_init(struct sd *sd)
469 struct hdcs *hdcs = sd->sensor_priv;
472 /* Set the STV0602AA in STV0600 emulation mode */
474 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
476 /* Execute the bridge init */
477 for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
478 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
479 stv_bridge_init[i][1]);
484 /* sensor soft reset */
487 /* Execute the sensor init */
488 for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
489 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
490 stv_sensor_init[i][1]);
495 /* Enable continous frame capture, bit 2: stop when frame complete */
496 err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
500 /* Set PGA sample duration
501 (was 0x7E for IS_870, but caused slow framerate with HDCS-1020) */
503 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
504 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
506 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
507 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
511 err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN, HDCS_DEFAULT_GAIN,
516 err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
520 err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
524 static int hdcs_dump(struct sd *sd)
528 info("Dumping sensor registers:");
530 for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
531 stv06xx_read_sensor(sd, reg, &val);
532 info("reg 0x%02x = 0x%02x", reg, val);