1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Intel Corporation
4 #include <linux/module.h>
5 #include <linux/pm_runtime.h>
7 #include <media/v4l2-event.h>
8 #include <media/v4l2-ioctl.h>
11 #include "ipu3-dmamap.h"
13 /******************** v4l2_subdev_ops ********************/
15 #define IPU3_RUNNING_MODE_VIDEO 0
16 #define IPU3_RUNNING_MODE_STILL 1
18 static int imgu_subdev_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
20 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
21 struct imgu_v4l2_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 = {
32 imgu_pipe->nodes[IMGU_NODE_IN].vdev_fmt.fmt.pix_mp.width;
34 imgu_pipe->nodes[IMGU_NODE_IN].vdev_fmt.fmt.pix_mp.height;
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);
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;
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;
53 static int imgu_subdev_s_stream(struct v4l2_subdev *sd, int enable)
58 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
59 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
60 struct imgu_v4l2_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];
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);
74 imgu_sd->active = false;
78 for (i = 0; i < IMGU_NODE_NUM; i++)
79 imgu_pipe->queue_enabled[i] = imgu_pipe->nodes[i].enabled;
81 /* This is handled specially */
82 imgu_pipe->queue_enabled[IPU3_CSS_QUEUE_PARAMS] = false;
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)
90 fmts[i] = imgu_pipe->queue_enabled[node] ?
91 &imgu_pipe->nodes[node].vdev_fmt.fmt.pix_mp : NULL;
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;
99 if (atomic_read(&imgu_sd->running_mode) == IPU3_RUNNING_MODE_VIDEO)
100 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_VIDEO;
102 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_CAPTURE;
104 dev_dbg(dev, "IPU3 pipe %u pipe_id %u", pipe, css_pipe->pipe_id);
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;
110 r = imgu_css_fmt_set(&imgu->css, fmts, rects, pipe);
112 dev_err(dev, "failed to set initial formats pipe %u with (%d)",
117 imgu_sd->active = true;
122 static int imgu_subdev_get_fmt(struct v4l2_subdev *sd,
123 struct v4l2_subdev_pad_config *cfg,
124 struct v4l2_subdev_format *fmt)
126 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
127 struct v4l2_mbus_framefmt *mf;
128 struct imgu_media_pipe *imgu_pipe;
130 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
131 struct imgu_v4l2_subdev,
133 unsigned int pipe = imgu_sd->pipe;
135 imgu_pipe = &imgu->imgu_pipe[pipe];
136 if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
137 fmt->format = imgu_pipe->nodes[pad].pad_fmt;
139 mf = v4l2_subdev_get_try_format(sd, cfg, pad);
146 static int imgu_subdev_set_fmt(struct v4l2_subdev *sd,
147 struct v4l2_subdev_pad_config *cfg,
148 struct v4l2_subdev_format *fmt)
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,
155 struct v4l2_mbus_framefmt *mf;
157 unsigned int pipe = imgu_sd->pipe;
159 dev_dbg(&imgu->pci_dev->dev, "set subdev %u pad %u fmt to [%ux%u]",
160 pipe, pad, fmt->format.width, fmt->format.height);
162 imgu_pipe = &imgu->imgu_pipe[pipe];
163 if (fmt->which == V4L2_SUBDEV_FORMAT_TRY)
164 mf = v4l2_subdev_get_try_format(sd, cfg, pad);
166 mf = &imgu_pipe->nodes[pad].pad_fmt;
168 fmt->format.code = mf->code;
169 /* Clamp the w and h based on the hardware capabilities */
170 if (imgu_sd->subdev_pads[pad].flags & MEDIA_PAD_FL_SOURCE) {
171 fmt->format.width = clamp(fmt->format.width,
172 IPU3_OUTPUT_MIN_WIDTH,
173 IPU3_OUTPUT_MAX_WIDTH);
174 fmt->format.height = clamp(fmt->format.height,
175 IPU3_OUTPUT_MIN_HEIGHT,
176 IPU3_OUTPUT_MAX_HEIGHT);
178 fmt->format.width = clamp(fmt->format.width,
179 IPU3_INPUT_MIN_WIDTH,
180 IPU3_INPUT_MAX_WIDTH);
181 fmt->format.height = clamp(fmt->format.height,
182 IPU3_INPUT_MIN_HEIGHT,
183 IPU3_INPUT_MAX_HEIGHT);
191 static int imgu_subdev_get_selection(struct v4l2_subdev *sd,
192 struct v4l2_subdev_pad_config *cfg,
193 struct v4l2_subdev_selection *sel)
195 struct v4l2_rect *try_sel, *r;
196 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
197 struct imgu_v4l2_subdev,
200 if (sel->pad != IMGU_NODE_IN)
203 switch (sel->target) {
204 case V4L2_SEL_TGT_CROP:
205 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
206 r = &imgu_sd->rect.eff;
208 case V4L2_SEL_TGT_COMPOSE:
209 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
210 r = &imgu_sd->rect.bds;
216 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
224 static int imgu_subdev_set_selection(struct v4l2_subdev *sd,
225 struct v4l2_subdev_pad_config *cfg,
226 struct v4l2_subdev_selection *sel)
228 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
229 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
230 struct imgu_v4l2_subdev,
232 struct v4l2_rect *rect, *try_sel;
234 dev_dbg(&imgu->pci_dev->dev,
235 "set subdev %u sel which %u target 0x%4x rect [%ux%u]",
236 imgu_sd->pipe, sel->which, sel->target,
237 sel->r.width, sel->r.height);
239 if (sel->pad != IMGU_NODE_IN)
242 switch (sel->target) {
243 case V4L2_SEL_TGT_CROP:
244 try_sel = v4l2_subdev_get_try_crop(sd, cfg, sel->pad);
245 rect = &imgu_sd->rect.eff;
247 case V4L2_SEL_TGT_COMPOSE:
248 try_sel = v4l2_subdev_get_try_compose(sd, cfg, sel->pad);
249 rect = &imgu_sd->rect.bds;
255 if (sel->which == V4L2_SUBDEV_FORMAT_TRY)
263 /******************** media_entity_operations ********************/
265 static int imgu_link_setup(struct media_entity *entity,
266 const struct media_pad *local,
267 const struct media_pad *remote, u32 flags)
269 struct imgu_media_pipe *imgu_pipe;
270 struct v4l2_subdev *sd = container_of(entity, struct v4l2_subdev,
272 struct imgu_device *imgu = v4l2_get_subdevdata(sd);
273 struct imgu_v4l2_subdev *imgu_sd = container_of(sd,
274 struct imgu_v4l2_subdev,
276 unsigned int pipe = imgu_sd->pipe;
277 u32 pad = local->index;
279 WARN_ON(pad >= IMGU_NODE_NUM);
281 dev_dbg(&imgu->pci_dev->dev, "pipe %u pad %u is %s", pipe, pad,
282 flags & MEDIA_LNK_FL_ENABLED ? "enabled" : "disabled");
284 imgu_pipe = &imgu->imgu_pipe[pipe];
285 imgu_pipe->nodes[pad].enabled = flags & MEDIA_LNK_FL_ENABLED;
287 /* enable input node to enable the pipe */
288 if (pad != IMGU_NODE_IN)
291 if (flags & MEDIA_LNK_FL_ENABLED)
292 __set_bit(pipe, imgu->css.enabled_pipes);
294 __clear_bit(pipe, imgu->css.enabled_pipes);
296 dev_dbg(&imgu->pci_dev->dev, "pipe %u is %s", pipe,
297 flags & MEDIA_LNK_FL_ENABLED ? "enabled" : "disabled");
302 /******************** vb2_ops ********************/
304 static int imgu_vb2_buf_init(struct vb2_buffer *vb)
306 struct sg_table *sg = vb2_dma_sg_plane_desc(vb, 0);
307 struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
308 struct imgu_buffer *buf = container_of(vb,
309 struct imgu_buffer, vid_buf.vbb.vb2_buf);
310 struct imgu_video_device *node =
311 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
312 unsigned int queue = imgu_node_to_queue(node->id);
314 if (queue == IPU3_CSS_QUEUE_PARAMS)
317 return imgu_dmamap_map_sg(imgu, sg->sgl, sg->nents, &buf->map);
320 /* Called when each buffer is freed */
321 static void imgu_vb2_buf_cleanup(struct vb2_buffer *vb)
323 struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
324 struct imgu_buffer *buf = container_of(vb,
325 struct imgu_buffer, vid_buf.vbb.vb2_buf);
326 struct imgu_video_device *node =
327 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
328 unsigned int queue = imgu_node_to_queue(node->id);
330 if (queue == IPU3_CSS_QUEUE_PARAMS)
333 imgu_dmamap_unmap(imgu, &buf->map);
336 /* Transfer buffer ownership to me */
337 static void imgu_vb2_buf_queue(struct vb2_buffer *vb)
339 struct imgu_device *imgu = vb2_get_drv_priv(vb->vb2_queue);
340 struct imgu_video_device *node =
341 container_of(vb->vb2_queue, struct imgu_video_device, vbq);
342 unsigned int queue = imgu_node_to_queue(node->id);
343 struct imgu_buffer *buf = container_of(vb, struct imgu_buffer,
344 vid_buf.vbb.vb2_buf);
345 unsigned long need_bytes;
346 unsigned long payload = vb2_get_plane_payload(vb, 0);
348 if (vb->vb2_queue->type == V4L2_BUF_TYPE_META_CAPTURE ||
349 vb->vb2_queue->type == V4L2_BUF_TYPE_META_OUTPUT)
350 need_bytes = node->vdev_fmt.fmt.meta.buffersize;
352 need_bytes = node->vdev_fmt.fmt.pix_mp.plane_fmt[0].sizeimage;
354 if (queue == IPU3_CSS_QUEUE_PARAMS && payload && payload < need_bytes) {
355 dev_err(&imgu->pci_dev->dev, "invalid data size for params.");
356 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
360 mutex_lock(&imgu->lock);
361 if (queue != IPU3_CSS_QUEUE_PARAMS)
362 imgu_css_buf_init(&buf->css_buf, queue, buf->map.daddr);
364 list_add_tail(&buf->vid_buf.list, &node->buffers);
365 mutex_unlock(&imgu->lock);
367 vb2_set_plane_payload(vb, 0, need_bytes);
369 mutex_lock(&imgu->streaming_lock);
371 imgu_queue_buffers(imgu, false, node->pipe);
372 mutex_unlock(&imgu->streaming_lock);
374 dev_dbg(&imgu->pci_dev->dev, "%s for pipe %u node %u", __func__,
375 node->pipe, node->id);
378 static int imgu_vb2_queue_setup(struct vb2_queue *vq,
379 unsigned int *num_buffers,
380 unsigned int *num_planes,
381 unsigned int sizes[],
382 struct device *alloc_devs[])
384 struct imgu_device *imgu = vb2_get_drv_priv(vq);
385 struct imgu_video_device *node =
386 container_of(vq, struct imgu_video_device, vbq);
387 const struct v4l2_format *fmt = &node->vdev_fmt;
390 *num_buffers = clamp_val(*num_buffers, 1, VB2_MAX_FRAME);
391 alloc_devs[0] = &imgu->pci_dev->dev;
393 if (vq->type == V4L2_BUF_TYPE_META_CAPTURE ||
394 vq->type == V4L2_BUF_TYPE_META_OUTPUT)
395 size = fmt->fmt.meta.buffersize;
397 size = fmt->fmt.pix_mp.plane_fmt[0].sizeimage;
408 /* Initialize buffer queue */
409 INIT_LIST_HEAD(&node->buffers);
414 /* Check if all enabled video nodes are streaming, exception ignored */
415 static bool imgu_all_nodes_streaming(struct imgu_device *imgu,
416 struct imgu_video_device *except)
418 unsigned int i, pipe, p;
419 struct imgu_video_device *node;
420 struct device *dev = &imgu->pci_dev->dev;
423 if (!test_bit(pipe, imgu->css.enabled_pipes)) {
424 dev_warn(&imgu->pci_dev->dev,
425 "pipe %u link is not ready yet", pipe);
429 for_each_set_bit(p, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) {
430 for (i = 0; i < IMGU_NODE_NUM; i++) {
431 node = &imgu->imgu_pipe[p].nodes[i];
432 dev_dbg(dev, "%s pipe %u queue %u name %s enabled = %u",
433 __func__, p, i, node->name, node->enabled);
436 if (node->enabled && !vb2_start_streaming_called(&node->vbq))
444 static void imgu_return_all_buffers(struct imgu_device *imgu,
445 struct imgu_video_device *node,
446 enum vb2_buffer_state state)
448 struct imgu_vb2_buffer *b, *b0;
450 /* Return all buffers */
451 mutex_lock(&imgu->lock);
452 list_for_each_entry_safe(b, b0, &node->buffers, list) {
454 vb2_buffer_done(&b->vbb.vb2_buf, state);
456 mutex_unlock(&imgu->lock);
459 static int imgu_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
461 struct imgu_media_pipe *imgu_pipe;
462 struct imgu_device *imgu = vb2_get_drv_priv(vq);
463 struct device *dev = &imgu->pci_dev->dev;
464 struct imgu_video_device *node =
465 container_of(vq, struct imgu_video_device, vbq);
469 dev_dbg(dev, "%s node name %s pipe %u id %u", __func__,
470 node->name, node->pipe, node->id);
472 mutex_lock(&imgu->streaming_lock);
473 if (imgu->streaming) {
475 mutex_unlock(&imgu->streaming_lock);
476 goto fail_return_bufs;
478 mutex_unlock(&imgu->streaming_lock);
480 if (!node->enabled) {
481 dev_err(dev, "IMGU node is not enabled");
483 goto fail_return_bufs;
487 imgu_pipe = &imgu->imgu_pipe[pipe];
488 r = media_pipeline_start(&node->vdev.entity, &imgu_pipe->pipeline);
490 goto fail_return_bufs;
492 if (!imgu_all_nodes_streaming(imgu, node))
495 for_each_set_bit(pipe, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) {
496 r = v4l2_subdev_call(&imgu->imgu_pipe[pipe].imgu_sd.subdev,
499 goto fail_stop_pipeline;
502 /* Start streaming of the whole pipeline now */
503 dev_dbg(dev, "IMGU streaming is ready to start");
504 mutex_lock(&imgu->streaming_lock);
505 r = imgu_s_stream(imgu, true);
507 imgu->streaming = true;
508 mutex_unlock(&imgu->streaming_lock);
513 media_pipeline_stop(&node->vdev.entity);
515 imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_QUEUED);
520 static void imgu_vb2_stop_streaming(struct vb2_queue *vq)
522 struct imgu_media_pipe *imgu_pipe;
523 struct imgu_device *imgu = vb2_get_drv_priv(vq);
524 struct device *dev = &imgu->pci_dev->dev;
525 struct imgu_video_device *node =
526 container_of(vq, struct imgu_video_device, vbq);
530 WARN_ON(!node->enabled);
533 dev_dbg(dev, "Try to stream off node [%u][%u]", pipe, node->id);
534 imgu_pipe = &imgu->imgu_pipe[pipe];
535 r = v4l2_subdev_call(&imgu_pipe->imgu_sd.subdev, video, s_stream, 0);
537 dev_err(&imgu->pci_dev->dev,
538 "failed to stop subdev streaming\n");
540 mutex_lock(&imgu->streaming_lock);
541 /* Was this the first node with streaming disabled? */
542 if (imgu->streaming && imgu_all_nodes_streaming(imgu, node)) {
543 /* Yes, really stop streaming now */
544 dev_dbg(dev, "IMGU streaming is ready to stop");
545 r = imgu_s_stream(imgu, false);
547 imgu->streaming = false;
550 imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_ERROR);
551 mutex_unlock(&imgu->streaming_lock);
553 media_pipeline_stop(&node->vdev.entity);
556 /******************** v4l2_ioctl_ops ********************/
558 #define VID_CAPTURE 0
560 #define DEF_VID_CAPTURE 0
561 #define DEF_VID_OUTPUT 1
565 u16 type; /* VID_CAPTURE or VID_OUTPUT not both */
568 /* format descriptions for capture and preview */
569 static const struct imgu_fmt formats[] = {
570 { V4L2_PIX_FMT_NV12, VID_CAPTURE },
571 { V4L2_PIX_FMT_IPU3_SGRBG10, VID_OUTPUT },
572 { V4L2_PIX_FMT_IPU3_SBGGR10, VID_OUTPUT },
573 { V4L2_PIX_FMT_IPU3_SGBRG10, VID_OUTPUT },
574 { V4L2_PIX_FMT_IPU3_SRGGB10, VID_OUTPUT },
577 /* Find the first matched format, return default if not found */
578 static const struct imgu_fmt *find_format(struct v4l2_format *f, u32 type)
582 for (i = 0; i < ARRAY_SIZE(formats); i++) {
583 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
584 formats[i].type == type)
588 return type == VID_CAPTURE ? &formats[DEF_VID_CAPTURE] :
589 &formats[DEF_VID_OUTPUT];
592 static int imgu_vidioc_querycap(struct file *file, void *fh,
593 struct v4l2_capability *cap)
595 struct imgu_video_device *node = file_to_intel_imgu_node(file);
597 strscpy(cap->driver, IMGU_NAME, sizeof(cap->driver));
598 strscpy(cap->card, IMGU_NAME, sizeof(cap->card));
599 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", node->name);
604 static int enum_fmts(struct v4l2_fmtdesc *f, u32 type)
608 for (i = j = 0; i < ARRAY_SIZE(formats); ++i) {
609 if (formats[i].type == type) {
616 if (i < ARRAY_SIZE(formats)) {
617 f->pixelformat = formats[i].fourcc;
624 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
625 struct v4l2_fmtdesc *f)
627 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
630 return enum_fmts(f, VID_CAPTURE);
633 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
634 struct v4l2_fmtdesc *f)
636 if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
639 return enum_fmts(f, VID_OUTPUT);
642 /* Propagate forward always the format from the CIO2 subdev */
643 static int imgu_vidioc_g_fmt(struct file *file, void *fh,
644 struct v4l2_format *f)
646 struct imgu_video_device *node = file_to_intel_imgu_node(file);
648 f->fmt = node->vdev_fmt.fmt;
654 * Set input/output format. Unless it is just a try, this also resets
655 * selections (ie. effective and BDS resolutions) to defaults.
657 static int imgu_fmt(struct imgu_device *imgu, unsigned int pipe, int node,
658 struct v4l2_format *f, bool try)
660 struct device *dev = &imgu->pci_dev->dev;
661 struct v4l2_pix_format_mplane *fmts[IPU3_CSS_QUEUES] = { NULL };
662 struct v4l2_rect *rects[IPU3_CSS_RECTS] = { NULL };
663 struct v4l2_mbus_framefmt pad_fmt;
664 unsigned int i, css_q;
666 struct imgu_css_pipe *css_pipe = &imgu->css.pipes[pipe];
667 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
668 struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd;
670 dev_dbg(dev, "set fmt node [%u][%u](try = %u)", pipe, node, try);
672 for (i = 0; i < IMGU_NODE_NUM; i++)
673 dev_dbg(dev, "IMGU pipe %u node %u enabled = %u",
674 pipe, i, imgu_pipe->nodes[i].enabled);
676 if (imgu_pipe->nodes[IMGU_NODE_VF].enabled)
677 css_pipe->vf_output_en = true;
679 if (atomic_read(&imgu_sd->running_mode) == IPU3_RUNNING_MODE_VIDEO)
680 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_VIDEO;
682 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_CAPTURE;
684 dev_dbg(dev, "IPU3 pipe %u pipe_id = %u", pipe, css_pipe->pipe_id);
686 for (i = 0; i < IPU3_CSS_QUEUES; i++) {
687 unsigned int inode = imgu_map_node(imgu, i);
689 /* Skip the meta node */
690 if (inode == IMGU_NODE_STAT_3A || inode == IMGU_NODE_PARAMS)
694 fmts[i] = kmemdup(&imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp,
695 sizeof(struct v4l2_pix_format_mplane),
702 fmts[i] = &imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp;
705 /* CSS expects some format on OUT queue */
706 if (i != IPU3_CSS_QUEUE_OUT &&
707 !imgu_pipe->nodes[inode].enabled)
712 /* eff and bds res got by imgu_s_sel */
713 struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd;
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;
719 /* suppose that pad fmt was set by subdev s_fmt before */
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;
726 * imgu doesn't set the node to the value given by user
727 * before we return success from this function, so set it here.
729 css_q = imgu_node_to_queue(node);
734 *fmts[css_q] = f->fmt.pix_mp;
737 ret = imgu_css_fmt_try(&imgu->css, fmts, rects, pipe);
739 ret = imgu_css_fmt_set(&imgu->css, fmts, rects, pipe);
741 /* ret is the binary number in the firmware blob */
746 f->fmt.pix_mp = *fmts[css_q];
748 f->fmt = imgu_pipe->nodes[node].vdev_fmt.fmt;
752 for (i = 0; i < IPU3_CSS_QUEUES; i++)
759 static int imgu_try_fmt(struct file *file, void *fh, struct v4l2_format *f)
761 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp;
762 const struct imgu_fmt *fmt;
764 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
765 fmt = find_format(f, VID_CAPTURE);
766 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
767 fmt = find_format(f, VID_OUTPUT);
771 pixm->pixelformat = fmt->fourcc;
773 memset(pixm->plane_fmt[0].reserved, 0,
774 sizeof(pixm->plane_fmt[0].reserved));
779 static int imgu_vidioc_try_fmt(struct file *file, void *fh,
780 struct v4l2_format *f)
782 struct imgu_device *imgu = video_drvdata(file);
783 struct device *dev = &imgu->pci_dev->dev;
784 struct imgu_video_device *node = file_to_intel_imgu_node(file);
785 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
788 dev_dbg(dev, "%s [%ux%u] for node %u\n", __func__,
789 pix_mp->width, pix_mp->height, node->id);
791 r = imgu_try_fmt(file, fh, f);
795 return imgu_fmt(imgu, node->pipe, node->id, f, true);
798 static int imgu_vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
800 struct imgu_device *imgu = video_drvdata(file);
801 struct device *dev = &imgu->pci_dev->dev;
802 struct imgu_video_device *node = file_to_intel_imgu_node(file);
803 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
806 dev_dbg(dev, "%s [%ux%u] for node %u\n", __func__,
807 pix_mp->width, pix_mp->height, node->id);
809 r = imgu_try_fmt(file, fh, f);
813 return imgu_fmt(imgu, node->pipe, node->id, f, false);
816 struct imgu_meta_fmt {
821 /* From drivers/media/v4l2-core/v4l2-ioctl.c */
822 static const struct imgu_meta_fmt meta_fmts[] = {
823 { V4L2_META_FMT_IPU3_PARAMS, "IPU3 processing parameters" },
824 { V4L2_META_FMT_IPU3_STAT_3A, "IPU3 3A statistics" },
827 static int imgu_meta_enum_format(struct file *file, void *fh,
828 struct v4l2_fmtdesc *fmt)
830 struct imgu_video_device *node = file_to_intel_imgu_node(file);
831 unsigned int i = fmt->type == V4L2_BUF_TYPE_META_OUTPUT ? 0 : 1;
833 /* Each node is dedicated to only one meta format */
834 if (fmt->index > 0 || fmt->type != node->vbq.type)
837 strscpy(fmt->description, meta_fmts[i].name, sizeof(fmt->description));
838 fmt->pixelformat = meta_fmts[i].fourcc;
843 static int imgu_vidioc_g_meta_fmt(struct file *file, void *fh,
844 struct v4l2_format *f)
846 struct imgu_video_device *node = file_to_intel_imgu_node(file);
848 if (f->type != node->vbq.type)
851 f->fmt = node->vdev_fmt.fmt;
856 static int imgu_vidioc_enum_input(struct file *file, void *fh,
857 struct v4l2_input *input)
859 if (input->index > 0)
861 strscpy(input->name, "camera", sizeof(input->name));
862 input->type = V4L2_INPUT_TYPE_CAMERA;
867 static int imgu_vidioc_g_input(struct file *file, void *fh, unsigned int *input)
874 static int imgu_vidioc_s_input(struct file *file, void *fh, unsigned int input)
876 return input == 0 ? 0 : -EINVAL;
879 static int imgu_vidioc_enum_output(struct file *file, void *fh,
880 struct v4l2_output *output)
882 if (output->index > 0)
884 strscpy(output->name, "camera", sizeof(output->name));
885 output->type = V4L2_INPUT_TYPE_CAMERA;
890 static int imgu_vidioc_g_output(struct file *file, void *fh,
891 unsigned int *output)
898 static int imgu_vidioc_s_output(struct file *file, void *fh,
901 return output == 0 ? 0 : -EINVAL;
904 /******************** function pointers ********************/
906 static struct v4l2_subdev_internal_ops imgu_subdev_internal_ops = {
907 .open = imgu_subdev_open,
910 static const struct v4l2_subdev_core_ops imgu_subdev_core_ops = {
911 .subscribe_event = v4l2_ctrl_subdev_subscribe_event,
912 .unsubscribe_event = v4l2_event_subdev_unsubscribe,
915 static const struct v4l2_subdev_video_ops imgu_subdev_video_ops = {
916 .s_stream = imgu_subdev_s_stream,
919 static const struct v4l2_subdev_pad_ops imgu_subdev_pad_ops = {
920 .link_validate = v4l2_subdev_link_validate_default,
921 .get_fmt = imgu_subdev_get_fmt,
922 .set_fmt = imgu_subdev_set_fmt,
923 .get_selection = imgu_subdev_get_selection,
924 .set_selection = imgu_subdev_set_selection,
927 static const struct v4l2_subdev_ops imgu_subdev_ops = {
928 .core = &imgu_subdev_core_ops,
929 .video = &imgu_subdev_video_ops,
930 .pad = &imgu_subdev_pad_ops,
933 static const struct media_entity_operations imgu_media_ops = {
934 .link_setup = imgu_link_setup,
935 .link_validate = v4l2_subdev_link_validate,
938 /****************** vb2_ops of the Q ********************/
940 static const struct vb2_ops imgu_vb2_ops = {
941 .buf_init = imgu_vb2_buf_init,
942 .buf_cleanup = imgu_vb2_buf_cleanup,
943 .buf_queue = imgu_vb2_buf_queue,
944 .queue_setup = imgu_vb2_queue_setup,
945 .start_streaming = imgu_vb2_start_streaming,
946 .stop_streaming = imgu_vb2_stop_streaming,
947 .wait_prepare = vb2_ops_wait_prepare,
948 .wait_finish = vb2_ops_wait_finish,
951 /****************** v4l2_file_operations *****************/
953 static const struct v4l2_file_operations imgu_v4l2_fops = {
954 .unlocked_ioctl = video_ioctl2,
955 .open = v4l2_fh_open,
956 .release = vb2_fop_release,
957 .poll = vb2_fop_poll,
958 .mmap = vb2_fop_mmap,
961 /******************** v4l2_ioctl_ops ********************/
963 static const struct v4l2_ioctl_ops imgu_v4l2_ioctl_ops = {
964 .vidioc_querycap = imgu_vidioc_querycap,
966 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
967 .vidioc_g_fmt_vid_cap_mplane = imgu_vidioc_g_fmt,
968 .vidioc_s_fmt_vid_cap_mplane = imgu_vidioc_s_fmt,
969 .vidioc_try_fmt_vid_cap_mplane = imgu_vidioc_try_fmt,
971 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
972 .vidioc_g_fmt_vid_out_mplane = imgu_vidioc_g_fmt,
973 .vidioc_s_fmt_vid_out_mplane = imgu_vidioc_s_fmt,
974 .vidioc_try_fmt_vid_out_mplane = imgu_vidioc_try_fmt,
976 .vidioc_enum_output = imgu_vidioc_enum_output,
977 .vidioc_g_output = imgu_vidioc_g_output,
978 .vidioc_s_output = imgu_vidioc_s_output,
980 .vidioc_enum_input = imgu_vidioc_enum_input,
981 .vidioc_g_input = imgu_vidioc_g_input,
982 .vidioc_s_input = imgu_vidioc_s_input,
984 /* buffer queue management */
985 .vidioc_reqbufs = vb2_ioctl_reqbufs,
986 .vidioc_create_bufs = vb2_ioctl_create_bufs,
987 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
988 .vidioc_querybuf = vb2_ioctl_querybuf,
989 .vidioc_qbuf = vb2_ioctl_qbuf,
990 .vidioc_dqbuf = vb2_ioctl_dqbuf,
991 .vidioc_streamon = vb2_ioctl_streamon,
992 .vidioc_streamoff = vb2_ioctl_streamoff,
993 .vidioc_expbuf = vb2_ioctl_expbuf,
996 static const struct v4l2_ioctl_ops imgu_v4l2_meta_ioctl_ops = {
997 .vidioc_querycap = imgu_vidioc_querycap,
1000 .vidioc_enum_fmt_meta_cap = imgu_meta_enum_format,
1001 .vidioc_g_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
1002 .vidioc_s_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
1003 .vidioc_try_fmt_meta_cap = imgu_vidioc_g_meta_fmt,
1006 .vidioc_enum_fmt_meta_out = imgu_meta_enum_format,
1007 .vidioc_g_fmt_meta_out = imgu_vidioc_g_meta_fmt,
1008 .vidioc_s_fmt_meta_out = imgu_vidioc_g_meta_fmt,
1009 .vidioc_try_fmt_meta_out = imgu_vidioc_g_meta_fmt,
1011 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1012 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1013 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
1014 .vidioc_querybuf = vb2_ioctl_querybuf,
1015 .vidioc_qbuf = vb2_ioctl_qbuf,
1016 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1017 .vidioc_streamon = vb2_ioctl_streamon,
1018 .vidioc_streamoff = vb2_ioctl_streamoff,
1019 .vidioc_expbuf = vb2_ioctl_expbuf,
1022 static int imgu_sd_s_ctrl(struct v4l2_ctrl *ctrl)
1024 struct imgu_v4l2_subdev *imgu_sd =
1025 container_of(ctrl->handler, struct imgu_v4l2_subdev, ctrl_handler);
1026 struct imgu_device *imgu = v4l2_get_subdevdata(&imgu_sd->subdev);
1027 struct device *dev = &imgu->pci_dev->dev;
1029 dev_dbg(dev, "set val %d to ctrl 0x%8x for subdev %u",
1030 ctrl->val, ctrl->id, imgu_sd->pipe);
1033 case V4L2_CID_INTEL_IPU3_MODE:
1034 atomic_set(&imgu_sd->running_mode, ctrl->val);
1041 static const struct v4l2_ctrl_ops imgu_subdev_ctrl_ops = {
1042 .s_ctrl = imgu_sd_s_ctrl,
1045 static const char * const imgu_ctrl_mode_strings[] = {
1050 static const struct v4l2_ctrl_config imgu_subdev_ctrl_mode = {
1051 .ops = &imgu_subdev_ctrl_ops,
1052 .id = V4L2_CID_INTEL_IPU3_MODE,
1053 .name = "IPU3 Pipe Mode",
1054 .type = V4L2_CTRL_TYPE_MENU,
1055 .max = ARRAY_SIZE(imgu_ctrl_mode_strings) - 1,
1056 .def = IPU3_RUNNING_MODE_VIDEO,
1057 .qmenu = imgu_ctrl_mode_strings,
1060 /******************** Framework registration ********************/
1062 /* helper function to config node's video properties */
1063 static void imgu_node_to_v4l2(u32 node, struct video_device *vdev,
1064 struct v4l2_format *f)
1068 /* Should not happen */
1069 WARN_ON(node >= IMGU_NODE_NUM);
1073 cap = V4L2_CAP_VIDEO_OUTPUT_MPLANE;
1074 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
1075 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops;
1077 case IMGU_NODE_PARAMS:
1078 cap = V4L2_CAP_META_OUTPUT;
1079 f->type = V4L2_BUF_TYPE_META_OUTPUT;
1080 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_PARAMS;
1081 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops;
1082 imgu_css_meta_fmt_set(&f->fmt.meta);
1084 case IMGU_NODE_STAT_3A:
1085 cap = V4L2_CAP_META_CAPTURE;
1086 f->type = V4L2_BUF_TYPE_META_CAPTURE;
1087 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_STAT_3A;
1088 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops;
1089 imgu_css_meta_fmt_set(&f->fmt.meta);
1092 cap = V4L2_CAP_VIDEO_CAPTURE_MPLANE;
1093 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
1094 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops;
1097 vdev->device_caps = V4L2_CAP_STREAMING | cap;
1100 static int imgu_v4l2_subdev_register(struct imgu_device *imgu,
1101 struct imgu_v4l2_subdev *imgu_sd,
1105 struct v4l2_ctrl_handler *hdl = &imgu_sd->ctrl_handler;
1106 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1108 /* Initialize subdev media entity */
1109 r = media_entity_pads_init(&imgu_sd->subdev.entity, IMGU_NODE_NUM,
1110 imgu_sd->subdev_pads);
1112 dev_err(&imgu->pci_dev->dev,
1113 "failed initialize subdev media entity (%d)\n", r);
1116 imgu_sd->subdev.entity.ops = &imgu_media_ops;
1117 for (i = 0; i < IMGU_NODE_NUM; i++) {
1118 imgu_sd->subdev_pads[i].flags = imgu_pipe->nodes[i].output ?
1119 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE;
1122 /* Initialize subdev */
1123 v4l2_subdev_init(&imgu_sd->subdev, &imgu_subdev_ops);
1124 imgu_sd->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_STATISTICS;
1125 imgu_sd->subdev.internal_ops = &imgu_subdev_internal_ops;
1126 imgu_sd->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE |
1127 V4L2_SUBDEV_FL_HAS_EVENTS;
1128 snprintf(imgu_sd->subdev.name, sizeof(imgu_sd->subdev.name),
1129 "%s %u", IMGU_NAME, pipe);
1130 v4l2_set_subdevdata(&imgu_sd->subdev, imgu);
1131 atomic_set(&imgu_sd->running_mode, IPU3_RUNNING_MODE_VIDEO);
1132 v4l2_ctrl_handler_init(hdl, 1);
1133 imgu_sd->subdev.ctrl_handler = hdl;
1134 imgu_sd->ctrl = v4l2_ctrl_new_custom(hdl, &imgu_subdev_ctrl_mode, NULL);
1137 dev_err(&imgu->pci_dev->dev,
1138 "failed to create subdev v4l2 ctrl with err %d", r);
1141 r = v4l2_device_register_subdev(&imgu->v4l2_dev, &imgu_sd->subdev);
1143 dev_err(&imgu->pci_dev->dev,
1144 "failed initialize subdev (%d)\n", r);
1148 imgu_sd->pipe = pipe;
1152 v4l2_ctrl_handler_free(imgu_sd->subdev.ctrl_handler);
1153 media_entity_cleanup(&imgu_sd->subdev.entity);
1158 static int imgu_v4l2_node_setup(struct imgu_device *imgu, unsigned int pipe,
1163 struct v4l2_mbus_framefmt def_bus_fmt = { 0 };
1164 struct v4l2_pix_format_mplane def_pix_fmt = { 0 };
1165 struct device *dev = &imgu->pci_dev->dev;
1166 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1167 struct v4l2_subdev *sd = &imgu_pipe->imgu_sd.subdev;
1168 struct imgu_video_device *node = &imgu_pipe->nodes[node_num];
1169 struct video_device *vdev = &node->vdev;
1170 struct vb2_queue *vbq = &node->vbq;
1172 /* Initialize formats to default values */
1173 def_bus_fmt.width = 1920;
1174 def_bus_fmt.height = 1080;
1175 def_bus_fmt.code = MEDIA_BUS_FMT_FIXED;
1176 def_bus_fmt.field = V4L2_FIELD_NONE;
1177 def_bus_fmt.colorspace = V4L2_COLORSPACE_RAW;
1178 def_bus_fmt.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
1179 def_bus_fmt.quantization = V4L2_QUANTIZATION_DEFAULT;
1180 def_bus_fmt.xfer_func = V4L2_XFER_FUNC_DEFAULT;
1182 def_pix_fmt.width = def_bus_fmt.width;
1183 def_pix_fmt.height = def_bus_fmt.height;
1184 def_pix_fmt.field = def_bus_fmt.field;
1185 def_pix_fmt.num_planes = 1;
1186 def_pix_fmt.plane_fmt[0].bytesperline = def_pix_fmt.width * 2;
1187 def_pix_fmt.plane_fmt[0].sizeimage =
1188 def_pix_fmt.height * def_pix_fmt.plane_fmt[0].bytesperline;
1189 def_pix_fmt.flags = 0;
1190 def_pix_fmt.colorspace = def_bus_fmt.colorspace;
1191 def_pix_fmt.ycbcr_enc = def_bus_fmt.ycbcr_enc;
1192 def_pix_fmt.quantization = def_bus_fmt.quantization;
1193 def_pix_fmt.xfer_func = def_bus_fmt.xfer_func;
1195 /* Initialize miscellaneous variables */
1196 mutex_init(&node->lock);
1197 INIT_LIST_HEAD(&node->buffers);
1199 /* Initialize formats to default values */
1200 node->pad_fmt = def_bus_fmt;
1201 node->id = node_num;
1203 imgu_node_to_v4l2(node_num, vdev, &node->vdev_fmt);
1204 if (node->vdev_fmt.type ==
1205 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ||
1206 node->vdev_fmt.type ==
1207 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1208 def_pix_fmt.pixelformat = node->output ?
1209 V4L2_PIX_FMT_IPU3_SGRBG10 :
1211 node->vdev_fmt.fmt.pix_mp = def_pix_fmt;
1214 /* Initialize media entities */
1215 r = media_entity_pads_init(&vdev->entity, 1, &node->vdev_pad);
1217 dev_err(dev, "failed initialize media entity (%d)\n", r);
1218 mutex_destroy(&node->lock);
1221 node->vdev_pad.flags = node->output ?
1222 MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK;
1223 vdev->entity.ops = NULL;
1225 /* Initialize vbq */
1226 vbq->type = node->vdev_fmt.type;
1227 vbq->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF;
1228 vbq->ops = &imgu_vb2_ops;
1229 vbq->mem_ops = &vb2_dma_sg_memops;
1230 if (imgu->buf_struct_size <= 0)
1231 imgu->buf_struct_size =
1232 sizeof(struct imgu_vb2_buffer);
1233 vbq->buf_struct_size = imgu->buf_struct_size;
1234 vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1235 /* can streamon w/o buffers */
1236 vbq->min_buffers_needed = 0;
1237 vbq->drv_priv = imgu;
1238 vbq->lock = &node->lock;
1239 r = vb2_queue_init(vbq);
1241 dev_err(dev, "failed to initialize video queue (%d)", r);
1242 media_entity_cleanup(&vdev->entity);
1246 /* Initialize vdev */
1247 snprintf(vdev->name, sizeof(vdev->name), "%s %u %s",
1248 IMGU_NAME, pipe, node->name);
1249 vdev->release = video_device_release_empty;
1250 vdev->fops = &imgu_v4l2_fops;
1251 vdev->lock = &node->lock;
1252 vdev->v4l2_dev = &imgu->v4l2_dev;
1253 vdev->queue = &node->vbq;
1254 vdev->vfl_dir = node->output ? VFL_DIR_TX : VFL_DIR_RX;
1255 video_set_drvdata(vdev, imgu);
1256 r = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
1258 dev_err(dev, "failed to register video device (%d)", r);
1259 media_entity_cleanup(&vdev->entity);
1263 /* Create link between video node and the subdev pad */
1266 flags |= MEDIA_LNK_FL_ENABLED;
1268 r = media_create_pad_link(&vdev->entity, 0, &sd->entity,
1271 if (node->id == IMGU_NODE_OUT) {
1272 flags |= MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE;
1273 node->enabled = true;
1276 r = media_create_pad_link(&sd->entity, node_num, &vdev->entity,
1280 dev_err(dev, "failed to create pad link (%d)", r);
1281 video_unregister_device(vdev);
1288 static void imgu_v4l2_nodes_cleanup_pipe(struct imgu_device *imgu,
1289 unsigned int pipe, int node)
1292 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe];
1294 for (i = 0; i < node; i++) {
1295 video_unregister_device(&imgu_pipe->nodes[i].vdev);
1296 media_entity_cleanup(&imgu_pipe->nodes[i].vdev.entity);
1297 mutex_destroy(&imgu_pipe->nodes[i].lock);
1301 static int imgu_v4l2_nodes_setup_pipe(struct imgu_device *imgu, int pipe)
1305 for (i = 0; i < IMGU_NODE_NUM; i++) {
1306 int r = imgu_v4l2_node_setup(imgu, pipe, i);
1309 imgu_v4l2_nodes_cleanup_pipe(imgu, pipe, i);
1316 static void imgu_v4l2_subdev_cleanup(struct imgu_device *imgu, unsigned int i)
1318 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[i];
1320 v4l2_device_unregister_subdev(&imgu_pipe->imgu_sd.subdev);
1321 v4l2_ctrl_handler_free(imgu_pipe->imgu_sd.subdev.ctrl_handler);
1322 media_entity_cleanup(&imgu_pipe->imgu_sd.subdev.entity);
1325 static void imgu_v4l2_cleanup_pipes(struct imgu_device *imgu, unsigned int pipe)
1329 for (i = 0; i < pipe; i++) {
1330 imgu_v4l2_nodes_cleanup_pipe(imgu, i, IMGU_NODE_NUM);
1331 imgu_v4l2_subdev_cleanup(imgu, i);
1335 static int imgu_v4l2_register_pipes(struct imgu_device *imgu)
1337 struct imgu_media_pipe *imgu_pipe;
1340 for (i = 0; i < IMGU_MAX_PIPE_NUM; i++) {
1341 imgu_pipe = &imgu->imgu_pipe[i];
1342 r = imgu_v4l2_subdev_register(imgu, &imgu_pipe->imgu_sd, i);
1344 dev_err(&imgu->pci_dev->dev,
1345 "failed to register subdev%u ret (%d)\n", i, r);
1348 r = imgu_v4l2_nodes_setup_pipe(imgu, i);
1350 imgu_v4l2_subdev_cleanup(imgu, i);
1358 imgu_v4l2_cleanup_pipes(imgu, i);
1362 int imgu_v4l2_register(struct imgu_device *imgu)
1366 /* Initialize miscellaneous variables */
1367 imgu->streaming = false;
1369 /* Set up media device */
1370 media_device_pci_init(&imgu->media_dev, imgu->pci_dev, IMGU_NAME);
1372 /* Set up v4l2 device */
1373 imgu->v4l2_dev.mdev = &imgu->media_dev;
1374 imgu->v4l2_dev.ctrl_handler = NULL;
1375 r = v4l2_device_register(&imgu->pci_dev->dev, &imgu->v4l2_dev);
1377 dev_err(&imgu->pci_dev->dev,
1378 "failed to register V4L2 device (%d)\n", r);
1382 r = imgu_v4l2_register_pipes(imgu);
1384 dev_err(&imgu->pci_dev->dev,
1385 "failed to register pipes (%d)\n", r);
1386 goto fail_v4l2_pipes;
1389 r = v4l2_device_register_subdev_nodes(&imgu->v4l2_dev);
1391 dev_err(&imgu->pci_dev->dev,
1392 "failed to register subdevs (%d)\n", r);
1396 r = media_device_register(&imgu->media_dev);
1398 dev_err(&imgu->pci_dev->dev,
1399 "failed to register media device (%d)\n", r);
1406 imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM);
1408 v4l2_device_unregister(&imgu->v4l2_dev);
1410 media_device_cleanup(&imgu->media_dev);
1415 int imgu_v4l2_unregister(struct imgu_device *imgu)
1417 media_device_unregister(&imgu->media_dev);
1418 imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM);
1419 v4l2_device_unregister(&imgu->v4l2_dev);
1420 media_device_cleanup(&imgu->media_dev);
1425 void imgu_v4l2_buffer_done(struct vb2_buffer *vb,
1426 enum vb2_buffer_state state)
1428 struct imgu_vb2_buffer *b =
1429 container_of(vb, struct imgu_vb2_buffer, vbb.vb2_buf);
1432 vb2_buffer_done(&b->vbb.vb2_buf, state);