V4L/DVB (7336): soc-camera: streamline hardware parameter negotiation
[linux-2.6/kvm.git] / drivers / media / video / mt9v022.c
bloba2f161dcc72d071ed5fcd78a926599d7f1600041
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-common.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/soc_camera.h>
21 #ifdef CONFIG_MT9M001_PCA9536_SWITCH
22 #include <asm/gpio.h>
23 #endif
25 /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
26 * The platform has to define i2c_board_info
27 * and call i2c_register_board_info() */
29 static char *sensor_type;
30 module_param(sensor_type, charp, S_IRUGO);
31 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"\n");
33 /* mt9v022 selected register addresses */
34 #define MT9V022_CHIP_VERSION 0x00
35 #define MT9V022_COLUMN_START 0x01
36 #define MT9V022_ROW_START 0x02
37 #define MT9V022_WINDOW_HEIGHT 0x03
38 #define MT9V022_WINDOW_WIDTH 0x04
39 #define MT9V022_HORIZONTAL_BLANKING 0x05
40 #define MT9V022_VERTICAL_BLANKING 0x06
41 #define MT9V022_CHIP_CONTROL 0x07
42 #define MT9V022_SHUTTER_WIDTH1 0x08
43 #define MT9V022_SHUTTER_WIDTH2 0x09
44 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
45 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
46 #define MT9V022_RESET 0x0c
47 #define MT9V022_READ_MODE 0x0d
48 #define MT9V022_MONITOR_MODE 0x0e
49 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
50 #define MT9V022_LED_OUT_CONTROL 0x1b
51 #define MT9V022_ADC_MODE_CONTROL 0x1c
52 #define MT9V022_ANALOG_GAIN 0x34
53 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
54 #define MT9V022_PIXCLK_FV_LV 0x74
55 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
56 #define MT9V022_AEC_AGC_ENABLE 0xAF
57 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
59 /* Progressive scan, master, defaults */
60 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
62 static const struct soc_camera_data_format mt9v022_colour_formats[] = {
63 /* Order important: first natively supported,
64 * second supported with a GPIO extender */
66 .name = "Bayer (sRGB) 10 bit",
67 .depth = 10,
68 .fourcc = V4L2_PIX_FMT_SBGGR16,
69 .colorspace = V4L2_COLORSPACE_SRGB,
70 }, {
71 .name = "Bayer (sRGB) 8 bit",
72 .depth = 8,
73 .fourcc = V4L2_PIX_FMT_SBGGR8,
74 .colorspace = V4L2_COLORSPACE_SRGB,
78 static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
79 /* Order important - see above */
81 .name = "Monochrome 10 bit",
82 .depth = 10,
83 .fourcc = V4L2_PIX_FMT_Y16,
84 }, {
85 .name = "Monochrome 8 bit",
86 .depth = 8,
87 .fourcc = V4L2_PIX_FMT_GREY,
91 struct mt9v022 {
92 struct i2c_client *client;
93 struct soc_camera_device icd;
94 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
95 int switch_gpio;
96 u16 chip_control;
97 unsigned char datawidth;
100 static int reg_read(struct soc_camera_device *icd, const u8 reg)
102 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
103 struct i2c_client *client = mt9v022->client;
104 s32 data = i2c_smbus_read_word_data(client, reg);
105 return data < 0 ? data : swab16(data);
108 static int reg_write(struct soc_camera_device *icd, const u8 reg,
109 const u16 data)
111 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
112 return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
115 static int reg_set(struct soc_camera_device *icd, const u8 reg,
116 const u16 data)
118 int ret;
120 ret = reg_read(icd, reg);
121 if (ret < 0)
122 return ret;
123 return reg_write(icd, reg, ret | data);
126 static int reg_clear(struct soc_camera_device *icd, const u8 reg,
127 const u16 data)
129 int ret;
131 ret = reg_read(icd, reg);
132 if (ret < 0)
133 return ret;
134 return reg_write(icd, reg, ret & ~data);
137 static int mt9v022_init(struct soc_camera_device *icd)
139 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
140 int ret;
142 /* Almost the default mode: master, parallel, simultaneous, and an
143 * undocumented bit 0x200, which is present in table 7, but not in 8,
144 * plus snapshot mode to disable scan for now */
145 mt9v022->chip_control |= 0x10;
146 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
147 if (ret >= 0)
148 reg_write(icd, MT9V022_READ_MODE, 0x300);
150 /* All defaults */
151 if (ret >= 0)
152 /* AEC, AGC on */
153 ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
154 if (ret >= 0)
155 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
156 if (ret >= 0)
157 /* default - auto */
158 ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
159 if (ret >= 0)
160 ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
162 return ret >= 0 ? 0 : -EIO;
165 static int mt9v022_release(struct soc_camera_device *icd)
167 /* Nothing? */
168 return 0;
171 static int mt9v022_start_capture(struct soc_camera_device *icd)
173 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
174 /* Switch to master "normal" mode */
175 mt9v022->chip_control &= ~0x10;
176 if (reg_write(icd, MT9V022_CHIP_CONTROL,
177 mt9v022->chip_control) < 0)
178 return -EIO;
179 return 0;
182 static int mt9v022_stop_capture(struct soc_camera_device *icd)
184 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
185 /* Switch to snapshot mode */
186 mt9v022->chip_control |= 0x10;
187 if (reg_write(icd, MT9V022_CHIP_CONTROL,
188 mt9v022->chip_control) < 0)
189 return -EIO;
190 return 0;
193 static int bus_switch_request(struct mt9v022 *mt9v022, struct soc_camera_link *icl)
195 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
196 int ret;
197 unsigned int gpio = icl->gpio;
199 if (gpio_is_valid(gpio)) {
200 /* We have a data bus switch. */
201 ret = gpio_request(gpio, "mt9v022");
202 if (ret < 0) {
203 dev_err(&mt9v022->client->dev, "Cannot get GPIO %u\n", gpio);
204 return ret;
207 ret = gpio_direction_output(gpio, 0);
208 if (ret < 0) {
209 dev_err(&mt9v022->client->dev,
210 "Cannot set GPIO %u to output\n", gpio);
211 gpio_free(gpio);
212 return ret;
216 mt9v022->switch_gpio = gpio;
217 #else
218 mt9v022->switch_gpio = -EINVAL;
219 #endif
220 return 0;
223 static void bus_switch_release(struct mt9v022 *mt9v022)
225 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
226 if (gpio_is_valid(mt9v022->switch_gpio))
227 gpio_free(mt9v022->switch_gpio);
228 #endif
231 static int bus_switch_act(struct mt9v022 *mt9v022, int go8bit)
233 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
234 if (!gpio_is_valid(mt9v022->switch_gpio))
235 return -ENODEV;
237 gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit);
238 return 0;
239 #else
240 return -ENODEV;
241 #endif
244 static int bus_switch_possible(struct mt9v022 *mt9v022)
246 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
247 return gpio_is_valid(mt9v022->switch_gpio);
248 #else
249 return 0;
250 #endif
253 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
254 unsigned long flags)
256 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
257 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
258 int ret;
259 u16 pixclk = 0;
261 /* Only one width bit may be set */
262 if (!is_power_of_2(width_flag))
263 return -EINVAL;
265 if ((mt9v022->datawidth != 10 && (width_flag == SOCAM_DATAWIDTH_10)) ||
266 (mt9v022->datawidth != 9 && (width_flag == SOCAM_DATAWIDTH_9)) ||
267 (mt9v022->datawidth != 8 && (width_flag == SOCAM_DATAWIDTH_8))) {
268 /* Well, we actually only can do 10 or 8 bits... */
269 if (width_flag == SOCAM_DATAWIDTH_9)
270 return -EINVAL;
272 ret = bus_switch_act(mt9v022,
273 width_flag == SOCAM_DATAWIDTH_8);
274 if (ret < 0)
275 return ret;
277 mt9v022->datawidth = width_flag == SOCAM_DATAWIDTH_8 ? 8 : 10;
280 if (flags & SOCAM_PCLK_SAMPLE_RISING)
281 pixclk |= 0x10;
283 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
284 pixclk |= 0x1;
286 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
287 pixclk |= 0x2;
289 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
290 if (ret < 0)
291 return ret;
293 if (!(flags & SOCAM_MASTER))
294 mt9v022->chip_control &= ~0x8;
296 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
297 if (ret < 0)
298 return ret;
300 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
301 pixclk, mt9v022->chip_control);
303 return 0;
306 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
308 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
309 unsigned int width_flag = SOCAM_DATAWIDTH_10;
311 if (bus_switch_possible(mt9v022))
312 width_flag |= SOCAM_DATAWIDTH_8;
314 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
315 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
316 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
317 SOCAM_MASTER | SOCAM_SLAVE |
318 width_flag;
321 static int mt9v022_set_fmt_cap(struct soc_camera_device *icd,
322 __u32 pixfmt, struct v4l2_rect *rect)
324 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
325 int ret;
327 /* The caller provides a supported format, as verified per call to
328 * icd->try_fmt_cap(), datawidth is from our supported format list */
329 switch (pixfmt) {
330 case V4L2_PIX_FMT_GREY:
331 case V4L2_PIX_FMT_Y16:
332 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
333 return -EINVAL;
334 break;
335 case V4L2_PIX_FMT_SBGGR8:
336 case V4L2_PIX_FMT_SBGGR16:
337 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
338 return -EINVAL;
339 break;
340 case 0:
341 /* No format change, only geometry */
342 break;
343 default:
344 return -EINVAL;
347 /* Like in example app. Contradicts the datasheet though */
348 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
349 if (ret >= 0) {
350 if (ret & 1) /* Autoexposure */
351 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
352 rect->height + icd->y_skip_top + 43);
353 else
354 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
355 rect->height + icd->y_skip_top + 43);
357 /* Setup frame format: defaults apart from width and height */
358 if (ret >= 0)
359 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
360 if (ret >= 0)
361 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
362 if (ret >= 0)
363 /* Default 94, Phytec driver says:
364 * "width + horizontal blank >= 660" */
365 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
366 rect->width > 660 - 43 ? 43 :
367 660 - rect->width);
368 if (ret >= 0)
369 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
370 if (ret >= 0)
371 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
372 if (ret >= 0)
373 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
374 rect->height + icd->y_skip_top);
376 if (ret < 0)
377 return ret;
379 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
381 return 0;
384 static int mt9v022_try_fmt_cap(struct soc_camera_device *icd,
385 struct v4l2_format *f)
387 if (f->fmt.pix.height < 32 + icd->y_skip_top)
388 f->fmt.pix.height = 32 + icd->y_skip_top;
389 if (f->fmt.pix.height > 480 + icd->y_skip_top)
390 f->fmt.pix.height = 480 + icd->y_skip_top;
391 if (f->fmt.pix.width < 48)
392 f->fmt.pix.width = 48;
393 if (f->fmt.pix.width > 752)
394 f->fmt.pix.width = 752;
395 f->fmt.pix.width &= ~0x03; /* ? */
397 return 0;
400 static int mt9v022_get_chip_id(struct soc_camera_device *icd,
401 struct v4l2_chip_ident *id)
403 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
405 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
406 return -EINVAL;
408 if (id->match_chip != mt9v022->client->addr)
409 return -ENODEV;
411 id->ident = mt9v022->model;
412 id->revision = 0;
414 return 0;
417 #ifdef CONFIG_VIDEO_ADV_DEBUG
418 static int mt9v022_get_register(struct soc_camera_device *icd,
419 struct v4l2_register *reg)
421 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
423 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
424 return -EINVAL;
426 if (reg->match_chip != mt9v022->client->addr)
427 return -ENODEV;
429 reg->val = reg_read(icd, reg->reg);
431 if (reg->val > 0xffff)
432 return -EIO;
434 return 0;
437 static int mt9v022_set_register(struct soc_camera_device *icd,
438 struct v4l2_register *reg)
440 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
442 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
443 return -EINVAL;
445 if (reg->match_chip != mt9v022->client->addr)
446 return -ENODEV;
448 if (reg_write(icd, reg->reg, reg->val) < 0)
449 return -EIO;
451 return 0;
453 #endif
455 const struct v4l2_queryctrl mt9v022_controls[] = {
457 .id = V4L2_CID_VFLIP,
458 .type = V4L2_CTRL_TYPE_BOOLEAN,
459 .name = "Flip Vertically",
460 .minimum = 0,
461 .maximum = 1,
462 .step = 1,
463 .default_value = 0,
464 }, {
465 .id = V4L2_CID_HFLIP,
466 .type = V4L2_CTRL_TYPE_BOOLEAN,
467 .name = "Flip Horizontally",
468 .minimum = 0,
469 .maximum = 1,
470 .step = 1,
471 .default_value = 0,
472 }, {
473 .id = V4L2_CID_GAIN,
474 .type = V4L2_CTRL_TYPE_INTEGER,
475 .name = "Analog Gain",
476 .minimum = 64,
477 .maximum = 127,
478 .step = 1,
479 .default_value = 64,
480 .flags = V4L2_CTRL_FLAG_SLIDER,
481 }, {
482 .id = V4L2_CID_EXPOSURE,
483 .type = V4L2_CTRL_TYPE_INTEGER,
484 .name = "Exposure",
485 .minimum = 1,
486 .maximum = 255,
487 .step = 1,
488 .default_value = 255,
489 .flags = V4L2_CTRL_FLAG_SLIDER,
490 }, {
491 .id = V4L2_CID_AUTOGAIN,
492 .type = V4L2_CTRL_TYPE_BOOLEAN,
493 .name = "Automatic Gain",
494 .minimum = 0,
495 .maximum = 1,
496 .step = 1,
497 .default_value = 1,
498 }, {
499 .id = V4L2_CID_EXPOSURE_AUTO,
500 .type = V4L2_CTRL_TYPE_BOOLEAN,
501 .name = "Automatic Exposure",
502 .minimum = 0,
503 .maximum = 1,
504 .step = 1,
505 .default_value = 1,
509 static int mt9v022_get_control(struct soc_camera_device *icd,
510 struct v4l2_control *ctrl);
511 static int mt9v022_set_control(struct soc_camera_device *icd,
512 struct v4l2_control *ctrl);
514 static struct soc_camera_ops mt9v022_ops = {
515 .owner = THIS_MODULE,
516 .init = mt9v022_init,
517 .release = mt9v022_release,
518 .start_capture = mt9v022_start_capture,
519 .stop_capture = mt9v022_stop_capture,
520 .set_fmt_cap = mt9v022_set_fmt_cap,
521 .try_fmt_cap = mt9v022_try_fmt_cap,
522 .set_bus_param = mt9v022_set_bus_param,
523 .query_bus_param = mt9v022_query_bus_param,
524 .formats = NULL, /* Filled in later depending on the */
525 .num_formats = 0, /* sensor type and data widths */
526 .controls = mt9v022_controls,
527 .num_controls = ARRAY_SIZE(mt9v022_controls),
528 .get_control = mt9v022_get_control,
529 .set_control = mt9v022_set_control,
530 .get_chip_id = mt9v022_get_chip_id,
531 #ifdef CONFIG_VIDEO_ADV_DEBUG
532 .get_register = mt9v022_get_register,
533 .set_register = mt9v022_set_register,
534 #endif
537 static int mt9v022_get_control(struct soc_camera_device *icd,
538 struct v4l2_control *ctrl)
540 int data;
542 switch (ctrl->id) {
543 case V4L2_CID_VFLIP:
544 data = reg_read(icd, MT9V022_READ_MODE);
545 if (data < 0)
546 return -EIO;
547 ctrl->value = !!(data & 0x10);
548 break;
549 case V4L2_CID_HFLIP:
550 data = reg_read(icd, MT9V022_READ_MODE);
551 if (data < 0)
552 return -EIO;
553 ctrl->value = !!(data & 0x20);
554 break;
555 case V4L2_CID_EXPOSURE_AUTO:
556 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
557 if (data < 0)
558 return -EIO;
559 ctrl->value = !!(data & 0x1);
560 break;
561 case V4L2_CID_AUTOGAIN:
562 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
563 if (data < 0)
564 return -EIO;
565 ctrl->value = !!(data & 0x2);
566 break;
568 return 0;
571 static int mt9v022_set_control(struct soc_camera_device *icd,
572 struct v4l2_control *ctrl)
574 int data;
575 const struct v4l2_queryctrl *qctrl;
577 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
579 if (!qctrl)
580 return -EINVAL;
582 switch (ctrl->id) {
583 case V4L2_CID_VFLIP:
584 if (ctrl->value)
585 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
586 else
587 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
588 if (data < 0)
589 return -EIO;
590 break;
591 case V4L2_CID_HFLIP:
592 if (ctrl->value)
593 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
594 else
595 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
596 if (data < 0)
597 return -EIO;
598 break;
599 case V4L2_CID_GAIN:
600 /* mt9v022 has minimum == default */
601 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
602 return -EINVAL;
603 else {
604 unsigned long range = qctrl->maximum - qctrl->minimum;
605 /* Datasheet says 16 to 64. autogain only works properly
606 * after setting gain to maximum 14. Larger values
607 * produce "white fly" noise effect. On the whole,
608 * manually setting analog gain does no good. */
609 unsigned long gain = ((ctrl->value - qctrl->minimum) *
610 10 + range / 2) / range + 4;
611 if (gain >= 32)
612 gain &= ~1;
613 /* The user wants to set gain manually, hope, she
614 * knows, what she's doing... Switch AGC off. */
616 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
617 return -EIO;
619 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
620 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
621 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
622 return -EIO;
623 icd->gain = ctrl->value;
625 break;
626 case V4L2_CID_EXPOSURE:
627 /* mt9v022 has maximum == default */
628 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
629 return -EINVAL;
630 else {
631 unsigned long range = qctrl->maximum - qctrl->minimum;
632 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
633 479 + range / 2) / range + 1;
634 /* The user wants to set shutter width manually, hope,
635 * she knows, what she's doing... Switch AEC off. */
637 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
638 return -EIO;
640 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
641 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
642 shutter);
643 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
644 shutter) < 0)
645 return -EIO;
646 icd->exposure = ctrl->value;
648 break;
649 case V4L2_CID_AUTOGAIN:
650 if (ctrl->value)
651 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
652 else
653 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
654 if (data < 0)
655 return -EIO;
656 break;
657 case V4L2_CID_EXPOSURE_AUTO:
658 if (ctrl->value)
659 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
660 else
661 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
662 if (data < 0)
663 return -EIO;
664 break;
666 return 0;
669 /* Interface active, can use i2c. If it fails, it can indeed mean, that
670 * this wasn't our capture interface, so, we wait for the right one */
671 static int mt9v022_video_probe(struct soc_camera_device *icd)
673 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
674 s32 data;
675 int ret;
677 if (!icd->dev.parent ||
678 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
679 return -ENODEV;
681 /* Read out the chip version register */
682 data = reg_read(icd, MT9V022_CHIP_VERSION);
684 /* must be 0x1311 or 0x1313 */
685 if (data != 0x1311 && data != 0x1313) {
686 ret = -ENODEV;
687 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
688 data);
689 goto ei2c;
692 /* Soft reset */
693 ret = reg_write(icd, MT9V022_RESET, 1);
694 if (ret < 0)
695 goto ei2c;
696 /* 15 clock cycles */
697 udelay(200);
698 if (reg_read(icd, MT9V022_RESET)) {
699 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
700 goto ei2c;
703 /* Set monochrome or colour sensor type */
704 if (sensor_type && (!strcmp("colour", sensor_type) ||
705 !strcmp("color", sensor_type))) {
706 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
707 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
708 mt9v022_ops.formats = mt9v022_colour_formats;
709 if (mt9v022->client->dev.platform_data)
710 mt9v022_ops.num_formats = ARRAY_SIZE(mt9v022_colour_formats);
711 else
712 mt9v022_ops.num_formats = 1;
713 } else {
714 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
715 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
716 mt9v022_ops.formats = mt9v022_monochrome_formats;
717 if (mt9v022->client->dev.platform_data)
718 mt9v022_ops.num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
719 else
720 mt9v022_ops.num_formats = 1;
723 if (ret >= 0)
724 ret = soc_camera_video_start(icd);
725 if (ret < 0)
726 goto eisis;
728 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
729 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
730 "monochrome" : "colour");
732 return 0;
734 eisis:
735 ei2c:
736 return ret;
739 static void mt9v022_video_remove(struct soc_camera_device *icd)
741 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
743 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
744 mt9v022->icd.dev.parent, mt9v022->icd.vdev);
745 soc_camera_video_stop(&mt9v022->icd);
748 static int mt9v022_probe(struct i2c_client *client)
750 struct mt9v022 *mt9v022;
751 struct soc_camera_device *icd;
752 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
753 struct soc_camera_link *icl = client->dev.platform_data;
754 int ret;
756 if (!icl) {
757 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
758 return -EINVAL;
761 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
762 dev_warn(&adapter->dev,
763 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
764 return -EIO;
767 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
768 if (!mt9v022)
769 return -ENOMEM;
771 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
772 mt9v022->client = client;
773 i2c_set_clientdata(client, mt9v022);
775 icd = &mt9v022->icd;
776 icd->probe = mt9v022_video_probe;
777 icd->remove = mt9v022_video_remove;
778 icd->ops = &mt9v022_ops;
779 icd->control = &client->dev;
780 icd->x_min = 1;
781 icd->y_min = 4;
782 icd->x_current = 1;
783 icd->y_current = 4;
784 icd->width_min = 48;
785 icd->width_max = 752;
786 icd->height_min = 32;
787 icd->height_max = 480;
788 icd->y_skip_top = 1;
789 icd->iface = icl->bus_id;
790 /* Default datawidth - this is the only width this camera (normally)
791 * supports. It is only with extra logic that it can support
792 * other widths. Therefore it seems to be a sensible default. */
793 mt9v022->datawidth = 10;
795 ret = bus_switch_request(mt9v022, icl);
796 if (ret)
797 goto eswinit;
799 ret = soc_camera_device_register(icd);
800 if (ret)
801 goto eisdr;
803 return 0;
805 eisdr:
806 bus_switch_release(mt9v022);
807 eswinit:
808 kfree(mt9v022);
809 return ret;
812 static int mt9v022_remove(struct i2c_client *client)
814 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
816 soc_camera_device_unregister(&mt9v022->icd);
817 bus_switch_release(mt9v022);
818 kfree(mt9v022);
820 return 0;
823 static struct i2c_driver mt9v022_i2c_driver = {
824 .driver = {
825 .name = "mt9v022",
827 .probe = mt9v022_probe,
828 .remove = mt9v022_remove,
831 static int __init mt9v022_mod_init(void)
833 return i2c_add_driver(&mt9v022_i2c_driver);
836 static void __exit mt9v022_mod_exit(void)
838 i2c_del_driver(&mt9v022_i2c_driver);
841 module_init(mt9v022_mod_init);
842 module_exit(mt9v022_mod_exit);
844 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
845 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
846 MODULE_LICENSE("GPL");