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