2 * Driver for MT9V022 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/delay.h>
15 #include <linux/log2.h>
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/soc_camera.h>
21 /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
22 * The platform has to define i2c_board_info
23 * and call i2c_register_board_info() */
25 static char *sensor_type;
26 module_param(sensor_type, charp, S_IRUGO);
27 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
29 /* mt9v022 selected register addresses */
30 #define MT9V022_CHIP_VERSION 0x00
31 #define MT9V022_COLUMN_START 0x01
32 #define MT9V022_ROW_START 0x02
33 #define MT9V022_WINDOW_HEIGHT 0x03
34 #define MT9V022_WINDOW_WIDTH 0x04
35 #define MT9V022_HORIZONTAL_BLANKING 0x05
36 #define MT9V022_VERTICAL_BLANKING 0x06
37 #define MT9V022_CHIP_CONTROL 0x07
38 #define MT9V022_SHUTTER_WIDTH1 0x08
39 #define MT9V022_SHUTTER_WIDTH2 0x09
40 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
41 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
42 #define MT9V022_RESET 0x0c
43 #define MT9V022_READ_MODE 0x0d
44 #define MT9V022_MONITOR_MODE 0x0e
45 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
46 #define MT9V022_LED_OUT_CONTROL 0x1b
47 #define MT9V022_ADC_MODE_CONTROL 0x1c
48 #define MT9V022_ANALOG_GAIN 0x34
49 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
50 #define MT9V022_PIXCLK_FV_LV 0x74
51 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
52 #define MT9V022_AEC_AGC_ENABLE 0xAF
53 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
55 /* Progressive scan, master, defaults */
56 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
58 static const struct soc_camera_data_format mt9v022_colour_formats[] = {
59 /* Order important: first natively supported,
60 * second supported with a GPIO extender */
62 .name = "Bayer (sRGB) 10 bit",
64 .fourcc = V4L2_PIX_FMT_SBGGR16,
65 .colorspace = V4L2_COLORSPACE_SRGB,
67 .name = "Bayer (sRGB) 8 bit",
69 .fourcc = V4L2_PIX_FMT_SBGGR8,
70 .colorspace = V4L2_COLORSPACE_SRGB,
74 static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
75 /* Order important - see above */
77 .name = "Monochrome 10 bit",
79 .fourcc = V4L2_PIX_FMT_Y16,
81 .name = "Monochrome 8 bit",
83 .fourcc = V4L2_PIX_FMT_GREY,
88 struct i2c_client *client;
89 struct soc_camera_device icd;
90 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
94 static int reg_read(struct soc_camera_device *icd, const u8 reg)
96 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
97 struct i2c_client *client = mt9v022->client;
98 s32 data = i2c_smbus_read_word_data(client, reg);
99 return data < 0 ? data : swab16(data);
102 static int reg_write(struct soc_camera_device *icd, const u8 reg,
105 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
106 return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
109 static int reg_set(struct soc_camera_device *icd, const u8 reg,
114 ret = reg_read(icd, reg);
117 return reg_write(icd, reg, ret | data);
120 static int reg_clear(struct soc_camera_device *icd, const u8 reg,
125 ret = reg_read(icd, reg);
128 return reg_write(icd, reg, ret & ~data);
131 static int mt9v022_init(struct soc_camera_device *icd)
133 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
134 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
138 ret = icl->power(&mt9v022->client->dev, 1);
140 dev_err(icd->vdev->parent,
141 "Platform failed to power-on the camera.\n");
147 * The camera could have been already on, we hard-reset it additionally,
148 * if available. Soft reset is done in video_probe().
151 icl->reset(&mt9v022->client->dev);
153 /* Almost the default mode: master, parallel, simultaneous, and an
154 * undocumented bit 0x200, which is present in table 7, but not in 8,
155 * plus snapshot mode to disable scan for now */
156 mt9v022->chip_control |= 0x10;
157 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
159 ret = reg_write(icd, MT9V022_READ_MODE, 0x300);
164 ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
166 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
169 ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
171 ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
176 static int mt9v022_release(struct soc_camera_device *icd)
178 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
179 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
182 icl->power(&mt9v022->client->dev, 0);
187 static int mt9v022_start_capture(struct soc_camera_device *icd)
189 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
190 /* Switch to master "normal" mode */
191 mt9v022->chip_control &= ~0x10;
192 if (reg_write(icd, MT9V022_CHIP_CONTROL,
193 mt9v022->chip_control) < 0)
198 static int mt9v022_stop_capture(struct soc_camera_device *icd)
200 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
201 /* Switch to snapshot mode */
202 mt9v022->chip_control |= 0x10;
203 if (reg_write(icd, MT9V022_CHIP_CONTROL,
204 mt9v022->chip_control) < 0)
209 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
212 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
213 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
214 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
218 /* Only one width bit may be set */
219 if (!is_power_of_2(width_flag))
222 if (icl->set_bus_param) {
223 ret = icl->set_bus_param(icl, width_flag);
228 * Without board specific bus width settings we only support the
229 * sensors native bus width
231 if (width_flag != SOCAM_DATAWIDTH_10)
235 flags = soc_camera_apply_sensor_flags(icl, flags);
237 if (flags & SOCAM_PCLK_SAMPLE_RISING)
240 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
243 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
246 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
250 if (!(flags & SOCAM_MASTER))
251 mt9v022->chip_control &= ~0x8;
253 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
257 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
258 pixclk, mt9v022->chip_control);
263 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
265 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
266 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
267 unsigned int width_flag;
269 if (icl->query_bus_param)
270 width_flag = icl->query_bus_param(icl) &
271 SOCAM_DATAWIDTH_MASK;
273 width_flag = SOCAM_DATAWIDTH_10;
275 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
276 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
277 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
278 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
282 static int mt9v022_set_crop(struct soc_camera_device *icd,
283 struct v4l2_rect *rect)
287 /* Like in example app. Contradicts the datasheet though */
288 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
290 if (ret & 1) /* Autoexposure */
291 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
292 rect->height + icd->y_skip_top + 43);
294 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
295 rect->height + icd->y_skip_top + 43);
297 /* Setup frame format: defaults apart from width and height */
299 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
301 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
303 /* Default 94, Phytec driver says:
304 * "width + horizontal blank >= 660" */
305 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
306 rect->width > 660 - 43 ? 43 :
309 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
311 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
313 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
314 rect->height + icd->y_skip_top);
319 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
324 static int mt9v022_set_fmt(struct soc_camera_device *icd,
325 struct v4l2_format *f)
327 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
328 struct v4l2_pix_format *pix = &f->fmt.pix;
329 struct v4l2_rect rect = {
330 .left = icd->x_current,
331 .top = icd->y_current,
333 .height = pix->height,
336 /* The caller provides a supported format, as verified per call to
337 * icd->try_fmt(), datawidth is from our supported format list */
338 switch (pix->pixelformat) {
339 case V4L2_PIX_FMT_GREY:
340 case V4L2_PIX_FMT_Y16:
341 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
344 case V4L2_PIX_FMT_SBGGR8:
345 case V4L2_PIX_FMT_SBGGR16:
346 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
350 /* No format change, only geometry */
356 /* No support for scaling on this camera, just crop. */
357 return mt9v022_set_crop(icd, &rect);
360 static int mt9v022_try_fmt(struct soc_camera_device *icd,
361 struct v4l2_format *f)
363 struct v4l2_pix_format *pix = &f->fmt.pix;
365 if (pix->height < 32 + icd->y_skip_top)
366 pix->height = 32 + icd->y_skip_top;
367 if (pix->height > 480 + icd->y_skip_top)
368 pix->height = 480 + icd->y_skip_top;
371 if (pix->width > 752)
373 pix->width &= ~0x03; /* ? */
378 static int mt9v022_get_chip_id(struct soc_camera_device *icd,
379 struct v4l2_dbg_chip_ident *id)
381 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
383 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
386 if (id->match.addr != mt9v022->client->addr)
389 id->ident = mt9v022->model;
395 #ifdef CONFIG_VIDEO_ADV_DEBUG
396 static int mt9v022_get_register(struct soc_camera_device *icd,
397 struct v4l2_dbg_register *reg)
399 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
401 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
404 if (reg->match.addr != mt9v022->client->addr)
408 reg->val = reg_read(icd, reg->reg);
410 if (reg->val > 0xffff)
416 static int mt9v022_set_register(struct soc_camera_device *icd,
417 struct v4l2_dbg_register *reg)
419 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
421 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
424 if (reg->match.addr != mt9v022->client->addr)
427 if (reg_write(icd, reg->reg, reg->val) < 0)
434 static const struct v4l2_queryctrl mt9v022_controls[] = {
436 .id = V4L2_CID_VFLIP,
437 .type = V4L2_CTRL_TYPE_BOOLEAN,
438 .name = "Flip Vertically",
444 .id = V4L2_CID_HFLIP,
445 .type = V4L2_CTRL_TYPE_BOOLEAN,
446 .name = "Flip Horizontally",
453 .type = V4L2_CTRL_TYPE_INTEGER,
454 .name = "Analog Gain",
459 .flags = V4L2_CTRL_FLAG_SLIDER,
461 .id = V4L2_CID_EXPOSURE,
462 .type = V4L2_CTRL_TYPE_INTEGER,
467 .default_value = 255,
468 .flags = V4L2_CTRL_FLAG_SLIDER,
470 .id = V4L2_CID_AUTOGAIN,
471 .type = V4L2_CTRL_TYPE_BOOLEAN,
472 .name = "Automatic Gain",
478 .id = V4L2_CID_EXPOSURE_AUTO,
479 .type = V4L2_CTRL_TYPE_BOOLEAN,
480 .name = "Automatic Exposure",
488 static int mt9v022_video_probe(struct soc_camera_device *);
489 static void mt9v022_video_remove(struct soc_camera_device *);
490 static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
491 static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
493 static struct soc_camera_ops mt9v022_ops = {
494 .owner = THIS_MODULE,
495 .probe = mt9v022_video_probe,
496 .remove = mt9v022_video_remove,
497 .init = mt9v022_init,
498 .release = mt9v022_release,
499 .start_capture = mt9v022_start_capture,
500 .stop_capture = mt9v022_stop_capture,
501 .set_crop = mt9v022_set_crop,
502 .set_fmt = mt9v022_set_fmt,
503 .try_fmt = mt9v022_try_fmt,
504 .set_bus_param = mt9v022_set_bus_param,
505 .query_bus_param = mt9v022_query_bus_param,
506 .controls = mt9v022_controls,
507 .num_controls = ARRAY_SIZE(mt9v022_controls),
508 .get_control = mt9v022_get_control,
509 .set_control = mt9v022_set_control,
510 .get_chip_id = mt9v022_get_chip_id,
511 #ifdef CONFIG_VIDEO_ADV_DEBUG
512 .get_register = mt9v022_get_register,
513 .set_register = mt9v022_set_register,
517 static int mt9v022_get_control(struct soc_camera_device *icd,
518 struct v4l2_control *ctrl)
524 data = reg_read(icd, MT9V022_READ_MODE);
527 ctrl->value = !!(data & 0x10);
530 data = reg_read(icd, MT9V022_READ_MODE);
533 ctrl->value = !!(data & 0x20);
535 case V4L2_CID_EXPOSURE_AUTO:
536 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
539 ctrl->value = !!(data & 0x1);
541 case V4L2_CID_AUTOGAIN:
542 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
545 ctrl->value = !!(data & 0x2);
551 static int mt9v022_set_control(struct soc_camera_device *icd,
552 struct v4l2_control *ctrl)
555 const struct v4l2_queryctrl *qctrl;
557 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
565 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
567 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
573 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
575 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
580 /* mt9v022 has minimum == default */
581 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
584 unsigned long range = qctrl->maximum - qctrl->minimum;
585 /* Datasheet says 16 to 64. autogain only works properly
586 * after setting gain to maximum 14. Larger values
587 * produce "white fly" noise effect. On the whole,
588 * manually setting analog gain does no good. */
589 unsigned long gain = ((ctrl->value - qctrl->minimum) *
590 10 + range / 2) / range + 4;
593 /* The user wants to set gain manually, hope, she
594 * knows, what she's doing... Switch AGC off. */
596 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
599 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
600 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
601 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
603 icd->gain = ctrl->value;
606 case V4L2_CID_EXPOSURE:
607 /* mt9v022 has maximum == default */
608 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
611 unsigned long range = qctrl->maximum - qctrl->minimum;
612 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
613 479 + range / 2) / range + 1;
614 /* The user wants to set shutter width manually, hope,
615 * she knows, what she's doing... Switch AEC off. */
617 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
620 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
621 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
623 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
626 icd->exposure = ctrl->value;
629 case V4L2_CID_AUTOGAIN:
631 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
633 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
637 case V4L2_CID_EXPOSURE_AUTO:
639 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
641 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
649 /* Interface active, can use i2c. If it fails, it can indeed mean, that
650 * this wasn't our capture interface, so, we wait for the right one */
651 static int mt9v022_video_probe(struct soc_camera_device *icd)
653 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
654 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
659 if (!icd->dev.parent ||
660 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
663 /* Read out the chip version register */
664 data = reg_read(icd, MT9V022_CHIP_VERSION);
666 /* must be 0x1311 or 0x1313 */
667 if (data != 0x1311 && data != 0x1313) {
669 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
675 ret = reg_write(icd, MT9V022_RESET, 1);
678 /* 15 clock cycles */
680 if (reg_read(icd, MT9V022_RESET)) {
681 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
685 /* Set monochrome or colour sensor type */
686 if (sensor_type && (!strcmp("colour", sensor_type) ||
687 !strcmp("color", sensor_type))) {
688 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
689 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
690 icd->formats = mt9v022_colour_formats;
692 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
693 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
694 icd->formats = mt9v022_monochrome_formats;
700 icd->num_formats = 0;
703 * This is a 10bit sensor, so by default we only allow 10bit.
704 * The platform may support different bus widths due to
705 * different routing of the data lines.
707 if (icl->query_bus_param)
708 flags = icl->query_bus_param(icl);
710 flags = SOCAM_DATAWIDTH_10;
712 if (flags & SOCAM_DATAWIDTH_10)
717 if (flags & SOCAM_DATAWIDTH_8)
720 ret = soc_camera_video_start(icd);
724 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
725 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
726 "monochrome" : "colour");
735 static void mt9v022_video_remove(struct soc_camera_device *icd)
737 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
739 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
740 icd->dev.parent, icd->vdev);
741 soc_camera_video_stop(icd);
744 static int mt9v022_probe(struct i2c_client *client,
745 const struct i2c_device_id *did)
747 struct mt9v022 *mt9v022;
748 struct soc_camera_device *icd;
749 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
750 struct soc_camera_link *icl = client->dev.platform_data;
754 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
758 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
759 dev_warn(&adapter->dev,
760 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
764 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
768 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
769 mt9v022->client = client;
770 i2c_set_clientdata(client, mt9v022);
773 icd->ops = &mt9v022_ops;
774 icd->control = &client->dev;
780 icd->width_max = 752;
781 icd->height_min = 32;
782 icd->height_max = 480;
784 icd->iface = icl->bus_id;
786 ret = soc_camera_device_register(icd);
797 static int mt9v022_remove(struct i2c_client *client)
799 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
801 soc_camera_device_unregister(&mt9v022->icd);
806 static const struct i2c_device_id mt9v022_id[] = {
810 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
812 static struct i2c_driver mt9v022_i2c_driver = {
816 .probe = mt9v022_probe,
817 .remove = mt9v022_remove,
818 .id_table = mt9v022_id,
821 static int __init mt9v022_mod_init(void)
823 return i2c_add_driver(&mt9v022_i2c_driver);
826 static void __exit mt9v022_mod_exit(void)
828 i2c_del_driver(&mt9v022_i2c_driver);
831 module_init(mt9v022_mod_init);
832 module_exit(mt9v022_mod_exit);
834 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
835 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
836 MODULE_LICENSE("GPL");