]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/gspca/stv06xx/stv06xx_hdcs.c
wireless: remove duplicated .ndo_set_mac_address
[linux-2.6-omap-h63xx.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 enum hdcs_power_state {
34         HDCS_STATE_SLEEP,
35         HDCS_STATE_IDLE,
36         HDCS_STATE_RUN
37 };
38
39 /* no lock? */
40 struct hdcs {
41         enum hdcs_power_state state;
42         int w, h;
43
44         /* visible area of the sensor array */
45         struct {
46                 int left, top;
47                 int width, height;
48                 int border;
49         } array;
50
51         struct {
52                 /* Column timing overhead */
53                 u8 cto;
54                 /* Column processing overhead */
55                 u8 cpo;
56                 /* Row sample period constant */
57                 u16 rs;
58                 /* Exposure reset duration */
59                 u16 er;
60         } exp;
61
62         int psmp;
63 };
64
65 static int hdcs_reg_write_seq(struct sd *sd, u8 reg, u8 *vals, u8 len)
66 {
67         u8 regs[I2C_MAX_BYTES * 2];
68         int i;
69
70         if (unlikely((len <= 0) || (len >= I2C_MAX_BYTES) ||
71                      (reg + len > 0xff)))
72                 return -EINVAL;
73
74         for (i = 0; i < len; i++, reg++) {
75                 regs[2*i] = reg;
76                 regs[2*i+1] = vals[i];
77         }
78
79         return stv06xx_write_sensor_bytes(sd, regs, len);
80 }
81
82 static int hdcs_set_state(struct sd *sd, enum hdcs_power_state state)
83 {
84         struct hdcs *hdcs = sd->sensor_priv;
85         u8 val;
86         int ret;
87
88         if (hdcs->state == state)
89                 return 0;
90
91         /* we need to go idle before running or sleeping */
92         if (hdcs->state != HDCS_STATE_IDLE) {
93                 ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
94                 if (ret)
95                         return ret;
96         }
97
98         hdcs->state = HDCS_STATE_IDLE;
99
100         if (state == HDCS_STATE_IDLE)
101                 return 0;
102
103         switch (state) {
104         case HDCS_STATE_SLEEP:
105                 val = HDCS_SLEEP_MODE;
106                 break;
107
108         case HDCS_STATE_RUN:
109                 val = HDCS_RUN_ENABLE;
110                 break;
111
112         default:
113                 return -EINVAL;
114         }
115
116         ret = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), val);
117         if (ret < 0)
118                 hdcs->state = state;
119
120         return ret;
121 }
122
123 static int hdcs_reset(struct sd *sd)
124 {
125         struct hdcs *hdcs = sd->sensor_priv;
126         int err;
127
128         err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 1);
129         if (err < 0)
130                 return err;
131
132         err = stv06xx_write_sensor(sd, HDCS_REG_CONTROL(sd), 0);
133         if (err < 0)
134                 hdcs->state = HDCS_STATE_IDLE;
135
136         return err;
137 }
138
139 static int hdcs_get_exposure(struct gspca_dev *gspca_dev, __s32 *val)
140 {
141         struct sd *sd = (struct sd *) gspca_dev;
142         struct hdcs *hdcs = sd->sensor_priv;
143
144         /* Column time period */
145         int ct;
146         /* Column processing period */
147         int cp;
148         /* Row processing period */
149         int rp;
150         int cycles;
151         int err;
152         int rowexp;
153         u16 data[2];
154
155         err = stv06xx_read_sensor(sd, HDCS_ROWEXPL, &data[0]);
156         if (err < 0)
157                 return err;
158
159         err = stv06xx_read_sensor(sd, HDCS_ROWEXPH, &data[1]);
160         if (err < 0)
161                 return err;
162
163         rowexp = (data[1] << 8) | data[0];
164
165         ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
166         cp = hdcs->exp.cto + (hdcs->w * ct / 2);
167         rp = hdcs->exp.rs + cp;
168
169         cycles = rp * rowexp;
170         *val = cycles / HDCS_CLK_FREQ_MHZ;
171         PDEBUG(D_V4L2, "Read exposure %d", *val);
172         return 0;
173 }
174
175 static int hdcs_set_exposure(struct gspca_dev *gspca_dev, __s32 val)
176 {
177         struct sd *sd = (struct sd *) gspca_dev;
178         struct hdcs *hdcs = sd->sensor_priv;
179         int rowexp, srowexp;
180         int max_srowexp;
181         /* Column time period */
182         int ct;
183         /* Column processing period */
184         int cp;
185         /* Row processing period */
186         int rp;
187         /* Minimum number of column timing periods
188            within the column processing period */
189         int mnct;
190         int cycles, err;
191         u8 exp[4];
192
193         cycles = val * HDCS_CLK_FREQ_MHZ;
194
195         ct = hdcs->exp.cto + hdcs->psmp + (HDCS_ADC_START_SIG_DUR + 2);
196         cp = hdcs->exp.cto + (hdcs->w * ct / 2);
197
198         /* the cycles one row takes */
199         rp = hdcs->exp.rs + cp;
200
201         rowexp = cycles / rp;
202
203         /* the remaining cycles */
204         cycles -= rowexp * rp;
205
206         /* calculate sub-row exposure */
207         if (IS_1020(sd)) {
208                 /* see HDCS-1020 datasheet 3.5.6.4, p. 63 */
209                 srowexp = hdcs->w - (cycles + hdcs->exp.er + 13) / ct;
210
211                 mnct = (hdcs->exp.er + 12 + ct - 1) / ct;
212                 max_srowexp = hdcs->w - mnct;
213         } else {
214                 /* see HDCS-1000 datasheet 3.4.5.5, p. 61 */
215                 srowexp = cp - hdcs->exp.er - 6 - cycles;
216
217                 mnct = (hdcs->exp.er + 5 + ct - 1) / ct;
218                 max_srowexp = cp - mnct * ct - 1;
219         }
220
221         if (srowexp < 0)
222                 srowexp = 0;
223         else if (srowexp > max_srowexp)
224                 srowexp = max_srowexp;
225
226         if (IS_1020(sd)) {
227                 exp[0] = rowexp & 0xff;
228                 exp[1] = rowexp >> 8;
229                 exp[2] = (srowexp >> 2) & 0xff;
230                 /* this clears exposure error flag */
231                 exp[3] = 0x1;
232                 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
233         } else {
234                 exp[0] = rowexp & 0xff;
235                 exp[1] = rowexp >> 8;
236                 exp[2] = srowexp & 0xff;
237                 exp[3] = srowexp >> 8;
238                 err = hdcs_reg_write_seq(sd, HDCS_ROWEXPL, exp, 4);
239                 if (err < 0)
240                         return err;
241
242                 /* clear exposure error flag */
243                 err = stv06xx_write_sensor(sd,
244                      HDCS_STATUS, BIT(4));
245         }
246         PDEBUG(D_V4L2, "Writing exposure %d, rowexp %d, srowexp %d",
247                val, rowexp, srowexp);
248         return err;
249 }
250
251 static int hdcs_set_gains(struct sd *sd, u8 r, u8 g, u8 b)
252 {
253         u8 gains[4];
254
255         /* the voltage gain Av = (1 + 19 * val / 127) * (1 + bit7) */
256         if (r > 127)
257                 r = 0x80 | (r / 2);
258         if (g > 127)
259                 g = 0x80 | (g / 2);
260         if (b > 127)
261                 b = 0x80 | (b / 2);
262
263         gains[0] = g;
264         gains[1] = r;
265         gains[2] = b;
266         gains[3] = g;
267
268         return hdcs_reg_write_seq(sd, HDCS_ERECPGA, gains, 4);
269 }
270
271 static int hdcs_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
272 {
273         struct sd *sd = (struct sd *) gspca_dev;
274         int err;
275         u16 data;
276
277         err = stv06xx_read_sensor(sd, HDCS_ERECPGA, &data);
278
279         /* Bit 7 doubles the gain */
280         if (data & 0x80)
281                 *val = (data & 0x7f) * 2;
282         else
283                 *val = data;
284
285         PDEBUG(D_V4L2, "Read gain %d", *val);
286         return err;
287 }
288
289 static int hdcs_set_gain(struct gspca_dev *gspca_dev, __s32 val)
290 {
291         PDEBUG(D_V4L2, "Writing gain %d", val);
292         return hdcs_set_gains((struct sd *) gspca_dev,
293                                val & 0xff, val & 0xff, val & 0xff);
294 }
295
296 static int hdcs_set_size(struct sd *sd,
297                 unsigned int width, unsigned int height)
298 {
299         struct hdcs *hdcs = sd->sensor_priv;
300         u8 win[4];
301         unsigned int x, y;
302         int err;
303
304         /* must be multiple of 4 */
305         width = (width + 3) & ~0x3;
306         height = (height + 3) & ~0x3;
307
308         if (width > hdcs->array.width)
309                 width = hdcs->array.width;
310
311         if (IS_1020(sd)) {
312                 /* the borders are also invalid */
313                 if (height + 2 * hdcs->array.border + HDCS_1020_BOTTOM_Y_SKIP
314                                   > hdcs->array.height)
315                         height = hdcs->array.height - 2 * hdcs->array.border -
316                                 HDCS_1020_BOTTOM_Y_SKIP;
317
318                 y = (hdcs->array.height - HDCS_1020_BOTTOM_Y_SKIP - height) / 2
319                                 + hdcs->array.top;
320         } else {
321                 if (height > hdcs->array.height)
322                         height = hdcs->array.height;
323
324                 y = hdcs->array.top + (hdcs->array.height - height) / 2;
325         }
326
327         x = hdcs->array.left + (hdcs->array.width - width) / 2;
328
329         win[0] = y / 4;
330         win[1] = x / 4;
331         win[2] = (y + height) / 4 - 1;
332         win[3] = (x + width) / 4 - 1;
333
334         err = hdcs_reg_write_seq(sd, HDCS_FWROW, win, 4);
335         if (err < 0)
336                 return err;
337
338         /* Update the current width and height */
339         hdcs->w = width;
340         hdcs->h = height;
341         return err;
342 }
343
344 static int hdcs_probe_1x00(struct sd *sd)
345 {
346         struct hdcs *hdcs;
347         u16 sensor;
348         int ret;
349
350         ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
351         if (ret < 0 || sensor != 0x08)
352                 return -ENODEV;
353
354         info("HDCS-1000/1100 sensor detected");
355
356         sd->gspca_dev.cam.cam_mode = stv06xx_sensor_hdcs1x00.modes;
357         sd->gspca_dev.cam.nmodes = stv06xx_sensor_hdcs1x00.nmodes;
358         sd->desc.ctrls = stv06xx_sensor_hdcs1x00.ctrls;
359         sd->desc.nctrls = stv06xx_sensor_hdcs1x00.nctrls;
360
361         hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
362         if (!hdcs)
363                 return -ENOMEM;
364
365         hdcs->array.left = 8;
366         hdcs->array.top = 8;
367         hdcs->array.width = HDCS_1X00_DEF_WIDTH;
368         hdcs->array.height = HDCS_1X00_DEF_HEIGHT;
369         hdcs->array.border = 4;
370
371         hdcs->exp.cto = 4;
372         hdcs->exp.cpo = 2;
373         hdcs->exp.rs = 186;
374         hdcs->exp.er = 100;
375
376         /*
377          * Frame rate on HDCS-1000 0x46D:0x840 depends on PSMP:
378          *  4 = doesn't work at all
379          *  5 = 7.8 fps,
380          *  6 = 6.9 fps,
381          *  8 = 6.3 fps,
382          * 10 = 5.5 fps,
383          * 15 = 4.4 fps,
384          * 31 = 2.8 fps
385          *
386          * Frame rate on HDCS-1000 0x46D:0x870 depends on PSMP:
387          * 15 = doesn't work at all
388          * 18 = doesn't work at all
389          * 19 = 7.3 fps
390          * 20 = 7.4 fps
391          * 21 = 7.4 fps
392          * 22 = 7.4 fps
393          * 24 = 6.3 fps
394          * 30 = 5.4 fps
395          */
396         hdcs->psmp = IS_870(sd) ? 20 : 5;
397
398         sd->sensor_priv = hdcs;
399
400         return 0;
401 }
402
403 static int hdcs_probe_1020(struct sd *sd)
404 {
405         struct hdcs *hdcs;
406         u16 sensor;
407         int ret;
408
409         ret = stv06xx_read_sensor(sd, HDCS_IDENT, &sensor);
410         if (ret < 0 || sensor != 0x10)
411                 return -ENODEV;
412
413         info("HDCS-1020 sensor detected");
414
415         sd->gspca_dev.cam.cam_mode = stv06xx_sensor_hdcs1020.modes;
416         sd->gspca_dev.cam.nmodes = stv06xx_sensor_hdcs1020.nmodes;
417         sd->desc.ctrls = stv06xx_sensor_hdcs1020.ctrls;
418         sd->desc.nctrls = stv06xx_sensor_hdcs1020.nctrls;
419
420         hdcs = kmalloc(sizeof(struct hdcs), GFP_KERNEL);
421         if (!hdcs)
422                 return -ENOMEM;
423
424         /*
425          * From Andrey's test image: looks like HDCS-1020 upper-left
426          * visible pixel is at 24,8 (y maybe even smaller?) and lower-right
427          * visible pixel at 375,299 (x maybe even larger?)
428          */
429         hdcs->array.left = 24;
430         hdcs->array.top  = 4;
431         hdcs->array.width = HDCS_1020_DEF_WIDTH;
432         hdcs->array.height = 304;
433         hdcs->array.border = 4;
434
435         hdcs->psmp = 6;
436
437         hdcs->exp.cto = 3;
438         hdcs->exp.cpo = 3;
439         hdcs->exp.rs = 155;
440         hdcs->exp.er = 96;
441
442         sd->sensor_priv = hdcs;
443
444         return 0;
445 }
446
447 static int hdcs_start(struct sd *sd)
448 {
449         PDEBUG(D_STREAM, "Starting stream");
450
451         return hdcs_set_state(sd, HDCS_STATE_RUN);
452 }
453
454 static int hdcs_stop(struct sd *sd)
455 {
456         PDEBUG(D_STREAM, "Halting stream");
457
458         return hdcs_set_state(sd, HDCS_STATE_SLEEP);
459 }
460
461 static void hdcs_disconnect(struct sd *sd)
462 {
463         PDEBUG(D_PROBE, "Disconnecting the sensor");
464         kfree(sd->sensor_priv);
465 }
466
467 static int hdcs_init(struct sd *sd)
468 {
469         struct hdcs *hdcs = sd->sensor_priv;
470         int i, err = 0;
471
472         /* Set the STV0602AA in STV0600 emulation mode */
473         if (IS_870(sd))
474                 stv06xx_write_bridge(sd, STV_STV0600_EMULATION, 1);
475
476         /* Execute the bridge init */
477         for (i = 0; i < ARRAY_SIZE(stv_bridge_init) && !err; i++) {
478                 err = stv06xx_write_bridge(sd, stv_bridge_init[i][0],
479                                            stv_bridge_init[i][1]);
480         }
481         if (err < 0)
482                 return err;
483
484         /* sensor soft reset */
485         hdcs_reset(sd);
486
487         /* Execute the sensor init */
488         for (i = 0; i < ARRAY_SIZE(stv_sensor_init) && !err; i++) {
489                 err = stv06xx_write_sensor(sd, stv_sensor_init[i][0],
490                                              stv_sensor_init[i][1]);
491         }
492         if (err < 0)
493                 return err;
494
495         /* Enable continous frame capture, bit 2: stop when frame complete */
496         err = stv06xx_write_sensor(sd, HDCS_REG_CONFIG(sd), BIT(3));
497         if (err < 0)
498                 return err;
499
500         /* Set PGA sample duration
501         (was 0x7E for IS_870, but caused slow framerate with HDCS-1020) */
502         if (IS_1020(sd))
503                 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
504                                 (HDCS_ADC_START_SIG_DUR << 6) | hdcs->psmp);
505         else
506                 err = stv06xx_write_sensor(sd, HDCS_TCTRL,
507                                 (HDCS_ADC_START_SIG_DUR << 5) | hdcs->psmp);
508         if (err < 0)
509                 return err;
510
511         err = hdcs_set_gains(sd, HDCS_DEFAULT_GAIN, HDCS_DEFAULT_GAIN,
512                              HDCS_DEFAULT_GAIN);
513         if (err < 0)
514                 return err;
515
516         err = hdcs_set_exposure(&sd->gspca_dev, HDCS_DEFAULT_EXPOSURE);
517         if (err < 0)
518                 return err;
519
520         err = hdcs_set_size(sd, hdcs->array.width, hdcs->array.height);
521         return err;
522 }
523
524 static int hdcs_dump(struct sd *sd)
525 {
526         u16 reg, val;
527
528         info("Dumping sensor registers:");
529
530         for (reg = HDCS_IDENT; reg <= HDCS_ROWEXPH; reg++) {
531                 stv06xx_read_sensor(sd, reg, &val);
532                 info("reg 0x%02x = 0x%02x", reg, val);
533         }
534         return 0;
535 }