Commit | Line | Data |
---|---|---|
1c4b5f49 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
26e0ca22 LP |
2 | /* |
3 | * vsp1_video.c -- R-Car VSP1 Video Node | |
4 | * | |
139c9286 | 5 | * Copyright (C) 2013-2015 Renesas Electronics Corporation |
26e0ca22 LP |
6 | * |
7 | * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com) | |
26e0ca22 LP |
8 | */ |
9 | ||
10 | #include <linux/list.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/mutex.h> | |
26e0ca22 LP |
13 | #include <linux/slab.h> |
14 | #include <linux/v4l2-mediabus.h> | |
15 | #include <linux/videodev2.h> | |
dba4a180 | 16 | #include <linux/wait.h> |
26e0ca22 LP |
17 | |
18 | #include <media/media-entity.h> | |
19 | #include <media/v4l2-dev.h> | |
20 | #include <media/v4l2-fh.h> | |
21 | #include <media/v4l2-ioctl.h> | |
22 | #include <media/v4l2-subdev.h> | |
c139990e | 23 | #include <media/videobuf2-v4l2.h> |
26e0ca22 LP |
24 | #include <media/videobuf2-dma-contig.h> |
25 | ||
26 | #include "vsp1.h" | |
cbb7fa49 | 27 | #include "vsp1_brx.h" |
351bbf99 | 28 | #include "vsp1_dl.h" |
26e0ca22 | 29 | #include "vsp1_entity.h" |
f2421521 | 30 | #include "vsp1_hgo.h" |
0ac702d5 | 31 | #include "vsp1_hgt.h" |
dba4a180 | 32 | #include "vsp1_pipe.h" |
26e0ca22 | 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 | ||
26e0ca22 | 41 | #define VSP1_VIDEO_MAX_WIDTH 8190U |
26e0ca22 LP |
42 | #define VSP1_VIDEO_MAX_HEIGHT 8190U |
43 | ||
44 | /* ----------------------------------------------------------------------------- | |
45 | * Helper functions | |
46 | */ | |
47 | ||
26e0ca22 LP |
48 | static struct v4l2_subdev * |
49 | vsp1_video_remote_subdev(struct media_pad *local, u32 *pad) | |
50 | { | |
51 | struct media_pad *remote; | |
52 | ||
53 | remote = media_entity_remote_pad(local); | |
3efdf62c | 54 | if (!remote || !is_media_entity_v4l2_subdev(remote->entity)) |
26e0ca22 LP |
55 | return NULL; |
56 | ||
57 | if (pad) | |
58 | *pad = remote->index; | |
59 | ||
60 | return media_entity_to_v4l2_subdev(remote->entity); | |
61 | } | |
62 | ||
63 | static int vsp1_video_verify_format(struct vsp1_video *video) | |
64 | { | |
65 | struct v4l2_subdev_format fmt; | |
66 | struct v4l2_subdev *subdev; | |
67 | int ret; | |
68 | ||
69 | subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad); | |
70 | if (subdev == NULL) | |
71 | return -EINVAL; | |
72 | ||
73 | fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; | |
74 | ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); | |
75 | if (ret < 0) | |
76 | return ret == -ENOIOCTLCMD ? -EINVAL : ret; | |
77 | ||
86960eec LP |
78 | if (video->rwpf->fmtinfo->mbus != fmt.format.code || |
79 | video->rwpf->format.height != fmt.format.height || | |
80 | video->rwpf->format.width != fmt.format.width) | |
26e0ca22 LP |
81 | return -EINVAL; |
82 | ||
83 | return 0; | |
84 | } | |
85 | ||
86 | static int __vsp1_video_try_format(struct vsp1_video *video, | |
87 | struct v4l2_pix_format_mplane *pix, | |
88 | const struct vsp1_format_info **fmtinfo) | |
89 | { | |
56bfef3e LP |
90 | static const u32 xrgb_formats[][2] = { |
91 | { V4L2_PIX_FMT_RGB444, V4L2_PIX_FMT_XRGB444 }, | |
92 | { V4L2_PIX_FMT_RGB555, V4L2_PIX_FMT_XRGB555 }, | |
93 | { V4L2_PIX_FMT_BGR32, V4L2_PIX_FMT_XBGR32 }, | |
94 | { V4L2_PIX_FMT_RGB32, V4L2_PIX_FMT_XRGB32 }, | |
95 | }; | |
96 | ||
26e0ca22 LP |
97 | const struct vsp1_format_info *info; |
98 | unsigned int width = pix->width; | |
99 | unsigned int height = pix->height; | |
100 | unsigned int i; | |
101 | ||
9dbed95b LP |
102 | /* |
103 | * Backward compatibility: replace deprecated RGB formats by their XRGB | |
56bfef3e LP |
104 | * equivalent. This selects the format older userspace applications want |
105 | * while still exposing the new format. | |
106 | */ | |
107 | for (i = 0; i < ARRAY_SIZE(xrgb_formats); ++i) { | |
108 | if (xrgb_formats[i][0] == pix->pixelformat) { | |
109 | pix->pixelformat = xrgb_formats[i][1]; | |
110 | break; | |
111 | } | |
112 | } | |
113 | ||
9dbed95b LP |
114 | /* |
115 | * Retrieve format information and select the default format if the | |
26e0ca22 LP |
116 | * requested format isn't supported. |
117 | */ | |
c9f49607 | 118 | info = vsp1_get_format_info(video->vsp1, pix->pixelformat); |
26e0ca22 | 119 | if (info == NULL) |
c9f49607 | 120 | info = vsp1_get_format_info(video->vsp1, VSP1_VIDEO_DEF_FORMAT); |
26e0ca22 LP |
121 | |
122 | pix->pixelformat = info->fourcc; | |
123 | pix->colorspace = V4L2_COLORSPACE_SRGB; | |
124 | pix->field = V4L2_FIELD_NONE; | |
bc9b91e6 LP |
125 | |
126 | if (info->fourcc == V4L2_PIX_FMT_HSV24 || | |
127 | info->fourcc == V4L2_PIX_FMT_HSV32) | |
128 | pix->hsv_enc = V4L2_HSV_ENC_256; | |
129 | ||
26e0ca22 LP |
130 | memset(pix->reserved, 0, sizeof(pix->reserved)); |
131 | ||
132 | /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */ | |
133 | width = round_down(width, info->hsub); | |
134 | height = round_down(height, info->vsub); | |
135 | ||
136 | /* Clamp the width and height. */ | |
8a7db647 KB |
137 | pix->width = clamp(width, info->hsub, VSP1_VIDEO_MAX_WIDTH); |
138 | pix->height = clamp(height, info->vsub, VSP1_VIDEO_MAX_HEIGHT); | |
26e0ca22 | 139 | |
9dbed95b LP |
140 | /* |
141 | * Compute and clamp the stride and image size. While not documented in | |
26e0ca22 LP |
142 | * the datasheet, strides not aligned to a multiple of 128 bytes result |
143 | * in image corruption. | |
144 | */ | |
df5c3e7c | 145 | for (i = 0; i < min(info->planes, 2U); ++i) { |
26e0ca22 LP |
146 | unsigned int hsub = i > 0 ? info->hsub : 1; |
147 | unsigned int vsub = i > 0 ? info->vsub : 1; | |
148 | unsigned int align = 128; | |
149 | unsigned int bpl; | |
150 | ||
151 | bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline, | |
152 | pix->width / hsub * info->bpp[i] / 8, | |
153 | round_down(65535U, align)); | |
154 | ||
155 | pix->plane_fmt[i].bytesperline = round_up(bpl, align); | |
156 | pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline | |
157 | * pix->height / vsub; | |
158 | } | |
159 | ||
160 | if (info->planes == 3) { | |
161 | /* The second and third planes must have the same stride. */ | |
162 | pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline; | |
163 | pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage; | |
164 | } | |
165 | ||
166 | pix->num_planes = info->planes; | |
167 | ||
168 | if (fmtinfo) | |
169 | *fmtinfo = info; | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
df32c924 KB |
174 | /* ----------------------------------------------------------------------------- |
175 | * VSP1 Partition Algorithm support | |
176 | */ | |
177 | ||
b6187392 | 178 | /** |
40650268 | 179 | * vsp1_video_calculate_partition - Calculate the active partition output window |
fc6e514a | 180 | * |
40650268 KB |
181 | * @pipe: the pipeline |
182 | * @partition: partition that will hold the calculated values | |
fc6e514a KB |
183 | * @div_size: pre-determined maximum partition division size |
184 | * @index: partition index | |
fc6e514a | 185 | */ |
40650268 KB |
186 | static void vsp1_video_calculate_partition(struct vsp1_pipeline *pipe, |
187 | struct vsp1_partition *partition, | |
188 | unsigned int div_size, | |
189 | unsigned int index) | |
fc6e514a KB |
190 | { |
191 | const struct v4l2_mbus_framefmt *format; | |
ab45e858 | 192 | struct vsp1_partition_window window; |
fc6e514a KB |
193 | unsigned int modulus; |
194 | ||
3e9a0e0b LP |
195 | /* |
196 | * Partitions are computed on the size before rotation, use the format | |
197 | * at the WPF sink. | |
198 | */ | |
fc6e514a KB |
199 | format = vsp1_entity_get_pad_format(&pipe->output->entity, |
200 | pipe->output->entity.config, | |
3e9a0e0b | 201 | RWPF_PAD_SINK); |
fc6e514a KB |
202 | |
203 | /* A single partition simply processes the output size in full. */ | |
204 | if (pipe->partitions <= 1) { | |
ab45e858 KB |
205 | window.left = 0; |
206 | window.width = format->width; | |
207 | ||
208 | vsp1_pipeline_propagate_partition(pipe, partition, index, | |
209 | &window); | |
40650268 | 210 | return; |
fc6e514a KB |
211 | } |
212 | ||
213 | /* Initialise the partition with sane starting conditions. */ | |
ab45e858 KB |
214 | window.left = index * div_size; |
215 | window.width = div_size; | |
fc6e514a KB |
216 | |
217 | modulus = format->width % div_size; | |
218 | ||
b6187392 MCC |
219 | /* |
220 | * We need to prevent the last partition from being smaller than the | |
fc6e514a KB |
221 | * *minimum* width of the hardware capabilities. |
222 | * | |
223 | * If the modulus is less than half of the partition size, | |
224 | * the penultimate partition is reduced to half, which is added | |
225 | * to the final partition: |1234|1234|1234|12|341| | |
226 | * to prevents this: |1234|1234|1234|1234|1|. | |
227 | */ | |
228 | if (modulus) { | |
b6187392 MCC |
229 | /* |
230 | * pipe->partitions is 1 based, whilst index is a 0 based index. | |
fc6e514a KB |
231 | * Normalise this locally. |
232 | */ | |
233 | unsigned int partitions = pipe->partitions - 1; | |
234 | ||
235 | if (modulus < div_size / 2) { | |
236 | if (index == partitions - 1) { | |
237 | /* Halve the penultimate partition. */ | |
ab45e858 | 238 | window.width = div_size / 2; |
fc6e514a KB |
239 | } else if (index == partitions) { |
240 | /* Increase the final partition. */ | |
ab45e858 KB |
241 | window.width = (div_size / 2) + modulus; |
242 | window.left -= div_size / 2; | |
fc6e514a KB |
243 | } |
244 | } else if (index == partitions) { | |
ab45e858 | 245 | window.width = modulus; |
fc6e514a KB |
246 | } |
247 | } | |
248 | ||
ab45e858 | 249 | vsp1_pipeline_propagate_partition(pipe, partition, index, &window); |
fc6e514a KB |
250 | } |
251 | ||
25b6beab | 252 | static int vsp1_video_pipeline_setup_partitions(struct vsp1_pipeline *pipe) |
801c6a17 KB |
253 | { |
254 | struct vsp1_device *vsp1 = pipe->output->entity.vsp1; | |
255 | const struct v4l2_mbus_framefmt *format; | |
256 | struct vsp1_entity *entity; | |
257 | unsigned int div_size; | |
25b6beab | 258 | unsigned int i; |
801c6a17 KB |
259 | |
260 | /* | |
261 | * Partitions are computed on the size before rotation, use the format | |
262 | * at the WPF sink. | |
263 | */ | |
264 | format = vsp1_entity_get_pad_format(&pipe->output->entity, | |
265 | pipe->output->entity.config, | |
266 | RWPF_PAD_SINK); | |
267 | div_size = format->width; | |
268 | ||
25b6beab KB |
269 | /* |
270 | * Only Gen3 hardware requires image partitioning, Gen2 will operate | |
271 | * with a single partition that covers the whole output. | |
272 | */ | |
273 | if (vsp1->info->gen == 3) { | |
274 | list_for_each_entry(entity, &pipe->entities, list_pipe) { | |
275 | unsigned int entity_max; | |
801c6a17 | 276 | |
25b6beab KB |
277 | if (!entity->ops->max_width) |
278 | continue; | |
801c6a17 | 279 | |
801c6a17 KB |
280 | entity_max = entity->ops->max_width(entity, pipe); |
281 | if (entity_max) | |
282 | div_size = min(div_size, entity_max); | |
283 | } | |
284 | } | |
285 | ||
801c6a17 | 286 | pipe->partitions = DIV_ROUND_UP(format->width, div_size); |
25b6beab KB |
287 | pipe->part_table = kcalloc(pipe->partitions, sizeof(*pipe->part_table), |
288 | GFP_KERNEL); | |
289 | if (!pipe->part_table) | |
290 | return -ENOMEM; | |
291 | ||
292 | for (i = 0; i < pipe->partitions; ++i) | |
40650268 KB |
293 | vsp1_video_calculate_partition(pipe, &pipe->part_table[i], |
294 | div_size, i); | |
25b6beab KB |
295 | |
296 | return 0; | |
fc6e514a KB |
297 | } |
298 | ||
26e0ca22 LP |
299 | /* ----------------------------------------------------------------------------- |
300 | * Pipeline Management | |
301 | */ | |
302 | ||
76c29755 LP |
303 | /* |
304 | * vsp1_video_complete_buffer - Complete the current buffer | |
305 | * @video: the video node | |
306 | * | |
307 | * This function completes the current buffer by filling its sequence number, | |
308 | * time stamp and payload size, and hands it back to the videobuf core. | |
309 | * | |
310 | * When operating in DU output mode (deep pipeline to the DU through the LIF), | |
311 | * the VSP1 needs to constantly supply frames to the display. In that case, if | |
312 | * no other buffer is queued, reuse the one that has just been processed instead | |
313 | * of handing it back to the videobuf core. | |
314 | * | |
315 | * Return the next queued buffer or NULL if the queue is empty. | |
316 | */ | |
317 | static struct vsp1_vb2_buffer * | |
318 | vsp1_video_complete_buffer(struct vsp1_video *video) | |
319 | { | |
1ccbb32c | 320 | struct vsp1_pipeline *pipe = video->rwpf->entity.pipe; |
76c29755 LP |
321 | struct vsp1_vb2_buffer *next = NULL; |
322 | struct vsp1_vb2_buffer *done; | |
323 | unsigned long flags; | |
324 | unsigned int i; | |
325 | ||
326 | spin_lock_irqsave(&video->irqlock, flags); | |
327 | ||
328 | if (list_empty(&video->irqqueue)) { | |
329 | spin_unlock_irqrestore(&video->irqlock, flags); | |
330 | return NULL; | |
331 | } | |
332 | ||
333 | done = list_first_entry(&video->irqqueue, | |
334 | struct vsp1_vb2_buffer, queue); | |
335 | ||
336 | /* In DU output mode reuse the buffer if the list is singular. */ | |
337 | if (pipe->lif && list_is_singular(&video->irqqueue)) { | |
338 | spin_unlock_irqrestore(&video->irqlock, flags); | |
339 | return done; | |
340 | } | |
341 | ||
342 | list_del(&done->queue); | |
343 | ||
344 | if (!list_empty(&video->irqqueue)) | |
345 | next = list_first_entry(&video->irqqueue, | |
346 | struct vsp1_vb2_buffer, queue); | |
347 | ||
348 | spin_unlock_irqrestore(&video->irqlock, flags); | |
349 | ||
0c1a41b5 | 350 | done->buf.sequence = pipe->sequence; |
76c29755 LP |
351 | done->buf.vb2_buf.timestamp = ktime_get_ns(); |
352 | for (i = 0; i < done->buf.vb2_buf.num_planes; ++i) | |
353 | vb2_set_plane_payload(&done->buf.vb2_buf, i, | |
354 | vb2_plane_size(&done->buf.vb2_buf, i)); | |
355 | vb2_buffer_done(&done->buf.vb2_buf, VB2_BUF_STATE_DONE); | |
356 | ||
357 | return next; | |
358 | } | |
359 | ||
360 | static void vsp1_video_frame_end(struct vsp1_pipeline *pipe, | |
361 | struct vsp1_rwpf *rwpf) | |
362 | { | |
363 | struct vsp1_video *video = rwpf->video; | |
364 | struct vsp1_vb2_buffer *buf; | |
76c29755 LP |
365 | |
366 | buf = vsp1_video_complete_buffer(video); | |
367 | if (buf == NULL) | |
368 | return; | |
369 | ||
76c29755 LP |
370 | video->rwpf->mem = buf->mem; |
371 | pipe->buffers_ready |= 1 << video->pipe_index; | |
76c29755 LP |
372 | } |
373 | ||
fc6e514a | 374 | static void vsp1_video_pipeline_run_partition(struct vsp1_pipeline *pipe, |
62dad91c KB |
375 | struct vsp1_dl_list *dl, |
376 | unsigned int partition) | |
fc6e514a | 377 | { |
12832dd9 | 378 | struct vsp1_dl_body *dlb = vsp1_dl_list_get_body0(dl); |
fc6e514a KB |
379 | struct vsp1_entity *entity; |
380 | ||
40650268 | 381 | pipe->partition = &pipe->part_table[partition]; |
fc6e514a | 382 | |
46ce3639 | 383 | list_for_each_entry(entity, &pipe->entities, list_pipe) |
12832dd9 | 384 | vsp1_entity_configure_partition(entity, pipe, dl, dlb); |
fc6e514a KB |
385 | } |
386 | ||
76c29755 LP |
387 | static void vsp1_video_pipeline_run(struct vsp1_pipeline *pipe) |
388 | { | |
fc6e514a | 389 | struct vsp1_device *vsp1 = pipe->output->entity.vsp1; |
fc845e52 | 390 | struct vsp1_entity *entity; |
12832dd9 | 391 | struct vsp1_dl_body *dlb; |
e646e177 | 392 | struct vsp1_dl_list *dl; |
62dad91c | 393 | unsigned int partition; |
76c29755 | 394 | |
e646e177 | 395 | dl = vsp1_dl_list_get(pipe->output->dlm); |
76c29755 | 396 | |
e646e177 KB |
397 | /* |
398 | * If the VSP hardware isn't configured yet (which occurs either when | |
399 | * processing the first frame or after a system suspend/resume), add the | |
400 | * cached stream configuration to the display list to perform a full | |
401 | * initialisation. | |
402 | */ | |
403 | if (!pipe->configured) | |
404 | vsp1_dl_list_add_body(dl, pipe->stream_config); | |
405 | ||
406 | dlb = vsp1_dl_list_get_body0(dl); | |
12832dd9 | 407 | |
46ce3639 | 408 | list_for_each_entry(entity, &pipe->entities, list_pipe) |
e646e177 | 409 | vsp1_entity_configure_frame(entity, pipe, dl, dlb); |
fc6e514a | 410 | |
46ce3639 | 411 | /* Run the first partition. */ |
e646e177 | 412 | vsp1_video_pipeline_run_partition(pipe, dl, 0); |
fc6e514a | 413 | |
46ce3639 | 414 | /* Process consecutive partitions as necessary. */ |
62dad91c | 415 | for (partition = 1; partition < pipe->partitions; ++partition) { |
e646e177 | 416 | struct vsp1_dl_list *dl_next; |
fc6e514a | 417 | |
e646e177 | 418 | dl_next = vsp1_dl_list_get(pipe->output->dlm); |
fc6e514a | 419 | |
b6187392 MCC |
420 | /* |
421 | * An incomplete chain will still function, but output only | |
fc6e514a KB |
422 | * the partitions that had a dl available. The frame end |
423 | * interrupt will be marked on the last dl in the chain. | |
424 | */ | |
e646e177 | 425 | if (!dl_next) { |
fc6e514a KB |
426 | dev_err(vsp1->dev, "Failed to obtain a dl list. Frame will be incomplete\n"); |
427 | break; | |
8ddf3784 | 428 | } |
fc6e514a | 429 | |
e646e177 KB |
430 | vsp1_video_pipeline_run_partition(pipe, dl_next, partition); |
431 | vsp1_dl_list_add_chain(dl, dl_next); | |
fc845e52 LP |
432 | } |
433 | ||
fc6e514a | 434 | /* Complete, and commit the head display list. */ |
e646e177 KB |
435 | vsp1_dl_list_commit(dl, false); |
436 | pipe->configured = true; | |
76c29755 LP |
437 | |
438 | vsp1_pipeline_run(pipe); | |
439 | } | |
440 | ||
5e0594fd | 441 | static void vsp1_video_pipeline_frame_end(struct vsp1_pipeline *pipe, |
bbc56faf | 442 | unsigned int completion) |
76c29755 LP |
443 | { |
444 | struct vsp1_device *vsp1 = pipe->output->entity.vsp1; | |
445 | enum vsp1_pipeline_state state; | |
446 | unsigned long flags; | |
447 | unsigned int i; | |
448 | ||
5e0594fd | 449 | /* M2M Pipelines should never call here with an incomplete frame. */ |
bbc56faf | 450 | WARN_ON_ONCE(!(completion & VSP1_DL_FRAME_END_COMPLETED)); |
5e0594fd | 451 | |
bfb4d5be KB |
452 | spin_lock_irqsave(&pipe->irqlock, flags); |
453 | ||
76c29755 LP |
454 | /* Complete buffers on all video nodes. */ |
455 | for (i = 0; i < vsp1->info->rpf_count; ++i) { | |
456 | if (!pipe->inputs[i]) | |
457 | continue; | |
458 | ||
459 | vsp1_video_frame_end(pipe, pipe->inputs[i]); | |
460 | } | |
461 | ||
462 | vsp1_video_frame_end(pipe, pipe->output); | |
463 | ||
76c29755 LP |
464 | state = pipe->state; |
465 | pipe->state = VSP1_PIPELINE_STOPPED; | |
466 | ||
9dbed95b LP |
467 | /* |
468 | * If a stop has been requested, mark the pipeline as stopped and | |
76c29755 LP |
469 | * return. Otherwise restart the pipeline if ready. |
470 | */ | |
471 | if (state == VSP1_PIPELINE_STOPPING) | |
472 | wake_up(&pipe->wq); | |
473 | else if (vsp1_pipeline_ready(pipe)) | |
474 | vsp1_video_pipeline_run(pipe); | |
475 | ||
476 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
477 | } | |
478 | ||
d2219824 LP |
479 | static int vsp1_video_pipeline_build_branch(struct vsp1_pipeline *pipe, |
480 | struct vsp1_rwpf *input, | |
481 | struct vsp1_rwpf *output) | |
26e0ca22 | 482 | { |
54b5a749 | 483 | struct media_entity_enum ent_enum; |
6b1446bc | 484 | struct vsp1_entity *entity; |
26e0ca22 | 485 | struct media_pad *pad; |
cbb7fa49 | 486 | struct vsp1_brx *brx = NULL; |
6b1446bc | 487 | int ret; |
26e0ca22 | 488 | |
6b1446bc LP |
489 | ret = media_entity_enum_init(&ent_enum, &input->entity.vsp1->media_dev); |
490 | if (ret < 0) | |
491 | return ret; | |
54b5a749 | 492 | |
c8663c8e LP |
493 | /* |
494 | * The main data path doesn't include the HGO or HGT, use | |
495 | * vsp1_entity_remote_pad() to traverse the graph. | |
496 | */ | |
497 | ||
498 | pad = vsp1_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]); | |
26e0ca22 LP |
499 | |
500 | while (1) { | |
54b5a749 | 501 | if (pad == NULL) { |
6b1446bc | 502 | ret = -EPIPE; |
54b5a749 SA |
503 | goto out; |
504 | } | |
26e0ca22 LP |
505 | |
506 | /* We've reached a video node, that shouldn't have happened. */ | |
54b5a749 | 507 | if (!is_media_entity_v4l2_subdev(pad->entity)) { |
6b1446bc | 508 | ret = -EPIPE; |
54b5a749 SA |
509 | goto out; |
510 | } | |
26e0ca22 | 511 | |
54b5a749 SA |
512 | entity = to_vsp1_entity( |
513 | media_entity_to_v4l2_subdev(pad->entity)); | |
26e0ca22 | 514 | |
9dbed95b | 515 | /* |
6134148f | 516 | * A BRU or BRS is present in the pipeline, store its input pad |
b7e5107e | 517 | * number in the input RPF for use when configuring the RPF. |
629bb6d4 | 518 | */ |
6134148f LP |
519 | if (entity->type == VSP1_ENTITY_BRU || |
520 | entity->type == VSP1_ENTITY_BRS) { | |
521 | /* BRU and BRS can't be chained. */ | |
cbb7fa49 | 522 | if (brx) { |
6134148f LP |
523 | ret = -EPIPE; |
524 | goto out; | |
525 | } | |
6418b4d6 | 526 | |
cbb7fa49 LP |
527 | brx = to_brx(&entity->subdev); |
528 | brx->inputs[pad->index].rpf = input; | |
529 | input->brx_input = pad->index; | |
629bb6d4 LP |
530 | } |
531 | ||
26e0ca22 LP |
532 | /* We've reached the WPF, we're done. */ |
533 | if (entity->type == VSP1_ENTITY_WPF) | |
534 | break; | |
535 | ||
536 | /* Ensure the branch has no loop. */ | |
54b5a749 SA |
537 | if (media_entity_enum_test_and_set(&ent_enum, |
538 | &entity->subdev.entity)) { | |
6b1446bc | 539 | ret = -EPIPE; |
54b5a749 SA |
540 | goto out; |
541 | } | |
26e0ca22 LP |
542 | |
543 | /* UDS can't be chained. */ | |
544 | if (entity->type == VSP1_ENTITY_UDS) { | |
54b5a749 | 545 | if (pipe->uds) { |
6b1446bc | 546 | ret = -EPIPE; |
54b5a749 SA |
547 | goto out; |
548 | } | |
bdc2df62 LP |
549 | |
550 | pipe->uds = entity; | |
cbb7fa49 | 551 | pipe->uds_input = brx ? &brx->entity : &input->entity; |
26e0ca22 LP |
552 | } |
553 | ||
c8663c8e | 554 | /* Follow the source link, ignoring any HGO or HGT. */ |
26e0ca22 | 555 | pad = &entity->pads[entity->source_pad]; |
c8663c8e | 556 | pad = vsp1_entity_remote_pad(pad); |
26e0ca22 LP |
557 | } |
558 | ||
559 | /* The last entity must be the output WPF. */ | |
560 | if (entity != &output->entity) | |
6b1446bc | 561 | ret = -EPIPE; |
26e0ca22 | 562 | |
54b5a749 SA |
563 | out: |
564 | media_entity_enum_cleanup(&ent_enum); | |
565 | ||
6b1446bc | 566 | return ret; |
26e0ca22 LP |
567 | } |
568 | ||
d2219824 LP |
569 | static int vsp1_video_pipeline_build(struct vsp1_pipeline *pipe, |
570 | struct vsp1_video *video) | |
26e0ca22 | 571 | { |
20b85227 | 572 | struct media_graph graph; |
26e0ca22 | 573 | struct media_entity *entity = &video->video.entity; |
d10c9894 | 574 | struct media_device *mdev = entity->graph_obj.mdev; |
26e0ca22 LP |
575 | unsigned int i; |
576 | int ret; | |
577 | ||
26e0ca22 | 578 | /* Walk the graph to locate the entities and video nodes. */ |
20b85227 | 579 | ret = media_graph_walk_init(&graph, mdev); |
a0cdac56 | 580 | if (ret) |
c1a5f1bc | 581 | return ret; |
c1a5f1bc | 582 | |
20b85227 | 583 | media_graph_walk_start(&graph, entity); |
26e0ca22 | 584 | |
20b85227 | 585 | while ((entity = media_graph_walk_next(&graph))) { |
26e0ca22 LP |
586 | struct v4l2_subdev *subdev; |
587 | struct vsp1_rwpf *rwpf; | |
588 | struct vsp1_entity *e; | |
589 | ||
02650ebd | 590 | if (!is_media_entity_v4l2_subdev(entity)) |
26e0ca22 | 591 | continue; |
26e0ca22 LP |
592 | |
593 | subdev = media_entity_to_v4l2_subdev(entity); | |
594 | e = to_vsp1_entity(subdev); | |
595 | list_add_tail(&e->list_pipe, &pipe->entities); | |
1ccbb32c | 596 | e->pipe = pipe; |
26e0ca22 | 597 | |
6134148f LP |
598 | switch (e->type) { |
599 | case VSP1_ENTITY_RPF: | |
26e0ca22 | 600 | rwpf = to_rwpf(subdev); |
96bfa6a5 LP |
601 | pipe->inputs[rwpf->entity.index] = rwpf; |
602 | rwpf->video->pipe_index = ++pipe->num_inputs; | |
6134148f LP |
603 | break; |
604 | ||
605 | case VSP1_ENTITY_WPF: | |
26e0ca22 | 606 | rwpf = to_rwpf(subdev); |
f8562f21 | 607 | pipe->output = rwpf; |
faf2644d | 608 | rwpf->video->pipe_index = 0; |
6134148f LP |
609 | break; |
610 | ||
611 | case VSP1_ENTITY_LIF: | |
26e0ca22 | 612 | pipe->lif = e; |
6134148f LP |
613 | break; |
614 | ||
615 | case VSP1_ENTITY_BRU: | |
616 | case VSP1_ENTITY_BRS: | |
cbb7fa49 | 617 | pipe->brx = e; |
6134148f | 618 | break; |
f2421521 | 619 | |
6134148f | 620 | case VSP1_ENTITY_HGO: |
f2421521 | 621 | pipe->hgo = e; |
6134148f | 622 | break; |
0ac702d5 | 623 | |
6134148f | 624 | case VSP1_ENTITY_HGT: |
0ac702d5 | 625 | pipe->hgt = e; |
6134148f LP |
626 | break; |
627 | ||
628 | default: | |
629 | break; | |
26e0ca22 LP |
630 | } |
631 | } | |
632 | ||
20b85227 | 633 | media_graph_walk_cleanup(&graph); |
c1a5f1bc | 634 | |
26e0ca22 | 635 | /* We need one output and at least one input. */ |
a0cdac56 LP |
636 | if (pipe->num_inputs == 0 || !pipe->output) |
637 | return -EPIPE; | |
26e0ca22 | 638 | |
9dbed95b LP |
639 | /* |
640 | * Follow links downstream for each input and make sure the graph | |
26e0ca22 LP |
641 | * contains no loop and that all branches end at the output WPF. |
642 | */ | |
5aa2eb3c | 643 | for (i = 0; i < video->vsp1->info->rpf_count; ++i) { |
96bfa6a5 LP |
644 | if (!pipe->inputs[i]) |
645 | continue; | |
646 | ||
d2219824 LP |
647 | ret = vsp1_video_pipeline_build_branch(pipe, pipe->inputs[i], |
648 | pipe->output); | |
26e0ca22 | 649 | if (ret < 0) |
a0cdac56 | 650 | return ret; |
26e0ca22 LP |
651 | } |
652 | ||
653 | return 0; | |
26e0ca22 LP |
654 | } |
655 | ||
945f1276 LP |
656 | static int vsp1_video_pipeline_init(struct vsp1_pipeline *pipe, |
657 | struct vsp1_video *video) | |
26e0ca22 | 658 | { |
a0cdac56 LP |
659 | vsp1_pipeline_init(pipe); |
660 | ||
661 | pipe->frame_end = vsp1_video_pipeline_frame_end; | |
662 | ||
663 | return vsp1_video_pipeline_build(pipe, video); | |
664 | } | |
665 | ||
666 | static struct vsp1_pipeline *vsp1_video_pipeline_get(struct vsp1_video *video) | |
667 | { | |
668 | struct vsp1_pipeline *pipe; | |
26e0ca22 LP |
669 | int ret; |
670 | ||
9dbed95b LP |
671 | /* |
672 | * Get a pipeline object for the video node. If a pipeline has already | |
a0cdac56 LP |
673 | * been allocated just increment its reference count and return it. |
674 | * Otherwise allocate a new pipeline and initialize it, it will be freed | |
675 | * when the last reference is released. | |
676 | */ | |
1ccbb32c | 677 | if (!video->rwpf->entity.pipe) { |
a0cdac56 LP |
678 | pipe = kzalloc(sizeof(*pipe), GFP_KERNEL); |
679 | if (!pipe) | |
680 | return ERR_PTR(-ENOMEM); | |
26e0ca22 | 681 | |
a0cdac56 LP |
682 | ret = vsp1_video_pipeline_init(pipe, video); |
683 | if (ret < 0) { | |
684 | vsp1_pipeline_reset(pipe); | |
685 | kfree(pipe); | |
686 | return ERR_PTR(ret); | |
687 | } | |
688 | } else { | |
1ccbb32c | 689 | pipe = video->rwpf->entity.pipe; |
a0cdac56 | 690 | kref_get(&pipe->kref); |
26e0ca22 LP |
691 | } |
692 | ||
a0cdac56 | 693 | return pipe; |
26e0ca22 LP |
694 | } |
695 | ||
a0cdac56 | 696 | static void vsp1_video_pipeline_release(struct kref *kref) |
26e0ca22 | 697 | { |
a0cdac56 | 698 | struct vsp1_pipeline *pipe = container_of(kref, typeof(*pipe), kref); |
26e0ca22 | 699 | |
a0cdac56 LP |
700 | vsp1_pipeline_reset(pipe); |
701 | kfree(pipe); | |
702 | } | |
26e0ca22 | 703 | |
a0cdac56 LP |
704 | static void vsp1_video_pipeline_put(struct vsp1_pipeline *pipe) |
705 | { | |
706 | struct media_device *mdev = &pipe->output->entity.vsp1->media_dev; | |
707 | ||
708 | mutex_lock(&mdev->graph_mutex); | |
709 | kref_put(&pipe->kref, vsp1_video_pipeline_release); | |
710 | mutex_unlock(&mdev->graph_mutex); | |
26e0ca22 LP |
711 | } |
712 | ||
26e0ca22 LP |
713 | /* ----------------------------------------------------------------------------- |
714 | * videobuf2 Queue Operations | |
715 | */ | |
716 | ||
717 | static int | |
df9ecb0c | 718 | vsp1_video_queue_setup(struct vb2_queue *vq, |
36c0f8b3 HV |
719 | unsigned int *nbuffers, unsigned int *nplanes, |
720 | unsigned int sizes[], struct device *alloc_devs[]) | |
26e0ca22 LP |
721 | { |
722 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
86960eec | 723 | const struct v4l2_pix_format_mplane *format = &video->rwpf->format; |
26e0ca22 LP |
724 | unsigned int i; |
725 | ||
df9ecb0c HV |
726 | if (*nplanes) { |
727 | if (*nplanes != format->num_planes) | |
26e0ca22 LP |
728 | return -EINVAL; |
729 | ||
36c0f8b3 | 730 | for (i = 0; i < *nplanes; i++) |
df9ecb0c HV |
731 | if (sizes[i] < format->plane_fmt[i].sizeimage) |
732 | return -EINVAL; | |
df9ecb0c | 733 | return 0; |
26e0ca22 LP |
734 | } |
735 | ||
736 | *nplanes = format->num_planes; | |
737 | ||
dce57314 | 738 | for (i = 0; i < format->num_planes; ++i) |
26e0ca22 | 739 | sizes[i] = format->plane_fmt[i].sizeimage; |
26e0ca22 LP |
740 | |
741 | return 0; | |
742 | } | |
743 | ||
744 | static int vsp1_video_buffer_prepare(struct vb2_buffer *vb) | |
745 | { | |
2d700715 | 746 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
26e0ca22 | 747 | struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue); |
f7ebf3ca | 748 | struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf); |
86960eec | 749 | const struct v4l2_pix_format_mplane *format = &video->rwpf->format; |
26e0ca22 LP |
750 | unsigned int i; |
751 | ||
752 | if (vb->num_planes < format->num_planes) | |
753 | return -EINVAL; | |
754 | ||
26e0ca22 | 755 | for (i = 0; i < vb->num_planes; ++i) { |
b58faa95 | 756 | buf->mem.addr[i] = vb2_dma_contig_plane_dma_addr(vb, i); |
26e0ca22 | 757 | |
351bbf99 | 758 | if (vb2_plane_size(vb, i) < format->plane_fmt[i].sizeimage) |
26e0ca22 LP |
759 | return -EINVAL; |
760 | } | |
761 | ||
351bbf99 | 762 | for ( ; i < 3; ++i) |
4d346be5 | 763 | buf->mem.addr[i] = 0; |
4d346be5 | 764 | |
26e0ca22 LP |
765 | return 0; |
766 | } | |
767 | ||
768 | static void vsp1_video_buffer_queue(struct vb2_buffer *vb) | |
769 | { | |
2d700715 | 770 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
26e0ca22 | 771 | struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue); |
1ccbb32c | 772 | struct vsp1_pipeline *pipe = video->rwpf->entity.pipe; |
f7ebf3ca | 773 | struct vsp1_vb2_buffer *buf = to_vsp1_vb2_buffer(vbuf); |
26e0ca22 LP |
774 | unsigned long flags; |
775 | bool empty; | |
776 | ||
777 | spin_lock_irqsave(&video->irqlock, flags); | |
778 | empty = list_empty(&video->irqqueue); | |
779 | list_add_tail(&buf->queue, &video->irqqueue); | |
780 | spin_unlock_irqrestore(&video->irqlock, flags); | |
781 | ||
782 | if (!empty) | |
783 | return; | |
784 | ||
785 | spin_lock_irqsave(&pipe->irqlock, flags); | |
786 | ||
351bbf99 | 787 | video->rwpf->mem = buf->mem; |
26e0ca22 LP |
788 | pipe->buffers_ready |= 1 << video->pipe_index; |
789 | ||
790 | if (vb2_is_streaming(&video->queue) && | |
791 | vsp1_pipeline_ready(pipe)) | |
351bbf99 | 792 | vsp1_video_pipeline_run(pipe); |
26e0ca22 LP |
793 | |
794 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
795 | } | |
796 | ||
351bbf99 LP |
797 | static int vsp1_video_setup_pipeline(struct vsp1_pipeline *pipe) |
798 | { | |
799 | struct vsp1_entity *entity; | |
25b6beab | 800 | int ret; |
351bbf99 | 801 | |
df32c924 | 802 | /* Determine this pipelines sizes for image partitioning support. */ |
25b6beab KB |
803 | ret = vsp1_video_pipeline_setup_partitions(pipe); |
804 | if (ret < 0) | |
805 | return ret; | |
df32c924 | 806 | |
351bbf99 LP |
807 | if (pipe->uds) { |
808 | struct vsp1_uds *uds = to_uds(&pipe->uds->subdev); | |
809 | ||
9dbed95b | 810 | /* |
6134148f LP |
811 | * If a BRU or BRS is present in the pipeline before the UDS, |
812 | * the alpha component doesn't need to be scaled as the BRU and | |
813 | * BRS output alpha value is fixed to 255. Otherwise we need to | |
814 | * scale the alpha component only when available at the input | |
815 | * RPF. | |
351bbf99 | 816 | */ |
6134148f LP |
817 | if (pipe->uds_input->type == VSP1_ENTITY_BRU || |
818 | pipe->uds_input->type == VSP1_ENTITY_BRS) { | |
351bbf99 LP |
819 | uds->scale_alpha = false; |
820 | } else { | |
821 | struct vsp1_rwpf *rpf = | |
822 | to_rwpf(&pipe->uds_input->subdev); | |
823 | ||
824 | uds->scale_alpha = rpf->fmtinfo->alpha; | |
825 | } | |
826 | } | |
827 | ||
e646e177 KB |
828 | /* |
829 | * Compute and cache the stream configuration into a body. The cached | |
830 | * body will be added to the display list by vsp1_video_pipeline_run() | |
831 | * whenever the pipeline needs to be fully reconfigured. | |
832 | */ | |
833 | pipe->stream_config = vsp1_dlm_dl_body_get(pipe->output->dlm); | |
834 | if (!pipe->stream_config) | |
835 | return -ENOMEM; | |
836 | ||
351bbf99 | 837 | list_for_each_entry(entity, &pipe->entities, list_pipe) { |
e646e177 KB |
838 | vsp1_entity_route_setup(entity, pipe, pipe->stream_config); |
839 | vsp1_entity_configure_stream(entity, pipe, pipe->stream_config); | |
351bbf99 LP |
840 | } |
841 | ||
7b905f05 | 842 | return 0; |
351bbf99 LP |
843 | } |
844 | ||
83967993 | 845 | static void vsp1_video_release_buffers(struct vsp1_video *video) |
372b2b03 | 846 | { |
372b2b03 KB |
847 | struct vsp1_vb2_buffer *buffer; |
848 | unsigned long flags; | |
849 | ||
850 | /* Remove all buffers from the IRQ queue. */ | |
851 | spin_lock_irqsave(&video->irqlock, flags); | |
852 | list_for_each_entry(buffer, &video->irqqueue, queue) | |
853 | vb2_buffer_done(&buffer->buf.vb2_buf, VB2_BUF_STATE_ERROR); | |
854 | INIT_LIST_HEAD(&video->irqqueue); | |
855 | spin_unlock_irqrestore(&video->irqlock, flags); | |
83967993 KB |
856 | } |
857 | ||
858 | static void vsp1_video_cleanup_pipeline(struct vsp1_pipeline *pipe) | |
859 | { | |
860 | lockdep_assert_held(&pipe->lock); | |
25b6beab | 861 | |
e646e177 KB |
862 | /* Release any cached configuration from our output video. */ |
863 | vsp1_dl_body_put(pipe->stream_config); | |
864 | pipe->stream_config = NULL; | |
865 | pipe->configured = false; | |
866 | ||
23a99e80 | 867 | /* Release our partition table allocation. */ |
25b6beab KB |
868 | kfree(pipe->part_table); |
869 | pipe->part_table = NULL; | |
372b2b03 KB |
870 | } |
871 | ||
26e0ca22 LP |
872 | static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count) |
873 | { | |
874 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
1ccbb32c | 875 | struct vsp1_pipeline *pipe = video->rwpf->entity.pipe; |
4461c84b | 876 | bool start_pipeline = false; |
26e0ca22 LP |
877 | unsigned long flags; |
878 | int ret; | |
879 | ||
880 | mutex_lock(&pipe->lock); | |
2f2db2f2 | 881 | if (pipe->stream_count == pipe->num_inputs) { |
351bbf99 LP |
882 | ret = vsp1_video_setup_pipeline(pipe); |
883 | if (ret < 0) { | |
83967993 | 884 | vsp1_video_release_buffers(video); |
372b2b03 | 885 | vsp1_video_cleanup_pipeline(pipe); |
83967993 | 886 | mutex_unlock(&pipe->lock); |
351bbf99 | 887 | return ret; |
26e0ca22 | 888 | } |
4461c84b KB |
889 | |
890 | start_pipeline = true; | |
26e0ca22 LP |
891 | } |
892 | ||
893 | pipe->stream_count++; | |
894 | mutex_unlock(&pipe->lock); | |
895 | ||
4461c84b KB |
896 | /* |
897 | * vsp1_pipeline_ready() is not sufficient to establish that all streams | |
898 | * are prepared and the pipeline is configured, as multiple streams | |
899 | * can race through streamon with buffers already queued; Therefore we | |
900 | * don't even attempt to start the pipeline until the last stream has | |
901 | * called through here. | |
902 | */ | |
903 | if (!start_pipeline) | |
904 | return 0; | |
905 | ||
26e0ca22 LP |
906 | spin_lock_irqsave(&pipe->irqlock, flags); |
907 | if (vsp1_pipeline_ready(pipe)) | |
351bbf99 | 908 | vsp1_video_pipeline_run(pipe); |
26e0ca22 LP |
909 | spin_unlock_irqrestore(&pipe->irqlock, flags); |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
e37559b2 | 914 | static void vsp1_video_stop_streaming(struct vb2_queue *vq) |
26e0ca22 LP |
915 | { |
916 | struct vsp1_video *video = vb2_get_drv_priv(vq); | |
1ccbb32c | 917 | struct vsp1_pipeline *pipe = video->rwpf->entity.pipe; |
26e0ca22 LP |
918 | unsigned long flags; |
919 | int ret; | |
920 | ||
b6187392 MCC |
921 | /* |
922 | * Clear the buffers ready flag to make sure the device won't be started | |
e4e70a14 LP |
923 | * by a QBUF on the video node on the other side of the pipeline. |
924 | */ | |
925 | spin_lock_irqsave(&video->irqlock, flags); | |
926 | pipe->buffers_ready &= ~(1 << video->pipe_index); | |
927 | spin_unlock_irqrestore(&video->irqlock, flags); | |
928 | ||
26e0ca22 | 929 | mutex_lock(&pipe->lock); |
b4dfb9b3 | 930 | if (--pipe->stream_count == pipe->num_inputs) { |
26e0ca22 LP |
931 | /* Stop the pipeline. */ |
932 | ret = vsp1_pipeline_stop(pipe); | |
933 | if (ret == -ETIMEDOUT) | |
934 | dev_err(video->vsp1->dev, "pipeline stop timeout\n"); | |
351bbf99 | 935 | |
83967993 | 936 | vsp1_video_cleanup_pipeline(pipe); |
26e0ca22 LP |
937 | } |
938 | mutex_unlock(&pipe->lock); | |
939 | ||
20b85227 | 940 | media_pipeline_stop(&video->video.entity); |
83967993 | 941 | vsp1_video_release_buffers(video); |
a0cdac56 | 942 | vsp1_video_pipeline_put(pipe); |
26e0ca22 LP |
943 | } |
944 | ||
eb9163d3 | 945 | static const struct vb2_ops vsp1_video_queue_qops = { |
26e0ca22 LP |
946 | .queue_setup = vsp1_video_queue_setup, |
947 | .buf_prepare = vsp1_video_buffer_prepare, | |
948 | .buf_queue = vsp1_video_buffer_queue, | |
949 | .wait_prepare = vb2_ops_wait_prepare, | |
950 | .wait_finish = vb2_ops_wait_finish, | |
951 | .start_streaming = vsp1_video_start_streaming, | |
952 | .stop_streaming = vsp1_video_stop_streaming, | |
953 | }; | |
954 | ||
955 | /* ----------------------------------------------------------------------------- | |
956 | * V4L2 ioctls | |
957 | */ | |
958 | ||
959 | static int | |
960 | vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap) | |
961 | { | |
962 | struct v4l2_fh *vfh = file->private_data; | |
963 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
964 | ||
965 | cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING | |
966 | | V4L2_CAP_VIDEO_CAPTURE_MPLANE | |
967 | | V4L2_CAP_VIDEO_OUTPUT_MPLANE; | |
968 | ||
969 | if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) | |
970 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | |
971 | | V4L2_CAP_STREAMING; | |
972 | else | |
973 | cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE | |
974 | | V4L2_CAP_STREAMING; | |
975 | ||
c0decac1 MCC |
976 | strscpy(cap->driver, "vsp1", sizeof(cap->driver)); |
977 | strscpy(cap->card, video->video.name, sizeof(cap->card)); | |
26e0ca22 LP |
978 | snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", |
979 | dev_name(video->vsp1->dev)); | |
980 | ||
981 | return 0; | |
982 | } | |
983 | ||
984 | static int | |
985 | vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format) | |
986 | { | |
987 | struct v4l2_fh *vfh = file->private_data; | |
988 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
989 | ||
990 | if (format->type != video->queue.type) | |
991 | return -EINVAL; | |
992 | ||
993 | mutex_lock(&video->lock); | |
86960eec | 994 | format->fmt.pix_mp = video->rwpf->format; |
26e0ca22 LP |
995 | mutex_unlock(&video->lock); |
996 | ||
997 | return 0; | |
998 | } | |
999 | ||
1000 | static int | |
1001 | vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format) | |
1002 | { | |
1003 | struct v4l2_fh *vfh = file->private_data; | |
1004 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
1005 | ||
1006 | if (format->type != video->queue.type) | |
1007 | return -EINVAL; | |
1008 | ||
1009 | return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL); | |
1010 | } | |
1011 | ||
1012 | static int | |
1013 | vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format) | |
1014 | { | |
1015 | struct v4l2_fh *vfh = file->private_data; | |
1016 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
1017 | const struct vsp1_format_info *info; | |
1018 | int ret; | |
1019 | ||
1020 | if (format->type != video->queue.type) | |
1021 | return -EINVAL; | |
1022 | ||
1023 | ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info); | |
1024 | if (ret < 0) | |
1025 | return ret; | |
1026 | ||
1027 | mutex_lock(&video->lock); | |
1028 | ||
1029 | if (vb2_is_busy(&video->queue)) { | |
1030 | ret = -EBUSY; | |
1031 | goto done; | |
1032 | } | |
1033 | ||
86960eec LP |
1034 | video->rwpf->format = format->fmt.pix_mp; |
1035 | video->rwpf->fmtinfo = info; | |
26e0ca22 LP |
1036 | |
1037 | done: | |
1038 | mutex_unlock(&video->lock); | |
1039 | return ret; | |
1040 | } | |
1041 | ||
1042 | static int | |
1043 | vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type) | |
1044 | { | |
1045 | struct v4l2_fh *vfh = file->private_data; | |
1046 | struct vsp1_video *video = to_vsp1_video(vfh->vdev); | |
a0cdac56 | 1047 | struct media_device *mdev = &video->vsp1->media_dev; |
26e0ca22 LP |
1048 | struct vsp1_pipeline *pipe; |
1049 | int ret; | |
1050 | ||
26e0ca22 LP |
1051 | if (video->queue.owner && video->queue.owner != file->private_data) |
1052 | return -EBUSY; | |
1053 | ||
9dbed95b LP |
1054 | /* |
1055 | * Get a pipeline for the video node and start streaming on it. No link | |
a0cdac56 LP |
1056 | * touching an entity in the pipeline can be activated or deactivated |
1057 | * once streaming is started. | |
26e0ca22 | 1058 | */ |
a0cdac56 | 1059 | mutex_lock(&mdev->graph_mutex); |
26e0ca22 | 1060 | |
a0cdac56 LP |
1061 | pipe = vsp1_video_pipeline_get(video); |
1062 | if (IS_ERR(pipe)) { | |
1063 | mutex_unlock(&mdev->graph_mutex); | |
1064 | return PTR_ERR(pipe); | |
1065 | } | |
1066 | ||
20b85227 | 1067 | ret = __media_pipeline_start(&video->video.entity, &pipe->pipe); |
a0cdac56 LP |
1068 | if (ret < 0) { |
1069 | mutex_unlock(&mdev->graph_mutex); | |
1070 | goto err_pipe; | |
1071 | } | |
1072 | ||
1073 | mutex_unlock(&mdev->graph_mutex); | |
26e0ca22 | 1074 | |
9dbed95b LP |
1075 | /* |
1076 | * Verify that the configured format matches the output of the connected | |
26e0ca22 LP |
1077 | * subdev. |
1078 | */ | |
1079 | ret = vsp1_video_verify_format(video); | |
1080 | if (ret < 0) | |
1081 | goto err_stop; | |
1082 | ||
26e0ca22 LP |
1083 | /* Start the queue. */ |
1084 | ret = vb2_streamon(&video->queue, type); | |
1085 | if (ret < 0) | |
a0cdac56 | 1086 | goto err_stop; |
26e0ca22 LP |
1087 | |
1088 | return 0; | |
1089 | ||
26e0ca22 | 1090 | err_stop: |
20b85227 | 1091 | media_pipeline_stop(&video->video.entity); |
a0cdac56 LP |
1092 | err_pipe: |
1093 | vsp1_video_pipeline_put(pipe); | |
26e0ca22 LP |
1094 | return ret; |
1095 | } | |
1096 | ||
1097 | static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = { | |
1098 | .vidioc_querycap = vsp1_video_querycap, | |
1099 | .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format, | |
1100 | .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format, | |
1101 | .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format, | |
1102 | .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format, | |
1103 | .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format, | |
1104 | .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format, | |
1105 | .vidioc_reqbufs = vb2_ioctl_reqbufs, | |
1106 | .vidioc_querybuf = vb2_ioctl_querybuf, | |
1107 | .vidioc_qbuf = vb2_ioctl_qbuf, | |
1108 | .vidioc_dqbuf = vb2_ioctl_dqbuf, | |
5a66e2f6 | 1109 | .vidioc_expbuf = vb2_ioctl_expbuf, |
26e0ca22 LP |
1110 | .vidioc_create_bufs = vb2_ioctl_create_bufs, |
1111 | .vidioc_prepare_buf = vb2_ioctl_prepare_buf, | |
1112 | .vidioc_streamon = vsp1_video_streamon, | |
1113 | .vidioc_streamoff = vb2_ioctl_streamoff, | |
1114 | }; | |
1115 | ||
1116 | /* ----------------------------------------------------------------------------- | |
1117 | * V4L2 File Operations | |
1118 | */ | |
1119 | ||
1120 | static int vsp1_video_open(struct file *file) | |
1121 | { | |
1122 | struct vsp1_video *video = video_drvdata(file); | |
1123 | struct v4l2_fh *vfh; | |
1124 | int ret = 0; | |
1125 | ||
1126 | vfh = kzalloc(sizeof(*vfh), GFP_KERNEL); | |
1127 | if (vfh == NULL) | |
1128 | return -ENOMEM; | |
1129 | ||
1130 | v4l2_fh_init(vfh, &video->video); | |
1131 | v4l2_fh_add(vfh); | |
1132 | ||
1133 | file->private_data = vfh; | |
1134 | ||
4c16d6a0 LP |
1135 | ret = vsp1_device_get(video->vsp1); |
1136 | if (ret < 0) { | |
26e0ca22 | 1137 | v4l2_fh_del(vfh); |
dadc3be6 | 1138 | v4l2_fh_exit(vfh); |
26e0ca22 LP |
1139 | kfree(vfh); |
1140 | } | |
1141 | ||
1142 | return ret; | |
1143 | } | |
1144 | ||
1145 | static int vsp1_video_release(struct file *file) | |
1146 | { | |
1147 | struct vsp1_video *video = video_drvdata(file); | |
1148 | struct v4l2_fh *vfh = file->private_data; | |
1149 | ||
1150 | mutex_lock(&video->lock); | |
1151 | if (video->queue.owner == vfh) { | |
1152 | vb2_queue_release(&video->queue); | |
1153 | video->queue.owner = NULL; | |
1154 | } | |
1155 | mutex_unlock(&video->lock); | |
1156 | ||
1157 | vsp1_device_put(video->vsp1); | |
1158 | ||
1159 | v4l2_fh_release(file); | |
1160 | ||
1161 | file->private_data = NULL; | |
1162 | ||
1163 | return 0; | |
1164 | } | |
1165 | ||
eb9163d3 | 1166 | static const struct v4l2_file_operations vsp1_video_fops = { |
26e0ca22 LP |
1167 | .owner = THIS_MODULE, |
1168 | .unlocked_ioctl = video_ioctl2, | |
1169 | .open = vsp1_video_open, | |
1170 | .release = vsp1_video_release, | |
1171 | .poll = vb2_fop_poll, | |
1172 | .mmap = vb2_fop_mmap, | |
1173 | }; | |
1174 | ||
fce34e49 KB |
1175 | /* ----------------------------------------------------------------------------- |
1176 | * Suspend and Resume | |
1177 | */ | |
1178 | ||
1179 | void vsp1_video_suspend(struct vsp1_device *vsp1) | |
1180 | { | |
1181 | unsigned long flags; | |
1182 | unsigned int i; | |
1183 | int ret; | |
1184 | ||
1185 | /* | |
1186 | * To avoid increasing the system suspend time needlessly, loop over the | |
1187 | * pipelines twice, first to set them all to the stopping state, and | |
1188 | * then to wait for the stop to complete. | |
1189 | */ | |
1190 | for (i = 0; i < vsp1->info->wpf_count; ++i) { | |
1191 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
1192 | struct vsp1_pipeline *pipe; | |
1193 | ||
1194 | if (wpf == NULL) | |
1195 | continue; | |
1196 | ||
1197 | pipe = wpf->entity.pipe; | |
1198 | if (pipe == NULL) | |
1199 | continue; | |
1200 | ||
1201 | spin_lock_irqsave(&pipe->irqlock, flags); | |
1202 | if (pipe->state == VSP1_PIPELINE_RUNNING) | |
1203 | pipe->state = VSP1_PIPELINE_STOPPING; | |
1204 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
1205 | } | |
1206 | ||
1207 | for (i = 0; i < vsp1->info->wpf_count; ++i) { | |
1208 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
1209 | struct vsp1_pipeline *pipe; | |
1210 | ||
1211 | if (wpf == NULL) | |
1212 | continue; | |
1213 | ||
1214 | pipe = wpf->entity.pipe; | |
1215 | if (pipe == NULL) | |
1216 | continue; | |
1217 | ||
1218 | ret = wait_event_timeout(pipe->wq, vsp1_pipeline_stopped(pipe), | |
1219 | msecs_to_jiffies(500)); | |
1220 | if (ret == 0) | |
1221 | dev_warn(vsp1->dev, "pipeline %u stop timeout\n", | |
1222 | wpf->entity.index); | |
1223 | } | |
1224 | } | |
1225 | ||
1226 | void vsp1_video_resume(struct vsp1_device *vsp1) | |
1227 | { | |
1228 | unsigned long flags; | |
1229 | unsigned int i; | |
1230 | ||
1231 | /* Resume all running pipelines. */ | |
1232 | for (i = 0; i < vsp1->info->wpf_count; ++i) { | |
1233 | struct vsp1_rwpf *wpf = vsp1->wpf[i]; | |
1234 | struct vsp1_pipeline *pipe; | |
1235 | ||
1236 | if (wpf == NULL) | |
1237 | continue; | |
1238 | ||
1239 | pipe = wpf->entity.pipe; | |
1240 | if (pipe == NULL) | |
1241 | continue; | |
1242 | ||
e646e177 KB |
1243 | /* |
1244 | * The hardware may have been reset during a suspend and will | |
1245 | * need a full reconfiguration. | |
1246 | */ | |
1247 | pipe->configured = false; | |
1248 | ||
fce34e49 KB |
1249 | spin_lock_irqsave(&pipe->irqlock, flags); |
1250 | if (vsp1_pipeline_ready(pipe)) | |
1251 | vsp1_video_pipeline_run(pipe); | |
1252 | spin_unlock_irqrestore(&pipe->irqlock, flags); | |
1253 | } | |
1254 | } | |
1255 | ||
26e0ca22 LP |
1256 | /* ----------------------------------------------------------------------------- |
1257 | * Initialization and Cleanup | |
1258 | */ | |
1259 | ||
9d40637a LP |
1260 | struct vsp1_video *vsp1_video_create(struct vsp1_device *vsp1, |
1261 | struct vsp1_rwpf *rwpf) | |
26e0ca22 | 1262 | { |
9d40637a | 1263 | struct vsp1_video *video; |
26e0ca22 LP |
1264 | const char *direction; |
1265 | int ret; | |
1266 | ||
9d40637a LP |
1267 | video = devm_kzalloc(vsp1->dev, sizeof(*video), GFP_KERNEL); |
1268 | if (!video) | |
1269 | return ERR_PTR(-ENOMEM); | |
26e0ca22 | 1270 | |
faf2644d | 1271 | rwpf->video = video; |
9d40637a LP |
1272 | |
1273 | video->vsp1 = vsp1; | |
1274 | video->rwpf = rwpf; | |
1275 | ||
1276 | if (rwpf->entity.type == VSP1_ENTITY_RPF) { | |
26e0ca22 | 1277 | direction = "input"; |
9d40637a | 1278 | video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
26e0ca22 LP |
1279 | video->pad.flags = MEDIA_PAD_FL_SOURCE; |
1280 | video->video.vfl_dir = VFL_DIR_TX; | |
9d40637a LP |
1281 | } else { |
1282 | direction = "output"; | |
1283 | video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; | |
1284 | video->pad.flags = MEDIA_PAD_FL_SINK; | |
1285 | video->video.vfl_dir = VFL_DIR_RX; | |
26e0ca22 LP |
1286 | } |
1287 | ||
26e0ca22 LP |
1288 | mutex_init(&video->lock); |
1289 | spin_lock_init(&video->irqlock); | |
1290 | INIT_LIST_HEAD(&video->irqqueue); | |
1291 | ||
26e0ca22 | 1292 | /* Initialize the media entity... */ |
ab22e77c | 1293 | ret = media_entity_pads_init(&video->video.entity, 1, &video->pad); |
26e0ca22 | 1294 | if (ret < 0) |
9d40637a | 1295 | return ERR_PTR(ret); |
26e0ca22 LP |
1296 | |
1297 | /* ... and the format ... */ | |
b911605d | 1298 | rwpf->format.pixelformat = VSP1_VIDEO_DEF_FORMAT; |
86960eec LP |
1299 | rwpf->format.width = VSP1_VIDEO_DEF_WIDTH; |
1300 | rwpf->format.height = VSP1_VIDEO_DEF_HEIGHT; | |
b911605d | 1301 | __vsp1_video_try_format(video, &rwpf->format, &rwpf->fmtinfo); |
26e0ca22 LP |
1302 | |
1303 | /* ... and the video node... */ | |
1304 | video->video.v4l2_dev = &video->vsp1->v4l2_dev; | |
1305 | video->video.fops = &vsp1_video_fops; | |
1306 | snprintf(video->video.name, sizeof(video->video.name), "%s %s", | |
8b4a0563 | 1307 | rwpf->entity.subdev.name, direction); |
26e0ca22 LP |
1308 | video->video.vfl_type = VFL_TYPE_GRABBER; |
1309 | video->video.release = video_device_release_empty; | |
1310 | video->video.ioctl_ops = &vsp1_video_ioctl_ops; | |
1311 | ||
1312 | video_set_drvdata(&video->video, video); | |
1313 | ||
26e0ca22 LP |
1314 | video->queue.type = video->type; |
1315 | video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; | |
1316 | video->queue.lock = &video->lock; | |
1317 | video->queue.drv_priv = video; | |
f7ebf3ca | 1318 | video->queue.buf_struct_size = sizeof(struct vsp1_vb2_buffer); |
26e0ca22 LP |
1319 | video->queue.ops = &vsp1_video_queue_qops; |
1320 | video->queue.mem_ops = &vb2_dma_contig_memops; | |
ade48681 | 1321 | video->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
2cc2137f | 1322 | video->queue.dev = video->vsp1->bus_master; |
26e0ca22 LP |
1323 | ret = vb2_queue_init(&video->queue); |
1324 | if (ret < 0) { | |
1325 | dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n"); | |
1326 | goto error; | |
1327 | } | |
1328 | ||
1329 | /* ... and register the video device. */ | |
1330 | video->video.queue = &video->queue; | |
1331 | ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1); | |
1332 | if (ret < 0) { | |
1333 | dev_err(video->vsp1->dev, "failed to register video device\n"); | |
1334 | goto error; | |
1335 | } | |
1336 | ||
9d40637a | 1337 | return video; |
26e0ca22 LP |
1338 | |
1339 | error: | |
26e0ca22 | 1340 | vsp1_video_cleanup(video); |
9d40637a | 1341 | return ERR_PTR(ret); |
26e0ca22 LP |
1342 | } |
1343 | ||
1344 | void vsp1_video_cleanup(struct vsp1_video *video) | |
1345 | { | |
1346 | if (video_is_registered(&video->video)) | |
1347 | video_unregister_device(&video->video); | |
1348 | ||
26e0ca22 LP |
1349 | media_entity_cleanup(&video->video.entity); |
1350 | } |