1 /***************************************************************************
2 * Plug-in for MI-0360 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 mi0360_init(struct sn9c102_device* cam)
27 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
30 err += sn9c102_write_reg(cam, 0x00, 0x10);
31 err += sn9c102_write_reg(cam, 0x00, 0x11);
32 err += sn9c102_write_reg(cam, 0x0a, 0x14);
33 err += sn9c102_write_reg(cam, 0x40, 0x01);
34 err += sn9c102_write_reg(cam, 0x20, 0x17);
35 err += sn9c102_write_reg(cam, 0x07, 0x18);
36 err += sn9c102_write_reg(cam, 0xa0, 0x19);
37 err += sn9c102_write_reg(cam, 0x02, 0x1c);
38 err += sn9c102_write_reg(cam, 0x03, 0x1d);
39 err += sn9c102_write_reg(cam, 0x0f, 0x1e);
40 err += sn9c102_write_reg(cam, 0x0c, 0x1f);
41 err += sn9c102_write_reg(cam, 0x00, 0x20);
42 err += sn9c102_write_reg(cam, 0x10, 0x21);
43 err += sn9c102_write_reg(cam, 0x20, 0x22);
44 err += sn9c102_write_reg(cam, 0x30, 0x23);
45 err += sn9c102_write_reg(cam, 0x40, 0x24);
46 err += sn9c102_write_reg(cam, 0x50, 0x25);
47 err += sn9c102_write_reg(cam, 0x60, 0x26);
48 err += sn9c102_write_reg(cam, 0x70, 0x27);
49 err += sn9c102_write_reg(cam, 0x80, 0x28);
50 err += sn9c102_write_reg(cam, 0x90, 0x29);
51 err += sn9c102_write_reg(cam, 0xa0, 0x2a);
52 err += sn9c102_write_reg(cam, 0xb0, 0x2b);
53 err += sn9c102_write_reg(cam, 0xc0, 0x2c);
54 err += sn9c102_write_reg(cam, 0xd0, 0x2d);
55 err += sn9c102_write_reg(cam, 0xe0, 0x2e);
56 err += sn9c102_write_reg(cam, 0xf0, 0x2f);
57 err += sn9c102_write_reg(cam, 0xff, 0x30);
59 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
61 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
63 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
65 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
67 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
69 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
71 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62,
78 static int mi0360_get_ctrl(struct sn9c102_device* cam,
79 struct v4l2_control* ctrl)
81 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
85 case V4L2_CID_EXPOSURE:
86 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09,
89 ctrl->value = data[2];
92 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35,
95 ctrl->value = data[3];
97 case V4L2_CID_RED_BALANCE:
98 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c,
101 ctrl->value = data[3];
103 case V4L2_CID_BLUE_BALANCE:
104 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d,
107 ctrl->value = data[3];
109 case SN9C102_V4L2_CID_GREEN_BALANCE:
110 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e,
113 ctrl->value = data[3];
116 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20,
119 ctrl->value = data[3] & 0x20 ? 1 : 0;
122 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20,
125 ctrl->value = data[3] & 0x80 ? 1 : 0;
135 static int mi0360_set_ctrl(struct sn9c102_device* cam,
136 const struct v4l2_control* ctrl)
138 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
142 case V4L2_CID_EXPOSURE:
143 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
144 0x09, ctrl->value, 0x00,
148 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
149 0x35, 0x03, ctrl->value,
152 case V4L2_CID_RED_BALANCE:
153 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
154 0x2c, 0x03, ctrl->value,
157 case V4L2_CID_BLUE_BALANCE:
158 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
159 0x2d, 0x03, ctrl->value,
162 case SN9C102_V4L2_CID_GREEN_BALANCE:
163 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
164 0x2b, 0x03, ctrl->value,
166 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
167 0x2e, 0x03, ctrl->value,
171 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
172 0x20, ctrl->value ? 0x40:0x00,
173 ctrl->value ? 0x20:0x00,
177 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
178 0x20, ctrl->value ? 0x80:0x00,
179 ctrl->value ? 0x80:0x00,
186 return err ? -EIO : 0;
190 static int mi0360_set_crop(struct sn9c102_device* cam,
191 const struct v4l2_rect* rect)
193 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
195 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0,
196 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
198 err += sn9c102_write_reg(cam, h_start, 0x12);
199 err += sn9c102_write_reg(cam, v_start, 0x13);
205 static int mi0360_set_pix_format(struct sn9c102_device* cam,
206 const struct v4l2_pix_format* pix)
208 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
211 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) {
212 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
213 0x0a, 0x00, 0x02, 0, 0);
214 err += sn9c102_write_reg(cam, 0x20, 0x19);
216 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
217 0x0a, 0x00, 0x05, 0, 0);
218 err += sn9c102_write_reg(cam, 0x60, 0x19);
225 static struct sn9c102_sensor mi0360 = {
227 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
228 .supported_bridge = BRIDGE_SN9C103,
229 .frequency = SN9C102_I2C_100KHZ,
230 .interface = SN9C102_I2C_2WIRES,
231 .i2c_slave_id = 0x5d,
232 .init = &mi0360_init,
235 .id = V4L2_CID_EXPOSURE,
236 .type = V4L2_CTRL_TYPE_INTEGER,
241 .default_value = 0x05,
246 .type = V4L2_CTRL_TYPE_INTEGER,
247 .name = "global gain",
251 .default_value = 0x25,
255 .id = V4L2_CID_HFLIP,
256 .type = V4L2_CTRL_TYPE_BOOLEAN,
257 .name = "horizontal mirror",
265 .id = V4L2_CID_VFLIP,
266 .type = V4L2_CTRL_TYPE_BOOLEAN,
267 .name = "vertical mirror",
275 .id = V4L2_CID_BLUE_BALANCE,
276 .type = V4L2_CTRL_TYPE_INTEGER,
277 .name = "blue balance",
281 .default_value = 0x0f,
285 .id = V4L2_CID_RED_BALANCE,
286 .type = V4L2_CTRL_TYPE_INTEGER,
287 .name = "red balance",
291 .default_value = 0x32,
295 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
296 .type = V4L2_CTRL_TYPE_INTEGER,
297 .name = "green balance",
301 .default_value = 0x25,
305 .get_ctrl = &mi0360_get_ctrl,
306 .set_ctrl = &mi0360_set_ctrl,
321 .set_crop = &mi0360_set_crop,
325 .pixelformat = V4L2_PIX_FMT_SBGGR8,
328 .set_pix_format = &mi0360_set_pix_format
332 int sn9c102_probe_mi0360(struct sn9c102_device* cam)
337 err += sn9c102_write_reg(cam, 0x01, 0x01);
338 err += sn9c102_write_reg(cam, 0x00, 0x01);
339 err += sn9c102_write_reg(cam, 0x28, 0x17);
343 if (sn9c102_i2c_try_raw_read(cam, &mi0360, mi0360.i2c_slave_id, 0x00,
347 if (data[2] != 0x82 || data[3] != 0x43)
350 sn9c102_attach_sensor(cam, &mi0360);