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