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