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