1 From 372b138a6bc43b0fdff4e46ae4c799fd1b1f2785 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_TESTP_BLUE_DEFAULT 0
25 #define IMX219_TESTP_GREENB_DEFAULT 0
27 +/* Embedded metadata stream structure */
28 +#define IMX219_EMBEDDED_LINE_WIDTH 16384
29 +#define IMX219_NUM_EMBEDDED_LINES 1
40 @@ -503,7 +513,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 @@ -652,17 +662,25 @@ 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);
60 mutex_lock(&imx219->mutex);
62 - /* Initialize try_fmt */
63 - try_fmt->width = supported_modes[0].width;
64 - try_fmt->height = supported_modes[0].height;
65 - try_fmt->code = imx219_get_format_code(imx219,
66 - MEDIA_BUS_FMT_SRGGB10_1X10);
67 - try_fmt->field = V4L2_FIELD_NONE;
68 + /* Initialize try_fmt for the image pad */
69 + try_fmt_img->width = supported_modes[0].width;
70 + try_fmt_img->height = supported_modes[0].height;
71 + try_fmt_img->code = imx219_get_format_code(imx219,
72 + MEDIA_BUS_FMT_SRGGB10_1X10);
73 + try_fmt_img->field = V4L2_FIELD_NONE;
75 + /* Initialize try_fmt for the embedded metadata pad */
76 + try_fmt_meta->width = IMX219_EMBEDDED_LINE_WIDTH;
77 + try_fmt_meta->height = IMX219_NUM_EMBEDDED_LINES;
78 + try_fmt_meta->code = MEDIA_BUS_FMT_SENSOR_DATA;
79 + try_fmt_meta->field = V4L2_FIELD_NONE;
81 mutex_unlock(&imx219->mutex);
83 @@ -764,10 +782,21 @@ static int imx219_enum_mbus_code(struct
85 struct imx219 *imx219 = to_imx219(sd);
87 - if (code->index >= (ARRAY_SIZE(codes) / 4))
88 + if (code->pad >= NUM_PADS)
91 - code->code = imx219_get_format_code(imx219, codes[code->index * 4]);
92 + if (code->pad == IMAGE_PAD) {
93 + if (code->index >= (ARRAY_SIZE(codes) / 4))
96 + code->code = imx219_get_format_code(imx219,
97 + codes[code->index * 4]);
99 + if (code->index > 0)
102 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
107 @@ -778,16 +807,29 @@ static int imx219_enum_frame_size(struct
109 struct imx219 *imx219 = to_imx219(sd);
111 - if (fse->index >= ARRAY_SIZE(supported_modes))
112 + if (fse->pad >= NUM_PADS)
115 - if (fse->code != imx219_get_format_code(imx219, fse->code))
117 + if (fse->pad == IMAGE_PAD) {
118 + if (fse->index >= ARRAY_SIZE(supported_modes))
121 + if (fse->code != imx219_get_format_code(imx219, fse->code))
124 + fse->min_width = supported_modes[fse->index].width;
125 + fse->max_width = fse->min_width;
126 + fse->min_height = supported_modes[fse->index].height;
127 + fse->max_height = fse->min_height;
129 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
132 - fse->min_width = supported_modes[fse->index].width;
133 - fse->max_width = fse->min_width;
134 - fse->min_height = supported_modes[fse->index].height;
135 - fse->max_height = fse->min_height;
136 + fse->min_width = IMX219_EMBEDDED_LINE_WIDTH;
137 + fse->max_width = fse->min_width;
138 + fse->min_height = IMX219_NUM_EMBEDDED_LINES;
139 + fse->max_height = fse->min_height;
144 @@ -802,9 +844,9 @@ static void imx219_reset_colorspace(stru
145 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
148 -static void imx219_update_pad_format(struct imx219 *imx219,
149 - const struct imx219_mode *mode,
150 - struct v4l2_subdev_format *fmt)
151 +static void imx219_update_image_pad_format(struct imx219 *imx219,
152 + const struct imx219_mode *mode,
153 + struct v4l2_subdev_format *fmt)
155 fmt->format.width = mode->width;
156 fmt->format.height = mode->height;
157 @@ -812,20 +854,38 @@ static void imx219_update_pad_format(str
158 imx219_reset_colorspace(&fmt->format);
161 +static void imx219_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
163 + fmt->format.width = IMX219_EMBEDDED_LINE_WIDTH;
164 + fmt->format.height = IMX219_NUM_EMBEDDED_LINES;
165 + fmt->format.code = MEDIA_BUS_FMT_SENSOR_DATA;
166 + fmt->format.field = V4L2_FIELD_NONE;
169 static int __imx219_get_pad_format(struct imx219 *imx219,
170 struct v4l2_subdev_pad_config *cfg,
171 struct v4l2_subdev_format *fmt)
173 + if (fmt->pad >= NUM_PADS)
176 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
177 struct v4l2_mbus_framefmt *try_fmt =
178 v4l2_subdev_get_try_format(&imx219->sd, cfg, fmt->pad);
179 /* update the code which could change due to vflip or hflip: */
180 - try_fmt->code = imx219_get_format_code(imx219, try_fmt->code);
181 + try_fmt->code = fmt->pad == IMAGE_PAD ?
182 + imx219_get_format_code(imx219, try_fmt->code) :
183 + MEDIA_BUS_FMT_SENSOR_DATA;
184 fmt->format = *try_fmt;
186 - imx219_update_pad_format(imx219, imx219->mode, fmt);
187 - fmt->format.code = imx219_get_format_code(imx219,
189 + if (fmt->pad == IMAGE_PAD) {
190 + imx219_update_image_pad_format(imx219, imx219->mode,
192 + fmt->format.code = imx219_get_format_code(imx219,
195 + imx219_update_metadata_pad_format(fmt);
200 @@ -855,51 +915,74 @@ static int imx219_set_pad_format(struct
201 int exposure_max, exposure_def, hblank;
204 - mutex_lock(&imx219->mutex);
206 - for (i = 0; i < ARRAY_SIZE(codes); i++)
207 - if (codes[i] == fmt->format.code)
209 - if (i >= ARRAY_SIZE(codes))
211 + if (fmt->pad >= NUM_PADS)
214 - /* Bayer order varies with flips */
215 - fmt->format.code = imx219_get_format_code(imx219, codes[i]);
216 + mutex_lock(&imx219->mutex);
218 - mode = v4l2_find_nearest_size(supported_modes,
219 - ARRAY_SIZE(supported_modes),
221 - fmt->format.width, fmt->format.height);
222 - imx219_update_pad_format(imx219, mode, fmt);
223 - if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
224 - framefmt = v4l2_subdev_get_try_format(sd, cfg, fmt->pad);
225 - *framefmt = fmt->format;
226 - } else if (imx219->mode != mode ||
227 - imx219->fmt.code != fmt->format.code) {
228 - imx219->fmt = fmt->format;
229 - imx219->mode = mode;
230 - /* Update limits and set FPS to default */
231 - __v4l2_ctrl_modify_range(imx219->vblank, IMX219_VBLANK_MIN,
232 - IMX219_VTS_MAX - mode->height, 1,
233 - mode->vts_def - mode->height);
234 - __v4l2_ctrl_s_ctrl(imx219->vblank,
235 - mode->vts_def - mode->height);
236 - /* Update max exposure while meeting expected vblanking */
237 - exposure_max = mode->vts_def - 4;
238 - exposure_def = (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
239 - exposure_max : IMX219_EXPOSURE_DEFAULT;
240 - __v4l2_ctrl_modify_range(imx219->exposure,
241 - imx219->exposure->minimum,
242 - exposure_max, imx219->exposure->step,
245 - * Currently PPL is fixed to IMX219_PPL_DEFAULT, so hblank
246 - * depends on mode->width only, and is not changeble in any
247 - * way other than changing the mode.
249 - hblank = IMX219_PPL_DEFAULT - mode->width;
250 - __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
252 + if (fmt->pad == IMAGE_PAD) {
253 + for (i = 0; i < ARRAY_SIZE(codes); i++)
254 + if (codes[i] == fmt->format.code)
256 + if (i >= ARRAY_SIZE(codes))
259 + /* Bayer order varies with flips */
260 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
262 + mode = v4l2_find_nearest_size(supported_modes,
263 + ARRAY_SIZE(supported_modes),
266 + fmt->format.height);
267 + imx219_update_image_pad_format(imx219, mode, fmt);
268 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
269 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
271 + *framefmt = fmt->format;
272 + } else if (imx219->mode != mode ||
273 + imx219->fmt.code != fmt->format.code) {
274 + imx219->fmt = fmt->format;
275 + imx219->mode = mode;
276 + /* Update limits and set FPS to default */
277 + __v4l2_ctrl_modify_range(imx219->vblank,
279 + IMX219_VTS_MAX - mode->height,
281 + mode->vts_def - mode->height);
282 + __v4l2_ctrl_s_ctrl(imx219->vblank,
283 + mode->vts_def - mode->height);
285 + * Update max exposure while meeting
286 + * expected vblanking
288 + exposure_max = mode->vts_def - 4;
290 + (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
291 + exposure_max : IMX219_EXPOSURE_DEFAULT;
292 + __v4l2_ctrl_modify_range(imx219->exposure,
293 + imx219->exposure->minimum,
295 + imx219->exposure->step,
298 + * Currently PPL is fixed to IMX219_PPL_DEFAULT, so
299 + * hblank depends on mode->width only, and is not
300 + * changeble in any way other than changing the mode.
302 + hblank = IMX219_PPL_DEFAULT - mode->width;
303 + __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank,
307 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
308 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
310 + *framefmt = fmt->format;
312 + /* Only one embedded data mode is supported */
313 + imx219_update_metadata_pad_format(fmt);
317 mutex_unlock(&imx219->mutex);
318 @@ -1399,13 +1482,14 @@ static int imx219_probe(struct i2c_clien
319 imx219->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
320 imx219->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;
322 - /* Initialize source pad */
323 - imx219->pad.flags = MEDIA_PAD_FL_SOURCE;
324 + /* Initialize source pads */
325 + imx219->pad[IMAGE_PAD].flags = MEDIA_PAD_FL_SOURCE;
326 + imx219->pad[METADATA_PAD].flags = MEDIA_PAD_FL_SOURCE;
328 /* Initialize default format */
329 imx219_set_default_format(imx219);
331 - ret = media_entity_pads_init(&imx219->sd.entity, 1, &imx219->pad);
332 + ret = media_entity_pads_init(&imx219->sd.entity, NUM_PADS, imx219->pad);
334 dev_err(dev, "failed to init entity pads: %d\n", ret);
335 goto error_handler_free;