aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/mt9v022.c
diff options
context:
space:
mode:
authorGuennadi Liakhovetski <g.liakhovetski@pengutronix.de>2008-04-22 13:42:04 -0400
committerMauro Carvalho Chehab <mchehab@infradead.org>2008-04-24 13:07:43 -0400
commit7397bfbe10b52a47f1f6c69aa87192d97ffa1910 (patch)
treed926ac868030038c03c1a449cd6fb150dcbcd424 /drivers/media/video/mt9v022.c
parentf523dd0da985ef618d6f986217f0dd0975072515 (diff)
V4L/DVB (7174): Add support for the MT9V022 camera
This driver supports Micron MT9V022 colour camera. Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@pengutronix.de> Signed-off-by: Mauro Carvalho Chehab <mchehab@infradead.org>
Diffstat (limited to 'drivers/media/video/mt9v022.c')
-rw-r--r--drivers/media/video/mt9v022.c804
1 files changed, 804 insertions, 0 deletions
diff --git a/drivers/media/video/mt9v022.c b/drivers/media/video/mt9v022.c
new file mode 100644
index 000000000000..9b406e41902a
--- /dev/null
+++ b/drivers/media/video/mt9v022.c
@@ -0,0 +1,804 @@
1/*
2 * Driver for MT9V022 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
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 */
10
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
17#include <media/v4l2-common.h>
18#include <media/v4l2-chip-ident.h>
19#include <media/soc_camera.h>
20
21#include <asm/gpio.h>
22
23/* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
24 * The platform has to define i2c_board_info
25 * and call i2c_register_board_info() */
26
27static char *sensor_type;
28module_param(sensor_type, charp, S_IRUGO);
29MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"\n");
30
31/* mt9v022 selected register addresses */
32#define MT9V022_CHIP_VERSION 0x00
33#define MT9V022_COLUMN_START 0x01
34#define MT9V022_ROW_START 0x02
35#define MT9V022_WINDOW_HEIGHT 0x03
36#define MT9V022_WINDOW_WIDTH 0x04
37#define MT9V022_HORIZONTAL_BLANKING 0x05
38#define MT9V022_VERTICAL_BLANKING 0x06
39#define MT9V022_CHIP_CONTROL 0x07
40#define MT9V022_SHUTTER_WIDTH1 0x08
41#define MT9V022_SHUTTER_WIDTH2 0x09
42#define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
43#define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
44#define MT9V022_RESET 0x0c
45#define MT9V022_READ_MODE 0x0d
46#define MT9V022_MONITOR_MODE 0x0e
47#define MT9V022_PIXEL_OPERATION_MODE 0x0f
48#define MT9V022_LED_OUT_CONTROL 0x1b
49#define MT9V022_ADC_MODE_CONTROL 0x1c
50#define MT9V022_ANALOG_GAIN 0x34
51#define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
52#define MT9V022_PIXCLK_FV_LV 0x74
53#define MT9V022_DIGITAL_TEST_PATTERN 0x7f
54#define MT9V022_AEC_AGC_ENABLE 0xAF
55#define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
56
57/* Progressive scan, master, defaults */
58#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
59
60static const struct soc_camera_data_format mt9v022_formats[] = {
61 {
62 .name = "RGB Bayer (sRGB)",
63 .depth = 8,
64 .fourcc = V4L2_PIX_FMT_SBGGR8,
65 .colorspace = V4L2_COLORSPACE_SRGB,
66 }, {
67 .name = "RGB Bayer (sRGB)",
68 .depth = 10,
69 .fourcc = V4L2_PIX_FMT_SBGGR16,
70 .colorspace = V4L2_COLORSPACE_SRGB,
71 }, {
72 .name = "Monochrome 10 bit",
73 .depth = 10,
74 .fourcc = V4L2_PIX_FMT_Y16,
75 }, {
76 .name = "Monochrome 8 bit",
77 .depth = 8,
78 .fourcc = V4L2_PIX_FMT_GREY,
79 },
80};
81
82struct mt9v022 {
83 struct i2c_client *client;
84 struct soc_camera_device icd;
85 int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
86 int switch_gpio;
87 u16 chip_control;
88 unsigned char datawidth;
89};
90
91static int reg_read(struct soc_camera_device *icd, const u8 reg)
92{
93 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
94 struct i2c_client *client = mt9v022->client;
95 s32 data = i2c_smbus_read_word_data(client, reg);
96 return data < 0 ? data : swab16(data);
97}
98
99static int reg_write(struct soc_camera_device *icd, const u8 reg,
100 const u16 data)
101{
102 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
103 return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
104}
105
106static int reg_set(struct soc_camera_device *icd, const u8 reg,
107 const u16 data)
108{
109 int ret;
110
111 ret = reg_read(icd, reg);
112 if (ret < 0)
113 return ret;
114 return reg_write(icd, reg, ret | data);
115}
116
117static int reg_clear(struct soc_camera_device *icd, const u8 reg,
118 const u16 data)
119{
120 int ret;
121
122 ret = reg_read(icd, reg);
123 if (ret < 0)
124 return ret;
125 return reg_write(icd, reg, ret & ~data);
126}
127
128static int mt9v022_init(struct soc_camera_device *icd)
129{
130 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
131 int ret;
132
133 /* Almost the default mode: master, parallel, simultaneous, and an
134 * undocumented bit 0x200, which is present in table 7, but not in 8,
135 * plus snapshot mode to disable scan for now */
136 mt9v022->chip_control |= 0x10;
137 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
138 if (ret >= 0)
139 reg_write(icd, MT9V022_READ_MODE, 0x300);
140
141 /* All defaults */
142 if (ret >= 0)
143 /* AEC, AGC on */
144 ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
145 if (ret >= 0)
146 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
147 if (ret >= 0)
148 /* default - auto */
149 ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
150 if (ret >= 0)
151 ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
152
153 return ret >= 0 ? 0 : -EIO;
154}
155
156static int mt9v022_release(struct soc_camera_device *icd)
157{
158 /* Nothing? */
159 return 0;
160}
161
162static int mt9v022_start_capture(struct soc_camera_device *icd)
163{
164 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
165 /* Switch to master "normal" mode */
166 mt9v022->chip_control &= ~0x10;
167 if (reg_write(icd, MT9V022_CHIP_CONTROL,
168 mt9v022->chip_control) < 0)
169 return -EIO;
170 return 0;
171}
172
173static int mt9v022_stop_capture(struct soc_camera_device *icd)
174{
175 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
176 /* Switch to snapshot mode */
177 mt9v022->chip_control |= 0x10;
178 if (reg_write(icd, MT9V022_CHIP_CONTROL,
179 mt9v022->chip_control) < 0)
180 return -EIO;
181 return 0;
182}
183
184static int bus_switch_request(struct mt9v022 *mt9v022, struct soc_camera_link *icl)
185{
186#ifdef CONFIG_MT9V022_PCA9536_SWITCH
187 int ret;
188 unsigned int gpio = icl->gpio;
189
190 if (gpio != NO_GPIO) {
191 /* We have a data bus switch. */
192 ret = gpio_request(gpio, "mt9v022");
193 if (ret < 0) {
194 dev_err(&mt9v022->client->dev, "Cannot get GPIO %u\n", gpio);
195 return ret;
196 }
197
198 ret = gpio_direction_output(gpio, 0);
199 if (ret < 0) {
200 dev_err(&mt9v022->client->dev,
201 "Cannot set GPIO %u to output\n", gpio);
202 gpio_free(gpio);
203 return ret;
204 }
205 }
206
207 mt9v022->switch_gpio = gpio;
208#else
209 mt9v022->switch_gpio = NO_GPIO;
210#endif
211 return 0;
212}
213
214static void bus_switch_release(struct mt9v022 *mt9v022)
215{
216#ifdef CONFIG_MT9V022_PCA9536_SWITCH
217 if (mt9v022->switch_gpio != NO_GPIO)
218 gpio_free(mt9v022->switch_gpio);
219#endif
220}
221
222static int bus_switch_act(struct mt9v022 *mt9v022, int go8bit)
223{
224#ifdef CONFIG_MT9V022_PCA9536_SWITCH
225 if (mt9v022->switch_gpio == NO_GPIO)
226 return -ENODEV;
227
228 gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit);
229 return 0;
230#else
231 return -ENODEV;
232#endif
233}
234
235static int mt9v022_set_capture_format(struct soc_camera_device *icd,
236 __u32 pixfmt, struct v4l2_rect *rect, unsigned int flags)
237{
238 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
239 unsigned int width_flag = flags & (IS_DATAWIDTH_10 | IS_DATAWIDTH_9 |
240 IS_DATAWIDTH_8);
241 u16 pixclk = 0;
242 int ret;
243
244 /* Only one width bit may be set */
245 if (!is_power_of_2(width_flag))
246 return -EINVAL;
247
248 /* The caller provides a supported format, as verified per call to
249 * icd->try_fmt_cap(), datawidth is from our supported format list */
250 switch (pixfmt) {
251 case V4L2_PIX_FMT_GREY:
252 case V4L2_PIX_FMT_Y16:
253 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
254 return -EINVAL;
255 break;
256 case V4L2_PIX_FMT_SBGGR8:
257 case V4L2_PIX_FMT_SBGGR16:
258 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
259 return -EINVAL;
260 break;
261 case 0:
262 /* No format change, only geometry */
263 break;
264 default:
265 return -EINVAL;
266 }
267
268 /* Like in example app. Contradicts the datasheet though */
269 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
270 if (ret >= 0) {
271 if (ret & 1) /* Autoexposure */
272 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
273 rect->height + icd->y_skip_top + 43);
274 else
275 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
276 rect->height + icd->y_skip_top + 43);
277 }
278 /* Setup frame format: defaults apart from width and height */
279 if (ret >= 0)
280 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
281 if (ret >= 0)
282 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
283 if (ret >= 0)
284 /* Default 94, Phytec driver says:
285 * "width + horizontal blank >= 660" */
286 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
287 rect->width > 660 - 43 ? 43 :
288 660 - rect->width);
289 if (ret >= 0)
290 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
291 if (ret >= 0)
292 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
293 if (ret >= 0)
294 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
295 rect->height + icd->y_skip_top);
296
297 if (ret < 0)
298 return ret;
299
300 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
301
302 if ((mt9v022->datawidth != 10 && (width_flag == IS_DATAWIDTH_10)) ||
303 (mt9v022->datawidth != 9 && (width_flag == IS_DATAWIDTH_9)) ||
304 (mt9v022->datawidth != 8 && (width_flag == IS_DATAWIDTH_8))) {
305 /* data width switch requested */
306 if (mt9v022->switch_gpio == NO_GPIO)
307 return -EINVAL;
308
309 /* Well, we actually only can do 10 or 8 bits... */
310 if (width_flag == IS_DATAWIDTH_9)
311 return -EINVAL;
312
313 ret = bus_switch_act(mt9v022,
314 width_flag == IS_DATAWIDTH_8);
315 if (ret < 0)
316 return ret;
317
318 mt9v022->datawidth = width_flag == IS_DATAWIDTH_8 ? 8 : 10;
319 }
320
321 if (flags & IS_PCLK_SAMPLE_RISING)
322 pixclk |= 0x10;
323
324 if (!(flags & IS_HSYNC_ACTIVE_HIGH))
325 pixclk |= 0x1;
326
327 if (!(flags & IS_VSYNC_ACTIVE_HIGH))
328 pixclk |= 0x2;
329
330 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
331 if (ret < 0)
332 return ret;
333
334 if (!(flags & IS_MASTER))
335 mt9v022->chip_control &= ~0x8;
336
337 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
338 if (ret < 0)
339 return ret;
340
341 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
342 pixclk, mt9v022->chip_control);
343
344 return 0;
345}
346
347static int mt9v022_try_fmt_cap(struct soc_camera_device *icd,
348 struct v4l2_format *f)
349{
350 if (f->fmt.pix.height < 32 + icd->y_skip_top)
351 f->fmt.pix.height = 32 + icd->y_skip_top;
352 if (f->fmt.pix.height > 480 + icd->y_skip_top)
353 f->fmt.pix.height = 480 + icd->y_skip_top;
354 if (f->fmt.pix.width < 48)
355 f->fmt.pix.width = 48;
356 if (f->fmt.pix.width > 752)
357 f->fmt.pix.width = 752;
358 f->fmt.pix.width &= ~0x03; /* ? */
359
360 return 0;
361}
362
363static int mt9v022_get_chip_id(struct soc_camera_device *icd,
364 struct v4l2_chip_ident *id)
365{
366 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
367
368 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
369 return -EINVAL;
370
371 if (id->match_chip != mt9v022->client->addr)
372 return -ENODEV;
373
374 id->ident = mt9v022->model;
375 id->revision = 0;
376
377 return 0;
378}
379
380#ifdef CONFIG_VIDEO_ADV_DEBUG
381static int mt9v022_get_register(struct soc_camera_device *icd,
382 struct v4l2_register *reg)
383{
384 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
385
386 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
387 return -EINVAL;
388
389 if (reg->match_chip != mt9v022->client->addr)
390 return -ENODEV;
391
392 reg->val = reg_read(icd, reg->reg);
393
394 if (reg->val > 0xffff)
395 return -EIO;
396
397 return 0;
398}
399
400static int mt9v022_set_register(struct soc_camera_device *icd,
401 struct v4l2_register *reg)
402{
403 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
404
405 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
406 return -EINVAL;
407
408 if (reg->match_chip != mt9v022->client->addr)
409 return -ENODEV;
410
411 if (reg_write(icd, reg->reg, reg->val) < 0)
412 return -EIO;
413
414 return 0;
415}
416#endif
417
418static unsigned int mt9v022_get_datawidth(struct soc_camera_device *icd)
419{
420 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
421 return mt9v022->datawidth;
422}
423
424const struct v4l2_queryctrl mt9v022_controls[] = {
425 {
426 .id = V4L2_CID_VFLIP,
427 .type = V4L2_CTRL_TYPE_BOOLEAN,
428 .name = "Flip Vertically",
429 .minimum = 0,
430 .maximum = 1,
431 .step = 1,
432 .default_value = 0,
433 }, {
434 .id = V4L2_CID_HFLIP,
435 .type = V4L2_CTRL_TYPE_BOOLEAN,
436 .name = "Flip Horizontally",
437 .minimum = 0,
438 .maximum = 1,
439 .step = 1,
440 .default_value = 0,
441 }, {
442 .id = V4L2_CID_GAIN,
443 .type = V4L2_CTRL_TYPE_INTEGER,
444 .name = "Analog Gain",
445 .minimum = 64,
446 .maximum = 127,
447 .step = 1,
448 .default_value = 64,
449 .flags = V4L2_CTRL_FLAG_SLIDER,
450 }, {
451 .id = V4L2_CID_EXPOSURE,
452 .type = V4L2_CTRL_TYPE_INTEGER,
453 .name = "Exposure",
454 .minimum = 1,
455 .maximum = 255,
456 .step = 1,
457 .default_value = 255,
458 .flags = V4L2_CTRL_FLAG_SLIDER,
459 }, {
460 .id = V4L2_CID_AUTOGAIN,
461 .type = V4L2_CTRL_TYPE_BOOLEAN,
462 .name = "Automatic Gain",
463 .minimum = 0,
464 .maximum = 1,
465 .step = 1,
466 .default_value = 1,
467 }, {
468 .id = V4L2_CID_EXPOSURE_AUTO,
469 .type = V4L2_CTRL_TYPE_BOOLEAN,
470 .name = "Automatic Exposure",
471 .minimum = 0,
472 .maximum = 1,
473 .step = 1,
474 .default_value = 1,
475 }
476};
477
478static int mt9v022_get_control(struct soc_camera_device *icd,
479 struct v4l2_control *ctrl);
480static int mt9v022_set_control(struct soc_camera_device *icd,
481 struct v4l2_control *ctrl);
482
483static struct soc_camera_ops mt9v022_ops = {
484 .owner = THIS_MODULE,
485 .init = mt9v022_init,
486 .release = mt9v022_release,
487 .start_capture = mt9v022_start_capture,
488 .stop_capture = mt9v022_stop_capture,
489 .set_capture_format = mt9v022_set_capture_format,
490 .try_fmt_cap = mt9v022_try_fmt_cap,
491 .formats = mt9v022_formats,
492 .num_formats = ARRAY_SIZE(mt9v022_formats),
493 .get_datawidth = mt9v022_get_datawidth,
494 .controls = mt9v022_controls,
495 .num_controls = ARRAY_SIZE(mt9v022_controls),
496 .get_control = mt9v022_get_control,
497 .set_control = mt9v022_set_control,
498 .get_chip_id = mt9v022_get_chip_id,
499#ifdef CONFIG_VIDEO_ADV_DEBUG
500 .get_register = mt9v022_get_register,
501 .set_register = mt9v022_set_register,
502#endif
503};
504
505static int mt9v022_get_control(struct soc_camera_device *icd,
506 struct v4l2_control *ctrl)
507{
508 int data;
509
510 switch (ctrl->id) {
511 case V4L2_CID_VFLIP:
512 data = reg_read(icd, MT9V022_READ_MODE);
513 if (data < 0)
514 return -EIO;
515 ctrl->value = !!(data & 0x10);
516 break;
517 case V4L2_CID_HFLIP:
518 data = reg_read(icd, MT9V022_READ_MODE);
519 if (data < 0)
520 return -EIO;
521 ctrl->value = !!(data & 0x20);
522 break;
523 case V4L2_CID_EXPOSURE_AUTO:
524 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
525 if (data < 0)
526 return -EIO;
527 ctrl->value = !!(data & 0x1);
528 break;
529 case V4L2_CID_AUTOGAIN:
530 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
531 if (data < 0)
532 return -EIO;
533 ctrl->value = !!(data & 0x2);
534 break;
535 }
536 return 0;
537}
538
539static int mt9v022_set_control(struct soc_camera_device *icd,
540 struct v4l2_control *ctrl)
541{
542 int data;
543 const struct v4l2_queryctrl *qctrl;
544
545 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
546
547 if (!qctrl)
548 return -EINVAL;
549
550 switch (ctrl->id) {
551 case V4L2_CID_VFLIP:
552 if (ctrl->value)
553 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
554 else
555 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
556 if (data < 0)
557 return -EIO;
558 break;
559 case V4L2_CID_HFLIP:
560 if (ctrl->value)
561 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
562 else
563 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
564 if (data < 0)
565 return -EIO;
566 break;
567 case V4L2_CID_GAIN:
568 /* mt9v022 has minimum == default */
569 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
570 return -EINVAL;
571 else {
572 unsigned long range = qctrl->maximum - qctrl->minimum;
573 /* Datasheet says 16 to 64. autogain only works properly
574 * after setting gain to maximum 14. Larger values
575 * produce "white fly" noise effect. On the whole,
576 * manually setting analog gain does no good. */
577 unsigned long gain = ((ctrl->value - qctrl->minimum) *
578 10 + range / 2) / range + 4;
579 if (gain >= 32)
580 gain &= ~1;
581 /* The user wants to set gain manually, hope, she
582 * knows, what she's doing... Switch AGC off. */
583
584 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
585 return -EIO;
586
587 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
588 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
589 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
590 return -EIO;
591 icd->gain = ctrl->value;
592 }
593 break;
594 case V4L2_CID_EXPOSURE:
595 /* mt9v022 has maximum == default */
596 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
597 return -EINVAL;
598 else {
599 unsigned long range = qctrl->maximum - qctrl->minimum;
600 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
601 479 + range / 2) / range + 1;
602 /* The user wants to set shutter width manually, hope,
603 * she knows, what she's doing... Switch AEC off. */
604
605 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
606 return -EIO;
607
608 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
609 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
610 shutter);
611 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
612 shutter) < 0)
613 return -EIO;
614 icd->exposure = ctrl->value;
615 }
616 break;
617 case V4L2_CID_AUTOGAIN:
618 if (ctrl->value)
619 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
620 else
621 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
622 if (data < 0)
623 return -EIO;
624 break;
625 case V4L2_CID_EXPOSURE_AUTO:
626 if (ctrl->value)
627 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
628 else
629 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
630 if (data < 0)
631 return -EIO;
632 break;
633 }
634 return 0;
635}
636
637/* Interface active, can use i2c. If it fails, it can indeed mean, that
638 * this wasn't our capture interface, so, we wait for the right one */
639static int mt9v022_video_probe(struct soc_camera_device *icd)
640{
641 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
642 s32 data;
643 int ret;
644
645 if (!icd->dev.parent ||
646 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
647 return -ENODEV;
648
649 /* Read out the chip version register */
650 data = reg_read(icd, MT9V022_CHIP_VERSION);
651
652 /* must be 0x1311 or 0x1313 */
653 if (data != 0x1311 && data != 0x1313) {
654 ret = -ENODEV;
655 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
656 data);
657 goto ei2c;
658 }
659
660 /* Soft reset */
661 ret = reg_write(icd, MT9V022_RESET, 1);
662 if (ret < 0)
663 goto ei2c;
664 /* 15 clock cycles */
665 udelay(200);
666 if (reg_read(icd, MT9V022_RESET)) {
667 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
668 goto ei2c;
669 }
670
671 /* Set monochrome or colour sensor type */
672 if (sensor_type && (!strcmp("colour", sensor_type) ||
673 !strcmp("color", sensor_type))) {
674 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
675 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
676 } else {
677 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
678 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
679 }
680
681 if (ret >= 0)
682 ret = soc_camera_video_start(icd);
683 if (ret < 0)
684 goto eisis;
685
686 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
687 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
688 "monochrome" : "colour");
689
690 return 0;
691
692eisis:
693ei2c:
694 return ret;
695}
696
697static void mt9v022_video_remove(struct soc_camera_device *icd)
698{
699 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
700
701 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
702 mt9v022->icd.dev.parent, mt9v022->icd.vdev);
703 soc_camera_video_stop(&mt9v022->icd);
704}
705
706static int mt9v022_probe(struct i2c_client *client)
707{
708 struct mt9v022 *mt9v022;
709 struct soc_camera_device *icd;
710 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
711 struct soc_camera_link *icl = client->dev.platform_data;
712 int ret;
713
714 if (!icl) {
715 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
716 return -EINVAL;
717 }
718
719 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
720 dev_warn(&adapter->dev,
721 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
722 return -EIO;
723 }
724
725 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
726 if (!mt9v022)
727 return -ENOMEM;
728
729 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
730 mt9v022->client = client;
731 i2c_set_clientdata(client, mt9v022);
732
733 icd = &mt9v022->icd;
734 icd->probe = mt9v022_video_probe;
735 icd->remove = mt9v022_video_remove;
736 icd->ops = &mt9v022_ops;
737 icd->control = &client->dev;
738 icd->x_min = 1;
739 icd->y_min = 4;
740 icd->x_current = 1;
741 icd->y_current = 4;
742 icd->width_min = 48;
743 icd->width_max = 752;
744 icd->height_min = 32;
745 icd->height_max = 480;
746 icd->y_skip_top = 1;
747 icd->iface = icl->bus_id;
748 /* Default datawidth - this is the only width this camera (normally)
749 * supports. It is only with extra logic that it can support
750 * other widths. Therefore it seems to be a sensible default. */
751 mt9v022->datawidth = 10;
752
753 ret = bus_switch_request(mt9v022, icl);
754 if (ret)
755 goto eswinit;
756
757 ret = soc_camera_device_register(icd);
758 if (ret)
759 goto eisdr;
760
761 return 0;
762
763eisdr:
764 bus_switch_release(mt9v022);
765eswinit:
766 kfree(mt9v022);
767 return ret;
768}
769
770static int mt9v022_remove(struct i2c_client *client)
771{
772 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
773
774 soc_camera_device_unregister(&mt9v022->icd);
775 bus_switch_release(mt9v022);
776 kfree(mt9v022);
777
778 return 0;
779}
780
781static struct i2c_driver mt9v022_i2c_driver = {
782 .driver = {
783 .name = "mt9v022",
784 },
785 .probe = mt9v022_probe,
786 .remove = mt9v022_remove,
787};
788
789static int __init mt9v022_mod_init(void)
790{
791 return i2c_add_driver(&mt9v022_i2c_driver);
792}
793
794static void __exit mt9v022_mod_exit(void)
795{
796 i2c_del_driver(&mt9v022_i2c_driver);
797}
798
799module_init(mt9v022_mod_init);
800module_exit(mt9v022_mod_exit);
801
802MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
803MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
804MODULE_LICENSE("GPL");