V4L/DVB (13008): gspca - stv06xx-hdcs: Fixup exposure
[pandora-kernel.git] / drivers / media / video / gspca / stv06xx / stv06xx_hdcs.c
1 /*
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
33 static 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
63 static struct v4l2_pix_format hdcs1x00_mode[] = {
64         {
65                 HDCS_1X00_DEF_WIDTH,
66                 HDCS_1X00_DEF_HEIGHT,
67                 V4L2_PIX_FMT_SGRBG8,
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
77 static 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 };
106
107 static struct v4l2_pix_format hdcs1020_mode[] = {
108         {
109                 HDCS_1020_DEF_WIDTH,
110                 HDCS_1020_DEF_HEIGHT,
111                 V4L2_PIX_FMT_SGRBG8,
112                 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
121 enum hdcs_power_state {
122         HDCS_STATE_SLEEP,
123         HDCS_STATE_IDLE,
124         HDCS_STATE_RUN
125 };
126
127 /* no lock? */
128 struct 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
153 static 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
162         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;
167         }
168
169         return stv06xx_write_sensor_bytes(sd, regs, len);
170 }
171
172 static 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);
207
208         /* Update the state if the write succeeded */
209         if (!ret)
210                 hdcs->state = state;
211
212         return ret;
213 }
214
215 static 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
231 static 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
267 static 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[14];
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] = HDCS20_CONTROL;
320                 exp[1] = 0x00;          /* Stop streaming */
321                 exp[2] = HDCS_ROWEXPL;
322                 exp[3] = rowexp & 0xff;
323                 exp[4] = HDCS_ROWEXPH;
324                 exp[5] = rowexp >> 8;
325                 exp[6] = HDCS20_SROWEXP;
326                 exp[7] = (srowexp >> 2) & 0xff;
327                 exp[8] = HDCS20_ERROR;
328                 exp[9] = 0x10;          /* Clear exposure error flag*/
329                 exp[10] = HDCS20_CONTROL;
330                 exp[11] = 0x04;         /* Restart streaming */
331                 err = stv06xx_write_sensor_bytes(sd, exp, 6);
332         } else {
333                 exp[0] = HDCS00_CONTROL;
334                 exp[1] = 0x00;         /* Stop streaming */
335                 exp[2] = HDCS_ROWEXPL;
336                 exp[3] = rowexp & 0xff;
337                 exp[4] = HDCS_ROWEXPH;
338                 exp[5] = rowexp >> 8;
339                 exp[6] = HDCS00_SROWEXPL;
340                 exp[7] = srowexp & 0xff;
341                 exp[8] = HDCS00_SROWEXPH;
342                 exp[9] = srowexp >> 8;
343                 exp[10] = HDCS_STATUS;
344                 exp[11] = 0x10;         /* Clear exposure error flag*/
345                 exp[12] = HDCS00_CONTROL;
346                 exp[13] = 0x04;         /* Restart streaming */
347                 err = stv06xx_write_sensor_bytes(sd, exp, 7);
348                 if (err < 0)
349                         return err;
350         }
351         PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
352                val, rowexp, srowexp);
353         return err;
354 }
355
356 static int hdcs_set_gains(struct sd *sd, u8 r, u8 g, u8 b)
357 {
358         u8 gains[4];
359
360         /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
361         if (r > 127)
362                 r = 0x80 | (r / 2);
363         if (g > 127)
364                 g = 0x80 | (g / 2);
365         if (b > 127)
366                 b = 0x80 | (b / 2);
367
368         gains[0] = g;
369         gains[1] = r;
370         gains[2] = b;
371         gains[3] = g;
372
373         return hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
374 }
375
376 static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
377 {
378         struct sd *sd = (struct sd *) gspca_dev;
379         int err;
380         u16 data;
381
382         err = stv06xx_read_sensor(sd, HDCS_ERECPGA, &data);
383
384         /* Bit 7 doubles the gain */
385         if (data & 0x80)
386                 *val = (data & 0x7f) * 2;
387         else
388                 *val = data;
389
390         PDEBUG(D_V4L2, "Read gain %d", *val);
391         return err;
392 }
393
394 static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
395 {
396         PDEBUG(D_V4L2, "Writing gain %d", val);
397         return hdcs_set_gains((struct sd *) gspca_dev,
398                                val & 0xff, val & 0xff, val & 0xff);
399 }
400
401 static int hdcs_set_size(struct sd *sd,
402                 unsigned int width, unsigned int height)
403 {
404         struct hdcs *hdcs = sd->sensor_priv;
405         u8 win[4];
406         unsigned int x, y;
407         int err;
408
409         /* must be multiple of 4 */
410         width = (width + 3) & ~0x3;
411         height = (height + 3) & ~0x3;
412
413         if (width > hdcs->array.width)
414                 width = hdcs->array.width;
415
416         if (IS_1020(sd)) {
417                 /* the borders are also invalid */
418                 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
419                                   > hdcs->array.height)
420                         height = hdcs->array.height - 2 * hdcs->array.border -
421                                 HDCS_1020_BOTTOM_Y_SKIP;
422
423                 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
424                                 + hdcs->array.top;
425         } else {
426                 if (height > hdcs->array.height)
427                         height = hdcs->array.height;
428
429                 y = hdcs->array.top + (hdcs->array.height - height) / 2;
430         }
431
432         x = hdcs->array.left + (hdcs->array.width - width) / 2;
433
434         win[0] = y / 4;
435         win[1] = x / 4;
436         win[2] = (y + height) / 4 - 1;
437         win[3] = (x + width) / 4 - 1;
438
439         err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
440         if (err < 0)
441                 return err;
442
443         /* Update the current width and height */
444         hdcs->w = width;
445         hdcs->h = height;
446         return err;
447 }
448
449 static int hdcs_probe_1x00(struct sd *sd)
450 {
451         struct hdcs *hdcs;
452         u16 sensor;
453         int ret;
454
455         ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
456         if (ret < 0 || sensor != 0x08)
457                 return -ENODEV;
458
459         info("HDCS-1000/1100 sensor detected");
460
461         sd->gspca_dev.cam.cam_mode = hdcs1x00_mode;
462         sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1x00_mode);
463         sd->desc.ctrls = hdcs1x00_ctrl;
464         sd->desc.nctrls = ARRAY_SIZE(hdcs1x00_ctrl);
465
466         hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
467         if (!hdcs)
468                 return -ENOMEM;
469
470         hdcs->array.left = 8;
471         hdcs->array.top = 8;
472         hdcs->array.width = HDCS_1X00_DEF_WIDTH;
473         hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
474         hdcs->array.border = 4;
475
476         hdcs->exp.cto = 4;
477         hdcs->exp.cpo = 2;
478         hdcs->exp.rs = 186;
479         hdcs->exp.er = 100;
480
481         /*
482          * Frame rate on HDCS-1000 with STV600 depends on PSMP:
483          *  4 = doesn't work at all
484          *  5 = 7.8 fps,
485          *  6 = 6.9 fps,
486          *  8 = 6.3 fps,
487          * 10 = 5.5 fps,
488          * 15 = 4.4 fps,
489          * 31 = 2.8 fps
490          *
491          * Frame rate on HDCS-1000 with STV602 depends on PSMP:
492          * 15 = doesn't work at all
493          * 18 = doesn't work at all
494          * 19 = 7.3 fps
495          * 20 = 7.4 fps
496          * 21 = 7.4 fps
497          * 22 = 7.4 fps
498          * 24 = 6.3 fps
499          * 30 = 5.4 fps
500          */
501         hdcs->psmp = (sd->bridge == BRIDGE_STV602) ? 20 : 5;
502
503         sd->sensor_priv = hdcs;
504
505         return 0;
506 }
507
508 static int hdcs_probe_1020(struct sd *sd)
509 {
510         struct hdcs *hdcs;
511         u16 sensor;
512         int ret;
513
514         ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
515         if (ret < 0 || sensor != 0x10)
516                 return -ENODEV;
517
518         info("HDCS-1020 sensor detected");
519
520         sd->gspca_dev.cam.cam_mode = hdcs1020_mode;
521         sd->gspca_dev.cam.nmodes = ARRAY_SIZE(hdcs1020_mode);
522         sd->desc.ctrls = hdcs1020_ctrl;
523         sd->desc.nctrls = ARRAY_SIZE(hdcs1020_ctrl);
524
525         hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
526         if (!hdcs)
527                 return -ENOMEM;
528
529         /*
530          * From Andrey's test image: looks like HDCS-1020 upper-left
531          * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
532          * visible pixel at 375,299 (x maybe even larger?)
533          */
534         hdcs->array.left = 24;
535         hdcs->array.top  = 4;
536         hdcs->array.width = HDCS_1020_DEF_WIDTH;
537         hdcs->array.height = 304;
538         hdcs->array.border = 4;
539
540         hdcs->psmp = 6;
541
542         hdcs->exp.cto = 3;
543         hdcs->exp.cpo = 3;
544         hdcs->exp.rs = 155;
545         hdcs->exp.er = 96;
546
547         sd->sensor_priv = hdcs;
548
549         return 0;
550 }
551
552 static int hdcs_start(struct sd *sd)
553 {
554         PDEBUG(D_STREAM, "Starting stream");
555
556         return hdcs_set_state(sd, HDCS_STATE_RUN);
557 }
558
559 static int hdcs_stop(struct sd *sd)
560 {
561         PDEBUG(D_STREAM, "Halting stream");
562
563         return hdcs_set_state(sd, HDCS_STATE_SLEEP);
564 }
565
566 static void hdcs_disconnect(struct sd *sd)
567 {
568         PDEBUG(D_PROBE, "Disconnecting the sensor");
569         kfree(sd->sensor_priv);
570 }
571
572 static int hdcs_init(struct sd *sd)
573 {
574         struct hdcs *hdcs = sd->sensor_priv;
575         int i, err = 0;
576
577         /* Set the STV0602AA in STV0600 emulation mode */
578         if (sd->bridge == BRIDGE_STV602)
579                 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
580
581         /* Execute the bridge init */
582         for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
583                 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
584                                            stv_bridge_init[i][1]);
585         }
586         if (err < 0)
587                 return err;
588
589         /* sensor soft reset */
590         hdcs_reset(sd);
591
592         /* Execute the sensor init */
593         for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
594                 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
595                                              stv_sensor_init[i][1]);
596         }
597         if (err < 0)
598                 return err;
599
600         /* Enable continous frame capture, bit 2: stop when frame complete */
601         err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
602         if (err < 0)
603                 return err;
604
605         /* Set PGA sample duration
606         (was 0x7E for the STV602, but caused slow framerate with HDCS-1020) */
607         if (IS_1020(sd))
608                 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
609                                 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
610         else
611                 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
612                                 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
613         if (err < 0)
614                 return err;
615
616         err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN, HDCS_DEFAULT_GAIN,
617                              HDCS_DEFAULT_GAIN);
618         if (err < 0)
619                 return err;
620
621         err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
622         if (err < 0)
623                 return err;
624
625         err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
626         return err;
627 }
628
629 static int hdcs_dump(struct sd *sd)
630 {
631         u16 reg, val;
632
633         info("Dumping sensor registers:");
634
635         for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
636                 stv06xx_read_sensor(sd, reg, &val);
637                 info("reg 0x%02x = 0x%02x", reg, val);
638         }
639         return 0;
640 }