Merge branch 'integrity-check-patch-v2' of git://btrfs.giantdisaster.de/git/btrfs...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / mt9v022.c
blob690ee0d42eebbcbb8aaf7374ca49a7302716180f
1 /*
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.
9 */
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 ctruct 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 /* Progressive scan, master, defaults */
61 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
63 #define MT9V022_MAX_WIDTH 752
64 #define MT9V022_MAX_HEIGHT 480
65 #define MT9V022_MIN_WIDTH 48
66 #define MT9V022_MIN_HEIGHT 32
67 #define MT9V022_COLUMN_SKIP 1
68 #define MT9V022_ROW_SKIP 4
70 /* MT9V022 has only one fixed colorspace per pixelcode */
71 struct mt9v022_datafmt {
72 enum v4l2_mbus_pixelcode code;
73 enum v4l2_colorspace colorspace;
76 /* Find a data format by a pixel code in an array */
77 static const struct mt9v022_datafmt *mt9v022_find_datafmt(
78 enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
79 int n)
81 int i;
82 for (i = 0; i < n; i++)
83 if (fmt[i].code == code)
84 return fmt + i;
86 return NULL;
89 static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
91 * Order important: first natively supported,
92 * second supported with a GPIO extender
94 {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
95 {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
98 static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
99 /* Order important - see above */
100 {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
101 {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
104 struct mt9v022 {
105 struct v4l2_subdev subdev;
106 struct v4l2_ctrl_handler hdl;
107 struct {
108 /* exposure/auto-exposure cluster */
109 struct v4l2_ctrl *autoexposure;
110 struct v4l2_ctrl *exposure;
112 struct {
113 /* gain/auto-gain cluster */
114 struct v4l2_ctrl *autogain;
115 struct v4l2_ctrl *gain;
117 struct v4l2_rect rect; /* Sensor window */
118 const struct mt9v022_datafmt *fmt;
119 const struct mt9v022_datafmt *fmts;
120 int num_fmts;
121 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
122 u16 chip_control;
123 unsigned short y_skip_top; /* Lines to skip at the top */
126 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
128 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
131 static int reg_read(struct i2c_client *client, const u8 reg)
133 s32 data = i2c_smbus_read_word_data(client, reg);
134 return data < 0 ? data : swab16(data);
137 static int reg_write(struct i2c_client *client, const u8 reg,
138 const u16 data)
140 return i2c_smbus_write_word_data(client, reg, swab16(data));
143 static int reg_set(struct i2c_client *client, const u8 reg,
144 const u16 data)
146 int ret;
148 ret = reg_read(client, reg);
149 if (ret < 0)
150 return ret;
151 return reg_write(client, reg, ret | data);
154 static int reg_clear(struct i2c_client *client, const u8 reg,
155 const u16 data)
157 int ret;
159 ret = reg_read(client, reg);
160 if (ret < 0)
161 return ret;
162 return reg_write(client, reg, ret & ~data);
165 static int mt9v022_init(struct i2c_client *client)
167 struct mt9v022 *mt9v022 = to_mt9v022(client);
168 int ret;
171 * Almost the default mode: master, parallel, simultaneous, and an
172 * undocumented bit 0x200, which is present in table 7, but not in 8,
173 * plus snapshot mode to disable scan for now
175 mt9v022->chip_control |= 0x10;
176 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
177 if (!ret)
178 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
180 /* All defaults */
181 if (!ret)
182 /* AEC, AGC on */
183 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
184 if (!ret)
185 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
186 if (!ret)
187 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
188 if (!ret)
189 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
190 if (!ret)
191 /* default - auto */
192 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
193 if (!ret)
194 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
195 if (!ret)
196 return v4l2_ctrl_handler_setup(&mt9v022->hdl);
198 return ret;
201 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
203 struct i2c_client *client = v4l2_get_subdevdata(sd);
204 struct mt9v022 *mt9v022 = to_mt9v022(client);
206 if (enable)
207 /* Switch to master "normal" mode */
208 mt9v022->chip_control &= ~0x10;
209 else
210 /* Switch to snapshot mode */
211 mt9v022->chip_control |= 0x10;
213 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
214 return -EIO;
215 return 0;
218 static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
220 struct i2c_client *client = v4l2_get_subdevdata(sd);
221 struct mt9v022 *mt9v022 = to_mt9v022(client);
222 struct v4l2_rect rect = a->c;
223 int ret;
225 /* Bayer format - even size lengths */
226 if (mt9v022->fmts == mt9v022_colour_fmts) {
227 rect.width = ALIGN(rect.width, 2);
228 rect.height = ALIGN(rect.height, 2);
229 /* Let the user play with the starting pixel */
232 soc_camera_limit_side(&rect.left, &rect.width,
233 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
235 soc_camera_limit_side(&rect.top, &rect.height,
236 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
238 /* Like in example app. Contradicts the datasheet though */
239 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
240 if (ret >= 0) {
241 if (ret & 1) /* Autoexposure */
242 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
243 rect.height + mt9v022->y_skip_top + 43);
244 else
245 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
246 rect.height + mt9v022->y_skip_top + 43);
248 /* Setup frame format: defaults apart from width and height */
249 if (!ret)
250 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
251 if (!ret)
252 ret = reg_write(client, MT9V022_ROW_START, rect.top);
253 if (!ret)
255 * Default 94, Phytec driver says:
256 * "width + horizontal blank >= 660"
258 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
259 rect.width > 660 - 43 ? 43 :
260 660 - rect.width);
261 if (!ret)
262 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
263 if (!ret)
264 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
265 if (!ret)
266 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
267 rect.height + mt9v022->y_skip_top);
269 if (ret < 0)
270 return ret;
272 dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
274 mt9v022->rect = rect;
276 return 0;
279 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
281 struct i2c_client *client = v4l2_get_subdevdata(sd);
282 struct mt9v022 *mt9v022 = to_mt9v022(client);
284 a->c = mt9v022->rect;
285 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
287 return 0;
290 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
292 a->bounds.left = MT9V022_COLUMN_SKIP;
293 a->bounds.top = MT9V022_ROW_SKIP;
294 a->bounds.width = MT9V022_MAX_WIDTH;
295 a->bounds.height = MT9V022_MAX_HEIGHT;
296 a->defrect = a->bounds;
297 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
298 a->pixelaspect.numerator = 1;
299 a->pixelaspect.denominator = 1;
301 return 0;
304 static int mt9v022_g_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);
310 mf->width = mt9v022->rect.width;
311 mf->height = mt9v022->rect.height;
312 mf->code = mt9v022->fmt->code;
313 mf->colorspace = mt9v022->fmt->colorspace;
314 mf->field = V4L2_FIELD_NONE;
316 return 0;
319 static int mt9v022_s_fmt(struct v4l2_subdev *sd,
320 struct v4l2_mbus_framefmt *mf)
322 struct i2c_client *client = v4l2_get_subdevdata(sd);
323 struct mt9v022 *mt9v022 = to_mt9v022(client);
324 struct v4l2_crop a = {
325 .c = {
326 .left = mt9v022->rect.left,
327 .top = mt9v022->rect.top,
328 .width = mf->width,
329 .height = mf->height,
332 int ret;
335 * The caller provides a supported format, as verified per call to
336 * .try_mbus_fmt(), datawidth is from our supported format list
338 switch (mf->code) {
339 case V4L2_MBUS_FMT_Y8_1X8:
340 case V4L2_MBUS_FMT_Y10_1X10:
341 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
342 return -EINVAL;
343 break;
344 case V4L2_MBUS_FMT_SBGGR8_1X8:
345 case V4L2_MBUS_FMT_SBGGR10_1X10:
346 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
347 return -EINVAL;
348 break;
349 default:
350 return -EINVAL;
353 /* No support for scaling on this camera, just crop. */
354 ret = mt9v022_s_crop(sd, &a);
355 if (!ret) {
356 mf->width = mt9v022->rect.width;
357 mf->height = mt9v022->rect.height;
358 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
359 mt9v022->fmts, mt9v022->num_fmts);
360 mf->colorspace = mt9v022->fmt->colorspace;
363 return ret;
366 static int mt9v022_try_fmt(struct v4l2_subdev *sd,
367 struct v4l2_mbus_framefmt *mf)
369 struct i2c_client *client = v4l2_get_subdevdata(sd);
370 struct mt9v022 *mt9v022 = to_mt9v022(client);
371 const struct mt9v022_datafmt *fmt;
372 int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
373 mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
375 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
376 MT9V022_MAX_WIDTH, align,
377 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
378 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
380 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
381 mt9v022->num_fmts);
382 if (!fmt) {
383 fmt = mt9v022->fmt;
384 mf->code = fmt->code;
387 mf->colorspace = fmt->colorspace;
389 return 0;
392 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
393 struct v4l2_dbg_chip_ident *id)
395 struct i2c_client *client = v4l2_get_subdevdata(sd);
396 struct mt9v022 *mt9v022 = to_mt9v022(client);
398 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
399 return -EINVAL;
401 if (id->match.addr != client->addr)
402 return -ENODEV;
404 id->ident = mt9v022->model;
405 id->revision = 0;
407 return 0;
410 #ifdef CONFIG_VIDEO_ADV_DEBUG
411 static int mt9v022_g_register(struct v4l2_subdev *sd,
412 struct v4l2_dbg_register *reg)
414 struct i2c_client *client = v4l2_get_subdevdata(sd);
416 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
417 return -EINVAL;
419 if (reg->match.addr != client->addr)
420 return -ENODEV;
422 reg->size = 2;
423 reg->val = reg_read(client, reg->reg);
425 if (reg->val > 0xffff)
426 return -EIO;
428 return 0;
431 static int mt9v022_s_register(struct v4l2_subdev *sd,
432 struct v4l2_dbg_register *reg)
434 struct i2c_client *client = v4l2_get_subdevdata(sd);
436 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
437 return -EINVAL;
439 if (reg->match.addr != client->addr)
440 return -ENODEV;
442 if (reg_write(client, reg->reg, reg->val) < 0)
443 return -EIO;
445 return 0;
447 #endif
449 static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
451 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
452 struct mt9v022, hdl);
453 struct v4l2_subdev *sd = &mt9v022->subdev;
454 struct i2c_client *client = v4l2_get_subdevdata(sd);
455 struct v4l2_ctrl *gain = mt9v022->gain;
456 struct v4l2_ctrl *exp = mt9v022->exposure;
457 unsigned long range;
458 int data;
460 switch (ctrl->id) {
461 case V4L2_CID_AUTOGAIN:
462 data = reg_read(client, MT9V022_ANALOG_GAIN);
463 if (data < 0)
464 return -EIO;
466 range = gain->maximum - gain->minimum;
467 gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
468 return 0;
469 case V4L2_CID_EXPOSURE_AUTO:
470 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
471 if (data < 0)
472 return -EIO;
474 range = exp->maximum - exp->minimum;
475 exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
476 return 0;
478 return -EINVAL;
481 static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
483 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
484 struct mt9v022, hdl);
485 struct v4l2_subdev *sd = &mt9v022->subdev;
486 struct i2c_client *client = v4l2_get_subdevdata(sd);
487 int data;
489 switch (ctrl->id) {
490 case V4L2_CID_VFLIP:
491 if (ctrl->val)
492 data = reg_set(client, MT9V022_READ_MODE, 0x10);
493 else
494 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
495 if (data < 0)
496 return -EIO;
497 return 0;
498 case V4L2_CID_HFLIP:
499 if (ctrl->val)
500 data = reg_set(client, MT9V022_READ_MODE, 0x20);
501 else
502 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
503 if (data < 0)
504 return -EIO;
505 return 0;
506 case V4L2_CID_AUTOGAIN:
507 if (ctrl->val) {
508 if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
509 return -EIO;
510 } else {
511 struct v4l2_ctrl *gain = mt9v022->gain;
512 /* mt9v022 has minimum == default */
513 unsigned long range = gain->maximum - gain->minimum;
514 /* Valid values 16 to 64, 32 to 64 must be even. */
515 unsigned long gain_val = ((gain->val - gain->minimum) *
516 48 + range / 2) / range + 16;
518 if (gain_val >= 32)
519 gain_val &= ~1;
522 * The user wants to set gain manually, hope, she
523 * knows, what she's doing... Switch AGC off.
525 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
526 return -EIO;
528 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
529 reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
530 if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
531 return -EIO;
533 return 0;
534 case V4L2_CID_EXPOSURE_AUTO:
535 if (ctrl->val == V4L2_EXPOSURE_AUTO) {
536 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
537 } else {
538 struct v4l2_ctrl *exp = mt9v022->exposure;
539 unsigned long range = exp->maximum - exp->minimum;
540 unsigned long shutter = ((exp->val - exp->minimum) *
541 479 + range / 2) / range + 1;
544 * The user wants to set shutter width manually, hope,
545 * she knows, what she's doing... Switch AEC off.
547 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
548 if (data < 0)
549 return -EIO;
550 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
551 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
552 shutter);
553 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
554 shutter) < 0)
555 return -EIO;
557 return 0;
559 return -EINVAL;
563 * Interface active, can use i2c. If it fails, it can indeed mean, that
564 * this wasn't our capture interface, so, we wait for the right one
566 static int mt9v022_video_probe(struct i2c_client *client)
568 struct mt9v022 *mt9v022 = to_mt9v022(client);
569 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
570 s32 data;
571 int ret;
572 unsigned long flags;
574 /* Read out the chip version register */
575 data = reg_read(client, MT9V022_CHIP_VERSION);
577 /* must be 0x1311 or 0x1313 */
578 if (data != 0x1311 && data != 0x1313) {
579 ret = -ENODEV;
580 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
581 data);
582 goto ei2c;
585 /* Soft reset */
586 ret = reg_write(client, MT9V022_RESET, 1);
587 if (ret < 0)
588 goto ei2c;
589 /* 15 clock cycles */
590 udelay(200);
591 if (reg_read(client, MT9V022_RESET)) {
592 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
593 if (ret > 0)
594 ret = -EIO;
595 goto ei2c;
598 /* Set monochrome or colour sensor type */
599 if (sensor_type && (!strcmp("colour", sensor_type) ||
600 !strcmp("color", sensor_type))) {
601 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
602 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
603 mt9v022->fmts = mt9v022_colour_fmts;
604 } else {
605 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
606 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
607 mt9v022->fmts = mt9v022_monochrome_fmts;
610 if (ret < 0)
611 goto ei2c;
613 mt9v022->num_fmts = 0;
616 * This is a 10bit sensor, so by default we only allow 10bit.
617 * The platform may support different bus widths due to
618 * different routing of the data lines.
620 if (icl->query_bus_param)
621 flags = icl->query_bus_param(icl);
622 else
623 flags = SOCAM_DATAWIDTH_10;
625 if (flags & SOCAM_DATAWIDTH_10)
626 mt9v022->num_fmts++;
627 else
628 mt9v022->fmts++;
630 if (flags & SOCAM_DATAWIDTH_8)
631 mt9v022->num_fmts++;
633 mt9v022->fmt = &mt9v022->fmts[0];
635 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
636 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
637 "monochrome" : "colour");
639 ret = mt9v022_init(client);
640 if (ret < 0)
641 dev_err(&client->dev, "Failed to initialise the camera\n");
643 ei2c:
644 return ret;
647 static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
649 struct i2c_client *client = v4l2_get_subdevdata(sd);
650 struct mt9v022 *mt9v022 = to_mt9v022(client);
652 *lines = mt9v022->y_skip_top;
654 return 0;
657 static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
658 .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
659 .s_ctrl = mt9v022_s_ctrl,
662 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
663 .g_chip_ident = mt9v022_g_chip_ident,
664 #ifdef CONFIG_VIDEO_ADV_DEBUG
665 .g_register = mt9v022_g_register,
666 .s_register = mt9v022_s_register,
667 #endif
670 static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
671 enum v4l2_mbus_pixelcode *code)
673 struct i2c_client *client = v4l2_get_subdevdata(sd);
674 struct mt9v022 *mt9v022 = to_mt9v022(client);
676 if (index >= mt9v022->num_fmts)
677 return -EINVAL;
679 *code = mt9v022->fmts[index].code;
680 return 0;
683 static int mt9v022_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_link *icl = soc_camera_i2c_to_link(client);
689 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
690 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
691 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
692 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
693 V4L2_MBUS_DATA_ACTIVE_HIGH;
694 cfg->type = V4L2_MBUS_PARALLEL;
695 cfg->flags = soc_camera_apply_board_flags(icl, cfg);
697 return 0;
700 static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
701 const struct v4l2_mbus_config *cfg)
703 struct i2c_client *client = v4l2_get_subdevdata(sd);
704 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
705 struct mt9v022 *mt9v022 = to_mt9v022(client);
706 unsigned long flags = soc_camera_apply_board_flags(icl, cfg);
707 unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
708 int ret;
709 u16 pixclk = 0;
711 if (icl->set_bus_param) {
712 ret = icl->set_bus_param(icl, 1 << (bps - 1));
713 if (ret)
714 return ret;
715 } else if (bps != 10) {
717 * Without board specific bus width settings we only support the
718 * sensors native bus width
720 return -EINVAL;
723 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
724 pixclk |= 0x10;
726 if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
727 pixclk |= 0x1;
729 if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
730 pixclk |= 0x2;
732 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
733 if (ret < 0)
734 return ret;
736 if (!(flags & V4L2_MBUS_MASTER))
737 mt9v022->chip_control &= ~0x8;
739 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
740 if (ret < 0)
741 return ret;
743 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
744 pixclk, mt9v022->chip_control);
746 return 0;
749 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
750 .s_stream = mt9v022_s_stream,
751 .s_mbus_fmt = mt9v022_s_fmt,
752 .g_mbus_fmt = mt9v022_g_fmt,
753 .try_mbus_fmt = mt9v022_try_fmt,
754 .s_crop = mt9v022_s_crop,
755 .g_crop = mt9v022_g_crop,
756 .cropcap = mt9v022_cropcap,
757 .enum_mbus_fmt = mt9v022_enum_fmt,
758 .g_mbus_config = mt9v022_g_mbus_config,
759 .s_mbus_config = mt9v022_s_mbus_config,
762 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
763 .g_skip_top_lines = mt9v022_g_skip_top_lines,
766 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
767 .core = &mt9v022_subdev_core_ops,
768 .video = &mt9v022_subdev_video_ops,
769 .sensor = &mt9v022_subdev_sensor_ops,
772 static int mt9v022_probe(struct i2c_client *client,
773 const struct i2c_device_id *did)
775 struct mt9v022 *mt9v022;
776 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
777 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
778 int ret;
780 if (!icl) {
781 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
782 return -EINVAL;
785 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
786 dev_warn(&adapter->dev,
787 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
788 return -EIO;
791 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
792 if (!mt9v022)
793 return -ENOMEM;
795 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
796 v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
797 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
798 V4L2_CID_VFLIP, 0, 1, 1, 0);
799 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
800 V4L2_CID_HFLIP, 0, 1, 1, 0);
801 mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
802 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
803 mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
804 V4L2_CID_GAIN, 0, 127, 1, 64);
807 * Simulated autoexposure. If enabled, we calculate shutter width
808 * ourselves in the driver based on vertical blanking and frame width
810 mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
811 &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
812 V4L2_EXPOSURE_AUTO);
813 mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
814 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
816 mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
817 if (mt9v022->hdl.error) {
818 int err = mt9v022->hdl.error;
820 kfree(mt9v022);
821 return err;
823 v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
824 V4L2_EXPOSURE_MANUAL, true);
825 v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
827 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
830 * MT9V022 _really_ corrupts the first read out line.
831 * TODO: verify on i.MX31
833 mt9v022->y_skip_top = 1;
834 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
835 mt9v022->rect.top = MT9V022_ROW_SKIP;
836 mt9v022->rect.width = MT9V022_MAX_WIDTH;
837 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
839 ret = mt9v022_video_probe(client);
840 if (ret) {
841 v4l2_ctrl_handler_free(&mt9v022->hdl);
842 kfree(mt9v022);
845 return ret;
848 static int mt9v022_remove(struct i2c_client *client)
850 struct mt9v022 *mt9v022 = to_mt9v022(client);
851 struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
853 v4l2_device_unregister_subdev(&mt9v022->subdev);
854 if (icl->free_bus)
855 icl->free_bus(icl);
856 v4l2_ctrl_handler_free(&mt9v022->hdl);
857 kfree(mt9v022);
859 return 0;
861 static const struct i2c_device_id mt9v022_id[] = {
862 { "mt9v022", 0 },
865 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
867 static struct i2c_driver mt9v022_i2c_driver = {
868 .driver = {
869 .name = "mt9v022",
871 .probe = mt9v022_probe,
872 .remove = mt9v022_remove,
873 .id_table = mt9v022_id,
876 static int __init mt9v022_mod_init(void)
878 return i2c_add_driver(&mt9v022_i2c_driver);
881 static void __exit mt9v022_mod_exit(void)
883 i2c_del_driver(&mt9v022_i2c_driver);
886 module_init(mt9v022_mod_init);
887 module_exit(mt9v022_mod_exit);
889 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
890 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
891 MODULE_LICENSE("GPL");