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/v4l2-mediabus.h>
17 #include <linux/videodev2.h>
18 #include <linux/module.h>
20 #include <media/soc_camera.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/v4l2-subdev.h>
23 #include <media/v4l2-ctrls.h>
26 * ATTENTION: this driver still cannot be used outside of the soc-camera
27 * framework because of its PM implementation, using the video_device node.
28 * If hardware becomes available for testing, alternative PM approaches shall
29 * be considered and tested.
33 * mt9t031 i2c address 0x5d
34 * The platform has to define i2c_board_info and link to it from
35 * struct soc_camera_link
38 /* mt9t031 selected register addresses */
39 #define MT9T031_CHIP_VERSION 0x00
40 #define MT9T031_ROW_START 0x01
41 #define MT9T031_COLUMN_START 0x02
42 #define MT9T031_WINDOW_HEIGHT 0x03
43 #define MT9T031_WINDOW_WIDTH 0x04
44 #define MT9T031_HORIZONTAL_BLANKING 0x05
45 #define MT9T031_VERTICAL_BLANKING 0x06
46 #define MT9T031_OUTPUT_CONTROL 0x07
47 #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
48 #define MT9T031_SHUTTER_WIDTH 0x09
49 #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
50 #define MT9T031_FRAME_RESTART 0x0b
51 #define MT9T031_SHUTTER_DELAY 0x0c
52 #define MT9T031_RESET 0x0d
53 #define MT9T031_READ_MODE_1 0x1e
54 #define MT9T031_READ_MODE_2 0x20
55 #define MT9T031_READ_MODE_3 0x21
56 #define MT9T031_ROW_ADDRESS_MODE 0x22
57 #define MT9T031_COLUMN_ADDRESS_MODE 0x23
58 #define MT9T031_GLOBAL_GAIN 0x35
59 #define MT9T031_CHIP_ENABLE 0xF8
61 #define MT9T031_MAX_HEIGHT 1536
62 #define MT9T031_MAX_WIDTH 2048
63 #define MT9T031_MIN_HEIGHT 2
64 #define MT9T031_MIN_WIDTH 18
65 #define MT9T031_HORIZONTAL_BLANK 142
66 #define MT9T031_VERTICAL_BLANK 25
67 #define MT9T031_COLUMN_SKIP 32
68 #define MT9T031_ROW_SKIP 20
71 struct v4l2_subdev subdev;
72 struct v4l2_ctrl_handler hdl;
74 /* exposure/auto-exposure cluster */
75 struct v4l2_ctrl *autoexposure;
76 struct v4l2_ctrl *exposure;
78 struct v4l2_rect rect; /* Sensor window */
79 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
83 unsigned short y_skip_top; /* Lines to skip at the top */
86 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
88 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
91 static int reg_read(struct i2c_client *client, const u8 reg)
93 s32 data = i2c_smbus_read_word_data(client, reg);
94 return data < 0 ? data : swab16(data);
97 static int reg_write(struct i2c_client *client, const u8 reg,
100 return i2c_smbus_write_word_data(client, reg, swab16(data));
103 static int reg_set(struct i2c_client *client, const u8 reg,
108 ret = reg_read(client, reg);
111 return reg_write(client, reg, ret | data);
114 static int reg_clear(struct i2c_client *client, const u8 reg,
119 ret = reg_read(client, reg);
122 return reg_write(client, reg, ret & ~data);
125 static int set_shutter(struct i2c_client *client, const u32 data)
129 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
132 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
137 static int get_shutter(struct i2c_client *client, u32 *data)
141 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
145 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
146 *data |= ret & 0xffff;
148 return ret < 0 ? ret : 0;
151 static int mt9t031_idle(struct i2c_client *client)
155 /* Disable chip output, synchronous option update */
156 ret = reg_write(client, MT9T031_RESET, 1);
158 ret = reg_write(client, MT9T031_RESET, 0);
160 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
162 return ret >= 0 ? 0 : -EIO;
165 static int mt9t031_disable(struct i2c_client *client)
167 /* Disable the chip */
168 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
173 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
175 struct i2c_client *client = v4l2_get_subdevdata(sd);
179 /* Switch to master "normal" mode */
180 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
182 /* Stop sensor readout */
183 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
191 /* target must be _even_ */
192 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
196 if (*source < target + target / 2) {
201 skip = min(max, *source + target / 2) / target;
204 *source = target * skip;
209 /* rect is the sensor rectangle, the caller guarantees parameter validity */
210 static int mt9t031_set_params(struct i2c_client *client,
211 struct v4l2_rect *rect, u16 xskip, u16 yskip)
213 struct mt9t031 *mt9t031 = to_mt9t031(client);
216 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
217 vblank = MT9T031_VERTICAL_BLANK;
219 xbin = min(xskip, (u16)3);
220 ybin = min(yskip, (u16)3);
223 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
224 * There is always a valid suitably aligned value. The worst case is
225 * xbin = 3, width = 2048. Then we will start at 36, the last read out
226 * pixel will be 2083, which is < 2085 - first black pixel.
228 * MT9T031 datasheet imposes window left border alignment, depending on
229 * the selected xskip. Failing to conform to this requirement produces
230 * dark horizontal stripes in the image. However, even obeying to this
231 * requirement doesn't eliminate the stripes in all configurations. They
232 * appear "locally reproducibly," but can differ between tests under
233 * different lighting conditions.
243 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
244 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
249 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
250 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
252 /* Disable register update, reconfigure atomically */
253 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
257 /* Blanking and start values - default... */
258 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
260 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
262 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
263 /* Binning, skipping */
265 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
266 ((xbin - 1) << 4) | (xskip - 1));
268 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
269 ((ybin - 1) << 4) | (yskip - 1));
271 dev_dbg(&client->dev, "new physical left %u, top %u\n",
272 rect->left, rect->top);
275 * The caller provides a supported format, as guaranteed by
276 * .try_mbus_fmt(), soc_camera_s_crop() and soc_camera_cropcap()
279 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
281 ret = reg_write(client, MT9T031_ROW_START, rect->top);
283 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
285 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
286 rect->height + mt9t031->y_skip_top - 1);
287 if (ret >= 0 && v4l2_ctrl_g_ctrl(mt9t031->autoexposure) == V4L2_EXPOSURE_AUTO) {
288 mt9t031->total_h = rect->height + mt9t031->y_skip_top + vblank;
290 ret = set_shutter(client, mt9t031->total_h);
293 /* Re-enable register update, commit all changes */
295 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
298 mt9t031->rect = *rect;
299 mt9t031->xskip = xskip;
300 mt9t031->yskip = yskip;
303 return ret < 0 ? ret : 0;
306 static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
308 struct v4l2_rect rect = a->c;
309 struct i2c_client *client = v4l2_get_subdevdata(sd);
310 struct mt9t031 *mt9t031 = to_mt9t031(client);
312 rect.width = ALIGN(rect.width, 2);
313 rect.height = ALIGN(rect.height, 2);
315 soc_camera_limit_side(&rect.left, &rect.width,
316 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
318 soc_camera_limit_side(&rect.top, &rect.height,
319 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
321 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
324 static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
326 struct i2c_client *client = v4l2_get_subdevdata(sd);
327 struct mt9t031 *mt9t031 = to_mt9t031(client);
329 a->c = mt9t031->rect;
330 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
335 static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
337 a->bounds.left = MT9T031_COLUMN_SKIP;
338 a->bounds.top = MT9T031_ROW_SKIP;
339 a->bounds.width = MT9T031_MAX_WIDTH;
340 a->bounds.height = MT9T031_MAX_HEIGHT;
341 a->defrect = a->bounds;
342 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
343 a->pixelaspect.numerator = 1;
344 a->pixelaspect.denominator = 1;
349 static int mt9t031_g_fmt(struct v4l2_subdev *sd,
350 struct v4l2_mbus_framefmt *mf)
352 struct i2c_client *client = v4l2_get_subdevdata(sd);
353 struct mt9t031 *mt9t031 = to_mt9t031(client);
355 mf->width = mt9t031->rect.width / mt9t031->xskip;
356 mf->height = mt9t031->rect.height / mt9t031->yskip;
357 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
358 mf->colorspace = V4L2_COLORSPACE_SRGB;
359 mf->field = V4L2_FIELD_NONE;
364 static int mt9t031_s_fmt(struct v4l2_subdev *sd,
365 struct v4l2_mbus_framefmt *mf)
367 struct i2c_client *client = v4l2_get_subdevdata(sd);
368 struct mt9t031 *mt9t031 = to_mt9t031(client);
370 struct v4l2_rect rect = mt9t031->rect;
373 * try_fmt has put width and height within limits.
374 * S_FMT: use binning and skipping for scaling
376 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
377 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
379 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
380 mf->colorspace = V4L2_COLORSPACE_SRGB;
382 /* mt9t031_set_params() doesn't change width and height */
383 return mt9t031_set_params(client, &rect, xskip, yskip);
387 * If a user window larger than sensor window is requested, we'll increase the
390 static int mt9t031_try_fmt(struct v4l2_subdev *sd,
391 struct v4l2_mbus_framefmt *mf)
393 v4l_bound_align_image(
394 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
395 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
397 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
398 mf->colorspace = V4L2_COLORSPACE_SRGB;
403 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
404 struct v4l2_dbg_chip_ident *id)
406 struct i2c_client *client = v4l2_get_subdevdata(sd);
407 struct mt9t031 *mt9t031 = to_mt9t031(client);
409 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
412 if (id->match.addr != client->addr)
415 id->ident = mt9t031->model;
421 #ifdef CONFIG_VIDEO_ADV_DEBUG
422 static int mt9t031_g_register(struct v4l2_subdev *sd,
423 struct v4l2_dbg_register *reg)
425 struct i2c_client *client = v4l2_get_subdevdata(sd);
427 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
430 if (reg->match.addr != client->addr)
433 reg->val = reg_read(client, reg->reg);
435 if (reg->val > 0xffff)
441 static int mt9t031_s_register(struct v4l2_subdev *sd,
442 struct v4l2_dbg_register *reg)
444 struct i2c_client *client = v4l2_get_subdevdata(sd);
446 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
449 if (reg->match.addr != client->addr)
452 if (reg_write(client, reg->reg, reg->val) < 0)
459 static int mt9t031_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
461 struct mt9t031 *mt9t031 = container_of(ctrl->handler,
462 struct mt9t031, hdl);
463 const u32 shutter_max = MT9T031_MAX_HEIGHT + MT9T031_VERTICAL_BLANK;
467 case V4L2_CID_EXPOSURE_AUTO:
468 min = mt9t031->exposure->minimum;
469 max = mt9t031->exposure->maximum;
470 mt9t031->exposure->val =
471 (shutter_max / 2 + (mt9t031->total_h - 1) * (max - min))
478 static int mt9t031_s_ctrl(struct v4l2_ctrl *ctrl)
480 struct mt9t031 *mt9t031 = container_of(ctrl->handler,
481 struct mt9t031, hdl);
482 struct v4l2_subdev *sd = &mt9t031->subdev;
483 struct i2c_client *client = v4l2_get_subdevdata(sd);
484 struct v4l2_ctrl *exp = mt9t031->exposure;
490 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
492 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
498 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
500 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
505 /* See Datasheet Table 7, Gain settings. */
506 if (ctrl->val <= ctrl->default_value) {
507 /* Pack it into 0..1 step 0.125, register values 0..8 */
508 unsigned long range = ctrl->default_value - ctrl->minimum;
509 data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
511 dev_dbg(&client->dev, "Setting gain %d\n", data);
512 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
516 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
517 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
518 unsigned long range = ctrl->maximum - ctrl->default_value - 1;
519 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
520 unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
521 1015 + range / 2) / range + 9;
523 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
525 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
526 data = ((gain - 32) * 16 + 16) / 32 + 80;
528 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
529 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
531 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
532 reg_read(client, MT9T031_GLOBAL_GAIN), data);
533 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
539 case V4L2_CID_EXPOSURE_AUTO:
540 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
541 unsigned int range = exp->maximum - exp->minimum;
542 unsigned int shutter = ((exp->val - exp->minimum) * 1048 +
543 range / 2) / range + 1;
546 get_shutter(client, &old);
547 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
549 if (set_shutter(client, shutter) < 0)
552 const u16 vblank = MT9T031_VERTICAL_BLANK;
553 mt9t031->total_h = mt9t031->rect.height +
554 mt9t031->y_skip_top + vblank;
556 if (set_shutter(client, mt9t031->total_h) < 0)
568 * This function does nothing for now but must be present for pm to work
570 static int mt9t031_runtime_suspend(struct device *dev)
577 * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
578 * they are however changed at reset if the platform hook is present
579 * thus we rewrite them with the values stored by the driver
581 static int mt9t031_runtime_resume(struct device *dev)
583 struct video_device *vdev = to_video_device(dev);
584 struct v4l2_subdev *sd = soc_camera_vdev_to_subdev(vdev);
585 struct i2c_client *client = v4l2_get_subdevdata(sd);
586 struct mt9t031 *mt9t031 = to_mt9t031(client);
591 xbin = min(mt9t031->xskip, (u16)3);
592 ybin = min(mt9t031->yskip, (u16)3);
594 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
595 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
599 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
600 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
607 static struct dev_pm_ops mt9t031_dev_pm_ops = {
608 .runtime_suspend = mt9t031_runtime_suspend,
609 .runtime_resume = mt9t031_runtime_resume,
612 static struct device_type mt9t031_dev_type = {
614 .pm = &mt9t031_dev_pm_ops,
617 static int mt9t031_s_power(struct v4l2_subdev *sd, int on)
619 struct i2c_client *client = v4l2_get_subdevdata(sd);
620 struct video_device *vdev = soc_camera_i2c_to_vdev(client);
623 vdev->dev.type = &mt9t031_dev_type;
625 vdev->dev.type = NULL;
631 * Interface active, can use i2c. If it fails, it can indeed mean, that
632 * this wasn't our capture interface, so, we wait for the right one
634 static int mt9t031_video_probe(struct i2c_client *client)
636 struct mt9t031 *mt9t031 = to_mt9t031(client);
640 /* Enable the chip */
641 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
642 dev_dbg(&client->dev, "write: %d\n", data);
644 /* Read out the chip version register */
645 data = reg_read(client, MT9T031_CHIP_VERSION);
649 mt9t031->model = V4L2_IDENT_MT9T031;
652 dev_err(&client->dev,
653 "No MT9T031 chip detected, register read %x\n", data);
657 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
659 ret = mt9t031_idle(client);
661 dev_err(&client->dev, "Failed to initialise the camera\n");
663 v4l2_ctrl_handler_setup(&mt9t031->hdl);
668 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
670 struct i2c_client *client = v4l2_get_subdevdata(sd);
671 struct mt9t031 *mt9t031 = to_mt9t031(client);
673 *lines = mt9t031->y_skip_top;
678 static const struct v4l2_ctrl_ops mt9t031_ctrl_ops = {
679 .g_volatile_ctrl = mt9t031_g_volatile_ctrl,
680 .s_ctrl = mt9t031_s_ctrl,
683 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
684 .g_chip_ident = mt9t031_g_chip_ident,
685 .s_power = mt9t031_s_power,
686 #ifdef CONFIG_VIDEO_ADV_DEBUG
687 .g_register = mt9t031_g_register,
688 .s_register = mt9t031_s_register,
692 static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
693 enum v4l2_mbus_pixelcode *code)
698 *code = V4L2_MBUS_FMT_SBGGR10_1X10;
702 static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
703 struct v4l2_mbus_config *cfg)
705 struct i2c_client *client = v4l2_get_subdevdata(sd);
706 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
708 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
709 V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
710 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
711 cfg->type = V4L2_MBUS_PARALLEL;
712 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
717 static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
718 const struct v4l2_mbus_config *cfg)
720 struct i2c_client *client = v4l2_get_subdevdata(sd);
721 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
723 if (soc_camera_apply_board_flags(icl, cfg) &
724 V4L2_MBUS_PCLK_SAMPLE_FALLING)
725 return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
727 return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
730 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
731 .s_stream = mt9t031_s_stream,
732 .s_mbus_fmt = mt9t031_s_fmt,
733 .g_mbus_fmt = mt9t031_g_fmt,
734 .try_mbus_fmt = mt9t031_try_fmt,
735 .s_crop = mt9t031_s_crop,
736 .g_crop = mt9t031_g_crop,
737 .cropcap = mt9t031_cropcap,
738 .enum_mbus_fmt = mt9t031_enum_fmt,
739 .g_mbus_config = mt9t031_g_mbus_config,
740 .s_mbus_config = mt9t031_s_mbus_config,
743 static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
744 .g_skip_top_lines = mt9t031_g_skip_top_lines,
747 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
748 .core = &mt9t031_subdev_core_ops,
749 .video = &mt9t031_subdev_video_ops,
750 .sensor = &mt9t031_subdev_sensor_ops,
753 static int mt9t031_probe(struct i2c_client *client,
754 const struct i2c_device_id *did)
756 struct mt9t031 *mt9t031;
757 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
758 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
762 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
766 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
767 dev_warn(&adapter->dev,
768 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
772 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
776 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
777 v4l2_ctrl_handler_init(&mt9t031->hdl, 5);
778 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
779 V4L2_CID_VFLIP, 0, 1, 1, 0);
780 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
781 V4L2_CID_HFLIP, 0, 1, 1, 0);
782 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
783 V4L2_CID_GAIN, 0, 127, 1, 64);
786 * Simulated autoexposure. If enabled, we calculate shutter width
787 * ourselves in the driver based on vertical blanking and frame width
789 mt9t031->autoexposure = v4l2_ctrl_new_std_menu(&mt9t031->hdl,
790 &mt9t031_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
792 mt9t031->exposure = v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
793 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
795 mt9t031->subdev.ctrl_handler = &mt9t031->hdl;
796 if (mt9t031->hdl.error) {
797 int err = mt9t031->hdl.error;
802 v4l2_ctrl_auto_cluster(2, &mt9t031->autoexposure,
803 V4L2_EXPOSURE_MANUAL, true);
805 mt9t031->y_skip_top = 0;
806 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
807 mt9t031->rect.top = MT9T031_ROW_SKIP;
808 mt9t031->rect.width = MT9T031_MAX_WIDTH;
809 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
814 mt9t031_idle(client);
816 ret = mt9t031_video_probe(client);
818 mt9t031_disable(client);
821 v4l2_ctrl_handler_free(&mt9t031->hdl);
828 static int mt9t031_remove(struct i2c_client *client)
830 struct mt9t031 *mt9t031 = to_mt9t031(client);
832 v4l2_device_unregister_subdev(&mt9t031->subdev);
833 v4l2_ctrl_handler_free(&mt9t031->hdl);
839 static const struct i2c_device_id mt9t031_id[] = {
843 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
845 static struct i2c_driver mt9t031_i2c_driver = {
849 .probe = mt9t031_probe,
850 .remove = mt9t031_remove,
851 .id_table = mt9t031_id,
854 static int __init mt9t031_mod_init(void)
856 return i2c_add_driver(&mt9t031_i2c_driver);
859 static void __exit mt9t031_mod_exit(void)
861 i2c_del_driver(&mt9t031_i2c_driver);
864 module_init(mt9t031_mod_init);
865 module_exit(mt9t031_mod_exit);
867 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
868 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
869 MODULE_LICENSE("GPL v2");