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