V4L/DVB (12510): soc-camera: (partially) convert to v4l2-(sub)dev API
[safe/jmp/linux-2.6] / 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               2
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         int model;      /* V4L2_IDENT_MT9T031* codes from v4l2-chip-ident.h */
73         unsigned char autoexposure;
74         u16 xskip;
75         u16 yskip;
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_init(struct soc_camera_device *icd)
166 {
167         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
168
169         return mt9t031_idle(client);
170 }
171
172 static int mt9t031_release(struct soc_camera_device *icd)
173 {
174         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
175
176         return mt9t031_disable(client);
177 }
178
179 static int mt9t031_s_stream(struct v4l2_subdev *sd, int enable)
180 {
181         struct i2c_client *client = sd->priv;
182         int ret;
183
184         if (enable)
185                 /* Switch to master "normal" mode */
186                 ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 2);
187         else
188                 /* Stop sensor readout */
189                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 2);
190
191         if (ret < 0)
192                 return -EIO;
193
194         return 0;
195 }
196
197 static int mt9t031_set_bus_param(struct soc_camera_device *icd,
198                                  unsigned long flags)
199 {
200         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
201
202         /* The caller should have queried our parameters, check anyway */
203         if (flags & ~MT9T031_BUS_PARAM)
204                 return -EINVAL;
205
206         if (flags & SOCAM_PCLK_SAMPLE_FALLING)
207                 reg_clear(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
208         else
209                 reg_set(client, MT9T031_PIXEL_CLOCK_CONTROL, 0x8000);
210
211         return 0;
212 }
213
214 static unsigned long mt9t031_query_bus_param(struct soc_camera_device *icd)
215 {
216         struct soc_camera_link *icl = to_soc_camera_link(icd);
217
218         return soc_camera_apply_sensor_flags(icl, MT9T031_BUS_PARAM);
219 }
220
221 /* Round up minima and round down maxima */
222 static void recalculate_limits(struct soc_camera_device *icd,
223                                u16 xskip, u16 yskip)
224 {
225         icd->x_min = (MT9T031_COLUMN_SKIP + xskip - 1) / xskip;
226         icd->y_min = (MT9T031_ROW_SKIP + yskip - 1) / yskip;
227         icd->width_min = (MT9T031_MIN_WIDTH + xskip - 1) / xskip;
228         icd->height_min = (MT9T031_MIN_HEIGHT + yskip - 1) / yskip;
229         icd->width_max = MT9T031_MAX_WIDTH / xskip;
230         icd->height_max = MT9T031_MAX_HEIGHT / yskip;
231 }
232
233 static int mt9t031_set_params(struct soc_camera_device *icd,
234                               struct v4l2_rect *rect, u16 xskip, u16 yskip)
235 {
236         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
237         struct mt9t031 *mt9t031 = to_mt9t031(client);
238         int ret;
239         u16 xbin, ybin, width, height, left, top;
240         const u16 hblank = MT9T031_HORIZONTAL_BLANK,
241                 vblank = MT9T031_VERTICAL_BLANK;
242
243         /* Make sure we don't exceed sensor limits */
244         if (rect->left + rect->width > icd->width_max)
245                 rect->left = (icd->width_max - rect->width) / 2 + icd->x_min;
246
247         if (rect->top + rect->height > icd->height_max)
248                 rect->top = (icd->height_max - rect->height) / 2 + icd->y_min;
249
250         width = rect->width * xskip;
251         height = rect->height * yskip;
252         left = rect->left * xskip;
253         top = rect->top * yskip;
254
255         xbin = min(xskip, (u16)3);
256         ybin = min(yskip, (u16)3);
257
258         dev_dbg(&icd->dev, "xskip %u, width %u/%u, yskip %u, height %u/%u\n",
259                 xskip, width, rect->width, yskip, height, rect->height);
260
261         /* Could just do roundup(rect->left, [xy]bin * 2); but this is cheaper */
262         switch (xbin) {
263         case 2:
264                 left = (left + 3) & ~3;
265                 break;
266         case 3:
267                 left = roundup(left, 6);
268         }
269
270         switch (ybin) {
271         case 2:
272                 top = (top + 3) & ~3;
273                 break;
274         case 3:
275                 top = roundup(top, 6);
276         }
277
278         /* Disable register update, reconfigure atomically */
279         ret = reg_set(client, MT9T031_OUTPUT_CONTROL, 1);
280         if (ret < 0)
281                 return ret;
282
283         /* Blanking and start values - default... */
284         ret = reg_write(client, MT9T031_HORIZONTAL_BLANKING, hblank);
285         if (ret >= 0)
286                 ret = reg_write(client, MT9T031_VERTICAL_BLANKING, vblank);
287
288         if (yskip != mt9t031->yskip || xskip != mt9t031->xskip) {
289                 /* Binning, skipping */
290                 if (ret >= 0)
291                         ret = reg_write(client, MT9T031_COLUMN_ADDRESS_MODE,
292                                         ((xbin - 1) << 4) | (xskip - 1));
293                 if (ret >= 0)
294                         ret = reg_write(client, MT9T031_ROW_ADDRESS_MODE,
295                                         ((ybin - 1) << 4) | (yskip - 1));
296         }
297         dev_dbg(&icd->dev, "new physical left %u, top %u\n", left, top);
298
299         /* The caller provides a supported format, as guaranteed by
300          * icd->try_fmt_cap(), soc_camera_s_crop() and soc_camera_cropcap() */
301         if (ret >= 0)
302                 ret = reg_write(client, MT9T031_COLUMN_START, left);
303         if (ret >= 0)
304                 ret = reg_write(client, MT9T031_ROW_START, top);
305         if (ret >= 0)
306                 ret = reg_write(client, MT9T031_WINDOW_WIDTH, width - 1);
307         if (ret >= 0)
308                 ret = reg_write(client, MT9T031_WINDOW_HEIGHT,
309                                 height + icd->y_skip_top - 1);
310         if (ret >= 0 && mt9t031->autoexposure) {
311                 ret = set_shutter(client, height + icd->y_skip_top + vblank);
312                 if (ret >= 0) {
313                         const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
314                         const struct v4l2_queryctrl *qctrl =
315                                 soc_camera_find_qctrl(icd->ops,
316                                                       V4L2_CID_EXPOSURE);
317                         icd->exposure = (shutter_max / 2 + (height +
318                                          icd->y_skip_top + vblank - 1) *
319                                          (qctrl->maximum - qctrl->minimum)) /
320                                 shutter_max + qctrl->minimum;
321                 }
322         }
323
324         /* Re-enable register update, commit all changes */
325         if (ret >= 0)
326                 ret = reg_clear(client, MT9T031_OUTPUT_CONTROL, 1);
327
328         return ret < 0 ? ret : 0;
329 }
330
331 static int mt9t031_set_crop(struct soc_camera_device *icd,
332                             struct v4l2_rect *rect)
333 {
334         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
335         struct mt9t031 *mt9t031 = to_mt9t031(client);
336
337         /* CROP - no change in scaling, or in limits */
338         return mt9t031_set_params(icd, rect, mt9t031->xskip, mt9t031->yskip);
339 }
340
341 static int mt9t031_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
342 {
343         struct i2c_client *client = sd->priv;
344         struct mt9t031 *mt9t031 = to_mt9t031(client);
345         struct soc_camera_device *icd = client->dev.platform_data;
346         int ret;
347         u16 xskip, yskip;
348         struct v4l2_rect rect = {
349                 .left   = icd->x_current,
350                 .top    = icd->y_current,
351                 .width  = f->fmt.pix.width,
352                 .height = f->fmt.pix.height,
353         };
354
355         /*
356          * try_fmt has put rectangle within limits.
357          * S_FMT - use binning and skipping for scaling, recalculate
358          * limits, used for cropping
359          */
360         /* Is this more optimal than just a division? */
361         for (xskip = 8; xskip > 1; xskip--)
362                 if (rect.width * xskip <= MT9T031_MAX_WIDTH)
363                         break;
364
365         for (yskip = 8; yskip > 1; yskip--)
366                 if (rect.height * yskip <= MT9T031_MAX_HEIGHT)
367                         break;
368
369         recalculate_limits(icd, xskip, yskip);
370
371         ret = mt9t031_set_params(icd, &rect, xskip, yskip);
372         if (!ret) {
373                 mt9t031->xskip = xskip;
374                 mt9t031->yskip = yskip;
375         }
376
377         return ret;
378 }
379
380 static int mt9t031_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
381 {
382         struct v4l2_pix_format *pix = &f->fmt.pix;
383
384         v4l_bound_align_image(
385                 &pix->width, MT9T031_MIN_WIDTH, MT9T031_MAX_WIDTH, 1,
386                 &pix->height, MT9T031_MIN_HEIGHT, MT9T031_MAX_HEIGHT, 1, 0);
387
388         return 0;
389 }
390
391 static int mt9t031_g_chip_ident(struct v4l2_subdev *sd,
392                                 struct v4l2_dbg_chip_ident *id)
393 {
394         struct i2c_client *client = sd->priv;
395         struct mt9t031 *mt9t031 = to_mt9t031(client);
396
397         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
398                 return -EINVAL;
399
400         if (id->match.addr != client->addr)
401                 return -ENODEV;
402
403         id->ident       = mt9t031->model;
404         id->revision    = 0;
405
406         return 0;
407 }
408
409 #ifdef CONFIG_VIDEO_ADV_DEBUG
410 static int mt9t031_g_register(struct v4l2_subdev *sd,
411                               struct v4l2_dbg_register *reg)
412 {
413         struct i2c_client *client = sd->priv;
414
415         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
416                 return -EINVAL;
417
418         if (reg->match.addr != client->addr)
419                 return -ENODEV;
420
421         reg->val = reg_read(client, reg->reg);
422
423         if (reg->val > 0xffff)
424                 return -EIO;
425
426         return 0;
427 }
428
429 static int mt9t031_s_register(struct v4l2_subdev *sd,
430                               struct v4l2_dbg_register *reg)
431 {
432         struct i2c_client *client = sd->priv;
433
434         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
435                 return -EINVAL;
436
437         if (reg->match.addr != client->addr)
438                 return -ENODEV;
439
440         if (reg_write(client, reg->reg, reg->val) < 0)
441                 return -EIO;
442
443         return 0;
444 }
445 #endif
446
447 static const struct v4l2_queryctrl mt9t031_controls[] = {
448         {
449                 .id             = V4L2_CID_VFLIP,
450                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
451                 .name           = "Flip Vertically",
452                 .minimum        = 0,
453                 .maximum        = 1,
454                 .step           = 1,
455                 .default_value  = 0,
456         }, {
457                 .id             = V4L2_CID_HFLIP,
458                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
459                 .name           = "Flip Horizontally",
460                 .minimum        = 0,
461                 .maximum        = 1,
462                 .step           = 1,
463                 .default_value  = 0,
464         }, {
465                 .id             = V4L2_CID_GAIN,
466                 .type           = V4L2_CTRL_TYPE_INTEGER,
467                 .name           = "Gain",
468                 .minimum        = 0,
469                 .maximum        = 127,
470                 .step           = 1,
471                 .default_value  = 64,
472                 .flags          = V4L2_CTRL_FLAG_SLIDER,
473         }, {
474                 .id             = V4L2_CID_EXPOSURE,
475                 .type           = V4L2_CTRL_TYPE_INTEGER,
476                 .name           = "Exposure",
477                 .minimum        = 1,
478                 .maximum        = 255,
479                 .step           = 1,
480                 .default_value  = 255,
481                 .flags          = V4L2_CTRL_FLAG_SLIDER,
482         }, {
483                 .id             = V4L2_CID_EXPOSURE_AUTO,
484                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
485                 .name           = "Automatic Exposure",
486                 .minimum        = 0,
487                 .maximum        = 1,
488                 .step           = 1,
489                 .default_value  = 1,
490         }
491 };
492
493 static struct soc_camera_ops mt9t031_ops = {
494         .init                   = mt9t031_init,
495         .release                = mt9t031_release,
496         .set_crop               = mt9t031_set_crop,
497         .set_bus_param          = mt9t031_set_bus_param,
498         .query_bus_param        = mt9t031_query_bus_param,
499         .controls               = mt9t031_controls,
500         .num_controls           = ARRAY_SIZE(mt9t031_controls),
501 };
502
503 static int mt9t031_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
504 {
505         struct i2c_client *client = sd->priv;
506         struct mt9t031 *mt9t031 = to_mt9t031(client);
507         int data;
508
509         switch (ctrl->id) {
510         case V4L2_CID_VFLIP:
511                 data = reg_read(client, MT9T031_READ_MODE_2);
512                 if (data < 0)
513                         return -EIO;
514                 ctrl->value = !!(data & 0x8000);
515                 break;
516         case V4L2_CID_HFLIP:
517                 data = reg_read(client, MT9T031_READ_MODE_2);
518                 if (data < 0)
519                         return -EIO;
520                 ctrl->value = !!(data & 0x4000);
521                 break;
522         case V4L2_CID_EXPOSURE_AUTO:
523                 ctrl->value = mt9t031->autoexposure;
524                 break;
525         }
526         return 0;
527 }
528
529 static int mt9t031_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
530 {
531         struct i2c_client *client = sd->priv;
532         struct mt9t031 *mt9t031 = to_mt9t031(client);
533         struct soc_camera_device *icd = client->dev.platform_data;
534         const struct v4l2_queryctrl *qctrl;
535         int data;
536
537         qctrl = soc_camera_find_qctrl(&mt9t031_ops, ctrl->id);
538
539         if (!qctrl)
540                 return -EINVAL;
541
542         switch (ctrl->id) {
543         case V4L2_CID_VFLIP:
544                 if (ctrl->value)
545                         data = reg_set(client, MT9T031_READ_MODE_2, 0x8000);
546                 else
547                         data = reg_clear(client, MT9T031_READ_MODE_2, 0x8000);
548                 if (data < 0)
549                         return -EIO;
550                 break;
551         case V4L2_CID_HFLIP:
552                 if (ctrl->value)
553                         data = reg_set(client, MT9T031_READ_MODE_2, 0x4000);
554                 else
555                         data = reg_clear(client, MT9T031_READ_MODE_2, 0x4000);
556                 if (data < 0)
557                         return -EIO;
558                 break;
559         case V4L2_CID_GAIN:
560                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
561                         return -EINVAL;
562                 /* See Datasheet Table 7, Gain settings. */
563                 if (ctrl->value <= qctrl->default_value) {
564                         /* Pack it into 0..1 step 0.125, register values 0..8 */
565                         unsigned long range = qctrl->default_value - qctrl->minimum;
566                         data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
567
568                         dev_dbg(&icd->dev, "Setting gain %d\n", data);
569                         data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
570                         if (data < 0)
571                                 return -EIO;
572                 } else {
573                         /* Pack it into 1.125..128 variable step, register values 9..0x7860 */
574                         /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
575                         unsigned long range = qctrl->maximum - qctrl->default_value - 1;
576                         /* calculated gain: map 65..127 to 9..1024 step 0.125 */
577                         unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
578                                                1015 + range / 2) / range + 9;
579
580                         if (gain <= 32)         /* calculated gain 9..32 -> 9..32 */
581                                 data = gain;
582                         else if (gain <= 64)    /* calculated gain 33..64 -> 0x51..0x60 */
583                                 data = ((gain - 32) * 16 + 16) / 32 + 80;
584                         else
585                                 /* calculated gain 65..1024 -> (1..120) << 8 + 0x60 */
586                                 data = (((gain - 64 + 7) * 32) & 0xff00) | 0x60;
587
588                         dev_dbg(&icd->dev, "Setting gain from 0x%x to 0x%x\n",
589                                 reg_read(client, MT9T031_GLOBAL_GAIN), data);
590                         data = reg_write(client, MT9T031_GLOBAL_GAIN, data);
591                         if (data < 0)
592                                 return -EIO;
593                 }
594
595                 /* Success */
596                 icd->gain = ctrl->value;
597                 break;
598         case V4L2_CID_EXPOSURE:
599                 /* mt9t031 has maximum == default */
600                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
601                         return -EINVAL;
602                 else {
603                         const unsigned long range = qctrl->maximum - qctrl->minimum;
604                         const u32 shutter = ((ctrl->value - qctrl->minimum) * 1048 +
605                                              range / 2) / range + 1;
606                         u32 old;
607
608                         get_shutter(client, &old);
609                         dev_dbg(&icd->dev, "Setting shutter width from %u to %u\n",
610                                 old, shutter);
611                         if (set_shutter(client, shutter) < 0)
612                                 return -EIO;
613                         icd->exposure = ctrl->value;
614                         mt9t031->autoexposure = 0;
615                 }
616                 break;
617         case V4L2_CID_EXPOSURE_AUTO:
618                 if (ctrl->value) {
619                         const u16 vblank = MT9T031_VERTICAL_BLANK;
620                         const u32 shutter_max = MT9T031_MAX_HEIGHT + vblank;
621                         if (set_shutter(client, icd->height +
622                                         icd->y_skip_top + vblank) < 0)
623                                 return -EIO;
624                         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
625                         icd->exposure = (shutter_max / 2 + (icd->height +
626                                          icd->y_skip_top + vblank - 1) *
627                                          (qctrl->maximum - qctrl->minimum)) /
628                                 shutter_max + qctrl->minimum;
629                         mt9t031->autoexposure = 1;
630                 } else
631                         mt9t031->autoexposure = 0;
632                 break;
633         }
634         return 0;
635 }
636
637 /* Interface active, can use i2c. If it fails, it can indeed mean, that
638  * this wasn't our capture interface, so, we wait for the right one */
639 static int mt9t031_video_probe(struct i2c_client *client)
640 {
641         struct soc_camera_device *icd = client->dev.platform_data;
642         struct mt9t031 *mt9t031 = to_mt9t031(client);
643         s32 data;
644
645         /* We must have a parent by now. And it cannot be a wrong one.
646          * So this entire test is completely redundant. */
647         if (!icd->dev.parent ||
648             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
649                 return -ENODEV;
650
651         /* Enable the chip */
652         data = reg_write(client, MT9T031_CHIP_ENABLE, 1);
653         dev_dbg(&icd->dev, "write: %d\n", data);
654
655         /* Read out the chip version register */
656         data = reg_read(client, MT9T031_CHIP_VERSION);
657
658         switch (data) {
659         case 0x1621:
660                 mt9t031->model = V4L2_IDENT_MT9T031;
661                 icd->formats = mt9t031_colour_formats;
662                 icd->num_formats = ARRAY_SIZE(mt9t031_colour_formats);
663                 break;
664         default:
665                 dev_err(&icd->dev,
666                         "No MT9T031 chip detected, register read %x\n", data);
667                 return -ENODEV;
668         }
669
670         dev_info(&icd->dev, "Detected a MT9T031 chip ID %x\n", data);
671
672         return 0;
673 }
674
675 static struct v4l2_subdev_core_ops mt9t031_subdev_core_ops = {
676         .g_ctrl         = mt9t031_g_ctrl,
677         .s_ctrl         = mt9t031_s_ctrl,
678         .g_chip_ident   = mt9t031_g_chip_ident,
679 #ifdef CONFIG_VIDEO_ADV_DEBUG
680         .g_register     = mt9t031_g_register,
681         .s_register     = mt9t031_s_register,
682 #endif
683 };
684
685 static struct v4l2_subdev_video_ops mt9t031_subdev_video_ops = {
686         .s_stream       = mt9t031_s_stream,
687         .s_fmt          = mt9t031_s_fmt,
688         .try_fmt        = mt9t031_try_fmt,
689 };
690
691 static struct v4l2_subdev_ops mt9t031_subdev_ops = {
692         .core   = &mt9t031_subdev_core_ops,
693         .video  = &mt9t031_subdev_video_ops,
694 };
695
696 static int mt9t031_probe(struct i2c_client *client,
697                          const struct i2c_device_id *did)
698 {
699         struct mt9t031 *mt9t031;
700         struct soc_camera_device *icd = client->dev.platform_data;
701         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
702         struct soc_camera_link *icl;
703         int ret;
704
705         if (!icd) {
706                 dev_err(&client->dev, "MT9T031: missing soc-camera data!\n");
707                 return -EINVAL;
708         }
709
710         icl = to_soc_camera_link(icd);
711         if (!icl) {
712                 dev_err(&client->dev, "MT9T031 driver needs platform data\n");
713                 return -EINVAL;
714         }
715
716         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
717                 dev_warn(&adapter->dev,
718                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
719                 return -EIO;
720         }
721
722         mt9t031 = kzalloc(sizeof(struct mt9t031), GFP_KERNEL);
723         if (!mt9t031)
724                 return -ENOMEM;
725
726         v4l2_i2c_subdev_init(&mt9t031->subdev, client, &mt9t031_subdev_ops);
727
728         /* Second stage probe - when a capture adapter is there */
729         icd->ops        = &mt9t031_ops;
730         icd->x_min      = MT9T031_COLUMN_SKIP;
731         icd->y_min      = MT9T031_ROW_SKIP;
732         icd->x_current  = icd->x_min;
733         icd->y_current  = icd->y_min;
734         icd->width_min  = MT9T031_MIN_WIDTH;
735         icd->width_max  = MT9T031_MAX_WIDTH;
736         icd->height_min = MT9T031_MIN_HEIGHT;
737         icd->height_max = MT9T031_MAX_HEIGHT;
738         icd->y_skip_top = 0;
739         /* Simulated autoexposure. If enabled, we calculate shutter width
740          * ourselves in the driver based on vertical blanking and frame width */
741         mt9t031->autoexposure = 1;
742
743         mt9t031->xskip = 1;
744         mt9t031->yskip = 1;
745
746         mt9t031_idle(client);
747
748         ret = mt9t031_video_probe(client);
749
750         mt9t031_disable(client);
751
752         if (ret) {
753                 icd->ops = NULL;
754                 i2c_set_clientdata(client, NULL);
755                 kfree(mt9t031);
756         }
757
758         return ret;
759 }
760
761 static int mt9t031_remove(struct i2c_client *client)
762 {
763         struct mt9t031 *mt9t031 = to_mt9t031(client);
764         struct soc_camera_device *icd = client->dev.platform_data;
765
766         icd->ops = NULL;
767         i2c_set_clientdata(client, NULL);
768         client->driver = NULL;
769         kfree(mt9t031);
770
771         return 0;
772 }
773
774 static const struct i2c_device_id mt9t031_id[] = {
775         { "mt9t031", 0 },
776         { }
777 };
778 MODULE_DEVICE_TABLE(i2c, mt9t031_id);
779
780 static struct i2c_driver mt9t031_i2c_driver = {
781         .driver = {
782                 .name = "mt9t031",
783         },
784         .probe          = mt9t031_probe,
785         .remove         = mt9t031_remove,
786         .id_table       = mt9t031_id,
787 };
788
789 static int __init mt9t031_mod_init(void)
790 {
791         return i2c_add_driver(&mt9t031_i2c_driver);
792 }
793
794 static void __exit mt9t031_mod_exit(void)
795 {
796         i2c_del_driver(&mt9t031_i2c_driver);
797 }
798
799 module_init(mt9t031_mod_init);
800 module_exit(mt9t031_mod_exit);
801
802 MODULE_DESCRIPTION("Micron MT9T031 Camera driver");
803 MODULE_AUTHOR("Guennadi Liakhovetski <lg@denx.de>");
804 MODULE_LICENSE("GPL v2");