]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/media/video/mt9v022.c
V4L/DVB (10079): sh_mobile_ceu: use new pixel format translation code
[net-next-2.6.git] / drivers / media / video / mt9v022.c
CommitLineData
7397bfbe
GL
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>
91e64c88 16#include <linux/gpio.h>
7397bfbe
GL
17
18#include <media/v4l2-common.h>
19#include <media/v4l2-chip-ident.h>
20#include <media/soc_camera.h>
21
7397bfbe
GL
22/* 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);
61a2d07d 28MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
7397bfbe
GL
29
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
bb55de3b
GL
59static const struct soc_camera_data_format mt9v022_colour_formats[] = {
60 /* Order important: first natively supported,
61 * second supported with a GPIO extender */
7397bfbe 62 {
bb55de3b 63 .name = "Bayer (sRGB) 10 bit",
7397bfbe
GL
64 .depth = 10,
65 .fourcc = V4L2_PIX_FMT_SBGGR16,
66 .colorspace = V4L2_COLORSPACE_SRGB,
67 }, {
bb55de3b
GL
68 .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 {
7397bfbe
GL
78 .name = "Monochrome 10 bit",
79 .depth = 10,
80 .fourcc = V4L2_PIX_FMT_Y16,
81 }, {
82 .name = "Monochrome 8 bit",
83 .depth = 8,
84 .fourcc = V4L2_PIX_FMT_GREY,
85 },
86};
87
88struct mt9v022 {
89 struct i2c_client *client;
90 struct soc_camera_device icd;
7fb0fd05 91 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
7397bfbe
GL
92 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);
81034663 137 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
7397bfbe
GL
138 int ret;
139
81034663
SH
140 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
7397bfbe
GL
156 /* 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);
11211641
GL
161 if (!ret)
162 ret = reg_write(icd, MT9V022_READ_MODE, 0x300);
7397bfbe
GL
163
164 /* All defaults */
11211641 165 if (!ret)
7397bfbe
GL
166 /* AEC, AGC on */
167 ret = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x3);
11211641 168 if (!ret)
7397bfbe 169 ret = reg_write(icd, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
11211641 170 if (!ret)
7397bfbe
GL
171 /* default - auto */
172 ret = reg_clear(icd, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
11211641 173 if (!ret)
7397bfbe
GL
174 ret = reg_write(icd, MT9V022_DIGITAL_TEST_PATTERN, 0);
175
11211641 176 return ret;
7397bfbe
GL
177}
178
179static int mt9v022_release(struct soc_camera_device *icd)
180{
81034663
SH
181 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
7397bfbe
GL
187 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
b4333a3b 218 if (gpio_is_valid(gpio)) {
7397bfbe
GL
219 /* 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
b4333a3b 237 mt9v022->switch_gpio = -EINVAL;
7397bfbe
GL
238#endif
239 return 0;
240}
241
242static void bus_switch_release(struct mt9v022 *mt9v022)
243{
244#ifdef CONFIG_MT9V022_PCA9536_SWITCH
b4333a3b 245 if (gpio_is_valid(mt9v022->switch_gpio))
7397bfbe
GL
246 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
b4333a3b 253 if (!gpio_is_valid(mt9v022->switch_gpio))
7397bfbe
GL
254 return -ENODEV;
255
256 gpio_set_value_cansleep(mt9v022->switch_gpio, go8bit);
257 return 0;
258#else
259 return -ENODEV;
260#endif
261}
262
ad5f2e85
GL
263static 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)
7397bfbe
GL
274{
275 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
bd73b36f 276 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
ad5f2e85 277 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
7397bfbe 278 int ret;
ad5f2e85 279 u16 pixclk = 0;
7397bfbe
GL
280
281 /* Only one width bit may be set */
282 if (!is_power_of_2(width_flag))
283 return -EINVAL;
284
ad5f2e85
GL
285 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
bd73b36f
GL
300 flags = soc_camera_apply_sensor_flags(icl, flags);
301
ad5f2e85
GL
302 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
d8fac217
GL
343static int mt9v022_set_fmt(struct soc_camera_device *icd,
344 __u32 pixfmt, struct v4l2_rect *rect)
ad5f2e85
GL
345{
346 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
347 int ret;
348
7397bfbe 349 /* The caller provides a supported format, as verified per call to
d8fac217 350 * icd->try_fmt(), datawidth is from our supported format list */
7397bfbe
GL
351 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 */
11211641 380 if (!ret)
7397bfbe 381 ret = reg_write(icd, MT9V022_COLUMN_START, rect->left);
11211641 382 if (!ret)
7397bfbe 383 ret = reg_write(icd, MT9V022_ROW_START, rect->top);
11211641 384 if (!ret)
7397bfbe
GL
385 /* 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);
11211641 390 if (!ret)
7397bfbe 391 ret = reg_write(icd, MT9V022_VERTICAL_BLANKING, 45);
11211641 392 if (!ret)
7397bfbe 393 ret = reg_write(icd, MT9V022_WINDOW_WIDTH, rect->width);
11211641 394 if (!ret)
7397bfbe
GL
395 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
7397bfbe
GL
403 return 0;
404}
405
d8fac217
GL
406static int mt9v022_try_fmt(struct soc_camera_device *icd,
407 struct v4l2_format *f)
7397bfbe
GL
408{
409 if (f->fmt.pix.height < 32 + icd->y_skip_top)
410 f->fmt.pix.height = 32 + icd->y_skip_top;
411 if (f->fmt.pix.height > 480 + icd->y_skip_top)
412 f->fmt.pix.height = 480 + icd->y_skip_top;
413 if (f->fmt.pix.width < 48)
414 f->fmt.pix.width = 48;
415 if (f->fmt.pix.width > 752)
416 f->fmt.pix.width = 752;
417 f->fmt.pix.width &= ~0x03; /* ? */
418
419 return 0;
420}
421
422static int mt9v022_get_chip_id(struct soc_camera_device *icd,
423 struct v4l2_chip_ident *id)
424{
425 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
426
427 if (id->match_type != V4L2_CHIP_MATCH_I2C_ADDR)
428 return -EINVAL;
429
430 if (id->match_chip != mt9v022->client->addr)
431 return -ENODEV;
432
433 id->ident = mt9v022->model;
434 id->revision = 0;
435
436 return 0;
437}
438
439#ifdef CONFIG_VIDEO_ADV_DEBUG
440static int mt9v022_get_register(struct soc_camera_device *icd,
441 struct v4l2_register *reg)
442{
443 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
444
445 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
446 return -EINVAL;
447
448 if (reg->match_chip != mt9v022->client->addr)
449 return -ENODEV;
450
451 reg->val = reg_read(icd, reg->reg);
452
453 if (reg->val > 0xffff)
454 return -EIO;
455
456 return 0;
457}
458
459static int mt9v022_set_register(struct soc_camera_device *icd,
460 struct v4l2_register *reg)
461{
462 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
463
464 if (reg->match_type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
465 return -EINVAL;
466
467 if (reg->match_chip != mt9v022->client->addr)
468 return -ENODEV;
469
470 if (reg_write(icd, reg->reg, reg->val) < 0)
471 return -EIO;
472
473 return 0;
474}
475#endif
476
4407a463 477static const struct v4l2_queryctrl mt9v022_controls[] = {
7397bfbe
GL
478 {
479 .id = V4L2_CID_VFLIP,
480 .type = V4L2_CTRL_TYPE_BOOLEAN,
481 .name = "Flip Vertically",
482 .minimum = 0,
483 .maximum = 1,
484 .step = 1,
485 .default_value = 0,
486 }, {
487 .id = V4L2_CID_HFLIP,
488 .type = V4L2_CTRL_TYPE_BOOLEAN,
489 .name = "Flip Horizontally",
490 .minimum = 0,
491 .maximum = 1,
492 .step = 1,
493 .default_value = 0,
494 }, {
495 .id = V4L2_CID_GAIN,
496 .type = V4L2_CTRL_TYPE_INTEGER,
497 .name = "Analog Gain",
498 .minimum = 64,
499 .maximum = 127,
500 .step = 1,
501 .default_value = 64,
502 .flags = V4L2_CTRL_FLAG_SLIDER,
503 }, {
504 .id = V4L2_CID_EXPOSURE,
505 .type = V4L2_CTRL_TYPE_INTEGER,
506 .name = "Exposure",
507 .minimum = 1,
508 .maximum = 255,
509 .step = 1,
510 .default_value = 255,
511 .flags = V4L2_CTRL_FLAG_SLIDER,
512 }, {
513 .id = V4L2_CID_AUTOGAIN,
514 .type = V4L2_CTRL_TYPE_BOOLEAN,
515 .name = "Automatic Gain",
516 .minimum = 0,
517 .maximum = 1,
518 .step = 1,
519 .default_value = 1,
520 }, {
521 .id = V4L2_CID_EXPOSURE_AUTO,
522 .type = V4L2_CTRL_TYPE_BOOLEAN,
523 .name = "Automatic Exposure",
524 .minimum = 0,
525 .maximum = 1,
526 .step = 1,
527 .default_value = 1,
528 }
529};
530
26f1b942
GL
531static int mt9v022_video_probe(struct soc_camera_device *);
532static void mt9v022_video_remove(struct soc_camera_device *);
533static int mt9v022_get_control(struct soc_camera_device *, struct v4l2_control *);
534static int mt9v022_set_control(struct soc_camera_device *, struct v4l2_control *);
7397bfbe
GL
535
536static struct soc_camera_ops mt9v022_ops = {
537 .owner = THIS_MODULE,
26f1b942
GL
538 .probe = mt9v022_video_probe,
539 .remove = mt9v022_video_remove,
7397bfbe
GL
540 .init = mt9v022_init,
541 .release = mt9v022_release,
542 .start_capture = mt9v022_start_capture,
543 .stop_capture = mt9v022_stop_capture,
d8fac217
GL
544 .set_fmt = mt9v022_set_fmt,
545 .try_fmt = mt9v022_try_fmt,
ad5f2e85
GL
546 .set_bus_param = mt9v022_set_bus_param,
547 .query_bus_param = mt9v022_query_bus_param,
7397bfbe
GL
548 .controls = mt9v022_controls,
549 .num_controls = ARRAY_SIZE(mt9v022_controls),
550 .get_control = mt9v022_get_control,
551 .set_control = mt9v022_set_control,
552 .get_chip_id = mt9v022_get_chip_id,
553#ifdef CONFIG_VIDEO_ADV_DEBUG
554 .get_register = mt9v022_get_register,
555 .set_register = mt9v022_set_register,
556#endif
557};
558
559static int mt9v022_get_control(struct soc_camera_device *icd,
560 struct v4l2_control *ctrl)
561{
562 int data;
563
564 switch (ctrl->id) {
565 case V4L2_CID_VFLIP:
566 data = reg_read(icd, MT9V022_READ_MODE);
567 if (data < 0)
568 return -EIO;
569 ctrl->value = !!(data & 0x10);
570 break;
571 case V4L2_CID_HFLIP:
572 data = reg_read(icd, MT9V022_READ_MODE);
573 if (data < 0)
574 return -EIO;
575 ctrl->value = !!(data & 0x20);
576 break;
577 case V4L2_CID_EXPOSURE_AUTO:
578 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
579 if (data < 0)
580 return -EIO;
581 ctrl->value = !!(data & 0x1);
582 break;
583 case V4L2_CID_AUTOGAIN:
584 data = reg_read(icd, MT9V022_AEC_AGC_ENABLE);
585 if (data < 0)
586 return -EIO;
587 ctrl->value = !!(data & 0x2);
588 break;
589 }
590 return 0;
591}
592
593static int mt9v022_set_control(struct soc_camera_device *icd,
594 struct v4l2_control *ctrl)
595{
596 int data;
597 const struct v4l2_queryctrl *qctrl;
598
599 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
600
601 if (!qctrl)
602 return -EINVAL;
603
604 switch (ctrl->id) {
605 case V4L2_CID_VFLIP:
606 if (ctrl->value)
607 data = reg_set(icd, MT9V022_READ_MODE, 0x10);
608 else
609 data = reg_clear(icd, MT9V022_READ_MODE, 0x10);
610 if (data < 0)
611 return -EIO;
612 break;
613 case V4L2_CID_HFLIP:
614 if (ctrl->value)
615 data = reg_set(icd, MT9V022_READ_MODE, 0x20);
616 else
617 data = reg_clear(icd, MT9V022_READ_MODE, 0x20);
618 if (data < 0)
619 return -EIO;
620 break;
621 case V4L2_CID_GAIN:
622 /* mt9v022 has minimum == default */
623 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
624 return -EINVAL;
625 else {
626 unsigned long range = qctrl->maximum - qctrl->minimum;
627 /* Datasheet says 16 to 64. autogain only works properly
628 * after setting gain to maximum 14. Larger values
629 * produce "white fly" noise effect. On the whole,
630 * manually setting analog gain does no good. */
631 unsigned long gain = ((ctrl->value - qctrl->minimum) *
632 10 + range / 2) / range + 4;
633 if (gain >= 32)
634 gain &= ~1;
635 /* The user wants to set gain manually, hope, she
636 * knows, what she's doing... Switch AGC off. */
637
638 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
639 return -EIO;
640
641 dev_info(&icd->dev, "Setting gain from %d to %lu\n",
642 reg_read(icd, MT9V022_ANALOG_GAIN), gain);
643 if (reg_write(icd, MT9V022_ANALOG_GAIN, gain) < 0)
644 return -EIO;
645 icd->gain = ctrl->value;
646 }
647 break;
648 case V4L2_CID_EXPOSURE:
649 /* mt9v022 has maximum == default */
650 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
651 return -EINVAL;
652 else {
653 unsigned long range = qctrl->maximum - qctrl->minimum;
654 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
655 479 + range / 2) / range + 1;
656 /* The user wants to set shutter width manually, hope,
657 * she knows, what she's doing... Switch AEC off. */
658
659 if (reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
660 return -EIO;
661
662 dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
663 reg_read(icd, MT9V022_TOTAL_SHUTTER_WIDTH),
664 shutter);
665 if (reg_write(icd, MT9V022_TOTAL_SHUTTER_WIDTH,
666 shutter) < 0)
667 return -EIO;
668 icd->exposure = ctrl->value;
669 }
670 break;
671 case V4L2_CID_AUTOGAIN:
672 if (ctrl->value)
673 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
674 else
675 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x2);
676 if (data < 0)
677 return -EIO;
678 break;
679 case V4L2_CID_EXPOSURE_AUTO:
680 if (ctrl->value)
681 data = reg_set(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
682 else
683 data = reg_clear(icd, MT9V022_AEC_AGC_ENABLE, 0x1);
684 if (data < 0)
685 return -EIO;
686 break;
687 }
688 return 0;
689}
690
691/* Interface active, can use i2c. If it fails, it can indeed mean, that
692 * this wasn't our capture interface, so, we wait for the right one */
693static int mt9v022_video_probe(struct soc_camera_device *icd)
694{
695 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
297a7ef7 696 struct soc_camera_link *icl = mt9v022->client->dev.platform_data;
7397bfbe
GL
697 s32 data;
698 int ret;
699
700 if (!icd->dev.parent ||
701 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
702 return -ENODEV;
703
704 /* Read out the chip version register */
705 data = reg_read(icd, MT9V022_CHIP_VERSION);
706
707 /* must be 0x1311 or 0x1313 */
708 if (data != 0x1311 && data != 0x1313) {
709 ret = -ENODEV;
710 dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
711 data);
712 goto ei2c;
713 }
714
715 /* Soft reset */
716 ret = reg_write(icd, MT9V022_RESET, 1);
717 if (ret < 0)
718 goto ei2c;
719 /* 15 clock cycles */
720 udelay(200);
721 if (reg_read(icd, MT9V022_RESET)) {
722 dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
723 goto ei2c;
724 }
725
726 /* Set monochrome or colour sensor type */
727 if (sensor_type && (!strcmp("colour", sensor_type) ||
728 !strcmp("color", sensor_type))) {
729 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
730 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
26f1b942 731 icd->formats = mt9v022_colour_formats;
297a7ef7 732 if (gpio_is_valid(icl->gpio))
26f1b942 733 icd->num_formats = ARRAY_SIZE(mt9v022_colour_formats);
bb55de3b 734 else
26f1b942 735 icd->num_formats = 1;
7397bfbe
GL
736 } else {
737 ret = reg_write(icd, MT9V022_PIXEL_OPERATION_MODE, 0x11);
738 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
26f1b942 739 icd->formats = mt9v022_monochrome_formats;
297a7ef7 740 if (gpio_is_valid(icl->gpio))
26f1b942 741 icd->num_formats = ARRAY_SIZE(mt9v022_monochrome_formats);
bb55de3b 742 else
26f1b942 743 icd->num_formats = 1;
7397bfbe
GL
744 }
745
11211641 746 if (!ret)
7397bfbe
GL
747 ret = soc_camera_video_start(icd);
748 if (ret < 0)
749 goto eisis;
750
751 dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
752 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
753 "monochrome" : "colour");
754
755 return 0;
756
757eisis:
758ei2c:
759 return ret;
760}
761
762static void mt9v022_video_remove(struct soc_camera_device *icd)
763{
764 struct mt9v022 *mt9v022 = container_of(icd, struct mt9v022, icd);
765
766 dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9v022->client->addr,
767 mt9v022->icd.dev.parent, mt9v022->icd.vdev);
768 soc_camera_video_stop(&mt9v022->icd);
769}
770
d2653e92
JD
771static int mt9v022_probe(struct i2c_client *client,
772 const struct i2c_device_id *did)
7397bfbe
GL
773{
774 struct mt9v022 *mt9v022;
775 struct soc_camera_device *icd;
776 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
777 struct soc_camera_link *icl = client->dev.platform_data;
778 int ret;
779
780 if (!icl) {
781 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
782 return -EINVAL;
783 }
784
785 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
786 dev_warn(&adapter->dev,
787 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
788 return -EIO;
789 }
790
791 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
792 if (!mt9v022)
793 return -ENOMEM;
794
795 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
796 mt9v022->client = client;
797 i2c_set_clientdata(client, mt9v022);
798
799 icd = &mt9v022->icd;
7397bfbe
GL
800 icd->ops = &mt9v022_ops;
801 icd->control = &client->dev;
802 icd->x_min = 1;
803 icd->y_min = 4;
804 icd->x_current = 1;
805 icd->y_current = 4;
806 icd->width_min = 48;
807 icd->width_max = 752;
808 icd->height_min = 32;
809 icd->height_max = 480;
810 icd->y_skip_top = 1;
811 icd->iface = icl->bus_id;
812 /* Default datawidth - this is the only width this camera (normally)
813 * supports. It is only with extra logic that it can support
814 * other widths. Therefore it seems to be a sensible default. */
815 mt9v022->datawidth = 10;
816
817 ret = bus_switch_request(mt9v022, icl);
818 if (ret)
819 goto eswinit;
820
821 ret = soc_camera_device_register(icd);
822 if (ret)
823 goto eisdr;
824
825 return 0;
826
827eisdr:
828 bus_switch_release(mt9v022);
829eswinit:
830 kfree(mt9v022);
831 return ret;
832}
833
834static int mt9v022_remove(struct i2c_client *client)
835{
836 struct mt9v022 *mt9v022 = i2c_get_clientdata(client);
837
838 soc_camera_device_unregister(&mt9v022->icd);
839 bus_switch_release(mt9v022);
840 kfree(mt9v022);
841
842 return 0;
843}
3760f736
JD
844static const struct i2c_device_id mt9v022_id[] = {
845 { "mt9v022", 0 },
846 { }
847};
848MODULE_DEVICE_TABLE(i2c, mt9v022_id);
849
7397bfbe
GL
850static struct i2c_driver mt9v022_i2c_driver = {
851 .driver = {
852 .name = "mt9v022",
853 },
854 .probe = mt9v022_probe,
855 .remove = mt9v022_remove,
3760f736 856 .id_table = mt9v022_id,
7397bfbe
GL
857};
858
859static int __init mt9v022_mod_init(void)
860{
861 return i2c_add_driver(&mt9v022_i2c_driver);
862}
863
864static void __exit mt9v022_mod_exit(void)
865{
866 i2c_del_driver(&mt9v022_i2c_driver);
867}
868
869module_init(mt9v022_mod_init);
870module_exit(mt9v022_mod_exit);
871
872MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
873MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
874MODULE_LICENSE("GPL");