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