1 /* OmniVision OV6620/OV6120 Camera Chip Support Code
3 * Copyright (c) 1999-2004 Mark McClelland <mark@alpha.dyndns.org>
4 * http://alpha.dyndns.org/ov511/
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
14 #include <linux/slab.h>
15 #include "ovcamchip_priv.h"
18 #define REG_GAIN 0x00 /* gain [5:0] */
19 #define REG_BLUE 0x01 /* blue gain */
20 #define REG_RED 0x02 /* red gain */
21 #define REG_SAT 0x03 /* saturation */
22 #define REG_CNT 0x05 /* Y contrast */
23 #define REG_BRT 0x06 /* Y brightness */
24 #define REG_WB_BLUE 0x0C /* WB blue ratio [5:0] */
25 #define REG_WB_RED 0x0D /* WB red ratio [5:0] */
26 #define REG_EXP 0x10 /* exposure */
28 /* Window parameters */
42 /* Initial values for use with OV511/OV511+ cameras */
43 static struct ovcamchip_regvals regvals_init_6x20_511[] = {
44 { 0x12, 0x80 }, /* reset */
47 { 0x05, 0x7f }, /* For when autoadjust is off */
51 { 0x0f, 0x15 }, /* COMS */
52 { 0x10, 0x75 }, /* AEC Exposure time */
53 { 0x12, 0x24 }, /* Enable AGC and AWB */
56 { 0x26, 0xb2 }, /* BLC enable */
57 /* 0x28: 0x05 Selects RGB format if RGB on */
59 { 0x2a, 0x04 }, /* Disable framerate adjust */
61 { 0x33, 0xa0 }, /* Color Processing Parameter */
62 { 0x34, 0xd2 }, /* Max A/D range */
66 { 0x3c, 0x39 }, /* Enable AEC mode changing */
67 { 0x3c, 0x3c }, /* Change AEC mode */
68 { 0x3c, 0x24 }, /* Disable AEC mode changing */
71 /* These next two registers (0x4a, 0x4b) are undocumented. They
72 * control the color balance */
75 { 0x4d, 0xd2 }, /* This reduces noise a bit */
78 { 0xff, 0xff }, /* END MARKER */
81 /* Initial values for use with OV518 cameras */
82 static struct ovcamchip_regvals regvals_init_6x20_518[] = {
83 { 0x12, 0x80 }, /* Do a reset */
84 { 0x03, 0xc0 }, /* Saturation */
85 { 0x05, 0x8a }, /* Contrast */
86 { 0x0c, 0x24 }, /* AWB blue */
87 { 0x0d, 0x24 }, /* AWB red */
88 { 0x0e, 0x8d }, /* Additional 2x gain */
89 { 0x0f, 0x25 }, /* Black expanding level = 1.3V */
90 { 0x11, 0x01 }, /* Clock div. */
91 { 0x12, 0x24 }, /* Enable AGC and AWB */
92 { 0x13, 0x01 }, /* (default) */
93 { 0x14, 0x80 }, /* Set reserved bit 7 */
94 { 0x15, 0x01 }, /* (default) */
95 { 0x16, 0x03 }, /* (default) */
96 { 0x17, 0x38 }, /* (default) */
97 { 0x18, 0xea }, /* (default) */
100 { 0x1b, 0x00 }, /* (default) */
101 { 0x1e, 0xc4 }, /* (default) */
102 { 0x1f, 0x04 }, /* (default) */
103 { 0x20, 0x20 }, /* Enable 1st stage aperture correction */
104 { 0x21, 0x10 }, /* Y offset */
105 { 0x22, 0x88 }, /* U offset */
106 { 0x23, 0xc0 }, /* Set XTAL power level */
107 { 0x24, 0x53 }, /* AEC bright ratio */
108 { 0x25, 0x7a }, /* AEC black ratio */
109 { 0x26, 0xb2 }, /* BLC enable */
110 { 0x27, 0xa2 }, /* Full output range */
111 { 0x28, 0x01 }, /* (default) */
112 { 0x29, 0x00 }, /* (default) */
113 { 0x2a, 0x84 }, /* (default) */
114 { 0x2b, 0xa8 }, /* Set custom frame rate */
115 { 0x2c, 0xa0 }, /* (reserved) */
116 { 0x2d, 0x95 }, /* Enable banding filter */
117 { 0x2e, 0x88 }, /* V offset */
118 { 0x33, 0x22 }, /* Luminance gamma on */
119 { 0x34, 0xc7 }, /* A/D bias */
120 { 0x36, 0x12 }, /* (reserved) */
121 { 0x37, 0x63 }, /* (reserved) */
122 { 0x38, 0x8b }, /* Quick AEC/AEB */
123 { 0x39, 0x00 }, /* (default) */
124 { 0x3a, 0x0f }, /* (default) */
125 { 0x3b, 0x3c }, /* (default) */
126 { 0x3c, 0x5c }, /* AEC controls */
127 { 0x3d, 0x80 }, /* Drop 1 (bad) frame when AEC change */
128 { 0x3e, 0x80 }, /* (default) */
129 { 0x3f, 0x02 }, /* (default) */
130 { 0x40, 0x10 }, /* (reserved) */
131 { 0x41, 0x10 }, /* (reserved) */
132 { 0x42, 0x00 }, /* (reserved) */
133 { 0x43, 0x7f }, /* (reserved) */
134 { 0x44, 0x80 }, /* (reserved) */
135 { 0x45, 0x1c }, /* (reserved) */
136 { 0x46, 0x1c }, /* (reserved) */
137 { 0x47, 0x80 }, /* (reserved) */
138 { 0x48, 0x5f }, /* (reserved) */
139 { 0x49, 0x00 }, /* (reserved) */
140 { 0x4a, 0x00 }, /* Color balance (undocumented) */
141 { 0x4b, 0x80 }, /* Color balance (undocumented) */
142 { 0x4c, 0x58 }, /* (reserved) */
143 { 0x4d, 0xd2 }, /* U *= .938, V *= .838 */
144 { 0x4e, 0xa0 }, /* (default) */
145 { 0x4f, 0x04 }, /* UV 3-point average */
146 { 0x50, 0xff }, /* (reserved) */
147 { 0x51, 0x58 }, /* (reserved) */
148 { 0x52, 0xc0 }, /* (reserved) */
149 { 0x53, 0x42 }, /* (reserved) */
150 { 0x27, 0xa6 }, /* Enable manual offset adj. (reg 21 & 22) */
154 { 0xff, 0xff }, /* END MARKER */
157 /* This initializes the OV6x20 camera chip and relevant variables. */
158 static int ov6x20_init(struct i2c_client *c)
160 struct ovcamchip *ov = i2c_get_clientdata(c);
164 DDEBUG(4, &c->dev, "entered");
166 switch (c->adapter->id) {
167 case I2C_HW_SMBUS_OV511:
168 rc = ov_write_regvals(c, regvals_init_6x20_511);
170 case I2C_HW_SMBUS_OV518:
171 rc = ov_write_regvals(c, regvals_init_6x20_518);
174 dev_err(&c->dev, "ov6x20: Unsupported adapter\n");
181 ov->spriv = s = kzalloc(sizeof *s, GFP_KERNEL);
191 static int ov6x20_free(struct i2c_client *c)
193 struct ovcamchip *ov = i2c_get_clientdata(c);
199 static int ov6x20_set_control(struct i2c_client *c,
200 struct ovcamchip_control *ctl)
202 struct ovcamchip *ov = i2c_get_clientdata(c);
203 struct ov6x20 *s = ov->spriv;
208 case OVCAMCHIP_CID_CONT:
209 rc = ov_write(c, REG_CNT, v >> 8);
211 case OVCAMCHIP_CID_BRIGHT:
212 rc = ov_write(c, REG_BRT, v >> 8);
214 case OVCAMCHIP_CID_SAT:
215 rc = ov_write(c, REG_SAT, v >> 8);
217 case OVCAMCHIP_CID_HUE:
218 rc = ov_write(c, REG_RED, 0xFF - (v >> 8));
222 rc = ov_write(c, REG_BLUE, v >> 8);
224 case OVCAMCHIP_CID_EXP:
225 rc = ov_write(c, REG_EXP, v);
227 case OVCAMCHIP_CID_FREQ:
229 int sixty = (v == 60);
231 rc = ov_write(c, 0x2b, sixty?0xa8:0x28);
235 rc = ov_write(c, 0x2a, sixty?0x84:0xa4);
238 case OVCAMCHIP_CID_BANDFILT:
239 rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
242 case OVCAMCHIP_CID_AUTOBRIGHT:
243 rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
246 case OVCAMCHIP_CID_AUTOEXP:
247 rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
250 case OVCAMCHIP_CID_BACKLIGHT:
252 rc = ov_write_mask(c, 0x4e, v?0xe0:0xc0, 0xe0);
256 rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
260 rc = ov_write_mask(c, 0x0e, v?0x80:0x00, 0x80);
264 case OVCAMCHIP_CID_MIRROR:
265 rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
269 DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
274 DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
278 static int ov6x20_get_control(struct i2c_client *c,
279 struct ovcamchip_control *ctl)
281 struct ovcamchip *ov = i2c_get_clientdata(c);
282 struct ov6x20 *s = ov->spriv;
284 unsigned char val = 0;
287 case OVCAMCHIP_CID_CONT:
288 rc = ov_read(c, REG_CNT, &val);
289 ctl->value = val << 8;
291 case OVCAMCHIP_CID_BRIGHT:
292 rc = ov_read(c, REG_BRT, &val);
293 ctl->value = val << 8;
295 case OVCAMCHIP_CID_SAT:
296 rc = ov_read(c, REG_SAT, &val);
297 ctl->value = val << 8;
299 case OVCAMCHIP_CID_HUE:
300 rc = ov_read(c, REG_BLUE, &val);
301 ctl->value = val << 8;
303 case OVCAMCHIP_CID_EXP:
304 rc = ov_read(c, REG_EXP, &val);
307 case OVCAMCHIP_CID_BANDFILT:
308 ctl->value = s->bandfilt;
310 case OVCAMCHIP_CID_AUTOBRIGHT:
311 ctl->value = s->auto_brt;
313 case OVCAMCHIP_CID_AUTOEXP:
314 ctl->value = s->auto_exp;
316 case OVCAMCHIP_CID_BACKLIGHT:
317 ctl->value = s->backlight;
319 case OVCAMCHIP_CID_MIRROR:
320 ctl->value = s->mirror;
323 DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
327 DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
331 static int ov6x20_mode_init(struct i2c_client *c, struct ovcamchip_window *win)
333 /******** QCIF-specific regs ********/
335 ov_write(c, 0x14, win->quarter?0x24:0x04);
337 /******** Palette-specific regs ********/
339 /* OV518 needs 8 bit multiplexed in color mode, and 16 bit in B&W */
340 if (c->adapter->id == I2C_HW_SMBUS_OV518) {
341 if (win->format == VIDEO_PALETTE_GREY)
342 ov_write_mask(c, 0x13, 0x00, 0x20);
344 ov_write_mask(c, 0x13, 0x20, 0x20);
346 if (win->format == VIDEO_PALETTE_GREY)
347 ov_write_mask(c, 0x13, 0x20, 0x20);
349 ov_write_mask(c, 0x13, 0x00, 0x20);
352 /******** Clock programming ********/
354 /* The OV6620 needs special handling. This prevents the
355 * severe banding that normally occurs */
358 ov_write(c, 0x2a, 0x04);
360 ov_write(c, 0x11, win->clockdiv);
362 ov_write(c, 0x2a, 0x84);
363 /* This next setting is critical. It seems to improve
364 * the gain or the contrast. The "reserved" bits seem
365 * to have some effect in this case. */
366 ov_write(c, 0x2d, 0x85); /* FIXME: This messes up banding filter */
371 static int ov6x20_set_window(struct i2c_client *c, struct ovcamchip_window *win)
373 int ret, hwscale, vwscale;
375 ret = ov6x20_mode_init(c, win);
384 vwscale = 1; /* The datasheet says 0; it's wrong */
387 ov_write(c, 0x17, HWSBASE + (win->x >> hwscale));
388 ov_write(c, 0x18, HWEBASE + ((win->x + win->width) >> hwscale));
389 ov_write(c, 0x19, VWSBASE + (win->y >> vwscale));
390 ov_write(c, 0x1a, VWEBASE + ((win->y + win->height) >> vwscale));
395 static int ov6x20_command(struct i2c_client *c, unsigned int cmd, void *arg)
398 case OVCAMCHIP_CMD_S_CTRL:
399 return ov6x20_set_control(c, arg);
400 case OVCAMCHIP_CMD_G_CTRL:
401 return ov6x20_get_control(c, arg);
402 case OVCAMCHIP_CMD_S_MODE:
403 return ov6x20_set_window(c, arg);
405 DDEBUG(2, &c->dev, "command not supported: %d", cmd);
410 struct ovcamchip_ops ov6x20_ops = {
413 .command = ov6x20_command,