1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020 NVIDIA CORPORATION. All rights reserved. 4 */ 5 6 #include <linux/bitmap.h> 7 #include <linux/clk.h> 8 #include <linux/delay.h> 9 #include <linux/host1x.h> 10 #include <linux/lcm.h> 11 #include <linux/list.h> 12 #include <linux/module.h> 13 #include <linux/of.h> 14 #include <linux/of_graph.h> 15 #include <linux/of_platform.h> 16 #include <linux/platform_device.h> 17 #include <linux/regulator/consumer.h> 18 #include <linux/pm_runtime.h> 19 #include <linux/slab.h> 20 21 #include <media/v4l2-dv-timings.h> 22 #include <media/v4l2-event.h> 23 #include <media/v4l2-fh.h> 24 #include <media/v4l2-fwnode.h> 25 #include <media/v4l2-ioctl.h> 26 #include <media/videobuf2-dma-contig.h> 27 28 #include <soc/tegra/pmc.h> 29 30 #include "vi.h" 31 #include "video.h" 32 33 #define MAX_CID_CONTROLS 3 34 35 /** 36 * struct tegra_vi_graph_entity - Entity in the video graph 37 * 38 * @asd: subdev asynchronous registration information 39 * @entity: media entity from the corresponding V4L2 subdev 40 * @subdev: V4L2 subdev 41 */ 42 struct tegra_vi_graph_entity { 43 struct v4l2_async_connection asd; 44 struct media_entity *entity; 45 struct v4l2_subdev *subdev; 46 }; 47 48 static inline struct tegra_vi * 49 host1x_client_to_vi(struct host1x_client *client) 50 { 51 return container_of(client, struct tegra_vi, client); 52 } 53 54 static inline struct tegra_channel_buffer * 55 to_tegra_channel_buffer(struct vb2_v4l2_buffer *vb) 56 { 57 return container_of(vb, struct tegra_channel_buffer, buf); 58 } 59 60 static inline struct tegra_vi_graph_entity * 61 to_tegra_vi_graph_entity(struct v4l2_async_connection *asd) 62 { 63 return container_of(asd, struct tegra_vi_graph_entity, asd); 64 } 65 66 static int tegra_get_format_idx_by_code(struct tegra_vi *vi, 67 unsigned int code, 68 unsigned int offset) 69 { 70 unsigned int i; 71 72 for (i = offset; i < vi->soc->nformats; ++i) { 73 if (vi->soc->video_formats[i].code == code) 74 return i; 75 } 76 77 return -1; 78 } 79 80 static u32 tegra_get_format_fourcc_by_idx(struct tegra_vi *vi, 81 unsigned int index) 82 { 83 if (index >= vi->soc->nformats) 84 return -EINVAL; 85 86 return vi->soc->video_formats[index].fourcc; 87 } 88 89 static const struct tegra_video_format * 90 tegra_get_format_by_fourcc(struct tegra_vi *vi, u32 fourcc) 91 { 92 unsigned int i; 93 94 for (i = 0; i < vi->soc->nformats; ++i) { 95 if (vi->soc->video_formats[i].fourcc == fourcc) 96 return &vi->soc->video_formats[i]; 97 } 98 99 return NULL; 100 } 101 102 /* 103 * videobuf2 queue operations 104 */ 105 106 static int tegra_channel_queue_setup(struct vb2_queue *vq, 107 unsigned int *nbuffers, 108 unsigned int *nplanes, 109 unsigned int sizes[], 110 struct device *alloc_devs[]) 111 { 112 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq); 113 114 if (*nplanes) 115 return sizes[0] < chan->format.sizeimage ? -EINVAL : 0; 116 117 *nplanes = 1; 118 sizes[0] = chan->format.sizeimage; 119 alloc_devs[0] = chan->vi->dev; 120 121 if (chan->vi->ops->channel_queue_setup) 122 chan->vi->ops->channel_queue_setup(chan); 123 124 return 0; 125 } 126 127 static int tegra_channel_buffer_prepare(struct vb2_buffer *vb) 128 { 129 struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue); 130 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 131 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf); 132 unsigned long size = chan->format.sizeimage; 133 134 if (vb2_plane_size(vb, 0) < size) { 135 v4l2_err(chan->video.v4l2_dev, 136 "buffer too small (%lu < %lu)\n", 137 vb2_plane_size(vb, 0), size); 138 return -EINVAL; 139 } 140 141 vb2_set_plane_payload(vb, 0, size); 142 buf->chan = chan; 143 buf->addr = vb2_dma_contig_plane_dma_addr(vb, 0); 144 145 return 0; 146 } 147 148 static void tegra_channel_buffer_queue(struct vb2_buffer *vb) 149 { 150 struct tegra_vi_channel *chan = vb2_get_drv_priv(vb->vb2_queue); 151 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 152 struct tegra_channel_buffer *buf = to_tegra_channel_buffer(vbuf); 153 154 /* put buffer into the capture queue */ 155 spin_lock(&chan->start_lock); 156 list_add_tail(&buf->queue, &chan->capture); 157 spin_unlock(&chan->start_lock); 158 159 /* wait up kthread for capture */ 160 wake_up_interruptible(&chan->start_wait); 161 } 162 163 static struct v4l2_subdev * 164 tegra_channel_get_remote_bridge_subdev(struct tegra_vi_channel *chan) 165 { 166 struct media_pad *pad; 167 168 pad = media_pad_remote_pad_first(&chan->pad); 169 if (!pad) 170 return NULL; 171 172 return media_entity_to_v4l2_subdev(pad->entity); 173 } 174 175 /* 176 * Walk up the chain until the initial source (e.g. image sensor) 177 */ 178 struct v4l2_subdev * 179 tegra_channel_get_remote_source_subdev(struct tegra_vi_channel *chan) 180 { 181 struct media_pad *pad; 182 struct v4l2_subdev *subdev; 183 struct media_entity *entity; 184 185 subdev = tegra_channel_get_remote_bridge_subdev(chan); 186 if (!subdev) 187 return NULL; 188 189 pad = &subdev->entity.pads[0]; 190 while (!(pad->flags & MEDIA_PAD_FL_SOURCE)) { 191 pad = media_pad_remote_pad_first(pad); 192 if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) 193 break; 194 entity = pad->entity; 195 pad = &entity->pads[0]; 196 subdev = media_entity_to_v4l2_subdev(entity); 197 } 198 199 return subdev; 200 } 201 202 static int tegra_channel_enable_stream(struct tegra_vi_channel *chan) 203 { 204 struct v4l2_subdev *subdev; 205 int ret; 206 207 subdev = tegra_channel_get_remote_bridge_subdev(chan); 208 ret = v4l2_subdev_call(subdev, video, s_stream, true); 209 if (ret < 0 && ret != -ENOIOCTLCMD) 210 return ret; 211 212 return 0; 213 } 214 215 static int tegra_channel_disable_stream(struct tegra_vi_channel *chan) 216 { 217 struct v4l2_subdev *subdev; 218 int ret; 219 220 subdev = tegra_channel_get_remote_bridge_subdev(chan); 221 ret = v4l2_subdev_call(subdev, video, s_stream, false); 222 if (ret < 0 && ret != -ENOIOCTLCMD) 223 return ret; 224 225 return 0; 226 } 227 228 int tegra_channel_set_stream(struct tegra_vi_channel *chan, bool on) 229 { 230 int ret; 231 232 if (on) 233 ret = tegra_channel_enable_stream(chan); 234 else 235 ret = tegra_channel_disable_stream(chan); 236 237 return ret; 238 } 239 240 void tegra_channel_release_buffers(struct tegra_vi_channel *chan, 241 enum vb2_buffer_state state) 242 { 243 struct tegra_channel_buffer *buf, *nbuf; 244 245 spin_lock(&chan->start_lock); 246 list_for_each_entry_safe(buf, nbuf, &chan->capture, queue) { 247 vb2_buffer_done(&buf->buf.vb2_buf, state); 248 list_del(&buf->queue); 249 } 250 spin_unlock(&chan->start_lock); 251 252 spin_lock(&chan->done_lock); 253 list_for_each_entry_safe(buf, nbuf, &chan->done, queue) { 254 vb2_buffer_done(&buf->buf.vb2_buf, state); 255 list_del(&buf->queue); 256 } 257 spin_unlock(&chan->done_lock); 258 } 259 260 static int tegra_channel_start_streaming(struct vb2_queue *vq, u32 count) 261 { 262 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq); 263 int ret; 264 265 ret = pm_runtime_resume_and_get(chan->vi->dev); 266 if (ret < 0) { 267 dev_err(chan->vi->dev, "failed to get runtime PM: %d\n", ret); 268 return ret; 269 } 270 271 ret = chan->vi->ops->vi_start_streaming(vq, count); 272 if (ret < 0) 273 pm_runtime_put(chan->vi->dev); 274 275 return ret; 276 } 277 278 static void tegra_channel_stop_streaming(struct vb2_queue *vq) 279 { 280 struct tegra_vi_channel *chan = vb2_get_drv_priv(vq); 281 282 chan->vi->ops->vi_stop_streaming(vq); 283 pm_runtime_put(chan->vi->dev); 284 } 285 286 static const struct vb2_ops tegra_channel_queue_qops = { 287 .queue_setup = tegra_channel_queue_setup, 288 .buf_prepare = tegra_channel_buffer_prepare, 289 .buf_queue = tegra_channel_buffer_queue, 290 .start_streaming = tegra_channel_start_streaming, 291 .stop_streaming = tegra_channel_stop_streaming, 292 }; 293 294 /* 295 * V4L2 ioctl operations 296 */ 297 static int tegra_channel_querycap(struct file *file, void *fh, 298 struct v4l2_capability *cap) 299 { 300 struct tegra_vi_channel *chan = video_drvdata(file); 301 302 strscpy(cap->driver, "tegra-video", sizeof(cap->driver)); 303 strscpy(cap->card, chan->video.name, sizeof(cap->card)); 304 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", 305 dev_name(chan->vi->dev)); 306 307 return 0; 308 } 309 310 static int tegra_channel_g_parm(struct file *file, void *fh, 311 struct v4l2_streamparm *a) 312 { 313 struct tegra_vi_channel *chan = video_drvdata(file); 314 struct v4l2_subdev *subdev; 315 316 subdev = tegra_channel_get_remote_source_subdev(chan); 317 return v4l2_g_parm_cap(&chan->video, subdev, a); 318 } 319 320 static int tegra_channel_s_parm(struct file *file, void *fh, 321 struct v4l2_streamparm *a) 322 { 323 struct tegra_vi_channel *chan = video_drvdata(file); 324 struct v4l2_subdev *subdev; 325 326 subdev = tegra_channel_get_remote_source_subdev(chan); 327 return v4l2_s_parm_cap(&chan->video, subdev, a); 328 } 329 330 static int tegra_channel_enum_framesizes(struct file *file, void *fh, 331 struct v4l2_frmsizeenum *sizes) 332 { 333 int ret; 334 struct tegra_vi_channel *chan = video_drvdata(file); 335 struct v4l2_subdev *subdev; 336 const struct tegra_video_format *fmtinfo; 337 struct v4l2_subdev_frame_size_enum fse = { 338 .index = sizes->index, 339 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 340 }; 341 342 fmtinfo = tegra_get_format_by_fourcc(chan->vi, sizes->pixel_format); 343 if (!fmtinfo) 344 return -EINVAL; 345 346 fse.code = fmtinfo->code; 347 348 subdev = tegra_channel_get_remote_source_subdev(chan); 349 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, NULL, &fse); 350 if (ret) 351 return ret; 352 353 sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE; 354 sizes->discrete.width = fse.max_width; 355 sizes->discrete.height = fse.max_height; 356 357 return 0; 358 } 359 360 static int tegra_channel_enum_frameintervals(struct file *file, void *fh, 361 struct v4l2_frmivalenum *ivals) 362 { 363 int ret; 364 struct tegra_vi_channel *chan = video_drvdata(file); 365 struct v4l2_subdev *subdev; 366 const struct tegra_video_format *fmtinfo; 367 struct v4l2_subdev_frame_interval_enum fie = { 368 .index = ivals->index, 369 .width = ivals->width, 370 .height = ivals->height, 371 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 372 }; 373 374 fmtinfo = tegra_get_format_by_fourcc(chan->vi, ivals->pixel_format); 375 if (!fmtinfo) 376 return -EINVAL; 377 378 fie.code = fmtinfo->code; 379 380 subdev = tegra_channel_get_remote_source_subdev(chan); 381 ret = v4l2_subdev_call(subdev, pad, enum_frame_interval, NULL, &fie); 382 if (ret) 383 return ret; 384 385 ivals->type = V4L2_FRMIVAL_TYPE_DISCRETE; 386 ivals->discrete.numerator = fie.interval.numerator; 387 ivals->discrete.denominator = fie.interval.denominator; 388 389 return 0; 390 } 391 392 static int tegra_channel_enum_format(struct file *file, void *fh, 393 struct v4l2_fmtdesc *f) 394 { 395 struct tegra_vi_channel *chan = video_drvdata(file); 396 unsigned int index = 0, i; 397 unsigned long *fmts_bitmap = chan->tpg_fmts_bitmap; 398 399 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) 400 fmts_bitmap = chan->fmts_bitmap; 401 402 if (f->index >= bitmap_weight(fmts_bitmap, MAX_FORMAT_NUM)) 403 return -EINVAL; 404 405 for (i = 0; i < f->index + 1; i++, index++) 406 index = find_next_bit(fmts_bitmap, MAX_FORMAT_NUM, index); 407 408 f->pixelformat = tegra_get_format_fourcc_by_idx(chan->vi, index - 1); 409 410 return 0; 411 } 412 413 static int tegra_channel_get_format(struct file *file, void *fh, 414 struct v4l2_format *format) 415 { 416 struct tegra_vi_channel *chan = video_drvdata(file); 417 418 format->fmt.pix = chan->format; 419 420 return 0; 421 } 422 423 static int __tegra_channel_try_format(struct tegra_vi_channel *chan, 424 struct v4l2_pix_format *pix) 425 { 426 const struct tegra_video_format *fmtinfo; 427 static struct lock_class_key key; 428 struct v4l2_subdev *subdev; 429 struct v4l2_subdev_format fmt = { 430 .which = V4L2_SUBDEV_FORMAT_TRY, 431 }; 432 struct v4l2_subdev_state *sd_state; 433 struct v4l2_subdev_frame_size_enum fse = { 434 .which = V4L2_SUBDEV_FORMAT_TRY, 435 }; 436 struct v4l2_subdev_selection sdsel = { 437 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 438 .target = V4L2_SEL_TGT_CROP_BOUNDS, 439 }; 440 struct v4l2_rect *try_crop; 441 int ret = 0; 442 443 subdev = tegra_channel_get_remote_source_subdev(chan); 444 if (!subdev) 445 return -ENODEV; 446 447 /* 448 * FIXME: Drop this call, drivers are not supposed to use 449 * __v4l2_subdev_state_alloc(). 450 */ 451 sd_state = __v4l2_subdev_state_alloc(subdev, "tegra:state->lock", 452 &key); 453 if (IS_ERR(sd_state)) 454 return PTR_ERR(sd_state); 455 /* 456 * Retrieve the format information and if requested format isn't 457 * supported, keep the current format. 458 */ 459 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat); 460 if (!fmtinfo) { 461 pix->pixelformat = chan->format.pixelformat; 462 pix->colorspace = chan->format.colorspace; 463 fmtinfo = tegra_get_format_by_fourcc(chan->vi, 464 pix->pixelformat); 465 } 466 467 pix->field = V4L2_FIELD_NONE; 468 fmt.pad = 0; 469 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code); 470 471 /* 472 * Attempt to obtain the format size from subdev. 473 * If not available, try to get crop boundary from subdev. 474 */ 475 try_crop = v4l2_subdev_state_get_crop(sd_state, 0); 476 fse.code = fmtinfo->code; 477 ret = v4l2_subdev_call(subdev, pad, enum_frame_size, sd_state, &fse); 478 if (ret) { 479 if (!v4l2_subdev_has_op(subdev, pad, get_selection) || 480 v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel)) { 481 try_crop->width = 0; 482 try_crop->height = 0; 483 } else { 484 try_crop->width = sdsel.r.width; 485 try_crop->height = sdsel.r.height; 486 } 487 } else { 488 try_crop->width = fse.max_width; 489 try_crop->height = fse.max_height; 490 } 491 492 ret = v4l2_subdev_call(subdev, pad, set_fmt, sd_state, &fmt); 493 if (ret < 0) 494 goto out_free; 495 496 v4l2_fill_pix_format(pix, &fmt.format); 497 chan->vi->ops->vi_fmt_align(pix, fmtinfo->bpp); 498 499 out_free: 500 __v4l2_subdev_state_free(sd_state); 501 502 return ret; 503 } 504 505 static int tegra_channel_try_format(struct file *file, void *fh, 506 struct v4l2_format *format) 507 { 508 struct tegra_vi_channel *chan = video_drvdata(file); 509 510 return __tegra_channel_try_format(chan, &format->fmt.pix); 511 } 512 513 static void tegra_channel_update_gangports(struct tegra_vi_channel *chan) 514 { 515 if (chan->format.width <= 1920) 516 chan->numgangports = 1; 517 else 518 chan->numgangports = chan->totalports; 519 } 520 521 static int tegra_channel_set_format(struct file *file, void *fh, 522 struct v4l2_format *format) 523 { 524 struct tegra_vi_channel *chan = video_drvdata(file); 525 const struct tegra_video_format *fmtinfo; 526 struct v4l2_subdev_format fmt = { 527 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 528 }; 529 struct v4l2_subdev *subdev; 530 struct v4l2_pix_format *pix = &format->fmt.pix; 531 int ret; 532 533 if (vb2_is_busy(&chan->queue)) 534 return -EBUSY; 535 536 /* get supported format by try_fmt */ 537 ret = __tegra_channel_try_format(chan, pix); 538 if (ret) 539 return ret; 540 541 fmtinfo = tegra_get_format_by_fourcc(chan->vi, pix->pixelformat); 542 543 fmt.pad = 0; 544 v4l2_fill_mbus_format(&fmt.format, pix, fmtinfo->code); 545 subdev = tegra_channel_get_remote_source_subdev(chan); 546 ret = v4l2_subdev_call(subdev, pad, set_fmt, NULL, &fmt); 547 if (ret < 0) 548 return ret; 549 550 v4l2_fill_pix_format(pix, &fmt.format); 551 chan->vi->ops->vi_fmt_align(pix, fmtinfo->bpp); 552 553 chan->format = *pix; 554 chan->fmtinfo = fmtinfo; 555 tegra_channel_update_gangports(chan); 556 557 return 0; 558 } 559 560 static int tegra_channel_set_subdev_active_fmt(struct tegra_vi_channel *chan) 561 { 562 int ret, index; 563 struct v4l2_subdev *subdev; 564 struct v4l2_subdev_format fmt = { 565 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 566 }; 567 568 /* 569 * Initialize channel format to the sub-device active format if there 570 * is corresponding match in the Tegra supported video formats. 571 */ 572 subdev = tegra_channel_get_remote_source_subdev(chan); 573 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); 574 if (ret) 575 return ret; 576 577 index = tegra_get_format_idx_by_code(chan->vi, fmt.format.code, 0); 578 if (index < 0) 579 return -EINVAL; 580 581 chan->fmtinfo = &chan->vi->soc->video_formats[index]; 582 v4l2_fill_pix_format(&chan->format, &fmt.format); 583 chan->format.pixelformat = chan->fmtinfo->fourcc; 584 chan->format.bytesperline = chan->format.width * chan->fmtinfo->bpp; 585 chan->format.sizeimage = chan->format.bytesperline * 586 chan->format.height; 587 chan->vi->ops->vi_fmt_align(&chan->format, chan->fmtinfo->bpp); 588 tegra_channel_update_gangports(chan); 589 590 return 0; 591 } 592 593 static int 594 tegra_channel_subscribe_event(struct v4l2_fh *fh, 595 const struct v4l2_event_subscription *sub) 596 { 597 switch (sub->type) { 598 case V4L2_EVENT_SOURCE_CHANGE: 599 return v4l2_event_subscribe(fh, sub, 4, NULL); 600 } 601 602 return v4l2_ctrl_subscribe_event(fh, sub); 603 } 604 605 static int tegra_channel_g_selection(struct file *file, void *priv, 606 struct v4l2_selection *sel) 607 { 608 struct tegra_vi_channel *chan = video_drvdata(file); 609 struct v4l2_subdev *subdev; 610 struct v4l2_subdev_format fmt = { 611 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 612 }; 613 struct v4l2_subdev_selection sdsel = { 614 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 615 .target = sel->target, 616 }; 617 int ret; 618 619 subdev = tegra_channel_get_remote_source_subdev(chan); 620 if (!v4l2_subdev_has_op(subdev, pad, get_selection)) 621 return -ENOTTY; 622 623 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 624 return -EINVAL; 625 /* 626 * Try the get selection operation and fallback to get format if not 627 * implemented. 628 */ 629 ret = v4l2_subdev_call(subdev, pad, get_selection, NULL, &sdsel); 630 if (!ret) 631 sel->r = sdsel.r; 632 if (ret != -ENOIOCTLCMD) 633 return ret; 634 635 ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt); 636 if (ret < 0) 637 return ret; 638 639 sel->r.left = 0; 640 sel->r.top = 0; 641 sel->r.width = fmt.format.width; 642 sel->r.height = fmt.format.height; 643 644 return 0; 645 } 646 647 static int tegra_channel_s_selection(struct file *file, void *fh, 648 struct v4l2_selection *sel) 649 { 650 struct tegra_vi_channel *chan = video_drvdata(file); 651 struct v4l2_subdev *subdev; 652 int ret; 653 struct v4l2_subdev_selection sdsel = { 654 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 655 .target = sel->target, 656 .flags = sel->flags, 657 .r = sel->r, 658 }; 659 660 subdev = tegra_channel_get_remote_source_subdev(chan); 661 if (!v4l2_subdev_has_op(subdev, pad, set_selection)) 662 return -ENOTTY; 663 664 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 665 return -EINVAL; 666 667 if (vb2_is_busy(&chan->queue)) 668 return -EBUSY; 669 670 ret = v4l2_subdev_call(subdev, pad, set_selection, NULL, &sdsel); 671 if (!ret) { 672 sel->r = sdsel.r; 673 /* 674 * Subdev active format resolution may have changed during 675 * set selection operation. So, update channel format to 676 * the sub-device active format. 677 */ 678 return tegra_channel_set_subdev_active_fmt(chan); 679 } 680 681 return ret; 682 } 683 684 static int tegra_channel_g_edid(struct file *file, void *fh, 685 struct v4l2_edid *edid) 686 { 687 struct tegra_vi_channel *chan = video_drvdata(file); 688 struct v4l2_subdev *subdev; 689 690 subdev = tegra_channel_get_remote_source_subdev(chan); 691 if (!v4l2_subdev_has_op(subdev, pad, get_edid)) 692 return -ENOTTY; 693 694 return v4l2_subdev_call(subdev, pad, get_edid, edid); 695 } 696 697 static int tegra_channel_s_edid(struct file *file, void *fh, 698 struct v4l2_edid *edid) 699 { 700 struct tegra_vi_channel *chan = video_drvdata(file); 701 struct v4l2_subdev *subdev; 702 703 subdev = tegra_channel_get_remote_source_subdev(chan); 704 if (!v4l2_subdev_has_op(subdev, pad, set_edid)) 705 return -ENOTTY; 706 707 return v4l2_subdev_call(subdev, pad, set_edid, edid); 708 } 709 710 static int tegra_channel_g_dv_timings(struct file *file, void *fh, 711 struct v4l2_dv_timings *timings) 712 { 713 struct tegra_vi_channel *chan = video_drvdata(file); 714 struct v4l2_subdev *subdev; 715 716 subdev = tegra_channel_get_remote_source_subdev(chan); 717 if (!v4l2_subdev_has_op(subdev, pad, g_dv_timings)) 718 return -ENOTTY; 719 720 return v4l2_device_call_until_err(chan->video.v4l2_dev, 0, 721 pad, g_dv_timings, 0, timings); 722 } 723 724 static int tegra_channel_s_dv_timings(struct file *file, void *fh, 725 struct v4l2_dv_timings *timings) 726 { 727 struct tegra_vi_channel *chan = video_drvdata(file); 728 struct v4l2_subdev *subdev; 729 struct v4l2_bt_timings *bt = &timings->bt; 730 struct v4l2_dv_timings curr_timings; 731 int ret; 732 733 subdev = tegra_channel_get_remote_source_subdev(chan); 734 if (!v4l2_subdev_has_op(subdev, pad, s_dv_timings)) 735 return -ENOTTY; 736 737 ret = tegra_channel_g_dv_timings(file, fh, &curr_timings); 738 if (ret) 739 return ret; 740 741 if (v4l2_match_dv_timings(timings, &curr_timings, 0, false)) 742 return 0; 743 744 if (vb2_is_busy(&chan->queue)) 745 return -EBUSY; 746 747 ret = v4l2_device_call_until_err(chan->video.v4l2_dev, 0, 748 pad, s_dv_timings, 0, timings); 749 if (ret) 750 return ret; 751 752 chan->format.width = bt->width; 753 chan->format.height = bt->height; 754 chan->format.bytesperline = bt->width * chan->fmtinfo->bpp; 755 chan->format.sizeimage = chan->format.bytesperline * bt->height; 756 chan->vi->ops->vi_fmt_align(&chan->format, chan->fmtinfo->bpp); 757 tegra_channel_update_gangports(chan); 758 759 return 0; 760 } 761 762 static int tegra_channel_query_dv_timings(struct file *file, void *fh, 763 struct v4l2_dv_timings *timings) 764 { 765 struct tegra_vi_channel *chan = video_drvdata(file); 766 struct v4l2_subdev *subdev; 767 768 subdev = tegra_channel_get_remote_source_subdev(chan); 769 if (!v4l2_subdev_has_op(subdev, pad, query_dv_timings)) 770 return -ENOTTY; 771 772 return v4l2_device_call_until_err(chan->video.v4l2_dev, 0, 773 pad, query_dv_timings, 0, timings); 774 } 775 776 static int tegra_channel_enum_dv_timings(struct file *file, void *fh, 777 struct v4l2_enum_dv_timings *timings) 778 { 779 struct tegra_vi_channel *chan = video_drvdata(file); 780 struct v4l2_subdev *subdev; 781 782 subdev = tegra_channel_get_remote_source_subdev(chan); 783 if (!v4l2_subdev_has_op(subdev, pad, enum_dv_timings)) 784 return -ENOTTY; 785 786 return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings); 787 } 788 789 static int tegra_channel_dv_timings_cap(struct file *file, void *fh, 790 struct v4l2_dv_timings_cap *cap) 791 { 792 struct tegra_vi_channel *chan = video_drvdata(file); 793 struct v4l2_subdev *subdev; 794 795 subdev = tegra_channel_get_remote_source_subdev(chan); 796 if (!v4l2_subdev_has_op(subdev, pad, dv_timings_cap)) 797 return -ENOTTY; 798 799 return v4l2_subdev_call(subdev, pad, dv_timings_cap, cap); 800 } 801 802 static int tegra_channel_log_status(struct file *file, void *fh) 803 { 804 struct tegra_vi_channel *chan = video_drvdata(file); 805 806 v4l2_device_call_all(chan->video.v4l2_dev, 0, core, log_status); 807 808 return 0; 809 } 810 811 static int tegra_channel_enum_input(struct file *file, void *fh, 812 struct v4l2_input *inp) 813 { 814 struct tegra_vi_channel *chan = video_drvdata(file); 815 struct v4l2_subdev *subdev; 816 817 if (inp->index) 818 return -EINVAL; 819 820 inp->type = V4L2_INPUT_TYPE_CAMERA; 821 subdev = tegra_channel_get_remote_source_subdev(chan); 822 strscpy(inp->name, subdev->name, sizeof(inp->name)); 823 if (v4l2_subdev_has_op(subdev, pad, dv_timings_cap)) 824 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS; 825 826 return 0; 827 } 828 829 static int tegra_channel_g_input(struct file *file, void *priv, 830 unsigned int *i) 831 { 832 *i = 0; 833 834 return 0; 835 } 836 837 static int tegra_channel_s_input(struct file *file, void *priv, 838 unsigned int input) 839 { 840 if (input > 0) 841 return -EINVAL; 842 843 return 0; 844 } 845 846 static const struct v4l2_ioctl_ops tegra_channel_ioctl_ops = { 847 .vidioc_querycap = tegra_channel_querycap, 848 .vidioc_g_parm = tegra_channel_g_parm, 849 .vidioc_s_parm = tegra_channel_s_parm, 850 .vidioc_enum_framesizes = tegra_channel_enum_framesizes, 851 .vidioc_enum_frameintervals = tegra_channel_enum_frameintervals, 852 .vidioc_enum_fmt_vid_cap = tegra_channel_enum_format, 853 .vidioc_g_fmt_vid_cap = tegra_channel_get_format, 854 .vidioc_s_fmt_vid_cap = tegra_channel_set_format, 855 .vidioc_try_fmt_vid_cap = tegra_channel_try_format, 856 .vidioc_enum_input = tegra_channel_enum_input, 857 .vidioc_g_input = tegra_channel_g_input, 858 .vidioc_s_input = tegra_channel_s_input, 859 .vidioc_reqbufs = vb2_ioctl_reqbufs, 860 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 861 .vidioc_querybuf = vb2_ioctl_querybuf, 862 .vidioc_qbuf = vb2_ioctl_qbuf, 863 .vidioc_dqbuf = vb2_ioctl_dqbuf, 864 .vidioc_create_bufs = vb2_ioctl_create_bufs, 865 .vidioc_expbuf = vb2_ioctl_expbuf, 866 .vidioc_streamon = vb2_ioctl_streamon, 867 .vidioc_streamoff = vb2_ioctl_streamoff, 868 .vidioc_subscribe_event = tegra_channel_subscribe_event, 869 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 870 .vidioc_g_selection = tegra_channel_g_selection, 871 .vidioc_s_selection = tegra_channel_s_selection, 872 .vidioc_g_edid = tegra_channel_g_edid, 873 .vidioc_s_edid = tegra_channel_s_edid, 874 .vidioc_g_dv_timings = tegra_channel_g_dv_timings, 875 .vidioc_s_dv_timings = tegra_channel_s_dv_timings, 876 .vidioc_query_dv_timings = tegra_channel_query_dv_timings, 877 .vidioc_enum_dv_timings = tegra_channel_enum_dv_timings, 878 .vidioc_dv_timings_cap = tegra_channel_dv_timings_cap, 879 .vidioc_log_status = tegra_channel_log_status, 880 }; 881 882 /* 883 * V4L2 file operations 884 */ 885 static const struct v4l2_file_operations tegra_channel_fops = { 886 .owner = THIS_MODULE, 887 .unlocked_ioctl = video_ioctl2, 888 .open = v4l2_fh_open, 889 .release = vb2_fop_release, 890 .read = vb2_fop_read, 891 .poll = vb2_fop_poll, 892 .mmap = vb2_fop_mmap, 893 }; 894 895 /* 896 * V4L2 control operations 897 */ 898 static int vi_s_ctrl(struct v4l2_ctrl *ctrl) 899 { 900 struct tegra_vi_channel *chan = container_of(ctrl->handler, 901 struct tegra_vi_channel, 902 ctrl_handler); 903 904 switch (ctrl->id) { 905 case V4L2_CID_TEST_PATTERN: 906 /* pattern change takes effect on next stream */ 907 chan->pg_mode = ctrl->val + 1; 908 break; 909 case V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY: 910 chan->syncpt_timeout_retry = ctrl->val; 911 break; 912 case V4L2_CID_HFLIP: 913 chan->hflip = ctrl->val; 914 break; 915 case V4L2_CID_VFLIP: 916 chan->vflip = ctrl->val; 917 break; 918 default: 919 return -EINVAL; 920 } 921 922 return 0; 923 } 924 925 static const struct v4l2_ctrl_ops vi_ctrl_ops = { 926 .s_ctrl = vi_s_ctrl, 927 }; 928 929 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG) 930 static const char *const vi_pattern_strings[] = { 931 "Black/White Direct Mode", 932 "Color Patch Mode", 933 }; 934 #else 935 static const struct v4l2_ctrl_config syncpt_timeout_ctrl = { 936 .ops = &vi_ctrl_ops, 937 .id = V4L2_CID_TEGRA_SYNCPT_TIMEOUT_RETRY, 938 .name = "Syncpt timeout retry", 939 .type = V4L2_CTRL_TYPE_INTEGER, 940 .min = 1, 941 .max = 10000, 942 .step = 1, 943 .def = 5, 944 }; 945 #endif 946 947 static int tegra_channel_setup_ctrl_handler(struct tegra_vi_channel *chan) 948 { 949 int ret; 950 951 #if IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG) 952 /* add test pattern control handler to v4l2 device */ 953 v4l2_ctrl_new_std_menu_items(&chan->ctrl_handler, &vi_ctrl_ops, 954 V4L2_CID_TEST_PATTERN, 955 ARRAY_SIZE(vi_pattern_strings) - 1, 956 0, 0, vi_pattern_strings); 957 if (chan->ctrl_handler.error) { 958 dev_err(chan->vi->dev, "failed to add TPG ctrl handler: %d\n", 959 chan->ctrl_handler.error); 960 v4l2_ctrl_handler_free(&chan->ctrl_handler); 961 return chan->ctrl_handler.error; 962 } 963 #else 964 struct v4l2_subdev *subdev; 965 struct v4l2_ctrl *hflip, *vflip; 966 967 /* custom control */ 968 v4l2_ctrl_new_custom(&chan->ctrl_handler, &syncpt_timeout_ctrl, NULL); 969 if (chan->ctrl_handler.error) { 970 dev_err(chan->vi->dev, "failed to add %s ctrl handler: %d\n", 971 syncpt_timeout_ctrl.name, 972 chan->ctrl_handler.error); 973 v4l2_ctrl_handler_free(&chan->ctrl_handler); 974 return chan->ctrl_handler.error; 975 } 976 977 subdev = tegra_channel_get_remote_source_subdev(chan); 978 if (!subdev) 979 return -ENODEV; 980 981 ret = v4l2_ctrl_add_handler(&chan->ctrl_handler, subdev->ctrl_handler, 982 NULL, true); 983 if (ret < 0) { 984 dev_err(chan->vi->dev, 985 "failed to add subdev %s ctrl handler: %d\n", 986 subdev->name, ret); 987 v4l2_ctrl_handler_free(&chan->ctrl_handler); 988 return ret; 989 } 990 991 hflip = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_HFLIP); 992 if (chan->vi->soc->has_h_v_flip && !hflip) 993 v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0); 994 995 vflip = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_VFLIP); 996 if (chan->vi->soc->has_h_v_flip && !vflip) 997 v4l2_ctrl_new_std(&chan->ctrl_handler, &vi_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0); 998 #endif 999 1000 /* setup the controls */ 1001 ret = v4l2_ctrl_handler_setup(&chan->ctrl_handler); 1002 if (ret < 0) { 1003 dev_err(chan->vi->dev, 1004 "failed to setup v4l2 ctrl handler: %d\n", ret); 1005 return ret; 1006 } 1007 1008 return 0; 1009 } 1010 1011 /* VI only support 2 formats in TPG mode */ 1012 static void vi_tpg_fmts_bitmap_init(struct tegra_vi_channel *chan) 1013 { 1014 int index; 1015 1016 bitmap_zero(chan->tpg_fmts_bitmap, MAX_FORMAT_NUM); 1017 1018 index = tegra_get_format_idx_by_code(chan->vi, 1019 MEDIA_BUS_FMT_SRGGB10_1X10, 0); 1020 bitmap_set(chan->tpg_fmts_bitmap, index, 1); 1021 1022 index = tegra_get_format_idx_by_code(chan->vi, 1023 MEDIA_BUS_FMT_RGB888_1X32_PADHI, 1024 0); 1025 bitmap_set(chan->tpg_fmts_bitmap, index, 1); 1026 } 1027 1028 static int vi_fmts_bitmap_init(struct tegra_vi_channel *chan) 1029 { 1030 int index, ret, match_code = 0; 1031 struct v4l2_subdev *subdev; 1032 struct v4l2_subdev_mbus_code_enum code = { 1033 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 1034 }; 1035 1036 bitmap_zero(chan->fmts_bitmap, MAX_FORMAT_NUM); 1037 1038 /* 1039 * Set the bitmap bits based on all the matched formats between the 1040 * available media bus formats of sub-device and the pre-defined Tegra 1041 * supported video formats. 1042 */ 1043 subdev = tegra_channel_get_remote_source_subdev(chan); 1044 while (1) { 1045 ret = v4l2_subdev_call(subdev, pad, enum_mbus_code, 1046 NULL, &code); 1047 if (ret < 0) 1048 break; 1049 1050 index = tegra_get_format_idx_by_code(chan->vi, code.code, 0); 1051 while (index >= 0) { 1052 bitmap_set(chan->fmts_bitmap, index, 1); 1053 if (!match_code) 1054 match_code = code.code; 1055 /* look for other formats with same mbus code */ 1056 index = tegra_get_format_idx_by_code(chan->vi, 1057 code.code, 1058 index + 1); 1059 } 1060 1061 code.index++; 1062 } 1063 1064 /* 1065 * Set the bitmap bit corresponding to default tegra video format if 1066 * there are no matched formats. 1067 */ 1068 if (!match_code) { 1069 match_code = chan->vi->soc->default_video_format->code; 1070 index = tegra_get_format_idx_by_code(chan->vi, match_code, 0); 1071 if (WARN_ON(index < 0)) 1072 return -EINVAL; 1073 1074 bitmap_set(chan->fmts_bitmap, index, 1); 1075 } 1076 1077 /* initialize channel format to the sub-device active format */ 1078 tegra_channel_set_subdev_active_fmt(chan); 1079 1080 return 0; 1081 } 1082 1083 static void tegra_channel_cleanup(struct tegra_vi_channel *chan) 1084 { 1085 v4l2_ctrl_handler_free(&chan->ctrl_handler); 1086 media_entity_cleanup(&chan->video.entity); 1087 chan->vi->ops->channel_host1x_syncpt_free(chan); 1088 mutex_destroy(&chan->video_lock); 1089 } 1090 1091 void tegra_channels_cleanup(struct tegra_vi *vi) 1092 { 1093 struct tegra_vi_channel *chan, *tmp; 1094 1095 if (!vi) 1096 return; 1097 1098 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) { 1099 tegra_channel_cleanup(chan); 1100 list_del(&chan->list); 1101 kfree(chan); 1102 } 1103 } 1104 1105 static int tegra_channel_init(struct tegra_vi_channel *chan) 1106 { 1107 struct tegra_vi *vi = chan->vi; 1108 struct tegra_video_device *vid = dev_get_drvdata(vi->client.host); 1109 int ret; 1110 1111 mutex_init(&chan->video_lock); 1112 INIT_LIST_HEAD(&chan->capture); 1113 INIT_LIST_HEAD(&chan->done); 1114 spin_lock_init(&chan->start_lock); 1115 spin_lock_init(&chan->done_lock); 1116 init_waitqueue_head(&chan->start_wait); 1117 init_waitqueue_head(&chan->done_wait); 1118 1119 /* initialize the video format */ 1120 chan->fmtinfo = chan->vi->soc->default_video_format; 1121 chan->format.pixelformat = chan->fmtinfo->fourcc; 1122 chan->format.colorspace = V4L2_COLORSPACE_SRGB; 1123 chan->format.field = V4L2_FIELD_NONE; 1124 chan->format.width = TEGRA_DEF_WIDTH; 1125 chan->format.height = TEGRA_DEF_HEIGHT; 1126 chan->format.bytesperline = TEGRA_DEF_WIDTH * chan->fmtinfo->bpp; 1127 chan->format.sizeimage = chan->format.bytesperline * TEGRA_DEF_HEIGHT; 1128 vi->ops->vi_fmt_align(&chan->format, chan->fmtinfo->bpp); 1129 1130 ret = vi->ops->channel_host1x_syncpt_init(chan); 1131 if (ret) 1132 return ret; 1133 1134 /* initialize the media entity */ 1135 chan->pad.flags = MEDIA_PAD_FL_SINK; 1136 ret = media_entity_pads_init(&chan->video.entity, 1, &chan->pad); 1137 if (ret < 0) { 1138 dev_err(vi->dev, 1139 "failed to initialize media entity: %d\n", ret); 1140 goto free_syncpts; 1141 } 1142 1143 ret = v4l2_ctrl_handler_init(&chan->ctrl_handler, MAX_CID_CONTROLS); 1144 if (chan->ctrl_handler.error) { 1145 dev_err(vi->dev, 1146 "failed to initialize v4l2 ctrl handler: %d\n", ret); 1147 goto cleanup_media; 1148 } 1149 1150 /* initialize the video_device */ 1151 chan->video.fops = &tegra_channel_fops; 1152 chan->video.v4l2_dev = &vid->v4l2_dev; 1153 chan->video.release = video_device_release_empty; 1154 chan->video.queue = &chan->queue; 1155 snprintf(chan->video.name, sizeof(chan->video.name), "%s-%s-%u", 1156 dev_name(vi->dev), "output", chan->portnos[0]); 1157 chan->video.vfl_type = VFL_TYPE_VIDEO; 1158 chan->video.vfl_dir = VFL_DIR_RX; 1159 chan->video.ioctl_ops = &tegra_channel_ioctl_ops; 1160 chan->video.ctrl_handler = &chan->ctrl_handler; 1161 chan->video.lock = &chan->video_lock; 1162 chan->video.device_caps = V4L2_CAP_VIDEO_CAPTURE | 1163 V4L2_CAP_STREAMING | 1164 V4L2_CAP_READWRITE; 1165 video_set_drvdata(&chan->video, chan); 1166 1167 chan->queue.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1168 chan->queue.io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ; 1169 chan->queue.lock = &chan->video_lock; 1170 chan->queue.drv_priv = chan; 1171 chan->queue.buf_struct_size = sizeof(struct tegra_channel_buffer); 1172 chan->queue.ops = &tegra_channel_queue_qops; 1173 chan->queue.mem_ops = &vb2_dma_contig_memops; 1174 chan->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1175 chan->queue.min_queued_buffers = 2; 1176 chan->queue.dev = vi->dev; 1177 ret = vb2_queue_init(&chan->queue); 1178 if (ret < 0) { 1179 dev_err(vi->dev, "failed to initialize vb2 queue: %d\n", ret); 1180 goto free_v4l2_ctrl_hdl; 1181 } 1182 1183 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) 1184 v4l2_async_nf_init(&chan->notifier, &vid->v4l2_dev); 1185 1186 return 0; 1187 1188 free_v4l2_ctrl_hdl: 1189 v4l2_ctrl_handler_free(&chan->ctrl_handler); 1190 cleanup_media: 1191 media_entity_cleanup(&chan->video.entity); 1192 free_syncpts: 1193 vi->ops->channel_host1x_syncpt_free(chan); 1194 return ret; 1195 } 1196 1197 static int tegra_vi_channel_alloc(struct tegra_vi *vi, unsigned int port_num, 1198 struct device_node *node, unsigned int lanes) 1199 { 1200 struct tegra_vi_channel *chan; 1201 unsigned int i; 1202 1203 /* 1204 * Do not use devm_kzalloc as memory is freed immediately 1205 * when device instance is unbound but application might still 1206 * be holding the device node open. Channel memory allocated 1207 * with kzalloc is freed during video device release callback. 1208 */ 1209 chan = kzalloc_obj(*chan); 1210 if (!chan) 1211 return -ENOMEM; 1212 1213 chan->vi = vi; 1214 chan->portnos[0] = port_num; 1215 /* 1216 * For data lanes more than maximum csi lanes per brick, multiple of 1217 * x4 ports are used simultaneously for capture. 1218 */ 1219 if (lanes <= CSI_LANES_PER_BRICK) 1220 chan->totalports = 1; 1221 else 1222 chan->totalports = lanes / CSI_LANES_PER_BRICK; 1223 chan->numgangports = chan->totalports; 1224 1225 for (i = 1; i < chan->totalports; i++) 1226 chan->portnos[i] = chan->portnos[0] + i * CSI_PORTS_PER_BRICK; 1227 1228 chan->of_node = node; 1229 list_add_tail(&chan->list, &vi->vi_chans); 1230 1231 return 0; 1232 } 1233 1234 static int tegra_vi_tpg_channels_alloc(struct tegra_vi *vi) 1235 { 1236 unsigned int port_num; 1237 unsigned int nchannels = vi->soc->vi_max_channels; 1238 int ret; 1239 1240 for (port_num = 0; port_num < nchannels; port_num++) { 1241 ret = tegra_vi_channel_alloc(vi, port_num, 1242 vi->dev->of_node, 2); 1243 if (ret < 0) 1244 return ret; 1245 } 1246 1247 return 0; 1248 } 1249 1250 static int tegra_vi_channels_alloc(struct tegra_vi *vi) 1251 { 1252 struct device_node *node = vi->dev->of_node; 1253 struct device_node *ep = NULL; 1254 struct device_node *ports; 1255 struct device_node *port = NULL; 1256 unsigned int port_num; 1257 struct device_node *parent; 1258 struct v4l2_fwnode_endpoint v4l2_ep = { .bus_type = 0 }; 1259 unsigned int lanes; 1260 int ret = 0; 1261 1262 ports = of_get_child_by_name(node, "ports"); 1263 if (!ports) 1264 return dev_err_probe(vi->dev, -ENODEV, "%pOF: missing 'ports' node\n", node); 1265 1266 for_each_child_of_node(ports, port) { 1267 if (!of_node_name_eq(port, "port")) 1268 continue; 1269 1270 ret = of_property_read_u32(port, "reg", &port_num); 1271 if (ret < 0) 1272 continue; 1273 1274 if (port_num > vi->soc->vi_max_channels) { 1275 dev_err(vi->dev, "invalid port num %d for %pOF\n", 1276 port_num, port); 1277 ret = -EINVAL; 1278 goto cleanup; 1279 } 1280 1281 ep = of_get_child_by_name(port, "endpoint"); 1282 if (!ep) 1283 continue; 1284 1285 parent = of_graph_get_remote_port_parent(ep); 1286 of_node_put(ep); 1287 if (!parent) 1288 continue; 1289 1290 ep = of_graph_get_endpoint_by_regs(parent, 0, 0); 1291 of_node_put(parent); 1292 ret = v4l2_fwnode_endpoint_parse(of_fwnode_handle(ep), 1293 &v4l2_ep); 1294 of_node_put(ep); 1295 if (ret) 1296 continue; 1297 1298 lanes = v4l2_ep.bus.mipi_csi2.num_data_lanes; 1299 ret = tegra_vi_channel_alloc(vi, port_num, port, lanes); 1300 if (ret < 0) 1301 goto cleanup; 1302 } 1303 1304 cleanup: 1305 of_node_put(port); 1306 of_node_put(ports); 1307 return ret; 1308 } 1309 1310 static int tegra_vi_channels_init(struct tegra_vi *vi) 1311 { 1312 struct tegra_vi_channel *chan; 1313 int ret; 1314 1315 list_for_each_entry(chan, &vi->vi_chans, list) { 1316 ret = tegra_channel_init(chan); 1317 if (ret < 0) { 1318 dev_err(vi->dev, 1319 "failed to initialize channel-%d: %d\n", 1320 chan->portnos[0], ret); 1321 goto cleanup; 1322 } 1323 } 1324 1325 return 0; 1326 1327 cleanup: 1328 list_for_each_entry_continue_reverse(chan, &vi->vi_chans, list) 1329 tegra_channel_cleanup(chan); 1330 1331 return ret; 1332 } 1333 1334 void tegra_v4l2_nodes_cleanup_tpg(struct tegra_video_device *vid) 1335 { 1336 struct tegra_vi *vi = vid->vi; 1337 struct tegra_csi *csi = vid->csi; 1338 struct tegra_csi_channel *csi_chan; 1339 struct tegra_vi_channel *chan; 1340 1341 list_for_each_entry(chan, &vi->vi_chans, list) 1342 vb2_video_unregister_device(&chan->video); 1343 1344 list_for_each_entry(csi_chan, &csi->csi_chans, list) 1345 v4l2_device_unregister_subdev(&csi_chan->subdev); 1346 } 1347 1348 int tegra_v4l2_nodes_setup_tpg(struct tegra_video_device *vid) 1349 { 1350 struct tegra_vi *vi = vid->vi; 1351 struct tegra_csi *csi = vid->csi; 1352 struct tegra_vi_channel *vi_chan; 1353 struct tegra_csi_channel *csi_chan; 1354 u32 link_flags = MEDIA_LNK_FL_ENABLED; 1355 int ret; 1356 1357 if (!vi || !csi) 1358 return -ENODEV; 1359 1360 csi_chan = list_first_entry(&csi->csi_chans, 1361 struct tegra_csi_channel, list); 1362 1363 list_for_each_entry(vi_chan, &vi->vi_chans, list) { 1364 struct media_entity *source = &csi_chan->subdev.entity; 1365 struct media_entity *sink = &vi_chan->video.entity; 1366 struct media_pad *source_pad = csi_chan->pads; 1367 struct media_pad *sink_pad = &vi_chan->pad; 1368 1369 ret = v4l2_device_register_subdev(&vid->v4l2_dev, 1370 &csi_chan->subdev); 1371 if (ret) { 1372 dev_err(vi->dev, 1373 "failed to register subdev: %d\n", ret); 1374 goto cleanup; 1375 } 1376 1377 ret = video_register_device(&vi_chan->video, 1378 VFL_TYPE_VIDEO, -1); 1379 if (ret < 0) { 1380 dev_err(vi->dev, 1381 "failed to register video device: %d\n", ret); 1382 goto cleanup; 1383 } 1384 1385 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n", 1386 source->name, source_pad->index, 1387 sink->name, sink_pad->index); 1388 1389 ret = media_create_pad_link(source, source_pad->index, 1390 sink, sink_pad->index, 1391 link_flags); 1392 if (ret < 0) { 1393 dev_err(vi->dev, 1394 "failed to create %s:%u -> %s:%u link: %d\n", 1395 source->name, source_pad->index, 1396 sink->name, sink_pad->index, ret); 1397 goto cleanup; 1398 } 1399 1400 ret = tegra_channel_setup_ctrl_handler(vi_chan); 1401 if (ret < 0) 1402 goto cleanup; 1403 1404 v4l2_set_subdev_hostdata(&csi_chan->subdev, vi_chan); 1405 vi_tpg_fmts_bitmap_init(vi_chan); 1406 csi_chan = list_next_entry(csi_chan, list); 1407 } 1408 1409 return 0; 1410 1411 cleanup: 1412 tegra_v4l2_nodes_cleanup_tpg(vid); 1413 return ret; 1414 } 1415 1416 static int __maybe_unused vi_runtime_resume(struct device *dev) 1417 { 1418 struct tegra_vi *vi = dev_get_drvdata(dev); 1419 int ret; 1420 1421 ret = clk_set_rate(vi->clk, vi->soc->vi_max_clk_hz); 1422 if (ret) { 1423 dev_err(dev, "failed to set vi clock rate: %d\n", ret); 1424 return ret; 1425 } 1426 1427 ret = clk_prepare_enable(vi->clk); 1428 if (ret) { 1429 dev_err(dev, "failed to enable vi clock: %d\n", ret); 1430 return ret; 1431 } 1432 1433 return 0; 1434 } 1435 1436 static int __maybe_unused vi_runtime_suspend(struct device *dev) 1437 { 1438 struct tegra_vi *vi = dev_get_drvdata(dev); 1439 1440 clk_disable_unprepare(vi->clk); 1441 1442 return 0; 1443 } 1444 1445 /* 1446 * Find the entity matching a given fwnode in an v4l2_async_notifier list 1447 */ 1448 static struct tegra_vi_graph_entity * 1449 tegra_vi_graph_find_entity(struct list_head *list, 1450 const struct fwnode_handle *fwnode) 1451 { 1452 struct tegra_vi_graph_entity *entity; 1453 struct v4l2_async_connection *asd; 1454 1455 list_for_each_entry(asd, list, asc_entry) { 1456 entity = to_tegra_vi_graph_entity(asd); 1457 1458 if (entity->asd.match.fwnode == fwnode) 1459 return entity; 1460 } 1461 1462 return NULL; 1463 } 1464 1465 static int tegra_vi_graph_build(struct tegra_vi_channel *chan, 1466 struct tegra_vi_graph_entity *entity) 1467 { 1468 struct tegra_vi *vi = chan->vi; 1469 struct tegra_vi_graph_entity *ent; 1470 struct fwnode_handle *ep = NULL; 1471 struct v4l2_fwnode_link link; 1472 struct media_entity *local = entity->entity; 1473 struct media_entity *remote; 1474 struct media_pad *local_pad; 1475 struct media_pad *remote_pad; 1476 u32 link_flags = MEDIA_LNK_FL_ENABLED; 1477 int ret = 0; 1478 1479 dev_dbg(vi->dev, "creating links for entity %s\n", local->name); 1480 1481 while (1) { 1482 ep = fwnode_graph_get_next_endpoint(entity->asd.match.fwnode, 1483 ep); 1484 if (!ep) 1485 break; 1486 1487 ret = v4l2_fwnode_parse_link(ep, &link); 1488 if (ret < 0) { 1489 dev_err(vi->dev, "failed to parse link for %pOF: %d\n", 1490 to_of_node(ep), ret); 1491 continue; 1492 } 1493 1494 if (link.local_port >= local->num_pads) { 1495 dev_err(vi->dev, "invalid port number %u on %pOF\n", 1496 link.local_port, to_of_node(link.local_node)); 1497 v4l2_fwnode_put_link(&link); 1498 ret = -EINVAL; 1499 break; 1500 } 1501 1502 local_pad = &local->pads[link.local_port]; 1503 /* Remote node is vi node. So use channel video entity and pad 1504 * as remote/sink. 1505 */ 1506 if (link.remote_node == of_fwnode_handle(vi->dev->of_node)) { 1507 remote = &chan->video.entity; 1508 remote_pad = &chan->pad; 1509 goto create_link; 1510 } 1511 1512 /* 1513 * Skip sink ports, they will be processed from the other end 1514 * of the link. 1515 */ 1516 if (local_pad->flags & MEDIA_PAD_FL_SINK) { 1517 dev_dbg(vi->dev, "skipping sink port %pOF:%u\n", 1518 to_of_node(link.local_node), link.local_port); 1519 v4l2_fwnode_put_link(&link); 1520 continue; 1521 } 1522 1523 /* find the remote entity from notifier list */ 1524 ent = tegra_vi_graph_find_entity(&chan->notifier.done_list, 1525 link.remote_node); 1526 if (!ent) { 1527 dev_err(vi->dev, "no entity found for %pOF\n", 1528 to_of_node(link.remote_node)); 1529 v4l2_fwnode_put_link(&link); 1530 ret = -ENODEV; 1531 break; 1532 } 1533 1534 remote = ent->entity; 1535 if (link.remote_port >= remote->num_pads) { 1536 dev_err(vi->dev, "invalid port number %u on %pOF\n", 1537 link.remote_port, 1538 to_of_node(link.remote_node)); 1539 v4l2_fwnode_put_link(&link); 1540 ret = -EINVAL; 1541 break; 1542 } 1543 1544 remote_pad = &remote->pads[link.remote_port]; 1545 1546 create_link: 1547 dev_dbg(vi->dev, "creating %s:%u -> %s:%u link\n", 1548 local->name, local_pad->index, 1549 remote->name, remote_pad->index); 1550 1551 ret = media_create_pad_link(local, local_pad->index, 1552 remote, remote_pad->index, 1553 link_flags); 1554 v4l2_fwnode_put_link(&link); 1555 if (ret < 0) { 1556 dev_err(vi->dev, 1557 "failed to create %s:%u -> %s:%u link: %d\n", 1558 local->name, local_pad->index, 1559 remote->name, remote_pad->index, ret); 1560 break; 1561 } 1562 } 1563 1564 fwnode_handle_put(ep); 1565 return ret; 1566 } 1567 1568 static int tegra_vi_graph_notify_complete(struct v4l2_async_notifier *notifier) 1569 { 1570 struct tegra_vi_graph_entity *entity; 1571 struct v4l2_async_connection *asd; 1572 struct v4l2_subdev *subdev; 1573 struct tegra_vi_channel *chan; 1574 struct tegra_vi *vi; 1575 int ret; 1576 1577 chan = container_of(notifier, struct tegra_vi_channel, notifier); 1578 vi = chan->vi; 1579 1580 dev_dbg(vi->dev, "notify complete, all subdevs registered\n"); 1581 1582 /* 1583 * Video device node should be created at the end of all the device 1584 * related initialization/setup. 1585 * Current video_register_device() does both initialize and register 1586 * video device in same API. 1587 * 1588 * TODO: Update v4l2-dev driver to split initialize and register into 1589 * separate APIs and then update Tegra video driver to do video device 1590 * initialize followed by all video device related setup and then 1591 * register the video device. 1592 */ 1593 ret = video_register_device(&chan->video, VFL_TYPE_VIDEO, -1); 1594 if (ret < 0) { 1595 dev_err(vi->dev, 1596 "failed to register video device: %d\n", ret); 1597 goto unregister_video; 1598 } 1599 1600 /* create links between the entities */ 1601 list_for_each_entry(asd, &chan->notifier.done_list, asc_entry) { 1602 entity = to_tegra_vi_graph_entity(asd); 1603 ret = tegra_vi_graph_build(chan, entity); 1604 if (ret < 0) 1605 goto unregister_video; 1606 } 1607 1608 ret = tegra_channel_setup_ctrl_handler(chan); 1609 if (ret < 0) { 1610 dev_err(vi->dev, 1611 "failed to setup channel controls: %d\n", ret); 1612 goto unregister_video; 1613 } 1614 1615 ret = vi_fmts_bitmap_init(chan); 1616 if (ret < 0) { 1617 dev_err(vi->dev, 1618 "failed to initialize formats bitmap: %d\n", ret); 1619 goto unregister_video; 1620 } 1621 1622 subdev = tegra_channel_get_remote_bridge_subdev(chan); 1623 if (!subdev) { 1624 ret = -ENODEV; 1625 dev_err(vi->dev, 1626 "failed to get remote bridge subdev: %d\n", ret); 1627 goto unregister_video; 1628 } 1629 1630 v4l2_set_subdev_hostdata(subdev, chan); 1631 1632 subdev = tegra_channel_get_remote_source_subdev(chan); 1633 v4l2_set_subdev_hostdata(subdev, chan); 1634 1635 return 0; 1636 1637 unregister_video: 1638 vb2_video_unregister_device(&chan->video); 1639 return ret; 1640 } 1641 1642 static int tegra_vi_graph_notify_bound(struct v4l2_async_notifier *notifier, 1643 struct v4l2_subdev *subdev, 1644 struct v4l2_async_connection *asd) 1645 { 1646 struct tegra_vi_graph_entity *entity; 1647 struct tegra_vi *vi; 1648 struct tegra_vi_channel *chan; 1649 1650 chan = container_of(notifier, struct tegra_vi_channel, notifier); 1651 vi = chan->vi; 1652 1653 /* 1654 * Locate the entity corresponding to the bound subdev and store the 1655 * subdev pointer. 1656 */ 1657 entity = tegra_vi_graph_find_entity(&chan->notifier.waiting_list, 1658 subdev->fwnode); 1659 if (!entity) { 1660 dev_err(vi->dev, "no entity for subdev %s\n", subdev->name); 1661 return -EINVAL; 1662 } 1663 1664 if (entity->subdev) { 1665 dev_err(vi->dev, "duplicate subdev for node %pOF\n", 1666 to_of_node(entity->asd.match.fwnode)); 1667 return -EINVAL; 1668 } 1669 1670 dev_dbg(vi->dev, "subdev %s bound\n", subdev->name); 1671 entity->entity = &subdev->entity; 1672 entity->subdev = subdev; 1673 1674 return 0; 1675 } 1676 1677 static const struct v4l2_async_notifier_operations tegra_vi_async_ops = { 1678 .bound = tegra_vi_graph_notify_bound, 1679 .complete = tegra_vi_graph_notify_complete, 1680 }; 1681 1682 static int tegra_vi_graph_parse_one(struct tegra_vi_channel *chan, 1683 struct fwnode_handle *fwnode) 1684 { 1685 struct tegra_vi *vi = chan->vi; 1686 struct fwnode_handle *ep = NULL; 1687 struct fwnode_handle *remote = NULL; 1688 struct tegra_vi_graph_entity *tvge; 1689 struct device_node *node = NULL; 1690 int ret; 1691 1692 dev_dbg(vi->dev, "parsing node %pOF\n", to_of_node(fwnode)); 1693 1694 /* parse all the remote entities and put them into the list */ 1695 for_each_endpoint_of_node(to_of_node(fwnode), node) { 1696 ep = of_fwnode_handle(node); 1697 remote = fwnode_graph_get_remote_port_parent(ep); 1698 if (!remote) { 1699 dev_err(vi->dev, 1700 "remote device at %pOF not found\n", node); 1701 ret = -EINVAL; 1702 goto cleanup; 1703 } 1704 1705 /* skip entities that are already processed */ 1706 if (device_match_fwnode(vi->dev, remote) || 1707 tegra_vi_graph_find_entity(&chan->notifier.waiting_list, 1708 remote)) { 1709 fwnode_handle_put(remote); 1710 continue; 1711 } 1712 1713 tvge = v4l2_async_nf_add_fwnode(&chan->notifier, remote, 1714 struct tegra_vi_graph_entity); 1715 if (IS_ERR(tvge)) { 1716 ret = PTR_ERR(tvge); 1717 dev_err(vi->dev, 1718 "failed to add subdev to notifier: %d\n", ret); 1719 fwnode_handle_put(remote); 1720 goto cleanup; 1721 } 1722 1723 ret = tegra_vi_graph_parse_one(chan, remote); 1724 if (ret < 0) { 1725 fwnode_handle_put(remote); 1726 goto cleanup; 1727 } 1728 1729 fwnode_handle_put(remote); 1730 } 1731 1732 return 0; 1733 1734 cleanup: 1735 dev_err(vi->dev, "failed parsing the graph: %d\n", ret); 1736 v4l2_async_nf_cleanup(&chan->notifier); 1737 of_node_put(node); 1738 return ret; 1739 } 1740 1741 static int tegra_vi_graph_init(struct tegra_vi *vi) 1742 { 1743 struct tegra_vi_channel *chan; 1744 struct fwnode_handle *fwnode = dev_fwnode(vi->dev); 1745 int ret; 1746 1747 /* 1748 * Walk the links to parse the full graph. Each channel will have 1749 * one endpoint of the composite node. Start by parsing the 1750 * composite node and parse the remote entities in turn. 1751 * Each channel will register a v4l2 async notifier to make the graph 1752 * independent between the channels so we can skip the current channel 1753 * in case of something wrong during graph parsing and continue with 1754 * the next channels. 1755 */ 1756 list_for_each_entry(chan, &vi->vi_chans, list) { 1757 struct fwnode_handle *ep, *remote; 1758 1759 ep = fwnode_graph_get_endpoint_by_id(fwnode, 1760 chan->portnos[0], 0, 0); 1761 if (!ep) 1762 continue; 1763 1764 remote = fwnode_graph_get_remote_port_parent(ep); 1765 fwnode_handle_put(ep); 1766 1767 ret = tegra_vi_graph_parse_one(chan, remote); 1768 fwnode_handle_put(remote); 1769 if (ret < 0 || list_empty(&chan->notifier.waiting_list)) 1770 continue; 1771 1772 chan->notifier.ops = &tegra_vi_async_ops; 1773 ret = v4l2_async_nf_register(&chan->notifier); 1774 if (ret < 0) { 1775 dev_err(vi->dev, 1776 "failed to register channel %d notifier: %d\n", 1777 chan->portnos[0], ret); 1778 v4l2_async_nf_cleanup(&chan->notifier); 1779 } 1780 } 1781 1782 return 0; 1783 } 1784 1785 static void tegra_vi_graph_cleanup(struct tegra_vi *vi) 1786 { 1787 struct tegra_vi_channel *chan; 1788 1789 list_for_each_entry(chan, &vi->vi_chans, list) { 1790 vb2_video_unregister_device(&chan->video); 1791 v4l2_async_nf_unregister(&chan->notifier); 1792 v4l2_async_nf_cleanup(&chan->notifier); 1793 } 1794 } 1795 1796 static int tegra_vi_init(struct host1x_client *client) 1797 { 1798 struct tegra_video_device *vid = dev_get_drvdata(client->host); 1799 struct tegra_vi *vi = host1x_client_to_vi(client); 1800 struct tegra_vi_channel *chan, *tmp; 1801 int ret; 1802 1803 vid->media_dev.hw_revision = vi->soc->hw_revision; 1804 snprintf(vid->media_dev.bus_info, sizeof(vid->media_dev.bus_info), 1805 "platform:%s", dev_name(vi->dev)); 1806 1807 INIT_LIST_HEAD(&vi->vi_chans); 1808 1809 if (IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) 1810 ret = tegra_vi_tpg_channels_alloc(vi); 1811 else 1812 ret = tegra_vi_channels_alloc(vi); 1813 if (ret < 0) 1814 goto free_chans; 1815 1816 ret = tegra_vi_channels_init(vi); 1817 if (ret < 0) 1818 goto free_chans; 1819 1820 vid->vi = vi; 1821 1822 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) { 1823 ret = tegra_vi_graph_init(vi); 1824 if (ret < 0) 1825 goto cleanup_chans; 1826 } 1827 1828 return 0; 1829 1830 cleanup_chans: 1831 list_for_each_entry(chan, &vi->vi_chans, list) 1832 tegra_channel_cleanup(chan); 1833 free_chans: 1834 list_for_each_entry_safe(chan, tmp, &vi->vi_chans, list) { 1835 list_del(&chan->list); 1836 kfree(chan); 1837 } 1838 1839 return ret; 1840 } 1841 1842 static int tegra_vi_exit(struct host1x_client *client) 1843 { 1844 struct tegra_vi *vi = host1x_client_to_vi(client); 1845 1846 /* 1847 * Do not cleanup the channels here as application might still be 1848 * holding video device nodes. Channels cleanup will happen during 1849 * v4l2_device release callback which gets called after all video 1850 * device nodes are released. 1851 */ 1852 1853 if (!IS_ENABLED(CONFIG_VIDEO_TEGRA_TPG)) 1854 tegra_vi_graph_cleanup(vi); 1855 1856 return 0; 1857 } 1858 1859 static const struct host1x_client_ops vi_client_ops = { 1860 .init = tegra_vi_init, 1861 .exit = tegra_vi_exit, 1862 }; 1863 1864 static int tegra_vi_probe(struct platform_device *pdev) 1865 { 1866 struct tegra_vi *vi; 1867 int ret; 1868 1869 vi = devm_kzalloc(&pdev->dev, sizeof(*vi), GFP_KERNEL); 1870 if (!vi) 1871 return -ENOMEM; 1872 1873 vi->iomem = devm_platform_ioremap_resource(pdev, 0); 1874 if (IS_ERR(vi->iomem)) 1875 return PTR_ERR(vi->iomem); 1876 1877 vi->soc = of_device_get_match_data(&pdev->dev); 1878 1879 vi->clk = devm_clk_get(&pdev->dev, NULL); 1880 if (IS_ERR(vi->clk)) { 1881 ret = PTR_ERR(vi->clk); 1882 dev_err(&pdev->dev, "failed to get vi clock: %d\n", ret); 1883 return ret; 1884 } 1885 1886 if (!pdev->dev.pm_domain) { 1887 ret = -ENOENT; 1888 dev_warn(&pdev->dev, "PM domain is not attached: %d\n", ret); 1889 return ret; 1890 } 1891 1892 ret = devm_of_platform_populate(&pdev->dev); 1893 if (ret < 0) { 1894 dev_err(&pdev->dev, 1895 "failed to populate vi child device: %d\n", ret); 1896 return ret; 1897 } 1898 1899 vi->dev = &pdev->dev; 1900 vi->ops = vi->soc->ops; 1901 platform_set_drvdata(pdev, vi); 1902 pm_runtime_enable(&pdev->dev); 1903 1904 /* initialize host1x interface */ 1905 INIT_LIST_HEAD(&vi->client.list); 1906 vi->client.ops = &vi_client_ops; 1907 vi->client.dev = &pdev->dev; 1908 1909 if (vi->ops->vi_enable) 1910 vi->ops->vi_enable(vi, true); 1911 1912 ret = host1x_client_register(&vi->client); 1913 if (ret < 0) { 1914 dev_err(&pdev->dev, 1915 "failed to register host1x client: %d\n", ret); 1916 goto rpm_disable; 1917 } 1918 1919 return 0; 1920 1921 rpm_disable: 1922 if (vi->ops->vi_enable) 1923 vi->ops->vi_enable(vi, false); 1924 pm_runtime_disable(&pdev->dev); 1925 return ret; 1926 } 1927 1928 static void tegra_vi_remove(struct platform_device *pdev) 1929 { 1930 struct tegra_vi *vi = platform_get_drvdata(pdev); 1931 1932 host1x_client_unregister(&vi->client); 1933 1934 if (vi->ops->vi_enable) 1935 vi->ops->vi_enable(vi, false); 1936 pm_runtime_disable(&pdev->dev); 1937 } 1938 1939 static const struct of_device_id tegra_vi_of_id_table[] = { 1940 #if defined(CONFIG_ARCH_TEGRA_2x_SOC) || defined(CONFIG_ARCH_TEGRA_3x_SOC) 1941 { .compatible = "nvidia,tegra20-vi", .data = &tegra20_vi_soc }, 1942 #endif 1943 #if defined(CONFIG_ARCH_TEGRA_210_SOC) 1944 { .compatible = "nvidia,tegra210-vi", .data = &tegra210_vi_soc }, 1945 #endif 1946 { } 1947 }; 1948 MODULE_DEVICE_TABLE(of, tegra_vi_of_id_table); 1949 1950 static const struct dev_pm_ops tegra_vi_pm_ops = { 1951 SET_RUNTIME_PM_OPS(vi_runtime_suspend, vi_runtime_resume, NULL) 1952 }; 1953 1954 struct platform_driver tegra_vi_driver = { 1955 .driver = { 1956 .name = "tegra-vi", 1957 .of_match_table = tegra_vi_of_id_table, 1958 .pm = &tegra_vi_pm_ops, 1959 }, 1960 .probe = tegra_vi_probe, 1961 .remove = tegra_vi_remove, 1962 }; 1963