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