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/mt9v022.h>
19 #include <media/soc_camera.h>
20 #include <media/soc_mediabus.h>
21 #include <media/v4l2-subdev.h>
22 #include <media/v4l2-chip-ident.h>
23 #include <media/v4l2-ctrls.h>
26 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
27 * The platform has to define struct i2c_board_info objects and link to them
28 * from struct soc_camera_link
31 static char *sensor_type;
32 module_param(sensor_type, charp, S_IRUGO);
33 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
35 /* mt9v022 selected register addresses */
36 #define MT9V022_CHIP_VERSION 0x00
37 #define MT9V022_COLUMN_START 0x01
38 #define MT9V022_ROW_START 0x02
39 #define MT9V022_WINDOW_HEIGHT 0x03
40 #define MT9V022_WINDOW_WIDTH 0x04
41 #define MT9V022_HORIZONTAL_BLANKING 0x05
42 #define MT9V022_VERTICAL_BLANKING 0x06
43 #define MT9V022_CHIP_CONTROL 0x07
44 #define MT9V022_SHUTTER_WIDTH1 0x08
45 #define MT9V022_SHUTTER_WIDTH2 0x09
46 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
47 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
48 #define MT9V022_RESET 0x0c
49 #define MT9V022_READ_MODE 0x0d
50 #define MT9V022_MONITOR_MODE 0x0e
51 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
52 #define MT9V022_LED_OUT_CONTROL 0x1b
53 #define MT9V022_ADC_MODE_CONTROL 0x1c
54 #define MT9V022_REG32 0x20
55 #define MT9V022_ANALOG_GAIN 0x35
56 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
57 #define MT9V022_PIXCLK_FV_LV 0x74
58 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
59 #define MT9V022_AEC_AGC_ENABLE 0xAF
60 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
62 /* mt9v024 partial list register addresses changes with respect to mt9v022 */
63 #define MT9V024_PIXCLK_FV_LV 0x72
64 #define MT9V024_MAX_TOTAL_SHUTTER_WIDTH 0xAD
66 /* Progressive scan, master, defaults */
67 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
69 #define MT9V022_MAX_WIDTH 752
70 #define MT9V022_MAX_HEIGHT 480
71 #define MT9V022_MIN_WIDTH 48
72 #define MT9V022_MIN_HEIGHT 32
73 #define MT9V022_COLUMN_SKIP 1
74 #define MT9V022_ROW_SKIP 4
76 #define MT9V022_HORIZONTAL_BLANKING_MIN 43
77 #define MT9V022_HORIZONTAL_BLANKING_MAX 1023
78 #define MT9V022_HORIZONTAL_BLANKING_DEF 94
79 #define MT9V022_VERTICAL_BLANKING_MIN 2
80 #define MT9V022_VERTICAL_BLANKING_MAX 3000
81 #define MT9V022_VERTICAL_BLANKING_DEF 45
83 #define is_mt9v022_rev3(id) (id == 0x1313)
84 #define is_mt9v024(id) (id == 0x1324)
86 /* MT9V022 has only one fixed colorspace per pixelcode */
87 struct mt9v022_datafmt {
88 enum v4l2_mbus_pixelcode code;
89 enum v4l2_colorspace colorspace;
92 /* Find a data format by a pixel code in an array */
93 static const struct mt9v022_datafmt *mt9v022_find_datafmt(
94 enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
98 for (i = 0; i < n; i++)
99 if (fmt[i].code == code)
105 static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
107 * Order important: first natively supported,
108 * second supported with a GPIO extender
110 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
111 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
114 static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
115 /* Order important - see above */
116 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
117 {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
120 /* only registers with different addresses on different mt9v02x sensors */
121 struct mt9v02x_register {
122 u8 max_total_shutter_width;
126 static const struct mt9v02x_register mt9v022_register = {
127 .max_total_shutter_width = MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
128 .pixclk_fv_lv = MT9V022_PIXCLK_FV_LV,
131 static const struct mt9v02x_register mt9v024_register = {
132 .max_total_shutter_width = MT9V024_MAX_TOTAL_SHUTTER_WIDTH,
133 .pixclk_fv_lv = MT9V024_PIXCLK_FV_LV,
137 struct v4l2_subdev subdev;
138 struct v4l2_ctrl_handler hdl;
140 /* exposure/auto-exposure cluster */
141 struct v4l2_ctrl *autoexposure;
142 struct v4l2_ctrl *exposure;
145 /* gain/auto-gain cluster */
146 struct v4l2_ctrl *autogain;
147 struct v4l2_ctrl *gain;
149 struct v4l2_ctrl *hblank;
150 struct v4l2_ctrl *vblank;
151 struct v4l2_rect rect; /* Sensor window */
152 const struct mt9v022_datafmt *fmt;
153 const struct mt9v022_datafmt *fmts;
154 const struct mt9v02x_register *reg;
156 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
159 unsigned short y_skip_top; /* Lines to skip at the top */
162 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
164 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
167 static int reg_read(struct i2c_client *client, const u8 reg)
169 return i2c_smbus_read_word_swapped(client, reg);
172 static int reg_write(struct i2c_client *client, const u8 reg,
175 return i2c_smbus_write_word_swapped(client, reg, data);
178 static int reg_set(struct i2c_client *client, const u8 reg,
183 ret = reg_read(client, reg);
186 return reg_write(client, reg, ret | data);
189 static int reg_clear(struct i2c_client *client, const u8 reg,
194 ret = reg_read(client, reg);
197 return reg_write(client, reg, ret & ~data);
200 static int mt9v022_init(struct i2c_client *client)
202 struct mt9v022 *mt9v022 = to_mt9v022(client);
206 * Almost the default mode: master, parallel, simultaneous, and an
207 * undocumented bit 0x200, which is present in table 7, but not in 8,
208 * plus snapshot mode to disable scan for now
210 mt9v022->chip_control |= 0x10;
211 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
213 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
218 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
220 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
222 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
224 ret = reg_write(client, mt9v022->reg->max_total_shutter_width, 480);
227 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
229 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
231 return v4l2_ctrl_handler_setup(&mt9v022->hdl);
236 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
238 struct i2c_client *client = v4l2_get_subdevdata(sd);
239 struct mt9v022 *mt9v022 = to_mt9v022(client);
242 /* Switch to master "normal" mode */
243 mt9v022->chip_control &= ~0x10;
244 if (is_mt9v022_rev3(mt9v022->chip_version) ||
245 is_mt9v024(mt9v022->chip_version)) {
247 * Unset snapshot mode specific settings: clear bit 9
248 * and bit 2 in reg. 0x20 when in normal mode.
250 if (reg_clear(client, MT9V022_REG32, 0x204))
254 /* Switch to snapshot mode */
255 mt9v022->chip_control |= 0x10;
256 if (is_mt9v022_rev3(mt9v022->chip_version) ||
257 is_mt9v024(mt9v022->chip_version)) {
259 * Required settings for snapshot mode: set bit 9
260 * (RST enable) and bit 2 (CR enable) in reg. 0x20
261 * See TechNote TN0960 or TN-09-225.
263 if (reg_set(client, MT9V022_REG32, 0x204))
268 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
273 static int mt9v022_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
275 struct i2c_client *client = v4l2_get_subdevdata(sd);
276 struct mt9v022 *mt9v022 = to_mt9v022(client);
277 struct v4l2_rect rect = a->c;
280 /* Bayer format - even size lengths */
281 if (mt9v022->fmts == mt9v022_colour_fmts) {
282 rect.width = ALIGN(rect.width, 2);
283 rect.height = ALIGN(rect.height, 2);
284 /* Let the user play with the starting pixel */
287 soc_camera_limit_side(&rect.left, &rect.width,
288 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
290 soc_camera_limit_side(&rect.top, &rect.height,
291 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
293 /* Like in example app. Contradicts the datasheet though */
294 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
296 if (ret & 1) /* Autoexposure */
297 ret = reg_write(client, mt9v022->reg->max_total_shutter_width,
298 rect.height + mt9v022->y_skip_top + 43);
300 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
301 rect.height + mt9v022->y_skip_top + 43);
303 /* Setup frame format: defaults apart from width and height */
305 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
307 ret = reg_write(client, MT9V022_ROW_START, rect.top);
310 * Default 94, Phytec driver says:
311 * "width + horizontal blank >= 660"
313 ret = v4l2_ctrl_s_ctrl(mt9v022->hblank,
314 rect.width > 660 - 43 ? 43 : 660 - rect.width);
316 ret = v4l2_ctrl_s_ctrl(mt9v022->vblank, 45);
318 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
320 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
321 rect.height + mt9v022->y_skip_top);
326 dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
328 mt9v022->rect = rect;
333 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
335 struct i2c_client *client = v4l2_get_subdevdata(sd);
336 struct mt9v022 *mt9v022 = to_mt9v022(client);
338 a->c = mt9v022->rect;
339 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
344 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
346 a->bounds.left = MT9V022_COLUMN_SKIP;
347 a->bounds.top = MT9V022_ROW_SKIP;
348 a->bounds.width = MT9V022_MAX_WIDTH;
349 a->bounds.height = MT9V022_MAX_HEIGHT;
350 a->defrect = a->bounds;
351 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
352 a->pixelaspect.numerator = 1;
353 a->pixelaspect.denominator = 1;
358 static int mt9v022_g_fmt(struct v4l2_subdev *sd,
359 struct v4l2_mbus_framefmt *mf)
361 struct i2c_client *client = v4l2_get_subdevdata(sd);
362 struct mt9v022 *mt9v022 = to_mt9v022(client);
364 mf->width = mt9v022->rect.width;
365 mf->height = mt9v022->rect.height;
366 mf->code = mt9v022->fmt->code;
367 mf->colorspace = mt9v022->fmt->colorspace;
368 mf->field = V4L2_FIELD_NONE;
373 static int mt9v022_s_fmt(struct v4l2_subdev *sd,
374 struct v4l2_mbus_framefmt *mf)
376 struct i2c_client *client = v4l2_get_subdevdata(sd);
377 struct mt9v022 *mt9v022 = to_mt9v022(client);
378 struct v4l2_crop a = {
380 .left = mt9v022->rect.left,
381 .top = mt9v022->rect.top,
383 .height = mf->height,
389 * The caller provides a supported format, as verified per call to
390 * .try_mbus_fmt(), datawidth is from our supported format list
393 case V4L2_MBUS_FMT_Y8_1X8:
394 case V4L2_MBUS_FMT_Y10_1X10:
395 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
398 case V4L2_MBUS_FMT_SBGGR8_1X8:
399 case V4L2_MBUS_FMT_SBGGR10_1X10:
400 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
407 /* No support for scaling on this camera, just crop. */
408 ret = mt9v022_s_crop(sd, &a);
410 mf->width = mt9v022->rect.width;
411 mf->height = mt9v022->rect.height;
412 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
413 mt9v022->fmts, mt9v022->num_fmts);
414 mf->colorspace = mt9v022->fmt->colorspace;
420 static int mt9v022_try_fmt(struct v4l2_subdev *sd,
421 struct v4l2_mbus_framefmt *mf)
423 struct i2c_client *client = v4l2_get_subdevdata(sd);
424 struct mt9v022 *mt9v022 = to_mt9v022(client);
425 const struct mt9v022_datafmt *fmt;
426 int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
427 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
429 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
430 MT9V022_MAX_WIDTH, align,
431 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
432 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
434 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
438 mf->code = fmt->code;
441 mf->colorspace = fmt->colorspace;
446 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
447 struct v4l2_dbg_chip_ident *id)
449 struct i2c_client *client = v4l2_get_subdevdata(sd);
450 struct mt9v022 *mt9v022 = to_mt9v022(client);
452 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
455 if (id->match.addr != client->addr)
458 id->ident = mt9v022->model;
464 #ifdef CONFIG_VIDEO_ADV_DEBUG
465 static int mt9v022_g_register(struct v4l2_subdev *sd,
466 struct v4l2_dbg_register *reg)
468 struct i2c_client *client = v4l2_get_subdevdata(sd);
470 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
473 if (reg->match.addr != client->addr)
477 reg->val = reg_read(client, reg->reg);
479 if (reg->val > 0xffff)
485 static int mt9v022_s_register(struct v4l2_subdev *sd,
486 struct v4l2_dbg_register *reg)
488 struct i2c_client *client = v4l2_get_subdevdata(sd);
490 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
493 if (reg->match.addr != client->addr)
496 if (reg_write(client, reg->reg, reg->val) < 0)
503 static int mt9v022_s_power(struct v4l2_subdev *sd, int on)
505 struct i2c_client *client = v4l2_get_subdevdata(sd);
506 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
508 return soc_camera_set_power(&client->dev, icl, on);
511 static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
513 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
514 struct mt9v022, hdl);
515 struct v4l2_subdev *sd = &mt9v022->subdev;
516 struct i2c_client *client = v4l2_get_subdevdata(sd);
517 struct v4l2_ctrl *gain = mt9v022->gain;
518 struct v4l2_ctrl *exp = mt9v022->exposure;
523 case V4L2_CID_AUTOGAIN:
524 data = reg_read(client, MT9V022_ANALOG_GAIN);
528 range = gain->maximum - gain->minimum;
529 gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
531 case V4L2_CID_EXPOSURE_AUTO:
532 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
536 range = exp->maximum - exp->minimum;
537 exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
539 case V4L2_CID_HBLANK:
540 data = reg_read(client, MT9V022_HORIZONTAL_BLANKING);
545 case V4L2_CID_VBLANK:
546 data = reg_read(client, MT9V022_VERTICAL_BLANKING);
555 static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
557 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
558 struct mt9v022, hdl);
559 struct v4l2_subdev *sd = &mt9v022->subdev;
560 struct i2c_client *client = v4l2_get_subdevdata(sd);
566 data = reg_set(client, MT9V022_READ_MODE, 0x10);
568 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
574 data = reg_set(client, MT9V022_READ_MODE, 0x20);
576 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
580 case V4L2_CID_AUTOGAIN:
582 if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
585 struct v4l2_ctrl *gain = mt9v022->gain;
586 /* mt9v022 has minimum == default */
587 unsigned long range = gain->maximum - gain->minimum;
588 /* Valid values 16 to 64, 32 to 64 must be even. */
589 unsigned long gain_val = ((gain->val - gain->minimum) *
590 48 + range / 2) / range + 16;
596 * The user wants to set gain manually, hope, she
597 * knows, what she's doing... Switch AGC off.
599 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
602 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
603 reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
604 if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
608 case V4L2_CID_EXPOSURE_AUTO:
609 if (ctrl->val == V4L2_EXPOSURE_AUTO) {
610 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
612 struct v4l2_ctrl *exp = mt9v022->exposure;
613 unsigned long range = exp->maximum - exp->minimum;
614 unsigned long shutter = ((exp->val - exp->minimum) *
615 479 + range / 2) / range + 1;
618 * The user wants to set shutter width manually, hope,
619 * she knows, what she's doing... Switch AEC off.
621 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
624 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
625 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
627 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
632 case V4L2_CID_HBLANK:
633 if (reg_write(client, MT9V022_HORIZONTAL_BLANKING,
637 case V4L2_CID_VBLANK:
638 if (reg_write(client, MT9V022_VERTICAL_BLANKING,
647 * Interface active, can use i2c. If it fails, it can indeed mean, that
648 * this wasn't our capture interface, so, we wait for the right one
650 static int mt9v022_video_probe(struct i2c_client *client)
652 struct mt9v022 *mt9v022 = to_mt9v022(client);
653 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
658 ret = mt9v022_s_power(&mt9v022->subdev, 1);
662 /* Read out the chip version register */
663 data = reg_read(client, MT9V022_CHIP_VERSION);
665 /* must be 0x1311, 0x1313 or 0x1324 */
666 if (data != 0x1311 && data != 0x1313 && data != 0x1324) {
668 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
673 mt9v022->chip_version = data;
675 mt9v022->reg = is_mt9v024(data) ? &mt9v024_register :
679 ret = reg_write(client, MT9V022_RESET, 1);
682 /* 15 clock cycles */
684 if (reg_read(client, MT9V022_RESET)) {
685 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
691 /* Set monochrome or colour sensor type */
692 if (sensor_type && (!strcmp("colour", sensor_type) ||
693 !strcmp("color", sensor_type))) {
694 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
695 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
696 mt9v022->fmts = mt9v022_colour_fmts;
698 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
699 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
700 mt9v022->fmts = mt9v022_monochrome_fmts;
706 mt9v022->num_fmts = 0;
709 * This is a 10bit sensor, so by default we only allow 10bit.
710 * The platform may support different bus widths due to
711 * different routing of the data lines.
713 if (icl->query_bus_param)
714 flags = icl->query_bus_param(icl);
716 flags = SOCAM_DATAWIDTH_10;
718 if (flags & SOCAM_DATAWIDTH_10)
723 if (flags & SOCAM_DATAWIDTH_8)
726 mt9v022->fmt = &mt9v022->fmts[0];
728 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
729 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
730 "monochrome" : "colour");
732 ret = mt9v022_init(client);
734 dev_err(&client->dev, "Failed to initialise the camera\n");
737 mt9v022_s_power(&mt9v022->subdev, 0);
741 static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
743 struct i2c_client *client = v4l2_get_subdevdata(sd);
744 struct mt9v022 *mt9v022 = to_mt9v022(client);
746 *lines = mt9v022->y_skip_top;
751 static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
752 .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
753 .s_ctrl = mt9v022_s_ctrl,
756 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
757 .g_chip_ident = mt9v022_g_chip_ident,
758 #ifdef CONFIG_VIDEO_ADV_DEBUG
759 .g_register = mt9v022_g_register,
760 .s_register = mt9v022_s_register,
762 .s_power = mt9v022_s_power,
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_link *icl = soc_camera_i2c_to_link(client);
784 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
785 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
786 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
787 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
788 V4L2_MBUS_DATA_ACTIVE_HIGH;
789 cfg->type = V4L2_MBUS_PARALLEL;
790 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
795 static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
796 const struct v4l2_mbus_config *cfg)
798 struct i2c_client *client = v4l2_get_subdevdata(sd);
799 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
800 struct mt9v022 *mt9v022 = to_mt9v022(client);
801 unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
802 unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
806 if (icl->set_bus_param) {
807 ret = icl->set_bus_param(icl, 1 << (bps - 1));
810 } else if (bps != 10) {
812 * Without board specific bus width settings we only support the
813 * sensors native bus width
818 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
821 if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
824 if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
827 ret = reg_write(client, mt9v022->reg->pixclk_fv_lv, pixclk);
831 if (!(flags & V4L2_MBUS_MASTER))
832 mt9v022->chip_control &= ~0x8;
834 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
838 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
839 pixclk, mt9v022->chip_control);
844 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
845 .s_stream = mt9v022_s_stream,
846 .s_mbus_fmt = mt9v022_s_fmt,
847 .g_mbus_fmt = mt9v022_g_fmt,
848 .try_mbus_fmt = mt9v022_try_fmt,
849 .s_crop = mt9v022_s_crop,
850 .g_crop = mt9v022_g_crop,
851 .cropcap = mt9v022_cropcap,
852 .enum_mbus_fmt = mt9v022_enum_fmt,
853 .g_mbus_config = mt9v022_g_mbus_config,
854 .s_mbus_config = mt9v022_s_mbus_config,
857 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
858 .g_skip_top_lines = mt9v022_g_skip_top_lines,
861 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
862 .core = &mt9v022_subdev_core_ops,
863 .video = &mt9v022_subdev_video_ops,
864 .sensor = &mt9v022_subdev_sensor_ops,
867 static int mt9v022_probe(struct i2c_client *client,
868 const struct i2c_device_id *did)
870 struct mt9v022 *mt9v022;
871 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
872 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
873 struct mt9v022_platform_data *pdata = icl->priv;
877 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
881 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
882 dev_warn(&adapter->dev,
883 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
887 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
891 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
892 v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
893 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
894 V4L2_CID_VFLIP, 0, 1, 1, 0);
895 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
896 V4L2_CID_HFLIP, 0, 1, 1, 0);
897 mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
898 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
899 mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
900 V4L2_CID_GAIN, 0, 127, 1, 64);
903 * Simulated autoexposure. If enabled, we calculate shutter width
904 * ourselves in the driver based on vertical blanking and frame width
906 mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
907 &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
909 mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
910 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
912 mt9v022->hblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
913 V4L2_CID_HBLANK, MT9V022_HORIZONTAL_BLANKING_MIN,
914 MT9V022_HORIZONTAL_BLANKING_MAX, 1,
915 MT9V022_HORIZONTAL_BLANKING_DEF);
917 mt9v022->vblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
918 V4L2_CID_VBLANK, MT9V022_VERTICAL_BLANKING_MIN,
919 MT9V022_VERTICAL_BLANKING_MAX, 1,
920 MT9V022_VERTICAL_BLANKING_DEF);
922 mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
923 if (mt9v022->hdl.error) {
924 int err = mt9v022->hdl.error;
926 dev_err(&client->dev, "control initialisation err %d\n", err);
930 v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
931 V4L2_EXPOSURE_MANUAL, true);
932 v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
934 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
937 * On some platforms the first read out line is corrupted.
938 * Workaround it by skipping if indicated by platform data.
940 mt9v022->y_skip_top = pdata ? pdata->y_skip_top : 0;
941 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
942 mt9v022->rect.top = MT9V022_ROW_SKIP;
943 mt9v022->rect.width = MT9V022_MAX_WIDTH;
944 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
946 ret = mt9v022_video_probe(client);
948 v4l2_ctrl_handler_free(&mt9v022->hdl);
955 static int mt9v022_remove(struct i2c_client *client)
957 struct mt9v022 *mt9v022 = to_mt9v022(client);
958 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
960 v4l2_device_unregister_subdev(&mt9v022->subdev);
963 v4l2_ctrl_handler_free(&mt9v022->hdl);
968 static const struct i2c_device_id mt9v022_id[] = {
972 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
974 static struct i2c_driver mt9v022_i2c_driver = {
978 .probe = mt9v022_probe,
979 .remove = mt9v022_remove,
980 .id_table = mt9v022_id,
983 module_i2c_driver(mt9v022_i2c_driver);
985 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
986 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
987 MODULE_LICENSE("GPL");