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 | ||
26e0ca22 LP |
277 | /* ----------------------------------------------------------------------------- |
278 | * Pipeline Management | |
279 | */ | |
280 | ||
bdc2df62 LP |
281 | static int vsp1_pipeline_validate_branch(struct vsp1_pipeline *pipe, |
282 | struct vsp1_rwpf *input, | |
26e0ca22 LP |
283 | struct vsp1_rwpf *output) |
284 | { | |
285 | struct vsp1_entity *entity; | |
286 | unsigned int entities = 0; | |
287 | struct media_pad *pad; | |
bdc2df62 | 288 | bool bru_found = false; |
26e0ca22 | 289 | |
629bb6d4 LP |
290 | input->location.left = 0; |
291 | input->location.top = 0; | |
292 | ||
26e0ca22 LP |
293 | pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]); |
294 | ||
295 | while (1) { | |
296 | if (pad == NULL) | |
297 | return -EPIPE; | |
298 | ||
299 | /* We've reached a video node, that shouldn't have happened. */ | |
300 | if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
301 | return -EPIPE; | |
302 | ||
303 | entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity)); | |
304 | ||
629bb6d4 LP |
305 | /* A BRU is present in the pipeline, store the compose rectangle |
306 | * location in the input RPF for use when configuring the RPF. | |
307 | */ | |
308 | if (entity->type == VSP1_ENTITY_BRU) { | |
309 | struct vsp1_bru *bru = to_bru(&entity->subdev); | |
6418b4d6 LP |
310 | struct v4l2_rect *rect = |
311 | &bru->inputs[pad->index].compose; | |
312 | ||
313 | bru->inputs[pad->index].rpf = input; | |
629bb6d4 LP |
314 | |
315 | input->location.left = rect->left; | |
316 | input->location.top = rect->top; | |
bdc2df62 LP |
317 | |
318 | bru_found = true; | |
629bb6d4 LP |
319 | } |
320 | ||
26e0ca22 LP |
321 | /* We've reached the WPF, we're done. */ |
322 | if (entity->type == VSP1_ENTITY_WPF) | |
323 | break; | |
324 | ||
325 | /* Ensure the branch has no loop. */ | |
fa762394 | 326 | if (entities & (1 << media_entity_id(&entity->subdev.entity))) |
26e0ca22 LP |
327 | return -EPIPE; |
328 | ||
fa762394 | 329 | entities |= 1 << media_entity_id(&entity->subdev.entity); |
26e0ca22 LP |
330 | |
331 | /* UDS can't be chained. */ | |
332 | if (entity->type == VSP1_ENTITY_UDS) { | |
bdc2df62 | 333 | if (pipe->uds) |
26e0ca22 | 334 | return -EPIPE; |
bdc2df62 LP |
335 | |
336 | pipe->uds = entity; | |
337 | pipe->uds_input = bru_found ? pipe->bru | |
338 | : &input->entity; | |
26e0ca22 LP |
339 | } |
340 | ||
341 | /* Follow the source link. The link setup operations ensure | |
342 | * that the output fan-out can't be more than one, there is thus | |
343 | * no need to verify here that only a single source link is | |
344 | * activated. | |
345 | */ | |
346 | pad = &entity->pads[entity->source_pad]; | |
347 | pad = media_entity_remote_pad(pad); | |
348 | } | |
349 | ||
350 | /* The last entity must be the output WPF. */ | |
351 | if (entity != &output->entity) | |
352 | return -EPIPE; | |
353 | ||
354 | return 0; | |
355 | } | |
356 | ||
6418b4d6 LP |
357 | static void __vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe) |
358 | { | |
359 | if (pipe->bru) { | |
360 | struct vsp1_bru *bru = to_bru(&pipe->bru->subdev); | |
361 | unsigned int i; | |
362 | ||
363 | for (i = 0; i < ARRAY_SIZE(bru->inputs); ++i) | |
364 | bru->inputs[i].rpf = NULL; | |
365 | } | |
366 | ||
367 | INIT_LIST_HEAD(&pipe->entities); | |
368 | pipe->state = VSP1_PIPELINE_STOPPED; | |
369 | pipe->buffers_ready = 0; | |
370 | pipe->num_video = 0; | |
371 | pipe->num_inputs = 0; | |
372 | pipe->output = NULL; | |
373 | pipe->bru = NULL; | |
374 | pipe->lif = NULL; | |
bdc2df62 | 375 | pipe->uds = NULL; |
6418b4d6 LP |
376 | } |
377 | ||
26e0ca22 LP |
378 | static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe, |
379 | struct vsp1_video *video) | |
380 | { | |
381 | struct media_entity_graph graph; | |
382 | struct media_entity *entity = &video->video.entity; | |
383 | struct media_device *mdev = entity->parent; | |
384 | unsigned int i; | |
385 | int ret; | |
386 | ||
387 | mutex_lock(&mdev->graph_mutex); | |
388 | ||
389 | /* Walk the graph to locate the entities and video nodes. */ | |
390 | media_entity_graph_walk_start(&graph, entity); | |
391 | ||
392 | while ((entity = media_entity_graph_walk_next(&graph))) { | |
393 | struct v4l2_subdev *subdev; | |
394 | struct vsp1_rwpf *rwpf; | |
395 | struct vsp1_entity *e; | |
396 | ||
397 | if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) { | |
398 | pipe->num_video++; | |
399 | continue; | |
400 | } | |
401 | ||
402 | subdev = media_entity_to_v4l2_subdev(entity); | |
403 | e = to_vsp1_entity(subdev); | |
404 | list_add_tail(&e->list_pipe, &pipe->entities); | |
405 | ||
406 | if (e->type == VSP1_ENTITY_RPF) { | |
407 | rwpf = to_rwpf(subdev); | |
408 | pipe->inputs[pipe->num_inputs++] = rwpf; | |
409 | rwpf->video.pipe_index = pipe->num_inputs; | |
410 | } else if (e->type == VSP1_ENTITY_WPF) { | |
411 | rwpf = to_rwpf(subdev); | |
412 | pipe->output = to_rwpf(subdev); | |
413 | rwpf->video.pipe_index = 0; | |
414 | } else if (e->type == VSP1_ENTITY_LIF) { | |
415 | pipe->lif = e; | |
629bb6d4 LP |
416 | } else if (e->type == VSP1_ENTITY_BRU) { |
417 | pipe->bru = e; | |
26e0ca22 LP |
418 | } |
419 | } | |
420 | ||
421 | mutex_unlock(&mdev->graph_mutex); | |
422 | ||
423 | /* We need one output and at least one input. */ | |
424 | if (pipe->num_inputs == 0 || !pipe->output) { | |
425 | ret = -EPIPE; | |
426 | goto error; | |
427 | } | |
428 | ||
429 | /* Follow links downstream for each input and make sure the graph | |
430 | * contains no loop and that all branches end at the output WPF. | |
431 | */ | |
432 | for (i = 0; i < pipe->num_inputs; ++i) { | |
bdc2df62 | 433 | ret = vsp1_pipeline_validate_branch(pipe, pipe->inputs[i], |
26e0ca22 LP |
434 | pipe->output); |
435 | if (ret < 0) | |
436 | goto error; | |
437 | } | |
438 | ||
439 | return 0; | |
440 | ||
441 | error: | |
6418b4d6 | 442 | __vsp1_pipeline_cleanup(pipe); |
26e0ca22 LP |
443 | return ret; |
444 | } | |
445 | ||
446 | static int vsp1_pipeline_init(struct vsp1_pipeline *pipe, | |
447 | struct vsp1_video *video) | |
448 | { | |
449 | int ret; | |
450 | ||
451 | mutex_lock(&pipe->lock); | |
452 | ||
453 | /* If we're the first user validate and initialize the pipeline. */ | |
454 | if (pipe->use_count == 0) { | |
455 | ret = vsp1_pipeline_validate(pipe, video); | |
456 | if (ret < 0) | |
457 | goto done; | |
458 | } | |
459 | ||
460 | pipe->use_count++; | |
461 | ret = 0; | |
462 | ||
463 | done: | |
464 | mutex_unlock(&pipe->lock); | |
465 | return ret; | |
466 | } | |
467 | ||
468 | static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe) | |
469 | { | |
470 | mutex_lock(&pipe->lock); | |
471 | ||
472 | /* If we're the last user clean up the pipeline. */ | |
6418b4d6 LP |
473 | if (--pipe->use_count == 0) |
474 | __vsp1_pipeline_cleanup(pipe); | |
26e0ca22 LP |
475 | |
476 | mutex_unlock(&pipe->lock); | |
477 | } | |
478 | ||
479 | static void vsp1_pipeline_run(struct vsp1_pipeline *pipe) | |
480 | { | |
481 | struct vsp1_device *vsp1 = pipe->output->entity.vsp1; | |
482 | ||
483 | vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD); | |
484 | pipe->state = VSP1_PIPELINE_RUNNING; | |
485 | pipe->buffers_ready = 0; | |
486 | } | |
487 | ||
41bdc3cf | 488 | static bool vsp1_pipeline_stopped(struct vsp1_pipeline *pipe) |
1c991fee LP |
489 | { |
490 | unsigned long flags; | |
491 | bool stopped; | |
492 | ||
493 | spin_lock_irqsave(&pipe->irqlock, flags); | |
494 | stopped = pipe->state == VSP1_PIPELINE_STOPPED, | |
495 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
496 | ||
497 | return stopped; | |
498 | } | |
499 | ||
26e0ca22 LP |
500 | static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe) |
501 | { | |
502 | struct vsp1_entity *entity; | |
503 | unsigned long flags; | |
504 | int ret; | |
505 | ||
506 | spin_lock_irqsave(&pipe->irqlock, flags); | |
9b3e6e2a LP |
507 | if (pipe->state == VSP1_PIPELINE_RUNNING) |
508 | pipe->state = VSP1_PIPELINE_STOPPING; | |
26e0ca22 LP |
509 | spin_unlock_irqrestore(&pipe->irqlock, flags); |
510 | ||
1c991fee | 511 | ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe), |
26e0ca22 LP |
512 | msecs_to_jiffies(500)); |
513 | ret = ret == 0 ? -ETIMEDOUT : 0; | |
514 | ||
515 | list_for_each_entry(entity, &pipe->entities, list_pipe) { | |
aaa48cbb | 516 | if (entity->route && entity->route->reg) |
d9b45ed3 | 517 | vsp1_write(entity->vsp1, entity->route->reg, |
26e0ca22 LP |
518 | VI6_DPR_NODE_UNUSED); |
519 | ||
520 | v4l2_subdev_call(&entity->subdev, video, s_stream, 0); | |
521 | } | |
522 | ||
523 | return ret; | |
524 | } | |
525 | ||
526 | static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe) | |
527 | { | |
528 | unsigned int mask; | |
529 | ||
530 | mask = ((1 << pipe->num_inputs) - 1) << 1; | |
531 | if (!pipe->lif) | |
532 | mask |= 1 << 0; | |
533 | ||
534 | return pipe->buffers_ready == mask; | |
535 | } | |
536 | ||
537 | /* | |
538 | * vsp1_video_complete_buffer - Complete the current buffer | |
539 | * @video: the video node | |
540 | * | |
541 | * This function completes the current buffer by filling its sequence number, | |
542 | * time stamp and payload size, and hands it back to the videobuf core. | |
543 | * | |
3299ba5c LP |
544 | * When operating in DU output mode (deep pipeline to the DU through the LIF), |
545 | * the VSP1 needs to constantly supply frames to the display. In that case, if | |
546 | * no other buffer is queued, reuse the one that has just been processed instead | |
547 | * of handing it back to the videobuf core. | |
548 | * | |
26e0ca22 LP |
549 | * Return the next queued buffer or NULL if the queue is empty. |
550 | */ | |
551 | static struct vsp1_video_buffer * | |
552 | vsp1_video_complete_buffer(struct vsp1_video *video) | |
553 | { | |
3299ba5c | 554 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); |
26e0ca22 LP |
555 | struct vsp1_video_buffer *next = NULL; |
556 | struct vsp1_video_buffer *done; | |
557 | unsigned long flags; | |
558 | unsigned int i; | |
559 | ||
560 | spin_lock_irqsave(&video->irqlock, flags); | |
561 | ||
562 | if (list_empty(&video->irqqueue)) { | |
563 | spin_unlock_irqrestore(&video->irqlock, flags); | |
564 | return NULL; | |
565 | } | |
566 | ||
567 | done = list_first_entry(&video->irqqueue, | |
568 | struct vsp1_video_buffer, queue); | |
3299ba5c LP |
569 | |
570 | /* In DU output mode reuse the buffer if the list is singular. */ | |
571 | if (pipe->lif && list_is_singular(&video->irqqueue)) { | |
572 | spin_unlock_irqrestore(&video->irqlock, flags); | |
573 | return done; | |
574 | } | |
575 | ||
26e0ca22 LP |
576 | list_del(&done->queue); |
577 | ||
578 | if (!list_empty(&video->irqqueue)) | |
579 | next = list_first_entry(&video->irqqueue, | |
580 | struct vsp1_video_buffer, queue); | |
581 | ||
582 | spin_unlock_irqrestore(&video->irqlock, flags); | |
583 | ||
2d700715 | 584 | done->buf.sequence = video->sequence++; |
d6dd645e | 585 | done->buf.vb2_buf.timestamp = ktime_get_ns(); |
2d700715 JS |
586 | for (i = 0; i < done->buf.vb2_buf.num_planes; ++i) |
587 | vb2_set_plane_payload(&done->buf.vb2_buf, i, done->length[i]); | |
588 | vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE); | |
26e0ca22 LP |
589 | |
590 | return next; | |
591 | } | |
592 | ||
593 | static void vsp1_video_frame_end(struct vsp1_pipeline *pipe, | |
594 | struct vsp1_video *video) | |
595 | { | |
596 | struct vsp1_video_buffer *buf; | |
597 | unsigned long flags; | |
598 | ||
599 | buf = vsp1_video_complete_buffer(video); | |
600 | if (buf == NULL) | |
601 | return; | |
602 | ||
603 | spin_lock_irqsave(&pipe->irqlock, flags); | |
604 | ||
605 | video->ops->queue(video, buf); | |
606 | pipe->buffers_ready |= 1 << video->pipe_index; | |
607 | ||
608 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
609 | } | |
610 | ||
611 | void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe) | |
612 | { | |
9b3e6e2a | 613 | enum vsp1_pipeline_state state; |
26e0ca22 LP |
614 | unsigned long flags; |
615 | unsigned int i; | |
616 | ||
617 | if (pipe == NULL) | |
618 | return; | |
619 | ||
620 | /* Complete buffers on all video nodes. */ | |
621 | for (i = 0; i < pipe->num_inputs; ++i) | |
622 | vsp1_video_frame_end(pipe, &pipe->inputs[i]->video); | |
623 | ||
624 | if (!pipe->lif) | |
625 | vsp1_video_frame_end(pipe, &pipe->output->video); | |
626 | ||
627 | spin_lock_irqsave(&pipe->irqlock, flags); | |
628 | ||
9b3e6e2a LP |
629 | state = pipe->state; |
630 | pipe->state = VSP1_PIPELINE_STOPPED; | |
631 | ||
26e0ca22 LP |
632 | /* If a stop has been requested, mark the pipeline as stopped and |
633 | * return. | |
634 | */ | |
9b3e6e2a | 635 | if (state == VSP1_PIPELINE_STOPPING) { |
26e0ca22 LP |
636 | wake_up(&pipe->wq); |
637 | goto done; | |
638 | } | |
639 | ||
640 | /* Restart the pipeline if ready. */ | |
641 | if (vsp1_pipeline_ready(pipe)) | |
642 | vsp1_pipeline_run(pipe); | |
643 | ||
644 | done: | |
645 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
646 | } | |
647 | ||
bdc2df62 LP |
648 | /* |
649 | * Propagate the alpha value through the pipeline. | |
650 | * | |
651 | * As the UDS has restricted scaling capabilities when the alpha component needs | |
652 | * to be scaled, we disable alpha scaling when the UDS input has a fixed alpha | |
653 | * value. The UDS then outputs a fixed alpha value which needs to be programmed | |
654 | * from the input RPF alpha. | |
655 | */ | |
656 | void vsp1_pipeline_propagate_alpha(struct vsp1_pipeline *pipe, | |
657 | struct vsp1_entity *input, | |
658 | unsigned int alpha) | |
659 | { | |
660 | struct vsp1_entity *entity; | |
661 | struct media_pad *pad; | |
662 | ||
663 | pad = media_entity_remote_pad(&input->pads[RWPF_PAD_SOURCE]); | |
664 | ||
665 | while (pad) { | |
666 | if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV) | |
667 | break; | |
668 | ||
669 | entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity)); | |
670 | ||
671 | /* The BRU background color has a fixed alpha value set to 255, | |
672 | * the output alpha value is thus always equal to 255. | |
673 | */ | |
674 | if (entity->type == VSP1_ENTITY_BRU) | |
675 | alpha = 255; | |
676 | ||
677 | if (entity->type == VSP1_ENTITY_UDS) { | |
678 | struct vsp1_uds *uds = to_uds(&entity->subdev); | |
679 | ||
680 | vsp1_uds_set_alpha(uds, alpha); | |
681 | break; | |
682 | } | |
683 | ||
684 | pad = &entity->pads[entity->source_pad]; | |
685 | pad = media_entity_remote_pad(pad); | |
686 | } | |
687 | } | |
688 | ||
139c9286 SF |
689 | void vsp1_pipelines_suspend(struct vsp1_device *vsp1) |
690 | { | |
691 | unsigned long flags; | |
692 | unsigned int i; | |
693 | int ret; | |
694 | ||
695 | /* To avoid increasing the system suspend time needlessly, loop over the | |
696 | * pipelines twice, first to set them all to the stopping state, and then | |
697 | * to wait for the stop to complete. | |
698 | */ | |
699 | for (i = 0; i < vsp1->pdata.wpf_count; ++i) { | |
700 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
701 | struct vsp1_pipeline *pipe; | |
702 | ||
703 | if (wpf == NULL) | |
704 | continue; | |
705 | ||
706 | pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity); | |
707 | if (pipe == NULL) | |
708 | continue; | |
709 | ||
710 | spin_lock_irqsave(&pipe->irqlock, flags); | |
711 | if (pipe->state == VSP1_PIPELINE_RUNNING) | |
712 | pipe->state = VSP1_PIPELINE_STOPPING; | |
713 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
714 | } | |
715 | ||
716 | for (i = 0; i < vsp1->pdata.wpf_count; ++i) { | |
717 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
718 | struct vsp1_pipeline *pipe; | |
719 | ||
720 | if (wpf == NULL) | |
721 | continue; | |
722 | ||
723 | pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity); | |
724 | if (pipe == NULL) | |
725 | continue; | |
726 | ||
1c991fee | 727 | ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe), |
139c9286 SF |
728 | msecs_to_jiffies(500)); |
729 | if (ret == 0) | |
730 | dev_warn(vsp1->dev, "pipeline %u stop timeout\n", | |
731 | wpf->entity.index); | |
732 | } | |
733 | } | |
734 | ||
735 | void vsp1_pipelines_resume(struct vsp1_device *vsp1) | |
736 | { | |
737 | unsigned int i; | |
738 | ||
739 | /* Resume pipeline all running pipelines. */ | |
740 | for (i = 0; i < vsp1->pdata.wpf_count; ++i) { | |
741 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
742 | struct vsp1_pipeline *pipe; | |
743 | ||
744 | if (wpf == NULL) | |
745 | continue; | |
746 | ||
747 | pipe = to_vsp1_pipeline(&wpf->entity.subdev.entity); | |
748 | if (pipe == NULL) | |
749 | continue; | |
750 | ||
751 | if (vsp1_pipeline_ready(pipe)) | |
752 | vsp1_pipeline_run(pipe); | |
753 | } | |
754 | } | |
755 | ||
26e0ca22 LP |
756 | /* ----------------------------------------------------------------------------- |
757 | * videobuf2 Queue Operations | |
758 | */ | |
759 | ||
760 | static int | |
df9ecb0c | 761 | vsp1_video_queue_setup(struct vb2_queue *vq, |
26e0ca22 LP |
762 | unsigned int *nbuffers, unsigned int *nplanes, |
763 | unsigned int sizes[], void *alloc_ctxs[]) | |
764 | { | |
765 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
df9ecb0c | 766 | const struct v4l2_pix_format_mplane *format = &video->format; |
26e0ca22 LP |
767 | unsigned int i; |
768 | ||
df9ecb0c HV |
769 | if (*nplanes) { |
770 | if (*nplanes != format->num_planes) | |
26e0ca22 LP |
771 | return -EINVAL; |
772 | ||
df9ecb0c HV |
773 | for (i = 0; i < *nplanes; i++) { |
774 | if (sizes[i] < format->plane_fmt[i].sizeimage) | |
775 | return -EINVAL; | |
776 | alloc_ctxs[i] = video->alloc_ctx; | |
777 | } | |
778 | return 0; | |
26e0ca22 LP |
779 | } |
780 | ||
781 | *nplanes = format->num_planes; | |
782 | ||
783 | for (i = 0; i < format->num_planes; ++i) { | |
784 | sizes[i] = format->plane_fmt[i].sizeimage; | |
785 | alloc_ctxs[i] = video->alloc_ctx; | |
786 | } | |
787 | ||
788 | return 0; | |
789 | } | |
790 | ||
791 | static int vsp1_video_buffer_prepare(struct vb2_buffer *vb) | |
792 | { | |
2d700715 | 793 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
26e0ca22 | 794 | struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue); |
2d700715 | 795 | struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vbuf); |
26e0ca22 LP |
796 | const struct v4l2_pix_format_mplane *format = &video->format; |
797 | unsigned int i; | |
798 | ||
799 | if (vb->num_planes < format->num_planes) | |
800 | return -EINVAL; | |
801 | ||
26e0ca22 LP |
802 | for (i = 0; i < vb->num_planes; ++i) { |
803 | buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i); | |
804 | buf->length[i] = vb2_plane_size(vb, i); | |
805 | ||
806 | if (buf->length[i] < format->plane_fmt[i].sizeimage) | |
807 | return -EINVAL; | |
808 | } | |
809 | ||
810 | return 0; | |
811 | } | |
812 | ||
813 | static void vsp1_video_buffer_queue(struct vb2_buffer *vb) | |
814 | { | |
2d700715 | 815 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
26e0ca22 LP |
816 | struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue); |
817 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); | |
2d700715 | 818 | struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vbuf); |
26e0ca22 LP |
819 | unsigned long flags; |
820 | bool empty; | |
821 | ||
822 | spin_lock_irqsave(&video->irqlock, flags); | |
823 | empty = list_empty(&video->irqqueue); | |
824 | list_add_tail(&buf->queue, &video->irqqueue); | |
825 | spin_unlock_irqrestore(&video->irqlock, flags); | |
826 | ||
827 | if (!empty) | |
828 | return; | |
829 | ||
830 | spin_lock_irqsave(&pipe->irqlock, flags); | |
831 | ||
832 | video->ops->queue(video, buf); | |
833 | pipe->buffers_ready |= 1 << video->pipe_index; | |
834 | ||
835 | if (vb2_is_streaming(&video->queue) && | |
836 | vsp1_pipeline_ready(pipe)) | |
837 | vsp1_pipeline_run(pipe); | |
838 | ||
839 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
840 | } | |
841 | ||
842 | static void vsp1_entity_route_setup(struct vsp1_entity *source) | |
843 | { | |
844 | struct vsp1_entity *sink; | |
845 | ||
d9b45ed3 | 846 | if (source->route->reg == 0) |
26e0ca22 LP |
847 | return; |
848 | ||
849 | sink = container_of(source->sink, struct vsp1_entity, subdev.entity); | |
d9b45ed3 LP |
850 | vsp1_write(source->vsp1, source->route->reg, |
851 | sink->route->inputs[source->sink_pad]); | |
26e0ca22 LP |
852 | } |
853 | ||
854 | static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count) | |
855 | { | |
856 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
857 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); | |
858 | struct vsp1_entity *entity; | |
859 | unsigned long flags; | |
860 | int ret; | |
861 | ||
862 | mutex_lock(&pipe->lock); | |
863 | if (pipe->stream_count == pipe->num_video - 1) { | |
bdc2df62 LP |
864 | if (pipe->uds) { |
865 | struct vsp1_uds *uds = to_uds(&pipe->uds->subdev); | |
866 | ||
867 | /* If a BRU is present in the pipeline before the UDS, | |
868 | * the alpha component doesn't need to be scaled as the | |
869 | * BRU output alpha value is fixed to 255. Otherwise we | |
870 | * need to scale the alpha component only when available | |
871 | * at the input RPF. | |
872 | */ | |
873 | if (pipe->uds_input->type == VSP1_ENTITY_BRU) { | |
874 | uds->scale_alpha = false; | |
875 | } else { | |
876 | struct vsp1_rwpf *rpf = | |
877 | to_rwpf(&pipe->uds_input->subdev); | |
878 | ||
879 | uds->scale_alpha = rpf->video.fmtinfo->alpha; | |
880 | } | |
881 | } | |
882 | ||
26e0ca22 LP |
883 | list_for_each_entry(entity, &pipe->entities, list_pipe) { |
884 | vsp1_entity_route_setup(entity); | |
885 | ||
886 | ret = v4l2_subdev_call(&entity->subdev, video, | |
887 | s_stream, 1); | |
888 | if (ret < 0) { | |
889 | mutex_unlock(&pipe->lock); | |
890 | return ret; | |
891 | } | |
892 | } | |
893 | } | |
894 | ||
895 | pipe->stream_count++; | |
896 | mutex_unlock(&pipe->lock); | |
897 | ||
898 | spin_lock_irqsave(&pipe->irqlock, flags); | |
899 | if (vsp1_pipeline_ready(pipe)) | |
900 | vsp1_pipeline_run(pipe); | |
901 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
902 | ||
903 | return 0; | |
904 | } | |
905 | ||
e37559b2 | 906 | static void vsp1_video_stop_streaming(struct vb2_queue *vq) |
26e0ca22 LP |
907 | { |
908 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
909 | struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity); | |
9df04e9d | 910 | struct vsp1_video_buffer *buffer; |
26e0ca22 LP |
911 | unsigned long flags; |
912 | int ret; | |
913 | ||
914 | mutex_lock(&pipe->lock); | |
915 | if (--pipe->stream_count == 0) { | |
916 | /* Stop the pipeline. */ | |
917 | ret = vsp1_pipeline_stop(pipe); | |
918 | if (ret == -ETIMEDOUT) | |
919 | dev_err(video->vsp1->dev, "pipeline stop timeout\n"); | |
920 | } | |
921 | mutex_unlock(&pipe->lock); | |
922 | ||
923 | vsp1_pipeline_cleanup(pipe); | |
924 | media_entity_pipeline_stop(&video->video.entity); | |
925 | ||
926 | /* Remove all buffers from the IRQ queue. */ | |
927 | spin_lock_irqsave(&video->irqlock, flags); | |
9df04e9d | 928 | list_for_each_entry(buffer, &video->irqqueue, queue) |
2d700715 | 929 | vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR); |
26e0ca22 LP |
930 | INIT_LIST_HEAD(&video->irqqueue); |
931 | spin_unlock_irqrestore(&video->irqlock, flags); | |
26e0ca22 LP |
932 | } |
933 | ||
934 | static struct vb2_ops vsp1_video_queue_qops = { | |
935 | .queue_setup = vsp1_video_queue_setup, | |
936 | .buf_prepare = vsp1_video_buffer_prepare, | |
937 | .buf_queue = vsp1_video_buffer_queue, | |
938 | .wait_prepare = vb2_ops_wait_prepare, | |
939 | .wait_finish = vb2_ops_wait_finish, | |
940 | .start_streaming = vsp1_video_start_streaming, | |
941 | .stop_streaming = vsp1_video_stop_streaming, | |
942 | }; | |
943 | ||
944 | /* ----------------------------------------------------------------------------- | |
945 | * V4L2 ioctls | |
946 | */ | |
947 | ||
948 | static int | |
949 | vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap) | |
950 | { | |
951 | struct v4l2_fh *vfh = file->private_data; | |
952 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
953 | ||
954 | cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING | |
955 | | V4L2_CAP_VIDEO_CAPTURE_MPLANE | |
956 | | V4L2_CAP_VIDEO_OUTPUT_MPLANE; | |
957 | ||
958 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | |
959 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | |
960 | | V4L2_CAP_STREAMING; | |
961 | else | |
962 | cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE | |
963 | | V4L2_CAP_STREAMING; | |
964 | ||
965 | strlcpy(cap->driver, "vsp1", sizeof(cap->driver)); | |
966 | strlcpy(cap->card, video->video.name, sizeof(cap->card)); | |
967 | snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", | |
968 | dev_name(video->vsp1->dev)); | |
969 | ||
970 | return 0; | |
971 | } | |
972 | ||
973 | static int | |
974 | vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format) | |
975 | { | |
976 | struct v4l2_fh *vfh = file->private_data; | |
977 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
978 | ||
979 | if (format->type != video->queue.type) | |
980 | return -EINVAL; | |
981 | ||
982 | mutex_lock(&video->lock); | |
983 | format->fmt.pix_mp = video->format; | |
984 | mutex_unlock(&video->lock); | |
985 | ||
986 | return 0; | |
987 | } | |
988 | ||
989 | static int | |
990 | vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format) | |
991 | { | |
992 | struct v4l2_fh *vfh = file->private_data; | |
993 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
994 | ||
995 | if (format->type != video->queue.type) | |
996 | return -EINVAL; | |
997 | ||
998 | return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL); | |
999 | } | |
1000 | ||
1001 | static int | |
1002 | vsp1_video_set_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 | const struct vsp1_format_info *info; | |
1007 | int ret; | |
1008 | ||
1009 | if (format->type != video->queue.type) | |
1010 | return -EINVAL; | |
1011 | ||
1012 | ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info); | |
1013 | if (ret < 0) | |
1014 | return ret; | |
1015 | ||
1016 | mutex_lock(&video->lock); | |
1017 | ||
1018 | if (vb2_is_busy(&video->queue)) { | |
1019 | ret = -EBUSY; | |
1020 | goto done; | |
1021 | } | |
1022 | ||
1023 | video->format = format->fmt.pix_mp; | |
1024 | video->fmtinfo = info; | |
1025 | ||
1026 | done: | |
1027 | mutex_unlock(&video->lock); | |
1028 | return ret; | |
1029 | } | |
1030 | ||
1031 | static int | |
1032 | vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type) | |
1033 | { | |
1034 | struct v4l2_fh *vfh = file->private_data; | |
1035 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
1036 | struct vsp1_pipeline *pipe; | |
1037 | int ret; | |
1038 | ||
26e0ca22 LP |
1039 | if (video->queue.owner && video->queue.owner != file->private_data) |
1040 | return -EBUSY; | |
1041 | ||
1042 | video->sequence = 0; | |
1043 | ||
1044 | /* Start streaming on the pipeline. No link touching an entity in the | |
1045 | * pipeline can be activated or deactivated once streaming is started. | |
1046 | * | |
1047 | * Use the VSP1 pipeline object embedded in the first video object that | |
1048 | * starts streaming. | |
1049 | */ | |
1050 | pipe = video->video.entity.pipe | |
1051 | ? to_vsp1_pipeline(&video->video.entity) : &video->pipe; | |
1052 | ||
1053 | ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe); | |
1054 | if (ret < 0) | |
1055 | return ret; | |
1056 | ||
1057 | /* Verify that the configured format matches the output of the connected | |
1058 | * subdev. | |
1059 | */ | |
1060 | ret = vsp1_video_verify_format(video); | |
1061 | if (ret < 0) | |
1062 | goto err_stop; | |
1063 | ||
1064 | ret = vsp1_pipeline_init(pipe, video); | |
1065 | if (ret < 0) | |
1066 | goto err_stop; | |
1067 | ||
1068 | /* Start the queue. */ | |
1069 | ret = vb2_streamon(&video->queue, type); | |
1070 | if (ret < 0) | |
1071 | goto err_cleanup; | |
1072 | ||
1073 | return 0; | |
1074 | ||
1075 | err_cleanup: | |
1076 | vsp1_pipeline_cleanup(pipe); | |
1077 | err_stop: | |
1078 | media_entity_pipeline_stop(&video->video.entity); | |
1079 | return ret; | |
1080 | } | |
1081 | ||
1082 | static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = { | |
1083 | .vidioc_querycap = vsp1_video_querycap, | |
1084 | .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format, | |
1085 | .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format, | |
1086 | .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format, | |
1087 | .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format, | |
1088 | .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format, | |
1089 | .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format, | |
1090 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
1091 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1092 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1093 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
1094 | .vidioc_create_bufs = vb2_ioctl_create_bufs, | |
1095 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
1096 | .vidioc_streamon = vsp1_video_streamon, | |
1097 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
1098 | }; | |
1099 | ||
1100 | /* ----------------------------------------------------------------------------- | |
1101 | * V4L2 File Operations | |
1102 | */ | |
1103 | ||
1104 | static int vsp1_video_open(struct file *file) | |
1105 | { | |
1106 | struct vsp1_video *video = video_drvdata(file); | |
1107 | struct v4l2_fh *vfh; | |
1108 | int ret = 0; | |
1109 | ||
1110 | vfh = kzalloc(sizeof(*vfh), GFP_KERNEL); | |
1111 | if (vfh == NULL) | |
1112 | return -ENOMEM; | |
1113 | ||
1114 | v4l2_fh_init(vfh, &video->video); | |
1115 | v4l2_fh_add(vfh); | |
1116 | ||
1117 | file->private_data = vfh; | |
1118 | ||
4c16d6a0 LP |
1119 | ret = vsp1_device_get(video->vsp1); |
1120 | if (ret < 0) { | |
26e0ca22 LP |
1121 | v4l2_fh_del(vfh); |
1122 | kfree(vfh); | |
1123 | } | |
1124 | ||
1125 | return ret; | |
1126 | } | |
1127 | ||
1128 | static int vsp1_video_release(struct file *file) | |
1129 | { | |
1130 | struct vsp1_video *video = video_drvdata(file); | |
1131 | struct v4l2_fh *vfh = file->private_data; | |
1132 | ||
1133 | mutex_lock(&video->lock); | |
1134 | if (video->queue.owner == vfh) { | |
1135 | vb2_queue_release(&video->queue); | |
1136 | video->queue.owner = NULL; | |
1137 | } | |
1138 | mutex_unlock(&video->lock); | |
1139 | ||
1140 | vsp1_device_put(video->vsp1); | |
1141 | ||
1142 | v4l2_fh_release(file); | |
1143 | ||
1144 | file->private_data = NULL; | |
1145 | ||
1146 | return 0; | |
1147 | } | |
1148 | ||
1149 | static struct v4l2_file_operations vsp1_video_fops = { | |
1150 | .owner = THIS_MODULE, | |
1151 | .unlocked_ioctl = video_ioctl2, | |
1152 | .open = vsp1_video_open, | |
1153 | .release = vsp1_video_release, | |
1154 | .poll = vb2_fop_poll, | |
1155 | .mmap = vb2_fop_mmap, | |
1156 | }; | |
1157 | ||
1158 | /* ----------------------------------------------------------------------------- | |
1159 | * Initialization and Cleanup | |
1160 | */ | |
1161 | ||
1162 | int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf) | |
1163 | { | |
1164 | const char *direction; | |
1165 | int ret; | |
1166 | ||
1167 | switch (video->type) { | |
1168 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: | |
1169 | direction = "output"; | |
1170 | video->pad.flags = MEDIA_PAD_FL_SINK; | |
1171 | break; | |
1172 | ||
1173 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: | |
1174 | direction = "input"; | |
1175 | video->pad.flags = MEDIA_PAD_FL_SOURCE; | |
1176 | video->video.vfl_dir = VFL_DIR_TX; | |
1177 | break; | |
1178 | ||
1179 | default: | |
1180 | return -EINVAL; | |
1181 | } | |
1182 | ||
1183 | video->rwpf = rwpf; | |
1184 | ||
1185 | mutex_init(&video->lock); | |
1186 | spin_lock_init(&video->irqlock); | |
1187 | INIT_LIST_HEAD(&video->irqqueue); | |
1188 | ||
1189 | mutex_init(&video->pipe.lock); | |
1190 | spin_lock_init(&video->pipe.irqlock); | |
1191 | INIT_LIST_HEAD(&video->pipe.entities); | |
1192 | init_waitqueue_head(&video->pipe.wq); | |
1193 | video->pipe.state = VSP1_PIPELINE_STOPPED; | |
1194 | ||
1195 | /* Initialize the media entity... */ | |
18095107 | 1196 | ret = media_entity_init(&video->video.entity, 1, &video->pad); |
26e0ca22 LP |
1197 | if (ret < 0) |
1198 | return ret; | |
1199 | ||
1200 | /* ... and the format ... */ | |
1201 | video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT); | |
1202 | video->format.pixelformat = video->fmtinfo->fourcc; | |
1203 | video->format.colorspace = V4L2_COLORSPACE_SRGB; | |
1204 | video->format.field = V4L2_FIELD_NONE; | |
1205 | video->format.width = VSP1_VIDEO_DEF_WIDTH; | |
1206 | video->format.height = VSP1_VIDEO_DEF_HEIGHT; | |
1207 | video->format.num_planes = 1; | |
1208 | video->format.plane_fmt[0].bytesperline = | |
1209 | video->format.width * video->fmtinfo->bpp[0] / 8; | |
1210 | video->format.plane_fmt[0].sizeimage = | |
1211 | video->format.plane_fmt[0].bytesperline * video->format.height; | |
1212 | ||
1213 | /* ... and the video node... */ | |
1214 | video->video.v4l2_dev = &video->vsp1->v4l2_dev; | |
1215 | video->video.fops = &vsp1_video_fops; | |
1216 | snprintf(video->video.name, sizeof(video->video.name), "%s %s", | |
1217 | rwpf->subdev.name, direction); | |
1218 | video->video.vfl_type = VFL_TYPE_GRABBER; | |
1219 | video->video.release = video_device_release_empty; | |
1220 | video->video.ioctl_ops = &vsp1_video_ioctl_ops; | |
1221 | ||
1222 | video_set_drvdata(&video->video, video); | |
1223 | ||
1224 | /* ... and the buffers queue... */ | |
1225 | video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev); | |
b317828b WY |
1226 | if (IS_ERR(video->alloc_ctx)) { |
1227 | ret = PTR_ERR(video->alloc_ctx); | |
26e0ca22 | 1228 | goto error; |
b317828b | 1229 | } |
26e0ca22 LP |
1230 | |
1231 | video->queue.type = video->type; | |
1232 | video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; | |
1233 | video->queue.lock = &video->lock; | |
1234 | video->queue.drv_priv = video; | |
1235 | video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer); | |
1236 | video->queue.ops = &vsp1_video_queue_qops; | |
1237 | video->queue.mem_ops = &vb2_dma_contig_memops; | |
ade48681 | 1238 | video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
26e0ca22 LP |
1239 | ret = vb2_queue_init(&video->queue); |
1240 | if (ret < 0) { | |
1241 | dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n"); | |
1242 | goto error; | |
1243 | } | |
1244 | ||
1245 | /* ... and register the video device. */ | |
1246 | video->video.queue = &video->queue; | |
1247 | ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1); | |
1248 | if (ret < 0) { | |
1249 | dev_err(video->vsp1->dev, "failed to register video device\n"); | |
1250 | goto error; | |
1251 | } | |
1252 | ||
1253 | return 0; | |
1254 | ||
1255 | error: | |
1256 | vb2_dma_contig_cleanup_ctx(video->alloc_ctx); | |
1257 | vsp1_video_cleanup(video); | |
1258 | return ret; | |
1259 | } | |
1260 | ||
1261 | void vsp1_video_cleanup(struct vsp1_video *video) | |
1262 | { | |
1263 | if (video_is_registered(&video->video)) | |
1264 | video_unregister_device(&video->video); | |
1265 | ||
1266 | vb2_dma_contig_cleanup_ctx(video->alloc_ctx); | |
1267 | media_entity_cleanup(&video->video.entity); | |
1268 | } |