usbvision: revert commit 588afcc1
[pandora-kernel.git] / drivers / media / video / mt9t031.c
1 /*
2  * Driver for MT9T031 CMOS Image Sensor from Micron
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski, DENX Software Engineering <lg@denx.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/device.h>
12 #include <linux/i2c.h>
13 #include <linux/log2.h>
14 #include <linux/pm.h>
15 #include <linux/slab.h>
16 #include <linux/v4l2-mediabus.h>
17 #include <linux/videodev2.h>
18 #include <linux/module.h>
19
20 #include <media/soc_camera.h>
21 #include <media/v4l2-chip-ident.h>
22 #include <media/v4l2-subdev.h>
23 #include <media/v4l2-ctrls.h>
24
25 /*
26  * ATTENTION: this driver still cannot be used outside of the soc-camera
27  * framework because of its PM implementation, using the video_device node.
28  * If hardware becomes available for testing, alternative PM approaches shall
29  * be considered and tested.
30  */
31
32 /*
33  * mt9t031 i2c address 0x5d
34  * The platform has to define i2c_board_info and link to it from
35  * struct soc_camera_link
36  */
37
38 /* mt9t031 selected register addresses */
39 #define MT9T031_CHIP_VERSION            0x00
40 #define MT9T031_ROW_START               0x01
41 #define MT9T031_COLUMN_START            0x02
42 #define MT9T031_WINDOW_HEIGHT           0x03
43 #define MT9T031_WINDOW_WIDTH            0x04
44 #define MT9T031_HORIZONTAL_BLANKING     0x05
45 #define MT9T031_VERTICAL_BLANKING       0x06
46 #define MT9T031_OUTPUT_CONTROL          0x07
47 #define MT9T031_SHUTTER_WIDTH_UPPER     0x08
48 #define MT9T031_SHUTTER_WIDTH           0x09
49 #define MT9T031_PIXEL_CLOCK_CONTROL     0x0a
50 #define MT9T031_FRAME_RESTART           0x0b
51 #define MT9T031_SHUTTER_DELAY           0x0c
52 #define MT9T031_RESET                   0x0d
53 #define MT9T031_READ_MODE_1             0x1e
54 #define MT9T031_READ_MODE_2             0x20
55 #define MT9T031_READ_MODE_3             0x21
56 #define MT9T031_ROW_ADDRESS_MODE        0x22
57 #define MT9T031_COLUMN_ADDRESS_MODE     0x23
58 #define MT9T031_GLOBAL_GAIN             0x35
59 #define MT9T031_CHIP_ENABLE             0xF8
60
61 #define MT9T031_MAX_HEIGHT              1536
62 #define MT9T031_MAX_WIDTH               2048
63 #define MT9T031_MIN_HEIGHT              2
64 #define MT9T031_MIN_WIDTH               18
65 #define MT9T031_HORIZONTAL_BLANK        142
66 #define MT9T031_VERTICAL_BLANK          25
67 #define MT9T031_COLUMN_SKIP             32
68 #define MT9T031_ROW_SKIP                20
69
70 struct mt9t031 {
71         struct v4l2_subdev subdev;
72         struct v4l2_ctrl_handler hdl;
73         struct {
74                 /* exposure/auto-exposure cluster */
75                 struct v4l2_ctrl *autoexposure;
76                 struct v4l2_ctrl *exposure;
77         };
78         struct v4l2_rect rect;  /* Sensor window */
79         int model;      /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
80         u16 xskip;
81         u16 yskip;
82         unsigned int total_h;
83         unsigned short y_skip_top;      /* Lines to skip at the top */
84 };
85
86 static struct mt9t031 *to_mt9t031(const struct i2c_client *client)
87 {
88         return container_of(i2c_get_clientdata(client), struct mt9t031, subdev);
89 }
90
91 static int reg_read(struct i2c_client *client, const u8 reg)
92 {
93         s32 data = i2c_smbus_read_word_data(client, reg);
94         return data < 0 ? data : swab16(data);
95 }
96
97 static int reg_write(struct i2c_client *client, const u8 reg,
98                      const u16 data)
99 {
100         return i2c_smbus_write_word_data(client, reg, swab16(data));
101 }
102
103 static int reg_set(struct i2c_client *client, const u8 reg,
104                    const u16 data)
105 {
106         int ret;
107
108         ret = reg_read(client, reg);
109         if (ret < 0)
110                 return ret;
111         return reg_write(client, reg, ret | data);
112 }
113
114 static int reg_clear(struct i2c_client *client, const u8 reg,
115                      const u16 data)
116 {
117         int ret;
118
119         ret = reg_read(client, reg);
120         if (ret < 0)
121                 return ret;
122         return reg_write(client, reg, ret & ~data);
123 }
124
125 static int set_shutter(struct i2c_client *client, const u32 data)
126 {
127         int ret;
128
129         ret = reg_write(client, MT9T031_SHUTTER_WIDTH_UPPER, data >> 16);
130
131         if (ret >= 0)
132                 ret = reg_write(client, MT9T031_SHUTTER_WIDTH, data & 0xffff);
133
134         return ret;
135 }
136
137 static int get_shutter(struct i2c_client *client, u32 *data)
138 {
139         int ret;
140
141         ret = reg_read(client, MT9T031_SHUTTER_WIDTH_UPPER);
142         *data = ret << 16;
143
144         if (ret >= 0)
145                 ret = reg_read(client, MT9T031_SHUTTER_WIDTH);
146         *data |= ret & 0xffff;
147
148         return ret < 0 ? ret : 0;
149 }
150
151 static int mt9t031_idle(struct i2c_client *client)
152 {
153         int ret;
154
155         /* Disable chip output, synchronous option update */
156         ret = reg_write(client, MT9T031_RESET, 1);
157         if (ret >= 0)
158                 ret = reg_write(client, MT9T031_RESET, 0);
159         if (ret >= 0)
160                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
161
162         return ret >= 0 ? 0 : -EIO;
163 }
164
165 static int mt9t031_disable(struct i2c_client *client)
166 {
167         /* Disable the chip */
168         reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
169
170         return 0;
171 }
172
173 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
174 {
175         struct i2c_client *client = v4l2_get_subdevdata(sd);
176         int ret;
177
178         if (enable)
179                 /* Switch to master "normal" mode */
180                 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
181         else
182                 /* Stop sensor readout */
183                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
184
185         if (ret < 0)
186                 return -EIO;
187
188         return 0;
189 }
190
191 /* target must be _even_ */
192 static u16 mt9t031_skip(s32 *source, s32 target, s32 max)
193 {
194         unsigned int skip;
195
196         if (*source < target + target / 2) {
197                 *source = target;
198                 return 1;
199         }
200
201         skip = min(max, *source + target / 2) / target;
202         if (skip > 8)
203                 skip = 8;
204         *source = target * skip;
205
206         return skip;
207 }
208
209 /* rect is the sensor rectangle, the caller guarantees parameter validity */
210 static int mt9t031_set_params(struct i2c_client *client,
211                               struct v4l2_rect *rect, u16 xskip, u16 yskip)
212 {
213         struct mt9t031 *mt9t031 = to_mt9t031(client);
214         int ret;
215         u16 xbin, ybin;
216         const u16 hblank = MT9T031_HORIZONTAL_BLANK,
217                 vblank = MT9T031_VERTICAL_BLANK;
218
219         xbin = min(xskip, (u16)3);
220         ybin = min(yskip, (u16)3);
221
222         /*
223          * Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper.
224          * There is always a valid suitably aligned value. The worst case is
225          * xbin = 3, width = 2048. Then we will start at 36, the last read out
226          * pixel will be 2083, which is < 2085 - first black pixel.
227          *
228          * MT9T031 datasheet imposes window left border alignment, depending on
229          * the selected xskip. Failing to conform to this requirement produces
230          * dark horizontal stripes in the image. However, even obeying to this
231          * requirement doesn't eliminate the stripes in all configurations. They
232          * appear "locally reproducibly," but can differ between tests under
233          * different lighting conditions.
234          */
235         switch (xbin) {
236         case 1:
237                 rect->left &= ~1;
238                 break;
239         case 2:
240                 rect->left &= ~3;
241                 break;
242         case 3:
243                 rect->left = rect->left > roundup(MT9T031_COLUMN_SKIP, 6) ?
244                         (rect->left / 6) * 6 : roundup(MT9T031_COLUMN_SKIP, 6);
245         }
246
247         rect->top &= ~1;
248
249         dev_dbg(&client->dev, "skip %u:%u, rect %ux%u@%u:%u\n",
250                 xskip, yskip, rect->width, rect->height, rect->left, rect->top);
251
252         /* Disable register update, reconfigure atomically */
253         ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
254         if (ret < 0)
255                 return ret;
256
257         /* Blanking and start values - default... */
258         ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
259         if (ret >= 0)
260                 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
261
262         if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
263                 /* Binning, skipping */
264                 if (ret >= 0)
265                         ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
266                                         ((xbin - 1) << 4) | (xskip - 1));
267                 if (ret >= 0)
268                         ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
269                                         ((ybin - 1) << 4) | (yskip - 1));
270         }
271         dev_dbg(&client->dev, "new physical left %u, top %u\n",
272                 rect->left, rect->top);
273
274         /*
275          * The caller provides a supported format, as guaranteed by
276          * .try_mbus_fmt(), soc_camera_s_crop() and soc_camera_cropcap()
277          */
278         if (ret >= 0)
279                 ret = reg_write(client, MT9T031_COLUMN_START, rect->left);
280         if (ret >= 0)
281                 ret = reg_write(client, MT9T031_ROW_START, rect->top);
282         if (ret >= 0)
283                 ret = reg_write(client, MT9T031_WINDOW_WIDTH, rect->width - 1);
284         if (ret >= 0)
285                 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
286                                 rect->height + mt9t031->y_skip_top - 1);
287         if (ret >= 0 && v4l2_ctrl_g_ctrl(mt9t031->autoexposure) == V4L2_EXPOSURE_AUTO) {
288                 mt9t031->total_h = rect->height + mt9t031->y_skip_top + vblank;
289
290                 ret = set_shutter(client, mt9t031->total_h);
291         }
292
293         /* Re-enable register update, commit all changes */
294         if (ret >= 0)
295                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
296
297         if (ret >= 0) {
298                 mt9t031->rect = *rect;
299                 mt9t031->xskip = xskip;
300                 mt9t031->yskip = yskip;
301         }
302
303         return ret < 0 ? ret : 0;
304 }
305
306 static int mt9t031_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
307 {
308         struct v4l2_rect rect = a->c;
309         struct i2c_client *client = v4l2_get_subdevdata(sd);
310         struct mt9t031 *mt9t031 = to_mt9t031(client);
311
312         rect.width = ALIGN(rect.width, 2);
313         rect.height = ALIGN(rect.height, 2);
314
315         soc_camera_limit_side(&rect.left, &rect.width,
316                      MT9T031_COLUMN_SKIP, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH);
317
318         soc_camera_limit_side(&rect.top, &rect.height,
319                      MT9T031_ROW_SKIP, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT);
320
321         return mt9t031_set_params(client, &rect, mt9t031->xskip, mt9t031->yskip);
322 }
323
324 static int mt9t031_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
325 {
326         struct i2c_client *client = v4l2_get_subdevdata(sd);
327         struct mt9t031 *mt9t031 = to_mt9t031(client);
328
329         a->c    = mt9t031->rect;
330         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
331
332         return 0;
333 }
334
335 static int mt9t031_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
336 {
337         a->bounds.left                  = MT9T031_COLUMN_SKIP;
338         a->bounds.top                   = MT9T031_ROW_SKIP;
339         a->bounds.width                 = MT9T031_MAX_WIDTH;
340         a->bounds.height                = MT9T031_MAX_HEIGHT;
341         a->defrect                      = a->bounds;
342         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
343         a->pixelaspect.numerator        = 1;
344         a->pixelaspect.denominator      = 1;
345
346         return 0;
347 }
348
349 static int mt9t031_g_fmt(struct v4l2_subdev *sd,
350                          struct v4l2_mbus_framefmt *mf)
351 {
352         struct i2c_client *client = v4l2_get_subdevdata(sd);
353         struct mt9t031 *mt9t031 = to_mt9t031(client);
354
355         mf->width       = mt9t031->rect.width / mt9t031->xskip;
356         mf->height      = mt9t031->rect.height / mt9t031->yskip;
357         mf->code        = V4L2_MBUS_FMT_SBGGR10_1X10;
358         mf->colorspace  = V4L2_COLORSPACE_SRGB;
359         mf->field       = V4L2_FIELD_NONE;
360
361         return 0;
362 }
363
364 static int mt9t031_s_fmt(struct v4l2_subdev *sd,
365                          struct v4l2_mbus_framefmt *mf)
366 {
367         struct i2c_client *client = v4l2_get_subdevdata(sd);
368         struct mt9t031 *mt9t031 = to_mt9t031(client);
369         u16 xskip, yskip;
370         struct v4l2_rect rect = mt9t031->rect;
371
372         /*
373          * try_fmt has put width and height within limits.
374          * S_FMT: use binning and skipping for scaling
375          */
376         xskip = mt9t031_skip(&rect.width, mf->width, MT9T031_MAX_WIDTH);
377         yskip = mt9t031_skip(&rect.height, mf->height, MT9T031_MAX_HEIGHT);
378
379         mf->code        = V4L2_MBUS_FMT_SBGGR10_1X10;
380         mf->colorspace  = V4L2_COLORSPACE_SRGB;
381
382         /* mt9t031_set_params() doesn't change width and height */
383         return mt9t031_set_params(client, &rect, xskip, yskip);
384 }
385
386 /*
387  * If a user window larger than sensor window is requested, we'll increase the
388  * sensor window.
389  */
390 static int mt9t031_try_fmt(struct v4l2_subdev *sd,
391                            struct v4l2_mbus_framefmt *mf)
392 {
393         v4l_bound_align_image(
394                 &mf->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
395                 &mf->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
396
397         mf->code        = V4L2_MBUS_FMT_SBGGR10_1X10;
398         mf->colorspace  = V4L2_COLORSPACE_SRGB;
399
400         return 0;
401 }
402
403 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
404                                 struct v4l2_dbg_chip_ident *id)
405 {
406         struct i2c_client *client = v4l2_get_subdevdata(sd);
407         struct mt9t031 *mt9t031 = to_mt9t031(client);
408
409         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
410                 return -EINVAL;
411
412         if (id->match.addr != client->addr)
413                 return -ENODEV;
414
415         id->ident       = mt9t031->model;
416         id->revision    = 0;
417
418         return 0;
419 }
420
421 #ifdef CONFIG_VIDEO_ADV_DEBUG
422 static int mt9t031_g_register(struct v4l2_subdev *sd,
423                               struct v4l2_dbg_register *reg)
424 {
425         struct i2c_client *client = v4l2_get_subdevdata(sd);
426
427         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
428                 return -EINVAL;
429
430         if (reg->match.addr != client->addr)
431                 return -ENODEV;
432
433         reg->val = reg_read(client, reg->reg);
434
435         if (reg->val > 0xffff)
436                 return -EIO;
437
438         return 0;
439 }
440
441 static int mt9t031_s_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         if (reg_write(client, reg->reg, reg->val) < 0)
453                 return -EIO;
454
455         return 0;
456 }
457 #endif
458
459 static int mt9t031_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
460 {
461         struct mt9t031 *mt9t031 = container_of(ctrl->handler,
462                                                struct mt9t031, hdl);
463         const u32 shutter_max = MT9T031_MAX_HEIGHT + MT9T031_VERTICAL_BLANK;
464         s32 min, max;
465
466         switch (ctrl->id) {
467         case V4L2_CID_EXPOSURE_AUTO:
468                 min = mt9t031->exposure->minimum;
469                 max = mt9t031->exposure->maximum;
470                 mt9t031->exposure->val =
471                         (shutter_max / 2 + (mt9t031->total_h - 1) * (max - min))
472                                 / shutter_max + min;
473                 break;
474         }
475         return 0;
476 }
477
478 static int mt9t031_s_ctrl(struct v4l2_ctrl *ctrl)
479 {
480         struct mt9t031 *mt9t031 = container_of(ctrl->handler,
481                                                struct mt9t031, hdl);
482         struct v4l2_subdev *sd = &mt9t031->subdev;
483         struct i2c_client *client = v4l2_get_subdevdata(sd);
484         struct v4l2_ctrl *exp = mt9t031->exposure;
485         int data;
486
487         switch (ctrl->id) {
488         case V4L2_CID_VFLIP:
489                 if (ctrl->val)
490                         data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
491                 else
492                         data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
493                 if (data < 0)
494                         return -EIO;
495                 return 0;
496         case V4L2_CID_HFLIP:
497                 if (ctrl->val)
498                         data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
499                 else
500                         data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
501                 if (data < 0)
502                         return -EIO;
503                 return 0;
504         case V4L2_CID_GAIN:
505                 /* See Datasheet Table 7, Gain settings. */
506                 if (ctrl->val <= ctrl->default_value) {
507                         /* Pack it into 0..1 step 0.125, register values 0..8 */
508                         unsigned long range = ctrl->default_value - ctrl->minimum;
509                         data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
510
511                         dev_dbg(&client->dev, "Setting gain %d\n", data);
512                         data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
513                         if (data < 0)
514                                 return -EIO;
515                 } else {
516                         /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
517                         /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
518                         unsigned long range = ctrl->maximum - ctrl->default_value - 1;
519                         /* calculated gain: map 65..127 to 9..1024 step 0.125 */
520                         unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
521                                                1015 + range / 2) / range + 9;
522
523                         if (gain <= 32)         /* calculated gain 9..32 -> 9..32 */
524                                 data = gain;
525                         else if (gain <= 64)    /* calculated gain 33..64 -> 0x51..0x60 */
526                                 data = ((gain - 32) * 16 + 16) / 32 + 80;
527                         else
528                                 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
529                                 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
530
531                         dev_dbg(&client->dev, "Set gain from 0x%x to 0x%x\n",
532                                 reg_read(client, MT9T031_GLOBAL_GAIN), data);
533                         data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
534                         if (data < 0)
535                                 return -EIO;
536                 }
537                 return 0;
538
539         case V4L2_CID_EXPOSURE_AUTO:
540                 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
541                         unsigned int range = exp->maximum - exp->minimum;
542                         unsigned int shutter = ((exp->val - exp->minimum) * 1048 +
543                                                  range / 2) / range + 1;
544                         u32 old;
545
546                         get_shutter(client, &old);
547                         dev_dbg(&client->dev, "Set shutter from %u to %u\n",
548                                 old, shutter);
549                         if (set_shutter(client, shutter) < 0)
550                                 return -EIO;
551                 } else {
552                         const u16 vblank = MT9T031_VERTICAL_BLANK;
553                         mt9t031->total_h = mt9t031->rect.height +
554                                 mt9t031->y_skip_top + vblank;
555
556                         if (set_shutter(client, mt9t031->total_h) < 0)
557                                 return -EIO;
558                 }
559                 return 0;
560         default:
561                 return -EINVAL;
562         }
563         return 0;
564 }
565
566 /*
567  * Power Management:
568  * This function does nothing for now but must be present for pm to work
569  */
570 static int mt9t031_runtime_suspend(struct device *dev)
571 {
572         return 0;
573 }
574
575 /*
576  * Power Management:
577  * COLUMN_ADDRESS_MODE and ROW_ADDRESS_MODE are not rewritten if unchanged
578  * they are however changed at reset if the platform hook is present
579  * thus we rewrite them with the values stored by the driver
580  */
581 static int mt9t031_runtime_resume(struct device *dev)
582 {
583         struct video_device *vdev = to_video_device(dev);
584         struct v4l2_subdev *sd = soc_camera_vdev_to_subdev(vdev);
585         struct i2c_client *client = v4l2_get_subdevdata(sd);
586         struct mt9t031 *mt9t031 = to_mt9t031(client);
587
588         int ret;
589         u16 xbin, ybin;
590
591         xbin = min(mt9t031->xskip, (u16)3);
592         ybin = min(mt9t031->yskip, (u16)3);
593
594         ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
595                 ((xbin - 1) << 4) | (mt9t031->xskip - 1));
596         if (ret < 0)
597                 return ret;
598
599         ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
600                 ((ybin - 1) << 4) | (mt9t031->yskip - 1));
601         if (ret < 0)
602                 return ret;
603
604         return 0;
605 }
606
607 static struct dev_pm_ops mt9t031_dev_pm_ops = {
608         .runtime_suspend        = mt9t031_runtime_suspend,
609         .runtime_resume         = mt9t031_runtime_resume,
610 };
611
612 static struct device_type mt9t031_dev_type = {
613         .name   = "MT9T031",
614         .pm     = &mt9t031_dev_pm_ops,
615 };
616
617 static int mt9t031_s_power(struct v4l2_subdev *sd, int on)
618 {
619         struct i2c_client *client = v4l2_get_subdevdata(sd);
620         struct video_device *vdev = soc_camera_i2c_to_vdev(client);
621
622         if (on)
623                 vdev->dev.type = &mt9t031_dev_type;
624         else
625                 vdev->dev.type = NULL;
626
627         return 0;
628 }
629
630 /*
631  * Interface active, can use i2c. If it fails, it can indeed mean, that
632  * this wasn't our capture interface, so, we wait for the right one
633  */
634 static int mt9t031_video_probe(struct i2c_client *client)
635 {
636         struct mt9t031 *mt9t031 = to_mt9t031(client);
637         s32 data;
638         int ret;
639
640         /* Enable the chip */
641         data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
642         dev_dbg(&client->dev, "write: %d\n", data);
643
644         /* Read out the chip version register */
645         data = reg_read(client, MT9T031_CHIP_VERSION);
646
647         switch (data) {
648         case 0x1621:
649                 mt9t031->model = V4L2_IDENT_MT9T031;
650                 break;
651         default:
652                 dev_err(&client->dev,
653                         "No MT9T031 chip detected, register read %x\n", data);
654                 return -ENODEV;
655         }
656
657         dev_info(&client->dev, "Detected a MT9T031 chip ID %x\n", data);
658
659         ret = mt9t031_idle(client);
660         if (ret < 0)
661                 dev_err(&client->dev, "Failed to initialise the camera\n");
662         else
663                 v4l2_ctrl_handler_setup(&mt9t031->hdl);
664
665         return ret;
666 }
667
668 static int mt9t031_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
669 {
670         struct i2c_client *client = v4l2_get_subdevdata(sd);
671         struct mt9t031 *mt9t031 = to_mt9t031(client);
672
673         *lines = mt9t031->y_skip_top;
674
675         return 0;
676 }
677
678 static const struct v4l2_ctrl_ops mt9t031_ctrl_ops = {
679         .g_volatile_ctrl = mt9t031_g_volatile_ctrl,
680         .s_ctrl = mt9t031_s_ctrl,
681 };
682
683 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
684         .g_chip_ident   = mt9t031_g_chip_ident,
685         .s_power        = mt9t031_s_power,
686 #ifdef CONFIG_VIDEO_ADV_DEBUG
687         .g_register     = mt9t031_g_register,
688         .s_register     = mt9t031_s_register,
689 #endif
690 };
691
692 static int mt9t031_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
693                             enum v4l2_mbus_pixelcode *code)
694 {
695         if (index)
696                 return -EINVAL;
697
698         *code = V4L2_MBUS_FMT_SBGGR10_1X10;
699         return 0;
700 }
701
702 static int mt9t031_g_mbus_config(struct v4l2_subdev *sd,
703                                 struct v4l2_mbus_config *cfg)
704 {
705         struct i2c_client *client = v4l2_get_subdevdata(sd);
706         struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
707
708         cfg->flags = V4L2_MBUS_MASTER | V4L2_MBUS_PCLK_SAMPLE_RISING |
709                 V4L2_MBUS_PCLK_SAMPLE_FALLING | V4L2_MBUS_HSYNC_ACTIVE_HIGH |
710                 V4L2_MBUS_VSYNC_ACTIVE_HIGH | V4L2_MBUS_DATA_ACTIVE_HIGH;
711         cfg->type = V4L2_MBUS_PARALLEL;
712         cfg->flags = soc_camera_apply_board_flags(icl, cfg);
713
714         return 0;
715 }
716
717 static int mt9t031_s_mbus_config(struct v4l2_subdev *sd,
718                                 const struct v4l2_mbus_config *cfg)
719 {
720         struct i2c_client *client = v4l2_get_subdevdata(sd);
721         struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
722
723         if (soc_camera_apply_board_flags(icl, cfg) &
724             V4L2_MBUS_PCLK_SAMPLE_FALLING)
725                 return reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
726         else
727                 return reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
728 }
729
730 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
731         .s_stream       = mt9t031_s_stream,
732         .s_mbus_fmt     = mt9t031_s_fmt,
733         .g_mbus_fmt     = mt9t031_g_fmt,
734         .try_mbus_fmt   = mt9t031_try_fmt,
735         .s_crop         = mt9t031_s_crop,
736         .g_crop         = mt9t031_g_crop,
737         .cropcap        = mt9t031_cropcap,
738         .enum_mbus_fmt  = mt9t031_enum_fmt,
739         .g_mbus_config  = mt9t031_g_mbus_config,
740         .s_mbus_config  = mt9t031_s_mbus_config,
741 };
742
743 static struct v4l2_subdev_sensor_ops mt9t031_subdev_sensor_ops = {
744         .g_skip_top_lines       = mt9t031_g_skip_top_lines,
745 };
746
747 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
748         .core   = &mt9t031_subdev_core_ops,
749         .video  = &mt9t031_subdev_video_ops,
750         .sensor = &mt9t031_subdev_sensor_ops,
751 };
752
753 static int mt9t031_probe(struct i2c_client *client,
754                          const struct i2c_device_id *did)
755 {
756         struct mt9t031 *mt9t031;
757         struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
758         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
759         int ret;
760
761         if (!icl) {
762                 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
763                 return -EINVAL;
764         }
765
766         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
767                 dev_warn(&adapter->dev,
768                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
769                 return -EIO;
770         }
771
772         mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
773         if (!mt9t031)
774                 return -ENOMEM;
775
776         v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
777         v4l2_ctrl_handler_init(&mt9t031->hdl, 5);
778         v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
779                         V4L2_CID_VFLIP, 0, 1, 1, 0);
780         v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
781                         V4L2_CID_HFLIP, 0, 1, 1, 0);
782         v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
783                         V4L2_CID_GAIN, 0, 127, 1, 64);
784
785         /*
786          * Simulated autoexposure. If enabled, we calculate shutter width
787          * ourselves in the driver based on vertical blanking and frame width
788          */
789         mt9t031->autoexposure = v4l2_ctrl_new_std_menu(&mt9t031->hdl,
790                         &mt9t031_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
791                         V4L2_EXPOSURE_AUTO);
792         mt9t031->exposure = v4l2_ctrl_new_std(&mt9t031->hdl, &mt9t031_ctrl_ops,
793                         V4L2_CID_EXPOSURE, 1, 255, 1, 255);
794
795         mt9t031->subdev.ctrl_handler = &mt9t031->hdl;
796         if (mt9t031->hdl.error) {
797                 int err = mt9t031->hdl.error;
798
799                 kfree(mt9t031);
800                 return err;
801         }
802         v4l2_ctrl_auto_cluster(2, &mt9t031->autoexposure,
803                                 V4L2_EXPOSURE_MANUAL, true);
804
805         mt9t031->y_skip_top     = 0;
806         mt9t031->rect.left      = MT9T031_COLUMN_SKIP;
807         mt9t031->rect.top       = MT9T031_ROW_SKIP;
808         mt9t031->rect.width     = MT9T031_MAX_WIDTH;
809         mt9t031->rect.height    = MT9T031_MAX_HEIGHT;
810
811         mt9t031->xskip = 1;
812         mt9t031->yskip = 1;
813
814         mt9t031_idle(client);
815
816         ret = mt9t031_video_probe(client);
817
818         mt9t031_disable(client);
819
820         if (ret) {
821                 v4l2_ctrl_handler_free(&mt9t031->hdl);
822                 kfree(mt9t031);
823         }
824
825         return ret;
826 }
827
828 static int mt9t031_remove(struct i2c_client *client)
829 {
830         struct mt9t031 *mt9t031 = to_mt9t031(client);
831
832         v4l2_device_unregister_subdev(&mt9t031->subdev);
833         v4l2_ctrl_handler_free(&mt9t031->hdl);
834         kfree(mt9t031);
835
836         return 0;
837 }
838
839 static const struct i2c_device_id mt9t031_id[] = {
840         { "mt9t031", 0 },
841         { }
842 };
843 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
844
845 static struct i2c_driver mt9t031_i2c_driver = {
846         .driver = {
847                 .name = "mt9t031",
848         },
849         .probe          = mt9t031_probe,
850         .remove         = mt9t031_remove,
851         .id_table       = mt9t031_id,
852 };
853
854 static int __init mt9t031_mod_init(void)
855 {
856         return i2c_add_driver(&mt9t031_i2c_driver);
857 }
858
859 static void __exit mt9t031_mod_exit(void)
860 {
861         i2c_del_driver(&mt9t031_i2c_driver);
862 }
863
864 module_init(mt9t031_mod_init);
865 module_exit(mt9t031_mod_exit);
866
867 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
868 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
869 MODULE_LICENSE("GPL v2");