Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.20 2873 lines 78 kB view raw
1/* 2 * Coda multi-standard codec IP 3 * 4 * Copyright (C) 2012 Vista Silicon S.L. 5 * Javier Martin, <javier.martin@vista-silicon.com> 6 * Xavier Duret 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 */ 13 14#include <linux/clk.h> 15#include <linux/debugfs.h> 16#include <linux/delay.h> 17#include <linux/firmware.h> 18#include <linux/gcd.h> 19#include <linux/genalloc.h> 20#include <linux/interrupt.h> 21#include <linux/io.h> 22#include <linux/irq.h> 23#include <linux/kfifo.h> 24#include <linux/module.h> 25#include <linux/of_device.h> 26#include <linux/platform_device.h> 27#include <linux/pm_runtime.h> 28#include <linux/slab.h> 29#include <linux/videodev2.h> 30#include <linux/of.h> 31#include <linux/platform_data/media/coda.h> 32#include <linux/reset.h> 33 34#include <media/v4l2-ctrls.h> 35#include <media/v4l2-device.h> 36#include <media/v4l2-event.h> 37#include <media/v4l2-ioctl.h> 38#include <media/v4l2-mem2mem.h> 39#include <media/videobuf2-v4l2.h> 40#include <media/videobuf2-dma-contig.h> 41#include <media/videobuf2-vmalloc.h> 42 43#include "coda.h" 44#include "imx-vdoa.h" 45 46#define CODA_NAME "coda" 47 48#define CODADX6_MAX_INSTANCES 4 49#define CODA_MAX_FORMATS 4 50 51#define CODA_ISRAM_SIZE (2048 * 2) 52 53#define MIN_W 176 54#define MIN_H 144 55 56#define S_ALIGN 1 /* multiple of 2 */ 57#define W_ALIGN 1 /* multiple of 2 */ 58#define H_ALIGN 1 /* multiple of 2 */ 59 60#define fh_to_ctx(__fh) container_of(__fh, struct coda_ctx, fh) 61 62int coda_debug; 63module_param(coda_debug, int, 0644); 64MODULE_PARM_DESC(coda_debug, "Debug level (0-2)"); 65 66static int disable_tiling; 67module_param(disable_tiling, int, 0644); 68MODULE_PARM_DESC(disable_tiling, "Disable tiled frame buffers"); 69 70static int disable_vdoa; 71module_param(disable_vdoa, int, 0644); 72MODULE_PARM_DESC(disable_vdoa, "Disable Video Data Order Adapter tiled to raster-scan conversion"); 73 74static int enable_bwb = 0; 75module_param(enable_bwb, int, 0644); 76MODULE_PARM_DESC(enable_bwb, "Enable BWB unit for decoding, may crash on certain streams"); 77 78void coda_write(struct coda_dev *dev, u32 data, u32 reg) 79{ 80 v4l2_dbg(2, coda_debug, &dev->v4l2_dev, 81 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); 82 writel(data, dev->regs_base + reg); 83} 84 85unsigned int coda_read(struct coda_dev *dev, u32 reg) 86{ 87 u32 data; 88 89 data = readl(dev->regs_base + reg); 90 v4l2_dbg(2, coda_debug, &dev->v4l2_dev, 91 "%s: data=0x%x, reg=0x%x\n", __func__, data, reg); 92 return data; 93} 94 95void coda_write_base(struct coda_ctx *ctx, struct coda_q_data *q_data, 96 struct vb2_v4l2_buffer *buf, unsigned int reg_y) 97{ 98 u32 base_y = vb2_dma_contig_plane_dma_addr(&buf->vb2_buf, 0); 99 u32 base_cb, base_cr; 100 101 switch (q_data->fourcc) { 102 case V4L2_PIX_FMT_YUYV: 103 /* Fallthrough: IN -H264-> CODA -NV12 MB-> VDOA -YUYV-> OUT */ 104 case V4L2_PIX_FMT_NV12: 105 case V4L2_PIX_FMT_YUV420: 106 default: 107 base_cb = base_y + q_data->bytesperline * q_data->height; 108 base_cr = base_cb + q_data->bytesperline * q_data->height / 4; 109 break; 110 case V4L2_PIX_FMT_YVU420: 111 /* Switch Cb and Cr for YVU420 format */ 112 base_cr = base_y + q_data->bytesperline * q_data->height; 113 base_cb = base_cr + q_data->bytesperline * q_data->height / 4; 114 break; 115 case V4L2_PIX_FMT_YUV422P: 116 base_cb = base_y + q_data->bytesperline * q_data->height; 117 base_cr = base_cb + q_data->bytesperline * q_data->height / 2; 118 } 119 120 coda_write(ctx->dev, base_y, reg_y); 121 coda_write(ctx->dev, base_cb, reg_y + 4); 122 coda_write(ctx->dev, base_cr, reg_y + 8); 123} 124 125#define CODA_CODEC(mode, src_fourcc, dst_fourcc, max_w, max_h) \ 126 { mode, src_fourcc, dst_fourcc, max_w, max_h } 127 128/* 129 * Arrays of codecs supported by each given version of Coda: 130 * i.MX27 -> codadx6 131 * i.MX51 -> codahx4 132 * i.MX53 -> coda7 133 * i.MX6 -> coda960 134 * Use V4L2_PIX_FMT_YUV420 as placeholder for all supported YUV 4:2:0 variants 135 */ 136static const struct coda_codec codadx6_codecs[] = { 137 CODA_CODEC(CODADX6_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576), 138 CODA_CODEC(CODADX6_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 720, 576), 139}; 140 141static const struct coda_codec codahx4_codecs[] = { 142 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 720, 576), 143 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088), 144 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088), 145 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1280, 720), 146}; 147 148static const struct coda_codec coda7_codecs[] = { 149 CODA_CODEC(CODA7_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1280, 720), 150 CODA_CODEC(CODA7_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1280, 720), 151 CODA_CODEC(CODA7_MODE_ENCODE_MJPG, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_JPEG, 8192, 8192), 152 CODA_CODEC(CODA7_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088), 153 CODA_CODEC(CODA7_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088), 154 CODA_CODEC(CODA7_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088), 155 CODA_CODEC(CODA7_MODE_DECODE_MJPG, V4L2_PIX_FMT_JPEG, V4L2_PIX_FMT_YUV420, 8192, 8192), 156}; 157 158static const struct coda_codec coda9_codecs[] = { 159 CODA_CODEC(CODA9_MODE_ENCODE_H264, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_H264, 1920, 1088), 160 CODA_CODEC(CODA9_MODE_ENCODE_MP4, V4L2_PIX_FMT_YUV420, V4L2_PIX_FMT_MPEG4, 1920, 1088), 161 CODA_CODEC(CODA9_MODE_DECODE_H264, V4L2_PIX_FMT_H264, V4L2_PIX_FMT_YUV420, 1920, 1088), 162 CODA_CODEC(CODA9_MODE_DECODE_MP2, V4L2_PIX_FMT_MPEG2, V4L2_PIX_FMT_YUV420, 1920, 1088), 163 CODA_CODEC(CODA9_MODE_DECODE_MP4, V4L2_PIX_FMT_MPEG4, V4L2_PIX_FMT_YUV420, 1920, 1088), 164}; 165 166struct coda_video_device { 167 const char *name; 168 enum coda_inst_type type; 169 const struct coda_context_ops *ops; 170 bool direct; 171 u32 src_formats[CODA_MAX_FORMATS]; 172 u32 dst_formats[CODA_MAX_FORMATS]; 173}; 174 175static const struct coda_video_device coda_bit_encoder = { 176 .name = "coda-encoder", 177 .type = CODA_INST_ENCODER, 178 .ops = &coda_bit_encode_ops, 179 .src_formats = { 180 V4L2_PIX_FMT_NV12, 181 V4L2_PIX_FMT_YUV420, 182 V4L2_PIX_FMT_YVU420, 183 }, 184 .dst_formats = { 185 V4L2_PIX_FMT_H264, 186 V4L2_PIX_FMT_MPEG4, 187 }, 188}; 189 190static const struct coda_video_device coda_bit_jpeg_encoder = { 191 .name = "coda-jpeg-encoder", 192 .type = CODA_INST_ENCODER, 193 .ops = &coda_bit_encode_ops, 194 .src_formats = { 195 V4L2_PIX_FMT_NV12, 196 V4L2_PIX_FMT_YUV420, 197 V4L2_PIX_FMT_YVU420, 198 V4L2_PIX_FMT_YUV422P, 199 }, 200 .dst_formats = { 201 V4L2_PIX_FMT_JPEG, 202 }, 203}; 204 205static const struct coda_video_device coda_bit_decoder = { 206 .name = "coda-decoder", 207 .type = CODA_INST_DECODER, 208 .ops = &coda_bit_decode_ops, 209 .src_formats = { 210 V4L2_PIX_FMT_H264, 211 V4L2_PIX_FMT_MPEG2, 212 V4L2_PIX_FMT_MPEG4, 213 }, 214 .dst_formats = { 215 V4L2_PIX_FMT_NV12, 216 V4L2_PIX_FMT_YUV420, 217 V4L2_PIX_FMT_YVU420, 218 /* 219 * If V4L2_PIX_FMT_YUYV should be default, 220 * set_default_params() must be adjusted. 221 */ 222 V4L2_PIX_FMT_YUYV, 223 }, 224}; 225 226static const struct coda_video_device coda_bit_jpeg_decoder = { 227 .name = "coda-jpeg-decoder", 228 .type = CODA_INST_DECODER, 229 .ops = &coda_bit_decode_ops, 230 .src_formats = { 231 V4L2_PIX_FMT_JPEG, 232 }, 233 .dst_formats = { 234 V4L2_PIX_FMT_NV12, 235 V4L2_PIX_FMT_YUV420, 236 V4L2_PIX_FMT_YVU420, 237 V4L2_PIX_FMT_YUV422P, 238 }, 239}; 240 241static const struct coda_video_device *codadx6_video_devices[] = { 242 &coda_bit_encoder, 243}; 244 245static const struct coda_video_device *codahx4_video_devices[] = { 246 &coda_bit_encoder, 247 &coda_bit_decoder, 248}; 249 250static const struct coda_video_device *coda7_video_devices[] = { 251 &coda_bit_jpeg_encoder, 252 &coda_bit_jpeg_decoder, 253 &coda_bit_encoder, 254 &coda_bit_decoder, 255}; 256 257static const struct coda_video_device *coda9_video_devices[] = { 258 &coda_bit_encoder, 259 &coda_bit_decoder, 260}; 261 262/* 263 * Normalize all supported YUV 4:2:0 formats to the value used in the codec 264 * tables. 265 */ 266static u32 coda_format_normalize_yuv(u32 fourcc) 267{ 268 switch (fourcc) { 269 case V4L2_PIX_FMT_NV12: 270 case V4L2_PIX_FMT_YUV420: 271 case V4L2_PIX_FMT_YVU420: 272 case V4L2_PIX_FMT_YUV422P: 273 case V4L2_PIX_FMT_YUYV: 274 return V4L2_PIX_FMT_YUV420; 275 default: 276 return fourcc; 277 } 278} 279 280static const struct coda_codec *coda_find_codec(struct coda_dev *dev, 281 int src_fourcc, int dst_fourcc) 282{ 283 const struct coda_codec *codecs = dev->devtype->codecs; 284 int num_codecs = dev->devtype->num_codecs; 285 int k; 286 287 src_fourcc = coda_format_normalize_yuv(src_fourcc); 288 dst_fourcc = coda_format_normalize_yuv(dst_fourcc); 289 if (src_fourcc == dst_fourcc) 290 return NULL; 291 292 for (k = 0; k < num_codecs; k++) { 293 if (codecs[k].src_fourcc == src_fourcc && 294 codecs[k].dst_fourcc == dst_fourcc) 295 break; 296 } 297 298 if (k == num_codecs) 299 return NULL; 300 301 return &codecs[k]; 302} 303 304static void coda_get_max_dimensions(struct coda_dev *dev, 305 const struct coda_codec *codec, 306 int *max_w, int *max_h) 307{ 308 const struct coda_codec *codecs = dev->devtype->codecs; 309 int num_codecs = dev->devtype->num_codecs; 310 unsigned int w, h; 311 int k; 312 313 if (codec) { 314 w = codec->max_w; 315 h = codec->max_h; 316 } else { 317 for (k = 0, w = 0, h = 0; k < num_codecs; k++) { 318 w = max(w, codecs[k].max_w); 319 h = max(h, codecs[k].max_h); 320 } 321 } 322 323 if (max_w) 324 *max_w = w; 325 if (max_h) 326 *max_h = h; 327} 328 329static const struct coda_video_device *to_coda_video_device(struct video_device 330 *vdev) 331{ 332 struct coda_dev *dev = video_get_drvdata(vdev); 333 unsigned int i = vdev - dev->vfd; 334 335 if (i >= dev->devtype->num_vdevs) 336 return NULL; 337 338 return dev->devtype->vdevs[i]; 339} 340 341const char *coda_product_name(int product) 342{ 343 static char buf[9]; 344 345 switch (product) { 346 case CODA_DX6: 347 return "CodaDx6"; 348 case CODA_HX4: 349 return "CodaHx4"; 350 case CODA_7541: 351 return "CODA7541"; 352 case CODA_960: 353 return "CODA960"; 354 default: 355 snprintf(buf, sizeof(buf), "(0x%04x)", product); 356 return buf; 357 } 358} 359 360static struct vdoa_data *coda_get_vdoa_data(void) 361{ 362 struct device_node *vdoa_node; 363 struct platform_device *vdoa_pdev; 364 struct vdoa_data *vdoa_data = NULL; 365 366 vdoa_node = of_find_compatible_node(NULL, NULL, "fsl,imx6q-vdoa"); 367 if (!vdoa_node) 368 return NULL; 369 370 vdoa_pdev = of_find_device_by_node(vdoa_node); 371 if (!vdoa_pdev) 372 goto out; 373 374 vdoa_data = platform_get_drvdata(vdoa_pdev); 375 if (!vdoa_data) 376 vdoa_data = ERR_PTR(-EPROBE_DEFER); 377 378out: 379 of_node_put(vdoa_node); 380 381 return vdoa_data; 382} 383 384/* 385 * V4L2 ioctl() operations. 386 */ 387static int coda_querycap(struct file *file, void *priv, 388 struct v4l2_capability *cap) 389{ 390 struct coda_ctx *ctx = fh_to_ctx(priv); 391 392 strscpy(cap->driver, CODA_NAME, sizeof(cap->driver)); 393 strscpy(cap->card, coda_product_name(ctx->dev->devtype->product), 394 sizeof(cap->card)); 395 strscpy(cap->bus_info, "platform:" CODA_NAME, sizeof(cap->bus_info)); 396 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; 397 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 398 399 return 0; 400} 401 402static int coda_enum_fmt(struct file *file, void *priv, 403 struct v4l2_fmtdesc *f) 404{ 405 struct video_device *vdev = video_devdata(file); 406 const struct coda_video_device *cvd = to_coda_video_device(vdev); 407 struct coda_ctx *ctx = fh_to_ctx(priv); 408 const u32 *formats; 409 410 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) 411 formats = cvd->src_formats; 412 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 413 formats = cvd->dst_formats; 414 else 415 return -EINVAL; 416 417 if (f->index >= CODA_MAX_FORMATS || formats[f->index] == 0) 418 return -EINVAL; 419 420 /* Skip YUYV if the vdoa is not available */ 421 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && 422 formats[f->index] == V4L2_PIX_FMT_YUYV) 423 return -EINVAL; 424 425 f->pixelformat = formats[f->index]; 426 427 return 0; 428} 429 430static int coda_g_fmt(struct file *file, void *priv, 431 struct v4l2_format *f) 432{ 433 struct coda_q_data *q_data; 434 struct coda_ctx *ctx = fh_to_ctx(priv); 435 436 q_data = get_q_data(ctx, f->type); 437 if (!q_data) 438 return -EINVAL; 439 440 f->fmt.pix.field = V4L2_FIELD_NONE; 441 f->fmt.pix.pixelformat = q_data->fourcc; 442 f->fmt.pix.width = q_data->width; 443 f->fmt.pix.height = q_data->height; 444 f->fmt.pix.bytesperline = q_data->bytesperline; 445 446 f->fmt.pix.sizeimage = q_data->sizeimage; 447 f->fmt.pix.colorspace = ctx->colorspace; 448 f->fmt.pix.xfer_func = ctx->xfer_func; 449 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 450 f->fmt.pix.quantization = ctx->quantization; 451 452 return 0; 453} 454 455static int coda_try_pixelformat(struct coda_ctx *ctx, struct v4l2_format *f) 456{ 457 struct coda_q_data *q_data; 458 const u32 *formats; 459 int i; 460 461 if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) 462 formats = ctx->cvd->src_formats; 463 else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 464 formats = ctx->cvd->dst_formats; 465 else 466 return -EINVAL; 467 468 for (i = 0; i < CODA_MAX_FORMATS; i++) { 469 /* Skip YUYV if the vdoa is not available */ 470 if (!ctx->vdoa && f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && 471 formats[i] == V4L2_PIX_FMT_YUYV) 472 continue; 473 474 if (formats[i] == f->fmt.pix.pixelformat) { 475 f->fmt.pix.pixelformat = formats[i]; 476 return 0; 477 } 478 } 479 480 /* Fall back to currently set pixelformat */ 481 q_data = get_q_data(ctx, f->type); 482 f->fmt.pix.pixelformat = q_data->fourcc; 483 484 return 0; 485} 486 487static int coda_try_fmt_vdoa(struct coda_ctx *ctx, struct v4l2_format *f, 488 bool *use_vdoa) 489{ 490 int err; 491 492 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 493 return -EINVAL; 494 495 if (!use_vdoa) 496 return -EINVAL; 497 498 if (!ctx->vdoa) { 499 *use_vdoa = false; 500 return 0; 501 } 502 503 err = vdoa_context_configure(NULL, round_up(f->fmt.pix.width, 16), 504 f->fmt.pix.height, f->fmt.pix.pixelformat); 505 if (err) { 506 *use_vdoa = false; 507 return 0; 508 } 509 510 *use_vdoa = true; 511 return 0; 512} 513 514static unsigned int coda_estimate_sizeimage(struct coda_ctx *ctx, u32 sizeimage, 515 u32 width, u32 height) 516{ 517 /* 518 * This is a rough estimate for sensible compressed buffer 519 * sizes (between 1 and 16 bits per pixel). This could be 520 * improved by better format specific worst case estimates. 521 */ 522 return round_up(clamp(sizeimage, width * height / 8, 523 width * height * 2), PAGE_SIZE); 524} 525 526static int coda_try_fmt(struct coda_ctx *ctx, const struct coda_codec *codec, 527 struct v4l2_format *f) 528{ 529 struct coda_dev *dev = ctx->dev; 530 unsigned int max_w, max_h; 531 enum v4l2_field field; 532 533 field = f->fmt.pix.field; 534 if (field == V4L2_FIELD_ANY) 535 field = V4L2_FIELD_NONE; 536 else if (V4L2_FIELD_NONE != field) 537 return -EINVAL; 538 539 /* V4L2 specification suggests the driver corrects the format struct 540 * if any of the dimensions is unsupported */ 541 f->fmt.pix.field = field; 542 543 coda_get_max_dimensions(dev, codec, &max_w, &max_h); 544 v4l_bound_align_image(&f->fmt.pix.width, MIN_W, max_w, W_ALIGN, 545 &f->fmt.pix.height, MIN_H, max_h, H_ALIGN, 546 S_ALIGN); 547 548 switch (f->fmt.pix.pixelformat) { 549 case V4L2_PIX_FMT_NV12: 550 case V4L2_PIX_FMT_YUV420: 551 case V4L2_PIX_FMT_YVU420: 552 /* 553 * Frame stride must be at least multiple of 8, 554 * but multiple of 16 for h.264 or JPEG 4:2:x 555 */ 556 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16); 557 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * 558 f->fmt.pix.height * 3 / 2; 559 break; 560 case V4L2_PIX_FMT_YUYV: 561 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2; 562 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * 563 f->fmt.pix.height; 564 break; 565 case V4L2_PIX_FMT_YUV422P: 566 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16); 567 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * 568 f->fmt.pix.height * 2; 569 break; 570 case V4L2_PIX_FMT_JPEG: 571 case V4L2_PIX_FMT_H264: 572 case V4L2_PIX_FMT_MPEG4: 573 case V4L2_PIX_FMT_MPEG2: 574 f->fmt.pix.bytesperline = 0; 575 f->fmt.pix.sizeimage = coda_estimate_sizeimage(ctx, 576 f->fmt.pix.sizeimage, 577 f->fmt.pix.width, 578 f->fmt.pix.height); 579 break; 580 default: 581 BUG(); 582 } 583 584 return 0; 585} 586 587static int coda_try_fmt_vid_cap(struct file *file, void *priv, 588 struct v4l2_format *f) 589{ 590 struct coda_ctx *ctx = fh_to_ctx(priv); 591 const struct coda_q_data *q_data_src; 592 const struct coda_codec *codec; 593 struct vb2_queue *src_vq; 594 int ret; 595 bool use_vdoa; 596 597 ret = coda_try_pixelformat(ctx, f); 598 if (ret < 0) 599 return ret; 600 601 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 602 603 /* 604 * If the source format is already fixed, only allow the same output 605 * resolution 606 */ 607 src_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 608 if (vb2_is_streaming(src_vq)) { 609 f->fmt.pix.width = q_data_src->width; 610 f->fmt.pix.height = q_data_src->height; 611 } 612 613 f->fmt.pix.colorspace = ctx->colorspace; 614 f->fmt.pix.xfer_func = ctx->xfer_func; 615 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; 616 f->fmt.pix.quantization = ctx->quantization; 617 618 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 619 codec = coda_find_codec(ctx->dev, q_data_src->fourcc, 620 f->fmt.pix.pixelformat); 621 if (!codec) 622 return -EINVAL; 623 624 ret = coda_try_fmt(ctx, codec, f); 625 if (ret < 0) 626 return ret; 627 628 /* The h.264 decoder only returns complete 16x16 macroblocks */ 629 if (codec && codec->src_fourcc == V4L2_PIX_FMT_H264) { 630 f->fmt.pix.height = round_up(f->fmt.pix.height, 16); 631 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16); 632 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * 633 f->fmt.pix.height * 3 / 2; 634 635 ret = coda_try_fmt_vdoa(ctx, f, &use_vdoa); 636 if (ret < 0) 637 return ret; 638 639 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) { 640 if (!use_vdoa) 641 return -EINVAL; 642 643 f->fmt.pix.bytesperline = round_up(f->fmt.pix.width, 16) * 2; 644 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * 645 f->fmt.pix.height; 646 } 647 } 648 649 return 0; 650} 651 652static void coda_set_default_colorspace(struct v4l2_pix_format *fmt) 653{ 654 enum v4l2_colorspace colorspace; 655 656 if (fmt->pixelformat == V4L2_PIX_FMT_JPEG) 657 colorspace = V4L2_COLORSPACE_JPEG; 658 else if (fmt->width <= 720 && fmt->height <= 576) 659 colorspace = V4L2_COLORSPACE_SMPTE170M; 660 else 661 colorspace = V4L2_COLORSPACE_REC709; 662 663 fmt->colorspace = colorspace; 664 fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT; 665 fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 666 fmt->quantization = V4L2_QUANTIZATION_DEFAULT; 667} 668 669static int coda_try_fmt_vid_out(struct file *file, void *priv, 670 struct v4l2_format *f) 671{ 672 struct coda_ctx *ctx = fh_to_ctx(priv); 673 struct coda_dev *dev = ctx->dev; 674 const struct coda_q_data *q_data_dst; 675 const struct coda_codec *codec; 676 int ret; 677 678 ret = coda_try_pixelformat(ctx, f); 679 if (ret < 0) 680 return ret; 681 682 if (f->fmt.pix.colorspace == V4L2_COLORSPACE_DEFAULT) 683 coda_set_default_colorspace(&f->fmt.pix); 684 685 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 686 codec = coda_find_codec(dev, f->fmt.pix.pixelformat, q_data_dst->fourcc); 687 688 return coda_try_fmt(ctx, codec, f); 689} 690 691static int coda_s_fmt(struct coda_ctx *ctx, struct v4l2_format *f, 692 struct v4l2_rect *r) 693{ 694 struct coda_q_data *q_data; 695 struct vb2_queue *vq; 696 697 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 698 if (!vq) 699 return -EINVAL; 700 701 q_data = get_q_data(ctx, f->type); 702 if (!q_data) 703 return -EINVAL; 704 705 if (vb2_is_busy(vq)) { 706 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); 707 return -EBUSY; 708 } 709 710 q_data->fourcc = f->fmt.pix.pixelformat; 711 q_data->width = f->fmt.pix.width; 712 q_data->height = f->fmt.pix.height; 713 q_data->bytesperline = f->fmt.pix.bytesperline; 714 q_data->sizeimage = f->fmt.pix.sizeimage; 715 if (r) { 716 q_data->rect = *r; 717 } else { 718 q_data->rect.left = 0; 719 q_data->rect.top = 0; 720 q_data->rect.width = f->fmt.pix.width; 721 q_data->rect.height = f->fmt.pix.height; 722 } 723 724 switch (f->fmt.pix.pixelformat) { 725 case V4L2_PIX_FMT_YUYV: 726 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP; 727 break; 728 case V4L2_PIX_FMT_NV12: 729 if (!disable_tiling) { 730 ctx->tiled_map_type = GDI_TILED_FRAME_MB_RASTER_MAP; 731 break; 732 } 733 /* else fall through */ 734 case V4L2_PIX_FMT_YUV420: 735 case V4L2_PIX_FMT_YVU420: 736 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP; 737 break; 738 default: 739 break; 740 } 741 742 if (ctx->tiled_map_type == GDI_TILED_FRAME_MB_RASTER_MAP && 743 !coda_try_fmt_vdoa(ctx, f, &ctx->use_vdoa) && 744 ctx->use_vdoa) 745 vdoa_context_configure(ctx->vdoa, 746 round_up(f->fmt.pix.width, 16), 747 f->fmt.pix.height, 748 f->fmt.pix.pixelformat); 749 else 750 ctx->use_vdoa = false; 751 752 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 753 "Setting format for type %d, wxh: %dx%d, fmt: %4.4s %c\n", 754 f->type, q_data->width, q_data->height, 755 (char *)&q_data->fourcc, 756 (ctx->tiled_map_type == GDI_LINEAR_FRAME_MAP) ? 'L' : 'T'); 757 758 return 0; 759} 760 761static int coda_s_fmt_vid_cap(struct file *file, void *priv, 762 struct v4l2_format *f) 763{ 764 struct coda_ctx *ctx = fh_to_ctx(priv); 765 struct coda_q_data *q_data_src; 766 struct v4l2_rect r; 767 int ret; 768 769 ret = coda_try_fmt_vid_cap(file, priv, f); 770 if (ret) 771 return ret; 772 773 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 774 r.left = 0; 775 r.top = 0; 776 r.width = q_data_src->width; 777 r.height = q_data_src->height; 778 779 ret = coda_s_fmt(ctx, f, &r); 780 if (ret) 781 return ret; 782 783 if (ctx->inst_type != CODA_INST_ENCODER) 784 return 0; 785 786 ctx->colorspace = f->fmt.pix.colorspace; 787 ctx->xfer_func = f->fmt.pix.xfer_func; 788 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc; 789 ctx->quantization = f->fmt.pix.quantization; 790 791 return 0; 792} 793 794static int coda_s_fmt_vid_out(struct file *file, void *priv, 795 struct v4l2_format *f) 796{ 797 struct coda_ctx *ctx = fh_to_ctx(priv); 798 struct v4l2_format f_cap; 799 struct vb2_queue *dst_vq; 800 int ret; 801 802 ret = coda_try_fmt_vid_out(file, priv, f); 803 if (ret) 804 return ret; 805 806 ret = coda_s_fmt(ctx, f, NULL); 807 if (ret) 808 return ret; 809 810 if (ctx->inst_type != CODA_INST_DECODER) 811 return 0; 812 813 ctx->colorspace = f->fmt.pix.colorspace; 814 ctx->xfer_func = f->fmt.pix.xfer_func; 815 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc; 816 ctx->quantization = f->fmt.pix.quantization; 817 818 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 819 if (!dst_vq) 820 return -EINVAL; 821 822 /* 823 * Setting the capture queue format is not possible while the capture 824 * queue is still busy. This is not an error, but the user will have to 825 * make sure themselves that the capture format is set correctly before 826 * starting the output queue again. 827 */ 828 if (vb2_is_busy(dst_vq)) 829 return 0; 830 831 memset(&f_cap, 0, sizeof(f_cap)); 832 f_cap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 833 coda_g_fmt(file, priv, &f_cap); 834 f_cap.fmt.pix.width = f->fmt.pix.width; 835 f_cap.fmt.pix.height = f->fmt.pix.height; 836 837 return coda_s_fmt_vid_cap(file, priv, &f_cap); 838} 839 840static int coda_reqbufs(struct file *file, void *priv, 841 struct v4l2_requestbuffers *rb) 842{ 843 struct coda_ctx *ctx = fh_to_ctx(priv); 844 int ret; 845 846 ret = v4l2_m2m_reqbufs(file, ctx->fh.m2m_ctx, rb); 847 if (ret) 848 return ret; 849 850 /* 851 * Allow to allocate instance specific per-context buffers, such as 852 * bitstream ringbuffer, slice buffer, work buffer, etc. if needed. 853 */ 854 if (rb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && ctx->ops->reqbufs) 855 return ctx->ops->reqbufs(ctx, rb); 856 857 return 0; 858} 859 860static int coda_qbuf(struct file *file, void *priv, 861 struct v4l2_buffer *buf) 862{ 863 struct coda_ctx *ctx = fh_to_ctx(priv); 864 865 return v4l2_m2m_qbuf(file, ctx->fh.m2m_ctx, buf); 866} 867 868static bool coda_buf_is_end_of_stream(struct coda_ctx *ctx, 869 struct vb2_v4l2_buffer *buf) 870{ 871 return ((ctx->bit_stream_param & CODA_BIT_STREAM_END_FLAG) && 872 (buf->sequence == (ctx->qsequence - 1))); 873} 874 875void coda_m2m_buf_done(struct coda_ctx *ctx, struct vb2_v4l2_buffer *buf, 876 enum vb2_buffer_state state) 877{ 878 const struct v4l2_event eos_event = { 879 .type = V4L2_EVENT_EOS 880 }; 881 882 if (coda_buf_is_end_of_stream(ctx, buf)) { 883 buf->flags |= V4L2_BUF_FLAG_LAST; 884 885 v4l2_event_queue_fh(&ctx->fh, &eos_event); 886 } 887 888 v4l2_m2m_buf_done(buf, state); 889} 890 891static int coda_g_selection(struct file *file, void *fh, 892 struct v4l2_selection *s) 893{ 894 struct coda_ctx *ctx = fh_to_ctx(fh); 895 struct coda_q_data *q_data; 896 struct v4l2_rect r, *rsel; 897 898 q_data = get_q_data(ctx, s->type); 899 if (!q_data) 900 return -EINVAL; 901 902 r.left = 0; 903 r.top = 0; 904 r.width = q_data->width; 905 r.height = q_data->height; 906 rsel = &q_data->rect; 907 908 switch (s->target) { 909 case V4L2_SEL_TGT_CROP_DEFAULT: 910 case V4L2_SEL_TGT_CROP_BOUNDS: 911 rsel = &r; 912 /* fallthrough */ 913 case V4L2_SEL_TGT_CROP: 914 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 915 return -EINVAL; 916 break; 917 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 918 case V4L2_SEL_TGT_COMPOSE_PADDED: 919 rsel = &r; 920 /* fallthrough */ 921 case V4L2_SEL_TGT_COMPOSE: 922 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 923 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 924 return -EINVAL; 925 break; 926 default: 927 return -EINVAL; 928 } 929 930 s->r = *rsel; 931 932 return 0; 933} 934 935static int coda_s_selection(struct file *file, void *fh, 936 struct v4l2_selection *s) 937{ 938 struct coda_ctx *ctx = fh_to_ctx(fh); 939 struct coda_q_data *q_data; 940 941 if (ctx->inst_type == CODA_INST_ENCODER && 942 s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT && 943 s->target == V4L2_SEL_TGT_CROP) { 944 q_data = get_q_data(ctx, s->type); 945 if (!q_data) 946 return -EINVAL; 947 948 s->r.left = 0; 949 s->r.top = 0; 950 s->r.width = clamp(s->r.width, 2U, q_data->width); 951 s->r.height = clamp(s->r.height, 2U, q_data->height); 952 953 if (s->flags & V4L2_SEL_FLAG_LE) { 954 s->r.width = round_up(s->r.width, 2); 955 s->r.height = round_up(s->r.height, 2); 956 } else { 957 s->r.width = round_down(s->r.width, 2); 958 s->r.height = round_down(s->r.height, 2); 959 } 960 961 q_data->rect = s->r; 962 963 return 0; 964 } 965 966 return coda_g_selection(file, fh, s); 967} 968 969static int coda_try_encoder_cmd(struct file *file, void *fh, 970 struct v4l2_encoder_cmd *ec) 971{ 972 if (ec->cmd != V4L2_ENC_CMD_STOP) 973 return -EINVAL; 974 975 if (ec->flags & V4L2_ENC_CMD_STOP_AT_GOP_END) 976 return -EINVAL; 977 978 return 0; 979} 980 981static int coda_encoder_cmd(struct file *file, void *fh, 982 struct v4l2_encoder_cmd *ec) 983{ 984 struct coda_ctx *ctx = fh_to_ctx(fh); 985 struct vb2_queue *dst_vq; 986 int ret; 987 988 ret = coda_try_encoder_cmd(file, fh, ec); 989 if (ret < 0) 990 return ret; 991 992 /* Ignore encoder stop command silently in decoder context */ 993 if (ctx->inst_type != CODA_INST_ENCODER) 994 return 0; 995 996 /* Set the stream-end flag on this context */ 997 ctx->bit_stream_param |= CODA_BIT_STREAM_END_FLAG; 998 999 /* If there is no buffer in flight, wake up */ 1000 if (!ctx->streamon_out || ctx->qsequence == ctx->osequence) { 1001 dst_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, 1002 V4L2_BUF_TYPE_VIDEO_CAPTURE); 1003 dst_vq->last_buffer_dequeued = true; 1004 wake_up(&dst_vq->done_wq); 1005 } 1006 1007 return 0; 1008} 1009 1010static int coda_try_decoder_cmd(struct file *file, void *fh, 1011 struct v4l2_decoder_cmd *dc) 1012{ 1013 if (dc->cmd != V4L2_DEC_CMD_STOP) 1014 return -EINVAL; 1015 1016 if (dc->flags & V4L2_DEC_CMD_STOP_TO_BLACK) 1017 return -EINVAL; 1018 1019 if (!(dc->flags & V4L2_DEC_CMD_STOP_IMMEDIATELY) && (dc->stop.pts != 0)) 1020 return -EINVAL; 1021 1022 return 0; 1023} 1024 1025static int coda_decoder_cmd(struct file *file, void *fh, 1026 struct v4l2_decoder_cmd *dc) 1027{ 1028 struct coda_ctx *ctx = fh_to_ctx(fh); 1029 int ret; 1030 1031 ret = coda_try_decoder_cmd(file, fh, dc); 1032 if (ret < 0) 1033 return ret; 1034 1035 /* Ignore decoder stop command silently in encoder context */ 1036 if (ctx->inst_type != CODA_INST_DECODER) 1037 return 0; 1038 1039 /* Set the stream-end flag on this context */ 1040 coda_bit_stream_end_flag(ctx); 1041 ctx->hold = false; 1042 v4l2_m2m_try_schedule(ctx->fh.m2m_ctx); 1043 1044 return 0; 1045} 1046 1047static int coda_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 1048{ 1049 struct coda_ctx *ctx = fh_to_ctx(fh); 1050 struct v4l2_fract *tpf; 1051 1052 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 1053 return -EINVAL; 1054 1055 a->parm.output.capability = V4L2_CAP_TIMEPERFRAME; 1056 tpf = &a->parm.output.timeperframe; 1057 tpf->denominator = ctx->params.framerate & CODA_FRATE_RES_MASK; 1058 tpf->numerator = 1 + (ctx->params.framerate >> 1059 CODA_FRATE_DIV_OFFSET); 1060 1061 return 0; 1062} 1063 1064/* 1065 * Approximate timeperframe v4l2_fract with values that can be written 1066 * into the 16-bit CODA_FRATE_DIV and CODA_FRATE_RES fields. 1067 */ 1068static void coda_approximate_timeperframe(struct v4l2_fract *timeperframe) 1069{ 1070 struct v4l2_fract s = *timeperframe; 1071 struct v4l2_fract f0; 1072 struct v4l2_fract f1 = { 1, 0 }; 1073 struct v4l2_fract f2 = { 0, 1 }; 1074 unsigned int i, div, s_denominator; 1075 1076 /* Lower bound is 1/65535 */ 1077 if (s.numerator == 0 || s.denominator / s.numerator > 65535) { 1078 timeperframe->numerator = 1; 1079 timeperframe->denominator = 65535; 1080 return; 1081 } 1082 1083 /* Upper bound is 65536/1, map everything above to infinity */ 1084 if (s.denominator == 0 || s.numerator / s.denominator > 65536) { 1085 timeperframe->numerator = 1; 1086 timeperframe->denominator = 0; 1087 return; 1088 } 1089 1090 /* Reduce fraction to lowest terms */ 1091 div = gcd(s.numerator, s.denominator); 1092 if (div > 1) { 1093 s.numerator /= div; 1094 s.denominator /= div; 1095 } 1096 1097 if (s.numerator <= 65536 && s.denominator < 65536) { 1098 *timeperframe = s; 1099 return; 1100 } 1101 1102 /* Find successive convergents from continued fraction expansion */ 1103 while (f2.numerator <= 65536 && f2.denominator < 65536) { 1104 f0 = f1; 1105 f1 = f2; 1106 1107 /* Stop when f2 exactly equals timeperframe */ 1108 if (s.numerator == 0) 1109 break; 1110 1111 i = s.denominator / s.numerator; 1112 1113 f2.numerator = f0.numerator + i * f1.numerator; 1114 f2.denominator = f0.denominator + i * f2.denominator; 1115 1116 s_denominator = s.numerator; 1117 s.numerator = s.denominator % s.numerator; 1118 s.denominator = s_denominator; 1119 } 1120 1121 *timeperframe = f1; 1122} 1123 1124static uint32_t coda_timeperframe_to_frate(struct v4l2_fract *timeperframe) 1125{ 1126 return ((timeperframe->numerator - 1) << CODA_FRATE_DIV_OFFSET) | 1127 timeperframe->denominator; 1128} 1129 1130static int coda_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 1131{ 1132 struct coda_ctx *ctx = fh_to_ctx(fh); 1133 struct v4l2_fract *tpf; 1134 1135 if (a->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 1136 return -EINVAL; 1137 1138 tpf = &a->parm.output.timeperframe; 1139 coda_approximate_timeperframe(tpf); 1140 ctx->params.framerate = coda_timeperframe_to_frate(tpf); 1141 1142 return 0; 1143} 1144 1145static int coda_subscribe_event(struct v4l2_fh *fh, 1146 const struct v4l2_event_subscription *sub) 1147{ 1148 switch (sub->type) { 1149 case V4L2_EVENT_EOS: 1150 return v4l2_event_subscribe(fh, sub, 0, NULL); 1151 default: 1152 return v4l2_ctrl_subscribe_event(fh, sub); 1153 } 1154} 1155 1156static const struct v4l2_ioctl_ops coda_ioctl_ops = { 1157 .vidioc_querycap = coda_querycap, 1158 1159 .vidioc_enum_fmt_vid_cap = coda_enum_fmt, 1160 .vidioc_g_fmt_vid_cap = coda_g_fmt, 1161 .vidioc_try_fmt_vid_cap = coda_try_fmt_vid_cap, 1162 .vidioc_s_fmt_vid_cap = coda_s_fmt_vid_cap, 1163 1164 .vidioc_enum_fmt_vid_out = coda_enum_fmt, 1165 .vidioc_g_fmt_vid_out = coda_g_fmt, 1166 .vidioc_try_fmt_vid_out = coda_try_fmt_vid_out, 1167 .vidioc_s_fmt_vid_out = coda_s_fmt_vid_out, 1168 1169 .vidioc_reqbufs = coda_reqbufs, 1170 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 1171 1172 .vidioc_qbuf = coda_qbuf, 1173 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 1174 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 1175 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 1176 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 1177 1178 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 1179 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 1180 1181 .vidioc_g_selection = coda_g_selection, 1182 .vidioc_s_selection = coda_s_selection, 1183 1184 .vidioc_try_encoder_cmd = coda_try_encoder_cmd, 1185 .vidioc_encoder_cmd = coda_encoder_cmd, 1186 .vidioc_try_decoder_cmd = coda_try_decoder_cmd, 1187 .vidioc_decoder_cmd = coda_decoder_cmd, 1188 1189 .vidioc_g_parm = coda_g_parm, 1190 .vidioc_s_parm = coda_s_parm, 1191 1192 .vidioc_subscribe_event = coda_subscribe_event, 1193 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1194}; 1195 1196/* 1197 * Mem-to-mem operations. 1198 */ 1199 1200static void coda_device_run(void *m2m_priv) 1201{ 1202 struct coda_ctx *ctx = m2m_priv; 1203 struct coda_dev *dev = ctx->dev; 1204 1205 queue_work(dev->workqueue, &ctx->pic_run_work); 1206} 1207 1208static void coda_pic_run_work(struct work_struct *work) 1209{ 1210 struct coda_ctx *ctx = container_of(work, struct coda_ctx, pic_run_work); 1211 struct coda_dev *dev = ctx->dev; 1212 int ret; 1213 1214 mutex_lock(&ctx->buffer_mutex); 1215 mutex_lock(&dev->coda_mutex); 1216 1217 ret = ctx->ops->prepare_run(ctx); 1218 if (ret < 0 && ctx->inst_type == CODA_INST_DECODER) { 1219 mutex_unlock(&dev->coda_mutex); 1220 mutex_unlock(&ctx->buffer_mutex); 1221 /* job_finish scheduled by prepare_decode */ 1222 return; 1223 } 1224 1225 if (!wait_for_completion_timeout(&ctx->completion, 1226 msecs_to_jiffies(1000))) { 1227 dev_err(&dev->plat_dev->dev, "CODA PIC_RUN timeout\n"); 1228 1229 ctx->hold = true; 1230 1231 coda_hw_reset(ctx); 1232 1233 if (ctx->ops->run_timeout) 1234 ctx->ops->run_timeout(ctx); 1235 } else if (!ctx->aborting) { 1236 ctx->ops->finish_run(ctx); 1237 } 1238 1239 if ((ctx->aborting || (!ctx->streamon_cap && !ctx->streamon_out)) && 1240 ctx->ops->seq_end_work) 1241 queue_work(dev->workqueue, &ctx->seq_end_work); 1242 1243 mutex_unlock(&dev->coda_mutex); 1244 mutex_unlock(&ctx->buffer_mutex); 1245 1246 v4l2_m2m_job_finish(ctx->dev->m2m_dev, ctx->fh.m2m_ctx); 1247} 1248 1249static int coda_job_ready(void *m2m_priv) 1250{ 1251 struct coda_ctx *ctx = m2m_priv; 1252 int src_bufs = v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx); 1253 1254 /* 1255 * For both 'P' and 'key' frame cases 1 picture 1256 * and 1 frame are needed. In the decoder case, 1257 * the compressed frame can be in the bitstream. 1258 */ 1259 if (!src_bufs && ctx->inst_type != CODA_INST_DECODER) { 1260 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1261 "not ready: not enough video buffers.\n"); 1262 return 0; 1263 } 1264 1265 if (!v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx)) { 1266 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1267 "not ready: not enough video capture buffers.\n"); 1268 return 0; 1269 } 1270 1271 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) { 1272 bool stream_end = ctx->bit_stream_param & 1273 CODA_BIT_STREAM_END_FLAG; 1274 int num_metas = ctx->num_metas; 1275 unsigned int count; 1276 1277 count = hweight32(ctx->frm_dis_flg); 1278 if (ctx->use_vdoa && count >= (ctx->num_internal_frames - 1)) { 1279 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1280 "%d: not ready: all internal buffers in use: %d/%d (0x%x)", 1281 ctx->idx, count, ctx->num_internal_frames, 1282 ctx->frm_dis_flg); 1283 return 0; 1284 } 1285 1286 if (ctx->hold && !src_bufs) { 1287 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1288 "%d: not ready: on hold for more buffers.\n", 1289 ctx->idx); 1290 return 0; 1291 } 1292 1293 if (!stream_end && (num_metas + src_bufs) < 2) { 1294 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1295 "%d: not ready: need 2 buffers available (%d, %d)\n", 1296 ctx->idx, num_metas, src_bufs); 1297 return 0; 1298 } 1299 1300 1301 if (!src_bufs && !stream_end && 1302 (coda_get_bitstream_payload(ctx) < 512)) { 1303 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1304 "%d: not ready: not enough bitstream data (%d).\n", 1305 ctx->idx, coda_get_bitstream_payload(ctx)); 1306 return 0; 1307 } 1308 } 1309 1310 if (ctx->aborting) { 1311 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1312 "not ready: aborting\n"); 1313 return 0; 1314 } 1315 1316 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1317 "job ready\n"); 1318 1319 return 1; 1320} 1321 1322static void coda_job_abort(void *priv) 1323{ 1324 struct coda_ctx *ctx = priv; 1325 1326 ctx->aborting = 1; 1327 1328 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1329 "Aborting task\n"); 1330} 1331 1332static const struct v4l2_m2m_ops coda_m2m_ops = { 1333 .device_run = coda_device_run, 1334 .job_ready = coda_job_ready, 1335 .job_abort = coda_job_abort, 1336}; 1337 1338static void set_default_params(struct coda_ctx *ctx) 1339{ 1340 unsigned int max_w, max_h, usize, csize; 1341 1342 ctx->codec = coda_find_codec(ctx->dev, ctx->cvd->src_formats[0], 1343 ctx->cvd->dst_formats[0]); 1344 max_w = min(ctx->codec->max_w, 1920U); 1345 max_h = min(ctx->codec->max_h, 1088U); 1346 usize = max_w * max_h * 3 / 2; 1347 csize = coda_estimate_sizeimage(ctx, usize, max_w, max_h); 1348 1349 ctx->params.codec_mode = ctx->codec->mode; 1350 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_JPEG) 1351 ctx->colorspace = V4L2_COLORSPACE_JPEG; 1352 else 1353 ctx->colorspace = V4L2_COLORSPACE_REC709; 1354 ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT; 1355 ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 1356 ctx->quantization = V4L2_QUANTIZATION_DEFAULT; 1357 ctx->params.framerate = 30; 1358 1359 /* Default formats for output and input queues */ 1360 ctx->q_data[V4L2_M2M_SRC].fourcc = ctx->cvd->src_formats[0]; 1361 ctx->q_data[V4L2_M2M_DST].fourcc = ctx->cvd->dst_formats[0]; 1362 ctx->q_data[V4L2_M2M_SRC].width = max_w; 1363 ctx->q_data[V4L2_M2M_SRC].height = max_h; 1364 ctx->q_data[V4L2_M2M_DST].width = max_w; 1365 ctx->q_data[V4L2_M2M_DST].height = max_h; 1366 if (ctx->codec->src_fourcc == V4L2_PIX_FMT_YUV420) { 1367 ctx->q_data[V4L2_M2M_SRC].bytesperline = max_w; 1368 ctx->q_data[V4L2_M2M_SRC].sizeimage = usize; 1369 ctx->q_data[V4L2_M2M_DST].bytesperline = 0; 1370 ctx->q_data[V4L2_M2M_DST].sizeimage = csize; 1371 } else { 1372 ctx->q_data[V4L2_M2M_SRC].bytesperline = 0; 1373 ctx->q_data[V4L2_M2M_SRC].sizeimage = csize; 1374 ctx->q_data[V4L2_M2M_DST].bytesperline = max_w; 1375 ctx->q_data[V4L2_M2M_DST].sizeimage = usize; 1376 } 1377 ctx->q_data[V4L2_M2M_SRC].rect.width = max_w; 1378 ctx->q_data[V4L2_M2M_SRC].rect.height = max_h; 1379 ctx->q_data[V4L2_M2M_DST].rect.width = max_w; 1380 ctx->q_data[V4L2_M2M_DST].rect.height = max_h; 1381 1382 /* 1383 * Since the RBC2AXI logic only supports a single chroma plane, 1384 * macroblock tiling only works for to NV12 pixel format. 1385 */ 1386 ctx->tiled_map_type = GDI_LINEAR_FRAME_MAP; 1387} 1388 1389/* 1390 * Queue operations 1391 */ 1392static int coda_queue_setup(struct vb2_queue *vq, 1393 unsigned int *nbuffers, unsigned int *nplanes, 1394 unsigned int sizes[], struct device *alloc_devs[]) 1395{ 1396 struct coda_ctx *ctx = vb2_get_drv_priv(vq); 1397 struct coda_q_data *q_data; 1398 unsigned int size; 1399 1400 q_data = get_q_data(ctx, vq->type); 1401 size = q_data->sizeimage; 1402 1403 *nplanes = 1; 1404 sizes[0] = size; 1405 1406 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1407 "get %d buffer(s) of size %d each.\n", *nbuffers, size); 1408 1409 return 0; 1410} 1411 1412static int coda_buf_prepare(struct vb2_buffer *vb) 1413{ 1414 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1415 struct coda_q_data *q_data; 1416 1417 q_data = get_q_data(ctx, vb->vb2_queue->type); 1418 1419 if (vb2_plane_size(vb, 0) < q_data->sizeimage) { 1420 v4l2_warn(&ctx->dev->v4l2_dev, 1421 "%s data will not fit into plane (%lu < %lu)\n", 1422 __func__, vb2_plane_size(vb, 0), 1423 (long)q_data->sizeimage); 1424 return -EINVAL; 1425 } 1426 1427 return 0; 1428} 1429 1430static void coda_update_menu_ctrl(struct v4l2_ctrl *ctrl, int value) 1431{ 1432 if (!ctrl) 1433 return; 1434 1435 v4l2_ctrl_lock(ctrl); 1436 1437 /* 1438 * Extend the control range if the parsed stream contains a known but 1439 * unsupported value or level. 1440 */ 1441 if (value > ctrl->maximum) { 1442 __v4l2_ctrl_modify_range(ctrl, ctrl->minimum, value, 1443 ctrl->menu_skip_mask & ~(1 << value), 1444 ctrl->default_value); 1445 } else if (value < ctrl->minimum) { 1446 __v4l2_ctrl_modify_range(ctrl, value, ctrl->maximum, 1447 ctrl->menu_skip_mask & ~(1 << value), 1448 ctrl->default_value); 1449 } 1450 1451 __v4l2_ctrl_s_ctrl(ctrl, value); 1452 1453 v4l2_ctrl_unlock(ctrl); 1454} 1455 1456static void coda_update_h264_profile_ctrl(struct coda_ctx *ctx) 1457{ 1458 const char * const *profile_names; 1459 int profile; 1460 1461 profile = coda_h264_profile(ctx->params.h264_profile_idc); 1462 if (profile < 0) { 1463 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Profile: %u\n", 1464 ctx->params.h264_profile_idc); 1465 return; 1466 } 1467 1468 coda_update_menu_ctrl(ctx->h264_profile_ctrl, profile); 1469 1470 profile_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_PROFILE); 1471 1472 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Profile: %s\n", 1473 profile_names[profile]); 1474} 1475 1476static void coda_update_h264_level_ctrl(struct coda_ctx *ctx) 1477{ 1478 const char * const *level_names; 1479 int level; 1480 1481 level = coda_h264_level(ctx->params.h264_level_idc); 1482 if (level < 0) { 1483 v4l2_warn(&ctx->dev->v4l2_dev, "Invalid H264 Level: %u\n", 1484 ctx->params.h264_level_idc); 1485 return; 1486 } 1487 1488 coda_update_menu_ctrl(ctx->h264_level_ctrl, level); 1489 1490 level_names = v4l2_ctrl_get_menu(V4L2_CID_MPEG_VIDEO_H264_LEVEL); 1491 1492 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, "Parsed H264 Level: %s\n", 1493 level_names[level]); 1494} 1495 1496static void coda_buf_queue(struct vb2_buffer *vb) 1497{ 1498 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1499 struct coda_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 1500 struct vb2_queue *vq = vb->vb2_queue; 1501 struct coda_q_data *q_data; 1502 1503 q_data = get_q_data(ctx, vb->vb2_queue->type); 1504 1505 /* 1506 * In the decoder case, immediately try to copy the buffer into the 1507 * bitstream ringbuffer and mark it as ready to be dequeued. 1508 */ 1509 if (ctx->bitstream.size && vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1510 /* 1511 * For backwards compatibility, queuing an empty buffer marks 1512 * the stream end 1513 */ 1514 if (vb2_get_plane_payload(vb, 0) == 0) 1515 coda_bit_stream_end_flag(ctx); 1516 1517 if (q_data->fourcc == V4L2_PIX_FMT_H264) { 1518 /* 1519 * Unless already done, try to obtain profile_idc and 1520 * level_idc from the SPS header. This allows to decide 1521 * whether to enable reordering during sequence 1522 * initialization. 1523 */ 1524 if (!ctx->params.h264_profile_idc) { 1525 coda_sps_parse_profile(ctx, vb); 1526 coda_update_h264_profile_ctrl(ctx); 1527 coda_update_h264_level_ctrl(ctx); 1528 } 1529 } 1530 1531 mutex_lock(&ctx->bitstream_mutex); 1532 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1533 if (vb2_is_streaming(vb->vb2_queue)) 1534 /* This set buf->sequence = ctx->qsequence++ */ 1535 coda_fill_bitstream(ctx, NULL); 1536 mutex_unlock(&ctx->bitstream_mutex); 1537 } else { 1538 if (ctx->inst_type == CODA_INST_ENCODER && 1539 vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) 1540 vbuf->sequence = ctx->qsequence++; 1541 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1542 } 1543} 1544 1545int coda_alloc_aux_buf(struct coda_dev *dev, struct coda_aux_buf *buf, 1546 size_t size, const char *name, struct dentry *parent) 1547{ 1548 buf->vaddr = dma_alloc_coherent(&dev->plat_dev->dev, size, &buf->paddr, 1549 GFP_KERNEL); 1550 if (!buf->vaddr) { 1551 v4l2_err(&dev->v4l2_dev, 1552 "Failed to allocate %s buffer of size %zu\n", 1553 name, size); 1554 return -ENOMEM; 1555 } 1556 1557 buf->size = size; 1558 1559 if (name && parent) { 1560 buf->blob.data = buf->vaddr; 1561 buf->blob.size = size; 1562 buf->dentry = debugfs_create_blob(name, 0644, parent, 1563 &buf->blob); 1564 if (!buf->dentry) 1565 dev_warn(&dev->plat_dev->dev, 1566 "failed to create debugfs entry %s\n", name); 1567 } 1568 1569 return 0; 1570} 1571 1572void coda_free_aux_buf(struct coda_dev *dev, 1573 struct coda_aux_buf *buf) 1574{ 1575 if (buf->vaddr) { 1576 dma_free_coherent(&dev->plat_dev->dev, buf->size, 1577 buf->vaddr, buf->paddr); 1578 buf->vaddr = NULL; 1579 buf->size = 0; 1580 debugfs_remove(buf->dentry); 1581 buf->dentry = NULL; 1582 } 1583} 1584 1585static int coda_start_streaming(struct vb2_queue *q, unsigned int count) 1586{ 1587 struct coda_ctx *ctx = vb2_get_drv_priv(q); 1588 struct v4l2_device *v4l2_dev = &ctx->dev->v4l2_dev; 1589 struct coda_q_data *q_data_src, *q_data_dst; 1590 struct v4l2_m2m_buffer *m2m_buf, *tmp; 1591 struct vb2_v4l2_buffer *buf; 1592 struct list_head list; 1593 int ret = 0; 1594 1595 if (count < 1) 1596 return -EINVAL; 1597 1598 INIT_LIST_HEAD(&list); 1599 1600 q_data_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); 1601 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1602 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) { 1603 /* copy the buffers that were queued before streamon */ 1604 mutex_lock(&ctx->bitstream_mutex); 1605 coda_fill_bitstream(ctx, &list); 1606 mutex_unlock(&ctx->bitstream_mutex); 1607 1608 if (coda_get_bitstream_payload(ctx) < 512) { 1609 ret = -EINVAL; 1610 goto err; 1611 } 1612 } 1613 1614 ctx->streamon_out = 1; 1615 } else { 1616 ctx->streamon_cap = 1; 1617 } 1618 1619 /* Don't start the coda unless both queues are on */ 1620 if (!(ctx->streamon_out && ctx->streamon_cap)) 1621 goto out; 1622 1623 q_data_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); 1624 if ((q_data_src->rect.width != q_data_dst->width && 1625 round_up(q_data_src->rect.width, 16) != q_data_dst->width) || 1626 (q_data_src->rect.height != q_data_dst->height && 1627 round_up(q_data_src->rect.height, 16) != q_data_dst->height)) { 1628 v4l2_err(v4l2_dev, "can't convert %dx%d to %dx%d\n", 1629 q_data_src->rect.width, q_data_src->rect.height, 1630 q_data_dst->width, q_data_dst->height); 1631 ret = -EINVAL; 1632 goto err; 1633 } 1634 1635 /* Allow BIT decoder device_run with no new buffers queued */ 1636 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) 1637 v4l2_m2m_set_src_buffered(ctx->fh.m2m_ctx, true); 1638 1639 ctx->gopcounter = ctx->params.gop_size - 1; 1640 1641 ctx->codec = coda_find_codec(ctx->dev, q_data_src->fourcc, 1642 q_data_dst->fourcc); 1643 if (!ctx->codec) { 1644 v4l2_err(v4l2_dev, "couldn't tell instance type.\n"); 1645 ret = -EINVAL; 1646 goto err; 1647 } 1648 1649 if (q_data_dst->fourcc == V4L2_PIX_FMT_JPEG) 1650 ctx->params.gop_size = 1; 1651 ctx->gopcounter = ctx->params.gop_size - 1; 1652 1653 ret = ctx->ops->start_streaming(ctx); 1654 if (ctx->inst_type == CODA_INST_DECODER) { 1655 if (ret == -EAGAIN) 1656 goto out; 1657 } 1658 if (ret < 0) 1659 goto err; 1660 1661out: 1662 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1663 list_for_each_entry_safe(m2m_buf, tmp, &list, list) { 1664 list_del(&m2m_buf->list); 1665 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_DONE); 1666 } 1667 } 1668 return 0; 1669 1670err: 1671 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1672 list_for_each_entry_safe(m2m_buf, tmp, &list, list) { 1673 list_del(&m2m_buf->list); 1674 v4l2_m2m_buf_done(&m2m_buf->vb, VB2_BUF_STATE_QUEUED); 1675 } 1676 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) 1677 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED); 1678 } else { 1679 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) 1680 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_QUEUED); 1681 } 1682 return ret; 1683} 1684 1685static void coda_stop_streaming(struct vb2_queue *q) 1686{ 1687 struct coda_ctx *ctx = vb2_get_drv_priv(q); 1688 struct coda_dev *dev = ctx->dev; 1689 struct vb2_v4l2_buffer *buf; 1690 unsigned long flags; 1691 bool stop; 1692 1693 stop = ctx->streamon_out && ctx->streamon_cap; 1694 1695 if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) { 1696 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, 1697 "%s: output\n", __func__); 1698 ctx->streamon_out = 0; 1699 1700 coda_bit_stream_end_flag(ctx); 1701 1702 ctx->qsequence = 0; 1703 1704 while ((buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) 1705 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR); 1706 } else { 1707 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, 1708 "%s: capture\n", __func__); 1709 ctx->streamon_cap = 0; 1710 1711 ctx->osequence = 0; 1712 ctx->sequence_offset = 0; 1713 1714 while ((buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) 1715 v4l2_m2m_buf_done(buf, VB2_BUF_STATE_ERROR); 1716 } 1717 1718 if (stop) { 1719 struct coda_buffer_meta *meta; 1720 1721 if (ctx->ops->seq_end_work) { 1722 queue_work(dev->workqueue, &ctx->seq_end_work); 1723 flush_work(&ctx->seq_end_work); 1724 } 1725 spin_lock_irqsave(&ctx->buffer_meta_lock, flags); 1726 while (!list_empty(&ctx->buffer_meta_list)) { 1727 meta = list_first_entry(&ctx->buffer_meta_list, 1728 struct coda_buffer_meta, list); 1729 list_del(&meta->list); 1730 kfree(meta); 1731 } 1732 ctx->num_metas = 0; 1733 spin_unlock_irqrestore(&ctx->buffer_meta_lock, flags); 1734 kfifo_init(&ctx->bitstream_fifo, 1735 ctx->bitstream.vaddr, ctx->bitstream.size); 1736 ctx->runcounter = 0; 1737 ctx->aborting = 0; 1738 ctx->hold = false; 1739 } 1740 1741 if (!ctx->streamon_out && !ctx->streamon_cap) 1742 ctx->bit_stream_param &= ~CODA_BIT_STREAM_END_FLAG; 1743} 1744 1745static const struct vb2_ops coda_qops = { 1746 .queue_setup = coda_queue_setup, 1747 .buf_prepare = coda_buf_prepare, 1748 .buf_queue = coda_buf_queue, 1749 .start_streaming = coda_start_streaming, 1750 .stop_streaming = coda_stop_streaming, 1751 .wait_prepare = vb2_ops_wait_prepare, 1752 .wait_finish = vb2_ops_wait_finish, 1753}; 1754 1755static int coda_s_ctrl(struct v4l2_ctrl *ctrl) 1756{ 1757 struct coda_ctx *ctx = 1758 container_of(ctrl->handler, struct coda_ctx, ctrls); 1759 1760 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1761 "s_ctrl: id = %d, val = %d\n", ctrl->id, ctrl->val); 1762 1763 switch (ctrl->id) { 1764 case V4L2_CID_HFLIP: 1765 if (ctrl->val) 1766 ctx->params.rot_mode |= CODA_MIR_HOR; 1767 else 1768 ctx->params.rot_mode &= ~CODA_MIR_HOR; 1769 break; 1770 case V4L2_CID_VFLIP: 1771 if (ctrl->val) 1772 ctx->params.rot_mode |= CODA_MIR_VER; 1773 else 1774 ctx->params.rot_mode &= ~CODA_MIR_VER; 1775 break; 1776 case V4L2_CID_MPEG_VIDEO_BITRATE: 1777 ctx->params.bitrate = ctrl->val / 1000; 1778 break; 1779 case V4L2_CID_MPEG_VIDEO_GOP_SIZE: 1780 ctx->params.gop_size = ctrl->val; 1781 break; 1782 case V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP: 1783 ctx->params.h264_intra_qp = ctrl->val; 1784 break; 1785 case V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP: 1786 ctx->params.h264_inter_qp = ctrl->val; 1787 break; 1788 case V4L2_CID_MPEG_VIDEO_H264_MIN_QP: 1789 ctx->params.h264_min_qp = ctrl->val; 1790 break; 1791 case V4L2_CID_MPEG_VIDEO_H264_MAX_QP: 1792 ctx->params.h264_max_qp = ctrl->val; 1793 break; 1794 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA: 1795 ctx->params.h264_deblk_alpha = ctrl->val; 1796 break; 1797 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA: 1798 ctx->params.h264_deblk_beta = ctrl->val; 1799 break; 1800 case V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE: 1801 ctx->params.h264_deblk_enabled = (ctrl->val == 1802 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED); 1803 break; 1804 case V4L2_CID_MPEG_VIDEO_H264_PROFILE: 1805 /* TODO: switch between baseline and constrained baseline */ 1806 if (ctx->inst_type == CODA_INST_ENCODER) 1807 ctx->params.h264_profile_idc = 66; 1808 break; 1809 case V4L2_CID_MPEG_VIDEO_H264_LEVEL: 1810 /* nothing to do, this is set by the encoder */ 1811 break; 1812 case V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP: 1813 ctx->params.mpeg4_intra_qp = ctrl->val; 1814 break; 1815 case V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP: 1816 ctx->params.mpeg4_inter_qp = ctrl->val; 1817 break; 1818 case V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE: 1819 case V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL: 1820 /* nothing to do, these are fixed */ 1821 break; 1822 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE: 1823 ctx->params.slice_mode = ctrl->val; 1824 break; 1825 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB: 1826 ctx->params.slice_max_mb = ctrl->val; 1827 break; 1828 case V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES: 1829 ctx->params.slice_max_bits = ctrl->val * 8; 1830 break; 1831 case V4L2_CID_MPEG_VIDEO_HEADER_MODE: 1832 break; 1833 case V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB: 1834 ctx->params.intra_refresh = ctrl->val; 1835 break; 1836 case V4L2_CID_MPEG_VIDEO_FORCE_KEY_FRAME: 1837 ctx->params.force_ipicture = true; 1838 break; 1839 case V4L2_CID_JPEG_COMPRESSION_QUALITY: 1840 coda_set_jpeg_compression_quality(ctx, ctrl->val); 1841 break; 1842 case V4L2_CID_JPEG_RESTART_INTERVAL: 1843 ctx->params.jpeg_restart_interval = ctrl->val; 1844 break; 1845 case V4L2_CID_MPEG_VIDEO_VBV_DELAY: 1846 ctx->params.vbv_delay = ctrl->val; 1847 break; 1848 case V4L2_CID_MPEG_VIDEO_VBV_SIZE: 1849 ctx->params.vbv_size = min(ctrl->val * 8192, 0x7fffffff); 1850 break; 1851 default: 1852 v4l2_dbg(1, coda_debug, &ctx->dev->v4l2_dev, 1853 "Invalid control, id=%d, val=%d\n", 1854 ctrl->id, ctrl->val); 1855 return -EINVAL; 1856 } 1857 1858 return 0; 1859} 1860 1861static const struct v4l2_ctrl_ops coda_ctrl_ops = { 1862 .s_ctrl = coda_s_ctrl, 1863}; 1864 1865static void coda_encode_ctrls(struct coda_ctx *ctx) 1866{ 1867 int max_gop_size = (ctx->dev->devtype->product == CODA_DX6) ? 60 : 99; 1868 1869 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1870 V4L2_CID_MPEG_VIDEO_BITRATE, 0, 32767000, 1000, 0); 1871 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1872 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, max_gop_size, 1, 16); 1873 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1874 V4L2_CID_MPEG_VIDEO_H264_I_FRAME_QP, 0, 51, 1, 25); 1875 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1876 V4L2_CID_MPEG_VIDEO_H264_P_FRAME_QP, 0, 51, 1, 25); 1877 if (ctx->dev->devtype->product != CODA_960) { 1878 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1879 V4L2_CID_MPEG_VIDEO_H264_MIN_QP, 0, 51, 1, 12); 1880 } 1881 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1882 V4L2_CID_MPEG_VIDEO_H264_MAX_QP, 0, 51, 1, 51); 1883 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1884 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_ALPHA, 0, 15, 1, 0); 1885 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1886 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_BETA, 0, 15, 1, 0); 1887 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1888 V4L2_CID_MPEG_VIDEO_H264_LOOP_FILTER_MODE, 1889 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_DISABLED, 0x0, 1890 V4L2_MPEG_VIDEO_H264_LOOP_FILTER_MODE_ENABLED); 1891 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1892 V4L2_CID_MPEG_VIDEO_H264_PROFILE, 1893 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE, 0x0, 1894 V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE); 1895 if (ctx->dev->devtype->product == CODA_HX4 || 1896 ctx->dev->devtype->product == CODA_7541) { 1897 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1898 V4L2_CID_MPEG_VIDEO_H264_LEVEL, 1899 V4L2_MPEG_VIDEO_H264_LEVEL_3_1, 1900 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) | 1901 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) | 1902 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1)), 1903 V4L2_MPEG_VIDEO_H264_LEVEL_3_1); 1904 } 1905 if (ctx->dev->devtype->product == CODA_960) { 1906 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1907 V4L2_CID_MPEG_VIDEO_H264_LEVEL, 1908 V4L2_MPEG_VIDEO_H264_LEVEL_4_0, 1909 ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) | 1910 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) | 1911 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) | 1912 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) | 1913 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)), 1914 V4L2_MPEG_VIDEO_H264_LEVEL_4_0); 1915 } 1916 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1917 V4L2_CID_MPEG_VIDEO_MPEG4_I_FRAME_QP, 1, 31, 1, 2); 1918 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1919 V4L2_CID_MPEG_VIDEO_MPEG4_P_FRAME_QP, 1, 31, 1, 2); 1920 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1921 V4L2_CID_MPEG_VIDEO_MPEG4_PROFILE, 1922 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE, 0x0, 1923 V4L2_MPEG_VIDEO_MPEG4_PROFILE_SIMPLE); 1924 if (ctx->dev->devtype->product == CODA_HX4 || 1925 ctx->dev->devtype->product == CODA_7541 || 1926 ctx->dev->devtype->product == CODA_960) { 1927 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1928 V4L2_CID_MPEG_VIDEO_MPEG4_LEVEL, 1929 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5, 1930 ~(1 << V4L2_MPEG_VIDEO_MPEG4_LEVEL_5), 1931 V4L2_MPEG_VIDEO_MPEG4_LEVEL_5); 1932 } 1933 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1934 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MODE, 1935 V4L2_MPEG_VIDEO_MULTI_SICE_MODE_MAX_BYTES, 0x0, 1936 V4L2_MPEG_VIDEO_MULTI_SLICE_MODE_SINGLE); 1937 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1938 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_MB, 1, 0x3fffffff, 1, 1); 1939 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1940 V4L2_CID_MPEG_VIDEO_MULTI_SLICE_MAX_BYTES, 1, 0x3fffffff, 1, 1941 500); 1942 v4l2_ctrl_new_std_menu(&ctx->ctrls, &coda_ctrl_ops, 1943 V4L2_CID_MPEG_VIDEO_HEADER_MODE, 1944 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME, 1945 (1 << V4L2_MPEG_VIDEO_HEADER_MODE_SEPARATE), 1946 V4L2_MPEG_VIDEO_HEADER_MODE_JOINED_WITH_1ST_FRAME); 1947 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1948 V4L2_CID_MPEG_VIDEO_CYCLIC_INTRA_REFRESH_MB, 0, 1949 1920 * 1088 / 256, 1, 0); 1950 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1951 V4L2_CID_MPEG_VIDEO_VBV_DELAY, 0, 0x7fff, 1, 0); 1952 /* 1953 * The maximum VBV size value is 0x7fffffff bits, 1954 * one bit less than 262144 KiB 1955 */ 1956 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1957 V4L2_CID_MPEG_VIDEO_VBV_SIZE, 0, 262144, 1, 0); 1958} 1959 1960static void coda_jpeg_encode_ctrls(struct coda_ctx *ctx) 1961{ 1962 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1963 V4L2_CID_JPEG_COMPRESSION_QUALITY, 5, 100, 1, 50); 1964 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 1965 V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 1, 0); 1966} 1967 1968static void coda_decode_ctrls(struct coda_ctx *ctx) 1969{ 1970 u64 mask; 1971 u8 max; 1972 1973 ctx->h264_profile_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls, 1974 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_PROFILE, 1975 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH, 1976 ~((1 << V4L2_MPEG_VIDEO_H264_PROFILE_BASELINE) | 1977 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_MAIN) | 1978 (1 << V4L2_MPEG_VIDEO_H264_PROFILE_HIGH)), 1979 V4L2_MPEG_VIDEO_H264_PROFILE_HIGH); 1980 if (ctx->h264_profile_ctrl) 1981 ctx->h264_profile_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1982 1983 if (ctx->dev->devtype->product == CODA_HX4 || 1984 ctx->dev->devtype->product == CODA_7541) { 1985 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_0; 1986 mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) | 1987 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) | 1988 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) | 1989 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) | 1990 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0)); 1991 } else if (ctx->dev->devtype->product == CODA_960) { 1992 max = V4L2_MPEG_VIDEO_H264_LEVEL_4_1; 1993 mask = ~((1 << V4L2_MPEG_VIDEO_H264_LEVEL_2_0) | 1994 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_0) | 1995 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_1) | 1996 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_3_2) | 1997 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_0) | 1998 (1 << V4L2_MPEG_VIDEO_H264_LEVEL_4_1)); 1999 } else { 2000 return; 2001 } 2002 ctx->h264_level_ctrl = v4l2_ctrl_new_std_menu(&ctx->ctrls, 2003 &coda_ctrl_ops, V4L2_CID_MPEG_VIDEO_H264_LEVEL, max, mask, 2004 max); 2005 if (ctx->h264_level_ctrl) 2006 ctx->h264_level_ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 2007} 2008 2009static int coda_ctrls_setup(struct coda_ctx *ctx) 2010{ 2011 v4l2_ctrl_handler_init(&ctx->ctrls, 2); 2012 2013 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 2014 V4L2_CID_HFLIP, 0, 1, 1, 0); 2015 v4l2_ctrl_new_std(&ctx->ctrls, &coda_ctrl_ops, 2016 V4L2_CID_VFLIP, 0, 1, 1, 0); 2017 if (ctx->inst_type == CODA_INST_ENCODER) { 2018 if (ctx->cvd->dst_formats[0] == V4L2_PIX_FMT_JPEG) 2019 coda_jpeg_encode_ctrls(ctx); 2020 else 2021 coda_encode_ctrls(ctx); 2022 } else { 2023 if (ctx->cvd->src_formats[0] == V4L2_PIX_FMT_H264) 2024 coda_decode_ctrls(ctx); 2025 } 2026 2027 if (ctx->ctrls.error) { 2028 v4l2_err(&ctx->dev->v4l2_dev, 2029 "control initialization error (%d)", 2030 ctx->ctrls.error); 2031 return -EINVAL; 2032 } 2033 2034 return v4l2_ctrl_handler_setup(&ctx->ctrls); 2035} 2036 2037static int coda_queue_init(struct coda_ctx *ctx, struct vb2_queue *vq) 2038{ 2039 vq->drv_priv = ctx; 2040 vq->ops = &coda_qops; 2041 vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 2042 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 2043 vq->lock = &ctx->dev->dev_mutex; 2044 /* One way to indicate end-of-stream for coda is to set the 2045 * bytesused == 0. However by default videobuf2 handles bytesused 2046 * equal to 0 as a special case and changes its value to the size 2047 * of the buffer. Set the allow_zero_bytesused flag, so 2048 * that videobuf2 will keep the value of bytesused intact. 2049 */ 2050 vq->allow_zero_bytesused = 1; 2051 /* 2052 * We might be fine with no buffers on some of the queues, but that 2053 * would need to be reflected in job_ready(). Currently we expect all 2054 * queues to have at least one buffer queued. 2055 */ 2056 vq->min_buffers_needed = 1; 2057 vq->dev = &ctx->dev->plat_dev->dev; 2058 2059 return vb2_queue_init(vq); 2060} 2061 2062int coda_encoder_queue_init(void *priv, struct vb2_queue *src_vq, 2063 struct vb2_queue *dst_vq) 2064{ 2065 int ret; 2066 2067 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 2068 src_vq->io_modes = VB2_DMABUF | VB2_MMAP; 2069 src_vq->mem_ops = &vb2_dma_contig_memops; 2070 2071 ret = coda_queue_init(priv, src_vq); 2072 if (ret) 2073 return ret; 2074 2075 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2076 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; 2077 dst_vq->mem_ops = &vb2_dma_contig_memops; 2078 2079 return coda_queue_init(priv, dst_vq); 2080} 2081 2082int coda_decoder_queue_init(void *priv, struct vb2_queue *src_vq, 2083 struct vb2_queue *dst_vq) 2084{ 2085 int ret; 2086 2087 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; 2088 src_vq->io_modes = VB2_DMABUF | VB2_MMAP | VB2_USERPTR; 2089 src_vq->mem_ops = &vb2_vmalloc_memops; 2090 2091 ret = coda_queue_init(priv, src_vq); 2092 if (ret) 2093 return ret; 2094 2095 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 2096 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; 2097 dst_vq->mem_ops = &vb2_dma_contig_memops; 2098 2099 return coda_queue_init(priv, dst_vq); 2100} 2101 2102static int coda_next_free_instance(struct coda_dev *dev) 2103{ 2104 int idx = ffz(dev->instance_mask); 2105 2106 if ((idx < 0) || 2107 (dev->devtype->product == CODA_DX6 && idx > CODADX6_MAX_INSTANCES)) 2108 return -EBUSY; 2109 2110 return idx; 2111} 2112 2113/* 2114 * File operations 2115 */ 2116 2117static int coda_open(struct file *file) 2118{ 2119 struct video_device *vdev = video_devdata(file); 2120 struct coda_dev *dev = video_get_drvdata(vdev); 2121 struct coda_ctx *ctx = NULL; 2122 char *name; 2123 int ret; 2124 int idx; 2125 2126 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 2127 if (!ctx) 2128 return -ENOMEM; 2129 2130 idx = coda_next_free_instance(dev); 2131 if (idx < 0) { 2132 ret = idx; 2133 goto err_coda_max; 2134 } 2135 set_bit(idx, &dev->instance_mask); 2136 2137 name = kasprintf(GFP_KERNEL, "context%d", idx); 2138 if (!name) { 2139 ret = -ENOMEM; 2140 goto err_coda_name_init; 2141 } 2142 2143 ctx->debugfs_entry = debugfs_create_dir(name, dev->debugfs_root); 2144 kfree(name); 2145 2146 ctx->cvd = to_coda_video_device(vdev); 2147 ctx->inst_type = ctx->cvd->type; 2148 ctx->ops = ctx->cvd->ops; 2149 ctx->use_bit = !ctx->cvd->direct; 2150 init_completion(&ctx->completion); 2151 INIT_WORK(&ctx->pic_run_work, coda_pic_run_work); 2152 if (ctx->ops->seq_end_work) 2153 INIT_WORK(&ctx->seq_end_work, ctx->ops->seq_end_work); 2154 v4l2_fh_init(&ctx->fh, video_devdata(file)); 2155 file->private_data = &ctx->fh; 2156 v4l2_fh_add(&ctx->fh); 2157 ctx->dev = dev; 2158 ctx->idx = idx; 2159 switch (dev->devtype->product) { 2160 case CODA_960: 2161 /* 2162 * Enabling the BWB when decoding can hang the firmware with 2163 * certain streams. The issue was tracked as ENGR00293425 by 2164 * Freescale. As a workaround, disable BWB for all decoders. 2165 * The enable_bwb module parameter allows to override this. 2166 */ 2167 if (enable_bwb || ctx->inst_type == CODA_INST_ENCODER) 2168 ctx->frame_mem_ctrl = CODA9_FRAME_ENABLE_BWB; 2169 /* fallthrough */ 2170 case CODA_HX4: 2171 case CODA_7541: 2172 ctx->reg_idx = 0; 2173 break; 2174 default: 2175 ctx->reg_idx = idx; 2176 } 2177 if (ctx->dev->vdoa && !disable_vdoa) { 2178 ctx->vdoa = vdoa_context_create(dev->vdoa); 2179 if (!ctx->vdoa) 2180 v4l2_warn(&dev->v4l2_dev, 2181 "Failed to create vdoa context: not using vdoa"); 2182 } 2183 ctx->use_vdoa = false; 2184 2185 /* Power up and upload firmware if necessary */ 2186 ret = pm_runtime_get_sync(&dev->plat_dev->dev); 2187 if (ret < 0) { 2188 v4l2_err(&dev->v4l2_dev, "failed to power up: %d\n", ret); 2189 goto err_pm_get; 2190 } 2191 2192 ret = clk_prepare_enable(dev->clk_per); 2193 if (ret) 2194 goto err_clk_per; 2195 2196 ret = clk_prepare_enable(dev->clk_ahb); 2197 if (ret) 2198 goto err_clk_ahb; 2199 2200 set_default_params(ctx); 2201 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, 2202 ctx->ops->queue_init); 2203 if (IS_ERR(ctx->fh.m2m_ctx)) { 2204 ret = PTR_ERR(ctx->fh.m2m_ctx); 2205 2206 v4l2_err(&dev->v4l2_dev, "%s return error (%d)\n", 2207 __func__, ret); 2208 goto err_ctx_init; 2209 } 2210 2211 ret = coda_ctrls_setup(ctx); 2212 if (ret) { 2213 v4l2_err(&dev->v4l2_dev, "failed to setup coda controls\n"); 2214 goto err_ctrls_setup; 2215 } 2216 2217 ctx->fh.ctrl_handler = &ctx->ctrls; 2218 2219 mutex_init(&ctx->bitstream_mutex); 2220 mutex_init(&ctx->buffer_mutex); 2221 INIT_LIST_HEAD(&ctx->buffer_meta_list); 2222 spin_lock_init(&ctx->buffer_meta_lock); 2223 2224 mutex_lock(&dev->dev_mutex); 2225 list_add(&ctx->list, &dev->instances); 2226 mutex_unlock(&dev->dev_mutex); 2227 2228 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Created instance %d (%p)\n", 2229 ctx->idx, ctx); 2230 2231 return 0; 2232 2233err_ctrls_setup: 2234 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 2235err_ctx_init: 2236 clk_disable_unprepare(dev->clk_ahb); 2237err_clk_ahb: 2238 clk_disable_unprepare(dev->clk_per); 2239err_clk_per: 2240 pm_runtime_put_sync(&dev->plat_dev->dev); 2241err_pm_get: 2242 v4l2_fh_del(&ctx->fh); 2243 v4l2_fh_exit(&ctx->fh); 2244 clear_bit(ctx->idx, &dev->instance_mask); 2245err_coda_name_init: 2246err_coda_max: 2247 kfree(ctx); 2248 return ret; 2249} 2250 2251static int coda_release(struct file *file) 2252{ 2253 struct coda_dev *dev = video_drvdata(file); 2254 struct coda_ctx *ctx = fh_to_ctx(file->private_data); 2255 2256 v4l2_dbg(1, coda_debug, &dev->v4l2_dev, "Releasing instance %p\n", 2257 ctx); 2258 2259 if (ctx->inst_type == CODA_INST_DECODER && ctx->use_bit) 2260 coda_bit_stream_end_flag(ctx); 2261 2262 /* If this instance is running, call .job_abort and wait for it to end */ 2263 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 2264 2265 if (ctx->vdoa) 2266 vdoa_context_destroy(ctx->vdoa); 2267 2268 /* In case the instance was not running, we still need to call SEQ_END */ 2269 if (ctx->ops->seq_end_work) { 2270 queue_work(dev->workqueue, &ctx->seq_end_work); 2271 flush_work(&ctx->seq_end_work); 2272 } 2273 2274 mutex_lock(&dev->dev_mutex); 2275 list_del(&ctx->list); 2276 mutex_unlock(&dev->dev_mutex); 2277 2278 if (ctx->dev->devtype->product == CODA_DX6) 2279 coda_free_aux_buf(dev, &ctx->workbuf); 2280 2281 v4l2_ctrl_handler_free(&ctx->ctrls); 2282 clk_disable_unprepare(dev->clk_ahb); 2283 clk_disable_unprepare(dev->clk_per); 2284 pm_runtime_put_sync(&dev->plat_dev->dev); 2285 v4l2_fh_del(&ctx->fh); 2286 v4l2_fh_exit(&ctx->fh); 2287 clear_bit(ctx->idx, &dev->instance_mask); 2288 if (ctx->ops->release) 2289 ctx->ops->release(ctx); 2290 debugfs_remove_recursive(ctx->debugfs_entry); 2291 kfree(ctx); 2292 2293 return 0; 2294} 2295 2296static const struct v4l2_file_operations coda_fops = { 2297 .owner = THIS_MODULE, 2298 .open = coda_open, 2299 .release = coda_release, 2300 .poll = v4l2_m2m_fop_poll, 2301 .unlocked_ioctl = video_ioctl2, 2302 .mmap = v4l2_m2m_fop_mmap, 2303}; 2304 2305static int coda_hw_init(struct coda_dev *dev) 2306{ 2307 u32 data; 2308 u16 *p; 2309 int i, ret; 2310 2311 ret = clk_prepare_enable(dev->clk_per); 2312 if (ret) 2313 goto err_clk_per; 2314 2315 ret = clk_prepare_enable(dev->clk_ahb); 2316 if (ret) 2317 goto err_clk_ahb; 2318 2319 reset_control_reset(dev->rstc); 2320 2321 /* 2322 * Copy the first CODA_ISRAM_SIZE in the internal SRAM. 2323 * The 16-bit chars in the code buffer are in memory access 2324 * order, re-sort them to CODA order for register download. 2325 * Data in this SRAM survives a reboot. 2326 */ 2327 p = (u16 *)dev->codebuf.vaddr; 2328 if (dev->devtype->product == CODA_DX6) { 2329 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { 2330 data = CODA_DOWN_ADDRESS_SET(i) | 2331 CODA_DOWN_DATA_SET(p[i ^ 1]); 2332 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); 2333 } 2334 } else { 2335 for (i = 0; i < (CODA_ISRAM_SIZE / 2); i++) { 2336 data = CODA_DOWN_ADDRESS_SET(i) | 2337 CODA_DOWN_DATA_SET(p[round_down(i, 4) + 2338 3 - (i % 4)]); 2339 coda_write(dev, data, CODA_REG_BIT_CODE_DOWN); 2340 } 2341 } 2342 2343 /* Clear registers */ 2344 for (i = 0; i < 64; i++) 2345 coda_write(dev, 0, CODA_REG_BIT_CODE_BUF_ADDR + i * 4); 2346 2347 /* Tell the BIT where to find everything it needs */ 2348 if (dev->devtype->product == CODA_960 || 2349 dev->devtype->product == CODA_7541 || 2350 dev->devtype->product == CODA_HX4) { 2351 coda_write(dev, dev->tempbuf.paddr, 2352 CODA_REG_BIT_TEMP_BUF_ADDR); 2353 coda_write(dev, 0, CODA_REG_BIT_BIT_STREAM_PARAM); 2354 } else { 2355 coda_write(dev, dev->workbuf.paddr, 2356 CODA_REG_BIT_WORK_BUF_ADDR); 2357 } 2358 coda_write(dev, dev->codebuf.paddr, 2359 CODA_REG_BIT_CODE_BUF_ADDR); 2360 coda_write(dev, 0, CODA_REG_BIT_CODE_RUN); 2361 2362 /* Set default values */ 2363 switch (dev->devtype->product) { 2364 case CODA_DX6: 2365 coda_write(dev, CODADX6_STREAM_BUF_PIC_FLUSH, 2366 CODA_REG_BIT_STREAM_CTRL); 2367 break; 2368 default: 2369 coda_write(dev, CODA7_STREAM_BUF_PIC_FLUSH, 2370 CODA_REG_BIT_STREAM_CTRL); 2371 } 2372 if (dev->devtype->product == CODA_960) 2373 coda_write(dev, CODA9_FRAME_ENABLE_BWB, 2374 CODA_REG_BIT_FRAME_MEM_CTRL); 2375 else 2376 coda_write(dev, 0, CODA_REG_BIT_FRAME_MEM_CTRL); 2377 2378 if (dev->devtype->product != CODA_DX6) 2379 coda_write(dev, 0, CODA7_REG_BIT_AXI_SRAM_USE); 2380 2381 coda_write(dev, CODA_INT_INTERRUPT_ENABLE, 2382 CODA_REG_BIT_INT_ENABLE); 2383 2384 /* Reset VPU and start processor */ 2385 data = coda_read(dev, CODA_REG_BIT_CODE_RESET); 2386 data |= CODA_REG_RESET_ENABLE; 2387 coda_write(dev, data, CODA_REG_BIT_CODE_RESET); 2388 udelay(10); 2389 data &= ~CODA_REG_RESET_ENABLE; 2390 coda_write(dev, data, CODA_REG_BIT_CODE_RESET); 2391 coda_write(dev, CODA_REG_RUN_ENABLE, CODA_REG_BIT_CODE_RUN); 2392 2393 clk_disable_unprepare(dev->clk_ahb); 2394 clk_disable_unprepare(dev->clk_per); 2395 2396 return 0; 2397 2398err_clk_ahb: 2399 clk_disable_unprepare(dev->clk_per); 2400err_clk_per: 2401 return ret; 2402} 2403 2404static int coda_register_device(struct coda_dev *dev, int i) 2405{ 2406 struct video_device *vfd = &dev->vfd[i]; 2407 2408 if (i >= dev->devtype->num_vdevs) 2409 return -EINVAL; 2410 2411 strscpy(vfd->name, dev->devtype->vdevs[i]->name, sizeof(vfd->name)); 2412 vfd->fops = &coda_fops; 2413 vfd->ioctl_ops = &coda_ioctl_ops; 2414 vfd->release = video_device_release_empty, 2415 vfd->lock = &dev->dev_mutex; 2416 vfd->v4l2_dev = &dev->v4l2_dev; 2417 vfd->vfl_dir = VFL_DIR_M2M; 2418 video_set_drvdata(vfd, dev); 2419 2420 /* Not applicable, use the selection API instead */ 2421 v4l2_disable_ioctl(vfd, VIDIOC_CROPCAP); 2422 v4l2_disable_ioctl(vfd, VIDIOC_G_CROP); 2423 v4l2_disable_ioctl(vfd, VIDIOC_S_CROP); 2424 2425 return video_register_device(vfd, VFL_TYPE_GRABBER, 0); 2426} 2427 2428static void coda_copy_firmware(struct coda_dev *dev, const u8 * const buf, 2429 size_t size) 2430{ 2431 u32 *src = (u32 *)buf; 2432 2433 /* Check if the firmware has a 16-byte Freescale header, skip it */ 2434 if (buf[0] == 'M' && buf[1] == 'X') 2435 src += 4; 2436 /* 2437 * Check whether the firmware is in native order or pre-reordered for 2438 * memory access. The first instruction opcode always is 0xe40e. 2439 */ 2440 if (__le16_to_cpup((__le16 *)src) == 0xe40e) { 2441 u32 *dst = dev->codebuf.vaddr; 2442 int i; 2443 2444 /* Firmware in native order, reorder while copying */ 2445 if (dev->devtype->product == CODA_DX6) { 2446 for (i = 0; i < (size - 16) / 4; i++) 2447 dst[i] = (src[i] << 16) | (src[i] >> 16); 2448 } else { 2449 for (i = 0; i < (size - 16) / 4; i += 2) { 2450 dst[i] = (src[i + 1] << 16) | (src[i + 1] >> 16); 2451 dst[i + 1] = (src[i] << 16) | (src[i] >> 16); 2452 } 2453 } 2454 } else { 2455 /* Copy the already reordered firmware image */ 2456 memcpy(dev->codebuf.vaddr, src, size); 2457 } 2458} 2459 2460static void coda_fw_callback(const struct firmware *fw, void *context); 2461 2462static int coda_firmware_request(struct coda_dev *dev) 2463{ 2464 char *fw; 2465 2466 if (dev->firmware >= ARRAY_SIZE(dev->devtype->firmware)) 2467 return -EINVAL; 2468 2469 fw = dev->devtype->firmware[dev->firmware]; 2470 2471 dev_dbg(&dev->plat_dev->dev, "requesting firmware '%s' for %s\n", fw, 2472 coda_product_name(dev->devtype->product)); 2473 2474 return request_firmware_nowait(THIS_MODULE, true, fw, 2475 &dev->plat_dev->dev, GFP_KERNEL, dev, 2476 coda_fw_callback); 2477} 2478 2479static void coda_fw_callback(const struct firmware *fw, void *context) 2480{ 2481 struct coda_dev *dev = context; 2482 struct platform_device *pdev = dev->plat_dev; 2483 int i, ret; 2484 2485 if (!fw) { 2486 dev->firmware++; 2487 ret = coda_firmware_request(dev); 2488 if (ret < 0) { 2489 v4l2_err(&dev->v4l2_dev, "firmware request failed\n"); 2490 goto put_pm; 2491 } 2492 return; 2493 } 2494 if (dev->firmware > 0) { 2495 /* 2496 * Since we can't suppress warnings for failed asynchronous 2497 * firmware requests, report that the fallback firmware was 2498 * found. 2499 */ 2500 dev_info(&pdev->dev, "Using fallback firmware %s\n", 2501 dev->devtype->firmware[dev->firmware]); 2502 } 2503 2504 /* allocate auxiliary per-device code buffer for the BIT processor */ 2505 ret = coda_alloc_aux_buf(dev, &dev->codebuf, fw->size, "codebuf", 2506 dev->debugfs_root); 2507 if (ret < 0) 2508 goto put_pm; 2509 2510 coda_copy_firmware(dev, fw->data, fw->size); 2511 release_firmware(fw); 2512 2513 ret = coda_hw_init(dev); 2514 if (ret < 0) { 2515 v4l2_err(&dev->v4l2_dev, "HW initialization failed\n"); 2516 goto put_pm; 2517 } 2518 2519 ret = coda_check_firmware(dev); 2520 if (ret < 0) 2521 goto put_pm; 2522 2523 dev->m2m_dev = v4l2_m2m_init(&coda_m2m_ops); 2524 if (IS_ERR(dev->m2m_dev)) { 2525 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); 2526 goto put_pm; 2527 } 2528 2529 for (i = 0; i < dev->devtype->num_vdevs; i++) { 2530 ret = coda_register_device(dev, i); 2531 if (ret) { 2532 v4l2_err(&dev->v4l2_dev, 2533 "Failed to register %s video device: %d\n", 2534 dev->devtype->vdevs[i]->name, ret); 2535 goto rel_vfd; 2536 } 2537 } 2538 2539 v4l2_info(&dev->v4l2_dev, "codec registered as /dev/video[%d-%d]\n", 2540 dev->vfd[0].num, dev->vfd[i - 1].num); 2541 2542 pm_runtime_put_sync(&pdev->dev); 2543 return; 2544 2545rel_vfd: 2546 while (--i >= 0) 2547 video_unregister_device(&dev->vfd[i]); 2548 v4l2_m2m_release(dev->m2m_dev); 2549put_pm: 2550 pm_runtime_put_sync(&pdev->dev); 2551} 2552 2553enum coda_platform { 2554 CODA_IMX27, 2555 CODA_IMX51, 2556 CODA_IMX53, 2557 CODA_IMX6Q, 2558 CODA_IMX6DL, 2559}; 2560 2561static const struct coda_devtype coda_devdata[] = { 2562 [CODA_IMX27] = { 2563 .firmware = { 2564 "vpu_fw_imx27_TO2.bin", 2565 "vpu/vpu_fw_imx27_TO2.bin", 2566 "v4l-codadx6-imx27.bin" 2567 }, 2568 .product = CODA_DX6, 2569 .codecs = codadx6_codecs, 2570 .num_codecs = ARRAY_SIZE(codadx6_codecs), 2571 .vdevs = codadx6_video_devices, 2572 .num_vdevs = ARRAY_SIZE(codadx6_video_devices), 2573 .workbuf_size = 288 * 1024 + FMO_SLICE_SAVE_BUF_SIZE * 8 * 1024, 2574 .iram_size = 0xb000, 2575 }, 2576 [CODA_IMX51] = { 2577 .firmware = { 2578 "vpu_fw_imx51.bin", 2579 "vpu/vpu_fw_imx51.bin", 2580 "v4l-codahx4-imx51.bin" 2581 }, 2582 .product = CODA_HX4, 2583 .codecs = codahx4_codecs, 2584 .num_codecs = ARRAY_SIZE(codahx4_codecs), 2585 .vdevs = codahx4_video_devices, 2586 .num_vdevs = ARRAY_SIZE(codahx4_video_devices), 2587 .workbuf_size = 128 * 1024, 2588 .tempbuf_size = 304 * 1024, 2589 .iram_size = 0x14000, 2590 }, 2591 [CODA_IMX53] = { 2592 .firmware = { 2593 "vpu_fw_imx53.bin", 2594 "vpu/vpu_fw_imx53.bin", 2595 "v4l-coda7541-imx53.bin" 2596 }, 2597 .product = CODA_7541, 2598 .codecs = coda7_codecs, 2599 .num_codecs = ARRAY_SIZE(coda7_codecs), 2600 .vdevs = coda7_video_devices, 2601 .num_vdevs = ARRAY_SIZE(coda7_video_devices), 2602 .workbuf_size = 128 * 1024, 2603 .tempbuf_size = 304 * 1024, 2604 .iram_size = 0x14000, 2605 }, 2606 [CODA_IMX6Q] = { 2607 .firmware = { 2608 "vpu_fw_imx6q.bin", 2609 "vpu/vpu_fw_imx6q.bin", 2610 "v4l-coda960-imx6q.bin" 2611 }, 2612 .product = CODA_960, 2613 .codecs = coda9_codecs, 2614 .num_codecs = ARRAY_SIZE(coda9_codecs), 2615 .vdevs = coda9_video_devices, 2616 .num_vdevs = ARRAY_SIZE(coda9_video_devices), 2617 .workbuf_size = 80 * 1024, 2618 .tempbuf_size = 204 * 1024, 2619 .iram_size = 0x21000, 2620 }, 2621 [CODA_IMX6DL] = { 2622 .firmware = { 2623 "vpu_fw_imx6d.bin", 2624 "vpu/vpu_fw_imx6d.bin", 2625 "v4l-coda960-imx6dl.bin" 2626 }, 2627 .product = CODA_960, 2628 .codecs = coda9_codecs, 2629 .num_codecs = ARRAY_SIZE(coda9_codecs), 2630 .vdevs = coda9_video_devices, 2631 .num_vdevs = ARRAY_SIZE(coda9_video_devices), 2632 .workbuf_size = 80 * 1024, 2633 .tempbuf_size = 204 * 1024, 2634 .iram_size = 0x1f000, /* leave 4k for suspend code */ 2635 }, 2636}; 2637 2638static const struct platform_device_id coda_platform_ids[] = { 2639 { .name = "coda-imx27", .driver_data = CODA_IMX27 }, 2640 { /* sentinel */ } 2641}; 2642MODULE_DEVICE_TABLE(platform, coda_platform_ids); 2643 2644#ifdef CONFIG_OF 2645static const struct of_device_id coda_dt_ids[] = { 2646 { .compatible = "fsl,imx27-vpu", .data = &coda_devdata[CODA_IMX27] }, 2647 { .compatible = "fsl,imx51-vpu", .data = &coda_devdata[CODA_IMX51] }, 2648 { .compatible = "fsl,imx53-vpu", .data = &coda_devdata[CODA_IMX53] }, 2649 { .compatible = "fsl,imx6q-vpu", .data = &coda_devdata[CODA_IMX6Q] }, 2650 { .compatible = "fsl,imx6dl-vpu", .data = &coda_devdata[CODA_IMX6DL] }, 2651 { /* sentinel */ } 2652}; 2653MODULE_DEVICE_TABLE(of, coda_dt_ids); 2654#endif 2655 2656static int coda_probe(struct platform_device *pdev) 2657{ 2658 const struct of_device_id *of_id = 2659 of_match_device(of_match_ptr(coda_dt_ids), &pdev->dev); 2660 const struct platform_device_id *pdev_id; 2661 struct coda_platform_data *pdata = pdev->dev.platform_data; 2662 struct device_node *np = pdev->dev.of_node; 2663 struct gen_pool *pool; 2664 struct coda_dev *dev; 2665 struct resource *res; 2666 int ret, irq; 2667 2668 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); 2669 if (!dev) 2670 return -ENOMEM; 2671 2672 pdev_id = of_id ? of_id->data : platform_get_device_id(pdev); 2673 2674 if (of_id) 2675 dev->devtype = of_id->data; 2676 else if (pdev_id) 2677 dev->devtype = &coda_devdata[pdev_id->driver_data]; 2678 else 2679 return -EINVAL; 2680 2681 spin_lock_init(&dev->irqlock); 2682 INIT_LIST_HEAD(&dev->instances); 2683 2684 dev->plat_dev = pdev; 2685 dev->clk_per = devm_clk_get(&pdev->dev, "per"); 2686 if (IS_ERR(dev->clk_per)) { 2687 dev_err(&pdev->dev, "Could not get per clock\n"); 2688 return PTR_ERR(dev->clk_per); 2689 } 2690 2691 dev->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 2692 if (IS_ERR(dev->clk_ahb)) { 2693 dev_err(&pdev->dev, "Could not get ahb clock\n"); 2694 return PTR_ERR(dev->clk_ahb); 2695 } 2696 2697 /* Get memory for physical registers */ 2698 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2699 dev->regs_base = devm_ioremap_resource(&pdev->dev, res); 2700 if (IS_ERR(dev->regs_base)) 2701 return PTR_ERR(dev->regs_base); 2702 2703 /* IRQ */ 2704 irq = platform_get_irq_byname(pdev, "bit"); 2705 if (irq < 0) 2706 irq = platform_get_irq(pdev, 0); 2707 if (irq < 0) { 2708 dev_err(&pdev->dev, "failed to get irq resource\n"); 2709 return irq; 2710 } 2711 2712 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL, coda_irq_handler, 2713 IRQF_ONESHOT, dev_name(&pdev->dev), dev); 2714 if (ret < 0) { 2715 dev_err(&pdev->dev, "failed to request irq: %d\n", ret); 2716 return ret; 2717 } 2718 2719 dev->rstc = devm_reset_control_get_optional_exclusive(&pdev->dev, 2720 NULL); 2721 if (IS_ERR(dev->rstc)) { 2722 ret = PTR_ERR(dev->rstc); 2723 dev_err(&pdev->dev, "failed get reset control: %d\n", ret); 2724 return ret; 2725 } 2726 2727 /* Get IRAM pool from device tree or platform data */ 2728 pool = of_gen_pool_get(np, "iram", 0); 2729 if (!pool && pdata) 2730 pool = gen_pool_get(pdata->iram_dev, NULL); 2731 if (!pool) { 2732 dev_err(&pdev->dev, "iram pool not available\n"); 2733 return -ENOMEM; 2734 } 2735 dev->iram_pool = pool; 2736 2737 /* Get vdoa_data if supported by the platform */ 2738 dev->vdoa = coda_get_vdoa_data(); 2739 if (PTR_ERR(dev->vdoa) == -EPROBE_DEFER) 2740 return -EPROBE_DEFER; 2741 2742 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); 2743 if (ret) 2744 return ret; 2745 2746 mutex_init(&dev->dev_mutex); 2747 mutex_init(&dev->coda_mutex); 2748 2749 dev->debugfs_root = debugfs_create_dir("coda", NULL); 2750 if (!dev->debugfs_root) 2751 dev_warn(&pdev->dev, "failed to create debugfs root\n"); 2752 2753 /* allocate auxiliary per-device buffers for the BIT processor */ 2754 if (dev->devtype->product == CODA_DX6) { 2755 ret = coda_alloc_aux_buf(dev, &dev->workbuf, 2756 dev->devtype->workbuf_size, "workbuf", 2757 dev->debugfs_root); 2758 if (ret < 0) 2759 goto err_v4l2_register; 2760 } 2761 2762 if (dev->devtype->tempbuf_size) { 2763 ret = coda_alloc_aux_buf(dev, &dev->tempbuf, 2764 dev->devtype->tempbuf_size, "tempbuf", 2765 dev->debugfs_root); 2766 if (ret < 0) 2767 goto err_v4l2_register; 2768 } 2769 2770 dev->iram.size = dev->devtype->iram_size; 2771 dev->iram.vaddr = gen_pool_dma_alloc(dev->iram_pool, dev->iram.size, 2772 &dev->iram.paddr); 2773 if (!dev->iram.vaddr) { 2774 dev_warn(&pdev->dev, "unable to alloc iram\n"); 2775 } else { 2776 memset(dev->iram.vaddr, 0, dev->iram.size); 2777 dev->iram.blob.data = dev->iram.vaddr; 2778 dev->iram.blob.size = dev->iram.size; 2779 dev->iram.dentry = debugfs_create_blob("iram", 0644, 2780 dev->debugfs_root, 2781 &dev->iram.blob); 2782 } 2783 2784 dev->workqueue = alloc_workqueue("coda", WQ_UNBOUND | WQ_MEM_RECLAIM, 1); 2785 if (!dev->workqueue) { 2786 dev_err(&pdev->dev, "unable to alloc workqueue\n"); 2787 ret = -ENOMEM; 2788 goto err_v4l2_register; 2789 } 2790 2791 platform_set_drvdata(pdev, dev); 2792 2793 /* 2794 * Start activated so we can directly call coda_hw_init in 2795 * coda_fw_callback regardless of whether CONFIG_PM is 2796 * enabled or whether the device is associated with a PM domain. 2797 */ 2798 pm_runtime_get_noresume(&pdev->dev); 2799 pm_runtime_set_active(&pdev->dev); 2800 pm_runtime_enable(&pdev->dev); 2801 2802 ret = coda_firmware_request(dev); 2803 if (ret) 2804 goto err_alloc_workqueue; 2805 return 0; 2806 2807err_alloc_workqueue: 2808 destroy_workqueue(dev->workqueue); 2809err_v4l2_register: 2810 v4l2_device_unregister(&dev->v4l2_dev); 2811 return ret; 2812} 2813 2814static int coda_remove(struct platform_device *pdev) 2815{ 2816 struct coda_dev *dev = platform_get_drvdata(pdev); 2817 int i; 2818 2819 for (i = 0; i < ARRAY_SIZE(dev->vfd); i++) { 2820 if (video_get_drvdata(&dev->vfd[i])) 2821 video_unregister_device(&dev->vfd[i]); 2822 } 2823 if (dev->m2m_dev) 2824 v4l2_m2m_release(dev->m2m_dev); 2825 pm_runtime_disable(&pdev->dev); 2826 v4l2_device_unregister(&dev->v4l2_dev); 2827 destroy_workqueue(dev->workqueue); 2828 if (dev->iram.vaddr) 2829 gen_pool_free(dev->iram_pool, (unsigned long)dev->iram.vaddr, 2830 dev->iram.size); 2831 coda_free_aux_buf(dev, &dev->codebuf); 2832 coda_free_aux_buf(dev, &dev->tempbuf); 2833 coda_free_aux_buf(dev, &dev->workbuf); 2834 debugfs_remove_recursive(dev->debugfs_root); 2835 return 0; 2836} 2837 2838#ifdef CONFIG_PM 2839static int coda_runtime_resume(struct device *dev) 2840{ 2841 struct coda_dev *cdev = dev_get_drvdata(dev); 2842 int ret = 0; 2843 2844 if (dev->pm_domain && cdev->codebuf.vaddr) { 2845 ret = coda_hw_init(cdev); 2846 if (ret) 2847 v4l2_err(&cdev->v4l2_dev, "HW initialization failed\n"); 2848 } 2849 2850 return ret; 2851} 2852#endif 2853 2854static const struct dev_pm_ops coda_pm_ops = { 2855 SET_RUNTIME_PM_OPS(NULL, coda_runtime_resume, NULL) 2856}; 2857 2858static struct platform_driver coda_driver = { 2859 .probe = coda_probe, 2860 .remove = coda_remove, 2861 .driver = { 2862 .name = CODA_NAME, 2863 .of_match_table = of_match_ptr(coda_dt_ids), 2864 .pm = &coda_pm_ops, 2865 }, 2866 .id_table = coda_platform_ids, 2867}; 2868 2869module_platform_driver(coda_driver); 2870 2871MODULE_LICENSE("GPL"); 2872MODULE_AUTHOR("Javier Martin <javier.martin@vista-silicon.com>"); 2873MODULE_DESCRIPTION("Coda multi-standard codec V4L2 driver");