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},
33 {0x60, 0x17}, {0x0f, 0x18},
36 err
+= sn9c102_i2c_write(cam
, 0x12, 0x8d);
37 err
+= sn9c102_i2c_write(cam
, 0x12, 0x0d);
38 err
+= sn9c102_i2c_write(cam
, 0x11, 0x00);
39 err
+= sn9c102_i2c_write(cam
, 0x15, 0x35);
40 err
+= sn9c102_i2c_write(cam
, 0x16, 0x03);
41 err
+= sn9c102_i2c_write(cam
, 0x17, 0x1c);
42 err
+= sn9c102_i2c_write(cam
, 0x18, 0xbd);
43 err
+= sn9c102_i2c_write(cam
, 0x19, 0x06);
44 err
+= sn9c102_i2c_write(cam
, 0x1a, 0xf6);
45 err
+= sn9c102_i2c_write(cam
, 0x1b, 0x04);
46 err
+= sn9c102_i2c_write(cam
, 0x20, 0x44);
47 err
+= sn9c102_i2c_write(cam
, 0x23, 0xee);
48 err
+= sn9c102_i2c_write(cam
, 0x26, 0xa0);
49 err
+= sn9c102_i2c_write(cam
, 0x27, 0x9a);
50 err
+= sn9c102_i2c_write(cam
, 0x28, 0x20);
51 err
+= sn9c102_i2c_write(cam
, 0x29, 0x30);
52 err
+= sn9c102_i2c_write(cam
, 0x2f, 0x3d);
53 err
+= sn9c102_i2c_write(cam
, 0x30, 0x24);
54 err
+= sn9c102_i2c_write(cam
, 0x32, 0x86);
55 err
+= sn9c102_i2c_write(cam
, 0x60, 0xa9);
56 err
+= sn9c102_i2c_write(cam
, 0x61, 0x42);
57 err
+= sn9c102_i2c_write(cam
, 0x65, 0x00);
58 err
+= sn9c102_i2c_write(cam
, 0x69, 0x38);
59 err
+= sn9c102_i2c_write(cam
, 0x6f, 0x88);
60 err
+= sn9c102_i2c_write(cam
, 0x70, 0x0b);
61 err
+= sn9c102_i2c_write(cam
, 0x71, 0x00);
62 err
+= sn9c102_i2c_write(cam
, 0x74, 0x21);
63 err
+= sn9c102_i2c_write(cam
, 0x7d, 0xf7);
67 err
= sn9c102_write_const_regs(cam
, {0x00, 0x02}, {0x00, 0x03},
68 {0x1a, 0x04}, {0x20, 0x05},
69 {0x20, 0x06}, {0x20, 0x07},
70 {0x03, 0x10}, {0x0a, 0x14},
71 {0x60, 0x17}, {0x0f, 0x18},
72 {0x50, 0x19}, {0x1d, 0x1a},
73 {0x10, 0x1b}, {0x02, 0x1c},
74 {0x03, 0x1d}, {0x0f, 0x1e},
75 {0x0c, 0x1f}, {0x00, 0x20},
76 {0x10, 0x21}, {0x20, 0x22},
77 {0x30, 0x23}, {0x40, 0x24},
78 {0x50, 0x25}, {0x60, 0x26},
79 {0x70, 0x27}, {0x80, 0x28},
80 {0x90, 0x29}, {0xa0, 0x2a},
81 {0xb0, 0x2b}, {0xc0, 0x2c},
82 {0xd0, 0x2d}, {0xe0, 0x2e},
83 {0xf0, 0x2f}, {0xff, 0x30});
85 err
+= sn9c102_i2c_write(cam
, 0x12, 0x8d);
86 err
+= sn9c102_i2c_write(cam
, 0x12, 0x0d);
87 err
+= sn9c102_i2c_write(cam
, 0x15, 0x34);
88 err
+= sn9c102_i2c_write(cam
, 0x11, 0x01);
89 err
+= sn9c102_i2c_write(cam
, 0x1b, 0x04);
90 err
+= sn9c102_i2c_write(cam
, 0x20, 0x44);
91 err
+= sn9c102_i2c_write(cam
, 0x23, 0xee);
92 err
+= sn9c102_i2c_write(cam
, 0x26, 0xa0);
93 err
+= sn9c102_i2c_write(cam
, 0x27, 0x9a);
94 err
+= sn9c102_i2c_write(cam
, 0x28, 0x20);
95 err
+= sn9c102_i2c_write(cam
, 0x29, 0x30);
96 err
+= sn9c102_i2c_write(cam
, 0x2f, 0x3d);
97 err
+= sn9c102_i2c_write(cam
, 0x30, 0x24);
98 err
+= sn9c102_i2c_write(cam
, 0x32, 0x86);
99 err
+= sn9c102_i2c_write(cam
, 0x60, 0xa9);
100 err
+= sn9c102_i2c_write(cam
, 0x61, 0x42);
101 err
+= sn9c102_i2c_write(cam
, 0x65, 0x00);
102 err
+= sn9c102_i2c_write(cam
, 0x69, 0x38);
103 err
+= sn9c102_i2c_write(cam
, 0x6f, 0x88);
104 err
+= sn9c102_i2c_write(cam
, 0x70, 0x0b);
105 err
+= sn9c102_i2c_write(cam
, 0x71, 0x00);
106 err
+= sn9c102_i2c_write(cam
, 0x74, 0x21);
107 err
+= sn9c102_i2c_write(cam
, 0x7d, 0xf7);
117 static int ov7630_get_ctrl(struct sn9c102_device
* cam
,
118 struct v4l2_control
* ctrl
)
123 case V4L2_CID_EXPOSURE
:
124 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x10)) < 0)
127 case V4L2_CID_RED_BALANCE
:
128 ctrl
->value
= sn9c102_pread_reg(cam
, 0x07);
130 case V4L2_CID_BLUE_BALANCE
:
131 ctrl
->value
= sn9c102_pread_reg(cam
, 0x06);
133 case SN9C102_V4L2_CID_GREEN_BALANCE
:
134 ctrl
->value
= sn9c102_pread_reg(cam
, 0x05);
137 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x00)) < 0)
141 case V4L2_CID_DO_WHITE_BALANCE
:
142 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x0c)) < 0)
146 case V4L2_CID_WHITENESS
:
147 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x0d)) < 0)
151 case V4L2_CID_AUTOGAIN
:
152 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x13)) < 0)
157 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x75)) < 0)
159 ctrl
->value
= (ctrl
->value
& 0x80) ? 1 : 0;
161 case SN9C102_V4L2_CID_GAMMA
:
162 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x14)) < 0)
164 ctrl
->value
= (ctrl
->value
& 0x02) ? 1 : 0;
166 case SN9C102_V4L2_CID_BAND_FILTER
:
167 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x2d)) < 0)
169 ctrl
->value
= (ctrl
->value
& 0x02) ? 1 : 0;
175 return err
? -EIO
: 0;
179 static int ov7630_set_ctrl(struct sn9c102_device
* cam
,
180 const struct v4l2_control
* ctrl
)
185 case V4L2_CID_EXPOSURE
:
186 err
+= sn9c102_i2c_write(cam
, 0x10, ctrl
->value
);
188 case V4L2_CID_RED_BALANCE
:
189 err
+= sn9c102_write_reg(cam
, ctrl
->value
, 0x07);
191 case V4L2_CID_BLUE_BALANCE
:
192 err
+= sn9c102_write_reg(cam
, ctrl
->value
, 0x06);
194 case SN9C102_V4L2_CID_GREEN_BALANCE
:
195 err
+= sn9c102_write_reg(cam
, ctrl
->value
, 0x05);
198 err
+= sn9c102_i2c_write(cam
, 0x00, ctrl
->value
);
200 case V4L2_CID_DO_WHITE_BALANCE
:
201 err
+= sn9c102_i2c_write(cam
, 0x0c, ctrl
->value
);
203 case V4L2_CID_WHITENESS
:
204 err
+= sn9c102_i2c_write(cam
, 0x0d, ctrl
->value
);
206 case V4L2_CID_AUTOGAIN
:
207 err
+= sn9c102_i2c_write(cam
, 0x13, ctrl
->value
|
211 err
+= sn9c102_i2c_write(cam
, 0x75, 0x0e | (ctrl
->value
<< 7));
213 case SN9C102_V4L2_CID_GAMMA
:
214 err
+= sn9c102_i2c_write(cam
, 0x14, ctrl
->value
<< 2);
216 case SN9C102_V4L2_CID_BAND_FILTER
:
217 err
+= sn9c102_i2c_write(cam
, 0x2d, ctrl
->value
<< 2);
223 return err
? -EIO
: 0;
227 static int ov7630_set_crop(struct sn9c102_device
* cam
,
228 const struct v4l2_rect
* rect
)
230 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
232 u8 h_start
= (u8
)(rect
->left
- s
->cropcap
.bounds
.left
) + 1,
233 v_start
= (u8
)(rect
->top
- s
->cropcap
.bounds
.top
) + 1;
235 err
+= sn9c102_write_reg(cam
, h_start
, 0x12);
236 err
+= sn9c102_write_reg(cam
, v_start
, 0x13);
242 static int ov7630_set_pix_format(struct sn9c102_device
* cam
,
243 const struct v4l2_pix_format
* pix
)
247 if (pix
->pixelformat
== V4L2_PIX_FMT_SN9C10X
)
248 err
+= sn9c102_write_reg(cam
, 0x20, 0x19);
250 err
+= sn9c102_write_reg(cam
, 0x50, 0x19);
256 static struct sn9c102_sensor ov7630
= {
258 .maintainer
= "Luca Risolia <luca.risolia@studio.unibo.it>",
259 .supported_bridge
= BRIDGE_SN9C101
| BRIDGE_SN9C102
| BRIDGE_SN9C103
,
260 .sysfs_ops
= SN9C102_I2C_READ
| SN9C102_I2C_WRITE
,
261 .frequency
= SN9C102_I2C_100KHZ
,
262 .interface
= SN9C102_I2C_2WIRES
,
263 .i2c_slave_id
= 0x21,
264 .init
= &ov7630_init
,
268 .type
= V4L2_CTRL_TYPE_INTEGER
,
269 .name
= "global gain",
273 .default_value
= 0x14,
277 .id
= V4L2_CID_EXPOSURE
,
278 .type
= V4L2_CTRL_TYPE_INTEGER
,
283 .default_value
= 0x60,
287 .id
= V4L2_CID_WHITENESS
,
288 .type
= V4L2_CTRL_TYPE_INTEGER
,
289 .name
= "white balance background: red",
293 .default_value
= 0x20,
297 .id
= V4L2_CID_DO_WHITE_BALANCE
,
298 .type
= V4L2_CTRL_TYPE_INTEGER
,
299 .name
= "white balance background: blue",
303 .default_value
= 0x20,
307 .id
= V4L2_CID_RED_BALANCE
,
308 .type
= V4L2_CTRL_TYPE_INTEGER
,
309 .name
= "red balance",
313 .default_value
= 0x20,
317 .id
= V4L2_CID_BLUE_BALANCE
,
318 .type
= V4L2_CTRL_TYPE_INTEGER
,
319 .name
= "blue balance",
323 .default_value
= 0x20,
327 .id
= V4L2_CID_AUTOGAIN
,
328 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
329 .name
= "auto adjust",
333 .default_value
= 0x00,
337 .id
= V4L2_CID_VFLIP
,
338 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
339 .name
= "vertical flip",
343 .default_value
= 0x01,
347 .id
= SN9C102_V4L2_CID_GREEN_BALANCE
,
348 .type
= V4L2_CTRL_TYPE_INTEGER
,
349 .name
= "green balance",
353 .default_value
= 0x20,
357 .id
= SN9C102_V4L2_CID_BAND_FILTER
,
358 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
359 .name
= "band filter",
363 .default_value
= 0x00,
367 .id
= SN9C102_V4L2_CID_GAMMA
,
368 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
373 .default_value
= 0x00,
377 .get_ctrl
= &ov7630_get_ctrl
,
378 .set_ctrl
= &ov7630_set_ctrl
,
393 .set_crop
= &ov7630_set_crop
,
397 .pixelformat
= V4L2_PIX_FMT_SN9C10X
,
400 .set_pix_format
= &ov7630_set_pix_format
404 int sn9c102_probe_ov7630(struct sn9c102_device
* cam
)
406 int pid
, ver
, err
= 0;
408 switch (sn9c102_get_bridge(cam
)) {
411 err
= sn9c102_write_const_regs(cam
, {0x01, 0x01},
412 {0x00, 0x01}, {0x28, 0x17});
415 case BRIDGE_SN9C103
: /* do _not_ change anything! */
416 err
= sn9c102_write_const_regs(cam
, {0x09, 0x01},
417 {0x42, 0x01}, {0x28, 0x17},
419 pid
= sn9c102_i2c_try_read(cam
, &ov7630
, 0x0a);
420 if (err
|| pid
< 0) { /* try a different initialization */
421 err
= sn9c102_write_reg(cam
, 0x01, 0x01);
422 err
+= sn9c102_write_reg(cam
, 0x00, 0x01);
429 pid
= sn9c102_i2c_try_read(cam
, &ov7630
, 0x0a);
430 ver
= sn9c102_i2c_try_read(cam
, &ov7630
, 0x0b);
431 if (err
|| pid
< 0 || ver
< 0)
433 if (pid
!= 0x76 || ver
!= 0x31)
435 sn9c102_attach_sensor(cam
, &ov7630
);