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 void mt9m111_dump_registers(struct sd
*sd
);
23 int mt9m111_probe(struct sd
*sd
)
25 u8 data
[2] = {0x00, 0x00};
29 if (force_sensor
== MT9M111_SENSOR
) {
30 info("Forcing a %s sensor", mt9m111
.name
);
33 /* If we want to force another sensor, don't try to probe this
38 info("Probing for a mt9m111 sensor");
41 for (i
= 0; i
< ARRAY_SIZE(preinit_mt9m111
); i
++) {
42 if (preinit_mt9m111
[i
][0] == BRIDGE
) {
43 m5602_write_bridge(sd
,
44 preinit_mt9m111
[i
][1],
45 preinit_mt9m111
[i
][2]);
47 data
[0] = preinit_mt9m111
[i
][2];
48 data
[1] = preinit_mt9m111
[i
][3];
49 m5602_write_sensor(sd
,
50 preinit_mt9m111
[i
][1], data
, 2);
54 if (m5602_read_sensor(sd
, MT9M111_SC_CHIPVER
, data
, 2))
57 if ((data
[0] == 0x14) && (data
[1] == 0x3a)) {
58 info("Detected a mt9m111 sensor");
65 sd
->gspca_dev
.cam
.cam_mode
= mt9m111
.modes
;
66 sd
->gspca_dev
.cam
.nmodes
= mt9m111
.nmodes
;
67 sd
->desc
->ctrls
= mt9m111
.ctrls
;
68 sd
->desc
->nctrls
= ARRAY_SIZE(mt9m111_ctrls
);
72 int mt9m111_init(struct sd
*sd
)
77 for (i
= 0; i
< ARRAY_SIZE(init_mt9m111
) && !err
; i
++) {
80 if (init_mt9m111
[i
][0] == BRIDGE
) {
81 err
= m5602_write_bridge(sd
,
85 data
[0] = init_mt9m111
[i
][2];
86 data
[1] = init_mt9m111
[i
][3];
87 err
= m5602_write_sensor(sd
,
88 init_mt9m111
[i
][1], data
, 2);
93 mt9m111_dump_registers(sd
);
95 return (err
< 0) ? err
: 0;
98 int mt9m111_power_down(struct sd
*sd
)
103 int mt9m111_get_vflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
106 u8 data
[2] = {0x00, 0x00};
107 struct sd
*sd
= (struct sd
*) gspca_dev
;
109 err
= m5602_read_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
,
111 *val
= data
[0] & MT9M111_RMB_MIRROR_ROWS
;
112 PDEBUG(D_V4L2
, "Read vertical flip %d", *val
);
117 int mt9m111_set_vflip(struct gspca_dev
*gspca_dev
, __s32 val
)
120 u8 data
[2] = {0x00, 0x00};
121 struct sd
*sd
= (struct sd
*) gspca_dev
;
123 PDEBUG(D_V4L2
, "Set vertical flip to %d", val
);
125 /* Set the correct page map */
126 err
= m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, data
, 2);
130 err
= m5602_read_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
, data
, 2);
134 data
[0] = (data
[0] & 0xfe) | val
;
135 err
= m5602_write_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
,
140 int mt9m111_get_hflip(struct gspca_dev
*gspca_dev
, __s32
*val
)
143 u8 data
[2] = {0x00, 0x00};
144 struct sd
*sd
= (struct sd
*) gspca_dev
;
146 err
= m5602_read_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
,
148 *val
= data
[0] & MT9M111_RMB_MIRROR_COLS
;
149 PDEBUG(D_V4L2
, "Read horizontal flip %d", *val
);
154 int mt9m111_set_hflip(struct gspca_dev
*gspca_dev
, __s32 val
)
157 u8 data
[2] = {0x00, 0x00};
158 struct sd
*sd
= (struct sd
*) gspca_dev
;
160 PDEBUG(D_V4L2
, "Set horizontal flip to %d", val
);
162 /* Set the correct page map */
163 err
= m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, data
, 2);
167 err
= m5602_read_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
, data
, 2);
171 data
[0] = (data
[0] & 0xfd) | ((val
<< 1) & 0x02);
172 err
= m5602_write_sensor(sd
, MT9M111_SC_R_MODE_CONTEXT_B
,
177 int mt9m111_get_gain(struct gspca_dev
*gspca_dev
, __s32
*val
)
180 u8 data
[2] = {0x00, 0x00};
181 struct sd
*sd
= (struct sd
*) gspca_dev
;
183 err
= m5602_read_sensor(sd
, MT9M111_SC_GLOBAL_GAIN
, data
, 2);
184 tmp
= ((data
[1] << 8) | data
[0]);
186 *val
= ((tmp
& (1 << 10)) * 2) |
187 ((tmp
& (1 << 9)) * 2) |
188 ((tmp
& (1 << 8)) * 2) |
191 PDEBUG(D_V4L2
, "Read gain %d", *val
);
196 int mt9m111_set_gain(struct gspca_dev
*gspca_dev
, __s32 val
)
199 u8 data
[2] = {0x00, 0x00};
200 struct sd
*sd
= (struct sd
*) gspca_dev
;
202 /* Set the correct page map */
203 err
= m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, data
, 2);
207 if (val
>= INITIAL_MAX_GAIN
* 2 * 2 * 2)
210 if ((val
>= INITIAL_MAX_GAIN
* 2 * 2) &&
211 (val
< (INITIAL_MAX_GAIN
- 1) * 2 * 2 * 2))
212 tmp
= (1 << 10) | (val
<< 9) |
213 (val
<< 8) | (val
/ 8);
214 else if ((val
>= INITIAL_MAX_GAIN
* 2) &&
215 (val
< INITIAL_MAX_GAIN
* 2 * 2))
216 tmp
= (1 << 9) | (1 << 8) | (val
/ 4);
217 else if ((val
>= INITIAL_MAX_GAIN
) &&
218 (val
< INITIAL_MAX_GAIN
* 2))
219 tmp
= (1 << 8) | (val
/ 2);
223 data
[1] = (tmp
& 0xff00) >> 8;
224 data
[0] = (tmp
& 0xff);
225 PDEBUG(D_V4L2
, "tmp=%d, data[1]=%d, data[0]=%d", tmp
,
228 err
= m5602_write_sensor(sd
, MT9M111_SC_GLOBAL_GAIN
,
234 static void mt9m111_dump_registers(struct sd
*sd
)
236 u8 address
, value
[2] = {0x00, 0x00};
238 info("Dumping the mt9m111 register state");
240 info("Dumping the mt9m111 sensor core registers");
241 value
[1] = MT9M111_SENSOR_CORE
;
242 m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, value
, 2);
243 for (address
= 0; address
< 0xff; address
++) {
244 m5602_read_sensor(sd
, address
, value
, 2);
245 info("register 0x%x contains 0x%x%x",
246 address
, value
[0], value
[1]);
249 info("Dumping the mt9m111 color pipeline registers");
250 value
[1] = MT9M111_COLORPIPE
;
251 m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, value
, 2);
252 for (address
= 0; address
< 0xff; address
++) {
253 m5602_read_sensor(sd
, address
, value
, 2);
254 info("register 0x%x contains 0x%x%x",
255 address
, value
[0], value
[1]);
258 info("Dumping the mt9m111 camera control registers");
259 value
[1] = MT9M111_CAMERA_CONTROL
;
260 m5602_write_sensor(sd
, MT9M111_PAGE_MAP
, value
, 2);
261 for (address
= 0; address
< 0xff; address
++) {
262 m5602_read_sensor(sd
, address
, value
, 2);
263 info("register 0x%x contains 0x%x%x",
264 address
, value
[0], value
[1]);
267 info("mt9m111 register state dump complete");