1 /***************************************************************************
2 * Plug-in for OV7630 image sensor connected to the SN9C1xx PC Camera *
5 * Copyright (C) 2006-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
20 ***************************************************************************/
22 #include "sn9c102_sensor.h"
25 static int ov7630_init(struct sn9c102_device* cam)
29 switch (sn9c102_get_bridge(cam)) {
32 err = sn9c102_write_const_regs(cam, {0x00, 0x14}, {0x60, 0x17},
33 {0x0f, 0x18}, {0x50, 0x19});
35 err += sn9c102_i2c_write(cam, 0x12, 0x8d);
36 err += sn9c102_i2c_write(cam, 0x12, 0x0d);
37 err += sn9c102_i2c_write(cam, 0x11, 0x00);
38 err += sn9c102_i2c_write(cam, 0x15, 0x35);
39 err += sn9c102_i2c_write(cam, 0x16, 0x03);
40 err += sn9c102_i2c_write(cam, 0x17, 0x1c);
41 err += sn9c102_i2c_write(cam, 0x18, 0xbd);
42 err += sn9c102_i2c_write(cam, 0x19, 0x06);
43 err += sn9c102_i2c_write(cam, 0x1a, 0xf6);
44 err += sn9c102_i2c_write(cam, 0x1b, 0x04);
45 err += sn9c102_i2c_write(cam, 0x20, 0x44);
46 err += sn9c102_i2c_write(cam, 0x23, 0xee);
47 err += sn9c102_i2c_write(cam, 0x26, 0xa0);
48 err += sn9c102_i2c_write(cam, 0x27, 0x9a);
49 err += sn9c102_i2c_write(cam, 0x28, 0x20);
50 err += sn9c102_i2c_write(cam, 0x29, 0x30);
51 err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
52 err += sn9c102_i2c_write(cam, 0x30, 0x24);
53 err += sn9c102_i2c_write(cam, 0x32, 0x86);
54 err += sn9c102_i2c_write(cam, 0x60, 0xa9);
55 err += sn9c102_i2c_write(cam, 0x61, 0x42);
56 err += sn9c102_i2c_write(cam, 0x65, 0x00);
57 err += sn9c102_i2c_write(cam, 0x69, 0x38);
58 err += sn9c102_i2c_write(cam, 0x6f, 0x88);
59 err += sn9c102_i2c_write(cam, 0x70, 0x0b);
60 err += sn9c102_i2c_write(cam, 0x71, 0x00);
61 err += sn9c102_i2c_write(cam, 0x74, 0x21);
62 err += sn9c102_i2c_write(cam, 0x7d, 0xf7);
65 err = sn9c102_write_const_regs(cam, {0x00, 0x02}, {0x00, 0x03},
66 {0x1a, 0x04}, {0x20, 0x05},
67 {0x20, 0x06}, {0x20, 0x07},
68 {0x03, 0x10}, {0x0a, 0x14},
69 {0x60, 0x17}, {0x0f, 0x18},
70 {0x50, 0x19}, {0x1d, 0x1a},
71 {0x10, 0x1b}, {0x02, 0x1c},
72 {0x03, 0x1d}, {0x0f, 0x1e},
73 {0x0c, 0x1f}, {0x00, 0x20},
74 {0x10, 0x21}, {0x20, 0x22},
75 {0x30, 0x23}, {0x40, 0x24},
76 {0x50, 0x25}, {0x60, 0x26},
77 {0x70, 0x27}, {0x80, 0x28},
78 {0x90, 0x29}, {0xa0, 0x2a},
79 {0xb0, 0x2b}, {0xc0, 0x2c},
80 {0xd0, 0x2d}, {0xe0, 0x2e},
81 {0xf0, 0x2f}, {0xff, 0x30});
83 err += sn9c102_i2c_write(cam, 0x12, 0x8d);
84 err += sn9c102_i2c_write(cam, 0x12, 0x0d);
85 err += sn9c102_i2c_write(cam, 0x15, 0x34);
86 err += sn9c102_i2c_write(cam, 0x11, 0x01);
87 err += sn9c102_i2c_write(cam, 0x1b, 0x04);
88 err += sn9c102_i2c_write(cam, 0x20, 0x44);
89 err += sn9c102_i2c_write(cam, 0x23, 0xee);
90 err += sn9c102_i2c_write(cam, 0x26, 0xa0);
91 err += sn9c102_i2c_write(cam, 0x27, 0x9a);
92 err += sn9c102_i2c_write(cam, 0x28, 0x20);
93 err += sn9c102_i2c_write(cam, 0x29, 0x30);
94 err += sn9c102_i2c_write(cam, 0x2f, 0x3d);
95 err += sn9c102_i2c_write(cam, 0x30, 0x24);
96 err += sn9c102_i2c_write(cam, 0x32, 0x86);
97 err += sn9c102_i2c_write(cam, 0x60, 0xa9);
98 err += sn9c102_i2c_write(cam, 0x61, 0x42);
99 err += sn9c102_i2c_write(cam, 0x65, 0x00);
100 err += sn9c102_i2c_write(cam, 0x69, 0x38);
101 err += sn9c102_i2c_write(cam, 0x6f, 0x88);
102 err += sn9c102_i2c_write(cam, 0x70, 0x0b);
103 err += sn9c102_i2c_write(cam, 0x71, 0x00);
104 err += sn9c102_i2c_write(cam, 0x74, 0x21);
105 err += sn9c102_i2c_write(cam, 0x7d, 0xf7);
115 static int ov7630_get_ctrl(struct sn9c102_device* cam,
116 struct v4l2_control* ctrl)
121 case V4L2_CID_EXPOSURE:
122 if ((ctrl->value = sn9c102_i2c_read(cam, 0x10)) < 0)
125 case V4L2_CID_RED_BALANCE:
126 ctrl->value = sn9c102_pread_reg(cam, 0x07);
128 case V4L2_CID_BLUE_BALANCE:
129 ctrl->value = sn9c102_pread_reg(cam, 0x06);
131 case SN9C102_V4L2_CID_GREEN_BALANCE:
132 ctrl->value = sn9c102_pread_reg(cam, 0x05);
135 if ((ctrl->value = sn9c102_i2c_read(cam, 0x00)) < 0)
139 case V4L2_CID_DO_WHITE_BALANCE:
140 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0c)) < 0)
144 case V4L2_CID_WHITENESS:
145 if ((ctrl->value = sn9c102_i2c_read(cam, 0x0d)) < 0)
149 case V4L2_CID_AUTOGAIN:
150 if ((ctrl->value = sn9c102_i2c_read(cam, 0x13)) < 0)
155 if ((ctrl->value = sn9c102_i2c_read(cam, 0x75)) < 0)
157 ctrl->value = (ctrl->value & 0x80) ? 1 : 0;
159 case SN9C102_V4L2_CID_GAMMA:
160 if ((ctrl->value = sn9c102_i2c_read(cam, 0x14)) < 0)
162 ctrl->value = (ctrl->value & 0x02) ? 1 : 0;
164 case SN9C102_V4L2_CID_BAND_FILTER:
165 if ((ctrl->value = sn9c102_i2c_read(cam, 0x2d)) < 0)
167 ctrl->value = (ctrl->value & 0x02) ? 1 : 0;
173 return err ? -EIO : 0;
177 static int ov7630_set_ctrl(struct sn9c102_device* cam,
178 const struct v4l2_control* ctrl)
183 case V4L2_CID_EXPOSURE:
184 err += sn9c102_i2c_write(cam, 0x10, ctrl->value);
186 case V4L2_CID_RED_BALANCE:
187 err += sn9c102_write_reg(cam, ctrl->value, 0x07);
189 case V4L2_CID_BLUE_BALANCE:
190 err += sn9c102_write_reg(cam, ctrl->value, 0x06);
192 case SN9C102_V4L2_CID_GREEN_BALANCE:
193 err += sn9c102_write_reg(cam, ctrl->value, 0x05);
196 err += sn9c102_i2c_write(cam, 0x00, ctrl->value);
198 case V4L2_CID_DO_WHITE_BALANCE:
199 err += sn9c102_i2c_write(cam, 0x0c, ctrl->value);
201 case V4L2_CID_WHITENESS:
202 err += sn9c102_i2c_write(cam, 0x0d, ctrl->value);
204 case V4L2_CID_AUTOGAIN:
205 err += sn9c102_i2c_write(cam, 0x13, ctrl->value |
209 err += sn9c102_i2c_write(cam, 0x75, 0x0e | (ctrl->value << 7));
211 case SN9C102_V4L2_CID_GAMMA:
212 err += sn9c102_i2c_write(cam, 0x14, ctrl->value << 2);
214 case SN9C102_V4L2_CID_BAND_FILTER:
215 err += sn9c102_i2c_write(cam, 0x2d, ctrl->value << 2);
221 return err ? -EIO : 0;
225 static int ov7630_set_crop(struct sn9c102_device* cam,
226 const struct v4l2_rect* rect)
228 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
230 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1,
231 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
233 err += sn9c102_write_reg(cam, h_start, 0x12);
234 err += sn9c102_write_reg(cam, v_start, 0x13);
240 static int ov7630_set_pix_format(struct sn9c102_device* cam,
241 const struct v4l2_pix_format* pix)
245 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X)
246 err += sn9c102_write_reg(cam, 0x20, 0x19);
248 err += sn9c102_write_reg(cam, 0x50, 0x19);
254 static const struct sn9c102_sensor ov7630 = {
256 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
257 .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102 | BRIDGE_SN9C103,
258 .sysfs_ops = SN9C102_I2C_READ | SN9C102_I2C_WRITE,
259 .frequency = SN9C102_I2C_100KHZ,
260 .interface = SN9C102_I2C_2WIRES,
261 .i2c_slave_id = 0x21,
262 .init = &ov7630_init,
266 .type = V4L2_CTRL_TYPE_INTEGER,
267 .name = "global gain",
271 .default_value = 0x14,
275 .id = V4L2_CID_EXPOSURE,
276 .type = V4L2_CTRL_TYPE_INTEGER,
281 .default_value = 0x60,
285 .id = V4L2_CID_WHITENESS,
286 .type = V4L2_CTRL_TYPE_INTEGER,
287 .name = "white balance background: red",
291 .default_value = 0x20,
295 .id = V4L2_CID_DO_WHITE_BALANCE,
296 .type = V4L2_CTRL_TYPE_INTEGER,
297 .name = "white balance background: blue",
301 .default_value = 0x20,
305 .id = V4L2_CID_RED_BALANCE,
306 .type = V4L2_CTRL_TYPE_INTEGER,
307 .name = "red balance",
311 .default_value = 0x20,
315 .id = V4L2_CID_BLUE_BALANCE,
316 .type = V4L2_CTRL_TYPE_INTEGER,
317 .name = "blue balance",
321 .default_value = 0x20,
325 .id = V4L2_CID_AUTOGAIN,
326 .type = V4L2_CTRL_TYPE_BOOLEAN,
327 .name = "auto adjust",
331 .default_value = 0x00,
335 .id = V4L2_CID_VFLIP,
336 .type = V4L2_CTRL_TYPE_BOOLEAN,
337 .name = "vertical flip",
341 .default_value = 0x01,
345 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
346 .type = V4L2_CTRL_TYPE_INTEGER,
347 .name = "green balance",
351 .default_value = 0x20,
355 .id = SN9C102_V4L2_CID_BAND_FILTER,
356 .type = V4L2_CTRL_TYPE_BOOLEAN,
357 .name = "band filter",
361 .default_value = 0x00,
365 .id = SN9C102_V4L2_CID_GAMMA,
366 .type = V4L2_CTRL_TYPE_BOOLEAN,
371 .default_value = 0x00,
375 .get_ctrl = &ov7630_get_ctrl,
376 .set_ctrl = &ov7630_set_ctrl,
391 .set_crop = &ov7630_set_crop,
395 .pixelformat = V4L2_PIX_FMT_SN9C10X,
398 .set_pix_format = &ov7630_set_pix_format
402 int sn9c102_probe_ov7630(struct sn9c102_device* cam)
404 int pid, ver, err = 0;
406 switch (sn9c102_get_bridge(cam)) {
409 err = sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01},
412 case BRIDGE_SN9C103: /* do _not_ change anything! */
413 err = sn9c102_write_const_regs(cam, {0x09, 0x01}, {0x42, 0x01},
414 {0x28, 0x17}, {0x44, 0x02});
415 pid = sn9c102_i2c_try_read(cam, &ov7630, 0x0a);
416 if (err || pid < 0) /* try a different initialization */
417 err += sn9c102_write_const_regs(cam, {0x01, 0x01},
424 pid = sn9c102_i2c_try_read(cam, &ov7630, 0x0a);
425 ver = sn9c102_i2c_try_read(cam, &ov7630, 0x0b);
426 if (err || pid < 0 || ver < 0)
428 if (pid != 0x76 || ver != 0x31)
430 sn9c102_attach_sensor(cam, &ov7630);