1 /***************************************************************************
2 * Plug-in for HV7131R image sensor connected to the SN9C1xx PC Camera *
5 * Copyright (C) 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 hv7131r_init(struct sn9c102_device
* cam
)
29 switch (sn9c102_get_bridge(cam
)) {
31 err
= sn9c102_write_const_regs(cam
, {0x00, 0x03}, {0x1a, 0x04},
32 {0x20, 0x05}, {0x20, 0x06},
33 {0x03, 0x10}, {0x00, 0x14},
34 {0x60, 0x17}, {0x0a, 0x18},
35 {0xf0, 0x19}, {0x1d, 0x1a},
36 {0x10, 0x1b}, {0x02, 0x1c},
37 {0x03, 0x1d}, {0x0f, 0x1e},
38 {0x0c, 0x1f}, {0x00, 0x20},
39 {0x10, 0x21}, {0x20, 0x22},
40 {0x30, 0x23}, {0x40, 0x24},
41 {0x50, 0x25}, {0x60, 0x26},
42 {0x70, 0x27}, {0x80, 0x28},
43 {0x90, 0x29}, {0xa0, 0x2a},
44 {0xb0, 0x2b}, {0xc0, 0x2c},
45 {0xd0, 0x2d}, {0xe0, 0x2e},
46 {0xf0, 0x2f}, {0xff, 0x30});
50 err
= sn9c102_write_const_regs(cam
, {0x44, 0x01}, {0x40, 0x02},
51 {0x00, 0x03}, {0x1a, 0x04},
52 {0x44, 0x05}, {0x3e, 0x06},
53 {0x1a, 0x07}, {0x03, 0x10},
54 {0x08, 0x14}, {0xa3, 0x17},
55 {0x4b, 0x18}, {0x00, 0x19},
56 {0x1d, 0x1a}, {0x10, 0x1b},
57 {0x02, 0x1c}, {0x03, 0x1d},
58 {0x0f, 0x1e}, {0x0c, 0x1f},
59 {0x00, 0x20}, {0x29, 0x21},
60 {0x40, 0x22}, {0x54, 0x23},
61 {0x66, 0x24}, {0x76, 0x25},
62 {0x85, 0x26}, {0x94, 0x27},
63 {0xa1, 0x28}, {0xae, 0x29},
64 {0xbb, 0x2a}, {0xc7, 0x2b},
65 {0xd3, 0x2c}, {0xde, 0x2d},
66 {0xea, 0x2e}, {0xf4, 0x2f},
67 {0xff, 0x30}, {0x00, 0x3F},
68 {0xC7, 0x40}, {0x01, 0x41},
69 {0x44, 0x42}, {0x00, 0x43},
70 {0x44, 0x44}, {0x00, 0x45},
71 {0x44, 0x46}, {0x00, 0x47},
72 {0xC7, 0x48}, {0x01, 0x49},
73 {0xC7, 0x4A}, {0x01, 0x4B},
74 {0xC7, 0x4C}, {0x01, 0x4D},
75 {0x44, 0x4E}, {0x00, 0x4F},
76 {0x44, 0x50}, {0x00, 0x51},
77 {0x44, 0x52}, {0x00, 0x53},
78 {0xC7, 0x54}, {0x01, 0x55},
79 {0xC7, 0x56}, {0x01, 0x57},
80 {0xC7, 0x58}, {0x01, 0x59},
81 {0x44, 0x5A}, {0x00, 0x5B},
82 {0x44, 0x5C}, {0x00, 0x5D},
83 {0x44, 0x5E}, {0x00, 0x5F},
84 {0xC7, 0x60}, {0x01, 0x61},
85 {0xC7, 0x62}, {0x01, 0x63},
86 {0xC7, 0x64}, {0x01, 0x65},
87 {0x44, 0x66}, {0x00, 0x67},
88 {0x44, 0x68}, {0x00, 0x69},
89 {0x44, 0x6A}, {0x00, 0x6B},
90 {0xC7, 0x6C}, {0x01, 0x6D},
91 {0xC7, 0x6E}, {0x01, 0x6F},
92 {0xC7, 0x70}, {0x01, 0x71},
93 {0x44, 0x72}, {0x00, 0x73},
94 {0x44, 0x74}, {0x00, 0x75},
95 {0x44, 0x76}, {0x00, 0x77},
96 {0xC7, 0x78}, {0x01, 0x79},
97 {0xC7, 0x7A}, {0x01, 0x7B},
98 {0xC7, 0x7C}, {0x01, 0x7D},
99 {0x44, 0x7E}, {0x00, 0x7F},
100 {0x14, 0x84}, {0x00, 0x85},
101 {0x27, 0x86}, {0x00, 0x87},
102 {0x07, 0x88}, {0x00, 0x89},
103 {0xEC, 0x8A}, {0x0f, 0x8B},
104 {0xD8, 0x8C}, {0x0f, 0x8D},
105 {0x3D, 0x8E}, {0x00, 0x8F},
106 {0x3D, 0x90}, {0x00, 0x91},
107 {0xCD, 0x92}, {0x0f, 0x93},
108 {0xf7, 0x94}, {0x0f, 0x95},
109 {0x0C, 0x96}, {0x00, 0x97},
110 {0x00, 0x98}, {0x66, 0x99},
111 {0x05, 0x9A}, {0x00, 0x9B},
112 {0x04, 0x9C}, {0x00, 0x9D},
113 {0x08, 0x9E}, {0x00, 0x9F},
114 {0x2D, 0xC0}, {0x2D, 0xC1},
115 {0x3A, 0xC2}, {0x05, 0xC3},
116 {0x04, 0xC4}, {0x3F, 0xC5},
117 {0x00, 0xC6}, {0x00, 0xC7},
118 {0x50, 0xC8}, {0x3C, 0xC9},
119 {0x28, 0xCA}, {0xD8, 0xCB},
120 {0x14, 0xCC}, {0xEC, 0xCD},
121 {0x32, 0xCE}, {0xDD, 0xCF},
122 {0x32, 0xD0}, {0xDD, 0xD1},
123 {0x6A, 0xD2}, {0x50, 0xD3},
124 {0x00, 0xD4}, {0x00, 0xD5},
131 err
+= sn9c102_i2c_write(cam
, 0x20, 0x00);
132 err
+= sn9c102_i2c_write(cam
, 0x21, 0xd6);
133 err
+= sn9c102_i2c_write(cam
, 0x25, 0x06);
139 static int hv7131r_get_ctrl(struct sn9c102_device
* cam
,
140 struct v4l2_control
* ctrl
)
144 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x30)) < 0)
147 case V4L2_CID_RED_BALANCE
:
148 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x31)) < 0)
150 ctrl
->value
= ctrl
->value
& 0x3f;
152 case V4L2_CID_BLUE_BALANCE
:
153 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x33)) < 0)
155 ctrl
->value
= ctrl
->value
& 0x3f;
157 case SN9C102_V4L2_CID_GREEN_BALANCE
:
158 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x32)) < 0)
160 ctrl
->value
= ctrl
->value
& 0x3f;
162 case V4L2_CID_BLACK_LEVEL
:
163 if ((ctrl
->value
= sn9c102_i2c_read(cam
, 0x01)) < 0)
165 ctrl
->value
= (ctrl
->value
& 0x08) ? 1 : 0;
173 static int hv7131r_set_ctrl(struct sn9c102_device
* cam
,
174 const struct v4l2_control
* ctrl
)
180 err
+= sn9c102_i2c_write(cam
, 0x30, ctrl
->value
);
182 case V4L2_CID_RED_BALANCE
:
183 err
+= sn9c102_i2c_write(cam
, 0x31, ctrl
->value
);
185 case V4L2_CID_BLUE_BALANCE
:
186 err
+= sn9c102_i2c_write(cam
, 0x33, ctrl
->value
);
188 case SN9C102_V4L2_CID_GREEN_BALANCE
:
189 err
+= sn9c102_i2c_write(cam
, 0x32, ctrl
->value
);
191 case V4L2_CID_BLACK_LEVEL
:
193 int r
= sn9c102_i2c_read(cam
, 0x01);
196 err
+= sn9c102_i2c_write(cam
, 0x01,
197 (ctrl
->value
<<3) | (r
&0xf7));
204 return err
? -EIO
: 0;
208 static int hv7131r_set_crop(struct sn9c102_device
* cam
,
209 const struct v4l2_rect
* rect
)
211 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
213 u8 h_start
= (u8
)(rect
->left
- s
->cropcap
.bounds
.left
) + 1,
214 v_start
= (u8
)(rect
->top
- s
->cropcap
.bounds
.top
) + 1;
216 err
+= sn9c102_write_reg(cam
, h_start
, 0x12);
217 err
+= sn9c102_write_reg(cam
, v_start
, 0x13);
223 static int hv7131r_set_pix_format(struct sn9c102_device
* cam
,
224 const struct v4l2_pix_format
* pix
)
228 switch (sn9c102_get_bridge(cam
)) {
230 if (pix
->pixelformat
== V4L2_PIX_FMT_SBGGR8
) {
231 err
+= sn9c102_write_reg(cam
, 0xa0, 0x19);
232 err
+= sn9c102_i2c_write(cam
, 0x01, 0x04);
234 err
+= sn9c102_write_reg(cam
, 0x30, 0x19);
235 err
+= sn9c102_i2c_write(cam
, 0x01, 0x04);
240 if (pix
->pixelformat
== V4L2_PIX_FMT_SBGGR8
) {
241 err
+= sn9c102_write_reg(cam
, 0xa5, 0x17);
242 err
+= sn9c102_i2c_write(cam
, 0x01, 0x24);
244 err
+= sn9c102_write_reg(cam
, 0xa3, 0x17);
245 err
+= sn9c102_i2c_write(cam
, 0x01, 0x04);
256 static const struct sn9c102_sensor hv7131r
= {
258 .maintainer
= "Luca Risolia <luca.risolia@studio.unibo.it>",
259 .supported_bridge
= BRIDGE_SN9C103
| BRIDGE_SN9C105
| BRIDGE_SN9C120
,
260 .sysfs_ops
= SN9C102_I2C_READ
| SN9C102_I2C_WRITE
,
261 .frequency
= SN9C102_I2C_100KHZ
,
262 .interface
= SN9C102_I2C_2WIRES
,
263 .i2c_slave_id
= 0x11,
264 .init
= &hv7131r_init
,
268 .type
= V4L2_CTRL_TYPE_INTEGER
,
269 .name
= "global gain",
273 .default_value
= 0x40,
277 .id
= V4L2_CID_RED_BALANCE
,
278 .type
= V4L2_CTRL_TYPE_INTEGER
,
279 .name
= "red balance",
283 .default_value
= 0x08,
287 .id
= V4L2_CID_BLUE_BALANCE
,
288 .type
= V4L2_CTRL_TYPE_INTEGER
,
289 .name
= "blue balance",
293 .default_value
= 0x1a,
297 .id
= SN9C102_V4L2_CID_GREEN_BALANCE
,
298 .type
= V4L2_CTRL_TYPE_INTEGER
,
299 .name
= "green balance",
303 .default_value
= 0x2f,
307 .id
= V4L2_CID_BLACK_LEVEL
,
308 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
309 .name
= "auto black level compensation",
313 .default_value
= 0x00,
317 .get_ctrl
= &hv7131r_get_ctrl
,
318 .set_ctrl
= &hv7131r_set_ctrl
,
333 .set_crop
= &hv7131r_set_crop
,
337 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
340 .set_pix_format
= &hv7131r_set_pix_format
344 int sn9c102_probe_hv7131r(struct sn9c102_device
* cam
)
348 err
= sn9c102_write_const_regs(cam
, {0x09, 0x01}, {0x44, 0x02},
349 {0x34, 0x01}, {0x20, 0x17},
350 {0x34, 0x01}, {0x46, 0x01});
352 devid
= sn9c102_i2c_try_read(cam
, &hv7131r
, 0x00);
353 if (err
|| devid
< 0)
359 sn9c102_attach_sensor(cam
, &hv7131r
);