blob: be20747b813875b188ff59aa3a29d62b8fea2d76 [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 Liakhovetskibd73b362008-12-23 05:54:45 -0300276 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300277 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300278 int ret;
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300279 u16 pixclk = 0;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300280
281 /* Only one width bit may be set */
282 if (!is_power_of_2(width_flag))
283 return -EINVAL;
284
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300285 if ((mt9v022->datawidth != 10 && (width_flag == SOCAM_DATAWIDTH_10)) ||
286 (mt9v022->datawidth != 9 && (width_flag == SOCAM_DATAWIDTH_9)) ||
287 (mt9v022->datawidth != 8 && (width_flag == SOCAM_DATAWIDTH_8))) {
288 /* Well, we actually only can do 10 or 8 bits... */
289 if (width_flag == SOCAM_DATAWIDTH_9)
290 return -EINVAL;
291
292 ret = bus_switch_act(mt9v022,
293 width_flag == SOCAM_DATAWIDTH_8);
294 if (ret < 0)
295 return ret;
296
297 mt9v022->datawidth = width_flag == SOCAM_DATAWIDTH_8 ? 8 : 10;
298 }
299
Guennadi Liakhovetskibd73b362008-12-23 05:54:45 -0300300 flags = soc_camera_apply_sensor_flags(icl, flags);
301
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300302 if (flags & SOCAM_PCLK_SAMPLE_RISING)
303 pixclk |= 0x10;
304
305 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
306 pixclk |= 0x1;
307
308 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
309 pixclk |= 0x2;
310
311 ret = reg_write(icd, MT9V022_PIXCLK_FV_LV, pixclk);
312 if (ret < 0)
313 return ret;
314
315 if (!(flags & SOCAM_MASTER))
316 mt9v022->chip_control &= ~0x8;
317
318 ret = reg_write(icd, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
319 if (ret < 0)
320 return ret;
321
322 dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
323 pixclk, mt9v022->chip_control);
324
325 return 0;
326}
327
328static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
329{
330 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
331 unsigned int width_flag = SOCAM_DATAWIDTH_10;
332
333 if (bus_switch_possible(mt9v022))
334 width_flag |= SOCAM_DATAWIDTH_8;
335
336 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
337 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
338 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
339 SOCAM_MASTER | SOCAM_SLAVE |
340 width_flag;
341}
342
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300343static int mt9v022_set_fmt(struct soc_camera_device *icd,
344 __u32 pixfmt, struct v4l2_rect *rect)
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300345{
346 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
347 int ret;
348
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300349 /* The caller provides a supported format, as verified per call to
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300350 * icd->try_fmt(), datawidth is from our supported format list */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300351 switch (pixfmt) {
352 case V4L2_PIX_FMT_GREY:
353 case V4L2_PIX_FMT_Y16:
354 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
355 return -EINVAL;
356 break;
357 case V4L2_PIX_FMT_SBGGR8:
358 case V4L2_PIX_FMT_SBGGR16:
359 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
360 return -EINVAL;
361 break;
362 case 0:
363 /* No format change, only geometry */
364 break;
365 default:
366 return -EINVAL;
367 }
368
369 /* Like in example app. Contradicts the datasheet though */
370 ret = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
371 if (ret >= 0) {
372 if (ret & 1) /* Autoexposure */
373 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
374 rect->height + icd->y_skip_top + 43);
375 else
376 ret = reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
377 rect->height + icd->y_skip_top + 43);
378 }
379 /* Setup frame format: defaults apart from width and height */
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300380 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300381 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300382 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300383 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300384 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300385 /* Default 94, Phytec driver says:
386 * "width + horizontal blank >= 660" */
387 ret = reg_write(icd, MT9V022_HORIZONTAL_BLANKING,
388 rect->width > 660 - 43 ? 43 :
389 660 - rect->width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300390 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300391 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300392 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300393 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300394 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300395 ret = reg_write(icd, MT9V022_WINDOW_HEIGHT,
396 rect->height + icd->y_skip_top);
397
398 if (ret < 0)
399 return ret;
400
401 dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
402
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300403 return 0;
404}
405
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300406static int mt9v022_try_fmt(struct soc_camera_device *icd,
407 struct v4l2_format *f)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300408{
Guennadi Liakhovetski64f59052008-12-18 11:51:55 -0300409 struct v4l2_pix_format *pix = &f->fmt.pix;
410
411 if (pix->height < 32 + icd->y_skip_top)
412 pix->height = 32 + icd->y_skip_top;
413 if (pix->height > 480 + icd->y_skip_top)
414 pix->height = 480 + icd->y_skip_top;
415 if (pix->width < 48)
416 pix->width = 48;
417 if (pix->width > 752)
418 pix->width = 752;
419 pix->width &= ~0x03; /* ? */
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300420
421 return 0;
422}
423
424static int mt9v022_get_chip_id(struct soc_camera_device *icd,
425 struct v4l2_chip_ident *id)
426{
427 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
428
429 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
430 return -EINVAL;
431
432 if (id->match_chip != mt9v022->client->addr)
433 return -ENODEV;
434
435 id->ident = mt9v022->model;
436 id->revision = 0;
437
438 return 0;
439}
440
441#ifdef CONFIG_VIDEO_ADV_DEBUG
442static int mt9v022_get_register(struct soc_camera_device *icd,
443 struct v4l2_register *reg)
444{
445 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
446
447 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
448 return -EINVAL;
449
450 if (reg->match_chip != mt9v022->client->addr)
451 return -ENODEV;
452
453 reg->val = reg_read(icd, reg->reg);
454
455 if (reg->val > 0xffff)
456 return -EIO;
457
458 return 0;
459}
460
461static int mt9v022_set_register(struct soc_camera_device *icd,
462 struct v4l2_register *reg)
463{
464 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
465
466 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
467 return -EINVAL;
468
469 if (reg->match_chip != mt9v022->client->addr)
470 return -ENODEV;
471
472 if (reg_write(icd, reg->reg, reg->val) < 0)
473 return -EIO;
474
475 return 0;
476}
477#endif
478
Adrian Bunk4407a462008-04-28 17:13:51 -0300479static const struct v4l2_queryctrl mt9v022_controls[] = {
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300480 {
481 .id = V4L2_CID_VFLIP,
482 .type = V4L2_CTRL_TYPE_BOOLEAN,
483 .name = "Flip Vertically",
484 .minimum = 0,
485 .maximum = 1,
486 .step = 1,
487 .default_value = 0,
488 }, {
489 .id = V4L2_CID_HFLIP,
490 .type = V4L2_CTRL_TYPE_BOOLEAN,
491 .name = "Flip Horizontally",
492 .minimum = 0,
493 .maximum = 1,
494 .step = 1,
495 .default_value = 0,
496 }, {
497 .id = V4L2_CID_GAIN,
498 .type = V4L2_CTRL_TYPE_INTEGER,
499 .name = "Analog Gain",
500 .minimum = 64,
501 .maximum = 127,
502 .step = 1,
503 .default_value = 64,
504 .flags = V4L2_CTRL_FLAG_SLIDER,
505 }, {
506 .id = V4L2_CID_EXPOSURE,
507 .type = V4L2_CTRL_TYPE_INTEGER,
508 .name = "Exposure",
509 .minimum = 1,
510 .maximum = 255,
511 .step = 1,
512 .default_value = 255,
513 .flags = V4L2_CTRL_FLAG_SLIDER,
514 }, {
515 .id = V4L2_CID_AUTOGAIN,
516 .type = V4L2_CTRL_TYPE_BOOLEAN,
517 .name = "Automatic Gain",
518 .minimum = 0,
519 .maximum = 1,
520 .step = 1,
521 .default_value = 1,
522 }, {
523 .id = V4L2_CID_EXPOSURE_AUTO,
524 .type = V4L2_CTRL_TYPE_BOOLEAN,
525 .name = "Automatic Exposure",
526 .minimum = 0,
527 .maximum = 1,
528 .step = 1,
529 .default_value = 1,
530 }
531};
532
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300533static int mt9v022_video_probe(struct soc_camera_device *);
534static void mt9v022_video_remove(struct soc_camera_device *);
535static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
536static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300537
538static struct soc_camera_ops mt9v022_ops = {
539 .owner = THIS_MODULE,
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300540 .probe = mt9v022_video_probe,
541 .remove = mt9v022_video_remove,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300542 .init = mt9v022_init,
543 .release = mt9v022_release,
544 .start_capture = mt9v022_start_capture,
545 .stop_capture = mt9v022_stop_capture,
Guennadi Liakhovetskid8fac212008-12-01 09:45:21 -0300546 .set_fmt = mt9v022_set_fmt,
547 .try_fmt = mt9v022_try_fmt,
Guennadi Liakhovetskiad5f2e82008-03-07 21:57:18 -0300548 .set_bus_param = mt9v022_set_bus_param,
549 .query_bus_param = mt9v022_query_bus_param,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300550 .controls = mt9v022_controls,
551 .num_controls = ARRAY_SIZE(mt9v022_controls),
552 .get_control = mt9v022_get_control,
553 .set_control = mt9v022_set_control,
554 .get_chip_id = mt9v022_get_chip_id,
555#ifdef CONFIG_VIDEO_ADV_DEBUG
556 .get_register = mt9v022_get_register,
557 .set_register = mt9v022_set_register,
558#endif
559};
560
561static int mt9v022_get_control(struct soc_camera_device *icd,
562 struct v4l2_control *ctrl)
563{
564 int data;
565
566 switch (ctrl->id) {
567 case V4L2_CID_VFLIP:
568 data = reg_read(icd, MT9V022_READ_MODE);
569 if (data < 0)
570 return -EIO;
571 ctrl->value = !!(data & 0x10);
572 break;
573 case V4L2_CID_HFLIP:
574 data = reg_read(icd, MT9V022_READ_MODE);
575 if (data < 0)
576 return -EIO;
577 ctrl->value = !!(data & 0x20);
578 break;
579 case V4L2_CID_EXPOSURE_AUTO:
580 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
581 if (data < 0)
582 return -EIO;
583 ctrl->value = !!(data & 0x1);
584 break;
585 case V4L2_CID_AUTOGAIN:
586 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
587 if (data < 0)
588 return -EIO;
589 ctrl->value = !!(data & 0x2);
590 break;
591 }
592 return 0;
593}
594
595static int mt9v022_set_control(struct soc_camera_device *icd,
596 struct v4l2_control *ctrl)
597{
598 int data;
599 const struct v4l2_queryctrl *qctrl;
600
601 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
602
603 if (!qctrl)
604 return -EINVAL;
605
606 switch (ctrl->id) {
607 case V4L2_CID_VFLIP:
608 if (ctrl->value)
609 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
610 else
611 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
612 if (data < 0)
613 return -EIO;
614 break;
615 case V4L2_CID_HFLIP:
616 if (ctrl->value)
617 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
618 else
619 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
620 if (data < 0)
621 return -EIO;
622 break;
623 case V4L2_CID_GAIN:
624 /* mt9v022 has minimum == 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 /* Datasheet says 16 to 64. autogain only works properly
630 * after setting gain to maximum 14. Larger values
631 * produce "white fly" noise effect. On the whole,
632 * manually setting analog gain does no good. */
633 unsigned long gain = ((ctrl->value - qctrl->minimum) *
634 10 + range / 2) / range + 4;
635 if (gain >= 32)
636 gain &= ~1;
637 /* The user wants to set gain manually, hope, she
638 * knows, what she's doing... Switch AGC off. */
639
640 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
641 return -EIO;
642
643 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
644 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
645 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
646 return -EIO;
647 icd->gain = ctrl->value;
648 }
649 break;
650 case V4L2_CID_EXPOSURE:
651 /* mt9v022 has maximum == default */
652 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
653 return -EINVAL;
654 else {
655 unsigned long range = qctrl->maximum - qctrl->minimum;
656 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
657 479 + range / 2) / range + 1;
658 /* The user wants to set shutter width manually, hope,
659 * she knows, what she's doing... Switch AEC off. */
660
661 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
662 return -EIO;
663
664 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
665 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
666 shutter);
667 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
668 shutter) < 0)
669 return -EIO;
670 icd->exposure = ctrl->value;
671 }
672 break;
673 case V4L2_CID_AUTOGAIN:
674 if (ctrl->value)
675 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
676 else
677 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
678 if (data < 0)
679 return -EIO;
680 break;
681 case V4L2_CID_EXPOSURE_AUTO:
682 if (ctrl->value)
683 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
684 else
685 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
686 if (data < 0)
687 return -EIO;
688 break;
689 }
690 return 0;
691}
692
693/* Interface active, can use i2c. If it fails, it can indeed mean, that
694 * this wasn't our capture interface, so, we wait for the right one */
695static int mt9v022_video_probe(struct soc_camera_device *icd)
696{
697 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
Guennadi Liakhovetski297a7ef2008-12-17 14:05:38 -0300698 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300699 s32 data;
700 int ret;
701
702 if (!icd->dev.parent ||
703 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
704 return -ENODEV;
705
706 /* Read out the chip version register */
707 data = reg_read(icd, MT9V022_CHIP_VERSION);
708
709 /* must be 0x1311 or 0x1313 */
710 if (data != 0x1311 && data != 0x1313) {
711 ret = -ENODEV;
712 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
713 data);
714 goto ei2c;
715 }
716
717 /* Soft reset */
718 ret = reg_write(icd, MT9V022_RESET, 1);
719 if (ret < 0)
720 goto ei2c;
721 /* 15 clock cycles */
722 udelay(200);
723 if (reg_read(icd, MT9V022_RESET)) {
724 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
725 goto ei2c;
726 }
727
728 /* Set monochrome or colour sensor type */
729 if (sensor_type && (!strcmp("colour", sensor_type) ||
730 !strcmp("color", sensor_type))) {
731 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
732 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300733 icd->formats = mt9v022_colour_formats;
Guennadi Liakhovetski297a7ef2008-12-17 14:05:38 -0300734 if (gpio_is_valid(icl->gpio))
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300735 icd->num_formats = ARRAY_SIZE(mt9v022_colour_formats);
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300736 else
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300737 icd->num_formats = 1;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300738 } else {
739 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
740 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300741 icd->formats = mt9v022_monochrome_formats;
Guennadi Liakhovetski297a7ef2008-12-17 14:05:38 -0300742 if (gpio_is_valid(icl->gpio))
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300743 icd->num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
Guennadi Liakhovetskibb55de32008-04-22 14:45:13 -0300744 else
Guennadi Liakhovetski26f1b942008-03-24 12:18:36 -0300745 icd->num_formats = 1;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300746 }
747
Guennadi Liakhovetski11211642008-08-14 12:03:18 -0300748 if (!ret)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300749 ret = soc_camera_video_start(icd);
750 if (ret < 0)
751 goto eisis;
752
753 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
754 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
755 "monochrome" : "colour");
756
757 return 0;
758
759eisis:
760ei2c:
761 return ret;
762}
763
764static void mt9v022_video_remove(struct soc_camera_device *icd)
765{
766 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
767
768 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
769 mt9v022->icd.dev.parent, mt9v022->icd.vdev);
770 soc_camera_video_stop(&mt9v022->icd);
771}
772
Jean Delvared2653e92008-04-29 23:11:39 +0200773static int mt9v022_probe(struct i2c_client *client,
774 const struct i2c_device_id *did)
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300775{
776 struct mt9v022 *mt9v022;
777 struct soc_camera_device *icd;
778 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
779 struct soc_camera_link *icl = client->dev.platform_data;
780 int ret;
781
782 if (!icl) {
783 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
784 return -EINVAL;
785 }
786
787 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
788 dev_warn(&adapter->dev,
789 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
790 return -EIO;
791 }
792
793 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
794 if (!mt9v022)
795 return -ENOMEM;
796
797 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
798 mt9v022->client = client;
799 i2c_set_clientdata(client, mt9v022);
800
801 icd = &mt9v022->icd;
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300802 icd->ops = &mt9v022_ops;
803 icd->control = &client->dev;
804 icd->x_min = 1;
805 icd->y_min = 4;
806 icd->x_current = 1;
807 icd->y_current = 4;
808 icd->width_min = 48;
809 icd->width_max = 752;
810 icd->height_min = 32;
811 icd->height_max = 480;
812 icd->y_skip_top = 1;
813 icd->iface = icl->bus_id;
814 /* Default datawidth - this is the only width this camera (normally)
815 * supports. It is only with extra logic that it can support
816 * other widths. Therefore it seems to be a sensible default. */
817 mt9v022->datawidth = 10;
818
819 ret = bus_switch_request(mt9v022, icl);
820 if (ret)
821 goto eswinit;
822
823 ret = soc_camera_device_register(icd);
824 if (ret)
825 goto eisdr;
826
827 return 0;
828
829eisdr:
830 bus_switch_release(mt9v022);
831eswinit:
832 kfree(mt9v022);
833 return ret;
834}
835
836static int mt9v022_remove(struct i2c_client *client)
837{
838 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
839
840 soc_camera_device_unregister(&mt9v022->icd);
841 bus_switch_release(mt9v022);
842 kfree(mt9v022);
843
844 return 0;
845}
Jean Delvare3760f732008-04-29 23:11:40 +0200846static const struct i2c_device_id mt9v022_id[] = {
847 { "mt9v022", 0 },
848 { }
849};
850MODULE_DEVICE_TABLE(i2c, mt9v022_id);
851
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300852static struct i2c_driver mt9v022_i2c_driver = {
853 .driver = {
854 .name = "mt9v022",
855 },
856 .probe = mt9v022_probe,
857 .remove = mt9v022_remove,
Jean Delvare3760f732008-04-29 23:11:40 +0200858 .id_table = mt9v022_id,
Guennadi Liakhovetski7397bfbe2008-04-22 14:42:04 -0300859};
860
861static int __init mt9v022_mod_init(void)
862{
863 return i2c_add_driver(&mt9v022_i2c_driver);
864}
865
866static void __exit mt9v022_mod_exit(void)
867{
868 i2c_del_driver(&mt9v022_i2c_driver);
869}
870
871module_init(mt9v022_mod_init);
872module_exit(mt9v022_mod_exit);
873
874MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
875MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
876MODULE_LICENSE("GPL");