blob: ec7f5536a8ad71c86b4b90b7d4f12d82be7832da [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,
67 V4L2_PIX_FMT_SBGGR8,
68 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,
83 V4L2_PIX_FMT_SBGGR8,
84 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
134 for (i = 0; i < len; i++, reg++) {
135 regs[2*i] = reg;
136 regs[2*i+1] = vals[i];
137 }
138
139 return stv06xx_write_sensor_bytes(sd, regs, len);
140}
141
142static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
143{
144 struct hdcs *hdcs = sd->sensor_priv;
145 u8 val;
146 int ret;
147
148 if (hdcs->state == state)
149 return 0;
150
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);
154 if (ret)
155 return ret;
156 }
157
158 hdcs->state = HDCS_STATE_IDLE;
159
160 if (state == HDCS_STATE_IDLE)
161 return 0;
162
163 switch (state) {
164 case HDCS_STATE_SLEEP:
165 val = HDCS_SLEEP_MODE;
166 break;
167
168 case HDCS_STATE_RUN:
169 val = HDCS_RUN_ENABLE;
170 break;
171
172 default:
173 return -EINVAL;
174 }
175
176 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
Erik Andrén36a516d2009-06-23 12:22:48 -0300177
178 /* Update the state if the write succeeded */
179 if (!ret)
Erik Andren4c988342008-12-29 07:35:23 -0300180 hdcs->state = state;
181
182 return ret;
183}
184
185static int hdcs_reset(struct sd *sd)
186{
187 struct hdcs *hdcs = sd->sensor_priv;
188 int err;
189
190 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
191 if (err < 0)
192 return err;
193
194 err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
195 if (err < 0)
196 hdcs->state = HDCS_STATE_IDLE;
197
198 return err;
199}
200
201static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
202{
203 struct sd *sd = (struct sd *) gspca_dev;
204 struct hdcs *hdcs = sd->sensor_priv;
205
206 /* Column time period */
207 int ct;
208 /* Column processing period */
209 int cp;
210 /* Row processing period */
211 int rp;
212 int cycles;
213 int err;
214 int rowexp;
215 u16 data[2];
216
217 err = stv06xx_read_sensor(sd, HDCS_ROWEXPL, &data[0]);
218 if (err < 0)
219 return err;
220
221 err = stv06xx_read_sensor(sd, HDCS_ROWEXPH, &data[1]);
222 if (err < 0)
223 return err;
224
225 rowexp = (data[1] << 8) | data[0];
226
227 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
228 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
229 rp = hdcs->exp.rs + cp;
230
231 cycles = rp * rowexp;
232 *val = cycles / HDCS_CLK_FREQ_MHZ;
233 PDEBUG(D_V4L2, "Read exposure %d", *val);
234 return 0;
235}
236
237static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
238{
239 struct sd *sd = (struct sd *) gspca_dev;
240 struct hdcs *hdcs = sd->sensor_priv;
241 int rowexp, srowexp;
242 int max_srowexp;
243 /* Column time period */
244 int ct;
245 /* Column processing period */
246 int cp;
247 /* Row processing period */
248 int rp;
249 /* Minimum number of column timing periods
250 within the column processing period */
251 int mnct;
252 int cycles, err;
253 u8 exp[4];
254
255 cycles = val * HDCS_CLK_FREQ_MHZ;
256
257 ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
258 cp = hdcs->exp.cto + (hdcs->w * ct / 2);
259
260 /* the cycles one row takes */
261 rp = hdcs->exp.rs + cp;
262
263 rowexp = cycles / rp;
264
265 /* the remaining cycles */
266 cycles -= rowexp * rp;
267
268 /* calculate sub-row exposure */
269 if (IS_1020(sd)) {
270 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
271 srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
272
273 mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
274 max_srowexp = hdcs->w - mnct;
275 } else {
276 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
277 srowexp = cp - hdcs->exp.er - 6 - cycles;
278
279 mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
280 max_srowexp = cp - mnct * ct - 1;
281 }
282
283 if (srowexp < 0)
284 srowexp = 0;
285 else if (srowexp > max_srowexp)
286 srowexp = max_srowexp;
287
288 if (IS_1020(sd)) {
289 exp[0] = rowexp & 0xff;
290 exp[1] = rowexp >> 8;
291 exp[2] = (srowexp >> 2) & 0xff;
292 /* this clears exposure error flag */
293 exp[3] = 0x1;
294 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
295 } else {
296 exp[0] = rowexp & 0xff;
297 exp[1] = rowexp >> 8;
298 exp[2] = srowexp & 0xff;
299 exp[3] = srowexp >> 8;
300 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
301 if (err < 0)
302 return err;
303
304 /* clear exposure error flag */
305 err = stv06xx_write_sensor(sd,
306 HDCS_STATUS, BIT(4));
307 }
308 PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
309 val, rowexp, srowexp);
310 return err;
311}
312
313static int hdcs_set_gains(struct sd *sd, u8 r, u8 g, u8 b)
314{
315 u8 gains[4];
316
317 /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
318 if (r > 127)
319 r = 0x80 | (r / 2);
320 if (g > 127)
321 g = 0x80 | (g / 2);
322 if (b > 127)
323 b = 0x80 | (b / 2);
324
325 gains[0] = g;
326 gains[1] = r;
327 gains[2] = b;
328 gains[3] = g;
329
330 return hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
331}
332
333static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
334{
335 struct sd *sd = (struct sd *) gspca_dev;
336 int err;
337 u16 data;
338
339 err = stv06xx_read_sensor(sd, HDCS_ERECPGA, &data);
340
341 /* Bit 7 doubles the gain */
342 if (data & 0x80)
343 *val = (data & 0x7f) * 2;
344 else
345 *val = data;
346
347 PDEBUG(D_V4L2, "Read gain %d", *val);
348 return err;
349}
350
351static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
352{
353 PDEBUG(D_V4L2, "Writing gain %d", val);
354 return hdcs_set_gains((struct sd *) gspca_dev,
355 val & 0xff, val & 0xff, val & 0xff);
356}
357
358static int hdcs_set_size(struct sd *sd,
359 unsigned int width, unsigned int height)
360{
361 struct hdcs *hdcs = sd->sensor_priv;
362 u8 win[4];
363 unsigned int x, y;
364 int err;
365
366 /* must be multiple of 4 */
367 width = (width + 3) & ~0x3;
368 height = (height + 3) & ~0x3;
369
370 if (width > hdcs->array.width)
371 width = hdcs->array.width;
372
373 if (IS_1020(sd)) {
374 /* the borders are also invalid */
375 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
376 > hdcs->array.height)
377 height = hdcs->array.height - 2 * hdcs->array.border -
378 HDCS_1020_BOTTOM_Y_SKIP;
379
380 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
381 + hdcs->array.top;
Erik Andrén1970f142008-12-30 04:58:36 -0300382 } else {
383 if (height > hdcs->array.height)
384 height = hdcs->array.height;
385
Erik Andren4c988342008-12-29 07:35:23 -0300386 y = hdcs->array.top + (hdcs->array.height - height) / 2;
387 }
388
389 x = hdcs->array.left + (hdcs->array.width - width) / 2;
390
391 win[0] = y / 4;
392 win[1] = x / 4;
393 win[2] = (y + height) / 4 - 1;
394 win[3] = (x + width) / 4 - 1;
395
396 err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
397 if (err < 0)
398 return err;
399
400 /* Update the current width and height */
401 hdcs->w = width;
402 hdcs->h = height;
403 return err;
404}
405
406static int hdcs_probe_1x00(struct sd *sd)
407{
408 struct hdcs *hdcs;
409 u16 sensor;
410 int ret;
411
412 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
413 if (ret < 0 || sensor != 0x08)
414 return -ENODEV;
415
416 info("HDCS-1000/1100 sensor detected");
417
Erik Andren766231a2008-12-31 14:33:53 -0300418 sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
419 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
420 sd->desc.ctrls = hdcs1x00_ctrl;
421 sd->desc.nctrls = ARRAY_SIZE(hdcs1x00_ctrl);
Erik Andren4c988342008-12-29 07:35:23 -0300422
423 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
424 if (!hdcs)
425 return -ENOMEM;
426
427 hdcs->array.left = 8;
428 hdcs->array.top = 8;
429 hdcs->array.width = HDCS_1X00_DEF_WIDTH;
430 hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
431 hdcs->array.border = 4;
432
433 hdcs->exp.cto = 4;
434 hdcs->exp.cpo = 2;
435 hdcs->exp.rs = 186;
436 hdcs->exp.er = 100;
437
438 /*
Hans de Goede8668d502009-06-17 18:37:57 -0300439 * Frame rate on HDCS-1000 with STV600 depends on PSMP:
Erik Andren4c988342008-12-29 07:35:23 -0300440 * 4 = doesn't work at all
441 * 5 = 7.8 fps,
442 * 6 = 6.9 fps,
443 * 8 = 6.3 fps,
444 * 10 = 5.5 fps,
445 * 15 = 4.4 fps,
446 * 31 = 2.8 fps
447 *
Hans de Goede8668d502009-06-17 18:37:57 -0300448 * Frame rate on HDCS-1000 with STV602 depends on PSMP:
Erik Andren4c988342008-12-29 07:35:23 -0300449 * 15 = doesn't work at all
450 * 18 = doesn't work at all
451 * 19 = 7.3 fps
452 * 20 = 7.4 fps
453 * 21 = 7.4 fps
454 * 22 = 7.4 fps
455 * 24 = 6.3 fps
456 * 30 = 5.4 fps
457 */
Hans de Goede8668d502009-06-17 18:37:57 -0300458 hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
Erik Andren4c988342008-12-29 07:35:23 -0300459
460 sd->sensor_priv = hdcs;
461
462 return 0;
463}
464
465static int hdcs_probe_1020(struct sd *sd)
466{
467 struct hdcs *hdcs;
468 u16 sensor;
469 int ret;
470
471 ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
472 if (ret < 0 || sensor != 0x10)
473 return -ENODEV;
474
475 info("HDCS-1020 sensor detected");
476
Erik Andren766231a2008-12-31 14:33:53 -0300477 sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
478 sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
479 sd->desc.ctrls = hdcs1020_ctrl;
480 sd->desc.nctrls = ARRAY_SIZE(hdcs1020_ctrl);
Erik Andren4c988342008-12-29 07:35:23 -0300481
482 hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
483 if (!hdcs)
484 return -ENOMEM;
485
486 /*
487 * From Andrey's test image: looks like HDCS-1020 upper-left
488 * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
489 * visible pixel at 375,299 (x maybe even larger?)
490 */
491 hdcs->array.left = 24;
492 hdcs->array.top = 4;
493 hdcs->array.width = HDCS_1020_DEF_WIDTH;
494 hdcs->array.height = 304;
495 hdcs->array.border = 4;
496
497 hdcs->psmp = 6;
498
499 hdcs->exp.cto = 3;
500 hdcs->exp.cpo = 3;
501 hdcs->exp.rs = 155;
502 hdcs->exp.er = 96;
503
504 sd->sensor_priv = hdcs;
505
506 return 0;
507}
508
509static int hdcs_start(struct sd *sd)
510{
511 PDEBUG(D_STREAM, "Starting stream");
512
513 return hdcs_set_state(sd, HDCS_STATE_RUN);
514}
515
516static int hdcs_stop(struct sd *sd)
517{
518 PDEBUG(D_STREAM, "Halting stream");
519
520 return hdcs_set_state(sd, HDCS_STATE_SLEEP);
521}
522
523static void hdcs_disconnect(struct sd *sd)
524{
525 PDEBUG(D_PROBE, "Disconnecting the sensor");
526 kfree(sd->sensor_priv);
527}
528
529static int hdcs_init(struct sd *sd)
530{
531 struct hdcs *hdcs = sd->sensor_priv;
532 int i, err = 0;
533
534 /* Set the STV0602AA in STV0600 emulation mode */
Hans de Goede8668d502009-06-17 18:37:57 -0300535 if (sd->bridge == BRIDGE_STV602)
Erik Andren4c988342008-12-29 07:35:23 -0300536 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
537
538 /* Execute the bridge init */
539 for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
540 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
541 stv_bridge_init[i][1]);
542 }
543 if (err < 0)
544 return err;
545
546 /* sensor soft reset */
547 hdcs_reset(sd);
548
549 /* Execute the sensor init */
550 for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
551 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
552 stv_sensor_init[i][1]);
553 }
554 if (err < 0)
555 return err;
556
557 /* Enable continous frame capture, bit 2: stop when frame complete */
558 err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
559 if (err < 0)
560 return err;
561
562 /* Set PGA sample duration
Hans de Goede8668d502009-06-17 18:37:57 -0300563 (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
Erik Andren4c988342008-12-29 07:35:23 -0300564 if (IS_1020(sd))
565 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
566 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
567 else
568 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
569 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
570 if (err < 0)
571 return err;
572
573 err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN, HDCS_DEFAULT_GAIN,
574 HDCS_DEFAULT_GAIN);
575 if (err < 0)
576 return err;
577
578 err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
579 if (err < 0)
580 return err;
581
582 err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
583 return err;
584}
585
586static int hdcs_dump(struct sd *sd)
587{
588 u16 reg, val;
589
590 info("Dumping sensor registers:");
591
592 for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
593 stv06xx_read_sensor(sd, reg, &val);
594 info("reg 0x%02x = 0x%02x", reg, val);
595 }
596 return 0;
597}