1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) STMicroelectronics SA 2015 4 * Authors: Yannick Fertre <yannick.fertre@st.com> 5 * Hugues Fruchet <hugues.fruchet@st.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/mod_devicetable.h> 10 #include <linux/platform_device.h> 11 #include <linux/slab.h> 12 #include <media/v4l2-event.h> 13 #include <media/v4l2-ioctl.h> 14 #include <media/videobuf2-dma-contig.h> 15 16 #include "hva.h" 17 #include "hva-hw.h" 18 19 #define MIN_FRAMES 1 20 #define MIN_STREAMS 1 21 22 #define HVA_MIN_WIDTH 32 23 #define HVA_MAX_WIDTH 1920 24 #define HVA_MIN_HEIGHT 32 25 #define HVA_MAX_HEIGHT 1920 26 27 /* HVA requires a 16x16 pixels alignment for frames */ 28 #define HVA_WIDTH_ALIGNMENT 16 29 #define HVA_HEIGHT_ALIGNMENT 16 30 31 #define HVA_DEFAULT_WIDTH HVA_MIN_WIDTH 32 #define HVA_DEFAULT_HEIGHT HVA_MIN_HEIGHT 33 #define HVA_DEFAULT_FRAME_NUM 1 34 #define HVA_DEFAULT_FRAME_DEN 30 35 36 #define to_type_str(type) (type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? \ 37 "frame" : "stream") 38 39 static inline struct hva_ctx *file_to_ctx(struct file *filp) 40 { 41 return container_of(file_to_v4l2_fh(filp), struct hva_ctx, fh); 42 } 43 44 /* registry of available encoders */ 45 static const struct hva_enc *hva_encoders[] = { 46 &nv12h264enc, 47 &nv21h264enc, 48 }; 49 50 static inline int frame_size(u32 w, u32 h, u32 fmt) 51 { 52 switch (fmt) { 53 case V4L2_PIX_FMT_NV12: 54 case V4L2_PIX_FMT_NV21: 55 return (w * h * 3) / 2; 56 default: 57 return 0; 58 } 59 } 60 61 static inline int frame_stride(u32 w, u32 fmt) 62 { 63 switch (fmt) { 64 case V4L2_PIX_FMT_NV12: 65 case V4L2_PIX_FMT_NV21: 66 return w; 67 default: 68 return 0; 69 } 70 } 71 72 static inline int frame_alignment(u32 fmt) 73 { 74 switch (fmt) { 75 case V4L2_PIX_FMT_NV12: 76 case V4L2_PIX_FMT_NV21: 77 /* multiple of 2 */ 78 return 2; 79 default: 80 return 1; 81 } 82 } 83 84 static inline int estimated_stream_size(u32 w, u32 h) 85 { 86 /* 87 * HVA only encodes in YUV420 format, whatever the frame format. 88 * A compression ratio of 2 is assumed: thus, the maximum size 89 * of a stream is estimated to ((width x height x 3 / 2) / 2) 90 */ 91 return (w * h * 3) / 4; 92 } 93 94 static void set_default_params(struct hva_ctx *ctx) 95 { 96 struct hva_frameinfo *frameinfo = &ctx->frameinfo; 97 struct hva_streaminfo *streaminfo = &ctx->streaminfo; 98 99 frameinfo->pixelformat = V4L2_PIX_FMT_NV12; 100 frameinfo->width = HVA_DEFAULT_WIDTH; 101 frameinfo->height = HVA_DEFAULT_HEIGHT; 102 frameinfo->aligned_width = ALIGN(frameinfo->width, 103 HVA_WIDTH_ALIGNMENT); 104 frameinfo->aligned_height = ALIGN(frameinfo->height, 105 HVA_HEIGHT_ALIGNMENT); 106 frameinfo->size = frame_size(frameinfo->aligned_width, 107 frameinfo->aligned_height, 108 frameinfo->pixelformat); 109 110 streaminfo->streamformat = V4L2_PIX_FMT_H264; 111 streaminfo->width = HVA_DEFAULT_WIDTH; 112 streaminfo->height = HVA_DEFAULT_HEIGHT; 113 114 ctx->colorspace = V4L2_COLORSPACE_REC709; 115 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT; 116 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 117 ctx->quantization = V4L2_QUANTIZATION_DEFAULT; 118 119 ctx->max_stream_size = estimated_stream_size(streaminfo->width, 120 streaminfo->height); 121 } 122 123 static const struct hva_enc *hva_find_encoder(struct hva_ctx *ctx, 124 u32 pixelformat, 125 u32 streamformat) 126 { 127 struct hva_dev *hva = ctx_to_hdev(ctx); 128 const struct hva_enc *enc; 129 unsigned int i; 130 131 for (i = 0; i < hva->nb_of_encoders; i++) { 132 enc = hva->encoders[i]; 133 if ((enc->pixelformat == pixelformat) && 134 (enc->streamformat == streamformat)) 135 return enc; 136 } 137 138 return NULL; 139 } 140 141 static void register_format(u32 format, u32 formats[], u32 *nb_of_formats) 142 { 143 u32 i; 144 bool found = false; 145 146 for (i = 0; i < *nb_of_formats; i++) { 147 if (format == formats[i]) { 148 found = true; 149 break; 150 } 151 } 152 153 if (!found) 154 formats[(*nb_of_formats)++] = format; 155 } 156 157 static void register_formats(struct hva_dev *hva) 158 { 159 unsigned int i; 160 161 for (i = 0; i < hva->nb_of_encoders; i++) { 162 register_format(hva->encoders[i]->pixelformat, 163 hva->pixelformats, 164 &hva->nb_of_pixelformats); 165 166 register_format(hva->encoders[i]->streamformat, 167 hva->streamformats, 168 &hva->nb_of_streamformats); 169 } 170 } 171 172 static void register_encoders(struct hva_dev *hva) 173 { 174 struct device *dev = hva_to_dev(hva); 175 unsigned int i; 176 177 for (i = 0; i < ARRAY_SIZE(hva_encoders); i++) { 178 if (hva->nb_of_encoders >= HVA_MAX_ENCODERS) { 179 dev_dbg(dev, 180 "%s failed to register %s encoder (%d maximum reached)\n", 181 HVA_PREFIX, hva_encoders[i]->name, 182 HVA_MAX_ENCODERS); 183 return; 184 } 185 186 hva->encoders[hva->nb_of_encoders++] = hva_encoders[i]; 187 dev_info(dev, "%s %s encoder registered\n", HVA_PREFIX, 188 hva_encoders[i]->name); 189 } 190 } 191 192 static int hva_open_encoder(struct hva_ctx *ctx, u32 streamformat, 193 u32 pixelformat, struct hva_enc **penc) 194 { 195 struct hva_dev *hva = ctx_to_hdev(ctx); 196 struct device *dev = ctx_to_dev(ctx); 197 struct hva_enc *enc; 198 int ret; 199 200 /* find an encoder which can deal with these formats */ 201 enc = (struct hva_enc *)hva_find_encoder(ctx, pixelformat, 202 streamformat); 203 if (!enc) { 204 dev_err(dev, "%s no encoder found matching %4.4s => %4.4s\n", 205 ctx->name, (char *)&pixelformat, (char *)&streamformat); 206 return -EINVAL; 207 } 208 209 dev_dbg(dev, "%s one encoder matching %4.4s => %4.4s\n", 210 ctx->name, (char *)&pixelformat, (char *)&streamformat); 211 212 /* update instance name */ 213 snprintf(ctx->name, sizeof(ctx->name), "[%3d:%4.4s]", 214 hva->instance_id, (char *)&streamformat); 215 216 /* open encoder instance */ 217 ret = enc->open(ctx); 218 if (ret) { 219 dev_err(dev, "%s failed to open encoder instance (%d)\n", 220 ctx->name, ret); 221 return ret; 222 } 223 224 dev_dbg(dev, "%s %s encoder opened\n", ctx->name, enc->name); 225 226 *penc = enc; 227 228 return ret; 229 } 230 231 static void hva_dbg_summary(struct hva_ctx *ctx) 232 { 233 struct device *dev = ctx_to_dev(ctx); 234 struct hva_streaminfo *stream = &ctx->streaminfo; 235 struct hva_frameinfo *frame = &ctx->frameinfo; 236 237 if (!(ctx->flags & HVA_FLAG_STREAMINFO)) 238 return; 239 240 dev_dbg(dev, "%s %4.4s %dx%d > %4.4s %dx%d %s %s: %d frames encoded, %d system errors, %d encoding errors, %d frame errors\n", 241 ctx->name, 242 (char *)&frame->pixelformat, 243 frame->aligned_width, frame->aligned_height, 244 (char *)&stream->streamformat, 245 stream->width, stream->height, 246 stream->profile, stream->level, 247 ctx->encoded_frames, 248 ctx->sys_errors, 249 ctx->encode_errors, 250 ctx->frame_errors); 251 } 252 253 /* 254 * V4L2 ioctl operations 255 */ 256 257 static int hva_querycap(struct file *file, void *priv, 258 struct v4l2_capability *cap) 259 { 260 struct hva_ctx *ctx = file_to_ctx(file); 261 struct hva_dev *hva = ctx_to_hdev(ctx); 262 263 strscpy(cap->driver, HVA_NAME, sizeof(cap->driver)); 264 strscpy(cap->card, hva->vdev->name, sizeof(cap->card)); 265 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", 266 hva->pdev->name); 267 268 return 0; 269 } 270 271 static int hva_enum_fmt_stream(struct file *file, void *priv, 272 struct v4l2_fmtdesc *f) 273 { 274 struct hva_ctx *ctx = file_to_ctx(file); 275 struct hva_dev *hva = ctx_to_hdev(ctx); 276 277 if (unlikely(f->index >= hva->nb_of_streamformats)) 278 return -EINVAL; 279 280 f->pixelformat = hva->streamformats[f->index]; 281 282 return 0; 283 } 284 285 static int hva_enum_fmt_frame(struct file *file, void *priv, 286 struct v4l2_fmtdesc *f) 287 { 288 struct hva_ctx *ctx = file_to_ctx(file); 289 struct hva_dev *hva = ctx_to_hdev(ctx); 290 291 if (unlikely(f->index >= hva->nb_of_pixelformats)) 292 return -EINVAL; 293 294 f->pixelformat = hva->pixelformats[f->index]; 295 296 return 0; 297 } 298 299 static int hva_g_fmt_stream(struct file *file, void *fh, struct v4l2_format *f) 300 { 301 struct hva_ctx *ctx = file_to_ctx(file); 302 struct hva_streaminfo *streaminfo = &ctx->streaminfo; 303 304 f->fmt.pix.width = streaminfo->width; 305 f->fmt.pix.height = streaminfo->height; 306 f->fmt.pix.field = V4L2_FIELD_NONE; 307 f->fmt.pix.colorspace = ctx->colorspace; 308 f->fmt.pix.xfer_func = ctx->xfer_func; 309 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 310 f->fmt.pix.quantization = ctx->quantization; 311 f->fmt.pix.pixelformat = streaminfo->streamformat; 312 f->fmt.pix.bytesperline = 0; 313 f->fmt.pix.sizeimage = ctx->max_stream_size; 314 315 return 0; 316 } 317 318 static int hva_g_fmt_frame(struct file *file, void *fh, struct v4l2_format *f) 319 { 320 struct hva_ctx *ctx = file_to_ctx(file); 321 struct hva_frameinfo *frameinfo = &ctx->frameinfo; 322 323 f->fmt.pix.width = frameinfo->width; 324 f->fmt.pix.height = frameinfo->height; 325 f->fmt.pix.field = V4L2_FIELD_NONE; 326 f->fmt.pix.colorspace = ctx->colorspace; 327 f->fmt.pix.xfer_func = ctx->xfer_func; 328 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 329 f->fmt.pix.quantization = ctx->quantization; 330 f->fmt.pix.pixelformat = frameinfo->pixelformat; 331 f->fmt.pix.bytesperline = frame_stride(frameinfo->aligned_width, 332 frameinfo->pixelformat); 333 f->fmt.pix.sizeimage = frameinfo->size; 334 335 return 0; 336 } 337 338 static int hva_try_fmt_stream(struct file *file, void *priv, 339 struct v4l2_format *f) 340 { 341 struct hva_ctx *ctx = file_to_ctx(file); 342 struct device *dev = ctx_to_dev(ctx); 343 struct v4l2_pix_format *pix = &f->fmt.pix; 344 u32 streamformat = pix->pixelformat; 345 const struct hva_enc *enc; 346 u32 width, height; 347 u32 stream_size; 348 349 enc = hva_find_encoder(ctx, ctx->frameinfo.pixelformat, streamformat); 350 if (!enc) { 351 dev_dbg(dev, 352 "%s V4L2 TRY_FMT (CAPTURE): unsupported format %.4s\n", 353 ctx->name, (char *)&pix->pixelformat); 354 return -EINVAL; 355 } 356 357 width = pix->width; 358 height = pix->height; 359 if (ctx->flags & HVA_FLAG_FRAMEINFO) { 360 /* 361 * if the frame resolution is already fixed, only allow the 362 * same stream resolution 363 */ 364 pix->width = ctx->frameinfo.width; 365 pix->height = ctx->frameinfo.height; 366 if ((pix->width != width) || (pix->height != height)) 367 dev_dbg(dev, 368 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit frame resolution\n", 369 ctx->name, width, height, 370 pix->width, pix->height); 371 } else { 372 /* adjust width & height */ 373 v4l_bound_align_image(&pix->width, 374 HVA_MIN_WIDTH, enc->max_width, 375 0, 376 &pix->height, 377 HVA_MIN_HEIGHT, enc->max_height, 378 0, 379 0); 380 381 if ((pix->width != width) || (pix->height != height)) 382 dev_dbg(dev, 383 "%s V4L2 TRY_FMT (CAPTURE): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n", 384 ctx->name, width, height, 385 pix->width, pix->height); 386 } 387 388 stream_size = estimated_stream_size(pix->width, pix->height); 389 if (pix->sizeimage < stream_size) 390 pix->sizeimage = stream_size; 391 392 pix->bytesperline = 0; 393 pix->colorspace = ctx->colorspace; 394 pix->xfer_func = ctx->xfer_func; 395 pix->ycbcr_enc = ctx->ycbcr_enc; 396 pix->quantization = ctx->quantization; 397 pix->field = V4L2_FIELD_NONE; 398 399 return 0; 400 } 401 402 static int hva_try_fmt_frame(struct file *file, void *priv, 403 struct v4l2_format *f) 404 { 405 struct hva_ctx *ctx = file_to_ctx(file); 406 struct device *dev = ctx_to_dev(ctx); 407 struct v4l2_pix_format *pix = &f->fmt.pix; 408 u32 pixelformat = pix->pixelformat; 409 const struct hva_enc *enc; 410 u32 width, height; 411 412 enc = hva_find_encoder(ctx, pixelformat, ctx->streaminfo.streamformat); 413 if (!enc) { 414 dev_dbg(dev, 415 "%s V4L2 TRY_FMT (OUTPUT): unsupported format %.4s\n", 416 ctx->name, (char *)&pixelformat); 417 return -EINVAL; 418 } 419 420 /* adjust width & height */ 421 width = pix->width; 422 height = pix->height; 423 v4l_bound_align_image(&pix->width, 424 HVA_MIN_WIDTH, HVA_MAX_WIDTH, 425 frame_alignment(pixelformat) - 1, 426 &pix->height, 427 HVA_MIN_HEIGHT, HVA_MAX_HEIGHT, 428 frame_alignment(pixelformat) - 1, 429 0); 430 431 if ((pix->width != width) || (pix->height != height)) 432 dev_dbg(dev, 433 "%s V4L2 TRY_FMT (OUTPUT): resolution updated %dx%d -> %dx%d to fit min/max/alignment\n", 434 ctx->name, width, height, pix->width, pix->height); 435 436 width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT); 437 height = ALIGN(pix->height, HVA_HEIGHT_ALIGNMENT); 438 439 if (!pix->colorspace) { 440 pix->colorspace = V4L2_COLORSPACE_REC709; 441 pix->xfer_func = V4L2_XFER_FUNC_DEFAULT; 442 pix->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 443 pix->quantization = V4L2_QUANTIZATION_DEFAULT; 444 } 445 446 pix->bytesperline = frame_stride(width, pixelformat); 447 pix->sizeimage = frame_size(width, height, pixelformat); 448 pix->field = V4L2_FIELD_NONE; 449 450 return 0; 451 } 452 453 static int hva_s_fmt_stream(struct file *file, void *fh, struct v4l2_format *f) 454 { 455 struct hva_ctx *ctx = file_to_ctx(file); 456 struct device *dev = ctx_to_dev(ctx); 457 struct vb2_queue *vq; 458 int ret; 459 460 ret = hva_try_fmt_stream(file, fh, f); 461 if (ret) { 462 dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): unsupported format %.4s\n", 463 ctx->name, (char *)&f->fmt.pix.pixelformat); 464 return ret; 465 } 466 467 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 468 if (vb2_is_streaming(vq)) { 469 dev_dbg(dev, "%s V4L2 S_FMT (CAPTURE): queue busy\n", 470 ctx->name); 471 return -EBUSY; 472 } 473 474 ctx->max_stream_size = f->fmt.pix.sizeimage; 475 ctx->streaminfo.width = f->fmt.pix.width; 476 ctx->streaminfo.height = f->fmt.pix.height; 477 ctx->streaminfo.streamformat = f->fmt.pix.pixelformat; 478 ctx->flags |= HVA_FLAG_STREAMINFO; 479 480 return 0; 481 } 482 483 static int hva_s_fmt_frame(struct file *file, void *fh, struct v4l2_format *f) 484 { 485 struct hva_ctx *ctx = file_to_ctx(file); 486 struct device *dev = ctx_to_dev(ctx); 487 struct v4l2_pix_format *pix = &f->fmt.pix; 488 struct vb2_queue *vq; 489 int ret; 490 491 ret = hva_try_fmt_frame(file, fh, f); 492 if (ret) { 493 dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): unsupported format %.4s\n", 494 ctx->name, (char *)&pix->pixelformat); 495 return ret; 496 } 497 498 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 499 if (vb2_is_streaming(vq)) { 500 dev_dbg(dev, "%s V4L2 S_FMT (OUTPUT): queue busy\n", ctx->name); 501 return -EBUSY; 502 } 503 504 ctx->colorspace = pix->colorspace; 505 ctx->xfer_func = pix->xfer_func; 506 ctx->ycbcr_enc = pix->ycbcr_enc; 507 ctx->quantization = pix->quantization; 508 509 ctx->frameinfo.aligned_width = ALIGN(pix->width, HVA_WIDTH_ALIGNMENT); 510 ctx->frameinfo.aligned_height = ALIGN(pix->height, 511 HVA_HEIGHT_ALIGNMENT); 512 ctx->frameinfo.size = pix->sizeimage; 513 ctx->frameinfo.pixelformat = pix->pixelformat; 514 ctx->frameinfo.width = pix->width; 515 ctx->frameinfo.height = pix->height; 516 ctx->flags |= HVA_FLAG_FRAMEINFO; 517 518 return 0; 519 } 520 521 static int hva_g_parm(struct file *file, void *fh, struct v4l2_streamparm *sp) 522 { 523 struct hva_ctx *ctx = file_to_ctx(file); 524 struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame; 525 526 if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 527 return -EINVAL; 528 529 sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 530 sp->parm.output.timeperframe.numerator = time_per_frame->numerator; 531 sp->parm.output.timeperframe.denominator = 532 time_per_frame->denominator; 533 534 return 0; 535 } 536 537 static int hva_s_parm(struct file *file, void *fh, struct v4l2_streamparm *sp) 538 { 539 struct hva_ctx *ctx = file_to_ctx(file); 540 struct v4l2_fract *time_per_frame = &ctx->ctrls.time_per_frame; 541 542 if (sp->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 543 return -EINVAL; 544 545 if (!sp->parm.output.timeperframe.numerator || 546 !sp->parm.output.timeperframe.denominator) 547 return hva_g_parm(file, fh, sp); 548 549 sp->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 550 time_per_frame->numerator = sp->parm.output.timeperframe.numerator; 551 time_per_frame->denominator = 552 sp->parm.output.timeperframe.denominator; 553 554 return 0; 555 } 556 557 static int hva_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) 558 { 559 struct hva_ctx *ctx = file_to_ctx(file); 560 struct device *dev = ctx_to_dev(ctx); 561 562 if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { 563 /* 564 * depending on the targeted compressed video format, the 565 * capture buffer might contain headers (e.g. H.264 SPS/PPS) 566 * filled in by the driver client; the size of these data is 567 * copied from the bytesused field of the V4L2 buffer in the 568 * payload field of the hva stream buffer 569 */ 570 struct vb2_queue *vq; 571 struct hva_stream *stream; 572 struct vb2_buffer *vb2_buf; 573 574 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, buf->type); 575 vb2_buf = vb2_get_buffer(vq, buf->index); 576 if (!vb2_buf) { 577 dev_dbg(dev, "%s buffer index %d not found\n", ctx->name, buf->index); 578 return -EINVAL; 579 } 580 stream = to_hva_stream(to_vb2_v4l2_buffer(vb2_buf)); 581 stream->bytesused = buf->bytesused; 582 } 583 584 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf); 585 } 586 587 /* V4L2 ioctl ops */ 588 static const struct v4l2_ioctl_ops hva_ioctl_ops = { 589 .vidioc_querycap = hva_querycap, 590 .vidioc_enum_fmt_vid_cap = hva_enum_fmt_stream, 591 .vidioc_enum_fmt_vid_out = hva_enum_fmt_frame, 592 .vidioc_g_fmt_vid_cap = hva_g_fmt_stream, 593 .vidioc_g_fmt_vid_out = hva_g_fmt_frame, 594 .vidioc_try_fmt_vid_cap = hva_try_fmt_stream, 595 .vidioc_try_fmt_vid_out = hva_try_fmt_frame, 596 .vidioc_s_fmt_vid_cap = hva_s_fmt_stream, 597 .vidioc_s_fmt_vid_out = hva_s_fmt_frame, 598 .vidioc_g_parm = hva_g_parm, 599 .vidioc_s_parm = hva_s_parm, 600 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 601 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 602 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 603 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 604 .vidioc_qbuf = hva_qbuf, 605 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 606 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 607 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 608 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 609 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 610 }; 611 612 /* 613 * V4L2 control operations 614 */ 615 616 static int hva_s_ctrl(struct v4l2_ctrl *ctrl) 617 { 618 struct hva_ctx *ctx = container_of(ctrl->handler, struct hva_ctx, 619 ctrl_handler); 620 struct device *dev = ctx_to_dev(ctx); 621 622 dev_dbg(dev, "%s S_CTRL: id = %d, val = %d\n", ctx->name, 623 ctrl->id, ctrl->val); 624 625 switch (ctrl->id) { 626 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: 627 ctx->ctrls.bitrate_mode = ctrl->val; 628 break; 629 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: 630 ctx->ctrls.gop_size = ctrl->val; 631 break; 632 case V4L2_CID_MPEG_VIDEO_BITRATE: 633 ctx->ctrls.bitrate = ctrl->val; 634 break; 635 case V4L2_CID_MPEG_VIDEO_ASPECT: 636 ctx->ctrls.aspect = ctrl->val; 637 break; 638 case V4L2_CID_MPEG_VIDEO_H264_PROFILE: 639 ctx->ctrls.profile = ctrl->val; 640 snprintf(ctx->streaminfo.profile, 641 sizeof(ctx->streaminfo.profile), 642 "%s profile", 643 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]); 644 break; 645 case V4L2_CID_MPEG_VIDEO_H264_LEVEL: 646 ctx->ctrls.level = ctrl->val; 647 snprintf(ctx->streaminfo.level, 648 sizeof(ctx->streaminfo.level), 649 "level %s", 650 v4l2_ctrl_get_menu(ctrl->id)[ctrl->val]); 651 break; 652 case V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE: 653 ctx->ctrls.entropy_mode = ctrl->val; 654 break; 655 case V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE: 656 ctx->ctrls.cpb_size = ctrl->val; 657 break; 658 case V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM: 659 ctx->ctrls.dct8x8 = ctrl->val; 660 break; 661 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: 662 ctx->ctrls.qpmin = ctrl->val; 663 break; 664 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: 665 ctx->ctrls.qpmax = ctrl->val; 666 break; 667 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE: 668 ctx->ctrls.vui_sar = ctrl->val; 669 break; 670 case V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC: 671 ctx->ctrls.vui_sar_idc = ctrl->val; 672 break; 673 case V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING: 674 ctx->ctrls.sei_fp = ctrl->val; 675 break; 676 case V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE: 677 ctx->ctrls.sei_fp_type = ctrl->val; 678 break; 679 default: 680 dev_dbg(dev, "%s S_CTRL: invalid control (id = %d)\n", 681 ctx->name, ctrl->id); 682 return -EINVAL; 683 } 684 685 return 0; 686 } 687 688 /* V4L2 control ops */ 689 static const struct v4l2_ctrl_ops hva_ctrl_ops = { 690 .s_ctrl = hva_s_ctrl, 691 }; 692 693 static int hva_ctrls_setup(struct hva_ctx *ctx) 694 { 695 struct device *dev = ctx_to_dev(ctx); 696 u64 mask; 697 enum v4l2_mpeg_video_h264_sei_fp_arrangement_type sei_fp_type = 698 V4L2_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE_TOP_BOTTOM; 699 700 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 15); 701 702 v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops, 703 V4L2_CID_MPEG_VIDEO_BITRATE_MODE, 704 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 705 0, 706 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR); 707 708 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 709 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 710 1, 60, 1, 16); 711 712 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 713 V4L2_CID_MPEG_VIDEO_BITRATE, 714 1000, 60000000, 1000, 20000000); 715 716 mask = ~(1 << V4L2_MPEG_VIDEO_ASPECT_1x1); 717 v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops, 718 V4L2_CID_MPEG_VIDEO_ASPECT, 719 V4L2_MPEG_VIDEO_ASPECT_1x1, 720 mask, 721 V4L2_MPEG_VIDEO_ASPECT_1x1); 722 723 mask = ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) | 724 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) | 725 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH) | 726 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH)); 727 v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops, 728 V4L2_CID_MPEG_VIDEO_H264_PROFILE, 729 V4L2_MPEG_VIDEO_H264_PROFILE_STEREO_HIGH, 730 mask, 731 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH); 732 733 v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops, 734 V4L2_CID_MPEG_VIDEO_H264_LEVEL, 735 V4L2_MPEG_VIDEO_H264_LEVEL_4_2, 736 0, 737 V4L2_MPEG_VIDEO_H264_LEVEL_4_0); 738 739 v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops, 740 V4L2_CID_MPEG_VIDEO_H264_ENTROPY_MODE, 741 V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CABAC, 742 0, 743 V4L2_MPEG_VIDEO_H264_ENTROPY_MODE_CAVLC); 744 745 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 746 V4L2_CID_MPEG_VIDEO_H264_CPB_SIZE, 747 1, 10000, 1, 3000); 748 749 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 750 V4L2_CID_MPEG_VIDEO_H264_8X8_TRANSFORM, 751 0, 1, 1, 0); 752 753 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 754 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 755 0, 51, 1, 5); 756 757 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 758 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 759 0, 51, 1, 51); 760 761 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 762 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_ENABLE, 763 0, 1, 1, 1); 764 765 mask = ~(1 << V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1); 766 v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops, 767 V4L2_CID_MPEG_VIDEO_H264_VUI_SAR_IDC, 768 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1, 769 mask, 770 V4L2_MPEG_VIDEO_H264_VUI_SAR_IDC_1x1); 771 772 v4l2_ctrl_new_std(&ctx->ctrl_handler, &hva_ctrl_ops, 773 V4L2_CID_MPEG_VIDEO_H264_SEI_FRAME_PACKING, 774 0, 1, 1, 0); 775 776 mask = ~(1 << sei_fp_type); 777 v4l2_ctrl_new_std_menu(&ctx->ctrl_handler, &hva_ctrl_ops, 778 V4L2_CID_MPEG_VIDEO_H264_SEI_FP_ARRANGEMENT_TYPE, 779 sei_fp_type, 780 mask, 781 sei_fp_type); 782 783 if (ctx->ctrl_handler.error) { 784 int err = ctx->ctrl_handler.error; 785 786 dev_dbg(dev, "%s controls setup failed (%d)\n", 787 ctx->name, err); 788 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 789 return err; 790 } 791 792 v4l2_ctrl_handler_setup(&ctx->ctrl_handler); 793 794 /* set default time per frame */ 795 ctx->ctrls.time_per_frame.numerator = HVA_DEFAULT_FRAME_NUM; 796 ctx->ctrls.time_per_frame.denominator = HVA_DEFAULT_FRAME_DEN; 797 798 return 0; 799 } 800 801 /* 802 * mem-to-mem operations 803 */ 804 805 static void hva_run_work(struct work_struct *work) 806 { 807 struct hva_ctx *ctx = container_of(work, struct hva_ctx, run_work); 808 struct vb2_v4l2_buffer *src_buf, *dst_buf; 809 const struct hva_enc *enc = ctx->enc; 810 struct hva_frame *frame; 811 struct hva_stream *stream; 812 int ret; 813 814 /* protect instance against reentrancy */ 815 mutex_lock(&ctx->lock); 816 817 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS 818 hva_dbg_perf_begin(ctx); 819 #endif 820 821 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 822 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 823 824 frame = to_hva_frame(src_buf); 825 stream = to_hva_stream(dst_buf); 826 frame->vbuf.sequence = ctx->frame_num++; 827 828 ret = enc->encode(ctx, frame, stream); 829 830 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, stream->bytesused); 831 if (ret) { 832 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); 833 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); 834 } else { 835 /* propagate frame timestamp */ 836 dst_buf->vb2_buf.timestamp = src_buf->vb2_buf.timestamp; 837 dst_buf->field = V4L2_FIELD_NONE; 838 dst_buf->sequence = ctx->stream_num - 1; 839 840 ctx->encoded_frames++; 841 842 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS 843 hva_dbg_perf_end(ctx, stream); 844 #endif 845 846 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); 847 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); 848 } 849 850 mutex_unlock(&ctx->lock); 851 852 v4l2_m2m_job_finish(ctx->hva_dev->m2m_dev, ctx->fh.m2m_ctx); 853 } 854 855 static void hva_device_run(void *priv) 856 { 857 struct hva_ctx *ctx = priv; 858 struct hva_dev *hva = ctx_to_hdev(ctx); 859 860 queue_work(hva->work_queue, &ctx->run_work); 861 } 862 863 static void hva_job_abort(void *priv) 864 { 865 struct hva_ctx *ctx = priv; 866 struct device *dev = ctx_to_dev(ctx); 867 868 dev_dbg(dev, "%s aborting job\n", ctx->name); 869 870 ctx->aborting = true; 871 } 872 873 static int hva_job_ready(void *priv) 874 { 875 struct hva_ctx *ctx = priv; 876 struct device *dev = ctx_to_dev(ctx); 877 878 if (!v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx)) { 879 dev_dbg(dev, "%s job not ready: no frame buffers\n", 880 ctx->name); 881 return 0; 882 } 883 884 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) { 885 dev_dbg(dev, "%s job not ready: no stream buffers\n", 886 ctx->name); 887 return 0; 888 } 889 890 if (ctx->aborting) { 891 dev_dbg(dev, "%s job not ready: aborting\n", ctx->name); 892 return 0; 893 } 894 895 return 1; 896 } 897 898 /* mem-to-mem ops */ 899 static const struct v4l2_m2m_ops hva_m2m_ops = { 900 .device_run = hva_device_run, 901 .job_abort = hva_job_abort, 902 .job_ready = hva_job_ready, 903 }; 904 905 /* 906 * VB2 queue operations 907 */ 908 909 static int hva_queue_setup(struct vb2_queue *vq, 910 unsigned int *num_buffers, unsigned int *num_planes, 911 unsigned int sizes[], struct device *alloc_devs[]) 912 { 913 struct hva_ctx *ctx = vb2_get_drv_priv(vq); 914 struct device *dev = ctx_to_dev(ctx); 915 unsigned int size; 916 917 dev_dbg(dev, "%s %s queue setup: num_buffers %d\n", ctx->name, 918 to_type_str(vq->type), *num_buffers); 919 920 size = vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ? 921 ctx->frameinfo.size : ctx->max_stream_size; 922 923 if (*num_planes) 924 return sizes[0] < size ? -EINVAL : 0; 925 926 /* only one plane supported */ 927 *num_planes = 1; 928 sizes[0] = size; 929 930 return 0; 931 } 932 933 static int hva_buf_prepare(struct vb2_buffer *vb) 934 { 935 struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 936 struct device *dev = ctx_to_dev(ctx); 937 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 938 939 if (vb->vb2_queue->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 940 struct hva_frame *frame = to_hva_frame(vbuf); 941 942 if (vbuf->field == V4L2_FIELD_ANY) 943 vbuf->field = V4L2_FIELD_NONE; 944 if (vbuf->field != V4L2_FIELD_NONE) { 945 dev_dbg(dev, 946 "%s frame[%d] prepare: %d field not supported\n", 947 ctx->name, vb->index, vbuf->field); 948 return -EINVAL; 949 } 950 951 if (!frame->prepared) { 952 /* get memory addresses */ 953 frame->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0); 954 frame->paddr = vb2_dma_contig_plane_dma_addr( 955 &vbuf->vb2_buf, 0); 956 frame->info = ctx->frameinfo; 957 frame->prepared = true; 958 959 dev_dbg(dev, 960 "%s frame[%d] prepared; virt=%p, phy=%pad\n", 961 ctx->name, vb->index, 962 frame->vaddr, &frame->paddr); 963 } 964 } else { 965 struct hva_stream *stream = to_hva_stream(vbuf); 966 967 if (!stream->prepared) { 968 /* get memory addresses */ 969 stream->vaddr = vb2_plane_vaddr(&vbuf->vb2_buf, 0); 970 stream->paddr = vb2_dma_contig_plane_dma_addr( 971 &vbuf->vb2_buf, 0); 972 stream->size = vb2_plane_size(&vbuf->vb2_buf, 0); 973 stream->prepared = true; 974 975 dev_dbg(dev, 976 "%s stream[%d] prepared; virt=%p, phy=%pad\n", 977 ctx->name, vb->index, 978 stream->vaddr, &stream->paddr); 979 } 980 } 981 982 return 0; 983 } 984 985 static void hva_buf_queue(struct vb2_buffer *vb) 986 { 987 struct hva_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 988 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 989 990 if (ctx->fh.m2m_ctx) 991 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 992 } 993 994 static int hva_start_streaming(struct vb2_queue *vq, unsigned int count) 995 { 996 struct hva_ctx *ctx = vb2_get_drv_priv(vq); 997 struct hva_dev *hva = ctx_to_hdev(ctx); 998 struct device *dev = ctx_to_dev(ctx); 999 struct vb2_v4l2_buffer *vbuf; 1000 int ret; 1001 unsigned int i; 1002 bool found = false; 1003 1004 dev_dbg(dev, "%s %s start streaming\n", ctx->name, 1005 to_type_str(vq->type)); 1006 1007 /* open encoder when both start_streaming have been called */ 1008 if (V4L2_TYPE_IS_OUTPUT(vq->type)) { 1009 if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->cap_q_ctx.q)) 1010 return 0; 1011 } else { 1012 if (!vb2_start_streaming_called(&ctx->fh.m2m_ctx->out_q_ctx.q)) 1013 return 0; 1014 } 1015 1016 /* store the instance context in the instances array */ 1017 for (i = 0; i < HVA_MAX_INSTANCES; i++) { 1018 if (!hva->instances[i]) { 1019 hva->instances[i] = ctx; 1020 /* save the context identifier in the context */ 1021 ctx->id = i; 1022 found = true; 1023 break; 1024 } 1025 } 1026 1027 if (!found) { 1028 dev_err(dev, "%s maximum instances reached\n", ctx->name); 1029 ret = -ENOMEM; 1030 goto err; 1031 } 1032 1033 hva->nb_of_instances++; 1034 1035 if (!ctx->enc) { 1036 ret = hva_open_encoder(ctx, 1037 ctx->streaminfo.streamformat, 1038 ctx->frameinfo.pixelformat, 1039 &ctx->enc); 1040 if (ret < 0) 1041 goto err_ctx; 1042 } 1043 1044 return 0; 1045 1046 err_ctx: 1047 hva->instances[ctx->id] = NULL; 1048 hva->nb_of_instances--; 1049 err: 1050 if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1051 /* return of all pending buffers to vb2 (in queued state) */ 1052 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) 1053 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED); 1054 } else { 1055 /* return of all pending buffers to vb2 (in queued state) */ 1056 while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) 1057 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_QUEUED); 1058 } 1059 1060 ctx->sys_errors++; 1061 1062 return ret; 1063 } 1064 1065 static void hva_stop_streaming(struct vb2_queue *vq) 1066 { 1067 struct hva_ctx *ctx = vb2_get_drv_priv(vq); 1068 struct hva_dev *hva = ctx_to_hdev(ctx); 1069 struct device *dev = ctx_to_dev(ctx); 1070 const struct hva_enc *enc = ctx->enc; 1071 struct vb2_v4l2_buffer *vbuf; 1072 1073 dev_dbg(dev, "%s %s stop streaming\n", ctx->name, 1074 to_type_str(vq->type)); 1075 1076 if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1077 /* return of all pending buffers to vb2 (in error state) */ 1078 ctx->frame_num = 0; 1079 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) 1080 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 1081 } else { 1082 /* return of all pending buffers to vb2 (in error state) */ 1083 ctx->stream_num = 0; 1084 while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) 1085 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); 1086 } 1087 1088 if ((V4L2_TYPE_IS_OUTPUT(vq->type) && 1089 vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)) || 1090 (V4L2_TYPE_IS_CAPTURE(vq->type) && 1091 vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))) { 1092 dev_dbg(dev, "%s %s out=%d cap=%d\n", 1093 ctx->name, to_type_str(vq->type), 1094 vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q), 1095 vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q)); 1096 return; 1097 } 1098 1099 /* close encoder when both stop_streaming have been called */ 1100 if (enc) { 1101 dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name); 1102 enc->close(ctx); 1103 ctx->enc = NULL; 1104 1105 /* clear instance context in instances array */ 1106 hva->instances[ctx->id] = NULL; 1107 hva->nb_of_instances--; 1108 } 1109 1110 ctx->aborting = false; 1111 } 1112 1113 /* VB2 queue ops */ 1114 static const struct vb2_ops hva_qops = { 1115 .queue_setup = hva_queue_setup, 1116 .buf_prepare = hva_buf_prepare, 1117 .buf_queue = hva_buf_queue, 1118 .start_streaming = hva_start_streaming, 1119 .stop_streaming = hva_stop_streaming, 1120 }; 1121 1122 /* 1123 * V4L2 file operations 1124 */ 1125 1126 static int queue_init(struct hva_ctx *ctx, struct vb2_queue *vq) 1127 { 1128 vq->io_modes = VB2_MMAP | VB2_DMABUF; 1129 vq->drv_priv = ctx; 1130 vq->ops = &hva_qops; 1131 vq->mem_ops = &vb2_dma_contig_memops; 1132 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1133 vq->lock = &ctx->hva_dev->lock; 1134 1135 return vb2_queue_init(vq); 1136 } 1137 1138 static int hva_queue_init(void *priv, struct vb2_queue *src_vq, 1139 struct vb2_queue *dst_vq) 1140 { 1141 struct hva_ctx *ctx = priv; 1142 int ret; 1143 1144 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 1145 src_vq->buf_struct_size = sizeof(struct hva_frame); 1146 src_vq->min_queued_buffers = MIN_FRAMES; 1147 src_vq->dev = ctx->hva_dev->dev; 1148 1149 ret = queue_init(ctx, src_vq); 1150 if (ret) 1151 return ret; 1152 1153 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1154 dst_vq->buf_struct_size = sizeof(struct hva_stream); 1155 dst_vq->min_queued_buffers = MIN_STREAMS; 1156 dst_vq->dev = ctx->hva_dev->dev; 1157 1158 return queue_init(ctx, dst_vq); 1159 } 1160 1161 static int hva_open(struct file *file) 1162 { 1163 struct hva_dev *hva = video_drvdata(file); 1164 struct device *dev = hva_to_dev(hva); 1165 struct hva_ctx *ctx; 1166 int ret; 1167 1168 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1169 if (!ctx) { 1170 ret = -ENOMEM; 1171 goto out; 1172 } 1173 ctx->hva_dev = hva; 1174 1175 INIT_WORK(&ctx->run_work, hva_run_work); 1176 v4l2_fh_init(&ctx->fh, video_devdata(file)); 1177 v4l2_fh_add(&ctx->fh, file); 1178 1179 ret = hva_ctrls_setup(ctx); 1180 if (ret) { 1181 dev_err(dev, "%s [x:x] failed to setup controls\n", 1182 HVA_PREFIX); 1183 ctx->sys_errors++; 1184 goto err_fh; 1185 } 1186 ctx->fh.ctrl_handler = &ctx->ctrl_handler; 1187 1188 mutex_init(&ctx->lock); 1189 1190 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(hva->m2m_dev, ctx, 1191 &hva_queue_init); 1192 if (IS_ERR(ctx->fh.m2m_ctx)) { 1193 ret = PTR_ERR(ctx->fh.m2m_ctx); 1194 dev_err(dev, "%s failed to initialize m2m context (%d)\n", 1195 HVA_PREFIX, ret); 1196 ctx->sys_errors++; 1197 goto err_ctrls; 1198 } 1199 1200 /* set the instance name */ 1201 mutex_lock(&hva->lock); 1202 hva->instance_id++; 1203 snprintf(ctx->name, sizeof(ctx->name), "[%3d:----]", 1204 hva->instance_id); 1205 mutex_unlock(&hva->lock); 1206 1207 /* default parameters for frame and stream */ 1208 set_default_params(ctx); 1209 1210 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS 1211 hva_dbg_ctx_create(ctx); 1212 #endif 1213 1214 dev_info(dev, "%s encoder instance created\n", ctx->name); 1215 1216 return 0; 1217 1218 err_ctrls: 1219 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 1220 err_fh: 1221 v4l2_fh_del(&ctx->fh, file); 1222 v4l2_fh_exit(&ctx->fh); 1223 kfree(ctx); 1224 out: 1225 return ret; 1226 } 1227 1228 static int hva_release(struct file *file) 1229 { 1230 struct hva_ctx *ctx = file_to_ctx(file); 1231 struct hva_dev *hva = ctx_to_hdev(ctx); 1232 struct device *dev = ctx_to_dev(ctx); 1233 const struct hva_enc *enc = ctx->enc; 1234 1235 if (enc) { 1236 dev_dbg(dev, "%s %s encoder closed\n", ctx->name, enc->name); 1237 enc->close(ctx); 1238 ctx->enc = NULL; 1239 1240 /* clear instance context in instances array */ 1241 hva->instances[ctx->id] = NULL; 1242 hva->nb_of_instances--; 1243 } 1244 1245 /* trace a summary of instance before closing (debug purpose) */ 1246 hva_dbg_summary(ctx); 1247 1248 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 1249 1250 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 1251 1252 v4l2_fh_del(&ctx->fh, file); 1253 v4l2_fh_exit(&ctx->fh); 1254 1255 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS 1256 hva_dbg_ctx_remove(ctx); 1257 #endif 1258 1259 dev_info(dev, "%s encoder instance released\n", ctx->name); 1260 1261 kfree(ctx); 1262 1263 return 0; 1264 } 1265 1266 /* V4L2 file ops */ 1267 static const struct v4l2_file_operations hva_fops = { 1268 .owner = THIS_MODULE, 1269 .open = hva_open, 1270 .release = hva_release, 1271 .unlocked_ioctl = video_ioctl2, 1272 .mmap = v4l2_m2m_fop_mmap, 1273 .poll = v4l2_m2m_fop_poll, 1274 }; 1275 1276 /* 1277 * Platform device operations 1278 */ 1279 1280 static int hva_register_device(struct hva_dev *hva) 1281 { 1282 int ret; 1283 struct video_device *vdev; 1284 struct device *dev; 1285 1286 if (!hva) 1287 return -ENODEV; 1288 dev = hva_to_dev(hva); 1289 1290 hva->m2m_dev = v4l2_m2m_init(&hva_m2m_ops); 1291 if (IS_ERR(hva->m2m_dev)) { 1292 dev_err(dev, "%s failed to initialize v4l2-m2m device\n", 1293 HVA_PREFIX); 1294 ret = PTR_ERR(hva->m2m_dev); 1295 goto err; 1296 } 1297 1298 vdev = video_device_alloc(); 1299 if (!vdev) { 1300 dev_err(dev, "%s failed to allocate video device\n", 1301 HVA_PREFIX); 1302 ret = -ENOMEM; 1303 goto err_m2m_release; 1304 } 1305 1306 vdev->fops = &hva_fops; 1307 vdev->ioctl_ops = &hva_ioctl_ops; 1308 vdev->release = video_device_release; 1309 vdev->lock = &hva->lock; 1310 vdev->vfl_dir = VFL_DIR_M2M; 1311 vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M; 1312 vdev->v4l2_dev = &hva->v4l2_dev; 1313 snprintf(vdev->name, sizeof(vdev->name), "%s%lx", HVA_NAME, 1314 hva->ip_version); 1315 1316 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); 1317 if (ret) { 1318 dev_err(dev, "%s failed to register video device\n", 1319 HVA_PREFIX); 1320 goto err_vdev_release; 1321 } 1322 1323 hva->vdev = vdev; 1324 video_set_drvdata(vdev, hva); 1325 return 0; 1326 1327 err_vdev_release: 1328 video_device_release(vdev); 1329 err_m2m_release: 1330 v4l2_m2m_release(hva->m2m_dev); 1331 err: 1332 return ret; 1333 } 1334 1335 static void hva_unregister_device(struct hva_dev *hva) 1336 { 1337 if (!hva) 1338 return; 1339 1340 if (hva->m2m_dev) 1341 v4l2_m2m_release(hva->m2m_dev); 1342 1343 video_unregister_device(hva->vdev); 1344 } 1345 1346 static int hva_probe(struct platform_device *pdev) 1347 { 1348 struct hva_dev *hva; 1349 struct device *dev = &pdev->dev; 1350 int ret; 1351 1352 hva = devm_kzalloc(dev, sizeof(*hva), GFP_KERNEL); 1353 if (!hva) { 1354 ret = -ENOMEM; 1355 goto err; 1356 } 1357 1358 ret = dma_coerce_mask_and_coherent(dev, DMA_BIT_MASK(32)); 1359 if (ret) 1360 return ret; 1361 1362 hva->dev = dev; 1363 hva->pdev = pdev; 1364 platform_set_drvdata(pdev, hva); 1365 1366 mutex_init(&hva->lock); 1367 1368 /* probe hardware */ 1369 ret = hva_hw_probe(pdev, hva); 1370 if (ret) 1371 goto err; 1372 1373 /* register all available encoders */ 1374 register_encoders(hva); 1375 1376 /* register all supported formats */ 1377 register_formats(hva); 1378 1379 /* register on V4L2 */ 1380 ret = v4l2_device_register(dev, &hva->v4l2_dev); 1381 if (ret) { 1382 dev_err(dev, "%s %s failed to register V4L2 device\n", 1383 HVA_PREFIX, HVA_NAME); 1384 goto err_hw; 1385 } 1386 1387 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS 1388 hva_debugfs_create(hva); 1389 #endif 1390 1391 hva->work_queue = create_workqueue(HVA_NAME); 1392 if (!hva->work_queue) { 1393 dev_err(dev, "%s %s failed to allocate work queue\n", 1394 HVA_PREFIX, HVA_NAME); 1395 ret = -ENOMEM; 1396 goto err_v4l2; 1397 } 1398 1399 /* register device */ 1400 ret = hva_register_device(hva); 1401 if (ret) 1402 goto err_work_queue; 1403 1404 dev_info(dev, "%s %s registered as /dev/video%d\n", HVA_PREFIX, 1405 HVA_NAME, hva->vdev->num); 1406 1407 return 0; 1408 1409 err_work_queue: 1410 destroy_workqueue(hva->work_queue); 1411 err_v4l2: 1412 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS 1413 hva_debugfs_remove(hva); 1414 #endif 1415 v4l2_device_unregister(&hva->v4l2_dev); 1416 err_hw: 1417 hva_hw_remove(hva); 1418 err: 1419 return ret; 1420 } 1421 1422 static void hva_remove(struct platform_device *pdev) 1423 { 1424 struct hva_dev *hva = platform_get_drvdata(pdev); 1425 struct device *dev = hva_to_dev(hva); 1426 1427 hva_unregister_device(hva); 1428 1429 destroy_workqueue(hva->work_queue); 1430 1431 hva_hw_remove(hva); 1432 1433 #ifdef CONFIG_VIDEO_STI_HVA_DEBUGFS 1434 hva_debugfs_remove(hva); 1435 #endif 1436 1437 v4l2_device_unregister(&hva->v4l2_dev); 1438 1439 dev_info(dev, "%s %s removed\n", HVA_PREFIX, pdev->name); 1440 } 1441 1442 /* PM ops */ 1443 static const struct dev_pm_ops hva_pm_ops = { 1444 .runtime_suspend = hva_hw_runtime_suspend, 1445 .runtime_resume = hva_hw_runtime_resume, 1446 }; 1447 1448 static const struct of_device_id hva_match_types[] = { 1449 { 1450 .compatible = "st,st-hva", 1451 }, 1452 { /* end node */ } 1453 }; 1454 1455 MODULE_DEVICE_TABLE(of, hva_match_types); 1456 1457 static struct platform_driver hva_driver = { 1458 .probe = hva_probe, 1459 .remove = hva_remove, 1460 .driver = { 1461 .name = HVA_NAME, 1462 .of_match_table = hva_match_types, 1463 .pm = &hva_pm_ops, 1464 }, 1465 }; 1466 1467 module_platform_driver(hva_driver); 1468 1469 MODULE_LICENSE("GPL"); 1470 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>"); 1471 MODULE_DESCRIPTION("STMicroelectronics HVA video encoder V4L2 driver"); 1472