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-subdev.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 ctruct i2c_board_info objects and link to them
23 * from struct soc_camera_link */
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 v4l2_subdev subdev;
89 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
93 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
95 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
98 static int reg_read(struct i2c_client *client, const u8 reg)
100 s32 data = i2c_smbus_read_word_data(client, reg);
101 return data < 0 ? data : swab16(data);
104 static int reg_write(struct i2c_client *client, const u8 reg,
107 return i2c_smbus_write_word_data(client, reg, swab16(data));
110 static int reg_set(struct i2c_client *client, const u8 reg,
115 ret = reg_read(client, reg);
118 return reg_write(client, reg, ret | data);
121 static int reg_clear(struct i2c_client *client, const u8 reg,
126 ret = reg_read(client, reg);
129 return reg_write(client, reg, ret & ~data);
132 static int mt9v022_init(struct soc_camera_device *icd)
134 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
135 struct mt9v022 *mt9v022 = to_mt9v022(client);
138 /* Almost the default mode: master, parallel, simultaneous, and an
139 * undocumented bit 0x200, which is present in table 7, but not in 8,
140 * plus snapshot mode to disable scan for now */
141 mt9v022->chip_control |= 0x10;
142 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
144 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
149 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
151 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
154 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
156 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
161 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
163 struct i2c_client *client = sd->priv;
164 struct mt9v022 *mt9v022 = to_mt9v022(client);
167 /* Switch to master "normal" mode */
168 mt9v022->chip_control &= ~0x10;
170 /* Switch to snapshot mode */
171 mt9v022->chip_control |= 0x10;
173 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
178 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
181 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
182 struct mt9v022 *mt9v022 = to_mt9v022(client);
183 struct soc_camera_link *icl = to_soc_camera_link(icd);
184 unsigned int 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 ret = icl->set_bus_param(icl, width_flag);
198 * Without board specific bus width settings we only support the
199 * sensors native bus width
201 if (width_flag != SOCAM_DATAWIDTH_10)
205 flags = soc_camera_apply_sensor_flags(icl, flags);
207 if (flags & SOCAM_PCLK_SAMPLE_RISING)
210 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
213 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
216 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
220 if (!(flags & SOCAM_MASTER))
221 mt9v022->chip_control &= ~0x8;
223 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
227 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
228 pixclk, mt9v022->chip_control);
233 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
235 struct soc_camera_link *icl = to_soc_camera_link(icd);
236 unsigned int width_flag;
238 if (icl->query_bus_param)
239 width_flag = icl->query_bus_param(icl) &
240 SOCAM_DATAWIDTH_MASK;
242 width_flag = SOCAM_DATAWIDTH_10;
244 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
245 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
246 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
247 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
251 static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
253 struct v4l2_rect *rect = &a->c;
254 struct i2c_client *client = sd->priv;
255 struct soc_camera_device *icd = client->dev.platform_data;
258 /* Like in example app. Contradicts the datasheet though */
259 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
261 if (ret & 1) /* Autoexposure */
262 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
263 rect->height + icd->y_skip_top + 43);
265 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
266 rect->height + icd->y_skip_top + 43);
268 /* Setup frame format: defaults apart from width and height */
270 ret = reg_write(client, MT9V022_COLUMN_START, rect->left);
272 ret = reg_write(client, MT9V022_ROW_START, rect->top);
274 /* Default 94, Phytec driver says:
275 * "width + horizontal blank >= 660" */
276 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
277 rect->width > 660 - 43 ? 43 :
280 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
282 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect->width);
284 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
285 rect->height + icd->y_skip_top);
290 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
295 static int mt9v022_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
297 struct i2c_client *client = sd->priv;
298 struct mt9v022 *mt9v022 = to_mt9v022(client);
299 struct soc_camera_device *icd = client->dev.platform_data;
300 struct v4l2_pix_format *pix = &f->fmt.pix;
301 struct v4l2_crop a = {
303 .left = icd->rect_current.left,
304 .top = icd->rect_current.top,
306 .height = pix->height,
310 /* The caller provides a supported format, as verified per call to
311 * icd->try_fmt(), datawidth is from our supported format list */
312 switch (pix->pixelformat) {
313 case V4L2_PIX_FMT_GREY:
314 case V4L2_PIX_FMT_Y16:
315 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
318 case V4L2_PIX_FMT_SBGGR8:
319 case V4L2_PIX_FMT_SBGGR16:
320 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
324 /* No format change, only geometry */
330 /* No support for scaling on this camera, just crop. */
331 return mt9v022_s_crop(sd, &a);
334 static int mt9v022_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
336 struct i2c_client *client = sd->priv;
337 struct soc_camera_device *icd = client->dev.platform_data;
338 struct v4l2_pix_format *pix = &f->fmt.pix;
340 v4l_bound_align_image(&pix->width, 48, 752, 2 /* ? */,
341 &pix->height, 32 + icd->y_skip_top,
342 480 + icd->y_skip_top, 0, 0);
347 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
348 struct v4l2_dbg_chip_ident *id)
350 struct i2c_client *client = sd->priv;
351 struct mt9v022 *mt9v022 = to_mt9v022(client);
353 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
356 if (id->match.addr != client->addr)
359 id->ident = mt9v022->model;
365 #ifdef CONFIG_VIDEO_ADV_DEBUG
366 static int mt9v022_g_register(struct v4l2_subdev *sd,
367 struct v4l2_dbg_register *reg)
369 struct i2c_client *client = sd->priv;
371 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
374 if (reg->match.addr != client->addr)
378 reg->val = reg_read(client, reg->reg);
380 if (reg->val > 0xffff)
386 static int mt9v022_s_register(struct v4l2_subdev *sd,
387 struct v4l2_dbg_register *reg)
389 struct i2c_client *client = sd->priv;
391 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
394 if (reg->match.addr != client->addr)
397 if (reg_write(client, reg->reg, reg->val) < 0)
404 static const struct v4l2_queryctrl mt9v022_controls[] = {
406 .id = V4L2_CID_VFLIP,
407 .type = V4L2_CTRL_TYPE_BOOLEAN,
408 .name = "Flip Vertically",
414 .id = V4L2_CID_HFLIP,
415 .type = V4L2_CTRL_TYPE_BOOLEAN,
416 .name = "Flip Horizontally",
423 .type = V4L2_CTRL_TYPE_INTEGER,
424 .name = "Analog Gain",
429 .flags = V4L2_CTRL_FLAG_SLIDER,
431 .id = V4L2_CID_EXPOSURE,
432 .type = V4L2_CTRL_TYPE_INTEGER,
437 .default_value = 255,
438 .flags = V4L2_CTRL_FLAG_SLIDER,
440 .id = V4L2_CID_AUTOGAIN,
441 .type = V4L2_CTRL_TYPE_BOOLEAN,
442 .name = "Automatic Gain",
448 .id = V4L2_CID_EXPOSURE_AUTO,
449 .type = V4L2_CTRL_TYPE_BOOLEAN,
450 .name = "Automatic Exposure",
458 static struct soc_camera_ops mt9v022_ops = {
459 .init = mt9v022_init,
460 .set_bus_param = mt9v022_set_bus_param,
461 .query_bus_param = mt9v022_query_bus_param,
462 .controls = mt9v022_controls,
463 .num_controls = ARRAY_SIZE(mt9v022_controls),
466 static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
468 struct i2c_client *client = sd->priv;
473 data = reg_read(client, MT9V022_READ_MODE);
476 ctrl->value = !!(data & 0x10);
479 data = reg_read(client, MT9V022_READ_MODE);
482 ctrl->value = !!(data & 0x20);
484 case V4L2_CID_EXPOSURE_AUTO:
485 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
488 ctrl->value = !!(data & 0x1);
490 case V4L2_CID_AUTOGAIN:
491 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
494 ctrl->value = !!(data & 0x2);
500 static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
503 struct i2c_client *client = sd->priv;
504 struct soc_camera_device *icd = client->dev.platform_data;
505 const struct v4l2_queryctrl *qctrl;
507 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
514 data = reg_set(client, MT9V022_READ_MODE, 0x10);
516 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
522 data = reg_set(client, MT9V022_READ_MODE, 0x20);
524 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
529 /* mt9v022 has minimum == default */
530 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
533 unsigned long range = qctrl->maximum - qctrl->minimum;
534 /* Datasheet says 16 to 64. autogain only works properly
535 * after setting gain to maximum 14. Larger values
536 * produce "white fly" noise effect. On the whole,
537 * manually setting analog gain does no good. */
538 unsigned long gain = ((ctrl->value - qctrl->minimum) *
539 10 + range / 2) / range + 4;
542 /* The user wants to set gain manually, hope, she
543 * knows, what she's doing... Switch AGC off. */
545 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
548 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
549 reg_read(client, MT9V022_ANALOG_GAIN), gain);
550 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
552 icd->gain = ctrl->value;
555 case V4L2_CID_EXPOSURE:
556 /* mt9v022 has maximum == default */
557 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
560 unsigned long range = qctrl->maximum - qctrl->minimum;
561 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
562 479 + range / 2) / range + 1;
563 /* The user wants to set shutter width manually, hope,
564 * she knows, what she's doing... Switch AEC off. */
566 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
569 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
570 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
572 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
575 icd->exposure = ctrl->value;
578 case V4L2_CID_AUTOGAIN:
580 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
582 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
586 case V4L2_CID_EXPOSURE_AUTO:
588 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
590 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
598 /* Interface active, can use i2c. If it fails, it can indeed mean, that
599 * this wasn't our capture interface, so, we wait for the right one */
600 static int mt9v022_video_probe(struct soc_camera_device *icd,
601 struct i2c_client *client)
603 struct mt9v022 *mt9v022 = to_mt9v022(client);
604 struct soc_camera_link *icl = to_soc_camera_link(icd);
609 if (!icd->dev.parent ||
610 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
613 /* Read out the chip version register */
614 data = reg_read(client, MT9V022_CHIP_VERSION);
616 /* must be 0x1311 or 0x1313 */
617 if (data != 0x1311 && data != 0x1313) {
619 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
625 ret = reg_write(client, MT9V022_RESET, 1);
628 /* 15 clock cycles */
630 if (reg_read(client, MT9V022_RESET)) {
631 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
637 /* Set monochrome or colour sensor type */
638 if (sensor_type && (!strcmp("colour", sensor_type) ||
639 !strcmp("color", sensor_type))) {
640 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
641 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
642 icd->formats = mt9v022_colour_formats;
644 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
645 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
646 icd->formats = mt9v022_monochrome_formats;
652 icd->num_formats = 0;
655 * This is a 10bit sensor, so by default we only allow 10bit.
656 * The platform may support different bus widths due to
657 * different routing of the data lines.
659 if (icl->query_bus_param)
660 flags = icl->query_bus_param(icl);
662 flags = SOCAM_DATAWIDTH_10;
664 if (flags & SOCAM_DATAWIDTH_10)
669 if (flags & SOCAM_DATAWIDTH_8)
672 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
673 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
674 "monochrome" : "colour");
680 static void mt9v022_video_remove(struct soc_camera_device *icd)
682 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
683 struct soc_camera_link *icl = to_soc_camera_link(icd);
685 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", client->addr,
686 icd->dev.parent, icd->vdev);
691 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
692 .g_ctrl = mt9v022_g_ctrl,
693 .s_ctrl = mt9v022_s_ctrl,
694 .g_chip_ident = mt9v022_g_chip_ident,
695 #ifdef CONFIG_VIDEO_ADV_DEBUG
696 .g_register = mt9v022_g_register,
697 .s_register = mt9v022_s_register,
701 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
702 .s_stream = mt9v022_s_stream,
703 .s_fmt = mt9v022_s_fmt,
704 .try_fmt = mt9v022_try_fmt,
705 .s_crop = mt9v022_s_crop,
708 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
709 .core = &mt9v022_subdev_core_ops,
710 .video = &mt9v022_subdev_video_ops,
713 static int mt9v022_probe(struct i2c_client *client,
714 const struct i2c_device_id *did)
716 struct mt9v022 *mt9v022;
717 struct soc_camera_device *icd = client->dev.platform_data;
718 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
719 struct soc_camera_link *icl;
723 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
727 icl = to_soc_camera_link(icd);
729 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
733 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
734 dev_warn(&adapter->dev,
735 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
739 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
743 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
745 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
747 icd->ops = &mt9v022_ops;
748 icd->rect_max.left = 1;
749 icd->rect_max.top = 4;
750 icd->rect_max.width = 752;
751 icd->rect_max.height = 480;
752 icd->rect_current.left = 1;
753 icd->rect_current.top = 4;
755 icd->height_min = 32;
758 ret = mt9v022_video_probe(icd, client);
761 i2c_set_clientdata(client, NULL);
768 static int mt9v022_remove(struct i2c_client *client)
770 struct mt9v022 *mt9v022 = to_mt9v022(client);
771 struct soc_camera_device *icd = client->dev.platform_data;
774 mt9v022_video_remove(icd);
775 i2c_set_clientdata(client, NULL);
776 client->driver = NULL;
781 static const struct i2c_device_id mt9v022_id[] = {
785 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
787 static struct i2c_driver mt9v022_i2c_driver = {
791 .probe = mt9v022_probe,
792 .remove = mt9v022_remove,
793 .id_table = mt9v022_id,
796 static int __init mt9v022_mod_init(void)
798 return i2c_add_driver(&mt9v022_i2c_driver);
801 static void __exit mt9v022_mod_exit(void)
803 i2c_del_driver(&mt9v022_i2c_driver);
806 module_init(mt9v022_mod_init);
807 module_exit(mt9v022_mod_exit);
809 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
810 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
811 MODULE_LICENSE("GPL");