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 | ||
658efb63 EA |
21 | static void mt9m111_dump_registers(struct sd *sd); |
22 | ||
c109f816 EA |
23 | int mt9m111_probe(struct sd *sd) |
24 | { | |
25 | u8 data[2] = {0x00, 0x00}; | |
26 | int i; | |
27 | ||
28 | if (force_sensor) { | |
29 | if (force_sensor == MT9M111_SENSOR) { | |
30 | info("Forcing a %s sensor", mt9m111.name); | |
31 | goto sensor_found; | |
32 | } | |
33 | /* If we want to force another sensor, don't try to probe this | |
34 | * one */ | |
35 | return -ENODEV; | |
36 | } | |
37 | ||
38 | info("Probing for a mt9m111 sensor"); | |
39 | ||
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]; | |
6dc4cff0 | 49 | m5602_write_sensor(sd, |
c109f816 EA |
50 | preinit_mt9m111[i][1], data, 2); |
51 | } | |
52 | } | |
53 | ||
4f93c450 | 54 | if (m5602_read_sensor(sd, MT9M111_SC_CHIPVER, data, 2)) |
c109f816 EA |
55 | return -ENODEV; |
56 | ||
57 | if ((data[0] == 0x14) && (data[1] == 0x3a)) { | |
58 | info("Detected a mt9m111 sensor"); | |
59 | goto sensor_found; | |
60 | } | |
61 | ||
62 | return -ENODEV; | |
63 | ||
64 | sensor_found: | |
65 | sd->gspca_dev.cam.cam_mode = mt9m111.modes; | |
66 | sd->gspca_dev.cam.nmodes = mt9m111.nmodes; | |
d2d7e9ae EA |
67 | sd->desc->ctrls = mt9m111.ctrls; |
68 | sd->desc->nctrls = mt9m111.nctrls; | |
c109f816 EA |
69 | return 0; |
70 | } | |
71 | ||
72 | int mt9m111_init(struct sd *sd) | |
73 | { | |
f1918fa2 | 74 | int i, err = 0; |
c109f816 EA |
75 | |
76 | /* Init the sensor */ | |
8bcbc911 | 77 | for (i = 0; i < ARRAY_SIZE(init_mt9m111) && !err; i++) { |
c109f816 EA |
78 | u8 data[2]; |
79 | ||
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]; | |
6dc4cff0 | 87 | err = m5602_write_sensor(sd, |
c109f816 EA |
88 | init_mt9m111[i][1], data, 2); |
89 | } | |
90 | } | |
91 | ||
92 | if (dump_sensor) | |
93 | mt9m111_dump_registers(sd); | |
94 | ||
95 | return (err < 0) ? err : 0; | |
96 | } | |
97 | ||
98 | int mt9m111_power_down(struct sd *sd) | |
99 | { | |
100 | return 0; | |
101 | } | |
102 | ||
103 | int mt9m111_get_vflip(struct gspca_dev *gspca_dev, __s32 *val) | |
104 | { | |
105 | int err; | |
106 | u8 data[2] = {0x00, 0x00}; | |
107 | struct sd *sd = (struct sd *) gspca_dev; | |
108 | ||
4f93c450 | 109 | err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, |
c109f816 EA |
110 | data, 2); |
111 | *val = data[0] & MT9M111_RMB_MIRROR_ROWS; | |
17ea88ae | 112 | PDEBUG(D_V4L2, "Read vertical flip %d", *val); |
c109f816 | 113 | |
8bcbc911 | 114 | return err; |
c109f816 EA |
115 | } |
116 | ||
117 | int mt9m111_set_vflip(struct gspca_dev *gspca_dev, __s32 val) | |
118 | { | |
119 | int err; | |
120 | u8 data[2] = {0x00, 0x00}; | |
121 | struct sd *sd = (struct sd *) gspca_dev; | |
122 | ||
17ea88ae | 123 | PDEBUG(D_V4L2, "Set vertical flip to %d", val); |
c109f816 EA |
124 | |
125 | /* Set the correct page map */ | |
6dc4cff0 | 126 | err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2); |
c109f816 EA |
127 | if (err < 0) |
128 | goto out; | |
129 | ||
4f93c450 | 130 | err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2); |
c109f816 EA |
131 | if (err < 0) |
132 | goto out; | |
133 | ||
134 | data[0] = (data[0] & 0xfe) | val; | |
6dc4cff0 | 135 | err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, |
c109f816 EA |
136 | data, 2); |
137 | out: | |
8bcbc911 | 138 | return err; |
c109f816 EA |
139 | } |
140 | ||
141 | int mt9m111_get_hflip(struct gspca_dev *gspca_dev, __s32 *val) | |
142 | { | |
143 | int err; | |
144 | u8 data[2] = {0x00, 0x00}; | |
145 | struct sd *sd = (struct sd *) gspca_dev; | |
146 | ||
4f93c450 | 147 | err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, |
c109f816 EA |
148 | data, 2); |
149 | *val = data[0] & MT9M111_RMB_MIRROR_COLS; | |
17ea88ae | 150 | PDEBUG(D_V4L2, "Read horizontal flip %d", *val); |
c109f816 | 151 | |
8bcbc911 | 152 | return err; |
c109f816 EA |
153 | } |
154 | ||
155 | int mt9m111_set_hflip(struct gspca_dev *gspca_dev, __s32 val) | |
156 | { | |
157 | int err; | |
158 | u8 data[2] = {0x00, 0x00}; | |
159 | struct sd *sd = (struct sd *) gspca_dev; | |
160 | ||
17ea88ae | 161 | PDEBUG(D_V4L2, "Set horizontal flip to %d", val); |
c109f816 EA |
162 | |
163 | /* Set the correct page map */ | |
6dc4cff0 | 164 | err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2); |
c109f816 EA |
165 | if (err < 0) |
166 | goto out; | |
167 | ||
4f93c450 | 168 | err = m5602_read_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, data, 2); |
c109f816 EA |
169 | if (err < 0) |
170 | goto out; | |
171 | ||
172 | data[0] = (data[0] & 0xfd) | ((val << 1) & 0x02); | |
6dc4cff0 | 173 | err = m5602_write_sensor(sd, MT9M111_SC_R_MODE_CONTEXT_B, |
c109f816 EA |
174 | data, 2); |
175 | out: | |
8bcbc911 | 176 | return err; |
c109f816 EA |
177 | } |
178 | ||
179 | int mt9m111_get_gain(struct gspca_dev *gspca_dev, __s32 *val) | |
180 | { | |
181 | int err, tmp; | |
182 | u8 data[2] = {0x00, 0x00}; | |
183 | struct sd *sd = (struct sd *) gspca_dev; | |
184 | ||
4f93c450 | 185 | err = m5602_read_sensor(sd, MT9M111_SC_GLOBAL_GAIN, data, 2); |
c109f816 EA |
186 | tmp = ((data[1] << 8) | data[0]); |
187 | ||
188 | *val = ((tmp & (1 << 10)) * 2) | | |
189 | ((tmp & (1 << 9)) * 2) | | |
190 | ((tmp & (1 << 8)) * 2) | | |
191 | (tmp & 0x7f); | |
192 | ||
17ea88ae | 193 | PDEBUG(D_V4L2, "Read gain %d", *val); |
c109f816 | 194 | |
8bcbc911 | 195 | return err; |
c109f816 EA |
196 | } |
197 | ||
198 | int mt9m111_set_gain(struct gspca_dev *gspca_dev, __s32 val) | |
199 | { | |
200 | int err, tmp; | |
201 | u8 data[2] = {0x00, 0x00}; | |
202 | struct sd *sd = (struct sd *) gspca_dev; | |
203 | ||
204 | /* Set the correct page map */ | |
6dc4cff0 | 205 | err = m5602_write_sensor(sd, MT9M111_PAGE_MAP, data, 2); |
c109f816 EA |
206 | if (err < 0) |
207 | goto out; | |
208 | ||
209 | if (val >= INITIAL_MAX_GAIN * 2 * 2 * 2) | |
210 | return -EINVAL; | |
211 | ||
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); | |
222 | else | |
223 | tmp = val; | |
224 | ||
225 | data[1] = (tmp & 0xff00) >> 8; | |
226 | data[0] = (tmp & 0xff); | |
17ea88ae | 227 | PDEBUG(D_V4L2, "tmp=%d, data[1]=%d, data[0]=%d", tmp, |
c109f816 EA |
228 | data[1], data[0]); |
229 | ||
6dc4cff0 | 230 | err = m5602_write_sensor(sd, MT9M111_SC_GLOBAL_GAIN, |
c109f816 EA |
231 | data, 2); |
232 | out: | |
8bcbc911 | 233 | return err; |
c109f816 EA |
234 | } |
235 | ||
658efb63 | 236 | static void mt9m111_dump_registers(struct sd *sd) |
c109f816 EA |
237 | { |
238 | u8 address, value[2] = {0x00, 0x00}; | |
239 | ||
240 | info("Dumping the mt9m111 register state"); | |
241 | ||
242 | info("Dumping the mt9m111 sensor core registers"); | |
243 | value[1] = MT9M111_SENSOR_CORE; | |
6dc4cff0 | 244 | m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2); |
c109f816 | 245 | for (address = 0; address < 0xff; address++) { |
4f93c450 | 246 | m5602_read_sensor(sd, address, value, 2); |
c109f816 EA |
247 | info("register 0x%x contains 0x%x%x", |
248 | address, value[0], value[1]); | |
249 | } | |
250 | ||
251 | info("Dumping the mt9m111 color pipeline registers"); | |
252 | value[1] = MT9M111_COLORPIPE; | |
6dc4cff0 | 253 | m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2); |
c109f816 | 254 | for (address = 0; address < 0xff; address++) { |
4f93c450 | 255 | m5602_read_sensor(sd, address, value, 2); |
c109f816 EA |
256 | info("register 0x%x contains 0x%x%x", |
257 | address, value[0], value[1]); | |
258 | } | |
259 | ||
260 | info("Dumping the mt9m111 camera control registers"); | |
261 | value[1] = MT9M111_CAMERA_CONTROL; | |
6dc4cff0 | 262 | m5602_write_sensor(sd, MT9M111_PAGE_MAP, value, 2); |
c109f816 | 263 | for (address = 0; address < 0xff; address++) { |
4f93c450 | 264 | m5602_read_sensor(sd, address, value, 2); |
c109f816 EA |
265 | info("register 0x%x contains 0x%x%x", |
266 | address, value[0], value[1]); | |
267 | } | |
268 | ||
269 | info("mt9m111 register state dump complete"); | |
270 | } |