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