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>
22 #include <media/v4l2-ctrls.h>
25 * mt9t031 i2c address 0x5d
26 * The platform has to define i2c_board_info and link to it from
27 * struct soc_camera_link
30 /* mt9t031 selected register addresses */
31 #define MT9T031_CHIP_VERSION 0x00
32 #define MT9T031_ROW_START 0x01
33 #define MT9T031_COLUMN_START 0x02
34 #define MT9T031_WINDOW_HEIGHT 0x03
35 #define MT9T031_WINDOW_WIDTH 0x04
36 #define MT9T031_HORIZONTAL_BLANKING 0x05
37 #define MT9T031_VERTICAL_BLANKING 0x06
38 #define MT9T031_OUTPUT_CONTROL 0x07
39 #define MT9T031_SHUTTER_WIDTH_UPPER 0x08
40 #define MT9T031_SHUTTER_WIDTH 0x09
41 #define MT9T031_PIXEL_CLOCK_CONTROL 0x0a
42 #define MT9T031_FRAME_RESTART 0x0b
43 #define MT9T031_SHUTTER_DELAY 0x0c
44 #define MT9T031_RESET 0x0d
45 #define MT9T031_READ_MODE_1 0x1e
46 #define MT9T031_READ_MODE_2 0x20
47 #define MT9T031_READ_MODE_3 0x21
48 #define MT9T031_ROW_ADDRESS_MODE 0x22
49 #define MT9T031_COLUMN_ADDRESS_MODE 0x23
50 #define MT9T031_GLOBAL_GAIN 0x35
51 #define MT9T031_CHIP_ENABLE 0xF8
53 #define MT9T031_MAX_HEIGHT 1536
54 #define MT9T031_MAX_WIDTH 2048
55 #define MT9T031_MIN_HEIGHT 2
56 #define MT9T031_MIN_WIDTH 18
57 #define MT9T031_HORIZONTAL_BLANK 142
58 #define MT9T031_VERTICAL_BLANK 25
59 #define MT9T031_COLUMN_SKIP 32
60 #define MT9T031_ROW_SKIP 20
63 struct v4l2_subdev subdev;
64 struct v4l2_ctrl_handler hdl;
66 /* exposure/auto-exposure cluster */
67 struct v4l2_ctrl *autoexposure;
68 struct v4l2_ctrl *exposure;
70 struct v4l2_rect rect; /* Sensor window */
71 int model; /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
75 unsigned short y_skip_top; /* Lines to skip at the top */
78 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
80 return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
83 static int reg_read(struct i2c_client *client, const u8 reg)
85 s32 data = i2c_smbus_read_word_data(client, reg);
86 return data < 0 ? data : swab16(data);
89 static int reg_write(struct i2c_client *client, const u8 reg,
92 return i2c_smbus_write_word_data(client, reg, swab16(data));
95 static int reg_set(struct i2c_client *client, const u8 reg,
100 ret = reg_read(client, reg);
103 return reg_write(client, reg, ret | data);
106 static int reg_clear(struct i2c_client *client, const u8 reg,
111 ret = reg_read(client, reg);
114 return reg_write(client, reg, ret & ~data);
117 static int set_shutter(struct i2c_client *client, const u32 data)
121 ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
124 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
129 static int get_shutter(struct i2c_client *client, u32 *data)
133 ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
137 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
138 *data |= ret & 0xffff;
140 return ret < 0 ? ret : 0;
143 static int mt9t031_idle(struct i2c_client *client)
147 /* Disable chip output, synchronous option update */
148 ret = reg_write(client, MT9T031_RESET, 1);
150 ret = reg_write(client, MT9T031_RESET, 0);
152 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
154 return ret >= 0 ? 0 : -EIO;
157 static int mt9t031_disable(struct i2c_client *client)
159 /* Disable the chip */
160 reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
165 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
167 struct i2c_client *client = v4l2_get_subdevdata(sd);
171 /* Switch to master "normal" mode */
172 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
174 /* Stop sensor readout */
175 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
183 /* target must be _even_ */
184 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
188 if (*source < target + target / 2) {
193 skip = min(max, *source + target / 2) / target;
196 *source = target * skip;
201 /* rect is the sensor rectangle, the caller guarantees parameter validity */
202 static int mt9t031_set_params(struct i2c_client *client,
203 struct v4l2_rect *rect, u16 xskip, u16 yskip)
205 struct mt9t031 *mt9t031 = to_mt9t031(client);
208 const u16 hblank = MT9T031_HORIZONTAL_BLANK,
209 vblank = MT9T031_VERTICAL_BLANK;
211 xbin = min(xskip, (u16)3);
212 ybin = min(yskip, (u16)3);
215 * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
216 * There is always a valid suitably aligned value. The worst case is
217 * xbin = 3, width = 2048. Then we will start at 36, the last read out
218 * pixel will be 2083, which is < 2085 - first black pixel.
220 * MT9T031 datasheet imposes window left border alignment, depending on
221 * the selected xskip. Failing to conform to this requirement produces
222 * dark horizontal stripes in the image. However, even obeying to this
223 * requirement doesn't eliminate the stripes in all configurations. They
224 * appear "locally reproducibly," but can differ between tests under
225 * different lighting conditions.
235 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
236 (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
241 dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
242 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
244 /* Disable register update, reconfigure atomically */
245 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
249 /* Blanking and start values - default... */
250 ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
252 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
254 if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
255 /* Binning, skipping */
257 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
258 ((xbin - 1) << 4) | (xskip - 1));
260 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
261 ((ybin - 1) << 4) | (yskip - 1));
263 dev_dbg(&client->dev, "new physical left %u, top %u\n",
264 rect->left, rect->top);
267 * The caller provides a supported format, as guaranteed by
268 * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap()
271 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
273 ret = reg_write(client, MT9T031_ROW_START, rect->top);
275 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
277 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
278 rect->height + mt9t031->y_skip_top - 1);
279 if (ret >= 0 && v4l2_ctrl_g_ctrl(mt9t031->autoexposure) == V4L2_EXPOSURE_AUTO) {
280 mt9t031->total_h = rect->height + mt9t031->y_skip_top + vblank;
282 ret = set_shutter(client, mt9t031->total_h);
285 /* Re-enable register update, commit all changes */
287 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
290 mt9t031->rect = *rect;
291 mt9t031->xskip = xskip;
292 mt9t031->yskip = yskip;
295 return ret < 0 ? ret : 0;
298 static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
300 struct v4l2_rect rect = a->c;
301 struct i2c_client *client = v4l2_get_subdevdata(sd);
302 struct mt9t031 *mt9t031 = to_mt9t031(client);
304 rect.width = ALIGN(rect.width, 2);
305 rect.height = ALIGN(rect.height, 2);
307 soc_camera_limit_side(&rect.left, &rect.width,
308 MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
310 soc_camera_limit_side(&rect.top, &rect.height,
311 MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
313 return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
316 static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
318 struct i2c_client *client = v4l2_get_subdevdata(sd);
319 struct mt9t031 *mt9t031 = to_mt9t031(client);
321 a->c = mt9t031->rect;
322 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
327 static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
329 a->bounds.left = MT9T031_COLUMN_SKIP;
330 a->bounds.top = MT9T031_ROW_SKIP;
331 a->bounds.width = MT9T031_MAX_WIDTH;
332 a->bounds.height = MT9T031_MAX_HEIGHT;
333 a->defrect = a->bounds;
334 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
335 a->pixelaspect.numerator = 1;
336 a->pixelaspect.denominator = 1;
341 static int mt9t031_g_fmt(struct v4l2_subdev *sd,
342 struct v4l2_mbus_framefmt *mf)
344 struct i2c_client *client = v4l2_get_subdevdata(sd);
345 struct mt9t031 *mt9t031 = to_mt9t031(client);
347 mf->width = mt9t031->rect.width / mt9t031->xskip;
348 mf->height = mt9t031->rect.height / mt9t031->yskip;
349 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
350 mf->colorspace = V4L2_COLORSPACE_SRGB;
351 mf->field = V4L2_FIELD_NONE;
356 static int mt9t031_s_fmt(struct v4l2_subdev *sd,
357 struct v4l2_mbus_framefmt *mf)
359 struct i2c_client *client = v4l2_get_subdevdata(sd);
360 struct mt9t031 *mt9t031 = to_mt9t031(client);
362 struct v4l2_rect rect = mt9t031->rect;
365 * try_fmt has put width and height within limits.
366 * S_FMT: use binning and skipping for scaling
368 xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
369 yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
371 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
372 mf->colorspace = V4L2_COLORSPACE_SRGB;
374 /* mt9t031_set_params() doesn't change width and height */
375 return mt9t031_set_params(client, &rect, xskip, yskip);
379 * If a user window larger than sensor window is requested, we'll increase the
382 static int mt9t031_try_fmt(struct v4l2_subdev *sd,
383 struct v4l2_mbus_framefmt *mf)
385 v4l_bound_align_image(
386 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
387 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
389 mf->code = V4L2_MBUS_FMT_SBGGR10_1X10;
390 mf->colorspace = V4L2_COLORSPACE_SRGB;
395 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
396 struct v4l2_dbg_chip_ident *id)
398 struct i2c_client *client = v4l2_get_subdevdata(sd);
399 struct mt9t031 *mt9t031 = to_mt9t031(client);
401 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
404 if (id->match.addr != client->addr)
407 id->ident = mt9t031->model;
413 #ifdef CONFIG_VIDEO_ADV_DEBUG
414 static int mt9t031_g_register(struct v4l2_subdev *sd,
415 struct v4l2_dbg_register *reg)
417 struct i2c_client *client = v4l2_get_subdevdata(sd);
419 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
422 if (reg->match.addr != client->addr)
425 reg->val = reg_read(client, reg->reg);
427 if (reg->val > 0xffff)
433 static int mt9t031_s_register(struct v4l2_subdev *sd,
434 struct v4l2_dbg_register *reg)
436 struct i2c_client *client = v4l2_get_subdevdata(sd);
438 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
441 if (reg->match.addr != client->addr)
444 if (reg_write(client, reg->reg, reg->val) < 0)
451 static int mt9t031_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
453 struct mt9t031 *mt9t031 = container_of(ctrl->handler,
454 struct mt9t031, hdl);
455 const u32 shutter_max = MT9T031_MAX_HEIGHT + MT9T031_VERTICAL_BLANK;
459 case V4L2_CID_EXPOSURE_AUTO:
460 min = mt9t031->exposure->minimum;
461 max = mt9t031->exposure->maximum;
462 mt9t031->exposure->val =
463 (shutter_max / 2 + (mt9t031->total_h - 1) * (max - min))
470 static int mt9t031_s_ctrl(struct v4l2_ctrl *ctrl)
472 struct mt9t031 *mt9t031 = container_of(ctrl->handler,
473 struct mt9t031, hdl);
474 struct v4l2_subdev *sd = &mt9t031->subdev;
475 struct i2c_client *client = v4l2_get_subdevdata(sd);
476 struct v4l2_ctrl *exp = mt9t031->exposure;
482 data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
484 data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
490 data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
492 data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
497 /* See Datasheet Table 7, Gain settings. */
498 if (ctrl->val <= ctrl->default_value) {
499 /* Pack it into 0..1 step 0.125, register values 0..8 */
500 unsigned long range = ctrl->default_value - ctrl->minimum;
501 data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
503 dev_dbg(&client->dev, "Setting gain %d\n", data);
504 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
508 /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
509 /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
510 unsigned long range = ctrl->maximum - ctrl->default_value - 1;
511 /* calculated gain: map 65..127 to 9..1024 step 0.125 */
512 unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
513 1015 + range / 2) / range + 9;
515 if (gain <= 32) /* calculated gain 9..32 -> 9..32 */
517 else if (gain <= 64) /* calculated gain 33..64 -> 0x51..0x60 */
518 data = ((gain - 32) * 16 + 16) / 32 + 80;
520 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
521 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
523 dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
524 reg_read(client, MT9T031_GLOBAL_GAIN), data);
525 data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
531 case V4L2_CID_EXPOSURE_AUTO:
532 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
533 unsigned int range = exp->maximum - exp->minimum;
534 unsigned int shutter = ((exp->val - exp->minimum) * 1048 +
535 range / 2) / range + 1;
538 get_shutter(client, &old);
539 dev_dbg(&client->dev, "Set shutter from %u to %u\n",
541 if (set_shutter(client, shutter) < 0)
544 const u16 vblank = MT9T031_VERTICAL_BLANK;
545 mt9t031->total_h = mt9t031->rect.height +
546 mt9t031->y_skip_top + vblank;
548 if (set_shutter(client, mt9t031->total_h) < 0)
560 * This function does nothing for now but must be present for pm to work
562 static int mt9t031_runtime_suspend(struct device *dev)
569 * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
570 * they are however changed at reset if the platform hook is present
571 * thus we rewrite them with the values stored by the driver
573 static int mt9t031_runtime_resume(struct device *dev)
575 struct video_device *vdev = to_video_device(dev);
576 struct soc_camera_device *icd = dev_get_drvdata(vdev->parent);
577 struct v4l2_subdev *sd = soc_camera_to_subdev(icd);
578 struct i2c_client *client = v4l2_get_subdevdata(sd);
579 struct mt9t031 *mt9t031 = to_mt9t031(client);
584 xbin = min(mt9t031->xskip, (u16)3);
585 ybin = min(mt9t031->yskip, (u16)3);
587 ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
588 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
592 ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
593 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
600 static struct dev_pm_ops mt9t031_dev_pm_ops = {
601 .runtime_suspend = mt9t031_runtime_suspend,
602 .runtime_resume = mt9t031_runtime_resume,
605 static struct device_type mt9t031_dev_type = {
607 .pm = &mt9t031_dev_pm_ops,
611 * Interface active, can use i2c. If it fails, it can indeed mean, that
612 * this wasn't our capture interface, so, we wait for the right one
614 static int mt9t031_video_probe(struct i2c_client *client)
616 struct mt9t031 *mt9t031 = to_mt9t031(client);
617 struct video_device *vdev = soc_camera_i2c_to_vdev(client);
621 /* Enable the chip */
622 data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
623 dev_dbg(&client->dev, "write: %d\n", data);
625 /* Read out the chip version register */
626 data = reg_read(client, MT9T031_CHIP_VERSION);
630 mt9t031->model = V4L2_IDENT_MT9T031;
633 dev_err(&client->dev,
634 "No MT9T031 chip detected, register read %x\n", data);
638 dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
640 ret = mt9t031_idle(client);
642 dev_err(&client->dev, "Failed to initialise the camera\n");
644 vdev->dev.type = &mt9t031_dev_type;
645 v4l2_ctrl_handler_setup(&mt9t031->hdl);
650 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
652 struct i2c_client *client = v4l2_get_subdevdata(sd);
653 struct mt9t031 *mt9t031 = to_mt9t031(client);
655 *lines = mt9t031->y_skip_top;
660 static const struct v4l2_ctrl_ops mt9t031_ctrl_ops = {
661 .g_volatile_ctrl = mt9t031_g_volatile_ctrl,
662 .s_ctrl = mt9t031_s_ctrl,
665 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
666 .g_chip_ident = mt9t031_g_chip_ident,
667 #ifdef CONFIG_VIDEO_ADV_DEBUG
668 .g_register = mt9t031_g_register,
669 .s_register = mt9t031_s_register,
673 static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
674 enum v4l2_mbus_pixelcode *code)
679 *code = V4L2_MBUS_FMT_SBGGR10_1X10;
683 static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
684 struct v4l2_mbus_config *cfg)
686 struct i2c_client *client = v4l2_get_subdevdata(sd);
687 struct soc_camera_device *icd = client->dev.platform_data;
688 struct soc_camera_link *icl = to_soc_camera_link(icd);
690 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
691 V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
692 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
693 cfg->type = V4L2_MBUS_PARALLEL;
694 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
699 static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
700 const struct v4l2_mbus_config *cfg)
702 struct i2c_client *client = v4l2_get_subdevdata(sd);
703 struct soc_camera_device *icd = client->dev.platform_data;
704 struct soc_camera_link *icl = to_soc_camera_link(icd);
706 if (soc_camera_apply_board_flags(icl, cfg) &
707 V4L2_MBUS_PCLK_SAMPLE_FALLING)
708 return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
710 return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
713 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
714 .s_stream = mt9t031_s_stream,
715 .s_mbus_fmt = mt9t031_s_fmt,
716 .g_mbus_fmt = mt9t031_g_fmt,
717 .try_mbus_fmt = mt9t031_try_fmt,
718 .s_crop = mt9t031_s_crop,
719 .g_crop = mt9t031_g_crop,
720 .cropcap = mt9t031_cropcap,
721 .enum_mbus_fmt = mt9t031_enum_fmt,
722 .g_mbus_config = mt9t031_g_mbus_config,
723 .s_mbus_config = mt9t031_s_mbus_config,
726 static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
727 .g_skip_top_lines = mt9t031_g_skip_top_lines,
730 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
731 .core = &mt9t031_subdev_core_ops,
732 .video = &mt9t031_subdev_video_ops,
733 .sensor = &mt9t031_subdev_sensor_ops,
736 static int mt9t031_probe(struct i2c_client *client,
737 const struct i2c_device_id *did)
739 struct mt9t031 *mt9t031;
740 struct soc_camera_device *icd = client->dev.platform_data;
741 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
745 struct soc_camera_link *icl = to_soc_camera_link(icd);
747 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
752 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
753 dev_warn(&adapter->dev,
754 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
758 mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
762 v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
763 v4l2_ctrl_handler_init(&mt9t031->hdl, 5);
764 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
765 V4L2_CID_VFLIP, 0, 1, 1, 0);
766 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
767 V4L2_CID_HFLIP, 0, 1, 1, 0);
768 v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
769 V4L2_CID_GAIN, 0, 127, 1, 64);
772 * Simulated autoexposure. If enabled, we calculate shutter width
773 * ourselves in the driver based on vertical blanking and frame width
775 mt9t031->autoexposure = v4l2_ctrl_new_std_menu(&mt9t031->hdl,
776 &mt9t031_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
778 mt9t031->exposure = v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
779 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
781 mt9t031->subdev.ctrl_handler = &mt9t031->hdl;
782 if (mt9t031->hdl.error) {
783 int err = mt9t031->hdl.error;
788 v4l2_ctrl_auto_cluster(2, &mt9t031->autoexposure,
789 V4L2_EXPOSURE_MANUAL, true);
791 mt9t031->y_skip_top = 0;
792 mt9t031->rect.left = MT9T031_COLUMN_SKIP;
793 mt9t031->rect.top = MT9T031_ROW_SKIP;
794 mt9t031->rect.width = MT9T031_MAX_WIDTH;
795 mt9t031->rect.height = MT9T031_MAX_HEIGHT;
800 mt9t031_idle(client);
802 ret = mt9t031_video_probe(client);
804 mt9t031_disable(client);
807 v4l2_ctrl_handler_free(&mt9t031->hdl);
814 static int mt9t031_remove(struct i2c_client *client)
816 struct mt9t031 *mt9t031 = to_mt9t031(client);
818 v4l2_device_unregister_subdev(&mt9t031->subdev);
819 v4l2_ctrl_handler_free(&mt9t031->hdl);
825 static const struct i2c_device_id mt9t031_id[] = {
829 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
831 static struct i2c_driver mt9t031_i2c_driver = {
835 .probe = mt9t031_probe,
836 .remove = mt9t031_remove,
837 .id_table = mt9t031_id,
840 static int __init mt9t031_mod_init(void)
842 return i2c_add_driver(&mt9t031_i2c_driver);
845 static void __exit mt9t031_mod_exit(void)
847 i2c_del_driver(&mt9t031_i2c_driver);
850 module_init(mt9t031_mod_init);
851 module_exit(mt9t031_mod_exit);
853 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
854 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
855 MODULE_LICENSE("GPL v2");