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