1 From c144d907a9067f948706a4cb8709672b0708c4c4 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Wed, 8 Dec 2021 13:22:48 +0100
4 Subject: [PATCH] media: imx219: Advertise embedded data node on media
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
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.
16 Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
17 [JMH: Adapt to the mainline 5.16 kernel]
18 Signed-off-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>
20 drivers/media/i2c/imx219.c | 254 ++++++++++++++++++++++++-------------
21 1 file changed, 169 insertions(+), 85 deletions(-)
23 --- a/drivers/media/i2c/imx219.c
24 +++ b/drivers/media/i2c/imx219.c
26 #define IMX219_PIXEL_ARRAY_WIDTH 3280U
27 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
29 +/* Embedded metadata stream structure */
30 +#define IMX219_EMBEDDED_LINE_WIDTH 16384
31 +#define IMX219_NUM_EMBEDDED_LINES 1
42 @@ -448,7 +458,7 @@ static const struct imx219_mode supporte
45 struct v4l2_subdev sd;
46 - struct media_pad pad;
47 + struct media_pad pad[NUM_PADS];
49 struct v4l2_mbus_framefmt fmt;
51 @@ -598,18 +608,26 @@ static void imx219_set_default_format(st
52 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
54 struct imx219 *imx219 = to_imx219(sd);
55 - struct v4l2_mbus_framefmt *try_fmt =
56 - v4l2_subdev_get_try_format(sd, fh->state, 0);
57 + struct v4l2_mbus_framefmt *try_fmt_img =
58 + v4l2_subdev_get_try_format(sd, fh->state, IMAGE_PAD);
59 + struct v4l2_mbus_framefmt *try_fmt_meta =
60 + v4l2_subdev_get_try_format(sd, fh->state, METADATA_PAD);
61 struct v4l2_rect *try_crop;
63 mutex_lock(&imx219->mutex);
65 - /* Initialize try_fmt */
66 - try_fmt->width = supported_modes[0].width;
67 - try_fmt->height = supported_modes[0].height;
68 - try_fmt->code = imx219_get_format_code(imx219,
69 - MEDIA_BUS_FMT_SRGGB10_1X10);
70 - try_fmt->field = V4L2_FIELD_NONE;
71 + /* Initialize try_fmt for the image pad */
72 + try_fmt_img->width = supported_modes[0].width;
73 + try_fmt_img->height = supported_modes[0].height;
74 + try_fmt_img->code = imx219_get_format_code(imx219,
75 + MEDIA_BUS_FMT_SRGGB10_1X10);
76 + try_fmt_img->field = V4L2_FIELD_NONE;
78 + /* Initialize try_fmt for the embedded metadata pad */
79 + try_fmt_meta->width = IMX219_EMBEDDED_LINE_WIDTH;
80 + try_fmt_meta->height = IMX219_NUM_EMBEDDED_LINES;
81 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
82 + try_fmt_meta->field = V4L2_FIELD_NONE;
84 /* Initialize try_crop rectangle. */
85 try_crop = v4l2_subdev_get_try_crop(sd, fh->state, 0);
86 @@ -718,12 +736,21 @@ static int imx219_enum_mbus_code(struct
88 struct imx219 *imx219 = to_imx219(sd);
90 - if (code->index >= (ARRAY_SIZE(codes) / 4))
91 + if (code->pad >= NUM_PADS)
94 - mutex_lock(&imx219->mutex);
95 - code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
96 - mutex_unlock(&imx219->mutex);
97 + if (code->pad == IMAGE_PAD) {
98 + if (code->index >= (ARRAY_SIZE(codes) / 4))
101 + code->code = imx219_get_format_code(imx219,
102 + codes[code->index * 4]);
104 + if (code->index > 0)
107 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
112 @@ -733,21 +760,30 @@ static int imx219_enum_frame_size(struct
113 struct v4l2_subdev_frame_size_enum *fse)
115 struct imx219 *imx219 = to_imx219(sd);
118 - if (fse->index >= ARRAY_SIZE(supported_modes))
119 + if (fse->pad >= NUM_PADS)
122 - mutex_lock(&imx219->mutex);
123 - code = imx219_get_format_code(imx219, fse->code);
124 - mutex_unlock(&imx219->mutex);
125 - if (fse->code != code)
127 + if (fse->pad == IMAGE_PAD) {
128 + if (fse->index >= ARRAY_SIZE(supported_modes))
131 + if (fse->code != imx219_get_format_code(imx219, fse->code))
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;
139 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
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;
154 @@ -762,9 +798,9 @@ static void imx219_reset_colorspace(stru
155 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
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)
165 fmt->format.width = mode->width;
166 fmt->format.height = mode->height;
167 @@ -772,21 +808,39 @@ static void imx219_update_pad_format(str
168 imx219_reset_colorspace(&fmt->format);
171 +static void imx219_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
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;
179 static int __imx219_get_pad_format(struct imx219 *imx219,
180 struct v4l2_subdev_state *sd_state,
181 struct v4l2_subdev_format *fmt)
183 + if (fmt->pad >= NUM_PADS)
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,
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;
197 - imx219_update_pad_format(imx219, imx219->mode, fmt);
198 - fmt->format.code = imx219_get_format_code(imx219,
200 + if (fmt->pad == IMAGE_PAD) {
201 + imx219_update_image_pad_format(imx219, imx219->mode,
203 + fmt->format.code = imx219_get_format_code(imx219,
206 + imx219_update_metadata_pad_format(fmt);
211 @@ -816,51 +870,74 @@ static int imx219_set_pad_format(struct
212 int exposure_max, exposure_def, hblank;
215 - mutex_lock(&imx219->mutex);
217 - for (i = 0; i < ARRAY_SIZE(codes); i++)
218 - if (codes[i] == fmt->format.code)
220 - if (i >= ARRAY_SIZE(codes))
222 + if (fmt->pad >= NUM_PADS)
225 - /* Bayer order varies with flips */
226 - fmt->format.code = imx219_get_format_code(imx219, codes[i]);
227 + mutex_lock(&imx219->mutex);
229 - mode = v4l2_find_nearest_size(supported_modes,
230 - ARRAY_SIZE(supported_modes),
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 - imx219->fmt = fmt->format;
240 - imx219->mode = mode;
241 - /* Update limits and set FPS to default */
242 - __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
243 - IMX219_VTS_MAX - mode->height, 1,
244 - mode->vts_def - mode->height);
245 - __v4l2_ctrl_s_ctrl(imx219->vblank,
246 - mode->vts_def - mode->height);
247 - /* Update max exposure while meeting expected vblanking */
248 - exposure_max = mode->vts_def - 4;
249 - exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
250 - exposure_max : IMX219_EXPOSURE_DEFAULT;
251 - __v4l2_ctrl_modify_range(imx219->exposure,
252 - imx219->exposure->minimum,
253 - exposure_max, imx219->exposure->step,
256 - * Currently PPL is fixed to IMX219_PPL_DEFAULT, so hblank
257 - * depends on mode->width only, and is not changeble in any
258 - * way other than changing the mode.
260 - hblank = IMX219_PPL_DEFAULT - mode->width;
261 - __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
263 + if (fmt->pad == IMAGE_PAD) {
264 + for (i = 0; i < ARRAY_SIZE(codes); i++)
265 + if (codes[i] == fmt->format.code)
267 + if (i >= ARRAY_SIZE(codes))
270 + /* Bayer order varies with flips */
271 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
273 + mode = v4l2_find_nearest_size(supported_modes,
274 + ARRAY_SIZE(supported_modes),
277 + fmt->format.height);
278 + imx219_update_image_pad_format(imx219, mode, fmt);
279 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
280 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
282 + *framefmt = fmt->format;
283 + } else if (imx219->mode != mode ||
284 + imx219->fmt.code != fmt->format.code) {
285 + imx219->fmt = fmt->format;
286 + imx219->mode = mode;
287 + /* Update limits and set FPS to default */
288 + __v4l2_ctrl_modify_range(imx219->vblank,
290 + IMX219_VTS_MAX - mode->height,
292 + mode->vts_def - mode->height);
293 + __v4l2_ctrl_s_ctrl(imx219->vblank,
294 + mode->vts_def - mode->height);
296 + * Update max exposure while meeting
297 + * expected vblanking
299 + exposure_max = mode->vts_def - 4;
301 + (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
302 + exposure_max : IMX219_EXPOSURE_DEFAULT;
303 + __v4l2_ctrl_modify_range(imx219->exposure,
304 + imx219->exposure->minimum,
306 + imx219->exposure->step,
309 + * Currently PPL is fixed to IMX219_PPL_DEFAULT, so
310 + * hblank depends on mode->width only, and is not
311 + * changeble in any way other than changing the mode.
313 + hblank = IMX219_PPL_DEFAULT - mode->width;
314 + __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank,
318 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
319 + framefmt = v4l2_subdev_get_try_format(sd, sd_state,
321 + *framefmt = fmt->format;
323 + /* Only one embedded data mode is supported */
324 + imx219_update_metadata_pad_format(fmt);
328 mutex_unlock(&imx219->mutex);
329 @@ -976,9 +1053,11 @@ static int imx219_start_streaming(struct
330 const struct imx219_reg_list *reg_list;
333 - ret = pm_runtime_resume_and_get(&client->dev);
335 + ret = pm_runtime_get_sync(&client->dev);
337 + pm_runtime_put_noidle(&client->dev);
341 /* Send all registers that are common to all modes */
342 ret = imx219_write_regs(imx219, imx219_common_regs, ARRAY_SIZE(imx219_common_regs));
343 @@ -1086,21 +1165,22 @@ err_unlock:
344 /* Power/clock management functions */
345 static int imx219_power_on(struct device *dev)
347 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
348 + struct i2c_client *client = to_i2c_client(dev);
349 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
350 struct imx219 *imx219 = to_imx219(sd);
353 ret = regulator_bulk_enable(IMX219_NUM_SUPPLIES,
356 - dev_err(dev, "%s: failed to enable regulators\n",
357 + dev_err(&client->dev, "%s: failed to enable regulators\n",
362 ret = clk_prepare_enable(imx219->xclk);
364 - dev_err(dev, "%s: failed to enable clock\n",
365 + dev_err(&client->dev, "%s: failed to enable clock\n",
369 @@ -1119,7 +1199,8 @@ reg_off:
371 static int imx219_power_off(struct device *dev)
373 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
374 + struct i2c_client *client = to_i2c_client(dev);
375 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
376 struct imx219 *imx219 = to_imx219(sd);
378 gpiod_set_value_cansleep(imx219->reset_gpio, 0);
379 @@ -1131,7 +1212,8 @@ static int imx219_power_off(struct devic
381 static int __maybe_unused imx219_suspend(struct device *dev)
383 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
384 + struct i2c_client *client = to_i2c_client(dev);
385 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
386 struct imx219 *imx219 = to_imx219(sd);
388 if (imx219->streaming)
389 @@ -1142,7 +1224,8 @@ static int __maybe_unused imx219_suspend
391 static int __maybe_unused imx219_resume(struct device *dev)
393 - struct v4l2_subdev *sd = dev_get_drvdata(dev);
394 + struct i2c_client *client = to_i2c_client(dev);
395 + struct v4l2_subdev *sd = i2c_get_clientdata(client);
396 struct imx219 *imx219 = to_imx219(sd);
399 @@ -1478,13 +1561,14 @@ static int imx219_probe(struct i2c_clien
400 V4L2_SUBDEV_FL_HAS_EVENTS;
401 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
403 - /* Initialize source pad */
404 - imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
405 + /* Initialize source pads */
406 + imx219->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
407 + imx219->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
409 /* Initialize default format */
410 imx219_set_default_format(imx219);
412 - ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
413 + ret = media_entity_pads_init(&imx219->sd.entity, NUM_PADS, imx219->pad);
415 dev_err(dev, "failed to init entity pads: %d\n", ret);
416 goto error_handler_free;