1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for MT9T031 CMOS Image Sensor from Micron
5 * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.de>
7 #include <linux/device.h>
9 #include <linux/log2.h>
11 #include <linux/slab.h>
12 #include <linux/v4l2-mediabus.h>
13 #include <linux/videodev2.h>
14 #include <linux/module.h>
16 #include <media/soc_camera.h>
17 #include <media/v4l2-clk.h>
18 #include <media/v4l2-subdev.h>
19 #include <media/v4l2-ctrls.h>
22 * ATTENTION: this driver still cannot be used outside of the soc-camera
23 * framework because of its PM implementation, using the video_device node.
24 * If hardware becomes available for testing, alternative PM approaches shall
25 * be considered and tested.
29 * mt9t031 i2c address 0x5d
30 * The platform has to define struct i2c_board_info objects and link to them
31 * from struct soc_camera_host_desc
34 /* mt9t031 selected register addresses */
35 #define MT9T031_CHIP_VERSION 0x00
36 #define MT9T031_ROW_START 0x01
37 #define MT9T031_COLUMN_START 0x02
38 #define MT9T031_WINDOW_HEIGHT 0x03
39 #define MT9T031_WINDOW_WIDTH 0x04
40 #define MT9T031_HORIZONTAL_BLANKING 0x05
41 #define MT9T031_VERTICAL_BLANKING 0x06
42 #define MT9T031_OUTPUT_CONTROL 0x07
43 #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
44 #define MT9T031_SHUTTER_WIDTH 0x09
45 #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
46 #define MT9T031_FRAME_RESTART 0x0b
47 #define MT9T031_SHUTTER_DELAY 0x0c
48 #define MT9T031_RESET 0x0d
49 #define MT9T031_READ_MODE_1 0x1e
50 #define MT9T031_READ_MODE_2 0x20
51 #define MT9T031_READ_MODE_3 0x21
52 #define MT9T031_ROW_ADDRESS_MODE 0x22
53 #define MT9T031_COLUMN_ADDRESS_MODE 0x23
54 #define MT9T031_GLOBAL_GAIN 0x35
55 #define MT9T031_CHIP_ENABLE 0xF8
57 #define MT9T031_MAX_HEIGHT 1536
58 #define MT9T031_MAX_WIDTH 2048
59 #define MT9T031_MIN_HEIGHT 2
60 #define MT9T031_MIN_WIDTH 18
61 #define MT9T031_HORIZONTAL_BLANK 142
62 #define MT9T031_VERTICAL_BLANK 25
63 #define MT9T031_COLUMN_SKIP 32
64 #define MT9T031_ROW_SKIP 20
67 struct v4l2_subdev subdev;
68 struct v4l2_ctrl_handler hdl;
70 /* exposure/auto-exposure cluster */
71 struct v4l2_ctrl *autoexposure;
72 struct v4l2_ctrl *exposure;
74 struct v4l2_rect rect; /* Sensor window */
79 unsigned short y_skip_top; /* Lines to skip at the top */
82 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
84 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
87 static int reg_read(struct i2c_client *client, const u8 reg)
89 return i2c_smbus_read_word_swapped(client, reg);
92 static int reg_write(struct i2c_client *client, const u8 reg,
95 return i2c_smbus_write_word_swapped(client, reg, data);
98 static int reg_set(struct i2c_client *client, const u8 reg,
103 ret = reg_read(client, reg);
106 return reg_write(client, reg, ret | data);
109 static int reg_clear(struct i2c_client *client, const u8 reg,
114 ret = reg_read(client, reg);
117 return reg_write(client, reg, ret & ~data);
120 static int set_shutter(struct i2c_client *client, const u32 data)
124 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
127 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
132 static int get_shutter(struct i2c_client *client, u32 *data)
136 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
140 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
141 *data |= ret & 0xffff;
143 return ret < 0 ? ret : 0;
146 static int mt9t031_idle(struct i2c_client *client)
150 /* Disable chip output, synchronous option update */
151 ret = reg_write(client, MT9T031_RESET, 1);
153 ret = reg_write(client, MT9T031_RESET, 0);
155 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
157 return ret >= 0 ? 0 : -EIO;
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);
178 /* target must be _even_ */
179 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
183 if (*source < target + target / 2) {
188 skip = min(max, *source + target / 2) / target;
191 *source = target * skip;
196 /* rect is the sensor rectangle, the caller guarantees parameter validity */
197 static int mt9t031_set_params(struct i2c_client *client,
198 struct v4l2_rect *rect, u16 xskip, u16 yskip)
200 struct mt9t031 *mt9t031 = to_mt9t031(client);
203 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
204 vblank = MT9T031_VERTICAL_BLANK;
206 xbin = min(xskip, (u16)3);
207 ybin = min(yskip, (u16)3);
210 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
211 * There is always a valid suitably aligned value. The worst case is
212 * xbin = 3, width = 2048. Then we will start at 36, the last read out
213 * pixel will be 2083, which is < 2085 - first black pixel.
215 * MT9T031 datasheet imposes window left border alignment, depending on
216 * the selected xskip. Failing to conform to this requirement produces
217 * dark horizontal stripes in the image. However, even obeying to this
218 * requirement doesn't eliminate the stripes in all configurations. They
219 * appear "locally reproducibly," but can differ between tests under
220 * different lighting conditions.
230 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
231 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
236 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
237 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
239 /* Disable register update, reconfigure atomically */
240 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
244 /* Blanking and start values - default... */
245 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
247 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
249 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
250 /* Binning, skipping */
252 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
253 ((xbin - 1) << 4) | (xskip - 1));
255 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
256 ((ybin - 1) << 4) | (yskip - 1));
258 dev_dbg(&client->dev, "new physical left %u, top %u\n",
259 rect->left, rect->top);
262 * The caller provides a supported format, as guaranteed by
263 * .set_fmt(FORMAT_TRY), soc_camera_s_selection() and soc_camera_cropcap()
266 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
268 ret = reg_write(client, MT9T031_ROW_START, rect->top);
270 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
272 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
273 rect->height + mt9t031->y_skip_top - 1);
274 if (ret >= 0 && v4l2_ctrl_g_ctrl(mt9t031->autoexposure) == V4L2_EXPOSURE_AUTO) {
275 mt9t031->total_h = rect->height + mt9t031->y_skip_top + vblank;
277 ret = set_shutter(client, mt9t031->total_h);
280 /* Re-enable register update, commit all changes */
282 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
285 mt9t031->rect = *rect;
286 mt9t031->xskip = xskip;
287 mt9t031->yskip = yskip;
290 return ret < 0 ? ret : 0;
293 static int mt9t031_set_selection(struct v4l2_subdev *sd,
294 struct v4l2_subdev_pad_config *cfg,
295 struct v4l2_subdev_selection *sel)
297 struct i2c_client *client = v4l2_get_subdevdata(sd);
298 struct mt9t031 *mt9t031 = to_mt9t031(client);
299 struct v4l2_rect rect = sel->r;
301 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE ||
302 sel->target != V4L2_SEL_TGT_CROP)
305 rect.width = ALIGN(rect.width, 2);
306 rect.height = ALIGN(rect.height, 2);
308 soc_camera_limit_side(&rect.left, &rect.width,
309 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
311 soc_camera_limit_side(&rect.top, &rect.height,
312 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
314 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
317 static int mt9t031_get_selection(struct v4l2_subdev *sd,
318 struct v4l2_subdev_pad_config *cfg,
319 struct v4l2_subdev_selection *sel)
321 struct i2c_client *client = v4l2_get_subdevdata(sd);
322 struct mt9t031 *mt9t031 = to_mt9t031(client);
324 if (sel->which != V4L2_SUBDEV_FORMAT_ACTIVE)
327 switch (sel->target) {
328 case V4L2_SEL_TGT_CROP_BOUNDS:
329 sel->r.left = MT9T031_COLUMN_SKIP;
330 sel->r.top = MT9T031_ROW_SKIP;
331 sel->r.width = MT9T031_MAX_WIDTH;
332 sel->r.height = MT9T031_MAX_HEIGHT;
334 case V4L2_SEL_TGT_CROP:
335 sel->r = mt9t031->rect;
342 static int mt9t031_get_fmt(struct v4l2_subdev *sd,
343 struct v4l2_subdev_pad_config *cfg,
344 struct v4l2_subdev_format *format)
346 struct v4l2_mbus_framefmt *mf = &format->format;
347 struct i2c_client *client = v4l2_get_subdevdata(sd);
348 struct mt9t031 *mt9t031 = to_mt9t031(client);
353 mf->width = mt9t031->rect.width / mt9t031->xskip;
354 mf->height = mt9t031->rect.height / mt9t031->yskip;
355 mf->code = MEDIA_BUS_FMT_SBGGR10_1X10;
356 mf->colorspace = V4L2_COLORSPACE_SRGB;
357 mf->field = V4L2_FIELD_NONE;
363 * If a user window larger than sensor window is requested, we'll increase the
366 static int mt9t031_set_fmt(struct v4l2_subdev *sd,
367 struct v4l2_subdev_pad_config *cfg,
368 struct v4l2_subdev_format *format)
370 struct v4l2_mbus_framefmt *mf = &format->format;
371 struct i2c_client *client = v4l2_get_subdevdata(sd);
372 struct mt9t031 *mt9t031 = to_mt9t031(client);
374 struct v4l2_rect rect = mt9t031->rect;
379 mf->code = MEDIA_BUS_FMT_SBGGR10_1X10;
380 mf->colorspace = V4L2_COLORSPACE_SRGB;
381 v4l_bound_align_image(
382 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
383 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
385 if (format->which == V4L2_SUBDEV_FORMAT_TRY) {
391 * Width and height are within limits.
392 * S_FMT: use binning and skipping for scaling
394 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
395 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
397 mf->code = MEDIA_BUS_FMT_SBGGR10_1X10;
398 mf->colorspace = V4L2_COLORSPACE_SRGB;
400 /* mt9t031_set_params() doesn't change width and height */
401 return mt9t031_set_params(client, &rect, xskip, yskip);
404 #ifdef CONFIG_VIDEO_ADV_DEBUG
405 static int mt9t031_g_register(struct v4l2_subdev *sd,
406 struct v4l2_dbg_register *reg)
408 struct i2c_client *client = v4l2_get_subdevdata(sd);
414 reg->val = reg_read(client, reg->reg);
416 if (reg->val > 0xffff)
422 static int mt9t031_s_register(struct v4l2_subdev *sd,
423 const struct v4l2_dbg_register *reg)
425 struct i2c_client *client = v4l2_get_subdevdata(sd);
430 if (reg_write(client, reg->reg, reg->val) < 0)
437 static int mt9t031_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
439 struct mt9t031 *mt9t031 = container_of(ctrl->handler,
440 struct mt9t031, hdl);
441 const u32 shutter_max = MT9T031_MAX_HEIGHT + MT9T031_VERTICAL_BLANK;
445 case V4L2_CID_EXPOSURE_AUTO:
446 min = mt9t031->exposure->minimum;
447 max = mt9t031->exposure->maximum;
448 mt9t031->exposure->val =
449 (shutter_max / 2 + (mt9t031->total_h - 1) * (max - min))
456 static int mt9t031_s_ctrl(struct v4l2_ctrl *ctrl)
458 struct mt9t031 *mt9t031 = container_of(ctrl->handler,
459 struct mt9t031, hdl);
460 struct v4l2_subdev *sd = &mt9t031->subdev;
461 struct i2c_client *client = v4l2_get_subdevdata(sd);
462 struct v4l2_ctrl *exp = mt9t031->exposure;
468 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
470 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
476 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
478 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
483 /* See Datasheet Table 7, Gain settings. */
484 if (ctrl->val <= ctrl->default_value) {
485 /* Pack it into 0..1 step 0.125, register values 0..8 */
486 unsigned long range = ctrl->default_value - ctrl->minimum;
487 data = ((ctrl->val - (s32)ctrl->minimum) * 8 + range / 2) / range;
489 dev_dbg(&client->dev, "Setting gain %d\n", data);
490 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
494 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
495 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
496 unsigned long range = ctrl->maximum - ctrl->default_value - 1;
497 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
498 unsigned long gain = ((ctrl->val - (s32)ctrl->default_value - 1) *
499 1015 + range / 2) / range + 9;
501 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
503 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
504 data = ((gain - 32) * 16 + 16) / 32 + 80;
506 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
507 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
509 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
510 reg_read(client, MT9T031_GLOBAL_GAIN), data);
511 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
517 case V4L2_CID_EXPOSURE_AUTO:
518 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
519 unsigned int range = exp->maximum - exp->minimum;
520 unsigned int shutter = ((exp->val - (s32)exp->minimum) * 1048 +
521 range / 2) / range + 1;
524 get_shutter(client, &old);
525 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
527 if (set_shutter(client, shutter) < 0)
530 const u16 vblank = MT9T031_VERTICAL_BLANK;
531 mt9t031->total_h = mt9t031->rect.height +
532 mt9t031->y_skip_top + vblank;
534 if (set_shutter(client, mt9t031->total_h) < 0)
546 * This function does nothing for now but must be present for pm to work
548 static int mt9t031_runtime_suspend(struct device *dev)
555 * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
556 * they are however changed at reset if the platform hook is present
557 * thus we rewrite them with the values stored by the driver
559 static int mt9t031_runtime_resume(struct device *dev)
561 struct video_device *vdev = to_video_device(dev);
562 struct v4l2_subdev *sd = soc_camera_vdev_to_subdev(vdev);
563 struct i2c_client *client = v4l2_get_subdevdata(sd);
564 struct mt9t031 *mt9t031 = to_mt9t031(client);
569 xbin = min(mt9t031->xskip, (u16)3);
570 ybin = min(mt9t031->yskip, (u16)3);
572 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
573 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
577 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
578 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
585 static const struct dev_pm_ops mt9t031_dev_pm_ops = {
586 .runtime_suspend = mt9t031_runtime_suspend,
587 .runtime_resume = mt9t031_runtime_resume,
590 static const struct device_type mt9t031_dev_type = {
592 .pm = &mt9t031_dev_pm_ops,
595 static int mt9t031_s_power(struct v4l2_subdev *sd, int on)
597 struct i2c_client *client = v4l2_get_subdevdata(sd);
598 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
599 struct video_device *vdev = soc_camera_i2c_to_vdev(client);
600 struct mt9t031 *mt9t031 = to_mt9t031(client);
604 ret = soc_camera_power_on(&client->dev, ssdd, mt9t031->clk);
608 /* Not needed during probing, when vdev isn't available yet */
609 vdev->dev.type = &mt9t031_dev_type;
612 vdev->dev.type = NULL;
613 soc_camera_power_off(&client->dev, ssdd, mt9t031->clk);
620 * Interface active, can use i2c. If it fails, it can indeed mean, that
621 * this wasn't our capture interface, so, we wait for the right one
623 static int mt9t031_video_probe(struct i2c_client *client)
625 struct mt9t031 *mt9t031 = to_mt9t031(client);
629 ret = mt9t031_s_power(&mt9t031->subdev, 1);
633 ret = mt9t031_idle(client);
635 dev_err(&client->dev, "Failed to initialise the camera\n");
639 /* Read out the chip version register */
640 data = reg_read(client, MT9T031_CHIP_VERSION);
646 dev_err(&client->dev,
647 "No MT9T031 chip detected, register read %x\n", data);
652 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
654 ret = v4l2_ctrl_handler_setup(&mt9t031->hdl);
657 mt9t031_s_power(&mt9t031->subdev, 0);
662 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
664 struct i2c_client *client = v4l2_get_subdevdata(sd);
665 struct mt9t031 *mt9t031 = to_mt9t031(client);
667 *lines = mt9t031->y_skip_top;
672 static const struct v4l2_ctrl_ops mt9t031_ctrl_ops = {
673 .g_volatile_ctrl = mt9t031_g_volatile_ctrl,
674 .s_ctrl = mt9t031_s_ctrl,
677 static const struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
678 .s_power = mt9t031_s_power,
679 #ifdef CONFIG_VIDEO_ADV_DEBUG
680 .g_register = mt9t031_g_register,
681 .s_register = mt9t031_s_register,
685 static int mt9t031_enum_mbus_code(struct v4l2_subdev *sd,
686 struct v4l2_subdev_pad_config *cfg,
687 struct v4l2_subdev_mbus_code_enum *code)
689 if (code->pad || code->index)
692 code->code = MEDIA_BUS_FMT_SBGGR10_1X10;
696 static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
697 struct v4l2_mbus_config *cfg)
699 struct i2c_client *client = v4l2_get_subdevdata(sd);
700 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
702 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
703 V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
704 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
705 cfg->type = V4L2_MBUS_PARALLEL;
706 cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
711 static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
712 const struct v4l2_mbus_config *cfg)
714 struct i2c_client *client = v4l2_get_subdevdata(sd);
715 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
717 if (soc_camera_apply_board_flags(ssdd, cfg) &
718 V4L2_MBUS_PCLK_SAMPLE_FALLING)
719 return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
721 return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
724 static const struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
725 .s_stream = mt9t031_s_stream,
726 .g_mbus_config = mt9t031_g_mbus_config,
727 .s_mbus_config = mt9t031_s_mbus_config,
730 static const struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
731 .g_skip_top_lines = mt9t031_g_skip_top_lines,
734 static const struct v4l2_subdev_pad_ops mt9t031_subdev_pad_ops = {
735 .enum_mbus_code = mt9t031_enum_mbus_code,
736 .get_selection = mt9t031_get_selection,
737 .set_selection = mt9t031_set_selection,
738 .get_fmt = mt9t031_get_fmt,
739 .set_fmt = mt9t031_set_fmt,
742 static const struct v4l2_subdev_ops mt9t031_subdev_ops = {
743 .core = &mt9t031_subdev_core_ops,
744 .video = &mt9t031_subdev_video_ops,
745 .sensor = &mt9t031_subdev_sensor_ops,
746 .pad = &mt9t031_subdev_pad_ops,
749 static int mt9t031_probe(struct i2c_client *client,
750 const struct i2c_device_id *did)
752 struct mt9t031 *mt9t031;
753 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
754 struct i2c_adapter *adapter = client->adapter;
758 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
762 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
763 dev_warn(&adapter->dev,
764 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
768 mt9t031 = devm_kzalloc(&client->dev, sizeof(struct mt9t031), GFP_KERNEL);
772 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
773 v4l2_ctrl_handler_init(&mt9t031->hdl, 5);
774 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
775 V4L2_CID_VFLIP, 0, 1, 1, 0);
776 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
777 V4L2_CID_HFLIP, 0, 1, 1, 0);
778 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
779 V4L2_CID_GAIN, 0, 127, 1, 64);
782 * Simulated autoexposure. If enabled, we calculate shutter width
783 * ourselves in the driver based on vertical blanking and frame width
785 mt9t031->autoexposure = v4l2_ctrl_new_std_menu(&mt9t031->hdl,
786 &mt9t031_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
788 mt9t031->exposure = v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
789 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
791 mt9t031->subdev.ctrl_handler = &mt9t031->hdl;
792 if (mt9t031->hdl.error)
793 return mt9t031->hdl.error;
795 v4l2_ctrl_auto_cluster(2, &mt9t031->autoexposure,
796 V4L2_EXPOSURE_MANUAL, true);
798 mt9t031->y_skip_top = 0;
799 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
800 mt9t031->rect.top = MT9T031_ROW_SKIP;
801 mt9t031->rect.width = MT9T031_MAX_WIDTH;
802 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
807 mt9t031->clk = v4l2_clk_get(&client->dev, "mclk");
808 if (IS_ERR(mt9t031->clk)) {
809 ret = PTR_ERR(mt9t031->clk);
813 ret = mt9t031_video_probe(client);
815 v4l2_clk_put(mt9t031->clk);
817 v4l2_ctrl_handler_free(&mt9t031->hdl);
823 static int mt9t031_remove(struct i2c_client *client)
825 struct mt9t031 *mt9t031 = to_mt9t031(client);
827 v4l2_clk_put(mt9t031->clk);
828 v4l2_device_unregister_subdev(&mt9t031->subdev);
829 v4l2_ctrl_handler_free(&mt9t031->hdl);
834 static const struct i2c_device_id mt9t031_id[] = {
838 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
840 static struct i2c_driver mt9t031_i2c_driver = {
844 .probe = mt9t031_probe,
845 .remove = mt9t031_remove,
846 .id_table = mt9t031_id,
849 module_i2c_driver(mt9t031_i2c_driver);
851 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
852 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
853 MODULE_LICENSE("GPL v2");