2 * Driver for MT9M001 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
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.
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/log2.h>
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/soc_camera.h>
20 /* mt9m001 i2c address 0x5d
21 * The platform has to define i2c_board_info
22 * and call i2c_register_board_info() */
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
42 static const struct soc_camera_data_format mt9m001_colour_formats[] = {
43 /* Order important: first natively supported,
44 * second supported with a GPIO extender */
46 .name = "Bayer (sRGB) 10 bit",
48 .fourcc = V4L2_PIX_FMT_SBGGR16,
49 .colorspace = V4L2_COLORSPACE_SRGB,
51 .name = "Bayer (sRGB) 8 bit",
53 .fourcc = V4L2_PIX_FMT_SBGGR8,
54 .colorspace = V4L2_COLORSPACE_SRGB,
58 static const struct soc_camera_data_format mt9m001_monochrome_formats[] = {
59 /* Order important - see above */
61 .name = "Monochrome 10 bit",
63 .fourcc = V4L2_PIX_FMT_Y16,
65 .name = "Monochrome 8 bit",
67 .fourcc = V4L2_PIX_FMT_GREY,
72 struct i2c_client *client;
73 struct soc_camera_device icd;
74 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
75 unsigned char autoexposure;
78 static int reg_read(struct soc_camera_device *icd, const u8 reg)
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);
86 static int reg_write(struct soc_camera_device *icd, const u8 reg,
89 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
90 return i2c_smbus_write_word_data(mt9m001->client, reg, swab16(data));
93 static int reg_set(struct soc_camera_device *icd, const u8 reg,
98 ret = reg_read(icd, reg);
101 return reg_write(icd, reg, ret | data);
104 static int reg_clear(struct soc_camera_device *icd, const u8 reg,
109 ret = reg_read(icd, reg);
112 return reg_write(icd, reg, ret & ~data);
115 static int mt9m001_init(struct soc_camera_device *icd)
117 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
118 struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
121 dev_dbg(icd->vdev->parent, "%s\n", __func__);
124 ret = icl->power(&mt9m001->client->dev, 1);
126 dev_err(icd->vdev->parent,
127 "Platform failed to power-on the camera.\n");
132 /* The camera could have been already on, we reset it additionally */
134 ret = icl->reset(&mt9m001->client->dev);
139 /* Either no platform reset, or platform reset failed */
140 ret = reg_write(icd, MT9M001_RESET, 1);
142 ret = reg_write(icd, MT9M001_RESET, 0);
144 /* Disable chip, synchronous option update */
146 ret = reg_write(icd, MT9M001_OUTPUT_CONTROL, 0);
151 static int mt9m001_release(struct soc_camera_device *icd)
153 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
154 struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
156 /* Disable the chip */
157 reg_write(icd, MT9M001_OUTPUT_CONTROL, 0);
160 icl->power(&mt9m001->client->dev, 0);
165 static int mt9m001_start_capture(struct soc_camera_device *icd)
167 /* Switch to master "normal" mode */
168 if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 2) < 0)
173 static int mt9m001_stop_capture(struct soc_camera_device *icd)
175 /* Stop sensor readout */
176 if (reg_write(icd, MT9M001_OUTPUT_CONTROL, 0) < 0)
181 static int mt9m001_set_bus_param(struct soc_camera_device *icd,
184 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
185 struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
186 unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
188 /* Only one width bit may be set */
189 if (!is_power_of_2(width_flag))
192 if (icl->set_bus_param)
193 return icl->set_bus_param(icl, width_flag);
196 * Without board specific bus width settings we only support the
197 * sensors native bus width
199 if (width_flag == SOCAM_DATAWIDTH_10)
205 static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
207 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
208 struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
209 /* MT9M001 has all capture_format parameters fixed */
210 unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
211 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
212 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
214 if (icl->query_bus_param)
215 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
217 flags |= SOCAM_DATAWIDTH_10;
219 return soc_camera_apply_sensor_flags(icl, flags);
222 static int mt9m001_set_crop(struct soc_camera_device *icd,
223 struct v4l2_rect *rect)
225 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
227 const u16 hblank = 9, vblank = 25;
229 /* Blanking and start values - default... */
230 ret = reg_write(icd, MT9M001_HORIZONTAL_BLANKING, hblank);
232 ret = reg_write(icd, MT9M001_VERTICAL_BLANKING, vblank);
234 /* The caller provides a supported format, as verified per
235 * call to icd->try_fmt() */
237 ret = reg_write(icd, MT9M001_COLUMN_START, rect->left);
239 ret = reg_write(icd, MT9M001_ROW_START, rect->top);
241 ret = reg_write(icd, MT9M001_WINDOW_WIDTH, rect->width - 1);
243 ret = reg_write(icd, MT9M001_WINDOW_HEIGHT,
244 rect->height + icd->y_skip_top - 1);
245 if (!ret && mt9m001->autoexposure) {
246 ret = reg_write(icd, MT9M001_SHUTTER_WIDTH,
247 rect->height + icd->y_skip_top + vblank);
249 const struct v4l2_queryctrl *qctrl =
250 soc_camera_find_qctrl(icd->ops,
252 icd->exposure = (524 + (rect->height + icd->y_skip_top +
254 (qctrl->maximum - qctrl->minimum)) /
255 1048 + qctrl->minimum;
262 static int mt9m001_set_fmt(struct soc_camera_device *icd,
263 struct v4l2_format *f)
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,
272 /* No support for scaling so far, just crop. TODO: use skipping */
273 return mt9m001_set_crop(icd, &rect);
276 static int mt9m001_try_fmt(struct soc_camera_device *icd,
277 struct v4l2_format *f)
279 struct v4l2_pix_format *pix = &f->fmt.pix;
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;
287 if (pix->width > 1280)
289 pix->width &= ~0x01; /* has to be even, unsure why was ~3 */
294 static int mt9m001_get_chip_id(struct soc_camera_device *icd,
295 struct v4l2_dbg_chip_ident *id)
297 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
299 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
302 if (id->match.addr != mt9m001->client->addr)
305 id->ident = mt9m001->model;
311 #ifdef CONFIG_VIDEO_ADV_DEBUG
312 static int mt9m001_get_register(struct soc_camera_device *icd,
313 struct v4l2_dbg_register *reg)
315 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
317 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
320 if (reg->match.addr != mt9m001->client->addr)
324 reg->val = reg_read(icd, reg->reg);
326 if (reg->val > 0xffff)
332 static int mt9m001_set_register(struct soc_camera_device *icd,
333 struct v4l2_dbg_register *reg)
335 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
337 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
340 if (reg->match.addr != mt9m001->client->addr)
343 if (reg_write(icd, reg->reg, reg->val) < 0)
350 static const struct v4l2_queryctrl mt9m001_controls[] = {
352 .id = V4L2_CID_VFLIP,
353 .type = V4L2_CTRL_TYPE_BOOLEAN,
354 .name = "Flip Vertically",
361 .type = V4L2_CTRL_TYPE_INTEGER,
367 .flags = V4L2_CTRL_FLAG_SLIDER,
369 .id = V4L2_CID_EXPOSURE,
370 .type = V4L2_CTRL_TYPE_INTEGER,
375 .default_value = 255,
376 .flags = V4L2_CTRL_FLAG_SLIDER,
378 .id = V4L2_CID_EXPOSURE_AUTO,
379 .type = V4L2_CTRL_TYPE_BOOLEAN,
380 .name = "Automatic Exposure",
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 *);
393 static struct soc_camera_ops mt9m001_ops = {
394 .owner = THIS_MODULE,
395 .probe = mt9m001_video_probe,
396 .remove = mt9m001_video_remove,
397 .init = mt9m001_init,
398 .release = mt9m001_release,
399 .start_capture = mt9m001_start_capture,
400 .stop_capture = mt9m001_stop_capture,
401 .set_crop = mt9m001_set_crop,
402 .set_fmt = mt9m001_set_fmt,
403 .try_fmt = mt9m001_try_fmt,
404 .set_bus_param = mt9m001_set_bus_param,
405 .query_bus_param = mt9m001_query_bus_param,
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,
417 static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
419 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
424 data = reg_read(icd, MT9M001_READ_OPTIONS2);
427 ctrl->value = !!(data & 0x8000);
429 case V4L2_CID_EXPOSURE_AUTO:
430 ctrl->value = mt9m001->autoexposure;
436 static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
438 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
439 const struct v4l2_queryctrl *qctrl;
442 qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
450 data = reg_set(icd, MT9M001_READ_OPTIONS2, 0x8000);
452 data = reg_clear(icd, MT9M001_READ_OPTIONS2, 0x8000);
457 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
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;
465 dev_dbg(&icd->dev, "Setting gain %d\n", data);
466 data = reg_write(icd, MT9M001_GLOBAL_GAIN, data);
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;
479 data = ((gain - 32) * 16 + 16) / 32 + 80;
481 data = ((gain - 64) * 7 + 28) / 56 + 96;
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);
491 icd->gain = ctrl->value;
493 case V4L2_CID_EXPOSURE:
494 /* mt9m001 has maximum == default */
495 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
498 unsigned long range = qctrl->maximum - qctrl->minimum;
499 unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
500 range / 2) / range + 1;
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)
506 icd->exposure = ctrl->value;
507 mt9m001->autoexposure = 0;
510 case V4L2_CID_EXPOSURE_AUTO:
512 const u16 vblank = 25;
513 if (reg_write(icd, MT9M001_SHUTTER_WIDTH, icd->height +
514 icd->y_skip_top + vblank) < 0)
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;
522 mt9m001->autoexposure = 0;
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)
532 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
533 struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
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)
544 /* Enable the chip */
545 data = reg_write(icd, MT9M001_CHIP_ENABLE, 1);
546 dev_dbg(&icd->dev, "write: %d\n", data);
548 /* Read out the chip version register */
549 data = reg_read(icd, MT9M001_CHIP_VERSION);
551 /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
555 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
556 icd->formats = mt9m001_colour_formats;
559 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
560 icd->formats = mt9m001_monochrome_formats;
565 "No MT9M001 chip detected, register read %x\n", data);
569 icd->num_formats = 0;
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.
576 if (icl->query_bus_param)
577 flags = icl->query_bus_param(icl);
579 flags = SOCAM_DATAWIDTH_10;
581 if (flags & SOCAM_DATAWIDTH_10)
586 if (flags & SOCAM_DATAWIDTH_8)
589 dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
590 data == 0x8431 ? "C12STM" : "C12ST");
592 /* Now that we know the model, we can start video */
593 ret = soc_camera_video_start(icd);
604 static void mt9m001_video_remove(struct soc_camera_device *icd)
606 struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
608 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
609 icd->dev.parent, icd->vdev);
610 soc_camera_video_stop(icd);
613 static int mt9m001_probe(struct i2c_client *client,
614 const struct i2c_device_id *did)
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;
623 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
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");
633 mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
637 mt9m001->client = client;
638 i2c_set_clientdata(client, mt9m001);
640 /* Second stage probe - when a capture adapter is there */
642 icd->ops = &mt9m001_ops;
643 icd->control = &client->dev;
649 icd->width_max = 1280;
650 icd->height_min = 32;
651 icd->height_max = 1024;
653 icd->iface = icl->bus_id;
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;
658 ret = soc_camera_device_register(icd);
669 static int mt9m001_remove(struct i2c_client *client)
671 struct mt9m001 *mt9m001 = i2c_get_clientdata(client);
673 soc_camera_device_unregister(&mt9m001->icd);
679 static const struct i2c_device_id mt9m001_id[] = {
683 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
685 static struct i2c_driver mt9m001_i2c_driver = {
689 .probe = mt9m001_probe,
690 .remove = mt9m001_remove,
691 .id_table = mt9m001_id,
694 static int __init mt9m001_mod_init(void)
696 return i2c_add_driver(&mt9m001_i2c_driver);
699 static void __exit mt9m001_mod_exit(void)
701 i2c_del_driver(&mt9m001_i2c_driver);
704 module_init(mt9m001_mod_init);
705 module_exit(mt9m001_mod_exit);
707 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
708 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
709 MODULE_LICENSE("GPL");