]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/usb/stkwebcam/stk-sensor.c
drm/etnaviv: Fix off-by-one error in reloc checking
[karo-tx-linux.git] / drivers / media / usb / stkwebcam / stk-sensor.c
1 /* stk-sensor.c: Driver for ov96xx sensor (used in some Syntek webcams)
2  *
3  * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
4  *
5  * Some parts derived from ov7670.c:
6  * Copyright 2006 One Laptop Per Child Association, Inc.  Written
7  * by Jonathan Corbet with substantial inspiration from Mark
8  * McClelland's ovcamchip code.
9  *
10  * Copyright 2006-7 Jonathan Corbet <corbet@lwn.net>
11  *
12  * This file may be distributed under the terms of the GNU General
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  */
23
24 /* Controlling the sensor via the STK1125 vendor specific control interface:
25  * The camera uses an OmniVision sensor and the stk1125 provides an
26  * SCCB(i2c)-USB bridge which let us program the sensor.
27  * In my case the sensor id is 0x9652, it can be read from sensor's register
28  * 0x0A and 0x0B as follows:
29  * - read register #R:
30  *   output #R to index 0x0208
31  *   output 0x0070 to index 0x0200
32  *   input 1 byte from index 0x0201 (some kind of status register)
33  *     until its value is 0x01
34  *   input 1 byte from index 0x0209. This is the value of #R
35  * - write value V to register #R
36  *   output #R to index 0x0204
37  *   output V to index 0x0205
38  *   output 0x0005 to index 0x0200
39  *   input 1 byte from index 0x0201 until its value becomes 0x04
40  */
41
42 /* It seems the i2c bus is controlled with these registers */
43
44 #include "stk-webcam.h"
45
46 #define STK_IIC_BASE            (0x0200)
47 #  define STK_IIC_OP            (STK_IIC_BASE)
48 #    define STK_IIC_OP_TX       (0x05)
49 #    define STK_IIC_OP_RX       (0x70)
50 #  define STK_IIC_STAT          (STK_IIC_BASE+1)
51 #    define STK_IIC_STAT_TX_OK  (0x04)
52 #    define STK_IIC_STAT_RX_OK  (0x01)
53 /* I don't know what does this register.
54  * when it is 0x00 or 0x01, we cannot talk to the sensor,
55  * other values work */
56 #  define STK_IIC_ENABLE        (STK_IIC_BASE+2)
57 #    define STK_IIC_ENABLE_NO   (0x00)
58 /* This is what the driver writes in windows */
59 #    define STK_IIC_ENABLE_YES  (0x1e)
60 /*
61  * Address of the slave. Seems like the binary driver look for the
62  * sensor in multiple places, attempting a reset sequence.
63  * We only know about the ov9650
64  */
65 #  define STK_IIC_ADDR          (STK_IIC_BASE+3)
66 #  define STK_IIC_TX_INDEX      (STK_IIC_BASE+4)
67 #  define STK_IIC_TX_VALUE      (STK_IIC_BASE+5)
68 #  define STK_IIC_RX_INDEX      (STK_IIC_BASE+8)
69 #  define STK_IIC_RX_VALUE      (STK_IIC_BASE+9)
70
71 #define MAX_RETRIES             (50)
72
73 #define SENSOR_ADDRESS          (0x60)
74
75 /* From ov7670.c (These registers aren't fully accurate) */
76
77 /* Registers */
78 #define REG_GAIN        0x00    /* Gain lower 8 bits (rest in vref) */
79 #define REG_BLUE        0x01    /* blue gain */
80 #define REG_RED         0x02    /* red gain */
81 #define REG_VREF        0x03    /* Pieces of GAIN, VSTART, VSTOP */
82 #define REG_COM1        0x04    /* Control 1 */
83 #define  COM1_CCIR656     0x40  /* CCIR656 enable */
84 #define  COM1_QFMT        0x20  /* QVGA/QCIF format */
85 #define  COM1_SKIP_0      0x00  /* Do not skip any row */
86 #define  COM1_SKIP_2      0x04  /* Skip 2 rows of 4 */
87 #define  COM1_SKIP_3      0x08  /* Skip 3 rows of 4 */
88 #define REG_BAVE        0x05    /* U/B Average level */
89 #define REG_GbAVE       0x06    /* Y/Gb Average level */
90 #define REG_AECHH       0x07    /* AEC MS 5 bits */
91 #define REG_RAVE        0x08    /* V/R Average level */
92 #define REG_COM2        0x09    /* Control 2 */
93 #define  COM2_SSLEEP      0x10  /* Soft sleep mode */
94 #define REG_PID         0x0a    /* Product ID MSB */
95 #define REG_VER         0x0b    /* Product ID LSB */
96 #define REG_COM3        0x0c    /* Control 3 */
97 #define  COM3_SWAP        0x40    /* Byte swap */
98 #define  COM3_SCALEEN     0x08    /* Enable scaling */
99 #define  COM3_DCWEN       0x04    /* Enable downsamp/crop/window */
100 #define REG_COM4        0x0d    /* Control 4 */
101 #define REG_COM5        0x0e    /* All "reserved" */
102 #define REG_COM6        0x0f    /* Control 6 */
103 #define REG_AECH        0x10    /* More bits of AEC value */
104 #define REG_CLKRC       0x11    /* Clock control */
105 #define   CLK_PLL         0x80    /* Enable internal PLL */
106 #define   CLK_EXT         0x40    /* Use external clock directly */
107 #define   CLK_SCALE       0x3f    /* Mask for internal clock scale */
108 #define REG_COM7        0x12    /* Control 7 */
109 #define   COM7_RESET      0x80    /* Register reset */
110 #define   COM7_FMT_MASK   0x38
111 #define   COM7_FMT_SXGA   0x00
112 #define   COM7_FMT_VGA    0x40
113 #define   COM7_FMT_CIF    0x20    /* CIF format */
114 #define   COM7_FMT_QVGA   0x10    /* QVGA format */
115 #define   COM7_FMT_QCIF   0x08    /* QCIF format */
116 #define   COM7_RGB        0x04    /* bits 0 and 2 - RGB format */
117 #define   COM7_YUV        0x00    /* YUV */
118 #define   COM7_BAYER      0x01    /* Bayer format */
119 #define   COM7_PBAYER     0x05    /* "Processed bayer" */
120 #define REG_COM8        0x13    /* Control 8 */
121 #define   COM8_FASTAEC    0x80    /* Enable fast AGC/AEC */
122 #define   COM8_AECSTEP    0x40    /* Unlimited AEC step size */
123 #define   COM8_BFILT      0x20    /* Band filter enable */
124 #define   COM8_AGC        0x04    /* Auto gain enable */
125 #define   COM8_AWB        0x02    /* White balance enable */
126 #define   COM8_AEC        0x01    /* Auto exposure enable */
127 #define REG_COM9        0x14    /* Control 9  - gain ceiling */
128 #define REG_COM10       0x15    /* Control 10 */
129 #define   COM10_HSYNC     0x40    /* HSYNC instead of HREF */
130 #define   COM10_PCLK_HB   0x20    /* Suppress PCLK on horiz blank */
131 #define   COM10_HREF_REV  0x08    /* Reverse HREF */
132 #define   COM10_VS_LEAD   0x04    /* VSYNC on clock leading edge */
133 #define   COM10_VS_NEG    0x02    /* VSYNC negative */
134 #define   COM10_HS_NEG    0x01    /* HSYNC negative */
135 #define REG_HSTART      0x17    /* Horiz start high bits */
136 #define REG_HSTOP       0x18    /* Horiz stop high bits */
137 #define REG_VSTART      0x19    /* Vert start high bits */
138 #define REG_VSTOP       0x1a    /* Vert stop high bits */
139 #define REG_PSHFT       0x1b    /* Pixel delay after HREF */
140 #define REG_MIDH        0x1c    /* Manuf. ID high */
141 #define REG_MIDL        0x1d    /* Manuf. ID low */
142 #define REG_MVFP        0x1e    /* Mirror / vflip */
143 #define   MVFP_MIRROR     0x20    /* Mirror image */
144 #define   MVFP_FLIP       0x10    /* Vertical flip */
145
146 #define REG_AEW         0x24    /* AGC upper limit */
147 #define REG_AEB         0x25    /* AGC lower limit */
148 #define REG_VPT         0x26    /* AGC/AEC fast mode op region */
149 #define REG_ADVFL       0x2d    /* Insert dummy lines (LSB) */
150 #define REG_ADVFH       0x2e    /* Insert dummy lines (MSB) */
151 #define REG_HSYST       0x30    /* HSYNC rising edge delay */
152 #define REG_HSYEN       0x31    /* HSYNC falling edge delay */
153 #define REG_HREF        0x32    /* HREF pieces */
154 #define REG_TSLB        0x3a    /* lots of stuff */
155 #define   TSLB_YLAST      0x04    /* UYVY or VYUY - see com13 */
156 #define   TSLB_BYTEORD    0x08    /* swap bytes in 16bit mode? */
157 #define REG_COM11       0x3b    /* Control 11 */
158 #define   COM11_NIGHT     0x80    /* NIght mode enable */
159 #define   COM11_NMFR      0x60    /* Two bit NM frame rate */
160 #define   COM11_HZAUTO    0x10    /* Auto detect 50/60 Hz */
161 #define   COM11_50HZ      0x08    /* Manual 50Hz select */
162 #define   COM11_EXP       0x02
163 #define REG_COM12       0x3c    /* Control 12 */
164 #define   COM12_HREF      0x80    /* HREF always */
165 #define REG_COM13       0x3d    /* Control 13 */
166 #define   COM13_GAMMA     0x80    /* Gamma enable */
167 #define   COM13_UVSAT     0x40    /* UV saturation auto adjustment */
168 #define   COM13_CMATRIX   0x10    /* Enable color matrix for RGB or YUV */
169 #define   COM13_UVSWAP    0x01    /* V before U - w/TSLB */
170 #define REG_COM14       0x3e    /* Control 14 */
171 #define   COM14_DCWEN     0x10    /* DCW/PCLK-scale enable */
172 #define REG_EDGE        0x3f    /* Edge enhancement factor */
173 #define REG_COM15       0x40    /* Control 15 */
174 #define   COM15_R10F0     0x00    /* Data range 10 to F0 */
175 #define   COM15_R01FE     0x80    /*            01 to FE */
176 #define   COM15_R00FF     0xc0    /*            00 to FF */
177 #define   COM15_RGB565    0x10    /* RGB565 output */
178 #define   COM15_RGBFIXME          0x20    /* FIXME  */
179 #define   COM15_RGB555    0x30    /* RGB555 output */
180 #define REG_COM16       0x41    /* Control 16 */
181 #define   COM16_AWBGAIN   0x08    /* AWB gain enable */
182 #define REG_COM17       0x42    /* Control 17 */
183 #define   COM17_AECWIN    0xc0    /* AEC window - must match COM4 */
184 #define   COM17_CBAR      0x08    /* DSP Color bar */
185
186 /*
187  * This matrix defines how the colors are generated, must be
188  * tweaked to adjust hue and saturation.
189  *
190  * Order: v-red, v-green, v-blue, u-red, u-green, u-blue
191  *
192  * They are nine-bit signed quantities, with the sign bit
193  * stored in 0x58.  Sign for v-red is bit 0, and up from there.
194  */
195 #define REG_CMATRIX_BASE 0x4f
196 #define   CMATRIX_LEN 6
197 #define REG_CMATRIX_SIGN 0x58
198
199
200 #define REG_BRIGHT      0x55    /* Brightness */
201 #define REG_CONTRAS     0x56    /* Contrast control */
202
203 #define REG_GFIX        0x69    /* Fix gain control */
204
205 #define REG_RGB444      0x8c    /* RGB 444 control */
206 #define   R444_ENABLE     0x02    /* Turn on RGB444, overrides 5x5 */
207 #define   R444_RGBX       0x01    /* Empty nibble at end */
208
209 #define REG_HAECC1      0x9f    /* Hist AEC/AGC control 1 */
210 #define REG_HAECC2      0xa0    /* Hist AEC/AGC control 2 */
211
212 #define REG_BD50MAX     0xa5    /* 50hz banding step limit */
213 #define REG_HAECC3      0xa6    /* Hist AEC/AGC control 3 */
214 #define REG_HAECC4      0xa7    /* Hist AEC/AGC control 4 */
215 #define REG_HAECC5      0xa8    /* Hist AEC/AGC control 5 */
216 #define REG_HAECC6      0xa9    /* Hist AEC/AGC control 6 */
217 #define REG_HAECC7      0xaa    /* Hist AEC/AGC control 7 */
218 #define REG_BD60MAX     0xab    /* 60hz banding step limit */
219
220
221
222
223 /* Returns 0 if OK */
224 static int stk_sensor_outb(struct stk_camera *dev, u8 reg, u8 val)
225 {
226         int i = 0;
227         u8 tmpval = 0;
228
229         if (stk_camera_write_reg(dev, STK_IIC_TX_INDEX, reg))
230                 return 1;
231         if (stk_camera_write_reg(dev, STK_IIC_TX_VALUE, val))
232                 return 1;
233         if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_TX))
234                 return 1;
235         do {
236                 if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
237                         return 1;
238                 i++;
239         } while (tmpval == 0 && i < MAX_RETRIES);
240         if (tmpval != STK_IIC_STAT_TX_OK) {
241                 if (tmpval)
242                         STK_ERROR("stk_sensor_outb failed, status=0x%02x\n",
243                                 tmpval);
244                 return 1;
245         } else
246                 return 0;
247 }
248
249 static int stk_sensor_inb(struct stk_camera *dev, u8 reg, u8 *val)
250 {
251         int i = 0;
252         u8 tmpval = 0;
253
254         if (stk_camera_write_reg(dev, STK_IIC_RX_INDEX, reg))
255                 return 1;
256         if (stk_camera_write_reg(dev, STK_IIC_OP, STK_IIC_OP_RX))
257                 return 1;
258         do {
259                 if (stk_camera_read_reg(dev, STK_IIC_STAT, &tmpval))
260                         return 1;
261                 i++;
262         } while (tmpval == 0 && i < MAX_RETRIES);
263         if (tmpval != STK_IIC_STAT_RX_OK) {
264                 if (tmpval)
265                         STK_ERROR("stk_sensor_inb failed, status=0x%02x\n",
266                                 tmpval);
267                 return 1;
268         }
269
270         if (stk_camera_read_reg(dev, STK_IIC_RX_VALUE, &tmpval))
271                 return 1;
272
273         *val = tmpval;
274         return 0;
275 }
276
277 static int stk_sensor_write_regvals(struct stk_camera *dev,
278                 struct regval *rv)
279 {
280         int ret;
281         if (rv == NULL)
282                 return 0;
283         while (rv->reg != 0xff || rv->val != 0xff) {
284                 ret = stk_sensor_outb(dev, rv->reg, rv->val);
285                 if (ret != 0)
286                         return ret;
287                 rv++;
288         }
289         return 0;
290 }
291
292 int stk_sensor_sleep(struct stk_camera *dev)
293 {
294         u8 tmp;
295         return stk_sensor_inb(dev, REG_COM2, &tmp)
296                 || stk_sensor_outb(dev, REG_COM2, tmp|COM2_SSLEEP);
297 }
298
299 int stk_sensor_wakeup(struct stk_camera *dev)
300 {
301         u8 tmp;
302         return stk_sensor_inb(dev, REG_COM2, &tmp)
303                 || stk_sensor_outb(dev, REG_COM2, tmp&~COM2_SSLEEP);
304 }
305
306 static struct regval ov_initvals[] = {
307         {REG_CLKRC, CLK_PLL},
308         {REG_COM11, 0x01},
309         {0x6a, 0x7d},
310         {REG_AECH, 0x40},
311         {REG_GAIN, 0x00},
312         {REG_BLUE, 0x80},
313         {REG_RED, 0x80},
314         /* Do not enable fast AEC for now */
315         /*{REG_COM8, COM8_FASTAEC|COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},*/
316         {REG_COM8, COM8_AECSTEP|COM8_BFILT|COM8_AGC|COM8_AEC},
317         {0x39, 0x50}, {0x38, 0x93},
318         {0x37, 0x00}, {0x35, 0x81},
319         {REG_COM5, 0x20},
320         {REG_COM1, 0x00},
321         {REG_COM3, 0x00},
322         {REG_COM4, 0x00},
323         {REG_PSHFT, 0x00},
324         {0x16, 0x07},
325         {0x33, 0xe2}, {0x34, 0xbf},
326         {REG_COM16, 0x00},
327         {0x96, 0x04},
328         /* Gamma curve values */
329 /*      { 0x7a, 0x20 },         { 0x7b, 0x10 },
330         { 0x7c, 0x1e },         { 0x7d, 0x35 },
331         { 0x7e, 0x5a },         { 0x7f, 0x69 },
332         { 0x80, 0x76 },         { 0x81, 0x80 },
333         { 0x82, 0x88 },         { 0x83, 0x8f },
334         { 0x84, 0x96 },         { 0x85, 0xa3 },
335         { 0x86, 0xaf },         { 0x87, 0xc4 },
336         { 0x88, 0xd7 },         { 0x89, 0xe8 },
337 */
338         {REG_GFIX, 0x40},
339         {0x8e, 0x00},
340         {REG_COM12, 0x73},
341         {0x8f, 0xdf}, {0x8b, 0x06},
342         {0x8c, 0x20},
343         {0x94, 0x88}, {0x95, 0x88},
344 /*      {REG_COM15, 0xc1}, TODO */
345         {0x29, 0x3f},
346         {REG_COM6, 0x42},
347         {REG_BD50MAX, 0x80},
348         {REG_HAECC6, 0xb8}, {REG_HAECC7, 0x92},
349         {REG_BD60MAX, 0x0a},
350         {0x90, 0x00}, {0x91, 0x00},
351         {REG_HAECC1, 0x00}, {REG_HAECC2, 0x00},
352         {REG_AEW, 0x68}, {REG_AEB, 0x5c},
353         {REG_VPT, 0xc3},
354         {REG_COM9, 0x2e},
355         {0x2a, 0x00}, {0x2b, 0x00},
356
357         {0xff, 0xff}, /* END MARKER */
358 };
359
360 /* Probe the I2C bus and initialise the sensor chip */
361 int stk_sensor_init(struct stk_camera *dev)
362 {
363         u8 idl = 0;
364         u8 idh = 0;
365
366         if (stk_camera_write_reg(dev, STK_IIC_ENABLE, STK_IIC_ENABLE_YES)
367                 || stk_camera_write_reg(dev, STK_IIC_ADDR, SENSOR_ADDRESS)
368                 || stk_sensor_outb(dev, REG_COM7, COM7_RESET)) {
369                 STK_ERROR("Sensor resetting failed\n");
370                 return -ENODEV;
371         }
372         msleep(10);
373         /* Read the manufacturer ID: ov = 0x7FA2 */
374         if (stk_sensor_inb(dev, REG_MIDH, &idh)
375             || stk_sensor_inb(dev, REG_MIDL, &idl)) {
376                 STK_ERROR("Strange error reading sensor ID\n");
377                 return -ENODEV;
378         }
379         if (idh != 0x7f || idl != 0xa2) {
380                 STK_ERROR("Huh? you don't have a sensor from ovt\n");
381                 return -ENODEV;
382         }
383         if (stk_sensor_inb(dev, REG_PID, &idh)
384             || stk_sensor_inb(dev, REG_VER, &idl)) {
385                 STK_ERROR("Could not read sensor model\n");
386                 return -ENODEV;
387         }
388         stk_sensor_write_regvals(dev, ov_initvals);
389         msleep(10);
390         STK_INFO("OmniVision sensor detected, id %02X%02X at address %x\n",
391                  idh, idl, SENSOR_ADDRESS);
392         return 0;
393 }
394
395 /* V4L2_PIX_FMT_UYVY */
396 static struct regval ov_fmt_uyvy[] = {
397         {REG_TSLB, TSLB_YLAST|0x08 },
398         { 0x4f, 0x80 },         /* "matrix coefficient 1" */
399         { 0x50, 0x80 },         /* "matrix coefficient 2" */
400         { 0x51, 0    },         /* vb */
401         { 0x52, 0x22 },         /* "matrix coefficient 4" */
402         { 0x53, 0x5e },         /* "matrix coefficient 5" */
403         { 0x54, 0x80 },         /* "matrix coefficient 6" */
404         {REG_COM13, COM13_UVSAT|COM13_CMATRIX},
405         {REG_COM15, COM15_R00FF },
406         {0xff, 0xff}, /* END MARKER */
407 };
408 /* V4L2_PIX_FMT_YUYV */
409 static struct regval ov_fmt_yuyv[] = {
410         {REG_TSLB, 0 },
411         { 0x4f, 0x80 },         /* "matrix coefficient 1" */
412         { 0x50, 0x80 },         /* "matrix coefficient 2" */
413         { 0x51, 0    },         /* vb */
414         { 0x52, 0x22 },         /* "matrix coefficient 4" */
415         { 0x53, 0x5e },         /* "matrix coefficient 5" */
416         { 0x54, 0x80 },         /* "matrix coefficient 6" */
417         {REG_COM13, COM13_UVSAT|COM13_CMATRIX},
418         {REG_COM15, COM15_R00FF },
419         {0xff, 0xff}, /* END MARKER */
420 };
421
422 /* V4L2_PIX_FMT_RGB565X rrrrrggg gggbbbbb */
423 static struct regval ov_fmt_rgbr[] = {
424         { REG_RGB444, 0 },      /* No RGB444 please */
425         {REG_TSLB, 0x00},
426         { REG_COM1, 0x0 },
427         { REG_COM9, 0x38 },     /* 16x gain ceiling; 0x8 is reserved bit */
428         { 0x4f, 0xb3 },         /* "matrix coefficient 1" */
429         { 0x50, 0xb3 },         /* "matrix coefficient 2" */
430         { 0x51, 0    },         /* vb */
431         { 0x52, 0x3d },         /* "matrix coefficient 4" */
432         { 0x53, 0xa7 },         /* "matrix coefficient 5" */
433         { 0x54, 0xe4 },         /* "matrix coefficient 6" */
434         { REG_COM13, COM13_GAMMA },
435         { REG_COM15, COM15_RGB565|COM15_R00FF },
436         { 0xff, 0xff },
437 };
438
439 /* V4L2_PIX_FMT_RGB565 gggbbbbb rrrrrggg */
440 static struct regval ov_fmt_rgbp[] = {
441         { REG_RGB444, 0 },      /* No RGB444 please */
442         {REG_TSLB, TSLB_BYTEORD },
443         { REG_COM1, 0x0 },
444         { REG_COM9, 0x38 },     /* 16x gain ceiling; 0x8 is reserved bit */
445         { 0x4f, 0xb3 },         /* "matrix coefficient 1" */
446         { 0x50, 0xb3 },         /* "matrix coefficient 2" */
447         { 0x51, 0    },         /* vb */
448         { 0x52, 0x3d },         /* "matrix coefficient 4" */
449         { 0x53, 0xa7 },         /* "matrix coefficient 5" */
450         { 0x54, 0xe4 },         /* "matrix coefficient 6" */
451         { REG_COM13, COM13_GAMMA },
452         { REG_COM15, COM15_RGB565|COM15_R00FF },
453         { 0xff, 0xff },
454 };
455
456 /* V4L2_PIX_FMT_SRGGB8 */
457 static struct regval ov_fmt_bayer[] = {
458         /* This changes color order */
459         {REG_TSLB, 0x40}, /* BGGR */
460         /* {REG_TSLB, 0x08}, */ /* BGGR with vertical image flipping */
461         {REG_COM15, COM15_R00FF },
462         {0xff, 0xff}, /* END MARKER */
463 };
464 /*
465  * Store a set of start/stop values into the camera.
466  */
467 static int stk_sensor_set_hw(struct stk_camera *dev,
468                 int hstart, int hstop, int vstart, int vstop)
469 {
470         int ret;
471         unsigned char v;
472 /*
473  * Horizontal: 11 bits, top 8 live in hstart and hstop.  Bottom 3 of
474  * hstart are in href[2:0], bottom 3 of hstop in href[5:3].  There is
475  * a mystery "edge offset" value in the top two bits of href.
476  */
477         ret =  stk_sensor_outb(dev, REG_HSTART, (hstart >> 3) & 0xff);
478         ret += stk_sensor_outb(dev, REG_HSTOP, (hstop >> 3) & 0xff);
479         ret += stk_sensor_inb(dev, REG_HREF, &v);
480         v = (v & 0xc0) | ((hstop & 0x7) << 3) | (hstart & 0x7);
481         msleep(10);
482         ret += stk_sensor_outb(dev, REG_HREF, v);
483 /*
484  * Vertical: similar arrangement (note: this is different from ov7670.c)
485  */
486         ret += stk_sensor_outb(dev, REG_VSTART, (vstart >> 3) & 0xff);
487         ret += stk_sensor_outb(dev, REG_VSTOP, (vstop >> 3) & 0xff);
488         ret += stk_sensor_inb(dev, REG_VREF, &v);
489         v = (v & 0xc0) | ((vstop & 0x7) << 3) | (vstart & 0x7);
490         msleep(10);
491         ret += stk_sensor_outb(dev, REG_VREF, v);
492         return ret;
493 }
494
495
496 int stk_sensor_configure(struct stk_camera *dev)
497 {
498         int com7;
499         /*
500          * We setup the sensor to output dummy lines in low-res modes,
501          * so we don't get absurdly hight framerates.
502          */
503         unsigned dummylines;
504         int flip;
505         struct regval *rv;
506
507         switch (dev->vsettings.mode) {
508         case MODE_QCIF: com7 = COM7_FMT_QCIF;
509                 dummylines = 604;
510                 break;
511         case MODE_QVGA: com7 = COM7_FMT_QVGA;
512                 dummylines = 267;
513                 break;
514         case MODE_CIF: com7 = COM7_FMT_CIF;
515                 dummylines = 412;
516                 break;
517         case MODE_VGA: com7 = COM7_FMT_VGA;
518                 dummylines = 11;
519                 break;
520         case MODE_SXGA: com7 = COM7_FMT_SXGA;
521                 dummylines = 0;
522                 break;
523         default: STK_ERROR("Unsupported mode %d\n", dev->vsettings.mode);
524                 return -EFAULT;
525         }
526         switch (dev->vsettings.palette) {
527         case V4L2_PIX_FMT_UYVY:
528                 com7 |= COM7_YUV;
529                 rv = ov_fmt_uyvy;
530                 break;
531         case V4L2_PIX_FMT_YUYV:
532                 com7 |= COM7_YUV;
533                 rv = ov_fmt_yuyv;
534                 break;
535         case V4L2_PIX_FMT_RGB565:
536                 com7 |= COM7_RGB;
537                 rv = ov_fmt_rgbp;
538                 break;
539         case V4L2_PIX_FMT_RGB565X:
540                 com7 |= COM7_RGB;
541                 rv = ov_fmt_rgbr;
542                 break;
543         case V4L2_PIX_FMT_SBGGR8:
544                 com7 |= COM7_PBAYER;
545                 rv = ov_fmt_bayer;
546                 break;
547         default: STK_ERROR("Unsupported colorspace\n");
548                 return -EFAULT;
549         }
550         /*FIXME sometimes the sensor go to a bad state
551         stk_sensor_write_regvals(dev, ov_initvals); */
552         stk_sensor_outb(dev, REG_COM7, com7);
553         msleep(50);
554         stk_sensor_write_regvals(dev, rv);
555         flip = (dev->vsettings.vflip?MVFP_FLIP:0)
556                 | (dev->vsettings.hflip?MVFP_MIRROR:0);
557         stk_sensor_outb(dev, REG_MVFP, flip);
558         if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8
559                         && !dev->vsettings.vflip)
560                 stk_sensor_outb(dev, REG_TSLB, 0x08);
561         stk_sensor_outb(dev, REG_ADVFH, dummylines >> 8);
562         stk_sensor_outb(dev, REG_ADVFL, dummylines & 0xff);
563         msleep(50);
564         switch (dev->vsettings.mode) {
565         case MODE_VGA:
566                 if (stk_sensor_set_hw(dev, 302, 1582, 6, 486))
567                         STK_ERROR("stk_sensor_set_hw failed (VGA)\n");
568                 break;
569         case MODE_SXGA:
570         case MODE_CIF:
571         case MODE_QVGA:
572         case MODE_QCIF:
573                 /*FIXME These settings seem ignored by the sensor
574                 if (stk_sensor_set_hw(dev, 220, 1500, 10, 1034))
575                         STK_ERROR("stk_sensor_set_hw failed (SXGA)\n");
576                 */
577                 break;
578         }
579         msleep(10);
580         return 0;
581 }
582
583 int stk_sensor_set_brightness(struct stk_camera *dev, int br)
584 {
585         if (br < 0 || br > 0xff)
586                 return -EINVAL;
587         stk_sensor_outb(dev, REG_AEB, max(0x00, br - 6));
588         stk_sensor_outb(dev, REG_AEW, min(0xff, br + 6));
589         return 0;
590 }
591