c4c25d15126710be5fd8456568117fd2b8fa6c9b
[openwrt/staging/ldir.git] /
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
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 @@ -112,6 +112,16 @@
24 #define IMX219_TESTP_BLUE_DEFAULT 0
25 #define IMX219_TESTP_GREENB_DEFAULT 0
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 @@ -503,7 +513,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 @@ -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)
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
60 mutex_lock(&imx219->mutex);
61
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;
74 +
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;
80
81 mutex_unlock(&imx219->mutex);
82
83 @@ -764,10 +782,21 @@ static int imx219_enum_mbus_code(struct
84 {
85 struct imx219 *imx219 = to_imx219(sd);
86
87 - if (code->index >= (ARRAY_SIZE(codes) / 4))
88 + if (code->pad >= NUM_PADS)
89 return -EINVAL;
90
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))
94 + return -EINVAL;
95 +
96 + code->code = imx219_get_format_code(imx219,
97 + codes[code->index * 4]);
98 + } else {
99 + if (code->index > 0)
100 + return -EINVAL;
101 +
102 + code->code = MEDIA_BUS_FMT_SENSOR_DATA;
103 + }
104
105 return 0;
106 }
107 @@ -778,16 +807,29 @@ static int imx219_enum_frame_size(struct
108 {
109 struct imx219 *imx219 = to_imx219(sd);
110
111 - if (fse->index >= ARRAY_SIZE(supported_modes))
112 + if (fse->pad >= NUM_PADS)
113 return -EINVAL;
114
115 - if (fse->code != imx219_get_format_code(imx219, fse->code))
116 - return -EINVAL;
117 + if (fse->pad == IMAGE_PAD) {
118 + if (fse->index >= ARRAY_SIZE(supported_modes))
119 + return -EINVAL;
120 +
121 + if (fse->code != imx219_get_format_code(imx219, fse->code))
122 + return -EINVAL;
123 +
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;
128 + } else {
129 + if (fse->code != MEDIA_BUS_FMT_SENSOR_DATA || fse->index > 0)
130 + return -EINVAL;
131
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;
140 + }
141
142 return 0;
143 }
144 @@ -802,9 +844,9 @@ static void imx219_reset_colorspace(stru
145 fmt->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(fmt->colorspace);
146 }
147
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)
154 {
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);
159 }
160
161 +static void imx219_update_metadata_pad_format(struct v4l2_subdev_format *fmt)
162 +{
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;
167 +}
168 +
169 static int __imx219_get_pad_format(struct imx219 *imx219,
170 struct v4l2_subdev_pad_config *cfg,
171 struct v4l2_subdev_format *fmt)
172 {
173 + if (fmt->pad >= NUM_PADS)
174 + return -EINVAL;
175 +
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;
185 } else {
186 - imx219_update_pad_format(imx219, imx219->mode, fmt);
187 - fmt->format.code = imx219_get_format_code(imx219,
188 - imx219->fmt.code);
189 + if (fmt->pad == IMAGE_PAD) {
190 + imx219_update_image_pad_format(imx219, imx219->mode,
191 + fmt);
192 + fmt->format.code = imx219_get_format_code(imx219,
193 + imx219->fmt.code);
194 + } else {
195 + imx219_update_metadata_pad_format(fmt);
196 + }
197 }
198
199 return 0;
200 @@ -855,51 +915,74 @@ static int imx219_set_pad_format(struct
201 int exposure_max, exposure_def, hblank;
202 unsigned int i;
203
204 - mutex_lock(&imx219->mutex);
205 -
206 - for (i = 0; i < ARRAY_SIZE(codes); i++)
207 - if (codes[i] == fmt->format.code)
208 - break;
209 - if (i >= ARRAY_SIZE(codes))
210 - i = 0;
211 + if (fmt->pad >= NUM_PADS)
212 + return -EINVAL;
213
214 - /* Bayer order varies with flips */
215 - fmt->format.code = imx219_get_format_code(imx219, codes[i]);
216 + mutex_lock(&imx219->mutex);
217
218 - mode = v4l2_find_nearest_size(supported_modes,
219 - ARRAY_SIZE(supported_modes),
220 - width, height,
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,
243 - exposure_def);
244 - /*
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.
248 - */
249 - hblank = IMX219_PPL_DEFAULT - mode->width;
250 - __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank, 1,
251 - hblank);
252 + if (fmt->pad == IMAGE_PAD) {
253 + for (i = 0; i < ARRAY_SIZE(codes); i++)
254 + if (codes[i] == fmt->format.code)
255 + break;
256 + if (i >= ARRAY_SIZE(codes))
257 + i = 0;
258 +
259 + /* Bayer order varies with flips */
260 + fmt->format.code = imx219_get_format_code(imx219, codes[i]);
261 +
262 + mode = v4l2_find_nearest_size(supported_modes,
263 + ARRAY_SIZE(supported_modes),
264 + width, height,
265 + fmt->format.width,
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,
270 + fmt->pad);
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,
278 + IMX219_VBLANK_MIN,
279 + IMX219_VTS_MAX - mode->height,
280 + 1,
281 + mode->vts_def - mode->height);
282 + __v4l2_ctrl_s_ctrl(imx219->vblank,
283 + mode->vts_def - mode->height);
284 + /*
285 + * Update max exposure while meeting
286 + * expected vblanking
287 + */
288 + exposure_max = mode->vts_def - 4;
289 + exposure_def =
290 + (exposure_max < IMX219_EXPOSURE_DEFAULT) ?
291 + exposure_max : IMX219_EXPOSURE_DEFAULT;
292 + __v4l2_ctrl_modify_range(imx219->exposure,
293 + imx219->exposure->minimum,
294 + exposure_max,
295 + imx219->exposure->step,
296 + exposure_def);
297 + /*
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.
301 + */
302 + hblank = IMX219_PPL_DEFAULT - mode->width;
303 + __v4l2_ctrl_modify_range(imx219->hblank, hblank, hblank,
304 + 1, hblank);
305 + }
306 + } else {
307 + if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
308 + framefmt = v4l2_subdev_get_try_format(sd, cfg,
309 + fmt->pad);
310 + *framefmt = fmt->format;
311 + } else {
312 + /* Only one embedded data mode is supported */
313 + imx219_update_metadata_pad_format(fmt);
314 + }
315 }
316
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;
321
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;
327
328 /* Initialize default format */
329 imx219_set_default_format(imx219);
330
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);
333 if (ret) {
334 dev_err(dev, "failed to init entity pads: %d\n", ret);
335 goto error_handler_free;