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 = kmalloc(sizeof *s, GFP_KERNEL);
238 memset(s, 0, sizeof *s);
241 s->auto_exp = DFL_AUTO_EXP;
242 s->auto_gain = DFL_AUTO_GAIN;
247 static int ov7x20_free(struct i2c_client *c)
249 struct ovcamchip *ov = i2c_get_clientdata(c);
255 static int ov7x20_set_v4l1_control(struct i2c_client *c,
256 struct ovcamchip_control *ctl)
258 struct ovcamchip *ov = i2c_get_clientdata(c);
259 struct ov7x20 *s = ov->spriv;
264 case OVCAMCHIP_CID_CONT:
266 /* Use Y gamma control instead. Bit 0 enables it. */
267 rc = ov_write(c, 0x64, ctab[v >> 12]);
270 case OVCAMCHIP_CID_BRIGHT:
271 /* 7620 doesn't like manual changes when in auto mode */
273 rc = ov_write(c, REG_BRT, v >> 8);
277 case OVCAMCHIP_CID_SAT:
278 rc = ov_write(c, REG_SAT, v >> 8);
280 case OVCAMCHIP_CID_EXP:
282 rc = ov_write(c, REG_EXP, v);
286 case OVCAMCHIP_CID_FREQ:
288 int sixty = (v == 60);
290 rc = ov_write_mask(c, 0x2a, sixty?0x00:0x80, 0x80);
294 rc = ov_write(c, 0x2b, sixty?0x00:0xac);
298 rc = ov_write_mask(c, 0x76, 0x01, 0x01);
301 case OVCAMCHIP_CID_BANDFILT:
302 rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
305 case OVCAMCHIP_CID_AUTOBRIGHT:
306 rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
309 case OVCAMCHIP_CID_AUTOEXP:
310 rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
313 case OVCAMCHIP_CID_BACKLIGHT:
315 rc = ov_write_mask(c, 0x68, v?0xe0:0xc0, 0xe0);
319 rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
323 rc = ov_write_mask(c, 0x28, v?0x02:0x00, 0x02);
327 case OVCAMCHIP_CID_MIRROR:
328 rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
332 DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
337 DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
341 static int ov7x20_get_v4l1_control(struct i2c_client *c,
342 struct ovcamchip_control *ctl)
344 struct ovcamchip *ov = i2c_get_clientdata(c);
345 struct ov7x20 *s = ov->spriv;
347 unsigned char val = 0;
350 case OVCAMCHIP_CID_CONT:
351 rc = ov_read(c, 0x64, &val);
352 ctl->value = ov7x20_lut_find(ctab, 16, val) << 12;
354 case OVCAMCHIP_CID_BRIGHT:
355 rc = ov_read(c, REG_BRT, &val);
356 ctl->value = val << 8;
358 case OVCAMCHIP_CID_SAT:
359 rc = ov_read(c, REG_SAT, &val);
360 ctl->value = val << 8;
362 case OVCAMCHIP_CID_EXP:
363 rc = ov_read(c, REG_EXP, &val);
366 case OVCAMCHIP_CID_BANDFILT:
367 ctl->value = s->bandfilt;
369 case OVCAMCHIP_CID_AUTOBRIGHT:
370 ctl->value = s->auto_brt;
372 case OVCAMCHIP_CID_AUTOEXP:
373 ctl->value = s->auto_exp;
375 case OVCAMCHIP_CID_BACKLIGHT:
376 ctl->value = s->backlight;
378 case OVCAMCHIP_CID_MIRROR:
379 ctl->value = s->mirror;
382 DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
386 DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
390 static int ov7x20_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
392 struct ovcamchip *ov = i2c_get_clientdata(c);
393 int qvga = win->quarter;
395 /******** QVGA-specific regs ********/
396 ov_write_mask(c, 0x14, qvga?0x20:0x00, 0x20);
397 ov_write_mask(c, 0x28, qvga?0x00:0x20, 0x20);
398 ov_write(c, 0x24, qvga?0x20:0x3a);
399 ov_write(c, 0x25, qvga?0x30:0x60);
400 ov_write_mask(c, 0x2d, qvga?0x40:0x00, 0x40);
402 ov_write_mask(c, 0x67, qvga?0xf0:0x90, 0xf0);
403 ov_write_mask(c, 0x74, qvga?0x20:0x00, 0x20);
405 /******** Clock programming ********/
407 ov_write(c, 0x11, win->clockdiv);
412 static int ov7x20_set_window(struct i2c_client *c, struct ovcamchip_window *win)
414 int ret, hwscale, vwscale;
416 ret = ov7x20_mode_init(c, win);
428 ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
429 ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
430 ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
431 ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
436 static int ov7x20_command(struct i2c_client *c, unsigned int cmd, void *arg)
439 case OVCAMCHIP_CMD_S_CTRL:
440 return ov7x20_set_v4l1_control(c, arg);
441 case OVCAMCHIP_CMD_G_CTRL:
442 return ov7x20_get_v4l1_control(c, arg);
443 case OVCAMCHIP_CMD_S_MODE:
444 return ov7x20_set_window(c, arg);
446 DDEBUG(2, &c->dev, "command not supported: %d", cmd);
451 struct ovcamchip_ops ov7x20_ops = {
454 .command = ov7x20_command,