2 * Driver for the ov9650 sensor
4 * Copyright (C) 2008 Erik Andrén
5 * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
6 * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
8 * Portions of code to USB interface and ALi driver software,
9 * Copyright (c) 2006 Willem Duinker
10 * v4l2 interface modeled after the V4L2 driver
11 * for SN9C10x PC Camera Controllers
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation, version 2.
19 #ifndef M5602_OV9650_H_
20 #define M5602_OV9650_H_
22 #include <linux/dmi.h>
23 #include "m5602_sensor.h"
25 /*****************************************************************************/
27 #define OV9650_GAIN 0x00
28 #define OV9650_BLUE 0x01
29 #define OV9650_RED 0x02
30 #define OV9650_VREF 0x03
31 #define OV9650_COM1 0x04
32 #define OV9650_BAVE 0x05
33 #define OV9650_GEAVE 0x06
34 #define OV9650_RSVD7 0x07
35 #define OV9650_PID 0x0a
36 #define OV9650_VER 0x0b
37 #define OV9650_COM3 0x0c
38 #define OV9650_COM5 0x0e
39 #define OV9650_COM6 0x0f
40 #define OV9650_AECH 0x10
41 #define OV9650_CLKRC 0x11
42 #define OV9650_COM7 0x12
43 #define OV9650_COM8 0x13
44 #define OV9650_COM9 0x14
45 #define OV9650_COM10 0x15
46 #define OV9650_RSVD16 0x16
47 #define OV9650_HSTART 0x17
48 #define OV9650_HSTOP 0x18
49 #define OV9650_VSTRT 0x19
50 #define OV9650_VSTOP 0x1a
51 #define OV9650_PSHFT 0x1b
52 #define OV9650_MVFP 0x1e
53 #define OV9650_AEW 0x24
54 #define OV9650_AEB 0x25
55 #define OV9650_VPT 0x26
56 #define OV9650_BBIAS 0x27
57 #define OV9650_GbBIAS 0x28
58 #define OV9650_Gr_COM 0x29
59 #define OV9650_RBIAS 0x2c
60 #define OV9650_HREF 0x32
61 #define OV9650_CHLF 0x33
62 #define OV9650_ARBLM 0x34
63 #define OV9650_RSVD35 0x35
64 #define OV9650_RSVD36 0x36
65 #define OV9650_ADC 0x37
66 #define OV9650_ACOM38 0x38
67 #define OV9650_OFON 0x39
68 #define OV9650_TSLB 0x3a
69 #define OV9650_COM12 0x3c
70 #define OV9650_COM13 0x3d
71 #define OV9650_COM15 0x40
72 #define OV9650_COM16 0x41
73 #define OV9650_LCC1 0x62
74 #define OV9650_LCC2 0x63
75 #define OV9650_LCC3 0x64
76 #define OV9650_LCC4 0x65
77 #define OV9650_LCC5 0x66
78 #define OV9650_HV 0x69
79 #define OV9650_DBLV 0x6b
80 #define OV9650_COM21 0x8b
81 #define OV9650_COM22 0x8c
82 #define OV9650_COM24 0x8e
83 #define OV9650_DBLC1 0x8f
84 #define OV9650_RSVD94 0x94
85 #define OV9650_RSVD95 0x95
86 #define OV9650_RSVD96 0x96
87 #define OV9650_LCCFB 0x9d
88 #define OV9650_LCCFR 0x9e
89 #define OV9650_AECHM 0xa1
90 #define OV9650_COM26 0xa5
91 #define OV9650_ACOMA8 0xa8
92 #define OV9650_ACOMA9 0xa9
94 #define OV9650_REGISTER_RESET (1 << 7)
95 #define OV9650_VGA_SELECT (1 << 6)
96 #define OV9650_QVGA_SELECT (1 << 4)
97 #define OV9650_RGB_SELECT (1 << 2)
98 #define OV9650_RAW_RGB_SELECT (1 << 0)
100 #define OV9650_FAST_AGC_AEC (1 << 7)
101 #define OV9650_AEC_UNLIM_STEP_SIZE (1 << 6)
102 #define OV9650_BANDING (1 << 5)
103 #define OV9650_AGC_EN (1 << 2)
104 #define OV9650_AWB_EN (1 << 1)
105 #define OV9650_AEC_EN (1 << 0)
107 #define OV9650_VARIOPIXEL (1 << 2)
108 #define OV9650_SYSTEM_CLK_SEL (1 << 7)
109 #define OV9650_SLAM_MODE (1 << 4)
111 #define OV9650_VFLIP (1 << 4)
112 #define OV9650_HFLIP (1 << 5)
114 #define GAIN_DEFAULT 0x14
115 #define RED_GAIN_DEFAULT 0x70
116 #define BLUE_GAIN_DEFAULT 0x20
117 #define EXPOSURE_DEFAULT 0x5003
119 /*****************************************************************************/
121 /* Kernel module parameters */
122 extern int force_sensor;
123 extern int dump_sensor;
125 int ov9650_probe(struct sd *sd);
126 int ov9650_init(struct sd *sd);
127 int ov9650_start(struct sd *sd);
128 int ov9650_power_down(struct sd *sd);
130 int ov9650_set_exposure(struct gspca_dev *gspca_dev, __s32 val);
131 int ov9650_get_exposure(struct gspca_dev *gspca_dev, __s32 *val);
132 int ov9650_get_gain(struct gspca_dev *gspca_dev, __s32 *val);
133 int ov9650_set_gain(struct gspca_dev *gspca_dev, __s32 val);
134 int ov9650_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val);
135 int ov9650_set_red_balance(struct gspca_dev *gspca_dev, __s32 val);
136 int ov9650_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val);
137 int ov9650_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val);
138 int ov9650_get_hflip(struct gspca_dev *gspca_dev, __s32 *val);
139 int ov9650_set_hflip(struct gspca_dev *gspca_dev, __s32 val);
140 int ov9650_get_vflip(struct gspca_dev *gspca_dev, __s32 *val);
141 int ov9650_set_vflip(struct gspca_dev *gspca_dev, __s32 val);
142 int ov9650_get_brightness(struct gspca_dev *gspca_dev, __s32 *val);
143 int ov9650_set_brightness(struct gspca_dev *gspca_dev, __s32 val);
144 int ov9650_get_auto_white_balance(struct gspca_dev *gspca_dev, __s32 *val);
145 int ov9650_set_auto_white_balance(struct gspca_dev *gspca_dev, __s32 val);
146 int ov9650_get_auto_gain(struct gspca_dev *gspca_dev, __s32 *val);
147 int ov9650_set_auto_gain(struct gspca_dev *gspca_dev, __s32 val);
149 static struct m5602_sensor ov9650 = {
151 .i2c_slave_id = 0x60,
153 .probe = ov9650_probe,
155 .start = ov9650_start,
156 .power_down = ov9650_power_down,
162 .id = V4L2_CID_EXPOSURE,
163 .type = V4L2_CTRL_TYPE_INTEGER,
168 .default_value = EXPOSURE_DEFAULT,
169 .flags = V4L2_CTRL_FLAG_SLIDER
171 .set = ov9650_set_exposure,
172 .get = ov9650_get_exposure
176 .type = V4L2_CTRL_TYPE_INTEGER,
181 .default_value = GAIN_DEFAULT,
182 .flags = V4L2_CTRL_FLAG_SLIDER
184 .set = ov9650_set_gain,
185 .get = ov9650_get_gain
188 .type = V4L2_CTRL_TYPE_INTEGER,
189 .name = "red balance",
193 .default_value = RED_GAIN_DEFAULT,
194 .flags = V4L2_CTRL_FLAG_SLIDER
196 .set = ov9650_set_red_balance,
197 .get = ov9650_get_red_balance
200 .type = V4L2_CTRL_TYPE_INTEGER,
201 .name = "blue balance",
205 .default_value = BLUE_GAIN_DEFAULT,
206 .flags = V4L2_CTRL_FLAG_SLIDER
208 .set = ov9650_set_blue_balance,
209 .get = ov9650_get_blue_balance
212 .id = V4L2_CID_HFLIP,
213 .type = V4L2_CTRL_TYPE_BOOLEAN,
214 .name = "horizontal flip",
220 .set = ov9650_set_hflip,
221 .get = ov9650_get_hflip
224 .id = V4L2_CID_VFLIP,
225 .type = V4L2_CTRL_TYPE_BOOLEAN,
226 .name = "vertical flip",
232 .set = ov9650_set_vflip,
233 .get = ov9650_get_vflip
236 .id = V4L2_CID_AUTO_WHITE_BALANCE,
237 .type = V4L2_CTRL_TYPE_BOOLEAN,
238 .name = "auto white balance",
244 .set = ov9650_set_auto_white_balance,
245 .get = ov9650_get_auto_white_balance
248 .id = V4L2_CID_AUTOGAIN,
249 .type = V4L2_CTRL_TYPE_BOOLEAN,
250 .name = "auto gain control",
256 .set = ov9650_set_auto_gain,
257 .get = ov9650_get_auto_gain
271 .colorspace = V4L2_COLORSPACE_SRGB,
281 .colorspace = V4L2_COLORSPACE_SRGB,
287 static const unsigned char preinit_ov9650[][3] =
290 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
291 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
292 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
293 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
294 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
295 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
297 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08},
298 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
299 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
300 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
301 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
302 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
303 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
304 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a},
306 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
307 /* Enable double clock */
308 {SENSOR, OV9650_CLKRC, 0x80},
309 /* Do something out of spec with the power */
310 {SENSOR, OV9650_OFON, 0x40}
313 static const unsigned char init_ov9650[][3] =
316 {BRIDGE, M5602_XB_MCU_CLK_DIV, 0x02},
317 {BRIDGE, M5602_XB_MCU_CLK_CTRL, 0xb0},
318 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x00},
319 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
320 {BRIDGE, M5602_XB_ADC_CTRL, 0xc0},
321 {BRIDGE, M5602_XB_SENSOR_CTRL, 0x00},
323 {BRIDGE, M5602_XB_SENSOR_TYPE, 0x08},
324 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
325 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
326 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
327 {BRIDGE, M5602_XB_GPIO_DIR_H, 0x06},
328 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x00},
329 {BRIDGE, M5602_XB_GPIO_DAT, 0x00},
330 {BRIDGE, M5602_XB_I2C_CLK_DIV, 0x0a},
332 {SENSOR, OV9650_COM7, OV9650_REGISTER_RESET},
333 /* Enable double clock */
334 {SENSOR, OV9650_CLKRC, 0x80},
335 /* Do something out of spec with the power */
336 {SENSOR, OV9650_OFON, 0x40},
339 {SENSOR, OV9650_COM1, 0x20},
340 /* Set fast AGC/AEC algorithm with unlimited step size */
341 {SENSOR, OV9650_COM8, OV9650_FAST_AGC_AEC |
342 OV9650_AEC_UNLIM_STEP_SIZE |
343 OV9650_AWB_EN | OV9650_AGC_EN},
345 {SENSOR, OV9650_CHLF, 0x10},
346 {SENSOR, OV9650_ARBLM, 0xbf},
347 {SENSOR, OV9650_ACOM38, 0x81},
348 /* Turn off color matrix coefficient double option */
349 {SENSOR, OV9650_COM16, 0x00},
350 /* Enable color matrix for RGB/YUV, Delay Y channel,
351 set output Y/UV delay to 1 */
352 {SENSOR, OV9650_COM13, 0x19},
353 /* Enable digital BLC, Set output mode to U Y V Y */
354 {SENSOR, OV9650_TSLB, 0x0c},
355 /* Limit the AGC/AEC stable upper region */
356 {SENSOR, OV9650_COM24, 0x00},
357 /* Enable HREF and some out of spec things */
358 {SENSOR, OV9650_COM12, 0x73},
359 /* Set all DBLC offset signs to positive and
360 do some out of spec stuff */
361 {SENSOR, OV9650_DBLC1, 0xdf},
362 {SENSOR, OV9650_COM21, 0x06},
363 {SENSOR, OV9650_RSVD35, 0x91},
364 /* Necessary, no camera stream without it */
365 {SENSOR, OV9650_RSVD16, 0x06},
366 {SENSOR, OV9650_RSVD94, 0x99},
367 {SENSOR, OV9650_RSVD95, 0x99},
368 {SENSOR, OV9650_RSVD96, 0x04},
369 /* Enable full range output */
370 {SENSOR, OV9650_COM15, 0x0},
371 /* Enable HREF at optical black, enable ADBLC bias,
372 enable ADBLC, reset timings at format change */
373 {SENSOR, OV9650_COM6, 0x4b},
374 /* Subtract 32 from the B channel bias */
375 {SENSOR, OV9650_BBIAS, 0xa0},
376 /* Subtract 32 from the Gb channel bias */
377 {SENSOR, OV9650_GbBIAS, 0xa0},
378 /* Do not bypass the analog BLC and to some out of spec stuff */
379 {SENSOR, OV9650_Gr_COM, 0x00},
380 /* Subtract 32 from the R channel bias */
381 {SENSOR, OV9650_RBIAS, 0xa0},
382 /* Subtract 32 from the R channel bias */
383 {SENSOR, OV9650_RBIAS, 0x0},
384 {SENSOR, OV9650_COM26, 0x80},
385 {SENSOR, OV9650_ACOMA9, 0x98},
386 /* Set the AGC/AEC stable region upper limit */
387 {SENSOR, OV9650_AEW, 0x68},
388 /* Set the AGC/AEC stable region lower limit */
389 {SENSOR, OV9650_AEB, 0x5c},
390 /* Set the high and low limit nibbles to 3 */
391 {SENSOR, OV9650_VPT, 0xc3},
392 /* Set the Automatic Gain Ceiling (AGC) to 128x,
393 drop VSYNC at frame drop,
394 limit exposure timing,
395 drop frame when the AEC step is larger than the exposure gap */
396 {SENSOR, OV9650_COM9, 0x6e},
397 /* Set VSYNC negative, Set RESET to SLHS (slave mode horizontal sync)
398 and set PWDN to SLVS (slave mode vertical sync) */
399 {SENSOR, OV9650_COM10, 0x42},
400 /* Set horizontal column start high to default value */
401 {SENSOR, OV9650_HSTART, 0x1a}, /* 210 */
402 /* Set horizontal column end */
403 {SENSOR, OV9650_HSTOP, 0xbf}, /* 1534 */
404 /* Complementing register to the two writes above */
405 {SENSOR, OV9650_HREF, 0xb2},
406 /* Set vertical row start high bits */
407 {SENSOR, OV9650_VSTRT, 0x02},
408 /* Set vertical row end low bits */
409 {SENSOR, OV9650_VSTOP, 0x7e},
410 /* Set complementing vertical frame control */
411 {SENSOR, OV9650_VREF, 0x10},
412 /* Set raw RGB output format with VGA resolution */
413 {SENSOR, OV9650_COM7, OV9650_VGA_SELECT |
415 OV9650_RAW_RGB_SELECT},
416 {SENSOR, OV9650_ADC, 0x04},
417 {SENSOR, OV9650_HV, 0x40},
418 /* Enable denoise, and white-pixel erase */
419 {SENSOR, OV9650_COM22, 0x23},
421 /* Set the high bits of the exposure value */
422 {SENSOR, OV9650_AECH, ((EXPOSURE_DEFAULT & 0xff00) >> 8)},
424 /* Set the low bits of the exposure value */
425 {SENSOR, OV9650_COM1, (EXPOSURE_DEFAULT & 0xff)},
426 {SENSOR, OV9650_GAIN, GAIN_DEFAULT},
427 {SENSOR, OV9650_BLUE, BLUE_GAIN_DEFAULT},
428 {SENSOR, OV9650_RED, RED_GAIN_DEFAULT},
431 static const unsigned char power_down_ov9650[][3] =
433 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
434 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0},
435 {SENSOR, OV9650_COM7, 0x80},
436 {SENSOR, OV9650_OFON, 0xf4},
437 {SENSOR, OV9650_MVFP, 0x80},
438 {SENSOR, OV9650_DBLV, 0x3f},
439 {SENSOR, OV9650_RSVD36, 0x49},
440 {SENSOR, OV9650_COM7, 0x05},
442 {BRIDGE, M5602_XB_GPIO_DIR, 0x05},
443 {BRIDGE, M5602_XB_GPIO_DAT, 0x04},
444 {BRIDGE, M5602_XB_GPIO_EN_H, 0x06},
445 {BRIDGE, M5602_XB_GPIO_EN_L, 0x06},
446 {BRIDGE, M5602_XB_GPIO_DAT_H, 0x02},
447 {BRIDGE, M5602_XB_SEN_CLK_DIV, 0x04},
448 {BRIDGE, M5602_XB_SEN_CLK_CTRL, 0xb0}
451 static const unsigned char VGA_ov9650[][3] =
453 {SENSOR, OV9650_COM7, OV9650_VGA_SELECT |
455 OV9650_RAW_RGB_SELECT},
457 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x82},
458 {BRIDGE, M5602_XB_LINE_OF_FRAME_L, 0x00},
459 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82},
460 {BRIDGE, M5602_XB_PIX_OF_LINE_L, 0x00},
461 {BRIDGE, M5602_XB_SIG_INI, 0x01},
463 /* Moves the view window in a vertical orientation */
464 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
465 {BRIDGE, M5602_XB_VSYNC_PARA, 0x09},
466 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
467 {BRIDGE, M5602_XB_VSYNC_PARA, 0x01},
468 {BRIDGE, M5602_XB_VSYNC_PARA, 0xe0}, /* 480 */
469 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
470 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
471 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00},
472 {BRIDGE, M5602_XB_HSYNC_PARA, 0x64}, /* 100 */
473 {BRIDGE, M5602_XB_HSYNC_PARA, 0x02}, /* 640 + 100 */
474 {BRIDGE, M5602_XB_HSYNC_PARA, 0xe4}
477 static const unsigned char QVGA_ov9650[][3] =
479 {SENSOR, OV9650_COM7, OV9650_QVGA_SELECT |
481 OV9650_RAW_RGB_SELECT},
483 {BRIDGE, M5602_XB_LINE_OF_FRAME_H, 0x82},
484 {BRIDGE, M5602_XB_LINE_OF_FRAME_L, 0x00},
485 {BRIDGE, M5602_XB_PIX_OF_LINE_H, 0x82},
486 {BRIDGE, M5602_XB_PIX_OF_LINE_L, 0x00},
487 {BRIDGE, M5602_XB_SIG_INI, 0x01},
489 /* Moves the view window in a vertical orientation */
490 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
491 {BRIDGE, M5602_XB_VSYNC_PARA, 0x08},
492 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
493 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
494 {BRIDGE, M5602_XB_VSYNC_PARA, 0xf0}, /* 240 */
495 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
496 {BRIDGE, M5602_XB_VSYNC_PARA, 0x00},
497 {BRIDGE, M5602_XB_HSYNC_PARA, 0x00},
498 {BRIDGE, M5602_XB_HSYNC_PARA, 0x31}, /* 50 */
499 {BRIDGE, M5602_XB_HSYNC_PARA, 0x01}, /* 320 + 50 */
500 {BRIDGE, M5602_XB_HSYNC_PARA, 0x71}