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