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"
25 static int mi0343_init(struct sn9c102_device
* cam
)
27 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
30 err
= sn9c102_write_const_regs(cam
, {0x00, 0x10}, {0x00, 0x11},
31 {0x0a, 0x14}, {0x40, 0x01},
32 {0x20, 0x17}, {0x07, 0x18},
35 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x0d,
37 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x0d,
39 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x03,
41 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x04,
43 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x05,
45 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x06,
47 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
, 0x62,
54 static int mi0343_get_ctrl(struct sn9c102_device
* cam
,
55 struct v4l2_control
* ctrl
)
57 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
61 case V4L2_CID_EXPOSURE
:
62 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x09,
65 ctrl
->value
= data
[2];
68 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x35,
73 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x20,
76 ctrl
->value
= data
[3] & 0x20 ? 1 : 0;
79 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x20,
82 ctrl
->value
= data
[3] & 0x80 ? 1 : 0;
84 case V4L2_CID_RED_BALANCE
:
85 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x2d,
89 case V4L2_CID_BLUE_BALANCE
:
90 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x2c,
94 case SN9C102_V4L2_CID_GREEN_BALANCE
:
95 if (sn9c102_i2c_try_raw_read(cam
, s
, s
->i2c_slave_id
, 0x2e,
105 case V4L2_CID_RED_BALANCE
:
106 case V4L2_CID_BLUE_BALANCE
:
107 case SN9C102_V4L2_CID_GREEN_BALANCE
:
108 ctrl
->value
= data
[3] | (data
[2] << 8);
109 if (ctrl
->value
>= 0x10 && ctrl
->value
<= 0x3f)
111 else if (ctrl
->value
>= 0x60 && ctrl
->value
<= 0x7f)
113 else if (ctrl
->value
>= 0xe0 && ctrl
->value
<= 0xff)
121 static int mi0343_set_ctrl(struct sn9c102_device
* cam
,
122 const struct v4l2_control
* ctrl
)
124 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
130 case V4L2_CID_RED_BALANCE
:
131 case V4L2_CID_BLUE_BALANCE
:
132 case SN9C102_V4L2_CID_GREEN_BALANCE
:
133 if (ctrl
->value
<= (0x3f-0x10))
134 reg
= 0x10 + ctrl
->value
;
135 else if (ctrl
->value
<= ((0x3f-0x10) + (0x7f-0x60)))
136 reg
= 0x60 + (ctrl
->value
- (0x3f-0x10));
138 reg
= 0xe0 + (ctrl
->value
- (0x3f-0x10) - (0x7f-0x60));
143 case V4L2_CID_EXPOSURE
:
144 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
145 0x09, ctrl
->value
, 0x00,
149 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
150 0x35, reg
>> 8, reg
& 0xff,
154 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
155 0x20, ctrl
->value
? 0x40:0x00,
156 ctrl
->value
? 0x20:0x00,
160 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
161 0x20, ctrl
->value
? 0x80:0x00,
162 ctrl
->value
? 0x80:0x00,
165 case V4L2_CID_RED_BALANCE
:
166 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
167 0x2d, reg
>> 8, reg
& 0xff,
170 case V4L2_CID_BLUE_BALANCE
:
171 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
172 0x2c, reg
>> 8, reg
& 0xff,
175 case SN9C102_V4L2_CID_GREEN_BALANCE
:
176 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
177 0x2b, reg
>> 8, reg
& 0xff,
179 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
180 0x2e, reg
>> 8, reg
& 0xff,
187 return err
? -EIO
: 0;
191 static int mi0343_set_crop(struct sn9c102_device
* cam
,
192 const struct v4l2_rect
* rect
)
194 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
196 u8 h_start
= (u8
)(rect
->left
- s
->cropcap
.bounds
.left
) + 0,
197 v_start
= (u8
)(rect
->top
- s
->cropcap
.bounds
.top
) + 2;
199 err
+= sn9c102_write_reg(cam
, h_start
, 0x12);
200 err
+= sn9c102_write_reg(cam
, v_start
, 0x13);
206 static int mi0343_set_pix_format(struct sn9c102_device
* cam
,
207 const struct v4l2_pix_format
* pix
)
209 struct sn9c102_sensor
* s
= sn9c102_get_sensor(cam
);
212 if (pix
->pixelformat
== V4L2_PIX_FMT_SN9C10X
) {
213 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
214 0x0a, 0x00, 0x03, 0, 0);
215 err
+= sn9c102_write_reg(cam
, 0x20, 0x19);
217 err
+= sn9c102_i2c_try_raw_write(cam
, s
, 4, s
->i2c_slave_id
,
218 0x0a, 0x00, 0x05, 0, 0);
219 err
+= sn9c102_write_reg(cam
, 0xa0, 0x19);
226 static struct sn9c102_sensor mi0343
= {
228 .maintainer
= "Luca Risolia <luca.risolia@studio.unibo.it>",
229 .supported_bridge
= BRIDGE_SN9C101
| BRIDGE_SN9C102
,
230 .frequency
= SN9C102_I2C_100KHZ
,
231 .interface
= SN9C102_I2C_2WIRES
,
232 .i2c_slave_id
= 0x5d,
233 .init
= &mi0343_init
,
236 .id
= V4L2_CID_EXPOSURE
,
237 .type
= V4L2_CTRL_TYPE_INTEGER
,
242 .default_value
= 0x06,
247 .type
= V4L2_CTRL_TYPE_INTEGER
,
248 .name
= "global gain",
250 .maximum
= (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),/*0x6d*/
252 .default_value
= 0x00,
256 .id
= V4L2_CID_HFLIP
,
257 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
258 .name
= "horizontal mirror",
266 .id
= V4L2_CID_VFLIP
,
267 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
268 .name
= "vertical mirror",
276 .id
= V4L2_CID_RED_BALANCE
,
277 .type
= V4L2_CTRL_TYPE_INTEGER
,
278 .name
= "red balance",
280 .maximum
= (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
282 .default_value
= 0x00,
286 .id
= V4L2_CID_BLUE_BALANCE
,
287 .type
= V4L2_CTRL_TYPE_INTEGER
,
288 .name
= "blue balance",
290 .maximum
= (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
292 .default_value
= 0x00,
296 .id
= SN9C102_V4L2_CID_GREEN_BALANCE
,
297 .type
= V4L2_CTRL_TYPE_INTEGER
,
298 .name
= "green balance",
300 .maximum
= ((0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0)),
302 .default_value
= 0x00,
306 .get_ctrl
= &mi0343_get_ctrl
,
307 .set_ctrl
= &mi0343_set_ctrl
,
322 .set_crop
= &mi0343_set_crop
,
326 .pixelformat
= V4L2_PIX_FMT_SBGGR8
,
329 .set_pix_format
= &mi0343_set_pix_format
333 int sn9c102_probe_mi0343(struct sn9c102_device
* cam
)
338 err
= sn9c102_write_const_regs(cam
, {0x01, 0x01}, {0x00, 0x01},
344 if (sn9c102_i2c_try_raw_read(cam
, &mi0343
, mi0343
.i2c_slave_id
, 0x00,
348 if (data
[4] != 0x32 || data
[3] != 0xe3)
351 sn9c102_attach_sensor(cam
, &mi0343
);