blob: 00b134ca0a3d56ac0d122bc15040c9f38cb99633 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/***************************************************************************
Luca Risoliaf327ebb2007-01-08 10:43:56 -03002 * Plug-in for MI-0343 image sensor connected to the SN9C1xx PC Camera *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Controllers *
4 * *
Luca Risoliaf327ebb2007-01-08 10:43:56 -03005 * Copyright (C) 2004-2007 by Luca Risolia <luca.risolia@studio.unibo.it> *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * *
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. *
11 * *
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. *
16 * *
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 ***************************************************************************/
21
22#include "sn9c102_sensor.h"
23
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025static int mi0343_init(struct sn9c102_device* cam)
26{
Luca Risoliaf423b9a2007-03-26 16:12:04 -030027 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 int err = 0;
29
Trent Piephoc680dd62007-04-04 17:11:04 -030030 err = sn9c102_write_const_regs(cam, {0x00, 0x10}, {0x00, 0x11},
31 {0x0a, 0x14}, {0x40, 0x01},
32 {0x20, 0x17}, {0x07, 0x18},
33 {0xa0, 0x19});
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Luca Risoliaf423b9a2007-03-26 16:12:04 -030035 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
36 0x00, 0x01, 0, 0);
37 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x0d,
38 0x00, 0x00, 0, 0);
39 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x03,
40 0x01, 0xe1, 0, 0);
41 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x04,
42 0x02, 0x81, 0, 0);
43 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x05,
44 0x00, 0x17, 0, 0);
45 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x06,
46 0x00, 0x11, 0, 0);
47 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id, 0x62,
48 0x04, 0x9a, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 return err;
51}
52
53
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -030054static int mi0343_get_ctrl(struct sn9c102_device* cam,
55 struct v4l2_control* ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Luca Risoliaf423b9a2007-03-26 16:12:04 -030057 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
Luca Risolia480b55c22007-05-02 10:04:03 -030058 u8 data[2];
Luca Risoliaf423b9a2007-03-26 16:12:04 -030059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 switch (ctrl->id) {
61 case V4L2_CID_EXPOSURE:
Luca Risolia480b55c22007-05-02 10:04:03 -030062 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x09, 2,
63 data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 return -EIO;
Luca Risolia480b55c22007-05-02 10:04:03 -030065 ctrl->value = data[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 return 0;
67 case V4L2_CID_GAIN:
Luca Risolia480b55c22007-05-02 10:04:03 -030068 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x35, 2,
69 data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return -EIO;
71 break;
72 case V4L2_CID_HFLIP:
Luca Risolia480b55c22007-05-02 10:04:03 -030073 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
74 data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EIO;
Luca Risolia480b55c22007-05-02 10:04:03 -030076 ctrl->value = data[1] & 0x20 ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return 0;
78 case V4L2_CID_VFLIP:
Luca Risolia480b55c22007-05-02 10:04:03 -030079 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x20, 2,
80 data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return -EIO;
Luca Risolia480b55c22007-05-02 10:04:03 -030082 ctrl->value = data[1] & 0x80 ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
84 case V4L2_CID_RED_BALANCE:
Luca Risolia480b55c22007-05-02 10:04:03 -030085 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2d, 2,
86 data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return -EIO;
88 break;
89 case V4L2_CID_BLUE_BALANCE:
Luca Risolia480b55c22007-05-02 10:04:03 -030090 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2c, 2,
91 data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 return -EIO;
93 break;
94 case SN9C102_V4L2_CID_GREEN_BALANCE:
Luca Risolia480b55c22007-05-02 10:04:03 -030095 if (sn9c102_i2c_try_raw_read(cam, s, s->i2c_slave_id, 0x2e, 2,
96 data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return -EIO;
98 break;
99 default:
100 return -EINVAL;
101 }
102
103 switch (ctrl->id) {
104 case V4L2_CID_GAIN:
105 case V4L2_CID_RED_BALANCE:
106 case V4L2_CID_BLUE_BALANCE:
107 case SN9C102_V4L2_CID_GREEN_BALANCE:
Luca Risolia480b55c22007-05-02 10:04:03 -0300108 ctrl->value = data[1] | (data[0] << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (ctrl->value >= 0x10 && ctrl->value <= 0x3f)
110 ctrl->value -= 0x10;
111 else if (ctrl->value >= 0x60 && ctrl->value <= 0x7f)
112 ctrl->value -= 0x60;
113 else if (ctrl->value >= 0xe0 && ctrl->value <= 0xff)
114 ctrl->value -= 0xe0;
115 }
116
117 return 0;
118}
119
120
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300121static int mi0343_set_ctrl(struct sn9c102_device* cam,
122 const struct v4l2_control* ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300124 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u16 reg = 0;
126 int err = 0;
127
128 switch (ctrl->id) {
129 case V4L2_CID_GAIN:
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));
137 else
138 reg = 0xe0 + (ctrl->value - (0x3f-0x10) - (0x7f-0x60));
139 break;
140 }
141
142 switch (ctrl->id) {
143 case V4L2_CID_EXPOSURE:
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300144 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300145 0x09, ctrl->value, 0x00,
146 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148 case V4L2_CID_GAIN:
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300149 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300150 0x35, reg >> 8, reg & 0xff,
151 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 break;
153 case V4L2_CID_HFLIP:
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300154 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300155 0x20, ctrl->value ? 0x40:0x00,
156 ctrl->value ? 0x20:0x00,
157 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 break;
159 case V4L2_CID_VFLIP:
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300160 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300161 0x20, ctrl->value ? 0x80:0x00,
162 ctrl->value ? 0x80:0x00,
163 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 break;
165 case V4L2_CID_RED_BALANCE:
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300166 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300167 0x2d, reg >> 8, reg & 0xff,
168 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 break;
170 case V4L2_CID_BLUE_BALANCE:
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300171 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300172 0x2c, reg >> 8, reg & 0xff,
173 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 break;
175 case SN9C102_V4L2_CID_GREEN_BALANCE:
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300176 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300177 0x2b, reg >> 8, reg & 0xff,
178 0, 0);
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300179 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300180 0x2e, reg >> 8, reg & 0xff,
181 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 break;
183 default:
184 return -EINVAL;
185 }
186
187 return err ? -EIO : 0;
188}
189
190
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300191static int mi0343_set_crop(struct sn9c102_device* cam,
192 const struct v4l2_rect* rect)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Luca Risoliaf327ebb2007-01-08 10:43:56 -0300194 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 int err = 0;
196 u8 h_start = (u8)(rect->left - s->cropcap.bounds.left) + 0,
197 v_start = (u8)(rect->top - s->cropcap.bounds.top) + 2;
198
199 err += sn9c102_write_reg(cam, h_start, 0x12);
200 err += sn9c102_write_reg(cam, v_start, 0x13);
201
202 return err;
203}
204
205
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300206static int mi0343_set_pix_format(struct sn9c102_device* cam,
207 const struct v4l2_pix_format* pix)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300209 struct sn9c102_sensor* s = sn9c102_get_sensor(cam);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int err = 0;
211
212 if (pix->pixelformat == V4L2_PIX_FMT_SN9C10X) {
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300213 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300214 0x0a, 0x00, 0x03, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 err += sn9c102_write_reg(cam, 0x20, 0x19);
216 } else {
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300217 err += sn9c102_i2c_try_raw_write(cam, s, 4, s->i2c_slave_id,
Mauro Carvalho Chehabd56410e2006-03-25 09:19:53 -0300218 0x0a, 0x00, 0x05, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 err += sn9c102_write_reg(cam, 0xa0, 0x19);
220 }
221
222 return err;
223}
224
225
Luca Risolia480b55c22007-05-02 10:04:03 -0300226static const struct sn9c102_sensor mi0343 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .name = "MI-0343",
228 .maintainer = "Luca Risolia <luca.risolia@studio.unibo.it>",
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300229 .supported_bridge = BRIDGE_SN9C101 | BRIDGE_SN9C102,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .frequency = SN9C102_I2C_100KHZ,
231 .interface = SN9C102_I2C_2WIRES,
232 .i2c_slave_id = 0x5d,
233 .init = &mi0343_init,
234 .qctrl = {
235 {
236 .id = V4L2_CID_EXPOSURE,
237 .type = V4L2_CTRL_TYPE_INTEGER,
238 .name = "exposure",
239 .minimum = 0x00,
240 .maximum = 0x0f,
241 .step = 0x01,
242 .default_value = 0x06,
243 .flags = 0,
244 },
245 {
246 .id = V4L2_CID_GAIN,
247 .type = V4L2_CTRL_TYPE_INTEGER,
248 .name = "global gain",
249 .minimum = 0x00,
250 .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),/*0x6d*/
251 .step = 0x01,
252 .default_value = 0x00,
253 .flags = 0,
254 },
255 {
256 .id = V4L2_CID_HFLIP,
257 .type = V4L2_CTRL_TYPE_BOOLEAN,
258 .name = "horizontal mirror",
259 .minimum = 0,
260 .maximum = 1,
261 .step = 1,
262 .default_value = 0,
263 .flags = 0,
264 },
265 {
266 .id = V4L2_CID_VFLIP,
267 .type = V4L2_CTRL_TYPE_BOOLEAN,
268 .name = "vertical mirror",
269 .minimum = 0,
270 .maximum = 1,
271 .step = 1,
272 .default_value = 0,
273 .flags = 0,
274 },
275 {
276 .id = V4L2_CID_RED_BALANCE,
277 .type = V4L2_CTRL_TYPE_INTEGER,
278 .name = "red balance",
279 .minimum = 0x00,
280 .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
281 .step = 0x01,
282 .default_value = 0x00,
283 .flags = 0,
284 },
285 {
286 .id = V4L2_CID_BLUE_BALANCE,
287 .type = V4L2_CTRL_TYPE_INTEGER,
288 .name = "blue balance",
289 .minimum = 0x00,
290 .maximum = (0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0),
291 .step = 0x01,
292 .default_value = 0x00,
293 .flags = 0,
294 },
295 {
296 .id = SN9C102_V4L2_CID_GREEN_BALANCE,
297 .type = V4L2_CTRL_TYPE_INTEGER,
298 .name = "green balance",
299 .minimum = 0x00,
300 .maximum = ((0x3f-0x10)+(0x7f-0x60)+(0xff-0xe0)),
301 .step = 0x01,
302 .default_value = 0x00,
303 .flags = 0,
304 },
305 },
306 .get_ctrl = &mi0343_get_ctrl,
307 .set_ctrl = &mi0343_set_ctrl,
308 .cropcap = {
309 .bounds = {
310 .left = 0,
311 .top = 0,
312 .width = 640,
313 .height = 480,
314 },
315 .defrect = {
316 .left = 0,
317 .top = 0,
318 .width = 640,
319 .height = 480,
320 },
321 },
322 .set_crop = &mi0343_set_crop,
323 .pix_format = {
324 .width = 640,
325 .height = 480,
326 .pixelformat = V4L2_PIX_FMT_SBGGR8,
327 .priv = 8,
328 },
329 .set_pix_format = &mi0343_set_pix_format
330};
331
332
333int sn9c102_probe_mi0343(struct sn9c102_device* cam)
334{
Luca Risolia480b55c22007-05-02 10:04:03 -0300335 u8 data[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Luca Risolia480b55c22007-05-02 10:04:03 -0300337 if (sn9c102_write_const_regs(cam, {0x01, 0x01}, {0x00, 0x01},
338 {0x28, 0x17}))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return -EIO;
340
341 if (sn9c102_i2c_try_raw_read(cam, &mi0343, mi0343.i2c_slave_id, 0x00,
Luca Risoliaf423b9a2007-03-26 16:12:04 -0300342 2, data) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return -EIO;
344
Luca Risolia480b55c22007-05-02 10:04:03 -0300345 if (data[1] != 0x42 || data[0] != 0xe3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -ENODEV;
347
348 sn9c102_attach_sensor(cam, &mi0343);
349
350 return 0;
351}