Merge branch 'core-debug-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / drivers / media / video / mt9m001.c
1 /*
2  * Driver for MT9M001 CMOS Image Sensor from Micron
3  *
4  * Copyright (C) 2008, Guennadi Liakhovetski <kernel@pengutronix.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-common.h>
17 #include <media/v4l2-chip-ident.h>
18 #include <media/soc_camera.h>
19
20 /* mt9m001 i2c address 0x5d
21  * The platform has to define i2c_board_info
22  * and call i2c_register_board_info() */
23
24 /* mt9m001 selected register addresses */
25 #define MT9M001_CHIP_VERSION            0x00
26 #define MT9M001_ROW_START               0x01
27 #define MT9M001_COLUMN_START            0x02
28 #define MT9M001_WINDOW_HEIGHT           0x03
29 #define MT9M001_WINDOW_WIDTH            0x04
30 #define MT9M001_HORIZONTAL_BLANKING     0x05
31 #define MT9M001_VERTICAL_BLANKING       0x06
32 #define MT9M001_OUTPUT_CONTROL          0x07
33 #define MT9M001_SHUTTER_WIDTH           0x09
34 #define MT9M001_FRAME_RESTART           0x0b
35 #define MT9M001_SHUTTER_DELAY           0x0c
36 #define MT9M001_RESET                   0x0d
37 #define MT9M001_READ_OPTIONS1           0x1e
38 #define MT9M001_READ_OPTIONS2           0x20
39 #define MT9M001_GLOBAL_GAIN             0x35
40 #define MT9M001_CHIP_ENABLE             0xF1
41
42 static const struct soc_camera_data_format mt9m001_colour_formats[] = {
43         /* Order important: first natively supported,
44          * second supported with a GPIO extender */
45         {
46                 .name           = "Bayer (sRGB) 10 bit",
47                 .depth          = 10,
48                 .fourcc         = V4L2_PIX_FMT_SBGGR16,
49                 .colorspace     = V4L2_COLORSPACE_SRGB,
50         }, {
51                 .name           = "Bayer (sRGB) 8 bit",
52                 .depth          = 8,
53                 .fourcc         = V4L2_PIX_FMT_SBGGR8,
54                 .colorspace     = V4L2_COLORSPACE_SRGB,
55         }
56 };
57
58 static const struct soc_camera_data_format mt9m001_monochrome_formats[] = {
59         /* Order important - see above */
60         {
61                 .name           = "Monochrome 10 bit",
62                 .depth          = 10,
63                 .fourcc         = V4L2_PIX_FMT_Y16,
64         }, {
65                 .name           = "Monochrome 8 bit",
66                 .depth          = 8,
67                 .fourcc         = V4L2_PIX_FMT_GREY,
68         },
69 };
70
71 struct mt9m001 {
72         struct i2c_client *client;
73         struct soc_camera_device icd;
74         int model;      /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
75         unsigned char autoexposure;
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 mt9m001_init(struct soc_camera_device *icd)
113 {
114         struct i2c_client *client = to_i2c_client(icd->control);
115         struct soc_camera_link *icl = client->dev.platform_data;
116         int ret;
117
118         dev_dbg(icd->vdev->parent, "%s\n", __func__);
119
120         if (icl->power) {
121                 ret = icl->power(&client->dev, 1);
122                 if (ret < 0) {
123                         dev_err(icd->vdev->parent,
124                                 "Platform failed to power-on the camera.\n");
125                         return ret;
126                 }
127         }
128
129         /* The camera could have been already on, we reset it additionally */
130         if (icl->reset)
131                 ret = icl->reset(&client->dev);
132         else
133                 ret = -ENODEV;
134
135         if (ret < 0) {
136                 /* Either no platform reset, or platform reset failed */
137                 ret = reg_write(client, MT9M001_RESET, 1);
138                 if (!ret)
139                         ret = reg_write(client, MT9M001_RESET, 0);
140         }
141         /* Disable chip, synchronous option update */
142         if (!ret)
143                 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
144
145         return ret;
146 }
147
148 static int mt9m001_release(struct soc_camera_device *icd)
149 {
150         struct i2c_client *client = to_i2c_client(icd->control);
151         struct soc_camera_link *icl = client->dev.platform_data;
152
153         /* Disable the chip */
154         reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
155
156         if (icl->power)
157                 icl->power(&client->dev, 0);
158
159         return 0;
160 }
161
162 static int mt9m001_start_capture(struct soc_camera_device *icd)
163 {
164         struct i2c_client *client = to_i2c_client(icd->control);
165
166         /* Switch to master "normal" mode */
167         if (reg_write(client, MT9M001_OUTPUT_CONTROL, 2) < 0)
168                 return -EIO;
169         return 0;
170 }
171
172 static int mt9m001_stop_capture(struct soc_camera_device *icd)
173 {
174         struct i2c_client *client = to_i2c_client(icd->control);
175
176         /* Stop sensor readout */
177         if (reg_write(client, MT9M001_OUTPUT_CONTROL, 0) < 0)
178                 return -EIO;
179         return 0;
180 }
181
182 static int mt9m001_set_bus_param(struct soc_camera_device *icd,
183                                  unsigned long flags)
184 {
185         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
186         struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
187         unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
188
189         /* Only one width bit may be set */
190         if (!is_power_of_2(width_flag))
191                 return -EINVAL;
192
193         if (icl->set_bus_param)
194                 return icl->set_bus_param(icl, width_flag);
195
196         /*
197          * Without board specific bus width settings we only support the
198          * sensors native bus width
199          */
200         if (width_flag == SOCAM_DATAWIDTH_10)
201                 return 0;
202
203         return -EINVAL;
204 }
205
206 static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
207 {
208         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
209         struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
210         /* MT9M001 has all capture_format parameters fixed */
211         unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
212                 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
213                 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
214
215         if (icl->query_bus_param)
216                 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
217         else
218                 flags |= SOCAM_DATAWIDTH_10;
219
220         return soc_camera_apply_sensor_flags(icl, flags);
221 }
222
223 static int mt9m001_set_crop(struct soc_camera_device *icd,
224                             struct v4l2_rect *rect)
225 {
226         struct i2c_client *client = to_i2c_client(icd->control);
227         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
228         int ret;
229         const u16 hblank = 9, vblank = 25;
230
231         /* Blanking and start values - default... */
232         ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
233         if (!ret)
234                 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
235
236         /* The caller provides a supported format, as verified per
237          * call to icd->try_fmt() */
238         if (!ret)
239                 ret = reg_write(client, MT9M001_COLUMN_START, rect->left);
240         if (!ret)
241                 ret = reg_write(client, MT9M001_ROW_START, rect->top);
242         if (!ret)
243                 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect->width - 1);
244         if (!ret)
245                 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
246                                 rect->height + icd->y_skip_top - 1);
247         if (!ret && mt9m001->autoexposure) {
248                 ret = reg_write(client, MT9M001_SHUTTER_WIDTH,
249                                 rect->height + icd->y_skip_top + vblank);
250                 if (!ret) {
251                         const struct v4l2_queryctrl *qctrl =
252                                 soc_camera_find_qctrl(icd->ops,
253                                                       V4L2_CID_EXPOSURE);
254                         icd->exposure = (524 + (rect->height + icd->y_skip_top +
255                                                 vblank - 1) *
256                                          (qctrl->maximum - qctrl->minimum)) /
257                                 1048 + qctrl->minimum;
258                 }
259         }
260
261         return ret;
262 }
263
264 static int mt9m001_set_fmt(struct soc_camera_device *icd,
265                            struct v4l2_format *f)
266 {
267         struct v4l2_rect rect = {
268                 .left   = icd->x_current,
269                 .top    = icd->y_current,
270                 .width  = f->fmt.pix.width,
271                 .height = f->fmt.pix.height,
272         };
273
274         /* No support for scaling so far, just crop. TODO: use skipping */
275         return mt9m001_set_crop(icd, &rect);
276 }
277
278 static int mt9m001_try_fmt(struct soc_camera_device *icd,
279                            struct v4l2_format *f)
280 {
281         struct v4l2_pix_format *pix = &f->fmt.pix;
282
283         v4l_bound_align_image(&pix->width, 48, 1280, 1,
284                               &pix->height, 32 + icd->y_skip_top,
285                               1024 + icd->y_skip_top, 0, 0);
286
287         return 0;
288 }
289
290 static int mt9m001_get_chip_id(struct soc_camera_device *icd,
291                                struct v4l2_dbg_chip_ident *id)
292 {
293         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
294
295         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
296                 return -EINVAL;
297
298         if (id->match.addr != mt9m001->client->addr)
299                 return -ENODEV;
300
301         id->ident       = mt9m001->model;
302         id->revision    = 0;
303
304         return 0;
305 }
306
307 #ifdef CONFIG_VIDEO_ADV_DEBUG
308 static int mt9m001_get_register(struct soc_camera_device *icd,
309                                 struct v4l2_dbg_register *reg)
310 {
311         struct i2c_client *client = to_i2c_client(icd->control);
312
313         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
314                 return -EINVAL;
315
316         if (reg->match.addr != client->addr)
317                 return -ENODEV;
318
319         reg->size = 2;
320         reg->val = reg_read(client, reg->reg);
321
322         if (reg->val > 0xffff)
323                 return -EIO;
324
325         return 0;
326 }
327
328 static int mt9m001_set_register(struct soc_camera_device *icd,
329                                 struct v4l2_dbg_register *reg)
330 {
331         struct i2c_client *client = to_i2c_client(icd->control);
332
333         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
334                 return -EINVAL;
335
336         if (reg->match.addr != client->addr)
337                 return -ENODEV;
338
339         if (reg_write(client, reg->reg, reg->val) < 0)
340                 return -EIO;
341
342         return 0;
343 }
344 #endif
345
346 static const struct v4l2_queryctrl mt9m001_controls[] = {
347         {
348                 .id             = V4L2_CID_VFLIP,
349                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
350                 .name           = "Flip Vertically",
351                 .minimum        = 0,
352                 .maximum        = 1,
353                 .step           = 1,
354                 .default_value  = 0,
355         }, {
356                 .id             = V4L2_CID_GAIN,
357                 .type           = V4L2_CTRL_TYPE_INTEGER,
358                 .name           = "Gain",
359                 .minimum        = 0,
360                 .maximum        = 127,
361                 .step           = 1,
362                 .default_value  = 64,
363                 .flags          = V4L2_CTRL_FLAG_SLIDER,
364         }, {
365                 .id             = V4L2_CID_EXPOSURE,
366                 .type           = V4L2_CTRL_TYPE_INTEGER,
367                 .name           = "Exposure",
368                 .minimum        = 1,
369                 .maximum        = 255,
370                 .step           = 1,
371                 .default_value  = 255,
372                 .flags          = V4L2_CTRL_FLAG_SLIDER,
373         }, {
374                 .id             = V4L2_CID_EXPOSURE_AUTO,
375                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
376                 .name           = "Automatic Exposure",
377                 .minimum        = 0,
378                 .maximum        = 1,
379                 .step           = 1,
380                 .default_value  = 1,
381         }
382 };
383
384 static int mt9m001_video_probe(struct soc_camera_device *);
385 static void mt9m001_video_remove(struct soc_camera_device *);
386 static int mt9m001_get_control(struct soc_camera_device *, struct v4l2_control *);
387 static int mt9m001_set_control(struct soc_camera_device *, struct v4l2_control *);
388
389 static struct soc_camera_ops mt9m001_ops = {
390         .owner                  = THIS_MODULE,
391         .probe                  = mt9m001_video_probe,
392         .remove                 = mt9m001_video_remove,
393         .init                   = mt9m001_init,
394         .release                = mt9m001_release,
395         .start_capture          = mt9m001_start_capture,
396         .stop_capture           = mt9m001_stop_capture,
397         .set_crop               = mt9m001_set_crop,
398         .set_fmt                = mt9m001_set_fmt,
399         .try_fmt                = mt9m001_try_fmt,
400         .set_bus_param          = mt9m001_set_bus_param,
401         .query_bus_param        = mt9m001_query_bus_param,
402         .controls               = mt9m001_controls,
403         .num_controls           = ARRAY_SIZE(mt9m001_controls),
404         .get_control            = mt9m001_get_control,
405         .set_control            = mt9m001_set_control,
406         .get_chip_id            = mt9m001_get_chip_id,
407 #ifdef CONFIG_VIDEO_ADV_DEBUG
408         .get_register           = mt9m001_get_register,
409         .set_register           = mt9m001_set_register,
410 #endif
411 };
412
413 static int mt9m001_get_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
414 {
415         struct i2c_client *client = to_i2c_client(icd->control);
416         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
417         int data;
418
419         switch (ctrl->id) {
420         case V4L2_CID_VFLIP:
421                 data = reg_read(client, MT9M001_READ_OPTIONS2);
422                 if (data < 0)
423                         return -EIO;
424                 ctrl->value = !!(data & 0x8000);
425                 break;
426         case V4L2_CID_EXPOSURE_AUTO:
427                 ctrl->value = mt9m001->autoexposure;
428                 break;
429         }
430         return 0;
431 }
432
433 static int mt9m001_set_control(struct soc_camera_device *icd, struct v4l2_control *ctrl)
434 {
435         struct i2c_client *client = to_i2c_client(icd->control);
436         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
437         const struct v4l2_queryctrl *qctrl;
438         int data;
439
440         qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
441
442         if (!qctrl)
443                 return -EINVAL;
444
445         switch (ctrl->id) {
446         case V4L2_CID_VFLIP:
447                 if (ctrl->value)
448                         data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
449                 else
450                         data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
451                 if (data < 0)
452                         return -EIO;
453                 break;
454         case V4L2_CID_GAIN:
455                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
456                         return -EINVAL;
457                 /* See Datasheet Table 7, Gain settings. */
458                 if (ctrl->value <= qctrl->default_value) {
459                         /* Pack it into 0..1 step 0.125, register values 0..8 */
460                         unsigned long range = qctrl->default_value - qctrl->minimum;
461                         data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
462
463                         dev_dbg(&icd->dev, "Setting gain %d\n", data);
464                         data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
465                         if (data < 0)
466                                 return -EIO;
467                 } else {
468                         /* Pack it into 1.125..15 variable step, register values 9..67 */
469                         /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
470                         unsigned long range = qctrl->maximum - qctrl->default_value - 1;
471                         unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
472                                                111 + range / 2) / range + 9;
473
474                         if (gain <= 32)
475                                 data = gain;
476                         else if (gain <= 64)
477                                 data = ((gain - 32) * 16 + 16) / 32 + 80;
478                         else
479                                 data = ((gain - 64) * 7 + 28) / 56 + 96;
480
481                         dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
482                                  reg_read(client, MT9M001_GLOBAL_GAIN), data);
483                         data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
484                         if (data < 0)
485                                 return -EIO;
486                 }
487
488                 /* Success */
489                 icd->gain = ctrl->value;
490                 break;
491         case V4L2_CID_EXPOSURE:
492                 /* mt9m001 has maximum == default */
493                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
494                         return -EINVAL;
495                 else {
496                         unsigned long range = qctrl->maximum - qctrl->minimum;
497                         unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
498                                                  range / 2) / range + 1;
499
500                         dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
501                                  reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
502                         if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
503                                 return -EIO;
504                         icd->exposure = ctrl->value;
505                         mt9m001->autoexposure = 0;
506                 }
507                 break;
508         case V4L2_CID_EXPOSURE_AUTO:
509                 if (ctrl->value) {
510                         const u16 vblank = 25;
511                         if (reg_write(client, MT9M001_SHUTTER_WIDTH, icd->height +
512                                       icd->y_skip_top + vblank) < 0)
513                                 return -EIO;
514                         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
515                         icd->exposure = (524 + (icd->height + icd->y_skip_top + vblank - 1) *
516                                          (qctrl->maximum - qctrl->minimum)) /
517                                 1048 + qctrl->minimum;
518                         mt9m001->autoexposure = 1;
519                 } else
520                         mt9m001->autoexposure = 0;
521                 break;
522         }
523         return 0;
524 }
525
526 /* Interface active, can use i2c. If it fails, it can indeed mean, that
527  * this wasn't our capture interface, so, we wait for the right one */
528 static int mt9m001_video_probe(struct soc_camera_device *icd)
529 {
530         struct i2c_client *client = to_i2c_client(icd->control);
531         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
532         struct soc_camera_link *icl = client->dev.platform_data;
533         s32 data;
534         int ret;
535         unsigned long flags;
536
537         /* We must have a parent by now. And it cannot be a wrong one.
538          * So this entire test is completely redundant. */
539         if (!icd->dev.parent ||
540             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
541                 return -ENODEV;
542
543         /* Enable the chip */
544         data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
545         dev_dbg(&icd->dev, "write: %d\n", data);
546
547         /* Read out the chip version register */
548         data = reg_read(client, MT9M001_CHIP_VERSION);
549
550         /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
551         switch (data) {
552         case 0x8411:
553         case 0x8421:
554                 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
555                 icd->formats = mt9m001_colour_formats;
556                 break;
557         case 0x8431:
558                 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
559                 icd->formats = mt9m001_monochrome_formats;
560                 break;
561         default:
562                 ret = -ENODEV;
563                 dev_err(&icd->dev,
564                         "No MT9M001 chip detected, register read %x\n", data);
565                 goto ei2c;
566         }
567
568         icd->num_formats = 0;
569
570         /*
571          * This is a 10bit sensor, so by default we only allow 10bit.
572          * The platform may support different bus widths due to
573          * different routing of the data lines.
574          */
575         if (icl->query_bus_param)
576                 flags = icl->query_bus_param(icl);
577         else
578                 flags = SOCAM_DATAWIDTH_10;
579
580         if (flags & SOCAM_DATAWIDTH_10)
581                 icd->num_formats++;
582         else
583                 icd->formats++;
584
585         if (flags & SOCAM_DATAWIDTH_8)
586                 icd->num_formats++;
587
588         dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
589                  data == 0x8431 ? "C12STM" : "C12ST");
590
591         /* Now that we know the model, we can start video */
592         ret = soc_camera_video_start(icd);
593         if (ret)
594                 goto eisis;
595
596         return 0;
597
598 eisis:
599 ei2c:
600         return ret;
601 }
602
603 static void mt9m001_video_remove(struct soc_camera_device *icd)
604 {
605         struct mt9m001 *mt9m001 = container_of(icd, struct mt9m001, icd);
606         struct soc_camera_link *icl = mt9m001->client->dev.platform_data;
607
608         dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", mt9m001->client->addr,
609                 icd->dev.parent, icd->vdev);
610         soc_camera_video_stop(icd);
611         if (icl->free_bus)
612                 icl->free_bus(icl);
613 }
614
615 static int mt9m001_probe(struct i2c_client *client,
616                          const struct i2c_device_id *did)
617 {
618         struct mt9m001 *mt9m001;
619         struct soc_camera_device *icd;
620         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
621         struct soc_camera_link *icl = client->dev.platform_data;
622         int ret;
623
624         if (!icl) {
625                 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
626                 return -EINVAL;
627         }
628
629         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
630                 dev_warn(&adapter->dev,
631                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
632                 return -EIO;
633         }
634
635         mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
636         if (!mt9m001)
637                 return -ENOMEM;
638
639         mt9m001->client = client;
640         i2c_set_clientdata(client, mt9m001);
641
642         /* Second stage probe - when a capture adapter is there */
643         icd = &mt9m001->icd;
644         icd->ops        = &mt9m001_ops;
645         icd->control    = &client->dev;
646         icd->x_min      = 20;
647         icd->y_min      = 12;
648         icd->x_current  = 20;
649         icd->y_current  = 12;
650         icd->width_min  = 48;
651         icd->width_max  = 1280;
652         icd->height_min = 32;
653         icd->height_max = 1024;
654         icd->y_skip_top = 1;
655         icd->iface      = icl->bus_id;
656         /* Simulated autoexposure. If enabled, we calculate shutter width
657          * ourselves in the driver based on vertical blanking and frame width */
658         mt9m001->autoexposure = 1;
659
660         ret = soc_camera_device_register(icd);
661         if (ret)
662                 goto eisdr;
663
664         return 0;
665
666 eisdr:
667         kfree(mt9m001);
668         return ret;
669 }
670
671 static int mt9m001_remove(struct i2c_client *client)
672 {
673         struct mt9m001 *mt9m001 = i2c_get_clientdata(client);
674
675         soc_camera_device_unregister(&mt9m001->icd);
676         kfree(mt9m001);
677
678         return 0;
679 }
680
681 static const struct i2c_device_id mt9m001_id[] = {
682         { "mt9m001", 0 },
683         { }
684 };
685 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
686
687 static struct i2c_driver mt9m001_i2c_driver = {
688         .driver = {
689                 .name = "mt9m001",
690         },
691         .probe          = mt9m001_probe,
692         .remove         = mt9m001_remove,
693         .id_table       = mt9m001_id,
694 };
695
696 static int __init mt9m001_mod_init(void)
697 {
698         return i2c_add_driver(&mt9m001_i2c_driver);
699 }
700
701 static void __exit mt9m001_mod_exit(void)
702 {
703         i2c_del_driver(&mt9m001_i2c_driver);
704 }
705
706 module_init(mt9m001_mod_init);
707 module_exit(mt9m001_mod_exit);
708
709 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
710 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
711 MODULE_LICENSE("GPL");