blob: e5024c8496ef75ddd5f35f2f325b77c0c8c4d853 [file] [log] [blame]
Erik Andren4c988342008-12-29 07:35:23 -03001/*
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
7 *
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.
12 *
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.
17 *
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
21 *
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
29 */
30
31#include "stv06xx_hdcs.h"
32
Erik Andren766231a2008-12-31 14:33:53 -030033static const struct ctrl hdcs1x00_ctrl[] = {
34 {
35 {
36 .id = V4L2_CID_EXPOSURE,
37 .type = V4L2_CTRL_TYPE_INTEGER,
38 .name = "exposure",
39 .minimum = 0x00,
40 .maximum = 0xffff,
41 .step = 0x1,
42 .default_value = HDCS_DEFAULT_EXPOSURE,
43 .flags = V4L2_CTRL_FLAG_SLIDER
44 },
45 .set = hdcs_set_exposure,
46 .get = hdcs_get_exposure
47 }, {
48 {
49 .id = V4L2_CID_GAIN,
50 .type = V4L2_CTRL_TYPE_INTEGER,
51 .name = "gain",
52 .minimum = 0x00,
53 .maximum = 0xff,
54 .step = 0x1,
55 .default_value = HDCS_DEFAULT_GAIN,
56 .flags = V4L2_CTRL_FLAG_SLIDER
57 },
58 .set = hdcs_set_gain,
59 .get = hdcs_get_gain
60 }
61};
62
63static struct v4l2_pix_format hdcs1x00_mode[] = {
64 {
65 HDCS_1X00_DEF_WIDTH,
66 HDCS_1X00_DEF_HEIGHT,
Erik Andrénc0ea8f52009-07-01 02:56:44 -030067 V4L2_PIX_FMT_SGRBG8,
Erik Andren766231a2008-12-31 14:33:53 -030068 V4L2_FIELD_NONE,
69 .sizeimage =
70 HDCS_1X00_DEF_WIDTH * HDCS_1X00_DEF_HEIGHT,
71 .bytesperline = HDCS_1X00_DEF_WIDTH,
72 .colorspace = V4L2_COLORSPACE_SRGB,
73 .priv = 1
74 }
75};
76
77static const struct ctrl hdcs1020_ctrl[] = {};
78
79static struct v4l2_pix_format hdcs1020_mode[] = {
80 {
81 HDCS_1020_DEF_WIDTH,
82 HDCS_1020_DEF_HEIGHT,
Erik Andrénc0ea8f52009-07-01 02:56:44 -030083 V4L2_PIX_FMT_SGRBG8,
Erik Andren766231a2008-12-31 14:33:53 -030084 V4L2_FIELD_NONE,
85 .sizeimage =
86 HDCS_1020_DEF_WIDTH * HDCS_1020_DEF_HEIGHT,
87 .bytesperline = HDCS_1020_DEF_WIDTH,
88 .colorspace = V4L2_COLORSPACE_SRGB,
89 .priv = 1
90 }
91};
92
Erik Andren4c988342008-12-29 07:35:23 -030093enum hdcs_power_state {
94 HDCS_STATE_SLEEP,
95 HDCS_STATE_IDLE,
96 HDCS_STATE_RUN
97};
98
99/* no lock? */
100struct hdcs {
101 enum hdcs_power_state state;
102 int w, h;
103
104 /* visible area of the sensor array */
105 struct {
106 int left, top;
107 int width, height;
108 int border;
109 } array;
110
111 struct {
112 /* Column timing overhead */
113 u8 cto;
114 /* Column processing overhead */
115 u8 cpo;
116 /* Row sample period constant */
117 u16 rs;
118 /* Exposure reset duration */
119 u16 er;
120 } exp;
121
122 int psmp;
123};
124
125static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)
126{
127 u8 regs[I2C_MAX_BYTES * 2];
128 int i;
129
130 if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||
131 (reg + len > 0xff)))
132 return -EINVAL;
133
Erik Andrénac512952009-06-24 04:30:56 -0300134 for (i = 0; i < len; i++) {
135 regs[2 * i] = reg;
136 regs[2 * i + 1] = vals[i];
137 /* All addresses are shifted left one bit as bit 0 toggles r/w */
138 reg += 2;
Erik Andren4c988342008-12-29 07:35:23 -0300139 }
140
141 return stv06xx_write_sensor_bytes(sd, regs, len);
142}
143
144static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
145{
146 struct hdcs *hdcs = sd->sensor_priv;
147 u8 val;
148 int ret;
149
150 if (hdcs->state == state)
151 return 0;
152
153 /* we need to go idle before running or sleeping */
154 if (hdcs->state != HDCS_STATE_IDLE) {
155 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
156 if (ret)
157 return ret;
158 }
159
160 hdcs->state = HDCS_STATE_IDLE;
161
162 if (state == HDCS_STATE_IDLE)
163 return 0;
164
165 switch (state) {
166 case HDCS_STATE_SLEEP:
167 val = HDCS_SLEEP_MODE;
168 break;
169
170 case HDCS_STATE_RUN:
171 val = HDCS_RUN_ENABLE;
172 break;
173
174 default:
175 return -EINVAL;
176 }
177
178 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
Erik Andrén36a516d2009-06-23 12:22:48 -0300179
180 /* Update the state if the write succeeded */
181 if (!ret)
Erik Andren4c988342008-12-29 07:35:23 -0300182 hdcs->state = state;
183
184 return ret;
185}
186
187static int hdcs_reset(struct sd *sd)
188{
189 struct hdcs *hdcs = sd->sensor_priv;
190 int err;
191
192 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
193 if (err < 0)
194 return err;
195
196 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
197 if (err < 0)
198 hdcs->state = HDCS_STATE_IDLE;
199
200 return err;
201}
202
203static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
204{
205 struct sd *sd = (struct sd *) gspca_dev;
206 struct hdcs *hdcs = sd->sensor_priv;
207
208 /* Column time period */
209 int ct;
210 /* Column processing period */
211 int cp;
212 /* Row processing period */
213 int rp;
214 int cycles;
215 int err;
216 int rowexp;
217 u16 data[2];
218
219 err = stv06xx_read_sensor(sd, HDCS_ROWEXPL, &data[0]);
220 if (err < 0)
221 return err;
222
223 err = stv06xx_read_sensor(sd, HDCS_ROWEXPH, &data[1]);
224 if (err < 0)
225 return err;
226
227 rowexp = (data[1] << 8) | data[0];
228
229 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
230 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
231 rp = hdcs->exp.rs + cp;
232
233 cycles = rp * rowexp;
234 *val = cycles / HDCS_CLK_FREQ_MHZ;
235 PDEBUG(D_V4L2, "Read exposure %d", *val);
236 return 0;
237}
238
239static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
240{
241 struct sd *sd = (struct sd *) gspca_dev;
242 struct hdcs *hdcs = sd->sensor_priv;
243 int rowexp, srowexp;
244 int max_srowexp;
245 /* Column time period */
246 int ct;
247 /* Column processing period */
248 int cp;
249 /* Row processing period */
250 int rp;
251 /* Minimum number of column timing periods
252 within the column processing period */
253 int mnct;
254 int cycles, err;
255 u8 exp[4];
256
257 cycles = val * HDCS_CLK_FREQ_MHZ;
258
259 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
260 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
261
262 /* the cycles one row takes */
263 rp = hdcs->exp.rs + cp;
264
265 rowexp = cycles / rp;
266
267 /* the remaining cycles */
268 cycles -= rowexp * rp;
269
270 /* calculate sub-row exposure */
271 if (IS_1020(sd)) {
272 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
273 srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
274
275 mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
276 max_srowexp = hdcs->w - mnct;
277 } else {
278 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
279 srowexp = cp - hdcs->exp.er - 6 - cycles;
280
281 mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
282 max_srowexp = cp - mnct * ct - 1;
283 }
284
285 if (srowexp < 0)
286 srowexp = 0;
287 else if (srowexp > max_srowexp)
288 srowexp = max_srowexp;
289
290 if (IS_1020(sd)) {
291 exp[0] = rowexp & 0xff;
292 exp[1] = rowexp >> 8;
293 exp[2] = (srowexp >> 2) & 0xff;
294 /* this clears exposure error flag */
295 exp[3] = 0x1;
296 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
297 } else {
298 exp[0] = rowexp & 0xff;
299 exp[1] = rowexp >> 8;
300 exp[2] = srowexp & 0xff;
301 exp[3] = srowexp >> 8;
302 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
303 if (err < 0)
304 return err;
305
306 /* clear exposure error flag */
307 err = stv06xx_write_sensor(sd,
308 HDCS_STATUS, BIT(4));
309 }
310 PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
311 val, rowexp, srowexp);
312 return err;
313}
314
315static int hdcs_set_gains(struct sd *sd, u8 r, u8 g, u8 b)
316{
317 u8 gains[4];
318
319 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
320 if (r > 127)
321 r = 0x80 | (r / 2);
322 if (g > 127)
323 g = 0x80 | (g / 2);
324 if (b > 127)
325 b = 0x80 | (b / 2);
326
327 gains[0] = g;
328 gains[1] = r;
329 gains[2] = b;
330 gains[3] = g;
331
332 return hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
333}
334
335static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
336{
337 struct sd *sd = (struct sd *) gspca_dev;
338 int err;
339 u16 data;
340
341 err = stv06xx_read_sensor(sd, HDCS_ERECPGA, &data);
342
343 /* Bit 7 doubles the gain */
344 if (data & 0x80)
345 *val = (data & 0x7f) * 2;
346 else
347 *val = data;
348
349 PDEBUG(D_V4L2, "Read gain %d", *val);
350 return err;
351}
352
353static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
354{
355 PDEBUG(D_V4L2, "Writing gain %d", val);
356 return hdcs_set_gains((struct sd *) gspca_dev,
357 val & 0xff, val & 0xff, val & 0xff);
358}
359
360static int hdcs_set_size(struct sd *sd,
361 unsigned int width, unsigned int height)
362{
363 struct hdcs *hdcs = sd->sensor_priv;
364 u8 win[4];
365 unsigned int x, y;
366 int err;
367
368 /* must be multiple of 4 */
369 width = (width + 3) & ~0x3;
370 height = (height + 3) & ~0x3;
371
372 if (width > hdcs->array.width)
373 width = hdcs->array.width;
374
375 if (IS_1020(sd)) {
376 /* the borders are also invalid */
377 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
378 > hdcs->array.height)
379 height = hdcs->array.height - 2 * hdcs->array.border -
380 HDCS_1020_BOTTOM_Y_SKIP;
381
382 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
383 + hdcs->array.top;
Erik Andrén1970f142008-12-30 04:58:36 -0300384 } else {
385 if (height > hdcs->array.height)
386 height = hdcs->array.height;
387
Erik Andren4c988342008-12-29 07:35:23 -0300388 y = hdcs->array.top + (hdcs->array.height - height) / 2;
389 }
390
391 x = hdcs->array.left + (hdcs->array.width - width) / 2;
392
393 win[0] = y / 4;
394 win[1] = x / 4;
395 win[2] = (y + height) / 4 - 1;
396 win[3] = (x + width) / 4 - 1;
397
398 err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
399 if (err < 0)
400 return err;
401
402 /* Update the current width and height */
403 hdcs->w = width;
404 hdcs->h = height;
405 return err;
406}
407
408static int hdcs_probe_1x00(struct sd *sd)
409{
410 struct hdcs *hdcs;
411 u16 sensor;
412 int ret;
413
414 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
415 if (ret < 0 || sensor != 0x08)
416 return -ENODEV;
417
418 info("HDCS-1000/1100 sensor detected");
419
Erik Andren766231a2008-12-31 14:33:53 -0300420 sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
421 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
422 sd->desc.ctrls = hdcs1x00_ctrl;
423 sd->desc.nctrls = ARRAY_SIZE(hdcs1x00_ctrl);
Erik Andren4c988342008-12-29 07:35:23 -0300424
425 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
426 if (!hdcs)
427 return -ENOMEM;
428
429 hdcs->array.left = 8;
430 hdcs->array.top = 8;
431 hdcs->array.width = HDCS_1X00_DEF_WIDTH;
432 hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
433 hdcs->array.border = 4;
434
435 hdcs->exp.cto = 4;
436 hdcs->exp.cpo = 2;
437 hdcs->exp.rs = 186;
438 hdcs->exp.er = 100;
439
440 /*
Hans de Goede8668d502009-06-17 18:37:57 -0300441 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
Erik Andren4c988342008-12-29 07:35:23 -0300442 * 4 = doesn't work at all
443 * 5 = 7.8 fps,
444 * 6 = 6.9 fps,
445 * 8 = 6.3 fps,
446 * 10 = 5.5 fps,
447 * 15 = 4.4 fps,
448 * 31 = 2.8 fps
449 *
Hans de Goede8668d502009-06-17 18:37:57 -0300450 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
Erik Andren4c988342008-12-29 07:35:23 -0300451 * 15 = doesn't work at all
452 * 18 = doesn't work at all
453 * 19 = 7.3 fps
454 * 20 = 7.4 fps
455 * 21 = 7.4 fps
456 * 22 = 7.4 fps
457 * 24 = 6.3 fps
458 * 30 = 5.4 fps
459 */
Hans de Goede8668d502009-06-17 18:37:57 -0300460 hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
Erik Andren4c988342008-12-29 07:35:23 -0300461
462 sd->sensor_priv = hdcs;
463
464 return 0;
465}
466
467static int hdcs_probe_1020(struct sd *sd)
468{
469 struct hdcs *hdcs;
470 u16 sensor;
471 int ret;
472
473 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
474 if (ret < 0 || sensor != 0x10)
475 return -ENODEV;
476
477 info("HDCS-1020 sensor detected");
478
Erik Andren766231a2008-12-31 14:33:53 -0300479 sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
480 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
481 sd->desc.ctrls = hdcs1020_ctrl;
482 sd->desc.nctrls = ARRAY_SIZE(hdcs1020_ctrl);
Erik Andren4c988342008-12-29 07:35:23 -0300483
484 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
485 if (!hdcs)
486 return -ENOMEM;
487
488 /*
489 * From Andrey's test image: looks like HDCS-1020 upper-left
490 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
491 * visible pixel at 375,299 (x maybe even larger?)
492 */
493 hdcs->array.left = 24;
494 hdcs->array.top = 4;
495 hdcs->array.width = HDCS_1020_DEF_WIDTH;
496 hdcs->array.height = 304;
497 hdcs->array.border = 4;
498
499 hdcs->psmp = 6;
500
501 hdcs->exp.cto = 3;
502 hdcs->exp.cpo = 3;
503 hdcs->exp.rs = 155;
504 hdcs->exp.er = 96;
505
506 sd->sensor_priv = hdcs;
507
508 return 0;
509}
510
511static int hdcs_start(struct sd *sd)
512{
513 PDEBUG(D_STREAM, "Starting stream");
514
515 return hdcs_set_state(sd, HDCS_STATE_RUN);
516}
517
518static int hdcs_stop(struct sd *sd)
519{
520 PDEBUG(D_STREAM, "Halting stream");
521
522 return hdcs_set_state(sd, HDCS_STATE_SLEEP);
523}
524
525static void hdcs_disconnect(struct sd *sd)
526{
527 PDEBUG(D_PROBE, "Disconnecting the sensor");
528 kfree(sd->sensor_priv);
529}
530
531static int hdcs_init(struct sd *sd)
532{
533 struct hdcs *hdcs = sd->sensor_priv;
534 int i, err = 0;
535
536 /* Set the STV0602AA in STV0600 emulation mode */
Hans de Goede8668d502009-06-17 18:37:57 -0300537 if (sd->bridge == BRIDGE_STV602)
Erik Andren4c988342008-12-29 07:35:23 -0300538 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
539
540 /* Execute the bridge init */
541 for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
542 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
543 stv_bridge_init[i][1]);
544 }
545 if (err < 0)
546 return err;
547
548 /* sensor soft reset */
549 hdcs_reset(sd);
550
551 /* Execute the sensor init */
552 for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
553 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
554 stv_sensor_init[i][1]);
555 }
556 if (err < 0)
557 return err;
558
559 /* Enable continous frame capture, bit 2: stop when frame complete */
560 err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
561 if (err < 0)
562 return err;
563
564 /* Set PGA sample duration
Hans de Goede8668d502009-06-17 18:37:57 -0300565 (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
Erik Andren4c988342008-12-29 07:35:23 -0300566 if (IS_1020(sd))
567 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
568 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
569 else
570 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
571 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
572 if (err < 0)
573 return err;
574
575 err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN, HDCS_DEFAULT_GAIN,
576 HDCS_DEFAULT_GAIN);
577 if (err < 0)
578 return err;
579
580 err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
581 if (err < 0)
582 return err;
583
584 err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
585 return err;
586}
587
588static int hdcs_dump(struct sd *sd)
589{
590 u16 reg, val;
591
592 info("Dumping sensor registers:");
593
594 for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
595 stv06xx_read_sensor(sd, reg, &val);
596 info("reg 0x%02x = 0x%02x", reg, val);
597 }
598 return 0;
599}