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