]>
Commit | Line | Data |
---|---|---|
26e0ca22 LP |
1 | /* |
2 | * vsp1_video.c -- R-Car VSP1 Video Node | |
3 | * | |
139c9286 | 4 | * Copyright (C) 2013-2015 Renesas Electronics Corporation |
26e0ca22 LP |
5 | * |
6 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | */ | |
13 | ||
14 | #include <linux/list.h> | |
15 | #include <linux/module.h> | |
16 | #include <linux/mutex.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/slab.h> | |
19 | #include <linux/v4l2-mediabus.h> | |
20 | #include <linux/videodev2.h> | |
21 | ||
22 | #include <media/media-entity.h> | |
23 | #include <media/v4l2-dev.h> | |
24 | #include <media/v4l2-fh.h> | |
25 | #include <media/v4l2-ioctl.h> | |
26 | #include <media/v4l2-subdev.h> | |
c139990e | 27 | #include <media/videobuf2-v4l2.h> |
26e0ca22 LP |
28 | #include <media/videobuf2-dma-contig.h> |
29 | ||
30 | #include "vsp1.h" | |
629bb6d4 | 31 | #include "vsp1_bru.h" |
26e0ca22 LP |
32 | #include "vsp1_entity.h" |
33 | #include "vsp1_rwpf.h" | |
bdc2df62 | 34 | #include "vsp1_uds.h" |
26e0ca22 LP |
35 | #include "vsp1_video.h" |
36 | ||
37 | #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV | |
38 | #define VSP1_VIDEO_DEF_WIDTH 1024 | |
39 | #define VSP1_VIDEO_DEF_HEIGHT 768 | |
40 | ||
41 | #define VSP1_VIDEO_MIN_WIDTH 2U | |
42 | #define VSP1_VIDEO_MAX_WIDTH 8190U | |
43 | #define VSP1_VIDEO_MIN_HEIGHT 2U | |
44 | #define VSP1_VIDEO_MAX_HEIGHT 8190U | |
45 | ||
46 | /* ----------------------------------------------------------------------------- | |
47 | * Helper functions | |
48 | */ | |
49 | ||
50 | static const struct vsp1_format_info vsp1_video_formats[] = { | |
27ffaeb0 | 51 | { V4L2_PIX_FMT_RGB332, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 LP |
52 | VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
53 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 54 | 1, { 8, 0, 0 }, false, false, 1, 1, false }, |
27ffaeb0 | 55 | { V4L2_PIX_FMT_ARGB444, MEDIA_BUS_FMT_ARGB8888_1X32, |
7a52b6de LP |
56 | VI6_FMT_ARGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
57 | VI6_RPF_DSWAP_P_WDS, | |
58 | 1, { 16, 0, 0 }, false, false, 1, 1, true }, | |
27ffaeb0 | 59 | { V4L2_PIX_FMT_XRGB444, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 LP |
60 | VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
61 | VI6_RPF_DSWAP_P_WDS, | |
7a52b6de | 62 | 1, { 16, 0, 0 }, false, false, 1, 1, true }, |
27ffaeb0 | 63 | { V4L2_PIX_FMT_ARGB555, MEDIA_BUS_FMT_ARGB8888_1X32, |
7a52b6de LP |
64 | VI6_FMT_ARGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
65 | VI6_RPF_DSWAP_P_WDS, | |
66 | 1, { 16, 0, 0 }, false, false, 1, 1, true }, | |
27ffaeb0 | 67 | { V4L2_PIX_FMT_XRGB555, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 LP |
68 | VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
69 | VI6_RPF_DSWAP_P_WDS, | |
7a52b6de | 70 | 1, { 16, 0, 0 }, false, false, 1, 1, false }, |
27ffaeb0 | 71 | { V4L2_PIX_FMT_RGB565, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 LP |
72 | VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
73 | VI6_RPF_DSWAP_P_WDS, | |
7a52b6de | 74 | 1, { 16, 0, 0 }, false, false, 1, 1, false }, |
27ffaeb0 | 75 | { V4L2_PIX_FMT_BGR24, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 LP |
76 | VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
77 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 78 | 1, { 24, 0, 0 }, false, false, 1, 1, false }, |
27ffaeb0 | 79 | { V4L2_PIX_FMT_RGB24, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 LP |
80 | VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
81 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 82 | 1, { 24, 0, 0 }, false, false, 1, 1, false }, |
27ffaeb0 | 83 | { V4L2_PIX_FMT_ABGR32, MEDIA_BUS_FMT_ARGB8888_1X32, |
7a52b6de LP |
84 | VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS, |
85 | 1, { 32, 0, 0 }, false, false, 1, 1, true }, | |
27ffaeb0 | 86 | { V4L2_PIX_FMT_XBGR32, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 | 87 | VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS, |
7a52b6de | 88 | 1, { 32, 0, 0 }, false, false, 1, 1, false }, |
27ffaeb0 | 89 | { V4L2_PIX_FMT_ARGB32, MEDIA_BUS_FMT_ARGB8888_1X32, |
7a52b6de LP |
90 | VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
91 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
92 | 1, { 32, 0, 0 }, false, false, 1, 1, true }, | |
27ffaeb0 | 93 | { V4L2_PIX_FMT_XRGB32, MEDIA_BUS_FMT_ARGB8888_1X32, |
26e0ca22 LP |
94 | VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
95 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 96 | 1, { 32, 0, 0 }, false, false, 1, 1, false }, |
27ffaeb0 | 97 | { V4L2_PIX_FMT_UYVY, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
98 | VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
99 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 100 | 1, { 16, 0, 0 }, false, false, 2, 1, false }, |
27ffaeb0 | 101 | { V4L2_PIX_FMT_VYUY, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
102 | VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
103 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 104 | 1, { 16, 0, 0 }, false, true, 2, 1, false }, |
27ffaeb0 | 105 | { V4L2_PIX_FMT_YUYV, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
106 | VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
107 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 108 | 1, { 16, 0, 0 }, true, false, 2, 1, false }, |
27ffaeb0 | 109 | { V4L2_PIX_FMT_YVYU, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
110 | VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
111 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 112 | 1, { 16, 0, 0 }, true, true, 2, 1, false }, |
27ffaeb0 | 113 | { V4L2_PIX_FMT_NV12M, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
114 | VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
115 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 116 | 2, { 8, 16, 0 }, false, false, 2, 2, false }, |
27ffaeb0 | 117 | { V4L2_PIX_FMT_NV21M, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
118 | VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
119 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 120 | 2, { 8, 16, 0 }, false, true, 2, 2, false }, |
27ffaeb0 | 121 | { V4L2_PIX_FMT_NV16M, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
122 | VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
123 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 124 | 2, { 8, 16, 0 }, false, false, 2, 1, false }, |
27ffaeb0 | 125 | { V4L2_PIX_FMT_NV61M, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
126 | VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
127 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 128 | 2, { 8, 16, 0 }, false, true, 2, 1, false }, |
27ffaeb0 | 129 | { V4L2_PIX_FMT_YUV420M, MEDIA_BUS_FMT_AYUV8_1X32, |
26e0ca22 LP |
130 | VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS | |
131 | VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS, | |
7a52b6de | 132 | 3, { 8, 8, 8 }, false, false, 2, 2, false }, |
26e0ca22 LP |
133 | }; |
134 | ||
135 | /* | |
136 | * vsp1_get_format_info - Retrieve format information for a 4CC | |
137 | * @fourcc: the format 4CC | |
138 | * | |
139 | * Return a pointer to the format information structure corresponding to the | |
140 | * given V4L2 format 4CC, or NULL if no corresponding format can be found. | |
141 | */ | |
142 | static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc) | |
143 | { | |
144 | unsigned int i; | |
145 | ||
146 | for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) { | |
147 | const struct vsp1_format_info *info = &vsp1_video_formats[i]; | |
148 | ||
149 | if (info->fourcc == fourcc) | |
150 | return info; | |
151 | } | |
152 | ||
153 | return NULL; | |
154 | } | |
155 | ||
156 | ||
157 | static struct v4l2_subdev * | |
158 | vsp1_video_remote_subdev(struct media_pad *local, u32 *pad) | |
159 | { | |
160 | struct media_pad *remote; | |
161 | ||
162 | remote = media_entity_remote_pad(local); | |
163 | if (remote == NULL || | |
164 | media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
165 | return NULL; | |
166 | ||
167 | if (pad) | |
168 | *pad = remote->index; | |
169 | ||
170 | return media_entity_to_v4l2_subdev(remote->entity); | |
171 | } | |
172 | ||
173 | static int vsp1_video_verify_format(struct vsp1_video *video) | |
174 | { | |
175 | struct v4l2_subdev_format fmt; | |
176 | struct v4l2_subdev *subdev; | |
177 | int ret; | |
178 | ||
179 | subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad); | |
180 | if (subdev == NULL) | |
181 | return -EINVAL; | |
182 | ||
183 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
184 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); | |
185 | if (ret < 0) | |
186 | return ret == -ENOIOCTLCMD ? -EINVAL : ret; | |
187 | ||
188 | if (video->fmtinfo->mbus != fmt.format.code || | |
189 | video->format.height != fmt.format.height || | |
190 | video->format.width != fmt.format.width) | |
191 | return -EINVAL; | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static int __vsp1_video_try_format(struct vsp1_video *video, | |
197 | struct v4l2_pix_format_mplane *pix, | |
198 | const struct vsp1_format_info **fmtinfo) | |
199 | { | |
56bfef3e LP |
200 | static const u32 xrgb_formats[][2] = { |
201 | { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 }, | |
202 | { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 }, | |
203 | { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 }, | |
204 | { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 }, | |
205 | }; | |
206 | ||
26e0ca22 LP |
207 | const struct vsp1_format_info *info; |
208 | unsigned int width = pix->width; | |
209 | unsigned int height = pix->height; | |
210 | unsigned int i; | |
211 | ||
56bfef3e LP |
212 | /* Backward compatibility: replace deprecated RGB formats by their XRGB |
213 | * equivalent. This selects the format older userspace applications want | |
214 | * while still exposing the new format. | |
215 | */ | |
216 | for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) { | |
217 | if (xrgb_formats[i][0] == pix->pixelformat) { | |
218 | pix->pixelformat = xrgb_formats[i][1]; | |
219 | break; | |
220 | } | |
221 | } | |
222 | ||
26e0ca22 LP |
223 | /* Retrieve format information and select the default format if the |
224 | * requested format isn't supported. | |
225 | */ | |
226 | info = vsp1_get_format_info(pix->pixelformat); | |
227 | if (info == NULL) | |
228 | info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT); | |
229 | ||
230 | pix->pixelformat = info->fourcc; | |
231 | pix->colorspace = V4L2_COLORSPACE_SRGB; | |
232 | pix->field = V4L2_FIELD_NONE; | |
233 | memset(pix->reserved, 0, sizeof(pix->reserved)); | |
234 | ||
235 | /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */ | |
236 | width = round_down(width, info->hsub); | |
237 | height = round_down(height, info->vsub); | |
238 | ||
239 | /* Clamp the width and height. */ | |
240 | pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH); | |
241 | pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT, | |
242 | VSP1_VIDEO_MAX_HEIGHT); | |
243 | ||
244 | /* Compute and clamp the stride and image size. While not documented in | |
245 | * the datasheet, strides not aligned to a multiple of 128 bytes result | |
246 | * in image corruption. | |
247 | */ | |
df5c3e7c | 248 | for (i = 0; i < min(info->planes, 2U); ++i) { |
26e0ca22 LP |
249 | unsigned int hsub = i > 0 ? info->hsub : 1; |
250 | unsigned int vsub = i > 0 ? info->vsub : 1; | |
251 | unsigned int align = 128; | |
252 | unsigned int bpl; | |
253 | ||
254 | bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline, | |
255 | pix->width / hsub * info->bpp[i] / 8, | |
256 | round_down(65535U, align)); | |
257 | ||
258 | pix->plane_fmt[i].bytesperline = round_up(bpl, align); | |
259 | pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline | |
260 | * pix->height / vsub; | |
261 | } | |
262 | ||
263 | if (info->planes == 3) { | |
264 | /* The second and third planes must have the same stride. */ | |
265 | pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline; | |
266 | pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage; | |
267 | } | |
268 | ||
269 | pix->num_planes = info->planes; | |
270 | ||
271 | if (fmtinfo) | |
272 | *fmtinfo = info; | |
273 | ||
274 | return 0; | |
275 | } | |
276 | ||
277 | static bool | |
278 | vsp1_video_format_adjust(struct vsp1_video *video, | |
279 | const struct v4l2_pix_format_mplane *format, | |
280 | struct v4l2_pix_format_mplane *adjust) | |
281 | { | |
282 | unsigned int i; | |
283 | ||
284 | *adjust = *format; | |
285 | __vsp1_video_try_format(video, adjust, NULL); | |
286 | ||
287 | if (format->width != adjust->width || | |
288 | format->height != adjust->height || | |
289 | format->pixelformat != adjust->pixelformat || | |
290 | format->num_planes != adjust->num_planes) | |
291 | return false; | |
292 | ||
293 | for (i = 0; i < format->num_planes; ++i) { | |
294 | if (format->plane_fmt[i].bytesperline != | |
295 | adjust->plane_fmt[i].bytesperline) | |
296 | return false; | |
297 | ||
298 | adjust->plane_fmt[i].sizeimage = | |
299 | max(adjust->plane_fmt[i].sizeimage, | |
300 | format->plane_fmt[i].sizeimage); | |
301 | } | |
302 | ||
303 | return true; | |
304 | } | |
305 | ||
306 | /* ----------------------------------------------------------------------------- | |
307 | * Pipeline Management | |
308 | */ | |
309 | ||
bdc2df62 LP |
310 | static int vsp1_pipeline_validate_branch(struct vsp1_pipeline *pipe, |
311 | struct vsp1_rwpf *input, | |
26e0ca22 LP |
312 | struct vsp1_rwpf *output) |
313 | { | |
314 | struct vsp1_entity *entity; | |
315 | unsigned int entities = 0; | |
316 | struct media_pad *pad; | |
bdc2df62 | 317 | bool bru_found = false; |
26e0ca22 | 318 | |
629bb6d4 LP |
319 | input->location.left = 0; |
320 | input->location.top = 0; | |
321 | ||
26e0ca22 LP |
322 | pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]); |
323 | ||
324 | while (1) { | |
325 | if (pad == NULL) | |
326 | return -EPIPE; | |
327 | ||
328 | /* We've reached a video node, that shouldn't have happened. */ | |
329 | if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
330 | return -EPIPE; | |
331 | ||
332 | entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity)); | |
333 | ||
629bb6d4 LP |
334 | /* A BRU is present in the pipeline, store the compose rectangle |
335 | * location in the input RPF for use when configuring the RPF. | |
336 | */ | |
337 | if (entity->type == VSP1_ENTITY_BRU) { | |
338 | struct vsp1_bru *bru = to_bru(&entity->subdev); | |
6418b4d6 LP |
339 | struct v4l2_rect *rect = |
340 | &bru->inputs[pad->index].compose; | |
341 | ||
342 | bru->inputs[pad->index].rpf = input; | |
629bb6d4 LP |
343 | |
344 | input->location.left = rect->left; | |
345 | input->location.top = rect->top; | |
bdc2df62 LP |
346 | |
347 | bru_found = true; | |
629bb6d4 LP |
348 | } |
349 | ||
26e0ca22 LP |
350 | /* We've reached the WPF, we're done. */ |
351 | if (entity->type == VSP1_ENTITY_WPF) | |
352 | break; | |
353 | ||
354 | /* Ensure the branch has no loop. */ | |
355 | if (entities & (1 << entity->subdev.entity.id)) | |
356 | return -EPIPE; | |
357 | ||
358 | entities |= 1 << entity->subdev.entity.id; | |
359 | ||
360 | /* UDS can't be chained. */ | |
361 | if (entity->type == VSP1_ENTITY_UDS) { | |
bdc2df62 | 362 | if (pipe->uds) |
26e0ca22 | 363 | return -EPIPE; |
bdc2df62 LP |
364 | |
365 | pipe->uds = entity; | |
366 | pipe->uds_input = bru_found ? pipe->bru | |
367 | : &input->entity; | |
26e0ca22 LP |
368 | } |
369 | ||
370 | /* Follow the source link. The link setup operations ensure | |
371 | * that the output fan-out can't be more than one, there is thus | |
372 | * no need to verify here that only a single source link is | |
373 | * activated. | |
374 | */ | |
375 | pad = &entity->pads[entity->source_pad]; | |
376 | pad = media_entity_remote_pad(pad); | |
377 | } | |
378 | ||
379 | /* The last entity must be the output WPF. */ | |
380 | if (entity != &output->entity) | |
381 | return -EPIPE; | |
382 | ||
383 | return 0; | |
384 | } | |
385 | ||
6418b4d6 LP |
386 | static void __vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe) |
387 | { | |
388 | if (pipe->bru) { | |
389 | struct vsp1_bru *bru = to_bru(&pipe->bru->subdev); | |
390 | unsigned int i; | |
391 | ||
392 | for (i = 0; i < ARRAY_SIZE(bru->inputs); ++i) | |
393 | bru->inputs[i].rpf = NULL; | |
394 | } | |
395 | ||
396 | INIT_LIST_HEAD(&pipe->entities); | |
397 | pipe->state = VSP1_PIPELINE_STOPPED; | |
398 | pipe->buffers_ready = 0; | |
399 | pipe->num_video = 0; | |
400 | pipe->num_inputs = 0; | |
401 | pipe->output = NULL; | |
402 | pipe->bru = NULL; | |
403 | pipe->lif = NULL; | |
bdc2df62 | 404 | pipe->uds = NULL; |
6418b4d6 LP |
405 | } |
406 | ||
26e0ca22 LP |
407 | static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe, |
408 | struct vsp1_video *video) | |
409 | { | |
410 | struct media_entity_graph graph; | |
411 | struct media_entity *entity = &video->video.entity; | |
412 | struct media_device *mdev = entity->parent; | |
413 | unsigned int i; | |
414 | int ret; | |
415 | ||
416 | mutex_lock(&mdev->graph_mutex); | |
417 | ||
418 | /* Walk the graph to locate the entities and video nodes. */ | |
419 | media_entity_graph_walk_start(&graph, entity); | |
420 | ||
421 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
422 | struct v4l2_subdev *subdev; | |
423 | struct vsp1_rwpf *rwpf; | |
424 | struct vsp1_entity *e; | |
425 | ||
426 | if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) { | |
427 | pipe->num_video++; | |
428 | continue; | |
429 | } | |
430 | ||
431 | subdev = media_entity_to_v4l2_subdev(entity); | |
432 | e = to_vsp1_entity(subdev); | |
433 | list_add_tail(&e->list_pipe, &pipe->entities); | |
434 | ||
435 | if (e->type == VSP1_ENTITY_RPF) { | |
436 | rwpf = to_rwpf(subdev); | |
437 | pipe->inputs[pipe->num_inputs++] = rwpf; | |
438 | rwpf->video.pipe_index = pipe->num_inputs; | |
439 | } else if (e->type == VSP1_ENTITY_WPF) { | |
440 | rwpf = to_rwpf(subdev); | |
441 | pipe->output = to_rwpf(subdev); | |
442 | rwpf->video.pipe_index = 0; | |
443 | } else if (e->type == VSP1_ENTITY_LIF) { | |
444 | pipe->lif = e; | |
629bb6d4 LP |
445 | } else if (e->type == VSP1_ENTITY_BRU) { |
446 | pipe->bru = e; | |
26e0ca22 LP |
447 | } |
448 | } | |
449 | ||
450 | mutex_unlock(&mdev->graph_mutex); | |
451 | ||
452 | /* We need one output and at least one input. */ | |
453 | if (pipe->num_inputs == 0 || !pipe->output) { | |
454 | ret = -EPIPE; | |
455 | goto error; | |
456 | } | |
457 | ||
458 | /* Follow links downstream for each input and make sure the graph | |
459 | * contains no loop and that all branches end at the output WPF. | |
460 | */ | |
461 | for (i = 0; i < pipe->num_inputs; ++i) { | |
bdc2df62 | 462 | ret = vsp1_pipeline_validate_branch(pipe, pipe->inputs[i], |
26e0ca22 LP |
463 | pipe->output); |
464 | if (ret < 0) | |
465 | goto error; | |
466 | } | |
467 | ||
468 | return 0; | |
469 | ||
470 | error: | |
6418b4d6 | 471 | __vsp1_pipeline_cleanup(pipe); |
26e0ca22 LP |
472 | return ret; |
473 | } | |
474 | ||
475 | static int vsp1_pipeline_init(struct vsp1_pipeline *pipe, | |
476 | struct vsp1_video *video) | |
477 | { | |
478 | int ret; | |
479 | ||
480 | mutex_lock(&pipe->lock); | |
481 | ||
482 | /* If we're the first user validate and initialize the pipeline. */ | |
483 | if (pipe->use_count == 0) { | |
484 | ret = vsp1_pipeline_validate(pipe, video); | |
485 | if (ret < 0) | |
486 | goto done; | |
487 | } | |
488 | ||
489 | pipe->use_count++; | |
490 | ret = 0; | |
491 | ||
492 | done: | |
493 | mutex_unlock(&pipe->lock); | |
494 | return ret; | |
495 | } | |
496 | ||
497 | static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe) | |
498 | { | |
499 | mutex_lock(&pipe->lock); | |
500 | ||
501 | /* If we're the last user clean up the pipeline. */ | |
6418b4d6 LP |
502 | if (--pipe->use_count == 0) |
503 | __vsp1_pipeline_cleanup(pipe); | |
26e0ca22 LP |
504 | |
505 | mutex_unlock(&pipe->lock); | |
506 | } | |
507 | ||
508 | static void vsp1_pipeline_run(struct vsp1_pipeline *pipe) | |
509 | { | |
510 | struct vsp1_device *vsp1 = pipe->output->entity.vsp1; | |
511 | ||
512 | vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD); | |
513 | pipe->state = VSP1_PIPELINE_RUNNING; | |
514 | pipe->buffers_ready = 0; | |
515 | } | |
516 | ||
41bdc3cf | 517 | static bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe) |
1c991fee LP |
518 | { |
519 | unsigned long flags; | |
520 | bool stopped; | |
521 | ||
522 | spin_lock_irqsave(&pipe->irqlock, flags); | |
523 | stopped = pipe->state == VSP1_PIPELINE_STOPPED, | |
524 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
525 | ||
526 | return stopped; | |
527 | } | |
528 | ||
26e0ca22 LP |
529 | static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe) |
530 | { | |
531 | struct vsp1_entity *entity; | |
532 | unsigned long flags; | |
533 | int ret; | |
534 | ||
535 | spin_lock_irqsave(&pipe->irqlock, flags); | |
9b3e6e2a LP |
536 | if (pipe->state == VSP1_PIPELINE_RUNNING) |
537 | pipe->state = VSP1_PIPELINE_STOPPING; | |
26e0ca22 LP |
538 | spin_unlock_irqrestore(&pipe->irqlock, flags); |
539 | ||
1c991fee | 540 | ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe), |
26e0ca22 LP |
541 | msecs_to_jiffies(500)); |
542 | ret = ret == 0 ? -ETIMEDOUT : 0; | |
543 | ||
544 | list_for_each_entry(entity, &pipe->entities, list_pipe) { | |
aaa48cbb | 545 | if (entity->route && entity->route->reg) |
d9b45ed3 | 546 | vsp1_write(entity->vsp1, entity->route->reg, |
26e0ca22 LP |
547 | VI6_DPR_NODE_UNUSED); |
548 | ||
549 | v4l2_subdev_call(&entity->subdev, video, s_stream, 0); | |
550 | } | |
551 | ||
552 | return ret; | |
553 | } | |
554 | ||
555 | static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe) | |
556 | { | |
557 | unsigned int mask; | |
558 | ||
559 | mask = ((1 << pipe->num_inputs) - 1) << 1; | |
560 | if (!pipe->lif) | |
561 | mask |= 1 << 0; | |
562 | ||
563 | return pipe->buffers_ready == mask; | |
564 | } | |
565 | ||
566 | /* | |
567 | * vsp1_video_complete_buffer - Complete the current buffer | |
568 | * @video: the video node | |
569 | * | |
570 | * This function completes the current buffer by filling its sequence number, | |
571 | * time stamp and payload size, and hands it back to the videobuf core. | |
572 | * | |
3299ba5c LP |
573 | * When operating in DU output mode (deep pipeline to the DU through the LIF), |
574 | * the VSP1 needs to constantly supply frames to the display. In that case, if | |
575 | * no other buffer is queued, reuse the one that has just been processed instead | |
576 | * of handing it back to the videobuf core. | |
577 | * | |
26e0ca22 LP |
578 | * Return the next queued buffer or NULL if the queue is empty. |
579 | */ | |
580 | static struct vsp1_video_buffer * | |
581 | vsp1_video_complete_buffer(struct vsp1_video *video) | |
582 | { | |
3299ba5c | 583 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); |
26e0ca22 LP |
584 | struct vsp1_video_buffer *next = NULL; |
585 | struct vsp1_video_buffer *done; | |
586 | unsigned long flags; | |
587 | unsigned int i; | |
588 | ||
589 | spin_lock_irqsave(&video->irqlock, flags); | |
590 | ||
591 | if (list_empty(&video->irqqueue)) { | |
592 | spin_unlock_irqrestore(&video->irqlock, flags); | |
593 | return NULL; | |
594 | } | |
595 | ||
596 | done = list_first_entry(&video->irqqueue, | |
597 | struct vsp1_video_buffer, queue); | |
3299ba5c LP |
598 | |
599 | /* In DU output mode reuse the buffer if the list is singular. */ | |
600 | if (pipe->lif && list_is_singular(&video->irqqueue)) { | |
601 | spin_unlock_irqrestore(&video->irqlock, flags); | |
602 | return done; | |
603 | } | |
604 | ||
26e0ca22 LP |
605 | list_del(&done->queue); |
606 | ||
607 | if (!list_empty(&video->irqqueue)) | |
608 | next = list_first_entry(&video->irqqueue, | |
609 | struct vsp1_video_buffer, queue); | |
610 | ||
611 | spin_unlock_irqrestore(&video->irqlock, flags); | |
612 | ||
613 | done->buf.v4l2_buf.sequence = video->sequence++; | |
614 | v4l2_get_timestamp(&done->buf.v4l2_buf.timestamp); | |
615 | for (i = 0; i < done->buf.num_planes; ++i) | |
616 | vb2_set_plane_payload(&done->buf, i, done->length[i]); | |
617 | vb2_buffer_done(&done->buf, VB2_BUF_STATE_DONE); | |
618 | ||
619 | return next; | |
620 | } | |
621 | ||
622 | static void vsp1_video_frame_end(struct vsp1_pipeline *pipe, | |
623 | struct vsp1_video *video) | |
624 | { | |
625 | struct vsp1_video_buffer *buf; | |
626 | unsigned long flags; | |
627 | ||
628 | buf = vsp1_video_complete_buffer(video); | |
629 | if (buf == NULL) | |
630 | return; | |
631 | ||
632 | spin_lock_irqsave(&pipe->irqlock, flags); | |
633 | ||
634 | video->ops->queue(video, buf); | |
635 | pipe->buffers_ready |= 1 << video->pipe_index; | |
636 | ||
637 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
638 | } | |
639 | ||
640 | void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe) | |
641 | { | |
9b3e6e2a | 642 | enum vsp1_pipeline_state state; |
26e0ca22 LP |
643 | unsigned long flags; |
644 | unsigned int i; | |
645 | ||
646 | if (pipe == NULL) | |
647 | return; | |
648 | ||
649 | /* Complete buffers on all video nodes. */ | |
650 | for (i = 0; i < pipe->num_inputs; ++i) | |
651 | vsp1_video_frame_end(pipe, &pipe->inputs[i]->video); | |
652 | ||
653 | if (!pipe->lif) | |
654 | vsp1_video_frame_end(pipe, &pipe->output->video); | |
655 | ||
656 | spin_lock_irqsave(&pipe->irqlock, flags); | |
657 | ||
9b3e6e2a LP |
658 | state = pipe->state; |
659 | pipe->state = VSP1_PIPELINE_STOPPED; | |
660 | ||
26e0ca22 LP |
661 | /* If a stop has been requested, mark the pipeline as stopped and |
662 | * return. | |
663 | */ | |
9b3e6e2a | 664 | if (state == VSP1_PIPELINE_STOPPING) { |
26e0ca22 LP |
665 | wake_up(&pipe->wq); |
666 | goto done; | |
667 | } | |
668 | ||
669 | /* Restart the pipeline if ready. */ | |
670 | if (vsp1_pipeline_ready(pipe)) | |
671 | vsp1_pipeline_run(pipe); | |
672 | ||
673 | done: | |
674 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
675 | } | |
676 | ||
bdc2df62 LP |
677 | /* |
678 | * Propagate the alpha value through the pipeline. | |
679 | * | |
680 | * As the UDS has restricted scaling capabilities when the alpha component needs | |
681 | * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha | |
682 | * value. The UDS then outputs a fixed alpha value which needs to be programmed | |
683 | * from the input RPF alpha. | |
684 | */ | |
685 | void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe, | |
686 | struct vsp1_entity *input, | |
687 | unsigned int alpha) | |
688 | { | |
689 | struct vsp1_entity *entity; | |
690 | struct media_pad *pad; | |
691 | ||
692 | pad = media_entity_remote_pad(&input->pads[RWPF_PAD_SOURCE]); | |
693 | ||
694 | while (pad) { | |
695 | if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
696 | break; | |
697 | ||
698 | entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity)); | |
699 | ||
700 | /* The BRU background color has a fixed alpha value set to 255, | |
701 | * the output alpha value is thus always equal to 255. | |
702 | */ | |
703 | if (entity->type == VSP1_ENTITY_BRU) | |
704 | alpha = 255; | |
705 | ||
706 | if (entity->type == VSP1_ENTITY_UDS) { | |
707 | struct vsp1_uds *uds = to_uds(&entity->subdev); | |
708 | ||
709 | vsp1_uds_set_alpha(uds, alpha); | |
710 | break; | |
711 | } | |
712 | ||
713 | pad = &entity->pads[entity->source_pad]; | |
714 | pad = media_entity_remote_pad(pad); | |
715 | } | |
716 | } | |
717 | ||
139c9286 SF |
718 | void vsp1_pipelines_suspend(struct vsp1_device *vsp1) |
719 | { | |
720 | unsigned long flags; | |
721 | unsigned int i; | |
722 | int ret; | |
723 | ||
724 | /* To avoid increasing the system suspend time needlessly, loop over the | |
725 | * pipelines twice, first to set them all to the stopping state, and then | |
726 | * to wait for the stop to complete. | |
727 | */ | |
728 | for (i = 0; i < vsp1->pdata.wpf_count; ++i) { | |
729 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
730 | struct vsp1_pipeline *pipe; | |
731 | ||
732 | if (wpf == NULL) | |
733 | continue; | |
734 | ||
735 | pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity); | |
736 | if (pipe == NULL) | |
737 | continue; | |
738 | ||
739 | spin_lock_irqsave(&pipe->irqlock, flags); | |
740 | if (pipe->state == VSP1_PIPELINE_RUNNING) | |
741 | pipe->state = VSP1_PIPELINE_STOPPING; | |
742 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
743 | } | |
744 | ||
745 | for (i = 0; i < vsp1->pdata.wpf_count; ++i) { | |
746 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
747 | struct vsp1_pipeline *pipe; | |
748 | ||
749 | if (wpf == NULL) | |
750 | continue; | |
751 | ||
752 | pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity); | |
753 | if (pipe == NULL) | |
754 | continue; | |
755 | ||
1c991fee | 756 | ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe), |
139c9286 SF |
757 | msecs_to_jiffies(500)); |
758 | if (ret == 0) | |
759 | dev_warn(vsp1->dev, "pipeline %u stop timeout\n", | |
760 | wpf->entity.index); | |
761 | } | |
762 | } | |
763 | ||
764 | void vsp1_pipelines_resume(struct vsp1_device *vsp1) | |
765 | { | |
766 | unsigned int i; | |
767 | ||
768 | /* Resume pipeline all running pipelines. */ | |
769 | for (i = 0; i < vsp1->pdata.wpf_count; ++i) { | |
770 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
771 | struct vsp1_pipeline *pipe; | |
772 | ||
773 | if (wpf == NULL) | |
774 | continue; | |
775 | ||
776 | pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity); | |
777 | if (pipe == NULL) | |
778 | continue; | |
779 | ||
780 | if (vsp1_pipeline_ready(pipe)) | |
781 | vsp1_pipeline_run(pipe); | |
782 | } | |
783 | } | |
784 | ||
26e0ca22 LP |
785 | /* ----------------------------------------------------------------------------- |
786 | * videobuf2 Queue Operations | |
787 | */ | |
788 | ||
789 | static int | |
790 | vsp1_video_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, | |
791 | unsigned int *nbuffers, unsigned int *nplanes, | |
792 | unsigned int sizes[], void *alloc_ctxs[]) | |
793 | { | |
794 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
795 | const struct v4l2_pix_format_mplane *format; | |
796 | struct v4l2_pix_format_mplane pix_mp; | |
797 | unsigned int i; | |
798 | ||
799 | if (fmt) { | |
800 | /* Make sure the format is valid and adjust the sizeimage field | |
801 | * if needed. | |
802 | */ | |
803 | if (!vsp1_video_format_adjust(video, &fmt->fmt.pix_mp, &pix_mp)) | |
804 | return -EINVAL; | |
805 | ||
806 | format = &pix_mp; | |
807 | } else { | |
808 | format = &video->format; | |
809 | } | |
810 | ||
811 | *nplanes = format->num_planes; | |
812 | ||
813 | for (i = 0; i < format->num_planes; ++i) { | |
814 | sizes[i] = format->plane_fmt[i].sizeimage; | |
815 | alloc_ctxs[i] = video->alloc_ctx; | |
816 | } | |
817 | ||
818 | return 0; | |
819 | } | |
820 | ||
821 | static int vsp1_video_buffer_prepare(struct vb2_buffer *vb) | |
822 | { | |
823 | struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue); | |
824 | struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb); | |
825 | const struct v4l2_pix_format_mplane *format = &video->format; | |
826 | unsigned int i; | |
827 | ||
828 | if (vb->num_planes < format->num_planes) | |
829 | return -EINVAL; | |
830 | ||
26e0ca22 LP |
831 | for (i = 0; i < vb->num_planes; ++i) { |
832 | buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i); | |
833 | buf->length[i] = vb2_plane_size(vb, i); | |
834 | ||
835 | if (buf->length[i] < format->plane_fmt[i].sizeimage) | |
836 | return -EINVAL; | |
837 | } | |
838 | ||
839 | return 0; | |
840 | } | |
841 | ||
842 | static void vsp1_video_buffer_queue(struct vb2_buffer *vb) | |
843 | { | |
844 | struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue); | |
845 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); | |
846 | struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb); | |
847 | unsigned long flags; | |
848 | bool empty; | |
849 | ||
850 | spin_lock_irqsave(&video->irqlock, flags); | |
851 | empty = list_empty(&video->irqqueue); | |
852 | list_add_tail(&buf->queue, &video->irqqueue); | |
853 | spin_unlock_irqrestore(&video->irqlock, flags); | |
854 | ||
855 | if (!empty) | |
856 | return; | |
857 | ||
858 | spin_lock_irqsave(&pipe->irqlock, flags); | |
859 | ||
860 | video->ops->queue(video, buf); | |
861 | pipe->buffers_ready |= 1 << video->pipe_index; | |
862 | ||
863 | if (vb2_is_streaming(&video->queue) && | |
864 | vsp1_pipeline_ready(pipe)) | |
865 | vsp1_pipeline_run(pipe); | |
866 | ||
867 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
868 | } | |
869 | ||
870 | static void vsp1_entity_route_setup(struct vsp1_entity *source) | |
871 | { | |
872 | struct vsp1_entity *sink; | |
873 | ||
d9b45ed3 | 874 | if (source->route->reg == 0) |
26e0ca22 LP |
875 | return; |
876 | ||
877 | sink = container_of(source->sink, struct vsp1_entity, subdev.entity); | |
d9b45ed3 LP |
878 | vsp1_write(source->vsp1, source->route->reg, |
879 | sink->route->inputs[source->sink_pad]); | |
26e0ca22 LP |
880 | } |
881 | ||
882 | static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count) | |
883 | { | |
884 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
885 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); | |
886 | struct vsp1_entity *entity; | |
887 | unsigned long flags; | |
888 | int ret; | |
889 | ||
890 | mutex_lock(&pipe->lock); | |
891 | if (pipe->stream_count == pipe->num_video - 1) { | |
bdc2df62 LP |
892 | if (pipe->uds) { |
893 | struct vsp1_uds *uds = to_uds(&pipe->uds->subdev); | |
894 | ||
895 | /* If a BRU is present in the pipeline before the UDS, | |
896 | * the alpha component doesn't need to be scaled as the | |
897 | * BRU output alpha value is fixed to 255. Otherwise we | |
898 | * need to scale the alpha component only when available | |
899 | * at the input RPF. | |
900 | */ | |
901 | if (pipe->uds_input->type == VSP1_ENTITY_BRU) { | |
902 | uds->scale_alpha = false; | |
903 | } else { | |
904 | struct vsp1_rwpf *rpf = | |
905 | to_rwpf(&pipe->uds_input->subdev); | |
906 | ||
907 | uds->scale_alpha = rpf->video.fmtinfo->alpha; | |
908 | } | |
909 | } | |
910 | ||
26e0ca22 LP |
911 | list_for_each_entry(entity, &pipe->entities, list_pipe) { |
912 | vsp1_entity_route_setup(entity); | |
913 | ||
914 | ret = v4l2_subdev_call(&entity->subdev, video, | |
915 | s_stream, 1); | |
916 | if (ret < 0) { | |
917 | mutex_unlock(&pipe->lock); | |
918 | return ret; | |
919 | } | |
920 | } | |
921 | } | |
922 | ||
923 | pipe->stream_count++; | |
924 | mutex_unlock(&pipe->lock); | |
925 | ||
926 | spin_lock_irqsave(&pipe->irqlock, flags); | |
927 | if (vsp1_pipeline_ready(pipe)) | |
928 | vsp1_pipeline_run(pipe); | |
929 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
930 | ||
931 | return 0; | |
932 | } | |
933 | ||
e37559b2 | 934 | static void vsp1_video_stop_streaming(struct vb2_queue *vq) |
26e0ca22 LP |
935 | { |
936 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
937 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); | |
9df04e9d | 938 | struct vsp1_video_buffer *buffer; |
26e0ca22 LP |
939 | unsigned long flags; |
940 | int ret; | |
941 | ||
942 | mutex_lock(&pipe->lock); | |
943 | if (--pipe->stream_count == 0) { | |
944 | /* Stop the pipeline. */ | |
945 | ret = vsp1_pipeline_stop(pipe); | |
946 | if (ret == -ETIMEDOUT) | |
947 | dev_err(video->vsp1->dev, "pipeline stop timeout\n"); | |
948 | } | |
949 | mutex_unlock(&pipe->lock); | |
950 | ||
951 | vsp1_pipeline_cleanup(pipe); | |
952 | media_entity_pipeline_stop(&video->video.entity); | |
953 | ||
954 | /* Remove all buffers from the IRQ queue. */ | |
955 | spin_lock_irqsave(&video->irqlock, flags); | |
9df04e9d LP |
956 | list_for_each_entry(buffer, &video->irqqueue, queue) |
957 | vb2_buffer_done(&buffer->buf, VB2_BUF_STATE_ERROR); | |
26e0ca22 LP |
958 | INIT_LIST_HEAD(&video->irqqueue); |
959 | spin_unlock_irqrestore(&video->irqlock, flags); | |
26e0ca22 LP |
960 | } |
961 | ||
962 | static struct vb2_ops vsp1_video_queue_qops = { | |
963 | .queue_setup = vsp1_video_queue_setup, | |
964 | .buf_prepare = vsp1_video_buffer_prepare, | |
965 | .buf_queue = vsp1_video_buffer_queue, | |
966 | .wait_prepare = vb2_ops_wait_prepare, | |
967 | .wait_finish = vb2_ops_wait_finish, | |
968 | .start_streaming = vsp1_video_start_streaming, | |
969 | .stop_streaming = vsp1_video_stop_streaming, | |
970 | }; | |
971 | ||
972 | /* ----------------------------------------------------------------------------- | |
973 | * V4L2 ioctls | |
974 | */ | |
975 | ||
976 | static int | |
977 | vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap) | |
978 | { | |
979 | struct v4l2_fh *vfh = file->private_data; | |
980 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
981 | ||
982 | cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING | |
983 | | V4L2_CAP_VIDEO_CAPTURE_MPLANE | |
984 | | V4L2_CAP_VIDEO_OUTPUT_MPLANE; | |
985 | ||
986 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | |
987 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | |
988 | | V4L2_CAP_STREAMING; | |
989 | else | |
990 | cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE | |
991 | | V4L2_CAP_STREAMING; | |
992 | ||
993 | strlcpy(cap->driver, "vsp1", sizeof(cap->driver)); | |
994 | strlcpy(cap->card, video->video.name, sizeof(cap->card)); | |
995 | snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", | |
996 | dev_name(video->vsp1->dev)); | |
997 | ||
998 | return 0; | |
999 | } | |
1000 | ||
1001 | static int | |
1002 | vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format) | |
1003 | { | |
1004 | struct v4l2_fh *vfh = file->private_data; | |
1005 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
1006 | ||
1007 | if (format->type != video->queue.type) | |
1008 | return -EINVAL; | |
1009 | ||
1010 | mutex_lock(&video->lock); | |
1011 | format->fmt.pix_mp = video->format; | |
1012 | mutex_unlock(&video->lock); | |
1013 | ||
1014 | return 0; | |
1015 | } | |
1016 | ||
1017 | static int | |
1018 | vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format) | |
1019 | { | |
1020 | struct v4l2_fh *vfh = file->private_data; | |
1021 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
1022 | ||
1023 | if (format->type != video->queue.type) | |
1024 | return -EINVAL; | |
1025 | ||
1026 | return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL); | |
1027 | } | |
1028 | ||
1029 | static int | |
1030 | vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format) | |
1031 | { | |
1032 | struct v4l2_fh *vfh = file->private_data; | |
1033 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
1034 | const struct vsp1_format_info *info; | |
1035 | int ret; | |
1036 | ||
1037 | if (format->type != video->queue.type) | |
1038 | return -EINVAL; | |
1039 | ||
1040 | ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info); | |
1041 | if (ret < 0) | |
1042 | return ret; | |
1043 | ||
1044 | mutex_lock(&video->lock); | |
1045 | ||
1046 | if (vb2_is_busy(&video->queue)) { | |
1047 | ret = -EBUSY; | |
1048 | goto done; | |
1049 | } | |
1050 | ||
1051 | video->format = format->fmt.pix_mp; | |
1052 | video->fmtinfo = info; | |
1053 | ||
1054 | done: | |
1055 | mutex_unlock(&video->lock); | |
1056 | return ret; | |
1057 | } | |
1058 | ||
1059 | static int | |
1060 | vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type) | |
1061 | { | |
1062 | struct v4l2_fh *vfh = file->private_data; | |
1063 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
1064 | struct vsp1_pipeline *pipe; | |
1065 | int ret; | |
1066 | ||
26e0ca22 LP |
1067 | if (video->queue.owner && video->queue.owner != file->private_data) |
1068 | return -EBUSY; | |
1069 | ||
1070 | video->sequence = 0; | |
1071 | ||
1072 | /* Start streaming on the pipeline. No link touching an entity in the | |
1073 | * pipeline can be activated or deactivated once streaming is started. | |
1074 | * | |
1075 | * Use the VSP1 pipeline object embedded in the first video object that | |
1076 | * starts streaming. | |
1077 | */ | |
1078 | pipe = video->video.entity.pipe | |
1079 | ? to_vsp1_pipeline(&video->video.entity) : &video->pipe; | |
1080 | ||
1081 | ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe); | |
1082 | if (ret < 0) | |
1083 | return ret; | |
1084 | ||
1085 | /* Verify that the configured format matches the output of the connected | |
1086 | * subdev. | |
1087 | */ | |
1088 | ret = vsp1_video_verify_format(video); | |
1089 | if (ret < 0) | |
1090 | goto err_stop; | |
1091 | ||
1092 | ret = vsp1_pipeline_init(pipe, video); | |
1093 | if (ret < 0) | |
1094 | goto err_stop; | |
1095 | ||
1096 | /* Start the queue. */ | |
1097 | ret = vb2_streamon(&video->queue, type); | |
1098 | if (ret < 0) | |
1099 | goto err_cleanup; | |
1100 | ||
1101 | return 0; | |
1102 | ||
1103 | err_cleanup: | |
1104 | vsp1_pipeline_cleanup(pipe); | |
1105 | err_stop: | |
1106 | media_entity_pipeline_stop(&video->video.entity); | |
1107 | return ret; | |
1108 | } | |
1109 | ||
1110 | static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = { | |
1111 | .vidioc_querycap = vsp1_video_querycap, | |
1112 | .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format, | |
1113 | .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format, | |
1114 | .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format, | |
1115 | .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format, | |
1116 | .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format, | |
1117 | .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format, | |
1118 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
1119 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1120 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1121 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
1122 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
1123 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
1124 | .vidioc_streamon = vsp1_video_streamon, | |
1125 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
1126 | }; | |
1127 | ||
1128 | /* ----------------------------------------------------------------------------- | |
1129 | * V4L2 File Operations | |
1130 | */ | |
1131 | ||
1132 | static int vsp1_video_open(struct file *file) | |
1133 | { | |
1134 | struct vsp1_video *video = video_drvdata(file); | |
1135 | struct v4l2_fh *vfh; | |
1136 | int ret = 0; | |
1137 | ||
1138 | vfh = kzalloc(sizeof(*vfh), GFP_KERNEL); | |
1139 | if (vfh == NULL) | |
1140 | return -ENOMEM; | |
1141 | ||
1142 | v4l2_fh_init(vfh, &video->video); | |
1143 | v4l2_fh_add(vfh); | |
1144 | ||
1145 | file->private_data = vfh; | |
1146 | ||
4c16d6a0 LP |
1147 | ret = vsp1_device_get(video->vsp1); |
1148 | if (ret < 0) { | |
26e0ca22 LP |
1149 | v4l2_fh_del(vfh); |
1150 | kfree(vfh); | |
1151 | } | |
1152 | ||
1153 | return ret; | |
1154 | } | |
1155 | ||
1156 | static int vsp1_video_release(struct file *file) | |
1157 | { | |
1158 | struct vsp1_video *video = video_drvdata(file); | |
1159 | struct v4l2_fh *vfh = file->private_data; | |
1160 | ||
1161 | mutex_lock(&video->lock); | |
1162 | if (video->queue.owner == vfh) { | |
1163 | vb2_queue_release(&video->queue); | |
1164 | video->queue.owner = NULL; | |
1165 | } | |
1166 | mutex_unlock(&video->lock); | |
1167 | ||
1168 | vsp1_device_put(video->vsp1); | |
1169 | ||
1170 | v4l2_fh_release(file); | |
1171 | ||
1172 | file->private_data = NULL; | |
1173 | ||
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | static struct v4l2_file_operations vsp1_video_fops = { | |
1178 | .owner = THIS_MODULE, | |
1179 | .unlocked_ioctl = video_ioctl2, | |
1180 | .open = vsp1_video_open, | |
1181 | .release = vsp1_video_release, | |
1182 | .poll = vb2_fop_poll, | |
1183 | .mmap = vb2_fop_mmap, | |
1184 | }; | |
1185 | ||
1186 | /* ----------------------------------------------------------------------------- | |
1187 | * Initialization and Cleanup | |
1188 | */ | |
1189 | ||
1190 | int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf) | |
1191 | { | |
1192 | const char *direction; | |
1193 | int ret; | |
1194 | ||
1195 | switch (video->type) { | |
1196 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: | |
1197 | direction = "output"; | |
1198 | video->pad.flags = MEDIA_PAD_FL_SINK; | |
1199 | break; | |
1200 | ||
1201 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: | |
1202 | direction = "input"; | |
1203 | video->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1204 | video->video.vfl_dir = VFL_DIR_TX; | |
1205 | break; | |
1206 | ||
1207 | default: | |
1208 | return -EINVAL; | |
1209 | } | |
1210 | ||
1211 | video->rwpf = rwpf; | |
1212 | ||
1213 | mutex_init(&video->lock); | |
1214 | spin_lock_init(&video->irqlock); | |
1215 | INIT_LIST_HEAD(&video->irqqueue); | |
1216 | ||
1217 | mutex_init(&video->pipe.lock); | |
1218 | spin_lock_init(&video->pipe.irqlock); | |
1219 | INIT_LIST_HEAD(&video->pipe.entities); | |
1220 | init_waitqueue_head(&video->pipe.wq); | |
1221 | video->pipe.state = VSP1_PIPELINE_STOPPED; | |
1222 | ||
1223 | /* Initialize the media entity... */ | |
1224 | ret = media_entity_init(&video->video.entity, 1, &video->pad, 0); | |
1225 | if (ret < 0) | |
1226 | return ret; | |
1227 | ||
1228 | /* ... and the format ... */ | |
1229 | video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT); | |
1230 | video->format.pixelformat = video->fmtinfo->fourcc; | |
1231 | video->format.colorspace = V4L2_COLORSPACE_SRGB; | |
1232 | video->format.field = V4L2_FIELD_NONE; | |
1233 | video->format.width = VSP1_VIDEO_DEF_WIDTH; | |
1234 | video->format.height = VSP1_VIDEO_DEF_HEIGHT; | |
1235 | video->format.num_planes = 1; | |
1236 | video->format.plane_fmt[0].bytesperline = | |
1237 | video->format.width * video->fmtinfo->bpp[0] / 8; | |
1238 | video->format.plane_fmt[0].sizeimage = | |
1239 | video->format.plane_fmt[0].bytesperline * video->format.height; | |
1240 | ||
1241 | /* ... and the video node... */ | |
1242 | video->video.v4l2_dev = &video->vsp1->v4l2_dev; | |
1243 | video->video.fops = &vsp1_video_fops; | |
1244 | snprintf(video->video.name, sizeof(video->video.name), "%s %s", | |
1245 | rwpf->subdev.name, direction); | |
1246 | video->video.vfl_type = VFL_TYPE_GRABBER; | |
1247 | video->video.release = video_device_release_empty; | |
1248 | video->video.ioctl_ops = &vsp1_video_ioctl_ops; | |
1249 | ||
1250 | video_set_drvdata(&video->video, video); | |
1251 | ||
1252 | /* ... and the buffers queue... */ | |
1253 | video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev); | |
b317828b WY |
1254 | if (IS_ERR(video->alloc_ctx)) { |
1255 | ret = PTR_ERR(video->alloc_ctx); | |
26e0ca22 | 1256 | goto error; |
b317828b | 1257 | } |
26e0ca22 LP |
1258 | |
1259 | video->queue.type = video->type; | |
1260 | video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; | |
1261 | video->queue.lock = &video->lock; | |
1262 | video->queue.drv_priv = video; | |
1263 | video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer); | |
1264 | video->queue.ops = &vsp1_video_queue_qops; | |
1265 | video->queue.mem_ops = &vb2_dma_contig_memops; | |
ade48681 | 1266 | video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
26e0ca22 LP |
1267 | ret = vb2_queue_init(&video->queue); |
1268 | if (ret < 0) { | |
1269 | dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n"); | |
1270 | goto error; | |
1271 | } | |
1272 | ||
1273 | /* ... and register the video device. */ | |
1274 | video->video.queue = &video->queue; | |
1275 | ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1); | |
1276 | if (ret < 0) { | |
1277 | dev_err(video->vsp1->dev, "failed to register video device\n"); | |
1278 | goto error; | |
1279 | } | |
1280 | ||
1281 | return 0; | |
1282 | ||
1283 | error: | |
1284 | vb2_dma_contig_cleanup_ctx(video->alloc_ctx); | |
1285 | vsp1_video_cleanup(video); | |
1286 | return ret; | |
1287 | } | |
1288 | ||
1289 | void vsp1_video_cleanup(struct vsp1_video *video) | |
1290 | { | |
1291 | if (video_is_registered(&video->video)) | |
1292 | video_unregister_device(&video->video); | |
1293 | ||
1294 | vb2_dma_contig_cleanup_ctx(video->alloc_ctx); | |
1295 | media_entity_cleanup(&video->video.entity); | |
1296 | } |