Skip to contentSkip to conversation

media: i2c: imx219: Implement get_selection

PATCH Superseded Jacopo Mondi 2 participants 6 messages Apr 26, 2020 – Apr 28, 2020

Subsystems (3)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 media: i2c: imx219: Implement get_selection

Implement the get_selection pad operation for the IMX219 sensor driver. The supported targets report the sensor's native size, the crop default rectangle and the crop rectangle.

---
v1->v2:
- Add crop rectangle for 640x480 mode that was missing in v1
- Rework get selection function as suggested by Laurent
- Add Laurent's tag

---
 drivers/media/i2c/imx219.c | 97 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
drivers/media/i2c/imx219.c +97 -0
index cb03bdec1f9c..2744669fb419 100644
@@ -112,6 +112,14 @@
 #define IMX219_TESTP_BLUE_DEFAULT	0
 #define IMX219_TESTP_GREENB_DEFAULT	0
 
+/* IMX219 native and active pixel array size. */
+#define IMX219_NATIVE_WIDTH		3296U
+#define IMX219_NATIVE_HEIGHT		2480U
+#define IMX219_PIXEL_ARRAY_LEFT		8U
+#define IMX219_PIXEL_ARRAY_TOP		8U
+#define IMX219_PIXEL_ARRAY_WIDTH	3280U
+#define IMX219_PIXEL_ARRAY_HEIGHT	2464U
+
 struct imx219_reg {
 	u16 address;
 	u8 val;
@@ -129,6 +137,9 @@ struct imx219_mode {
 	/* Frame height */
 	unsigned int height;
 
+	/* Analog crop rectangle. */
+	struct v4l2_rect crop;
+
 	/* V-timing */
 	unsigned int vts_def;
 
@@ -463,6 +474,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 8MPix 15fps mode */
 		.width = 3280,
 		.height = 2464,
+		.crop = {
+			.top = 0,
+			.left = 0,
+			.width = 3280,
+			.height = 2464
+		},
 		.vts_def = IMX219_VTS_15FPS,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
@@ -473,6 +490,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 1080P 30fps cropped */
 		.width = 1920,
 		.height = 1080,
+		.crop = {
+			.top = 692,
+			.left = 680,
+			.width = 1920,
+			.height = 1080
+		},
 		.vts_def = IMX219_VTS_30FPS_1080P,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_1920_1080_regs),
@@ -483,6 +506,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 2x2 binned 30fps mode */
 		.width = 1640,
 		.height = 1232,
+		.crop = {
+			.top = 0,
+			.left = 0,
+			.width = 3280,
+			.height = 2464
+		},
 		.vts_def = IMX219_VTS_30FPS_BINNED,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_1640_1232_regs),
@@ -654,6 +683,7 @@ static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 	struct imx219 *imx219 = to_imx219(sd);
 	struct v4l2_mbus_framefmt *try_fmt =
 		v4l2_subdev_get_try_format(sd, fh->pad, 0);
+	struct v4l2_rect *try_crop;
 
 	mutex_lock(&imx219->mutex);
 
@@ -664,6 +694,13 @@ static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 					       MEDIA_BUS_FMT_SRGGB10_1X10);
 	try_fmt->field = V4L2_FIELD_NONE;
 
+	/* Initialize try_crop rectangle. */
+	try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
+	try_crop->top = IMX219_PIXEL_ARRAY_TOP;
+	try_crop->left = IMX219_PIXEL_ARRAY_LEFT;
+	try_crop->width = IMX219_PIXEL_ARRAY_WIDTH;
+	try_crop->height = IMX219_PIXEL_ARRAY_HEIGHT;
+
 	mutex_unlock(&imx219->mutex);
 
 	return 0;
@@ -928,6 +965,65 @@ static int imx219_set_framefmt(struct imx219 *imx219)
 	return -EINVAL;
 }
 
+static const struct v4l2_rect *
+__imx219_get_pad_crop(struct imx219 *imx219, struct v4l2_subdev_pad_config *cfg,
+		      unsigned int pad, enum v4l2_subdev_format_whence which)
+{
+	switch (which) {
+	case V4L2_SUBDEV_FORMAT_TRY:
+		return v4l2_subdev_get_try_crop(&imx219->sd, cfg, pad);
+	case V4L2_SUBDEV_FORMAT_ACTIVE:
+		return &imx219->mode->crop;
+	default:
+		return NULL;
+	}
+}
+
+static int imx219_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_pad_config *cfg,
+				struct v4l2_subdev_selection *sel)
+{
+	struct imx219 *imx219 = to_imx219(sd);
+	const struct v4l2_rect *__crop;
+
+	if (sel->pad != 0)
+		return -EINVAL;
+
+	mutex_lock(&imx219->mutex);
+
+	switch (sel->target) {
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = IMX219_NATIVE_WIDTH;
+		sel->r.height = IMX219_NATIVE_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = IMX219_PIXEL_ARRAY_TOP;
+		sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
+		sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
+		sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
+	case V4L2_SEL_TGT_CROP:
+		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
+					       sel->which);
+		sel->r = *__crop;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+	}
+
+	mutex_unlock(&imx219->mutex);
+
+	return -EINVAL;
+}
+
 static int imx219_start_streaming(struct imx219 *imx219)
 {
 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
@@ -1152,6 +1248,7 @@ static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
 	.enum_mbus_code = imx219_enum_mbus_code,
 	.get_fmt = imx219_get_pad_format,
 	.set_fmt = imx219_set_pad_format,
+	.get_selection = imx219_get_selection,
 	.enum_frame_size = imx219_enum_frame_size,
 };
 

--
2.26.1

#1 lore raw

Hi Jacopo,

Thank you for the patch.

Quoted 150 lines · Jacopo Mondi

Implement the get_selection pad operation for the IMX219 sensor driver. The supported targets report the sensor's native size, the crop default rectangle and the crop rectangle.

---
v1->v2:
- Add crop rectangle for 640x480 mode that was missing in v1
- Rework get selection function as suggested by Laurent
- Add Laurent's tag

---
 drivers/media/i2c/imx219.c | 97 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)
drivers/media/i2c/imx219.c +77 -0
index cb03bdec1f9c..2744669fb419 100644
@@ -112,6 +112,14 @@
 #define IMX219_TESTP_BLUE_DEFAULT	0
 #define IMX219_TESTP_GREENB_DEFAULT	0
 
+/* IMX219 native and active pixel array size. */
+#define IMX219_NATIVE_WIDTH		3296U
+#define IMX219_NATIVE_HEIGHT		2480U
+#define IMX219_PIXEL_ARRAY_LEFT		8U
+#define IMX219_PIXEL_ARRAY_TOP		8U
+#define IMX219_PIXEL_ARRAY_WIDTH	3280U
+#define IMX219_PIXEL_ARRAY_HEIGHT	2464U
+
 struct imx219_reg {
 	u16 address;
 	u8 val;
@@ -129,6 +137,9 @@ struct imx219_mode {
 	/* Frame height */
 	unsigned int height;
 
+	/* Analog crop rectangle. */
+	struct v4l2_rect crop;
+
 	/* V-timing */
 	unsigned int vts_def;
 
@@ -463,6 +474,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 8MPix 15fps mode */
 		.width = 3280,
 		.height = 2464,
+		.crop = {
+			.top = 0,
+			.left = 0,
+			.width = 3280,
+			.height = 2464
+		},
 		.vts_def = IMX219_VTS_15FPS,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
@@ -473,6 +490,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 1080P 30fps cropped */
 		.width = 1920,
 		.height = 1080,
+		.crop = {
+			.top = 692,
+			.left = 680,
+			.width = 1920,
+			.height = 1080
+		},
 		.vts_def = IMX219_VTS_30FPS_1080P,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_1920_1080_regs),
@@ -483,6 +506,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 2x2 binned 30fps mode */
 		.width = 1640,
 		.height = 1232,
+		.crop = {
+			.top = 0,
+			.left = 0,
+			.width = 3280,
+			.height = 2464
+		},
 		.vts_def = IMX219_VTS_30FPS_BINNED,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_1640_1232_regs),
@@ -654,6 +683,7 @@ static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 	struct imx219 *imx219 = to_imx219(sd);
 	struct v4l2_mbus_framefmt *try_fmt =
 		v4l2_subdev_get_try_format(sd, fh->pad, 0);
+	struct v4l2_rect *try_crop;
 
 	mutex_lock(&imx219->mutex);
 
@@ -664,6 +694,13 @@ static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 					       MEDIA_BUS_FMT_SRGGB10_1X10);
 	try_fmt->field = V4L2_FIELD_NONE;
 
+	/* Initialize try_crop rectangle. */
+	try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
+	try_crop->top = IMX219_PIXEL_ARRAY_TOP;
+	try_crop->left = IMX219_PIXEL_ARRAY_LEFT;
+	try_crop->width = IMX219_PIXEL_ARRAY_WIDTH;
+	try_crop->height = IMX219_PIXEL_ARRAY_HEIGHT;
+
 	mutex_unlock(&imx219->mutex);
 
 	return 0;
@@ -928,6 +965,65 @@ static int imx219_set_framefmt(struct imx219 *imx219)
 	return -EINVAL;
 }
 
+static const struct v4l2_rect *
+__imx219_get_pad_crop(struct imx219 *imx219, struct v4l2_subdev_pad_config *cfg,
+		      unsigned int pad, enum v4l2_subdev_format_whence which)
+{
+	switch (which) {
+	case V4L2_SUBDEV_FORMAT_TRY:
+		return v4l2_subdev_get_try_crop(&imx219->sd, cfg, pad);
+	case V4L2_SUBDEV_FORMAT_ACTIVE:
+		return &imx219->mode->crop;
+	default:
+		return NULL;
+	}
+}
+
+static int imx219_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_pad_config *cfg,
+				struct v4l2_subdev_selection *sel)
+{
+	struct imx219 *imx219 = to_imx219(sd);
+	const struct v4l2_rect *__crop;
+
+	if (sel->pad != 0)
+		return -EINVAL;
+
+	mutex_lock(&imx219->mutex);
+
+	switch (sel->target) {
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = IMX219_NATIVE_WIDTH;
+		sel->r.height = IMX219_NATIVE_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = IMX219_PIXEL_ARRAY_TOP;
+		sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
+		sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;

drivers/media/i2c/imx219.c static int imx219_set_framefmt(struct imx219 *imx219) · line 1017

Quoted 10 lines
drivers/media/i2c/imx219.c +10 -0
+		sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
+	case V4L2_SEL_TGT_CROP:
+		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
+					       sel->which);
+		sel->r = *__crop;
+		mutex_unlock(&imx219->mutex);

I should have thought about it before, but only this case requires locking. Maybe

		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
					       sel->which);
		mutex_lock(&imx219->mutex);
		sel->r = *__crop;
		mutex_unlock(&imx219->mutex);

and removing the mutex_lock() at the beginning ?

Quoted 23 lines
drivers/media/i2c/imx219.c +10 -1
+
+		return 0;
+	}
+
+	mutex_unlock(&imx219->mutex);
+
+	return -EINVAL;
+}
+
 static int imx219_start_streaming(struct imx219 *imx219)
 {
 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
@@ -1152,6 +1248,7 @@ static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
 	.enum_mbus_code = imx219_enum_mbus_code,
 	.get_fmt = imx219_get_pad_format,
 	.set_fmt = imx219_set_pad_format,
+	.get_selection = imx219_get_selection,
 	.enum_frame_size = imx219_enum_frame_size,
 };
 
--

2.26.1

Signature · 3 lines
Regards,

Laurent Pinchart
#2 lore raw

Hi Laurent

Quoted 4 lines · Laurent Pinchart

Hi Jacopo,

Thank you for the patch.

[snip]

Quoted 32 lines
Quoted 32 lines
drivers/media/i2c/imx219.c +32 -0
+
+static int imx219_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_pad_config *cfg,
+				struct v4l2_subdev_selection *sel)
+{
+	struct imx219 *imx219 = to_imx219(sd);
+	const struct v4l2_rect *__crop;
+
+	if (sel->pad != 0)
+		return -EINVAL;
+
+	mutex_lock(&imx219->mutex);
+
+	switch (sel->target) {
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = IMX219_NATIVE_WIDTH;
+		sel->r.height = IMX219_NATIVE_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = IMX219_PIXEL_ARRAY_TOP;
+		sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
+		sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
+		sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
Quoted 8 lines

drivers/media/i2c/imx219.c static int imx219_set_framefmt(struct imx219 *imx219) · line 1017

Quoted 5 lines
drivers/media/i2c/imx219.c +5 -0
+	case V4L2_SEL_TGT_CROP:
+		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
+					       sel->which);
+		sel->r = *__crop;
+		mutex_unlock(&imx219->mutex);

I should have thought about it before, but only this case requires locking. Maybe

I should have thought it better, yes

Quoted 4 lines
		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
					       sel->which);
		mutex_lock(&imx219->mutex);

Actually, retreiveing __crop should be protected too, as the crop rectangle is part of the imx219->mode, which is updated by the set_fmt operation.

I'll wrap the whole case content in a critical session.

Thanks
j

Quoted 33 lines
		sel->r = *__crop;
		mutex_unlock(&imx219->mutex);

and removing the mutex_lock() at the beginning ?

Quoted 21 lines
drivers/media/i2c/imx219.c +10 -1
+
+		return 0;
+	}
+
+	mutex_unlock(&imx219->mutex);
+
+	return -EINVAL;
+}
+
 static int imx219_start_streaming(struct imx219 *imx219)
 {
 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
@@ -1152,6 +1248,7 @@ static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
 	.enum_mbus_code = imx219_enum_mbus_code,
 	.get_fmt = imx219_get_pad_format,
 	.set_fmt = imx219_set_pad_format,
+	.get_selection = imx219_get_selection,
 	.enum_frame_size = imx219_enum_frame_size,
 };
 
--
Quoted 2 lines

2.26.1

--
Regards,

Laurent Pinchart

#3 lore raw

Hi Jacopo,

Quoted 52 lines · Jacopo Mondi
Quoted 4 lines · Laurent Pinchart

Hi Jacopo,

Thank you for the patch.

[snip]

Quoted 32 lines
Quoted 32 lines
drivers/media/i2c/imx219.c +32 -0
+
+static int imx219_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_pad_config *cfg,
+				struct v4l2_subdev_selection *sel)
+{
+	struct imx219 *imx219 = to_imx219(sd);
+	const struct v4l2_rect *__crop;
+
+	if (sel->pad != 0)
+		return -EINVAL;
+
+	mutex_lock(&imx219->mutex);
+
+	switch (sel->target) {
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = IMX219_NATIVE_WIDTH;
+		sel->r.height = IMX219_NATIVE_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = IMX219_PIXEL_ARRAY_TOP;
+		sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
+		sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
+		sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
Quoted 8 lines

drivers/media/i2c/imx219.c static int imx219_set_framefmt(struct imx219 *imx219) · line 1017

Quoted 5 lines
drivers/media/i2c/imx219.c +5 -0
+	case V4L2_SEL_TGT_CROP:
+		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
+					       sel->which);
+		sel->r = *__crop;
+		mutex_unlock(&imx219->mutex);

I should have thought about it before, but only this case requires locking. Maybe

I should have thought it better, yes

Quoted 1 line
Quoted 7 lines
Quoted 3 lines
		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
					       sel->which);
		mutex_lock(&imx219->mutex);

Actually, retreiveing __crop should be protected too, as the crop rectangle is part of the imx219->mode, which is updated by the set_fmt operation.

__crop is a pointer, retrieving the pointer doesn't need protection. Accessing its contents does. No big deal though, it's cheap, as long as we don't lock for the bounds and default cases.

Quoted 27 lines

I'll wrap the whole case content in a critical session.

Quoted 25 lines
		sel->r = *__crop;
		mutex_unlock(&imx219->mutex);

and removing the mutex_lock() at the beginning ?

Quoted 20 lines
drivers/media/i2c/imx219.c +10 -0
+
+		return 0;
+	}
+
+	mutex_unlock(&imx219->mutex);
+
+	return -EINVAL;
+}
+
 static int imx219_start_streaming(struct imx219 *imx219)
 {
 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
@@ -1152,6 +1248,7 @@ static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
 	.enum_mbus_code = imx219_enum_mbus_code,
 	.get_fmt = imx219_get_pad_format,
 	.set_fmt = imx219_set_pad_format,
+	.get_selection = imx219_get_selection,
 	.enum_frame_size = imx219_enum_frame_size,
 };
 
Signature · 3 lines
Regards,

Laurent Pinchart
#4 lore raw

Hi Laurent,

Quoted 59 lines · Laurent Pinchart

Hi Jacopo,

Quoted 56 lines · Jacopo Mondi
Quoted 4 lines · Laurent Pinchart

Hi Jacopo,

Thank you for the patch.

[snip]

Quoted 32 lines
Quoted 32 lines
drivers/media/i2c/imx219.c +32 -0
+
+static int imx219_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_pad_config *cfg,
+				struct v4l2_subdev_selection *sel)
+{
+	struct imx219 *imx219 = to_imx219(sd);
+	const struct v4l2_rect *__crop;
+
+	if (sel->pad != 0)
+		return -EINVAL;
+
+	mutex_lock(&imx219->mutex);
+
+	switch (sel->target) {
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = IMX219_NATIVE_WIDTH;
+		sel->r.height = IMX219_NATIVE_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = IMX219_PIXEL_ARRAY_TOP;
+		sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
+		sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
+		sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
+		mutex_unlock(&imx219->mutex);
+
+		return 0;
+
Quoted 8 lines

drivers/media/i2c/imx219.c static int imx219_set_framefmt(struct imx219 *imx219) · line 1017

Quoted 5 lines
drivers/media/i2c/imx219.c +5 -0
+	case V4L2_SEL_TGT_CROP:
+		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
+					       sel->which);
+		sel->r = *__crop;
+		mutex_unlock(&imx219->mutex);

I should have thought about it before, but only this case requires locking. Maybe

I should have thought it better, yes

Quoted 4 lines
		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
					       sel->which);
		mutex_lock(&imx219->mutex);
Quoted 7 lines
Quoted 3 lines

Actually, retreiveing __crop should be protected too, as the crop rectangle is part of the imx219->mode, which is updated by the set_fmt operation.

__crop is a pointer, retrieving the pointer doesn't need protection. Accessing its contents does. No big deal though, it's cheap, as long as we don't lock for the bounds and default cases.

Sorry, I was not clear maybe.

- __crop points to imx219->mode->crop
- set_fmt updates imx219->mode
- in the tiny window between when we get a pointer to imx219->mode->crop and we enter the critical section here a set_fmt call can update the imx219->mode, so we'll have here a pointer to the previous crop rectangle.

Unlikely, but yet, possible.

#5 lore raw

PATCH 1/1 media: i2c: imx219: Implement get_selection

In reply to Jacopo Mondi

Implement the get_selection pad operation for the IMX219 sensor driver. The supported targets report the sensor's native size, the crop default rectangle and the crop rectangle.

---
v2->v2,1
- This is really v2, sorry for the mess

v1->v2:
- Add crop rectangle for 640x480 mode that was missing in v1
- Rework get selection function as suggested by Laurent
- Add Laurent's tag

---
 drivers/media/i2c/imx219.c | 102 +++++++++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
drivers/media/i2c/imx219.c +102 -0
index cb03bdec1f9c..f6fa211ce25e 100644
@@ -112,6 +112,14 @@
 #define IMX219_TESTP_BLUE_DEFAULT	0
 #define IMX219_TESTP_GREENB_DEFAULT	0
 
+/* IMX219 native and active pixel array size. */
+#define IMX219_NATIVE_WIDTH		3296U
+#define IMX219_NATIVE_HEIGHT		2480U
+#define IMX219_PIXEL_ARRAY_LEFT		8U
+#define IMX219_PIXEL_ARRAY_TOP		8U
+#define IMX219_PIXEL_ARRAY_WIDTH	3280U
+#define IMX219_PIXEL_ARRAY_HEIGHT	2464U
+
 struct imx219_reg {
 	u16 address;
 	u8 val;
@@ -129,6 +137,9 @@ struct imx219_mode {
 	/* Frame height */
 	unsigned int height;
 
+	/* Analog crop rectangle. */
+	struct v4l2_rect crop;
+
 	/* V-timing */
 	unsigned int vts_def;
 
@@ -463,6 +474,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 8MPix 15fps mode */
 		.width = 3280,
 		.height = 2464,
+		.crop = {
+			.left = 0,
+			.top = 0,
+			.width = 3280,
+			.height = 2464
+		},
 		.vts_def = IMX219_VTS_15FPS,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
@@ -473,6 +490,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 1080P 30fps cropped */
 		.width = 1920,
 		.height = 1080,
+		.crop = {
+			.left = 680,
+			.top = 692,
+			.width = 1920,
+			.height = 1080
+		},
 		.vts_def = IMX219_VTS_30FPS_1080P,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_1920_1080_regs),
@@ -483,6 +506,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 2x2 binned 30fps mode */
 		.width = 1640,
 		.height = 1232,
+		.crop = {
+			.left = 0,
+			.top = 0,
+			.width = 3280,
+			.height = 2464
+		},
 		.vts_def = IMX219_VTS_30FPS_BINNED,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_1640_1232_regs),
@@ -493,6 +522,12 @@ static const struct imx219_mode supported_modes[] = {
 		/* 640x480 30fps mode */
 		.width = 640,
 		.height = 480,
+		.crop = {
+			.left = 1000,
+			.top = 752,
+			.width = 1280,
+			.height = 960
+		},
 		.vts_def = IMX219_VTS_30FPS_640x480,
 		.reg_list = {
 			.num_of_regs = ARRAY_SIZE(mode_640_480_regs),
@@ -654,6 +689,7 @@ static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 	struct imx219 *imx219 = to_imx219(sd);
 	struct v4l2_mbus_framefmt *try_fmt =
 		v4l2_subdev_get_try_format(sd, fh->pad, 0);
+	struct v4l2_rect *try_crop;
 
 	mutex_lock(&imx219->mutex);
 
@@ -664,6 +700,13 @@ static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
 					       MEDIA_BUS_FMT_SRGGB10_1X10);
 	try_fmt->field = V4L2_FIELD_NONE;
 
+	/* Initialize try_crop rectangle. */
+	try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
+	try_crop->top = IMX219_PIXEL_ARRAY_TOP;
+	try_crop->left = IMX219_PIXEL_ARRAY_LEFT;
+	try_crop->width = IMX219_PIXEL_ARRAY_WIDTH;
+	try_crop->height = IMX219_PIXEL_ARRAY_HEIGHT;
+
 	mutex_unlock(&imx219->mutex);
 
 	return 0;
@@ -928,6 +971,64 @@ static int imx219_set_framefmt(struct imx219 *imx219)
 	return -EINVAL;
 }
 
+static const struct v4l2_rect *
+__imx219_get_pad_crop(struct imx219 *imx219, struct v4l2_subdev_pad_config *cfg,
+		      unsigned int pad, enum v4l2_subdev_format_whence which)
+{
+	switch (which) {
+	case V4L2_SUBDEV_FORMAT_TRY:
+		return v4l2_subdev_get_try_crop(&imx219->sd, cfg, pad);
+	case V4L2_SUBDEV_FORMAT_ACTIVE:
+		return &imx219->mode->crop;
+	default:
+		return NULL;
+	}
+}
+
+static int imx219_get_selection(struct v4l2_subdev *sd,
+				struct v4l2_subdev_pad_config *cfg,
+				struct v4l2_subdev_selection *sel)
+{
+	struct imx219 *imx219 = to_imx219(sd);
+	const struct v4l2_rect *__crop;
+	int ret = 0;
+
+	if (sel->pad != 0)
+		return -EINVAL;
+
+	mutex_lock(&imx219->mutex);
+
+	switch (sel->target) {
+	case V4L2_SEL_TGT_NATIVE_SIZE:
+		sel->r.top = 0;
+		sel->r.left = 0;
+		sel->r.width = IMX219_NATIVE_WIDTH;
+		sel->r.height = IMX219_NATIVE_HEIGHT;
+		break;
+
+	case V4L2_SEL_TGT_CROP_DEFAULT:
+		sel->r.top = IMX219_PIXEL_ARRAY_TOP;
+		sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
+		sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
+		sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
+		break;
+
+	case V4L2_SEL_TGT_CROP:
+		__crop = __imx219_get_pad_crop(imx219, cfg, sel->pad,
+					       sel->which);
+		sel->r = *__crop;
+		break;
+
+	default:
+		ret = -EINVAL;
+		break;
+	}
+
+	mutex_unlock(&imx219->mutex);
+
+	return ret;
+}
+
 static int imx219_start_streaming(struct imx219 *imx219)
 {
 	struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
@@ -1152,6 +1253,7 @@ static const struct v4l2_subdev_pad_ops imx219_pad_ops = {
 	.enum_mbus_code = imx219_enum_mbus_code,
 	.get_fmt = imx219_get_pad_format,
 	.set_fmt = imx219_set_pad_format,
+	.get_selection = imx219_get_selection,
 	.enum_frame_size = imx219_enum_frame_size,
 };
 

--
2.26.1