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"
23 #include "sn9c102_devtable.h"
26 static int mi0360_init(struct sn9c102_device* cam)
28 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
31 switch (sn9c102_get_bridge(cam)) {
33 err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11},
34 {0x0a, 0x14}, {0x40, 0x01},
35 {0x20, 0x17}, {0x07, 0x18},
36 {0xa0, 0x19}, {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 {0x50, 0x05}, {0x20, 0x06},
53 {0x10, 0x07}, {0x03, 0x10},
54 {0x08, 0x14}, {0xa2, 0x17},
55 {0x47, 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_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
133 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
135 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
137 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
139 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
141 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
143 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62,
150 static int mi0360_get_ctrl(struct sn9c102_device* cam,
151 struct v4l2_control* ctrl)
153 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
157 case V4L2_CID_EXPOSURE:
158 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09, 2,
161 ctrl->value = data[0];
164 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35, 2,
167 ctrl->value = data[1];
169 case V4L2_CID_RED_BALANCE:
170 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c, 2,
173 ctrl->value = data[1];
175 case V4L2_CID_BLUE_BALANCE:
176 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d, 2,
179 ctrl->value = data[1];
181 case SN9C102_V4L2_CID_GREEN_BALANCE:
182 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e, 2,
185 ctrl->value = data[1];
188 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
191 ctrl->value = data[1] & 0x20 ? 1 : 0;
194 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
197 ctrl->value = data[1] & 0x80 ? 1 : 0;
207 static int mi0360_set_ctrl(struct sn9c102_device* cam,
208 const struct v4l2_control* ctrl)
210 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
214 case V4L2_CID_EXPOSURE:
215 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
216 0x09, ctrl->value, 0x00,
220 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
221 0x35, 0x03, ctrl->value,
224 case V4L2_CID_RED_BALANCE:
225 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
226 0x2c, 0x03, ctrl->value,
229 case V4L2_CID_BLUE_BALANCE:
230 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
231 0x2d, 0x03, ctrl->value,
234 case SN9C102_V4L2_CID_GREEN_BALANCE:
235 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
236 0x2b, 0x03, ctrl->value,
238 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
239 0x2e, 0x03, ctrl->value,
243 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
244 0x20, ctrl->value ? 0x40:0x00,
245 ctrl->value ? 0x20:0x00,
249 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
250 0x20, ctrl->value ? 0x80:0x00,
251 ctrl->value ? 0x80:0x00,
258 return err ? -EIO : 0;
262 static int mi0360_set_crop(struct sn9c102_device* cam,
263 const struct v4l2_rect* rect)
265 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
267 u8 h_start = 0, v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
269 switch (sn9c102_get_bridge(cam)) {
271 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0;
275 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1;
281 err += sn9c102_write_reg(cam, h_start, 0x12);
282 err += sn9c102_write_reg(cam, v_start, 0x13);
288 static int mi0360_set_pix_format(struct sn9c102_device* cam,
289 const struct v4l2_pix_format* pix)
291 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
294 if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) {
295 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
296 0x0a, 0x00, 0x05, 0, 0);
297 err += sn9c102_write_reg(cam, 0x60, 0x19);
298 if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 ||
299 sn9c102_get_bridge(cam) == BRIDGE_SN9C120)
300 err += sn9c102_write_reg(cam, 0xa6, 0x17);
302 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
303 0x0a, 0x00, 0x02, 0, 0);
304 err += sn9c102_write_reg(cam, 0x20, 0x19);
305 if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 ||
306 sn9c102_get_bridge(cam) == BRIDGE_SN9C120)
307 err += sn9c102_write_reg(cam, 0xa2, 0x17);
314 static const struct sn9c102_sensor mi0360 = {
316 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
317 .supported_bridge = BRIDGE_SN9C103 | BRIDGE_SN9C105 | BRIDGE_SN9C120,
318 .frequency = SN9C102_I2C_100KHZ,
319 .interface = SN9C102_I2C_2WIRES,
320 .i2c_slave_id = 0x5d,
321 .init = &mi0360_init,
324 .id = V4L2_CID_EXPOSURE,
325 .type = V4L2_CTRL_TYPE_INTEGER,
330 .default_value = 0x05,
335 .type = V4L2_CTRL_TYPE_INTEGER,
336 .name = "global gain",
340 .default_value = 0x25,
344 .id = V4L2_CID_HFLIP,
345 .type = V4L2_CTRL_TYPE_BOOLEAN,
346 .name = "horizontal mirror",
354 .id = V4L2_CID_VFLIP,
355 .type = V4L2_CTRL_TYPE_BOOLEAN,
356 .name = "vertical mirror",
364 .id = V4L2_CID_BLUE_BALANCE,
365 .type = V4L2_CTRL_TYPE_INTEGER,
366 .name = "blue balance",
370 .default_value = 0x0f,
374 .id = V4L2_CID_RED_BALANCE,
375 .type = V4L2_CTRL_TYPE_INTEGER,
376 .name = "red balance",
380 .default_value = 0x32,
384 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
385 .type = V4L2_CTRL_TYPE_INTEGER,
386 .name = "green balance",
390 .default_value = 0x25,
394 .get_ctrl = &mi0360_get_ctrl,
395 .set_ctrl = &mi0360_set_ctrl,
410 .set_crop = &mi0360_set_crop,
414 .pixelformat = V4L2_PIX_FMT_SBGGR8,
417 .set_pix_format = &mi0360_set_pix_format
421 int sn9c102_probe_mi0360(struct sn9c102_device* cam)
426 switch (sn9c102_get_bridge(cam)) {
428 if (sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01},
434 if (sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1},
435 {0x01, 0x01}, {0x00, 0x01},
443 if (sn9c102_i2c_try_raw_read(cam, &mi0360, mi0360.i2c_slave_id, 0x00,
447 if (data[0] != 0x82 || data[1] != 0x43)
450 sn9c102_attach_sensor(cam, &mi0360);