bcm27xx: update to latest patches from RPi foundation
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.4 / 950-0683-media-i2c-imx219-Implement-get_selection.patch
1 From f479cf37ccda2be7204a964fe2747dfcb4b56bf6 Mon Sep 17 00:00:00 2001
2 From: Jacopo Mondi <jacopo@jmondi.org>
3 Date: Wed, 29 Apr 2020 11:50:38 +0200
4 Subject: [PATCH] media: i2c: imx219: Implement get_selection
5
6 Implement the get_selection pad operation for the IMX219 sensor driver.
7 The supported targets report the sensor's native size, the crop default
8 rectangle and the crop rectangle.
9
10 Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 Signed-off-by: Jacopo Mondi <jacopo@jmondi.org>
12 ---
13 drivers/media/i2c/imx219.c | 94 ++++++++++++++++++++++++++++++++++++++
14 1 file changed, 94 insertions(+)
15
16 --- a/drivers/media/i2c/imx219.c
17 +++ b/drivers/media/i2c/imx219.c
18 @@ -122,6 +122,14 @@ enum pad_types {
19 NUM_PADS
20 };
21
22 +/* IMX219 native and active pixel array size. */
23 +#define IMX219_NATIVE_WIDTH 3296U
24 +#define IMX219_NATIVE_HEIGHT 2480U
25 +#define IMX219_PIXEL_ARRAY_LEFT 8U
26 +#define IMX219_PIXEL_ARRAY_TOP 8U
27 +#define IMX219_PIXEL_ARRAY_WIDTH 3280U
28 +#define IMX219_PIXEL_ARRAY_HEIGHT 2464U
29 +
30 struct imx219_reg {
31 u16 address;
32 u8 val;
33 @@ -139,6 +147,9 @@ struct imx219_mode {
34 /* Frame height */
35 unsigned int height;
36
37 + /* Analog crop rectangle. */
38 + struct v4l2_rect crop;
39 +
40 /* V-timing */
41 unsigned int vts_def;
42
43 @@ -473,6 +484,12 @@ static const struct imx219_mode supporte
44 /* 8MPix 15fps mode */
45 .width = 3280,
46 .height = 2464,
47 + .crop = {
48 + .left = 0,
49 + .top = 0,
50 + .width = 3280,
51 + .height = 2464
52 + },
53 .vts_def = IMX219_VTS_15FPS,
54 .reg_list = {
55 .num_of_regs = ARRAY_SIZE(mode_3280x2464_regs),
56 @@ -483,6 +500,12 @@ static const struct imx219_mode supporte
57 /* 1080P 30fps cropped */
58 .width = 1920,
59 .height = 1080,
60 + .crop = {
61 + .left = 680,
62 + .top = 692,
63 + .width = 1920,
64 + .height = 1080
65 + },
66 .vts_def = IMX219_VTS_30FPS_1080P,
67 .reg_list = {
68 .num_of_regs = ARRAY_SIZE(mode_1920_1080_regs),
69 @@ -493,6 +516,12 @@ static const struct imx219_mode supporte
70 /* 2x2 binned 30fps mode */
71 .width = 1640,
72 .height = 1232,
73 + .crop = {
74 + .left = 0,
75 + .top = 0,
76 + .width = 3280,
77 + .height = 2464
78 + },
79 .vts_def = IMX219_VTS_30FPS_BINNED,
80 .reg_list = {
81 .num_of_regs = ARRAY_SIZE(mode_1640_1232_regs),
82 @@ -503,6 +532,12 @@ static const struct imx219_mode supporte
83 /* 640x480 30fps mode */
84 .width = 640,
85 .height = 480,
86 + .crop = {
87 + .left = 1000,
88 + .top = 752,
89 + .width = 1280,
90 + .height = 960
91 + },
92 .vts_def = IMX219_VTS_30FPS_640x480,
93 .reg_list = {
94 .num_of_regs = ARRAY_SIZE(mode_640_480_regs),
95 @@ -666,6 +701,7 @@ static int imx219_open(struct v4l2_subde
96 v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
97 struct v4l2_mbus_framefmt *try_fmt_meta =
98 v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
99 + struct v4l2_rect *try_crop;
100
101 mutex_lock(&imx219->mutex);
102
103 @@ -682,6 +718,13 @@ static int imx219_open(struct v4l2_subde
104 try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
105 try_fmt_meta->field = V4L2_FIELD_NONE;
106
107 + /* Initialize try_crop rectangle. */
108 + try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
109 + try_crop->top = IMX219_PIXEL_ARRAY_TOP;
110 + try_crop->left = IMX219_PIXEL_ARRAY_LEFT;
111 + try_crop->width = IMX219_PIXEL_ARRAY_WIDTH;
112 + try_crop->height = IMX219_PIXEL_ARRAY_HEIGHT;
113 +
114 mutex_unlock(&imx219->mutex);
115
116 return 0;
117 @@ -1011,6 +1054,56 @@ static int imx219_set_framefmt(struct im
118 return -EINVAL;
119 }
120
121 +static const struct v4l2_rect *
122 +__imx219_get_pad_crop(struct imx219 *imx219, struct v4l2_subdev_pad_config *cfg,
123 + unsigned int pad, enum v4l2_subdev_format_whence which)
124 +{
125 + switch (which) {
126 + case V4L2_SUBDEV_FORMAT_TRY:
127 + return v4l2_subdev_get_try_crop(&imx219->sd, cfg, pad);
128 + case V4L2_SUBDEV_FORMAT_ACTIVE:
129 + return &imx219->mode->crop;
130 + }
131 +
132 + return NULL;
133 +}
134 +
135 +static int imx219_get_selection(struct v4l2_subdev *sd,
136 + struct v4l2_subdev_pad_config *cfg,
137 + struct v4l2_subdev_selection *sel)
138 +{
139 + switch (sel->target) {
140 + case V4L2_SEL_TGT_CROP: {
141 + struct imx219 *imx219 = to_imx219(sd);
142 +
143 + mutex_lock(&imx219->mutex);
144 + sel->r = *__imx219_get_pad_crop(imx219, cfg, sel->pad,
145 + sel->which);
146 + mutex_unlock(&imx219->mutex);
147 +
148 + return 0;
149 + }
150 +
151 + case V4L2_SEL_TGT_NATIVE_SIZE:
152 + sel->r.top = 0;
153 + sel->r.left = 0;
154 + sel->r.width = IMX219_NATIVE_WIDTH;
155 + sel->r.height = IMX219_NATIVE_HEIGHT;
156 +
157 + return 0;
158 +
159 + case V4L2_SEL_TGT_CROP_DEFAULT:
160 + sel->r.top = IMX219_PIXEL_ARRAY_TOP;
161 + sel->r.left = IMX219_PIXEL_ARRAY_LEFT;
162 + sel->r.width = IMX219_PIXEL_ARRAY_WIDTH;
163 + sel->r.height = IMX219_PIXEL_ARRAY_HEIGHT;
164 +
165 + return 0;
166 + }
167 +
168 + return -EINVAL;
169 +}
170 +
171 static int imx219_start_streaming(struct imx219 *imx219)
172 {
173 struct i2c_client *client = v4l2_get_subdevdata(&imx219->sd);
174 @@ -1235,6 +1328,7 @@ static const struct v4l2_subdev_pad_ops
175 .enum_mbus_code = imx219_enum_mbus_code,
176 .get_fmt = imx219_get_pad_format,
177 .set_fmt = imx219_set_pad_format,
178 + .get_selection = imx219_get_selection,
179 .enum_frame_size = imx219_enum_frame_size,
180 };
181