V4L/DVB (11411): gspca - m5602: Rework v4l ctrl handling in all sensors
[linux-2.6/mini2440.git] / drivers / media / video / gspca / m5602 / m5602_mt9m111.c
blob69f8f6ce5811e76feacee62fef7edcf0fb5e3b07
1 /*
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};
26 int i;
28 if (force_sensor) {
29 if (force_sensor == MT9M111_SENSOR) {
30 info("Forcing a %s sensor", mt9m111.name);
31 goto sensor_found;
33 /* If we want to force another sensor, don't try to probe this
34 * one */
35 return -ENODEV;
38 info("Probing for a mt9m111 sensor");
40 /* Do the preinit */
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]);
46 } else {
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))
55 return -ENODEV;
57 if ((data[0] == 0x14) && (data[1] == 0x3a)) {
58 info("Detected a mt9m111 sensor");
59 goto sensor_found;
62 return -ENODEV;
64 sensor_found:
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);
69 return 0;
72 int mt9m111_init(struct sd *sd)
74 int i, err = 0;
76 /* Init the sensor */
77 for (i = 0; i < ARRAY_SIZE(init_mt9m111) && !err; i++) {
78 u8 data[2];
80 if (init_mt9m111[i][0] == BRIDGE) {
81 err = m5602_write_bridge(sd,
82 init_mt9m111[i][1],
83 init_mt9m111[i][2]);
84 } else {
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);
92 if (dump_sensor)
93 mt9m111_dump_registers(sd);
95 return (err < 0) ? err : 0;
98 int mt9m111_power_down(struct sd *sd)
100 return 0;
103 int mt9m111_get_vflip(struct gspca_dev *gspca_dev, __s32 *val)
105 int err;
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,
110 data, 2);
111 *val = data[0] & MT9M111_RMB_MIRROR_ROWS;
112 PDEBUG(D_V4L2, "Read vertical flip %d", *val);
114 return err;
117 int mt9m111_set_vflip(struct gspca_dev *gspca_dev, __s32 val)
119 int err;
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);
127 if (err < 0)
128 return err;
130 err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2);
131 if (err < 0)
132 return err;
134 data[0] = (data[0] & 0xfe) | val;
135 err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
136 data, 2);
137 return err;
140 int mt9m111_get_hflip(struct gspca_dev *gspca_dev, __s32 *val)
142 int err;
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,
147 data, 2);
148 *val = data[0] & MT9M111_RMB_MIRROR_COLS;
149 PDEBUG(D_V4L2, "Read horizontal flip %d", *val);
151 return err;
154 int mt9m111_set_hflip(struct gspca_dev *gspca_dev, __s32 val)
156 int err;
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);
164 if (err < 0)
165 return err;
167 err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2);
168 if (err < 0)
169 return err;
171 data[0] = (data[0] & 0xfd) | ((val << 1) & 0x02);
172 err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B,
173 data, 2);
174 return err;
177 int mt9m111_get_gain(struct gspca_dev *gspca_dev, __s32 *val)
179 int err, tmp;
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) |
189 (tmp & 0x7f);
191 PDEBUG(D_V4L2, "Read gain %d", *val);
193 return err;
196 int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val)
198 int err, tmp;
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);
204 if (err < 0)
205 return err;
207 if (val >= INITIAL_MAX_GAIN * 2 * 2 * 2)
208 return -EINVAL;
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);
220 else
221 tmp = val;
223 data[1] = (tmp & 0xff00) >> 8;
224 data[0] = (tmp & 0xff);
225 PDEBUG(D_V4L2, "tmp=%d, data[1]=%d, data[0]=%d", tmp,
226 data[1], data[0]);
228 err = m5602_write_sensor(sd, MT9M111_SC_GLOBAL_GAIN,
229 data, 2);
231 return err;
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");