1 From fde54dfff6652b153af9fab8114b27510b0fe8b4 Mon Sep 17 00:00:00 2001
2 From: Naushir Patuck <naush@raspberrypi.com>
3 Date: Thu, 12 Mar 2020 14:09:38 +0000
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>
18 drivers/media/i2c/imx219.c | 226 +++++++++++++++++++++++++------------
19 1 file changed, 155 insertions(+), 71 deletions(-)
21 --- a/drivers/media/i2c/imx219.c
22 +++ b/drivers/media/i2c/imx219.c
24 #define IMX219_PIXEL_ARRAY_WIDTH 3280U
25 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
27 +/* Embedded metadata stream structure */
28 +#define IMX219_EMBEDDED_LINE_WIDTH 16384
29 +#define IMX219_NUM_EMBEDDED_LINES 1
40 @@ -536,7 +546,7 @@ static const struct imx219_mode supporte
43 struct v4l2_subdev sd;
44 - struct media_pad pad;
45 + struct media_pad pad[NUM_PADS];
47 struct v4l2_mbus_framefmt fmt;
49 @@ -685,18 +695,26 @@ static void imx219_set_default_format(st
50 static int imx219_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
52 struct imx219 *imx219 = to_imx219(sd);
53 - struct v4l2_mbus_framefmt *try_fmt =
54 - v4l2_subdev_get_try_format(sd, fh->pad, 0);
55 + struct v4l2_mbus_framefmt *try_fmt_img =
56 + v4l2_subdev_get_try_format(sd, fh->pad, IMAGE_PAD);
57 + struct v4l2_mbus_framefmt *try_fmt_meta =
58 + v4l2_subdev_get_try_format(sd, fh->pad, METADATA_PAD);
59 struct v4l2_rect *try_crop;
61 mutex_lock(&imx219->mutex);
63 - /* Initialize try_fmt */
64 - try_fmt->width = supported_modes[0].width;
65 - try_fmt->height = supported_modes[0].height;
66 - try_fmt->code = imx219_get_format_code(imx219,
67 - MEDIA_BUS_FMT_SRGGB10_1X10);
68 - try_fmt->field = V4L2_FIELD_NONE;
69 + /* Initialize try_fmt for the image pad */
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;
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;
82 /* Initialize try_crop rectangle. */
83 try_crop = v4l2_subdev_get_try_crop(sd, fh->pad, 0);
84 @@ -805,10 +823,21 @@ static int imx219_enum_mbus_code(struct
86 struct imx219 *imx219 = to_imx219(sd);
88 - if (code->index >= (ARRAY_SIZE(codes) / 4))
89 + if (code->pad >= NUM_PADS)
92 - code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
93 + if (code->pad == IMAGE_PAD) {
94 + if (code->index >= (ARRAY_SIZE(codes) / 4))
97 + code->code = imx219_get_format_code(imx219,
98 + codes[code->index * 4]);
100 + if (code->index > 0)
103 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
108 @@ -819,16 +848,29 @@ static int imx219_enum_frame_size(struct
110 struct imx219 *imx219 = to_imx219(sd);
112 - if (fse->index >= ARRAY_SIZE(supported_modes))
113 + if (fse->pad >= NUM_PADS)
116 - if (fse->code != imx219_get_format_code(imx219, fse->code))
118 + if (fse->pad == IMAGE_PAD) {
119 + if (fse->index >= ARRAY_SIZE(supported_modes))
122 + if (fse->code != imx219_get_format_code(imx219, fse->code))
125 + fse->min_width = supported_modes[fse->index].width;
126 + fse->max_width = fse->min_width;
127 + fse->min_height = supported_modes[fse->index].height;
128 + fse->max_height = fse->min_height;
130 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
133 - fse->min_width = supported_modes[fse->index].width;
134 - fse->max_width = fse->min_width;
135 - fse->min_height = supported_modes[fse->index].height;
136 - fse->max_height = fse->min_height;
137 + fse->min_width = IMX219_EMBEDDED_LINE_WIDTH;
138 + fse->max_width = fse->min_width;
139 + fse->min_height = IMX219_NUM_EMBEDDED_LINES;
140 + fse->max_height = fse->min_height;
145 @@ -843,9 +885,9 @@ static void imx219_reset_colorspace(stru
146 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
149 -static void imx219_update_pad_format(struct imx219 *imx219,
150 - const struct imx219_mode *mode,
151 - struct v4l2_subdev_format *fmt)
152 +static void imx219_update_image_pad_format(struct imx219 *imx219,
153 + const struct imx219_mode *mode,
154 + struct v4l2_subdev_format *fmt)
156 fmt->format.width = mode->width;
157 fmt->format.height = mode->height;
158 @@ -853,20 +895,38 @@ static void imx219_update_pad_format(str
159 imx219_reset_colorspace(&fmt->format);
162 +static void imx219_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
164 + fmt->format.width = IMX219_EMBEDDED_LINE_WIDTH;
165 + fmt->format.height = IMX219_NUM_EMBEDDED_LINES;
166 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
167 + fmt->format.field = V4L2_FIELD_NONE;
170 static int __imx219_get_pad_format(struct imx219 *imx219,
171 struct v4l2_subdev_pad_config *cfg,
172 struct v4l2_subdev_format *fmt)
174 + if (fmt->pad >= NUM_PADS)
177 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
178 struct v4l2_mbus_framefmt *try_fmt =
179 v4l2_subdev_get_try_format(&imx219->sd, cfg, fmt->pad);
180 /* update the code which could change due to vflip or hflip: */
181 - try_fmt->code = imx219_get_format_code(imx219, try_fmt->code);
182 + try_fmt->code = fmt->pad == IMAGE_PAD ?
183 + imx219_get_format_code(imx219, try_fmt->code) :
184 + MEDIA_BUS_FMT_SENSOR_DATA;
185 fmt->format = *try_fmt;
187 - imx219_update_pad_format(imx219, imx219->mode, fmt);
188 - fmt->format.code = imx219_get_format_code(imx219,
190 + if (fmt->pad == IMAGE_PAD) {
191 + imx219_update_image_pad_format(imx219, imx219->mode,
193 + fmt->format.code = imx219_get_format_code(imx219,
196 + imx219_update_metadata_pad_format(fmt);
201 @@ -896,51 +956,74 @@ static int imx219_set_pad_format(struct
202 int exposure_max, exposure_def, hblank;
205 - mutex_lock(&imx219->mutex);
207 - for (i = 0; i < ARRAY_SIZE(codes); i++)
208 - if (codes[i] == fmt->format.code)
210 - if (i >= ARRAY_SIZE(codes))
212 + if (fmt->pad >= NUM_PADS)
215 - /* Bayer order varies with flips */
216 - fmt->format.code = imx219_get_format_code(imx219, codes[i]);
217 + mutex_lock(&imx219->mutex);
219 - mode = v4l2_find_nearest_size(supported_modes,
220 - ARRAY_SIZE(supported_modes),
222 - fmt->format.width, fmt->format.height);
223 - imx219_update_pad_format(imx219, mode, fmt);
224 - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
225 - framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
226 - *framefmt = fmt->format;
227 - } else if (imx219->mode != mode ||
228 - imx219->fmt.code != fmt->format.code) {
229 - imx219->fmt = fmt->format;
230 - imx219->mode = mode;
231 - /* Update limits and set FPS to default */
232 - __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
233 - IMX219_VTS_MAX - mode->height, 1,
234 - mode->vts_def - mode->height);
235 - __v4l2_ctrl_s_ctrl(imx219->vblank,
236 - mode->vts_def - mode->height);
237 - /* Update max exposure while meeting expected vblanking */
238 - exposure_max = mode->vts_def - 4;
239 - exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
240 - exposure_max : IMX219_EXPOSURE_DEFAULT;
241 - __v4l2_ctrl_modify_range(imx219->exposure,
242 - imx219->exposure->minimum,
243 - exposure_max, imx219->exposure->step,
246 - * Currently PPL is fixed to IMX219_PPL_DEFAULT, so hblank
247 - * depends on mode->width only, and is not changeble in any
248 - * way other than changing the mode.
250 - hblank = IMX219_PPL_DEFAULT - mode->width;
251 - __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
253 + if (fmt->pad == IMAGE_PAD) {
254 + for (i = 0; i < ARRAY_SIZE(codes); i++)
255 + if (codes[i] == fmt->format.code)
257 + if (i >= ARRAY_SIZE(codes))
260 + /* Bayer order varies with flips */
261 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
263 + mode = v4l2_find_nearest_size(supported_modes,
264 + ARRAY_SIZE(supported_modes),
267 + fmt->format.height);
268 + imx219_update_image_pad_format(imx219, mode, fmt);
269 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
270 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
272 + *framefmt = fmt->format;
273 + } else if (imx219->mode != mode ||
274 + imx219->fmt.code != fmt->format.code) {
275 + imx219->fmt = fmt->format;
276 + imx219->mode = mode;
277 + /* Update limits and set FPS to default */
278 + __v4l2_ctrl_modify_range(imx219->vblank,
280 + IMX219_VTS_MAX - mode->height,
282 + mode->vts_def - mode->height);
283 + __v4l2_ctrl_s_ctrl(imx219->vblank,
284 + mode->vts_def - mode->height);
286 + * Update max exposure while meeting
287 + * expected vblanking
289 + exposure_max = mode->vts_def - 4;
291 + (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
292 + exposure_max : IMX219_EXPOSURE_DEFAULT;
293 + __v4l2_ctrl_modify_range(imx219->exposure,
294 + imx219->exposure->minimum,
296 + imx219->exposure->step,
299 + * Currently PPL is fixed to IMX219_PPL_DEFAULT, so
300 + * hblank depends on mode->width only, and is not
301 + * changeble in any way other than changing the mode.
303 + hblank = IMX219_PPL_DEFAULT - mode->width;
304 + __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank,
308 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
309 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
311 + *framefmt = fmt->format;
313 + /* Only one embedded data mode is supported */
314 + imx219_update_metadata_pad_format(fmt);
318 mutex_unlock(&imx219->mutex);
319 @@ -1511,13 +1594,14 @@ static int imx219_probe(struct i2c_clien
320 imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
321 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
323 - /* Initialize source pad */
324 - imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
325 + /* Initialize source pads */
326 + imx219->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
327 + imx219->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
329 /* Initialize default format */
330 imx219_set_default_format(imx219);
332 - ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
333 + ret = media_entity_pads_init(&imx219->sd.entity, NUM_PADS, imx219->pad);
335 dev_err(dev, "failed to init entity pads: %d\n", ret);
336 goto error_handler_free;