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