cecf3dd3669877d2c1bdd5003fd734c784e3bcf1
[openwrt/staging/blocktrron.git] /
1 From 99c350f5550237cf14e6d90149e87837b995c11b Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Fri, 10 Mar 2023 17:43:57 +0000
4 Subject: [PATCH] media: imx219: Advertise embedded data node on media
5 pad 1
6
7 This commit updates the imx219 driver to adverise support for embedded
8 data streams. This can then be used by the bcm2835-unicam driver, which
9 has recently been updated to expose the embedded data stream to
10 userland.
11
12 The imx219 sensor subdevice overloads the media pad to differentiate
13 between image stream (pad 0) and embedded data stream (pad 1) when
14 performing the v4l2_subdev_pad_ops functions.
15
16 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
17 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
18 ---
19 drivers/media/i2c/imx219.c | 243 ++++++++++++++++++++++++-------------
20 1 file changed, 161 insertions(+), 82 deletions(-)
21
22 --- a/drivers/media/i2c/imx219.c
23 +++ b/drivers/media/i2c/imx219.c
24 @@ -126,6 +126,16 @@
25 #define IMX219_PIXEL_ARRAY_WIDTH 3280U
26 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
27
28 +/* Embedded metadata stream structure */
29 +#define IMX219_EMBEDDED_LINE_WIDTH 16384
30 +#define IMX219_NUM_EMBEDDED_LINES 1
31 +
32 +enum pad_types {
33 + IMAGE_PAD,
34 + METADATA_PAD,
35 + NUM_PADS
36 +};
37 +
38 struct imx219_reg {
39 u16 address;
40 u8 val;
41 @@ -448,7 +458,7 @@ static const struct imx219_mode supporte
42
43 struct imx219 {
44 struct v4l2_subdev sd;
45 - struct media_pad pad;
46 + struct media_pad pad[NUM_PADS];
47
48 struct v4l2_mbus_framefmt fmt;
49
50 @@ -598,18 +608,26 @@ static void imx219_set_default_format(st
51 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
52 {
53 struct imx219 *imx219 = to_imx219(sd);
54 - struct v4l2_mbus_framefmt *try_fmt =
55 - v4l2_subdev_get_try_format(sd, fh->state, 0);
56 + struct v4l2_mbus_framefmt *try_fmt_img =
57 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
58 + struct v4l2_mbus_framefmt *try_fmt_meta =
59 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
60 struct v4l2_rect *try_crop;
61
62 mutex_lock(&imx219->mutex);
63
64 /* Initialize try_fmt */
65 - try_fmt->width = supported_modes[0].width;
66 - try_fmt->height = supported_modes[0].height;
67 - try_fmt->code = imx219_get_format_code(imx219,
68 - MEDIA_BUS_FMT_SRGGB10_1X10);
69 - try_fmt->field = V4L2_FIELD_NONE;
70 + try_fmt_img->width = supported_modes[0].width;
71 + try_fmt_img->height = supported_modes[0].height;
72 + try_fmt_img->code = imx219_get_format_code(imx219,
73 + MEDIA_BUS_FMT_SRGGB10_1X10);
74 + try_fmt_img->field = V4L2_FIELD_NONE;
75 +
76 + /* Initialize try_fmt for the embedded metadata pad */
77 + try_fmt_meta->width = IMX219_EMBEDDED_LINE_WIDTH;
78 + try_fmt_meta->height = IMX219_NUM_EMBEDDED_LINES;
79 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
80 + try_fmt_meta->field = V4L2_FIELD_NONE;
81
82 /* Initialize try_crop rectangle. */
83 try_crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
84 @@ -723,12 +741,22 @@ static int imx219_enum_mbus_code(struct
85 {
86 struct imx219 *imx219 = to_imx219(sd);
87
88 - if (code->index >= (ARRAY_SIZE(codes) / 4))
89 + if (code->pad >= NUM_PADS)
90 return -EINVAL;
91
92 - mutex_lock(&imx219->mutex);
93 - code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
94 - mutex_unlock(&imx219->mutex);
95 + if (code->pad == IMAGE_PAD) {
96 + if (code->index >= (ARRAY_SIZE(codes) / 4))
97 + return -EINVAL;
98 +
99 + mutex_lock(&imx219->mutex);
100 + code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
101 + mutex_unlock(&imx219->mutex);
102 + } else {
103 + if (code->index > 0)
104 + return -EINVAL;
105 +
106 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
107 + }
108
109 return 0;
110 }
111 @@ -740,19 +768,32 @@ static int imx219_enum_frame_size(struct
112 struct imx219 *imx219 = to_imx219(sd);
113 u32 code;
114
115 - if (fse->index >= ARRAY_SIZE(supported_modes))
116 + if (fse->pad >= NUM_PADS)
117 return -EINVAL;
118
119 - mutex_lock(&imx219->mutex);
120 - code = imx219_get_format_code(imx219, fse->code);
121 - mutex_unlock(&imx219->mutex);
122 - if (fse->code != code)
123 - return -EINVAL;
124 + if (fse->pad == IMAGE_PAD) {
125 + if (fse->index >= ARRAY_SIZE(supported_modes))
126 + return -EINVAL;
127 +
128 + mutex_lock(&imx219->mutex);
129 + code = imx219_get_format_code(imx219, fse->code);
130 + mutex_unlock(&imx219->mutex);
131 + if (fse->code != code)
132 + return -EINVAL;
133 +
134 + fse->min_width = supported_modes[fse->index].width;
135 + fse->max_width = fse->min_width;
136 + fse->min_height = supported_modes[fse->index].height;
137 + fse->max_height = fse->min_height;
138 + } else {
139 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
140 + return -EINVAL;
141
142 - fse->min_width = supported_modes[fse->index].width;
143 - fse->max_width = fse->min_width;
144 - fse->min_height = supported_modes[fse->index].height;
145 - fse->max_height = fse->min_height;
146 + fse->min_width = IMX219_EMBEDDED_LINE_WIDTH;
147 + fse->max_width = fse->min_width;
148 + fse->min_height = IMX219_NUM_EMBEDDED_LINES;
149 + fse->max_height = fse->min_height;
150 + }
151
152 return 0;
153 }
154 @@ -767,9 +808,9 @@ static void imx219_reset_colorspace(stru
155 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
156 }
157
158 -static void imx219_update_pad_format(struct imx219 *imx219,
159 - const struct imx219_mode *mode,
160 - struct v4l2_subdev_format *fmt)
161 +static void imx219_update_image_pad_format(struct imx219 *imx219,
162 + const struct imx219_mode *mode,
163 + struct v4l2_subdev_format *fmt)
164 {
165 fmt->format.width = mode->width;
166 fmt->format.height = mode->height;
167 @@ -777,21 +818,39 @@ static void imx219_update_pad_format(str
168 imx219_reset_colorspace(&fmt->format);
169 }
170
171 +static void imx219_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
172 +{
173 + fmt->format.width = IMX219_EMBEDDED_LINE_WIDTH;
174 + fmt->format.height = IMX219_NUM_EMBEDDED_LINES;
175 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
176 + fmt->format.field = V4L2_FIELD_NONE;
177 +}
178 +
179 static int __imx219_get_pad_format(struct imx219 *imx219,
180 struct v4l2_subdev_state *sd_state,
181 struct v4l2_subdev_format *fmt)
182 {
183 + if (fmt->pad >= NUM_PADS)
184 + return -EINVAL;
185 +
186 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
187 struct v4l2_mbus_framefmt *try_fmt =
188 v4l2_subdev_get_try_format(&imx219->sd, sd_state,
189 fmt->pad);
190 /* update the code which could change due to vflip or hflip: */
191 - try_fmt->code = imx219_get_format_code(imx219, try_fmt->code);
192 + try_fmt->code = fmt->pad == IMAGE_PAD ?
193 + imx219_get_format_code(imx219, try_fmt->code) :
194 + MEDIA_BUS_FMT_SENSOR_DATA;
195 fmt->format = *try_fmt;
196 } else {
197 - imx219_update_pad_format(imx219, imx219->mode, fmt);
198 - fmt->format.code = imx219_get_format_code(imx219,
199 - imx219->fmt.code);
200 + if (fmt->pad == IMAGE_PAD) {
201 + imx219_update_image_pad_format(imx219, imx219->mode,
202 + fmt);
203 + fmt->format.code = imx219_get_format_code(imx219,
204 + imx219->fmt.code);
205 + } else {
206 + imx219_update_metadata_pad_format(fmt);
207 + }
208 }
209
210 return 0;
211 @@ -821,59 +880,78 @@ static int imx219_set_pad_format(struct
212 int exposure_max, exposure_def, hblank;
213 unsigned int i;
214
215 - mutex_lock(&imx219->mutex);
216 -
217 - for (i = 0; i < ARRAY_SIZE(codes); i++)
218 - if (codes[i] == fmt->format.code)
219 - break;
220 - if (i >= ARRAY_SIZE(codes))
221 - i = 0;
222 + if (fmt->pad >= NUM_PADS)
223 + return -EINVAL;
224
225 - /* Bayer order varies with flips */
226 - fmt->format.code = imx219_get_format_code(imx219, codes[i]);
227 + mutex_lock(&imx219->mutex);
228
229 - mode = v4l2_find_nearest_size(supported_modes,
230 - ARRAY_SIZE(supported_modes),
231 - width, height,
232 - fmt->format.width, fmt->format.height);
233 - imx219_update_pad_format(imx219, mode, fmt);
234 - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
235 - framefmt = v4l2_subdev_get_try_format(sd, sd_state, fmt->pad);
236 - *framefmt = fmt->format;
237 - } else if (imx219->mode != mode ||
238 - imx219->fmt.code != fmt->format.code) {
239 - u32 prev_hts = imx219->mode->width + imx219->hblank->val;
240 -
241 - imx219->fmt = fmt->format;
242 - imx219->mode = mode;
243 - /* Update limits and set FPS to default */
244 - __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
245 - IMX219_VTS_MAX - mode->height, 1,
246 - mode->vts_def - mode->height);
247 - __v4l2_ctrl_s_ctrl(imx219->vblank,
248 - mode->vts_def - mode->height);
249 - /* Update max exposure while meeting expected vblanking */
250 - exposure_max = mode->vts_def - 4;
251 - exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
252 - exposure_max : IMX219_EXPOSURE_DEFAULT;
253 - __v4l2_ctrl_modify_range(imx219->exposure,
254 - imx219->exposure->minimum,
255 - exposure_max, imx219->exposure->step,
256 - exposure_def);
257 - /*
258 - * Retain PPL setting from previous mode so that the
259 - * line time does not change on a mode change.
260 - * Limits have to be recomputed as the controls define
261 - * the blanking only, so PPL values need to have the
262 - * mode width subtracted.
263 - */
264 - hblank = prev_hts - mode->width;
265 - __v4l2_ctrl_modify_range(imx219->hblank,
266 - IMX219_PPL_MIN - mode->width,
267 - IMX219_PPL_MAX - mode->width,
268 - 1,
269 - IMX219_PPL_MIN - mode->width);
270 - __v4l2_ctrl_s_ctrl(imx219->hblank, hblank);
271 + if (fmt->pad == IMAGE_PAD) {
272 + for (i = 0; i < ARRAY_SIZE(codes); i++)
273 + if (codes[i] == fmt->format.code)
274 + break;
275 + if (i >= ARRAY_SIZE(codes))
276 + i = 0;
277 +
278 + /* Bayer order varies with flips */
279 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
280 +
281 + mode = v4l2_find_nearest_size(supported_modes,
282 + ARRAY_SIZE(supported_modes),
283 + width, height,
284 + fmt->format.width,
285 + fmt->format.height);
286 + imx219_update_image_pad_format(imx219, mode, fmt);
287 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
288 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
289 + fmt->pad);
290 + *framefmt = fmt->format;
291 + } else if (imx219->mode != mode ||
292 + imx219->fmt.code != fmt->format.code) {
293 + u32 prev_hts = imx219->mode->width + imx219->hblank->val;
294 +
295 + imx219->fmt = fmt->format;
296 + imx219->mode = mode;
297 + /* Update limits and set FPS to default */
298 + __v4l2_ctrl_modify_range(imx219->vblank,
299 + IMX219_VBLANK_MIN,
300 + IMX219_VTS_MAX - mode->height,
301 + 1,
302 + mode->vts_def - mode->height);
303 + __v4l2_ctrl_s_ctrl(imx219->vblank,
304 + mode->vts_def - mode->height);
305 + /* Update max exposure while meeting expected vblanking */
306 + exposure_max = mode->vts_def - 4;
307 + exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
308 + exposure_max : IMX219_EXPOSURE_DEFAULT;
309 + __v4l2_ctrl_modify_range(imx219->exposure,
310 + imx219->exposure->minimum,
311 + exposure_max,
312 + imx219->exposure->step,
313 + exposure_def);
314 + /*
315 + * Retain PPL setting from previous mode so that the
316 + * line time does not change on a mode change.
317 + * Limits have to be recomputed as the controls define
318 + * the blanking only, so PPL values need to have the
319 + * mode width subtracted.
320 + */
321 + hblank = prev_hts - mode->width;
322 + __v4l2_ctrl_modify_range(imx219->hblank,
323 + IMX219_PPL_MIN - mode->width,
324 + IMX219_PPL_MAX - mode->width,
325 + 1,
326 + IMX219_PPL_MIN - mode->width);
327 + __v4l2_ctrl_s_ctrl(imx219->hblank, hblank);
328 + }
329 + } else {
330 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
331 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
332 + fmt->pad);
333 + *framefmt = fmt->format;
334 + } else {
335 + /* Only one embedded data mode is supported */
336 + imx219_update_metadata_pad_format(fmt);
337 + }
338 }
339
340 mutex_unlock(&imx219->mutex);
341 @@ -1490,12 +1568,13 @@ static int imx219_probe(struct i2c_clien
342 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
343
344 /* Initialize source pad */
345 - imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
346 + imx219->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
347 + imx219->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
348
349 /* Initialize default format */
350 imx219_set_default_format(imx219);
351
352 - ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
353 + ret = media_entity_pads_init(&imx219->sd.entity, NUM_PADS, imx219->pad);
354 if (ret) {
355 dev_err(dev, "failed to init entity pads: %d\n", ret);
356 goto error_handler_free;