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 %d", __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 %d pipe_id %d", 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 %d 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 %d pad %d fmt to [%dx%d]", 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 %d sel which %d target 0x%4x rect [%dx%d]", 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 %d pad %d 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 %d 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 unsigned long need_bytes; 345 unsigned int pipe = node->pipe; 346 347 if (vb->vb2_queue->type == V4L2_BUF_TYPE_META_CAPTURE || 348 vb->vb2_queue->type == V4L2_BUF_TYPE_META_OUTPUT) 349 need_bytes = node->vdev_fmt.fmt.meta.buffersize; 350 else 351 need_bytes = node->vdev_fmt.fmt.pix_mp.plane_fmt[0].sizeimage; 352 353 if (queue == IPU3_CSS_QUEUE_PARAMS) { 354 unsigned long payload = vb2_get_plane_payload(vb, 0); 355 struct vb2_v4l2_buffer *buf = 356 container_of(vb, struct vb2_v4l2_buffer, vb2_buf); 357 int r = -EINVAL; 358 359 if (payload == 0) { 360 payload = need_bytes; 361 vb2_set_plane_payload(vb, 0, payload); 362 } 363 if (payload >= need_bytes) 364 r = imgu_css_set_parameters(&imgu->css, pipe, 365 vb2_plane_vaddr(vb, 0)); 366 buf->flags = V4L2_BUF_FLAG_DONE; 367 vb2_buffer_done(vb, r == 0 ? VB2_BUF_STATE_DONE 368 : VB2_BUF_STATE_ERROR); 369 370 } else { 371 struct imgu_buffer *buf = container_of(vb, struct imgu_buffer, 372 vid_buf.vbb.vb2_buf); 373 374 mutex_lock(&imgu->lock); 375 imgu_css_buf_init(&buf->css_buf, queue, buf->map.daddr); 376 list_add_tail(&buf->vid_buf.list, 377 &node->buffers); 378 mutex_unlock(&imgu->lock); 379 380 vb2_set_plane_payload(&buf->vid_buf.vbb.vb2_buf, 0, need_bytes); 381 382 if (imgu->streaming) 383 imgu_queue_buffers(imgu, false, pipe); 384 } 385 386 dev_dbg(&imgu->pci_dev->dev, "%s for pipe %d node %d", __func__, 387 node->pipe, node->id); 388 389 } 390 391 static int imgu_vb2_queue_setup(struct vb2_queue *vq, 392 unsigned int *num_buffers, 393 unsigned int *num_planes, 394 unsigned int sizes[], 395 struct device *alloc_devs[]) 396 { 397 struct imgu_device *imgu = vb2_get_drv_priv(vq); 398 struct imgu_video_device *node = 399 container_of(vq, struct imgu_video_device, vbq); 400 const struct v4l2_format *fmt = &node->vdev_fmt; 401 unsigned int size; 402 403 *num_buffers = clamp_val(*num_buffers, 1, VB2_MAX_FRAME); 404 alloc_devs[0] = &imgu->pci_dev->dev; 405 406 if (vq->type == V4L2_BUF_TYPE_META_CAPTURE || 407 vq->type == V4L2_BUF_TYPE_META_OUTPUT) 408 size = fmt->fmt.meta.buffersize; 409 else 410 size = fmt->fmt.pix_mp.plane_fmt[0].sizeimage; 411 412 if (*num_planes) { 413 if (sizes[0] < size) 414 return -EINVAL; 415 size = sizes[0]; 416 } 417 418 *num_planes = 1; 419 sizes[0] = size; 420 421 /* Initialize buffer queue */ 422 INIT_LIST_HEAD(&node->buffers); 423 424 return 0; 425 } 426 427 /* Check if all enabled video nodes are streaming, exception ignored */ 428 static bool imgu_all_nodes_streaming(struct imgu_device *imgu, 429 struct imgu_video_device *except) 430 { 431 unsigned int i, pipe, p; 432 struct imgu_video_device *node; 433 struct device *dev = &imgu->pci_dev->dev; 434 435 pipe = except->pipe; 436 if (!test_bit(pipe, imgu->css.enabled_pipes)) { 437 dev_warn(&imgu->pci_dev->dev, 438 "pipe %d link is not ready yet", pipe); 439 return false; 440 } 441 442 for_each_set_bit(p, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) { 443 for (i = 0; i < IMGU_NODE_NUM; i++) { 444 node = &imgu->imgu_pipe[p].nodes[i]; 445 dev_dbg(dev, "%s pipe %u queue %u name %s enabled = %u", 446 __func__, p, i, node->name, node->enabled); 447 if (node == except) 448 continue; 449 if (node->enabled && !vb2_start_streaming_called(&node->vbq)) 450 return false; 451 } 452 } 453 454 return true; 455 } 456 457 static void imgu_return_all_buffers(struct imgu_device *imgu, 458 struct imgu_video_device *node, 459 enum vb2_buffer_state state) 460 { 461 struct imgu_vb2_buffer *b, *b0; 462 463 /* Return all buffers */ 464 mutex_lock(&imgu->lock); 465 list_for_each_entry_safe(b, b0, &node->buffers, list) { 466 list_del(&b->list); 467 vb2_buffer_done(&b->vbb.vb2_buf, state); 468 } 469 mutex_unlock(&imgu->lock); 470 } 471 472 static int imgu_vb2_start_streaming(struct vb2_queue *vq, unsigned int count) 473 { 474 struct imgu_media_pipe *imgu_pipe; 475 struct imgu_device *imgu = vb2_get_drv_priv(vq); 476 struct device *dev = &imgu->pci_dev->dev; 477 struct imgu_video_device *node = 478 container_of(vq, struct imgu_video_device, vbq); 479 int r; 480 unsigned int pipe; 481 482 dev_dbg(dev, "%s node name %s pipe %d id %u", __func__, 483 node->name, node->pipe, node->id); 484 485 if (imgu->streaming) { 486 r = -EBUSY; 487 goto fail_return_bufs; 488 } 489 490 if (!node->enabled) { 491 dev_err(dev, "IMGU node is not enabled"); 492 r = -EINVAL; 493 goto fail_return_bufs; 494 } 495 496 pipe = node->pipe; 497 imgu_pipe = &imgu->imgu_pipe[pipe]; 498 r = media_pipeline_start(&node->vdev.entity, &imgu_pipe->pipeline); 499 if (r < 0) 500 goto fail_return_bufs; 501 502 503 if (!imgu_all_nodes_streaming(imgu, node)) 504 return 0; 505 506 for_each_set_bit(pipe, imgu->css.enabled_pipes, IMGU_MAX_PIPE_NUM) { 507 r = v4l2_subdev_call(&imgu->imgu_pipe[pipe].imgu_sd.subdev, 508 video, s_stream, 1); 509 if (r < 0) 510 goto fail_stop_pipeline; 511 } 512 513 /* Start streaming of the whole pipeline now */ 514 dev_dbg(dev, "IMGU streaming is ready to start"); 515 r = imgu_s_stream(imgu, true); 516 if (!r) 517 imgu->streaming = true; 518 519 return 0; 520 521 fail_stop_pipeline: 522 media_pipeline_stop(&node->vdev.entity); 523 fail_return_bufs: 524 imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_QUEUED); 525 526 return r; 527 } 528 529 static void imgu_vb2_stop_streaming(struct vb2_queue *vq) 530 { 531 struct imgu_media_pipe *imgu_pipe; 532 struct imgu_device *imgu = vb2_get_drv_priv(vq); 533 struct device *dev = &imgu->pci_dev->dev; 534 struct imgu_video_device *node = 535 container_of(vq, struct imgu_video_device, vbq); 536 int r; 537 unsigned int pipe; 538 539 WARN_ON(!node->enabled); 540 541 pipe = node->pipe; 542 dev_dbg(dev, "Try to stream off node [%d][%d]", pipe, node->id); 543 imgu_pipe = &imgu->imgu_pipe[pipe]; 544 r = v4l2_subdev_call(&imgu_pipe->imgu_sd.subdev, video, s_stream, 0); 545 if (r) 546 dev_err(&imgu->pci_dev->dev, 547 "failed to stop subdev streaming\n"); 548 549 /* Was this the first node with streaming disabled? */ 550 if (imgu->streaming && imgu_all_nodes_streaming(imgu, node)) { 551 /* Yes, really stop streaming now */ 552 dev_dbg(dev, "IMGU streaming is ready to stop"); 553 r = imgu_s_stream(imgu, false); 554 if (!r) 555 imgu->streaming = false; 556 } 557 558 imgu_return_all_buffers(imgu, node, VB2_BUF_STATE_ERROR); 559 media_pipeline_stop(&node->vdev.entity); 560 } 561 562 /******************** v4l2_ioctl_ops ********************/ 563 564 #define VID_CAPTURE 0 565 #define VID_OUTPUT 1 566 #define DEF_VID_CAPTURE 0 567 #define DEF_VID_OUTPUT 1 568 569 struct imgu_fmt { 570 u32 fourcc; 571 u16 type; /* VID_CAPTURE or VID_OUTPUT not both */ 572 }; 573 574 /* format descriptions for capture and preview */ 575 static const struct imgu_fmt formats[] = { 576 { V4L2_PIX_FMT_NV12, VID_CAPTURE }, 577 { V4L2_PIX_FMT_IPU3_SGRBG10, VID_OUTPUT }, 578 { V4L2_PIX_FMT_IPU3_SBGGR10, VID_OUTPUT }, 579 { V4L2_PIX_FMT_IPU3_SGBRG10, VID_OUTPUT }, 580 { V4L2_PIX_FMT_IPU3_SRGGB10, VID_OUTPUT }, 581 }; 582 583 /* Find the first matched format, return default if not found */ 584 static const struct imgu_fmt *find_format(struct v4l2_format *f, u32 type) 585 { 586 unsigned int i; 587 588 for (i = 0; i < ARRAY_SIZE(formats); i++) { 589 if (formats[i].fourcc == f->fmt.pix_mp.pixelformat && 590 formats[i].type == type) 591 return &formats[i]; 592 } 593 594 return type == VID_CAPTURE ? &formats[DEF_VID_CAPTURE] : 595 &formats[DEF_VID_OUTPUT]; 596 } 597 598 static int imgu_vidioc_querycap(struct file *file, void *fh, 599 struct v4l2_capability *cap) 600 { 601 struct imgu_video_device *node = file_to_intel_imgu_node(file); 602 603 strscpy(cap->driver, IMGU_NAME, sizeof(cap->driver)); 604 strscpy(cap->card, IMGU_NAME, sizeof(cap->card)); 605 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s", node->name); 606 607 return 0; 608 } 609 610 static int enum_fmts(struct v4l2_fmtdesc *f, u32 type) 611 { 612 unsigned int i, j; 613 614 for (i = j = 0; i < ARRAY_SIZE(formats); ++i) { 615 if (formats[i].type == type) { 616 if (j == f->index) 617 break; 618 ++j; 619 } 620 } 621 622 if (i < ARRAY_SIZE(formats)) { 623 f->pixelformat = formats[i].fourcc; 624 return 0; 625 } 626 627 return -EINVAL; 628 } 629 630 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 631 struct v4l2_fmtdesc *f) 632 { 633 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 634 return -EINVAL; 635 636 return enum_fmts(f, VID_CAPTURE); 637 } 638 639 static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, 640 struct v4l2_fmtdesc *f) 641 { 642 if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 643 return -EINVAL; 644 645 return enum_fmts(f, VID_OUTPUT); 646 } 647 648 /* Propagate forward always the format from the CIO2 subdev */ 649 static int imgu_vidioc_g_fmt(struct file *file, void *fh, 650 struct v4l2_format *f) 651 { 652 struct imgu_video_device *node = file_to_intel_imgu_node(file); 653 654 f->fmt = node->vdev_fmt.fmt; 655 656 return 0; 657 } 658 659 /* 660 * Set input/output format. Unless it is just a try, this also resets 661 * selections (ie. effective and BDS resolutions) to defaults. 662 */ 663 static int imgu_fmt(struct imgu_device *imgu, unsigned int pipe, int node, 664 struct v4l2_format *f, bool try) 665 { 666 struct device *dev = &imgu->pci_dev->dev; 667 struct v4l2_pix_format_mplane try_fmts[IPU3_CSS_QUEUES]; 668 struct v4l2_pix_format_mplane *fmts[IPU3_CSS_QUEUES] = { NULL }; 669 struct v4l2_rect *rects[IPU3_CSS_RECTS] = { NULL }; 670 struct v4l2_mbus_framefmt pad_fmt; 671 unsigned int i, css_q; 672 int r; 673 struct imgu_css_pipe *css_pipe = &imgu->css.pipes[pipe]; 674 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe]; 675 struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd; 676 677 dev_dbg(dev, "set fmt node [%u][%u](try = %d)", pipe, node, try); 678 679 for (i = 0; i < IMGU_NODE_NUM; i++) 680 dev_dbg(dev, "IMGU pipe %d node %d enabled = %d", 681 pipe, i, imgu_pipe->nodes[i].enabled); 682 683 if (imgu_pipe->nodes[IMGU_NODE_VF].enabled) 684 css_pipe->vf_output_en = true; 685 686 if (atomic_read(&imgu_sd->running_mode) == IPU3_RUNNING_MODE_VIDEO) 687 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_VIDEO; 688 else 689 css_pipe->pipe_id = IPU3_CSS_PIPE_ID_CAPTURE; 690 691 dev_dbg(dev, "IPU3 pipe %d pipe_id = %d", pipe, css_pipe->pipe_id); 692 693 for (i = 0; i < IPU3_CSS_QUEUES; i++) { 694 unsigned int inode = imgu_map_node(imgu, i); 695 696 /* Skip the meta node */ 697 if (inode == IMGU_NODE_STAT_3A || inode == IMGU_NODE_PARAMS) 698 continue; 699 700 if (try) { 701 try_fmts[i] = 702 imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp; 703 fmts[i] = &try_fmts[i]; 704 } else { 705 fmts[i] = &imgu_pipe->nodes[inode].vdev_fmt.fmt.pix_mp; 706 } 707 708 /* CSS expects some format on OUT queue */ 709 if (i != IPU3_CSS_QUEUE_OUT && 710 !imgu_pipe->nodes[inode].enabled) 711 fmts[i] = NULL; 712 } 713 714 if (!try) { 715 /* eff and bds res got by imgu_s_sel */ 716 struct imgu_v4l2_subdev *imgu_sd = &imgu_pipe->imgu_sd; 717 718 rects[IPU3_CSS_RECT_EFFECTIVE] = &imgu_sd->rect.eff; 719 rects[IPU3_CSS_RECT_BDS] = &imgu_sd->rect.bds; 720 rects[IPU3_CSS_RECT_GDC] = &imgu_sd->rect.gdc; 721 722 /* suppose that pad fmt was set by subdev s_fmt before */ 723 pad_fmt = imgu_pipe->nodes[IMGU_NODE_IN].pad_fmt; 724 rects[IPU3_CSS_RECT_GDC]->width = pad_fmt.width; 725 rects[IPU3_CSS_RECT_GDC]->height = pad_fmt.height; 726 } 727 728 /* 729 * imgu doesn't set the node to the value given by user 730 * before we return success from this function, so set it here. 731 */ 732 css_q = imgu_node_to_queue(node); 733 if (fmts[css_q]) 734 *fmts[css_q] = f->fmt.pix_mp; 735 else 736 return -EINVAL; 737 738 if (try) 739 r = imgu_css_fmt_try(&imgu->css, fmts, rects, pipe); 740 else 741 r = imgu_css_fmt_set(&imgu->css, fmts, rects, pipe); 742 743 /* r is the binary number in the firmware blob */ 744 if (r < 0) 745 return r; 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 return 0; 753 } 754 755 static int imgu_try_fmt(struct file *file, void *fh, struct v4l2_format *f) 756 { 757 struct v4l2_pix_format_mplane *pixm = &f->fmt.pix_mp; 758 const struct imgu_fmt *fmt; 759 760 if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) 761 fmt = find_format(f, VID_CAPTURE); 762 else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 763 fmt = find_format(f, VID_OUTPUT); 764 else 765 return -EINVAL; 766 767 pixm->pixelformat = fmt->fourcc; 768 769 memset(pixm->plane_fmt[0].reserved, 0, 770 sizeof(pixm->plane_fmt[0].reserved)); 771 772 return 0; 773 } 774 775 static int imgu_vidioc_try_fmt(struct file *file, void *fh, 776 struct v4l2_format *f) 777 { 778 struct imgu_device *imgu = video_drvdata(file); 779 struct device *dev = &imgu->pci_dev->dev; 780 struct imgu_video_device *node = file_to_intel_imgu_node(file); 781 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 782 int r; 783 784 dev_dbg(dev, "%s [%ux%u] for node %d\n", __func__, 785 pix_mp->width, pix_mp->height, node->id); 786 787 r = imgu_try_fmt(file, fh, f); 788 if (r) 789 return r; 790 791 return imgu_fmt(imgu, node->pipe, node->id, f, true); 792 } 793 794 static int imgu_vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f) 795 { 796 struct imgu_device *imgu = video_drvdata(file); 797 struct device *dev = &imgu->pci_dev->dev; 798 struct imgu_video_device *node = file_to_intel_imgu_node(file); 799 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 800 int r; 801 802 dev_dbg(dev, "%s [%ux%u] for node %d\n", __func__, 803 pix_mp->width, pix_mp->height, node->id); 804 805 r = imgu_try_fmt(file, fh, f); 806 if (r) 807 return r; 808 809 return imgu_fmt(imgu, node->pipe, node->id, f, false); 810 } 811 812 struct imgu_meta_fmt { 813 __u32 fourcc; 814 char *name; 815 }; 816 817 /* From drivers/media/v4l2-core/v4l2-ioctl.c */ 818 static const struct imgu_meta_fmt meta_fmts[] = { 819 { V4L2_META_FMT_IPU3_PARAMS, "IPU3 processing parameters" }, 820 { V4L2_META_FMT_IPU3_STAT_3A, "IPU3 3A statistics" }, 821 }; 822 823 static int imgu_meta_enum_format(struct file *file, void *fh, 824 struct v4l2_fmtdesc *fmt) 825 { 826 struct imgu_video_device *node = file_to_intel_imgu_node(file); 827 unsigned int i = fmt->type == V4L2_BUF_TYPE_META_OUTPUT ? 0 : 1; 828 829 /* Each node is dedicated to only one meta format */ 830 if (fmt->index > 0 || fmt->type != node->vbq.type) 831 return -EINVAL; 832 833 strscpy(fmt->description, meta_fmts[i].name, sizeof(fmt->description)); 834 fmt->pixelformat = meta_fmts[i].fourcc; 835 836 return 0; 837 } 838 839 static int imgu_vidioc_g_meta_fmt(struct file *file, void *fh, 840 struct v4l2_format *f) 841 { 842 struct imgu_video_device *node = file_to_intel_imgu_node(file); 843 844 if (f->type != node->vbq.type) 845 return -EINVAL; 846 847 f->fmt = node->vdev_fmt.fmt; 848 849 return 0; 850 } 851 852 static int imgu_vidioc_enum_input(struct file *file, void *fh, 853 struct v4l2_input *input) 854 { 855 if (input->index > 0) 856 return -EINVAL; 857 strscpy(input->name, "camera", sizeof(input->name)); 858 input->type = V4L2_INPUT_TYPE_CAMERA; 859 860 return 0; 861 } 862 863 static int imgu_vidioc_g_input(struct file *file, void *fh, unsigned int *input) 864 { 865 *input = 0; 866 867 return 0; 868 } 869 870 static int imgu_vidioc_s_input(struct file *file, void *fh, unsigned int input) 871 { 872 return input == 0 ? 0 : -EINVAL; 873 } 874 875 static int imgu_vidioc_enum_output(struct file *file, void *fh, 876 struct v4l2_output *output) 877 { 878 if (output->index > 0) 879 return -EINVAL; 880 strscpy(output->name, "camera", sizeof(output->name)); 881 output->type = V4L2_INPUT_TYPE_CAMERA; 882 883 return 0; 884 } 885 886 static int imgu_vidioc_g_output(struct file *file, void *fh, 887 unsigned int *output) 888 { 889 *output = 0; 890 891 return 0; 892 } 893 894 static int imgu_vidioc_s_output(struct file *file, void *fh, 895 unsigned int output) 896 { 897 return output == 0 ? 0 : -EINVAL; 898 } 899 900 /******************** function pointers ********************/ 901 902 static struct v4l2_subdev_internal_ops imgu_subdev_internal_ops = { 903 .open = imgu_subdev_open, 904 }; 905 906 static const struct v4l2_subdev_core_ops imgu_subdev_core_ops = { 907 .subscribe_event = v4l2_ctrl_subdev_subscribe_event, 908 .unsubscribe_event = v4l2_event_subdev_unsubscribe, 909 }; 910 911 static const struct v4l2_subdev_video_ops imgu_subdev_video_ops = { 912 .s_stream = imgu_subdev_s_stream, 913 }; 914 915 static const struct v4l2_subdev_pad_ops imgu_subdev_pad_ops = { 916 .link_validate = v4l2_subdev_link_validate_default, 917 .get_fmt = imgu_subdev_get_fmt, 918 .set_fmt = imgu_subdev_set_fmt, 919 .get_selection = imgu_subdev_get_selection, 920 .set_selection = imgu_subdev_set_selection, 921 }; 922 923 static const struct v4l2_subdev_ops imgu_subdev_ops = { 924 .core = &imgu_subdev_core_ops, 925 .video = &imgu_subdev_video_ops, 926 .pad = &imgu_subdev_pad_ops, 927 }; 928 929 static const struct media_entity_operations imgu_media_ops = { 930 .link_setup = imgu_link_setup, 931 .link_validate = v4l2_subdev_link_validate, 932 }; 933 934 /****************** vb2_ops of the Q ********************/ 935 936 static const struct vb2_ops imgu_vb2_ops = { 937 .buf_init = imgu_vb2_buf_init, 938 .buf_cleanup = imgu_vb2_buf_cleanup, 939 .buf_queue = imgu_vb2_buf_queue, 940 .queue_setup = imgu_vb2_queue_setup, 941 .start_streaming = imgu_vb2_start_streaming, 942 .stop_streaming = imgu_vb2_stop_streaming, 943 .wait_prepare = vb2_ops_wait_prepare, 944 .wait_finish = vb2_ops_wait_finish, 945 }; 946 947 /****************** v4l2_file_operations *****************/ 948 949 static const struct v4l2_file_operations imgu_v4l2_fops = { 950 .unlocked_ioctl = video_ioctl2, 951 .open = v4l2_fh_open, 952 .release = vb2_fop_release, 953 .poll = vb2_fop_poll, 954 .mmap = vb2_fop_mmap, 955 }; 956 957 /******************** v4l2_ioctl_ops ********************/ 958 959 static const struct v4l2_ioctl_ops imgu_v4l2_ioctl_ops = { 960 .vidioc_querycap = imgu_vidioc_querycap, 961 962 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap, 963 .vidioc_g_fmt_vid_cap_mplane = imgu_vidioc_g_fmt, 964 .vidioc_s_fmt_vid_cap_mplane = imgu_vidioc_s_fmt, 965 .vidioc_try_fmt_vid_cap_mplane = imgu_vidioc_try_fmt, 966 967 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out, 968 .vidioc_g_fmt_vid_out_mplane = imgu_vidioc_g_fmt, 969 .vidioc_s_fmt_vid_out_mplane = imgu_vidioc_s_fmt, 970 .vidioc_try_fmt_vid_out_mplane = imgu_vidioc_try_fmt, 971 972 .vidioc_enum_output = imgu_vidioc_enum_output, 973 .vidioc_g_output = imgu_vidioc_g_output, 974 .vidioc_s_output = imgu_vidioc_s_output, 975 976 .vidioc_enum_input = imgu_vidioc_enum_input, 977 .vidioc_g_input = imgu_vidioc_g_input, 978 .vidioc_s_input = imgu_vidioc_s_input, 979 980 /* buffer queue management */ 981 .vidioc_reqbufs = vb2_ioctl_reqbufs, 982 .vidioc_create_bufs = vb2_ioctl_create_bufs, 983 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 984 .vidioc_querybuf = vb2_ioctl_querybuf, 985 .vidioc_qbuf = vb2_ioctl_qbuf, 986 .vidioc_dqbuf = vb2_ioctl_dqbuf, 987 .vidioc_streamon = vb2_ioctl_streamon, 988 .vidioc_streamoff = vb2_ioctl_streamoff, 989 .vidioc_expbuf = vb2_ioctl_expbuf, 990 }; 991 992 static const struct v4l2_ioctl_ops imgu_v4l2_meta_ioctl_ops = { 993 .vidioc_querycap = imgu_vidioc_querycap, 994 995 /* meta capture */ 996 .vidioc_enum_fmt_meta_cap = imgu_meta_enum_format, 997 .vidioc_g_fmt_meta_cap = imgu_vidioc_g_meta_fmt, 998 .vidioc_s_fmt_meta_cap = imgu_vidioc_g_meta_fmt, 999 .vidioc_try_fmt_meta_cap = imgu_vidioc_g_meta_fmt, 1000 1001 /* meta output */ 1002 .vidioc_enum_fmt_meta_out = imgu_meta_enum_format, 1003 .vidioc_g_fmt_meta_out = imgu_vidioc_g_meta_fmt, 1004 .vidioc_s_fmt_meta_out = imgu_vidioc_g_meta_fmt, 1005 .vidioc_try_fmt_meta_out = imgu_vidioc_g_meta_fmt, 1006 1007 .vidioc_reqbufs = vb2_ioctl_reqbufs, 1008 .vidioc_create_bufs = vb2_ioctl_create_bufs, 1009 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 1010 .vidioc_querybuf = vb2_ioctl_querybuf, 1011 .vidioc_qbuf = vb2_ioctl_qbuf, 1012 .vidioc_dqbuf = vb2_ioctl_dqbuf, 1013 .vidioc_streamon = vb2_ioctl_streamon, 1014 .vidioc_streamoff = vb2_ioctl_streamoff, 1015 .vidioc_expbuf = vb2_ioctl_expbuf, 1016 }; 1017 1018 static int imgu_sd_s_ctrl(struct v4l2_ctrl *ctrl) 1019 { 1020 struct imgu_v4l2_subdev *imgu_sd = 1021 container_of(ctrl->handler, struct imgu_v4l2_subdev, ctrl_handler); 1022 struct imgu_device *imgu = v4l2_get_subdevdata(&imgu_sd->subdev); 1023 struct device *dev = &imgu->pci_dev->dev; 1024 1025 dev_dbg(dev, "set val %d to ctrl 0x%8x for subdev %d", 1026 ctrl->val, ctrl->id, imgu_sd->pipe); 1027 1028 switch (ctrl->id) { 1029 case V4L2_CID_INTEL_IPU3_MODE: 1030 atomic_set(&imgu_sd->running_mode, ctrl->val); 1031 return 0; 1032 default: 1033 return -EINVAL; 1034 } 1035 } 1036 1037 static const struct v4l2_ctrl_ops imgu_subdev_ctrl_ops = { 1038 .s_ctrl = imgu_sd_s_ctrl, 1039 }; 1040 1041 static const char * const imgu_ctrl_mode_strings[] = { 1042 "Video mode", 1043 "Still mode", 1044 }; 1045 1046 static const struct v4l2_ctrl_config imgu_subdev_ctrl_mode = { 1047 .ops = &imgu_subdev_ctrl_ops, 1048 .id = V4L2_CID_INTEL_IPU3_MODE, 1049 .name = "IPU3 Pipe Mode", 1050 .type = V4L2_CTRL_TYPE_MENU, 1051 .max = ARRAY_SIZE(imgu_ctrl_mode_strings) - 1, 1052 .def = IPU3_RUNNING_MODE_VIDEO, 1053 .qmenu = imgu_ctrl_mode_strings, 1054 }; 1055 1056 /******************** Framework registration ********************/ 1057 1058 /* helper function to config node's video properties */ 1059 static void imgu_node_to_v4l2(u32 node, struct video_device *vdev, 1060 struct v4l2_format *f) 1061 { 1062 u32 cap; 1063 1064 /* Should not happen */ 1065 WARN_ON(node >= IMGU_NODE_NUM); 1066 1067 switch (node) { 1068 case IMGU_NODE_IN: 1069 cap = V4L2_CAP_VIDEO_OUTPUT_MPLANE; 1070 f->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1071 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops; 1072 break; 1073 case IMGU_NODE_PARAMS: 1074 cap = V4L2_CAP_META_OUTPUT; 1075 f->type = V4L2_BUF_TYPE_META_OUTPUT; 1076 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_PARAMS; 1077 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops; 1078 imgu_css_meta_fmt_set(&f->fmt.meta); 1079 break; 1080 case IMGU_NODE_STAT_3A: 1081 cap = V4L2_CAP_META_CAPTURE; 1082 f->type = V4L2_BUF_TYPE_META_CAPTURE; 1083 f->fmt.meta.dataformat = V4L2_META_FMT_IPU3_STAT_3A; 1084 vdev->ioctl_ops = &imgu_v4l2_meta_ioctl_ops; 1085 imgu_css_meta_fmt_set(&f->fmt.meta); 1086 break; 1087 default: 1088 cap = V4L2_CAP_VIDEO_CAPTURE_MPLANE; 1089 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1090 vdev->ioctl_ops = &imgu_v4l2_ioctl_ops; 1091 } 1092 1093 vdev->device_caps = V4L2_CAP_STREAMING | cap; 1094 } 1095 1096 static int imgu_v4l2_subdev_register(struct imgu_device *imgu, 1097 struct imgu_v4l2_subdev *imgu_sd, 1098 unsigned int pipe) 1099 { 1100 int i, r; 1101 struct v4l2_ctrl_handler *hdl = &imgu_sd->ctrl_handler; 1102 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe]; 1103 1104 /* Initialize subdev media entity */ 1105 r = media_entity_pads_init(&imgu_sd->subdev.entity, IMGU_NODE_NUM, 1106 imgu_sd->subdev_pads); 1107 if (r) { 1108 dev_err(&imgu->pci_dev->dev, 1109 "failed initialize subdev media entity (%d)\n", r); 1110 return r; 1111 } 1112 imgu_sd->subdev.entity.ops = &imgu_media_ops; 1113 for (i = 0; i < IMGU_NODE_NUM; i++) { 1114 imgu_sd->subdev_pads[i].flags = imgu_pipe->nodes[i].output ? 1115 MEDIA_PAD_FL_SINK : MEDIA_PAD_FL_SOURCE; 1116 } 1117 1118 /* Initialize subdev */ 1119 v4l2_subdev_init(&imgu_sd->subdev, &imgu_subdev_ops); 1120 imgu_sd->subdev.entity.function = MEDIA_ENT_F_PROC_VIDEO_STATISTICS; 1121 imgu_sd->subdev.internal_ops = &imgu_subdev_internal_ops; 1122 imgu_sd->subdev.flags = V4L2_SUBDEV_FL_HAS_DEVNODE | 1123 V4L2_SUBDEV_FL_HAS_EVENTS; 1124 snprintf(imgu_sd->subdev.name, sizeof(imgu_sd->subdev.name), 1125 "%s %d", IMGU_NAME, pipe); 1126 v4l2_set_subdevdata(&imgu_sd->subdev, imgu); 1127 atomic_set(&imgu_sd->running_mode, IPU3_RUNNING_MODE_VIDEO); 1128 v4l2_ctrl_handler_init(hdl, 1); 1129 imgu_sd->subdev.ctrl_handler = hdl; 1130 imgu_sd->ctrl = v4l2_ctrl_new_custom(hdl, &imgu_subdev_ctrl_mode, NULL); 1131 if (hdl->error) { 1132 r = hdl->error; 1133 dev_err(&imgu->pci_dev->dev, 1134 "failed to create subdev v4l2 ctrl with err %d", r); 1135 goto fail_subdev; 1136 } 1137 r = v4l2_device_register_subdev(&imgu->v4l2_dev, &imgu_sd->subdev); 1138 if (r) { 1139 dev_err(&imgu->pci_dev->dev, 1140 "failed initialize subdev (%d)\n", r); 1141 goto fail_subdev; 1142 } 1143 1144 imgu_sd->pipe = pipe; 1145 return 0; 1146 1147 fail_subdev: 1148 v4l2_ctrl_handler_free(imgu_sd->subdev.ctrl_handler); 1149 media_entity_cleanup(&imgu_sd->subdev.entity); 1150 1151 return r; 1152 } 1153 1154 static int imgu_v4l2_node_setup(struct imgu_device *imgu, unsigned int pipe, 1155 int node_num) 1156 { 1157 int r; 1158 u32 flags; 1159 struct v4l2_mbus_framefmt def_bus_fmt = { 0 }; 1160 struct v4l2_pix_format_mplane def_pix_fmt = { 0 }; 1161 struct device *dev = &imgu->pci_dev->dev; 1162 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe]; 1163 struct v4l2_subdev *sd = &imgu_pipe->imgu_sd.subdev; 1164 struct imgu_video_device *node = &imgu_pipe->nodes[node_num]; 1165 struct video_device *vdev = &node->vdev; 1166 struct vb2_queue *vbq = &node->vbq; 1167 1168 /* Initialize formats to default values */ 1169 def_bus_fmt.width = 1920; 1170 def_bus_fmt.height = 1080; 1171 def_bus_fmt.code = MEDIA_BUS_FMT_FIXED; 1172 def_bus_fmt.field = V4L2_FIELD_NONE; 1173 def_bus_fmt.colorspace = V4L2_COLORSPACE_RAW; 1174 def_bus_fmt.ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 1175 def_bus_fmt.quantization = V4L2_QUANTIZATION_DEFAULT; 1176 def_bus_fmt.xfer_func = V4L2_XFER_FUNC_DEFAULT; 1177 1178 def_pix_fmt.width = def_bus_fmt.width; 1179 def_pix_fmt.height = def_bus_fmt.height; 1180 def_pix_fmt.field = def_bus_fmt.field; 1181 def_pix_fmt.num_planes = 1; 1182 def_pix_fmt.plane_fmt[0].bytesperline = def_pix_fmt.width * 2; 1183 def_pix_fmt.plane_fmt[0].sizeimage = 1184 def_pix_fmt.height * def_pix_fmt.plane_fmt[0].bytesperline; 1185 def_pix_fmt.flags = 0; 1186 def_pix_fmt.colorspace = def_bus_fmt.colorspace; 1187 def_pix_fmt.ycbcr_enc = def_bus_fmt.ycbcr_enc; 1188 def_pix_fmt.quantization = def_bus_fmt.quantization; 1189 def_pix_fmt.xfer_func = def_bus_fmt.xfer_func; 1190 1191 /* Initialize miscellaneous variables */ 1192 mutex_init(&node->lock); 1193 INIT_LIST_HEAD(&node->buffers); 1194 1195 /* Initialize formats to default values */ 1196 node->pad_fmt = def_bus_fmt; 1197 node->id = node_num; 1198 node->pipe = pipe; 1199 imgu_node_to_v4l2(node_num, vdev, &node->vdev_fmt); 1200 if (node->vdev_fmt.type == 1201 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE || 1202 node->vdev_fmt.type == 1203 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) { 1204 def_pix_fmt.pixelformat = node->output ? 1205 V4L2_PIX_FMT_IPU3_SGRBG10 : 1206 V4L2_PIX_FMT_NV12; 1207 node->vdev_fmt.fmt.pix_mp = def_pix_fmt; 1208 } 1209 1210 /* Initialize media entities */ 1211 r = media_entity_pads_init(&vdev->entity, 1, &node->vdev_pad); 1212 if (r) { 1213 dev_err(dev, "failed initialize media entity (%d)\n", r); 1214 mutex_destroy(&node->lock); 1215 return r; 1216 } 1217 node->vdev_pad.flags = node->output ? 1218 MEDIA_PAD_FL_SOURCE : MEDIA_PAD_FL_SINK; 1219 vdev->entity.ops = NULL; 1220 1221 /* Initialize vbq */ 1222 vbq->type = node->vdev_fmt.type; 1223 vbq->io_modes = VB2_USERPTR | VB2_MMAP | VB2_DMABUF; 1224 vbq->ops = &imgu_vb2_ops; 1225 vbq->mem_ops = &vb2_dma_sg_memops; 1226 if (imgu->buf_struct_size <= 0) 1227 imgu->buf_struct_size = 1228 sizeof(struct imgu_vb2_buffer); 1229 vbq->buf_struct_size = imgu->buf_struct_size; 1230 vbq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1231 /* can streamon w/o buffers */ 1232 vbq->min_buffers_needed = 0; 1233 vbq->drv_priv = imgu; 1234 vbq->lock = &node->lock; 1235 r = vb2_queue_init(vbq); 1236 if (r) { 1237 dev_err(dev, "failed to initialize video queue (%d)", r); 1238 media_entity_cleanup(&vdev->entity); 1239 return r; 1240 } 1241 1242 /* Initialize vdev */ 1243 snprintf(vdev->name, sizeof(vdev->name), "%s %d %s", 1244 IMGU_NAME, pipe, node->name); 1245 vdev->release = video_device_release_empty; 1246 vdev->fops = &imgu_v4l2_fops; 1247 vdev->lock = &node->lock; 1248 vdev->v4l2_dev = &imgu->v4l2_dev; 1249 vdev->queue = &node->vbq; 1250 vdev->vfl_dir = node->output ? VFL_DIR_TX : VFL_DIR_RX; 1251 video_set_drvdata(vdev, imgu); 1252 r = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 1253 if (r) { 1254 dev_err(dev, "failed to register video device (%d)", r); 1255 media_entity_cleanup(&vdev->entity); 1256 return r; 1257 } 1258 1259 /* Create link between video node and the subdev pad */ 1260 flags = 0; 1261 if (node->enabled) 1262 flags |= MEDIA_LNK_FL_ENABLED; 1263 if (node->output) { 1264 r = media_create_pad_link(&vdev->entity, 0, &sd->entity, 1265 node_num, flags); 1266 } else { 1267 r = media_create_pad_link(&sd->entity, node_num, &vdev->entity, 1268 0, flags); 1269 } 1270 if (r) { 1271 dev_err(dev, "failed to create pad link (%d)", r); 1272 video_unregister_device(vdev); 1273 return r; 1274 } 1275 1276 return 0; 1277 } 1278 1279 static void imgu_v4l2_nodes_cleanup_pipe(struct imgu_device *imgu, 1280 unsigned int pipe, int node) 1281 { 1282 int i; 1283 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[pipe]; 1284 1285 for (i = 0; i < node; i++) { 1286 video_unregister_device(&imgu_pipe->nodes[i].vdev); 1287 media_entity_cleanup(&imgu_pipe->nodes[i].vdev.entity); 1288 mutex_destroy(&imgu_pipe->nodes[i].lock); 1289 } 1290 } 1291 1292 static int imgu_v4l2_nodes_setup_pipe(struct imgu_device *imgu, int pipe) 1293 { 1294 int i, r; 1295 1296 for (i = 0; i < IMGU_NODE_NUM; i++) { 1297 r = imgu_v4l2_node_setup(imgu, pipe, i); 1298 if (r) 1299 goto cleanup; 1300 } 1301 1302 return 0; 1303 1304 cleanup: 1305 imgu_v4l2_nodes_cleanup_pipe(imgu, pipe, i); 1306 return r; 1307 } 1308 1309 static void imgu_v4l2_subdev_cleanup(struct imgu_device *imgu, unsigned int i) 1310 { 1311 struct imgu_media_pipe *imgu_pipe = &imgu->imgu_pipe[i]; 1312 1313 v4l2_device_unregister_subdev(&imgu_pipe->imgu_sd.subdev); 1314 v4l2_ctrl_handler_free(imgu_pipe->imgu_sd.subdev.ctrl_handler); 1315 media_entity_cleanup(&imgu_pipe->imgu_sd.subdev.entity); 1316 } 1317 1318 static void imgu_v4l2_cleanup_pipes(struct imgu_device *imgu, unsigned int pipe) 1319 { 1320 int i; 1321 1322 for (i = 0; i < pipe; i++) { 1323 imgu_v4l2_nodes_cleanup_pipe(imgu, i, IMGU_NODE_NUM); 1324 imgu_v4l2_subdev_cleanup(imgu, i); 1325 } 1326 } 1327 1328 static int imgu_v4l2_register_pipes(struct imgu_device *imgu) 1329 { 1330 struct imgu_media_pipe *imgu_pipe; 1331 int i, r; 1332 1333 for (i = 0; i < IMGU_MAX_PIPE_NUM; i++) { 1334 imgu_pipe = &imgu->imgu_pipe[i]; 1335 r = imgu_v4l2_subdev_register(imgu, &imgu_pipe->imgu_sd, i); 1336 if (r) { 1337 dev_err(&imgu->pci_dev->dev, 1338 "failed to register subdev%d ret (%d)\n", i, r); 1339 goto pipes_cleanup; 1340 } 1341 r = imgu_v4l2_nodes_setup_pipe(imgu, i); 1342 if (r) { 1343 imgu_v4l2_subdev_cleanup(imgu, i); 1344 goto pipes_cleanup; 1345 } 1346 } 1347 1348 return 0; 1349 1350 pipes_cleanup: 1351 imgu_v4l2_cleanup_pipes(imgu, i); 1352 return r; 1353 } 1354 1355 int imgu_v4l2_register(struct imgu_device *imgu) 1356 { 1357 int r; 1358 1359 /* Initialize miscellaneous variables */ 1360 imgu->streaming = false; 1361 1362 /* Set up media device */ 1363 media_device_pci_init(&imgu->media_dev, imgu->pci_dev, IMGU_NAME); 1364 1365 /* Set up v4l2 device */ 1366 imgu->v4l2_dev.mdev = &imgu->media_dev; 1367 imgu->v4l2_dev.ctrl_handler = NULL; 1368 r = v4l2_device_register(&imgu->pci_dev->dev, &imgu->v4l2_dev); 1369 if (r) { 1370 dev_err(&imgu->pci_dev->dev, 1371 "failed to register V4L2 device (%d)\n", r); 1372 goto fail_v4l2_dev; 1373 } 1374 1375 r = imgu_v4l2_register_pipes(imgu); 1376 if (r) { 1377 dev_err(&imgu->pci_dev->dev, 1378 "failed to register pipes (%d)\n", r); 1379 goto fail_v4l2_pipes; 1380 } 1381 1382 r = v4l2_device_register_subdev_nodes(&imgu->v4l2_dev); 1383 if (r) { 1384 dev_err(&imgu->pci_dev->dev, 1385 "failed to register subdevs (%d)\n", r); 1386 goto fail_subdevs; 1387 } 1388 1389 r = media_device_register(&imgu->media_dev); 1390 if (r) { 1391 dev_err(&imgu->pci_dev->dev, 1392 "failed to register media device (%d)\n", r); 1393 goto fail_subdevs; 1394 } 1395 1396 return 0; 1397 1398 fail_subdevs: 1399 imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM); 1400 fail_v4l2_pipes: 1401 v4l2_device_unregister(&imgu->v4l2_dev); 1402 fail_v4l2_dev: 1403 media_device_cleanup(&imgu->media_dev); 1404 1405 return r; 1406 } 1407 1408 int imgu_v4l2_unregister(struct imgu_device *imgu) 1409 { 1410 media_device_unregister(&imgu->media_dev); 1411 imgu_v4l2_cleanup_pipes(imgu, IMGU_MAX_PIPE_NUM); 1412 v4l2_device_unregister(&imgu->v4l2_dev); 1413 media_device_cleanup(&imgu->media_dev); 1414 1415 return 0; 1416 } 1417 1418 void imgu_v4l2_buffer_done(struct vb2_buffer *vb, 1419 enum vb2_buffer_state state) 1420 { 1421 struct imgu_vb2_buffer *b = 1422 container_of(vb, struct imgu_vb2_buffer, vbb.vb2_buf); 1423 1424 list_del(&b->list); 1425 vb2_buffer_done(&b->vbb.vb2_buf, state); 1426 } 1427