2 * Driver for the mt9m111 sensor
4 * Copyright (C) 2008 Erik Andrén
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>
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
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.
19 #include "m5602_mt9m111.h"
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
);
27 static int mt9m111_set_auto_white_balance(struct gspca_dev
*gspca_dev
,
29 static int mt9m111_get_auto_white_balance(struct gspca_dev
*gspca_dev
,
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
);
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
);
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
);
38 static struct v4l2_pix_format mt9m111_modes
[] = {
44 .sizeimage
= 640 * 480,
46 .colorspace
= V4L2_COLORSPACE_SRGB
,
51 static const struct ctrl mt9m111_ctrls
[] = {
56 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
57 .name
= "vertical flip",
63 .set
= mt9m111_set_vflip
,
64 .get
= mt9m111_get_vflip
70 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
71 .name
= "horizontal flip",
77 .set
= mt9m111_set_hflip
,
78 .get
= mt9m111_get_hflip
84 .type
= V4L2_CTRL_TYPE_INTEGER
,
87 .maximum
= (INITIAL_MAX_GAIN
- 1) * 2 * 2 * 2,
89 .default_value
= MT9M111_DEFAULT_GAIN
,
90 .flags
= V4L2_CTRL_FLAG_SLIDER
92 .set
= mt9m111_set_gain
,
93 .get
= mt9m111_get_gain
95 #define AUTO_WHITE_BALANCE_IDX 3
98 .id
= V4L2_CID_AUTO_WHITE_BALANCE
,
99 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
100 .name
= "auto white balance",
106 .set
= mt9m111_set_auto_white_balance
,
107 .get
= mt9m111_get_auto_white_balance
109 #define GREEN_BALANCE_IDX 4
112 .id
= M5602_V4L2_CID_GREEN_BALANCE
,
113 .type
= V4L2_CTRL_TYPE_INTEGER
,
114 .name
= "green balance",
118 .default_value
= MT9M111_GREEN_GAIN_DEFAULT
,
119 .flags
= V4L2_CTRL_FLAG_SLIDER
121 .set
= mt9m111_set_green_balance
,
122 .get
= mt9m111_get_green_balance
124 #define BLUE_BALANCE_IDX 5
127 .id
= V4L2_CID_BLUE_BALANCE
,
128 .type
= V4L2_CTRL_TYPE_INTEGER
,
129 .name
= "blue balance",
133 .default_value
= MT9M111_BLUE_GAIN_DEFAULT
,
134 .flags
= V4L2_CTRL_FLAG_SLIDER
136 .set
= mt9m111_set_blue_balance
,
137 .get
= mt9m111_get_blue_balance
139 #define RED_BALANCE_IDX 5
142 .id
= V4L2_CID_RED_BALANCE
,
143 .type
= V4L2_CTRL_TYPE_INTEGER
,
144 .name
= "red balance",
148 .default_value
= MT9M111_RED_GAIN_DEFAULT
,
149 .flags
= V4L2_CTRL_FLAG_SLIDER
151 .set
= mt9m111_set_red_balance
,
152 .get
= mt9m111_get_red_balance
156 static void mt9m111_dump_registers(struct sd
*sd
);
158 int mt9m111_probe(struct sd
*sd
)
160 u8 data
[2] = {0x00, 0x00};
162 s32
*sensor_settings
;
165 if (force_sensor
== MT9M111_SENSOR
) {
166 info("Forcing a %s sensor", mt9m111
.name
);
169 /* If we want to force another sensor, don't try to probe this
174 PDEBUG(D_PROBE
, "Probing for a mt9m111 sensor");
177 for (i
= 0; i
< ARRAY_SIZE(preinit_mt9m111
); i
++) {
178 if (preinit_mt9m111
[i
][0] == BRIDGE
) {
179 m5602_write_bridge(sd
,
180 preinit_mt9m111
[i
][1],
181 preinit_mt9m111
[i
][2]);
183 data
[0] = preinit_mt9m111
[i
][2];
184 data
[1] = preinit_mt9m111
[i
][3];
185 m5602_write_sensor(sd
,
186 preinit_mt9m111
[i
][1], data
, 2);
190 if (m5602_read_sensor(sd
, MT9M111_SC_CHIPVER
, data
, 2))
193 if ((data
[0] == 0x14) && (data
[1] == 0x3a)) {
194 info("Detected a mt9m111 sensor");
201 sensor_settings
= kmalloc(ARRAY_SIZE(mt9m111_ctrls
) * sizeof(s32
),
203 if (!sensor_settings
)
206 sd
->gspca_dev
.cam
.cam_mode
= mt9m111_modes
;
207 sd
->gspca_dev
.cam
.nmodes
= ARRAY_SIZE(mt9m111_modes
);
208 sd
->desc
->ctrls
= mt9m111_ctrls
;
209 sd
->desc
->nctrls
= ARRAY_SIZE(mt9m111_ctrls
);
211 for (i
= 0; i
< ARRAY_SIZE(mt9m111_ctrls
); i
++)
212 sensor_settings
[i
] = mt9m111_ctrls
[i
].qctrl
.default_value
;
213 sd
->sensor_priv
= sensor_settings
;
218 int mt9m111_init(struct sd
*sd
)
221 s32
*sensor_settings
= sd
->sensor_priv
;
223 /* Init the sensor */
224 for (i
= 0; i
< ARRAY_SIZE(init_mt9m111
) && !err
; i
++) {
227 if (init_mt9m111
[i
][0] == BRIDGE
) {
228 err
= m5602_write_bridge(sd
,
232 data
[0] = init_mt9m111
[i
][2];
233 data
[1] = init_mt9m111
[i
][3];
234 err
= m5602_write_sensor(sd
,
235 init_mt9m111
[i
][1], data
, 2);
240 mt9m111_dump_registers(sd
);
242 err
= mt9m111_set_vflip(&sd
->gspca_dev
, sensor_settings
[VFLIP_IDX
]);
246 err
= mt9m111_set_hflip(&sd
->gspca_dev
, sensor_settings
[HFLIP_IDX
]);
250 err
= mt9m111_set_green_balance(&sd
->gspca_dev
,
251 sensor_settings
[GREEN_BALANCE_IDX
]);
255 err
= mt9m111_set_blue_balance(&sd
->gspca_dev
,
256 sensor_settings
[BLUE_BALANCE_IDX
]);
260 err
= mt9m111_set_red_balance(&sd
->gspca_dev
,
261 sensor_settings
[RED_BALANCE_IDX
]);
265 return mt9m111_set_gain(&sd
->gspca_dev
, sensor_settings
[GAIN_IDX
]);
268 int mt9m111_start(struct sd
*sd
)
272 struct cam
*cam
= &sd
->gspca_dev
.cam
;
273 s32
*sensor_settings
= sd
->sensor_priv
;
275 int width
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].width
- 1;
276 int height
= cam
->cam_mode
[sd
->gspca_dev
.curr_mode
].height
;
278 for (i
= 0; i
< ARRAY_SIZE(start_mt9m111
) && !err
; i
++) {
279 if (start_mt9m111
[i
][0] == BRIDGE
) {
280 err
= m5602_write_bridge(sd
,
282 start_mt9m111
[i
][2]);
284 data
[0] = start_mt9m111
[i
][2];
285 data
[1] = start_mt9m111
[i
][3];
286 err
= m5602_write_sensor(sd
,
287 start_mt9m111
[i
][1], data
, 2);
293 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (height
>> 8) & 0xff);
297 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, (height
& 0xff));
301 for (i
= 0; i
< 2 && !err
; i
++)
302 err
= m5602_write_bridge(sd
, M5602_XB_VSYNC_PARA
, 0);
306 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0);
310 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 2);
314 for (i
= 0; i
< 2 && !err
; i
++)
315 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, 0);
319 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
,
320 (width
>> 8) & 0xff);
324 err
= m5602_write_bridge(sd
, M5602_XB_HSYNC_PARA
, width
& 0xff);
328 err
= m5602_write_bridge(sd
, M5602_XB_SIG_INI
, 0);
334 PDEBUG(D_V4L2
, "Configuring camera for VGA mode");
335 data
[0] = MT9M111_RMB_OVER_SIZED
;
336 data
[1] = MT9M111_RMB_ROW_SKIP_2X
|
337 MT9M111_RMB_COLUMN_SKIP_2X
|
338 (sensor_settings
[VFLIP_IDX
] << 0) |
339 (sensor_settings
[HFLIP_IDX
] << 1);
341 err
= m5602_write_sensor(sd
,
342 MT9M111_SC_R_MODE_CONTEXT_B
, data
, 2);
346 PDEBUG(D_V4L2
, "Configuring camera for QVGA mode");
347 data
[0] = MT9M111_RMB_OVER_SIZED
;
348 data
[1] = MT9M111_RMB_ROW_SKIP_4X
|
349 MT9M111_RMB_COLUMN_SKIP_4X
|
350 (sensor_settings
[VFLIP_IDX
] << 0) |
351 (sensor_settings
[HFLIP_IDX
] << 1);
352 err
= m5602_write_sensor(sd
,
353 MT9M111_SC_R_MODE_CONTEXT_B
, data
, 2);
359 void mt9m111_disconnect(struct sd
*sd
)
362 kfree(sd
->sensor_priv
);
365 static int mt9m111_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
367 struct sd
*sd
= (struct sd
*) gspca_dev
;
368 s32
*sensor_settings
= sd
->sensor_priv
;
370 *val
= sensor_settings
[VFLIP_IDX
];
371 PDEBUG(D_V4L2
, "Read vertical flip %d", *val
);
376 static int mt9m111_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
)
379 u8 data
[2] = {0x00, 0x00};
380 struct sd
*sd
= (struct sd
*) gspca_dev
;
381 s32
*sensor_settings
= sd
->sensor_priv
;
383 PDEBUG(D_V4L2
, "Set vertical flip to %d", val
);
385 sensor_settings
[VFLIP_IDX
] = val
;
387 /* The mt9m111 is flipped by default */
390 /* Set the correct page map */
391 err
= m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, data
, 2);
395 err
= m5602_read_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
, data
, 2);
399 data
[1] = (data
[1] & 0xfe) | val
;
400 err
= m5602_write_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
,
405 static int mt9m111_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
407 struct sd
*sd
= (struct sd
*) gspca_dev
;
408 s32
*sensor_settings
= sd
->sensor_priv
;
410 *val
= sensor_settings
[HFLIP_IDX
];
411 PDEBUG(D_V4L2
, "Read horizontal flip %d", *val
);
416 static int mt9m111_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
)
419 u8 data
[2] = {0x00, 0x00};
420 struct sd
*sd
= (struct sd
*) gspca_dev
;
421 s32
*sensor_settings
= sd
->sensor_priv
;
423 PDEBUG(D_V4L2
, "Set horizontal flip to %d", val
);
425 sensor_settings
[HFLIP_IDX
] = val
;
427 /* The mt9m111 is flipped by default */
430 /* Set the correct page map */
431 err
= m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, data
, 2);
435 err
= m5602_read_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
, data
, 2);
439 data
[1] = (data
[1] & 0xfd) | ((val
<< 1) & 0x02);
440 err
= m5602_write_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
,
445 static int mt9m111_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
447 struct sd
*sd
= (struct sd
*) gspca_dev
;
448 s32
*sensor_settings
= sd
->sensor_priv
;
450 *val
= sensor_settings
[GAIN_IDX
];
451 PDEBUG(D_V4L2
, "Read gain %d", *val
);
456 static int mt9m111_set_auto_white_balance(struct gspca_dev
*gspca_dev
,
459 struct sd
*sd
= (struct sd
*) gspca_dev
;
460 s32
*sensor_settings
= sd
->sensor_priv
;
464 err
= m5602_read_sensor(sd
, MT9M111_CP_OPERATING_MODE_CTL
, data
, 2);
468 sensor_settings
[AUTO_WHITE_BALANCE_IDX
] = val
& 0x01;
469 data
[1] = ((data
[1] & 0xfd) | ((val
& 0x01) << 1));
471 err
= m5602_write_sensor(sd
, MT9M111_CP_OPERATING_MODE_CTL
, data
, 2);
473 PDEBUG(D_V4L2
, "Set auto white balance %d", val
);
477 static int mt9m111_get_auto_white_balance(struct gspca_dev
*gspca_dev
,
479 struct sd
*sd
= (struct sd
*) gspca_dev
;
480 s32
*sensor_settings
= sd
->sensor_priv
;
482 *val
= sensor_settings
[AUTO_WHITE_BALANCE_IDX
];
483 PDEBUG(D_V4L2
, "Read auto white balance %d", *val
);
487 static int mt9m111_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
490 u8 data
[2] = {0x00, 0x00};
491 struct sd
*sd
= (struct sd
*) gspca_dev
;
492 s32
*sensor_settings
= sd
->sensor_priv
;
494 sensor_settings
[GAIN_IDX
] = val
;
496 /* Set the correct page map */
497 err
= m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, data
, 2);
501 if (val
>= INITIAL_MAX_GAIN
* 2 * 2 * 2)
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);
517 data
[1] = (tmp
& 0xff);
518 data
[0] = (tmp
& 0xff00) >> 8;
519 PDEBUG(D_V4L2
, "tmp=%d, data[1]=%d, data[0]=%d", tmp
,
522 err
= m5602_write_sensor(sd
, MT9M111_SC_GLOBAL_GAIN
,
528 static int mt9m111_set_green_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
532 struct sd
*sd
= (struct sd
*) gspca_dev
;
533 s32
*sensor_settings
= sd
->sensor_priv
;
535 sensor_settings
[GREEN_BALANCE_IDX
] = val
;
536 data
[1] = (val
& 0xff);
537 data
[0] = (val
& 0xff00) >> 8;
539 PDEBUG(D_V4L2
, "Set green balance %d", val
);
540 err
= m5602_write_sensor(sd
, MT9M111_SC_GREEN_1_GAIN
,
545 return m5602_write_sensor(sd
, MT9M111_SC_GREEN_2_GAIN
,
549 static int mt9m111_get_green_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
551 struct sd
*sd
= (struct sd
*) gspca_dev
;
552 s32
*sensor_settings
= sd
->sensor_priv
;
554 *val
= sensor_settings
[GREEN_BALANCE_IDX
];
555 PDEBUG(D_V4L2
, "Read green balance %d", *val
);
559 static int mt9m111_set_blue_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
562 struct sd
*sd
= (struct sd
*) gspca_dev
;
563 s32
*sensor_settings
= sd
->sensor_priv
;
565 sensor_settings
[BLUE_BALANCE_IDX
] = val
;
566 data
[1] = (val
& 0xff);
567 data
[0] = (val
& 0xff00) >> 8;
569 PDEBUG(D_V4L2
, "Set blue balance %d", val
);
571 return m5602_write_sensor(sd
, MT9M111_SC_BLUE_GAIN
,
575 static int mt9m111_get_blue_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
577 struct sd
*sd
= (struct sd
*) gspca_dev
;
578 s32
*sensor_settings
= sd
->sensor_priv
;
580 *val
= sensor_settings
[BLUE_BALANCE_IDX
];
581 PDEBUG(D_V4L2
, "Read blue balance %d", *val
);
585 static int mt9m111_set_red_balance(struct gspca_dev
*gspca_dev
, __s32 val
)
588 struct sd
*sd
= (struct sd
*) gspca_dev
;
589 s32
*sensor_settings
= sd
->sensor_priv
;
591 sensor_settings
[RED_BALANCE_IDX
] = val
;
592 data
[1] = (val
& 0xff);
593 data
[0] = (val
& 0xff00) >> 8;
595 PDEBUG(D_V4L2
, "Set red balance %d", val
);
597 return m5602_write_sensor(sd
, MT9M111_SC_RED_GAIN
,
601 static int mt9m111_get_red_balance(struct gspca_dev
*gspca_dev
, __s32
*val
)
603 struct sd
*sd
= (struct sd
*) gspca_dev
;
604 s32
*sensor_settings
= sd
->sensor_priv
;
606 *val
= sensor_settings
[RED_BALANCE_IDX
];
607 PDEBUG(D_V4L2
, "Read red balance %d", *val
);
611 static void mt9m111_dump_registers(struct sd
*sd
)
613 u8 address
, value
[2] = {0x00, 0x00};
615 info("Dumping the mt9m111 register state");
617 info("Dumping the mt9m111 sensor core registers");
618 value
[1] = MT9M111_SENSOR_CORE
;
619 m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, value
, 2);
620 for (address
= 0; address
< 0xff; address
++) {
621 m5602_read_sensor(sd
, address
, value
, 2);
622 info("register 0x%x contains 0x%x%x",
623 address
, value
[0], value
[1]);
626 info("Dumping the mt9m111 color pipeline registers");
627 value
[1] = MT9M111_COLORPIPE
;
628 m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, value
, 2);
629 for (address
= 0; address
< 0xff; address
++) {
630 m5602_read_sensor(sd
, address
, value
, 2);
631 info("register 0x%x contains 0x%x%x",
632 address
, value
[0], value
[1]);
635 info("Dumping the mt9m111 camera control registers");
636 value
[1] = MT9M111_CAMERA_CONTROL
;
637 m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, value
, 2);
638 for (address
= 0; address
< 0xff; address
++) {
639 m5602_read_sensor(sd
, address
, value
, 2);
640 info("register 0x%x contains 0x%x%x",
641 address
, value
[0], value
[1]);
644 info("mt9m111 register state dump complete");