1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Driver for Renesas R-Car VIN 4 * 5 * Copyright (C) 2016 Renesas Electronics Corp. 6 * Copyright (C) 2011-2013 Renesas Solutions Corp. 7 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com> 8 * Copyright (C) 2008 Magnus Damm 9 * 10 * Based on the soc-camera rcar_vin driver 11 */ 12 13 #include <linux/pm_runtime.h> 14 15 #include <media/v4l2-event.h> 16 #include <media/v4l2-ioctl.h> 17 #include <media/v4l2-mc.h> 18 #include <media/v4l2-rect.h> 19 20 #include "rcar-vin.h" 21 22 #define RVIN_DEFAULT_FORMAT V4L2_PIX_FMT_YUYV 23 #define RVIN_DEFAULT_WIDTH 800 24 #define RVIN_DEFAULT_HEIGHT 600 25 #define RVIN_DEFAULT_FIELD V4L2_FIELD_NONE 26 #define RVIN_DEFAULT_COLORSPACE V4L2_COLORSPACE_SRGB 27 28 /* ----------------------------------------------------------------------------- 29 * Format Conversions 30 */ 31 32 static const struct rvin_video_format rvin_formats[] = { 33 { 34 .fourcc = V4L2_PIX_FMT_NV12, 35 .bpp = 1, 36 }, 37 { 38 .fourcc = V4L2_PIX_FMT_NV16, 39 .bpp = 1, 40 }, 41 { 42 .fourcc = V4L2_PIX_FMT_YUYV, 43 .bpp = 2, 44 }, 45 { 46 .fourcc = V4L2_PIX_FMT_UYVY, 47 .bpp = 2, 48 }, 49 { 50 .fourcc = V4L2_PIX_FMT_RGB565, 51 .bpp = 2, 52 }, 53 { 54 .fourcc = V4L2_PIX_FMT_XRGB555, 55 .bpp = 2, 56 }, 57 { 58 .fourcc = V4L2_PIX_FMT_XBGR32, 59 .bpp = 4, 60 }, 61 { 62 .fourcc = V4L2_PIX_FMT_ARGB555, 63 .bpp = 2, 64 }, 65 { 66 .fourcc = V4L2_PIX_FMT_ABGR32, 67 .bpp = 4, 68 }, 69 { 70 .fourcc = V4L2_PIX_FMT_SBGGR8, 71 .bpp = 1, 72 }, 73 { 74 .fourcc = V4L2_PIX_FMT_SGBRG8, 75 .bpp = 1, 76 }, 77 { 78 .fourcc = V4L2_PIX_FMT_SGRBG8, 79 .bpp = 1, 80 }, 81 { 82 .fourcc = V4L2_PIX_FMT_SRGGB8, 83 .bpp = 1, 84 }, 85 { 86 .fourcc = V4L2_PIX_FMT_GREY, 87 .bpp = 1, 88 }, 89 }; 90 91 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin, 92 u32 pixelformat) 93 { 94 int i; 95 96 switch (pixelformat) { 97 case V4L2_PIX_FMT_XBGR32: 98 if (vin->info->model == RCAR_M1) 99 return NULL; 100 break; 101 case V4L2_PIX_FMT_NV12: 102 /* 103 * If NV12 is supported it's only supported on channels 0, 1, 4, 104 * 5, 8, 9, 12 and 13. 105 */ 106 if (!vin->info->nv12 || !(BIT(vin->id) & 0x3333)) 107 return NULL; 108 break; 109 default: 110 break; 111 } 112 113 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++) 114 if (rvin_formats[i].fourcc == pixelformat) 115 return rvin_formats + i; 116 117 return NULL; 118 } 119 120 static u32 rvin_format_bytesperline(struct rvin_dev *vin, 121 struct v4l2_pix_format *pix) 122 { 123 const struct rvin_video_format *fmt; 124 u32 align; 125 126 fmt = rvin_format_from_pixel(vin, pix->pixelformat); 127 128 if (WARN_ON(!fmt)) 129 return -EINVAL; 130 131 switch (pix->pixelformat) { 132 case V4L2_PIX_FMT_NV12: 133 case V4L2_PIX_FMT_NV16: 134 align = 0x20; 135 break; 136 default: 137 align = 0x10; 138 break; 139 } 140 141 if (V4L2_FIELD_IS_SEQUENTIAL(pix->field)) 142 align = 0x80; 143 144 return ALIGN(pix->width, align) * fmt->bpp; 145 } 146 147 static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix) 148 { 149 switch (pix->pixelformat) { 150 case V4L2_PIX_FMT_NV12: 151 return pix->bytesperline * pix->height * 3 / 2; 152 case V4L2_PIX_FMT_NV16: 153 return pix->bytesperline * pix->height * 2; 154 default: 155 return pix->bytesperline * pix->height; 156 } 157 } 158 159 static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix) 160 { 161 u32 walign; 162 163 if (!rvin_format_from_pixel(vin, pix->pixelformat)) 164 pix->pixelformat = RVIN_DEFAULT_FORMAT; 165 166 switch (pix->field) { 167 case V4L2_FIELD_TOP: 168 case V4L2_FIELD_BOTTOM: 169 case V4L2_FIELD_NONE: 170 case V4L2_FIELD_INTERLACED_TB: 171 case V4L2_FIELD_INTERLACED_BT: 172 case V4L2_FIELD_INTERLACED: 173 case V4L2_FIELD_ALTERNATE: 174 case V4L2_FIELD_SEQ_TB: 175 case V4L2_FIELD_SEQ_BT: 176 break; 177 default: 178 pix->field = RVIN_DEFAULT_FIELD; 179 break; 180 } 181 182 /* Hardware limits width alignment based on format. */ 183 switch (pix->pixelformat) { 184 /* Multiple of 32 (2^5) for NV12/16. */ 185 case V4L2_PIX_FMT_NV12: 186 case V4L2_PIX_FMT_NV16: 187 walign = 5; 188 break; 189 /* Multiple of 2 (2^1) for YUV. */ 190 case V4L2_PIX_FMT_YUYV: 191 case V4L2_PIX_FMT_UYVY: 192 walign = 1; 193 break; 194 /* No multiple for RGB. */ 195 default: 196 walign = 0; 197 break; 198 } 199 200 /* Limit to VIN capabilities */ 201 v4l_bound_align_image(&pix->width, 5, vin->info->max_width, walign, 202 &pix->height, 2, vin->info->max_height, 0, 0); 203 204 pix->bytesperline = rvin_format_bytesperline(vin, pix); 205 pix->sizeimage = rvin_format_sizeimage(pix); 206 207 vin_dbg(vin, "Format %ux%u bpl: %u size: %u\n", 208 pix->width, pix->height, pix->bytesperline, pix->sizeimage); 209 } 210 211 /* ----------------------------------------------------------------------------- 212 * V4L2 213 */ 214 215 static int rvin_reset_format(struct rvin_dev *vin) 216 { 217 struct v4l2_subdev_format fmt = { 218 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 219 .pad = vin->parallel.source_pad, 220 }; 221 int ret; 222 223 ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt); 224 if (ret) 225 return ret; 226 227 v4l2_fill_pix_format(&vin->format, &fmt.format); 228 229 vin->src_rect.top = 0; 230 vin->src_rect.left = 0; 231 vin->src_rect.width = vin->format.width; 232 vin->src_rect.height = vin->format.height; 233 234 /* Make use of the hardware interlacer by default. */ 235 if (vin->format.field == V4L2_FIELD_ALTERNATE) { 236 vin->format.field = V4L2_FIELD_INTERLACED; 237 vin->format.height *= 2; 238 } 239 240 rvin_format_align(vin, &vin->format); 241 242 vin->crop = vin->src_rect; 243 244 vin->compose.top = 0; 245 vin->compose.left = 0; 246 vin->compose.width = vin->format.width; 247 vin->compose.height = vin->format.height; 248 249 return 0; 250 } 251 252 static int rvin_try_format(struct rvin_dev *vin, u32 which, 253 struct v4l2_pix_format *pix, 254 struct v4l2_rect *src_rect) 255 { 256 struct v4l2_subdev *sd = vin_to_source(vin); 257 struct v4l2_subdev_state *sd_state; 258 struct v4l2_subdev_format format = { 259 .which = which, 260 .pad = vin->parallel.source_pad, 261 }; 262 enum v4l2_field field; 263 u32 width, height; 264 int ret; 265 266 sd_state = v4l2_subdev_alloc_state(sd); 267 if (IS_ERR(sd_state)) 268 return PTR_ERR(sd_state); 269 270 if (!rvin_format_from_pixel(vin, pix->pixelformat)) 271 pix->pixelformat = RVIN_DEFAULT_FORMAT; 272 273 v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code); 274 275 /* Allow the video device to override field and to scale */ 276 field = pix->field; 277 width = pix->width; 278 height = pix->height; 279 280 ret = v4l2_subdev_call(sd, pad, set_fmt, sd_state, &format); 281 if (ret < 0 && ret != -ENOIOCTLCMD) 282 goto done; 283 ret = 0; 284 285 v4l2_fill_pix_format(pix, &format.format); 286 287 if (src_rect) { 288 src_rect->top = 0; 289 src_rect->left = 0; 290 src_rect->width = pix->width; 291 src_rect->height = pix->height; 292 } 293 294 if (field != V4L2_FIELD_ANY) 295 pix->field = field; 296 297 pix->width = width; 298 pix->height = height; 299 300 rvin_format_align(vin, pix); 301 done: 302 v4l2_subdev_free_state(sd_state); 303 304 return ret; 305 } 306 307 static int rvin_querycap(struct file *file, void *priv, 308 struct v4l2_capability *cap) 309 { 310 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); 311 strscpy(cap->card, "R_Car_VIN", sizeof(cap->card)); 312 return 0; 313 } 314 315 static int rvin_try_fmt_vid_cap(struct file *file, void *priv, 316 struct v4l2_format *f) 317 { 318 struct rvin_dev *vin = video_drvdata(file); 319 320 return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL); 321 } 322 323 static int rvin_s_fmt_vid_cap(struct file *file, void *priv, 324 struct v4l2_format *f) 325 { 326 struct rvin_dev *vin = video_drvdata(file); 327 struct v4l2_rect fmt_rect, src_rect; 328 int ret; 329 330 if (vb2_is_busy(&vin->queue)) 331 return -EBUSY; 332 333 ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix, 334 &src_rect); 335 if (ret) 336 return ret; 337 338 vin->format = f->fmt.pix; 339 340 fmt_rect.top = 0; 341 fmt_rect.left = 0; 342 fmt_rect.width = vin->format.width; 343 fmt_rect.height = vin->format.height; 344 345 v4l2_rect_map_inside(&vin->crop, &src_rect); 346 v4l2_rect_map_inside(&vin->compose, &fmt_rect); 347 vin->src_rect = src_rect; 348 349 return 0; 350 } 351 352 static int rvin_g_fmt_vid_cap(struct file *file, void *priv, 353 struct v4l2_format *f) 354 { 355 struct rvin_dev *vin = video_drvdata(file); 356 357 f->fmt.pix = vin->format; 358 359 return 0; 360 } 361 362 static int rvin_enum_fmt_vid_cap(struct file *file, void *priv, 363 struct v4l2_fmtdesc *f) 364 { 365 struct rvin_dev *vin = video_drvdata(file); 366 unsigned int i; 367 int matched; 368 369 /* 370 * If mbus_code is set only enumerate supported pixel formats for that 371 * bus code. Converting from YCbCr to RGB and RGB to YCbCr is possible 372 * with VIN, so all supported YCbCr and RGB media bus codes can produce 373 * all of the related pixel formats. If mbus_code is not set enumerate 374 * all possible pixelformats. 375 * 376 * TODO: Once raw MEDIA_BUS_FMT_SRGGB12_1X12 format is added to the 377 * driver this needs to be extended so raw media bus code only result in 378 * raw pixel format. 379 */ 380 switch (f->mbus_code) { 381 case 0: 382 case MEDIA_BUS_FMT_YUYV8_1X16: 383 case MEDIA_BUS_FMT_UYVY8_1X16: 384 case MEDIA_BUS_FMT_UYVY8_2X8: 385 case MEDIA_BUS_FMT_UYVY10_2X10: 386 case MEDIA_BUS_FMT_RGB888_1X24: 387 break; 388 case MEDIA_BUS_FMT_SBGGR8_1X8: 389 if (f->index) 390 return -EINVAL; 391 f->pixelformat = V4L2_PIX_FMT_SBGGR8; 392 return 0; 393 case MEDIA_BUS_FMT_SGBRG8_1X8: 394 if (f->index) 395 return -EINVAL; 396 f->pixelformat = V4L2_PIX_FMT_SGBRG8; 397 return 0; 398 case MEDIA_BUS_FMT_SGRBG8_1X8: 399 if (f->index) 400 return -EINVAL; 401 f->pixelformat = V4L2_PIX_FMT_SGRBG8; 402 return 0; 403 case MEDIA_BUS_FMT_SRGGB8_1X8: 404 if (f->index) 405 return -EINVAL; 406 f->pixelformat = V4L2_PIX_FMT_SRGGB8; 407 return 0; 408 default: 409 return -EINVAL; 410 } 411 412 matched = -1; 413 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++) { 414 if (rvin_format_from_pixel(vin, rvin_formats[i].fourcc)) 415 matched++; 416 417 if (matched == f->index) { 418 f->pixelformat = rvin_formats[i].fourcc; 419 return 0; 420 } 421 } 422 423 return -EINVAL; 424 } 425 426 static int rvin_g_selection(struct file *file, void *fh, 427 struct v4l2_selection *s) 428 { 429 struct rvin_dev *vin = video_drvdata(file); 430 431 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 432 return -EINVAL; 433 434 switch (s->target) { 435 case V4L2_SEL_TGT_CROP_BOUNDS: 436 case V4L2_SEL_TGT_CROP_DEFAULT: 437 s->r.left = s->r.top = 0; 438 s->r.width = vin->src_rect.width; 439 s->r.height = vin->src_rect.height; 440 break; 441 case V4L2_SEL_TGT_CROP: 442 s->r = vin->crop; 443 break; 444 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 445 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 446 s->r.left = s->r.top = 0; 447 s->r.width = vin->format.width; 448 s->r.height = vin->format.height; 449 break; 450 case V4L2_SEL_TGT_COMPOSE: 451 s->r = vin->compose; 452 break; 453 default: 454 return -EINVAL; 455 } 456 457 return 0; 458 } 459 460 static int rvin_s_selection(struct file *file, void *fh, 461 struct v4l2_selection *s) 462 { 463 struct rvin_dev *vin = video_drvdata(file); 464 const struct rvin_video_format *fmt; 465 struct v4l2_rect r = s->r; 466 struct v4l2_rect max_rect; 467 struct v4l2_rect min_rect = { 468 .width = 6, 469 .height = 2, 470 }; 471 472 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 473 return -EINVAL; 474 475 v4l2_rect_set_min_size(&r, &min_rect); 476 477 switch (s->target) { 478 case V4L2_SEL_TGT_CROP: 479 /* Can't crop outside of source input */ 480 max_rect.top = max_rect.left = 0; 481 max_rect.width = vin->src_rect.width; 482 max_rect.height = vin->src_rect.height; 483 v4l2_rect_map_inside(&r, &max_rect); 484 485 v4l_bound_align_image(&r.width, 6, vin->src_rect.width, 0, 486 &r.height, 2, vin->src_rect.height, 0, 0); 487 488 r.top = clamp_t(s32, r.top, 0, 489 vin->src_rect.height - r.height); 490 r.left = clamp_t(s32, r.left, 0, vin->src_rect.width - r.width); 491 492 vin->crop = s->r = r; 493 494 vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n", 495 r.width, r.height, r.left, r.top, 496 vin->src_rect.width, vin->src_rect.height); 497 break; 498 case V4L2_SEL_TGT_COMPOSE: 499 /* Make sure compose rect fits inside output format */ 500 max_rect.top = max_rect.left = 0; 501 max_rect.width = vin->format.width; 502 max_rect.height = vin->format.height; 503 v4l2_rect_map_inside(&r, &max_rect); 504 505 /* 506 * Composing is done by adding a offset to the buffer address, 507 * the HW wants this address to be aligned to HW_BUFFER_MASK. 508 * Make sure the top and left values meets this requirement. 509 */ 510 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK) 511 r.top--; 512 513 fmt = rvin_format_from_pixel(vin, vin->format.pixelformat); 514 while ((r.left * fmt->bpp) & HW_BUFFER_MASK) 515 r.left--; 516 517 vin->compose = s->r = r; 518 519 vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n", 520 r.width, r.height, r.left, r.top, 521 vin->format.width, vin->format.height); 522 break; 523 default: 524 return -EINVAL; 525 } 526 527 /* HW supports modifying configuration while running */ 528 rvin_crop_scale_comp(vin); 529 530 return 0; 531 } 532 533 static int rvin_g_parm(struct file *file, void *priv, 534 struct v4l2_streamparm *parm) 535 { 536 struct rvin_dev *vin = video_drvdata(file); 537 struct v4l2_subdev *sd = vin_to_source(vin); 538 539 return v4l2_g_parm_cap(&vin->vdev, sd, parm); 540 } 541 542 static int rvin_s_parm(struct file *file, void *priv, 543 struct v4l2_streamparm *parm) 544 { 545 struct rvin_dev *vin = video_drvdata(file); 546 struct v4l2_subdev *sd = vin_to_source(vin); 547 548 return v4l2_s_parm_cap(&vin->vdev, sd, parm); 549 } 550 551 static int rvin_g_pixelaspect(struct file *file, void *priv, 552 int type, struct v4l2_fract *f) 553 { 554 struct rvin_dev *vin = video_drvdata(file); 555 struct v4l2_subdev *sd = vin_to_source(vin); 556 557 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 558 return -EINVAL; 559 560 return v4l2_subdev_call(sd, video, g_pixelaspect, f); 561 } 562 563 static int rvin_enum_input(struct file *file, void *priv, 564 struct v4l2_input *i) 565 { 566 struct rvin_dev *vin = video_drvdata(file); 567 struct v4l2_subdev *sd = vin_to_source(vin); 568 int ret; 569 570 if (i->index != 0) 571 return -EINVAL; 572 573 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status); 574 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 575 return ret; 576 577 i->type = V4L2_INPUT_TYPE_CAMERA; 578 579 if (v4l2_subdev_has_op(sd, pad, dv_timings_cap)) { 580 i->capabilities = V4L2_IN_CAP_DV_TIMINGS; 581 i->std = 0; 582 } else { 583 i->capabilities = V4L2_IN_CAP_STD; 584 i->std = vin->vdev.tvnorms; 585 } 586 587 strscpy(i->name, "Camera", sizeof(i->name)); 588 589 return 0; 590 } 591 592 static int rvin_g_input(struct file *file, void *priv, unsigned int *i) 593 { 594 *i = 0; 595 return 0; 596 } 597 598 static int rvin_s_input(struct file *file, void *priv, unsigned int i) 599 { 600 if (i > 0) 601 return -EINVAL; 602 return 0; 603 } 604 605 static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a) 606 { 607 struct rvin_dev *vin = video_drvdata(file); 608 struct v4l2_subdev *sd = vin_to_source(vin); 609 610 return v4l2_subdev_call(sd, video, querystd, a); 611 } 612 613 static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a) 614 { 615 struct rvin_dev *vin = video_drvdata(file); 616 int ret; 617 618 ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a); 619 if (ret < 0) 620 return ret; 621 622 vin->std = a; 623 624 /* Changing the standard will change the width/height */ 625 return rvin_reset_format(vin); 626 } 627 628 static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a) 629 { 630 struct rvin_dev *vin = video_drvdata(file); 631 632 if (v4l2_subdev_has_op(vin_to_source(vin), pad, dv_timings_cap)) 633 return -ENOIOCTLCMD; 634 635 *a = vin->std; 636 637 return 0; 638 } 639 640 static int rvin_subscribe_event(struct v4l2_fh *fh, 641 const struct v4l2_event_subscription *sub) 642 { 643 switch (sub->type) { 644 case V4L2_EVENT_SOURCE_CHANGE: 645 return v4l2_event_subscribe(fh, sub, 4, NULL); 646 } 647 return v4l2_ctrl_subscribe_event(fh, sub); 648 } 649 650 static int rvin_enum_dv_timings(struct file *file, void *priv_fh, 651 struct v4l2_enum_dv_timings *timings) 652 { 653 struct rvin_dev *vin = video_drvdata(file); 654 struct v4l2_subdev *sd = vin_to_source(vin); 655 int ret; 656 657 if (timings->pad) 658 return -EINVAL; 659 660 timings->pad = vin->parallel.sink_pad; 661 662 ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings); 663 664 timings->pad = 0; 665 666 return ret; 667 } 668 669 static int rvin_s_dv_timings(struct file *file, void *priv_fh, 670 struct v4l2_dv_timings *timings) 671 { 672 struct rvin_dev *vin = video_drvdata(file); 673 struct v4l2_subdev *sd = vin_to_source(vin); 674 int ret; 675 676 ret = v4l2_subdev_call(sd, video, s_dv_timings, timings); 677 if (ret) 678 return ret; 679 680 /* Changing the timings will change the width/height */ 681 return rvin_reset_format(vin); 682 } 683 684 static int rvin_g_dv_timings(struct file *file, void *priv_fh, 685 struct v4l2_dv_timings *timings) 686 { 687 struct rvin_dev *vin = video_drvdata(file); 688 struct v4l2_subdev *sd = vin_to_source(vin); 689 690 return v4l2_subdev_call(sd, video, g_dv_timings, timings); 691 } 692 693 static int rvin_query_dv_timings(struct file *file, void *priv_fh, 694 struct v4l2_dv_timings *timings) 695 { 696 struct rvin_dev *vin = video_drvdata(file); 697 struct v4l2_subdev *sd = vin_to_source(vin); 698 699 return v4l2_subdev_call(sd, video, query_dv_timings, timings); 700 } 701 702 static int rvin_dv_timings_cap(struct file *file, void *priv_fh, 703 struct v4l2_dv_timings_cap *cap) 704 { 705 struct rvin_dev *vin = video_drvdata(file); 706 struct v4l2_subdev *sd = vin_to_source(vin); 707 int ret; 708 709 if (cap->pad) 710 return -EINVAL; 711 712 cap->pad = vin->parallel.sink_pad; 713 714 ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap); 715 716 cap->pad = 0; 717 718 return ret; 719 } 720 721 static int rvin_g_edid(struct file *file, void *fh, struct v4l2_edid *edid) 722 { 723 struct rvin_dev *vin = video_drvdata(file); 724 struct v4l2_subdev *sd = vin_to_source(vin); 725 int ret; 726 727 if (edid->pad) 728 return -EINVAL; 729 730 edid->pad = vin->parallel.sink_pad; 731 732 ret = v4l2_subdev_call(sd, pad, get_edid, edid); 733 734 edid->pad = 0; 735 736 return ret; 737 } 738 739 static int rvin_s_edid(struct file *file, void *fh, struct v4l2_edid *edid) 740 { 741 struct rvin_dev *vin = video_drvdata(file); 742 struct v4l2_subdev *sd = vin_to_source(vin); 743 int ret; 744 745 if (edid->pad) 746 return -EINVAL; 747 748 edid->pad = vin->parallel.sink_pad; 749 750 ret = v4l2_subdev_call(sd, pad, set_edid, edid); 751 752 edid->pad = 0; 753 754 return ret; 755 } 756 757 static const struct v4l2_ioctl_ops rvin_ioctl_ops = { 758 .vidioc_querycap = rvin_querycap, 759 .vidioc_try_fmt_vid_cap = rvin_try_fmt_vid_cap, 760 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap, 761 .vidioc_s_fmt_vid_cap = rvin_s_fmt_vid_cap, 762 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap, 763 764 .vidioc_g_selection = rvin_g_selection, 765 .vidioc_s_selection = rvin_s_selection, 766 767 .vidioc_g_parm = rvin_g_parm, 768 .vidioc_s_parm = rvin_s_parm, 769 770 .vidioc_g_pixelaspect = rvin_g_pixelaspect, 771 772 .vidioc_enum_input = rvin_enum_input, 773 .vidioc_g_input = rvin_g_input, 774 .vidioc_s_input = rvin_s_input, 775 776 .vidioc_dv_timings_cap = rvin_dv_timings_cap, 777 .vidioc_enum_dv_timings = rvin_enum_dv_timings, 778 .vidioc_g_dv_timings = rvin_g_dv_timings, 779 .vidioc_s_dv_timings = rvin_s_dv_timings, 780 .vidioc_query_dv_timings = rvin_query_dv_timings, 781 782 .vidioc_g_edid = rvin_g_edid, 783 .vidioc_s_edid = rvin_s_edid, 784 785 .vidioc_querystd = rvin_querystd, 786 .vidioc_g_std = rvin_g_std, 787 .vidioc_s_std = rvin_s_std, 788 789 .vidioc_reqbufs = vb2_ioctl_reqbufs, 790 .vidioc_create_bufs = vb2_ioctl_create_bufs, 791 .vidioc_querybuf = vb2_ioctl_querybuf, 792 .vidioc_qbuf = vb2_ioctl_qbuf, 793 .vidioc_dqbuf = vb2_ioctl_dqbuf, 794 .vidioc_expbuf = vb2_ioctl_expbuf, 795 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 796 .vidioc_streamon = vb2_ioctl_streamon, 797 .vidioc_streamoff = vb2_ioctl_streamoff, 798 799 .vidioc_log_status = v4l2_ctrl_log_status, 800 .vidioc_subscribe_event = rvin_subscribe_event, 801 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 802 }; 803 804 /* ----------------------------------------------------------------------------- 805 * V4L2 Media Controller 806 */ 807 808 static void rvin_mc_try_format(struct rvin_dev *vin, 809 struct v4l2_pix_format *pix) 810 { 811 /* 812 * The V4L2 specification clearly documents the colorspace fields 813 * as being set by drivers for capture devices. Using the values 814 * supplied by userspace thus wouldn't comply with the API. Until 815 * the API is updated force fixed values. 816 */ 817 pix->colorspace = RVIN_DEFAULT_COLORSPACE; 818 pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace); 819 pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace); 820 pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace, 821 pix->ycbcr_enc); 822 823 rvin_format_align(vin, pix); 824 } 825 826 static int rvin_mc_try_fmt_vid_cap(struct file *file, void *priv, 827 struct v4l2_format *f) 828 { 829 struct rvin_dev *vin = video_drvdata(file); 830 831 rvin_mc_try_format(vin, &f->fmt.pix); 832 833 return 0; 834 } 835 836 static int rvin_mc_s_fmt_vid_cap(struct file *file, void *priv, 837 struct v4l2_format *f) 838 { 839 struct rvin_dev *vin = video_drvdata(file); 840 841 if (vb2_is_busy(&vin->queue)) 842 return -EBUSY; 843 844 rvin_mc_try_format(vin, &f->fmt.pix); 845 846 vin->format = f->fmt.pix; 847 848 vin->crop.top = 0; 849 vin->crop.left = 0; 850 vin->crop.width = vin->format.width; 851 vin->crop.height = vin->format.height; 852 vin->compose = vin->crop; 853 854 return 0; 855 } 856 857 static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops = { 858 .vidioc_querycap = rvin_querycap, 859 .vidioc_try_fmt_vid_cap = rvin_mc_try_fmt_vid_cap, 860 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap, 861 .vidioc_s_fmt_vid_cap = rvin_mc_s_fmt_vid_cap, 862 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap, 863 864 .vidioc_reqbufs = vb2_ioctl_reqbufs, 865 .vidioc_create_bufs = vb2_ioctl_create_bufs, 866 .vidioc_querybuf = vb2_ioctl_querybuf, 867 .vidioc_qbuf = vb2_ioctl_qbuf, 868 .vidioc_dqbuf = vb2_ioctl_dqbuf, 869 .vidioc_expbuf = vb2_ioctl_expbuf, 870 .vidioc_prepare_buf = vb2_ioctl_prepare_buf, 871 .vidioc_streamon = vb2_ioctl_streamon, 872 .vidioc_streamoff = vb2_ioctl_streamoff, 873 874 .vidioc_log_status = v4l2_ctrl_log_status, 875 .vidioc_subscribe_event = rvin_subscribe_event, 876 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 877 }; 878 879 /* ----------------------------------------------------------------------------- 880 * File Operations 881 */ 882 883 static int rvin_power_parallel(struct rvin_dev *vin, bool on) 884 { 885 struct v4l2_subdev *sd = vin_to_source(vin); 886 int power = on ? 1 : 0; 887 int ret; 888 889 ret = v4l2_subdev_call(sd, core, s_power, power); 890 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) 891 return ret; 892 893 return 0; 894 } 895 896 static int rvin_open(struct file *file) 897 { 898 struct rvin_dev *vin = video_drvdata(file); 899 int ret; 900 901 ret = pm_runtime_resume_and_get(vin->dev); 902 if (ret < 0) 903 return ret; 904 905 ret = mutex_lock_interruptible(&vin->lock); 906 if (ret) 907 goto err_pm; 908 909 file->private_data = vin; 910 911 ret = v4l2_fh_open(file); 912 if (ret) 913 goto err_unlock; 914 915 if (vin->info->use_mc) 916 ret = v4l2_pipeline_pm_get(&vin->vdev.entity); 917 else if (v4l2_fh_is_singular_file(file)) 918 ret = rvin_power_parallel(vin, true); 919 920 if (ret < 0) 921 goto err_open; 922 923 ret = v4l2_ctrl_handler_setup(&vin->ctrl_handler); 924 if (ret) 925 goto err_power; 926 927 mutex_unlock(&vin->lock); 928 929 return 0; 930 err_power: 931 if (vin->info->use_mc) 932 v4l2_pipeline_pm_put(&vin->vdev.entity); 933 else if (v4l2_fh_is_singular_file(file)) 934 rvin_power_parallel(vin, false); 935 err_open: 936 v4l2_fh_release(file); 937 err_unlock: 938 mutex_unlock(&vin->lock); 939 err_pm: 940 pm_runtime_put(vin->dev); 941 942 return ret; 943 } 944 945 static int rvin_release(struct file *file) 946 { 947 struct rvin_dev *vin = video_drvdata(file); 948 bool fh_singular; 949 int ret; 950 951 mutex_lock(&vin->lock); 952 953 /* Save the singular status before we call the clean-up helper */ 954 fh_singular = v4l2_fh_is_singular_file(file); 955 956 /* the release helper will cleanup any on-going streaming */ 957 ret = _vb2_fop_release(file, NULL); 958 959 if (vin->info->use_mc) { 960 v4l2_pipeline_pm_put(&vin->vdev.entity); 961 } else { 962 if (fh_singular) 963 rvin_power_parallel(vin, false); 964 } 965 966 mutex_unlock(&vin->lock); 967 968 pm_runtime_put(vin->dev); 969 970 return ret; 971 } 972 973 static const struct v4l2_file_operations rvin_fops = { 974 .owner = THIS_MODULE, 975 .unlocked_ioctl = video_ioctl2, 976 .open = rvin_open, 977 .release = rvin_release, 978 .poll = vb2_fop_poll, 979 .mmap = vb2_fop_mmap, 980 .read = vb2_fop_read, 981 }; 982 983 void rvin_v4l2_unregister(struct rvin_dev *vin) 984 { 985 if (!video_is_registered(&vin->vdev)) 986 return; 987 988 v4l2_info(&vin->v4l2_dev, "Removing %s\n", 989 video_device_node_name(&vin->vdev)); 990 991 /* Checks internally if vdev have been init or not */ 992 video_unregister_device(&vin->vdev); 993 } 994 995 static void rvin_notify_video_device(struct rvin_dev *vin, 996 unsigned int notification, void *arg) 997 { 998 switch (notification) { 999 case V4L2_DEVICE_NOTIFY_EVENT: 1000 v4l2_event_queue(&vin->vdev, arg); 1001 break; 1002 default: 1003 break; 1004 } 1005 } 1006 1007 static void rvin_notify(struct v4l2_subdev *sd, 1008 unsigned int notification, void *arg) 1009 { 1010 struct v4l2_subdev *remote; 1011 struct rvin_group *group; 1012 struct media_pad *pad; 1013 struct rvin_dev *vin = 1014 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev); 1015 unsigned int i; 1016 1017 /* If no media controller, no need to route the event. */ 1018 if (!vin->info->use_mc) { 1019 rvin_notify_video_device(vin, notification, arg); 1020 return; 1021 } 1022 1023 group = vin->group; 1024 1025 for (i = 0; i < RCAR_VIN_NUM; i++) { 1026 vin = group->vin[i]; 1027 if (!vin) 1028 continue; 1029 1030 pad = media_entity_remote_pad(&vin->pad); 1031 if (!pad) 1032 continue; 1033 1034 remote = media_entity_to_v4l2_subdev(pad->entity); 1035 if (remote != sd) 1036 continue; 1037 1038 rvin_notify_video_device(vin, notification, arg); 1039 } 1040 } 1041 1042 int rvin_v4l2_register(struct rvin_dev *vin) 1043 { 1044 struct video_device *vdev = &vin->vdev; 1045 int ret; 1046 1047 vin->v4l2_dev.notify = rvin_notify; 1048 1049 /* video node */ 1050 vdev->v4l2_dev = &vin->v4l2_dev; 1051 vdev->queue = &vin->queue; 1052 snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id); 1053 vdev->release = video_device_release_empty; 1054 vdev->lock = &vin->lock; 1055 vdev->fops = &rvin_fops; 1056 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | 1057 V4L2_CAP_READWRITE; 1058 1059 /* Set a default format */ 1060 vin->format.pixelformat = RVIN_DEFAULT_FORMAT; 1061 vin->format.width = RVIN_DEFAULT_WIDTH; 1062 vin->format.height = RVIN_DEFAULT_HEIGHT; 1063 vin->format.field = RVIN_DEFAULT_FIELD; 1064 vin->format.colorspace = RVIN_DEFAULT_COLORSPACE; 1065 1066 if (vin->info->use_mc) { 1067 vdev->device_caps |= V4L2_CAP_IO_MC; 1068 vdev->ioctl_ops = &rvin_mc_ioctl_ops; 1069 } else { 1070 vdev->ioctl_ops = &rvin_ioctl_ops; 1071 rvin_reset_format(vin); 1072 } 1073 1074 rvin_format_align(vin, &vin->format); 1075 1076 ret = video_register_device(&vin->vdev, VFL_TYPE_VIDEO, -1); 1077 if (ret) { 1078 vin_err(vin, "Failed to register video device\n"); 1079 return ret; 1080 } 1081 1082 video_set_drvdata(&vin->vdev, vin); 1083 1084 v4l2_info(&vin->v4l2_dev, "Device registered as %s\n", 1085 video_device_node_name(&vin->vdev)); 1086 1087 return ret; 1088 } 1089