mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / media / platform / vsp1 / vsp1_video.c
blob2960ff1637d1ff6feecd855f299a1db9737a16d2
1 /*
2 * vsp1_video.c -- R-Car VSP1 Video Node
4 * Copyright (C) 2013 Renesas Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
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.
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/v4l2-mediabus.h>
20 #include <linux/videodev2.h>
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>
27 #include <media/videobuf2-core.h>
28 #include <media/videobuf2-dma-contig.h>
30 #include "vsp1.h"
31 #include "vsp1_entity.h"
32 #include "vsp1_rwpf.h"
33 #include "vsp1_video.h"
35 #define VSP1_VIDEO_DEF_FORMAT V4L2_PIX_FMT_YUYV
36 #define VSP1_VIDEO_DEF_WIDTH 1024
37 #define VSP1_VIDEO_DEF_HEIGHT 768
39 #define VSP1_VIDEO_MIN_WIDTH 2U
40 #define VSP1_VIDEO_MAX_WIDTH 8190U
41 #define VSP1_VIDEO_MIN_HEIGHT 2U
42 #define VSP1_VIDEO_MAX_HEIGHT 8190U
44 /* -----------------------------------------------------------------------------
45 * Helper functions
48 static const struct vsp1_format_info vsp1_video_formats[] = {
49 { V4L2_PIX_FMT_RGB332, V4L2_MBUS_FMT_ARGB8888_1X32,
50 VI6_FMT_RGB_332, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
51 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
52 1, { 8, 0, 0 }, false, false, 1, 1 },
53 { V4L2_PIX_FMT_RGB444, V4L2_MBUS_FMT_ARGB8888_1X32,
54 VI6_FMT_XRGB_4444, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
55 VI6_RPF_DSWAP_P_WDS,
56 1, { 16, 0, 0 }, false, false, 1, 1 },
57 { V4L2_PIX_FMT_RGB555, V4L2_MBUS_FMT_ARGB8888_1X32,
58 VI6_FMT_XRGB_1555, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
59 VI6_RPF_DSWAP_P_WDS,
60 1, { 16, 0, 0 }, false, false, 1, 1 },
61 { V4L2_PIX_FMT_RGB565, V4L2_MBUS_FMT_ARGB8888_1X32,
62 VI6_FMT_RGB_565, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
63 VI6_RPF_DSWAP_P_WDS,
64 1, { 16, 0, 0 }, false, false, 1, 1 },
65 { V4L2_PIX_FMT_BGR24, V4L2_MBUS_FMT_ARGB8888_1X32,
66 VI6_FMT_BGR_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
67 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
68 1, { 24, 0, 0 }, false, false, 1, 1 },
69 { V4L2_PIX_FMT_RGB24, V4L2_MBUS_FMT_ARGB8888_1X32,
70 VI6_FMT_RGB_888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
71 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
72 1, { 24, 0, 0 }, false, false, 1, 1 },
73 { V4L2_PIX_FMT_BGR32, V4L2_MBUS_FMT_ARGB8888_1X32,
74 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS,
75 1, { 32, 0, 0 }, false, false, 1, 1 },
76 { V4L2_PIX_FMT_RGB32, V4L2_MBUS_FMT_ARGB8888_1X32,
77 VI6_FMT_ARGB_8888, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
78 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
79 1, { 32, 0, 0 }, false, false, 1, 1 },
80 { V4L2_PIX_FMT_UYVY, V4L2_MBUS_FMT_AYUV8_1X32,
81 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
82 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
83 1, { 16, 0, 0 }, false, false, 2, 1 },
84 { V4L2_PIX_FMT_VYUY, V4L2_MBUS_FMT_AYUV8_1X32,
85 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
86 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
87 1, { 16, 0, 0 }, false, true, 2, 1 },
88 { V4L2_PIX_FMT_YUYV, V4L2_MBUS_FMT_AYUV8_1X32,
89 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
90 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
91 1, { 16, 0, 0 }, true, false, 2, 1 },
92 { V4L2_PIX_FMT_YVYU, V4L2_MBUS_FMT_AYUV8_1X32,
93 VI6_FMT_YUYV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
94 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
95 1, { 16, 0, 0 }, true, true, 2, 1 },
96 { V4L2_PIX_FMT_NV12M, V4L2_MBUS_FMT_AYUV8_1X32,
97 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
98 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
99 2, { 8, 16, 0 }, false, false, 2, 2 },
100 { V4L2_PIX_FMT_NV21M, V4L2_MBUS_FMT_AYUV8_1X32,
101 VI6_FMT_Y_UV_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
102 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
103 2, { 8, 16, 0 }, false, true, 2, 2 },
104 { V4L2_PIX_FMT_NV16M, V4L2_MBUS_FMT_AYUV8_1X32,
105 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
106 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
107 2, { 8, 16, 0 }, false, false, 2, 1 },
108 { V4L2_PIX_FMT_NV61M, V4L2_MBUS_FMT_AYUV8_1X32,
109 VI6_FMT_Y_UV_422, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
110 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
111 2, { 8, 16, 0 }, false, true, 2, 1 },
112 { V4L2_PIX_FMT_YUV420M, V4L2_MBUS_FMT_AYUV8_1X32,
113 VI6_FMT_Y_U_V_420, VI6_RPF_DSWAP_P_LLS | VI6_RPF_DSWAP_P_LWS |
114 VI6_RPF_DSWAP_P_WDS | VI6_RPF_DSWAP_P_BTS,
115 3, { 8, 8, 8 }, false, false, 2, 2 },
119 * vsp1_get_format_info - Retrieve format information for a 4CC
120 * @fourcc: the format 4CC
122 * Return a pointer to the format information structure corresponding to the
123 * given V4L2 format 4CC, or NULL if no corresponding format can be found.
125 static const struct vsp1_format_info *vsp1_get_format_info(u32 fourcc)
127 unsigned int i;
129 for (i = 0; i < ARRAY_SIZE(vsp1_video_formats); ++i) {
130 const struct vsp1_format_info *info = &vsp1_video_formats[i];
132 if (info->fourcc == fourcc)
133 return info;
136 return NULL;
140 static struct v4l2_subdev *
141 vsp1_video_remote_subdev(struct media_pad *local, u32 *pad)
143 struct media_pad *remote;
145 remote = media_entity_remote_pad(local);
146 if (remote == NULL ||
147 media_entity_type(remote->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
148 return NULL;
150 if (pad)
151 *pad = remote->index;
153 return media_entity_to_v4l2_subdev(remote->entity);
156 static int vsp1_video_verify_format(struct vsp1_video *video)
158 struct v4l2_subdev_format fmt;
159 struct v4l2_subdev *subdev;
160 int ret;
162 subdev = vsp1_video_remote_subdev(&video->pad, &fmt.pad);
163 if (subdev == NULL)
164 return -EINVAL;
166 fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
167 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
168 if (ret < 0)
169 return ret == -ENOIOCTLCMD ? -EINVAL : ret;
171 if (video->fmtinfo->mbus != fmt.format.code ||
172 video->format.height != fmt.format.height ||
173 video->format.width != fmt.format.width)
174 return -EINVAL;
176 return 0;
179 static int __vsp1_video_try_format(struct vsp1_video *video,
180 struct v4l2_pix_format_mplane *pix,
181 const struct vsp1_format_info **fmtinfo)
183 const struct vsp1_format_info *info;
184 unsigned int width = pix->width;
185 unsigned int height = pix->height;
186 unsigned int i;
188 /* Retrieve format information and select the default format if the
189 * requested format isn't supported.
191 info = vsp1_get_format_info(pix->pixelformat);
192 if (info == NULL)
193 info = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
195 pix->pixelformat = info->fourcc;
196 pix->colorspace = V4L2_COLORSPACE_SRGB;
197 pix->field = V4L2_FIELD_NONE;
198 memset(pix->reserved, 0, sizeof(pix->reserved));
200 /* Align the width and height for YUV 4:2:2 and 4:2:0 formats. */
201 width = round_down(width, info->hsub);
202 height = round_down(height, info->vsub);
204 /* Clamp the width and height. */
205 pix->width = clamp(width, VSP1_VIDEO_MIN_WIDTH, VSP1_VIDEO_MAX_WIDTH);
206 pix->height = clamp(height, VSP1_VIDEO_MIN_HEIGHT,
207 VSP1_VIDEO_MAX_HEIGHT);
209 /* Compute and clamp the stride and image size. While not documented in
210 * the datasheet, strides not aligned to a multiple of 128 bytes result
211 * in image corruption.
213 for (i = 0; i < max(info->planes, 2U); ++i) {
214 unsigned int hsub = i > 0 ? info->hsub : 1;
215 unsigned int vsub = i > 0 ? info->vsub : 1;
216 unsigned int align = 128;
217 unsigned int bpl;
219 bpl = clamp_t(unsigned int, pix->plane_fmt[i].bytesperline,
220 pix->width / hsub * info->bpp[i] / 8,
221 round_down(65535U, align));
223 pix->plane_fmt[i].bytesperline = round_up(bpl, align);
224 pix->plane_fmt[i].sizeimage = pix->plane_fmt[i].bytesperline
225 * pix->height / vsub;
228 if (info->planes == 3) {
229 /* The second and third planes must have the same stride. */
230 pix->plane_fmt[2].bytesperline = pix->plane_fmt[1].bytesperline;
231 pix->plane_fmt[2].sizeimage = pix->plane_fmt[1].sizeimage;
234 pix->num_planes = info->planes;
236 if (fmtinfo)
237 *fmtinfo = info;
239 return 0;
242 static bool
243 vsp1_video_format_adjust(struct vsp1_video *video,
244 const struct v4l2_pix_format_mplane *format,
245 struct v4l2_pix_format_mplane *adjust)
247 unsigned int i;
249 *adjust = *format;
250 __vsp1_video_try_format(video, adjust, NULL);
252 if (format->width != adjust->width ||
253 format->height != adjust->height ||
254 format->pixelformat != adjust->pixelformat ||
255 format->num_planes != adjust->num_planes)
256 return false;
258 for (i = 0; i < format->num_planes; ++i) {
259 if (format->plane_fmt[i].bytesperline !=
260 adjust->plane_fmt[i].bytesperline)
261 return false;
263 adjust->plane_fmt[i].sizeimage =
264 max(adjust->plane_fmt[i].sizeimage,
265 format->plane_fmt[i].sizeimage);
268 return true;
271 /* -----------------------------------------------------------------------------
272 * Pipeline Management
275 static int vsp1_pipeline_validate_branch(struct vsp1_rwpf *input,
276 struct vsp1_rwpf *output)
278 struct vsp1_entity *entity;
279 unsigned int entities = 0;
280 struct media_pad *pad;
281 bool uds_found = false;
283 pad = media_entity_remote_pad(&input->entity.pads[RWPF_PAD_SOURCE]);
285 while (1) {
286 if (pad == NULL)
287 return -EPIPE;
289 /* We've reached a video node, that shouldn't have happened. */
290 if (media_entity_type(pad->entity) != MEDIA_ENT_T_V4L2_SUBDEV)
291 return -EPIPE;
293 entity = to_vsp1_entity(media_entity_to_v4l2_subdev(pad->entity));
295 /* We've reached the WPF, we're done. */
296 if (entity->type == VSP1_ENTITY_WPF)
297 break;
299 /* Ensure the branch has no loop. */
300 if (entities & (1 << entity->subdev.entity.id))
301 return -EPIPE;
303 entities |= 1 << entity->subdev.entity.id;
305 /* UDS can't be chained. */
306 if (entity->type == VSP1_ENTITY_UDS) {
307 if (uds_found)
308 return -EPIPE;
309 uds_found = true;
312 /* Follow the source link. The link setup operations ensure
313 * that the output fan-out can't be more than one, there is thus
314 * no need to verify here that only a single source link is
315 * activated.
317 pad = &entity->pads[entity->source_pad];
318 pad = media_entity_remote_pad(pad);
321 /* The last entity must be the output WPF. */
322 if (entity != &output->entity)
323 return -EPIPE;
325 return 0;
328 static int vsp1_pipeline_validate(struct vsp1_pipeline *pipe,
329 struct vsp1_video *video)
331 struct media_entity_graph graph;
332 struct media_entity *entity = &video->video.entity;
333 struct media_device *mdev = entity->parent;
334 unsigned int i;
335 int ret;
337 mutex_lock(&mdev->graph_mutex);
339 /* Walk the graph to locate the entities and video nodes. */
340 media_entity_graph_walk_start(&graph, entity);
342 while ((entity = media_entity_graph_walk_next(&graph))) {
343 struct v4l2_subdev *subdev;
344 struct vsp1_rwpf *rwpf;
345 struct vsp1_entity *e;
347 if (media_entity_type(entity) != MEDIA_ENT_T_V4L2_SUBDEV) {
348 pipe->num_video++;
349 continue;
352 subdev = media_entity_to_v4l2_subdev(entity);
353 e = to_vsp1_entity(subdev);
354 list_add_tail(&e->list_pipe, &pipe->entities);
356 if (e->type == VSP1_ENTITY_RPF) {
357 rwpf = to_rwpf(subdev);
358 pipe->inputs[pipe->num_inputs++] = rwpf;
359 rwpf->video.pipe_index = pipe->num_inputs;
360 } else if (e->type == VSP1_ENTITY_WPF) {
361 rwpf = to_rwpf(subdev);
362 pipe->output = to_rwpf(subdev);
363 rwpf->video.pipe_index = 0;
364 } else if (e->type == VSP1_ENTITY_LIF) {
365 pipe->lif = e;
369 mutex_unlock(&mdev->graph_mutex);
371 /* We need one output and at least one input. */
372 if (pipe->num_inputs == 0 || !pipe->output) {
373 ret = -EPIPE;
374 goto error;
377 /* Follow links downstream for each input and make sure the graph
378 * contains no loop and that all branches end at the output WPF.
380 for (i = 0; i < pipe->num_inputs; ++i) {
381 ret = vsp1_pipeline_validate_branch(pipe->inputs[i],
382 pipe->output);
383 if (ret < 0)
384 goto error;
387 return 0;
389 error:
390 INIT_LIST_HEAD(&pipe->entities);
391 pipe->buffers_ready = 0;
392 pipe->num_video = 0;
393 pipe->num_inputs = 0;
394 pipe->output = NULL;
395 pipe->lif = NULL;
396 return ret;
399 static int vsp1_pipeline_init(struct vsp1_pipeline *pipe,
400 struct vsp1_video *video)
402 int ret;
404 mutex_lock(&pipe->lock);
406 /* If we're the first user validate and initialize the pipeline. */
407 if (pipe->use_count == 0) {
408 ret = vsp1_pipeline_validate(pipe, video);
409 if (ret < 0)
410 goto done;
413 pipe->use_count++;
414 ret = 0;
416 done:
417 mutex_unlock(&pipe->lock);
418 return ret;
421 static void vsp1_pipeline_cleanup(struct vsp1_pipeline *pipe)
423 mutex_lock(&pipe->lock);
425 /* If we're the last user clean up the pipeline. */
426 if (--pipe->use_count == 0) {
427 INIT_LIST_HEAD(&pipe->entities);
428 pipe->state = VSP1_PIPELINE_STOPPED;
429 pipe->buffers_ready = 0;
430 pipe->num_video = 0;
431 pipe->num_inputs = 0;
432 pipe->output = NULL;
433 pipe->lif = NULL;
436 mutex_unlock(&pipe->lock);
439 static void vsp1_pipeline_run(struct vsp1_pipeline *pipe)
441 struct vsp1_device *vsp1 = pipe->output->entity.vsp1;
443 vsp1_write(vsp1, VI6_CMD(pipe->output->entity.index), VI6_CMD_STRCMD);
444 pipe->state = VSP1_PIPELINE_RUNNING;
445 pipe->buffers_ready = 0;
448 static int vsp1_pipeline_stop(struct vsp1_pipeline *pipe)
450 struct vsp1_entity *entity;
451 unsigned long flags;
452 int ret;
454 spin_lock_irqsave(&pipe->irqlock, flags);
455 pipe->state = VSP1_PIPELINE_STOPPING;
456 spin_unlock_irqrestore(&pipe->irqlock, flags);
458 ret = wait_event_timeout(pipe->wq, pipe->state == VSP1_PIPELINE_STOPPED,
459 msecs_to_jiffies(500));
460 ret = ret == 0 ? -ETIMEDOUT : 0;
462 list_for_each_entry(entity, &pipe->entities, list_pipe) {
463 if (entity->route)
464 vsp1_write(entity->vsp1, entity->route,
465 VI6_DPR_NODE_UNUSED);
467 v4l2_subdev_call(&entity->subdev, video, s_stream, 0);
470 return ret;
473 static bool vsp1_pipeline_ready(struct vsp1_pipeline *pipe)
475 unsigned int mask;
477 mask = ((1 << pipe->num_inputs) - 1) << 1;
478 if (!pipe->lif)
479 mask |= 1 << 0;
481 return pipe->buffers_ready == mask;
485 * vsp1_video_complete_buffer - Complete the current buffer
486 * @video: the video node
488 * This function completes the current buffer by filling its sequence number,
489 * time stamp and payload size, and hands it back to the videobuf core.
491 * Return the next queued buffer or NULL if the queue is empty.
493 static struct vsp1_video_buffer *
494 vsp1_video_complete_buffer(struct vsp1_video *video)
496 struct vsp1_video_buffer *next = NULL;
497 struct vsp1_video_buffer *done;
498 unsigned long flags;
499 unsigned int i;
501 spin_lock_irqsave(&video->irqlock, flags);
503 if (list_empty(&video->irqqueue)) {
504 spin_unlock_irqrestore(&video->irqlock, flags);
505 return NULL;
508 done = list_first_entry(&video->irqqueue,
509 struct vsp1_video_buffer, queue);
510 list_del(&done->queue);
512 if (!list_empty(&video->irqqueue))
513 next = list_first_entry(&video->irqqueue,
514 struct vsp1_video_buffer, queue);
516 spin_unlock_irqrestore(&video->irqlock, flags);
518 done->buf.v4l2_buf.sequence = video->sequence++;
519 v4l2_get_timestamp(&done->buf.v4l2_buf.timestamp);
520 for (i = 0; i < done->buf.num_planes; ++i)
521 vb2_set_plane_payload(&done->buf, i, done->length[i]);
522 vb2_buffer_done(&done->buf, VB2_BUF_STATE_DONE);
524 return next;
527 static void vsp1_video_frame_end(struct vsp1_pipeline *pipe,
528 struct vsp1_video *video)
530 struct vsp1_video_buffer *buf;
531 unsigned long flags;
533 buf = vsp1_video_complete_buffer(video);
534 if (buf == NULL)
535 return;
537 spin_lock_irqsave(&pipe->irqlock, flags);
539 video->ops->queue(video, buf);
540 pipe->buffers_ready |= 1 << video->pipe_index;
542 spin_unlock_irqrestore(&pipe->irqlock, flags);
545 void vsp1_pipeline_frame_end(struct vsp1_pipeline *pipe)
547 unsigned long flags;
548 unsigned int i;
550 if (pipe == NULL)
551 return;
553 /* Complete buffers on all video nodes. */
554 for (i = 0; i < pipe->num_inputs; ++i)
555 vsp1_video_frame_end(pipe, &pipe->inputs[i]->video);
557 if (!pipe->lif)
558 vsp1_video_frame_end(pipe, &pipe->output->video);
560 spin_lock_irqsave(&pipe->irqlock, flags);
562 /* If a stop has been requested, mark the pipeline as stopped and
563 * return.
565 if (pipe->state == VSP1_PIPELINE_STOPPING) {
566 pipe->state = VSP1_PIPELINE_STOPPED;
567 wake_up(&pipe->wq);
568 goto done;
571 /* Restart the pipeline if ready. */
572 if (vsp1_pipeline_ready(pipe))
573 vsp1_pipeline_run(pipe);
575 done:
576 spin_unlock_irqrestore(&pipe->irqlock, flags);
579 /* -----------------------------------------------------------------------------
580 * videobuf2 Queue Operations
583 static int
584 vsp1_video_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
585 unsigned int *nbuffers, unsigned int *nplanes,
586 unsigned int sizes[], void *alloc_ctxs[])
588 struct vsp1_video *video = vb2_get_drv_priv(vq);
589 const struct v4l2_pix_format_mplane *format;
590 struct v4l2_pix_format_mplane pix_mp;
591 unsigned int i;
593 if (fmt) {
594 /* Make sure the format is valid and adjust the sizeimage field
595 * if needed.
597 if (!vsp1_video_format_adjust(video, &fmt->fmt.pix_mp, &pix_mp))
598 return -EINVAL;
600 format = &pix_mp;
601 } else {
602 format = &video->format;
605 *nplanes = format->num_planes;
607 for (i = 0; i < format->num_planes; ++i) {
608 sizes[i] = format->plane_fmt[i].sizeimage;
609 alloc_ctxs[i] = video->alloc_ctx;
612 return 0;
615 static int vsp1_video_buffer_prepare(struct vb2_buffer *vb)
617 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
618 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
619 const struct v4l2_pix_format_mplane *format = &video->format;
620 unsigned int i;
622 if (vb->num_planes < format->num_planes)
623 return -EINVAL;
625 for (i = 0; i < vb->num_planes; ++i) {
626 buf->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
627 buf->length[i] = vb2_plane_size(vb, i);
629 if (buf->length[i] < format->plane_fmt[i].sizeimage)
630 return -EINVAL;
633 return 0;
636 static void vsp1_video_buffer_queue(struct vb2_buffer *vb)
638 struct vsp1_video *video = vb2_get_drv_priv(vb->vb2_queue);
639 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
640 struct vsp1_video_buffer *buf = to_vsp1_video_buffer(vb);
641 unsigned long flags;
642 bool empty;
644 spin_lock_irqsave(&video->irqlock, flags);
645 empty = list_empty(&video->irqqueue);
646 list_add_tail(&buf->queue, &video->irqqueue);
647 spin_unlock_irqrestore(&video->irqlock, flags);
649 if (!empty)
650 return;
652 spin_lock_irqsave(&pipe->irqlock, flags);
654 video->ops->queue(video, buf);
655 pipe->buffers_ready |= 1 << video->pipe_index;
657 if (vb2_is_streaming(&video->queue) &&
658 vsp1_pipeline_ready(pipe))
659 vsp1_pipeline_run(pipe);
661 spin_unlock_irqrestore(&pipe->irqlock, flags);
664 static void vsp1_entity_route_setup(struct vsp1_entity *source)
666 struct vsp1_entity *sink;
668 if (source->route == 0)
669 return;
671 sink = container_of(source->sink, struct vsp1_entity, subdev.entity);
672 vsp1_write(source->vsp1, source->route, sink->id);
675 static int vsp1_video_start_streaming(struct vb2_queue *vq, unsigned int count)
677 struct vsp1_video *video = vb2_get_drv_priv(vq);
678 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
679 struct vsp1_entity *entity;
680 unsigned long flags;
681 int ret;
683 mutex_lock(&pipe->lock);
684 if (pipe->stream_count == pipe->num_video - 1) {
685 list_for_each_entry(entity, &pipe->entities, list_pipe) {
686 vsp1_entity_route_setup(entity);
688 ret = v4l2_subdev_call(&entity->subdev, video,
689 s_stream, 1);
690 if (ret < 0) {
691 mutex_unlock(&pipe->lock);
692 return ret;
697 pipe->stream_count++;
698 mutex_unlock(&pipe->lock);
700 spin_lock_irqsave(&pipe->irqlock, flags);
701 if (vsp1_pipeline_ready(pipe))
702 vsp1_pipeline_run(pipe);
703 spin_unlock_irqrestore(&pipe->irqlock, flags);
705 return 0;
708 static int vsp1_video_stop_streaming(struct vb2_queue *vq)
710 struct vsp1_video *video = vb2_get_drv_priv(vq);
711 struct vsp1_pipeline *pipe = to_vsp1_pipeline(&video->video.entity);
712 unsigned long flags;
713 int ret;
715 mutex_lock(&pipe->lock);
716 if (--pipe->stream_count == 0) {
717 /* Stop the pipeline. */
718 ret = vsp1_pipeline_stop(pipe);
719 if (ret == -ETIMEDOUT)
720 dev_err(video->vsp1->dev, "pipeline stop timeout\n");
722 mutex_unlock(&pipe->lock);
724 vsp1_pipeline_cleanup(pipe);
725 media_entity_pipeline_stop(&video->video.entity);
727 /* Remove all buffers from the IRQ queue. */
728 spin_lock_irqsave(&video->irqlock, flags);
729 INIT_LIST_HEAD(&video->irqqueue);
730 spin_unlock_irqrestore(&video->irqlock, flags);
732 return 0;
735 static struct vb2_ops vsp1_video_queue_qops = {
736 .queue_setup = vsp1_video_queue_setup,
737 .buf_prepare = vsp1_video_buffer_prepare,
738 .buf_queue = vsp1_video_buffer_queue,
739 .wait_prepare = vb2_ops_wait_prepare,
740 .wait_finish = vb2_ops_wait_finish,
741 .start_streaming = vsp1_video_start_streaming,
742 .stop_streaming = vsp1_video_stop_streaming,
745 /* -----------------------------------------------------------------------------
746 * V4L2 ioctls
749 static int
750 vsp1_video_querycap(struct file *file, void *fh, struct v4l2_capability *cap)
752 struct v4l2_fh *vfh = file->private_data;
753 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
755 cap->capabilities = V4L2_CAP_DEVICE_CAPS | V4L2_CAP_STREAMING
756 | V4L2_CAP_VIDEO_CAPTURE_MPLANE
757 | V4L2_CAP_VIDEO_OUTPUT_MPLANE;
759 if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
760 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE
761 | V4L2_CAP_STREAMING;
762 else
763 cap->device_caps = V4L2_CAP_VIDEO_OUTPUT_MPLANE
764 | V4L2_CAP_STREAMING;
766 strlcpy(cap->driver, "vsp1", sizeof(cap->driver));
767 strlcpy(cap->card, video->video.name, sizeof(cap->card));
768 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
769 dev_name(video->vsp1->dev));
771 return 0;
774 static int
775 vsp1_video_get_format(struct file *file, void *fh, struct v4l2_format *format)
777 struct v4l2_fh *vfh = file->private_data;
778 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
780 if (format->type != video->queue.type)
781 return -EINVAL;
783 mutex_lock(&video->lock);
784 format->fmt.pix_mp = video->format;
785 mutex_unlock(&video->lock);
787 return 0;
790 static int
791 vsp1_video_try_format(struct file *file, void *fh, struct v4l2_format *format)
793 struct v4l2_fh *vfh = file->private_data;
794 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
796 if (format->type != video->queue.type)
797 return -EINVAL;
799 return __vsp1_video_try_format(video, &format->fmt.pix_mp, NULL);
802 static int
803 vsp1_video_set_format(struct file *file, void *fh, struct v4l2_format *format)
805 struct v4l2_fh *vfh = file->private_data;
806 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
807 const struct vsp1_format_info *info;
808 int ret;
810 if (format->type != video->queue.type)
811 return -EINVAL;
813 ret = __vsp1_video_try_format(video, &format->fmt.pix_mp, &info);
814 if (ret < 0)
815 return ret;
817 mutex_lock(&video->lock);
819 if (vb2_is_busy(&video->queue)) {
820 ret = -EBUSY;
821 goto done;
824 video->format = format->fmt.pix_mp;
825 video->fmtinfo = info;
827 done:
828 mutex_unlock(&video->lock);
829 return ret;
832 static int
833 vsp1_video_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
835 struct v4l2_fh *vfh = file->private_data;
836 struct vsp1_video *video = to_vsp1_video(vfh->vdev);
837 struct vsp1_pipeline *pipe;
838 int ret;
840 if (video->queue.owner && video->queue.owner != file->private_data)
841 return -EBUSY;
843 video->sequence = 0;
845 /* Start streaming on the pipeline. No link touching an entity in the
846 * pipeline can be activated or deactivated once streaming is started.
848 * Use the VSP1 pipeline object embedded in the first video object that
849 * starts streaming.
851 pipe = video->video.entity.pipe
852 ? to_vsp1_pipeline(&video->video.entity) : &video->pipe;
854 ret = media_entity_pipeline_start(&video->video.entity, &pipe->pipe);
855 if (ret < 0)
856 return ret;
858 /* Verify that the configured format matches the output of the connected
859 * subdev.
861 ret = vsp1_video_verify_format(video);
862 if (ret < 0)
863 goto err_stop;
865 ret = vsp1_pipeline_init(pipe, video);
866 if (ret < 0)
867 goto err_stop;
869 /* Start the queue. */
870 ret = vb2_streamon(&video->queue, type);
871 if (ret < 0)
872 goto err_cleanup;
874 return 0;
876 err_cleanup:
877 vsp1_pipeline_cleanup(pipe);
878 err_stop:
879 media_entity_pipeline_stop(&video->video.entity);
880 return ret;
883 static const struct v4l2_ioctl_ops vsp1_video_ioctl_ops = {
884 .vidioc_querycap = vsp1_video_querycap,
885 .vidioc_g_fmt_vid_cap_mplane = vsp1_video_get_format,
886 .vidioc_s_fmt_vid_cap_mplane = vsp1_video_set_format,
887 .vidioc_try_fmt_vid_cap_mplane = vsp1_video_try_format,
888 .vidioc_g_fmt_vid_out_mplane = vsp1_video_get_format,
889 .vidioc_s_fmt_vid_out_mplane = vsp1_video_set_format,
890 .vidioc_try_fmt_vid_out_mplane = vsp1_video_try_format,
891 .vidioc_reqbufs = vb2_ioctl_reqbufs,
892 .vidioc_querybuf = vb2_ioctl_querybuf,
893 .vidioc_qbuf = vb2_ioctl_qbuf,
894 .vidioc_dqbuf = vb2_ioctl_dqbuf,
895 .vidioc_create_bufs = vb2_ioctl_create_bufs,
896 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
897 .vidioc_streamon = vsp1_video_streamon,
898 .vidioc_streamoff = vb2_ioctl_streamoff,
901 /* -----------------------------------------------------------------------------
902 * V4L2 File Operations
905 static int vsp1_video_open(struct file *file)
907 struct vsp1_video *video = video_drvdata(file);
908 struct v4l2_fh *vfh;
909 int ret = 0;
911 vfh = kzalloc(sizeof(*vfh), GFP_KERNEL);
912 if (vfh == NULL)
913 return -ENOMEM;
915 v4l2_fh_init(vfh, &video->video);
916 v4l2_fh_add(vfh);
918 file->private_data = vfh;
920 if (!vsp1_device_get(video->vsp1)) {
921 ret = -EBUSY;
922 v4l2_fh_del(vfh);
923 kfree(vfh);
926 return ret;
929 static int vsp1_video_release(struct file *file)
931 struct vsp1_video *video = video_drvdata(file);
932 struct v4l2_fh *vfh = file->private_data;
934 mutex_lock(&video->lock);
935 if (video->queue.owner == vfh) {
936 vb2_queue_release(&video->queue);
937 video->queue.owner = NULL;
939 mutex_unlock(&video->lock);
941 vsp1_device_put(video->vsp1);
943 v4l2_fh_release(file);
945 file->private_data = NULL;
947 return 0;
950 static struct v4l2_file_operations vsp1_video_fops = {
951 .owner = THIS_MODULE,
952 .unlocked_ioctl = video_ioctl2,
953 .open = vsp1_video_open,
954 .release = vsp1_video_release,
955 .poll = vb2_fop_poll,
956 .mmap = vb2_fop_mmap,
959 /* -----------------------------------------------------------------------------
960 * Initialization and Cleanup
963 int vsp1_video_init(struct vsp1_video *video, struct vsp1_entity *rwpf)
965 const char *direction;
966 int ret;
968 switch (video->type) {
969 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
970 direction = "output";
971 video->pad.flags = MEDIA_PAD_FL_SINK;
972 break;
974 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
975 direction = "input";
976 video->pad.flags = MEDIA_PAD_FL_SOURCE;
977 video->video.vfl_dir = VFL_DIR_TX;
978 break;
980 default:
981 return -EINVAL;
984 video->rwpf = rwpf;
986 mutex_init(&video->lock);
987 spin_lock_init(&video->irqlock);
988 INIT_LIST_HEAD(&video->irqqueue);
990 mutex_init(&video->pipe.lock);
991 spin_lock_init(&video->pipe.irqlock);
992 INIT_LIST_HEAD(&video->pipe.entities);
993 init_waitqueue_head(&video->pipe.wq);
994 video->pipe.state = VSP1_PIPELINE_STOPPED;
996 /* Initialize the media entity... */
997 ret = media_entity_init(&video->video.entity, 1, &video->pad, 0);
998 if (ret < 0)
999 return ret;
1001 /* ... and the format ... */
1002 video->fmtinfo = vsp1_get_format_info(VSP1_VIDEO_DEF_FORMAT);
1003 video->format.pixelformat = video->fmtinfo->fourcc;
1004 video->format.colorspace = V4L2_COLORSPACE_SRGB;
1005 video->format.field = V4L2_FIELD_NONE;
1006 video->format.width = VSP1_VIDEO_DEF_WIDTH;
1007 video->format.height = VSP1_VIDEO_DEF_HEIGHT;
1008 video->format.num_planes = 1;
1009 video->format.plane_fmt[0].bytesperline =
1010 video->format.width * video->fmtinfo->bpp[0] / 8;
1011 video->format.plane_fmt[0].sizeimage =
1012 video->format.plane_fmt[0].bytesperline * video->format.height;
1014 /* ... and the video node... */
1015 video->video.v4l2_dev = &video->vsp1->v4l2_dev;
1016 video->video.fops = &vsp1_video_fops;
1017 snprintf(video->video.name, sizeof(video->video.name), "%s %s",
1018 rwpf->subdev.name, direction);
1019 video->video.vfl_type = VFL_TYPE_GRABBER;
1020 video->video.release = video_device_release_empty;
1021 video->video.ioctl_ops = &vsp1_video_ioctl_ops;
1023 video_set_drvdata(&video->video, video);
1025 /* ... and the buffers queue... */
1026 video->alloc_ctx = vb2_dma_contig_init_ctx(video->vsp1->dev);
1027 if (IS_ERR(video->alloc_ctx))
1028 goto error;
1030 video->queue.type = video->type;
1031 video->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1032 video->queue.lock = &video->lock;
1033 video->queue.drv_priv = video;
1034 video->queue.buf_struct_size = sizeof(struct vsp1_video_buffer);
1035 video->queue.ops = &vsp1_video_queue_qops;
1036 video->queue.mem_ops = &vb2_dma_contig_memops;
1037 video->queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1038 ret = vb2_queue_init(&video->queue);
1039 if (ret < 0) {
1040 dev_err(video->vsp1->dev, "failed to initialize vb2 queue\n");
1041 goto error;
1044 /* ... and register the video device. */
1045 video->video.queue = &video->queue;
1046 ret = video_register_device(&video->video, VFL_TYPE_GRABBER, -1);
1047 if (ret < 0) {
1048 dev_err(video->vsp1->dev, "failed to register video device\n");
1049 goto error;
1052 return 0;
1054 error:
1055 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1056 vsp1_video_cleanup(video);
1057 return ret;
1060 void vsp1_video_cleanup(struct vsp1_video *video)
1062 if (video_is_registered(&video->video))
1063 video_unregister_device(&video->video);
1065 vb2_dma_contig_cleanup_ctx(video->alloc_ctx);
1066 media_entity_cleanup(&video->video.entity);