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>
17 #include <media/soc_camera.h>
18 #include <media/soc_mediabus.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/v4l2-chip-ident.h>
23 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
24 * The platform has to define ctruct i2c_board_info objects and link to them
25 * from struct soc_camera_link
28 static char *sensor_type;
29 module_param(sensor_type, charp, S_IRUGO);
30 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
32 /* mt9v022 selected register addresses */
33 #define MT9V022_CHIP_VERSION 0x00
34 #define MT9V022_COLUMN_START 0x01
35 #define MT9V022_ROW_START 0x02
36 #define MT9V022_WINDOW_HEIGHT 0x03
37 #define MT9V022_WINDOW_WIDTH 0x04
38 #define MT9V022_HORIZONTAL_BLANKING 0x05
39 #define MT9V022_VERTICAL_BLANKING 0x06
40 #define MT9V022_CHIP_CONTROL 0x07
41 #define MT9V022_SHUTTER_WIDTH1 0x08
42 #define MT9V022_SHUTTER_WIDTH2 0x09
43 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
44 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
45 #define MT9V022_RESET 0x0c
46 #define MT9V022_READ_MODE 0x0d
47 #define MT9V022_MONITOR_MODE 0x0e
48 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
49 #define MT9V022_LED_OUT_CONTROL 0x1b
50 #define MT9V022_ADC_MODE_CONTROL 0x1c
51 #define MT9V022_ANALOG_GAIN 0x35
52 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
53 #define MT9V022_PIXCLK_FV_LV 0x74
54 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
55 #define MT9V022_AEC_AGC_ENABLE 0xAF
56 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
58 /* Progressive scan, master, defaults */
59 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
61 #define MT9V022_MAX_WIDTH 752
62 #define MT9V022_MAX_HEIGHT 480
63 #define MT9V022_MIN_WIDTH 48
64 #define MT9V022_MIN_HEIGHT 32
65 #define MT9V022_COLUMN_SKIP 1
66 #define MT9V022_ROW_SKIP 4
68 /* MT9V022 has only one fixed colorspace per pixelcode */
69 struct mt9v022_datafmt {
70 enum v4l2_mbus_pixelcode code;
71 enum v4l2_colorspace colorspace;
74 /* Find a data format by a pixel code in an array */
75 static const struct mt9v022_datafmt *mt9v022_find_datafmt(
76 enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
80 for (i = 0; i < n; i++)
81 if (fmt[i].code == code)
87 static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
89 * Order important: first natively supported,
90 * second supported with a GPIO extender
92 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
93 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
96 static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
97 /* Order important - see above */
98 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
99 {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
103 struct v4l2_subdev subdev;
104 struct v4l2_rect rect; /* Sensor window */
105 const struct mt9v022_datafmt *fmt;
106 const struct mt9v022_datafmt *fmts;
108 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
110 unsigned short y_skip_top; /* Lines to skip at the top */
113 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
115 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
118 static int reg_read(struct i2c_client *client, const u8 reg)
120 s32 data = i2c_smbus_read_word_data(client, reg);
121 return data < 0 ? data : swab16(data);
124 static int reg_write(struct i2c_client *client, const u8 reg,
127 return i2c_smbus_write_word_data(client, reg, swab16(data));
130 static int reg_set(struct i2c_client *client, const u8 reg,
135 ret = reg_read(client, reg);
138 return reg_write(client, reg, ret | data);
141 static int reg_clear(struct i2c_client *client, const u8 reg,
146 ret = reg_read(client, reg);
149 return reg_write(client, reg, ret & ~data);
152 static int mt9v022_init(struct i2c_client *client)
154 struct mt9v022 *mt9v022 = to_mt9v022(client);
158 * Almost the default mode: master, parallel, simultaneous, and an
159 * undocumented bit 0x200, which is present in table 7, but not in 8,
160 * plus snapshot mode to disable scan for now
162 mt9v022->chip_control |= 0x10;
163 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
165 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
170 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
172 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
174 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
176 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
179 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
181 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
186 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
188 struct i2c_client *client = v4l2_get_subdevdata(sd);
189 struct mt9v022 *mt9v022 = to_mt9v022(client);
192 /* Switch to master "normal" mode */
193 mt9v022->chip_control &= ~0x10;
195 /* Switch to snapshot mode */
196 mt9v022->chip_control |= 0x10;
198 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
203 static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
205 struct i2c_client *client = v4l2_get_subdevdata(sd);
206 struct mt9v022 *mt9v022 = to_mt9v022(client);
207 struct v4l2_rect rect = a->c;
210 /* Bayer format - even size lengths */
211 if (mt9v022->fmts == mt9v022_colour_fmts) {
212 rect.width = ALIGN(rect.width, 2);
213 rect.height = ALIGN(rect.height, 2);
214 /* Let the user play with the starting pixel */
217 soc_camera_limit_side(&rect.left, &rect.width,
218 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
220 soc_camera_limit_side(&rect.top, &rect.height,
221 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
223 /* Like in example app. Contradicts the datasheet though */
224 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
226 if (ret & 1) /* Autoexposure */
227 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
228 rect.height + mt9v022->y_skip_top + 43);
230 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
231 rect.height + mt9v022->y_skip_top + 43);
233 /* Setup frame format: defaults apart from width and height */
235 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
237 ret = reg_write(client, MT9V022_ROW_START, rect.top);
240 * Default 94, Phytec driver says:
241 * "width + horizontal blank >= 660"
243 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
244 rect.width > 660 - 43 ? 43 :
247 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
249 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
251 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
252 rect.height + mt9v022->y_skip_top);
257 dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
259 mt9v022->rect = rect;
264 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
266 struct i2c_client *client = v4l2_get_subdevdata(sd);
267 struct mt9v022 *mt9v022 = to_mt9v022(client);
269 a->c = mt9v022->rect;
270 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
275 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
277 a->bounds.left = MT9V022_COLUMN_SKIP;
278 a->bounds.top = MT9V022_ROW_SKIP;
279 a->bounds.width = MT9V022_MAX_WIDTH;
280 a->bounds.height = MT9V022_MAX_HEIGHT;
281 a->defrect = a->bounds;
282 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
283 a->pixelaspect.numerator = 1;
284 a->pixelaspect.denominator = 1;
289 static int mt9v022_g_fmt(struct v4l2_subdev *sd,
290 struct v4l2_mbus_framefmt *mf)
292 struct i2c_client *client = v4l2_get_subdevdata(sd);
293 struct mt9v022 *mt9v022 = to_mt9v022(client);
295 mf->width = mt9v022->rect.width;
296 mf->height = mt9v022->rect.height;
297 mf->code = mt9v022->fmt->code;
298 mf->colorspace = mt9v022->fmt->colorspace;
299 mf->field = V4L2_FIELD_NONE;
304 static int mt9v022_s_fmt(struct v4l2_subdev *sd,
305 struct v4l2_mbus_framefmt *mf)
307 struct i2c_client *client = v4l2_get_subdevdata(sd);
308 struct mt9v022 *mt9v022 = to_mt9v022(client);
309 struct v4l2_crop a = {
311 .left = mt9v022->rect.left,
312 .top = mt9v022->rect.top,
314 .height = mf->height,
320 * The caller provides a supported format, as verified per call to
321 * icd->try_fmt(), datawidth is from our supported format list
324 case V4L2_MBUS_FMT_Y8_1X8:
325 case V4L2_MBUS_FMT_Y10_1X10:
326 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
329 case V4L2_MBUS_FMT_SBGGR8_1X8:
330 case V4L2_MBUS_FMT_SBGGR10_1X10:
331 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
338 /* No support for scaling on this camera, just crop. */
339 ret = mt9v022_s_crop(sd, &a);
341 mf->width = mt9v022->rect.width;
342 mf->height = mt9v022->rect.height;
343 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
344 mt9v022->fmts, mt9v022->num_fmts);
345 mf->colorspace = mt9v022->fmt->colorspace;
351 static int mt9v022_try_fmt(struct v4l2_subdev *sd,
352 struct v4l2_mbus_framefmt *mf)
354 struct i2c_client *client = v4l2_get_subdevdata(sd);
355 struct mt9v022 *mt9v022 = to_mt9v022(client);
356 const struct mt9v022_datafmt *fmt;
357 int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
358 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
360 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
361 MT9V022_MAX_WIDTH, align,
362 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
363 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
365 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
369 mf->code = fmt->code;
372 mf->colorspace = fmt->colorspace;
377 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
378 struct v4l2_dbg_chip_ident *id)
380 struct i2c_client *client = v4l2_get_subdevdata(sd);
381 struct mt9v022 *mt9v022 = to_mt9v022(client);
383 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
386 if (id->match.addr != client->addr)
389 id->ident = mt9v022->model;
395 #ifdef CONFIG_VIDEO_ADV_DEBUG
396 static int mt9v022_g_register(struct v4l2_subdev *sd,
397 struct v4l2_dbg_register *reg)
399 struct i2c_client *client = v4l2_get_subdevdata(sd);
401 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
404 if (reg->match.addr != client->addr)
408 reg->val = reg_read(client, reg->reg);
410 if (reg->val > 0xffff)
416 static int mt9v022_s_register(struct v4l2_subdev *sd,
417 struct v4l2_dbg_register *reg)
419 struct i2c_client *client = v4l2_get_subdevdata(sd);
421 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
424 if (reg->match.addr != client->addr)
427 if (reg_write(client, reg->reg, reg->val) < 0)
434 static const struct v4l2_queryctrl mt9v022_controls[] = {
436 .id = V4L2_CID_VFLIP,
437 .type = V4L2_CTRL_TYPE_BOOLEAN,
438 .name = "Flip Vertically",
444 .id = V4L2_CID_HFLIP,
445 .type = V4L2_CTRL_TYPE_BOOLEAN,
446 .name = "Flip Horizontally",
453 .type = V4L2_CTRL_TYPE_INTEGER,
454 .name = "Analog Gain",
459 .flags = V4L2_CTRL_FLAG_SLIDER,
461 .id = V4L2_CID_EXPOSURE,
462 .type = V4L2_CTRL_TYPE_INTEGER,
467 .default_value = 255,
468 .flags = V4L2_CTRL_FLAG_SLIDER,
470 .id = V4L2_CID_AUTOGAIN,
471 .type = V4L2_CTRL_TYPE_BOOLEAN,
472 .name = "Automatic Gain",
478 .id = V4L2_CID_EXPOSURE_AUTO,
479 .type = V4L2_CTRL_TYPE_BOOLEAN,
480 .name = "Automatic Exposure",
488 static struct soc_camera_ops mt9v022_ops = {
489 .controls = mt9v022_controls,
490 .num_controls = ARRAY_SIZE(mt9v022_controls),
493 static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
495 struct i2c_client *client = v4l2_get_subdevdata(sd);
496 const struct v4l2_queryctrl *qctrl;
500 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
504 data = reg_read(client, MT9V022_READ_MODE);
507 ctrl->value = !!(data & 0x10);
510 data = reg_read(client, MT9V022_READ_MODE);
513 ctrl->value = !!(data & 0x20);
515 case V4L2_CID_EXPOSURE_AUTO:
516 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
519 ctrl->value = !!(data & 0x1);
521 case V4L2_CID_AUTOGAIN:
522 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
525 ctrl->value = !!(data & 0x2);
528 data = reg_read(client, MT9V022_ANALOG_GAIN);
532 range = qctrl->maximum - qctrl->minimum;
533 ctrl->value = ((data - 16) * range + 24) / 48 + qctrl->minimum;
536 case V4L2_CID_EXPOSURE:
537 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
541 range = qctrl->maximum - qctrl->minimum;
542 ctrl->value = ((data - 1) * range + 239) / 479 + qctrl->minimum;
549 static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
552 struct i2c_client *client = v4l2_get_subdevdata(sd);
553 const struct v4l2_queryctrl *qctrl;
555 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
562 data = reg_set(client, MT9V022_READ_MODE, 0x10);
564 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
570 data = reg_set(client, MT9V022_READ_MODE, 0x20);
572 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
577 /* mt9v022 has minimum == default */
578 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
581 unsigned long range = qctrl->maximum - qctrl->minimum;
582 /* Valid values 16 to 64, 32 to 64 must be even. */
583 unsigned long gain = ((ctrl->value - qctrl->minimum) *
584 48 + range / 2) / range + 16;
588 * The user wants to set gain manually, hope, she
589 * knows, what she's doing... Switch AGC off.
592 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
595 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
596 reg_read(client, MT9V022_ANALOG_GAIN), gain);
597 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
601 case V4L2_CID_EXPOSURE:
602 /* mt9v022 has maximum == default */
603 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
606 unsigned long range = qctrl->maximum - qctrl->minimum;
607 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
608 479 + range / 2) / range + 1;
610 * The user wants to set shutter width manually, hope,
611 * she knows, what she's doing... Switch AEC off.
614 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
617 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
618 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
620 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
625 case V4L2_CID_AUTOGAIN:
627 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
629 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
633 case V4L2_CID_EXPOSURE_AUTO:
635 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
637 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
646 * Interface active, can use i2c. If it fails, it can indeed mean, that
647 * this wasn't our capture interface, so, we wait for the right one
649 static int mt9v022_video_probe(struct soc_camera_device *icd,
650 struct i2c_client *client)
652 struct mt9v022 *mt9v022 = to_mt9v022(client);
653 struct soc_camera_link *icl = to_soc_camera_link(icd);
658 /* We must have a parent by now. And it cannot be a wrong one. */
659 BUG_ON(!icd->parent ||
660 to_soc_camera_host(icd->parent)->nr != icd->iface);
662 /* Read out the chip version register */
663 data = reg_read(client, MT9V022_CHIP_VERSION);
665 /* must be 0x1311 or 0x1313 */
666 if (data != 0x1311 && data != 0x1313) {
668 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
674 ret = reg_write(client, MT9V022_RESET, 1);
677 /* 15 clock cycles */
679 if (reg_read(client, MT9V022_RESET)) {
680 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
686 /* Set monochrome or colour sensor type */
687 if (sensor_type && (!strcmp("colour", sensor_type) ||
688 !strcmp("color", sensor_type))) {
689 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
690 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
691 mt9v022->fmts = mt9v022_colour_fmts;
693 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
694 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
695 mt9v022->fmts = mt9v022_monochrome_fmts;
701 mt9v022->num_fmts = 0;
704 * This is a 10bit sensor, so by default we only allow 10bit.
705 * The platform may support different bus widths due to
706 * different routing of the data lines.
708 if (icl->query_bus_param)
709 flags = icl->query_bus_param(icl);
711 flags = SOCAM_DATAWIDTH_10;
713 if (flags & SOCAM_DATAWIDTH_10)
718 if (flags & SOCAM_DATAWIDTH_8)
721 mt9v022->fmt = &mt9v022->fmts[0];
723 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
724 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
725 "monochrome" : "colour");
727 ret = mt9v022_init(client);
729 dev_err(&client->dev, "Failed to initialise the camera\n");
735 static void mt9v022_video_remove(struct soc_camera_device *icd)
737 struct soc_camera_link *icl = to_soc_camera_link(icd);
739 dev_dbg(icd->pdev, "Video removed: %p, %p\n",
740 icd->parent, icd->vdev);
745 static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
747 struct i2c_client *client = v4l2_get_subdevdata(sd);
748 struct mt9v022 *mt9v022 = to_mt9v022(client);
750 *lines = mt9v022->y_skip_top;
755 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
756 .g_ctrl = mt9v022_g_ctrl,
757 .s_ctrl = mt9v022_s_ctrl,
758 .g_chip_ident = mt9v022_g_chip_ident,
759 #ifdef CONFIG_VIDEO_ADV_DEBUG
760 .g_register = mt9v022_g_register,
761 .s_register = mt9v022_s_register,
765 static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
766 enum v4l2_mbus_pixelcode *code)
768 struct i2c_client *client = v4l2_get_subdevdata(sd);
769 struct mt9v022 *mt9v022 = to_mt9v022(client);
771 if (index >= mt9v022->num_fmts)
774 *code = mt9v022->fmts[index].code;
778 static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
779 struct v4l2_mbus_config *cfg)
781 struct i2c_client *client = v4l2_get_subdevdata(sd);
782 struct soc_camera_device *icd = client->dev.platform_data;
783 struct soc_camera_link *icl = to_soc_camera_link(icd);
785 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
786 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
787 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
788 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
789 V4L2_MBUS_DATA_ACTIVE_HIGH;
790 cfg->type = V4L2_MBUS_PARALLEL;
791 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
796 static int mt9v022_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);
802 struct mt9v022 *mt9v022 = to_mt9v022(client);
803 unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
805 * Cannot use icd->current_fmt->host_fmt->bits_per_sample, because that
806 * is the number of bits, that the host has to sample, not the number of
807 * bits, that we have to send. See mx3_camera.c for an example of 10-bit
808 * formats being truncated to 8 bits by the host.
810 unsigned int bps = soc_mbus_get_fmtdesc(icd->current_fmt->code)->bits_per_sample;
814 dev_info(icd->pdev, "set %d: %s, %dbps\n", icd->current_fmt->code,
815 icd->current_fmt->host_fmt->name, bps);
817 if (icl->set_bus_param) {
818 ret = icl->set_bus_param(icl, 1 << (bps - 1));
821 } else if (bps != 10) {
823 * Without board specific bus width settings we only support the
824 * sensors native bus width
829 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
832 if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
835 if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
838 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
842 if (!(flags & V4L2_MBUS_MASTER))
843 mt9v022->chip_control &= ~0x8;
845 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
849 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
850 pixclk, mt9v022->chip_control);
855 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
856 .s_stream = mt9v022_s_stream,
857 .s_mbus_fmt = mt9v022_s_fmt,
858 .g_mbus_fmt = mt9v022_g_fmt,
859 .try_mbus_fmt = mt9v022_try_fmt,
860 .s_crop = mt9v022_s_crop,
861 .g_crop = mt9v022_g_crop,
862 .cropcap = mt9v022_cropcap,
863 .enum_mbus_fmt = mt9v022_enum_fmt,
864 .g_mbus_config = mt9v022_g_mbus_config,
865 .s_mbus_config = mt9v022_s_mbus_config,
868 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
869 .g_skip_top_lines = mt9v022_g_skip_top_lines,
872 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
873 .core = &mt9v022_subdev_core_ops,
874 .video = &mt9v022_subdev_video_ops,
875 .sensor = &mt9v022_subdev_sensor_ops,
878 static int mt9v022_probe(struct i2c_client *client,
879 const struct i2c_device_id *did)
881 struct mt9v022 *mt9v022;
882 struct soc_camera_device *icd = client->dev.platform_data;
883 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
884 struct soc_camera_link *icl;
888 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
892 icl = to_soc_camera_link(icd);
894 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
898 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
899 dev_warn(&adapter->dev,
900 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
904 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
908 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
910 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
912 icd->ops = &mt9v022_ops;
914 * MT9V022 _really_ corrupts the first read out line.
915 * TODO: verify on i.MX31
917 mt9v022->y_skip_top = 1;
918 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
919 mt9v022->rect.top = MT9V022_ROW_SKIP;
920 mt9v022->rect.width = MT9V022_MAX_WIDTH;
921 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
923 ret = mt9v022_video_probe(icd, client);
932 static int mt9v022_remove(struct i2c_client *client)
934 struct mt9v022 *mt9v022 = to_mt9v022(client);
935 struct soc_camera_device *icd = client->dev.platform_data;
938 mt9v022_video_remove(icd);
943 static const struct i2c_device_id mt9v022_id[] = {
947 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
949 static struct i2c_driver mt9v022_i2c_driver = {
953 .probe = mt9v022_probe,
954 .remove = mt9v022_remove,
955 .id_table = mt9v022_id,
958 static int __init mt9v022_mod_init(void)
960 return i2c_add_driver(&mt9v022_i2c_driver);
963 static void __exit mt9v022_mod_exit(void)
965 i2c_del_driver(&mt9v022_i2c_driver);
968 module_init(mt9v022_mod_init);
969 module_exit(mt9v022_mod_exit);
971 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
972 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
973 MODULE_LICENSE("GPL");