8b36a74b1be0ba55616af715d33403bacc149ab1
[safe/jmp/linux-2.6] / 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-subdev.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 ctruct i2c_board_info objects and link to them
22  * from struct soc_camera_link */
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 v4l2_subdev subdev;
73         int model;      /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
74         unsigned char autoexposure;
75 };
76
77 static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
78 {
79         return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
80 }
81
82 static int reg_read(struct i2c_client *client, const u8 reg)
83 {
84         s32 data = i2c_smbus_read_word_data(client, reg);
85         return data < 0 ? data : swab16(data);
86 }
87
88 static int reg_write(struct i2c_client *client, const u8 reg,
89                      const u16 data)
90 {
91         return i2c_smbus_write_word_data(client, reg, swab16(data));
92 }
93
94 static int reg_set(struct i2c_client *client, const u8 reg,
95                    const u16 data)
96 {
97         int ret;
98
99         ret = reg_read(client, reg);
100         if (ret < 0)
101                 return ret;
102         return reg_write(client, reg, ret | data);
103 }
104
105 static int reg_clear(struct i2c_client *client, const u8 reg,
106                      const u16 data)
107 {
108         int ret;
109
110         ret = reg_read(client, reg);
111         if (ret < 0)
112                 return ret;
113         return reg_write(client, reg, ret & ~data);
114 }
115
116 static int mt9m001_init(struct soc_camera_device *icd)
117 {
118         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
119         int ret;
120
121         dev_dbg(&icd->dev, "%s\n", __func__);
122
123         /*
124          * We don't know, whether platform provides reset,
125          * issue a soft reset too
126          */
127         ret = reg_write(client, MT9M001_RESET, 1);
128         if (!ret)
129                 ret = reg_write(client, MT9M001_RESET, 0);
130
131         /* Disable chip, synchronous option update */
132         if (!ret)
133                 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
134
135         return ret;
136 }
137
138 static int mt9m001_release(struct soc_camera_device *icd)
139 {
140         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
141
142         /* Disable the chip */
143         reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
144
145         return 0;
146 }
147
148 static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
149 {
150         struct i2c_client *client = sd->priv;
151
152         /* Switch to master "normal" mode or stop sensor readout */
153         if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
154                 return -EIO;
155         return 0;
156 }
157
158 static int mt9m001_set_bus_param(struct soc_camera_device *icd,
159                                  unsigned long flags)
160 {
161         struct soc_camera_link *icl = to_soc_camera_link(icd);
162         unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
163
164         /* Only one width bit may be set */
165         if (!is_power_of_2(width_flag))
166                 return -EINVAL;
167
168         if (icl->set_bus_param)
169                 return icl->set_bus_param(icl, width_flag);
170
171         /*
172          * Without board specific bus width settings we only support the
173          * sensors native bus width
174          */
175         if (width_flag == SOCAM_DATAWIDTH_10)
176                 return 0;
177
178         return -EINVAL;
179 }
180
181 static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
182 {
183         struct soc_camera_link *icl = to_soc_camera_link(icd);
184         /* MT9M001 has all capture_format parameters fixed */
185         unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
186                 SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
187                 SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
188
189         if (icl->query_bus_param)
190                 flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
191         else
192                 flags |= SOCAM_DATAWIDTH_10;
193
194         return soc_camera_apply_sensor_flags(icl, flags);
195 }
196
197 static int mt9m001_set_crop(struct soc_camera_device *icd,
198                             struct v4l2_rect *rect)
199 {
200         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
201         struct mt9m001 *mt9m001 = to_mt9m001(client);
202         int ret;
203         const u16 hblank = 9, vblank = 25;
204
205         /* Blanking and start values - default... */
206         ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
207         if (!ret)
208                 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
209
210         /* The caller provides a supported format, as verified per
211          * call to icd->try_fmt() */
212         if (!ret)
213                 ret = reg_write(client, MT9M001_COLUMN_START, rect->left);
214         if (!ret)
215                 ret = reg_write(client, MT9M001_ROW_START, rect->top);
216         if (!ret)
217                 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect->width - 1);
218         if (!ret)
219                 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
220                                 rect->height + icd->y_skip_top - 1);
221         if (!ret && mt9m001->autoexposure) {
222                 ret = reg_write(client, MT9M001_SHUTTER_WIDTH,
223                                 rect->height + icd->y_skip_top + vblank);
224                 if (!ret) {
225                         const struct v4l2_queryctrl *qctrl =
226                                 soc_camera_find_qctrl(icd->ops,
227                                                       V4L2_CID_EXPOSURE);
228                         icd->exposure = (524 + (rect->height + icd->y_skip_top +
229                                                 vblank - 1) *
230                                          (qctrl->maximum - qctrl->minimum)) /
231                                 1048 + qctrl->minimum;
232                 }
233         }
234
235         return ret;
236 }
237
238 static int mt9m001_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
239 {
240         struct i2c_client *client = sd->priv;
241         struct soc_camera_device *icd = client->dev.platform_data;
242         struct v4l2_rect rect = {
243                 .left   = icd->rect_current.left,
244                 .top    = icd->rect_current.top,
245                 .width  = f->fmt.pix.width,
246                 .height = f->fmt.pix.height,
247         };
248
249         /* No support for scaling so far, just crop. TODO: use skipping */
250         return mt9m001_set_crop(icd, &rect);
251 }
252
253 static int mt9m001_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
254 {
255         struct i2c_client *client = sd->priv;
256         struct soc_camera_device *icd = client->dev.platform_data;
257         struct v4l2_pix_format *pix = &f->fmt.pix;
258
259         v4l_bound_align_image(&pix->width, 48, 1280, 1,
260                               &pix->height, 32 + icd->y_skip_top,
261                               1024 + icd->y_skip_top, 0, 0);
262
263         return 0;
264 }
265
266 static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
267                                 struct v4l2_dbg_chip_ident *id)
268 {
269         struct i2c_client *client = sd->priv;
270         struct mt9m001 *mt9m001 = to_mt9m001(client);
271
272         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
273                 return -EINVAL;
274
275         if (id->match.addr != client->addr)
276                 return -ENODEV;
277
278         id->ident       = mt9m001->model;
279         id->revision    = 0;
280
281         return 0;
282 }
283
284 #ifdef CONFIG_VIDEO_ADV_DEBUG
285 static int mt9m001_g_register(struct v4l2_subdev *sd,
286                               struct v4l2_dbg_register *reg)
287 {
288         struct i2c_client *client = sd->priv;
289
290         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
291                 return -EINVAL;
292
293         if (reg->match.addr != client->addr)
294                 return -ENODEV;
295
296         reg->size = 2;
297         reg->val = reg_read(client, reg->reg);
298
299         if (reg->val > 0xffff)
300                 return -EIO;
301
302         return 0;
303 }
304
305 static int mt9m001_s_register(struct v4l2_subdev *sd,
306                               struct v4l2_dbg_register *reg)
307 {
308         struct i2c_client *client = sd->priv;
309
310         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
311                 return -EINVAL;
312
313         if (reg->match.addr != client->addr)
314                 return -ENODEV;
315
316         if (reg_write(client, reg->reg, reg->val) < 0)
317                 return -EIO;
318
319         return 0;
320 }
321 #endif
322
323 static const struct v4l2_queryctrl mt9m001_controls[] = {
324         {
325                 .id             = V4L2_CID_VFLIP,
326                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
327                 .name           = "Flip Vertically",
328                 .minimum        = 0,
329                 .maximum        = 1,
330                 .step           = 1,
331                 .default_value  = 0,
332         }, {
333                 .id             = V4L2_CID_GAIN,
334                 .type           = V4L2_CTRL_TYPE_INTEGER,
335                 .name           = "Gain",
336                 .minimum        = 0,
337                 .maximum        = 127,
338                 .step           = 1,
339                 .default_value  = 64,
340                 .flags          = V4L2_CTRL_FLAG_SLIDER,
341         }, {
342                 .id             = V4L2_CID_EXPOSURE,
343                 .type           = V4L2_CTRL_TYPE_INTEGER,
344                 .name           = "Exposure",
345                 .minimum        = 1,
346                 .maximum        = 255,
347                 .step           = 1,
348                 .default_value  = 255,
349                 .flags          = V4L2_CTRL_FLAG_SLIDER,
350         }, {
351                 .id             = V4L2_CID_EXPOSURE_AUTO,
352                 .type           = V4L2_CTRL_TYPE_BOOLEAN,
353                 .name           = "Automatic Exposure",
354                 .minimum        = 0,
355                 .maximum        = 1,
356                 .step           = 1,
357                 .default_value  = 1,
358         }
359 };
360
361 static struct soc_camera_ops mt9m001_ops = {
362         .init                   = mt9m001_init,
363         .release                = mt9m001_release,
364         .set_crop               = mt9m001_set_crop,
365         .set_bus_param          = mt9m001_set_bus_param,
366         .query_bus_param        = mt9m001_query_bus_param,
367         .controls               = mt9m001_controls,
368         .num_controls           = ARRAY_SIZE(mt9m001_controls),
369 };
370
371 static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
372 {
373         struct i2c_client *client = sd->priv;
374         struct mt9m001 *mt9m001 = to_mt9m001(client);
375         int data;
376
377         switch (ctrl->id) {
378         case V4L2_CID_VFLIP:
379                 data = reg_read(client, MT9M001_READ_OPTIONS2);
380                 if (data < 0)
381                         return -EIO;
382                 ctrl->value = !!(data & 0x8000);
383                 break;
384         case V4L2_CID_EXPOSURE_AUTO:
385                 ctrl->value = mt9m001->autoexposure;
386                 break;
387         }
388         return 0;
389 }
390
391 static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
392 {
393         struct i2c_client *client = sd->priv;
394         struct mt9m001 *mt9m001 = to_mt9m001(client);
395         struct soc_camera_device *icd = client->dev.platform_data;
396         const struct v4l2_queryctrl *qctrl;
397         int data;
398
399         qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
400
401         if (!qctrl)
402                 return -EINVAL;
403
404         switch (ctrl->id) {
405         case V4L2_CID_VFLIP:
406                 if (ctrl->value)
407                         data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
408                 else
409                         data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
410                 if (data < 0)
411                         return -EIO;
412                 break;
413         case V4L2_CID_GAIN:
414                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
415                         return -EINVAL;
416                 /* See Datasheet Table 7, Gain settings. */
417                 if (ctrl->value <= qctrl->default_value) {
418                         /* Pack it into 0..1 step 0.125, register values 0..8 */
419                         unsigned long range = qctrl->default_value - qctrl->minimum;
420                         data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
421
422                         dev_dbg(&icd->dev, "Setting gain %d\n", data);
423                         data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
424                         if (data < 0)
425                                 return -EIO;
426                 } else {
427                         /* Pack it into 1.125..15 variable step, register values 9..67 */
428                         /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
429                         unsigned long range = qctrl->maximum - qctrl->default_value - 1;
430                         unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
431                                                111 + range / 2) / range + 9;
432
433                         if (gain <= 32)
434                                 data = gain;
435                         else if (gain <= 64)
436                                 data = ((gain - 32) * 16 + 16) / 32 + 80;
437                         else
438                                 data = ((gain - 64) * 7 + 28) / 56 + 96;
439
440                         dev_dbg(&icd->dev, "Setting gain from %d to %d\n",
441                                  reg_read(client, MT9M001_GLOBAL_GAIN), data);
442                         data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
443                         if (data < 0)
444                                 return -EIO;
445                 }
446
447                 /* Success */
448                 icd->gain = ctrl->value;
449                 break;
450         case V4L2_CID_EXPOSURE:
451                 /* mt9m001 has maximum == default */
452                 if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
453                         return -EINVAL;
454                 else {
455                         unsigned long range = qctrl->maximum - qctrl->minimum;
456                         unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
457                                                  range / 2) / range + 1;
458
459                         dev_dbg(&icd->dev, "Setting shutter width from %d to %lu\n",
460                                  reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
461                         if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
462                                 return -EIO;
463                         icd->exposure = ctrl->value;
464                         mt9m001->autoexposure = 0;
465                 }
466                 break;
467         case V4L2_CID_EXPOSURE_AUTO:
468                 if (ctrl->value) {
469                         const u16 vblank = 25;
470                         if (reg_write(client, MT9M001_SHUTTER_WIDTH,
471                                       icd->rect_current.height +
472                                       icd->y_skip_top + vblank) < 0)
473                                 return -EIO;
474                         qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
475                         icd->exposure = (524 + (icd->rect_current.height +
476                                                 icd->y_skip_top + vblank - 1) *
477                                          (qctrl->maximum - qctrl->minimum)) /
478                                 1048 + qctrl->minimum;
479                         mt9m001->autoexposure = 1;
480                 } else
481                         mt9m001->autoexposure = 0;
482                 break;
483         }
484         return 0;
485 }
486
487 /* Interface active, can use i2c. If it fails, it can indeed mean, that
488  * this wasn't our capture interface, so, we wait for the right one */
489 static int mt9m001_video_probe(struct soc_camera_device *icd,
490                                struct i2c_client *client)
491 {
492         struct mt9m001 *mt9m001 = to_mt9m001(client);
493         struct soc_camera_link *icl = to_soc_camera_link(icd);
494         s32 data;
495         unsigned long flags;
496
497         /* We must have a parent by now. And it cannot be a wrong one.
498          * So this entire test is completely redundant. */
499         if (!icd->dev.parent ||
500             to_soc_camera_host(icd->dev.parent)->nr != icd->iface)
501                 return -ENODEV;
502
503         /* Enable the chip */
504         data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
505         dev_dbg(&icd->dev, "write: %d\n", data);
506
507         /* Read out the chip version register */
508         data = reg_read(client, MT9M001_CHIP_VERSION);
509
510         /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
511         switch (data) {
512         case 0x8411:
513         case 0x8421:
514                 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
515                 icd->formats = mt9m001_colour_formats;
516                 break;
517         case 0x8431:
518                 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
519                 icd->formats = mt9m001_monochrome_formats;
520                 break;
521         default:
522                 dev_err(&icd->dev,
523                         "No MT9M001 chip detected, register read %x\n", data);
524                 return -ENODEV;
525         }
526
527         icd->num_formats = 0;
528
529         /*
530          * This is a 10bit sensor, so by default we only allow 10bit.
531          * The platform may support different bus widths due to
532          * different routing of the data lines.
533          */
534         if (icl->query_bus_param)
535                 flags = icl->query_bus_param(icl);
536         else
537                 flags = SOCAM_DATAWIDTH_10;
538
539         if (flags & SOCAM_DATAWIDTH_10)
540                 icd->num_formats++;
541         else
542                 icd->formats++;
543
544         if (flags & SOCAM_DATAWIDTH_8)
545                 icd->num_formats++;
546
547         dev_info(&icd->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
548                  data == 0x8431 ? "C12STM" : "C12ST");
549
550         return 0;
551 }
552
553 static void mt9m001_video_remove(struct soc_camera_device *icd)
554 {
555         struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
556         struct soc_camera_link *icl = to_soc_camera_link(icd);
557
558         dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", client->addr,
559                 icd->dev.parent, icd->vdev);
560         if (icl->free_bus)
561                 icl->free_bus(icl);
562 }
563
564 static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
565         .g_ctrl         = mt9m001_g_ctrl,
566         .s_ctrl         = mt9m001_s_ctrl,
567         .g_chip_ident   = mt9m001_g_chip_ident,
568 #ifdef CONFIG_VIDEO_ADV_DEBUG
569         .g_register     = mt9m001_g_register,
570         .s_register     = mt9m001_s_register,
571 #endif
572 };
573
574 static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
575         .s_stream       = mt9m001_s_stream,
576         .s_fmt          = mt9m001_s_fmt,
577         .try_fmt        = mt9m001_try_fmt,
578 };
579
580 static struct v4l2_subdev_ops mt9m001_subdev_ops = {
581         .core   = &mt9m001_subdev_core_ops,
582         .video  = &mt9m001_subdev_video_ops,
583 };
584
585 static int mt9m001_probe(struct i2c_client *client,
586                          const struct i2c_device_id *did)
587 {
588         struct mt9m001 *mt9m001;
589         struct soc_camera_device *icd = client->dev.platform_data;
590         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
591         struct soc_camera_link *icl;
592         int ret;
593
594         if (!icd) {
595                 dev_err(&client->dev, "MT9M001: missing soc-camera data!\n");
596                 return -EINVAL;
597         }
598
599         icl = to_soc_camera_link(icd);
600         if (!icl) {
601                 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
602                 return -EINVAL;
603         }
604
605         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
606                 dev_warn(&adapter->dev,
607                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
608                 return -EIO;
609         }
610
611         mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
612         if (!mt9m001)
613                 return -ENOMEM;
614
615         v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
616
617         /* Second stage probe - when a capture adapter is there */
618         icd->ops                = &mt9m001_ops;
619         icd->rect_max.left      = 20;
620         icd->rect_max.top       = 12;
621         icd->rect_max.width     = 1280;
622         icd->rect_max.height    = 1024;
623         icd->rect_current.left  = 20;
624         icd->rect_current.top   = 12;
625         icd->width_min          = 48;
626         icd->height_min         = 32;
627         icd->y_skip_top         = 1;
628         /* Simulated autoexposure. If enabled, we calculate shutter width
629          * ourselves in the driver based on vertical blanking and frame width */
630         mt9m001->autoexposure = 1;
631
632         ret = mt9m001_video_probe(icd, client);
633         if (ret) {
634                 icd->ops = NULL;
635                 i2c_set_clientdata(client, NULL);
636                 kfree(mt9m001);
637         }
638
639         return ret;
640 }
641
642 static int mt9m001_remove(struct i2c_client *client)
643 {
644         struct mt9m001 *mt9m001 = to_mt9m001(client);
645         struct soc_camera_device *icd = client->dev.platform_data;
646
647         icd->ops = NULL;
648         mt9m001_video_remove(icd);
649         i2c_set_clientdata(client, NULL);
650         client->driver = NULL;
651         kfree(mt9m001);
652
653         return 0;
654 }
655
656 static const struct i2c_device_id mt9m001_id[] = {
657         { "mt9m001", 0 },
658         { }
659 };
660 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
661
662 static struct i2c_driver mt9m001_i2c_driver = {
663         .driver = {
664                 .name = "mt9m001",
665         },
666         .probe          = mt9m001_probe,
667         .remove         = mt9m001_remove,
668         .id_table       = mt9m001_id,
669 };
670
671 static int __init mt9m001_mod_init(void)
672 {
673         return i2c_add_driver(&mt9m001_i2c_driver);
674 }
675
676 static void __exit mt9m001_mod_exit(void)
677 {
678         i2c_del_driver(&mt9m001_i2c_driver);
679 }
680
681 module_init(mt9m001_mod_init);
682 module_exit(mt9m001_mod_exit);
683
684 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
685 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
686 MODULE_LICENSE("GPL");