[media] mt9m001: convert to the control framework

Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
[g.liakhovetski@gmx.de: simplified pointer arithmetic]
Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
This commit is contained in:
Hans Verkuil 2011-09-07 06:04:30 -03:00 committed by Mauro Carvalho Chehab
parent 34e181c521
commit 2dd7d29c78

View File

@ -17,6 +17,7 @@
#include <media/soc_mediabus.h> #include <media/soc_mediabus.h>
#include <media/v4l2-subdev.h> #include <media/v4l2-subdev.h>
#include <media/v4l2-chip-ident.h> #include <media/v4l2-chip-ident.h>
#include <media/v4l2-ctrls.h>
/* /*
* mt9m001 i2c address 0x5d * mt9m001 i2c address 0x5d
@ -85,15 +86,19 @@ static const struct mt9m001_datafmt mt9m001_monochrome_fmts[] = {
struct mt9m001 { struct mt9m001 {
struct v4l2_subdev subdev; 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 */ struct v4l2_rect rect; /* Sensor window */
const struct mt9m001_datafmt *fmt; const struct mt9m001_datafmt *fmt;
const struct mt9m001_datafmt *fmts; const struct mt9m001_datafmt *fmts;
int num_fmts; int num_fmts;
int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */ int model; /* V4L2_IDENT_MT9M001* codes from v4l2-chip-ident.h */
unsigned int gain; unsigned int total_h;
unsigned int exposure;
unsigned short y_skip_top; /* Lines to skip at the top */ unsigned short y_skip_top; /* Lines to skip at the top */
unsigned char autoexposure;
}; };
static struct mt9m001 *to_mt9m001(const struct i2c_client *client) static struct mt9m001 *to_mt9m001(const struct i2c_client *client)
@ -171,10 +176,8 @@ static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
struct i2c_client *client = v4l2_get_subdevdata(sd); struct i2c_client *client = v4l2_get_subdevdata(sd);
struct mt9m001 *mt9m001 = to_mt9m001(client); struct mt9m001 *mt9m001 = to_mt9m001(client);
struct v4l2_rect rect = a->c; struct v4l2_rect rect = a->c;
struct soc_camera_device *icd = client->dev.platform_data;
int ret; int ret;
const u16 hblank = 9, vblank = 25; const u16 hblank = 9, vblank = 25;
unsigned int total_h;
if (mt9m001->fmts == mt9m001_colour_fmts) if (mt9m001->fmts == mt9m001_colour_fmts)
/* /*
@ -193,7 +196,7 @@ static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
soc_camera_limit_side(&rect.top, &rect.height, soc_camera_limit_side(&rect.top, &rect.height,
MT9M001_ROW_SKIP, MT9M001_MIN_HEIGHT, MT9M001_MAX_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... */ /* Blanking and start values - default... */
ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank); ret = reg_write(client, MT9M001_HORIZONTAL_BLANKING, hblank);
@ -213,17 +216,8 @@ static int mt9m001_s_crop(struct v4l2_subdev *sd, struct v4l2_crop *a)
if (!ret) if (!ret)
ret = reg_write(client, MT9M001_WINDOW_HEIGHT, ret = reg_write(client, MT9M001_WINDOW_HEIGHT,
rect.height + mt9m001->y_skip_top - 1); rect.height + mt9m001->y_skip_top - 1);
if (!ret && mt9m001->autoexposure) { if (!ret && v4l2_ctrl_g_ctrl(mt9m001->autoexposure) == V4L2_EXPOSURE_AUTO)
ret = reg_write(client, MT9M001_SHUTTER_WIDTH, total_h); ret = reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->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) if (!ret)
mt9m001->rect = rect; mt9m001->rect = rect;
@ -383,105 +377,48 @@ static int mt9m001_s_register(struct v4l2_subdev *sd,
} }
#endif #endif
static const struct v4l2_queryctrl mt9m001_controls[] = { static int mt9m001_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
.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 = {
.controls = mt9m001_controls,
.num_controls = ARRAY_SIZE(mt9m001_controls),
};
static int mt9m001_g_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
{ {
struct i2c_client *client = v4l2_get_subdevdata(sd); struct mt9m001 *mt9m001 = container_of(ctrl->handler,
struct mt9m001 *mt9m001 = to_mt9m001(client); struct mt9m001, hdl);
int data; s32 min, max;
switch (ctrl->id) { 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: case V4L2_CID_EXPOSURE_AUTO:
ctrl->value = mt9m001->autoexposure; min = mt9m001->exposure->minimum;
break; max = mt9m001->exposure->maximum;
case V4L2_CID_GAIN: mt9m001->exposure->val =
ctrl->value = mt9m001->gain; (524 + (mt9m001->total_h - 1) * (max - min)) / 1048 + min;
break;
case V4L2_CID_EXPOSURE:
ctrl->value = mt9m001->exposure;
break; break;
} }
return 0; 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 i2c_client *client = v4l2_get_subdevdata(sd);
struct mt9m001 *mt9m001 = to_mt9m001(client); struct v4l2_ctrl *exp = mt9m001->exposure;
struct soc_camera_device *icd = client->dev.platform_data;
const struct v4l2_queryctrl *qctrl;
int data; int data;
qctrl = soc_camera_find_qctrl(&mt9m001_ops, ctrl->id);
if (!qctrl)
return -EINVAL;
switch (ctrl->id) { switch (ctrl->id) {
case V4L2_CID_VFLIP: case V4L2_CID_VFLIP:
if (ctrl->value) if (ctrl->val)
data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000); data = reg_set(client, MT9M001_READ_OPTIONS2, 0x8000);
else else
data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000); data = reg_clear(client, MT9M001_READ_OPTIONS2, 0x8000);
if (data < 0) if (data < 0)
return -EIO; return -EIO;
break; return 0;
case V4L2_CID_GAIN: case V4L2_CID_GAIN:
if (ctrl->value > qctrl->maximum || ctrl->value < qctrl->minimum)
return -EINVAL;
/* See Datasheet Table 7, Gain settings. */ /* 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 */ /* Pack it into 0..1 step 0.125, register values 0..8 */
unsigned long range = qctrl->default_value - qctrl->minimum; unsigned long range = ctrl->default_value - ctrl->minimum;
data = ((ctrl->value - qctrl->minimum) * 8 + range / 2) / range; data = ((ctrl->val - ctrl->minimum) * 8 + range / 2) / range;
dev_dbg(&client->dev, "Setting gain %d\n", data); dev_dbg(&client->dev, "Setting gain %d\n", data);
data = reg_write(client, MT9M001_GLOBAL_GAIN, data); data = reg_write(client, MT9M001_GLOBAL_GAIN, data);
@ -490,8 +427,8 @@ static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
} else { } else {
/* Pack it into 1.125..15 variable step, register values 9..67 */ /* Pack it into 1.125..15 variable step, register values 9..67 */
/* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */ /* We assume qctrl->maximum - qctrl->default_value - 1 > 0 */
unsigned long range = qctrl->maximum - qctrl->default_value - 1; unsigned long range = ctrl->maximum - ctrl->default_value - 1;
unsigned long gain = ((ctrl->value - qctrl->default_value - 1) * unsigned long gain = ((ctrl->val - ctrl->default_value - 1) *
111 + range / 2) / range + 9; 111 + range / 2) / range + 9;
if (gain <= 32) if (gain <= 32)
@ -507,47 +444,30 @@ static int mt9m001_s_ctrl(struct v4l2_subdev *sd, struct v4l2_control *ctrl)
if (data < 0) if (data < 0)
return -EIO; return -EIO;
} }
return 0;
/* Success */ case V4L2_CID_EXPOSURE_AUTO:
mt9m001->gain = ctrl->value; if (ctrl->val == V4L2_EXPOSURE_MANUAL) {
break; unsigned long range = exp->maximum - exp->minimum;
case V4L2_CID_EXPOSURE: unsigned long shutter = ((exp->val - exp->minimum) * 1048 +
/* 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 +
range / 2) / range + 1; range / 2) / range + 1;
dev_dbg(&client->dev, dev_dbg(&client->dev,
"Setting shutter width from %d to %lu\n", "Setting shutter width from %d to %lu\n",
reg_read(client, MT9M001_SHUTTER_WIDTH), reg_read(client, MT9M001_SHUTTER_WIDTH), shutter);
shutter);
if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0) if (reg_write(client, MT9M001_SHUTTER_WIDTH, shutter) < 0)
return -EIO; return -EIO;
mt9m001->exposure = ctrl->value; } else {
mt9m001->autoexposure = 0;
}
break;
case V4L2_CID_EXPOSURE_AUTO:
if (ctrl->value) {
const u16 vblank = 25; const u16 vblank = 25;
unsigned int total_h = mt9m001->rect.height +
mt9m001->total_h = mt9m001->rect.height +
mt9m001->y_skip_top + vblank; mt9m001->y_skip_top + vblank;
if (reg_write(client, MT9M001_SHUTTER_WIDTH, if (reg_write(client, MT9M001_SHUTTER_WIDTH, mt9m001->total_h) < 0)
total_h) < 0)
return -EIO; return -EIO;
qctrl = soc_camera_find_qctrl(icd->ops, V4L2_CID_EXPOSURE); }
mt9m001->exposure = (524 + (total_h - 1) * return 0;
(qctrl->maximum - qctrl->minimum)) /
1048 + qctrl->minimum;
mt9m001->autoexposure = 1;
} else
mt9m001->autoexposure = 0;
break;
} }
return 0; return -EINVAL;
} }
/* /*
@ -621,10 +541,7 @@ static int mt9m001_video_probe(struct soc_camera_device *icd,
dev_err(&client->dev, "Failed to initialise the camera\n"); dev_err(&client->dev, "Failed to initialise the camera\n");
/* mt9m001_init() has reset the chip, returning registers to defaults */ /* mt9m001_init() has reset the chip, returning registers to defaults */
mt9m001->gain = 64; return v4l2_ctrl_handler_setup(&mt9m001->hdl);
mt9m001->exposure = 255;
return ret;
} }
static void mt9m001_video_remove(struct soc_camera_device *icd) static void mt9m001_video_remove(struct soc_camera_device *icd)
@ -647,9 +564,12 @@ static int mt9m001_g_skip_top_lines(struct v4l2_subdev *sd, u32 *lines)
return 0; 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 = { 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, .g_chip_ident = mt9m001_g_chip_ident,
#ifdef CONFIG_VIDEO_ADV_DEBUG #ifdef CONFIG_VIDEO_ADV_DEBUG
.g_register = mt9m001_g_register, .g_register = mt9m001_g_register,
@ -765,25 +685,40 @@ static int mt9m001_probe(struct i2c_client *client,
return -ENOMEM; return -ENOMEM;
v4l2_i2c_subdev_init(&mt9m001->subdev, client, &mt9m001_subdev_ops); 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;
kfree(mt9m001);
return err;
}
v4l2_ctrl_auto_cluster(2, &mt9m001->autoexposure,
V4L2_EXPOSURE_MANUAL, true);
/* Second stage probe - when a capture adapter is there */ /* Second stage probe - when a capture adapter is there */
icd->ops = &mt9m001_ops;
mt9m001->y_skip_top = 0; mt9m001->y_skip_top = 0;
mt9m001->rect.left = MT9M001_COLUMN_SKIP; mt9m001->rect.left = MT9M001_COLUMN_SKIP;
mt9m001->rect.top = MT9M001_ROW_SKIP; mt9m001->rect.top = MT9M001_ROW_SKIP;
mt9m001->rect.width = MT9M001_MAX_WIDTH; mt9m001->rect.width = MT9M001_MAX_WIDTH;
mt9m001->rect.height = MT9M001_MAX_HEIGHT; 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(icd, client);
if (ret) { if (ret) {
icd->ops = NULL; v4l2_ctrl_handler_free(&mt9m001->hdl);
kfree(mt9m001); kfree(mt9m001);
} }
@ -795,7 +730,8 @@ static int mt9m001_remove(struct i2c_client *client)
struct mt9m001 *mt9m001 = to_mt9m001(client); struct mt9m001 *mt9m001 = to_mt9m001(client);
struct soc_camera_device *icd = client->dev.platform_data; struct soc_camera_device *icd = client->dev.platform_data;
icd->ops = NULL; v4l2_device_unregister_subdev(&mt9m001->subdev);
v4l2_ctrl_handler_free(&mt9m001->hdl);
mt9m001_video_remove(icd); mt9m001_video_remove(icd);
kfree(mt9m001); kfree(mt9m001);