blob: 2584201059d8570efff3d72a59325e9a11a9d82a [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>
Ingo Molnar91e64c82008-05-05 13:57:50 -030016#include <linux/gpio.h>
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030017
18#include <media/v4l2-common.h>
19#include <media/v4l2-chip-ident.h>
20#include <media/soc_camera.h>
21
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030022/* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
23 * The platform has to define i2c_board_info
24 * and call i2c_register_board_info() */
25
26static char *sensor_type;
27module_param(sensor_type, charp, S_IRUGO);
Niels de Vos61a2d072008-07-31 00:07:23 -070028MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030029
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
55
56/* Progressive scan, master, defaults */
57#define MT9V022_CHIP_CONTROL_DEFAULT 0x188
58
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030059static const struct soc_camera_data_format mt9v022_colour_formats[] = {
60 /* Order important: first natively supported,
61 * second supported with a GPIO extender */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030062 {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030063 .name = "Bayer (sRGB) 10 bit",
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030064 .depth = 10,
65 .fourcc = V4L2_PIX_FMT_SBGGR16,
66 .colorspace = V4L2_COLORSPACE_SRGB,
67 }, {
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -030068 .name = "Bayer (sRGB) 8 bit",
69 .depth = 8,
70 .fourcc = V4L2_PIX_FMT_SBGGR8,
71 .colorspace = V4L2_COLORSPACE_SRGB,
72 }
73};
74
75static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
76 /* Order important - see above */
77 {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030078 .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,
85 },
86};
87
88struct mt9v022 {
89 struct i2c_client *client;
90 struct soc_camera_device icd;
Guennadi Liakhovetski7fb0fd02008-05-05 14:12:30 -030091 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -030092 int switch_gpio;
93 u16 chip_control;
94 unsigned char datawidth;
95};
96
97static int reg_read(struct soc_camera_device *icd, const u8 reg)
98{
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);
103}
104
105static int reg_write(struct soc_camera_device *icd, const u8 reg,
106 const u16 data)
107{
108 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
109 return i2c_smbus_write_word_data(mt9v022->client, reg, swab16(data));
110}
111
112static int reg_set(struct soc_camera_device *icd, const u8 reg,
113 const u16 data)
114{
115 int ret;
116
117 ret = reg_read(icd, reg);
118 if (ret < 0)
119 return ret;
120 return reg_write(icd, reg, ret | data);
121}
122
123static int reg_clear(struct soc_camera_device *icd, const u8 reg,
124 const u16 data)
125{
126 int ret;
127
128 ret = reg_read(icd, reg);
129 if (ret < 0)
130 return ret;
131 return reg_write(icd, reg, ret & ~data);
132}
133
134static int mt9v022_init(struct soc_camera_device *icd)
135{
136 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300137 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300138 int ret;
139
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300140 if (icl->power) {
141 ret = icl->power(&mt9v022->client->dev, 1);
142 if (ret < 0) {
143 dev_err(icd->vdev->parent,
144 "Platform failed to power-on the camera.\n");
145 return ret;
146 }
147 }
148
149 /*
150 * The camera could have been already on, we hard-reset it additionally,
151 * if available. Soft reset is done in video_probe().
152 */
153 if (icl->reset)
154 icl->reset(&mt9v022->client->dev);
155
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300156 /* Almost the default mode: master, parallel, simultaneous, and an
157 * undocumented bit 0x200, which is present in table 7, but not in 8,
158 * plus snapshot mode to disable scan for now */
159 mt9v022->chip_control |= 0x10;
160 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300161 if (!ret)
162 ret = reg_write(icd, MT9V022_READ_MODE, 0x300);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300163
164 /* All defaults */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300165 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300166 /* AEC, AGC on */
167 ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300168 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300169 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300170 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300171 /* default - auto */
172 ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300173 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300174 ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
175
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300176 return ret;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300177}
178
179static int mt9v022_release(struct soc_camera_device *icd)
180{
Stefan Herbrechtsmeier81034662008-08-14 12:04:11 -0300181 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
182 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
183
184 if (icl->power)
185 icl->power(&mt9v022->client->dev, 0);
186
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300187 return 0;
188}
189
190static int mt9v022_start_capture(struct soc_camera_device *icd)
191{
192 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
193 /* Switch to master "normal" mode */
194 mt9v022->chip_control &= ~0x10;
195 if (reg_write(icd, MT9V022_CHIP_CONTROL,
196 mt9v022->chip_control) < 0)
197 return -EIO;
198 return 0;
199}
200
201static int mt9v022_stop_capture(struct soc_camera_device *icd)
202{
203 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
204 /* Switch to snapshot mode */
205 mt9v022->chip_control |= 0x10;
206 if (reg_write(icd, MT9V022_CHIP_CONTROL,
207 mt9v022->chip_control) < 0)
208 return -EIO;
209 return 0;
210}
211
212static int bus_switch_request(struct mt9v022 *mt9v022, struct soc_camera_link *icl)
213{
214#ifdef CONFIG_MT9V022_PCA9536_SWITCH
215 int ret;
216 unsigned int gpio = icl->gpio;
217
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300218 if (gpio_is_valid(gpio)) {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300219 /* We have a data bus switch. */
220 ret = gpio_request(gpio, "mt9v022");
221 if (ret < 0) {
222 dev_err(&mt9v022->client->dev, "Cannot get GPIO %u\n", gpio);
223 return ret;
224 }
225
226 ret = gpio_direction_output(gpio, 0);
227 if (ret < 0) {
228 dev_err(&mt9v022->client->dev,
229 "Cannot set GPIO %u to output\n", gpio);
230 gpio_free(gpio);
231 return ret;
232 }
233 }
234
235 mt9v022->switch_gpio = gpio;
236#else
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300237 mt9v022->switch_gpio = -EINVAL;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300238#endif
239 return 0;
240}
241
242static void bus_switch_release(struct mt9v022 *mt9v022)
243{
244#ifdef CONFIG_MT9V022_PCA9536_SWITCH
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300245 if (gpio_is_valid(mt9v022->switch_gpio))
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300246 gpio_free(mt9v022->switch_gpio);
247#endif
248}
249
250static int bus_switch_act(struct mt9v022 *mt9v022, int go8bit)
251{
252#ifdef CONFIG_MT9V022_PCA9536_SWITCH
Guennadi Liakhovetskib4333a32008-04-22 14:42:08 -0300253 if (!gpio_is_valid(mt9v022->switch_gpio))
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300254 return -ENODEV;
255
256 gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit);
257 return 0;
258#else
259 return -ENODEV;
260#endif
261}
262
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300263static int bus_switch_possible(struct mt9v022 *mt9v022)
264{
265#ifdef CONFIG_MT9V022_PCA9536_SWITCH
266 return gpio_is_valid(mt9v022->switch_gpio);
267#else
268 return 0;
269#endif
270}
271
272static int mt9v022_set_bus_param(struct soc_camera_device *icd,
273 unsigned long flags)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300274{
275 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300276 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300277 int ret;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300278 u16 pixclk = 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300279
280 /* Only one width bit may be set */
281 if (!is_power_of_2(width_flag))
282 return -EINVAL;
283
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300284 if ((mt9v022->datawidth != 10 && (width_flag == SOCAM_DATAWIDTH_10)) ||
285 (mt9v022->datawidth != 9 && (width_flag == SOCAM_DATAWIDTH_9)) ||
286 (mt9v022->datawidth != 8 && (width_flag == SOCAM_DATAWIDTH_8))) {
287 /* Well, we actually only can do 10 or 8 bits... */
288 if (width_flag == SOCAM_DATAWIDTH_9)
289 return -EINVAL;
290
291 ret = bus_switch_act(mt9v022,
292 width_flag == SOCAM_DATAWIDTH_8);
293 if (ret < 0)
294 return ret;
295
296 mt9v022->datawidth = width_flag == SOCAM_DATAWIDTH_8 ? 8 : 10;
297 }
298
299 if (flags & SOCAM_PCLK_SAMPLE_RISING)
300 pixclk |= 0x10;
301
302 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
303 pixclk |= 0x1;
304
305 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
306 pixclk |= 0x2;
307
308 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
309 if (ret < 0)
310 return ret;
311
312 if (!(flags & SOCAM_MASTER))
313 mt9v022->chip_control &= ~0x8;
314
315 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
316 if (ret < 0)
317 return ret;
318
319 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
320 pixclk, mt9v022->chip_control);
321
322 return 0;
323}
324
325static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
326{
327 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
328 unsigned int width_flag = SOCAM_DATAWIDTH_10;
329
330 if (bus_switch_possible(mt9v022))
331 width_flag |= SOCAM_DATAWIDTH_8;
332
333 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
334 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
335 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
336 SOCAM_MASTER | SOCAM_SLAVE |
337 width_flag;
338}
339
340static int mt9v022_set_fmt_cap(struct soc_camera_device *icd,
341 __u32 pixfmt, struct v4l2_rect *rect)
342{
343 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
344 int ret;
345
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300346 /* The caller provides a supported format, as verified per call to
347 * icd->try_fmt_cap(), datawidth is from our supported format list */
348 switch (pixfmt) {
349 case V4L2_PIX_FMT_GREY:
350 case V4L2_PIX_FMT_Y16:
351 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
352 return -EINVAL;
353 break;
354 case V4L2_PIX_FMT_SBGGR8:
355 case V4L2_PIX_FMT_SBGGR16:
356 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
357 return -EINVAL;
358 break;
359 case 0:
360 /* No format change, only geometry */
361 break;
362 default:
363 return -EINVAL;
364 }
365
366 /* Like in example app. Contradicts the datasheet though */
367 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
368 if (ret >= 0) {
369 if (ret & 1) /* Autoexposure */
370 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
371 rect->height + icd->y_skip_top + 43);
372 else
373 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
374 rect->height + icd->y_skip_top + 43);
375 }
376 /* Setup frame format: defaults apart from width and height */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300377 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300378 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300379 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300380 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300381 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300382 /* Default 94, Phytec driver says:
383 * "width + horizontal blank >= 660" */
384 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
385 rect->width > 660 - 43 ? 43 :
386 660 - rect->width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300387 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300388 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300389 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300390 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300391 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300392 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
393 rect->height + icd->y_skip_top);
394
395 if (ret < 0)
396 return ret;
397
398 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
399
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300400 return 0;
401}
402
403static int mt9v022_try_fmt_cap(struct soc_camera_device *icd,
404 struct v4l2_format *f)
405{
406 if (f->fmt.pix.height < 32 + icd->y_skip_top)
407 f->fmt.pix.height = 32 + icd->y_skip_top;
408 if (f->fmt.pix.height > 480 + icd->y_skip_top)
409 f->fmt.pix.height = 480 + icd->y_skip_top;
410 if (f->fmt.pix.width < 48)
411 f->fmt.pix.width = 48;
412 if (f->fmt.pix.width > 752)
413 f->fmt.pix.width = 752;
414 f->fmt.pix.width &= ~0x03; /* ? */
415
416 return 0;
417}
418
419static int mt9v022_get_chip_id(struct soc_camera_device *icd,
420 struct v4l2_chip_ident *id)
421{
422 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
423
424 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
425 return -EINVAL;
426
427 if (id->match_chip != mt9v022->client->addr)
428 return -ENODEV;
429
430 id->ident = mt9v022->model;
431 id->revision = 0;
432
433 return 0;
434}
435
436#ifdef CONFIG_VIDEO_ADV_DEBUG
437static int mt9v022_get_register(struct soc_camera_device *icd,
438 struct v4l2_register *reg)
439{
440 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
441
442 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
443 return -EINVAL;
444
445 if (reg->match_chip != mt9v022->client->addr)
446 return -ENODEV;
447
448 reg->val = reg_read(icd, reg->reg);
449
450 if (reg->val > 0xffff)
451 return -EIO;
452
453 return 0;
454}
455
456static int mt9v022_set_register(struct soc_camera_device *icd,
457 struct v4l2_register *reg)
458{
459 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
460
461 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
462 return -EINVAL;
463
464 if (reg->match_chip != mt9v022->client->addr)
465 return -ENODEV;
466
467 if (reg_write(icd, reg->reg, reg->val) < 0)
468 return -EIO;
469
470 return 0;
471}
472#endif
473
Adrian Bunk4407a462008-04-28 17:13:51 -0300474static const struct v4l2_queryctrl mt9v022_controls[] = {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300475 {
476 .id = V4L2_CID_VFLIP,
477 .type = V4L2_CTRL_TYPE_BOOLEAN,
478 .name = "Flip Vertically",
479 .minimum = 0,
480 .maximum = 1,
481 .step = 1,
482 .default_value = 0,
483 }, {
484 .id = V4L2_CID_HFLIP,
485 .type = V4L2_CTRL_TYPE_BOOLEAN,
486 .name = "Flip Horizontally",
487 .minimum = 0,
488 .maximum = 1,
489 .step = 1,
490 .default_value = 0,
491 }, {
492 .id = V4L2_CID_GAIN,
493 .type = V4L2_CTRL_TYPE_INTEGER,
494 .name = "Analog Gain",
495 .minimum = 64,
496 .maximum = 127,
497 .step = 1,
498 .default_value = 64,
499 .flags = V4L2_CTRL_FLAG_SLIDER,
500 }, {
501 .id = V4L2_CID_EXPOSURE,
502 .type = V4L2_CTRL_TYPE_INTEGER,
503 .name = "Exposure",
504 .minimum = 1,
505 .maximum = 255,
506 .step = 1,
507 .default_value = 255,
508 .flags = V4L2_CTRL_FLAG_SLIDER,
509 }, {
510 .id = V4L2_CID_AUTOGAIN,
511 .type = V4L2_CTRL_TYPE_BOOLEAN,
512 .name = "Automatic Gain",
513 .minimum = 0,
514 .maximum = 1,
515 .step = 1,
516 .default_value = 1,
517 }, {
518 .id = V4L2_CID_EXPOSURE_AUTO,
519 .type = V4L2_CTRL_TYPE_BOOLEAN,
520 .name = "Automatic Exposure",
521 .minimum = 0,
522 .maximum = 1,
523 .step = 1,
524 .default_value = 1,
525 }
526};
527
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300528static int mt9v022_video_probe(struct soc_camera_device *);
529static void mt9v022_video_remove(struct soc_camera_device *);
530static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
531static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300532
533static struct soc_camera_ops mt9v022_ops = {
534 .owner = THIS_MODULE,
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300535 .probe = mt9v022_video_probe,
536 .remove = mt9v022_video_remove,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300537 .init = mt9v022_init,
538 .release = mt9v022_release,
539 .start_capture = mt9v022_start_capture,
540 .stop_capture = mt9v022_stop_capture,
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300541 .set_fmt_cap = mt9v022_set_fmt_cap,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300542 .try_fmt_cap = mt9v022_try_fmt_cap,
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300543 .set_bus_param = mt9v022_set_bus_param,
544 .query_bus_param = mt9v022_query_bus_param,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300545 .controls = mt9v022_controls,
546 .num_controls = ARRAY_SIZE(mt9v022_controls),
547 .get_control = mt9v022_get_control,
548 .set_control = mt9v022_set_control,
549 .get_chip_id = mt9v022_get_chip_id,
550#ifdef CONFIG_VIDEO_ADV_DEBUG
551 .get_register = mt9v022_get_register,
552 .set_register = mt9v022_set_register,
553#endif
554};
555
556static int mt9v022_get_control(struct soc_camera_device *icd,
557 struct v4l2_control *ctrl)
558{
559 int data;
560
561 switch (ctrl->id) {
562 case V4L2_CID_VFLIP:
563 data = reg_read(icd, MT9V022_READ_MODE);
564 if (data < 0)
565 return -EIO;
566 ctrl->value = !!(data & 0x10);
567 break;
568 case V4L2_CID_HFLIP:
569 data = reg_read(icd, MT9V022_READ_MODE);
570 if (data < 0)
571 return -EIO;
572 ctrl->value = !!(data & 0x20);
573 break;
574 case V4L2_CID_EXPOSURE_AUTO:
575 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
576 if (data < 0)
577 return -EIO;
578 ctrl->value = !!(data & 0x1);
579 break;
580 case V4L2_CID_AUTOGAIN:
581 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
582 if (data < 0)
583 return -EIO;
584 ctrl->value = !!(data & 0x2);
585 break;
586 }
587 return 0;
588}
589
590static int mt9v022_set_control(struct soc_camera_device *icd,
591 struct v4l2_control *ctrl)
592{
593 int data;
594 const struct v4l2_queryctrl *qctrl;
595
596 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
597
598 if (!qctrl)
599 return -EINVAL;
600
601 switch (ctrl->id) {
602 case V4L2_CID_VFLIP:
603 if (ctrl->value)
604 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
605 else
606 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
607 if (data < 0)
608 return -EIO;
609 break;
610 case V4L2_CID_HFLIP:
611 if (ctrl->value)
612 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
613 else
614 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
615 if (data < 0)
616 return -EIO;
617 break;
618 case V4L2_CID_GAIN:
619 /* mt9v022 has minimum == default */
620 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
621 return -EINVAL;
622 else {
623 unsigned long range = qctrl->maximum - qctrl->minimum;
624 /* Datasheet says 16 to 64. autogain only works properly
625 * after setting gain to maximum 14. Larger values
626 * produce "white fly" noise effect. On the whole,
627 * manually setting analog gain does no good. */
628 unsigned long gain = ((ctrl->value - qctrl->minimum) *
629 10 + range / 2) / range + 4;
630 if (gain >= 32)
631 gain &= ~1;
632 /* The user wants to set gain manually, hope, she
633 * knows, what she's doing... Switch AGC off. */
634
635 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
636 return -EIO;
637
638 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
639 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
640 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
641 return -EIO;
642 icd->gain = ctrl->value;
643 }
644 break;
645 case V4L2_CID_EXPOSURE:
646 /* mt9v022 has maximum == default */
647 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
648 return -EINVAL;
649 else {
650 unsigned long range = qctrl->maximum - qctrl->minimum;
651 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
652 479 + range / 2) / range + 1;
653 /* The user wants to set shutter width manually, hope,
654 * she knows, what she's doing... Switch AEC off. */
655
656 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
657 return -EIO;
658
659 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
660 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
661 shutter);
662 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
663 shutter) < 0)
664 return -EIO;
665 icd->exposure = ctrl->value;
666 }
667 break;
668 case V4L2_CID_AUTOGAIN:
669 if (ctrl->value)
670 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
671 else
672 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
673 if (data < 0)
674 return -EIO;
675 break;
676 case V4L2_CID_EXPOSURE_AUTO:
677 if (ctrl->value)
678 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
679 else
680 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
681 if (data < 0)
682 return -EIO;
683 break;
684 }
685 return 0;
686}
687
688/* Interface active, can use i2c. If it fails, it can indeed mean, that
689 * this wasn't our capture interface, so, we wait for the right one */
690static int mt9v022_video_probe(struct soc_camera_device *icd)
691{
692 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
693 s32 data;
694 int ret;
695
696 if (!icd->dev.parent ||
697 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
698 return -ENODEV;
699
700 /* Read out the chip version register */
701 data = reg_read(icd, MT9V022_CHIP_VERSION);
702
703 /* must be 0x1311 or 0x1313 */
704 if (data != 0x1311 && data != 0x1313) {
705 ret = -ENODEV;
706 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
707 data);
708 goto ei2c;
709 }
710
711 /* Soft reset */
712 ret = reg_write(icd, MT9V022_RESET, 1);
713 if (ret < 0)
714 goto ei2c;
715 /* 15 clock cycles */
716 udelay(200);
717 if (reg_read(icd, MT9V022_RESET)) {
718 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
719 goto ei2c;
720 }
721
722 /* Set monochrome or colour sensor type */
723 if (sensor_type && (!strcmp("colour", sensor_type) ||
724 !strcmp("color", sensor_type))) {
725 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
726 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300727 icd->formats = mt9v022_colour_formats;
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300728 if (mt9v022->client->dev.platform_data)
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300729 icd->num_formats = ARRAY_SIZE(mt9v022_colour_formats);
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300730 else
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300731 icd->num_formats = 1;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300732 } else {
733 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
734 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300735 icd->formats = mt9v022_monochrome_formats;
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300736 if (mt9v022->client->dev.platform_data)
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300737 icd->num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300738 else
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300739 icd->num_formats = 1;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300740 }
741
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300742 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300743 ret = soc_camera_video_start(icd);
744 if (ret < 0)
745 goto eisis;
746
747 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
748 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
749 "monochrome" : "colour");
750
751 return 0;
752
753eisis:
754ei2c:
755 return ret;
756}
757
758static void mt9v022_video_remove(struct soc_camera_device *icd)
759{
760 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
761
762 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
763 mt9v022->icd.dev.parent, mt9v022->icd.vdev);
764 soc_camera_video_stop(&mt9v022->icd);
765}
766
Jean Delvared2653e92008-04-29 23:11:39 +0200767static int mt9v022_probe(struct i2c_client *client,
768 const struct i2c_device_id *did)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300769{
770 struct mt9v022 *mt9v022;
771 struct soc_camera_device *icd;
772 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
773 struct soc_camera_link *icl = client->dev.platform_data;
774 int ret;
775
776 if (!icl) {
777 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
778 return -EINVAL;
779 }
780
781 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
782 dev_warn(&adapter->dev,
783 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
784 return -EIO;
785 }
786
787 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
788 if (!mt9v022)
789 return -ENOMEM;
790
791 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
792 mt9v022->client = client;
793 i2c_set_clientdata(client, mt9v022);
794
795 icd = &mt9v022->icd;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300796 icd->ops = &mt9v022_ops;
797 icd->control = &client->dev;
798 icd->x_min = 1;
799 icd->y_min = 4;
800 icd->x_current = 1;
801 icd->y_current = 4;
802 icd->width_min = 48;
803 icd->width_max = 752;
804 icd->height_min = 32;
805 icd->height_max = 480;
806 icd->y_skip_top = 1;
807 icd->iface = icl->bus_id;
808 /* Default datawidth - this is the only width this camera (normally)
809 * supports. It is only with extra logic that it can support
810 * other widths. Therefore it seems to be a sensible default. */
811 mt9v022->datawidth = 10;
812
813 ret = bus_switch_request(mt9v022, icl);
814 if (ret)
815 goto eswinit;
816
817 ret = soc_camera_device_register(icd);
818 if (ret)
819 goto eisdr;
820
821 return 0;
822
823eisdr:
824 bus_switch_release(mt9v022);
825eswinit:
826 kfree(mt9v022);
827 return ret;
828}
829
830static int mt9v022_remove(struct i2c_client *client)
831{
832 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
833
834 soc_camera_device_unregister(&mt9v022->icd);
835 bus_switch_release(mt9v022);
836 kfree(mt9v022);
837
838 return 0;
839}
Jean Delvare3760f732008-04-29 23:11:40 +0200840static const struct i2c_device_id mt9v022_id[] = {
841 { "mt9v022", 0 },
842 { }
843};
844MODULE_DEVICE_TABLE(i2c, mt9v022_id);
845
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300846static struct i2c_driver mt9v022_i2c_driver = {
847 .driver = {
848 .name = "mt9v022",
849 },
850 .probe = mt9v022_probe,
851 .remove = mt9v022_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200852 .id_table = mt9v022_id,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300853};
854
855static int __init mt9v022_mod_init(void)
856{
857 return i2c_add_driver(&mt9v022_i2c_driver);
858}
859
860static void __exit mt9v022_mod_exit(void)
861{
862 i2c_del_driver(&mt9v022_i2c_driver);
863}
864
865module_init(mt9v022_mod_init);
866module_exit(mt9v022_mod_exit);
867
868MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
869MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
870MODULE_LICENSE("GPL");