V4L/DVB (13285): vpfe_capture: keep index within bound in vpfe_cropcap()
[safe/jmp/linux-2.6] / drivers / media / video / ov772x.c
index 702e61a..eccb40a 100644 (file)
@@ -22,7 +22,7 @@
 #include <linux/delay.h>
 #include <linux/videodev2.h>
 #include <media/v4l2-chip-ident.h>
-#include <media/v4l2-common.h>
+#include <media/v4l2-subdev.h>
 #include <media/soc_camera.h>
 #include <media/ov772x.h>
 
 #define GAM15       0x8C /* Gamma Curve 15th segment input end point */
 #define SLOP        0x8D /* Gamma curve highest segment slope */
 #define DNSTH       0x8E /* De-noise threshold */
-#define EDGE0       0x8F /* Edge enhancement control 0 */
-#define EDGE1       0x90 /* Edge enhancement control 1 */
+#define EDGE_STRNGT 0x8F /* Edge strength  control when manual mode */
+#define EDGE_TRSHLD 0x90 /* Edge threshold control when manual mode */
 #define DNSOFF      0x91 /* Auto De-noise threshold control */
-#define EDGE2       0x92 /* Edge enhancement strength low  point control */
-#define EDGE3       0x93 /* Edge enhancement strength high point control */
+#define EDGE_UPPER  0x92 /* Edge strength upper limit when Auto mode */
+#define EDGE_LOWER  0x93 /* Edge strength lower limit when Auto mode */
 #define MTX1        0x94 /* Matrix coefficient 1 */
 #define MTX2        0x95 /* Matrix coefficient 2 */
 #define MTX3        0x96 /* Matrix coefficient 3 */
 #define OCAP_4x         0x03   /* 4x */
 
 /* COM3 */
-#define SWAP_MASK       0x38
+#define SWAP_MASK       (SWAP_RGB | SWAP_YUV | SWAP_ML)
+#define IMG_MASK        (VFLIP_IMG | HFLIP_IMG)
 
-#define VFIMG_ON_OFF    0x80   /* Vertical flip image ON/OFF selection */
-#define HMIMG_ON_OFF    0x40   /* Horizontal mirror image ON/OFF selection */
+#define VFLIP_IMG       0x80   /* Vertical flip image ON/OFF selection */
+#define HFLIP_IMG       0x40   /* Horizontal mirror image ON/OFF selection */
 #define SWAP_RGB        0x20   /* Swap B/R  output sequence in RGB mode */
 #define SWAP_YUV        0x10   /* Swap Y/UV output sequence in YUV mode */
 #define SWAP_ML         0x08   /* Swap output MSB/LSB */
 #define VOSZ_VGA        0xF0
 #define VOSZ_QVGA       0x78
 
+/* DSPAUTO (DSP Auto Function ON/OFF Control) */
+#define AWB_ACTRL       0x80 /* AWB auto threshold control */
+#define DENOISE_ACTRL   0x40 /* De-noise auto threshold control */
+#define EDGE_ACTRL      0x20 /* Edge enhancement auto strength control */
+#define UV_ACTRL        0x10 /* UV adjust auto slope control */
+#define SCAL0_ACTRL     0x08 /* Auto scaling factor control */
+#define SCAL1_2_ACTRL   0x04 /* Auto scaling factor control */
+
 /*
  * ID
  */
@@ -373,11 +382,10 @@ struct regval_list {
 };
 
 struct ov772x_color_format {
-       char                     *name;
-       __u32                     fourcc;
-       u8                        dsp3;
-       u8                        com3;
-       u8                        com7;
+       const struct soc_camera_data_format *format;
+       u8 dsp3;
+       u8 com3;
+       u8 com7;
 };
 
 struct ov772x_win_size {
@@ -389,12 +397,15 @@ struct ov772x_win_size {
 };
 
 struct ov772x_priv {
+       struct v4l2_subdev                subdev;
        struct ov772x_camera_info        *info;
-       struct i2c_client                *client;
-       struct soc_camera_device          icd;
        const struct ov772x_color_format *fmt;
        const struct ov772x_win_size     *win;
        int                               model;
+       unsigned short                    flag_vflip:1;
+       unsigned short                    flag_hflip:1;
+       /* band_filter = COM8[5] ? 256 - BDBASE : 0 */
+       unsigned short                    band_filter;
 };
 
 #define ENDMARKER { 0xff, 0xff }
@@ -470,43 +481,43 @@ static const struct soc_camera_data_format ov772x_fmt_lists[] = {
  */
 static const struct ov772x_color_format ov772x_cfmts[] = {
        {
-               SETFOURCC(YUYV),
+               .format = &ov772x_fmt_lists[0],
                .dsp3   = 0x0,
                .com3   = SWAP_YUV,
                .com7   = OFMT_YUV,
        },
        {
-               SETFOURCC(YVYU),
+               .format = &ov772x_fmt_lists[1],
                .dsp3   = UV_ON,
                .com3   = SWAP_YUV,
                .com7   = OFMT_YUV,
        },
        {
-               SETFOURCC(UYVY),
+               .format = &ov772x_fmt_lists[2],
                .dsp3   = 0x0,
                .com3   = 0x0,
                .com7   = OFMT_YUV,
        },
        {
-               SETFOURCC(RGB555),
+               .format = &ov772x_fmt_lists[3],
                .dsp3   = 0x0,
                .com3   = SWAP_RGB,
                .com7   = FMT_RGB555 | OFMT_RGB,
        },
        {
-               SETFOURCC(RGB555X),
+               .format = &ov772x_fmt_lists[4],
                .dsp3   = 0x0,
                .com3   = 0x0,
                .com7   = FMT_RGB555 | OFMT_RGB,
        },
        {
-               SETFOURCC(RGB565),
+               .format = &ov772x_fmt_lists[5],
                .dsp3   = 0x0,
                .com3   = SWAP_RGB,
                .com7   = FMT_RGB565 | OFMT_RGB,
        },
        {
-               SETFOURCC(RGB565X),
+               .format = &ov772x_fmt_lists[6],
                .dsp3   = 0x0,
                .com3   = 0x0,
                .com7   = FMT_RGB565 | OFMT_RGB,
@@ -540,11 +551,47 @@ static const struct ov772x_win_size ov772x_win_qvga = {
        .regs     = ov772x_qvga_regs,
 };
 
+static const struct v4l2_queryctrl ov772x_controls[] = {
+       {
+               .id             = V4L2_CID_VFLIP,
+               .type           = V4L2_CTRL_TYPE_BOOLEAN,
+               .name           = "Flip Vertically",
+               .minimum        = 0,
+               .maximum        = 1,
+               .step           = 1,
+               .default_value  = 0,
+       },
+       {
+               .id             = V4L2_CID_HFLIP,
+               .type           = V4L2_CTRL_TYPE_BOOLEAN,
+               .name           = "Flip Horizontally",
+               .minimum        = 0,
+               .maximum        = 1,
+               .step           = 1,
+               .default_value  = 0,
+       },
+       {
+               .id             = V4L2_CID_BAND_STOP_FILTER,
+               .type           = V4L2_CTRL_TYPE_INTEGER,
+               .name           = "Band-stop filter",
+               .minimum        = 0,
+               .maximum        = 256,
+               .step           = 1,
+               .default_value  = 0,
+       },
+};
+
 
 /*
  * general function
  */
 
+static struct ov772x_priv *to_ov772x(const struct i2c_client *client)
+{
+       return container_of(i2c_get_clientdata(client), struct ov772x_priv,
+                           subdev);
+}
+
 static int ov772x_write_array(struct i2c_client        *client,
                              const struct regval_list *vals)
 {
@@ -565,8 +612,11 @@ static int ov772x_mask_set(struct i2c_client *client,
                                          u8  set)
 {
        s32 val = i2c_smbus_read_byte_data(client, command);
+       if (val < 0)
+               return val;
+
        val &= ~mask;
-       val |=  set;
+       val |= set & mask;
 
        return i2c_smbus_write_byte_data(client, command, val);
 }
@@ -582,51 +632,26 @@ static int ov772x_reset(struct i2c_client *client)
  * soc_camera_ops function
  */
 
-static int ov772x_init(struct soc_camera_device *icd)
+static int ov772x_s_stream(struct v4l2_subdev *sd, int enable)
 {
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
-       int ret = 0;
+       struct i2c_client *client = sd->priv;
+       struct ov772x_priv *priv = to_ov772x(client);
 
-       if (priv->info->link.power) {
-               ret = priv->info->link.power(&priv->client->dev, 1);
-               if (ret < 0)
-                       return ret;
+       if (!enable) {
+               ov772x_mask_set(client, COM2, SOFT_SLEEP_MODE, SOFT_SLEEP_MODE);
+               return 0;
        }
 
-       if (priv->info->link.reset)
-               ret = priv->info->link.reset(&priv->client->dev);
-
-       return ret;
-}
-
-static int ov772x_release(struct soc_camera_device *icd)
-{
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
-       int ret = 0;
-
-       if (priv->info->link.power)
-               ret = priv->info->link.power(&priv->client->dev, 0);
-
-       return ret;
-}
-
-static int ov772x_start_capture(struct soc_camera_device *icd)
-{
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
-
        if (!priv->win || !priv->fmt) {
-               dev_err(&icd->dev, "norm or win select error\n");
+               dev_err(&client->dev, "norm or win select error\n");
                return -EPERM;
        }
 
-       dev_dbg(&icd->dev,
-                "format %s, win %s\n", priv->fmt->name, priv->win->name);
+       ov772x_mask_set(client, COM2, SOFT_SLEEP_MODE, 0);
 
-       return 0;
-}
+       dev_dbg(&client->dev, "format %s, win %s\n",
+               priv->fmt->format->name, priv->win->name);
 
-static int ov772x_stop_capture(struct soc_camera_device *icd)
-{
        return 0;
 }
 
@@ -638,8 +663,9 @@ static int ov772x_set_bus_param(struct soc_camera_device *icd,
 
 static unsigned long ov772x_query_bus_param(struct soc_camera_device *icd)
 {
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
-       struct soc_camera_link *icl = priv->client->dev.platform_data;
+       struct i2c_client *client = to_i2c_client(to_soc_camera_control(icd));
+       struct ov772x_priv *priv = i2c_get_clientdata(client);
+       struct soc_camera_link *icl = to_soc_camera_link(icd);
        unsigned long flags = SOCAM_PCLK_SAMPLE_RISING | SOCAM_MASTER |
                SOCAM_VSYNC_ACTIVE_HIGH | SOCAM_HSYNC_ACTIVE_HIGH |
                SOCAM_DATA_ACTIVE_HIGH | priv->info->buswidth;
@@ -647,10 +673,80 @@ static unsigned long ov772x_query_bus_param(struct soc_camera_device *icd)
        return soc_camera_apply_sensor_flags(icl, flags);
 }
 
-static int ov772x_get_chip_id(struct soc_camera_device *icd,
-                             struct v4l2_dbg_chip_ident   *id)
+static int ov772x_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
 {
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
+       struct i2c_client *client = sd->priv;
+       struct ov772x_priv *priv = to_ov772x(client);
+
+       switch (ctrl->id) {
+       case V4L2_CID_VFLIP:
+               ctrl->value = priv->flag_vflip;
+               break;
+       case V4L2_CID_HFLIP:
+               ctrl->value = priv->flag_hflip;
+               break;
+       case V4L2_CID_BAND_STOP_FILTER:
+               ctrl->value = priv->band_filter;
+               break;
+       }
+       return 0;
+}
+
+static int ov772x_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
+{
+       struct i2c_client *client = sd->priv;
+       struct ov772x_priv *priv = to_ov772x(client);
+       int ret = 0;
+       u8 val;
+
+       switch (ctrl->id) {
+       case V4L2_CID_VFLIP:
+               val = ctrl->value ? VFLIP_IMG : 0x00;
+               priv->flag_vflip = ctrl->value;
+               if (priv->info->flags & OV772X_FLAG_VFLIP)
+                       val ^= VFLIP_IMG;
+               ret = ov772x_mask_set(client, COM3, VFLIP_IMG, val);
+               break;
+       case V4L2_CID_HFLIP:
+               val = ctrl->value ? HFLIP_IMG : 0x00;
+               priv->flag_hflip = ctrl->value;
+               if (priv->info->flags & OV772X_FLAG_HFLIP)
+                       val ^= HFLIP_IMG;
+               ret = ov772x_mask_set(client, COM3, HFLIP_IMG, val);
+               break;
+       case V4L2_CID_BAND_STOP_FILTER:
+               if ((unsigned)ctrl->value > 256)
+                       ctrl->value = 256;
+               if (ctrl->value == priv->band_filter)
+                       break;
+               if (!ctrl->value) {
+                       /* Switch the filter off, it is on now */
+                       ret = ov772x_mask_set(client, BDBASE, 0xff, 0xff);
+                       if (!ret)
+                               ret = ov772x_mask_set(client, COM8,
+                                                     BNDF_ON_OFF, 0);
+               } else {
+                       /* Switch the filter on, set AEC low limit */
+                       val = 256 - ctrl->value;
+                       ret = ov772x_mask_set(client, COM8,
+                                             BNDF_ON_OFF, BNDF_ON_OFF);
+                       if (!ret)
+                               ret = ov772x_mask_set(client, BDBASE,
+                                                     0xff, val);
+               }
+               if (!ret)
+                       priv->band_filter = ctrl->value;
+               break;
+       }
+
+       return ret;
+}
+
+static int ov772x_g_chip_ident(struct v4l2_subdev *sd,
+                              struct v4l2_dbg_chip_ident *id)
+{
+       struct i2c_client *client = sd->priv;
+       struct ov772x_priv *priv = to_ov772x(client);
 
        id->ident    = priv->model;
        id->revision = 0;
@@ -659,17 +755,17 @@ static int ov772x_get_chip_id(struct soc_camera_device *icd,
 }
 
 #ifdef CONFIG_VIDEO_ADV_DEBUG
-static int ov772x_get_register(struct soc_camera_device *icd,
-                              struct v4l2_dbg_register *reg)
+static int ov772x_g_register(struct v4l2_subdev *sd,
+                            struct v4l2_dbg_register *reg)
 {
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
-       int                 ret;
+       struct i2c_client *client = sd->priv;
+       int ret;
 
        reg->size = 1;
        if (reg->reg > 0xff)
                return -EINVAL;
 
-       ret = i2c_smbus_read_byte_data(priv->client, reg->reg);
+       ret = i2c_smbus_read_byte_data(client, reg->reg);
        if (ret < 0)
                return ret;
 
@@ -678,21 +774,20 @@ static int ov772x_get_register(struct soc_camera_device *icd,
        return 0;
 }
 
-static int ov772x_set_register(struct soc_camera_device *icd,
-                              struct v4l2_dbg_register *reg)
+static int ov772x_s_register(struct v4l2_subdev *sd,
+                            struct v4l2_dbg_register *reg)
 {
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
+       struct i2c_client *client = sd->priv;
 
        if (reg->reg > 0xff ||
            reg->val > 0xff)
                return -EINVAL;
 
-       return i2c_smbus_write_byte_data(priv->client, reg->reg, reg->val);
+       return i2c_smbus_write_byte_data(client, reg->reg, reg->val);
 }
 #endif
 
-static const struct ov772x_win_size*
-ov772x_select_win(u32 width, u32 height)
+static const struct ov772x_win_size *ov772x_select_win(u32 width, u32 height)
 {
        __u32 diff;
        const struct ov772x_win_size *win;
@@ -711,11 +806,10 @@ ov772x_select_win(u32 width, u32 height)
        return win;
 }
 
-static int ov772x_set_fmt(struct soc_camera_device *icd,
-                         __u32                     pixfmt,
-                         struct v4l2_rect         *rect)
+static int ov772x_set_params(struct i2c_client *client,
+                            u32 *width, u32 *height, u32 pixfmt)
 {
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
+       struct ov772x_priv *priv = to_ov772x(client);
        int ret = -EINVAL;
        u8  val;
        int i;
@@ -725,7 +819,7 @@ static int ov772x_set_fmt(struct soc_camera_device *icd,
         */
        priv->fmt = NULL;
        for (i = 0; i < ARRAY_SIZE(ov772x_cfmts); i++) {
-               if (pixfmt == ov772x_cfmts[i].fourcc) {
+               if (pixfmt == ov772x_cfmts[i].format->fourcc) {
                        priv->fmt = ov772x_cfmts + i;
                        break;
                }
@@ -736,17 +830,64 @@ static int ov772x_set_fmt(struct soc_camera_device *icd,
        /*
         * select win
         */
-       priv->win = ov772x_select_win(rect->width, rect->height);
+       priv->win = ov772x_select_win(*width, *height);
 
        /*
         * reset hardware
         */
-       ov772x_reset(priv->client);
+       ov772x_reset(client);
+
+       /*
+        * Edge Ctrl
+        */
+       if (priv->info->edgectrl.strength & OV772X_MANUAL_EDGE_CTRL) {
+
+               /*
+                * Manual Edge Control Mode
+                *
+                * Edge auto strength bit is set by default.
+                * Remove it when manual mode.
+                */
+
+               ret = ov772x_mask_set(client, DSPAUTO, EDGE_ACTRL, 0x00);
+               if (ret < 0)
+                       goto ov772x_set_fmt_error;
+
+               ret = ov772x_mask_set(client,
+                                     EDGE_TRSHLD, EDGE_THRESHOLD_MASK,
+                                     priv->info->edgectrl.threshold);
+               if (ret < 0)
+                       goto ov772x_set_fmt_error;
+
+               ret = ov772x_mask_set(client,
+                                     EDGE_STRNGT, EDGE_STRENGTH_MASK,
+                                     priv->info->edgectrl.strength);
+               if (ret < 0)
+                       goto ov772x_set_fmt_error;
+
+       } else if (priv->info->edgectrl.upper > priv->info->edgectrl.lower) {
+               /*
+                * Auto Edge Control Mode
+                *
+                * set upper and lower limit
+                */
+               ret = ov772x_mask_set(client,
+                                     EDGE_UPPER, EDGE_UPPER_MASK,
+                                     priv->info->edgectrl.upper);
+               if (ret < 0)
+                       goto ov772x_set_fmt_error;
+
+               ret = ov772x_mask_set(client,
+                                     EDGE_LOWER, EDGE_LOWER_MASK,
+                                     priv->info->edgectrl.lower);
+               if (ret < 0)
+                       goto ov772x_set_fmt_error;
+       }
 
        /*
         * set size format
         */
-       ret = ov772x_write_array(priv->client, priv->win->regs);
+       ret = ov772x_write_array(client, priv->win->regs);
        if (ret < 0)
                goto ov772x_set_fmt_error;
 
@@ -755,7 +896,7 @@ static int ov772x_set_fmt(struct soc_camera_device *icd,
         */
        val = priv->fmt->dsp3;
        if (val) {
-               ret = ov772x_mask_set(priv->client,
+               ret = ov772x_mask_set(client,
                                      DSP_CTRL3, UV_MASK, val);
                if (ret < 0)
                        goto ov772x_set_fmt_error;
@@ -765,8 +906,17 @@ static int ov772x_set_fmt(struct soc_camera_device *icd,
         * set COM3
         */
        val = priv->fmt->com3;
-       ret = ov772x_mask_set(priv->client,
-                             COM3, SWAP_MASK, val);
+       if (priv->info->flags & OV772X_FLAG_VFLIP)
+               val |= VFLIP_IMG;
+       if (priv->info->flags & OV772X_FLAG_HFLIP)
+               val |= HFLIP_IMG;
+       if (priv->flag_vflip)
+               val ^= VFLIP_IMG;
+       if (priv->flag_hflip)
+               val ^= HFLIP_IMG;
+
+       ret = ov772x_mask_set(client,
+                             COM3, SWAP_MASK | IMG_MASK, val);
        if (ret < 0)
                goto ov772x_set_fmt_error;
 
@@ -774,25 +924,99 @@ static int ov772x_set_fmt(struct soc_camera_device *icd,
         * set COM7
         */
        val = priv->win->com7_bit | priv->fmt->com7;
-       ret = ov772x_mask_set(priv->client,
+       ret = ov772x_mask_set(client,
                              COM7, (SLCT_MASK | FMT_MASK | OFMT_MASK),
                              val);
        if (ret < 0)
                goto ov772x_set_fmt_error;
 
+       /*
+        * set COM8
+        */
+       if (priv->band_filter) {
+               ret = ov772x_mask_set(client, COM8, BNDF_ON_OFF, 1);
+               if (!ret)
+                       ret = ov772x_mask_set(client, BDBASE,
+                                             0xff, 256 - priv->band_filter);
+               if (ret < 0)
+                       goto ov772x_set_fmt_error;
+       }
+
+       *width = priv->win->width;
+       *height = priv->win->height;
+
        return ret;
 
 ov772x_set_fmt_error:
 
-       ov772x_reset(priv->client);
+       ov772x_reset(client);
        priv->win = NULL;
        priv->fmt = NULL;
 
        return ret;
 }
 
-static int ov772x_try_fmt(struct soc_camera_device *icd,
-                         struct v4l2_format       *f)
+static int ov772x_g_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
+{
+       a->c.left       = 0;
+       a->c.top        = 0;
+       a->c.width      = VGA_WIDTH;
+       a->c.height     = VGA_HEIGHT;
+       a->type         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+       return 0;
+}
+
+static int ov772x_cropcap(struct v4l2_subdev *sd, struct v4l2_cropcap *a)
+{
+       a->bounds.left                  = 0;
+       a->bounds.top                   = 0;
+       a->bounds.width                 = VGA_WIDTH;
+       a->bounds.height                = VGA_HEIGHT;
+       a->defrect                      = a->bounds;
+       a->type                         = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+       a->pixelaspect.numerator        = 1;
+       a->pixelaspect.denominator      = 1;
+
+       return 0;
+}
+
+static int ov772x_g_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
+{
+       struct i2c_client *client = sd->priv;
+       struct ov772x_priv *priv = to_ov772x(client);
+       struct v4l2_pix_format *pix = &f->fmt.pix;
+
+       if (!priv->win || !priv->fmt) {
+               u32 width = VGA_WIDTH, height = VGA_HEIGHT;
+               int ret = ov772x_set_params(client, &width, &height,
+                                           V4L2_PIX_FMT_YUYV);
+               if (ret < 0)
+                       return ret;
+       }
+
+       f->type                 = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+
+       pix->width              = priv->win->width;
+       pix->height             = priv->win->height;
+       pix->pixelformat        = priv->fmt->format->fourcc;
+       pix->colorspace         = priv->fmt->format->colorspace;
+       pix->field              = V4L2_FIELD_NONE;
+
+       return 0;
+}
+
+static int ov772x_s_fmt(struct v4l2_subdev *sd, struct v4l2_format *f)
+{
+       struct i2c_client *client = sd->priv;
+       struct v4l2_pix_format *pix = &f->fmt.pix;
+
+       return ov772x_set_params(client, &pix->width, &pix->height,
+                                pix->pixelformat);
+}
+
+static int ov772x_try_fmt(struct v4l2_subdev *sd,
+                         struct v4l2_format *f)
 {
        struct v4l2_pix_format *pix = &f->fmt.pix;
        const struct ov772x_win_size *win;
@@ -809,9 +1033,10 @@ static int ov772x_try_fmt(struct soc_camera_device *icd,
        return 0;
 }
 
-static int ov772x_video_probe(struct soc_camera_device *icd)
+static int ov772x_video_probe(struct soc_camera_device *icd,
+                             struct i2c_client *client)
 {
-       struct ov772x_priv *priv = container_of(icd, struct ov772x_priv, icd);
+       struct ov772x_priv *priv = to_ov772x(client);
        u8                  pid, ver;
        const char         *devname;
 
@@ -828,7 +1053,7 @@ static int ov772x_video_probe(struct soc_camera_device *icd)
         */
        if (SOCAM_DATAWIDTH_10 != priv->info->buswidth &&
            SOCAM_DATAWIDTH_8  != priv->info->buswidth) {
-               dev_err(&icd->dev, "bus width error\n");
+               dev_err(&client->dev, "bus width error\n");
                return -ENODEV;
        }
 
@@ -838,8 +1063,8 @@ static int ov772x_video_probe(struct soc_camera_device *icd)
        /*
         * check and show product ID and manufacturer ID
         */
-       pid = i2c_smbus_read_byte_data(priv->client, PID);
-       ver = i2c_smbus_read_byte_data(priv->client, VER);
+       pid = i2c_smbus_read_byte_data(client, PID);
+       ver = i2c_smbus_read_byte_data(client, VER);
 
        switch (VERSION(pid, ver)) {
        case OV7720:
@@ -851,63 +1076,78 @@ static int ov772x_video_probe(struct soc_camera_device *icd)
                priv->model = V4L2_IDENT_OV7725;
                break;
        default:
-               dev_err(&icd->dev,
+               dev_err(&client->dev,
                        "Product ID error %x:%x\n", pid, ver);
                return -ENODEV;
        }
 
-       dev_info(&icd->dev,
+       dev_info(&client->dev,
                 "%s Product ID %0x:%0x Manufacturer ID %x:%x\n",
                 devname,
                 pid,
                 ver,
-                i2c_smbus_read_byte_data(priv->client, MIDH),
-                i2c_smbus_read_byte_data(priv->client, MIDL));
-
-       return soc_camera_video_start(icd);
-}
+                i2c_smbus_read_byte_data(client, MIDH),
+                i2c_smbus_read_byte_data(client, MIDL));
 
-static void ov772x_video_remove(struct soc_camera_device *icd)
-{
-       soc_camera_video_stop(icd);
+       return 0;
 }
 
 static struct soc_camera_ops ov772x_ops = {
-       .owner                  = THIS_MODULE,
-       .probe                  = ov772x_video_probe,
-       .remove                 = ov772x_video_remove,
-       .init                   = ov772x_init,
-       .release                = ov772x_release,
-       .start_capture          = ov772x_start_capture,
-       .stop_capture           = ov772x_stop_capture,
-       .set_fmt                = ov772x_set_fmt,
-       .try_fmt                = ov772x_try_fmt,
        .set_bus_param          = ov772x_set_bus_param,
        .query_bus_param        = ov772x_query_bus_param,
-       .get_chip_id            = ov772x_get_chip_id,
+       .controls               = ov772x_controls,
+       .num_controls           = ARRAY_SIZE(ov772x_controls),
+};
+
+static struct v4l2_subdev_core_ops ov772x_subdev_core_ops = {
+       .g_ctrl         = ov772x_g_ctrl,
+       .s_ctrl         = ov772x_s_ctrl,
+       .g_chip_ident   = ov772x_g_chip_ident,
 #ifdef CONFIG_VIDEO_ADV_DEBUG
-       .get_register           = ov772x_get_register,
-       .set_register           = ov772x_set_register,
+       .g_register     = ov772x_g_register,
+       .s_register     = ov772x_s_register,
 #endif
 };
 
+static struct v4l2_subdev_video_ops ov772x_subdev_video_ops = {
+       .s_stream       = ov772x_s_stream,
+       .g_fmt          = ov772x_g_fmt,
+       .s_fmt          = ov772x_s_fmt,
+       .try_fmt        = ov772x_try_fmt,
+       .cropcap        = ov772x_cropcap,
+       .g_crop         = ov772x_g_crop,
+};
+
+static struct v4l2_subdev_ops ov772x_subdev_ops = {
+       .core   = &ov772x_subdev_core_ops,
+       .video  = &ov772x_subdev_video_ops,
+};
+
 /*
  * i2c_driver function
  */
 
 static int ov772x_probe(struct i2c_client *client,
-                        const struct i2c_device_id *did)
+                       const struct i2c_device_id *did)
 {
        struct ov772x_priv        *priv;
        struct ov772x_camera_info *info;
-       struct soc_camera_device  *icd;
+       struct soc_camera_device  *icd = client->dev.platform_data;
        struct i2c_adapter        *adapter = to_i2c_adapter(client->dev.parent);
+       struct soc_camera_link    *icl;
        int                        ret;
 
-       info = client->dev.platform_data;
-       if (!info)
+       if (!icd) {
+               dev_err(&client->dev, "OV772X: missing soc-camera data!\n");
+               return -EINVAL;
+       }
+
+       icl = to_soc_camera_link(icd);
+       if (!icl)
                return -EINVAL;
 
+       info = container_of(icl, struct ov772x_camera_info, link);
+
        if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
                dev_err(&adapter->dev,
                        "I2C-Adapter doesn't support "
@@ -919,20 +1159,15 @@ static int ov772x_probe(struct i2c_client *client,
        if (!priv)
                return -ENOMEM;
 
-       priv->info   = info;
-       priv->client = client;
-       i2c_set_clientdata(client, priv);
+       priv->info = info;
 
-       icd             = &priv->icd;
-       icd->ops        = &ov772x_ops;
-       icd->control    = &client->dev;
-       icd->width_max  = MAX_WIDTH;
-       icd->height_max = MAX_HEIGHT;
-       icd->iface      = priv->info->link.bus_id;
+       v4l2_i2c_subdev_init(&priv->subdev, client, &ov772x_subdev_ops);
 
-       ret = soc_camera_device_register(icd);
+       icd->ops                = &ov772x_ops;
 
+       ret = ov772x_video_probe(icd, client);
        if (ret) {
+               icd->ops = NULL;
                i2c_set_clientdata(client, NULL);
                kfree(priv);
        }
@@ -942,9 +1177,10 @@ static int ov772x_probe(struct i2c_client *client,
 
 static int ov772x_remove(struct i2c_client *client)
 {
-       struct ov772x_priv *priv = i2c_get_clientdata(client);
+       struct ov772x_priv *priv = to_ov772x(client);
+       struct soc_camera_device *icd = client->dev.platform_data;
 
-       soc_camera_device_unregister(&priv->icd);
+       icd->ops = NULL;
        i2c_set_clientdata(client, NULL);
        kfree(priv);
        return 0;