blob: d677344d233d4db6f7be7a06dbca2ed6ad9033be [file] [log] [blame]
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -03001/*
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
Guennadi Liakhovetskief6ad5c2008-04-22 14:42:08 -030021#ifdef CONFIG_MT9M001_PCA9536_SWITCH
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030022#include <asm/gpio.h>
Guennadi Liakhovetskief6ad5c2008-04-22 14:42:08 -030023#endif
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030024
25/* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
26 * The platform has to define i2c_board_info
27 * and call i2c_register_board_info() */
28
29static char *sensor_type;
30module_param(sensor_type, charp, S_IRUGO);
31MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"\n");
32
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
58
59/* Progressive scan, master, defaults */
60#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
61
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030062static const struct soc_camera_data_format mt9v022_colour_formats[] = {
63 /* Order important: first natively supported,
64 * second supported with a GPIO extender */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030065 {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030066 .name = "Bayer (sRGB) 10 bit",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030067 .depth = 10,
68 .fourcc = V4L2_PIX_FMT_SBGGR16,
69 .colorspace = V4L2_COLORSPACE_SRGB,
70 }, {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030071 .name = "Bayer (sRGB) 8 bit",
72 .depth = 8,
73 .fourcc = V4L2_PIX_FMT_SBGGR8,
74 .colorspace = V4L2_COLORSPACE_SRGB,
75 }
76};
77
78static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
79 /* Order important - see above */
80 {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030081 .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,
88 },
89};
90
91struct 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;
98};
99
100static int reg_read(struct soc_camera_device *icd, const u8 reg)
101{
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);
106}
107
108static int reg_write(struct soc_camera_device *icd, const u8 reg,
109 const u16 data)
110{
111 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
112 return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
113}
114
115static int reg_set(struct soc_camera_device *icd, const u8 reg,
116 const u16 data)
117{
118 int ret;
119
120 ret = reg_read(icd, reg);
121 if (ret < 0)
122 return ret;
123 return reg_write(icd, reg, ret | data);
124}
125
126static int reg_clear(struct soc_camera_device *icd, const u8 reg,
127 const u16 data)
128{
129 int ret;
130
131 ret = reg_read(icd, reg);
132 if (ret < 0)
133 return ret;
134 return reg_write(icd, reg, ret & ~data);
135}
136
137static int mt9v022_init(struct soc_camera_device *icd)
138{
139 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
140 int ret;
141
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);
149
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);
161
162 return ret >= 0 ? 0 : -EIO;
163}
164
165static int mt9v022_release(struct soc_camera_device *icd)
166{
167 /* Nothing? */
168 return 0;
169}
170
171static int mt9v022_start_capture(struct soc_camera_device *icd)
172{
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;
180}
181
182static int mt9v022_stop_capture(struct soc_camera_device *icd)
183{
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;
191}
192
193static int bus_switch_request(struct mt9v022 *mt9v022, struct soc_camera_link *icl)
194{
195#ifdef CONFIG_MT9V022_PCA9536_SWITCH
196 int ret;
197 unsigned int gpio = icl->gpio;
198
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300199 if (gpio_is_valid(gpio)) {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300200 /* 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;
205 }
206
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;
213 }
214 }
215
216 mt9v022->switch_gpio = gpio;
217#else
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300218 mt9v022->switch_gpio = -EINVAL;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300219#endif
220 return 0;
221}
222
223static void bus_switch_release(struct mt9v022 *mt9v022)
224{
225#ifdef CONFIG_MT9V022_PCA9536_SWITCH
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300226 if (gpio_is_valid(mt9v022->switch_gpio))
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300227 gpio_free(mt9v022->switch_gpio);
228#endif
229}
230
231static int bus_switch_act(struct mt9v022 *mt9v022, int go8bit)
232{
233#ifdef CONFIG_MT9V022_PCA9536_SWITCH
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300234 if (!gpio_is_valid(mt9v022->switch_gpio))
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300235 return -ENODEV;
236
237 gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit);
238 return 0;
239#else
240 return -ENODEV;
241#endif
242}
243
244static int mt9v022_set_capture_format(struct soc_camera_device *icd,
245 __u32 pixfmt, struct v4l2_rect *rect, unsigned int flags)
246{
247 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
248 unsigned int width_flag = flags & (IS_DATAWIDTH_10 | IS_DATAWIDTH_9 |
249 IS_DATAWIDTH_8);
250 u16 pixclk = 0;
251 int ret;
252
253 /* Only one width bit may be set */
254 if (!is_power_of_2(width_flag))
255 return -EINVAL;
256
257 /* The caller provides a supported format, as verified per call to
258 * icd->try_fmt_cap(), datawidth is from our supported format list */
259 switch (pixfmt) {
260 case V4L2_PIX_FMT_GREY:
261 case V4L2_PIX_FMT_Y16:
262 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
263 return -EINVAL;
264 break;
265 case V4L2_PIX_FMT_SBGGR8:
266 case V4L2_PIX_FMT_SBGGR16:
267 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
268 return -EINVAL;
269 break;
270 case 0:
271 /* No format change, only geometry */
272 break;
273 default:
274 return -EINVAL;
275 }
276
277 /* Like in example app. Contradicts the datasheet though */
278 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
279 if (ret >= 0) {
280 if (ret & 1) /* Autoexposure */
281 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
282 rect->height + icd->y_skip_top + 43);
283 else
284 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
285 rect->height + icd->y_skip_top + 43);
286 }
287 /* Setup frame format: defaults apart from width and height */
288 if (ret >= 0)
289 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
290 if (ret >= 0)
291 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
292 if (ret >= 0)
293 /* Default 94, Phytec driver says:
294 * "width + horizontal blank >= 660" */
295 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
296 rect->width > 660 - 43 ? 43 :
297 660 - rect->width);
298 if (ret >= 0)
299 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
300 if (ret >= 0)
301 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
302 if (ret >= 0)
303 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
304 rect->height + icd->y_skip_top);
305
306 if (ret < 0)
307 return ret;
308
309 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
310
311 if ((mt9v022->datawidth != 10 && (width_flag == IS_DATAWIDTH_10)) ||
312 (mt9v022->datawidth != 9 && (width_flag == IS_DATAWIDTH_9)) ||
313 (mt9v022->datawidth != 8 && (width_flag == IS_DATAWIDTH_8))) {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300314 /* Well, we actually only can do 10 or 8 bits... */
315 if (width_flag == IS_DATAWIDTH_9)
316 return -EINVAL;
317
318 ret = bus_switch_act(mt9v022,
319 width_flag == IS_DATAWIDTH_8);
320 if (ret < 0)
321 return ret;
322
323 mt9v022->datawidth = width_flag == IS_DATAWIDTH_8 ? 8 : 10;
324 }
325
326 if (flags & IS_PCLK_SAMPLE_RISING)
327 pixclk |= 0x10;
328
329 if (!(flags & IS_HSYNC_ACTIVE_HIGH))
330 pixclk |= 0x1;
331
332 if (!(flags & IS_VSYNC_ACTIVE_HIGH))
333 pixclk |= 0x2;
334
335 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
336 if (ret < 0)
337 return ret;
338
339 if (!(flags & IS_MASTER))
340 mt9v022->chip_control &= ~0x8;
341
342 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
343 if (ret < 0)
344 return ret;
345
346 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
347 pixclk, mt9v022->chip_control);
348
349 return 0;
350}
351
352static int mt9v022_try_fmt_cap(struct soc_camera_device *icd,
353 struct v4l2_format *f)
354{
355 if (f->fmt.pix.height < 32 + icd->y_skip_top)
356 f->fmt.pix.height = 32 + icd->y_skip_top;
357 if (f->fmt.pix.height > 480 + icd->y_skip_top)
358 f->fmt.pix.height = 480 + icd->y_skip_top;
359 if (f->fmt.pix.width < 48)
360 f->fmt.pix.width = 48;
361 if (f->fmt.pix.width > 752)
362 f->fmt.pix.width = 752;
363 f->fmt.pix.width &= ~0x03; /* ? */
364
365 return 0;
366}
367
368static int mt9v022_get_chip_id(struct soc_camera_device *icd,
369 struct v4l2_chip_ident *id)
370{
371 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
372
373 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
374 return -EINVAL;
375
376 if (id->match_chip != mt9v022->client->addr)
377 return -ENODEV;
378
379 id->ident = mt9v022->model;
380 id->revision = 0;
381
382 return 0;
383}
384
385#ifdef CONFIG_VIDEO_ADV_DEBUG
386static int mt9v022_get_register(struct soc_camera_device *icd,
387 struct v4l2_register *reg)
388{
389 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
390
391 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
392 return -EINVAL;
393
394 if (reg->match_chip != mt9v022->client->addr)
395 return -ENODEV;
396
397 reg->val = reg_read(icd, reg->reg);
398
399 if (reg->val > 0xffff)
400 return -EIO;
401
402 return 0;
403}
404
405static int mt9v022_set_register(struct soc_camera_device *icd,
406 struct v4l2_register *reg)
407{
408 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
409
410 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
411 return -EINVAL;
412
413 if (reg->match_chip != mt9v022->client->addr)
414 return -ENODEV;
415
416 if (reg_write(icd, reg->reg, reg->val) < 0)
417 return -EIO;
418
419 return 0;
420}
421#endif
422
423static unsigned int mt9v022_get_datawidth(struct soc_camera_device *icd)
424{
425 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
426 return mt9v022->datawidth;
427}
428
429const struct v4l2_queryctrl mt9v022_controls[] = {
430 {
431 .id = V4L2_CID_VFLIP,
432 .type = V4L2_CTRL_TYPE_BOOLEAN,
433 .name = "Flip Vertically",
434 .minimum = 0,
435 .maximum = 1,
436 .step = 1,
437 .default_value = 0,
438 }, {
439 .id = V4L2_CID_HFLIP,
440 .type = V4L2_CTRL_TYPE_BOOLEAN,
441 .name = "Flip Horizontally",
442 .minimum = 0,
443 .maximum = 1,
444 .step = 1,
445 .default_value = 0,
446 }, {
447 .id = V4L2_CID_GAIN,
448 .type = V4L2_CTRL_TYPE_INTEGER,
449 .name = "Analog Gain",
450 .minimum = 64,
451 .maximum = 127,
452 .step = 1,
453 .default_value = 64,
454 .flags = V4L2_CTRL_FLAG_SLIDER,
455 }, {
456 .id = V4L2_CID_EXPOSURE,
457 .type = V4L2_CTRL_TYPE_INTEGER,
458 .name = "Exposure",
459 .minimum = 1,
460 .maximum = 255,
461 .step = 1,
462 .default_value = 255,
463 .flags = V4L2_CTRL_FLAG_SLIDER,
464 }, {
465 .id = V4L2_CID_AUTOGAIN,
466 .type = V4L2_CTRL_TYPE_BOOLEAN,
467 .name = "Automatic Gain",
468 .minimum = 0,
469 .maximum = 1,
470 .step = 1,
471 .default_value = 1,
472 }, {
473 .id = V4L2_CID_EXPOSURE_AUTO,
474 .type = V4L2_CTRL_TYPE_BOOLEAN,
475 .name = "Automatic Exposure",
476 .minimum = 0,
477 .maximum = 1,
478 .step = 1,
479 .default_value = 1,
480 }
481};
482
483static int mt9v022_get_control(struct soc_camera_device *icd,
484 struct v4l2_control *ctrl);
485static int mt9v022_set_control(struct soc_camera_device *icd,
486 struct v4l2_control *ctrl);
487
488static struct soc_camera_ops mt9v022_ops = {
489 .owner = THIS_MODULE,
490 .init = mt9v022_init,
491 .release = mt9v022_release,
492 .start_capture = mt9v022_start_capture,
493 .stop_capture = mt9v022_stop_capture,
494 .set_capture_format = mt9v022_set_capture_format,
495 .try_fmt_cap = mt9v022_try_fmt_cap,
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300496 .formats = NULL, /* Filled in later depending on the */
497 .num_formats = 0, /* sensor type and data widths */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300498 .get_datawidth = mt9v022_get_datawidth,
499 .controls = mt9v022_controls,
500 .num_controls = ARRAY_SIZE(mt9v022_controls),
501 .get_control = mt9v022_get_control,
502 .set_control = mt9v022_set_control,
503 .get_chip_id = mt9v022_get_chip_id,
504#ifdef CONFIG_VIDEO_ADV_DEBUG
505 .get_register = mt9v022_get_register,
506 .set_register = mt9v022_set_register,
507#endif
508};
509
510static int mt9v022_get_control(struct soc_camera_device *icd,
511 struct v4l2_control *ctrl)
512{
513 int data;
514
515 switch (ctrl->id) {
516 case V4L2_CID_VFLIP:
517 data = reg_read(icd, MT9V022_READ_MODE);
518 if (data < 0)
519 return -EIO;
520 ctrl->value = !!(data & 0x10);
521 break;
522 case V4L2_CID_HFLIP:
523 data = reg_read(icd, MT9V022_READ_MODE);
524 if (data < 0)
525 return -EIO;
526 ctrl->value = !!(data & 0x20);
527 break;
528 case V4L2_CID_EXPOSURE_AUTO:
529 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
530 if (data < 0)
531 return -EIO;
532 ctrl->value = !!(data & 0x1);
533 break;
534 case V4L2_CID_AUTOGAIN:
535 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
536 if (data < 0)
537 return -EIO;
538 ctrl->value = !!(data & 0x2);
539 break;
540 }
541 return 0;
542}
543
544static int mt9v022_set_control(struct soc_camera_device *icd,
545 struct v4l2_control *ctrl)
546{
547 int data;
548 const struct v4l2_queryctrl *qctrl;
549
550 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
551
552 if (!qctrl)
553 return -EINVAL;
554
555 switch (ctrl->id) {
556 case V4L2_CID_VFLIP:
557 if (ctrl->value)
558 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
559 else
560 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
561 if (data < 0)
562 return -EIO;
563 break;
564 case V4L2_CID_HFLIP:
565 if (ctrl->value)
566 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
567 else
568 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
569 if (data < 0)
570 return -EIO;
571 break;
572 case V4L2_CID_GAIN:
573 /* mt9v022 has minimum == default */
574 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
575 return -EINVAL;
576 else {
577 unsigned long range = qctrl->maximum - qctrl->minimum;
578 /* Datasheet says 16 to 64. autogain only works properly
579 * after setting gain to maximum 14. Larger values
580 * produce "white fly" noise effect. On the whole,
581 * manually setting analog gain does no good. */
582 unsigned long gain = ((ctrl->value - qctrl->minimum) *
583 10 + range / 2) / range + 4;
584 if (gain >= 32)
585 gain &= ~1;
586 /* The user wants to set gain manually, hope, she
587 * knows, what she's doing... Switch AGC off. */
588
589 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
590 return -EIO;
591
592 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
593 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
594 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
595 return -EIO;
596 icd->gain = ctrl->value;
597 }
598 break;
599 case V4L2_CID_EXPOSURE:
600 /* mt9v022 has maximum == 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 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
606 479 + range / 2) / range + 1;
607 /* The user wants to set shutter width manually, hope,
608 * she knows, what she's doing... Switch AEC off. */
609
610 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
611 return -EIO;
612
613 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
614 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
615 shutter);
616 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
617 shutter) < 0)
618 return -EIO;
619 icd->exposure = ctrl->value;
620 }
621 break;
622 case V4L2_CID_AUTOGAIN:
623 if (ctrl->value)
624 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
625 else
626 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
627 if (data < 0)
628 return -EIO;
629 break;
630 case V4L2_CID_EXPOSURE_AUTO:
631 if (ctrl->value)
632 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
633 else
634 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
635 if (data < 0)
636 return -EIO;
637 break;
638 }
639 return 0;
640}
641
642/* Interface active, can use i2c. If it fails, it can indeed mean, that
643 * this wasn't our capture interface, so, we wait for the right one */
644static int mt9v022_video_probe(struct soc_camera_device *icd)
645{
646 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
647 s32 data;
648 int ret;
649
650 if (!icd->dev.parent ||
651 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
652 return -ENODEV;
653
654 /* Read out the chip version register */
655 data = reg_read(icd, MT9V022_CHIP_VERSION);
656
657 /* must be 0x1311 or 0x1313 */
658 if (data != 0x1311 && data != 0x1313) {
659 ret = -ENODEV;
660 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
661 data);
662 goto ei2c;
663 }
664
665 /* Soft reset */
666 ret = reg_write(icd, MT9V022_RESET, 1);
667 if (ret < 0)
668 goto ei2c;
669 /* 15 clock cycles */
670 udelay(200);
671 if (reg_read(icd, MT9V022_RESET)) {
672 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
673 goto ei2c;
674 }
675
676 /* Set monochrome or colour sensor type */
677 if (sensor_type && (!strcmp("colour", sensor_type) ||
678 !strcmp("color", sensor_type))) {
679 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
680 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300681 mt9v022_ops.formats = mt9v022_colour_formats;
682 if (mt9v022->client->dev.platform_data)
683 mt9v022_ops.num_formats = ARRAY_SIZE(mt9v022_colour_formats);
684 else
685 mt9v022_ops.num_formats = 1;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300686 } else {
687 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
688 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300689 mt9v022_ops.formats = mt9v022_monochrome_formats;
690 if (mt9v022->client->dev.platform_data)
691 mt9v022_ops.num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
692 else
693 mt9v022_ops.num_formats = 1;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300694 }
695
696 if (ret >= 0)
697 ret = soc_camera_video_start(icd);
698 if (ret < 0)
699 goto eisis;
700
701 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
702 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
703 "monochrome" : "colour");
704
705 return 0;
706
707eisis:
708ei2c:
709 return ret;
710}
711
712static void mt9v022_video_remove(struct soc_camera_device *icd)
713{
714 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
715
716 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
717 mt9v022->icd.dev.parent, mt9v022->icd.vdev);
718 soc_camera_video_stop(&mt9v022->icd);
719}
720
721static int mt9v022_probe(struct i2c_client *client)
722{
723 struct mt9v022 *mt9v022;
724 struct soc_camera_device *icd;
725 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
726 struct soc_camera_link *icl = client->dev.platform_data;
727 int ret;
728
729 if (!icl) {
730 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
731 return -EINVAL;
732 }
733
734 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
735 dev_warn(&adapter->dev,
736 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
737 return -EIO;
738 }
739
740 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
741 if (!mt9v022)
742 return -ENOMEM;
743
744 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
745 mt9v022->client = client;
746 i2c_set_clientdata(client, mt9v022);
747
748 icd = &mt9v022->icd;
749 icd->probe = mt9v022_video_probe;
750 icd->remove = mt9v022_video_remove;
751 icd->ops = &mt9v022_ops;
752 icd->control = &client->dev;
753 icd->x_min = 1;
754 icd->y_min = 4;
755 icd->x_current = 1;
756 icd->y_current = 4;
757 icd->width_min = 48;
758 icd->width_max = 752;
759 icd->height_min = 32;
760 icd->height_max = 480;
761 icd->y_skip_top = 1;
762 icd->iface = icl->bus_id;
763 /* Default datawidth - this is the only width this camera (normally)
764 * supports. It is only with extra logic that it can support
765 * other widths. Therefore it seems to be a sensible default. */
766 mt9v022->datawidth = 10;
767
768 ret = bus_switch_request(mt9v022, icl);
769 if (ret)
770 goto eswinit;
771
772 ret = soc_camera_device_register(icd);
773 if (ret)
774 goto eisdr;
775
776 return 0;
777
778eisdr:
779 bus_switch_release(mt9v022);
780eswinit:
781 kfree(mt9v022);
782 return ret;
783}
784
785static int mt9v022_remove(struct i2c_client *client)
786{
787 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
788
789 soc_camera_device_unregister(&mt9v022->icd);
790 bus_switch_release(mt9v022);
791 kfree(mt9v022);
792
793 return 0;
794}
795
796static struct i2c_driver mt9v022_i2c_driver = {
797 .driver = {
798 .name = "mt9v022",
799 },
800 .probe = mt9v022_probe,
801 .remove = mt9v022_remove,
802};
803
804static int __init mt9v022_mod_init(void)
805{
806 return i2c_add_driver(&mt9v022_i2c_driver);
807}
808
809static void __exit mt9v022_mod_exit(void)
810{
811 i2c_del_driver(&mt9v022_i2c_driver);
812}
813
814module_init(mt9v022_mod_init);
815module_exit(mt9v022_mod_exit);
816
817MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
818MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
819MODULE_LICENSE("GPL");