]>
Commit | Line | Data |
---|---|---|
f523dd0d GL |
1 | /* |
2 | * Driver for MT9M001 CMOS Image Sensor from Micron | |
3 | * | |
4 | * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License version 2 as | |
8 | * published by the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/videodev2.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/i2c.h> | |
14 | #include <linux/log2.h> | |
15 | ||
16 | #include <media/v4l2-common.h> | |
17 | #include <media/v4l2-chip-ident.h> | |
18 | #include <media/soc_camera.h> | |
19 | ||
f523dd0d GL |
20 | /* mt9m001 i2c address 0x5d |
21 | * The platform has to define i2c_board_info | |
22 | * and call i2c_register_board_info() */ | |
23 | ||
24 | /* mt9m001 selected register addresses */ | |
25 | #define MT9M001_CHIP_VERSION 0x00 | |
26 | #define MT9M001_ROW_START 0x01 | |
27 | #define MT9M001_COLUMN_START 0x02 | |
28 | #define MT9M001_WINDOW_HEIGHT 0x03 | |
29 | #define MT9M001_WINDOW_WIDTH 0x04 | |
30 | #define MT9M001_HORIZONTAL_BLANKING 0x05 | |
31 | #define MT9M001_VERTICAL_BLANKING 0x06 | |
32 | #define MT9M001_OUTPUT_CONTROL 0x07 | |
33 | #define MT9M001_SHUTTER_WIDTH 0x09 | |
34 | #define MT9M001_FRAME_RESTART 0x0b | |
35 | #define MT9M001_SHUTTER_DELAY 0x0c | |
36 | #define MT9M001_RESET 0x0d | |
37 | #define MT9M001_READ_OPTIONS1 0x1e | |
38 | #define MT9M001_READ_OPTIONS2 0x20 | |
39 | #define MT9M001_GLOBAL_GAIN 0x35 | |
40 | #define MT9M001_CHIP_ENABLE 0xF1 | |
41 | ||
42 | static const struct soc_camera_data_format mt9m001_colour_formats[] = { | |
bb55de3b GL |
43 | /* Order important: first natively supported, |
44 | * second supported with a GPIO extender */ | |
f523dd0d | 45 | { |
bb55de3b GL |
46 | .name = "Bayer (sRGB) 10 bit", |
47 | .depth = 10, | |
48 | .fourcc = V4L2_PIX_FMT_SBGGR16, | |
49 | .colorspace = V4L2_COLORSPACE_SRGB, | |
50 | }, { | |
51 | .name = "Bayer (sRGB) 8 bit", | |
52 | .depth = 8, | |
f523dd0d GL |
53 | .fourcc = V4L2_PIX_FMT_SBGGR8, |
54 | .colorspace = V4L2_COLORSPACE_SRGB, | |
55 | } | |
56 | }; | |
57 | ||
58 | static const struct soc_camera_data_format mt9m001_monochrome_formats[] = { | |
bb55de3b | 59 | /* Order important - see above */ |
f523dd0d GL |
60 | { |
61 | .name = "Monochrome 10 bit", | |
62 | .depth = 10, | |
63 | .fourcc = V4L2_PIX_FMT_Y16, | |
64 | }, { | |
65 | .name = "Monochrome 8 bit", | |
66 | .depth = 8, | |
67 | .fourcc = V4L2_PIX_FMT_GREY, | |
68 | }, | |
69 | }; | |
70 | ||
71 | struct mt9m001 { | |
72 | struct i2c_client *client; | |
73 | struct soc_camera_device icd; | |
74 | int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ | |
f523dd0d | 75 | unsigned char autoexposure; |
f523dd0d GL |
76 | }; |
77 | ||
78 | static int reg_read(struct soc_camera_device *icd, const u8 reg) | |
79 | { | |
80 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
81 | struct i2c_client *client = mt9m001->client; | |
82 | s32 data = i2c_smbus_read_word_data(client, reg); | |
83 | return data < 0 ? data : swab16(data); | |
84 | } | |
85 | ||
86 | static int reg_write(struct soc_camera_device *icd, const u8 reg, | |
87 | const u16 data) | |
88 | { | |
89 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
90 | return i2c_smbus_write_word_data(mt9m001->client, reg, swab16(data)); | |
91 | } | |
92 | ||
93 | static int reg_set(struct soc_camera_device *icd, const u8 reg, | |
94 | const u16 data) | |
95 | { | |
96 | int ret; | |
97 | ||
98 | ret = reg_read(icd, reg); | |
99 | if (ret < 0) | |
100 | return ret; | |
101 | return reg_write(icd, reg, ret | data); | |
102 | } | |
103 | ||
104 | static int reg_clear(struct soc_camera_device *icd, const u8 reg, | |
105 | const u16 data) | |
106 | { | |
107 | int ret; | |
108 | ||
109 | ret = reg_read(icd, reg); | |
110 | if (ret < 0) | |
111 | return ret; | |
112 | return reg_write(icd, reg, ret & ~data); | |
113 | } | |
114 | ||
115 | static int mt9m001_init(struct soc_camera_device *icd) | |
116 | { | |
81034663 SH |
117 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
118 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; | |
f523dd0d GL |
119 | int ret; |
120 | ||
5e85e732 | 121 | dev_dbg(icd->vdev->parent, "%s\n", __func__); |
f523dd0d | 122 | |
81034663 SH |
123 | if (icl->power) { |
124 | ret = icl->power(&mt9m001->client->dev, 1); | |
125 | if (ret < 0) { | |
126 | dev_err(icd->vdev->parent, | |
127 | "Platform failed to power-on the camera.\n"); | |
128 | return ret; | |
129 | } | |
130 | } | |
131 | ||
132 | /* The camera could have been already on, we reset it additionally */ | |
133 | if (icl->reset) | |
134 | ret = icl->reset(&mt9m001->client->dev); | |
135 | else | |
136 | ret = -ENODEV; | |
137 | ||
138 | if (ret < 0) { | |
139 | /* Either no platform reset, or platform reset failed */ | |
140 | ret = reg_write(icd, MT9M001_RESET, 1); | |
141 | if (!ret) | |
142 | ret = reg_write(icd, MT9M001_RESET, 0); | |
143 | } | |
11211641 GL |
144 | /* Disable chip, synchronous option update */ |
145 | if (!ret) | |
f523dd0d GL |
146 | ret = reg_write(icd, MT9M001_OUTPUT_CONTROL, 0); |
147 | ||
11211641 | 148 | return ret; |
f523dd0d GL |
149 | } |
150 | ||
151 | static int mt9m001_release(struct soc_camera_device *icd) | |
152 | { | |
81034663 SH |
153 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); |
154 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; | |
155 | ||
f523dd0d GL |
156 | /* Disable the chip */ |
157 | reg_write(icd, MT9M001_OUTPUT_CONTROL, 0); | |
81034663 SH |
158 | |
159 | if (icl->power) | |
160 | icl->power(&mt9m001->client->dev, 0); | |
161 | ||
f523dd0d GL |
162 | return 0; |
163 | } | |
164 | ||
165 | static int mt9m001_start_capture(struct soc_camera_device *icd) | |
166 | { | |
167 | /* Switch to master "normal" mode */ | |
168 | if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 2) < 0) | |
169 | return -EIO; | |
170 | return 0; | |
171 | } | |
172 | ||
173 | static int mt9m001_stop_capture(struct soc_camera_device *icd) | |
174 | { | |
175 | /* Stop sensor readout */ | |
176 | if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 0) < 0) | |
177 | return -EIO; | |
178 | return 0; | |
179 | } | |
180 | ||
ad5f2e85 GL |
181 | static int mt9m001_set_bus_param(struct soc_camera_device *icd, |
182 | unsigned long flags) | |
f523dd0d GL |
183 | { |
184 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
36034dc3 SH |
185 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
186 | unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK; | |
f523dd0d | 187 | |
36034dc3 SH |
188 | /* Only one width bit may be set */ |
189 | if (!is_power_of_2(width_flag)) | |
190 | return -EINVAL; | |
f523dd0d | 191 | |
36034dc3 SH |
192 | if (icl->set_bus_param) |
193 | return icl->set_bus_param(icl, width_flag); | |
f523dd0d | 194 | |
36034dc3 SH |
195 | /* |
196 | * Without board specific bus width settings we only support the | |
197 | * sensors native bus width | |
198 | */ | |
199 | if (width_flag == SOCAM_DATAWIDTH_10) | |
200 | return 0; | |
f523dd0d | 201 | |
36034dc3 | 202 | return -EINVAL; |
ad5f2e85 GL |
203 | } |
204 | ||
205 | static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd) | |
206 | { | |
207 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
bd73b36f GL |
208 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
209 | /* MT9M001 has all capture_format parameters fixed */ | |
e951cbf2 | 210 | unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING | |
bd73b36f | 211 | SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH | |
2d9329f3 | 212 | SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER; |
ad5f2e85 | 213 | |
36034dc3 SH |
214 | if (icl->query_bus_param) |
215 | flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK; | |
216 | else | |
217 | flags |= SOCAM_DATAWIDTH_10; | |
ad5f2e85 | 218 | |
bd73b36f | 219 | return soc_camera_apply_sensor_flags(icl, flags); |
ad5f2e85 GL |
220 | } |
221 | ||
09e231b3 GL |
222 | static int mt9m001_set_crop(struct soc_camera_device *icd, |
223 | struct v4l2_rect *rect) | |
ad5f2e85 GL |
224 | { |
225 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
226 | int ret; | |
227 | const u16 hblank = 9, vblank = 25; | |
228 | ||
f523dd0d GL |
229 | /* Blanking and start values - default... */ |
230 | ret = reg_write(icd, MT9M001_HORIZONTAL_BLANKING, hblank); | |
11211641 | 231 | if (!ret) |
f523dd0d GL |
232 | ret = reg_write(icd, MT9M001_VERTICAL_BLANKING, vblank); |
233 | ||
234 | /* The caller provides a supported format, as verified per | |
d8fac217 | 235 | * call to icd->try_fmt() */ |
11211641 | 236 | if (!ret) |
f523dd0d | 237 | ret = reg_write(icd, MT9M001_COLUMN_START, rect->left); |
11211641 | 238 | if (!ret) |
f523dd0d | 239 | ret = reg_write(icd, MT9M001_ROW_START, rect->top); |
11211641 | 240 | if (!ret) |
f523dd0d | 241 | ret = reg_write(icd, MT9M001_WINDOW_WIDTH, rect->width - 1); |
11211641 | 242 | if (!ret) |
f523dd0d GL |
243 | ret = reg_write(icd, MT9M001_WINDOW_HEIGHT, |
244 | rect->height + icd->y_skip_top - 1); | |
11211641 | 245 | if (!ret && mt9m001->autoexposure) { |
f523dd0d GL |
246 | ret = reg_write(icd, MT9M001_SHUTTER_WIDTH, |
247 | rect->height + icd->y_skip_top + vblank); | |
11211641 | 248 | if (!ret) { |
f523dd0d GL |
249 | const struct v4l2_queryctrl *qctrl = |
250 | soc_camera_find_qctrl(icd->ops, | |
251 | V4L2_CID_EXPOSURE); | |
252 | icd->exposure = (524 + (rect->height + icd->y_skip_top + | |
253 | vblank - 1) * | |
254 | (qctrl->maximum - qctrl->minimum)) / | |
255 | 1048 + qctrl->minimum; | |
256 | } | |
257 | } | |
258 | ||
11211641 | 259 | return ret; |
f523dd0d GL |
260 | } |
261 | ||
09e231b3 GL |
262 | static int mt9m001_set_fmt(struct soc_camera_device *icd, |
263 | struct v4l2_format *f) | |
264 | { | |
265 | struct v4l2_rect rect = { | |
266 | .left = icd->x_current, | |
267 | .top = icd->y_current, | |
268 | .width = f->fmt.pix.width, | |
269 | .height = f->fmt.pix.height, | |
270 | }; | |
271 | ||
272 | /* No support for scaling so far, just crop. TODO: use skipping */ | |
273 | return mt9m001_set_crop(icd, &rect); | |
274 | } | |
275 | ||
d8fac217 GL |
276 | static int mt9m001_try_fmt(struct soc_camera_device *icd, |
277 | struct v4l2_format *f) | |
f523dd0d | 278 | { |
64f5905e GL |
279 | struct v4l2_pix_format *pix = &f->fmt.pix; |
280 | ||
281 | if (pix->height < 32 + icd->y_skip_top) | |
282 | pix->height = 32 + icd->y_skip_top; | |
283 | if (pix->height > 1024 + icd->y_skip_top) | |
284 | pix->height = 1024 + icd->y_skip_top; | |
285 | if (pix->width < 48) | |
286 | pix->width = 48; | |
287 | if (pix->width > 1280) | |
288 | pix->width = 1280; | |
289 | pix->width &= ~0x01; /* has to be even, unsure why was ~3 */ | |
f523dd0d GL |
290 | |
291 | return 0; | |
292 | } | |
293 | ||
294 | static int mt9m001_get_chip_id(struct soc_camera_device *icd, | |
aecde8b5 | 295 | struct v4l2_dbg_chip_ident *id) |
f523dd0d GL |
296 | { |
297 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
298 | ||
aecde8b5 | 299 | if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR) |
f523dd0d GL |
300 | return -EINVAL; |
301 | ||
aecde8b5 | 302 | if (id->match.addr != mt9m001->client->addr) |
f523dd0d GL |
303 | return -ENODEV; |
304 | ||
305 | id->ident = mt9m001->model; | |
306 | id->revision = 0; | |
307 | ||
308 | return 0; | |
309 | } | |
310 | ||
311 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
312 | static int mt9m001_get_register(struct soc_camera_device *icd, | |
aecde8b5 | 313 | struct v4l2_dbg_register *reg) |
f523dd0d GL |
314 | { |
315 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
316 | ||
aecde8b5 | 317 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
318 | return -EINVAL; |
319 | ||
aecde8b5 | 320 | if (reg->match.addr != mt9m001->client->addr) |
f523dd0d GL |
321 | return -ENODEV; |
322 | ||
aecde8b5 | 323 | reg->size = 2; |
f523dd0d GL |
324 | reg->val = reg_read(icd, reg->reg); |
325 | ||
326 | if (reg->val > 0xffff) | |
327 | return -EIO; | |
328 | ||
329 | return 0; | |
330 | } | |
331 | ||
332 | static int mt9m001_set_register(struct soc_camera_device *icd, | |
aecde8b5 | 333 | struct v4l2_dbg_register *reg) |
f523dd0d GL |
334 | { |
335 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
336 | ||
aecde8b5 | 337 | if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff) |
f523dd0d GL |
338 | return -EINVAL; |
339 | ||
aecde8b5 | 340 | if (reg->match.addr != mt9m001->client->addr) |
f523dd0d GL |
341 | return -ENODEV; |
342 | ||
343 | if (reg_write(icd, reg->reg, reg->val) < 0) | |
344 | return -EIO; | |
345 | ||
346 | return 0; | |
347 | } | |
348 | #endif | |
349 | ||
4407a463 | 350 | static const struct v4l2_queryctrl mt9m001_controls[] = { |
f523dd0d GL |
351 | { |
352 | .id = V4L2_CID_VFLIP, | |
353 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
354 | .name = "Flip Vertically", | |
355 | .minimum = 0, | |
356 | .maximum = 1, | |
357 | .step = 1, | |
358 | .default_value = 0, | |
359 | }, { | |
360 | .id = V4L2_CID_GAIN, | |
361 | .type = V4L2_CTRL_TYPE_INTEGER, | |
362 | .name = "Gain", | |
363 | .minimum = 0, | |
364 | .maximum = 127, | |
365 | .step = 1, | |
366 | .default_value = 64, | |
367 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
368 | }, { | |
369 | .id = V4L2_CID_EXPOSURE, | |
370 | .type = V4L2_CTRL_TYPE_INTEGER, | |
371 | .name = "Exposure", | |
372 | .minimum = 1, | |
373 | .maximum = 255, | |
374 | .step = 1, | |
375 | .default_value = 255, | |
376 | .flags = V4L2_CTRL_FLAG_SLIDER, | |
377 | }, { | |
378 | .id = V4L2_CID_EXPOSURE_AUTO, | |
379 | .type = V4L2_CTRL_TYPE_BOOLEAN, | |
380 | .name = "Automatic Exposure", | |
381 | .minimum = 0, | |
382 | .maximum = 1, | |
383 | .step = 1, | |
384 | .default_value = 1, | |
385 | } | |
386 | }; | |
387 | ||
26f1b942 GL |
388 | static int mt9m001_video_probe(struct soc_camera_device *); |
389 | static void mt9m001_video_remove(struct soc_camera_device *); | |
390 | static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *); | |
391 | static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *); | |
f523dd0d GL |
392 | |
393 | static struct soc_camera_ops mt9m001_ops = { | |
394 | .owner = THIS_MODULE, | |
26f1b942 GL |
395 | .probe = mt9m001_video_probe, |
396 | .remove = mt9m001_video_remove, | |
f523dd0d GL |
397 | .init = mt9m001_init, |
398 | .release = mt9m001_release, | |
399 | .start_capture = mt9m001_start_capture, | |
400 | .stop_capture = mt9m001_stop_capture, | |
09e231b3 | 401 | .set_crop = mt9m001_set_crop, |
d8fac217 GL |
402 | .set_fmt = mt9m001_set_fmt, |
403 | .try_fmt = mt9m001_try_fmt, | |
ad5f2e85 GL |
404 | .set_bus_param = mt9m001_set_bus_param, |
405 | .query_bus_param = mt9m001_query_bus_param, | |
f523dd0d GL |
406 | .controls = mt9m001_controls, |
407 | .num_controls = ARRAY_SIZE(mt9m001_controls), | |
408 | .get_control = mt9m001_get_control, | |
409 | .set_control = mt9m001_set_control, | |
410 | .get_chip_id = mt9m001_get_chip_id, | |
411 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
412 | .get_register = mt9m001_get_register, | |
413 | .set_register = mt9m001_set_register, | |
414 | #endif | |
415 | }; | |
416 | ||
417 | static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) | |
418 | { | |
419 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
420 | int data; | |
421 | ||
422 | switch (ctrl->id) { | |
423 | case V4L2_CID_VFLIP: | |
424 | data = reg_read(icd, MT9M001_READ_OPTIONS2); | |
425 | if (data < 0) | |
426 | return -EIO; | |
427 | ctrl->value = !!(data & 0x8000); | |
428 | break; | |
429 | case V4L2_CID_EXPOSURE_AUTO: | |
430 | ctrl->value = mt9m001->autoexposure; | |
431 | break; | |
432 | } | |
433 | return 0; | |
434 | } | |
435 | ||
436 | static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl) | |
437 | { | |
438 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
439 | const struct v4l2_queryctrl *qctrl; | |
440 | int data; | |
441 | ||
442 | qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id); | |
443 | ||
444 | if (!qctrl) | |
445 | return -EINVAL; | |
446 | ||
447 | switch (ctrl->id) { | |
448 | case V4L2_CID_VFLIP: | |
449 | if (ctrl->value) | |
450 | data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000); | |
451 | else | |
452 | data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000); | |
453 | if (data < 0) | |
454 | return -EIO; | |
455 | break; | |
456 | case V4L2_CID_GAIN: | |
457 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
458 | return -EINVAL; | |
459 | /* See Datasheet Table 7, Gain settings. */ | |
460 | if (ctrl->value <= qctrl->default_value) { | |
461 | /* Pack it into 0..1 step 0.125, register values 0..8 */ | |
462 | unsigned long range = qctrl->default_value - qctrl->minimum; | |
463 | data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; | |
464 | ||
465 | dev_dbg(&icd->dev, "Setting gain %d\n", data); | |
466 | data = reg_write(icd, MT9M001_GLOBAL_GAIN, data); | |
467 | if (data < 0) | |
468 | return -EIO; | |
469 | } else { | |
470 | /* Pack it into 1.125..15 variable step, register values 9..67 */ | |
471 | /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ | |
472 | unsigned long range = qctrl->maximum - qctrl->default_value - 1; | |
473 | unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * | |
474 | 111 + range / 2) / range + 9; | |
475 | ||
476 | if (gain <= 32) | |
477 | data = gain; | |
478 | else if (gain <= 64) | |
479 | data = ((gain - 32) * 16 + 16) / 32 + 80; | |
480 | else | |
481 | data = ((gain - 64) * 7 + 28) / 56 + 96; | |
482 | ||
483 | dev_dbg(&icd->dev, "Setting gain from %d to %d\n", | |
484 | reg_read(icd, MT9M001_GLOBAL_GAIN), data); | |
485 | data = reg_write(icd, MT9M001_GLOBAL_GAIN, data); | |
486 | if (data < 0) | |
487 | return -EIO; | |
488 | } | |
489 | ||
490 | /* Success */ | |
491 | icd->gain = ctrl->value; | |
492 | break; | |
493 | case V4L2_CID_EXPOSURE: | |
494 | /* mt9m001 has maximum == default */ | |
495 | if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum) | |
496 | return -EINVAL; | |
497 | else { | |
498 | unsigned long range = qctrl->maximum - qctrl->minimum; | |
499 | unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 + | |
500 | range / 2) / range + 1; | |
501 | ||
502 | dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n", | |
503 | reg_read(icd, MT9M001_SHUTTER_WIDTH), shutter); | |
504 | if (reg_write(icd, MT9M001_SHUTTER_WIDTH, shutter) < 0) | |
505 | return -EIO; | |
506 | icd->exposure = ctrl->value; | |
507 | mt9m001->autoexposure = 0; | |
508 | } | |
509 | break; | |
510 | case V4L2_CID_EXPOSURE_AUTO: | |
511 | if (ctrl->value) { | |
512 | const u16 vblank = 25; | |
513 | if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height + | |
514 | icd->y_skip_top + vblank) < 0) | |
515 | return -EIO; | |
516 | qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); | |
517 | icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) * | |
518 | (qctrl->maximum - qctrl->minimum)) / | |
519 | 1048 + qctrl->minimum; | |
520 | mt9m001->autoexposure = 1; | |
521 | } else | |
522 | mt9m001->autoexposure = 0; | |
523 | break; | |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
528 | /* Interface active, can use i2c. If it fails, it can indeed mean, that | |
529 | * this wasn't our capture interface, so, we wait for the right one */ | |
530 | static int mt9m001_video_probe(struct soc_camera_device *icd) | |
531 | { | |
532 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
297a7ef7 | 533 | struct soc_camera_link *icl = mt9m001->client->dev.platform_data; |
f523dd0d GL |
534 | s32 data; |
535 | int ret; | |
36034dc3 | 536 | unsigned long flags; |
f523dd0d GL |
537 | |
538 | /* We must have a parent by now. And it cannot be a wrong one. | |
539 | * So this entire test is completely redundant. */ | |
540 | if (!icd->dev.parent || | |
541 | to_soc_camera_host(icd->dev.parent)->nr != icd->iface) | |
542 | return -ENODEV; | |
543 | ||
544 | /* Enable the chip */ | |
a85bdace | 545 | data = reg_write(icd, MT9M001_CHIP_ENABLE, 1); |
f523dd0d GL |
546 | dev_dbg(&icd->dev, "write: %d\n", data); |
547 | ||
548 | /* Read out the chip version register */ | |
549 | data = reg_read(icd, MT9M001_CHIP_VERSION); | |
550 | ||
551 | /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */ | |
552 | switch (data) { | |
553 | case 0x8411: | |
554 | case 0x8421: | |
555 | mt9m001->model = V4L2_IDENT_MT9M001C12ST; | |
26f1b942 | 556 | icd->formats = mt9m001_colour_formats; |
f523dd0d GL |
557 | break; |
558 | case 0x8431: | |
559 | mt9m001->model = V4L2_IDENT_MT9M001C12STM; | |
26f1b942 | 560 | icd->formats = mt9m001_monochrome_formats; |
f523dd0d GL |
561 | break; |
562 | default: | |
563 | ret = -ENODEV; | |
564 | dev_err(&icd->dev, | |
565 | "No MT9M001 chip detected, register read %x\n", data); | |
566 | goto ei2c; | |
567 | } | |
568 | ||
36034dc3 SH |
569 | icd->num_formats = 0; |
570 | ||
571 | /* | |
572 | * This is a 10bit sensor, so by default we only allow 10bit. | |
573 | * The platform may support different bus widths due to | |
574 | * different routing of the data lines. | |
575 | */ | |
576 | if (icl->query_bus_param) | |
577 | flags = icl->query_bus_param(icl); | |
578 | else | |
579 | flags = SOCAM_DATAWIDTH_10; | |
580 | ||
581 | if (flags & SOCAM_DATAWIDTH_10) | |
582 | icd->num_formats++; | |
583 | else | |
584 | icd->formats++; | |
585 | ||
586 | if (flags & SOCAM_DATAWIDTH_8) | |
587 | icd->num_formats++; | |
588 | ||
f523dd0d GL |
589 | dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data, |
590 | data == 0x8431 ? "C12STM" : "C12ST"); | |
591 | ||
592 | /* Now that we know the model, we can start video */ | |
593 | ret = soc_camera_video_start(icd); | |
594 | if (ret) | |
595 | goto eisis; | |
596 | ||
597 | return 0; | |
598 | ||
599 | eisis: | |
600 | ei2c: | |
601 | return ret; | |
602 | } | |
603 | ||
604 | static void mt9m001_video_remove(struct soc_camera_device *icd) | |
605 | { | |
606 | struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd); | |
607 | ||
608 | dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr, | |
a85bdace GL |
609 | icd->dev.parent, icd->vdev); |
610 | soc_camera_video_stop(icd); | |
f523dd0d GL |
611 | } |
612 | ||
d2653e92 JD |
613 | static int mt9m001_probe(struct i2c_client *client, |
614 | const struct i2c_device_id *did) | |
f523dd0d GL |
615 | { |
616 | struct mt9m001 *mt9m001; | |
617 | struct soc_camera_device *icd; | |
618 | struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent); | |
619 | struct soc_camera_link *icl = client->dev.platform_data; | |
620 | int ret; | |
621 | ||
622 | if (!icl) { | |
623 | dev_err(&client->dev, "MT9M001 driver needs platform data\n"); | |
624 | return -EINVAL; | |
625 | } | |
626 | ||
627 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) { | |
628 | dev_warn(&adapter->dev, | |
629 | "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n"); | |
630 | return -EIO; | |
631 | } | |
632 | ||
633 | mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL); | |
634 | if (!mt9m001) | |
635 | return -ENOMEM; | |
636 | ||
637 | mt9m001->client = client; | |
638 | i2c_set_clientdata(client, mt9m001); | |
639 | ||
640 | /* Second stage probe - when a capture adapter is there */ | |
641 | icd = &mt9m001->icd; | |
f523dd0d GL |
642 | icd->ops = &mt9m001_ops; |
643 | icd->control = &client->dev; | |
644 | icd->x_min = 20; | |
645 | icd->y_min = 12; | |
646 | icd->x_current = 20; | |
647 | icd->y_current = 12; | |
648 | icd->width_min = 48; | |
649 | icd->width_max = 1280; | |
650 | icd->height_min = 32; | |
651 | icd->height_max = 1024; | |
652 | icd->y_skip_top = 1; | |
653 | icd->iface = icl->bus_id; | |
f523dd0d GL |
654 | /* Simulated autoexposure. If enabled, we calculate shutter width |
655 | * ourselves in the driver based on vertical blanking and frame width */ | |
656 | mt9m001->autoexposure = 1; | |
657 | ||
f523dd0d GL |
658 | ret = soc_camera_device_register(icd); |
659 | if (ret) | |
660 | goto eisdr; | |
661 | ||
662 | return 0; | |
663 | ||
664 | eisdr: | |
f523dd0d GL |
665 | kfree(mt9m001); |
666 | return ret; | |
667 | } | |
668 | ||
669 | static int mt9m001_remove(struct i2c_client *client) | |
670 | { | |
671 | struct mt9m001 *mt9m001 = i2c_get_clientdata(client); | |
672 | ||
673 | soc_camera_device_unregister(&mt9m001->icd); | |
f523dd0d GL |
674 | kfree(mt9m001); |
675 | ||
676 | return 0; | |
677 | } | |
678 | ||
3760f736 JD |
679 | static const struct i2c_device_id mt9m001_id[] = { |
680 | { "mt9m001", 0 }, | |
681 | { } | |
682 | }; | |
683 | MODULE_DEVICE_TABLE(i2c, mt9m001_id); | |
684 | ||
f523dd0d GL |
685 | static struct i2c_driver mt9m001_i2c_driver = { |
686 | .driver = { | |
687 | .name = "mt9m001", | |
688 | }, | |
689 | .probe = mt9m001_probe, | |
690 | .remove = mt9m001_remove, | |
3760f736 | 691 | .id_table = mt9m001_id, |
f523dd0d GL |
692 | }; |
693 | ||
694 | static int __init mt9m001_mod_init(void) | |
695 | { | |
696 | return i2c_add_driver(&mt9m001_i2c_driver); | |
697 | } | |
698 | ||
699 | static void __exit mt9m001_mod_exit(void) | |
700 | { | |
701 | i2c_del_driver(&mt9m001_i2c_driver); | |
702 | } | |
703 | ||
704 | module_init(mt9m001_mod_init); | |
705 | module_exit(mt9m001_mod_exit); | |
706 | ||
707 | MODULE_DESCRIPTION("Micron MT9M001 Camera driver"); | |
708 | MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>"); | |
709 | MODULE_LICENSE("GPL"); |