V4L/DVB (12535): soc-camera: remove .init() and .release() methods from struct soc_ca...
[linux-2.6/mini2440.git] / drivers / media / video / mt9v022.c
blob5c47b55823c80eafb36e48ebc8f2b8afd979cc6a
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>
17 #include <media/v4l2-subdev.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/soc_camera.h>
21 /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
22 * The platform has to define ctruct i2c_board_info objects and link to them
23 * from struct soc_camera_link */
25 static char *sensor_type;
26 module_param(sensor_type, charp, S_IRUGO);
27 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
29 /* mt9v022 selected register addresses */
30 #define MT9V022_CHIP_VERSION 0x00
31 #define MT9V022_COLUMN_START 0x01
32 #define MT9V022_ROW_START 0x02
33 #define MT9V022_WINDOW_HEIGHT 0x03
34 #define MT9V022_WINDOW_WIDTH 0x04
35 #define MT9V022_HORIZONTAL_BLANKING 0x05
36 #define MT9V022_VERTICAL_BLANKING 0x06
37 #define MT9V022_CHIP_CONTROL 0x07
38 #define MT9V022_SHUTTER_WIDTH1 0x08
39 #define MT9V022_SHUTTER_WIDTH2 0x09
40 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
41 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
42 #define MT9V022_RESET 0x0c
43 #define MT9V022_READ_MODE 0x0d
44 #define MT9V022_MONITOR_MODE 0x0e
45 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
46 #define MT9V022_LED_OUT_CONTROL 0x1b
47 #define MT9V022_ADC_MODE_CONTROL 0x1c
48 #define MT9V022_ANALOG_GAIN 0x34
49 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
50 #define MT9V022_PIXCLK_FV_LV 0x74
51 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
52 #define MT9V022_AEC_AGC_ENABLE 0xAF
53 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
55 /* Progressive scan, master, defaults */
56 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
58 #define MT9V022_MAX_WIDTH 752
59 #define MT9V022_MAX_HEIGHT 480
60 #define MT9V022_MIN_WIDTH 48
61 #define MT9V022_MIN_HEIGHT 32
62 #define MT9V022_COLUMN_SKIP 1
63 #define MT9V022_ROW_SKIP 4
65 static const struct soc_camera_data_format mt9v022_colour_formats[] = {
66 /* Order important: first natively supported,
67 * second supported with a GPIO extender */
69 .name = "Bayer (sRGB) 10 bit",
70 .depth = 10,
71 .fourcc = V4L2_PIX_FMT_SBGGR16,
72 .colorspace = V4L2_COLORSPACE_SRGB,
73 }, {
74 .name = "Bayer (sRGB) 8 bit",
75 .depth = 8,
76 .fourcc = V4L2_PIX_FMT_SBGGR8,
77 .colorspace = V4L2_COLORSPACE_SRGB,
81 static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
82 /* Order important - see above */
84 .name = "Monochrome 10 bit",
85 .depth = 10,
86 .fourcc = V4L2_PIX_FMT_Y16,
87 }, {
88 .name = "Monochrome 8 bit",
89 .depth = 8,
90 .fourcc = V4L2_PIX_FMT_GREY,
94 struct mt9v022 {
95 struct v4l2_subdev subdev;
96 struct v4l2_rect rect; /* Sensor window */
97 __u32 fourcc;
98 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
99 u16 chip_control;
102 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
104 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
107 static int reg_read(struct i2c_client *client, const u8 reg)
109 s32 data = i2c_smbus_read_word_data(client, reg);
110 return data < 0 ? data : swab16(data);
113 static int reg_write(struct i2c_client *client, const u8 reg,
114 const u16 data)
116 return i2c_smbus_write_word_data(client, reg, swab16(data));
119 static int reg_set(struct i2c_client *client, const u8 reg,
120 const u16 data)
122 int ret;
124 ret = reg_read(client, reg);
125 if (ret < 0)
126 return ret;
127 return reg_write(client, reg, ret | data);
130 static int reg_clear(struct i2c_client *client, const u8 reg,
131 const u16 data)
133 int ret;
135 ret = reg_read(client, reg);
136 if (ret < 0)
137 return ret;
138 return reg_write(client, reg, ret & ~data);
141 static int mt9v022_init(struct i2c_client *client)
143 struct mt9v022 *mt9v022 = to_mt9v022(client);
144 int ret;
146 /* Almost the default mode: master, parallel, simultaneous, and an
147 * undocumented bit 0x200, which is present in table 7, but not in 8,
148 * plus snapshot mode to disable scan for now */
149 mt9v022->chip_control |= 0x10;
150 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
151 if (!ret)
152 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
154 /* All defaults */
155 if (!ret)
156 /* AEC, AGC on */
157 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
158 if (!ret)
159 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
160 if (!ret)
161 /* default - auto */
162 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
163 if (!ret)
164 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
166 return ret;
169 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
171 struct i2c_client *client = sd->priv;
172 struct mt9v022 *mt9v022 = to_mt9v022(client);
174 if (enable)
175 /* Switch to master "normal" mode */
176 mt9v022->chip_control &= ~0x10;
177 else
178 /* Switch to snapshot mode */
179 mt9v022->chip_control |= 0x10;
181 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
182 return -EIO;
183 return 0;
186 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
187 unsigned long flags)
189 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
190 struct mt9v022 *mt9v022 = to_mt9v022(client);
191 struct soc_camera_link *icl = to_soc_camera_link(icd);
192 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
193 int ret;
194 u16 pixclk = 0;
196 /* Only one width bit may be set */
197 if (!is_power_of_2(width_flag))
198 return -EINVAL;
200 if (icl->set_bus_param) {
201 ret = icl->set_bus_param(icl, width_flag);
202 if (ret)
203 return ret;
204 } else {
206 * Without board specific bus width settings we only support the
207 * sensors native bus width
209 if (width_flag != SOCAM_DATAWIDTH_10)
210 return -EINVAL;
213 flags = soc_camera_apply_sensor_flags(icl, flags);
215 if (flags & SOCAM_PCLK_SAMPLE_RISING)
216 pixclk |= 0x10;
218 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
219 pixclk |= 0x1;
221 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
222 pixclk |= 0x2;
224 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
225 if (ret < 0)
226 return ret;
228 if (!(flags & SOCAM_MASTER))
229 mt9v022->chip_control &= ~0x8;
231 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
232 if (ret < 0)
233 return ret;
235 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
236 pixclk, mt9v022->chip_control);
238 return 0;
241 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
243 struct soc_camera_link *icl = to_soc_camera_link(icd);
244 unsigned int width_flag;
246 if (icl->query_bus_param)
247 width_flag = icl->query_bus_param(icl) &
248 SOCAM_DATAWIDTH_MASK;
249 else
250 width_flag = SOCAM_DATAWIDTH_10;
252 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
253 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
254 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
255 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
256 width_flag;
259 static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
261 struct i2c_client *client = sd->priv;
262 struct mt9v022 *mt9v022 = to_mt9v022(client);
263 struct v4l2_rect rect = a->c;
264 struct soc_camera_device *icd = client->dev.platform_data;
265 int ret;
267 /* Bayer format - even size lengths */
268 if (mt9v022->fourcc == V4L2_PIX_FMT_SBGGR8 ||
269 mt9v022->fourcc == V4L2_PIX_FMT_SBGGR16) {
270 rect.width = ALIGN(rect.width, 2);
271 rect.height = ALIGN(rect.height, 2);
272 /* Let the user play with the starting pixel */
275 soc_camera_limit_side(&rect.left, &rect.width,
276 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
278 soc_camera_limit_side(&rect.top, &rect.height,
279 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
281 /* Like in example app. Contradicts the datasheet though */
282 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
283 if (ret >= 0) {
284 if (ret & 1) /* Autoexposure */
285 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
286 rect.height + icd->y_skip_top + 43);
287 else
288 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
289 rect.height + icd->y_skip_top + 43);
291 /* Setup frame format: defaults apart from width and height */
292 if (!ret)
293 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
294 if (!ret)
295 ret = reg_write(client, MT9V022_ROW_START, rect.top);
296 if (!ret)
297 /* Default 94, Phytec driver says:
298 * "width + horizontal blank >= 660" */
299 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
300 rect.width > 660 - 43 ? 43 :
301 660 - rect.width);
302 if (!ret)
303 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
304 if (!ret)
305 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
306 if (!ret)
307 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
308 rect.height + icd->y_skip_top);
310 if (ret < 0)
311 return ret;
313 dev_dbg(&client->dev, "Frame %ux%u pixel\n", rect.width, rect.height);
315 mt9v022->rect = rect;
317 return 0;
320 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
322 struct i2c_client *client = sd->priv;
323 struct mt9v022 *mt9v022 = to_mt9v022(client);
325 a->c = mt9v022->rect;
326 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
328 return 0;
331 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
333 a->bounds.left = MT9V022_COLUMN_SKIP;
334 a->bounds.top = MT9V022_ROW_SKIP;
335 a->bounds.width = MT9V022_MAX_WIDTH;
336 a->bounds.height = MT9V022_MAX_HEIGHT;
337 a->defrect = a->bounds;
338 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
339 a->pixelaspect.numerator = 1;
340 a->pixelaspect.denominator = 1;
342 return 0;
345 static int mt9v022_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
347 struct i2c_client *client = sd->priv;
348 struct mt9v022 *mt9v022 = to_mt9v022(client);
349 struct v4l2_pix_format *pix = &f->fmt.pix;
351 pix->width = mt9v022->rect.width;
352 pix->height = mt9v022->rect.height;
353 pix->pixelformat = mt9v022->fourcc;
354 pix->field = V4L2_FIELD_NONE;
355 pix->colorspace = V4L2_COLORSPACE_SRGB;
357 return 0;
360 static int mt9v022_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
362 struct i2c_client *client = sd->priv;
363 struct mt9v022 *mt9v022 = to_mt9v022(client);
364 struct v4l2_pix_format *pix = &f->fmt.pix;
365 struct v4l2_crop a = {
366 .c = {
367 .left = mt9v022->rect.left,
368 .top = mt9v022->rect.top,
369 .width = pix->width,
370 .height = pix->height,
373 int ret;
375 /* The caller provides a supported format, as verified per call to
376 * icd->try_fmt(), datawidth is from our supported format list */
377 switch (pix->pixelformat) {
378 case V4L2_PIX_FMT_GREY:
379 case V4L2_PIX_FMT_Y16:
380 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
381 return -EINVAL;
382 break;
383 case V4L2_PIX_FMT_SBGGR8:
384 case V4L2_PIX_FMT_SBGGR16:
385 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
386 return -EINVAL;
387 break;
388 case 0:
389 /* No format change, only geometry */
390 break;
391 default:
392 return -EINVAL;
395 /* No support for scaling on this camera, just crop. */
396 ret = mt9v022_s_crop(sd, &a);
397 if (!ret) {
398 pix->width = mt9v022->rect.width;
399 pix->height = mt9v022->rect.height;
400 mt9v022->fourcc = pix->pixelformat;
403 return ret;
406 static int mt9v022_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
408 struct i2c_client *client = sd->priv;
409 struct soc_camera_device *icd = client->dev.platform_data;
410 struct v4l2_pix_format *pix = &f->fmt.pix;
411 int align = pix->pixelformat == V4L2_PIX_FMT_SBGGR8 ||
412 pix->pixelformat == V4L2_PIX_FMT_SBGGR16;
414 v4l_bound_align_image(&pix->width, MT9V022_MIN_WIDTH,
415 MT9V022_MAX_WIDTH, align,
416 &pix->height, MT9V022_MIN_HEIGHT + icd->y_skip_top,
417 MT9V022_MAX_HEIGHT + icd->y_skip_top, align, 0);
419 return 0;
422 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
423 struct v4l2_dbg_chip_ident *id)
425 struct i2c_client *client = sd->priv;
426 struct mt9v022 *mt9v022 = to_mt9v022(client);
428 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
429 return -EINVAL;
431 if (id->match.addr != client->addr)
432 return -ENODEV;
434 id->ident = mt9v022->model;
435 id->revision = 0;
437 return 0;
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 = sd->priv;
446 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
447 return -EINVAL;
449 if (reg->match.addr != client->addr)
450 return -ENODEV;
452 reg->size = 2;
453 reg->val = reg_read(client, reg->reg);
455 if (reg->val > 0xffff)
456 return -EIO;
458 return 0;
461 static int mt9v022_s_register(struct v4l2_subdev *sd,
462 struct v4l2_dbg_register *reg)
464 struct i2c_client *client = sd->priv;
466 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
467 return -EINVAL;
469 if (reg->match.addr != client->addr)
470 return -ENODEV;
472 if (reg_write(client, reg->reg, reg->val) < 0)
473 return -EIO;
475 return 0;
477 #endif
479 static const struct v4l2_queryctrl mt9v022_controls[] = {
481 .id = V4L2_CID_VFLIP,
482 .type = V4L2_CTRL_TYPE_BOOLEAN,
483 .name = "Flip Vertically",
484 .minimum = 0,
485 .maximum = 1,
486 .step = 1,
487 .default_value = 0,
488 }, {
489 .id = V4L2_CID_HFLIP,
490 .type = V4L2_CTRL_TYPE_BOOLEAN,
491 .name = "Flip Horizontally",
492 .minimum = 0,
493 .maximum = 1,
494 .step = 1,
495 .default_value = 0,
496 }, {
497 .id = V4L2_CID_GAIN,
498 .type = V4L2_CTRL_TYPE_INTEGER,
499 .name = "Analog Gain",
500 .minimum = 64,
501 .maximum = 127,
502 .step = 1,
503 .default_value = 64,
504 .flags = V4L2_CTRL_FLAG_SLIDER,
505 }, {
506 .id = V4L2_CID_EXPOSURE,
507 .type = V4L2_CTRL_TYPE_INTEGER,
508 .name = "Exposure",
509 .minimum = 1,
510 .maximum = 255,
511 .step = 1,
512 .default_value = 255,
513 .flags = V4L2_CTRL_FLAG_SLIDER,
514 }, {
515 .id = V4L2_CID_AUTOGAIN,
516 .type = V4L2_CTRL_TYPE_BOOLEAN,
517 .name = "Automatic Gain",
518 .minimum = 0,
519 .maximum = 1,
520 .step = 1,
521 .default_value = 1,
522 }, {
523 .id = V4L2_CID_EXPOSURE_AUTO,
524 .type = V4L2_CTRL_TYPE_BOOLEAN,
525 .name = "Automatic Exposure",
526 .minimum = 0,
527 .maximum = 1,
528 .step = 1,
529 .default_value = 1,
533 static struct soc_camera_ops mt9v022_ops = {
534 .set_bus_param = mt9v022_set_bus_param,
535 .query_bus_param = mt9v022_query_bus_param,
536 .controls = mt9v022_controls,
537 .num_controls = ARRAY_SIZE(mt9v022_controls),
540 static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
542 struct i2c_client *client = sd->priv;
543 int data;
545 switch (ctrl->id) {
546 case V4L2_CID_VFLIP:
547 data = reg_read(client, MT9V022_READ_MODE);
548 if (data < 0)
549 return -EIO;
550 ctrl->value = !!(data & 0x10);
551 break;
552 case V4L2_CID_HFLIP:
553 data = reg_read(client, MT9V022_READ_MODE);
554 if (data < 0)
555 return -EIO;
556 ctrl->value = !!(data & 0x20);
557 break;
558 case V4L2_CID_EXPOSURE_AUTO:
559 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
560 if (data < 0)
561 return -EIO;
562 ctrl->value = !!(data & 0x1);
563 break;
564 case V4L2_CID_AUTOGAIN:
565 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
566 if (data < 0)
567 return -EIO;
568 ctrl->value = !!(data & 0x2);
569 break;
571 return 0;
574 static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
576 int data;
577 struct i2c_client *client = sd->priv;
578 struct soc_camera_device *icd = client->dev.platform_data;
579 const struct v4l2_queryctrl *qctrl;
581 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
582 if (!qctrl)
583 return -EINVAL;
585 switch (ctrl->id) {
586 case V4L2_CID_VFLIP:
587 if (ctrl->value)
588 data = reg_set(client, MT9V022_READ_MODE, 0x10);
589 else
590 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
591 if (data < 0)
592 return -EIO;
593 break;
594 case V4L2_CID_HFLIP:
595 if (ctrl->value)
596 data = reg_set(client, MT9V022_READ_MODE, 0x20);
597 else
598 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
599 if (data < 0)
600 return -EIO;
601 break;
602 case V4L2_CID_GAIN:
603 /* mt9v022 has minimum == default */
604 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
605 return -EINVAL;
606 else {
607 unsigned long range = qctrl->maximum - qctrl->minimum;
608 /* Datasheet says 16 to 64. autogain only works properly
609 * after setting gain to maximum 14. Larger values
610 * produce "white fly" noise effect. On the whole,
611 * manually setting analog gain does no good. */
612 unsigned long gain = ((ctrl->value - qctrl->minimum) *
613 10 + range / 2) / range + 4;
614 if (gain >= 32)
615 gain &= ~1;
616 /* The user wants to set gain manually, hope, she
617 * knows, what she's doing... Switch AGC off. */
619 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
620 return -EIO;
622 dev_info(&client->dev, "Setting gain from %d to %lu\n",
623 reg_read(client, MT9V022_ANALOG_GAIN), gain);
624 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
625 return -EIO;
626 icd->gain = ctrl->value;
628 break;
629 case V4L2_CID_EXPOSURE:
630 /* mt9v022 has maximum == default */
631 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
632 return -EINVAL;
633 else {
634 unsigned long range = qctrl->maximum - qctrl->minimum;
635 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
636 479 + range / 2) / range + 1;
637 /* The user wants to set shutter width manually, hope,
638 * she knows, what she's doing... Switch AEC off. */
640 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
641 return -EIO;
643 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
644 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
645 shutter);
646 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
647 shutter) < 0)
648 return -EIO;
649 icd->exposure = ctrl->value;
651 break;
652 case V4L2_CID_AUTOGAIN:
653 if (ctrl->value)
654 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
655 else
656 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
657 if (data < 0)
658 return -EIO;
659 break;
660 case V4L2_CID_EXPOSURE_AUTO:
661 if (ctrl->value)
662 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
663 else
664 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
665 if (data < 0)
666 return -EIO;
667 break;
669 return 0;
672 /* Interface active, can use i2c. If it fails, it can indeed mean, that
673 * this wasn't our capture interface, so, we wait for the right one */
674 static int mt9v022_video_probe(struct soc_camera_device *icd,
675 struct i2c_client *client)
677 struct mt9v022 *mt9v022 = to_mt9v022(client);
678 struct soc_camera_link *icl = to_soc_camera_link(icd);
679 s32 data;
680 int ret;
681 unsigned long flags;
683 if (!icd->dev.parent ||
684 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
685 return -ENODEV;
687 /* Read out the chip version register */
688 data = reg_read(client, MT9V022_CHIP_VERSION);
690 /* must be 0x1311 or 0x1313 */
691 if (data != 0x1311 && data != 0x1313) {
692 ret = -ENODEV;
693 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
694 data);
695 goto ei2c;
698 /* Soft reset */
699 ret = reg_write(client, MT9V022_RESET, 1);
700 if (ret < 0)
701 goto ei2c;
702 /* 15 clock cycles */
703 udelay(200);
704 if (reg_read(client, MT9V022_RESET)) {
705 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
706 if (ret > 0)
707 ret = -EIO;
708 goto ei2c;
711 /* Set monochrome or colour sensor type */
712 if (sensor_type && (!strcmp("colour", sensor_type) ||
713 !strcmp("color", sensor_type))) {
714 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
715 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
716 icd->formats = mt9v022_colour_formats;
717 } else {
718 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
719 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
720 icd->formats = mt9v022_monochrome_formats;
723 if (ret < 0)
724 goto ei2c;
726 icd->num_formats = 0;
729 * This is a 10bit sensor, so by default we only allow 10bit.
730 * The platform may support different bus widths due to
731 * different routing of the data lines.
733 if (icl->query_bus_param)
734 flags = icl->query_bus_param(icl);
735 else
736 flags = SOCAM_DATAWIDTH_10;
738 if (flags & SOCAM_DATAWIDTH_10)
739 icd->num_formats++;
740 else
741 icd->formats++;
743 if (flags & SOCAM_DATAWIDTH_8)
744 icd->num_formats++;
746 mt9v022->fourcc = icd->formats->fourcc;
748 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
749 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
750 "monochrome" : "colour");
752 ret = mt9v022_init(client);
753 if (ret < 0)
754 dev_err(&client->dev, "Failed to initialise the camera\n");
756 ei2c:
757 return ret;
760 static void mt9v022_video_remove(struct soc_camera_device *icd)
762 struct soc_camera_link *icl = to_soc_camera_link(icd);
764 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
765 icd->dev.parent, icd->vdev);
766 if (icl->free_bus)
767 icl->free_bus(icl);
770 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
771 .g_ctrl = mt9v022_g_ctrl,
772 .s_ctrl = mt9v022_s_ctrl,
773 .g_chip_ident = mt9v022_g_chip_ident,
774 #ifdef CONFIG_VIDEO_ADV_DEBUG
775 .g_register = mt9v022_g_register,
776 .s_register = mt9v022_s_register,
777 #endif
780 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
781 .s_stream = mt9v022_s_stream,
782 .s_fmt = mt9v022_s_fmt,
783 .g_fmt = mt9v022_g_fmt,
784 .try_fmt = mt9v022_try_fmt,
785 .s_crop = mt9v022_s_crop,
786 .g_crop = mt9v022_g_crop,
787 .cropcap = mt9v022_cropcap,
790 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
791 .core = &mt9v022_subdev_core_ops,
792 .video = &mt9v022_subdev_video_ops,
795 static int mt9v022_probe(struct i2c_client *client,
796 const struct i2c_device_id *did)
798 struct mt9v022 *mt9v022;
799 struct soc_camera_device *icd = client->dev.platform_data;
800 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
801 struct soc_camera_link *icl;
802 int ret;
804 if (!icd) {
805 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
806 return -EINVAL;
809 icl = to_soc_camera_link(icd);
810 if (!icl) {
811 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
812 return -EINVAL;
815 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
816 dev_warn(&adapter->dev,
817 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
818 return -EIO;
821 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
822 if (!mt9v022)
823 return -ENOMEM;
825 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
827 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
829 icd->ops = &mt9v022_ops;
830 icd->y_skip_top = 1;
832 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
833 mt9v022->rect.top = MT9V022_ROW_SKIP;
834 mt9v022->rect.width = MT9V022_MAX_WIDTH;
835 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
837 ret = mt9v022_video_probe(icd, client);
838 if (ret) {
839 icd->ops = NULL;
840 i2c_set_clientdata(client, NULL);
841 kfree(mt9v022);
844 return ret;
847 static int mt9v022_remove(struct i2c_client *client)
849 struct mt9v022 *mt9v022 = to_mt9v022(client);
850 struct soc_camera_device *icd = client->dev.platform_data;
852 icd->ops = NULL;
853 mt9v022_video_remove(icd);
854 i2c_set_clientdata(client, NULL);
855 client->driver = NULL;
856 kfree(mt9v022);
858 return 0;
860 static const struct i2c_device_id mt9v022_id[] = {
861 { "mt9v022", 0 },
864 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
866 static struct i2c_driver mt9v022_i2c_driver = {
867 .driver = {
868 .name = "mt9v022",
870 .probe = mt9v022_probe,
871 .remove = mt9v022_remove,
872 .id_table = mt9v022_id,
875 static int __init mt9v022_mod_init(void)
877 return i2c_add_driver(&mt9v022_i2c_driver);
880 static void __exit mt9v022_mod_exit(void)
882 i2c_del_driver(&mt9v022_i2c_driver);
885 module_init(mt9v022_mod_init);
886 module_exit(mt9v022_mod_exit);
888 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
889 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
890 MODULE_LICENSE("GPL");