]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/media/video/mt9v022.c
35ea0ddd0715643dc839d7ab64243483fdcb938d
[mirror_ubuntu-artful-kernel.git] / drivers / media / video / mt9v022.c
1 /*
2 * Driver for MT9V022 CMOS Image Sensor from Micron
3 *
4 * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11 #include <linux/videodev2.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/delay.h>
15 #include <linux/log2.h>
16
17 #include <media/v4l2-subdev.h>
18 #include <media/v4l2-chip-ident.h>
19 #include <media/soc_camera.h>
20
21 /* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
22 * The platform has to define ctruct i2c_board_info objects and link to them
23 * from struct soc_camera_link */
24
25 static char *sensor_type;
26 module_param(sensor_type, charp, S_IRUGO);
27 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
28
29 /* mt9v022 selected register addresses */
30 #define MT9V022_CHIP_VERSION 0x00
31 #define MT9V022_COLUMN_START 0x01
32 #define MT9V022_ROW_START 0x02
33 #define MT9V022_WINDOW_HEIGHT 0x03
34 #define MT9V022_WINDOW_WIDTH 0x04
35 #define MT9V022_HORIZONTAL_BLANKING 0x05
36 #define MT9V022_VERTICAL_BLANKING 0x06
37 #define MT9V022_CHIP_CONTROL 0x07
38 #define MT9V022_SHUTTER_WIDTH1 0x08
39 #define MT9V022_SHUTTER_WIDTH2 0x09
40 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
41 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
42 #define MT9V022_RESET 0x0c
43 #define MT9V022_READ_MODE 0x0d
44 #define MT9V022_MONITOR_MODE 0x0e
45 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
46 #define MT9V022_LED_OUT_CONTROL 0x1b
47 #define MT9V022_ADC_MODE_CONTROL 0x1c
48 #define MT9V022_ANALOG_GAIN 0x34
49 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
50 #define MT9V022_PIXCLK_FV_LV 0x74
51 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
52 #define MT9V022_AEC_AGC_ENABLE 0xAF
53 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
54
55 /* Progressive scan, master, defaults */
56 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
57
58 #define MT9V022_MAX_WIDTH 752
59 #define MT9V022_MAX_HEIGHT 480
60 #define MT9V022_MIN_WIDTH 48
61 #define MT9V022_MIN_HEIGHT 32
62 #define MT9V022_COLUMN_SKIP 1
63 #define MT9V022_ROW_SKIP 4
64
65 static const struct soc_camera_data_format mt9v022_colour_formats[] = {
66 /* Order important: first natively supported,
67 * second supported with a GPIO extender */
68 {
69 .name = "Bayer (sRGB) 10 bit",
70 .depth = 10,
71 .fourcc = V4L2_PIX_FMT_SBGGR16,
72 .colorspace = V4L2_COLORSPACE_SRGB,
73 }, {
74 .name = "Bayer (sRGB) 8 bit",
75 .depth = 8,
76 .fourcc = V4L2_PIX_FMT_SBGGR8,
77 .colorspace = V4L2_COLORSPACE_SRGB,
78 }
79 };
80
81 static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
82 /* Order important - see above */
83 {
84 .name = "Monochrome 10 bit",
85 .depth = 10,
86 .fourcc = V4L2_PIX_FMT_Y16,
87 }, {
88 .name = "Monochrome 8 bit",
89 .depth = 8,
90 .fourcc = V4L2_PIX_FMT_GREY,
91 },
92 };
93
94 struct mt9v022 {
95 struct v4l2_subdev subdev;
96 struct v4l2_rect rect; /* Sensor window */
97 __u32 fourcc;
98 int model; /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
99 u16 chip_control;
100 };
101
102 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
103 {
104 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
105 }
106
107 static int reg_read(struct i2c_client *client, const u8 reg)
108 {
109 s32 data = i2c_smbus_read_word_data(client, reg);
110 return data < 0 ? data : swab16(data);
111 }
112
113 static int reg_write(struct i2c_client *client, const u8 reg,
114 const u16 data)
115 {
116 return i2c_smbus_write_word_data(client, reg, swab16(data));
117 }
118
119 static int reg_set(struct i2c_client *client, const u8 reg,
120 const u16 data)
121 {
122 int ret;
123
124 ret = reg_read(client, reg);
125 if (ret < 0)
126 return ret;
127 return reg_write(client, reg, ret | data);
128 }
129
130 static int reg_clear(struct i2c_client *client, const u8 reg,
131 const u16 data)
132 {
133 int ret;
134
135 ret = reg_read(client, reg);
136 if (ret < 0)
137 return ret;
138 return reg_write(client, reg, ret & ~data);
139 }
140
141 static int mt9v022_init(struct soc_camera_device *icd)
142 {
143 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
144 struct mt9v022 *mt9v022 = to_mt9v022(client);
145 int ret;
146
147 /* Almost the default mode: master, parallel, simultaneous, and an
148 * undocumented bit 0x200, which is present in table 7, but not in 8,
149 * plus snapshot mode to disable scan for now */
150 mt9v022->chip_control |= 0x10;
151 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
152 if (!ret)
153 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
154
155 /* All defaults */
156 if (!ret)
157 /* AEC, AGC on */
158 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
159 if (!ret)
160 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
161 if (!ret)
162 /* default - auto */
163 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
164 if (!ret)
165 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
166
167 return ret;
168 }
169
170 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
171 {
172 struct i2c_client *client = sd->priv;
173 struct mt9v022 *mt9v022 = to_mt9v022(client);
174
175 if (enable)
176 /* Switch to master "normal" mode */
177 mt9v022->chip_control &= ~0x10;
178 else
179 /* Switch to snapshot mode */
180 mt9v022->chip_control |= 0x10;
181
182 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
183 return -EIO;
184 return 0;
185 }
186
187 static int mt9v022_set_bus_param(struct soc_camera_device *icd,
188 unsigned long flags)
189 {
190 struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
191 struct mt9v022 *mt9v022 = to_mt9v022(client);
192 struct soc_camera_link *icl = to_soc_camera_link(icd);
193 unsigned int width_flag = flags & SOCAM_DATAWIDTH_MASK;
194 int ret;
195 u16 pixclk = 0;
196
197 /* Only one width bit may be set */
198 if (!is_power_of_2(width_flag))
199 return -EINVAL;
200
201 if (icl->set_bus_param) {
202 ret = icl->set_bus_param(icl, width_flag);
203 if (ret)
204 return ret;
205 } else {
206 /*
207 * Without board specific bus width settings we only support the
208 * sensors native bus width
209 */
210 if (width_flag != SOCAM_DATAWIDTH_10)
211 return -EINVAL;
212 }
213
214 flags = soc_camera_apply_sensor_flags(icl, flags);
215
216 if (flags & SOCAM_PCLK_SAMPLE_RISING)
217 pixclk |= 0x10;
218
219 if (!(flags & SOCAM_HSYNC_ACTIVE_HIGH))
220 pixclk |= 0x1;
221
222 if (!(flags & SOCAM_VSYNC_ACTIVE_HIGH))
223 pixclk |= 0x2;
224
225 ret = reg_write(client, MT9V022_PIXCLK_FV_LV, pixclk);
226 if (ret < 0)
227 return ret;
228
229 if (!(flags & SOCAM_MASTER))
230 mt9v022->chip_control &= ~0x8;
231
232 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
233 if (ret < 0)
234 return ret;
235
236 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
237 pixclk, mt9v022->chip_control);
238
239 return 0;
240 }
241
242 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
243 {
244 struct soc_camera_link *icl = to_soc_camera_link(icd);
245 unsigned int width_flag;
246
247 if (icl->query_bus_param)
248 width_flag = icl->query_bus_param(icl) &
249 SOCAM_DATAWIDTH_MASK;
250 else
251 width_flag = SOCAM_DATAWIDTH_10;
252
253 return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
254 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
255 SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
256 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
257 width_flag;
258 }
259
260 static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
261 {
262 struct i2c_client *client = sd->priv;
263 struct mt9v022 *mt9v022 = to_mt9v022(client);
264 struct v4l2_rect rect = a->c;
265 struct soc_camera_device *icd = client->dev.platform_data;
266 int ret;
267
268 /* Bayer format - even size lengths */
269 if (mt9v022->fourcc == V4L2_PIX_FMT_SBGGR8 ||
270 mt9v022->fourcc == V4L2_PIX_FMT_SBGGR16) {
271 rect.width = ALIGN(rect.width, 2);
272 rect.height = ALIGN(rect.height, 2);
273 /* Let the user play with the starting pixel */
274 }
275
276 soc_camera_limit_side(&rect.left, &rect.width,
277 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
278
279 soc_camera_limit_side(&rect.top, &rect.height,
280 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
281
282 /* Like in example app. Contradicts the datasheet though */
283 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
284 if (ret >= 0) {
285 if (ret & 1) /* Autoexposure */
286 ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
287 rect.height + icd->y_skip_top + 43);
288 else
289 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
290 rect.height + icd->y_skip_top + 43);
291 }
292 /* Setup frame format: defaults apart from width and height */
293 if (!ret)
294 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
295 if (!ret)
296 ret = reg_write(client, MT9V022_ROW_START, rect.top);
297 if (!ret)
298 /* Default 94, Phytec driver says:
299 * "width + horizontal blank >= 660" */
300 ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
301 rect.width > 660 - 43 ? 43 :
302 660 - rect.width);
303 if (!ret)
304 ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
305 if (!ret)
306 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
307 if (!ret)
308 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
309 rect.height + icd->y_skip_top);
310
311 if (ret < 0)
312 return ret;
313
314 dev_dbg(&client->dev, "Frame %ux%u pixel\n", rect.width, rect.height);
315
316 mt9v022->rect = rect;
317
318 return 0;
319 }
320
321 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
322 {
323 struct i2c_client *client = sd->priv;
324 struct mt9v022 *mt9v022 = to_mt9v022(client);
325
326 a->c = mt9v022->rect;
327 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
328
329 return 0;
330 }
331
332 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
333 {
334 a->bounds.left = MT9V022_COLUMN_SKIP;
335 a->bounds.top = MT9V022_ROW_SKIP;
336 a->bounds.width = MT9V022_MAX_WIDTH;
337 a->bounds.height = MT9V022_MAX_HEIGHT;
338 a->defrect = a->bounds;
339 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
340 a->pixelaspect.numerator = 1;
341 a->pixelaspect.denominator = 1;
342
343 return 0;
344 }
345
346 static int mt9v022_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
347 {
348 struct i2c_client *client = sd->priv;
349 struct mt9v022 *mt9v022 = to_mt9v022(client);
350 struct v4l2_pix_format *pix = &f->fmt.pix;
351
352 pix->width = mt9v022->rect.width;
353 pix->height = mt9v022->rect.height;
354 pix->pixelformat = mt9v022->fourcc;
355 pix->field = V4L2_FIELD_NONE;
356 pix->colorspace = V4L2_COLORSPACE_SRGB;
357
358 return 0;
359 }
360
361 static int mt9v022_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
362 {
363 struct i2c_client *client = sd->priv;
364 struct mt9v022 *mt9v022 = to_mt9v022(client);
365 struct v4l2_pix_format *pix = &f->fmt.pix;
366 struct v4l2_crop a = {
367 .c = {
368 .left = mt9v022->rect.left,
369 .top = mt9v022->rect.top,
370 .width = pix->width,
371 .height = pix->height,
372 },
373 };
374 int ret;
375
376 /* The caller provides a supported format, as verified per call to
377 * icd->try_fmt(), datawidth is from our supported format list */
378 switch (pix->pixelformat) {
379 case V4L2_PIX_FMT_GREY:
380 case V4L2_PIX_FMT_Y16:
381 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
382 return -EINVAL;
383 break;
384 case V4L2_PIX_FMT_SBGGR8:
385 case V4L2_PIX_FMT_SBGGR16:
386 if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
387 return -EINVAL;
388 break;
389 case 0:
390 /* No format change, only geometry */
391 break;
392 default:
393 return -EINVAL;
394 }
395
396 /* No support for scaling on this camera, just crop. */
397 ret = mt9v022_s_crop(sd, &a);
398 if (!ret) {
399 pix->width = mt9v022->rect.width;
400 pix->height = mt9v022->rect.height;
401 mt9v022->fourcc = pix->pixelformat;
402 }
403
404 return ret;
405 }
406
407 static int mt9v022_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
408 {
409 struct i2c_client *client = sd->priv;
410 struct soc_camera_device *icd = client->dev.platform_data;
411 struct v4l2_pix_format *pix = &f->fmt.pix;
412 int align = pix->pixelformat == V4L2_PIX_FMT_SBGGR8 ||
413 pix->pixelformat == V4L2_PIX_FMT_SBGGR16;
414
415 v4l_bound_align_image(&pix->width, MT9V022_MIN_WIDTH,
416 MT9V022_MAX_WIDTH, align,
417 &pix->height, MT9V022_MIN_HEIGHT + icd->y_skip_top,
418 MT9V022_MAX_HEIGHT + icd->y_skip_top, align, 0);
419
420 return 0;
421 }
422
423 static int mt9v022_g_chip_ident(struct v4l2_subdev *sd,
424 struct v4l2_dbg_chip_ident *id)
425 {
426 struct i2c_client *client = sd->priv;
427 struct mt9v022 *mt9v022 = to_mt9v022(client);
428
429 if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
430 return -EINVAL;
431
432 if (id->match.addr != 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
442 static int mt9v022_g_register(struct v4l2_subdev *sd,
443 struct v4l2_dbg_register *reg)
444 {
445 struct i2c_client *client = sd->priv;
446
447 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
448 return -EINVAL;
449
450 if (reg->match.addr != client->addr)
451 return -ENODEV;
452
453 reg->size = 2;
454 reg->val = reg_read(client, reg->reg);
455
456 if (reg->val > 0xffff)
457 return -EIO;
458
459 return 0;
460 }
461
462 static int mt9v022_s_register(struct v4l2_subdev *sd,
463 struct v4l2_dbg_register *reg)
464 {
465 struct i2c_client *client = sd->priv;
466
467 if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
468 return -EINVAL;
469
470 if (reg->match.addr != client->addr)
471 return -ENODEV;
472
473 if (reg_write(client, reg->reg, reg->val) < 0)
474 return -EIO;
475
476 return 0;
477 }
478 #endif
479
480 static const struct v4l2_queryctrl mt9v022_controls[] = {
481 {
482 .id = V4L2_CID_VFLIP,
483 .type = V4L2_CTRL_TYPE_BOOLEAN,
484 .name = "Flip Vertically",
485 .minimum = 0,
486 .maximum = 1,
487 .step = 1,
488 .default_value = 0,
489 }, {
490 .id = V4L2_CID_HFLIP,
491 .type = V4L2_CTRL_TYPE_BOOLEAN,
492 .name = "Flip Horizontally",
493 .minimum = 0,
494 .maximum = 1,
495 .step = 1,
496 .default_value = 0,
497 }, {
498 .id = V4L2_CID_GAIN,
499 .type = V4L2_CTRL_TYPE_INTEGER,
500 .name = "Analog Gain",
501 .minimum = 64,
502 .maximum = 127,
503 .step = 1,
504 .default_value = 64,
505 .flags = V4L2_CTRL_FLAG_SLIDER,
506 }, {
507 .id = V4L2_CID_EXPOSURE,
508 .type = V4L2_CTRL_TYPE_INTEGER,
509 .name = "Exposure",
510 .minimum = 1,
511 .maximum = 255,
512 .step = 1,
513 .default_value = 255,
514 .flags = V4L2_CTRL_FLAG_SLIDER,
515 }, {
516 .id = V4L2_CID_AUTOGAIN,
517 .type = V4L2_CTRL_TYPE_BOOLEAN,
518 .name = "Automatic Gain",
519 .minimum = 0,
520 .maximum = 1,
521 .step = 1,
522 .default_value = 1,
523 }, {
524 .id = V4L2_CID_EXPOSURE_AUTO,
525 .type = V4L2_CTRL_TYPE_BOOLEAN,
526 .name = "Automatic Exposure",
527 .minimum = 0,
528 .maximum = 1,
529 .step = 1,
530 .default_value = 1,
531 }
532 };
533
534 static struct soc_camera_ops mt9v022_ops = {
535 .init = mt9v022_init,
536 .set_bus_param = mt9v022_set_bus_param,
537 .query_bus_param = mt9v022_query_bus_param,
538 .controls = mt9v022_controls,
539 .num_controls = ARRAY_SIZE(mt9v022_controls),
540 };
541
542 static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
543 {
544 struct i2c_client *client = sd->priv;
545 int data;
546
547 switch (ctrl->id) {
548 case V4L2_CID_VFLIP:
549 data = reg_read(client, MT9V022_READ_MODE);
550 if (data < 0)
551 return -EIO;
552 ctrl->value = !!(data & 0x10);
553 break;
554 case V4L2_CID_HFLIP:
555 data = reg_read(client, MT9V022_READ_MODE);
556 if (data < 0)
557 return -EIO;
558 ctrl->value = !!(data & 0x20);
559 break;
560 case V4L2_CID_EXPOSURE_AUTO:
561 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
562 if (data < 0)
563 return -EIO;
564 ctrl->value = !!(data & 0x1);
565 break;
566 case V4L2_CID_AUTOGAIN:
567 data = reg_read(client, MT9V022_AEC_AGC_ENABLE);
568 if (data < 0)
569 return -EIO;
570 ctrl->value = !!(data & 0x2);
571 break;
572 }
573 return 0;
574 }
575
576 static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
577 {
578 int data;
579 struct i2c_client *client = sd->priv;
580 struct soc_camera_device *icd = client->dev.platform_data;
581 const struct v4l2_queryctrl *qctrl;
582
583 qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
584 if (!qctrl)
585 return -EINVAL;
586
587 switch (ctrl->id) {
588 case V4L2_CID_VFLIP:
589 if (ctrl->value)
590 data = reg_set(client, MT9V022_READ_MODE, 0x10);
591 else
592 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
593 if (data < 0)
594 return -EIO;
595 break;
596 case V4L2_CID_HFLIP:
597 if (ctrl->value)
598 data = reg_set(client, MT9V022_READ_MODE, 0x20);
599 else
600 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
601 if (data < 0)
602 return -EIO;
603 break;
604 case V4L2_CID_GAIN:
605 /* mt9v022 has minimum == default */
606 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
607 return -EINVAL;
608 else {
609 unsigned long range = qctrl->maximum - qctrl->minimum;
610 /* Datasheet says 16 to 64. autogain only works properly
611 * after setting gain to maximum 14. Larger values
612 * produce "white fly" noise effect. On the whole,
613 * manually setting analog gain does no good. */
614 unsigned long gain = ((ctrl->value - qctrl->minimum) *
615 10 + range / 2) / range + 4;
616 if (gain >= 32)
617 gain &= ~1;
618 /* The user wants to set gain manually, hope, she
619 * knows, what she's doing... Switch AGC off. */
620
621 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
622 return -EIO;
623
624 dev_info(&client->dev, "Setting gain from %d to %lu\n",
625 reg_read(client, MT9V022_ANALOG_GAIN), gain);
626 if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
627 return -EIO;
628 icd->gain = ctrl->value;
629 }
630 break;
631 case V4L2_CID_EXPOSURE:
632 /* mt9v022 has maximum == default */
633 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
634 return -EINVAL;
635 else {
636 unsigned long range = qctrl->maximum - qctrl->minimum;
637 unsigned long shutter = ((ctrl->value - qctrl->minimum) *
638 479 + range / 2) / range + 1;
639 /* The user wants to set shutter width manually, hope,
640 * she knows, what she's doing... Switch AEC off. */
641
642 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
643 return -EIO;
644
645 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
646 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
647 shutter);
648 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
649 shutter) < 0)
650 return -EIO;
651 icd->exposure = ctrl->value;
652 }
653 break;
654 case V4L2_CID_AUTOGAIN:
655 if (ctrl->value)
656 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2);
657 else
658 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2);
659 if (data < 0)
660 return -EIO;
661 break;
662 case V4L2_CID_EXPOSURE_AUTO:
663 if (ctrl->value)
664 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
665 else
666 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
667 if (data < 0)
668 return -EIO;
669 break;
670 }
671 return 0;
672 }
673
674 /* Interface active, can use i2c. If it fails, it can indeed mean, that
675 * this wasn't our capture interface, so, we wait for the right one */
676 static int mt9v022_video_probe(struct soc_camera_device *icd,
677 struct i2c_client *client)
678 {
679 struct mt9v022 *mt9v022 = to_mt9v022(client);
680 struct soc_camera_link *icl = to_soc_camera_link(icd);
681 s32 data;
682 int ret;
683 unsigned long flags;
684
685 if (!icd->dev.parent ||
686 to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
687 return -ENODEV;
688
689 /* Read out the chip version register */
690 data = reg_read(client, MT9V022_CHIP_VERSION);
691
692 /* must be 0x1311 or 0x1313 */
693 if (data != 0x1311 && data != 0x1313) {
694 ret = -ENODEV;
695 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
696 data);
697 goto ei2c;
698 }
699
700 /* Soft reset */
701 ret = reg_write(client, MT9V022_RESET, 1);
702 if (ret < 0)
703 goto ei2c;
704 /* 15 clock cycles */
705 udelay(200);
706 if (reg_read(client, MT9V022_RESET)) {
707 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
708 if (ret > 0)
709 ret = -EIO;
710 goto ei2c;
711 }
712
713 /* Set monochrome or colour sensor type */
714 if (sensor_type && (!strcmp("colour", sensor_type) ||
715 !strcmp("color", sensor_type))) {
716 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
717 mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
718 icd->formats = mt9v022_colour_formats;
719 } else {
720 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
721 mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
722 icd->formats = mt9v022_monochrome_formats;
723 }
724
725 if (ret < 0)
726 goto ei2c;
727
728 icd->num_formats = 0;
729
730 /*
731 * This is a 10bit sensor, so by default we only allow 10bit.
732 * The platform may support different bus widths due to
733 * different routing of the data lines.
734 */
735 if (icl->query_bus_param)
736 flags = icl->query_bus_param(icl);
737 else
738 flags = SOCAM_DATAWIDTH_10;
739
740 if (flags & SOCAM_DATAWIDTH_10)
741 icd->num_formats++;
742 else
743 icd->formats++;
744
745 if (flags & SOCAM_DATAWIDTH_8)
746 icd->num_formats++;
747
748 mt9v022->fourcc = icd->formats->fourcc;
749
750 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
751 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
752 "monochrome" : "colour");
753
754 ei2c:
755 return ret;
756 }
757
758 static void mt9v022_video_remove(struct soc_camera_device *icd)
759 {
760 struct soc_camera_link *icl = to_soc_camera_link(icd);
761
762 dev_dbg(&icd->dev, "Video removed: %p, %p\n",
763 icd->dev.parent, icd->vdev);
764 if (icl->free_bus)
765 icl->free_bus(icl);
766 }
767
768 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
769 .g_ctrl = mt9v022_g_ctrl,
770 .s_ctrl = mt9v022_s_ctrl,
771 .g_chip_ident = mt9v022_g_chip_ident,
772 #ifdef CONFIG_VIDEO_ADV_DEBUG
773 .g_register = mt9v022_g_register,
774 .s_register = mt9v022_s_register,
775 #endif
776 };
777
778 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
779 .s_stream = mt9v022_s_stream,
780 .s_fmt = mt9v022_s_fmt,
781 .g_fmt = mt9v022_g_fmt,
782 .try_fmt = mt9v022_try_fmt,
783 .s_crop = mt9v022_s_crop,
784 .g_crop = mt9v022_g_crop,
785 .cropcap = mt9v022_cropcap,
786 };
787
788 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
789 .core = &mt9v022_subdev_core_ops,
790 .video = &mt9v022_subdev_video_ops,
791 };
792
793 static int mt9v022_probe(struct i2c_client *client,
794 const struct i2c_device_id *did)
795 {
796 struct mt9v022 *mt9v022;
797 struct soc_camera_device *icd = client->dev.platform_data;
798 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
799 struct soc_camera_link *icl;
800 int ret;
801
802 if (!icd) {
803 dev_err(&client->dev, "MT9V022: missing soc-camera data!\n");
804 return -EINVAL;
805 }
806
807 icl = to_soc_camera_link(icd);
808 if (!icl) {
809 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
810 return -EINVAL;
811 }
812
813 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
814 dev_warn(&adapter->dev,
815 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
816 return -EIO;
817 }
818
819 mt9v022 = kzalloc(sizeof(struct mt9v022), GFP_KERNEL);
820 if (!mt9v022)
821 return -ENOMEM;
822
823 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
824
825 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
826
827 icd->ops = &mt9v022_ops;
828 icd->y_skip_top = 1;
829
830 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
831 mt9v022->rect.top = MT9V022_ROW_SKIP;
832 mt9v022->rect.width = MT9V022_MAX_WIDTH;
833 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
834
835 ret = mt9v022_video_probe(icd, client);
836 if (ret) {
837 icd->ops = NULL;
838 i2c_set_clientdata(client, NULL);
839 kfree(mt9v022);
840 }
841
842 return ret;
843 }
844
845 static int mt9v022_remove(struct i2c_client *client)
846 {
847 struct mt9v022 *mt9v022 = to_mt9v022(client);
848 struct soc_camera_device *icd = client->dev.platform_data;
849
850 icd->ops = NULL;
851 mt9v022_video_remove(icd);
852 i2c_set_clientdata(client, NULL);
853 client->driver = NULL;
854 kfree(mt9v022);
855
856 return 0;
857 }
858 static const struct i2c_device_id mt9v022_id[] = {
859 { "mt9v022", 0 },
860 { }
861 };
862 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
863
864 static struct i2c_driver mt9v022_i2c_driver = {
865 .driver = {
866 .name = "mt9v022",
867 },
868 .probe = mt9v022_probe,
869 .remove = mt9v022_remove,
870 .id_table = mt9v022_id,
871 };
872
873 static int __init mt9v022_mod_init(void)
874 {
875 return i2c_add_driver(&mt9v022_i2c_driver);
876 }
877
878 static void __exit mt9v022_mod_exit(void)
879 {
880 i2c_del_driver(&mt9v022_i2c_driver);
881 }
882
883 module_init(mt9v022_mod_init);
884 module_exit(mt9v022_mod_exit);
885
886 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
887 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
888 MODULE_LICENSE("GPL");