b4ba3c5930e3a10619d495937497249a8c200acf
[deliverable/linux.git] / drivers / media / i2c / soc_camera / 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 #include <linux/module.h>
17
18 #include <media/mt9v022.h>
19 #include <media/soc_camera.h>
20 #include <media/soc_mediabus.h>
21 #include <media/v4l2-subdev.h>
22 #include <media/v4l2-clk.h>
23 #include <media/v4l2-ctrls.h>
24
25 /*
26 * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
27 * The platform has to define struct i2c_board_info objects and link to them
28 * from struct soc_camera_host_desc
29 */
30
31 static char *sensor_type;
32 module_param(sensor_type, charp, S_IRUGO);
33 MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
34
35 /* mt9v022 selected register addresses */
36 #define MT9V022_CHIP_VERSION 0x00
37 #define MT9V022_COLUMN_START 0x01
38 #define MT9V022_ROW_START 0x02
39 #define MT9V022_WINDOW_HEIGHT 0x03
40 #define MT9V022_WINDOW_WIDTH 0x04
41 #define MT9V022_HORIZONTAL_BLANKING 0x05
42 #define MT9V022_VERTICAL_BLANKING 0x06
43 #define MT9V022_CHIP_CONTROL 0x07
44 #define MT9V022_SHUTTER_WIDTH1 0x08
45 #define MT9V022_SHUTTER_WIDTH2 0x09
46 #define MT9V022_SHUTTER_WIDTH_CTRL 0x0a
47 #define MT9V022_TOTAL_SHUTTER_WIDTH 0x0b
48 #define MT9V022_RESET 0x0c
49 #define MT9V022_READ_MODE 0x0d
50 #define MT9V022_MONITOR_MODE 0x0e
51 #define MT9V022_PIXEL_OPERATION_MODE 0x0f
52 #define MT9V022_LED_OUT_CONTROL 0x1b
53 #define MT9V022_ADC_MODE_CONTROL 0x1c
54 #define MT9V022_REG32 0x20
55 #define MT9V022_ANALOG_GAIN 0x35
56 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
57 #define MT9V022_PIXCLK_FV_LV 0x74
58 #define MT9V022_DIGITAL_TEST_PATTERN 0x7f
59 #define MT9V022_AEC_AGC_ENABLE 0xAF
60 #define MT9V022_MAX_TOTAL_SHUTTER_WIDTH 0xBD
61
62 /* mt9v024 partial list register addresses changes with respect to mt9v022 */
63 #define MT9V024_PIXCLK_FV_LV 0x72
64 #define MT9V024_MAX_TOTAL_SHUTTER_WIDTH 0xAD
65
66 /* Progressive scan, master, defaults */
67 #define MT9V022_CHIP_CONTROL_DEFAULT 0x188
68
69 #define MT9V022_MAX_WIDTH 752
70 #define MT9V022_MAX_HEIGHT 480
71 #define MT9V022_MIN_WIDTH 48
72 #define MT9V022_MIN_HEIGHT 32
73 #define MT9V022_COLUMN_SKIP 1
74 #define MT9V022_ROW_SKIP 4
75
76 #define MT9V022_HORIZONTAL_BLANKING_MIN 43
77 #define MT9V022_HORIZONTAL_BLANKING_MAX 1023
78 #define MT9V022_HORIZONTAL_BLANKING_DEF 94
79 #define MT9V022_VERTICAL_BLANKING_MIN 2
80 #define MT9V022_VERTICAL_BLANKING_MAX 3000
81 #define MT9V022_VERTICAL_BLANKING_DEF 45
82
83 #define is_mt9v022_rev3(id) (id == 0x1313)
84 #define is_mt9v024(id) (id == 0x1324)
85
86 /* MT9V022 has only one fixed colorspace per pixelcode */
87 struct mt9v022_datafmt {
88 u32 code;
89 enum v4l2_colorspace colorspace;
90 };
91
92 /* Find a data format by a pixel code in an array */
93 static const struct mt9v022_datafmt *mt9v022_find_datafmt(
94 u32 code, const struct mt9v022_datafmt *fmt,
95 int n)
96 {
97 int i;
98 for (i = 0; i < n; i++)
99 if (fmt[i].code == code)
100 return fmt + i;
101
102 return NULL;
103 }
104
105 static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
106 /*
107 * Order important: first natively supported,
108 * second supported with a GPIO extender
109 */
110 {MEDIA_BUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
111 {MEDIA_BUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
112 };
113
114 static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
115 /* Order important - see above */
116 {MEDIA_BUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
117 {MEDIA_BUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
118 };
119
120 /* only registers with different addresses on different mt9v02x sensors */
121 struct mt9v02x_register {
122 u8 max_total_shutter_width;
123 u8 pixclk_fv_lv;
124 };
125
126 static const struct mt9v02x_register mt9v022_register = {
127 .max_total_shutter_width = MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
128 .pixclk_fv_lv = MT9V022_PIXCLK_FV_LV,
129 };
130
131 static const struct mt9v02x_register mt9v024_register = {
132 .max_total_shutter_width = MT9V024_MAX_TOTAL_SHUTTER_WIDTH,
133 .pixclk_fv_lv = MT9V024_PIXCLK_FV_LV,
134 };
135
136 enum mt9v022_model {
137 MT9V022IX7ATM,
138 MT9V022IX7ATC,
139 };
140
141 struct mt9v022 {
142 struct v4l2_subdev subdev;
143 struct v4l2_ctrl_handler hdl;
144 struct {
145 /* exposure/auto-exposure cluster */
146 struct v4l2_ctrl *autoexposure;
147 struct v4l2_ctrl *exposure;
148 };
149 struct {
150 /* gain/auto-gain cluster */
151 struct v4l2_ctrl *autogain;
152 struct v4l2_ctrl *gain;
153 };
154 struct v4l2_ctrl *hblank;
155 struct v4l2_ctrl *vblank;
156 struct v4l2_rect rect; /* Sensor window */
157 struct v4l2_clk *clk;
158 const struct mt9v022_datafmt *fmt;
159 const struct mt9v022_datafmt *fmts;
160 const struct mt9v02x_register *reg;
161 int num_fmts;
162 enum mt9v022_model model;
163 u16 chip_control;
164 u16 chip_version;
165 unsigned short y_skip_top; /* Lines to skip at the top */
166 };
167
168 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
169 {
170 return container_of(i2c_get_clientdata(client), struct mt9v022, subdev);
171 }
172
173 static int reg_read(struct i2c_client *client, const u8 reg)
174 {
175 return i2c_smbus_read_word_swapped(client, reg);
176 }
177
178 static int reg_write(struct i2c_client *client, const u8 reg,
179 const u16 data)
180 {
181 return i2c_smbus_write_word_swapped(client, reg, data);
182 }
183
184 static int reg_set(struct i2c_client *client, const u8 reg,
185 const u16 data)
186 {
187 int ret;
188
189 ret = reg_read(client, reg);
190 if (ret < 0)
191 return ret;
192 return reg_write(client, reg, ret | data);
193 }
194
195 static int reg_clear(struct i2c_client *client, const u8 reg,
196 const u16 data)
197 {
198 int ret;
199
200 ret = reg_read(client, reg);
201 if (ret < 0)
202 return ret;
203 return reg_write(client, reg, ret & ~data);
204 }
205
206 static int mt9v022_init(struct i2c_client *client)
207 {
208 struct mt9v022 *mt9v022 = to_mt9v022(client);
209 int ret;
210
211 /*
212 * Almost the default mode: master, parallel, simultaneous, and an
213 * undocumented bit 0x200, which is present in table 7, but not in 8,
214 * plus snapshot mode to disable scan for now
215 */
216 mt9v022->chip_control |= 0x10;
217 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
218 if (!ret)
219 ret = reg_write(client, MT9V022_READ_MODE, 0x300);
220
221 /* All defaults */
222 if (!ret)
223 /* AEC, AGC on */
224 ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
225 if (!ret)
226 ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
227 if (!ret)
228 ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
229 if (!ret)
230 ret = reg_write(client, mt9v022->reg->max_total_shutter_width, 480);
231 if (!ret)
232 /* default - auto */
233 ret = reg_clear(client, MT9V022_BLACK_LEVEL_CALIB_CTRL, 1);
234 if (!ret)
235 ret = reg_write(client, MT9V022_DIGITAL_TEST_PATTERN, 0);
236 if (!ret)
237 return v4l2_ctrl_handler_setup(&mt9v022->hdl);
238
239 return ret;
240 }
241
242 static int mt9v022_s_stream(struct v4l2_subdev *sd, int enable)
243 {
244 struct i2c_client *client = v4l2_get_subdevdata(sd);
245 struct mt9v022 *mt9v022 = to_mt9v022(client);
246
247 if (enable) {
248 /* Switch to master "normal" mode */
249 mt9v022->chip_control &= ~0x10;
250 if (is_mt9v022_rev3(mt9v022->chip_version) ||
251 is_mt9v024(mt9v022->chip_version)) {
252 /*
253 * Unset snapshot mode specific settings: clear bit 9
254 * and bit 2 in reg. 0x20 when in normal mode.
255 */
256 if (reg_clear(client, MT9V022_REG32, 0x204))
257 return -EIO;
258 }
259 } else {
260 /* Switch to snapshot mode */
261 mt9v022->chip_control |= 0x10;
262 if (is_mt9v022_rev3(mt9v022->chip_version) ||
263 is_mt9v024(mt9v022->chip_version)) {
264 /*
265 * Required settings for snapshot mode: set bit 9
266 * (RST enable) and bit 2 (CR enable) in reg. 0x20
267 * See TechNote TN0960 or TN-09-225.
268 */
269 if (reg_set(client, MT9V022_REG32, 0x204))
270 return -EIO;
271 }
272 }
273
274 if (reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control) < 0)
275 return -EIO;
276 return 0;
277 }
278
279 static int mt9v022_s_crop(struct v4l2_subdev *sd, const struct v4l2_crop *a)
280 {
281 struct i2c_client *client = v4l2_get_subdevdata(sd);
282 struct mt9v022 *mt9v022 = to_mt9v022(client);
283 struct v4l2_rect rect = a->c;
284 int min_row, min_blank;
285 int ret;
286
287 /* Bayer format - even size lengths */
288 if (mt9v022->fmts == mt9v022_colour_fmts) {
289 rect.width = ALIGN(rect.width, 2);
290 rect.height = ALIGN(rect.height, 2);
291 /* Let the user play with the starting pixel */
292 }
293
294 soc_camera_limit_side(&rect.left, &rect.width,
295 MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
296
297 soc_camera_limit_side(&rect.top, &rect.height,
298 MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
299
300 /* Like in example app. Contradicts the datasheet though */
301 ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
302 if (ret >= 0) {
303 if (ret & 1) /* Autoexposure */
304 ret = reg_write(client, mt9v022->reg->max_total_shutter_width,
305 rect.height + mt9v022->y_skip_top + 43);
306 /*
307 * If autoexposure is off, there is no need to set
308 * MT9V022_TOTAL_SHUTTER_WIDTH here. Autoexposure can be off
309 * only if the user has set exposure manually, using the
310 * V4L2_CID_EXPOSURE_AUTO with the value V4L2_EXPOSURE_MANUAL.
311 * In this case the register MT9V022_TOTAL_SHUTTER_WIDTH
312 * already contains the correct value.
313 */
314 }
315 /* Setup frame format: defaults apart from width and height */
316 if (!ret)
317 ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
318 if (!ret)
319 ret = reg_write(client, MT9V022_ROW_START, rect.top);
320 /*
321 * mt9v022: min total row time is 660 columns, min blanking is 43
322 * mt9v024: min total row time is 690 columns, min blanking is 61
323 */
324 if (is_mt9v024(mt9v022->chip_version)) {
325 min_row = 690;
326 min_blank = 61;
327 } else {
328 min_row = 660;
329 min_blank = 43;
330 }
331 if (!ret)
332 ret = v4l2_ctrl_s_ctrl(mt9v022->hblank,
333 rect.width > min_row - min_blank ?
334 min_blank : min_row - rect.width);
335 if (!ret)
336 ret = v4l2_ctrl_s_ctrl(mt9v022->vblank, 45);
337 if (!ret)
338 ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
339 if (!ret)
340 ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
341 rect.height + mt9v022->y_skip_top);
342
343 if (ret < 0)
344 return ret;
345
346 dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
347
348 mt9v022->rect = rect;
349
350 return 0;
351 }
352
353 static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
354 {
355 struct i2c_client *client = v4l2_get_subdevdata(sd);
356 struct mt9v022 *mt9v022 = to_mt9v022(client);
357
358 a->c = mt9v022->rect;
359 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
360
361 return 0;
362 }
363
364 static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
365 {
366 a->bounds.left = MT9V022_COLUMN_SKIP;
367 a->bounds.top = MT9V022_ROW_SKIP;
368 a->bounds.width = MT9V022_MAX_WIDTH;
369 a->bounds.height = MT9V022_MAX_HEIGHT;
370 a->defrect = a->bounds;
371 a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
372 a->pixelaspect.numerator = 1;
373 a->pixelaspect.denominator = 1;
374
375 return 0;
376 }
377
378 static int mt9v022_get_fmt(struct v4l2_subdev *sd,
379 struct v4l2_subdev_pad_config *cfg,
380 struct v4l2_subdev_format *format)
381 {
382 struct v4l2_mbus_framefmt *mf = &format->format;
383 struct i2c_client *client = v4l2_get_subdevdata(sd);
384 struct mt9v022 *mt9v022 = to_mt9v022(client);
385
386 if (format->pad)
387 return -EINVAL;
388
389 mf->width = mt9v022->rect.width;
390 mf->height = mt9v022->rect.height;
391 mf->code = mt9v022->fmt->code;
392 mf->colorspace = mt9v022->fmt->colorspace;
393 mf->field = V4L2_FIELD_NONE;
394
395 return 0;
396 }
397
398 static int mt9v022_s_fmt(struct v4l2_subdev *sd,
399 struct v4l2_mbus_framefmt *mf)
400 {
401 struct i2c_client *client = v4l2_get_subdevdata(sd);
402 struct mt9v022 *mt9v022 = to_mt9v022(client);
403 struct v4l2_crop a = {
404 .c = {
405 .left = mt9v022->rect.left,
406 .top = mt9v022->rect.top,
407 .width = mf->width,
408 .height = mf->height,
409 },
410 };
411 int ret;
412
413 /*
414 * The caller provides a supported format, as verified per call to
415 * .try_mbus_fmt(), datawidth is from our supported format list
416 */
417 switch (mf->code) {
418 case MEDIA_BUS_FMT_Y8_1X8:
419 case MEDIA_BUS_FMT_Y10_1X10:
420 if (mt9v022->model != MT9V022IX7ATM)
421 return -EINVAL;
422 break;
423 case MEDIA_BUS_FMT_SBGGR8_1X8:
424 case MEDIA_BUS_FMT_SBGGR10_1X10:
425 if (mt9v022->model != MT9V022IX7ATC)
426 return -EINVAL;
427 break;
428 default:
429 return -EINVAL;
430 }
431
432 /* No support for scaling on this camera, just crop. */
433 ret = mt9v022_s_crop(sd, &a);
434 if (!ret) {
435 mf->width = mt9v022->rect.width;
436 mf->height = mt9v022->rect.height;
437 mt9v022->fmt = mt9v022_find_datafmt(mf->code,
438 mt9v022->fmts, mt9v022->num_fmts);
439 mf->colorspace = mt9v022->fmt->colorspace;
440 }
441
442 return ret;
443 }
444
445 static int mt9v022_try_fmt(struct v4l2_subdev *sd,
446 struct v4l2_mbus_framefmt *mf)
447 {
448 struct i2c_client *client = v4l2_get_subdevdata(sd);
449 struct mt9v022 *mt9v022 = to_mt9v022(client);
450 const struct mt9v022_datafmt *fmt;
451 int align = mf->code == MEDIA_BUS_FMT_SBGGR8_1X8 ||
452 mf->code == MEDIA_BUS_FMT_SBGGR10_1X10;
453
454 v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
455 MT9V022_MAX_WIDTH, align,
456 &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
457 MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
458
459 fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
460 mt9v022->num_fmts);
461 if (!fmt) {
462 fmt = mt9v022->fmt;
463 mf->code = fmt->code;
464 }
465
466 mf->colorspace = fmt->colorspace;
467
468 return 0;
469 }
470
471 #ifdef CONFIG_VIDEO_ADV_DEBUG
472 static int mt9v022_g_register(struct v4l2_subdev *sd,
473 struct v4l2_dbg_register *reg)
474 {
475 struct i2c_client *client = v4l2_get_subdevdata(sd);
476
477 if (reg->reg > 0xff)
478 return -EINVAL;
479
480 reg->size = 2;
481 reg->val = reg_read(client, reg->reg);
482
483 if (reg->val > 0xffff)
484 return -EIO;
485
486 return 0;
487 }
488
489 static int mt9v022_s_register(struct v4l2_subdev *sd,
490 const struct v4l2_dbg_register *reg)
491 {
492 struct i2c_client *client = v4l2_get_subdevdata(sd);
493
494 if (reg->reg > 0xff)
495 return -EINVAL;
496
497 if (reg_write(client, reg->reg, reg->val) < 0)
498 return -EIO;
499
500 return 0;
501 }
502 #endif
503
504 static int mt9v022_s_power(struct v4l2_subdev *sd, int on)
505 {
506 struct i2c_client *client = v4l2_get_subdevdata(sd);
507 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
508 struct mt9v022 *mt9v022 = to_mt9v022(client);
509
510 return soc_camera_set_power(&client->dev, ssdd, mt9v022->clk, on);
511 }
512
513 static int mt9v022_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
514 {
515 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
516 struct mt9v022, hdl);
517 struct v4l2_subdev *sd = &mt9v022->subdev;
518 struct i2c_client *client = v4l2_get_subdevdata(sd);
519 struct v4l2_ctrl *gain = mt9v022->gain;
520 struct v4l2_ctrl *exp = mt9v022->exposure;
521 unsigned long range;
522 int data;
523
524 switch (ctrl->id) {
525 case V4L2_CID_AUTOGAIN:
526 data = reg_read(client, MT9V022_ANALOG_GAIN);
527 if (data < 0)
528 return -EIO;
529
530 range = gain->maximum - gain->minimum;
531 gain->val = ((data - 16) * range + 24) / 48 + gain->minimum;
532 return 0;
533 case V4L2_CID_EXPOSURE_AUTO:
534 data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
535 if (data < 0)
536 return -EIO;
537
538 range = exp->maximum - exp->minimum;
539 exp->val = ((data - 1) * range + 239) / 479 + exp->minimum;
540 return 0;
541 case V4L2_CID_HBLANK:
542 data = reg_read(client, MT9V022_HORIZONTAL_BLANKING);
543 if (data < 0)
544 return -EIO;
545 ctrl->val = data;
546 return 0;
547 case V4L2_CID_VBLANK:
548 data = reg_read(client, MT9V022_VERTICAL_BLANKING);
549 if (data < 0)
550 return -EIO;
551 ctrl->val = data;
552 return 0;
553 }
554 return -EINVAL;
555 }
556
557 static int mt9v022_s_ctrl(struct v4l2_ctrl *ctrl)
558 {
559 struct mt9v022 *mt9v022 = container_of(ctrl->handler,
560 struct mt9v022, hdl);
561 struct v4l2_subdev *sd = &mt9v022->subdev;
562 struct i2c_client *client = v4l2_get_subdevdata(sd);
563 int data;
564
565 switch (ctrl->id) {
566 case V4L2_CID_VFLIP:
567 if (ctrl->val)
568 data = reg_set(client, MT9V022_READ_MODE, 0x10);
569 else
570 data = reg_clear(client, MT9V022_READ_MODE, 0x10);
571 if (data < 0)
572 return -EIO;
573 return 0;
574 case V4L2_CID_HFLIP:
575 if (ctrl->val)
576 data = reg_set(client, MT9V022_READ_MODE, 0x20);
577 else
578 data = reg_clear(client, MT9V022_READ_MODE, 0x20);
579 if (data < 0)
580 return -EIO;
581 return 0;
582 case V4L2_CID_AUTOGAIN:
583 if (ctrl->val) {
584 if (reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
585 return -EIO;
586 } else {
587 struct v4l2_ctrl *gain = mt9v022->gain;
588 /* mt9v022 has minimum == default */
589 unsigned long range = gain->maximum - gain->minimum;
590 /* Valid values 16 to 64, 32 to 64 must be even. */
591 unsigned long gain_val = ((gain->val - (s32)gain->minimum) *
592 48 + range / 2) / range + 16;
593
594 if (gain_val >= 32)
595 gain_val &= ~1;
596
597 /*
598 * The user wants to set gain manually, hope, she
599 * knows, what she's doing... Switch AGC off.
600 */
601 if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
602 return -EIO;
603
604 dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
605 reg_read(client, MT9V022_ANALOG_GAIN), gain_val);
606 if (reg_write(client, MT9V022_ANALOG_GAIN, gain_val) < 0)
607 return -EIO;
608 }
609 return 0;
610 case V4L2_CID_EXPOSURE_AUTO:
611 if (ctrl->val == V4L2_EXPOSURE_AUTO) {
612 data = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x1);
613 } else {
614 struct v4l2_ctrl *exp = mt9v022->exposure;
615 unsigned long range = exp->maximum - exp->minimum;
616 unsigned long shutter = ((exp->val - (s32)exp->minimum) *
617 479 + range / 2) / range + 1;
618
619 /*
620 * The user wants to set shutter width manually, hope,
621 * she knows, what she's doing... Switch AEC off.
622 */
623 data = reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1);
624 if (data < 0)
625 return -EIO;
626 dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
627 reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
628 shutter);
629 if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
630 shutter) < 0)
631 return -EIO;
632 }
633 return 0;
634 case V4L2_CID_HBLANK:
635 if (reg_write(client, MT9V022_HORIZONTAL_BLANKING,
636 ctrl->val) < 0)
637 return -EIO;
638 return 0;
639 case V4L2_CID_VBLANK:
640 if (reg_write(client, MT9V022_VERTICAL_BLANKING,
641 ctrl->val) < 0)
642 return -EIO;
643 return 0;
644 }
645 return -EINVAL;
646 }
647
648 /*
649 * Interface active, can use i2c. If it fails, it can indeed mean, that
650 * this wasn't our capture interface, so, we wait for the right one
651 */
652 static int mt9v022_video_probe(struct i2c_client *client)
653 {
654 struct mt9v022 *mt9v022 = to_mt9v022(client);
655 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
656 s32 data;
657 int ret;
658 unsigned long flags;
659
660 ret = mt9v022_s_power(&mt9v022->subdev, 1);
661 if (ret < 0)
662 return ret;
663
664 /* Read out the chip version register */
665 data = reg_read(client, MT9V022_CHIP_VERSION);
666
667 /* must be 0x1311, 0x1313 or 0x1324 */
668 if (data != 0x1311 && data != 0x1313 && data != 0x1324) {
669 ret = -ENODEV;
670 dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
671 data);
672 goto ei2c;
673 }
674
675 mt9v022->chip_version = data;
676
677 mt9v022->reg = is_mt9v024(data) ? &mt9v024_register :
678 &mt9v022_register;
679
680 /* Soft reset */
681 ret = reg_write(client, MT9V022_RESET, 1);
682 if (ret < 0)
683 goto ei2c;
684 /* 15 clock cycles */
685 udelay(200);
686 if (reg_read(client, MT9V022_RESET)) {
687 dev_err(&client->dev, "Resetting MT9V022 failed!\n");
688 if (ret > 0)
689 ret = -EIO;
690 goto ei2c;
691 }
692
693 /* Set monochrome or colour sensor type */
694 if (sensor_type && (!strcmp("colour", sensor_type) ||
695 !strcmp("color", sensor_type))) {
696 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
697 mt9v022->model = MT9V022IX7ATC;
698 mt9v022->fmts = mt9v022_colour_fmts;
699 } else {
700 ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
701 mt9v022->model = MT9V022IX7ATM;
702 mt9v022->fmts = mt9v022_monochrome_fmts;
703 }
704
705 if (ret < 0)
706 goto ei2c;
707
708 mt9v022->num_fmts = 0;
709
710 /*
711 * This is a 10bit sensor, so by default we only allow 10bit.
712 * The platform may support different bus widths due to
713 * different routing of the data lines.
714 */
715 if (ssdd->query_bus_param)
716 flags = ssdd->query_bus_param(ssdd);
717 else
718 flags = SOCAM_DATAWIDTH_10;
719
720 if (flags & SOCAM_DATAWIDTH_10)
721 mt9v022->num_fmts++;
722 else
723 mt9v022->fmts++;
724
725 if (flags & SOCAM_DATAWIDTH_8)
726 mt9v022->num_fmts++;
727
728 mt9v022->fmt = &mt9v022->fmts[0];
729
730 dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
731 data, mt9v022->model == MT9V022IX7ATM ?
732 "monochrome" : "colour");
733
734 ret = mt9v022_init(client);
735 if (ret < 0)
736 dev_err(&client->dev, "Failed to initialise the camera\n");
737
738 ei2c:
739 mt9v022_s_power(&mt9v022->subdev, 0);
740 return ret;
741 }
742
743 static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
744 {
745 struct i2c_client *client = v4l2_get_subdevdata(sd);
746 struct mt9v022 *mt9v022 = to_mt9v022(client);
747
748 *lines = mt9v022->y_skip_top;
749
750 return 0;
751 }
752
753 static const struct v4l2_ctrl_ops mt9v022_ctrl_ops = {
754 .g_volatile_ctrl = mt9v022_g_volatile_ctrl,
755 .s_ctrl = mt9v022_s_ctrl,
756 };
757
758 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
759 #ifdef CONFIG_VIDEO_ADV_DEBUG
760 .g_register = mt9v022_g_register,
761 .s_register = mt9v022_s_register,
762 #endif
763 .s_power = mt9v022_s_power,
764 };
765
766 static int mt9v022_enum_mbus_code(struct v4l2_subdev *sd,
767 struct v4l2_subdev_pad_config *cfg,
768 struct v4l2_subdev_mbus_code_enum *code)
769 {
770 struct i2c_client *client = v4l2_get_subdevdata(sd);
771 struct mt9v022 *mt9v022 = to_mt9v022(client);
772
773 if (code->pad || code->index >= mt9v022->num_fmts)
774 return -EINVAL;
775
776 code->code = mt9v022->fmts[code->index].code;
777 return 0;
778 }
779
780 static int mt9v022_g_mbus_config(struct v4l2_subdev *sd,
781 struct v4l2_mbus_config *cfg)
782 {
783 struct i2c_client *client = v4l2_get_subdevdata(sd);
784 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
785
786 cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_SLAVE |
787 V4L2_MBUS_PCLK_SAMPLE_RISING | V4L2_MBUS_PCLK_SAMPLE_FALLING |
788 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_HSYNC_ACTIVE_LOW |
789 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_LOW |
790 V4L2_MBUS_DATA_ACTIVE_HIGH;
791 cfg->type = V4L2_MBUS_PARALLEL;
792 cfg->flags = soc_camera_apply_board_flags(ssdd, cfg);
793
794 return 0;
795 }
796
797 static int mt9v022_s_mbus_config(struct v4l2_subdev *sd,
798 const struct v4l2_mbus_config *cfg)
799 {
800 struct i2c_client *client = v4l2_get_subdevdata(sd);
801 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
802 struct mt9v022 *mt9v022 = to_mt9v022(client);
803 unsigned long flags = soc_camera_apply_board_flags(ssdd, cfg);
804 unsigned int bps = soc_mbus_get_fmtdesc(mt9v022->fmt->code)->bits_per_sample;
805 int ret;
806 u16 pixclk = 0;
807
808 if (ssdd->set_bus_param) {
809 ret = ssdd->set_bus_param(ssdd, 1 << (bps - 1));
810 if (ret)
811 return ret;
812 } else if (bps != 10) {
813 /*
814 * Without board specific bus width settings we only support the
815 * sensors native bus width
816 */
817 return -EINVAL;
818 }
819
820 if (flags & V4L2_MBUS_PCLK_SAMPLE_FALLING)
821 pixclk |= 0x10;
822
823 if (!(flags & V4L2_MBUS_HSYNC_ACTIVE_HIGH))
824 pixclk |= 0x1;
825
826 if (!(flags & V4L2_MBUS_VSYNC_ACTIVE_HIGH))
827 pixclk |= 0x2;
828
829 ret = reg_write(client, mt9v022->reg->pixclk_fv_lv, pixclk);
830 if (ret < 0)
831 return ret;
832
833 if (!(flags & V4L2_MBUS_MASTER))
834 mt9v022->chip_control &= ~0x8;
835
836 ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
837 if (ret < 0)
838 return ret;
839
840 dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
841 pixclk, mt9v022->chip_control);
842
843 return 0;
844 }
845
846 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
847 .s_stream = mt9v022_s_stream,
848 .s_mbus_fmt = mt9v022_s_fmt,
849 .try_mbus_fmt = mt9v022_try_fmt,
850 .s_crop = mt9v022_s_crop,
851 .g_crop = mt9v022_g_crop,
852 .cropcap = mt9v022_cropcap,
853 .g_mbus_config = mt9v022_g_mbus_config,
854 .s_mbus_config = mt9v022_s_mbus_config,
855 };
856
857 static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
858 .g_skip_top_lines = mt9v022_g_skip_top_lines,
859 };
860
861 static const struct v4l2_subdev_pad_ops mt9v022_subdev_pad_ops = {
862 .enum_mbus_code = mt9v022_enum_mbus_code,
863 .get_fmt = mt9v022_get_fmt,
864 };
865
866 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
867 .core = &mt9v022_subdev_core_ops,
868 .video = &mt9v022_subdev_video_ops,
869 .sensor = &mt9v022_subdev_sensor_ops,
870 .pad = &mt9v022_subdev_pad_ops,
871 };
872
873 static int mt9v022_probe(struct i2c_client *client,
874 const struct i2c_device_id *did)
875 {
876 struct mt9v022 *mt9v022;
877 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
878 struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
879 struct mt9v022_platform_data *pdata;
880 int ret;
881
882 if (!ssdd) {
883 dev_err(&client->dev, "MT9V022 driver needs platform data\n");
884 return -EINVAL;
885 }
886
887 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
888 dev_warn(&adapter->dev,
889 "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
890 return -EIO;
891 }
892
893 mt9v022 = devm_kzalloc(&client->dev, sizeof(struct mt9v022), GFP_KERNEL);
894 if (!mt9v022)
895 return -ENOMEM;
896
897 pdata = ssdd->drv_priv;
898 v4l2_i2c_subdev_init(&mt9v022->subdev, client, &mt9v022_subdev_ops);
899 v4l2_ctrl_handler_init(&mt9v022->hdl, 6);
900 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
901 V4L2_CID_VFLIP, 0, 1, 1, 0);
902 v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
903 V4L2_CID_HFLIP, 0, 1, 1, 0);
904 mt9v022->autogain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
905 V4L2_CID_AUTOGAIN, 0, 1, 1, 1);
906 mt9v022->gain = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
907 V4L2_CID_GAIN, 0, 127, 1, 64);
908
909 /*
910 * Simulated autoexposure. If enabled, we calculate shutter width
911 * ourselves in the driver based on vertical blanking and frame width
912 */
913 mt9v022->autoexposure = v4l2_ctrl_new_std_menu(&mt9v022->hdl,
914 &mt9v022_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
915 V4L2_EXPOSURE_AUTO);
916 mt9v022->exposure = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
917 V4L2_CID_EXPOSURE, 1, 255, 1, 255);
918
919 mt9v022->hblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
920 V4L2_CID_HBLANK, MT9V022_HORIZONTAL_BLANKING_MIN,
921 MT9V022_HORIZONTAL_BLANKING_MAX, 1,
922 MT9V022_HORIZONTAL_BLANKING_DEF);
923
924 mt9v022->vblank = v4l2_ctrl_new_std(&mt9v022->hdl, &mt9v022_ctrl_ops,
925 V4L2_CID_VBLANK, MT9V022_VERTICAL_BLANKING_MIN,
926 MT9V022_VERTICAL_BLANKING_MAX, 1,
927 MT9V022_VERTICAL_BLANKING_DEF);
928
929 mt9v022->subdev.ctrl_handler = &mt9v022->hdl;
930 if (mt9v022->hdl.error) {
931 int err = mt9v022->hdl.error;
932
933 dev_err(&client->dev, "control initialisation err %d\n", err);
934 return err;
935 }
936 v4l2_ctrl_auto_cluster(2, &mt9v022->autoexposure,
937 V4L2_EXPOSURE_MANUAL, true);
938 v4l2_ctrl_auto_cluster(2, &mt9v022->autogain, 0, true);
939
940 mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
941
942 /*
943 * On some platforms the first read out line is corrupted.
944 * Workaround it by skipping if indicated by platform data.
945 */
946 mt9v022->y_skip_top = pdata ? pdata->y_skip_top : 0;
947 mt9v022->rect.left = MT9V022_COLUMN_SKIP;
948 mt9v022->rect.top = MT9V022_ROW_SKIP;
949 mt9v022->rect.width = MT9V022_MAX_WIDTH;
950 mt9v022->rect.height = MT9V022_MAX_HEIGHT;
951
952 mt9v022->clk = v4l2_clk_get(&client->dev, "mclk");
953 if (IS_ERR(mt9v022->clk)) {
954 ret = PTR_ERR(mt9v022->clk);
955 goto eclkget;
956 }
957
958 ret = mt9v022_video_probe(client);
959 if (ret) {
960 v4l2_clk_put(mt9v022->clk);
961 eclkget:
962 v4l2_ctrl_handler_free(&mt9v022->hdl);
963 }
964
965 return ret;
966 }
967
968 static int mt9v022_remove(struct i2c_client *client)
969 {
970 struct mt9v022 *mt9v022 = to_mt9v022(client);
971 struct soc_camera_subdev_desc *ssdd = soc_camera_i2c_to_desc(client);
972
973 v4l2_clk_put(mt9v022->clk);
974 v4l2_device_unregister_subdev(&mt9v022->subdev);
975 if (ssdd->free_bus)
976 ssdd->free_bus(ssdd);
977 v4l2_ctrl_handler_free(&mt9v022->hdl);
978
979 return 0;
980 }
981 static const struct i2c_device_id mt9v022_id[] = {
982 { "mt9v022", 0 },
983 { }
984 };
985 MODULE_DEVICE_TABLE(i2c, mt9v022_id);
986
987 static struct i2c_driver mt9v022_i2c_driver = {
988 .driver = {
989 .name = "mt9v022",
990 },
991 .probe = mt9v022_probe,
992 .remove = mt9v022_remove,
993 .id_table = mt9v022_id,
994 };
995
996 module_i2c_driver(mt9v022_i2c_driver);
997
998 MODULE_DESCRIPTION("Micron MT9V022 Camera driver");
999 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
1000 MODULE_LICENSE("GPL");
This page took 0.053883 seconds and 4 git commands to generate.