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