PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / media / video / mt9v022.c
blob1658fe5903928d076612e888ceb0be2d15641f0a
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/gpio.h>
18 #include <media/v4l2-common.h>
19 #include <media/v4l2-chip-ident.h>
20 #include <media/soc_camera.h>
22 /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
23 * The platform has to define i2c_board_info
24 * and call i2c_register_board_info() */
26 static char *sensor_type;
27 module_param(sensor_type, charp, S_IRUGO);
28 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"\n");
30 /* mt9v022 selected register addresses */
31 #define MT9V022_CHIP_VERSION 0x00
32 #define MT9V022_COLUMN_START 0x01
33 #define MT9V022_ROW_START 0x02
34 #define MT9V022_WINDOW_HEIGHT 0x03
35 #define MT9V022_WINDOW_WIDTH 0x04
36 #define MT9V022_HORIZONTAL_BLANKING 0x05
37 #define MT9V022_VERTICAL_BLANKING 0x06
38 #define MT9V022_CHIP_CONTROL 0x07
39 #define MT9V022_SHUTTER_WIDTH1 0x08
40 #define MT9V022_SHUTTER_WIDTH2 0x09
41 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
42 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
43 #define MT9V022_RESET 0x0c
44 #define MT9V022_READ_MODE 0x0d
45 #define MT9V022_MONITOR_MODE 0x0e
46 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
47 #define MT9V022_LED_OUT_CONTROL 0x1b
48 #define MT9V022_ADC_MODE_CONTROL 0x1c
49 #define MT9V022_ANALOG_GAIN 0x34
50 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
51 #define MT9V022_PIXCLK_FV_LV 0x74
52 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
53 #define MT9V022_AEC_AGC_ENABLE 0xAF
54 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
56 /* Progressive scan, master, defaults */
57 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
59 static const struct soc_camera_data_format mt9v022_colour_formats[] = {
60 /* Order important: first natively supported,
61 * second supported with a GPIO extender */
63 .name = "Bayer (sRGB) 10 bit",
64 .depth = 10,
65 .fourcc = V4L2_PIX_FMT_SBGGR16,
66 .colorspace = V4L2_COLORSPACE_SRGB,
67 }, {
68 .name = "Bayer (sRGB) 8 bit",
69 .depth = 8,
70 .fourcc = V4L2_PIX_FMT_SBGGR8,
71 .colorspace = V4L2_COLORSPACE_SRGB,
75 static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
76 /* Order important - see above */
78 .name = "Monochrome 10 bit",
79 .depth = 10,
80 .fourcc = V4L2_PIX_FMT_Y16,
81 }, {
82 .name = "Monochrome 8 bit",
83 .depth = 8,
84 .fourcc = V4L2_PIX_FMT_GREY,
88 struct mt9v022 {
89 struct i2c_client *client;
90 struct soc_camera_device icd;
91 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
92 int switch_gpio;
93 u16 chip_control;
94 unsigned char datawidth;
97 static int reg_read(struct soc_camera_device *icd, const u8 reg)
99 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
100 struct i2c_client *client = mt9v022->client;
101 s32 data = i2c_smbus_read_word_data(client, reg);
102 return data < 0 ? data : swab16(data);
105 static int reg_write(struct soc_camera_device *icd, const u8 reg,
106 const u16 data)
108 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
109 return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
112 static int reg_set(struct soc_camera_device *icd, const u8 reg,
113 const u16 data)
115 int ret;
117 ret = reg_read(icd, reg);
118 if (ret < 0)
119 return ret;
120 return reg_write(icd, reg, ret | data);
123 static int reg_clear(struct soc_camera_device *icd, const u8 reg,
124 const u16 data)
126 int ret;
128 ret = reg_read(icd, reg);
129 if (ret < 0)
130 return ret;
131 return reg_write(icd, reg, ret & ~data);
134 static int mt9v022_init(struct soc_camera_device *icd)
136 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
137 int ret;
139 /* Almost the default mode: master, parallel, simultaneous, and an
140 * undocumented bit 0x200, which is present in table 7, but not in 8,
141 * plus snapshot mode to disable scan for now */
142 mt9v022->chip_control |= 0x10;
143 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
144 if (ret >= 0)
145 reg_write(icd, MT9V022_READ_MODE, 0x300);
147 /* All defaults */
148 if (ret >= 0)
149 /* AEC, AGC on */
150 ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
151 if (ret >= 0)
152 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
153 if (ret >= 0)
154 /* default - auto */
155 ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
156 if (ret >= 0)
157 ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
159 return ret >= 0 ? 0 : -EIO;
162 static int mt9v022_release(struct soc_camera_device *icd)
164 /* Nothing? */
165 return 0;
168 static int mt9v022_start_capture(struct soc_camera_device *icd)
170 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
171 /* Switch to master "normal" mode */
172 mt9v022->chip_control &= ~0x10;
173 if (reg_write(icd, MT9V022_CHIP_CONTROL,
174 mt9v022->chip_control) < 0)
175 return -EIO;
176 return 0;
179 static int mt9v022_stop_capture(struct soc_camera_device *icd)
181 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
182 /* Switch to snapshot mode */
183 mt9v022->chip_control |= 0x10;
184 if (reg_write(icd, MT9V022_CHIP_CONTROL,
185 mt9v022->chip_control) < 0)
186 return -EIO;
187 return 0;
190 static int bus_switch_request(struct mt9v022 *mt9v022, struct soc_camera_link *icl)
192 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
193 int ret;
194 unsigned int gpio = icl->gpio;
196 if (gpio_is_valid(gpio)) {
197 /* We have a data bus switch. */
198 ret = gpio_request(gpio, "mt9v022");
199 if (ret < 0) {
200 dev_err(&mt9v022->client->dev, "Cannot get GPIO %u\n", gpio);
201 return ret;
204 ret = gpio_direction_output(gpio, 0);
205 if (ret < 0) {
206 dev_err(&mt9v022->client->dev,
207 "Cannot set GPIO %u to output\n", gpio);
208 gpio_free(gpio);
209 return ret;
213 mt9v022->switch_gpio = gpio;
214 #else
215 mt9v022->switch_gpio = -EINVAL;
216 #endif
217 return 0;
220 static void bus_switch_release(struct mt9v022 *mt9v022)
222 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
223 if (gpio_is_valid(mt9v022->switch_gpio))
224 gpio_free(mt9v022->switch_gpio);
225 #endif
228 static int bus_switch_act(struct mt9v022 *mt9v022, int go8bit)
230 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
231 if (!gpio_is_valid(mt9v022->switch_gpio))
232 return -ENODEV;
234 gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit);
235 return 0;
236 #else
237 return -ENODEV;
238 #endif
241 static int bus_switch_possible(struct mt9v022 *mt9v022)
243 #ifdef CONFIG_MT9V022_PCA9536_SWITCH
244 return gpio_is_valid(mt9v022->switch_gpio);
245 #else
246 return 0;
247 #endif
250 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
251 unsigned long flags)
253 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
254 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
255 int ret;
256 u16 pixclk = 0;
258 /* Only one width bit may be set */
259 if (!is_power_of_2(width_flag))
260 return -EINVAL;
262 if ((mt9v022->datawidth != 10 && (width_flag == SOCAM_DATAWIDTH_10)) ||
263 (mt9v022->datawidth != 9 && (width_flag == SOCAM_DATAWIDTH_9)) ||
264 (mt9v022->datawidth != 8 && (width_flag == SOCAM_DATAWIDTH_8))) {
265 /* Well, we actually only can do 10 or 8 bits... */
266 if (width_flag == SOCAM_DATAWIDTH_9)
267 return -EINVAL;
269 ret = bus_switch_act(mt9v022,
270 width_flag == SOCAM_DATAWIDTH_8);
271 if (ret < 0)
272 return ret;
274 mt9v022->datawidth = width_flag == SOCAM_DATAWIDTH_8 ? 8 : 10;
277 if (flags & SOCAM_PCLK_SAMPLE_RISING)
278 pixclk |= 0x10;
280 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
281 pixclk |= 0x1;
283 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
284 pixclk |= 0x2;
286 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
287 if (ret < 0)
288 return ret;
290 if (!(flags & SOCAM_MASTER))
291 mt9v022->chip_control &= ~0x8;
293 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
294 if (ret < 0)
295 return ret;
297 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
298 pixclk, mt9v022->chip_control);
300 return 0;
303 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
305 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
306 unsigned int width_flag = SOCAM_DATAWIDTH_10;
308 if (bus_switch_possible(mt9v022))
309 width_flag |= SOCAM_DATAWIDTH_8;
311 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
312 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
313 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
314 SOCAM_MASTER | SOCAM_SLAVE |
315 width_flag;
318 static int mt9v022_set_fmt_cap(struct soc_camera_device *icd,
319 __u32 pixfmt, struct v4l2_rect *rect)
321 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
322 int ret;
324 /* The caller provides a supported format, as verified per call to
325 * icd->try_fmt_cap(), datawidth is from our supported format list */
326 switch (pixfmt) {
327 case V4L2_PIX_FMT_GREY:
328 case V4L2_PIX_FMT_Y16:
329 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
330 return -EINVAL;
331 break;
332 case V4L2_PIX_FMT_SBGGR8:
333 case V4L2_PIX_FMT_SBGGR16:
334 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
335 return -EINVAL;
336 break;
337 case 0:
338 /* No format change, only geometry */
339 break;
340 default:
341 return -EINVAL;
344 /* Like in example app. Contradicts the datasheet though */
345 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
346 if (ret >= 0) {
347 if (ret & 1) /* Autoexposure */
348 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
349 rect->height + icd->y_skip_top + 43);
350 else
351 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
352 rect->height + icd->y_skip_top + 43);
354 /* Setup frame format: defaults apart from width and height */
355 if (ret >= 0)
356 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
357 if (ret >= 0)
358 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
359 if (ret >= 0)
360 /* Default 94, Phytec driver says:
361 * "width + horizontal blank >= 660" */
362 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
363 rect->width > 660 - 43 ? 43 :
364 660 - rect->width);
365 if (ret >= 0)
366 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
367 if (ret >= 0)
368 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
369 if (ret >= 0)
370 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
371 rect->height + icd->y_skip_top);
373 if (ret < 0)
374 return ret;
376 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
378 return 0;
381 static int mt9v022_try_fmt_cap(struct soc_camera_device *icd,
382 struct v4l2_format *f)
384 if (f->fmt.pix.height < 32 + icd->y_skip_top)
385 f->fmt.pix.height = 32 + icd->y_skip_top;
386 if (f->fmt.pix.height > 480 + icd->y_skip_top)
387 f->fmt.pix.height = 480 + icd->y_skip_top;
388 if (f->fmt.pix.width < 48)
389 f->fmt.pix.width = 48;
390 if (f->fmt.pix.width > 752)
391 f->fmt.pix.width = 752;
392 f->fmt.pix.width &= ~0x03; /* ? */
394 return 0;
397 static int mt9v022_get_chip_id(struct soc_camera_device *icd,
398 struct v4l2_chip_ident *id)
400 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
402 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
403 return -EINVAL;
405 if (id->match_chip != mt9v022->client->addr)
406 return -ENODEV;
408 id->ident = mt9v022->model;
409 id->revision = 0;
411 return 0;
414 #ifdef CONFIG_VIDEO_ADV_DEBUG
415 static int mt9v022_get_register(struct soc_camera_device *icd,
416 struct v4l2_register *reg)
418 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
420 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
421 return -EINVAL;
423 if (reg->match_chip != mt9v022->client->addr)
424 return -ENODEV;
426 reg->val = reg_read(icd, reg->reg);
428 if (reg->val > 0xffff)
429 return -EIO;
431 return 0;
434 static int mt9v022_set_register(struct soc_camera_device *icd,
435 struct v4l2_register *reg)
437 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
439 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
440 return -EINVAL;
442 if (reg->match_chip != mt9v022->client->addr)
443 return -ENODEV;
445 if (reg_write(icd, reg->reg, reg->val) < 0)
446 return -EIO;
448 return 0;
450 #endif
452 static const struct v4l2_queryctrl mt9v022_controls[] = {
454 .id = V4L2_CID_VFLIP,
455 .type = V4L2_CTRL_TYPE_BOOLEAN,
456 .name = "Flip Vertically",
457 .minimum = 0,
458 .maximum = 1,
459 .step = 1,
460 .default_value = 0,
461 }, {
462 .id = V4L2_CID_HFLIP,
463 .type = V4L2_CTRL_TYPE_BOOLEAN,
464 .name = "Flip Horizontally",
465 .minimum = 0,
466 .maximum = 1,
467 .step = 1,
468 .default_value = 0,
469 }, {
470 .id = V4L2_CID_GAIN,
471 .type = V4L2_CTRL_TYPE_INTEGER,
472 .name = "Analog Gain",
473 .minimum = 64,
474 .maximum = 127,
475 .step = 1,
476 .default_value = 64,
477 .flags = V4L2_CTRL_FLAG_SLIDER,
478 }, {
479 .id = V4L2_CID_EXPOSURE,
480 .type = V4L2_CTRL_TYPE_INTEGER,
481 .name = "Exposure",
482 .minimum = 1,
483 .maximum = 255,
484 .step = 1,
485 .default_value = 255,
486 .flags = V4L2_CTRL_FLAG_SLIDER,
487 }, {
488 .id = V4L2_CID_AUTOGAIN,
489 .type = V4L2_CTRL_TYPE_BOOLEAN,
490 .name = "Automatic Gain",
491 .minimum = 0,
492 .maximum = 1,
493 .step = 1,
494 .default_value = 1,
495 }, {
496 .id = V4L2_CID_EXPOSURE_AUTO,
497 .type = V4L2_CTRL_TYPE_BOOLEAN,
498 .name = "Automatic Exposure",
499 .minimum = 0,
500 .maximum = 1,
501 .step = 1,
502 .default_value = 1,
506 static int mt9v022_video_probe(struct soc_camera_device *);
507 static void mt9v022_video_remove(struct soc_camera_device *);
508 static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
509 static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
511 static struct soc_camera_ops mt9v022_ops = {
512 .owner = THIS_MODULE,
513 .probe = mt9v022_video_probe,
514 .remove = mt9v022_video_remove,
515 .init = mt9v022_init,
516 .release = mt9v022_release,
517 .start_capture = mt9v022_start_capture,
518 .stop_capture = mt9v022_stop_capture,
519 .set_fmt_cap = mt9v022_set_fmt_cap,
520 .try_fmt_cap = mt9v022_try_fmt_cap,
521 .set_bus_param = mt9v022_set_bus_param,
522 .query_bus_param = mt9v022_query_bus_param,
523 .controls = mt9v022_controls,
524 .num_controls = ARRAY_SIZE(mt9v022_controls),
525 .get_control = mt9v022_get_control,
526 .set_control = mt9v022_set_control,
527 .get_chip_id = mt9v022_get_chip_id,
528 #ifdef CONFIG_VIDEO_ADV_DEBUG
529 .get_register = mt9v022_get_register,
530 .set_register = mt9v022_set_register,
531 #endif
534 static int mt9v022_get_control(struct soc_camera_device *icd,
535 struct v4l2_control *ctrl)
537 int data;
539 switch (ctrl->id) {
540 case V4L2_CID_VFLIP:
541 data = reg_read(icd, MT9V022_READ_MODE);
542 if (data < 0)
543 return -EIO;
544 ctrl->value = !!(data & 0x10);
545 break;
546 case V4L2_CID_HFLIP:
547 data = reg_read(icd, MT9V022_READ_MODE);
548 if (data < 0)
549 return -EIO;
550 ctrl->value = !!(data & 0x20);
551 break;
552 case V4L2_CID_EXPOSURE_AUTO:
553 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
554 if (data < 0)
555 return -EIO;
556 ctrl->value = !!(data & 0x1);
557 break;
558 case V4L2_CID_AUTOGAIN:
559 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
560 if (data < 0)
561 return -EIO;
562 ctrl->value = !!(data & 0x2);
563 break;
565 return 0;
568 static int mt9v022_set_control(struct soc_camera_device *icd,
569 struct v4l2_control *ctrl)
571 int data;
572 const struct v4l2_queryctrl *qctrl;
574 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
576 if (!qctrl)
577 return -EINVAL;
579 switch (ctrl->id) {
580 case V4L2_CID_VFLIP:
581 if (ctrl->value)
582 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
583 else
584 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
585 if (data < 0)
586 return -EIO;
587 break;
588 case V4L2_CID_HFLIP:
589 if (ctrl->value)
590 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
591 else
592 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
593 if (data < 0)
594 return -EIO;
595 break;
596 case V4L2_CID_GAIN:
597 /* mt9v022 has minimum == default */
598 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
599 return -EINVAL;
600 else {
601 unsigned long range = qctrl->maximum - qctrl->minimum;
602 /* Datasheet says 16 to 64. autogain only works properly
603 * after setting gain to maximum 14. Larger values
604 * produce "white fly" noise effect. On the whole,
605 * manually setting analog gain does no good. */
606 unsigned long gain = ((ctrl->value - qctrl->minimum) *
607 10 + range / 2) / range + 4;
608 if (gain >= 32)
609 gain &= ~1;
610 /* The user wants to set gain manually, hope, she
611 * knows, what she's doing... Switch AGC off. */
613 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
614 return -EIO;
616 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
617 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
618 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
619 return -EIO;
620 icd->gain = ctrl->value;
622 break;
623 case V4L2_CID_EXPOSURE:
624 /* mt9v022 has maximum == default */
625 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
626 return -EINVAL;
627 else {
628 unsigned long range = qctrl->maximum - qctrl->minimum;
629 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
630 479 + range / 2) / range + 1;
631 /* The user wants to set shutter width manually, hope,
632 * she knows, what she's doing... Switch AEC off. */
634 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
635 return -EIO;
637 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
638 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
639 shutter);
640 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
641 shutter) < 0)
642 return -EIO;
643 icd->exposure = ctrl->value;
645 break;
646 case V4L2_CID_AUTOGAIN:
647 if (ctrl->value)
648 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
649 else
650 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
651 if (data < 0)
652 return -EIO;
653 break;
654 case V4L2_CID_EXPOSURE_AUTO:
655 if (ctrl->value)
656 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
657 else
658 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
659 if (data < 0)
660 return -EIO;
661 break;
663 return 0;
666 /* Interface active, can use i2c. If it fails, it can indeed mean, that
667 * this wasn't our capture interface, so, we wait for the right one */
668 static int mt9v022_video_probe(struct soc_camera_device *icd)
670 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
671 s32 data;
672 int ret;
674 if (!icd->dev.parent ||
675 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
676 return -ENODEV;
678 /* Read out the chip version register */
679 data = reg_read(icd, MT9V022_CHIP_VERSION);
681 /* must be 0x1311 or 0x1313 */
682 if (data != 0x1311 && data != 0x1313) {
683 ret = -ENODEV;
684 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
685 data);
686 goto ei2c;
689 /* Soft reset */
690 ret = reg_write(icd, MT9V022_RESET, 1);
691 if (ret < 0)
692 goto ei2c;
693 /* 15 clock cycles */
694 udelay(200);
695 if (reg_read(icd, MT9V022_RESET)) {
696 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
697 goto ei2c;
700 /* Set monochrome or colour sensor type */
701 if (sensor_type && (!strcmp("colour", sensor_type) ||
702 !strcmp("color", sensor_type))) {
703 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
704 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
705 icd->formats = mt9v022_colour_formats;
706 if (mt9v022->client->dev.platform_data)
707 icd->num_formats = ARRAY_SIZE(mt9v022_colour_formats);
708 else
709 icd->num_formats = 1;
710 } else {
711 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
712 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
713 icd->formats = mt9v022_monochrome_formats;
714 if (mt9v022->client->dev.platform_data)
715 icd->num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
716 else
717 icd->num_formats = 1;
720 if (ret >= 0)
721 ret = soc_camera_video_start(icd);
722 if (ret < 0)
723 goto eisis;
725 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
726 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
727 "monochrome" : "colour");
729 return 0;
731 eisis:
732 ei2c:
733 return ret;
736 static void mt9v022_video_remove(struct soc_camera_device *icd)
738 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
740 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
741 mt9v022->icd.dev.parent, mt9v022->icd.vdev);
742 soc_camera_video_stop(&mt9v022->icd);
745 static int mt9v022_probe(struct i2c_client *client,
746 const struct i2c_device_id *did)
748 struct mt9v022 *mt9v022;
749 struct soc_camera_device *icd;
750 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
751 struct soc_camera_link *icl = client->dev.platform_data;
752 int ret;
754 if (!icl) {
755 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
756 return -EINVAL;
759 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
760 dev_warn(&adapter->dev,
761 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
762 return -EIO;
765 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
766 if (!mt9v022)
767 return -ENOMEM;
769 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
770 mt9v022->client = client;
771 i2c_set_clientdata(client, mt9v022);
773 icd = &mt9v022->icd;
774 icd->ops = &mt9v022_ops;
775 icd->control = &client->dev;
776 icd->x_min = 1;
777 icd->y_min = 4;
778 icd->x_current = 1;
779 icd->y_current = 4;
780 icd->width_min = 48;
781 icd->width_max = 752;
782 icd->height_min = 32;
783 icd->height_max = 480;
784 icd->y_skip_top = 1;
785 icd->iface = icl->bus_id;
786 /* Default datawidth - this is the only width this camera (normally)
787 * supports. It is only with extra logic that it can support
788 * other widths. Therefore it seems to be a sensible default. */
789 mt9v022->datawidth = 10;
791 ret = bus_switch_request(mt9v022, icl);
792 if (ret)
793 goto eswinit;
795 ret = soc_camera_device_register(icd);
796 if (ret)
797 goto eisdr;
799 return 0;
801 eisdr:
802 bus_switch_release(mt9v022);
803 eswinit:
804 kfree(mt9v022);
805 return ret;
808 static int mt9v022_remove(struct i2c_client *client)
810 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
812 soc_camera_device_unregister(&mt9v022->icd);
813 bus_switch_release(mt9v022);
814 kfree(mt9v022);
816 return 0;
819 static const struct i2c_device_id mt9v022_id[] = {
820 { "mt9v022", 0 },
823 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
825 static struct i2c_driver mt9v022_i2c_driver = {
826 .driver = {
827 .name = "mt9v022",
829 .probe = mt9v022_probe,
830 .remove = mt9v022_remove,
831 .id_table = mt9v022_id,
834 static int __init mt9v022_mod_init(void)
836 return i2c_add_driver(&mt9v022_i2c_driver);
839 static void __exit mt9v022_mod_exit(void)
841 i2c_del_driver(&mt9v022_i2c_driver);
844 module_init(mt9v022_mod_init);
845 module_exit(mt9v022_mod_exit);
847 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
848 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
849 MODULE_LICENSE("GPL");