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 struct v4l2_pix_format hdcs1x00_mode[] = {
42 HDCS_1X00_DEF_WIDTH * HDCS_1X00_DEF_HEIGHT,
43 .bytesperline = HDCS_1X00_DEF_WIDTH,
44 .colorspace = V4L2_COLORSPACE_SRGB,
49 static struct v4l2_pix_format hdcs1020_mode[] = {
56 HDCS_1020_DEF_WIDTH * HDCS_1020_DEF_HEIGHT,
57 .bytesperline = HDCS_1020_DEF_WIDTH,
58 .colorspace = V4L2_COLORSPACE_SRGB,
63 enum hdcs_power_state {
71 enum hdcs_power_state state;
74 /* visible area of the sensor array */
82 /* Column timing overhead */
84 /* Column processing overhead */
86 /* Row sample period constant */
88 /* Exposure reset duration */
95 static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)
97 u8 regs[I2C_MAX_BYTES * 2];
100 if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||
104 for (i = 0; i < len; i++) {
106 regs[2 * i + 1] = vals[i];
107 /* All addresses are shifted left one bit
108 * as bit 0 toggles r/w */
112 return stv06xx_write_sensor_bytes(sd, regs, len);
115 static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
117 struct hdcs *hdcs = sd->sensor_priv;
121 if (hdcs->state == state)
124 /* we need to go idle before running or sleeping */
125 if (hdcs->state != HDCS_STATE_IDLE) {
126 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
131 hdcs->state = HDCS_STATE_IDLE;
133 if (state == HDCS_STATE_IDLE)
137 case HDCS_STATE_SLEEP:
138 val = HDCS_SLEEP_MODE;
142 val = HDCS_RUN_ENABLE;
149 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
151 /* Update the state if the write succeeded */
158 static int hdcs_reset(struct sd *sd)
160 struct hdcs *hdcs = sd->sensor_priv;
163 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
167 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
169 hdcs->state = HDCS_STATE_IDLE;
174 static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
176 struct sd *sd = (struct sd *) gspca_dev;
177 struct hdcs *hdcs = sd->sensor_priv;
180 /* Column time period */
182 /* Column processing period */
184 /* Row processing period */
186 /* Minimum number of column timing periods
187 within the column processing period */
192 cycles = val * HDCS_CLK_FREQ_MHZ * 257;
194 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
195 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
197 /* the cycles one row takes */
198 rp = hdcs->exp.rs + cp;
200 rowexp = cycles / rp;
202 /* the remaining cycles */
203 cycles -= rowexp * rp;
205 /* calculate sub-row exposure */
207 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
208 srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
210 mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
211 max_srowexp = hdcs->w - mnct;
213 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
214 srowexp = cp - hdcs->exp.er - 6 - cycles;
216 mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
217 max_srowexp = cp - mnct * ct - 1;
222 else if (srowexp > max_srowexp)
223 srowexp = max_srowexp;
226 exp[0] = HDCS20_CONTROL;
227 exp[1] = 0x00; /* Stop streaming */
228 exp[2] = HDCS_ROWEXPL;
229 exp[3] = rowexp & 0xff;
230 exp[4] = HDCS_ROWEXPH;
231 exp[5] = rowexp >> 8;
232 exp[6] = HDCS20_SROWEXP;
233 exp[7] = (srowexp >> 2) & 0xff;
234 exp[8] = HDCS20_ERROR;
235 exp[9] = 0x10; /* Clear exposure error flag*/
236 exp[10] = HDCS20_CONTROL;
237 exp[11] = 0x04; /* Restart streaming */
238 err = stv06xx_write_sensor_bytes(sd, exp, 6);
240 exp[0] = HDCS00_CONTROL;
241 exp[1] = 0x00; /* Stop streaming */
242 exp[2] = HDCS_ROWEXPL;
243 exp[3] = rowexp & 0xff;
244 exp[4] = HDCS_ROWEXPH;
245 exp[5] = rowexp >> 8;
246 exp[6] = HDCS00_SROWEXPL;
247 exp[7] = srowexp & 0xff;
248 exp[8] = HDCS00_SROWEXPH;
249 exp[9] = srowexp >> 8;
250 exp[10] = HDCS_STATUS;
251 exp[11] = 0x10; /* Clear exposure error flag*/
252 exp[12] = HDCS00_CONTROL;
253 exp[13] = 0x04; /* Restart streaming */
254 err = stv06xx_write_sensor_bytes(sd, exp, 7);
258 PDEBUG(D_CONF, "Writing exposure %d, rowexp %d, srowexp %d",
259 val, rowexp, srowexp);
263 static int hdcs_set_gains(struct sd *sd, u8 g)
268 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
277 err = hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
281 static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
283 PDEBUG(D_CONF, "Writing gain %d", val);
284 return hdcs_set_gains((struct sd *) gspca_dev,
288 static int hdcs_set_size(struct sd *sd,
289 unsigned int width, unsigned int height)
291 struct hdcs *hdcs = sd->sensor_priv;
296 /* must be multiple of 4 */
297 width = (width + 3) & ~0x3;
298 height = (height + 3) & ~0x3;
300 if (width > hdcs->array.width)
301 width = hdcs->array.width;
304 /* the borders are also invalid */
305 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
306 > hdcs->array.height)
307 height = hdcs->array.height - 2 * hdcs->array.border -
308 HDCS_1020_BOTTOM_Y_SKIP;
310 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
313 if (height > hdcs->array.height)
314 height = hdcs->array.height;
316 y = hdcs->array.top + (hdcs->array.height - height) / 2;
319 x = hdcs->array.left + (hdcs->array.width - width) / 2;
323 win[2] = (y + height) / 4 - 1;
324 win[3] = (x + width) / 4 - 1;
326 err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
330 /* Update the current width and height */
336 static int hdcs_s_ctrl(struct v4l2_ctrl *ctrl)
338 struct gspca_dev *gspca_dev =
339 container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
344 err = hdcs_set_gain(gspca_dev, ctrl->val);
346 case V4L2_CID_EXPOSURE:
347 err = hdcs_set_exposure(gspca_dev, ctrl->val);
353 static const struct v4l2_ctrl_ops hdcs_ctrl_ops = {
354 .s_ctrl = hdcs_s_ctrl,
357 static int hdcs_init_controls(struct sd *sd)
359 struct v4l2_ctrl_handler *hdl = &sd->gspca_dev.ctrl_handler;
361 v4l2_ctrl_handler_init(hdl, 2);
362 v4l2_ctrl_new_std(hdl, &hdcs_ctrl_ops,
363 V4L2_CID_EXPOSURE, 0, 0xff, 1, HDCS_DEFAULT_EXPOSURE);
364 v4l2_ctrl_new_std(hdl, &hdcs_ctrl_ops,
365 V4L2_CID_GAIN, 0, 0xff, 1, HDCS_DEFAULT_GAIN);
369 static int hdcs_probe_1x00(struct sd *sd)
375 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
376 if (ret < 0 || sensor != 0x08)
379 pr_info("HDCS-1000/1100 sensor detected\n");
381 sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
382 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
384 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
388 hdcs->array.left = 8;
390 hdcs->array.width = HDCS_1X00_DEF_WIDTH;
391 hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
392 hdcs->array.border = 4;
400 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
401 * 4 = doesn't work at all
409 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
410 * 15 = doesn't work at all
411 * 18 = doesn't work at all
419 hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
421 sd->sensor_priv = hdcs;
426 static int hdcs_probe_1020(struct sd *sd)
432 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
433 if (ret < 0 || sensor != 0x10)
436 pr_info("HDCS-1020 sensor detected\n");
438 sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
439 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
441 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
446 * From Andrey's test image: looks like HDCS-1020 upper-left
447 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
448 * visible pixel at 375,299 (x maybe even larger?)
450 hdcs->array.left = 24;
452 hdcs->array.width = HDCS_1020_DEF_WIDTH;
453 hdcs->array.height = 304;
454 hdcs->array.border = 4;
463 sd->sensor_priv = hdcs;
468 static int hdcs_start(struct sd *sd)
470 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
472 PDEBUG(D_STREAM, "Starting stream");
474 return hdcs_set_state(sd, HDCS_STATE_RUN);
477 static int hdcs_stop(struct sd *sd)
479 struct gspca_dev *gspca_dev = (struct gspca_dev *)sd;
481 PDEBUG(D_STREAM, "Halting stream");
483 return hdcs_set_state(sd, HDCS_STATE_SLEEP);
486 static int hdcs_init(struct sd *sd)
488 struct hdcs *hdcs = sd->sensor_priv;
491 /* Set the STV0602AA in STV0600 emulation mode */
492 if (sd->bridge == BRIDGE_STV602)
493 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
495 /* Execute the bridge init */
496 for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
497 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
498 stv_bridge_init[i][1]);
503 /* sensor soft reset */
506 /* Execute the sensor init */
507 for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
508 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
509 stv_sensor_init[i][1]);
514 /* Enable continuous frame capture, bit 2: stop when frame complete */
515 err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
519 /* Set PGA sample duration
520 (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
522 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
523 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
525 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
526 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
530 return hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
533 static int hdcs_dump(struct sd *sd)
537 pr_info("Dumping sensor registers:\n");
539 for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
540 stv06xx_read_sensor(sd, reg, &val);
541 pr_info("reg 0x%02x = 0x%02x\n", reg, val);