Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / ovcamchip / ov7x20.c
blob3c8c48f338bafff423d17b732111e34eb19167f3
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.
14 #define DEBUG
16 #include <linux/slab.h>
17 #include "ovcamchip_priv.h"
19 /* Registers */
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) */
46 #define HWEBASE 0x2F
47 #define VWSBASE 0x05
48 #define VWEBASE 0x05
50 struct ov7x20 {
51 int auto_brt;
52 int auto_exp;
53 int auto_gain;
54 int backlight;
55 int bandfilt;
56 int mirror;
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 },
72 { 0x01, 0x80 },
73 { 0x02, 0x80 },
74 { 0x03, OV7120_DFL_SAT },
75 { 0x06, OV7120_DFL_BRIGHT },
76 { 0x07, 0x00 },
77 { 0x0c, 0x20 },
78 { 0x0d, 0x20 },
79 { 0x11, 0x01 },
80 { 0x14, 0x84 },
81 { 0x15, 0x01 },
82 { 0x16, 0x03 },
83 { 0x17, 0x2f },
84 { 0x18, 0xcf },
85 { 0x19, 0x06 },
86 { 0x1a, 0xf5 },
87 { 0x1b, 0x00 },
88 { 0x20, 0x08 },
89 { 0x21, 0x80 },
90 { 0x22, 0x80 },
91 { 0x23, 0x00 },
92 { 0x26, 0xa0 },
93 { 0x27, 0xfa },
94 { 0x28, 0x20 }, /* DON'T set bit 6. It is for the OV7620 only */
95 { 0x29, DFL_AUTO_EXP?0x00:0x80 },
96 { 0x2a, 0x10 },
97 { 0x2b, 0x00 },
98 { 0x2c, 0x88 },
99 { 0x2d, 0x95 },
100 { 0x2e, 0x80 },
101 { 0x2f, 0x44 },
102 { 0x60, 0x20 },
103 { 0x61, 0x02 },
104 { 0x62, 0x5f },
105 { 0x63, 0xd5 },
106 { 0x64, 0x57 },
107 { 0x65, 0x83 }, /* OV says "don't change this value" */
108 { 0x66, 0x55 },
109 { 0x67, 0x92 },
110 { 0x68, 0xcf },
111 { 0x69, 0x76 },
112 { 0x6a, 0x22 },
113 { 0x6b, 0xe2 },
114 { 0x6c, 0x40 },
115 { 0x6d, 0x48 },
116 { 0x6e, 0x80 },
117 { 0x6f, 0x0d },
118 { 0x70, 0x89 },
119 { 0x71, 0x00 },
120 { 0x72, 0x14 },
121 { 0x73, 0x54 },
122 { 0x74, 0xa0 },
123 { 0x75, 0x8e },
124 { 0x76, 0x00 },
125 { 0x77, 0xff },
126 { 0x78, 0x80 },
127 { 0x79, 0x80 },
128 { 0x7a, 0x80 },
129 { 0x7b, 0xe6 },
130 { 0x7c, 0x00 },
131 { 0x24, 0x3a },
132 { 0x25, 0x60 },
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 },
140 { 0x01, 0x80 },
141 { 0x02, 0x80 },
142 { 0x03, OV7620_DFL_SAT },
143 { 0x06, OV7620_DFL_BRIGHT },
144 { 0x07, 0x00 },
145 { 0x0c, 0x24 },
146 { 0x0c, 0x24 },
147 { 0x0d, 0x24 },
148 { 0x11, 0x01 },
149 { 0x12, 0x24 },
150 { 0x13, DFL_AUTO_GAIN?0x01:0x00 },
151 { 0x14, 0x84 },
152 { 0x15, 0x01 },
153 { 0x16, 0x03 },
154 { 0x17, 0x2f },
155 { 0x18, 0xcf },
156 { 0x19, 0x06 },
157 { 0x1a, 0xf5 },
158 { 0x1b, 0x00 },
159 { 0x20, 0x18 },
160 { 0x21, 0x80 },
161 { 0x22, 0x80 },
162 { 0x23, 0x00 },
163 { 0x26, 0xa2 },
164 { 0x27, 0xea },
165 { 0x28, 0x20 },
166 { 0x29, DFL_AUTO_EXP?0x00:0x80 },
167 { 0x2a, 0x10 },
168 { 0x2b, 0x00 },
169 { 0x2c, 0x88 },
170 { 0x2d, 0x91 },
171 { 0x2e, 0x80 },
172 { 0x2f, 0x44 },
173 { 0x60, 0x27 },
174 { 0x61, 0x02 },
175 { 0x62, 0x5f },
176 { 0x63, 0xd5 },
177 { 0x64, 0x57 },
178 { 0x65, 0x83 },
179 { 0x66, 0x55 },
180 { 0x67, 0x92 },
181 { 0x68, 0xcf },
182 { 0x69, 0x76 },
183 { 0x6a, 0x22 },
184 { 0x6b, 0x00 },
185 { 0x6c, 0x02 },
186 { 0x6d, 0x44 },
187 { 0x6e, 0x80 },
188 { 0x6f, 0x1d },
189 { 0x70, 0x8b },
190 { 0x71, 0x00 },
191 { 0x72, 0x14 },
192 { 0x73, 0x54 },
193 { 0x74, 0x00 },
194 { 0x75, 0x8e },
195 { 0x76, 0x00 },
196 { 0x77, 0xff },
197 { 0x78, 0x80 },
198 { 0x79, 0x80 },
199 { 0x7a, 0x80 },
200 { 0x7b, 0xe2 },
201 { 0x7c, 0x00 },
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)
210 int i = 0;
212 while (lut[i] < val && i < n)
213 i++;
215 return i;
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);
222 struct ov7x20 *s;
223 int rc;
225 DDEBUG(4, &c->dev, "entered");
227 if (ov->mono)
228 rc = ov_write_regvals(c, regvals_init_7120);
229 else
230 rc = ov_write_regvals(c, regvals_init_7620);
232 if (rc < 0)
233 return rc;
235 ov->spriv = s = kmalloc(sizeof *s, GFP_KERNEL);
236 if (!s)
237 return -ENOMEM;
238 memset(s, 0, sizeof *s);
240 s->auto_brt = 1;
241 s->auto_exp = DFL_AUTO_EXP;
242 s->auto_gain = DFL_AUTO_GAIN;
244 return 0;
247 static int ov7x20_free(struct i2c_client *c)
249 struct ovcamchip *ov = i2c_get_clientdata(c);
251 kfree(ov->spriv);
252 return 0;
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;
260 int rc;
261 int v = ctl->value;
263 switch (ctl->id) {
264 case OVCAMCHIP_CID_CONT:
266 /* Use Y gamma control instead. Bit 0 enables it. */
267 rc = ov_write(c, 0x64, ctab[v >> 12]);
268 break;
270 case OVCAMCHIP_CID_BRIGHT:
271 /* 7620 doesn't like manual changes when in auto mode */
272 if (!s->auto_brt)
273 rc = ov_write(c, REG_BRT, v >> 8);
274 else
275 rc = 0;
276 break;
277 case OVCAMCHIP_CID_SAT:
278 rc = ov_write(c, REG_SAT, v >> 8);
279 break;
280 case OVCAMCHIP_CID_EXP:
281 if (!s->auto_exp)
282 rc = ov_write(c, REG_EXP, v);
283 else
284 rc = -EBUSY;
285 break;
286 case OVCAMCHIP_CID_FREQ:
288 int sixty = (v == 60);
290 rc = ov_write_mask(c, 0x2a, sixty?0x00:0x80, 0x80);
291 if (rc < 0)
292 goto out;
294 rc = ov_write(c, 0x2b, sixty?0x00:0xac);
295 if (rc < 0)
296 goto out;
298 rc = ov_write_mask(c, 0x76, 0x01, 0x01);
299 break;
301 case OVCAMCHIP_CID_BANDFILT:
302 rc = ov_write_mask(c, 0x2d, v?0x04:0x00, 0x04);
303 s->bandfilt = v;
304 break;
305 case OVCAMCHIP_CID_AUTOBRIGHT:
306 rc = ov_write_mask(c, 0x2d, v?0x10:0x00, 0x10);
307 s->auto_brt = v;
308 break;
309 case OVCAMCHIP_CID_AUTOEXP:
310 rc = ov_write_mask(c, 0x13, v?0x01:0x00, 0x01);
311 s->auto_exp = v;
312 break;
313 case OVCAMCHIP_CID_BACKLIGHT:
315 rc = ov_write_mask(c, 0x68, v?0xe0:0xc0, 0xe0);
316 if (rc < 0)
317 goto out;
319 rc = ov_write_mask(c, 0x29, v?0x08:0x00, 0x08);
320 if (rc < 0)
321 goto out;
323 rc = ov_write_mask(c, 0x28, v?0x02:0x00, 0x02);
324 s->backlight = v;
325 break;
327 case OVCAMCHIP_CID_MIRROR:
328 rc = ov_write_mask(c, 0x12, v?0x40:0x00, 0x40);
329 s->mirror = v;
330 break;
331 default:
332 DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
333 return -EPERM;
336 out:
337 DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, v, rc);
338 return 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;
346 int rc = 0;
347 unsigned char val = 0;
349 switch (ctl->id) {
350 case OVCAMCHIP_CID_CONT:
351 rc = ov_read(c, 0x64, &val);
352 ctl->value = ov7x20_lut_find(ctab, 16, val) << 12;
353 break;
354 case OVCAMCHIP_CID_BRIGHT:
355 rc = ov_read(c, REG_BRT, &val);
356 ctl->value = val << 8;
357 break;
358 case OVCAMCHIP_CID_SAT:
359 rc = ov_read(c, REG_SAT, &val);
360 ctl->value = val << 8;
361 break;
362 case OVCAMCHIP_CID_EXP:
363 rc = ov_read(c, REG_EXP, &val);
364 ctl->value = val;
365 break;
366 case OVCAMCHIP_CID_BANDFILT:
367 ctl->value = s->bandfilt;
368 break;
369 case OVCAMCHIP_CID_AUTOBRIGHT:
370 ctl->value = s->auto_brt;
371 break;
372 case OVCAMCHIP_CID_AUTOEXP:
373 ctl->value = s->auto_exp;
374 break;
375 case OVCAMCHIP_CID_BACKLIGHT:
376 ctl->value = s->backlight;
377 break;
378 case OVCAMCHIP_CID_MIRROR:
379 ctl->value = s->mirror;
380 break;
381 default:
382 DDEBUG(2, &c->dev, "control not supported: %d", ctl->id);
383 return -EPERM;
386 DDEBUG(3, &c->dev, "id=%d, arg=%d, rc=%d", ctl->id, ctl->value, rc);
387 return 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);
401 if (!ov->mono)
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);
409 return 0;
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);
417 if (ret < 0)
418 return ret;
420 if (win->quarter) {
421 hwscale = 1;
422 vwscale = 0;
423 } else {
424 hwscale = 2;
425 vwscale = 1;
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));
433 return 0;
436 static int ov7x20_command(struct i2c_client *c, unsigned int cmd, void *arg)
438 switch (cmd) {
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);
445 default:
446 DDEBUG(2, &c->dev, "command not supported: %d", cmd);
447 return -ENOIOCTLCMD;
451 struct ovcamchip_ops ov7x20_ops = {
452 .init = ov7x20_init,
453 .free = ov7x20_free,
454 .command = ov7x20_command,