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 switch (sn9c102_get_bridge(cam)) {
32 err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11},
33 {0x0a, 0x14}, {0x40, 0x01},
34 {0x20, 0x17}, {0x07, 0x18},
35 {0xa0, 0x19}, {0x02, 0x1c},
36 {0x03, 0x1d}, {0x0f, 0x1e},
37 {0x0c, 0x1f}, {0x00, 0x20},
38 {0x10, 0x21}, {0x20, 0x22},
39 {0x30, 0x23}, {0x40, 0x24},
40 {0x50, 0x25}, {0x60, 0x26},
41 {0x70, 0x27}, {0x80, 0x28},
42 {0x90, 0x29}, {0xa0, 0x2a},
43 {0xb0, 0x2b}, {0xc0, 0x2c},
44 {0xd0, 0x2d}, {0xe0, 0x2e},
45 {0xf0, 0x2f}, {0xff, 0x30});
49 err = sn9c102_write_const_regs(cam, {0x44, 0x01}, {0x40, 0x02},
50 {0x00, 0x03}, {0x1a, 0x04},
51 {0x50, 0x05}, {0x20, 0x06},
52 {0x10, 0x07}, {0x03, 0x10},
53 {0x08, 0x14}, {0xa2, 0x17},
54 {0x47, 0x18}, {0x00, 0x19},
55 {0x1d, 0x1a}, {0x10, 0x1b},
56 {0x02, 0x1c}, {0x03, 0x1d},
57 {0x0f, 0x1e}, {0x0c, 0x1f},
58 {0x00, 0x20}, {0x29, 0x21},
59 {0x40, 0x22}, {0x54, 0x23},
60 {0x66, 0x24}, {0x76, 0x25},
61 {0x85, 0x26}, {0x94, 0x27},
62 {0xa1, 0x28}, {0xae, 0x29},
63 {0xbb, 0x2a}, {0xc7, 0x2b},
64 {0xd3, 0x2c}, {0xde, 0x2d},
65 {0xea, 0x2e}, {0xf4, 0x2f},
66 {0xff, 0x30}, {0x00, 0x3F},
67 {0xC7, 0x40}, {0x01, 0x41},
68 {0x44, 0x42}, {0x00, 0x43},
69 {0x44, 0x44}, {0x00, 0x45},
70 {0x44, 0x46}, {0x00, 0x47},
71 {0xC7, 0x48}, {0x01, 0x49},
72 {0xC7, 0x4A}, {0x01, 0x4B},
73 {0xC7, 0x4C}, {0x01, 0x4D},
74 {0x44, 0x4E}, {0x00, 0x4F},
75 {0x44, 0x50}, {0x00, 0x51},
76 {0x44, 0x52}, {0x00, 0x53},
77 {0xC7, 0x54}, {0x01, 0x55},
78 {0xC7, 0x56}, {0x01, 0x57},
79 {0xC7, 0x58}, {0x01, 0x59},
80 {0x44, 0x5A}, {0x00, 0x5B},
81 {0x44, 0x5C}, {0x00, 0x5D},
82 {0x44, 0x5E}, {0x00, 0x5F},
83 {0xC7, 0x60}, {0x01, 0x61},
84 {0xC7, 0x62}, {0x01, 0x63},
85 {0xC7, 0x64}, {0x01, 0x65},
86 {0x44, 0x66}, {0x00, 0x67},
87 {0x44, 0x68}, {0x00, 0x69},
88 {0x44, 0x6A}, {0x00, 0x6B},
89 {0xC7, 0x6C}, {0x01, 0x6D},
90 {0xC7, 0x6E}, {0x01, 0x6F},
91 {0xC7, 0x70}, {0x01, 0x71},
92 {0x44, 0x72}, {0x00, 0x73},
93 {0x44, 0x74}, {0x00, 0x75},
94 {0x44, 0x76}, {0x00, 0x77},
95 {0xC7, 0x78}, {0x01, 0x79},
96 {0xC7, 0x7A}, {0x01, 0x7B},
97 {0xC7, 0x7C}, {0x01, 0x7D},
98 {0x44, 0x7E}, {0x00, 0x7F},
99 {0x14, 0x84}, {0x00, 0x85},
100 {0x27, 0x86}, {0x00, 0x87},
101 {0x07, 0x88}, {0x00, 0x89},
102 {0xEC, 0x8A}, {0x0f, 0x8B},
103 {0xD8, 0x8C}, {0x0f, 0x8D},
104 {0x3D, 0x8E}, {0x00, 0x8F},
105 {0x3D, 0x90}, {0x00, 0x91},
106 {0xCD, 0x92}, {0x0f, 0x93},
107 {0xf7, 0x94}, {0x0f, 0x95},
108 {0x0C, 0x96}, {0x00, 0x97},
109 {0x00, 0x98}, {0x66, 0x99},
110 {0x05, 0x9A}, {0x00, 0x9B},
111 {0x04, 0x9C}, {0x00, 0x9D},
112 {0x08, 0x9E}, {0x00, 0x9F},
113 {0x2D, 0xC0}, {0x2D, 0xC1},
114 {0x3A, 0xC2}, {0x05, 0xC3},
115 {0x04, 0xC4}, {0x3F, 0xC5},
116 {0x00, 0xC6}, {0x00, 0xC7},
117 {0x50, 0xC8}, {0x3C, 0xC9},
118 {0x28, 0xCA}, {0xD8, 0xCB},
119 {0x14, 0xCC}, {0xEC, 0xCD},
120 {0x32, 0xCE}, {0xDD, 0xCF},
121 {0x32, 0xD0}, {0xDD, 0xD1},
122 {0x6A, 0xD2}, {0x50, 0xD3},
123 {0x00, 0xD4}, {0x00, 0xD5},
130 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
132 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
134 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
136 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
138 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
140 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
142 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62,
149 static int mi0360_get_ctrl(struct sn9c102_device* cam,
150 struct v4l2_control* ctrl)
152 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
156 case V4L2_CID_EXPOSURE:
157 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09, 2,
160 ctrl->value = data[0];
163 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35, 2,
166 ctrl->value = data[1];
168 case V4L2_CID_RED_BALANCE:
169 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c, 2,
172 ctrl->value = data[1];
174 case V4L2_CID_BLUE_BALANCE:
175 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d, 2,
178 ctrl->value = data[1];
180 case SN9C102_V4L2_CID_GREEN_BALANCE:
181 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e, 2,
184 ctrl->value = data[1];
187 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
190 ctrl->value = data[1] & 0x20 ? 1 : 0;
193 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
196 ctrl->value = data[1] & 0x80 ? 1 : 0;
206 static int mi0360_set_ctrl(struct sn9c102_device* cam,
207 const struct v4l2_control* ctrl)
209 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
213 case V4L2_CID_EXPOSURE:
214 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
215 0x09, ctrl->value, 0x00,
219 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
220 0x35, 0x03, ctrl->value,
223 case V4L2_CID_RED_BALANCE:
224 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
225 0x2c, 0x03, ctrl->value,
228 case V4L2_CID_BLUE_BALANCE:
229 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
230 0x2d, 0x03, ctrl->value,
233 case SN9C102_V4L2_CID_GREEN_BALANCE:
234 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
235 0x2b, 0x03, ctrl->value,
237 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
238 0x2e, 0x03, ctrl->value,
242 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
243 0x20, ctrl->value ? 0x40:0x00,
244 ctrl->value ? 0x20:0x00,
248 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
249 0x20, ctrl->value ? 0x80:0x00,
250 ctrl->value ? 0x80:0x00,
257 return err ? -EIO : 0;
261 static int mi0360_set_crop(struct sn9c102_device* cam,
262 const struct v4l2_rect* rect)
264 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
266 u8 h_start = 0, v_start = (u8)(rect->top - s->cropcap.bounds.top) + 1;
268 switch (sn9c102_get_bridge(cam)) {
270 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0;
274 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 1;
280 err += sn9c102_write_reg(cam, h_start, 0x12);
281 err += sn9c102_write_reg(cam, v_start, 0x13);
287 static int mi0360_set_pix_format(struct sn9c102_device* cam,
288 const struct v4l2_pix_format* pix)
290 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
293 if (pix->pixelformat == V4L2_PIX_FMT_SBGGR8) {
294 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
295 0x0a, 0x00, 0x05, 0, 0);
296 err += sn9c102_write_reg(cam, 0x60, 0x19);
297 if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 ||
298 sn9c102_get_bridge(cam) == BRIDGE_SN9C120)
299 err += sn9c102_write_reg(cam, 0xa6, 0x17);
301 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
302 0x0a, 0x00, 0x02, 0, 0);
303 err += sn9c102_write_reg(cam, 0x20, 0x19);
304 if (sn9c102_get_bridge(cam) == BRIDGE_SN9C105 ||
305 sn9c102_get_bridge(cam) == BRIDGE_SN9C120)
306 err += sn9c102_write_reg(cam, 0xa2, 0x17);
313 static const struct sn9c102_sensor mi0360 = {
315 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
316 .supported_bridge = BRIDGE_SN9C103 | BRIDGE_SN9C105 | BRIDGE_SN9C120,
317 .frequency = SN9C102_I2C_100KHZ,
318 .interface = SN9C102_I2C_2WIRES,
319 .i2c_slave_id = 0x5d,
320 .init = &mi0360_init,
323 .id = V4L2_CID_EXPOSURE,
324 .type = V4L2_CTRL_TYPE_INTEGER,
329 .default_value = 0x05,
334 .type = V4L2_CTRL_TYPE_INTEGER,
335 .name = "global gain",
339 .default_value = 0x25,
343 .id = V4L2_CID_HFLIP,
344 .type = V4L2_CTRL_TYPE_BOOLEAN,
345 .name = "horizontal mirror",
353 .id = V4L2_CID_VFLIP,
354 .type = V4L2_CTRL_TYPE_BOOLEAN,
355 .name = "vertical mirror",
363 .id = V4L2_CID_BLUE_BALANCE,
364 .type = V4L2_CTRL_TYPE_INTEGER,
365 .name = "blue balance",
369 .default_value = 0x0f,
373 .id = V4L2_CID_RED_BALANCE,
374 .type = V4L2_CTRL_TYPE_INTEGER,
375 .name = "red balance",
379 .default_value = 0x32,
383 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
384 .type = V4L2_CTRL_TYPE_INTEGER,
385 .name = "green balance",
389 .default_value = 0x25,
393 .get_ctrl = &mi0360_get_ctrl,
394 .set_ctrl = &mi0360_set_ctrl,
409 .set_crop = &mi0360_set_crop,
413 .pixelformat = V4L2_PIX_FMT_SBGGR8,
416 .set_pix_format = &mi0360_set_pix_format
420 int sn9c102_probe_mi0360(struct sn9c102_device* cam)
425 switch (sn9c102_get_bridge(cam)) {
427 if (sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01},
433 if (sn9c102_write_const_regs(cam, {0x01, 0xf1}, {0x00, 0xf1},
434 {0x01, 0x01}, {0x00, 0x01},
442 if (sn9c102_i2c_try_raw_read(cam, &mi0360, mi0360.i2c_slave_id, 0x00,
446 if (data[0] != 0x82 || data[1] != 0x43)
449 sn9c102_attach_sensor(cam, &mi0360);