2 * Driver for MT9T031 CMOS Image Sensor from Micron
4 * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.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/device.h>
12 #include <linux/i2c.h>
13 #include <linux/log2.h>
15 #include <linux/slab.h>
16 #include <linux/videodev2.h>
18 #include <media/soc_camera.h>
19 #include <media/soc_mediabus.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/v4l2-subdev.h>
24 * mt9t031 i2c address 0x5d
25 * The platform has to define i2c_board_info and link to it from
26 * struct soc_camera_link
29 /* mt9t031 selected register addresses */
30 #define MT9T031_CHIP_VERSION 0x00
31 #define MT9T031_ROW_START 0x01
32 #define MT9T031_COLUMN_START 0x02
33 #define MT9T031_WINDOW_HEIGHT 0x03
34 #define MT9T031_WINDOW_WIDTH 0x04
35 #define MT9T031_HORIZONTAL_BLANKING 0x05
36 #define MT9T031_VERTICAL_BLANKING 0x06
37 #define MT9T031_OUTPUT_CONTROL 0x07
38 #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
39 #define MT9T031_SHUTTER_WIDTH 0x09
40 #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
41 #define MT9T031_FRAME_RESTART 0x0b
42 #define MT9T031_SHUTTER_DELAY 0x0c
43 #define MT9T031_RESET 0x0d
44 #define MT9T031_READ_MODE_1 0x1e
45 #define MT9T031_READ_MODE_2 0x20
46 #define MT9T031_READ_MODE_3 0x21
47 #define MT9T031_ROW_ADDRESS_MODE 0x22
48 #define MT9T031_COLUMN_ADDRESS_MODE 0x23
49 #define MT9T031_GLOBAL_GAIN 0x35
50 #define MT9T031_CHIP_ENABLE 0xF8
52 #define MT9T031_MAX_HEIGHT 1536
53 #define MT9T031_MAX_WIDTH 2048
54 #define MT9T031_MIN_HEIGHT 2
55 #define MT9T031_MIN_WIDTH 18
56 #define MT9T031_HORIZONTAL_BLANK 142
57 #define MT9T031_VERTICAL_BLANK 25
58 #define MT9T031_COLUMN_SKIP 32
59 #define MT9T031_ROW_SKIP 20
62 struct v4l2_subdev subdev;
63 struct v4l2_rect rect; /* Sensor window */
64 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
68 unsigned short y_skip_top; /* Lines to skip at the top */
69 unsigned int exposure;
70 unsigned char autoexposure;
73 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
75 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
78 static int reg_read(struct i2c_client *client, const u8 reg)
80 s32 data = i2c_smbus_read_word_data(client, reg);
81 return data < 0 ? data : swab16(data);
84 static int reg_write(struct i2c_client *client, const u8 reg,
87 return i2c_smbus_write_word_data(client, reg, swab16(data));
90 static int reg_set(struct i2c_client *client, const u8 reg,
95 ret = reg_read(client, reg);
98 return reg_write(client, reg, ret | data);
101 static int reg_clear(struct i2c_client *client, const u8 reg,
106 ret = reg_read(client, reg);
109 return reg_write(client, reg, ret & ~data);
112 static int set_shutter(struct i2c_client *client, const u32 data)
116 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
119 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
124 static int get_shutter(struct i2c_client *client, u32 *data)
128 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
132 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
133 *data |= ret & 0xffff;
135 return ret < 0 ? ret : 0;
138 static int mt9t031_idle(struct i2c_client *client)
142 /* Disable chip output, synchronous option update */
143 ret = reg_write(client, MT9T031_RESET, 1);
145 ret = reg_write(client, MT9T031_RESET, 0);
147 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
149 return ret >= 0 ? 0 : -EIO;
152 static int mt9t031_disable(struct i2c_client *client)
154 /* Disable the chip */
155 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
160 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
162 struct i2c_client *client = v4l2_get_subdevdata(sd);
166 /* Switch to master "normal" mode */
167 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
169 /* Stop sensor readout */
170 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
182 MT9T031_CTRL_EXPOSURE,
183 MT9T031_CTRL_EXPOSURE_AUTO,
186 static const struct v4l2_queryctrl mt9t031_controls[] = {
187 [MT9T031_CTRL_VFLIP] = {
188 .id = V4L2_CID_VFLIP,
189 .type = V4L2_CTRL_TYPE_BOOLEAN,
190 .name = "Flip Vertically",
196 [MT9T031_CTRL_HFLIP] = {
197 .id = V4L2_CID_HFLIP,
198 .type = V4L2_CTRL_TYPE_BOOLEAN,
199 .name = "Flip Horizontally",
205 [MT9T031_CTRL_GAIN] = {
207 .type = V4L2_CTRL_TYPE_INTEGER,
213 .flags = V4L2_CTRL_FLAG_SLIDER,
215 [MT9T031_CTRL_EXPOSURE] = {
216 .id = V4L2_CID_EXPOSURE,
217 .type = V4L2_CTRL_TYPE_INTEGER,
222 .default_value = 255,
223 .flags = V4L2_CTRL_FLAG_SLIDER,
225 [MT9T031_CTRL_EXPOSURE_AUTO] = {
226 .id = V4L2_CID_EXPOSURE_AUTO,
227 .type = V4L2_CTRL_TYPE_BOOLEAN,
228 .name = "Automatic Exposure",
236 static struct soc_camera_ops mt9t031_ops = {
237 .controls = mt9t031_controls,
238 .num_controls = ARRAY_SIZE(mt9t031_controls),
241 /* target must be _even_ */
242 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
246 if (*source < target + target / 2) {
251 skip = min(max, *source + target / 2) / target;
254 *source = target * skip;
259 /* rect is the sensor rectangle, the caller guarantees parameter validity */
260 static int mt9t031_set_params(struct i2c_client *client,
261 struct v4l2_rect *rect, u16 xskip, u16 yskip)
263 struct mt9t031 *mt9t031 = to_mt9t031(client);
266 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
267 vblank = MT9T031_VERTICAL_BLANK;
269 xbin = min(xskip, (u16)3);
270 ybin = min(yskip, (u16)3);
273 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
274 * There is always a valid suitably aligned value. The worst case is
275 * xbin = 3, width = 2048. Then we will start at 36, the last read out
276 * pixel will be 2083, which is < 2085 - first black pixel.
278 * MT9T031 datasheet imposes window left border alignment, depending on
279 * the selected xskip. Failing to conform to this requirement produces
280 * dark horizontal stripes in the image. However, even obeying to this
281 * requirement doesn't eliminate the stripes in all configurations. They
282 * appear "locally reproducibly," but can differ between tests under
283 * different lighting conditions.
293 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
294 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
299 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
300 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
302 /* Disable register update, reconfigure atomically */
303 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
307 /* Blanking and start values - default... */
308 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
310 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
312 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
313 /* Binning, skipping */
315 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
316 ((xbin - 1) << 4) | (xskip - 1));
318 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
319 ((ybin - 1) << 4) | (yskip - 1));
321 dev_dbg(&client->dev, "new physical left %u, top %u\n",
322 rect->left, rect->top);
325 * The caller provides a supported format, as guaranteed by
326 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
329 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
331 ret = reg_write(client, MT9T031_ROW_START, rect->top);
333 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
335 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
336 rect->height + mt9t031->y_skip_top - 1);
337 if (ret >= 0 && mt9t031->autoexposure) {
338 unsigned int total_h = rect->height + mt9t031->y_skip_top + vblank;
339 ret = set_shutter(client, total_h);
341 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
342 const struct v4l2_queryctrl *qctrl =
343 &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
344 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
345 (qctrl->maximum - qctrl->minimum)) /
346 shutter_max + qctrl->minimum;
350 /* Re-enable register update, commit all changes */
352 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
355 mt9t031->rect = *rect;
356 mt9t031->xskip = xskip;
357 mt9t031->yskip = yskip;
360 return ret < 0 ? ret : 0;
363 static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
365 struct v4l2_rect rect = a->c;
366 struct i2c_client *client = v4l2_get_subdevdata(sd);
367 struct mt9t031 *mt9t031 = to_mt9t031(client);
369 rect.width = ALIGN(rect.width, 2);
370 rect.height = ALIGN(rect.height, 2);
372 soc_camera_limit_side(&rect.left, &rect.width,
373 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
375 soc_camera_limit_side(&rect.top, &rect.height,
376 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
378 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
381 static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
383 struct i2c_client *client = v4l2_get_subdevdata(sd);
384 struct mt9t031 *mt9t031 = to_mt9t031(client);
386 a->c = mt9t031->rect;
387 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
392 static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
394 a->bounds.left = MT9T031_COLUMN_SKIP;
395 a->bounds.top = MT9T031_ROW_SKIP;
396 a->bounds.width = MT9T031_MAX_WIDTH;
397 a->bounds.height = MT9T031_MAX_HEIGHT;
398 a->defrect = a->bounds;
399 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
400 a->pixelaspect.numerator = 1;
401 a->pixelaspect.denominator = 1;
406 static int mt9t031_g_fmt(struct v4l2_subdev *sd,
407 struct v4l2_mbus_framefmt *mf)
409 struct i2c_client *client = v4l2_get_subdevdata(sd);
410 struct mt9t031 *mt9t031 = to_mt9t031(client);
412 mf->width = mt9t031->rect.width / mt9t031->xskip;
413 mf->height = mt9t031->rect.height / mt9t031->yskip;
414 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
415 mf->colorspace = V4L2_COLORSPACE_SRGB;
416 mf->field = V4L2_FIELD_NONE;
421 static int mt9t031_s_fmt(struct v4l2_subdev *sd,
422 struct v4l2_mbus_framefmt *mf)
424 struct i2c_client *client = v4l2_get_subdevdata(sd);
425 struct mt9t031 *mt9t031 = to_mt9t031(client);
427 struct v4l2_rect rect = mt9t031->rect;
430 * try_fmt has put width and height within limits.
431 * S_FMT: use binning and skipping for scaling
433 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
434 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
436 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
437 mf->colorspace = V4L2_COLORSPACE_SRGB;
439 /* mt9t031_set_params() doesn't change width and height */
440 return mt9t031_set_params(client, &rect, xskip, yskip);
444 * If a user window larger than sensor window is requested, we'll increase the
447 static int mt9t031_try_fmt(struct v4l2_subdev *sd,
448 struct v4l2_mbus_framefmt *mf)
450 v4l_bound_align_image(
451 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
452 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
454 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
455 mf->colorspace = V4L2_COLORSPACE_SRGB;
460 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
461 struct v4l2_dbg_chip_ident *id)
463 struct i2c_client *client = v4l2_get_subdevdata(sd);
464 struct mt9t031 *mt9t031 = to_mt9t031(client);
466 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
469 if (id->match.addr != client->addr)
472 id->ident = mt9t031->model;
478 #ifdef CONFIG_VIDEO_ADV_DEBUG
479 static int mt9t031_g_register(struct v4l2_subdev *sd,
480 struct v4l2_dbg_register *reg)
482 struct i2c_client *client = v4l2_get_subdevdata(sd);
484 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
487 if (reg->match.addr != client->addr)
490 reg->val = reg_read(client, reg->reg);
492 if (reg->val > 0xffff)
498 static int mt9t031_s_register(struct v4l2_subdev *sd,
499 struct v4l2_dbg_register *reg)
501 struct i2c_client *client = v4l2_get_subdevdata(sd);
503 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
506 if (reg->match.addr != client->addr)
509 if (reg_write(client, reg->reg, reg->val) < 0)
516 static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
518 struct i2c_client *client = v4l2_get_subdevdata(sd);
519 struct mt9t031 *mt9t031 = to_mt9t031(client);
524 data = reg_read(client, MT9T031_READ_MODE_2);
527 ctrl->value = !!(data & 0x8000);
530 data = reg_read(client, MT9T031_READ_MODE_2);
533 ctrl->value = !!(data & 0x4000);
535 case V4L2_CID_EXPOSURE_AUTO:
536 ctrl->value = mt9t031->autoexposure;
539 ctrl->value = mt9t031->gain;
541 case V4L2_CID_EXPOSURE:
542 ctrl->value = mt9t031->exposure;
548 static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
550 struct i2c_client *client = v4l2_get_subdevdata(sd);
551 struct mt9t031 *mt9t031 = to_mt9t031(client);
552 const struct v4l2_queryctrl *qctrl;
558 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
560 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
566 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
568 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
573 qctrl = &mt9t031_controls[MT9T031_CTRL_GAIN];
574 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
576 /* See Datasheet Table 7, Gain settings. */
577 if (ctrl->value <= qctrl->default_value) {
578 /* Pack it into 0..1 step 0.125, register values 0..8 */
579 unsigned long range = qctrl->default_value - qctrl->minimum;
580 data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
582 dev_dbg(&client->dev, "Setting gain %d\n", data);
583 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
587 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
588 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
589 unsigned long range = qctrl->maximum - qctrl->default_value - 1;
590 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
591 unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
592 1015 + range / 2) / range + 9;
594 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
596 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
597 data = ((gain - 32) * 16 + 16) / 32 + 80;
599 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
600 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
602 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
603 reg_read(client, MT9T031_GLOBAL_GAIN), data);
604 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
610 mt9t031->gain = ctrl->value;
612 case V4L2_CID_EXPOSURE:
613 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
614 /* mt9t031 has maximum == default */
615 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
618 const unsigned long range = qctrl->maximum - qctrl->minimum;
619 const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
620 range / 2) / range + 1;
623 get_shutter(client, &old);
624 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
626 if (set_shutter(client, shutter) < 0)
628 mt9t031->exposure = ctrl->value;
629 mt9t031->autoexposure = 0;
632 case V4L2_CID_EXPOSURE_AUTO:
634 const u16 vblank = MT9T031_VERTICAL_BLANK;
635 const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
636 unsigned int total_h = mt9t031->rect.height +
637 mt9t031->y_skip_top + vblank;
639 if (set_shutter(client, total_h) < 0)
641 qctrl = &mt9t031_controls[MT9T031_CTRL_EXPOSURE];
642 mt9t031->exposure = (shutter_max / 2 + (total_h - 1) *
643 (qctrl->maximum - qctrl->minimum)) /
644 shutter_max + qctrl->minimum;
645 mt9t031->autoexposure = 1;
647 mt9t031->autoexposure = 0;
657 * This function does nothing for now but must be present for pm to work
659 static int mt9t031_runtime_suspend(struct device *dev)
666 * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
667 * they are however changed at reset if the platform hook is present
668 * thus we rewrite them with the values stored by the driver
670 static int mt9t031_runtime_resume(struct device *dev)
672 struct video_device *vdev = to_video_device(dev);
673 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
674 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
675 struct i2c_client *client = v4l2_get_subdevdata(sd);
676 struct mt9t031 *mt9t031 = to_mt9t031(client);
681 xbin = min(mt9t031->xskip, (u16)3);
682 ybin = min(mt9t031->yskip, (u16)3);
684 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
685 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
689 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
690 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
697 static struct dev_pm_ops mt9t031_dev_pm_ops = {
698 .runtime_suspend = mt9t031_runtime_suspend,
699 .runtime_resume = mt9t031_runtime_resume,
702 static struct device_type mt9t031_dev_type = {
704 .pm = &mt9t031_dev_pm_ops,
708 * Interface active, can use i2c. If it fails, it can indeed mean, that
709 * this wasn't our capture interface, so, we wait for the right one
711 static int mt9t031_video_probe(struct i2c_client *client)
713 struct mt9t031 *mt9t031 = to_mt9t031(client);
714 struct video_device *vdev = soc_camera_i2c_to_vdev(client);
718 /* Enable the chip */
719 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
720 dev_dbg(&client->dev, "write: %d\n", data);
722 /* Read out the chip version register */
723 data = reg_read(client, MT9T031_CHIP_VERSION);
727 mt9t031->model = V4L2_IDENT_MT9T031;
730 dev_err(&client->dev,
731 "No MT9T031 chip detected, register read %x\n", data);
735 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
737 ret = mt9t031_idle(client);
739 dev_err(&client->dev, "Failed to initialise the camera\n");
741 vdev->dev.type = &mt9t031_dev_type;
743 /* mt9t031_idle() has reset the chip to default. */
744 mt9t031->exposure = 255;
750 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
752 struct i2c_client *client = v4l2_get_subdevdata(sd);
753 struct mt9t031 *mt9t031 = to_mt9t031(client);
755 *lines = mt9t031->y_skip_top;
760 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
761 .g_ctrl = mt9t031_g_ctrl,
762 .s_ctrl = mt9t031_s_ctrl,
763 .g_chip_ident = mt9t031_g_chip_ident,
764 #ifdef CONFIG_VIDEO_ADV_DEBUG
765 .g_register = mt9t031_g_register,
766 .s_register = mt9t031_s_register,
770 static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
771 enum v4l2_mbus_pixelcode *code)
776 *code = V4L2_MBUS_FMT_SBGGR10_1X10;
780 static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
781 struct v4l2_mbus_config *cfg)
783 struct i2c_client *client = v4l2_get_subdevdata(sd);
784 struct soc_camera_device *icd = client->dev.platform_data;
785 struct soc_camera_link *icl = to_soc_camera_link(icd);
787 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
788 V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
789 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
790 cfg->type = V4L2_MBUS_PARALLEL;
791 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
796 static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
797 const struct v4l2_mbus_config *cfg)
799 struct i2c_client *client = v4l2_get_subdevdata(sd);
800 struct soc_camera_device *icd = client->dev.platform_data;
801 struct soc_camera_link *icl = to_soc_camera_link(icd);
803 if (soc_camera_apply_board_flags(icl, cfg) &
804 V4L2_MBUS_PCLK_SAMPLE_FALLING)
805 return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
807 return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
810 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
811 .s_stream = mt9t031_s_stream,
812 .s_mbus_fmt = mt9t031_s_fmt,
813 .g_mbus_fmt = mt9t031_g_fmt,
814 .try_mbus_fmt = mt9t031_try_fmt,
815 .s_crop = mt9t031_s_crop,
816 .g_crop = mt9t031_g_crop,
817 .cropcap = mt9t031_cropcap,
818 .enum_mbus_fmt = mt9t031_enum_fmt,
819 .g_mbus_config = mt9t031_g_mbus_config,
820 .s_mbus_config = mt9t031_s_mbus_config,
823 static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
824 .g_skip_top_lines = mt9t031_g_skip_top_lines,
827 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
828 .core = &mt9t031_subdev_core_ops,
829 .video = &mt9t031_subdev_video_ops,
830 .sensor = &mt9t031_subdev_sensor_ops,
833 static int mt9t031_probe(struct i2c_client *client,
834 const struct i2c_device_id *did)
836 struct mt9t031 *mt9t031;
837 struct soc_camera_device *icd = client->dev.platform_data;
838 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
842 struct soc_camera_link *icl = to_soc_camera_link(icd);
844 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
848 icd->ops = &mt9t031_ops;
851 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
852 dev_warn(&adapter->dev,
853 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
857 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
861 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
863 mt9t031->y_skip_top = 0;
864 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
865 mt9t031->rect.top = MT9T031_ROW_SKIP;
866 mt9t031->rect.width = MT9T031_MAX_WIDTH;
867 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
870 * Simulated autoexposure. If enabled, we calculate shutter width
871 * ourselves in the driver based on vertical blanking and frame width
873 mt9t031->autoexposure = 1;
878 mt9t031_idle(client);
880 ret = mt9t031_video_probe(client);
882 mt9t031_disable(client);
893 static int mt9t031_remove(struct i2c_client *client)
895 struct mt9t031 *mt9t031 = to_mt9t031(client);
896 struct soc_camera_device *icd = client->dev.platform_data;
905 static const struct i2c_device_id mt9t031_id[] = {
909 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
911 static struct i2c_driver mt9t031_i2c_driver = {
915 .probe = mt9t031_probe,
916 .remove = mt9t031_remove,
917 .id_table = mt9t031_id,
920 static int __init mt9t031_mod_init(void)
922 return i2c_add_driver(&mt9t031_i2c_driver);
925 static void __exit mt9t031_mod_exit(void)
927 i2c_del_driver(&mt9t031_i2c_driver);
930 module_init(mt9t031_mod_init);
931 module_exit(mt9t031_mod_exit);
933 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
934 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
935 MODULE_LICENSE("GPL v2");