Linux-libre 5.4.48-gnu
[librecmc/linux-libre.git] / drivers / staging / media / ipu3 / ipu3-v4l2.c
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
3
4 #include <linux/module.h>
5 #include <linux/pm_runtime.h>
6
7 #include <media/v4l2-event.h>
8 #include <media/v4l2-ioctl.h>
9
10 #include "ipu3.h"
11 #include "ipu3-dmamap.h"
12
13 /******************** v4l2_subdev_ops ********************/
14
15 #define IPU3_RUNNING_MODE_VIDEO         0
16 #define IPU3_RUNNING_MODE_STILL         1
17
18 static int imgu_subdev_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
19 {
20         struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
21                                                         struct imgu_v4l2_subdev,
22                                                         subdev);
23         struct imgu_device *imgu = v4l2_get_subdevdata(sd);
24         struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[imgu_sd->pipe];
25         struct v4l2_rect try_crop = {
26                 .top = 0,
27                 .left = 0,
28         };
29         unsigned int i;
30
31         try_crop.width =
32                 imgu_pipe->nodes[IMGU_NODE_IN].vdev_fmt.fmt.pix_mp.width;
33         try_crop.height =
34                 imgu_pipe->nodes[IMGU_NODE_IN].vdev_fmt.fmt.pix_mp.height;
35
36         /* Initialize try_fmt */
37         for (i = 0; i < IMGU_NODE_NUM; i++) {
38                 struct v4l2_mbus_framefmt *try_fmt =
39                         v4l2_subdev_get_try_format(sd, fh->pad, i);
40
41                 try_fmt->width = try_crop.width;
42                 try_fmt->height = try_crop.height;
43                 try_fmt->code = imgu_pipe->nodes[i].pad_fmt.code;
44                 try_fmt->field = V4L2_FIELD_NONE;
45         }
46
47         *v4l2_subdev_get_try_crop(sd, fh->pad, IMGU_NODE_IN) = try_crop;
48         *v4l2_subdev_get_try_compose(sd, fh->pad, IMGU_NODE_IN) = try_crop;
49
50         return 0;
51 }
52
53 static int imgu_subdev_s_stream(struct v4l2_subdev *sd, int enable)
54 {
55         int i;
56         unsigned int node;
57         int r = 0;
58         struct imgu_device *imgu = v4l2_get_subdevdata(sd);
59         struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
60                                                         struct imgu_v4l2_subdev,
61                                                         subdev);
62         unsigned int pipe = imgu_sd->pipe;
63         struct device *dev = &imgu->pci_dev->dev;
64         struct v4l2_pix_format_mplane *fmts[IPU3_CSS_QUEUES] = { NULL };
65         struct v4l2_rect *rects[IPU3_CSS_RECTS] = { NULL };
66         struct imgu_css_pipe *css_pipe = &imgu->css.pipes[pipe];
67         struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
68
69         dev_dbg(dev, "%s %d for pipe %u", __func__, enable, pipe);
70         /* grab ctrl after streamon and return after off */
71         v4l2_ctrl_grab(imgu_sd->ctrl, enable);
72
73         if (!enable) {
74                 imgu_sd->active = false;
75                 return 0;
76         }
77
78         for (i = 0; i < IMGU_NODE_NUM; i++)
79                 imgu_pipe->queue_enabled[i] = imgu_pipe->nodes[i].enabled;
80
81         /* This is handled specially */
82         imgu_pipe->queue_enabled[IPU3_CSS_QUEUE_PARAMS] = false;
83
84         /* Initialize CSS formats */
85         for (i = 0; i < IPU3_CSS_QUEUES; i++) {
86                 node = imgu_map_node(imgu, i);
87                 /* No need to reconfig meta nodes */
88                 if (node == IMGU_NODE_STAT_3A || node == IMGU_NODE_PARAMS)
89                         continue;
90                 fmts[i] = imgu_pipe->queue_enabled[node] ?
91                         &imgu_pipe->nodes[node].vdev_fmt.fmt.pix_mp : NULL;
92         }
93
94         /* Enable VF output only when VF queue requested by user */
95         css_pipe->vf_output_en = false;
96         if (imgu_pipe->nodes[IMGU_NODE_VF].enabled)
97                 css_pipe->vf_output_en = true;
98
99         if (atomic_read(&imgu_sd->running_mode) == IPU3_RUNNING_MODE_VIDEO)
100                 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_VIDEO;
101         else
102                 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_CAPTURE;
103
104         dev_dbg(dev, "IPU3 pipe %u pipe_id %u", pipe, css_pipe->pipe_id);
105
106         rects[IPU3_CSS_RECT_EFFECTIVE] = &imgu_sd->rect.eff;
107         rects[IPU3_CSS_RECT_BDS] = &imgu_sd->rect.bds;
108         rects[IPU3_CSS_RECT_GDC] = &imgu_sd->rect.gdc;
109
110         r = imgu_css_fmt_set(&imgu->css, fmts, rects, pipe);
111         if (r) {
112                 dev_err(dev, "failed to set initial formats pipe %u with (%d)",
113                         pipe, r);
114                 return r;
115         }
116
117         imgu_sd->active = true;
118
119         return 0;
120 }
121
122 static int imgu_subdev_get_fmt(struct v4l2_subdev *sd,
123                                struct v4l2_subdev_pad_config *cfg,
124                                struct v4l2_subdev_format *fmt)
125 {
126         struct imgu_device *imgu = v4l2_get_subdevdata(sd);
127         struct v4l2_mbus_framefmt *mf;
128         struct imgu_media_pipe *imgu_pipe;
129         u32 pad = fmt->pad;
130         struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
131                                                         struct imgu_v4l2_subdev,
132                                                         subdev);
133         unsigned int pipe = imgu_sd->pipe;
134
135         imgu_pipe = &imgu->imgu_pipe[pipe];
136         if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
137                 fmt->format = imgu_pipe->nodes[pad].pad_fmt;
138         } else {
139                 mf = v4l2_subdev_get_try_format(sd, cfg, pad);
140                 fmt->format = *mf;
141         }
142
143         return 0;
144 }
145
146 static int imgu_subdev_set_fmt(struct v4l2_subdev *sd,
147                                struct v4l2_subdev_pad_config *cfg,
148                                struct v4l2_subdev_format *fmt)
149 {
150         struct imgu_media_pipe *imgu_pipe;
151         struct imgu_device *imgu = v4l2_get_subdevdata(sd);
152         struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
153                                                         struct imgu_v4l2_subdev,
154                                                         subdev);
155
156         struct v4l2_mbus_framefmt *mf;
157         u32 pad = fmt->pad;
158         unsigned int pipe = imgu_sd->pipe;
159
160         dev_dbg(&imgu->pci_dev->dev, "set subdev %u pad %u fmt to [%ux%u]",
161                 pipe, pad, fmt->format.width, fmt->format.height);
162
163         imgu_pipe = &imgu->imgu_pipe[pipe];
164         if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
165                 mf = v4l2_subdev_get_try_format(sd, cfg, pad);
166         else
167                 mf = &imgu_pipe->nodes[pad].pad_fmt;
168
169         fmt->format.code = mf->code;
170         /* Clamp the w and h based on the hardware capabilities */
171         if (imgu_sd->subdev_pads[pad].flags & MEDIA_PAD_FL_SOURCE) {
172                 fmt->format.width = clamp(fmt->format.width,
173                                           IPU3_OUTPUT_MIN_WIDTH,
174                                           IPU3_OUTPUT_MAX_WIDTH);
175                 fmt->format.height = clamp(fmt->format.height,
176                                            IPU3_OUTPUT_MIN_HEIGHT,
177                                            IPU3_OUTPUT_MAX_HEIGHT);
178         } else {
179                 fmt->format.width = clamp(fmt->format.width,
180                                           IPU3_INPUT_MIN_WIDTH,
181                                           IPU3_INPUT_MAX_WIDTH);
182                 fmt->format.height = clamp(fmt->format.height,
183                                            IPU3_INPUT_MIN_HEIGHT,
184                                            IPU3_INPUT_MAX_HEIGHT);
185         }
186
187         *mf = fmt->format;
188
189         return 0;
190 }
191
192 static int imgu_subdev_get_selection(struct v4l2_subdev *sd,
193                                      struct v4l2_subdev_pad_config *cfg,
194                                      struct v4l2_subdev_selection *sel)
195 {
196         struct v4l2_rect *try_sel, *r;
197         struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
198                                                         struct imgu_v4l2_subdev,
199                                                         subdev);
200
201         if (sel->pad != IMGU_NODE_IN)
202                 return -EINVAL;
203
204         switch (sel->target) {
205         case V4L2_SEL_TGT_CROP:
206                 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
207                 r = &imgu_sd->rect.eff;
208                 break;
209         case V4L2_SEL_TGT_COMPOSE:
210                 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
211                 r = &imgu_sd->rect.bds;
212                 break;
213         default:
214                 return -EINVAL;
215         }
216
217         if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
218                 sel->r = *try_sel;
219         else
220                 sel->r = *r;
221
222         return 0;
223 }
224
225 static int imgu_subdev_set_selection(struct v4l2_subdev *sd,
226                                      struct v4l2_subdev_pad_config *cfg,
227                                      struct v4l2_subdev_selection *sel)
228 {
229         struct imgu_device *imgu = v4l2_get_subdevdata(sd);
230         struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
231                                                         struct imgu_v4l2_subdev,
232                                                         subdev);
233         struct v4l2_rect *rect, *try_sel;
234
235         dev_dbg(&imgu->pci_dev->dev,
236                  "set subdev %u sel which %u target 0x%4x rect [%ux%u]",
237                  imgu_sd->pipe, sel->which, sel->target,
238                  sel->r.width, sel->r.height);
239
240         if (sel->pad != IMGU_NODE_IN)
241                 return -EINVAL;
242
243         switch (sel->target) {
244         case V4L2_SEL_TGT_CROP:
245                 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
246                 rect = &imgu_sd->rect.eff;
247                 break;
248         case V4L2_SEL_TGT_COMPOSE:
249                 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
250                 rect = &imgu_sd->rect.bds;
251                 break;
252         default:
253                 return -EINVAL;
254         }
255
256         if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
257                 *try_sel = sel->r;
258         else
259                 *rect = sel->r;
260
261         return 0;
262 }
263
264 /******************** media_entity_operations ********************/
265
266 static int imgu_link_setup(struct media_entity *entity,
267                            const struct media_pad *local,
268                            const struct media_pad *remote, u32 flags)
269 {
270         struct imgu_media_pipe *imgu_pipe;
271         struct v4l2_subdev *sd = container_of(entity, struct v4l2_subdev,
272                                               entity);
273         struct imgu_device *imgu = v4l2_get_subdevdata(sd);
274         struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
275                                                         struct imgu_v4l2_subdev,
276                                                         subdev);
277         unsigned int pipe = imgu_sd->pipe;
278         u32 pad = local->index;
279
280         WARN_ON(pad >= IMGU_NODE_NUM);
281
282         dev_dbg(&imgu->pci_dev->dev, "pipe %u pad %u is %s", pipe, pad,
283                  flags & MEDIA_LNK_FL_ENABLED ? "enabled" : "disabled");
284
285         imgu_pipe = &imgu->imgu_pipe[pipe];
286         imgu_pipe->nodes[pad].enabled = flags & MEDIA_LNK_FL_ENABLED;
287
288         /* enable input node to enable the pipe */
289         if (pad != IMGU_NODE_IN)
290                 return 0;
291
292         if (flags & MEDIA_LNK_FL_ENABLED)
293                 __set_bit(pipe, imgu->css.enabled_pipes);
294         else
295                 __clear_bit(pipe, imgu->css.enabled_pipes);
296
297         dev_dbg(&imgu->pci_dev->dev, "pipe %u is %s", pipe,
298                  flags & MEDIA_LNK_FL_ENABLED ? "enabled" : "disabled");
299
300         return 0;
301 }
302
303 /******************** vb2_ops ********************/
304
305 static int imgu_vb2_buf_init(struct vb2_buffer *vb)
306 {
307         struct sg_table *sg = vb2_dma_sg_plane_desc(vb, 0);
308         struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
309         struct imgu_buffer *buf = container_of(vb,
310                 struct imgu_buffer, vid_buf.vbb.vb2_buf);
311         struct imgu_video_device *node =
312                 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
313         unsigned int queue = imgu_node_to_queue(node->id);
314
315         if (queue == IPU3_CSS_QUEUE_PARAMS)
316                 return 0;
317
318         return imgu_dmamap_map_sg(imgu, sg->sgl, sg->nents, &buf->map);
319 }
320
321 /* Called when each buffer is freed */
322 static void imgu_vb2_buf_cleanup(struct vb2_buffer *vb)
323 {
324         struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
325         struct imgu_buffer *buf = container_of(vb,
326                 struct imgu_buffer, vid_buf.vbb.vb2_buf);
327         struct imgu_video_device *node =
328                 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
329         unsigned int queue = imgu_node_to_queue(node->id);
330
331         if (queue == IPU3_CSS_QUEUE_PARAMS)
332                 return;
333
334         imgu_dmamap_unmap(imgu, &buf->map);
335 }
336
337 /* Transfer buffer ownership to me */
338 static void imgu_vb2_buf_queue(struct vb2_buffer *vb)
339 {
340         struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
341         struct imgu_video_device *node =
342                 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
343         unsigned int queue = imgu_node_to_queue(node->id);
344         struct imgu_buffer *buf = container_of(vb, struct imgu_buffer,
345                                                vid_buf.vbb.vb2_buf);
346         unsigned long need_bytes;
347         unsigned long payload = vb2_get_plane_payload(vb, 0);
348
349         if (vb->vb2_queue->type == V4L2_BUF_TYPE_META_CAPTURE ||
350             vb->vb2_queue->type == V4L2_BUF_TYPE_META_OUTPUT)
351                 need_bytes = node->vdev_fmt.fmt.meta.buffersize;
352         else
353                 need_bytes = node->vdev_fmt.fmt.pix_mp.plane_fmt[0].sizeimage;
354
355         if (queue == IPU3_CSS_QUEUE_PARAMS && payload && payload < need_bytes) {
356                 dev_err(&imgu->pci_dev->dev, "invalid data size for params.");
357                 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
358                 return;
359         }
360
361         mutex_lock(&imgu->lock);
362         if (queue != IPU3_CSS_QUEUE_PARAMS)
363                 imgu_css_buf_init(&buf->css_buf, queue, buf->map.daddr);
364
365         list_add_tail(&buf->vid_buf.list, &node->buffers);
366         mutex_unlock(&imgu->lock);
367
368         vb2_set_plane_payload(vb, 0, need_bytes);
369
370         mutex_lock(&imgu->streaming_lock);
371         if (imgu->streaming)
372                 imgu_queue_buffers(imgu, false, node->pipe);
373         mutex_unlock(&imgu->streaming_lock);
374
375         dev_dbg(&imgu->pci_dev->dev, "%s for pipe %u node %u", __func__,
376                 node->pipe, node->id);
377 }
378
379 static int imgu_vb2_queue_setup(struct vb2_queue *vq,
380                                 unsigned int *num_buffers,
381                                 unsigned int *num_planes,
382                                 unsigned int sizes[],
383                                 struct device *alloc_devs[])
384 {
385         struct imgu_device *imgu = vb2_get_drv_priv(vq);
386         struct imgu_video_device *node =
387                 container_of(vq, struct imgu_video_device, vbq);
388         const struct v4l2_format *fmt = &node->vdev_fmt;
389         unsigned int size;
390
391         *num_buffers = clamp_val(*num_buffers, 1, VB2_MAX_FRAME);
392         alloc_devs[0] = &imgu->pci_dev->dev;
393
394         if (vq->type == V4L2_BUF_TYPE_META_CAPTURE ||
395             vq->type == V4L2_BUF_TYPE_META_OUTPUT)
396                 size = fmt->fmt.meta.buffersize;
397         else
398                 size = fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
399
400         if (*num_planes) {
401                 if (sizes[0] < size)
402                         return -EINVAL;
403                 size = sizes[0];
404         }
405
406         *num_planes = 1;
407         sizes[0] = size;
408
409         /* Initialize buffer queue */
410         INIT_LIST_HEAD(&node->buffers);
411
412         return 0;
413 }
414
415 /* Check if all enabled video nodes are streaming, exception ignored */
416 static bool imgu_all_nodes_streaming(struct imgu_device *imgu,
417                                      struct imgu_video_device *except)
418 {
419         unsigned int i, pipe, p;
420         struct imgu_video_device *node;
421         struct device *dev = &imgu->pci_dev->dev;
422
423         pipe = except->pipe;
424         if (!test_bit(pipe, imgu->css.enabled_pipes)) {
425                 dev_warn(&imgu->pci_dev->dev,
426                          "pipe %u link is not ready yet", pipe);
427                 return false;
428         }
429
430         for_each_set_bit(p, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) {
431                 for (i = 0; i < IMGU_NODE_NUM; i++) {
432                         node = &imgu->imgu_pipe[p].nodes[i];
433                         dev_dbg(dev, "%s pipe %u queue %u name %s enabled = %u",
434                                 __func__, p, i, node->name, node->enabled);
435                         if (node == except)
436                                 continue;
437                         if (node->enabled && !vb2_start_streaming_called(&node->vbq))
438                                 return false;
439                 }
440         }
441
442         return true;
443 }
444
445 static void imgu_return_all_buffers(struct imgu_device *imgu,
446                                     struct imgu_video_device *node,
447                                     enum vb2_buffer_state state)
448 {
449         struct imgu_vb2_buffer *b, *b0;
450
451         /* Return all buffers */
452         mutex_lock(&imgu->lock);
453         list_for_each_entry_safe(b, b0, &node->buffers, list) {
454                 list_del(&b->list);
455                 vb2_buffer_done(&b->vbb.vb2_buf, state);
456         }
457         mutex_unlock(&imgu->lock);
458 }
459
460 static int imgu_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
461 {
462         struct imgu_media_pipe *imgu_pipe;
463         struct imgu_device *imgu = vb2_get_drv_priv(vq);
464         struct device *dev = &imgu->pci_dev->dev;
465         struct imgu_video_device *node =
466                 container_of(vq, struct imgu_video_device, vbq);
467         int r;
468         unsigned int pipe;
469
470         dev_dbg(dev, "%s node name %s pipe %u id %u", __func__,
471                 node->name, node->pipe, node->id);
472
473         mutex_lock(&imgu->streaming_lock);
474         if (imgu->streaming) {
475                 r = -EBUSY;
476                 mutex_unlock(&imgu->streaming_lock);
477                 goto fail_return_bufs;
478         }
479         mutex_unlock(&imgu->streaming_lock);
480
481         if (!node->enabled) {
482                 dev_err(dev, "IMGU node is not enabled");
483                 r = -EINVAL;
484                 goto fail_return_bufs;
485         }
486
487         pipe = node->pipe;
488         imgu_pipe = &imgu->imgu_pipe[pipe];
489         r = media_pipeline_start(&node->vdev.entity, &imgu_pipe->pipeline);
490         if (r < 0)
491                 goto fail_return_bufs;
492
493
494         if (!imgu_all_nodes_streaming(imgu, node))
495                 return 0;
496
497         for_each_set_bit(pipe, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) {
498                 r = v4l2_subdev_call(&imgu->imgu_pipe[pipe].imgu_sd.subdev,
499                                      video, s_stream, 1);
500                 if (r < 0)
501                         goto fail_stop_pipeline;
502         }
503
504         /* Start streaming of the whole pipeline now */
505         dev_dbg(dev, "IMGU streaming is ready to start");
506         mutex_lock(&imgu->streaming_lock);
507         r = imgu_s_stream(imgu, true);
508         if (!r)
509                 imgu->streaming = true;
510         mutex_unlock(&imgu->streaming_lock);
511
512         return 0;
513
514 fail_stop_pipeline:
515         media_pipeline_stop(&node->vdev.entity);
516 fail_return_bufs:
517         imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_QUEUED);
518
519         return r;
520 }
521
522 static void imgu_vb2_stop_streaming(struct vb2_queue *vq)
523 {
524         struct imgu_media_pipe *imgu_pipe;
525         struct imgu_device *imgu = vb2_get_drv_priv(vq);
526         struct device *dev = &imgu->pci_dev->dev;
527         struct imgu_video_device *node =
528                 container_of(vq, struct imgu_video_device, vbq);
529         int r;
530         unsigned int pipe;
531
532         WARN_ON(!node->enabled);
533
534         pipe = node->pipe;
535         dev_dbg(dev, "Try to stream off node [%u][%u]", pipe, node->id);
536         imgu_pipe = &imgu->imgu_pipe[pipe];
537         r = v4l2_subdev_call(&imgu_pipe->imgu_sd.subdev, video, s_stream, 0);
538         if (r)
539                 dev_err(&imgu->pci_dev->dev,
540                         "failed to stop subdev streaming\n");
541
542         mutex_lock(&imgu->streaming_lock);
543         /* Was this the first node with streaming disabled? */
544         if (imgu->streaming && imgu_all_nodes_streaming(imgu, node)) {
545                 /* Yes, really stop streaming now */
546                 dev_dbg(dev, "IMGU streaming is ready to stop");
547                 r = imgu_s_stream(imgu, false);
548                 if (!r)
549                         imgu->streaming = false;
550         }
551
552         imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_ERROR);
553         mutex_unlock(&imgu->streaming_lock);
554
555         media_pipeline_stop(&node->vdev.entity);
556 }
557
558 /******************** v4l2_ioctl_ops ********************/
559
560 #define VID_CAPTURE     0
561 #define VID_OUTPUT      1
562 #define DEF_VID_CAPTURE 0
563 #define DEF_VID_OUTPUT  1
564
565 struct imgu_fmt {
566         u32     fourcc;
567         u16     type; /* VID_CAPTURE or VID_OUTPUT not both */
568 };
569
570 /* format descriptions for capture and preview */
571 static const struct imgu_fmt formats[] = {
572         { V4L2_PIX_FMT_NV12, VID_CAPTURE },
573         { V4L2_PIX_FMT_IPU3_SGRBG10, VID_OUTPUT },
574         { V4L2_PIX_FMT_IPU3_SBGGR10, VID_OUTPUT },
575         { V4L2_PIX_FMT_IPU3_SGBRG10, VID_OUTPUT },
576         { V4L2_PIX_FMT_IPU3_SRGGB10, VID_OUTPUT },
577 };
578
579 /* Find the first matched format, return default if not found */
580 static const struct imgu_fmt *find_format(struct v4l2_format *f, u32 type)
581 {
582         unsigned int i;
583
584         for (i = 0; i < ARRAY_SIZE(formats); i++) {
585                 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
586                     formats[i].type == type)
587                         return &formats[i];
588         }
589
590         return type == VID_CAPTURE ? &formats[DEF_VID_CAPTURE] :
591                                      &formats[DEF_VID_OUTPUT];
592 }
593
594 static int imgu_vidioc_querycap(struct file *file, void *fh,
595                                 struct v4l2_capability *cap)
596 {
597         struct imgu_video_device *node = file_to_intel_imgu_node(file);
598
599         strscpy(cap->driver, IMGU_NAME, sizeof(cap->driver));
600         strscpy(cap->card, IMGU_NAME, sizeof(cap->card));
601         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", node->name);
602
603         return 0;
604 }
605
606 static int enum_fmts(struct v4l2_fmtdesc *f, u32 type)
607 {
608         unsigned int i, j;
609
610         for (i = j = 0; i < ARRAY_SIZE(formats); ++i) {
611                 if (formats[i].type == type) {
612                         if (j == f->index)
613                                 break;
614                         ++j;
615                 }
616         }
617
618         if (i < ARRAY_SIZE(formats)) {
619                 f->pixelformat = formats[i].fourcc;
620                 return 0;
621         }
622
623         return -EINVAL;
624 }
625
626 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
627                                    struct v4l2_fmtdesc *f)
628 {
629         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
630                 return -EINVAL;
631
632         return enum_fmts(f, VID_CAPTURE);
633 }
634
635 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
636                                    struct v4l2_fmtdesc *f)
637 {
638         if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
639                 return -EINVAL;
640
641         return enum_fmts(f, VID_OUTPUT);
642 }
643
644 /* Propagate forward always the format from the CIO2 subdev */
645 static int imgu_vidioc_g_fmt(struct file *file, void *fh,
646                              struct v4l2_format *f)
647 {
648         struct imgu_video_device *node = file_to_intel_imgu_node(file);
649
650         f->fmt = node->vdev_fmt.fmt;
651
652         return 0;
653 }
654
655 /*
656  * Set input/output format. Unless it is just a try, this also resets
657  * selections (ie. effective and BDS resolutions) to defaults.
658  */
659 static int imgu_fmt(struct imgu_device *imgu, unsigned int pipe, int node,
660                     struct v4l2_format *f, bool try)
661 {
662         struct device *dev = &imgu->pci_dev->dev;
663         struct v4l2_pix_format_mplane *fmts[IPU3_CSS_QUEUES] = { NULL };
664         struct v4l2_rect *rects[IPU3_CSS_RECTS] = { NULL };
665         struct v4l2_mbus_framefmt pad_fmt;
666         unsigned int i, css_q;
667         int ret;
668         struct imgu_css_pipe *css_pipe = &imgu->css.pipes[pipe];
669         struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
670         struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd;
671
672         dev_dbg(dev, "set fmt node [%u][%u](try = %u)", pipe, node, try);
673
674         for (i = 0; i < IMGU_NODE_NUM; i++)
675                 dev_dbg(dev, "IMGU pipe %u node %u enabled = %u",
676                         pipe, i, imgu_pipe->nodes[i].enabled);
677
678         if (imgu_pipe->nodes[IMGU_NODE_VF].enabled)
679                 css_pipe->vf_output_en = true;
680
681         if (atomic_read(&imgu_sd->running_mode) == IPU3_RUNNING_MODE_VIDEO)
682                 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_VIDEO;
683         else
684                 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_CAPTURE;
685
686         dev_dbg(dev, "IPU3 pipe %u pipe_id = %u", pipe, css_pipe->pipe_id);
687
688         for (i = 0; i < IPU3_CSS_QUEUES; i++) {
689                 unsigned int inode = imgu_map_node(imgu, i);
690
691                 /* Skip the meta node */
692                 if (inode == IMGU_NODE_STAT_3A || inode == IMGU_NODE_PARAMS)
693                         continue;
694
695                 if (try) {
696                         fmts[i] = kmemdup(&imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp,
697                                           sizeof(struct v4l2_pix_format_mplane),
698                                           GFP_KERNEL);
699                         if (!fmts[i]) {
700                                 ret = -ENOMEM;
701                                 goto out;
702                         }
703                 } else {
704                         fmts[i] = &imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp;
705                 }
706
707                 /* CSS expects some format on OUT queue */
708                 if (i != IPU3_CSS_QUEUE_OUT &&
709                     !imgu_pipe->nodes[inode].enabled)
710                         fmts[i] = NULL;
711         }
712
713         if (!try) {
714                 /* eff and bds res got by imgu_s_sel */
715                 struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd;
716
717                 rects[IPU3_CSS_RECT_EFFECTIVE] = &imgu_sd->rect.eff;
718                 rects[IPU3_CSS_RECT_BDS] = &imgu_sd->rect.bds;
719                 rects[IPU3_CSS_RECT_GDC] = &imgu_sd->rect.gdc;
720
721                 /* suppose that pad fmt was set by subdev s_fmt before */
722                 pad_fmt = imgu_pipe->nodes[IMGU_NODE_IN].pad_fmt;
723                 rects[IPU3_CSS_RECT_GDC]->width = pad_fmt.width;
724                 rects[IPU3_CSS_RECT_GDC]->height = pad_fmt.height;
725         }
726
727         /*
728          * imgu doesn't set the node to the value given by user
729          * before we return success from this function, so set it here.
730          */
731         css_q = imgu_node_to_queue(node);
732         if (!fmts[css_q]) {
733                 ret = -EINVAL;
734                 goto out;
735         }
736         *fmts[css_q] = f->fmt.pix_mp;
737
738         if (try)
739                 ret = imgu_css_fmt_try(&imgu->css, fmts, rects, pipe);
740         else
741                 ret = imgu_css_fmt_set(&imgu->css, fmts, rects, pipe);
742
743         /* ret is the binary number in the firmware blob */
744         if (ret < 0)
745                 goto out;
746
747         if (try)
748                 f->fmt.pix_mp = *fmts[css_q];
749         else
750                 f->fmt = imgu_pipe->nodes[node].vdev_fmt.fmt;
751
752 out:
753         if (try) {
754                 for (i = 0; i < IPU3_CSS_QUEUES; i++)
755                         kfree(fmts[i]);
756         }
757
758         return ret;
759 }
760
761 static int imgu_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
762 {
763         struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
764         const struct imgu_fmt *fmt;
765
766         if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
767                 fmt = find_format(f, VID_CAPTURE);
768         else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
769                 fmt = find_format(f, VID_OUTPUT);
770         else
771                 return -EINVAL;
772
773         pixm->pixelformat = fmt->fourcc;
774
775         memset(pixm->plane_fmt[0].reserved, 0,
776                sizeof(pixm->plane_fmt[0].reserved));
777
778         return 0;
779 }
780
781 static int imgu_vidioc_try_fmt(struct file *file, void *fh,
782                                struct v4l2_format *f)
783 {
784         struct imgu_device *imgu = video_drvdata(file);
785         struct device *dev = &imgu->pci_dev->dev;
786         struct imgu_video_device *node = file_to_intel_imgu_node(file);
787         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
788         int r;
789
790         dev_dbg(dev, "%s [%ux%u] for node %u\n", __func__,
791                 pix_mp->width, pix_mp->height, node->id);
792
793         r = imgu_try_fmt(file, fh, f);
794         if (r)
795                 return r;
796
797         return imgu_fmt(imgu, node->pipe, node->id, f, true);
798 }
799
800 static int imgu_vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
801 {
802         struct imgu_device *imgu = video_drvdata(file);
803         struct device *dev = &imgu->pci_dev->dev;
804         struct imgu_video_device *node = file_to_intel_imgu_node(file);
805         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
806         int r;
807
808         dev_dbg(dev, "%s [%ux%u] for node %u\n", __func__,
809                 pix_mp->width, pix_mp->height, node->id);
810
811         r = imgu_try_fmt(file, fh, f);
812         if (r)
813                 return r;
814
815         return imgu_fmt(imgu, node->pipe, node->id, f, false);
816 }
817
818 struct imgu_meta_fmt {
819         __u32 fourcc;
820         char *name;
821 };
822
823 /* From drivers/media/v4l2-core/v4l2-ioctl.c */
824 static const struct imgu_meta_fmt meta_fmts[] = {
825         { V4L2_META_FMT_IPU3_PARAMS, "IPU3 processing parameters" },
826         { V4L2_META_FMT_IPU3_STAT_3A, "IPU3 3A statistics" },
827 };
828
829 static int imgu_meta_enum_format(struct file *file, void *fh,
830                                  struct v4l2_fmtdesc *fmt)
831 {
832         struct imgu_video_device *node = file_to_intel_imgu_node(file);
833         unsigned int i = fmt->type == V4L2_BUF_TYPE_META_OUTPUT ? 0 : 1;
834
835         /* Each node is dedicated to only one meta format */
836         if (fmt->index > 0 || fmt->type != node->vbq.type)
837                 return -EINVAL;
838
839         strscpy(fmt->description, meta_fmts[i].name, sizeof(fmt->description));
840         fmt->pixelformat = meta_fmts[i].fourcc;
841
842         return 0;
843 }
844
845 static int imgu_vidioc_g_meta_fmt(struct file *file, void *fh,
846                                   struct v4l2_format *f)
847 {
848         struct imgu_video_device *node = file_to_intel_imgu_node(file);
849
850         if (f->type != node->vbq.type)
851                 return -EINVAL;
852
853         f->fmt = node->vdev_fmt.fmt;
854
855         return 0;
856 }
857
858 static int imgu_vidioc_enum_input(struct file *file, void *fh,
859                                   struct v4l2_input *input)
860 {
861         if (input->index > 0)
862                 return -EINVAL;
863         strscpy(input->name, "camera", sizeof(input->name));
864         input->type = V4L2_INPUT_TYPE_CAMERA;
865
866         return 0;
867 }
868
869 static int imgu_vidioc_g_input(struct file *file, void *fh, unsigned int *input)
870 {
871         *input = 0;
872
873         return 0;
874 }
875
876 static int imgu_vidioc_s_input(struct file *file, void *fh, unsigned int input)
877 {
878         return input == 0 ? 0 : -EINVAL;
879 }
880
881 static int imgu_vidioc_enum_output(struct file *file, void *fh,
882                                    struct v4l2_output *output)
883 {
884         if (output->index > 0)
885                 return -EINVAL;
886         strscpy(output->name, "camera", sizeof(output->name));
887         output->type = V4L2_INPUT_TYPE_CAMERA;
888
889         return 0;
890 }
891
892 static int imgu_vidioc_g_output(struct file *file, void *fh,
893                                 unsigned int *output)
894 {
895         *output = 0;
896
897         return 0;
898 }
899
900 static int imgu_vidioc_s_output(struct file *file, void *fh,
901                                 unsigned int output)
902 {
903         return output == 0 ? 0 : -EINVAL;
904 }
905
906 /******************** function pointers ********************/
907
908 static struct v4l2_subdev_internal_ops imgu_subdev_internal_ops = {
909         .open = imgu_subdev_open,
910 };
911
912 static const struct v4l2_subdev_core_ops imgu_subdev_core_ops = {
913         .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
914         .unsubscribe_event = v4l2_event_subdev_unsubscribe,
915 };
916
917 static const struct v4l2_subdev_video_ops imgu_subdev_video_ops = {
918         .s_stream = imgu_subdev_s_stream,
919 };
920
921 static const struct v4l2_subdev_pad_ops imgu_subdev_pad_ops = {
922         .link_validate = v4l2_subdev_link_validate_default,
923         .get_fmt = imgu_subdev_get_fmt,
924         .set_fmt = imgu_subdev_set_fmt,
925         .get_selection = imgu_subdev_get_selection,
926         .set_selection = imgu_subdev_set_selection,
927 };
928
929 static const struct v4l2_subdev_ops imgu_subdev_ops = {
930         .core = &imgu_subdev_core_ops,
931         .video = &imgu_subdev_video_ops,
932         .pad = &imgu_subdev_pad_ops,
933 };
934
935 static const struct media_entity_operations imgu_media_ops = {
936         .link_setup = imgu_link_setup,
937         .link_validate = v4l2_subdev_link_validate,
938 };
939
940 /****************** vb2_ops of the Q ********************/
941
942 static const struct vb2_ops imgu_vb2_ops = {
943         .buf_init = imgu_vb2_buf_init,
944         .buf_cleanup = imgu_vb2_buf_cleanup,
945         .buf_queue = imgu_vb2_buf_queue,
946         .queue_setup = imgu_vb2_queue_setup,
947         .start_streaming = imgu_vb2_start_streaming,
948         .stop_streaming = imgu_vb2_stop_streaming,
949         .wait_prepare = vb2_ops_wait_prepare,
950         .wait_finish = vb2_ops_wait_finish,
951 };
952
953 /****************** v4l2_file_operations *****************/
954
955 static const struct v4l2_file_operations imgu_v4l2_fops = {
956         .unlocked_ioctl = video_ioctl2,
957         .open = v4l2_fh_open,
958         .release = vb2_fop_release,
959         .poll = vb2_fop_poll,
960         .mmap = vb2_fop_mmap,
961 };
962
963 /******************** v4l2_ioctl_ops ********************/
964
965 static const struct v4l2_ioctl_ops imgu_v4l2_ioctl_ops = {
966         .vidioc_querycap = imgu_vidioc_querycap,
967
968         .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
969         .vidioc_g_fmt_vid_cap_mplane = imgu_vidioc_g_fmt,
970         .vidioc_s_fmt_vid_cap_mplane = imgu_vidioc_s_fmt,
971         .vidioc_try_fmt_vid_cap_mplane = imgu_vidioc_try_fmt,
972
973         .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
974         .vidioc_g_fmt_vid_out_mplane = imgu_vidioc_g_fmt,
975         .vidioc_s_fmt_vid_out_mplane = imgu_vidioc_s_fmt,
976         .vidioc_try_fmt_vid_out_mplane = imgu_vidioc_try_fmt,
977
978         .vidioc_enum_output = imgu_vidioc_enum_output,
979         .vidioc_g_output = imgu_vidioc_g_output,
980         .vidioc_s_output = imgu_vidioc_s_output,
981
982         .vidioc_enum_input = imgu_vidioc_enum_input,
983         .vidioc_g_input = imgu_vidioc_g_input,
984         .vidioc_s_input = imgu_vidioc_s_input,
985
986         /* buffer queue management */
987         .vidioc_reqbufs = vb2_ioctl_reqbufs,
988         .vidioc_create_bufs = vb2_ioctl_create_bufs,
989         .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
990         .vidioc_querybuf = vb2_ioctl_querybuf,
991         .vidioc_qbuf = vb2_ioctl_qbuf,
992         .vidioc_dqbuf = vb2_ioctl_dqbuf,
993         .vidioc_streamon = vb2_ioctl_streamon,
994         .vidioc_streamoff = vb2_ioctl_streamoff,
995         .vidioc_expbuf = vb2_ioctl_expbuf,
996 };
997
998 static const struct v4l2_ioctl_ops imgu_v4l2_meta_ioctl_ops = {
999         .vidioc_querycap = imgu_vidioc_querycap,
1000
1001         /* meta capture */
1002         .vidioc_enum_fmt_meta_cap = imgu_meta_enum_format,
1003         .vidioc_g_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
1004         .vidioc_s_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
1005         .vidioc_try_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
1006
1007         /* meta output */
1008         .vidioc_enum_fmt_meta_out = imgu_meta_enum_format,
1009         .vidioc_g_fmt_meta_out = imgu_vidioc_g_meta_fmt,
1010         .vidioc_s_fmt_meta_out = imgu_vidioc_g_meta_fmt,
1011         .vidioc_try_fmt_meta_out = imgu_vidioc_g_meta_fmt,
1012
1013         .vidioc_reqbufs = vb2_ioctl_reqbufs,
1014         .vidioc_create_bufs = vb2_ioctl_create_bufs,
1015         .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1016         .vidioc_querybuf = vb2_ioctl_querybuf,
1017         .vidioc_qbuf = vb2_ioctl_qbuf,
1018         .vidioc_dqbuf = vb2_ioctl_dqbuf,
1019         .vidioc_streamon = vb2_ioctl_streamon,
1020         .vidioc_streamoff = vb2_ioctl_streamoff,
1021         .vidioc_expbuf = vb2_ioctl_expbuf,
1022 };
1023
1024 static int imgu_sd_s_ctrl(struct v4l2_ctrl *ctrl)
1025 {
1026         struct imgu_v4l2_subdev *imgu_sd =
1027                 container_of(ctrl->handler, struct imgu_v4l2_subdev, ctrl_handler);
1028         struct imgu_device *imgu = v4l2_get_subdevdata(&imgu_sd->subdev);
1029         struct device *dev = &imgu->pci_dev->dev;
1030
1031         dev_dbg(dev, "set val %d to ctrl 0x%8x for subdev %u",
1032                 ctrl->val, ctrl->id, imgu_sd->pipe);
1033
1034         switch (ctrl->id) {
1035         case V4L2_CID_INTEL_IPU3_MODE:
1036                 atomic_set(&imgu_sd->running_mode, ctrl->val);
1037                 return 0;
1038         default:
1039                 return -EINVAL;
1040         }
1041 }
1042
1043 static const struct v4l2_ctrl_ops imgu_subdev_ctrl_ops = {
1044         .s_ctrl = imgu_sd_s_ctrl,
1045 };
1046
1047 static const char * const imgu_ctrl_mode_strings[] = {
1048         "Video mode",
1049         "Still mode",
1050 };
1051
1052 static const struct v4l2_ctrl_config imgu_subdev_ctrl_mode = {
1053         .ops = &imgu_subdev_ctrl_ops,
1054         .id = V4L2_CID_INTEL_IPU3_MODE,
1055         .name = "IPU3 Pipe Mode",
1056         .type = V4L2_CTRL_TYPE_MENU,
1057         .max = ARRAY_SIZE(imgu_ctrl_mode_strings) - 1,
1058         .def = IPU3_RUNNING_MODE_VIDEO,
1059         .qmenu = imgu_ctrl_mode_strings,
1060 };
1061
1062 /******************** Framework registration ********************/
1063
1064 /* helper function to config node's video properties */
1065 static void imgu_node_to_v4l2(u32 node, struct video_device *vdev,
1066                               struct v4l2_format *f)
1067 {
1068         u32 cap;
1069
1070         /* Should not happen */
1071         WARN_ON(node >= IMGU_NODE_NUM);
1072
1073         switch (node) {
1074         case IMGU_NODE_IN:
1075                 cap = V4L2_CAP_VIDEO_OUTPUT_MPLANE;
1076                 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1077                 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops;
1078                 break;
1079         case IMGU_NODE_PARAMS:
1080                 cap = V4L2_CAP_META_OUTPUT;
1081                 f->type = V4L2_BUF_TYPE_META_OUTPUT;
1082                 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_PARAMS;
1083                 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops;
1084                 imgu_css_meta_fmt_set(&f->fmt.meta);
1085                 break;
1086         case IMGU_NODE_STAT_3A:
1087                 cap = V4L2_CAP_META_CAPTURE;
1088                 f->type = V4L2_BUF_TYPE_META_CAPTURE;
1089                 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_STAT_3A;
1090                 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops;
1091                 imgu_css_meta_fmt_set(&f->fmt.meta);
1092                 break;
1093         default:
1094                 cap = V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1095                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1096                 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops;
1097         }
1098
1099         vdev->device_caps = V4L2_CAP_STREAMING | cap;
1100 }
1101
1102 static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
1103                                      struct imgu_v4l2_subdev *imgu_sd,
1104                                      unsigned int pipe)
1105 {
1106         int i, r;
1107         struct v4l2_ctrl_handler *hdl = &imgu_sd->ctrl_handler;
1108         struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1109
1110         /* Initialize subdev media entity */
1111         r = media_entity_pads_init(&imgu_sd->subdev.entity, IMGU_NODE_NUM,
1112                                    imgu_sd->subdev_pads);
1113         if (r) {
1114                 dev_err(&imgu->pci_dev->dev,
1115                         "failed initialize subdev media entity (%d)\n", r);
1116                 return r;
1117         }
1118         imgu_sd->subdev.entity.ops = &imgu_media_ops;
1119         for (i = 0; i < IMGU_NODE_NUM; i++) {
1120                 imgu_sd->subdev_pads[i].flags = imgu_pipe->nodes[i].output ?
1121                         MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1122         }
1123
1124         /* Initialize subdev */
1125         v4l2_subdev_init(&imgu_sd->subdev, &imgu_subdev_ops);
1126         imgu_sd->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_STATISTICS;
1127         imgu_sd->subdev.internal_ops = &imgu_subdev_internal_ops;
1128         imgu_sd->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE |
1129                                 V4L2_SUBDEV_FL_HAS_EVENTS;
1130         snprintf(imgu_sd->subdev.name, sizeof(imgu_sd->subdev.name),
1131                  "%s %u", IMGU_NAME, pipe);
1132         v4l2_set_subdevdata(&imgu_sd->subdev, imgu);
1133         atomic_set(&imgu_sd->running_mode, IPU3_RUNNING_MODE_VIDEO);
1134         v4l2_ctrl_handler_init(hdl, 1);
1135         imgu_sd->subdev.ctrl_handler = hdl;
1136         imgu_sd->ctrl = v4l2_ctrl_new_custom(hdl, &imgu_subdev_ctrl_mode, NULL);
1137         if (hdl->error) {
1138                 r = hdl->error;
1139                 dev_err(&imgu->pci_dev->dev,
1140                         "failed to create subdev v4l2 ctrl with err %d", r);
1141                 goto fail_subdev;
1142         }
1143         r = v4l2_device_register_subdev(&imgu->v4l2_dev, &imgu_sd->subdev);
1144         if (r) {
1145                 dev_err(&imgu->pci_dev->dev,
1146                         "failed initialize subdev (%d)\n", r);
1147                 goto fail_subdev;
1148         }
1149
1150         imgu_sd->pipe = pipe;
1151         return 0;
1152
1153 fail_subdev:
1154         v4l2_ctrl_handler_free(imgu_sd->subdev.ctrl_handler);
1155         media_entity_cleanup(&imgu_sd->subdev.entity);
1156
1157         return r;
1158 }
1159
1160 static int imgu_v4l2_node_setup(struct imgu_device *imgu, unsigned int pipe,
1161                                 int node_num)
1162 {
1163         int r;
1164         u32 flags;
1165         struct v4l2_mbus_framefmt def_bus_fmt = { 0 };
1166         struct v4l2_pix_format_mplane def_pix_fmt = { 0 };
1167         struct device *dev = &imgu->pci_dev->dev;
1168         struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1169         struct v4l2_subdev *sd = &imgu_pipe->imgu_sd.subdev;
1170         struct imgu_video_device *node = &imgu_pipe->nodes[node_num];
1171         struct video_device *vdev = &node->vdev;
1172         struct vb2_queue *vbq = &node->vbq;
1173
1174         /* Initialize formats to default values */
1175         def_bus_fmt.width = 1920;
1176         def_bus_fmt.height = 1080;
1177         def_bus_fmt.code = MEDIA_BUS_FMT_FIXED;
1178         def_bus_fmt.field = V4L2_FIELD_NONE;
1179         def_bus_fmt.colorspace = V4L2_COLORSPACE_RAW;
1180         def_bus_fmt.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1181         def_bus_fmt.quantization = V4L2_QUANTIZATION_DEFAULT;
1182         def_bus_fmt.xfer_func = V4L2_XFER_FUNC_DEFAULT;
1183
1184         def_pix_fmt.width = def_bus_fmt.width;
1185         def_pix_fmt.height = def_bus_fmt.height;
1186         def_pix_fmt.field = def_bus_fmt.field;
1187         def_pix_fmt.num_planes = 1;
1188         def_pix_fmt.plane_fmt[0].bytesperline = def_pix_fmt.width * 2;
1189         def_pix_fmt.plane_fmt[0].sizeimage =
1190                 def_pix_fmt.height * def_pix_fmt.plane_fmt[0].bytesperline;
1191         def_pix_fmt.flags = 0;
1192         def_pix_fmt.colorspace = def_bus_fmt.colorspace;
1193         def_pix_fmt.ycbcr_enc = def_bus_fmt.ycbcr_enc;
1194         def_pix_fmt.quantization = def_bus_fmt.quantization;
1195         def_pix_fmt.xfer_func = def_bus_fmt.xfer_func;
1196
1197         /* Initialize miscellaneous variables */
1198         mutex_init(&node->lock);
1199         INIT_LIST_HEAD(&node->buffers);
1200
1201         /* Initialize formats to default values */
1202         node->pad_fmt = def_bus_fmt;
1203         node->id = node_num;
1204         node->pipe = pipe;
1205         imgu_node_to_v4l2(node_num, vdev, &node->vdev_fmt);
1206         if (node->vdev_fmt.type ==
1207             V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
1208             node->vdev_fmt.type ==
1209             V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1210                 def_pix_fmt.pixelformat = node->output ?
1211                         V4L2_PIX_FMT_IPU3_SGRBG10 :
1212                         V4L2_PIX_FMT_NV12;
1213                 node->vdev_fmt.fmt.pix_mp = def_pix_fmt;
1214         }
1215
1216         /* Initialize media entities */
1217         r = media_entity_pads_init(&vdev->entity, 1, &node->vdev_pad);
1218         if (r) {
1219                 dev_err(dev, "failed initialize media entity (%d)\n", r);
1220                 mutex_destroy(&node->lock);
1221                 return r;
1222         }
1223         node->vdev_pad.flags = node->output ?
1224                 MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK;
1225         vdev->entity.ops = NULL;
1226
1227         /* Initialize vbq */
1228         vbq->type = node->vdev_fmt.type;
1229         vbq->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF;
1230         vbq->ops = &imgu_vb2_ops;
1231         vbq->mem_ops = &vb2_dma_sg_memops;
1232         if (imgu->buf_struct_size <= 0)
1233                 imgu->buf_struct_size =
1234                         sizeof(struct imgu_vb2_buffer);
1235         vbq->buf_struct_size = imgu->buf_struct_size;
1236         vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1237         /* can streamon w/o buffers */
1238         vbq->min_buffers_needed = 0;
1239         vbq->drv_priv = imgu;
1240         vbq->lock = &node->lock;
1241         r = vb2_queue_init(vbq);
1242         if (r) {
1243                 dev_err(dev, "failed to initialize video queue (%d)", r);
1244                 media_entity_cleanup(&vdev->entity);
1245                 return r;
1246         }
1247
1248         /* Initialize vdev */
1249         snprintf(vdev->name, sizeof(vdev->name), "%s %u %s",
1250                  IMGU_NAME, pipe, node->name);
1251         vdev->release = video_device_release_empty;
1252         vdev->fops = &imgu_v4l2_fops;
1253         vdev->lock = &node->lock;
1254         vdev->v4l2_dev = &imgu->v4l2_dev;
1255         vdev->queue = &node->vbq;
1256         vdev->vfl_dir = node->output ? VFL_DIR_TX : VFL_DIR_RX;
1257         video_set_drvdata(vdev, imgu);
1258         r = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1259         if (r) {
1260                 dev_err(dev, "failed to register video device (%d)", r);
1261                 media_entity_cleanup(&vdev->entity);
1262                 return r;
1263         }
1264
1265         /* Create link between video node and the subdev pad */
1266         flags = 0;
1267         if (node->enabled)
1268                 flags |= MEDIA_LNK_FL_ENABLED;
1269         if (node->output) {
1270                 r = media_create_pad_link(&vdev->entity, 0, &sd->entity,
1271                                           node_num, flags);
1272         } else {
1273                 r = media_create_pad_link(&sd->entity, node_num, &vdev->entity,
1274                                           0, flags);
1275         }
1276         if (r) {
1277                 dev_err(dev, "failed to create pad link (%d)", r);
1278                 video_unregister_device(vdev);
1279                 return r;
1280         }
1281
1282         return 0;
1283 }
1284
1285 static void imgu_v4l2_nodes_cleanup_pipe(struct imgu_device *imgu,
1286                                          unsigned int pipe, int node)
1287 {
1288         int i;
1289         struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1290
1291         for (i = 0; i < node; i++) {
1292                 video_unregister_device(&imgu_pipe->nodes[i].vdev);
1293                 media_entity_cleanup(&imgu_pipe->nodes[i].vdev.entity);
1294                 mutex_destroy(&imgu_pipe->nodes[i].lock);
1295         }
1296 }
1297
1298 static int imgu_v4l2_nodes_setup_pipe(struct imgu_device *imgu, int pipe)
1299 {
1300         int i, r;
1301
1302         for (i = 0; i < IMGU_NODE_NUM; i++) {
1303                 r = imgu_v4l2_node_setup(imgu, pipe, i);
1304                 if (r)
1305                         goto cleanup;
1306         }
1307
1308         return 0;
1309
1310 cleanup:
1311         imgu_v4l2_nodes_cleanup_pipe(imgu, pipe, i);
1312         return r;
1313 }
1314
1315 static void imgu_v4l2_subdev_cleanup(struct imgu_device *imgu, unsigned int i)
1316 {
1317         struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[i];
1318
1319         v4l2_device_unregister_subdev(&imgu_pipe->imgu_sd.subdev);
1320         v4l2_ctrl_handler_free(imgu_pipe->imgu_sd.subdev.ctrl_handler);
1321         media_entity_cleanup(&imgu_pipe->imgu_sd.subdev.entity);
1322 }
1323
1324 static void imgu_v4l2_cleanup_pipes(struct imgu_device *imgu, unsigned int pipe)
1325 {
1326         int i;
1327
1328         for (i = 0; i < pipe; i++) {
1329                 imgu_v4l2_nodes_cleanup_pipe(imgu, i, IMGU_NODE_NUM);
1330                 imgu_v4l2_subdev_cleanup(imgu, i);
1331         }
1332 }
1333
1334 static int imgu_v4l2_register_pipes(struct imgu_device *imgu)
1335 {
1336         struct imgu_media_pipe *imgu_pipe;
1337         int i, r;
1338
1339         for (i = 0; i < IMGU_MAX_PIPE_NUM; i++) {
1340                 imgu_pipe = &imgu->imgu_pipe[i];
1341                 r = imgu_v4l2_subdev_register(imgu, &imgu_pipe->imgu_sd, i);
1342                 if (r) {
1343                         dev_err(&imgu->pci_dev->dev,
1344                                 "failed to register subdev%u ret (%d)\n", i, r);
1345                         goto pipes_cleanup;
1346                 }
1347                 r = imgu_v4l2_nodes_setup_pipe(imgu, i);
1348                 if (r) {
1349                         imgu_v4l2_subdev_cleanup(imgu, i);
1350                         goto pipes_cleanup;
1351                 }
1352         }
1353
1354         return 0;
1355
1356 pipes_cleanup:
1357         imgu_v4l2_cleanup_pipes(imgu, i);
1358         return r;
1359 }
1360
1361 int imgu_v4l2_register(struct imgu_device *imgu)
1362 {
1363         int r;
1364
1365         /* Initialize miscellaneous variables */
1366         imgu->streaming = false;
1367
1368         /* Set up media device */
1369         media_device_pci_init(&imgu->media_dev, imgu->pci_dev, IMGU_NAME);
1370
1371         /* Set up v4l2 device */
1372         imgu->v4l2_dev.mdev = &imgu->media_dev;
1373         imgu->v4l2_dev.ctrl_handler = NULL;
1374         r = v4l2_device_register(&imgu->pci_dev->dev, &imgu->v4l2_dev);
1375         if (r) {
1376                 dev_err(&imgu->pci_dev->dev,
1377                         "failed to register V4L2 device (%d)\n", r);
1378                 goto fail_v4l2_dev;
1379         }
1380
1381         r = imgu_v4l2_register_pipes(imgu);
1382         if (r) {
1383                 dev_err(&imgu->pci_dev->dev,
1384                         "failed to register pipes (%d)\n", r);
1385                 goto fail_v4l2_pipes;
1386         }
1387
1388         r = v4l2_device_register_subdev_nodes(&imgu->v4l2_dev);
1389         if (r) {
1390                 dev_err(&imgu->pci_dev->dev,
1391                         "failed to register subdevs (%d)\n", r);
1392                 goto fail_subdevs;
1393         }
1394
1395         r = media_device_register(&imgu->media_dev);
1396         if (r) {
1397                 dev_err(&imgu->pci_dev->dev,
1398                         "failed to register media device (%d)\n", r);
1399                 goto fail_subdevs;
1400         }
1401
1402         return 0;
1403
1404 fail_subdevs:
1405         imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM);
1406 fail_v4l2_pipes:
1407         v4l2_device_unregister(&imgu->v4l2_dev);
1408 fail_v4l2_dev:
1409         media_device_cleanup(&imgu->media_dev);
1410
1411         return r;
1412 }
1413
1414 int imgu_v4l2_unregister(struct imgu_device *imgu)
1415 {
1416         media_device_unregister(&imgu->media_dev);
1417         imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM);
1418         v4l2_device_unregister(&imgu->v4l2_dev);
1419         media_device_cleanup(&imgu->media_dev);
1420
1421         return 0;
1422 }
1423
1424 void imgu_v4l2_buffer_done(struct vb2_buffer *vb,
1425                            enum vb2_buffer_state state)
1426 {
1427         struct imgu_vb2_buffer *b =
1428                 container_of(vb, struct imgu_vb2_buffer, vbb.vb2_buf);
1429
1430         list_del(&b->list);
1431         vb2_buffer_done(&b->vbb.vb2_buf, state);
1432 }