omap_vout: fix compiler warning
[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 #include <linux/module.h>
16
17 #include <media/soc_camera.h>
18 #include <media/soc_mediabus.h>
19 #include <media/v4l2-subdev.h>
20 #include <media/v4l2-chip-ident.h>
21 #include <media/v4l2-ctrls.h>
22
23 /*
24  * mt9m001 i2c address 0x5d
25  * The platform has to define ctruct i2c_board_info objects and link to them
26  * from struct soc_camera_link
27  */
28
29 /* mt9m001 selected register addresses */
30 #define MT9M001_CHIP_VERSION            0x00
31 #define MT9M001_ROW_START               0x01
32 #define MT9M001_COLUMN_START            0x02
33 #define MT9M001_WINDOW_HEIGHT           0x03
34 #define MT9M001_WINDOW_WIDTH            0x04
35 #define MT9M001_HORIZONTAL_BLANKING     0x05
36 #define MT9M001_VERTICAL_BLANKING       0x06
37 #define MT9M001_OUTPUT_CONTROL          0x07
38 #define MT9M001_SHUTTER_WIDTH           0x09
39 #define MT9M001_FRAME_RESTART           0x0b
40 #define MT9M001_SHUTTER_DELAY           0x0c
41 #define MT9M001_RESET                   0x0d
42 #define MT9M001_READ_OPTIONS1           0x1e
43 #define MT9M001_READ_OPTIONS2           0x20
44 #define MT9M001_GLOBAL_GAIN             0x35
45 #define MT9M001_CHIP_ENABLE             0xF1
46
47 #define MT9M001_MAX_WIDTH               1280
48 #define MT9M001_MAX_HEIGHT              1024
49 #define MT9M001_MIN_WIDTH               48
50 #define MT9M001_MIN_HEIGHT              32
51 #define MT9M001_COLUMN_SKIP             20
52 #define MT9M001_ROW_SKIP                12
53
54 /* MT9M001 has only one fixed colorspace per pixelcode */
55 struct mt9m001_datafmt {
56         enum v4l2_mbus_pixelcode        code;
57         enum v4l2_colorspace            colorspace;
58 };
59
60 /* Find a data format by a pixel code in an array */
61 static const struct mt9m001_datafmt *mt9m001_find_datafmt(
62         enum v4l2_mbus_pixelcode code, const struct mt9m001_datafmt *fmt,
63         int n)
64 {
65         int i;
66         for (i = 0; i < n; i++)
67                 if (fmt[i].code == code)
68                         return fmt + i;
69
70         return NULL;
71 }
72
73 static const struct mt9m001_datafmt mt9m001_colour_fmts[] = {
74         /*
75          * Order important: first natively supported,
76          * second supported with a GPIO extender
77          */
78         {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
79         {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
80 };
81
82 static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
83         /* Order important - see above */
84         {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
85         {V4L2_MBUS_FMT_Y8_1X8, V4L2_COLORSPACE_JPEG},
86 };
87
88 struct mt9m001 {
89         struct v4l2_subdev subdev;
90         struct v4l2_ctrl_handler hdl;
91         struct {
92                 /* exposure/auto-exposure cluster */
93                 struct v4l2_ctrl *autoexposure;
94                 struct v4l2_ctrl *exposure;
95         };
96         struct v4l2_rect rect;  /* Sensor window */
97         const struct mt9m001_datafmt *fmt;
98         const struct mt9m001_datafmt *fmts;
99         int num_fmts;
100         int model;      /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
101         unsigned int total_h;
102         unsigned short y_skip_top;      /* Lines to skip at the top */
103 };
104
105 static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
106 {
107         return container_of(i2c_get_clientdata(client), struct mt9m001, subdev);
108 }
109
110 static int reg_read(struct i2c_client *client, const u8 reg)
111 {
112         s32 data = i2c_smbus_read_word_data(client, reg);
113         return data < 0 ? data : swab16(data);
114 }
115
116 static int reg_write(struct i2c_client *client, const u8 reg,
117                      const u16 data)
118 {
119         return i2c_smbus_write_word_data(client, reg, swab16(data));
120 }
121
122 static int reg_set(struct i2c_client *client, const u8 reg,
123                    const u16 data)
124 {
125         int ret;
126
127         ret = reg_read(client, reg);
128         if (ret < 0)
129                 return ret;
130         return reg_write(client, reg, ret | data);
131 }
132
133 static int reg_clear(struct i2c_client *client, const u8 reg,
134                      const u16 data)
135 {
136         int ret;
137
138         ret = reg_read(client, reg);
139         if (ret < 0)
140                 return ret;
141         return reg_write(client, reg, ret & ~data);
142 }
143
144 static int mt9m001_init(struct i2c_client *client)
145 {
146         int ret;
147
148         dev_dbg(&client->dev, "%s\n", __func__);
149
150         /*
151          * We don't know, whether platform provides reset, issue a soft reset
152          * too. This returns all registers to their default values.
153          */
154         ret = reg_write(client, MT9M001_RESET, 1);
155         if (!ret)
156                 ret = reg_write(client, MT9M001_RESET, 0);
157
158         /* Disable chip, synchronous option update */
159         if (!ret)
160                 ret = reg_write(client, MT9M001_OUTPUT_CONTROL, 0);
161
162         return ret;
163 }
164
165 static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
166 {
167         struct i2c_client *client = v4l2_get_subdevdata(sd);
168
169         /* Switch to master "normal" mode or stop sensor readout */
170         if (reg_write(client, MT9M001_OUTPUT_CONTROL, enable ? 2 : 0) < 0)
171                 return -EIO;
172         return 0;
173 }
174
175 static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
176 {
177         struct i2c_client *client = v4l2_get_subdevdata(sd);
178         struct mt9m001 *mt9m001 = to_mt9m001(client);
179         struct v4l2_rect rect = a->c;
180         int ret;
181         const u16 hblank = 9, vblank = 25;
182
183         if (mt9m001->fmts == mt9m001_colour_fmts)
184                 /*
185                  * Bayer format - even number of rows for simplicity,
186                  * but let the user play with the top row.
187                  */
188                 rect.height = ALIGN(rect.height, 2);
189
190         /* Datasheet requirement: see register description */
191         rect.width = ALIGN(rect.width, 2);
192         rect.left = ALIGN(rect.left, 2);
193
194         soc_camera_limit_side(&rect.left, &rect.width,
195                      MT9M001_COLUMN_SKIP, MT9M001_MIN_WIDTH, MT9M001_MAX_WIDTH);
196
197         soc_camera_limit_side(&rect.top, &rect.height,
198                      MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
199
200         mt9m001->total_h = rect.height + mt9m001->y_skip_top + vblank;
201
202         /* Blanking and start values - default... */
203         ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
204         if (!ret)
205                 ret = reg_write(client, MT9M001_VERTICAL_BLANKING, vblank);
206
207         /*
208          * The caller provides a supported format, as verified per
209          * call to .try_mbus_fmt()
210          */
211         if (!ret)
212                 ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
213         if (!ret)
214                 ret = reg_write(client, MT9M001_ROW_START, rect.top);
215         if (!ret)
216                 ret = reg_write(client, MT9M001_WINDOW_WIDTH, rect.width - 1);
217         if (!ret)
218                 ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
219                                 rect.height + mt9m001->y_skip_top - 1);
220         if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO)
221                 ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h);
222
223         if (!ret)
224                 mt9m001->rect = rect;
225
226         return ret;
227 }
228
229 static int mt9m001_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
230 {
231         struct i2c_client *client = v4l2_get_subdevdata(sd);
232         struct mt9m001 *mt9m001 = to_mt9m001(client);
233
234         a->c    = mt9m001->rect;
235         a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
236
237         return 0;
238 }
239
240 static int mt9m001_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
241 {
242         a->bounds.left                  = MT9M001_COLUMN_SKIP;
243         a->bounds.top                   = MT9M001_ROW_SKIP;
244         a->bounds.width                 = MT9M001_MAX_WIDTH;
245         a->bounds.height                = MT9M001_MAX_HEIGHT;
246         a->defrect                      = a->bounds;
247         a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
248         a->pixelaspect.numerator        = 1;
249         a->pixelaspect.denominator      = 1;
250
251         return 0;
252 }
253
254 static int mt9m001_g_fmt(struct v4l2_subdev *sd,
255                          struct v4l2_mbus_framefmt *mf)
256 {
257         struct i2c_client *client = v4l2_get_subdevdata(sd);
258         struct mt9m001 *mt9m001 = to_mt9m001(client);
259
260         mf->width       = mt9m001->rect.width;
261         mf->height      = mt9m001->rect.height;
262         mf->code        = mt9m001->fmt->code;
263         mf->colorspace  = mt9m001->fmt->colorspace;
264         mf->field       = V4L2_FIELD_NONE;
265
266         return 0;
267 }
268
269 static int mt9m001_s_fmt(struct v4l2_subdev *sd,
270                          struct v4l2_mbus_framefmt *mf)
271 {
272         struct i2c_client *client = v4l2_get_subdevdata(sd);
273         struct mt9m001 *mt9m001 = to_mt9m001(client);
274         struct v4l2_crop a = {
275                 .c = {
276                         .left   = mt9m001->rect.left,
277                         .top    = mt9m001->rect.top,
278                         .width  = mf->width,
279                         .height = mf->height,
280                 },
281         };
282         int ret;
283
284         /* No support for scaling so far, just crop. TODO: use skipping */
285         ret = mt9m001_s_crop(sd, &a);
286         if (!ret) {
287                 mf->width       = mt9m001->rect.width;
288                 mf->height      = mt9m001->rect.height;
289                 mt9m001->fmt    = mt9m001_find_datafmt(mf->code,
290                                         mt9m001->fmts, mt9m001->num_fmts);
291                 mf->colorspace  = mt9m001->fmt->colorspace;
292         }
293
294         return ret;
295 }
296
297 static int mt9m001_try_fmt(struct v4l2_subdev *sd,
298                            struct v4l2_mbus_framefmt *mf)
299 {
300         struct i2c_client *client = v4l2_get_subdevdata(sd);
301         struct mt9m001 *mt9m001 = to_mt9m001(client);
302         const struct mt9m001_datafmt *fmt;
303
304         v4l_bound_align_image(&mf->width, MT9M001_MIN_WIDTH,
305                 MT9M001_MAX_WIDTH, 1,
306                 &mf->height, MT9M001_MIN_HEIGHT + mt9m001->y_skip_top,
307                 MT9M001_MAX_HEIGHT + mt9m001->y_skip_top, 0, 0);
308
309         if (mt9m001->fmts == mt9m001_colour_fmts)
310                 mf->height = ALIGN(mf->height - 1, 2);
311
312         fmt = mt9m001_find_datafmt(mf->code, mt9m001->fmts,
313                                    mt9m001->num_fmts);
314         if (!fmt) {
315                 fmt = mt9m001->fmt;
316                 mf->code = fmt->code;
317         }
318
319         mf->colorspace  = fmt->colorspace;
320
321         return 0;
322 }
323
324 static int mt9m001_g_chip_ident(struct v4l2_subdev *sd,
325                                 struct v4l2_dbg_chip_ident *id)
326 {
327         struct i2c_client *client = v4l2_get_subdevdata(sd);
328         struct mt9m001 *mt9m001 = to_mt9m001(client);
329
330         if (id->match.type != V4L2_CHIP_MATCH_I2C_ADDR)
331                 return -EINVAL;
332
333         if (id->match.addr != client->addr)
334                 return -ENODEV;
335
336         id->ident       = mt9m001->model;
337         id->revision    = 0;
338
339         return 0;
340 }
341
342 #ifdef CONFIG_VIDEO_ADV_DEBUG
343 static int mt9m001_g_register(struct v4l2_subdev *sd,
344                               struct v4l2_dbg_register *reg)
345 {
346         struct i2c_client *client = v4l2_get_subdevdata(sd);
347
348         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
349                 return -EINVAL;
350
351         if (reg->match.addr != client->addr)
352                 return -ENODEV;
353
354         reg->size = 2;
355         reg->val = reg_read(client, reg->reg);
356
357         if (reg->val > 0xffff)
358                 return -EIO;
359
360         return 0;
361 }
362
363 static int mt9m001_s_register(struct v4l2_subdev *sd,
364                               struct v4l2_dbg_register *reg)
365 {
366         struct i2c_client *client = v4l2_get_subdevdata(sd);
367
368         if (reg->match.type != V4L2_CHIP_MATCH_I2C_ADDR || reg->reg > 0xff)
369                 return -EINVAL;
370
371         if (reg->match.addr != client->addr)
372                 return -ENODEV;
373
374         if (reg_write(client, reg->reg, reg->val) < 0)
375                 return -EIO;
376
377         return 0;
378 }
379 #endif
380
381 static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
382 {
383         struct mt9m001 *mt9m001 = container_of(ctrl->handler,
384                                                struct mt9m001, hdl);
385         s32 min, max;
386
387         switch (ctrl->id) {
388         case V4L2_CID_EXPOSURE_AUTO:
389                 min = mt9m001->exposure->minimum;
390                 max = mt9m001->exposure->maximum;
391                 mt9m001->exposure->val =
392                         (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
393                 break;
394         }
395         return 0;
396 }
397
398 static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
399 {
400         struct mt9m001 *mt9m001 = container_of(ctrl->handler,
401                                                struct mt9m001, hdl);
402         struct v4l2_subdev *sd = &mt9m001->subdev;
403         struct i2c_client *client = v4l2_get_subdevdata(sd);
404         struct v4l2_ctrl *exp = mt9m001->exposure;
405         int data;
406
407         switch (ctrl->id) {
408         case V4L2_CID_VFLIP:
409                 if (ctrl->val)
410                         data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
411                 else
412                         data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
413                 if (data < 0)
414                         return -EIO;
415                 return 0;
416
417         case V4L2_CID_GAIN:
418                 /* See Datasheet Table 7, Gain settings. */
419                 if (ctrl->val <= ctrl->default_value) {
420                         /* Pack it into 0..1 step 0.125, register values 0..8 */
421                         unsigned long range = ctrl->default_value - ctrl->minimum;
422                         data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
423
424                         dev_dbg(&client->dev, "Setting gain %d\n", data);
425                         data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
426                         if (data < 0)
427                                 return -EIO;
428                 } else {
429                         /* Pack it into 1.125..15 variable step, register values 9..67 */
430                         /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
431                         unsigned long range = ctrl->maximum - ctrl->default_value - 1;
432                         unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
433                                                111 + range / 2) / range + 9;
434
435                         if (gain <= 32)
436                                 data = gain;
437                         else if (gain <= 64)
438                                 data = ((gain - 32) * 16 + 16) / 32 + 80;
439                         else
440                                 data = ((gain - 64) * 7 + 28) / 56 + 96;
441
442                         dev_dbg(&client->dev, "Setting gain from %d to %d\n",
443                                  reg_read(client, MT9M001_GLOBAL_GAIN), data);
444                         data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
445                         if (data < 0)
446                                 return -EIO;
447                 }
448                 return 0;
449
450         case V4L2_CID_EXPOSURE_AUTO:
451                 if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
452                         unsigned long range = exp->maximum - exp->minimum;
453                         unsigned long shutter = ((exp->val - exp->minimum) * 1048 +
454                                                  range / 2) / range + 1;
455
456                         dev_dbg(&client->dev,
457                                 "Setting shutter width from %d to %lu\n",
458                                 reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
459                         if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
460                                 return -EIO;
461                 } else {
462                         const u16 vblank = 25;
463
464                         mt9m001->total_h = mt9m001->rect.height +
465                                 mt9m001->y_skip_top + vblank;
466                         if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
467                                 return -EIO;
468                 }
469                 return 0;
470         }
471         return -EINVAL;
472 }
473
474 /*
475  * Interface active, can use i2c. If it fails, it can indeed mean, that
476  * this wasn't our capture interface, so, we wait for the right one
477  */
478 static int mt9m001_video_probe(struct soc_camera_link *icl,
479                                struct i2c_client *client)
480 {
481         struct mt9m001 *mt9m001 = to_mt9m001(client);
482         s32 data;
483         unsigned long flags;
484         int ret;
485
486         /* Enable the chip */
487         data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
488         dev_dbg(&client->dev, "write: %d\n", data);
489
490         /* Read out the chip version register */
491         data = reg_read(client, MT9M001_CHIP_VERSION);
492
493         /* must be 0x8411 or 0x8421 for colour sensor and 8431 for bw */
494         switch (data) {
495         case 0x8411:
496         case 0x8421:
497                 mt9m001->model = V4L2_IDENT_MT9M001C12ST;
498                 mt9m001->fmts = mt9m001_colour_fmts;
499                 break;
500         case 0x8431:
501                 mt9m001->model = V4L2_IDENT_MT9M001C12STM;
502                 mt9m001->fmts = mt9m001_monochrome_fmts;
503                 break;
504         default:
505                 dev_err(&client->dev,
506                         "No MT9M001 chip detected, register read %x\n", data);
507                 return -ENODEV;
508         }
509
510         mt9m001->num_fmts = 0;
511
512         /*
513          * This is a 10bit sensor, so by default we only allow 10bit.
514          * The platform may support different bus widths due to
515          * different routing of the data lines.
516          */
517         if (icl->query_bus_param)
518                 flags = icl->query_bus_param(icl);
519         else
520                 flags = SOCAM_DATAWIDTH_10;
521
522         if (flags & SOCAM_DATAWIDTH_10)
523                 mt9m001->num_fmts++;
524         else
525                 mt9m001->fmts++;
526
527         if (flags & SOCAM_DATAWIDTH_8)
528                 mt9m001->num_fmts++;
529
530         mt9m001->fmt = &mt9m001->fmts[0];
531
532         dev_info(&client->dev, "Detected a MT9M001 chip ID %x (%s)\n", data,
533                  data == 0x8431 ? "C12STM" : "C12ST");
534
535         ret = mt9m001_init(client);
536         if (ret < 0)
537                 dev_err(&client->dev, "Failed to initialise the camera\n");
538
539         /* mt9m001_init() has reset the chip, returning registers to defaults */
540         return v4l2_ctrl_handler_setup(&mt9m001->hdl);
541 }
542
543 static void mt9m001_video_remove(struct soc_camera_link *icl)
544 {
545         if (icl->free_bus)
546                 icl->free_bus(icl);
547 }
548
549 static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
550 {
551         struct i2c_client *client = v4l2_get_subdevdata(sd);
552         struct mt9m001 *mt9m001 = to_mt9m001(client);
553
554         *lines = mt9m001->y_skip_top;
555
556         return 0;
557 }
558
559 static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
560         .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
561         .s_ctrl = mt9m001_s_ctrl,
562 };
563
564 static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
565         .g_chip_ident   = mt9m001_g_chip_ident,
566 #ifdef CONFIG_VIDEO_ADV_DEBUG
567         .g_register     = mt9m001_g_register,
568         .s_register     = mt9m001_s_register,
569 #endif
570 };
571
572 static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
573                             enum v4l2_mbus_pixelcode *code)
574 {
575         struct i2c_client *client = v4l2_get_subdevdata(sd);
576         struct mt9m001 *mt9m001 = to_mt9m001(client);
577
578         if (index >= mt9m001->num_fmts)
579                 return -EINVAL;
580
581         *code = mt9m001->fmts[index].code;
582         return 0;
583 }
584
585 static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
586                                 struct v4l2_mbus_config *cfg)
587 {
588         struct i2c_client *client = v4l2_get_subdevdata(sd);
589         struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
590
591         /* MT9M001 has all capture_format parameters fixed */
592         cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
593                 V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
594                 V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
595         cfg->type = V4L2_MBUS_PARALLEL;
596         cfg->flags = soc_camera_apply_board_flags(icl, cfg);
597
598         return 0;
599 }
600
601 static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
602                                 const struct v4l2_mbus_config *cfg)
603 {
604         const struct i2c_client *client = v4l2_get_subdevdata(sd);
605         struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
606         struct mt9m001 *mt9m001 = to_mt9m001(client);
607         unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
608
609         if (icl->set_bus_param)
610                 return icl->set_bus_param(icl, 1 << (bps - 1));
611
612         /*
613          * Without board specific bus width settings we only support the
614          * sensors native bus width
615          */
616         return bps == 10 ? 0 : -EINVAL;
617 }
618
619 static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
620         .s_stream       = mt9m001_s_stream,
621         .s_mbus_fmt     = mt9m001_s_fmt,
622         .g_mbus_fmt     = mt9m001_g_fmt,
623         .try_mbus_fmt   = mt9m001_try_fmt,
624         .s_crop         = mt9m001_s_crop,
625         .g_crop         = mt9m001_g_crop,
626         .cropcap        = mt9m001_cropcap,
627         .enum_mbus_fmt  = mt9m001_enum_fmt,
628         .g_mbus_config  = mt9m001_g_mbus_config,
629         .s_mbus_config  = mt9m001_s_mbus_config,
630 };
631
632 static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
633         .g_skip_top_lines       = mt9m001_g_skip_top_lines,
634 };
635
636 static struct v4l2_subdev_ops mt9m001_subdev_ops = {
637         .core   = &mt9m001_subdev_core_ops,
638         .video  = &mt9m001_subdev_video_ops,
639         .sensor = &mt9m001_subdev_sensor_ops,
640 };
641
642 static int mt9m001_probe(struct i2c_client *client,
643                          const struct i2c_device_id *did)
644 {
645         struct mt9m001 *mt9m001;
646         struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
647         struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
648         int ret;
649
650         if (!icl) {
651                 dev_err(&client->dev, "MT9M001 driver needs platform data\n");
652                 return -EINVAL;
653         }
654
655         if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
656                 dev_warn(&adapter->dev,
657                          "I2C-Adapter doesn't support I2C_FUNC_SMBUS_WORD\n");
658                 return -EIO;
659         }
660
661         mt9m001 = kzalloc(sizeof(struct mt9m001), GFP_KERNEL);
662         if (!mt9m001)
663                 return -ENOMEM;
664
665         v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
666         v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
667         v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
668                         V4L2_CID_VFLIP, 0, 1, 1, 0);
669         v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
670                         V4L2_CID_GAIN, 0, 127, 1, 64);
671         mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
672                         V4L2_CID_EXPOSURE, 1, 255, 1, 255);
673         /*
674          * Simulated autoexposure. If enabled, we calculate shutter width
675          * ourselves in the driver based on vertical blanking and frame width
676          */
677         mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
678                         &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
679                         V4L2_EXPOSURE_AUTO);
680         mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
681         if (mt9m001->hdl.error) {
682                 int err = mt9m001->hdl.error;
683
684                 kfree(mt9m001);
685                 return err;
686         }
687         v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
688                                         V4L2_EXPOSURE_MANUAL, true);
689
690         /* Second stage probe - when a capture adapter is there */
691         mt9m001->y_skip_top     = 0;
692         mt9m001->rect.left      = MT9M001_COLUMN_SKIP;
693         mt9m001->rect.top       = MT9M001_ROW_SKIP;
694         mt9m001->rect.width     = MT9M001_MAX_WIDTH;
695         mt9m001->rect.height    = MT9M001_MAX_HEIGHT;
696
697         ret = mt9m001_video_probe(icl, client);
698         if (ret) {
699                 v4l2_ctrl_handler_free(&mt9m001->hdl);
700                 kfree(mt9m001);
701         }
702
703         return ret;
704 }
705
706 static int mt9m001_remove(struct i2c_client *client)
707 {
708         struct mt9m001 *mt9m001 = to_mt9m001(client);
709         struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
710
711         v4l2_device_unregister_subdev(&mt9m001->subdev);
712         v4l2_ctrl_handler_free(&mt9m001->hdl);
713         mt9m001_video_remove(icl);
714         kfree(mt9m001);
715
716         return 0;
717 }
718
719 static const struct i2c_device_id mt9m001_id[] = {
720         { "mt9m001", 0 },
721         { }
722 };
723 MODULE_DEVICE_TABLE(i2c, mt9m001_id);
724
725 static struct i2c_driver mt9m001_i2c_driver = {
726         .driver = {
727                 .name = "mt9m001",
728         },
729         .probe          = mt9m001_probe,
730         .remove         = mt9m001_remove,
731         .id_table       = mt9m001_id,
732 };
733
734 static int __init mt9m001_mod_init(void)
735 {
736         return i2c_add_driver(&mt9m001_i2c_driver);
737 }
738
739 static void __exit mt9m001_mod_exit(void)
740 {
741         i2c_del_driver(&mt9m001_i2c_driver);
742 }
743
744 module_init(mt9m001_mod_init);
745 module_exit(mt9m001_mod_exit);
746
747 MODULE_DESCRIPTION("Micron MT9M001 Camera driver");
748 MODULE_AUTHOR("Guennadi Liakhovetski <kernel@pengutronix.de>");
749 MODULE_LICENSE("GPL");