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 = mt9m111.nctrls;
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,
141 int mt9m111_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
144 u8 data[2] = {0x00, 0x00};
145 struct sd *sd = (struct sd *) gspca_dev;
147 err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
149 *val = data[0] & MT9M111_RMB_MIRROR_COLS;
150 PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
155 int mt9m111_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
158 u8 data[2] = {0x00, 0x00};
159 struct sd *sd = (struct sd *) gspca_dev;
161 PDEBUG(D_V4L2, "Set horizontal flip to %d", val);
163 /* Set the correct page map */
164 err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
168 err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2);
172 data[0] = (data[0] & 0xfd) | ((val << 1) & 0x02);
173 err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
179 int mt9m111_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
182 u8 data[2] = {0x00, 0x00};
183 struct sd *sd = (struct sd *) gspca_dev;
185 err = m5602_read_sensor(sd, MT9M111_SC_GLOBAL_GAIN, data, 2);
186 tmp = ((data[1] << 8) | data[0]);
188 *val = ((tmp & (1 << 10)) * 2) |
189 ((tmp & (1 << 9)) * 2) |
190 ((tmp & (1 << 8)) * 2) |
193 PDEBUG(D_V4L2, "Read gain %d", *val);
198 int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val)
201 u8 data[2] = {0x00, 0x00};
202 struct sd *sd = (struct sd *) gspca_dev;
204 /* Set the correct page map */
205 err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2);
209 if (val >= INITIAL_MAX_GAIN * 2 * 2 * 2)
212 if ((val >= INITIAL_MAX_GAIN * 2 * 2) &&
213 (val < (INITIAL_MAX_GAIN - 1) * 2 * 2 * 2))
214 tmp = (1 << 10) | (val << 9) |
215 (val << 8) | (val / 8);
216 else if ((val >= INITIAL_MAX_GAIN * 2) &&
217 (val < INITIAL_MAX_GAIN * 2 * 2))
218 tmp = (1 << 9) | (1 << 8) | (val / 4);
219 else if ((val >= INITIAL_MAX_GAIN) &&
220 (val < INITIAL_MAX_GAIN * 2))
221 tmp = (1 << 8) | (val / 2);
225 data[1] = (tmp & 0xff00) >> 8;
226 data[0] = (tmp & 0xff);
227 PDEBUG(D_V4L2, "tmp=%d, data[1]=%d, data[0]=%d", tmp,
230 err = m5602_write_sensor(sd, MT9M111_SC_GLOBAL_GAIN,
236 static void mt9m111_dump_registers(struct sd *sd)
238 u8 address, value[2] = {0x00, 0x00};
240 info("Dumping the mt9m111 register state");
242 info("Dumping the mt9m111 sensor core registers");
243 value[1] = MT9M111_SENSOR_CORE;
244 m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
245 for (address = 0; address < 0xff; address++) {
246 m5602_read_sensor(sd, address, value, 2);
247 info("register 0x%x contains 0x%x%x",
248 address, value[0], value[1]);
251 info("Dumping the mt9m111 color pipeline registers");
252 value[1] = MT9M111_COLORPIPE;
253 m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
254 for (address = 0; address < 0xff; address++) {
255 m5602_read_sensor(sd, address, value, 2);
256 info("register 0x%x contains 0x%x%x",
257 address, value[0], value[1]);
260 info("Dumping the mt9m111 camera control registers");
261 value[1] = MT9M111_CAMERA_CONTROL;
262 m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2);
263 for (address = 0; address < 0xff; address++) {
264 m5602_read_sensor(sd, address, value, 2);
265 info("register 0x%x contains 0x%x%x",
266 address, value[0], value[1]);
269 info("mt9m111 register state dump complete");