1 From 6e8b94f48ce58e2b9c09000eb77642f3633a6b0b Mon Sep 17 00:00:00 2001
2 From: Dave Stevenson <dave.stevenson@raspberrypi.com>
3 Date: Wed, 23 Mar 2022 15:48:49 +0000
4 Subject: [PATCH] media: i2c: imx258: Add get_selection for pixel array
7 Libcamera requires the cropping information for each mode, so
8 add this information to the driver.
10 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
12 drivers/media/i2c/imx258.c | 90 ++++++++++++++++++++++++++++++++++++++
13 1 file changed, 90 insertions(+)
15 --- a/drivers/media/i2c/imx258.c
16 +++ b/drivers/media/i2c/imx258.c
18 #define REG_CONFIG_MIRROR_VFLIP 0x02
19 #define REG_CONFIG_FLIP_TEST_PATTERN 0x02
21 +/* IMX258 native and active pixel array size. */
22 +#define IMX258_NATIVE_WIDTH 4224U
23 +#define IMX258_NATIVE_HEIGHT 3192U
24 +#define IMX258_PIXEL_ARRAY_LEFT 8U
25 +#define IMX258_PIXEL_ARRAY_TOP 16U
26 +#define IMX258_PIXEL_ARRAY_WIDTH 4208U
27 +#define IMX258_PIXEL_ARRAY_HEIGHT 3120U
32 @@ -116,6 +124,9 @@ struct imx258_mode {
34 /* Default register values */
35 struct imx258_reg_list reg_list;
37 + /* Analog crop rectangle. */
38 + struct v4l2_rect crop;
41 /* 4208x3120 needs 1267Mbps/lane, 4 lanes. Use that rate on 2 lanes as well */
42 @@ -774,6 +785,12 @@ static const struct imx258_mode supporte
43 .regs = mode_4208x3120_regs,
45 .link_freq_index = IMX258_LINK_FREQ_1267MBPS,
47 + .left = IMX258_PIXEL_ARRAY_LEFT,
48 + .top = IMX258_PIXEL_ARRAY_TOP,
55 @@ -785,6 +802,12 @@ static const struct imx258_mode supporte
56 .regs = mode_2104_1560_regs,
58 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
60 + .left = IMX258_PIXEL_ARRAY_LEFT,
61 + .top = IMX258_PIXEL_ARRAY_TOP,
68 @@ -796,6 +819,12 @@ static const struct imx258_mode supporte
69 .regs = mode_1048_780_regs,
71 .link_freq_index = IMX258_LINK_FREQ_640MBPS,
73 + .left = IMX258_PIXEL_ARRAY_LEFT,
74 + .top = IMX258_PIXEL_ARRAY_TOP,
81 @@ -930,6 +959,7 @@ static int imx258_open(struct v4l2_subde
82 struct imx258 *imx258 = to_imx258(sd);
83 struct v4l2_mbus_framefmt *try_fmt =
84 v4l2_subdev_get_try_format(sd, fh->state, 0);
85 + struct v4l2_rect *try_crop;
87 /* Initialize try_fmt */
88 try_fmt->width = supported_modes[0].width;
89 @@ -937,6 +967,13 @@ static int imx258_open(struct v4l2_subde
90 try_fmt->code = imx258_get_format_code(imx258);
91 try_fmt->field = V4L2_FIELD_NONE;
93 + /* Initialize try_crop */
94 + try_crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
95 + try_crop->left = IMX258_PIXEL_ARRAY_LEFT;
96 + try_crop->top = IMX258_PIXEL_ARRAY_TOP;
97 + try_crop->width = IMX258_PIXEL_ARRAY_WIDTH;
98 + try_crop->height = IMX258_PIXEL_ARRAY_HEIGHT;
103 @@ -1193,6 +1230,58 @@ static int imx258_set_pad_format(struct
107 +static const struct v4l2_rect *
108 +__imx258_get_pad_crop(struct imx258 *imx258,
109 + struct v4l2_subdev_state *sd_state,
110 + unsigned int pad, enum v4l2_subdev_format_whence which)
113 + case V4L2_SUBDEV_FORMAT_TRY:
114 + return v4l2_subdev_get_try_crop(&imx258->sd, sd_state, pad);
115 + case V4L2_SUBDEV_FORMAT_ACTIVE:
116 + return &imx258->cur_mode->crop;
122 +static int imx258_get_selection(struct v4l2_subdev *sd,
123 + struct v4l2_subdev_state *sd_state,
124 + struct v4l2_subdev_selection *sel)
126 + switch (sel->target) {
127 + case V4L2_SEL_TGT_CROP: {
128 + struct imx258 *imx258 = to_imx258(sd);
130 + mutex_lock(&imx258->mutex);
131 + sel->r = *__imx258_get_pad_crop(imx258, sd_state, sel->pad,
133 + mutex_unlock(&imx258->mutex);
138 + case V4L2_SEL_TGT_NATIVE_SIZE:
141 + sel->r.width = IMX258_NATIVE_WIDTH;
142 + sel->r.height = IMX258_NATIVE_HEIGHT;
146 + case V4L2_SEL_TGT_CROP_DEFAULT:
147 + case V4L2_SEL_TGT_CROP_BOUNDS:
148 + sel->r.left = IMX258_PIXEL_ARRAY_LEFT;
149 + sel->r.top = IMX258_PIXEL_ARRAY_TOP;
150 + sel->r.width = IMX258_PIXEL_ARRAY_WIDTH;
151 + sel->r.height = IMX258_PIXEL_ARRAY_HEIGHT;
159 /* Start streaming */
160 static int imx258_start_streaming(struct imx258 *imx258)
162 @@ -1389,6 +1478,7 @@ static const struct v4l2_subdev_pad_ops
163 .get_fmt = imx258_get_pad_format,
164 .set_fmt = imx258_set_pad_format,
165 .enum_frame_size = imx258_enum_frame_size,
166 + .get_selection = imx258_get_selection,
169 static const struct v4l2_subdev_ops imx258_subdev_ops = {