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