d8055af5b281bb7732f005920fff290fd2134168
[openwrt/staging/robimarko.git] /
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
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 ---
18 drivers/media/i2c/imx219.c | 226 +++++++++++++++++++++++++------------
19 1 file changed, 155 insertions(+), 71 deletions(-)
20
21 --- a/drivers/media/i2c/imx219.c
22 +++ b/drivers/media/i2c/imx219.c
23 @@ -118,6 +118,16 @@
24 #define IMX219_PIXEL_ARRAY_WIDTH 3280U
25 #define IMX219_PIXEL_ARRAY_HEIGHT 2464U
26
27 +/* Embedded metadata stream structure */
28 +#define IMX219_EMBEDDED_LINE_WIDTH 16384
29 +#define IMX219_NUM_EMBEDDED_LINES 1
30 +
31 +enum pad_types {
32 + IMAGE_PAD,
33 + METADATA_PAD,
34 + NUM_PADS
35 +};
36 +
37 struct imx219_reg {
38 u16 address;
39 u8 val;
40 @@ -536,7 +546,7 @@ static const struct imx219_mode supporte
41
42 struct imx219 {
43 struct v4l2_subdev sd;
44 - struct media_pad pad;
45 + struct media_pad pad[NUM_PADS];
46
47 struct v4l2_mbus_framefmt fmt;
48
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)
51 {
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;
60
61 mutex_lock(&imx219->mutex);
62
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;
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->pad, 0);
84 @@ -805,10 +823,21 @@ 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 - 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))
95 + return -EINVAL;
96 +
97 + code->code = imx219_get_format_code(imx219,
98 + codes[code->index * 4]);
99 + } else {
100 + if (code->index > 0)
101 + return -EINVAL;
102 +
103 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
104 + }
105
106 return 0;
107 }
108 @@ -819,16 +848,29 @@ static int imx219_enum_frame_size(struct
109 {
110 struct imx219 *imx219 = to_imx219(sd);
111
112 - if (fse->index >= ARRAY_SIZE(supported_modes))
113 + if (fse->pad >= NUM_PADS)
114 return -EINVAL;
115
116 - if (fse->code != imx219_get_format_code(imx219, fse->code))
117 - return -EINVAL;
118 + if (fse->pad == IMAGE_PAD) {
119 + if (fse->index >= ARRAY_SIZE(supported_modes))
120 + return -EINVAL;
121 +
122 + if (fse->code != imx219_get_format_code(imx219, fse->code))
123 + return -EINVAL;
124 +
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;
129 + } else {
130 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
131 + return -EINVAL;
132
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;
141 + }
142
143 return 0;
144 }
145 @@ -843,9 +885,9 @@ static void imx219_reset_colorspace(stru
146 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
147 }
148
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)
155 {
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);
160 }
161
162 +static void imx219_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
163 +{
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;
168 +}
169 +
170 static int __imx219_get_pad_format(struct imx219 *imx219,
171 struct v4l2_subdev_pad_config *cfg,
172 struct v4l2_subdev_format *fmt)
173 {
174 + if (fmt->pad >= NUM_PADS)
175 + return -EINVAL;
176 +
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;
186 } else {
187 - imx219_update_pad_format(imx219, imx219->mode, fmt);
188 - fmt->format.code = imx219_get_format_code(imx219,
189 - imx219->fmt.code);
190 + if (fmt->pad == IMAGE_PAD) {
191 + imx219_update_image_pad_format(imx219, imx219->mode,
192 + fmt);
193 + fmt->format.code = imx219_get_format_code(imx219,
194 + imx219->fmt.code);
195 + } else {
196 + imx219_update_metadata_pad_format(fmt);
197 + }
198 }
199
200 return 0;
201 @@ -896,51 +956,74 @@ static int imx219_set_pad_format(struct
202 int exposure_max, exposure_def, hblank;
203 unsigned int i;
204
205 - mutex_lock(&imx219->mutex);
206 -
207 - for (i = 0; i < ARRAY_SIZE(codes); i++)
208 - if (codes[i] == fmt->format.code)
209 - break;
210 - if (i >= ARRAY_SIZE(codes))
211 - i = 0;
212 + if (fmt->pad >= NUM_PADS)
213 + return -EINVAL;
214
215 - /* Bayer order varies with flips */
216 - fmt->format.code = imx219_get_format_code(imx219, codes[i]);
217 + mutex_lock(&imx219->mutex);
218
219 - mode = v4l2_find_nearest_size(supported_modes,
220 - ARRAY_SIZE(supported_modes),
221 - width, height,
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,
244 - exposure_def);
245 - /*
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.
249 - */
250 - hblank = IMX219_PPL_DEFAULT - mode->width;
251 - __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
252 - hblank);
253 + if (fmt->pad == IMAGE_PAD) {
254 + for (i = 0; i < ARRAY_SIZE(codes); i++)
255 + if (codes[i] == fmt->format.code)
256 + break;
257 + if (i >= ARRAY_SIZE(codes))
258 + i = 0;
259 +
260 + /* Bayer order varies with flips */
261 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
262 +
263 + mode = v4l2_find_nearest_size(supported_modes,
264 + ARRAY_SIZE(supported_modes),
265 + width, height,
266 + fmt->format.width,
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,
271 + fmt->pad);
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,
279 + IMX219_VBLANK_MIN,
280 + IMX219_VTS_MAX - mode->height,
281 + 1,
282 + mode->vts_def - mode->height);
283 + __v4l2_ctrl_s_ctrl(imx219->vblank,
284 + mode->vts_def - mode->height);
285 + /*
286 + * Update max exposure while meeting
287 + * expected vblanking
288 + */
289 + exposure_max = mode->vts_def - 4;
290 + exposure_def =
291 + (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
292 + exposure_max : IMX219_EXPOSURE_DEFAULT;
293 + __v4l2_ctrl_modify_range(imx219->exposure,
294 + imx219->exposure->minimum,
295 + exposure_max,
296 + imx219->exposure->step,
297 + exposure_def);
298 + /*
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.
302 + */
303 + hblank = IMX219_PPL_DEFAULT - mode->width;
304 + __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank,
305 + 1, hblank);
306 + }
307 + } else {
308 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
309 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
310 + fmt->pad);
311 + *framefmt = fmt->format;
312 + } else {
313 + /* Only one embedded data mode is supported */
314 + imx219_update_metadata_pad_format(fmt);
315 + }
316 }
317
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;
322
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;
328
329 /* Initialize default format */
330 imx219_set_default_format(imx219);
331
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);
334 if (ret) {
335 dev_err(dev, "failed to init entity pads: %d\n", ret);
336 goto error_handler_free;