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>
16 #include <linux/module.h>
18 #include <media/soc_camera.h>
19 #include <media/soc_mediabus.h>
20 #include <media/v4l2-subdev.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/v4l2-ctrls.h>
25 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
26 * The platform has to define struct i2c_board_info objects and link to them
27 * from struct soc_camera_link
30 static char *sensor_type;
31 module_param(sensor_type, charp, S_IRUGO);
32 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
34 /* mt9v022 selected register addresses */
35 #define MT9V022_CHIP_VERSION 0x00
36 #define MT9V022_COLUMN_START 0x01
37 #define MT9V022_ROW_START 0x02
38 #define MT9V022_WINDOW_HEIGHT 0x03
39 #define MT9V022_WINDOW_WIDTH 0x04
40 #define MT9V022_HORIZONTAL_BLANKING 0x05
41 #define MT9V022_VERTICAL_BLANKING 0x06
42 #define MT9V022_CHIP_CONTROL 0x07
43 #define MT9V022_SHUTTER_WIDTH1 0x08
44 #define MT9V022_SHUTTER_WIDTH2 0x09
45 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
46 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
47 #define MT9V022_RESET 0x0c
48 #define MT9V022_READ_MODE 0x0d
49 #define MT9V022_MONITOR_MODE 0x0e
50 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
51 #define MT9V022_LED_OUT_CONTROL 0x1b
52 #define MT9V022_ADC_MODE_CONTROL 0x1c
53 #define MT9V022_ANALOG_GAIN 0x35
54 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
55 #define MT9V022_PIXCLK_FV_LV 0x74
56 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
57 #define MT9V022_AEC_AGC_ENABLE 0xAF
58 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
60 /* mt9v024 partial list register addresses changes with respect to mt9v022 */
61 #define MT9V024_PIXCLK_FV_LV 0x72
62 #define MT9V024_MAX_TOTAL_SHUTTER_WIDTH 0xAD
64 /* Progressive scan, master, defaults */
65 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
67 #define MT9V022_MAX_WIDTH 752
68 #define MT9V022_MAX_HEIGHT 480
69 #define MT9V022_MIN_WIDTH 48
70 #define MT9V022_MIN_HEIGHT 32
71 #define MT9V022_COLUMN_SKIP 1
72 #define MT9V022_ROW_SKIP 4
74 #define MT9V022_HORIZONTAL_BLANKING_MIN 43
75 #define MT9V022_HORIZONTAL_BLANKING_MAX 1023
76 #define MT9V022_HORIZONTAL_BLANKING_DEF 94
77 #define MT9V022_VERTICAL_BLANKING_MIN 2
78 #define MT9V022_VERTICAL_BLANKING_MAX 3000
79 #define MT9V022_VERTICAL_BLANKING_DEF 45
81 #define is_mt9v024(id) (id == 0x1324)
83 /* MT9V022 has only one fixed colorspace per pixelcode */
84 struct mt9v022_datafmt {
85 enum v4l2_mbus_pixelcode code;
86 enum v4l2_colorspace colorspace;
89 /* Find a data format by a pixel code in an array */
90 static const struct mt9v022_datafmt *mt9v022_find_datafmt(
91 enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
95 for (i = 0; i < n; i++)
96 if (fmt[i].code == code)
102 static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
104 * Order important: first natively supported,
105 * second supported with a GPIO extender
107 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
108 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
111 static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
112 /* Order important - see above */
113 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
114 {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
117 /* only registers with different addresses on different mt9v02x sensors */
118 struct mt9v02x_register {
119 u8 max_total_shutter_width;
123 static const struct mt9v02x_register mt9v022_register = {
124 .max_total_shutter_width = MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
125 .pixclk_fv_lv = MT9V022_PIXCLK_FV_LV,
128 static const struct mt9v02x_register mt9v024_register = {
129 .max_total_shutter_width = MT9V024_MAX_TOTAL_SHUTTER_WIDTH,
130 .pixclk_fv_lv = MT9V024_PIXCLK_FV_LV,
134 struct v4l2_subdev subdev;
135 struct v4l2_ctrl_handler hdl;
137 /* exposure/auto-exposure cluster */
138 struct v4l2_ctrl *autoexposure;
139 struct v4l2_ctrl *exposure;
142 /* gain/auto-gain cluster */
143 struct v4l2_ctrl *autogain;
144 struct v4l2_ctrl *gain;
146 struct v4l2_ctrl *hblank;
147 struct v4l2_ctrl *vblank;
148 struct v4l2_rect rect; /* Sensor window */
149 const struct mt9v022_datafmt *fmt;
150 const struct mt9v022_datafmt *fmts;
151 const struct mt9v02x_register *reg;
153 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
155 unsigned short y_skip_top; /* Lines to skip at the top */
158 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
160 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
163 static int reg_read(struct i2c_client *client, const u8 reg)
165 return i2c_smbus_read_word_swapped(client, reg);
168 static int reg_write(struct i2c_client *client, const u8 reg,
171 return i2c_smbus_write_word_swapped(client, reg, data);
174 static int reg_set(struct i2c_client *client, const u8 reg,
179 ret = reg_read(client, reg);
182 return reg_write(client, reg, ret | data);
185 static int reg_clear(struct i2c_client *client, const u8 reg,
190 ret = reg_read(client, reg);
193 return reg_write(client, reg, ret & ~data);
196 static int mt9v022_init(struct i2c_client *client)
198 struct mt9v022 *mt9v022 = to_mt9v022(client);
202 * Almost the default mode: master, parallel, simultaneous, and an
203 * undocumented bit 0x200, which is present in table 7, but not in 8,
204 * plus snapshot mode to disable scan for now
206 mt9v022->chip_control |= 0x10;
207 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
209 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
214 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
216 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
218 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
220 ret = reg_write(client, mt9v022->reg->max_total_shutter_width, 480);
223 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
225 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
227 return v4l2_ctrl_handler_setup(&mt9v022->hdl);
232 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
234 struct i2c_client *client = v4l2_get_subdevdata(sd);
235 struct mt9v022 *mt9v022 = to_mt9v022(client);
238 /* Switch to master "normal" mode */
239 mt9v022->chip_control &= ~0x10;
241 /* Switch to snapshot mode */
242 mt9v022->chip_control |= 0x10;
244 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
249 static int mt9v022_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
251 struct i2c_client *client = v4l2_get_subdevdata(sd);
252 struct mt9v022 *mt9v022 = to_mt9v022(client);
253 struct v4l2_rect rect = a->c;
256 /* Bayer format - even size lengths */
257 if (mt9v022->fmts == mt9v022_colour_fmts) {
258 rect.width = ALIGN(rect.width, 2);
259 rect.height = ALIGN(rect.height, 2);
260 /* Let the user play with the starting pixel */
263 soc_camera_limit_side(&rect.left, &rect.width,
264 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
266 soc_camera_limit_side(&rect.top, &rect.height,
267 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
269 /* Like in example app. Contradicts the datasheet though */
270 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
272 if (ret & 1) /* Autoexposure */
273 ret = reg_write(client, mt9v022->reg->max_total_shutter_width,
274 rect.height + mt9v022->y_skip_top + 43);
276 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
277 rect.height + mt9v022->y_skip_top + 43);
279 /* Setup frame format: defaults apart from width and height */
281 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
283 ret = reg_write(client, MT9V022_ROW_START, rect.top);
286 * Default 94, Phytec driver says:
287 * "width + horizontal blank >= 660"
289 ret = v4l2_ctrl_s_ctrl(mt9v022->hblank,
290 rect.width > 660 - 43 ? 43 : 660 - rect.width);
292 ret = v4l2_ctrl_s_ctrl(mt9v022->vblank, 45);
294 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
296 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
297 rect.height + mt9v022->y_skip_top);
302 dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
304 mt9v022->rect = rect;
309 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
311 struct i2c_client *client = v4l2_get_subdevdata(sd);
312 struct mt9v022 *mt9v022 = to_mt9v022(client);
314 a->c = mt9v022->rect;
315 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
320 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
322 a->bounds.left = MT9V022_COLUMN_SKIP;
323 a->bounds.top = MT9V022_ROW_SKIP;
324 a->bounds.width = MT9V022_MAX_WIDTH;
325 a->bounds.height = MT9V022_MAX_HEIGHT;
326 a->defrect = a->bounds;
327 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
328 a->pixelaspect.numerator = 1;
329 a->pixelaspect.denominator = 1;
334 static int mt9v022_g_fmt(struct v4l2_subdev *sd,
335 struct v4l2_mbus_framefmt *mf)
337 struct i2c_client *client = v4l2_get_subdevdata(sd);
338 struct mt9v022 *mt9v022 = to_mt9v022(client);
340 mf->width = mt9v022->rect.width;
341 mf->height = mt9v022->rect.height;
342 mf->code = mt9v022->fmt->code;
343 mf->colorspace = mt9v022->fmt->colorspace;
344 mf->field = V4L2_FIELD_NONE;
349 static int mt9v022_s_fmt(struct v4l2_subdev *sd,
350 struct v4l2_mbus_framefmt *mf)
352 struct i2c_client *client = v4l2_get_subdevdata(sd);
353 struct mt9v022 *mt9v022 = to_mt9v022(client);
354 struct v4l2_crop a = {
356 .left = mt9v022->rect.left,
357 .top = mt9v022->rect.top,
359 .height = mf->height,
365 * The caller provides a supported format, as verified per call to
366 * .try_mbus_fmt(), datawidth is from our supported format list
369 case V4L2_MBUS_FMT_Y8_1X8:
370 case V4L2_MBUS_FMT_Y10_1X10:
371 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
374 case V4L2_MBUS_FMT_SBGGR8_1X8:
375 case V4L2_MBUS_FMT_SBGGR10_1X10:
376 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
383 /* No support for scaling on this camera, just crop. */
384 ret = mt9v022_s_crop(sd, &a);
386 mf->width = mt9v022->rect.width;
387 mf->height = mt9v022->rect.height;
388 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
389 mt9v022->fmts, mt9v022->num_fmts);
390 mf->colorspace = mt9v022->fmt->colorspace;
396 static int mt9v022_try_fmt(struct v4l2_subdev *sd,
397 struct v4l2_mbus_framefmt *mf)
399 struct i2c_client *client = v4l2_get_subdevdata(sd);
400 struct mt9v022 *mt9v022 = to_mt9v022(client);
401 const struct mt9v022_datafmt *fmt;
402 int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
403 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
405 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
406 MT9V022_MAX_WIDTH, align,
407 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
408 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
410 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
414 mf->code = fmt->code;
417 mf->colorspace = fmt->colorspace;
422 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
423 struct v4l2_dbg_chip_ident *id)
425 struct i2c_client *client = v4l2_get_subdevdata(sd);
426 struct mt9v022 *mt9v022 = to_mt9v022(client);
428 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
431 if (id->match.addr != client->addr)
434 id->ident = mt9v022->model;
440 #ifdef CONFIG_VIDEO_ADV_DEBUG
441 static int mt9v022_g_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)
453 reg->val = reg_read(client, reg->reg);
455 if (reg->val > 0xffff)
461 static int mt9v022_s_register(struct v4l2_subdev *sd,
462 struct v4l2_dbg_register *reg)
464 struct i2c_client *client = v4l2_get_subdevdata(sd);
466 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
469 if (reg->match.addr != client->addr)
472 if (reg_write(client, reg->reg, reg->val) < 0)
479 static int mt9v022_s_power(struct v4l2_subdev *sd, int on)
481 struct i2c_client *client = v4l2_get_subdevdata(sd);
482 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
484 return soc_camera_set_power(&client->dev, icl, on);
487 static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
489 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
490 struct mt9v022, hdl);
491 struct v4l2_subdev *sd = &mt9v022->subdev;
492 struct i2c_client *client = v4l2_get_subdevdata(sd);
493 struct v4l2_ctrl *gain = mt9v022->gain;
494 struct v4l2_ctrl *exp = mt9v022->exposure;
499 case V4L2_CID_AUTOGAIN:
500 data = reg_read(client, MT9V022_ANALOG_GAIN);
504 range = gain->maximum - gain->minimum;
505 gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
507 case V4L2_CID_EXPOSURE_AUTO:
508 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
512 range = exp->maximum - exp->minimum;
513 exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
515 case V4L2_CID_HBLANK:
516 data = reg_read(client, MT9V022_HORIZONTAL_BLANKING);
521 case V4L2_CID_VBLANK:
522 data = reg_read(client, MT9V022_VERTICAL_BLANKING);
531 static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
533 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
534 struct mt9v022, hdl);
535 struct v4l2_subdev *sd = &mt9v022->subdev;
536 struct i2c_client *client = v4l2_get_subdevdata(sd);
542 data = reg_set(client, MT9V022_READ_MODE, 0x10);
544 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
550 data = reg_set(client, MT9V022_READ_MODE, 0x20);
552 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
556 case V4L2_CID_AUTOGAIN:
558 if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
561 struct v4l2_ctrl *gain = mt9v022->gain;
562 /* mt9v022 has minimum == default */
563 unsigned long range = gain->maximum - gain->minimum;
564 /* Valid values 16 to 64, 32 to 64 must be even. */
565 unsigned long gain_val = ((gain->val - gain->minimum) *
566 48 + range / 2) / range + 16;
572 * The user wants to set gain manually, hope, she
573 * knows, what she's doing... Switch AGC off.
575 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
578 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
579 reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
580 if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
584 case V4L2_CID_EXPOSURE_AUTO:
585 if (ctrl->val == V4L2_EXPOSURE_AUTO) {
586 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
588 struct v4l2_ctrl *exp = mt9v022->exposure;
589 unsigned long range = exp->maximum - exp->minimum;
590 unsigned long shutter = ((exp->val - exp->minimum) *
591 479 + range / 2) / range + 1;
594 * The user wants to set shutter width manually, hope,
595 * she knows, what she's doing... Switch AEC off.
597 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
600 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
601 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
603 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
608 case V4L2_CID_HBLANK:
609 if (reg_write(client, MT9V022_HORIZONTAL_BLANKING,
613 case V4L2_CID_VBLANK:
614 if (reg_write(client, MT9V022_VERTICAL_BLANKING,
623 * Interface active, can use i2c. If it fails, it can indeed mean, that
624 * this wasn't our capture interface, so, we wait for the right one
626 static int mt9v022_video_probe(struct i2c_client *client)
628 struct mt9v022 *mt9v022 = to_mt9v022(client);
629 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
634 ret = mt9v022_s_power(&mt9v022->subdev, 1);
638 /* Read out the chip version register */
639 data = reg_read(client, MT9V022_CHIP_VERSION);
641 /* must be 0x1311, 0x1313 or 0x1324 */
642 if (data != 0x1311 && data != 0x1313 && data != 0x1324) {
644 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
649 mt9v022->reg = is_mt9v024(data) ? &mt9v024_register :
653 ret = reg_write(client, MT9V022_RESET, 1);
656 /* 15 clock cycles */
658 if (reg_read(client, MT9V022_RESET)) {
659 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
665 /* Set monochrome or colour sensor type */
666 if (sensor_type && (!strcmp("colour", sensor_type) ||
667 !strcmp("color", sensor_type))) {
668 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
669 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
670 mt9v022->fmts = mt9v022_colour_fmts;
672 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
673 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
674 mt9v022->fmts = mt9v022_monochrome_fmts;
680 mt9v022->num_fmts = 0;
683 * This is a 10bit sensor, so by default we only allow 10bit.
684 * The platform may support different bus widths due to
685 * different routing of the data lines.
687 if (icl->query_bus_param)
688 flags = icl->query_bus_param(icl);
690 flags = SOCAM_DATAWIDTH_10;
692 if (flags & SOCAM_DATAWIDTH_10)
697 if (flags & SOCAM_DATAWIDTH_8)
700 mt9v022->fmt = &mt9v022->fmts[0];
702 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
703 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
704 "monochrome" : "colour");
706 ret = mt9v022_init(client);
708 dev_err(&client->dev, "Failed to initialise the camera\n");
711 mt9v022_s_power(&mt9v022->subdev, 0);
715 static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
717 struct i2c_client *client = v4l2_get_subdevdata(sd);
718 struct mt9v022 *mt9v022 = to_mt9v022(client);
720 *lines = mt9v022->y_skip_top;
725 static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
726 .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
727 .s_ctrl = mt9v022_s_ctrl,
730 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
731 .g_chip_ident = mt9v022_g_chip_ident,
732 #ifdef CONFIG_VIDEO_ADV_DEBUG
733 .g_register = mt9v022_g_register,
734 .s_register = mt9v022_s_register,
736 .s_power = mt9v022_s_power,
739 static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
740 enum v4l2_mbus_pixelcode *code)
742 struct i2c_client *client = v4l2_get_subdevdata(sd);
743 struct mt9v022 *mt9v022 = to_mt9v022(client);
745 if (index >= mt9v022->num_fmts)
748 *code = mt9v022->fmts[index].code;
752 static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
753 struct v4l2_mbus_config *cfg)
755 struct i2c_client *client = v4l2_get_subdevdata(sd);
756 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
758 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
759 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
760 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
761 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
762 V4L2_MBUS_DATA_ACTIVE_HIGH;
763 cfg->type = V4L2_MBUS_PARALLEL;
764 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
769 static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
770 const struct v4l2_mbus_config *cfg)
772 struct i2c_client *client = v4l2_get_subdevdata(sd);
773 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
774 struct mt9v022 *mt9v022 = to_mt9v022(client);
775 unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
776 unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
780 if (icl->set_bus_param) {
781 ret = icl->set_bus_param(icl, 1 << (bps - 1));
784 } else if (bps != 10) {
786 * Without board specific bus width settings we only support the
787 * sensors native bus width
792 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
795 if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
798 if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
801 ret = reg_write(client, mt9v022->reg->pixclk_fv_lv, pixclk);
805 if (!(flags & V4L2_MBUS_MASTER))
806 mt9v022->chip_control &= ~0x8;
808 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
812 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
813 pixclk, mt9v022->chip_control);
818 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
819 .s_stream = mt9v022_s_stream,
820 .s_mbus_fmt = mt9v022_s_fmt,
821 .g_mbus_fmt = mt9v022_g_fmt,
822 .try_mbus_fmt = mt9v022_try_fmt,
823 .s_crop = mt9v022_s_crop,
824 .g_crop = mt9v022_g_crop,
825 .cropcap = mt9v022_cropcap,
826 .enum_mbus_fmt = mt9v022_enum_fmt,
827 .g_mbus_config = mt9v022_g_mbus_config,
828 .s_mbus_config = mt9v022_s_mbus_config,
831 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
832 .g_skip_top_lines = mt9v022_g_skip_top_lines,
835 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
836 .core = &mt9v022_subdev_core_ops,
837 .video = &mt9v022_subdev_video_ops,
838 .sensor = &mt9v022_subdev_sensor_ops,
841 static int mt9v022_probe(struct i2c_client *client,
842 const struct i2c_device_id *did)
844 struct mt9v022 *mt9v022;
845 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
846 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
850 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
854 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
855 dev_warn(&adapter->dev,
856 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
860 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
864 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
865 v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
866 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
867 V4L2_CID_VFLIP, 0, 1, 1, 0);
868 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
869 V4L2_CID_HFLIP, 0, 1, 1, 0);
870 mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
871 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
872 mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
873 V4L2_CID_GAIN, 0, 127, 1, 64);
876 * Simulated autoexposure. If enabled, we calculate shutter width
877 * ourselves in the driver based on vertical blanking and frame width
879 mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
880 &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
882 mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
883 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
885 mt9v022->hblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
886 V4L2_CID_HBLANK, MT9V022_HORIZONTAL_BLANKING_MIN,
887 MT9V022_HORIZONTAL_BLANKING_MAX, 1,
888 MT9V022_HORIZONTAL_BLANKING_DEF);
890 mt9v022->vblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
891 V4L2_CID_VBLANK, MT9V022_VERTICAL_BLANKING_MIN,
892 MT9V022_VERTICAL_BLANKING_MAX, 1,
893 MT9V022_VERTICAL_BLANKING_DEF);
895 mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
896 if (mt9v022->hdl.error) {
897 int err = mt9v022->hdl.error;
899 dev_err(&client->dev, "control initialisation err %d\n", err);
903 v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
904 V4L2_EXPOSURE_MANUAL, true);
905 v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
907 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
910 * MT9V022 _really_ corrupts the first read out line.
911 * TODO: verify on i.MX31
913 mt9v022->y_skip_top = 1;
914 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
915 mt9v022->rect.top = MT9V022_ROW_SKIP;
916 mt9v022->rect.width = MT9V022_MAX_WIDTH;
917 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
919 ret = mt9v022_video_probe(client);
921 v4l2_ctrl_handler_free(&mt9v022->hdl);
928 static int mt9v022_remove(struct i2c_client *client)
930 struct mt9v022 *mt9v022 = to_mt9v022(client);
931 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
933 v4l2_device_unregister_subdev(&mt9v022->subdev);
936 v4l2_ctrl_handler_free(&mt9v022->hdl);
941 static const struct i2c_device_id mt9v022_id[] = {
945 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
947 static struct i2c_driver mt9v022_i2c_driver = {
951 .probe = mt9v022_probe,
952 .remove = mt9v022_remove,
953 .id_table = mt9v022_id,
956 module_i2c_driver(mt9v022_i2c_driver);
958 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
959 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
960 MODULE_LICENSE("GPL");