1 /***************************************************************************
2 * Plug-in for MI-0343 image sensor connected to the SN9C1xx PC Camera *
5 * Copyright (C) 2004-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"
23 #include "sn9c102_devtable.h"
26 static int mi0343_init(struct sn9c102_device
* cam
)
28 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
31 err
= sn9c102_write_const_regs(cam
, {0x00, 0x10}, {0x00, 0x11},
32 {0x0a, 0x14}, {0x40, 0x01},
33 {0x20, 0x17}, {0x07, 0x18},
36 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x0d,
38 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x0d,
40 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x03,
42 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x04,
44 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x05,
46 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x06,
48 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x62,
55 static int mi0343_get_ctrl(struct sn9c102_device
* cam
,
56 struct v4l2_control
* ctrl
)
58 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
62 case V4L2_CID_EXPOSURE
:
63 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x09, 2,
66 ctrl
->value
= data
[0];
69 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x35, 2,
74 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x20, 2,
77 ctrl
->value
= data
[1] & 0x20 ? 1 : 0;
80 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x20, 2,
83 ctrl
->value
= data
[1] & 0x80 ? 1 : 0;
85 case V4L2_CID_RED_BALANCE
:
86 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x2d, 2,
90 case V4L2_CID_BLUE_BALANCE
:
91 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x2c, 2,
95 case SN9C102_V4L2_CID_GREEN_BALANCE
:
96 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x2e, 2,
106 case V4L2_CID_RED_BALANCE
:
107 case V4L2_CID_BLUE_BALANCE
:
108 case SN9C102_V4L2_CID_GREEN_BALANCE
:
109 ctrl
->value
= data
[1] | (data
[0] << 8);
110 if (ctrl
->value
>= 0x10 && ctrl
->value
<= 0x3f)
112 else if (ctrl
->value
>= 0x60 && ctrl
->value
<= 0x7f)
114 else if (ctrl
->value
>= 0xe0 && ctrl
->value
<= 0xff)
122 static int mi0343_set_ctrl(struct sn9c102_device
* cam
,
123 const struct v4l2_control
* ctrl
)
125 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
131 case V4L2_CID_RED_BALANCE
:
132 case V4L2_CID_BLUE_BALANCE
:
133 case SN9C102_V4L2_CID_GREEN_BALANCE
:
134 if (ctrl
->value
<= (0x3f-0x10))
135 reg
= 0x10 + ctrl
->value
;
136 else if (ctrl
->value
<= ((0x3f-0x10) + (0x7f-0x60)))
137 reg
= 0x60 + (ctrl
->value
- (0x3f-0x10));
139 reg
= 0xe0 + (ctrl
->value
- (0x3f-0x10) - (0x7f-0x60));
144 case V4L2_CID_EXPOSURE
:
145 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
146 0x09, ctrl
->value
, 0x00,
150 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
151 0x35, reg
>> 8, reg
& 0xff,
155 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
156 0x20, ctrl
->value
? 0x40:0x00,
157 ctrl
->value
? 0x20:0x00,
161 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
162 0x20, ctrl
->value
? 0x80:0x00,
163 ctrl
->value
? 0x80:0x00,
166 case V4L2_CID_RED_BALANCE
:
167 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
168 0x2d, reg
>> 8, reg
& 0xff,
171 case V4L2_CID_BLUE_BALANCE
:
172 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
173 0x2c, reg
>> 8, reg
& 0xff,
176 case SN9C102_V4L2_CID_GREEN_BALANCE
:
177 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
178 0x2b, reg
>> 8, reg
& 0xff,
180 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
181 0x2e, reg
>> 8, reg
& 0xff,
188 return err
? -EIO
: 0;
192 static int mi0343_set_crop(struct sn9c102_device
* cam
,
193 const struct v4l2_rect
* rect
)
195 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
197 u8 h_start
= (u8
)(rect
->left
- s
->cropcap
.bounds
.left
) + 0,
198 v_start
= (u8
)(rect
->top
- s
->cropcap
.bounds
.top
) + 2;
200 err
+= sn9c102_write_reg(cam
, h_start
, 0x12);
201 err
+= sn9c102_write_reg(cam
, v_start
, 0x13);
207 static int mi0343_set_pix_format(struct sn9c102_device
* cam
,
208 const struct v4l2_pix_format
* pix
)
210 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
213 if (pix
->pixelformat
== V4L2_PIX_FMT_SN9C10X
) {
214 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
215 0x0a, 0x00, 0x03, 0, 0);
216 err
+= sn9c102_write_reg(cam
, 0x20, 0x19);
218 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
219 0x0a, 0x00, 0x05, 0, 0);
220 err
+= sn9c102_write_reg(cam
, 0xa0, 0x19);
227 static const struct sn9c102_sensor mi0343
= {
229 .maintainer
= "Luca Risolia <luca.risolia@studio.unibo.it>",
230 .supported_bridge
= BRIDGE_SN9C101
| BRIDGE_SN9C102
,
231 .frequency
= SN9C102_I2C_100KHZ
,
232 .interface
= SN9C102_I2C_2WIRES
,
233 .i2c_slave_id
= 0x5d,
234 .init
= &mi0343_init
,
237 .id
= V4L2_CID_EXPOSURE
,
238 .type
= V4L2_CTRL_TYPE_INTEGER
,
243 .default_value
= 0x06,
248 .type
= V4L2_CTRL_TYPE_INTEGER
,
249 .name
= "global gain",
251 .maximum
= (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),/*0x6d*/
253 .default_value
= 0x00,
257 .id
= V4L2_CID_HFLIP
,
258 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
259 .name
= "horizontal mirror",
267 .id
= V4L2_CID_VFLIP
,
268 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
269 .name
= "vertical mirror",
277 .id
= V4L2_CID_RED_BALANCE
,
278 .type
= V4L2_CTRL_TYPE_INTEGER
,
279 .name
= "red balance",
281 .maximum
= (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
283 .default_value
= 0x00,
287 .id
= V4L2_CID_BLUE_BALANCE
,
288 .type
= V4L2_CTRL_TYPE_INTEGER
,
289 .name
= "blue balance",
291 .maximum
= (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
293 .default_value
= 0x00,
297 .id
= SN9C102_V4L2_CID_GREEN_BALANCE
,
298 .type
= V4L2_CTRL_TYPE_INTEGER
,
299 .name
= "green balance",
301 .maximum
= ((0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0)),
303 .default_value
= 0x00,
307 .get_ctrl
= &mi0343_get_ctrl
,
308 .set_ctrl
= &mi0343_set_ctrl
,
323 .set_crop
= &mi0343_set_crop
,
327 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
330 .set_pix_format
= &mi0343_set_pix_format
334 int sn9c102_probe_mi0343(struct sn9c102_device
* cam
)
338 if (sn9c102_write_const_regs(cam
, {0x01, 0x01}, {0x00, 0x01},
342 if (sn9c102_i2c_try_raw_read(cam
, &mi0343
, mi0343
.i2c_slave_id
, 0x00,
346 if (data
[1] != 0x42 || data
[0] != 0xe3)
349 sn9c102_attach_sensor(cam
, &mi0343
);