Merge branch 'v4l_for_2.6.35' of git://git.kernel.org/pub/scm/linux/kernel/git/mcheha...
[safe/jmp/linux-2.6] / drivers / media / video / mt9v022.c
index d2b0981..e7cd23c 100644 (file)
 #include <media/v4l2-chip-ident.h>
 #include <media/soc_camera.h>
 
-/* mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
+/*
+ * mt9v022 i2c address 0x48, 0x4c, 0x58, 0x5c
  * The platform has to define ctruct i2c_board_info objects and link to them
- * from struct soc_camera_link */
+ * from struct soc_camera_link
+ */
 
 static char *sensor_type;
 module_param(sensor_type, charp, S_IRUGO);
@@ -45,7 +47,7 @@ MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
 #define MT9V022_PIXEL_OPERATION_MODE   0x0f
 #define MT9V022_LED_OUT_CONTROL                0x1b
 #define MT9V022_ADC_MODE_CONTROL       0x1c
-#define MT9V022_ANALOG_GAIN            0x34
+#define MT9V022_ANALOG_GAIN            0x35
 #define MT9V022_BLACK_LEVEL_CALIB_CTRL 0x47
 #define MT9V022_PIXCLK_FV_LV           0x74
 #define MT9V022_DIGITAL_TEST_PATTERN   0x7f
@@ -55,39 +57,56 @@ MODULE_PARM_DESC(sensor_type, "Sensor type: \"colour\" or \"monochrome\"");
 /* Progressive scan, master, defaults */
 #define MT9V022_CHIP_CONTROL_DEFAULT   0x188
 
-static const struct soc_camera_data_format mt9v022_colour_formats[] = {
-       /* Order important: first natively supported,
-        * second supported with a GPIO extender */
-       {
-               .name           = "Bayer (sRGB) 10 bit",
-               .depth          = 10,
-               .fourcc         = V4L2_PIX_FMT_SBGGR16,
-               .colorspace     = V4L2_COLORSPACE_SRGB,
-       }, {
-               .name           = "Bayer (sRGB) 8 bit",
-               .depth          = 8,
-               .fourcc         = V4L2_PIX_FMT_SBGGR8,
-               .colorspace     = V4L2_COLORSPACE_SRGB,
-       }
+#define MT9V022_MAX_WIDTH              752
+#define MT9V022_MAX_HEIGHT             480
+#define MT9V022_MIN_WIDTH              48
+#define MT9V022_MIN_HEIGHT             32
+#define MT9V022_COLUMN_SKIP            1
+#define MT9V022_ROW_SKIP               4
+
+/* MT9V022 has only one fixed colorspace per pixelcode */
+struct mt9v022_datafmt {
+       enum v4l2_mbus_pixelcode        code;
+       enum v4l2_colorspace            colorspace;
+};
+
+/* Find a data format by a pixel code in an array */
+static const struct mt9v022_datafmt *mt9v022_find_datafmt(
+       enum v4l2_mbus_pixelcode code, const struct mt9v022_datafmt *fmt,
+       int n)
+{
+       int i;
+       for (i = 0; i < n; i++)
+               if (fmt[i].code == code)
+                       return fmt + i;
+
+       return NULL;
+}
+
+static const struct mt9v022_datafmt mt9v022_colour_fmts[] = {
+       /*
+        * Order important: first natively supported,
+        * second supported with a GPIO extender
+        */
+       {V4L2_MBUS_FMT_SBGGR10_1X10, V4L2_COLORSPACE_SRGB},
+       {V4L2_MBUS_FMT_SBGGR8_1X8, V4L2_COLORSPACE_SRGB},
 };
 
-static const struct soc_camera_data_format mt9v022_monochrome_formats[] = {
+static const struct mt9v022_datafmt mt9v022_monochrome_fmts[] = {
        /* Order important - see above */
-       {
-               .name           = "Monochrome 10 bit",
-               .depth          = 10,
-               .fourcc         = V4L2_PIX_FMT_Y16,
-       }, {
-               .name           = "Monochrome 8 bit",
-               .depth          = 8,
-               .fourcc         = V4L2_PIX_FMT_GREY,
-       },
+       {V4L2_MBUS_FMT_Y10_1X10, V4L2_COLORSPACE_JPEG},
+       {V4L2_MBUS_FMT_GREY8_1X8, V4L2_COLORSPACE_JPEG},
 };
 
 struct mt9v022 {
        struct v4l2_subdev subdev;
+       struct v4l2_rect rect;  /* Sensor window */
+       const struct mt9v022_datafmt *fmt;
+       const struct mt9v022_datafmt *fmts;
+       int num_fmts;
        int model;      /* V4L2_IDENT_MT9V022* codes from v4l2-chip-ident.h */
        u16 chip_control;
+       unsigned short y_skip_top;      /* Lines to skip at the top */
 };
 
 static struct mt9v022 *to_mt9v022(const struct i2c_client *client)
@@ -129,15 +148,16 @@ static int reg_clear(struct i2c_client *client, const u8 reg,
        return reg_write(client, reg, ret & ~data);
 }
 
-static int mt9v022_init(struct soc_camera_device *icd)
+static int mt9v022_init(struct i2c_client *client)
 {
-       struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
        struct mt9v022 *mt9v022 = to_mt9v022(client);
        int ret;
 
-       /* Almost the default mode: master, parallel, simultaneous, and an
+       /*
+        * Almost the default mode: master, parallel, simultaneous, and an
         * undocumented bit 0x200, which is present in table 7, but not in 8,
-        * plus snapshot mode to disable scan for now */
+        * plus snapshot mode to disable scan for now
+        */
        mt9v022->chip_control |= 0x10;
        ret = reg_write(client, MT9V022_CHIP_CONTROL, mt9v022->chip_control);
        if (!ret)
@@ -148,6 +168,10 @@ static int mt9v022_init(struct soc_camera_device *icd)
                /* AEC, AGC on */
                ret = reg_set(client, MT9V022_AEC_AGC_ENABLE, 0x3);
        if (!ret)
+               ret = reg_write(client, MT9V022_ANALOG_GAIN, 16);
+       if (!ret)
+               ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH, 480);
+       if (!ret)
                ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH, 480);
        if (!ret)
                /* default - auto */
@@ -224,7 +248,7 @@ static int mt9v022_set_bus_param(struct soc_camera_device *icd,
        if (ret < 0)
                return ret;
 
-       dev_dbg(&icd->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
+       dev_dbg(&client->dev, "Calculated pixclk 0x%x, chip control 0x%x\n",
                pixclk, mt9v022->chip_control);
 
        return 0;
@@ -233,87 +257,148 @@ static int mt9v022_set_bus_param(struct soc_camera_device *icd,
 static unsigned long mt9v022_query_bus_param(struct soc_camera_device *icd)
 {
        struct soc_camera_link *icl = to_soc_camera_link(icd);
-       unsigned int width_flag;
+       unsigned int flags = SOCAM_MASTER | SOCAM_SLAVE |
+               SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
+               SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
+               SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
+               SOCAM_DATA_ACTIVE_HIGH;
 
        if (icl->query_bus_param)
-               width_flag = icl->query_bus_param(icl) &
-                       SOCAM_DATAWIDTH_MASK;
+               flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
        else
-               width_flag = SOCAM_DATAWIDTH_10;
+               flags |= SOCAM_DATAWIDTH_10;
 
-       return SOCAM_PCLK_SAMPLE_RISING | SOCAM_PCLK_SAMPLE_FALLING |
-               SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_LOW |
-               SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_LOW |
-               SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER | SOCAM_SLAVE |
-               width_flag;
+       return soc_camera_apply_sensor_flags(icl, flags);
 }
 
-static int mt9v022_set_crop(struct soc_camera_device *icd,
-                           struct v4l2_rect *rect)
+static int mt9v022_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
 {
-       struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
+       struct i2c_client *client = sd->priv;
+       struct mt9v022 *mt9v022 = to_mt9v022(client);
+       struct v4l2_rect rect = a->c;
        int ret;
 
+       /* Bayer format - even size lengths */
+       if (mt9v022->fmts == mt9v022_colour_fmts) {
+               rect.width      = ALIGN(rect.width, 2);
+               rect.height     = ALIGN(rect.height, 2);
+               /* Let the user play with the starting pixel */
+       }
+
+       soc_camera_limit_side(&rect.left, &rect.width,
+                    MT9V022_COLUMN_SKIP, MT9V022_MIN_WIDTH, MT9V022_MAX_WIDTH);
+
+       soc_camera_limit_side(&rect.top, &rect.height,
+                    MT9V022_ROW_SKIP, MT9V022_MIN_HEIGHT, MT9V022_MAX_HEIGHT);
+
        /* Like in example app. Contradicts the datasheet though */
        ret = reg_read(client, MT9V022_AEC_AGC_ENABLE);
        if (ret >= 0) {
                if (ret & 1) /* Autoexposure */
                        ret = reg_write(client, MT9V022_MAX_TOTAL_SHUTTER_WIDTH,
-                                       rect->height + icd->y_skip_top + 43);
+                                       rect.height + mt9v022->y_skip_top + 43);
                else
                        ret = reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
-                                       rect->height + icd->y_skip_top + 43);
+                                       rect.height + mt9v022->y_skip_top + 43);
        }
        /* Setup frame format: defaults apart from width and height */
        if (!ret)
-               ret = reg_write(client, MT9V022_COLUMN_START, rect->left);
+               ret = reg_write(client, MT9V022_COLUMN_START, rect.left);
        if (!ret)
-               ret = reg_write(client, MT9V022_ROW_START, rect->top);
+               ret = reg_write(client, MT9V022_ROW_START, rect.top);
        if (!ret)
-               /* Default 94, Phytec driver says:
-                * "width + horizontal blank >= 660" */
+               /*
+                * Default 94, Phytec driver says:
+                * "width + horizontal blank >= 660"
+                */
                ret = reg_write(client, MT9V022_HORIZONTAL_BLANKING,
-                               rect->width > 660 - 43 ? 43 :
-                               660 - rect->width);
+                               rect.width > 660 - 43 ? 43 :
+                               660 - rect.width);
        if (!ret)
                ret = reg_write(client, MT9V022_VERTICAL_BLANKING, 45);
        if (!ret)
-               ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect->width);
+               ret = reg_write(client, MT9V022_WINDOW_WIDTH, rect.width);
        if (!ret)
                ret = reg_write(client, MT9V022_WINDOW_HEIGHT,
-                               rect->height + icd->y_skip_top);
+                               rect.height + mt9v022->y_skip_top);
 
        if (ret < 0)
                return ret;
 
-       dev_dbg(&icd->dev, "Frame %ux%u pixel\n", rect->width, rect->height);
+       dev_dbg(&client->dev, "Frame %dx%d pixel\n", rect.width, rect.height);
+
+       mt9v022->rect = rect;
 
        return 0;
 }
 
-static int mt9v022_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
+static int mt9v022_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
 {
        struct i2c_client *client = sd->priv;
        struct mt9v022 *mt9v022 = to_mt9v022(client);
-       struct soc_camera_device *icd = client->dev.platform_data;
-       struct v4l2_pix_format *pix = &f->fmt.pix;
-       struct v4l2_rect rect = {
-               .left   = icd->rect_current.left,
-               .top    = icd->rect_current.top,
-               .width  = pix->width,
-               .height = pix->height,
+
+       a->c    = mt9v022->rect;
+       a->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+       return 0;
+}
+
+static int mt9v022_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
+{
+       a->bounds.left                  = MT9V022_COLUMN_SKIP;
+       a->bounds.top                   = MT9V022_ROW_SKIP;
+       a->bounds.width                 = MT9V022_MAX_WIDTH;
+       a->bounds.height                = MT9V022_MAX_HEIGHT;
+       a->defrect                      = a->bounds;
+       a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       a->pixelaspect.numerator        = 1;
+       a->pixelaspect.denominator      = 1;
+
+       return 0;
+}
+
+static int mt9v022_g_fmt(struct v4l2_subdev *sd,
+                        struct v4l2_mbus_framefmt *mf)
+{
+       struct i2c_client *client = sd->priv;
+       struct mt9v022 *mt9v022 = to_mt9v022(client);
+
+       mf->width       = mt9v022->rect.width;
+       mf->height      = mt9v022->rect.height;
+       mf->code        = mt9v022->fmt->code;
+       mf->colorspace  = mt9v022->fmt->colorspace;
+       mf->field       = V4L2_FIELD_NONE;
+
+       return 0;
+}
+
+static int mt9v022_s_fmt(struct v4l2_subdev *sd,
+                        struct v4l2_mbus_framefmt *mf)
+{
+       struct i2c_client *client = sd->priv;
+       struct mt9v022 *mt9v022 = to_mt9v022(client);
+       struct v4l2_crop a = {
+               .c = {
+                       .left   = mt9v022->rect.left,
+                       .top    = mt9v022->rect.top,
+                       .width  = mf->width,
+                       .height = mf->height,
+               },
        };
+       int ret;
 
-       /* The caller provides a supported format, as verified per call to
-        * icd->try_fmt(), datawidth is from our supported format list */
-       switch (pix->pixelformat) {
-       case V4L2_PIX_FMT_GREY:
-       case V4L2_PIX_FMT_Y16:
+       /*
+        * The caller provides a supported format, as verified per call to
+        * icd->try_fmt(), datawidth is from our supported format list
+        */
+       switch (mf->code) {
+       case V4L2_MBUS_FMT_GREY8_1X8:
+       case V4L2_MBUS_FMT_Y10_1X10:
                if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATM)
                        return -EINVAL;
                break;
-       case V4L2_PIX_FMT_SBGGR8:
-       case V4L2_PIX_FMT_SBGGR16:
+       case V4L2_MBUS_FMT_SBGGR8_1X8:
+       case V4L2_MBUS_FMT_SBGGR10_1X10:
                if (mt9v022->model != V4L2_IDENT_MT9V022IX7ATC)
                        return -EINVAL;
                break;
@@ -325,18 +410,40 @@ static int mt9v022_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
        }
 
        /* No support for scaling on this camera, just crop. */
-       return mt9v022_set_crop(icd, &rect);
+       ret = mt9v022_s_crop(sd, &a);
+       if (!ret) {
+               mf->width       = mt9v022->rect.width;
+               mf->height      = mt9v022->rect.height;
+               mt9v022->fmt    = mt9v022_find_datafmt(mf->code,
+                                       mt9v022->fmts, mt9v022->num_fmts);
+               mf->colorspace  = mt9v022->fmt->colorspace;
+       }
+
+       return ret;
 }
 
-static int mt9v022_try_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
+static int mt9v022_try_fmt(struct v4l2_subdev *sd,
+                          struct v4l2_mbus_framefmt *mf)
 {
        struct i2c_client *client = sd->priv;
-       struct soc_camera_device *icd = client->dev.platform_data;
-       struct v4l2_pix_format *pix = &f->fmt.pix;
+       struct mt9v022 *mt9v022 = to_mt9v022(client);
+       const struct mt9v022_datafmt *fmt;
+       int align = mf->code == V4L2_MBUS_FMT_SBGGR8_1X8 ||
+               mf->code == V4L2_MBUS_FMT_SBGGR10_1X10;
+
+       v4l_bound_align_image(&mf->width, MT9V022_MIN_WIDTH,
+               MT9V022_MAX_WIDTH, align,
+               &mf->height, MT9V022_MIN_HEIGHT + mt9v022->y_skip_top,
+               MT9V022_MAX_HEIGHT + mt9v022->y_skip_top, align, 0);
+
+       fmt = mt9v022_find_datafmt(mf->code, mt9v022->fmts,
+                                  mt9v022->num_fmts);
+       if (!fmt) {
+               fmt = mt9v022->fmt;
+               mf->code = fmt->code;
+       }
 
-       v4l_bound_align_image(&pix->width, 48, 752, 2 /* ? */,
-                             &pix->height, 32 + icd->y_skip_top,
-                             480 + icd->y_skip_top, 0, 0);
+       mf->colorspace  = fmt->colorspace;
 
        return 0;
 }
@@ -453,8 +560,6 @@ static const struct v4l2_queryctrl mt9v022_controls[] = {
 };
 
 static struct soc_camera_ops mt9v022_ops = {
-       .init                   = mt9v022_init,
-       .set_crop               = mt9v022_set_crop,
        .set_bus_param          = mt9v022_set_bus_param,
        .query_bus_param        = mt9v022_query_bus_param,
        .controls               = mt9v022_controls,
@@ -464,8 +569,12 @@ static struct soc_camera_ops mt9v022_ops = {
 static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
        struct i2c_client *client = sd->priv;
+       const struct v4l2_queryctrl *qctrl;
+       unsigned long range;
        int data;
 
+       qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
+
        switch (ctrl->id) {
        case V4L2_CID_VFLIP:
                data = reg_read(client, MT9V022_READ_MODE);
@@ -491,6 +600,24 @@ static int mt9v022_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
                        return -EIO;
                ctrl->value = !!(data & 0x2);
                break;
+       case V4L2_CID_GAIN:
+               data = reg_read(client, MT9V022_ANALOG_GAIN);
+               if (data < 0)
+                       return -EIO;
+
+               range = qctrl->maximum - qctrl->minimum;
+               ctrl->value = ((data - 16) * range + 24) / 48 + qctrl->minimum;
+
+               break;
+       case V4L2_CID_EXPOSURE:
+               data = reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH);
+               if (data < 0)
+                       return -EIO;
+
+               range = qctrl->maximum - qctrl->minimum;
+               ctrl->value = ((data - 1) * range + 239) / 479 + qctrl->minimum;
+
+               break;
        }
        return 0;
 }
@@ -499,7 +626,6 @@ static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
        int data;
        struct i2c_client *client = sd->priv;
-       struct soc_camera_device *icd = client->dev.platform_data;
        const struct v4l2_queryctrl *qctrl;
 
        qctrl = soc_camera_find_qctrl(&mt9v022_ops, ctrl->id);
@@ -529,25 +655,23 @@ static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
                        return -EINVAL;
                else {
                        unsigned long range = qctrl->maximum - qctrl->minimum;
-                       /* Datasheet says 16 to 64. autogain only works properly
-                        * after setting gain to maximum 14. Larger values
-                        * produce "white fly" noise effect. On the whole,
-                        * manually setting analog gain does no good. */
+                       /* Valid values 16 to 64, 32 to 64 must be even. */
                        unsigned long gain = ((ctrl->value - qctrl->minimum) *
-                                             10 + range / 2) / range + 4;
+                                             48 + range / 2) / range + 16;
                        if (gain >= 32)
                                gain &= ~1;
-                       /* The user wants to set gain manually, hope, she
-                        * knows, what she's doing... Switch AGC off. */
+                       /*
+                        * The user wants to set gain manually, hope, she
+                        * knows, what she's doing... Switch AGC off.
+                        */
 
                        if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x2) < 0)
                                return -EIO;
 
-                       dev_info(&icd->dev, "Setting gain from %d to %lu\n",
-                                reg_read(client, MT9V022_ANALOG_GAIN), gain);
+                       dev_dbg(&client->dev, "Setting gain from %d to %lu\n",
+                               reg_read(client, MT9V022_ANALOG_GAIN), gain);
                        if (reg_write(client, MT9V022_ANALOG_GAIN, gain) < 0)
                                return -EIO;
-                       icd->gain = ctrl->value;
                }
                break;
        case V4L2_CID_EXPOSURE:
@@ -558,19 +682,20 @@ static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
                        unsigned long range = qctrl->maximum - qctrl->minimum;
                        unsigned long shutter = ((ctrl->value - qctrl->minimum) *
                                                 479 + range / 2) / range + 1;
-                       /* The user wants to set shutter width manually, hope,
-                        * she knows, what she's doing... Switch AEC off. */
+                       /*
+                        * The user wants to set shutter width manually, hope,
+                        * she knows, what she's doing... Switch AEC off.
+                        */
 
                        if (reg_clear(client, MT9V022_AEC_AGC_ENABLE, 0x1) < 0)
                                return -EIO;
 
-                       dev_dbg(&icd->dev, "Shutter width from %d to %lu\n",
+                       dev_dbg(&client->dev, "Shutter width from %d to %lu\n",
                                reg_read(client, MT9V022_TOTAL_SHUTTER_WIDTH),
                                shutter);
                        if (reg_write(client, MT9V022_TOTAL_SHUTTER_WIDTH,
                                      shutter) < 0)
                                return -EIO;
-                       icd->exposure = ctrl->value;
                }
                break;
        case V4L2_CID_AUTOGAIN:
@@ -593,8 +718,10 @@ static int mt9v022_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
        return 0;
 }
 
-/* Interface active, can use i2c. If it fails, it can indeed mean, that
- * this wasn't our capture interface, so, we wait for the right one */
+/*
+ * Interface active, can use i2c. If it fails, it can indeed mean, that
+ * this wasn't our capture interface, so, we wait for the right one
+ */
 static int mt9v022_video_probe(struct soc_camera_device *icd,
                               struct i2c_client *client)
 {
@@ -614,7 +741,7 @@ static int mt9v022_video_probe(struct soc_camera_device *icd,
        /* must be 0x1311 or 0x1313 */
        if (data != 0x1311 && data != 0x1313) {
                ret = -ENODEV;
-               dev_info(&icd->dev, "No MT9V022 detected, ID register 0x%x\n",
+               dev_info(&client->dev, "No MT9V022 found, ID register 0x%x\n",
                         data);
                goto ei2c;
        }
@@ -626,7 +753,7 @@ static int mt9v022_video_probe(struct soc_camera_device *icd,
        /* 15 clock cycles */
        udelay(200);
        if (reg_read(client, MT9V022_RESET)) {
-               dev_err(&icd->dev, "Resetting MT9V022 failed!\n");
+               dev_err(&client->dev, "Resetting MT9V022 failed!\n");
                if (ret > 0)
                        ret = -EIO;
                goto ei2c;
@@ -637,17 +764,17 @@ static int mt9v022_video_probe(struct soc_camera_device *icd,
                            !strcmp("color", sensor_type))) {
                ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 4 | 0x11);
                mt9v022->model = V4L2_IDENT_MT9V022IX7ATC;
-               icd->formats = mt9v022_colour_formats;
+               mt9v022->fmts = mt9v022_colour_fmts;
        } else {
                ret = reg_write(client, MT9V022_PIXEL_OPERATION_MODE, 0x11);
                mt9v022->model = V4L2_IDENT_MT9V022IX7ATM;
-               icd->formats = mt9v022_monochrome_formats;
+               mt9v022->fmts = mt9v022_monochrome_fmts;
        }
 
        if (ret < 0)
                goto ei2c;
 
-       icd->num_formats = 0;
+       mt9v022->num_fmts = 0;
 
        /*
         * This is a 10bit sensor, so by default we only allow 10bit.
@@ -660,32 +787,47 @@ static int mt9v022_video_probe(struct soc_camera_device *icd,
                flags = SOCAM_DATAWIDTH_10;
 
        if (flags & SOCAM_DATAWIDTH_10)
-               icd->num_formats++;
+               mt9v022->num_fmts++;
        else
-               icd->formats++;
+               mt9v022->fmts++;
 
        if (flags & SOCAM_DATAWIDTH_8)
-               icd->num_formats++;
+               mt9v022->num_fmts++;
 
-       dev_info(&icd->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
+       mt9v022->fmt = &mt9v022->fmts[0];
+
+       dev_info(&client->dev, "Detected a MT9V022 chip ID %x, %s sensor\n",
                 data, mt9v022->model == V4L2_IDENT_MT9V022IX7ATM ?
                 "monochrome" : "colour");
 
+       ret = mt9v022_init(client);
+       if (ret < 0)
+               dev_err(&client->dev, "Failed to initialise the camera\n");
+
 ei2c:
        return ret;
 }
 
 static void mt9v022_video_remove(struct soc_camera_device *icd)
 {
-       struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
        struct soc_camera_link *icl = to_soc_camera_link(icd);
 
-       dev_dbg(&icd->dev, "Video %x removed: %p, %p\n", client->addr,
+       dev_dbg(&icd->dev, "Video removed: %p, %p\n",
                icd->dev.parent, icd->vdev);
        if (icl->free_bus)
                icl->free_bus(icl);
 }
 
+static int mt9v022_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
+{
+       struct i2c_client *client = sd->priv;
+       struct mt9v022 *mt9v022 = to_mt9v022(client);
+
+       *lines = mt9v022->y_skip_top;
+
+       return 0;
+}
+
 static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
        .g_ctrl         = mt9v022_g_ctrl,
        .s_ctrl         = mt9v022_s_ctrl,
@@ -696,15 +838,38 @@ static struct v4l2_subdev_core_ops mt9v022_subdev_core_ops = {
 #endif
 };
 
+static int mt9v022_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
+                           enum v4l2_mbus_pixelcode *code)
+{
+       struct i2c_client *client = sd->priv;
+       struct mt9v022 *mt9v022 = to_mt9v022(client);
+
+       if (index >= mt9v022->num_fmts)
+               return -EINVAL;
+
+       *code = mt9v022->fmts[index].code;
+       return 0;
+}
+
 static struct v4l2_subdev_video_ops mt9v022_subdev_video_ops = {
        .s_stream       = mt9v022_s_stream,
-       .s_fmt          = mt9v022_s_fmt,
-       .try_fmt        = mt9v022_try_fmt,
+       .s_mbus_fmt     = mt9v022_s_fmt,
+       .g_mbus_fmt     = mt9v022_g_fmt,
+       .try_mbus_fmt   = mt9v022_try_fmt,
+       .s_crop         = mt9v022_s_crop,
+       .g_crop         = mt9v022_g_crop,
+       .cropcap        = mt9v022_cropcap,
+       .enum_mbus_fmt  = mt9v022_enum_fmt,
+};
+
+static struct v4l2_subdev_sensor_ops mt9v022_subdev_sensor_ops = {
+       .g_skip_top_lines       = mt9v022_g_skip_top_lines,
 };
 
 static struct v4l2_subdev_ops mt9v022_subdev_ops = {
        .core   = &mt9v022_subdev_core_ops,
        .video  = &mt9v022_subdev_video_ops,
+       .sensor = &mt9v022_subdev_sensor_ops,
 };
 
 static int mt9v022_probe(struct i2c_client *client,
@@ -742,20 +907,19 @@ static int mt9v022_probe(struct i2c_client *client,
        mt9v022->chip_control = MT9V022_CHIP_CONTROL_DEFAULT;
 
        icd->ops                = &mt9v022_ops;
-       icd->rect_max.left      = 1;
-       icd->rect_max.top       = 4;
-       icd->rect_max.width     = 752;
-       icd->rect_max.height    = 480;
-       icd->rect_current.left  = 1;
-       icd->rect_current.top   = 4;
-       icd->width_min          = 48;
-       icd->height_min         = 32;
-       icd->y_skip_top         = 1;
+       /*
+        * MT9V022 _really_ corrupts the first read out line.
+        * TODO: verify on i.MX31
+        */
+       mt9v022->y_skip_top     = 1;
+       mt9v022->rect.left      = MT9V022_COLUMN_SKIP;
+       mt9v022->rect.top       = MT9V022_ROW_SKIP;
+       mt9v022->rect.width     = MT9V022_MAX_WIDTH;
+       mt9v022->rect.height    = MT9V022_MAX_HEIGHT;
 
        ret = mt9v022_video_probe(icd, client);
        if (ret) {
                icd->ops = NULL;
-               i2c_set_clientdata(client, NULL);
                kfree(mt9v022);
        }
 
@@ -769,7 +933,6 @@ static int mt9v022_remove(struct i2c_client *client)
 
        icd->ops = NULL;
        mt9v022_video_remove(icd);
-       i2c_set_clientdata(client, NULL);
        client->driver = NULL;
        kfree(mt9v022);