1 /* OmniVision OV7620/OV7120 Camera Chip Support Code
3 * Copyright (c) 1999-2004 Mark McClelland <mark@alpha.dyndns.org>
4 * http://alpha.dyndns.org/ov511/
6 * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
16 #include <linux/slab.h>
17 #include "ovcamchip_priv.h"
20 #define REG_GAIN 0x00 /* gain [5:0] */
21 #define REG_BLUE 0x01 /* blue gain */
22 #define REG_RED 0x02 /* red gain */
23 #define REG_SAT 0x03 /* saturation */
24 #define REG_BRT 0x06 /* Y brightness */
25 #define REG_SHARP 0x07 /* analog sharpness */
26 #define REG_BLUE_BIAS 0x0C /* WB blue ratio [5:0] */
27 #define REG_RED_BIAS 0x0D /* WB red ratio [5:0] */
28 #define REG_EXP 0x10 /* exposure */
30 /* Default control settings. Values are in terms of V4L2 controls. */
31 #define OV7120_DFL_BRIGHT 0x60
32 #define OV7620_DFL_BRIGHT 0x60
33 #define OV7120_DFL_SAT 0xb0
34 #define OV7620_DFL_SAT 0xc0
35 #define DFL_AUTO_EXP 1
36 #define DFL_AUTO_GAIN 1
37 #define OV7120_DFL_GAIN 0x00
38 #define OV7620_DFL_GAIN 0x00
39 /* NOTE: Since autoexposure is the default, these aren't programmed into the
40 * OV7x20 chip. They are just here because V4L2 expects a default */
41 #define OV7120_DFL_EXP 0x7f
42 #define OV7620_DFL_EXP 0x7f
44 /* Window parameters */
45 #define HWSBASE 0x2F /* From 7620.SET (spec is wrong) */
59 /* Contrast look-up table */
60 static unsigned char ctab
[] = {
61 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
62 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
65 /* Settings for (Black & White) OV7120 camera chip */
66 static struct ovcamchip_regvals regvals_init_7120
[] = {
67 { 0x12, 0x80 }, /* reset */
68 { 0x13, 0x00 }, /* Autoadjust off */
69 { 0x12, 0x20 }, /* Disable AWB */
70 { 0x13, DFL_AUTO_GAIN
?0x01:0x00 }, /* Autoadjust on (if desired) */
71 { 0x00, OV7120_DFL_GAIN
},
74 { 0x03, OV7120_DFL_SAT
},
75 { 0x06, OV7120_DFL_BRIGHT
},
94 { 0x28, 0x20 }, /* DON'T set bit 6. It is for the OV7620 only */
95 { 0x29, DFL_AUTO_EXP
?0x00:0x80 },
107 { 0x65, 0x83 }, /* OV says "don't change this value" */
133 { 0xff, 0xff }, /* END MARKER */
136 /* Settings for (color) OV7620 camera chip */
137 static struct ovcamchip_regvals regvals_init_7620
[] = {
138 { 0x12, 0x80 }, /* reset */
139 { 0x00, OV7620_DFL_GAIN
},
142 { 0x03, OV7620_DFL_SAT
},
143 { 0x06, OV7620_DFL_BRIGHT
},
150 { 0x13, DFL_AUTO_GAIN
?0x01:0x00 },
166 { 0x29, DFL_AUTO_EXP
?0x00:0x80 },
202 { 0xff, 0xff }, /* END MARKER */
205 /* Returns index into the specified look-up table, with 'n' elements, for which
206 * the value is greater than or equal to "val". If a match isn't found, (n-1)
207 * is returned. The entries in the table must be in ascending order. */
208 static inline int ov7x20_lut_find(unsigned char lut
[], int n
, unsigned char val
)
212 while (lut
[i
] < val
&& i
< n
)
218 /* This initializes the OV7x20 camera chip and relevant variables. */
219 static int ov7x20_init(struct i2c_client
*c
)
221 struct ovcamchip
*ov
= i2c_get_clientdata(c
);
225 DDEBUG(4, &c
->dev
, "entered");
228 rc
= ov_write_regvals(c
, regvals_init_7120
);
230 rc
= ov_write_regvals(c
, regvals_init_7620
);
235 ov
->spriv
= s
= kzalloc(sizeof *s
, GFP_KERNEL
);
240 s
->auto_exp
= DFL_AUTO_EXP
;
241 s
->auto_gain
= DFL_AUTO_GAIN
;
246 static int ov7x20_free(struct i2c_client
*c
)
248 struct ovcamchip
*ov
= i2c_get_clientdata(c
);
254 static int ov7x20_set_v4l1_control(struct i2c_client
*c
,
255 struct ovcamchip_control
*ctl
)
257 struct ovcamchip
*ov
= i2c_get_clientdata(c
);
258 struct ov7x20
*s
= ov
->spriv
;
263 case OVCAMCHIP_CID_CONT
:
265 /* Use Y gamma control instead. Bit 0 enables it. */
266 rc
= ov_write(c
, 0x64, ctab
[v
>> 12]);
269 case OVCAMCHIP_CID_BRIGHT
:
270 /* 7620 doesn't like manual changes when in auto mode */
272 rc
= ov_write(c
, REG_BRT
, v
>> 8);
276 case OVCAMCHIP_CID_SAT
:
277 rc
= ov_write(c
, REG_SAT
, v
>> 8);
279 case OVCAMCHIP_CID_EXP
:
281 rc
= ov_write(c
, REG_EXP
, v
);
285 case OVCAMCHIP_CID_FREQ
:
287 int sixty
= (v
== 60);
289 rc
= ov_write_mask(c
, 0x2a, sixty
?0x00:0x80, 0x80);
293 rc
= ov_write(c
, 0x2b, sixty
?0x00:0xac);
297 rc
= ov_write_mask(c
, 0x76, 0x01, 0x01);
300 case OVCAMCHIP_CID_BANDFILT
:
301 rc
= ov_write_mask(c
, 0x2d, v
?0x04:0x00, 0x04);
304 case OVCAMCHIP_CID_AUTOBRIGHT
:
305 rc
= ov_write_mask(c
, 0x2d, v
?0x10:0x00, 0x10);
308 case OVCAMCHIP_CID_AUTOEXP
:
309 rc
= ov_write_mask(c
, 0x13, v
?0x01:0x00, 0x01);
312 case OVCAMCHIP_CID_BACKLIGHT
:
314 rc
= ov_write_mask(c
, 0x68, v
?0xe0:0xc0, 0xe0);
318 rc
= ov_write_mask(c
, 0x29, v
?0x08:0x00, 0x08);
322 rc
= ov_write_mask(c
, 0x28, v
?0x02:0x00, 0x02);
326 case OVCAMCHIP_CID_MIRROR
:
327 rc
= ov_write_mask(c
, 0x12, v
?0x40:0x00, 0x40);
331 DDEBUG(2, &c
->dev
, "control not supported: %d", ctl
->id
);
336 DDEBUG(3, &c
->dev
, "id=%d, arg=%d, rc=%d", ctl
->id
, v
, rc
);
340 static int ov7x20_get_v4l1_control(struct i2c_client
*c
,
341 struct ovcamchip_control
*ctl
)
343 struct ovcamchip
*ov
= i2c_get_clientdata(c
);
344 struct ov7x20
*s
= ov
->spriv
;
346 unsigned char val
= 0;
349 case OVCAMCHIP_CID_CONT
:
350 rc
= ov_read(c
, 0x64, &val
);
351 ctl
->value
= ov7x20_lut_find(ctab
, 16, val
) << 12;
353 case OVCAMCHIP_CID_BRIGHT
:
354 rc
= ov_read(c
, REG_BRT
, &val
);
355 ctl
->value
= val
<< 8;
357 case OVCAMCHIP_CID_SAT
:
358 rc
= ov_read(c
, REG_SAT
, &val
);
359 ctl
->value
= val
<< 8;
361 case OVCAMCHIP_CID_EXP
:
362 rc
= ov_read(c
, REG_EXP
, &val
);
365 case OVCAMCHIP_CID_BANDFILT
:
366 ctl
->value
= s
->bandfilt
;
368 case OVCAMCHIP_CID_AUTOBRIGHT
:
369 ctl
->value
= s
->auto_brt
;
371 case OVCAMCHIP_CID_AUTOEXP
:
372 ctl
->value
= s
->auto_exp
;
374 case OVCAMCHIP_CID_BACKLIGHT
:
375 ctl
->value
= s
->backlight
;
377 case OVCAMCHIP_CID_MIRROR
:
378 ctl
->value
= s
->mirror
;
381 DDEBUG(2, &c
->dev
, "control not supported: %d", ctl
->id
);
385 DDEBUG(3, &c
->dev
, "id=%d, arg=%d, rc=%d", ctl
->id
, ctl
->value
, rc
);
389 static int ov7x20_mode_init(struct i2c_client
*c
, struct ovcamchip_window
*win
)
391 struct ovcamchip
*ov
= i2c_get_clientdata(c
);
392 int qvga
= win
->quarter
;
394 /******** QVGA-specific regs ********/
395 ov_write_mask(c
, 0x14, qvga
?0x20:0x00, 0x20);
396 ov_write_mask(c
, 0x28, qvga
?0x00:0x20, 0x20);
397 ov_write(c
, 0x24, qvga
?0x20:0x3a);
398 ov_write(c
, 0x25, qvga
?0x30:0x60);
399 ov_write_mask(c
, 0x2d, qvga
?0x40:0x00, 0x40);
401 ov_write_mask(c
, 0x67, qvga
?0xf0:0x90, 0xf0);
402 ov_write_mask(c
, 0x74, qvga
?0x20:0x00, 0x20);
404 /******** Clock programming ********/
406 ov_write(c
, 0x11, win
->clockdiv
);
411 static int ov7x20_set_window(struct i2c_client
*c
, struct ovcamchip_window
*win
)
413 int ret
, hwscale
, vwscale
;
415 ret
= ov7x20_mode_init(c
, win
);
427 ov_write(c
, 0x17, HWSBASE
+ (win
->x
>> hwscale
));
428 ov_write(c
, 0x18, HWEBASE
+ ((win
->x
+ win
->width
) >> hwscale
));
429 ov_write(c
, 0x19, VWSBASE
+ (win
->y
>> vwscale
));
430 ov_write(c
, 0x1a, VWEBASE
+ ((win
->y
+ win
->height
) >> vwscale
));
435 static int ov7x20_command(struct i2c_client
*c
, unsigned int cmd
, void *arg
)
438 case OVCAMCHIP_CMD_S_CTRL
:
439 return ov7x20_set_v4l1_control(c
, arg
);
440 case OVCAMCHIP_CMD_G_CTRL
:
441 return ov7x20_get_v4l1_control(c
, arg
);
442 case OVCAMCHIP_CMD_S_MODE
:
443 return ov7x20_set_window(c
, arg
);
445 DDEBUG(2, &c
->dev
, "command not supported: %d", cmd
);
450 struct ovcamchip_ops ov7x20_ops
= {
453 .command
= ov7x20_command
,