Merge branch 'modsplit-Oct31_2011' of git://git.kernel.org/pub/scm/linux/kernel/git...
[pandora-kernel.git] / drivers / media / video / mt9m001.c
index 6866a9e..e2b1029 100644 (file)
 #include <linux/log2.h>
 #include <linux/module.h>
 
+#include <media/soc_camera.h>
+#include <media/soc_mediabus.h>
 #include <media/v4l2-subdev.h>
 #include <media/v4l2-chip-ident.h>
-#include <media/soc_camera.h>
+#include <media/v4l2-ctrls.h>
 
 /*
  * mt9m001 i2c address 0x5d
@@ -85,15 +87,19 @@ static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
 
 struct mt9m001 {
        struct v4l2_subdev subdev;
+       struct v4l2_ctrl_handler hdl;
+       struct {
+               /* exposure/auto-exposure cluster */
+               struct v4l2_ctrl *autoexposure;
+               struct v4l2_ctrl *exposure;
+       };
        struct v4l2_rect rect;  /* Sensor window */
        const struct mt9m001_datafmt *fmt;
        const struct mt9m001_datafmt *fmts;
        int num_fmts;
        int model;      /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
-       unsigned int gain;
-       unsigned int exposure;
+       unsigned int total_h;
        unsigned short y_skip_top;      /* Lines to skip at the top */
-       unsigned char autoexposure;
 };
 
 static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
@@ -166,54 +172,13 @@ static int mt9m001_s_stream(struct v4l2_subdev *sd, int enable)
        return 0;
 }
 
-static int mt9m001_set_bus_param(struct soc_camera_device *icd,
-                                unsigned long flags)
-{
-       struct soc_camera_link *icl = to_soc_camera_link(icd);
-       unsigned long width_flag = flags & SOCAM_DATAWIDTH_MASK;
-
-       /* Only one width bit may be set */
-       if (!is_power_of_2(width_flag))
-               return -EINVAL;
-
-       if (icl->set_bus_param)
-               return icl->set_bus_param(icl, width_flag);
-
-       /*
-        * Without board specific bus width settings we only support the
-        * sensors native bus width
-        */
-       if (width_flag == SOCAM_DATAWIDTH_10)
-               return 0;
-
-       return -EINVAL;
-}
-
-static unsigned long mt9m001_query_bus_param(struct soc_camera_device *icd)
-{
-       struct soc_camera_link *icl = to_soc_camera_link(icd);
-       /* MT9M001 has all capture_format parameters fixed */
-       unsigned long flags = SOCAM_PCLK_SAMPLE_FALLING |
-               SOCAM_HSYNC_ACTIVE_HIGH | SOCAM_VSYNC_ACTIVE_HIGH |
-               SOCAM_DATA_ACTIVE_HIGH | SOCAM_MASTER;
-
-       if (icl->query_bus_param)
-               flags |= icl->query_bus_param(icl) & SOCAM_DATAWIDTH_MASK;
-       else
-               flags |= SOCAM_DATAWIDTH_10;
-
-       return soc_camera_apply_sensor_flags(icl, flags);
-}
-
 static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
 {
        struct i2c_client *client = v4l2_get_subdevdata(sd);
        struct mt9m001 *mt9m001 = to_mt9m001(client);
        struct v4l2_rect rect = a->c;
-       struct soc_camera_device *icd = client->dev.platform_data;
        int ret;
        const u16 hblank = 9, vblank = 25;
-       unsigned int total_h;
 
        if (mt9m001->fmts == mt9m001_colour_fmts)
                /*
@@ -232,7 +197,7 @@ static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
        soc_camera_limit_side(&rect.top, &rect.height,
                     MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_HEIGHT);
 
-       total_h = rect.height + mt9m001->y_skip_top + vblank;
+       mt9m001->total_h = rect.height + mt9m001->y_skip_top + vblank;
 
        /* Blanking and start values - default... */
        ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
@@ -241,7 +206,7 @@ static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
 
        /*
         * The caller provides a supported format, as verified per
-        * call to icd->try_fmt()
+        * call to .try_mbus_fmt()
         */
        if (!ret)
                ret = reg_write(client, MT9M001_COLUMN_START, rect.left);
@@ -252,17 +217,8 @@ static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
        if (!ret)
                ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
                                rect.height + mt9m001->y_skip_top - 1);
-       if (!ret && mt9m001->autoexposure) {
-               ret = reg_write(client, MT9M001_SHUTTER_WIDTH, total_h);
-               if (!ret) {
-                       const struct v4l2_queryctrl *qctrl =
-                               soc_camera_find_qctrl(icd->ops,
-                                                     V4L2_CID_EXPOSURE);
-                       mt9m001->exposure = (524 + (total_h - 1) *
-                                (qctrl->maximum - qctrl->minimum)) /
-                               1048 + qctrl->minimum;
-               }
-       }
+       if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO)
+               ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h);
 
        if (!ret)
                mt9m001->rect = rect;
@@ -422,107 +378,48 @@ static int mt9m001_s_register(struct v4l2_subdev *sd,
 }
 #endif
 
-static const struct v4l2_queryctrl mt9m001_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_GAIN,
-               .type           = V4L2_CTRL_TYPE_INTEGER,
-               .name           = "Gain",
-               .minimum        = 0,
-               .maximum        = 127,
-               .step           = 1,
-               .default_value  = 64,
-               .flags          = V4L2_CTRL_FLAG_SLIDER,
-       }, {
-               .id             = V4L2_CID_EXPOSURE,
-               .type           = V4L2_CTRL_TYPE_INTEGER,
-               .name           = "Exposure",
-               .minimum        = 1,
-               .maximum        = 255,
-               .step           = 1,
-               .default_value  = 255,
-               .flags          = V4L2_CTRL_FLAG_SLIDER,
-       }, {
-               .id             = V4L2_CID_EXPOSURE_AUTO,
-               .type           = V4L2_CTRL_TYPE_BOOLEAN,
-               .name           = "Automatic Exposure",
-               .minimum        = 0,
-               .maximum        = 1,
-               .step           = 1,
-               .default_value  = 1,
-       }
-};
-
-static struct soc_camera_ops mt9m001_ops = {
-       .set_bus_param          = mt9m001_set_bus_param,
-       .query_bus_param        = mt9m001_query_bus_param,
-       .controls               = mt9m001_controls,
-       .num_controls           = ARRAY_SIZE(mt9m001_controls),
-};
-
-static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
+static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
 {
-       struct i2c_client *client = v4l2_get_subdevdata(sd);
-       struct mt9m001 *mt9m001 = to_mt9m001(client);
-       int data;
+       struct mt9m001 *mt9m001 = container_of(ctrl->handler,
+                                              struct mt9m001, hdl);
+       s32 min, max;
 
        switch (ctrl->id) {
-       case V4L2_CID_VFLIP:
-               data = reg_read(client, MT9M001_READ_OPTIONS2);
-               if (data < 0)
-                       return -EIO;
-               ctrl->value = !!(data & 0x8000);
-               break;
        case V4L2_CID_EXPOSURE_AUTO:
-               ctrl->value = mt9m001->autoexposure;
-               break;
-       case V4L2_CID_GAIN:
-               ctrl->value = mt9m001->gain;
-               break;
-       case V4L2_CID_EXPOSURE:
-               ctrl->value = mt9m001->exposure;
+               min = mt9m001->exposure->minimum;
+               max = mt9m001->exposure->maximum;
+               mt9m001->exposure->val =
+                       (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
                break;
        }
        return 0;
 }
 
-static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
+static int mt9m001_s_ctrl(struct v4l2_ctrl *ctrl)
 {
+       struct mt9m001 *mt9m001 = container_of(ctrl->handler,
+                                              struct mt9m001, hdl);
+       struct v4l2_subdev *sd = &mt9m001->subdev;
        struct i2c_client *client = v4l2_get_subdevdata(sd);
-       struct mt9m001 *mt9m001 = to_mt9m001(client);
-       struct soc_camera_device *icd = client->dev.platform_data;
-       const struct v4l2_queryctrl *qctrl;
+       struct v4l2_ctrl *exp = mt9m001->exposure;
        int data;
 
-       qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
-
-       if (!qctrl)
-               return -EINVAL;
-
        switch (ctrl->id) {
        case V4L2_CID_VFLIP:
-               if (ctrl->value)
+               if (ctrl->val)
                        data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
                else
                        data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
                if (data < 0)
                        return -EIO;
-               break;
+               return 0;
+
        case V4L2_CID_GAIN:
-               if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
-                       return -EINVAL;
                /* See Datasheet Table 7, Gain settings. */
-               if (ctrl->value <= qctrl->default_value) {
+               if (ctrl->val <= ctrl->default_value) {
                        /* Pack it into 0..1 step 0.125, register values 0..8 */
-                       unsigned long range = qctrl->default_value - qctrl->minimum;
-                       data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range;
+                       unsigned long range = ctrl->default_value - ctrl->minimum;
+                       data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
 
                        dev_dbg(&client->dev, "Setting gain %d\n", data);
                        data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
@@ -531,8 +428,8 @@ static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
                } else {
                        /* Pack it into 1.125..15 variable step, register values 9..67 */
                        /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
-                       unsigned long range = qctrl->maximum - qctrl->default_value - 1;
-                       unsigned long gain = ((ctrl->value - qctrl->default_value - 1) *
+                       unsigned long range = ctrl->maximum - ctrl->default_value - 1;
+                       unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
                                               111 + range / 2) / range + 9;
 
                        if (gain <= 32)
@@ -548,66 +445,44 @@ static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
                        if (data < 0)
                                return -EIO;
                }
+               return 0;
 
-               /* Success */
-               mt9m001->gain = ctrl->value;
-               break;
-       case V4L2_CID_EXPOSURE:
-               /* mt9m001 has maximum == default */
-               if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
-                       return -EINVAL;
-               else {
-                       unsigned long range = qctrl->maximum - qctrl->minimum;
-                       unsigned long shutter = ((ctrl->value - qctrl->minimum) * 1048 +
+       case V4L2_CID_EXPOSURE_AUTO:
+               if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
+                       unsigned long range = exp->maximum - exp->minimum;
+                       unsigned long shutter = ((exp->val - exp->minimum) * 1048 +
                                                 range / 2) / range + 1;
 
                        dev_dbg(&client->dev,
                                "Setting shutter width from %d to %lu\n",
-                               reg_read(client, MT9M001_SHUTTER_WIDTH),
-                               shutter);
+                               reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
                        if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
                                return -EIO;
-                       mt9m001->exposure = ctrl->value;
-                       mt9m001->autoexposure = 0;
-               }
-               break;
-       case V4L2_CID_EXPOSURE_AUTO:
-               if (ctrl->value) {
+               } else {
                        const u16 vblank = 25;
-                       unsigned int total_h = mt9m001->rect.height +
+
+                       mt9m001->total_h = mt9m001->rect.height +
                                mt9m001->y_skip_top + vblank;
-                       if (reg_write(client, MT9M001_SHUTTER_WIDTH,
-                                     total_h) < 0)
+                       if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
                                return -EIO;
-                       qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE);
-                       mt9m001->exposure = (524 + (total_h - 1) *
-                                (qctrl->maximum - qctrl->minimum)) /
-                               1048 + qctrl->minimum;
-                       mt9m001->autoexposure = 1;
-               } else
-                       mt9m001->autoexposure = 0;
-               break;
+               }
+               return 0;
        }
-       return 0;
+       return -EINVAL;
 }
 
 /*
  * 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 mt9m001_video_probe(struct soc_camera_device *icd,
+static int mt9m001_video_probe(struct soc_camera_link *icl,
                               struct i2c_client *client)
 {
        struct mt9m001 *mt9m001 = to_mt9m001(client);
-       struct soc_camera_link *icl = to_soc_camera_link(icd);
        s32 data;
        unsigned long flags;
        int ret;
 
-       /* We must have a parent by now. And it cannot be a wrong one. */
-       BUG_ON(!icd->parent ||
-              to_soc_camera_host(icd->parent)->nr != icd->iface);
-
        /* Enable the chip */
        data = reg_write(client, MT9M001_CHIP_ENABLE, 1);
        dev_dbg(&client->dev, "write: %d\n", data);
@@ -662,18 +537,11 @@ static int mt9m001_video_probe(struct soc_camera_device *icd,
                dev_err(&client->dev, "Failed to initialise the camera\n");
 
        /* mt9m001_init() has reset the chip, returning registers to defaults */
-       mt9m001->gain = 64;
-       mt9m001->exposure = 255;
-
-       return ret;
+       return v4l2_ctrl_handler_setup(&mt9m001->hdl);
 }
 
-static void mt9m001_video_remove(struct soc_camera_device *icd)
+static void mt9m001_video_remove(struct soc_camera_link *icl)
 {
-       struct soc_camera_link *icl = to_soc_camera_link(icd);
-
-       dev_dbg(icd->pdev, "Video removed: %p, %p\n",
-               icd->parent, icd->vdev);
        if (icl->free_bus)
                icl->free_bus(icl);
 }
@@ -688,9 +556,12 @@ static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
        return 0;
 }
 
+static const struct v4l2_ctrl_ops mt9m001_ctrl_ops = {
+       .g_volatile_ctrl = mt9m001_g_volatile_ctrl,
+       .s_ctrl = mt9m001_s_ctrl,
+};
+
 static struct v4l2_subdev_core_ops mt9m001_subdev_core_ops = {
-       .g_ctrl         = mt9m001_g_ctrl,
-       .s_ctrl         = mt9m001_s_ctrl,
        .g_chip_ident   = mt9m001_g_chip_ident,
 #ifdef CONFIG_VIDEO_ADV_DEBUG
        .g_register     = mt9m001_g_register,
@@ -711,6 +582,40 @@ static int mt9m001_enum_fmt(struct v4l2_subdev *sd, unsigned int index,
        return 0;
 }
 
+static int mt9m001_g_mbus_config(struct v4l2_subdev *sd,
+                               struct v4l2_mbus_config *cfg)
+{
+       struct i2c_client *client = v4l2_get_subdevdata(sd);
+       struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
+
+       /* MT9M001 has all capture_format parameters fixed */
+       cfg->flags = V4L2_MBUS_PCLK_SAMPLE_FALLING |
+               V4L2_MBUS_HSYNC_ACTIVE_HIGH | V4L2_MBUS_VSYNC_ACTIVE_HIGH |
+               V4L2_MBUS_DATA_ACTIVE_HIGH | V4L2_MBUS_MASTER;
+       cfg->type = V4L2_MBUS_PARALLEL;
+       cfg->flags = soc_camera_apply_board_flags(icl, cfg);
+
+       return 0;
+}
+
+static int mt9m001_s_mbus_config(struct v4l2_subdev *sd,
+                               const struct v4l2_mbus_config *cfg)
+{
+       const struct i2c_client *client = v4l2_get_subdevdata(sd);
+       struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
+       struct mt9m001 *mt9m001 = to_mt9m001(client);
+       unsigned int bps = soc_mbus_get_fmtdesc(mt9m001->fmt->code)->bits_per_sample;
+
+       if (icl->set_bus_param)
+               return icl->set_bus_param(icl, 1 << (bps - 1));
+
+       /*
+        * Without board specific bus width settings we only support the
+        * sensors native bus width
+        */
+       return bps == 10 ? 0 : -EINVAL;
+}
+
 static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
        .s_stream       = mt9m001_s_stream,
        .s_mbus_fmt     = mt9m001_s_fmt,
@@ -720,6 +625,8 @@ static struct v4l2_subdev_video_ops mt9m001_subdev_video_ops = {
        .g_crop         = mt9m001_g_crop,
        .cropcap        = mt9m001_cropcap,
        .enum_mbus_fmt  = mt9m001_enum_fmt,
+       .g_mbus_config  = mt9m001_g_mbus_config,
+       .s_mbus_config  = mt9m001_s_mbus_config,
 };
 
 static struct v4l2_subdev_sensor_ops mt9m001_subdev_sensor_ops = {
@@ -736,17 +643,10 @@ static int mt9m001_probe(struct i2c_client *client,
                         const struct i2c_device_id *did)
 {
        struct mt9m001 *mt9m001;
-       struct soc_camera_device *icd = client->dev.platform_data;
        struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
-       struct soc_camera_link *icl;
+       struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
        int ret;
 
-       if (!icd) {
-               dev_err(&client->dev, "MT9M001: missing soc-camera data!\n");
-               return -EINVAL;
-       }
-
-       icl = to_soc_camera_link(icd);
        if (!icl) {
                dev_err(&client->dev, "MT9M001 driver needs platform data\n");
                return -EINVAL;
@@ -763,25 +663,40 @@ static int mt9m001_probe(struct i2c_client *client,
                return -ENOMEM;
 
        v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops);
+       v4l2_ctrl_handler_init(&mt9m001->hdl, 4);
+       v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
+                       V4L2_CID_VFLIP, 0, 1, 1, 0);
+       v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
+                       V4L2_CID_GAIN, 0, 127, 1, 64);
+       mt9m001->exposure = v4l2_ctrl_new_std(&mt9m001->hdl, &mt9m001_ctrl_ops,
+                       V4L2_CID_EXPOSURE, 1, 255, 1, 255);
+       /*
+        * Simulated autoexposure. If enabled, we calculate shutter width
+        * ourselves in the driver based on vertical blanking and frame width
+        */
+       mt9m001->autoexposure = v4l2_ctrl_new_std_menu(&mt9m001->hdl,
+                       &mt9m001_ctrl_ops, V4L2_CID_EXPOSURE_AUTO, 1, 0,
+                       V4L2_EXPOSURE_AUTO);
+       mt9m001->subdev.ctrl_handler = &mt9m001->hdl;
+       if (mt9m001->hdl.error) {
+               int err = mt9m001->hdl.error;
 
-       /* Second stage probe - when a capture adapter is there */
-       icd->ops                = &mt9m001_ops;
+               kfree(mt9m001);
+               return err;
+       }
+       v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
+                                       V4L2_EXPOSURE_MANUAL, true);
 
+       /* Second stage probe - when a capture adapter is there */
        mt9m001->y_skip_top     = 0;
        mt9m001->rect.left      = MT9M001_COLUMN_SKIP;
        mt9m001->rect.top       = MT9M001_ROW_SKIP;
        mt9m001->rect.width     = MT9M001_MAX_WIDTH;
        mt9m001->rect.height    = MT9M001_MAX_HEIGHT;
 
-       /*
-        * Simulated autoexposure. If enabled, we calculate shutter width
-        * ourselves in the driver based on vertical blanking and frame width
-        */
-       mt9m001->autoexposure = 1;
-
-       ret = mt9m001_video_probe(icd, client);
+       ret = mt9m001_video_probe(icl, client);
        if (ret) {
-               icd->ops = NULL;
+               v4l2_ctrl_handler_free(&mt9m001->hdl);
                kfree(mt9m001);
        }
 
@@ -791,10 +706,11 @@ static int mt9m001_probe(struct i2c_client *client,
 static int mt9m001_remove(struct i2c_client *client)
 {
        struct mt9m001 *mt9m001 = to_mt9m001(client);
-       struct soc_camera_device *icd = client->dev.platform_data;
+       struct soc_camera_link *icl = soc_camera_i2c_to_link(client);
 
-       icd->ops = NULL;
-       mt9m001_video_remove(icd);
+       v4l2_device_unregister_subdev(&mt9m001->subdev);
+       v4l2_ctrl_handler_free(&mt9m001->hdl);
+       mt9m001_video_remove(icl);
        kfree(mt9m001);
 
        return 0;