Commit | Line | Data |
---|---|---|
c109f816 EA |
1 | /* |
2 | * Driver for the mt9m111 sensor | |
3 | * | |
0c505e68 | 4 | * Copyright (C) 2008 Erik Andrén |
c109f816 EA |
5 | * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project. |
6 | * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br> | |
7 | * | |
8 | * Portions of code to USB interface and ALi driver software, | |
9 | * Copyright (c) 2006 Willem Duinker | |
10 | * v4l2 interface modeled after the V4L2 driver | |
11 | * for SN9C10x PC Camera Controllers | |
12 | * | |
13 | * This program is free software; you can redistribute it and/or | |
14 | * modify it under the terms of the GNU General Public License as | |
15 | * published by the Free Software Foundation, version 2. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include "m5602_mt9m111.h" | |
20 | ||
cf811d50 EA |
21 | static int mt9m111_set_vflip(struct gspca_dev *gspca_dev, __s32 val); |
22 | static int mt9m111_get_vflip(struct gspca_dev *gspca_dev, __s32 *val); | |
23 | static int mt9m111_get_hflip(struct gspca_dev *gspca_dev, __s32 *val); | |
24 | static int mt9m111_set_hflip(struct gspca_dev *gspca_dev, __s32 val); | |
25 | static int mt9m111_get_gain(struct gspca_dev *gspca_dev, __s32 *val); | |
26 | static int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val); | |
b154044a EA |
27 | static int mt9m111_set_auto_white_balance(struct gspca_dev *gspca_dev, |
28 | __s32 val); | |
29 | static int mt9m111_get_auto_white_balance(struct gspca_dev *gspca_dev, | |
30 | __s32 *val); | |
12e4ed7d EA |
31 | static int mt9m111_get_green_balance(struct gspca_dev *gspca_dev, __s32 *val); |
32 | static int mt9m111_set_green_balance(struct gspca_dev *gspca_dev, __s32 val); | |
74b123cf EA |
33 | static int mt9m111_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val); |
34 | static int mt9m111_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val); | |
0746673d EA |
35 | static int mt9m111_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val); |
36 | static int mt9m111_set_red_balance(struct gspca_dev *gspca_dev, __s32 val); | |
cf811d50 | 37 | |
74cadfe1 EA |
38 | static struct v4l2_pix_format mt9m111_modes[] = { |
39 | { | |
60ed6e4f EA |
40 | 320, |
41 | 240, | |
42 | V4L2_PIX_FMT_SBGGR8, | |
43 | V4L2_FIELD_NONE, | |
44 | .sizeimage = 320 * 240, | |
45 | .bytesperline = 320, | |
46 | .colorspace = V4L2_COLORSPACE_SRGB, | |
47 | .priv = 0 | |
48 | }, { | |
74cadfe1 EA |
49 | 640, |
50 | 480, | |
51 | V4L2_PIX_FMT_SBGGR8, | |
52 | V4L2_FIELD_NONE, | |
53 | .sizeimage = 640 * 480, | |
54 | .bytesperline = 640, | |
55 | .colorspace = V4L2_COLORSPACE_SRGB, | |
56 | .priv = 0 | |
57 | } | |
58 | }; | |
59 | ||
e17cc08c | 60 | const static struct ctrl mt9m111_ctrls[] = { |
04f15655 | 61 | #define VFLIP_IDX 0 |
e17cc08c EA |
62 | { |
63 | { | |
64 | .id = V4L2_CID_VFLIP, | |
65 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
66 | .name = "vertical flip", | |
67 | .minimum = 0, | |
68 | .maximum = 1, | |
69 | .step = 1, | |
70 | .default_value = 0 | |
71 | }, | |
72 | .set = mt9m111_set_vflip, | |
73 | .get = mt9m111_get_vflip | |
04f15655 EA |
74 | }, |
75 | #define HFLIP_IDX 1 | |
76 | { | |
e17cc08c EA |
77 | { |
78 | .id = V4L2_CID_HFLIP, | |
79 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
80 | .name = "horizontal flip", | |
81 | .minimum = 0, | |
82 | .maximum = 1, | |
83 | .step = 1, | |
84 | .default_value = 0 | |
85 | }, | |
86 | .set = mt9m111_set_hflip, | |
87 | .get = mt9m111_get_hflip | |
04f15655 EA |
88 | }, |
89 | #define GAIN_IDX 2 | |
90 | { | |
e17cc08c EA |
91 | { |
92 | .id = V4L2_CID_GAIN, | |
93 | .type = V4L2_CTRL_TYPE_INTEGER, | |
94 | .name = "gain", | |
95 | .minimum = 0, | |
96 | .maximum = (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2, | |
97 | .step = 1, | |
98 | .default_value = DEFAULT_GAIN, | |
99 | .flags = V4L2_CTRL_FLAG_SLIDER | |
100 | }, | |
101 | .set = mt9m111_set_gain, | |
102 | .get = mt9m111_get_gain | |
b154044a EA |
103 | }, |
104 | #define AUTO_WHITE_BALANCE_IDX 3 | |
105 | { | |
106 | { | |
107 | .id = V4L2_CID_AUTO_WHITE_BALANCE, | |
108 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
109 | .name = "auto white balance", | |
110 | .minimum = 0, | |
111 | .maximum = 1, | |
112 | .step = 1, | |
113 | .default_value = 0, | |
114 | }, | |
115 | .set = mt9m111_set_auto_white_balance, | |
116 | .get = mt9m111_get_auto_white_balance | |
12e4ed7d EA |
117 | }, |
118 | #define GREEN_BALANCE_IDX 4 | |
119 | { | |
120 | { | |
121 | .id = M5602_V4L2_CID_GREEN_BALANCE, | |
122 | .type = V4L2_CTRL_TYPE_INTEGER, | |
123 | .name = "green balance", | |
124 | .minimum = 0x00, | |
125 | .maximum = 0x7ff, | |
126 | .step = 0x1, | |
127 | .default_value = MT9M111_GREEN_GAIN_DEFAULT, | |
128 | .flags = V4L2_CTRL_FLAG_SLIDER | |
129 | }, | |
130 | .set = mt9m111_set_green_balance, | |
131 | .get = mt9m111_get_green_balance | |
132 | }, | |
74b123cf EA |
133 | #define BLUE_BALANCE_IDX 5 |
134 | { | |
135 | { | |
136 | .id = V4L2_CID_BLUE_BALANCE, | |
137 | .type = V4L2_CTRL_TYPE_INTEGER, | |
138 | .name = "blue balance", | |
139 | .minimum = 0x00, | |
140 | .maximum = 0x7ff, | |
141 | .step = 0x1, | |
142 | .default_value = MT9M111_BLUE_GAIN_DEFAULT, | |
143 | .flags = V4L2_CTRL_FLAG_SLIDER | |
144 | }, | |
145 | .set = mt9m111_set_blue_balance, | |
146 | .get = mt9m111_get_blue_balance | |
147 | }, | |
0746673d EA |
148 | #define RED_BALANCE_IDX 5 |
149 | { | |
150 | { | |
151 | .id = V4L2_CID_RED_BALANCE, | |
152 | .type = V4L2_CTRL_TYPE_INTEGER, | |
153 | .name = "red balance", | |
154 | .minimum = 0x00, | |
155 | .maximum = 0x7ff, | |
156 | .step = 0x1, | |
157 | .default_value = MT9M111_RED_GAIN_DEFAULT, | |
158 | .flags = V4L2_CTRL_FLAG_SLIDER | |
159 | }, | |
160 | .set = mt9m111_set_red_balance, | |
161 | .get = mt9m111_get_red_balance | |
162 | }, | |
e17cc08c EA |
163 | }; |
164 | ||
658efb63 EA |
165 | static void mt9m111_dump_registers(struct sd *sd); |
166 | ||
c109f816 EA |
167 | int mt9m111_probe(struct sd *sd) |
168 | { | |
169 | u8 data[2] = {0x00, 0x00}; | |
170 | int i; | |
04f15655 | 171 | s32 *sensor_settings; |
c109f816 EA |
172 | |
173 | if (force_sensor) { | |
174 | if (force_sensor == MT9M111_SENSOR) { | |
175 | info("Forcing a %s sensor", mt9m111.name); | |
176 | goto sensor_found; | |
177 | } | |
178 | /* If we want to force another sensor, don't try to probe this | |
179 | * one */ | |
180 | return -ENODEV; | |
181 | } | |
182 | ||
183 | info("Probing for a mt9m111 sensor"); | |
184 | ||
185 | /* Do the preinit */ | |
186 | for (i = 0; i < ARRAY_SIZE(preinit_mt9m111); i++) { | |
187 | if (preinit_mt9m111[i][0] == BRIDGE) { | |
188 | m5602_write_bridge(sd, | |
189 | preinit_mt9m111[i][1], | |
190 | preinit_mt9m111[i][2]); | |
191 | } else { | |
192 | data[0] = preinit_mt9m111[i][2]; | |
193 | data[1] = preinit_mt9m111[i][3]; | |
6dc4cff0 | 194 | m5602_write_sensor(sd, |
c109f816 EA |
195 | preinit_mt9m111[i][1], data, 2); |
196 | } | |
197 | } | |
198 | ||
4f93c450 | 199 | if (m5602_read_sensor(sd, MT9M111_SC_CHIPVER, data, 2)) |
c109f816 EA |
200 | return -ENODEV; |
201 | ||
202 | if ((data[0] == 0x14) && (data[1] == 0x3a)) { | |
203 | info("Detected a mt9m111 sensor"); | |
204 | goto sensor_found; | |
205 | } | |
206 | ||
207 | return -ENODEV; | |
208 | ||
209 | sensor_found: | |
cf811d50 EA |
210 | sensor_settings = kmalloc(ARRAY_SIZE(mt9m111_ctrls) * sizeof(s32), |
211 | GFP_KERNEL); | |
04f15655 EA |
212 | if (!sensor_settings) |
213 | return -ENOMEM; | |
214 | ||
74cadfe1 EA |
215 | sd->gspca_dev.cam.cam_mode = mt9m111_modes; |
216 | sd->gspca_dev.cam.nmodes = ARRAY_SIZE(mt9m111_modes); | |
e17cc08c | 217 | sd->desc->ctrls = mt9m111_ctrls; |
e4cc4fcc | 218 | sd->desc->nctrls = ARRAY_SIZE(mt9m111_ctrls); |
04f15655 EA |
219 | |
220 | for (i = 0; i < ARRAY_SIZE(mt9m111_ctrls); i++) | |
221 | sensor_settings[i] = mt9m111_ctrls[i].qctrl.default_value; | |
222 | sd->sensor_priv = sensor_settings; | |
223 | ||
c109f816 EA |
224 | return 0; |
225 | } | |
226 | ||
227 | int mt9m111_init(struct sd *sd) | |
228 | { | |
f1918fa2 | 229 | int i, err = 0; |
dd3ec398 | 230 | s32 *sensor_settings = sd->sensor_priv; |
c109f816 EA |
231 | |
232 | /* Init the sensor */ | |
8bcbc911 | 233 | for (i = 0; i < ARRAY_SIZE(init_mt9m111) && !err; i++) { |
c109f816 EA |
234 | u8 data[2]; |
235 | ||
236 | if (init_mt9m111[i][0] == BRIDGE) { | |
237 | err = m5602_write_bridge(sd, | |
238 | init_mt9m111[i][1], | |
239 | init_mt9m111[i][2]); | |
240 | } else { | |
241 | data[0] = init_mt9m111[i][2]; | |
242 | data[1] = init_mt9m111[i][3]; | |
6dc4cff0 | 243 | err = m5602_write_sensor(sd, |
c109f816 EA |
244 | init_mt9m111[i][1], data, 2); |
245 | } | |
246 | } | |
247 | ||
36e756c5 EA |
248 | if (dump_sensor) |
249 | mt9m111_dump_registers(sd); | |
250 | ||
dd3ec398 EA |
251 | err = mt9m111_set_vflip(&sd->gspca_dev, sensor_settings[VFLIP_IDX]); |
252 | if (err < 0) | |
253 | return err; | |
254 | ||
255 | err = mt9m111_set_hflip(&sd->gspca_dev, sensor_settings[HFLIP_IDX]); | |
256 | if (err < 0) | |
257 | return err; | |
258 | ||
12e4ed7d EA |
259 | err = mt9m111_set_green_balance(&sd->gspca_dev, |
260 | sensor_settings[GREEN_BALANCE_IDX]); | |
261 | if (err < 0) | |
262 | return err; | |
c109f816 | 263 | |
74b123cf EA |
264 | err = mt9m111_set_blue_balance(&sd->gspca_dev, |
265 | sensor_settings[BLUE_BALANCE_IDX]); | |
266 | if (err < 0) | |
267 | return err; | |
268 | ||
0746673d EA |
269 | err = mt9m111_set_red_balance(&sd->gspca_dev, |
270 | sensor_settings[RED_BALANCE_IDX]); | |
271 | if (err < 0) | |
272 | return err; | |
273 | ||
12e4ed7d | 274 | return mt9m111_set_gain(&sd->gspca_dev, sensor_settings[GAIN_IDX]); |
c109f816 EA |
275 | } |
276 | ||
bce0d2d4 EA |
277 | int mt9m111_start(struct sd *sd) |
278 | { | |
279 | int i, err = 0; | |
280 | u8 data[2]; | |
e7ae60f7 | 281 | struct cam *cam = &sd->gspca_dev.cam; |
f676bb39 | 282 | s32 *sensor_settings = sd->sensor_priv; |
e7ae60f7 | 283 | |
f676bb39 | 284 | int width = cam->cam_mode[sd->gspca_dev.curr_mode].width - 1; |
e7ae60f7 | 285 | int height = cam->cam_mode[sd->gspca_dev.curr_mode].height; |
bce0d2d4 EA |
286 | |
287 | for (i = 0; i < ARRAY_SIZE(start_mt9m111) && !err; i++) { | |
288 | if (start_mt9m111[i][0] == BRIDGE) { | |
289 | err = m5602_write_bridge(sd, | |
290 | start_mt9m111[i][1], | |
291 | start_mt9m111[i][2]); | |
292 | } else { | |
293 | data[0] = start_mt9m111[i][2]; | |
294 | data[1] = start_mt9m111[i][3]; | |
295 | err = m5602_write_sensor(sd, | |
296 | start_mt9m111[i][1], data, 2); | |
297 | } | |
298 | } | |
e7ae60f7 EA |
299 | if (err < 0) |
300 | return err; | |
301 | ||
302 | err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height >> 8) & 0xff); | |
303 | if (err < 0) | |
304 | return err; | |
305 | ||
306 | err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, (height & 0xff)); | |
307 | if (err < 0) | |
308 | return err; | |
309 | ||
310 | for (i = 0; i < 2 && !err; i++) | |
311 | err = m5602_write_bridge(sd, M5602_XB_VSYNC_PARA, 0); | |
312 | if (err < 0) | |
313 | return err; | |
314 | ||
315 | err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0); | |
316 | if (err < 0) | |
317 | return err; | |
318 | ||
319 | err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 2); | |
320 | if (err < 0) | |
321 | return err; | |
322 | ||
323 | for (i = 0; i < 2 && !err; i++) | |
324 | err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, 0); | |
325 | if (err < 0) | |
326 | return err; | |
327 | ||
328 | err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, | |
329 | (width >> 8) & 0xff); | |
330 | if (err < 0) | |
331 | return err; | |
332 | ||
333 | err = m5602_write_bridge(sd, M5602_XB_HSYNC_PARA, width & 0xff); | |
334 | if (err < 0) | |
335 | return err; | |
336 | ||
337 | err = m5602_write_bridge(sd, M5602_XB_SIG_INI, 0); | |
338 | if (err < 0) | |
339 | return err; | |
340 | ||
341 | switch (width) { | |
342 | case 640: | |
343 | PDEBUG(D_V4L2, "Configuring camera for VGA mode"); | |
f676bb39 EA |
344 | data[0] = MT9M111_RMB_OVER_SIZED; |
345 | data[1] = MT9M111_RMB_ROW_SKIP_2X | | |
346 | MT9M111_RMB_COLUMN_SKIP_2X | | |
347 | (sensor_settings[VFLIP_IDX] << 0) | | |
348 | (sensor_settings[HFLIP_IDX] << 1); | |
349 | ||
350 | err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2); | |
e7ae60f7 | 351 | break; |
60ed6e4f EA |
352 | |
353 | case 320: | |
354 | PDEBUG(D_V4L2, "Configuring camera for QVGA mode"); | |
355 | data[0] = MT9M111_RMB_OVER_SIZED; | |
356 | data[1] = MT9M111_RMB_ROW_SKIP_4X | | |
357 | MT9M111_RMB_COLUMN_SKIP_4X | | |
358 | (sensor_settings[VFLIP_IDX] << 0) | | |
359 | (sensor_settings[HFLIP_IDX] << 1); | |
360 | err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2); | |
361 | break; | |
e7ae60f7 | 362 | } |
bce0d2d4 EA |
363 | return err; |
364 | } | |
365 | ||
04f15655 EA |
366 | void mt9m111_disconnect(struct sd *sd) |
367 | { | |
368 | sd->sensor = NULL; | |
369 | kfree(sd->sensor_priv); | |
370 | } | |
371 | ||
cf811d50 | 372 | static int mt9m111_get_vflip(struct gspca_dev *gspca_dev, __s32 *val) |
c109f816 | 373 | { |
c109f816 | 374 | struct sd *sd = (struct sd *) gspca_dev; |
04f15655 | 375 | s32 *sensor_settings = sd->sensor_priv; |
c109f816 | 376 | |
04f15655 | 377 | *val = sensor_settings[VFLIP_IDX]; |
17ea88ae | 378 | PDEBUG(D_V4L2, "Read vertical flip %d", *val); |
c109f816 | 379 | |
04f15655 | 380 | return 0; |
c109f816 EA |
381 | } |
382 | ||
cf811d50 | 383 | static int mt9m111_set_vflip(struct gspca_dev *gspca_dev, __s32 val) |
c109f816 EA |
384 | { |
385 | int err; | |
386 | u8 data[2] = {0x00, 0x00}; | |
387 | struct sd *sd = (struct sd *) gspca_dev; | |
04f15655 | 388 | s32 *sensor_settings = sd->sensor_priv; |
c109f816 | 389 | |
17ea88ae | 390 | PDEBUG(D_V4L2, "Set vertical flip to %d", val); |
c109f816 | 391 | |
04f15655 EA |
392 | sensor_settings[VFLIP_IDX] = val; |
393 | ||
c109f816 | 394 | /* Set the correct page map */ |
6dc4cff0 | 395 | err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2); |
c109f816 | 396 | if (err < 0) |
051781b3 | 397 | return err; |
c109f816 | 398 | |
4f93c450 | 399 | err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2); |
c109f816 | 400 | if (err < 0) |
051781b3 | 401 | return err; |
c109f816 EA |
402 | |
403 | data[0] = (data[0] & 0xfe) | val; | |
6dc4cff0 | 404 | err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, |
c109f816 | 405 | data, 2); |
8bcbc911 | 406 | return err; |
c109f816 EA |
407 | } |
408 | ||
cf811d50 | 409 | static int mt9m111_get_hflip(struct gspca_dev *gspca_dev, __s32 *val) |
c109f816 | 410 | { |
c109f816 | 411 | struct sd *sd = (struct sd *) gspca_dev; |
04f15655 | 412 | s32 *sensor_settings = sd->sensor_priv; |
c109f816 | 413 | |
04f15655 | 414 | *val = sensor_settings[HFLIP_IDX]; |
17ea88ae | 415 | PDEBUG(D_V4L2, "Read horizontal flip %d", *val); |
c109f816 | 416 | |
04f15655 | 417 | return 0; |
c109f816 EA |
418 | } |
419 | ||
cf811d50 | 420 | static int mt9m111_set_hflip(struct gspca_dev *gspca_dev, __s32 val) |
c109f816 EA |
421 | { |
422 | int err; | |
423 | u8 data[2] = {0x00, 0x00}; | |
424 | struct sd *sd = (struct sd *) gspca_dev; | |
04f15655 | 425 | s32 *sensor_settings = sd->sensor_priv; |
c109f816 | 426 | |
17ea88ae | 427 | PDEBUG(D_V4L2, "Set horizontal flip to %d", val); |
c109f816 | 428 | |
04f15655 | 429 | sensor_settings[HFLIP_IDX] = val; |
c109f816 | 430 | /* Set the correct page map */ |
6dc4cff0 | 431 | err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2); |
c109f816 | 432 | if (err < 0) |
051781b3 | 433 | return err; |
c109f816 | 434 | |
4f93c450 | 435 | err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2); |
c109f816 | 436 | if (err < 0) |
051781b3 | 437 | return err; |
c109f816 EA |
438 | |
439 | data[0] = (data[0] & 0xfd) | ((val << 1) & 0x02); | |
6dc4cff0 | 440 | err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, |
c109f816 | 441 | data, 2); |
8bcbc911 | 442 | return err; |
c109f816 EA |
443 | } |
444 | ||
cf811d50 | 445 | static int mt9m111_get_gain(struct gspca_dev *gspca_dev, __s32 *val) |
c109f816 | 446 | { |
c109f816 | 447 | struct sd *sd = (struct sd *) gspca_dev; |
04f15655 | 448 | s32 *sensor_settings = sd->sensor_priv; |
c109f816 | 449 | |
04f15655 | 450 | *val = sensor_settings[GAIN_IDX]; |
17ea88ae | 451 | PDEBUG(D_V4L2, "Read gain %d", *val); |
c109f816 | 452 | |
04f15655 | 453 | return 0; |
c109f816 EA |
454 | } |
455 | ||
b154044a EA |
456 | static int mt9m111_set_auto_white_balance(struct gspca_dev *gspca_dev, |
457 | __s32 val) | |
458 | { | |
459 | struct sd *sd = (struct sd *) gspca_dev; | |
460 | s32 *sensor_settings = sd->sensor_priv; | |
461 | int err; | |
462 | u8 data[2]; | |
463 | ||
464 | err = m5602_read_sensor(sd, MT9M111_CP_OPERATING_MODE_CTL, data, 2); | |
465 | if (err < 0) | |
466 | return err; | |
467 | ||
468 | sensor_settings[AUTO_WHITE_BALANCE_IDX] = val & 0x01; | |
469 | data[0] = ((data[0] & 0xfd) | ((val & 0x01) << 1)); | |
470 | ||
471 | err = m5602_write_sensor(sd, MT9M111_CP_OPERATING_MODE_CTL, data, 2); | |
472 | ||
473 | PDEBUG(D_V4L2, "Set auto white balance %d", val); | |
474 | return err; | |
475 | } | |
476 | ||
477 | static int mt9m111_get_auto_white_balance(struct gspca_dev *gspca_dev, | |
478 | __s32 *val) { | |
479 | struct sd *sd = (struct sd *) gspca_dev; | |
480 | s32 *sensor_settings = sd->sensor_priv; | |
481 | ||
482 | *val = sensor_settings[AUTO_WHITE_BALANCE_IDX]; | |
483 | PDEBUG(D_V4L2, "Read auto white balance %d", *val); | |
484 | return 0; | |
485 | } | |
486 | ||
cf811d50 | 487 | static int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val) |
c109f816 EA |
488 | { |
489 | int err, tmp; | |
490 | u8 data[2] = {0x00, 0x00}; | |
491 | struct sd *sd = (struct sd *) gspca_dev; | |
04f15655 EA |
492 | s32 *sensor_settings = sd->sensor_priv; |
493 | ||
494 | sensor_settings[GAIN_IDX] = val; | |
c109f816 EA |
495 | |
496 | /* Set the correct page map */ | |
6dc4cff0 | 497 | err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2); |
c109f816 | 498 | if (err < 0) |
051781b3 | 499 | return err; |
c109f816 EA |
500 | |
501 | if (val >= INITIAL_MAX_GAIN * 2 * 2 * 2) | |
502 | return -EINVAL; | |
503 | ||
504 | if ((val >= INITIAL_MAX_GAIN * 2 * 2) && | |
505 | (val < (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2)) | |
506 | tmp = (1 << 10) | (val << 9) | | |
507 | (val << 8) | (val / 8); | |
508 | else if ((val >= INITIAL_MAX_GAIN * 2) && | |
509 | (val < INITIAL_MAX_GAIN * 2 * 2)) | |
510 | tmp = (1 << 9) | (1 << 8) | (val / 4); | |
511 | else if ((val >= INITIAL_MAX_GAIN) && | |
512 | (val < INITIAL_MAX_GAIN * 2)) | |
513 | tmp = (1 << 8) | (val / 2); | |
514 | else | |
515 | tmp = val; | |
516 | ||
517 | data[1] = (tmp & 0xff00) >> 8; | |
518 | data[0] = (tmp & 0xff); | |
17ea88ae | 519 | PDEBUG(D_V4L2, "tmp=%d, data[1]=%d, data[0]=%d", tmp, |
c109f816 EA |
520 | data[1], data[0]); |
521 | ||
6dc4cff0 | 522 | err = m5602_write_sensor(sd, MT9M111_SC_GLOBAL_GAIN, |
c109f816 | 523 | data, 2); |
051781b3 | 524 | |
8bcbc911 | 525 | return err; |
c109f816 EA |
526 | } |
527 | ||
12e4ed7d EA |
528 | static int mt9m111_set_green_balance(struct gspca_dev *gspca_dev, __s32 val) |
529 | { | |
530 | int err; | |
531 | u8 data[2]; | |
532 | struct sd *sd = (struct sd *) gspca_dev; | |
533 | s32 *sensor_settings = sd->sensor_priv; | |
534 | ||
535 | sensor_settings[GREEN_BALANCE_IDX] = val; | |
536 | data[0] = (val & 0xff); | |
537 | data[1] = (val & 0xff00) >> 8; | |
538 | ||
539 | PDEBUG(D_V4L2, "Set green balance %d", val); | |
540 | err = m5602_write_sensor(sd, MT9M111_SC_GREEN_1_GAIN, | |
541 | data, 2); | |
542 | if (err < 0) | |
543 | return err; | |
544 | ||
545 | return m5602_write_sensor(sd, MT9M111_SC_GREEN_2_GAIN, | |
546 | data, 2); | |
547 | } | |
548 | ||
549 | static int mt9m111_get_green_balance(struct gspca_dev *gspca_dev, __s32 *val) | |
550 | { | |
551 | struct sd *sd = (struct sd *) gspca_dev; | |
552 | s32 *sensor_settings = sd->sensor_priv; | |
553 | ||
554 | *val = sensor_settings[GREEN_BALANCE_IDX]; | |
555 | PDEBUG(D_V4L2, "Read green balance %d", *val); | |
556 | return 0; | |
557 | } | |
558 | ||
74b123cf EA |
559 | static int mt9m111_set_blue_balance(struct gspca_dev *gspca_dev, __s32 val) |
560 | { | |
561 | u8 data[2]; | |
562 | struct sd *sd = (struct sd *) gspca_dev; | |
563 | s32 *sensor_settings = sd->sensor_priv; | |
564 | ||
565 | sensor_settings[BLUE_BALANCE_IDX] = val; | |
566 | data[0] = (val & 0xff); | |
567 | data[1] = (val & 0xff00) >> 8; | |
568 | ||
569 | PDEBUG(D_V4L2, "Set blue balance %d", val); | |
570 | ||
571 | return m5602_write_sensor(sd, MT9M111_SC_BLUE_GAIN, | |
572 | data, 2); | |
573 | } | |
574 | ||
575 | static int mt9m111_get_blue_balance(struct gspca_dev *gspca_dev, __s32 *val) | |
576 | { | |
577 | struct sd *sd = (struct sd *) gspca_dev; | |
578 | s32 *sensor_settings = sd->sensor_priv; | |
579 | ||
580 | *val = sensor_settings[BLUE_BALANCE_IDX]; | |
581 | PDEBUG(D_V4L2, "Read blue balance %d", *val); | |
582 | return 0; | |
583 | } | |
12e4ed7d | 584 | |
0746673d EA |
585 | static int mt9m111_set_red_balance(struct gspca_dev *gspca_dev, __s32 val) |
586 | { | |
587 | u8 data[2]; | |
588 | struct sd *sd = (struct sd *) gspca_dev; | |
589 | s32 *sensor_settings = sd->sensor_priv; | |
590 | ||
591 | sensor_settings[RED_BALANCE_IDX] = val; | |
592 | data[0] = (val & 0xff); | |
593 | data[1] = (val & 0xff00) >> 8; | |
594 | ||
595 | PDEBUG(D_V4L2, "Set red balance %d", val); | |
596 | ||
597 | return m5602_write_sensor(sd, MT9M111_SC_RED_GAIN, | |
598 | data, 2); | |
599 | } | |
600 | ||
601 | static int mt9m111_get_red_balance(struct gspca_dev *gspca_dev, __s32 *val) | |
602 | { | |
603 | struct sd *sd = (struct sd *) gspca_dev; | |
604 | s32 *sensor_settings = sd->sensor_priv; | |
605 | ||
606 | *val = sensor_settings[RED_BALANCE_IDX]; | |
607 | PDEBUG(D_V4L2, "Read red balance %d", *val); | |
608 | return 0; | |
609 | } | |
610 | ||
658efb63 | 611 | static void mt9m111_dump_registers(struct sd *sd) |
c109f816 EA |
612 | { |
613 | u8 address, value[2] = {0x00, 0x00}; | |
614 | ||
615 | info("Dumping the mt9m111 register state"); | |
616 | ||
617 | info("Dumping the mt9m111 sensor core registers"); | |
618 | value[1] = MT9M111_SENSOR_CORE; | |
6dc4cff0 | 619 | m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2); |
c109f816 | 620 | for (address = 0; address < 0xff; address++) { |
4f93c450 | 621 | m5602_read_sensor(sd, address, value, 2); |
c109f816 EA |
622 | info("register 0x%x contains 0x%x%x", |
623 | address, value[0], value[1]); | |
624 | } | |
625 | ||
626 | info("Dumping the mt9m111 color pipeline registers"); | |
627 | value[1] = MT9M111_COLORPIPE; | |
6dc4cff0 | 628 | m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2); |
c109f816 | 629 | for (address = 0; address < 0xff; address++) { |
4f93c450 | 630 | m5602_read_sensor(sd, address, value, 2); |
c109f816 EA |
631 | info("register 0x%x contains 0x%x%x", |
632 | address, value[0], value[1]); | |
633 | } | |
634 | ||
635 | info("Dumping the mt9m111 camera control registers"); | |
636 | value[1] = MT9M111_CAMERA_CONTROL; | |
6dc4cff0 | 637 | m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2); |
c109f816 | 638 | for (address = 0; address < 0xff; address++) { |
4f93c450 | 639 | m5602_read_sensor(sd, address, value, 2); |
c109f816 EA |
640 | info("register 0x%x contains 0x%x%x", |
641 | address, value[0], value[1]); | |
642 | } | |
643 | ||
644 | info("mt9m111 register state dump complete"); | |
645 | } |