1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Imagination E5010 JPEG Encoder driver. 4 * 5 * TODO: Add MMU and memory tiling support 6 * 7 * Copyright (C) 2023 Texas Instruments Incorporated - https://www.ti.com/ 8 * 9 * Author: David Huang <d-huang@ti.com> 10 * Author: Devarsh Thakkar <devarsht@ti.com> 11 */ 12 13 #include <linux/clk.h> 14 #include <linux/dma-mapping.h> 15 #include <linux/err.h> 16 #include <linux/interrupt.h> 17 #include <linux/ioctl.h> 18 #include <linux/module.h> 19 #include <linux/of_device.h> 20 #include <linux/pm_runtime.h> 21 #include <media/jpeg.h> 22 #include <media/v4l2-common.h> 23 #include <media/v4l2-ctrls.h> 24 #include <media/v4l2-device.h> 25 #include <media/v4l2-event.h> 26 #include <media/v4l2-ioctl.h> 27 #include <media/v4l2-jpeg.h> 28 #include <media/v4l2-rect.h> 29 #include <media/v4l2-mem2mem.h> 30 #include <media/videobuf2-dma-contig.h> 31 #include <media/videobuf2-v4l2.h> 32 #include "e5010-jpeg-enc.h" 33 #include "e5010-jpeg-enc-hw.h" 34 35 /* forward declarations */ 36 static const struct of_device_id e5010_of_match[]; 37 38 static const struct v4l2_file_operations e5010_fops; 39 40 static const struct v4l2_ioctl_ops e5010_ioctl_ops; 41 42 static const struct vb2_ops e5010_video_ops; 43 44 static const struct v4l2_m2m_ops e5010_m2m_ops; 45 46 static struct e5010_fmt e5010_formats[] = { 47 { 48 .fourcc = V4L2_PIX_FMT_NV12, 49 .num_planes = 1, 50 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 51 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420, 52 .chroma_order = CHROMA_ORDER_CB_CR, 53 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 54 MIN_DIMENSION, MAX_DIMENSION, 8 }, 55 }, 56 { 57 .fourcc = V4L2_PIX_FMT_NV12M, 58 .num_planes = 2, 59 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 60 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420, 61 .chroma_order = CHROMA_ORDER_CB_CR, 62 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 63 MIN_DIMENSION, MAX_DIMENSION, 8 }, 64 }, 65 { 66 .fourcc = V4L2_PIX_FMT_NV21, 67 .num_planes = 1, 68 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 69 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420, 70 .chroma_order = CHROMA_ORDER_CR_CB, 71 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 72 MIN_DIMENSION, MAX_DIMENSION, 8 }, 73 }, 74 { 75 .fourcc = V4L2_PIX_FMT_NV21M, 76 .num_planes = 2, 77 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 78 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_420, 79 .chroma_order = CHROMA_ORDER_CR_CB, 80 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 81 MIN_DIMENSION, MAX_DIMENSION, 8 }, 82 }, 83 { 84 .fourcc = V4L2_PIX_FMT_NV16, 85 .num_planes = 1, 86 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 87 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422, 88 .chroma_order = CHROMA_ORDER_CB_CR, 89 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 90 MIN_DIMENSION, MAX_DIMENSION, 8 }, 91 }, 92 { 93 .fourcc = V4L2_PIX_FMT_NV16M, 94 .num_planes = 2, 95 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 96 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422, 97 .chroma_order = CHROMA_ORDER_CB_CR, 98 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 99 MIN_DIMENSION, MAX_DIMENSION, 8 }, 100 101 }, 102 { 103 .fourcc = V4L2_PIX_FMT_NV61, 104 .num_planes = 1, 105 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 106 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422, 107 .chroma_order = CHROMA_ORDER_CR_CB, 108 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 109 MIN_DIMENSION, MAX_DIMENSION, 8 }, 110 }, 111 { 112 .fourcc = V4L2_PIX_FMT_NV61M, 113 .num_planes = 2, 114 .type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE, 115 .subsampling = V4L2_JPEG_CHROMA_SUBSAMPLING_422, 116 .chroma_order = CHROMA_ORDER_CR_CB, 117 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 64, 118 MIN_DIMENSION, MAX_DIMENSION, 8 }, 119 }, 120 { 121 .fourcc = V4L2_PIX_FMT_JPEG, 122 .num_planes = 1, 123 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE, 124 .subsampling = 0, 125 .chroma_order = 0, 126 .frmsize = { MIN_DIMENSION, MAX_DIMENSION, 16, 127 MIN_DIMENSION, MAX_DIMENSION, 8 }, 128 }, 129 }; 130 131 static unsigned int debug; 132 module_param(debug, uint, 0644); 133 MODULE_PARM_DESC(debug, "debug level"); 134 135 #define dprintk(dev, lvl, fmt, arg...) \ 136 v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg) 137 138 static const struct v4l2_event e5010_eos_event = { 139 .type = V4L2_EVENT_EOS 140 }; 141 142 static const char *type_name(enum v4l2_buf_type type) 143 { 144 switch (type) { 145 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: 146 return "Output"; 147 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: 148 return "Capture"; 149 default: 150 return "Invalid"; 151 } 152 } 153 154 static struct e5010_q_data *get_queue(struct e5010_context *ctx, enum v4l2_buf_type type) 155 { 156 return (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ? &ctx->out_queue : &ctx->cap_queue; 157 } 158 159 static void calculate_qp_tables(struct e5010_context *ctx) 160 { 161 long long luminosity, contrast; 162 int quality, i; 163 164 quality = 50 - ctx->quality; 165 166 luminosity = LUMINOSITY * quality / 50; 167 contrast = CONTRAST * quality / 50; 168 169 if (quality > 0) { 170 luminosity *= INCREASE; 171 contrast *= INCREASE; 172 } 173 174 for (i = 0; i < V4L2_JPEG_PIXELS_IN_BLOCK; i++) { 175 long long delta = v4l2_jpeg_ref_table_chroma_qt[i] * contrast + luminosity; 176 int val = (int)(v4l2_jpeg_ref_table_chroma_qt[i] + delta); 177 178 clamp(val, 1, 255); 179 ctx->chroma_qp[i] = quality == -50 ? 1 : val; 180 181 delta = v4l2_jpeg_ref_table_luma_qt[i] * contrast + luminosity; 182 val = (int)(v4l2_jpeg_ref_table_luma_qt[i] + delta); 183 clamp(val, 1, 255); 184 ctx->luma_qp[i] = quality == -50 ? 1 : val; 185 } 186 187 ctx->update_qp = true; 188 } 189 190 static int update_qp_tables(struct e5010_context *ctx) 191 { 192 struct e5010_dev *e5010 = ctx->e5010; 193 int i, ret = 0; 194 u32 lvalue, cvalue; 195 196 lvalue = 0; 197 cvalue = 0; 198 199 for (i = 0; i < QP_TABLE_SIZE; i++) { 200 lvalue |= ctx->luma_qp[i] << (8 * (i % 4)); 201 cvalue |= ctx->chroma_qp[i] << (8 * (i % 4)); 202 if (i % 4 == 3) { 203 ret |= e5010_hw_set_qpvalue(e5010->core_base, 204 JASPER_LUMA_QUANTIZATION_TABLE0_OFFSET 205 + QP_TABLE_FIELD_OFFSET * ((i - 3) / 4), 206 lvalue); 207 ret |= e5010_hw_set_qpvalue(e5010->core_base, 208 JASPER_CHROMA_QUANTIZATION_TABLE0_OFFSET 209 + QP_TABLE_FIELD_OFFSET * ((i - 3) / 4), 210 cvalue); 211 lvalue = 0; 212 cvalue = 0; 213 } 214 } 215 216 return ret; 217 } 218 219 static int e5010_set_input_subsampling(void __iomem *core_base, int subsampling) 220 { 221 switch (subsampling) { 222 case V4L2_JPEG_CHROMA_SUBSAMPLING_420: 223 return e5010_hw_set_input_subsampling(core_base, SUBSAMPLING_420); 224 case V4L2_JPEG_CHROMA_SUBSAMPLING_422: 225 return e5010_hw_set_input_subsampling(core_base, SUBSAMPLING_422); 226 default: 227 return -EINVAL; 228 }; 229 } 230 231 static int e5010_querycap(struct file *file, void *priv, struct v4l2_capability *cap) 232 { 233 strscpy(cap->driver, E5010_MODULE_NAME, sizeof(cap->driver)); 234 strscpy(cap->card, E5010_MODULE_NAME, sizeof(cap->card)); 235 236 return 0; 237 } 238 239 static struct e5010_fmt *find_format(struct v4l2_format *f) 240 { 241 int i; 242 243 for (i = 0; i < ARRAY_SIZE(e5010_formats); ++i) { 244 if (e5010_formats[i].fourcc == f->fmt.pix_mp.pixelformat && 245 e5010_formats[i].type == f->type) 246 return &e5010_formats[i]; 247 } 248 249 return NULL; 250 } 251 252 static int e5010_enum_fmt(struct file *file, void *priv, struct v4l2_fmtdesc *f) 253 { 254 int i, index = 0; 255 struct e5010_fmt *fmt = NULL; 256 struct e5010_context *ctx = to_e5010_context(file); 257 258 if (!V4L2_TYPE_IS_MULTIPLANAR(f->type)) { 259 v4l2_err(&ctx->e5010->v4l2_dev, "ENUMFMT with Invalid type: %d\n", f->type); 260 return -EINVAL; 261 } 262 263 for (i = 0; i < ARRAY_SIZE(e5010_formats); ++i) { 264 if (e5010_formats[i].type == f->type) { 265 if (index == f->index) { 266 fmt = &e5010_formats[i]; 267 break; 268 } 269 index++; 270 } 271 } 272 273 if (!fmt) 274 return -EINVAL; 275 276 f->pixelformat = fmt->fourcc; 277 return 0; 278 } 279 280 static int e5010_g_fmt(struct file *file, void *priv, struct v4l2_format *f) 281 { 282 struct e5010_context *ctx = to_e5010_context(file); 283 struct e5010_q_data *queue; 284 int i; 285 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 286 struct v4l2_plane_pix_format *plane_fmt = pix_mp->plane_fmt; 287 288 queue = get_queue(ctx, f->type); 289 290 pix_mp->flags = 0; 291 pix_mp->field = V4L2_FIELD_NONE; 292 pix_mp->pixelformat = queue->fmt->fourcc; 293 pix_mp->width = queue->width_adjusted; 294 pix_mp->height = queue->height_adjusted; 295 pix_mp->num_planes = queue->fmt->num_planes; 296 297 if (V4L2_TYPE_IS_OUTPUT(f->type)) { 298 if (!pix_mp->colorspace) 299 pix_mp->colorspace = V4L2_COLORSPACE_SRGB; 300 301 for (i = 0; i < queue->fmt->num_planes; i++) { 302 plane_fmt[i].sizeimage = queue->sizeimage[i]; 303 plane_fmt[i].bytesperline = queue->bytesperline[i]; 304 } 305 306 } else { 307 pix_mp->colorspace = V4L2_COLORSPACE_JPEG; 308 plane_fmt[0].bytesperline = 0; 309 plane_fmt[0].sizeimage = queue->sizeimage[0]; 310 } 311 pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 312 pix_mp->xfer_func = V4L2_XFER_FUNC_DEFAULT; 313 pix_mp->quantization = V4L2_QUANTIZATION_DEFAULT; 314 315 return 0; 316 } 317 318 static int e5010_jpeg_try_fmt(struct v4l2_format *f, struct e5010_context *ctx) 319 { 320 struct e5010_fmt *fmt; 321 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 322 struct v4l2_plane_pix_format *plane_fmt = pix_mp->plane_fmt; 323 324 fmt = find_format(f); 325 if (!fmt) { 326 if (V4L2_TYPE_IS_OUTPUT(f->type)) 327 pix_mp->pixelformat = V4L2_PIX_FMT_NV12; 328 else 329 pix_mp->pixelformat = V4L2_PIX_FMT_JPEG; 330 fmt = find_format(f); 331 if (!fmt) 332 return -EINVAL; 333 } 334 335 if (V4L2_TYPE_IS_OUTPUT(f->type)) { 336 if (!pix_mp->colorspace) 337 pix_mp->colorspace = V4L2_COLORSPACE_JPEG; 338 if (!pix_mp->ycbcr_enc) 339 pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 340 if (!pix_mp->quantization) 341 pix_mp->quantization = V4L2_QUANTIZATION_DEFAULT; 342 if (!pix_mp->xfer_func) 343 pix_mp->xfer_func = V4L2_XFER_FUNC_DEFAULT; 344 345 v4l2_apply_frmsize_constraints(&pix_mp->width, 346 &pix_mp->height, 347 &fmt->frmsize); 348 349 v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat, 350 pix_mp->width, pix_mp->height); 351 352 } else { 353 pix_mp->colorspace = V4L2_COLORSPACE_JPEG; 354 pix_mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT; 355 pix_mp->quantization = V4L2_QUANTIZATION_DEFAULT; 356 pix_mp->xfer_func = V4L2_XFER_FUNC_DEFAULT; 357 v4l2_apply_frmsize_constraints(&pix_mp->width, 358 &pix_mp->height, 359 &fmt->frmsize); 360 plane_fmt[0].sizeimage = pix_mp->width * pix_mp->height * JPEG_MAX_BYTES_PER_PIXEL; 361 plane_fmt[0].sizeimage += HEADER_SIZE; 362 plane_fmt[0].bytesperline = 0; 363 pix_mp->pixelformat = fmt->fourcc; 364 pix_mp->num_planes = fmt->num_planes; 365 } 366 pix_mp->flags = 0; 367 pix_mp->field = V4L2_FIELD_NONE; 368 369 dprintk(ctx->e5010, 2, 370 "ctx: 0x%p: format type %s:, wxh: %dx%d (plane0 : %d bytes, plane1 : %d bytes),fmt: %c%c%c%c\n", 371 ctx, type_name(f->type), pix_mp->width, pix_mp->height, 372 plane_fmt[0].sizeimage, plane_fmt[1].sizeimage, 373 (fmt->fourcc & 0xff), 374 (fmt->fourcc >> 8) & 0xff, 375 (fmt->fourcc >> 16) & 0xff, 376 (fmt->fourcc >> 24) & 0xff); 377 378 return 0; 379 } 380 381 static int e5010_try_fmt(struct file *file, void *priv, struct v4l2_format *f) 382 { 383 struct e5010_context *ctx = to_e5010_context(file); 384 385 return e5010_jpeg_try_fmt(f, ctx); 386 } 387 388 static int e5010_s_fmt(struct file *file, void *priv, struct v4l2_format *f) 389 { 390 struct e5010_context *ctx = to_e5010_context(file); 391 struct vb2_queue *vq; 392 int ret = 0, i = 0; 393 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 394 struct v4l2_plane_pix_format *plane_fmt = pix_mp->plane_fmt; 395 struct e5010_q_data *queue; 396 struct e5010_fmt *fmt; 397 398 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 399 if (!vq) 400 return -EINVAL; 401 402 if (vb2_is_busy(vq)) { 403 v4l2_err(&ctx->e5010->v4l2_dev, "queue busy\n"); 404 return -EBUSY; 405 } 406 407 ret = e5010_jpeg_try_fmt(f, ctx); 408 if (ret) 409 return ret; 410 411 fmt = find_format(f); 412 queue = get_queue(ctx, f->type); 413 414 queue->fmt = fmt; 415 queue->width = pix_mp->width; 416 queue->height = pix_mp->height; 417 418 if (V4L2_TYPE_IS_OUTPUT(f->type)) { 419 for (i = 0; i < fmt->num_planes; i++) { 420 queue->bytesperline[i] = plane_fmt[i].bytesperline; 421 queue->sizeimage[i] = plane_fmt[i].sizeimage; 422 } 423 queue->crop.left = 0; 424 queue->crop.top = 0; 425 queue->crop.width = queue->width; 426 queue->crop.height = queue->height; 427 } else { 428 queue->sizeimage[0] = plane_fmt[0].sizeimage; 429 queue->sizeimage[1] = 0; 430 queue->bytesperline[0] = 0; 431 queue->bytesperline[1] = 0; 432 } 433 434 return 0; 435 } 436 437 static int e5010_enum_framesizes(struct file *file, void *priv, struct v4l2_frmsizeenum *fsize) 438 { 439 struct v4l2_format f; 440 struct e5010_fmt *fmt; 441 442 if (fsize->index != 0) 443 return -EINVAL; 444 445 f.fmt.pix_mp.pixelformat = fsize->pixel_format; 446 if (f.fmt.pix_mp.pixelformat == V4L2_PIX_FMT_JPEG) 447 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 448 else 449 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 450 451 fmt = find_format(&f); 452 if (!fmt) 453 return -EINVAL; 454 455 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE; 456 fsize->stepwise = fmt->frmsize; 457 fsize->reserved[0] = 0; 458 fsize->reserved[1] = 0; 459 460 return 0; 461 } 462 463 static int e5010_g_selection(struct file *file, void *fh, struct v4l2_selection *s) 464 { 465 struct e5010_context *ctx = to_e5010_context(file); 466 struct e5010_q_data *queue; 467 468 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 469 return -EINVAL; 470 471 queue = get_queue(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 472 473 switch (s->target) { 474 case V4L2_SEL_TGT_CROP_DEFAULT: 475 case V4L2_SEL_TGT_CROP_BOUNDS: 476 s->r.left = 0; 477 s->r.top = 0; 478 s->r.width = queue->width; 479 s->r.height = queue->height; 480 break; 481 case V4L2_SEL_TGT_CROP: 482 memcpy(&s->r, &queue->crop, sizeof(s->r)); 483 break; 484 default: 485 return -EINVAL; 486 } 487 488 return 0; 489 } 490 491 static int e5010_s_selection(struct file *file, void *fh, struct v4l2_selection *s) 492 { 493 struct e5010_context *ctx = to_e5010_context(file); 494 struct e5010_q_data *queue; 495 struct vb2_queue *vq; 496 struct v4l2_rect base_rect; 497 498 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, s->type); 499 if (!vq) 500 return -EINVAL; 501 502 if (vb2_is_streaming(vq)) 503 return -EBUSY; 504 505 if (s->target != V4L2_SEL_TGT_CROP || 506 s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 507 return -EINVAL; 508 509 queue = get_queue(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE); 510 base_rect.top = 0; 511 base_rect.left = 0; 512 base_rect.width = queue->width; 513 base_rect.height = queue->height; 514 515 switch (s->flags) { 516 case 0: 517 s->r.width = round_down(s->r.width, queue->fmt->frmsize.step_width); 518 s->r.height = round_down(s->r.height, queue->fmt->frmsize.step_height); 519 s->r.left = round_down(s->r.left, queue->fmt->frmsize.step_width); 520 s->r.top = round_down(s->r.top, 2); 521 522 if (s->r.left + s->r.width > queue->width) 523 s->r.width = round_down(s->r.width + s->r.left - queue->width, 524 queue->fmt->frmsize.step_width); 525 if (s->r.top + s->r.height > queue->height) 526 s->r.top = round_down(s->r.top + s->r.height - queue->height, 2); 527 break; 528 case V4L2_SEL_FLAG_GE: 529 s->r.width = round_up(s->r.width, queue->fmt->frmsize.step_width); 530 s->r.height = round_up(s->r.height, queue->fmt->frmsize.step_height); 531 s->r.left = round_up(s->r.left, queue->fmt->frmsize.step_width); 532 s->r.top = round_up(s->r.top, 2); 533 break; 534 case V4L2_SEL_FLAG_LE: 535 s->r.width = round_down(s->r.width, queue->fmt->frmsize.step_width); 536 s->r.height = round_down(s->r.height, queue->fmt->frmsize.step_height); 537 s->r.left = round_down(s->r.left, queue->fmt->frmsize.step_width); 538 s->r.top = round_down(s->r.top, 2); 539 break; 540 case V4L2_SEL_FLAG_LE | V4L2_SEL_FLAG_GE: 541 if (!IS_ALIGNED(s->r.width, queue->fmt->frmsize.step_width) || 542 !IS_ALIGNED(s->r.height, queue->fmt->frmsize.step_height) || 543 !IS_ALIGNED(s->r.left, queue->fmt->frmsize.step_width) || 544 !IS_ALIGNED(s->r.top, 2)) 545 return -ERANGE; 546 break; 547 default: 548 return -EINVAL; 549 } 550 551 if (!v4l2_rect_enclosed(&s->r, &base_rect)) 552 return -ERANGE; 553 554 memcpy(&queue->crop, &s->r, sizeof(s->r)); 555 556 if (!v4l2_rect_equal(&s->r, &base_rect)) 557 queue->crop_set = true; 558 559 dprintk(ctx->e5010, 2, "ctx: 0x%p: crop rectangle: w: %d, h : %d, l : %d, t : %d\n", 560 ctx, queue->crop.width, queue->crop.height, queue->crop.left, queue->crop.top); 561 562 return 0; 563 } 564 565 static int e5010_subscribe_event(struct v4l2_fh *fh, const struct v4l2_event_subscription *sub) 566 { 567 switch (sub->type) { 568 case V4L2_EVENT_EOS: 569 return v4l2_event_subscribe(fh, sub, 0, NULL); 570 case V4L2_EVENT_CTRL: 571 return v4l2_ctrl_subscribe_event(fh, sub); 572 default: 573 return -EINVAL; 574 } 575 576 return 0; 577 } 578 579 static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq) 580 { 581 struct e5010_context *ctx = priv; 582 struct e5010_dev *e5010 = ctx->e5010; 583 int ret = 0; 584 585 /* src_vq */ 586 memset(src_vq, 0, sizeof(*src_vq)); 587 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 588 src_vq->io_modes = VB2_MMAP | VB2_DMABUF; 589 src_vq->drv_priv = ctx; 590 src_vq->buf_struct_size = sizeof(struct e5010_buffer); 591 src_vq->ops = &e5010_video_ops; 592 src_vq->mem_ops = &vb2_dma_contig_memops; 593 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 594 src_vq->lock = &e5010->mutex; 595 src_vq->dev = e5010->v4l2_dev.dev; 596 597 ret = vb2_queue_init(src_vq); 598 if (ret) 599 return ret; 600 601 /* dst_vq */ 602 memset(dst_vq, 0, sizeof(*dst_vq)); 603 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 604 dst_vq->io_modes = VB2_MMAP | VB2_DMABUF; 605 dst_vq->drv_priv = ctx; 606 dst_vq->buf_struct_size = sizeof(struct e5010_buffer); 607 dst_vq->ops = &e5010_video_ops; 608 dst_vq->mem_ops = &vb2_dma_contig_memops; 609 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 610 dst_vq->lock = &e5010->mutex; 611 dst_vq->dev = e5010->v4l2_dev.dev; 612 613 ret = vb2_queue_init(dst_vq); 614 if (ret) { 615 vb2_queue_release(src_vq); 616 return ret; 617 } 618 619 return 0; 620 } 621 622 static int e5010_s_ctrl(struct v4l2_ctrl *ctrl) 623 { 624 struct e5010_context *ctx = 625 container_of(ctrl->handler, struct e5010_context, ctrl_handler); 626 627 switch (ctrl->id) { 628 case V4L2_CID_JPEG_COMPRESSION_QUALITY: 629 ctx->quality = ctrl->val; 630 calculate_qp_tables(ctx); 631 dprintk(ctx->e5010, 2, "ctx: 0x%p compression quality set to : %d\n", ctx, 632 ctx->quality); 633 break; 634 default: 635 return -EINVAL; 636 } 637 638 return 0; 639 } 640 641 static const struct v4l2_ctrl_ops e5010_ctrl_ops = { 642 .s_ctrl = e5010_s_ctrl, 643 }; 644 645 static void e5010_encode_ctrls(struct e5010_context *ctx) 646 { 647 v4l2_ctrl_new_std(&ctx->ctrl_handler, &e5010_ctrl_ops, 648 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 100, 1, 75); 649 } 650 651 static int e5010_ctrls_setup(struct e5010_context *ctx) 652 { 653 int err; 654 655 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 1); 656 657 e5010_encode_ctrls(ctx); 658 659 if (ctx->ctrl_handler.error) { 660 err = ctx->ctrl_handler.error; 661 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 662 663 return err; 664 } 665 666 err = v4l2_ctrl_handler_setup(&ctx->ctrl_handler); 667 if (err) 668 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 669 670 return err; 671 } 672 673 static void e5010_jpeg_set_default_params(struct e5010_context *ctx) 674 { 675 struct e5010_q_data *queue; 676 struct v4l2_format f; 677 struct e5010_fmt *fmt; 678 struct v4l2_pix_format_mplane *pix_mp = &f.fmt.pix_mp; 679 struct v4l2_plane_pix_format *plane_fmt = pix_mp->plane_fmt; 680 int i = 0; 681 682 f.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 683 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_NV12; 684 fmt = find_format(&f); 685 queue = &ctx->out_queue; 686 queue->fmt = fmt; 687 queue->width = DEFAULT_WIDTH; 688 queue->height = DEFAULT_HEIGHT; 689 pix_mp->width = queue->width; 690 pix_mp->height = queue->height; 691 queue->crop.left = 0; 692 queue->crop.top = 0; 693 queue->crop.width = queue->width; 694 queue->crop.height = queue->height; 695 v4l2_apply_frmsize_constraints(&pix_mp->width, 696 &pix_mp->height, 697 &fmt->frmsize); 698 v4l2_fill_pixfmt_mp(pix_mp, pix_mp->pixelformat, 699 pix_mp->width, pix_mp->height); 700 for (i = 0; i < fmt->num_planes; i++) { 701 queue->bytesperline[i] = plane_fmt[i].bytesperline; 702 queue->sizeimage[i] = plane_fmt[i].sizeimage; 703 } 704 queue->width_adjusted = pix_mp->width; 705 queue->height_adjusted = pix_mp->height; 706 707 f.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 708 f.fmt.pix_mp.pixelformat = V4L2_PIX_FMT_JPEG; 709 fmt = find_format(&f); 710 queue = &ctx->cap_queue; 711 queue->fmt = fmt; 712 queue->width = DEFAULT_WIDTH; 713 queue->height = DEFAULT_HEIGHT; 714 pix_mp->width = queue->width; 715 pix_mp->height = queue->height; 716 v4l2_apply_frmsize_constraints(&pix_mp->width, 717 &pix_mp->height, 718 &fmt->frmsize); 719 queue->sizeimage[0] = pix_mp->width * pix_mp->height * JPEG_MAX_BYTES_PER_PIXEL; 720 queue->sizeimage[0] += HEADER_SIZE; 721 queue->sizeimage[1] = 0; 722 queue->bytesperline[0] = 0; 723 queue->bytesperline[1] = 0; 724 queue->width_adjusted = pix_mp->width; 725 queue->height_adjusted = pix_mp->height; 726 } 727 728 static int e5010_open(struct file *file) 729 { 730 struct e5010_dev *e5010 = video_drvdata(file); 731 struct video_device *vdev = video_devdata(file); 732 struct e5010_context *ctx; 733 int ret = 0; 734 735 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 736 if (!ctx) 737 return -ENOMEM; 738 739 if (mutex_lock_interruptible(&e5010->mutex)) { 740 ret = -ERESTARTSYS; 741 goto free; 742 } 743 744 v4l2_fh_init(&ctx->fh, vdev); 745 v4l2_fh_add(&ctx->fh, file); 746 747 ctx->e5010 = e5010; 748 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(e5010->m2m_dev, ctx, queue_init); 749 if (IS_ERR(ctx->fh.m2m_ctx)) { 750 v4l2_err(&e5010->v4l2_dev, "failed to init m2m ctx\n"); 751 ret = PTR_ERR(ctx->fh.m2m_ctx); 752 goto exit; 753 } 754 755 ret = e5010_ctrls_setup(ctx); 756 if (ret) { 757 v4l2_err(&e5010->v4l2_dev, "failed to setup e5010 jpeg controls\n"); 758 goto err_ctrls_setup; 759 } 760 ctx->fh.ctrl_handler = &ctx->ctrl_handler; 761 762 e5010_jpeg_set_default_params(ctx); 763 764 dprintk(e5010, 1, "Created instance: 0x%p, m2m_ctx: 0x%p\n", ctx, ctx->fh.m2m_ctx); 765 766 mutex_unlock(&e5010->mutex); 767 return 0; 768 769 err_ctrls_setup: 770 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 771 exit: 772 v4l2_fh_del(&ctx->fh, file); 773 v4l2_fh_exit(&ctx->fh); 774 mutex_unlock(&e5010->mutex); 775 free: 776 kfree(ctx); 777 return ret; 778 } 779 780 static int e5010_release(struct file *file) 781 { 782 struct e5010_dev *e5010 = video_drvdata(file); 783 struct e5010_context *ctx = to_e5010_context(file); 784 785 dprintk(e5010, 1, "Releasing instance: 0x%p, m2m_ctx: 0x%p\n", ctx, ctx->fh.m2m_ctx); 786 mutex_lock(&e5010->mutex); 787 v4l2_ctrl_handler_free(&ctx->ctrl_handler); 788 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 789 v4l2_fh_del(&ctx->fh, file); 790 v4l2_fh_exit(&ctx->fh); 791 kfree(ctx); 792 mutex_unlock(&e5010->mutex); 793 794 return 0; 795 } 796 797 static void header_write(struct e5010_context *ctx, u8 *addr, unsigned int *offset, 798 unsigned int no_bytes, unsigned long bits) 799 { 800 u8 *w_addr = addr + *offset; 801 int i; 802 803 if ((*offset + no_bytes) > HEADER_SIZE) { 804 v4l2_warn(&ctx->e5010->v4l2_dev, "%s: %s: %d: Problem writing header. %d > HEADER_SIZE %d\n", 805 __FILE__, __func__, __LINE__, *offset + no_bytes, HEADER_SIZE); 806 return; 807 } 808 809 for (i = no_bytes - 1; i >= 0; i--) 810 *(w_addr++) = ((u8 *)&bits)[i]; 811 812 *offset += no_bytes; 813 } 814 815 static void encode_marker_segment(struct e5010_context *ctx, void *addr, unsigned int *offset) 816 { 817 u8 *buffer = (u8 *)addr; 818 int i; 819 820 header_write(ctx, buffer, offset, 2, START_OF_IMAGE); 821 header_write(ctx, buffer, offset, 2, DQT_MARKER); 822 header_write(ctx, buffer, offset, 3, LQPQ << 4); 823 for (i = 0; i < V4L2_JPEG_PIXELS_IN_BLOCK; i++) 824 header_write(ctx, buffer, offset, 1, ctx->luma_qp[v4l2_jpeg_zigzag_scan_index[i]]); 825 826 header_write(ctx, buffer, offset, 2, DQT_MARKER); 827 header_write(ctx, buffer, offset, 3, (LQPQ << 4) | 1); 828 for (i = 0; i < V4L2_JPEG_PIXELS_IN_BLOCK; i++) 829 header_write(ctx, buffer, offset, 1, 830 ctx->chroma_qp[v4l2_jpeg_zigzag_scan_index[i]]); 831 832 /* Huffman tables */ 833 header_write(ctx, buffer, offset, 2, DHT_MARKER); 834 header_write(ctx, buffer, offset, 2, LH_DC); 835 header_write(ctx, buffer, offset, 1, V4L2_JPEG_LUM_HT | V4L2_JPEG_DC_HT); 836 for (i = 0 ; i < V4L2_JPEG_REF_HT_DC_LEN; i++) 837 header_write(ctx, buffer, offset, 1, v4l2_jpeg_ref_table_luma_dc_ht[i]); 838 839 header_write(ctx, buffer, offset, 2, DHT_MARKER); 840 header_write(ctx, buffer, offset, 2, LH_AC); 841 header_write(ctx, buffer, offset, 1, V4L2_JPEG_LUM_HT | V4L2_JPEG_AC_HT); 842 for (i = 0 ; i < V4L2_JPEG_REF_HT_AC_LEN; i++) 843 header_write(ctx, buffer, offset, 1, v4l2_jpeg_ref_table_luma_ac_ht[i]); 844 845 header_write(ctx, buffer, offset, 2, DHT_MARKER); 846 header_write(ctx, buffer, offset, 2, LH_DC); 847 header_write(ctx, buffer, offset, 1, V4L2_JPEG_CHR_HT | V4L2_JPEG_DC_HT); 848 for (i = 0 ; i < V4L2_JPEG_REF_HT_DC_LEN; i++) 849 header_write(ctx, buffer, offset, 1, v4l2_jpeg_ref_table_chroma_dc_ht[i]); 850 851 header_write(ctx, buffer, offset, 2, DHT_MARKER); 852 header_write(ctx, buffer, offset, 2, LH_AC); 853 header_write(ctx, buffer, offset, 1, V4L2_JPEG_CHR_HT | V4L2_JPEG_AC_HT); 854 for (i = 0 ; i < V4L2_JPEG_REF_HT_AC_LEN; i++) 855 header_write(ctx, buffer, offset, 1, v4l2_jpeg_ref_table_chroma_ac_ht[i]); 856 } 857 858 static void encode_frame_header(struct e5010_context *ctx, void *addr, unsigned int *offset) 859 { 860 u8 *buffer = (u8 *)addr; 861 862 header_write(ctx, buffer, offset, 2, SOF_BASELINE_DCT); 863 header_write(ctx, buffer, offset, 2, 8 + (3 * UC_NUM_COMP)); 864 header_write(ctx, buffer, offset, 1, PRECISION); 865 header_write(ctx, buffer, offset, 2, ctx->out_queue.crop.height); 866 header_write(ctx, buffer, offset, 2, ctx->out_queue.crop.width); 867 header_write(ctx, buffer, offset, 1, UC_NUM_COMP); 868 869 /* Luma details */ 870 header_write(ctx, buffer, offset, 1, 1); 871 if (ctx->out_queue.fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) 872 header_write(ctx, buffer, offset, 1, 873 HORZ_SAMPLING_FACTOR | (VERT_SAMPLING_FACTOR_422)); 874 else 875 header_write(ctx, buffer, offset, 1, 876 HORZ_SAMPLING_FACTOR | (VERT_SAMPLING_FACTOR_420)); 877 header_write(ctx, buffer, offset, 1, 0); 878 /* Chroma details */ 879 header_write(ctx, buffer, offset, 1, 2); 880 header_write(ctx, buffer, offset, 1, (HORZ_SAMPLING_FACTOR >> 1) | 1); 881 header_write(ctx, buffer, offset, 1, 1); 882 header_write(ctx, buffer, offset, 1, 3); 883 header_write(ctx, buffer, offset, 1, (HORZ_SAMPLING_FACTOR >> 1) | 1); 884 header_write(ctx, buffer, offset, 1, 1); 885 } 886 887 static void jpg_encode_sos_header(struct e5010_context *ctx, void *addr, unsigned int *offset) 888 { 889 u8 *buffer = (u8 *)addr; 890 int i; 891 892 header_write(ctx, buffer, offset, 2, START_OF_SCAN); 893 header_write(ctx, buffer, offset, 2, 6 + (COMPONENTS_IN_SCAN << 1)); 894 header_write(ctx, buffer, offset, 1, COMPONENTS_IN_SCAN); 895 896 for (i = 0; i < COMPONENTS_IN_SCAN; i++) { 897 header_write(ctx, buffer, offset, 1, i + 1); 898 if (i == 0) 899 header_write(ctx, buffer, offset, 1, 0); 900 else 901 header_write(ctx, buffer, offset, 1, 17); 902 } 903 904 header_write(ctx, buffer, offset, 1, 0); 905 header_write(ctx, buffer, offset, 1, 63); 906 header_write(ctx, buffer, offset, 1, 0); 907 } 908 909 static void write_header(struct e5010_context *ctx, void *addr) 910 { 911 unsigned int offset = 0; 912 913 encode_marker_segment(ctx, addr, &offset); 914 encode_frame_header(ctx, addr, &offset); 915 jpg_encode_sos_header(ctx, addr, &offset); 916 } 917 918 static irqreturn_t e5010_irq(int irq, void *data) 919 { 920 struct e5010_dev *e5010 = data; 921 struct e5010_context *ctx; 922 int output_size; 923 struct vb2_v4l2_buffer *src_buf, *dst_buf; 924 bool pic_done, out_addr_err; 925 926 spin_lock(&e5010->hw_lock); 927 pic_done = e5010_hw_pic_done_irq(e5010->core_base); 928 out_addr_err = e5010_hw_output_address_irq(e5010->core_base); 929 930 if (!pic_done && !out_addr_err) { 931 spin_unlock(&e5010->hw_lock); 932 return IRQ_NONE; 933 } 934 935 ctx = v4l2_m2m_get_curr_priv(e5010->m2m_dev); 936 if (WARN_ON(!ctx)) 937 goto job_unlock; 938 939 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 940 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 941 if (!dst_buf || !src_buf) { 942 v4l2_err(&e5010->v4l2_dev, "ctx: 0x%p No source or destination buffer\n", ctx); 943 goto job_unlock; 944 } 945 946 if (out_addr_err) { 947 e5010_hw_clear_output_error(e5010->core_base, 1); 948 v4l2_warn(&e5010->v4l2_dev, 949 "ctx: 0x%p Output bitstream size exceeded max size\n", ctx); 950 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); 951 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, dst_buf->planes[0].length); 952 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); 953 if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) { 954 dst_buf->flags |= V4L2_BUF_FLAG_LAST; 955 v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx); 956 v4l2_event_queue_fh(&ctx->fh, &e5010_eos_event); 957 dprintk(e5010, 2, "ctx: 0x%p Sending EOS\n", ctx); 958 } 959 } 960 961 if (pic_done) { 962 e5010_hw_clear_picture_done(e5010->core_base, 1); 963 dprintk(e5010, 3, "ctx: 0x%p Got output bitstream of size %d bytes\n", 964 ctx, readl(e5010->core_base + JASPER_OUTPUT_SIZE_OFFSET)); 965 966 if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) { 967 dst_buf->flags |= V4L2_BUF_FLAG_LAST; 968 v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx); 969 v4l2_event_queue_fh(&ctx->fh, &e5010_eos_event); 970 dprintk(e5010, 2, "ctx: 0x%p Sending EOS\n", ctx); 971 } 972 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_DONE); 973 output_size = e5010_hw_get_output_size(e5010->core_base); 974 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, output_size + HEADER_SIZE); 975 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE); 976 dprintk(e5010, 3, 977 "ctx: 0x%p frame done for dst_buf->sequence: %d src_buf->sequence: %d\n", 978 ctx, dst_buf->sequence, src_buf->sequence); 979 } 980 981 v4l2_m2m_job_finish(e5010->m2m_dev, ctx->fh.m2m_ctx); 982 dprintk(e5010, 3, "ctx: 0x%p Finish job\n", ctx); 983 984 job_unlock: 985 spin_unlock(&e5010->hw_lock); 986 return IRQ_HANDLED; 987 } 988 989 static int e5010_init_device(struct e5010_dev *e5010) 990 { 991 int ret = 0; 992 993 /*TODO: Set MMU in bypass mode until support for the same is added in driver*/ 994 e5010_hw_bypass_mmu(e5010->mmu_base, 1); 995 996 if (e5010_hw_enable_auto_clock_gating(e5010->core_base, 1)) 997 v4l2_warn(&e5010->v4l2_dev, "failed to enable auto clock gating\n"); 998 999 if (e5010_hw_enable_manual_clock_gating(e5010->core_base, 0)) 1000 v4l2_warn(&e5010->v4l2_dev, "failed to disable manual clock gating\n"); 1001 1002 if (e5010_hw_enable_crc_check(e5010->core_base, 0)) 1003 v4l2_warn(&e5010->v4l2_dev, "failed to disable CRC check\n"); 1004 1005 if (e5010_hw_enable_output_address_error_irq(e5010->core_base, 1)) 1006 v4l2_err(&e5010->v4l2_dev, "failed to enable Output Address Error interrupts\n"); 1007 1008 ret = e5010_hw_set_input_source_to_memory(e5010->core_base, 1); 1009 if (ret) { 1010 v4l2_err(&e5010->v4l2_dev, "failed to set input source to memory\n"); 1011 return ret; 1012 } 1013 1014 ret = e5010_hw_enable_picture_done_irq(e5010->core_base, 1); 1015 if (ret) 1016 v4l2_err(&e5010->v4l2_dev, "failed to enable Picture Done interrupts\n"); 1017 1018 return ret; 1019 } 1020 1021 static int e5010_probe(struct platform_device *pdev) 1022 { 1023 struct e5010_dev *e5010; 1024 int irq, ret = 0; 1025 struct device *dev = &pdev->dev; 1026 1027 ret = dma_set_mask(dev, DMA_BIT_MASK(32)); 1028 if (ret) 1029 return dev_err_probe(dev, ret, "32-bit consistent DMA enable failed\n"); 1030 1031 e5010 = devm_kzalloc(dev, sizeof(*e5010), GFP_KERNEL); 1032 if (!e5010) 1033 return -ENOMEM; 1034 1035 platform_set_drvdata(pdev, e5010); 1036 1037 e5010->dev = dev; 1038 1039 mutex_init(&e5010->mutex); 1040 spin_lock_init(&e5010->hw_lock); 1041 1042 e5010->vdev = video_device_alloc(); 1043 if (!e5010->vdev) { 1044 dev_err(dev, "failed to allocate video device\n"); 1045 return -ENOMEM; 1046 } 1047 1048 snprintf(e5010->vdev->name, sizeof(e5010->vdev->name), "%s", E5010_MODULE_NAME); 1049 e5010->vdev->fops = &e5010_fops; 1050 e5010->vdev->ioctl_ops = &e5010_ioctl_ops; 1051 e5010->vdev->minor = -1; 1052 e5010->vdev->release = video_device_release; 1053 e5010->vdev->vfl_dir = VFL_DIR_M2M; 1054 e5010->vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING; 1055 e5010->vdev->v4l2_dev = &e5010->v4l2_dev; 1056 e5010->vdev->lock = &e5010->mutex; 1057 1058 ret = v4l2_device_register(dev, &e5010->v4l2_dev); 1059 if (ret) { 1060 dev_err_probe(dev, ret, "failed to register v4l2 device\n"); 1061 goto fail_after_video_device_alloc; 1062 } 1063 1064 1065 e5010->m2m_dev = v4l2_m2m_init(&e5010_m2m_ops); 1066 if (IS_ERR(e5010->m2m_dev)) { 1067 ret = PTR_ERR(e5010->m2m_dev); 1068 e5010->m2m_dev = NULL; 1069 dev_err_probe(dev, ret, "failed to init mem2mem device\n"); 1070 goto fail_after_v4l2_register; 1071 } 1072 1073 video_set_drvdata(e5010->vdev, e5010); 1074 1075 e5010->core_base = devm_platform_ioremap_resource_byname(pdev, "core"); 1076 if (IS_ERR(e5010->core_base)) { 1077 ret = PTR_ERR(e5010->core_base); 1078 dev_err_probe(dev, ret, "Missing 'core' resources area\n"); 1079 goto fail_after_v4l2_register; 1080 } 1081 1082 e5010->mmu_base = devm_platform_ioremap_resource_byname(pdev, "mmu"); 1083 if (IS_ERR(e5010->mmu_base)) { 1084 ret = PTR_ERR(e5010->mmu_base); 1085 dev_err_probe(dev, ret, "Missing 'mmu' resources area\n"); 1086 goto fail_after_v4l2_register; 1087 } 1088 1089 e5010->last_context_run = NULL; 1090 1091 irq = platform_get_irq(pdev, 0); 1092 ret = devm_request_irq(dev, irq, e5010_irq, 0, 1093 E5010_MODULE_NAME, e5010); 1094 if (ret) { 1095 dev_err_probe(dev, ret, "failed to register IRQ %d\n", irq); 1096 goto fail_after_v4l2_register; 1097 } 1098 1099 e5010->clk = devm_clk_get(dev, NULL); 1100 if (IS_ERR(e5010->clk)) { 1101 ret = PTR_ERR(e5010->clk); 1102 dev_err_probe(dev, ret, "failed to get clock\n"); 1103 goto fail_after_v4l2_register; 1104 } 1105 1106 pm_runtime_enable(dev); 1107 1108 ret = video_register_device(e5010->vdev, VFL_TYPE_VIDEO, 0); 1109 if (ret) { 1110 dev_err_probe(dev, ret, "failed to register video device\n"); 1111 goto fail_after_video_register_device; 1112 } 1113 1114 v4l2_info(&e5010->v4l2_dev, "Device registered as /dev/video%d\n", 1115 e5010->vdev->num); 1116 1117 return 0; 1118 1119 fail_after_video_register_device: 1120 v4l2_m2m_release(e5010->m2m_dev); 1121 fail_after_v4l2_register: 1122 v4l2_device_unregister(&e5010->v4l2_dev); 1123 fail_after_video_device_alloc: 1124 video_device_release(e5010->vdev); 1125 return ret; 1126 } 1127 1128 static void e5010_remove(struct platform_device *pdev) 1129 { 1130 struct e5010_dev *e5010 = platform_get_drvdata(pdev); 1131 1132 pm_runtime_disable(e5010->dev); 1133 video_unregister_device(e5010->vdev); 1134 v4l2_m2m_release(e5010->m2m_dev); 1135 v4l2_device_unregister(&e5010->v4l2_dev); 1136 } 1137 1138 static void e5010_vb2_buffers_return(struct vb2_queue *q, enum vb2_buffer_state state) 1139 { 1140 struct vb2_v4l2_buffer *vbuf; 1141 struct e5010_context *ctx = vb2_get_drv_priv(q); 1142 1143 if (V4L2_TYPE_IS_OUTPUT(q->type)) { 1144 while ((vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx))) { 1145 dprintk(ctx->e5010, 2, "ctx: 0x%p, buf type %s | index %d\n", 1146 ctx, type_name(vbuf->vb2_buf.type), vbuf->vb2_buf.index); 1147 v4l2_m2m_buf_done(vbuf, state); 1148 } 1149 } else { 1150 while ((vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx))) { 1151 dprintk(ctx->e5010, 2, "ctx: 0x%p, buf type %s | index %d\n", 1152 ctx, type_name(vbuf->vb2_buf.type), vbuf->vb2_buf.index); 1153 vb2_set_plane_payload(&vbuf->vb2_buf, 0, 0); 1154 v4l2_m2m_buf_done(vbuf, state); 1155 } 1156 } 1157 } 1158 1159 static int e5010_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers, unsigned int *nplanes, 1160 unsigned int sizes[], struct device *alloc_devs[]) 1161 { 1162 struct e5010_context *ctx = vb2_get_drv_priv(vq); 1163 struct e5010_q_data *queue; 1164 int i; 1165 1166 queue = get_queue(ctx, vq->type); 1167 1168 if (*nplanes) { 1169 if (*nplanes != queue->fmt->num_planes) 1170 return -EINVAL; 1171 for (i = 0; i < *nplanes; i++) { 1172 if (sizes[i] < queue->sizeimage[i]) 1173 return -EINVAL; 1174 } 1175 return 0; 1176 } 1177 1178 *nplanes = queue->fmt->num_planes; 1179 for (i = 0; i < *nplanes; i++) 1180 sizes[i] = queue->sizeimage[i]; 1181 1182 dprintk(ctx->e5010, 2, 1183 "ctx: 0x%p, type %s, buffer(s): %d, planes %d, plane1: bytes %d plane2: %d bytes\n", 1184 ctx, type_name(vq->type), *nbuffers, *nplanes, sizes[0], sizes[1]); 1185 1186 return 0; 1187 } 1188 1189 static void e5010_buf_finish(struct vb2_buffer *vb) 1190 { 1191 struct e5010_context *ctx = vb2_get_drv_priv(vb->vb2_queue); 1192 void *d_addr; 1193 1194 if (vb->state != VB2_BUF_STATE_DONE || V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) 1195 return; 1196 1197 d_addr = vb2_plane_vaddr(vb, 0); 1198 write_header(ctx, d_addr); 1199 } 1200 1201 static int e5010_buf_out_validate(struct vb2_buffer *vb) 1202 { 1203 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1204 struct e5010_context *ctx = vb2_get_drv_priv(vb->vb2_queue); 1205 1206 if (vbuf->field != V4L2_FIELD_NONE) 1207 dprintk(ctx->e5010, 1, "ctx: 0x%p, field isn't supported\n", ctx); 1208 1209 vbuf->field = V4L2_FIELD_NONE; 1210 1211 return 0; 1212 } 1213 1214 static int e5010_buf_prepare(struct vb2_buffer *vb) 1215 { 1216 struct e5010_context *ctx = vb2_get_drv_priv(vb->vb2_queue); 1217 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1218 struct e5010_q_data *queue; 1219 int i; 1220 1221 vbuf->field = V4L2_FIELD_NONE; 1222 1223 queue = get_queue(ctx, vb->vb2_queue->type); 1224 1225 for (i = 0; i < queue->fmt->num_planes; i++) { 1226 if (vb2_plane_size(vb, i) < (unsigned long)queue->sizeimage[i]) { 1227 v4l2_err(&ctx->e5010->v4l2_dev, "plane %d too small (%lu < %lu)", i, 1228 vb2_plane_size(vb, i), (unsigned long)queue->sizeimage[i]); 1229 1230 return -EINVAL; 1231 } 1232 } 1233 1234 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) { 1235 vb2_set_plane_payload(vb, 0, 0); 1236 vb2_set_plane_payload(vb, 1, 0); 1237 } 1238 1239 return 0; 1240 } 1241 1242 static void e5010_buf_queue(struct vb2_buffer *vb) 1243 { 1244 struct e5010_context *ctx = vb2_get_drv_priv(vb->vb2_queue); 1245 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 1246 1247 if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) && 1248 vb2_is_streaming(vb->vb2_queue) && 1249 v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) { 1250 struct e5010_q_data *queue = get_queue(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 1251 1252 vbuf->sequence = queue->sequence++; 1253 v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf); 1254 v4l2_event_queue_fh(&ctx->fh, &e5010_eos_event); 1255 return; 1256 } 1257 1258 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); 1259 } 1260 1261 static int e5010_encoder_cmd(struct file *file, void *priv, 1262 struct v4l2_encoder_cmd *cmd) 1263 { 1264 struct e5010_context *ctx = to_e5010_context(file); 1265 int ret; 1266 struct vb2_queue *cap_vq; 1267 1268 cap_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 1269 1270 ret = v4l2_m2m_ioctl_try_encoder_cmd(file, &ctx->fh, cmd); 1271 if (ret < 0) 1272 return ret; 1273 1274 if (!vb2_is_streaming(v4l2_m2m_get_src_vq(ctx->fh.m2m_ctx)) || 1275 !vb2_is_streaming(v4l2_m2m_get_dst_vq(ctx->fh.m2m_ctx))) 1276 return 0; 1277 1278 ret = v4l2_m2m_ioctl_encoder_cmd(file, &ctx->fh, cmd); 1279 if (ret < 0) 1280 return ret; 1281 1282 if (cmd->cmd == V4L2_ENC_CMD_STOP && 1283 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 1284 v4l2_event_queue_fh(&ctx->fh, &e5010_eos_event); 1285 1286 if (cmd->cmd == V4L2_ENC_CMD_START && 1287 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) 1288 vb2_clear_last_buffer_dequeued(cap_vq); 1289 1290 return 0; 1291 } 1292 1293 static int e5010_start_streaming(struct vb2_queue *q, unsigned int count) 1294 { 1295 struct e5010_context *ctx = vb2_get_drv_priv(q); 1296 int ret; 1297 1298 struct e5010_q_data *queue = get_queue(ctx, q->type); 1299 1300 v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q); 1301 queue->sequence = 0; 1302 1303 ret = pm_runtime_resume_and_get(ctx->e5010->dev); 1304 if (ret < 0) { 1305 v4l2_err(&ctx->e5010->v4l2_dev, "failed to power up jpeg\n"); 1306 goto fail; 1307 } 1308 1309 ret = e5010_init_device(ctx->e5010); 1310 if (ret) { 1311 v4l2_err(&ctx->e5010->v4l2_dev, "failed to Enable e5010 device\n"); 1312 goto fail; 1313 } 1314 1315 return 0; 1316 1317 fail: 1318 e5010_vb2_buffers_return(q, VB2_BUF_STATE_QUEUED); 1319 1320 return ret; 1321 } 1322 1323 static void e5010_stop_streaming(struct vb2_queue *q) 1324 { 1325 struct e5010_context *ctx = vb2_get_drv_priv(q); 1326 1327 e5010_vb2_buffers_return(q, VB2_BUF_STATE_ERROR); 1328 1329 if (V4L2_TYPE_IS_OUTPUT(q->type)) 1330 v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q); 1331 1332 if (V4L2_TYPE_IS_OUTPUT(q->type) && 1333 v4l2_m2m_has_stopped(ctx->fh.m2m_ctx)) { 1334 v4l2_event_queue_fh(&ctx->fh, &e5010_eos_event); 1335 } 1336 1337 pm_runtime_put_sync(ctx->e5010->dev); 1338 } 1339 1340 static void e5010_device_run(void *priv) 1341 { 1342 struct e5010_context *ctx = priv; 1343 struct e5010_dev *e5010 = ctx->e5010; 1344 struct vb2_v4l2_buffer *s_vb, *d_vb; 1345 u32 reg = 0; 1346 int ret = 0, luma_crop_offset = 0, chroma_crop_offset = 0; 1347 unsigned long flags; 1348 int num_planes = ctx->out_queue.fmt->num_planes; 1349 1350 spin_lock_irqsave(&e5010->hw_lock, flags); 1351 s_vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 1352 WARN_ON(!s_vb); 1353 d_vb = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 1354 WARN_ON(!d_vb); 1355 if (!s_vb || !d_vb) 1356 goto no_ready_buf_err; 1357 1358 s_vb->sequence = ctx->out_queue.sequence++; 1359 d_vb->sequence = ctx->cap_queue.sequence++; 1360 1361 v4l2_m2m_buf_copy_metadata(s_vb, d_vb, false); 1362 1363 if (ctx != e5010->last_context_run || ctx->update_qp) { 1364 dprintk(e5010, 1, "ctx updated: 0x%p -> 0x%p, updating qp tables\n", 1365 e5010->last_context_run, ctx); 1366 ret = update_qp_tables(ctx); 1367 } 1368 1369 if (ret) { 1370 ctx->update_qp = true; 1371 v4l2_err(&e5010->v4l2_dev, "failed to update QP tables\n"); 1372 goto device_busy_err; 1373 } else { 1374 e5010->last_context_run = ctx; 1375 ctx->update_qp = false; 1376 } 1377 1378 /* Set I/O Buffer addresses */ 1379 reg = (u32)vb2_dma_contig_plane_dma_addr(&s_vb->vb2_buf, 0); 1380 1381 if (ctx->out_queue.crop_set) { 1382 luma_crop_offset = ctx->out_queue.bytesperline[0] * ctx->out_queue.crop.top + 1383 ctx->out_queue.crop.left; 1384 1385 if (ctx->out_queue.fmt->subsampling == V4L2_JPEG_CHROMA_SUBSAMPLING_422) { 1386 chroma_crop_offset = 1387 ctx->out_queue.bytesperline[0] * ctx->out_queue.crop.top 1388 + ctx->out_queue.crop.left; 1389 } else { 1390 chroma_crop_offset = 1391 ctx->out_queue.bytesperline[0] * ctx->out_queue.crop.top / 2 1392 + ctx->out_queue.crop.left; 1393 } 1394 1395 dprintk(e5010, 1, "Luma crop offset : %x, chroma crop offset : %x\n", 1396 luma_crop_offset, chroma_crop_offset); 1397 } 1398 1399 ret = e5010_hw_set_input_luma_addr(e5010->core_base, reg + luma_crop_offset); 1400 if (ret || !reg) { 1401 v4l2_err(&e5010->v4l2_dev, "failed to set input luma address\n"); 1402 goto device_busy_err; 1403 } 1404 1405 if (num_planes == 1) 1406 reg += (ctx->out_queue.bytesperline[0]) * (ctx->out_queue.height); 1407 else 1408 reg = (u32)vb2_dma_contig_plane_dma_addr(&s_vb->vb2_buf, 1); 1409 1410 dprintk(e5010, 3, 1411 "ctx: 0x%p, luma_addr: 0x%x, chroma_addr: 0x%x, out_addr: 0x%x\n", 1412 ctx, (u32)vb2_dma_contig_plane_dma_addr(&s_vb->vb2_buf, 0) + luma_crop_offset, 1413 reg + chroma_crop_offset, (u32)vb2_dma_contig_plane_dma_addr(&d_vb->vb2_buf, 0)); 1414 1415 dprintk(e5010, 3, 1416 "ctx: 0x%p, buf indices: src_index: %d, dst_index: %d\n", 1417 ctx, s_vb->vb2_buf.index, d_vb->vb2_buf.index); 1418 1419 ret = e5010_hw_set_input_chroma_addr(e5010->core_base, reg + chroma_crop_offset); 1420 if (ret || !reg) { 1421 v4l2_err(&e5010->v4l2_dev, "failed to set input chroma address\n"); 1422 goto device_busy_err; 1423 } 1424 1425 reg = (u32)vb2_dma_contig_plane_dma_addr(&d_vb->vb2_buf, 0); 1426 reg += HEADER_SIZE; 1427 ret = e5010_hw_set_output_base_addr(e5010->core_base, reg); 1428 if (ret || !reg) { 1429 v4l2_err(&e5010->v4l2_dev, "failed to set output base address\n"); 1430 goto device_busy_err; 1431 } 1432 1433 /* Set input settings */ 1434 ret = e5010_hw_set_horizontal_size(e5010->core_base, ctx->out_queue.crop.width - 1); 1435 if (ret) { 1436 v4l2_err(&e5010->v4l2_dev, "failed to set input width\n"); 1437 goto device_busy_err; 1438 } 1439 1440 ret = e5010_hw_set_vertical_size(e5010->core_base, ctx->out_queue.crop.height - 1); 1441 if (ret) { 1442 v4l2_err(&e5010->v4l2_dev, "failed to set input width\n"); 1443 goto device_busy_err; 1444 } 1445 1446 ret = e5010_hw_set_luma_stride(e5010->core_base, ctx->out_queue.bytesperline[0]); 1447 if (ret) { 1448 v4l2_err(&e5010->v4l2_dev, "failed to set luma stride\n"); 1449 goto device_busy_err; 1450 } 1451 1452 ret = e5010_hw_set_chroma_stride(e5010->core_base, ctx->out_queue.bytesperline[0]); 1453 if (ret) { 1454 v4l2_err(&e5010->v4l2_dev, "failed to set chroma stride\n"); 1455 goto device_busy_err; 1456 } 1457 1458 ret = e5010_set_input_subsampling(e5010->core_base, ctx->out_queue.fmt->subsampling); 1459 if (ret) { 1460 v4l2_err(&e5010->v4l2_dev, "failed to set input subsampling\n"); 1461 goto device_busy_err; 1462 } 1463 1464 ret = e5010_hw_set_chroma_order(e5010->core_base, ctx->out_queue.fmt->chroma_order); 1465 if (ret) { 1466 v4l2_err(&e5010->v4l2_dev, "failed to set chroma order\n"); 1467 goto device_busy_err; 1468 } 1469 1470 e5010_hw_set_output_max_size(e5010->core_base, d_vb->planes[0].length); 1471 e5010_hw_encode_start(e5010->core_base, 1); 1472 1473 spin_unlock_irqrestore(&e5010->hw_lock, flags); 1474 1475 return; 1476 1477 device_busy_err: 1478 e5010_reset(e5010->dev, e5010->core_base, e5010->mmu_base); 1479 1480 no_ready_buf_err: 1481 if (s_vb) { 1482 v4l2_m2m_src_buf_remove_by_buf(ctx->fh.m2m_ctx, s_vb); 1483 v4l2_m2m_buf_done(s_vb, VB2_BUF_STATE_ERROR); 1484 } 1485 1486 if (d_vb) { 1487 v4l2_m2m_dst_buf_remove_by_buf(ctx->fh.m2m_ctx, d_vb); 1488 /* Payload set to 1 since 0 payload can trigger EOS */ 1489 vb2_set_plane_payload(&d_vb->vb2_buf, 0, 1); 1490 v4l2_m2m_buf_done(d_vb, VB2_BUF_STATE_ERROR); 1491 } 1492 v4l2_m2m_job_finish(e5010->m2m_dev, ctx->fh.m2m_ctx); 1493 spin_unlock_irqrestore(&e5010->hw_lock, flags); 1494 } 1495 1496 #ifdef CONFIG_PM 1497 static int e5010_runtime_resume(struct device *dev) 1498 { 1499 struct e5010_dev *e5010 = dev_get_drvdata(dev); 1500 int ret; 1501 1502 ret = clk_prepare_enable(e5010->clk); 1503 if (ret < 0) { 1504 v4l2_err(&e5010->v4l2_dev, "failed to enable clock\n"); 1505 return ret; 1506 } 1507 1508 return 0; 1509 } 1510 1511 static int e5010_runtime_suspend(struct device *dev) 1512 { 1513 struct e5010_dev *e5010 = dev_get_drvdata(dev); 1514 1515 clk_disable_unprepare(e5010->clk); 1516 1517 return 0; 1518 } 1519 #endif 1520 1521 #ifdef CONFIG_PM_SLEEP 1522 static int e5010_suspend(struct device *dev) 1523 { 1524 struct e5010_dev *e5010 = dev_get_drvdata(dev); 1525 1526 v4l2_m2m_suspend(e5010->m2m_dev); 1527 1528 return pm_runtime_force_suspend(dev); 1529 } 1530 1531 static int e5010_resume(struct device *dev) 1532 { 1533 struct e5010_dev *e5010 = dev_get_drvdata(dev); 1534 int ret; 1535 1536 ret = pm_runtime_force_resume(dev); 1537 if (ret < 0) 1538 return ret; 1539 1540 ret = e5010_init_device(e5010); 1541 if (ret) { 1542 dev_err(dev, "Failed to re-enable e5010 device\n"); 1543 return ret; 1544 } 1545 1546 v4l2_m2m_resume(e5010->m2m_dev); 1547 1548 return ret; 1549 } 1550 #endif 1551 1552 static const struct dev_pm_ops e5010_pm_ops = { 1553 SET_RUNTIME_PM_OPS(e5010_runtime_suspend, 1554 e5010_runtime_resume, NULL) 1555 SET_SYSTEM_SLEEP_PM_OPS(e5010_suspend, e5010_resume) 1556 }; 1557 1558 static const struct v4l2_ioctl_ops e5010_ioctl_ops = { 1559 .vidioc_querycap = e5010_querycap, 1560 1561 .vidioc_enum_fmt_vid_cap = e5010_enum_fmt, 1562 .vidioc_g_fmt_vid_cap_mplane = e5010_g_fmt, 1563 .vidioc_try_fmt_vid_cap_mplane = e5010_try_fmt, 1564 .vidioc_s_fmt_vid_cap_mplane = e5010_s_fmt, 1565 1566 .vidioc_enum_fmt_vid_out = e5010_enum_fmt, 1567 .vidioc_g_fmt_vid_out_mplane = e5010_g_fmt, 1568 .vidioc_try_fmt_vid_out_mplane = e5010_try_fmt, 1569 .vidioc_s_fmt_vid_out_mplane = e5010_s_fmt, 1570 1571 .vidioc_g_selection = e5010_g_selection, 1572 .vidioc_s_selection = e5010_s_selection, 1573 1574 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 1575 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 1576 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 1577 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 1578 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 1579 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 1580 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 1581 1582 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 1583 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 1584 .vidioc_log_status = v4l2_ctrl_log_status, 1585 1586 .vidioc_subscribe_event = e5010_subscribe_event, 1587 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1588 .vidioc_try_encoder_cmd = v4l2_m2m_ioctl_try_encoder_cmd, 1589 .vidioc_encoder_cmd = e5010_encoder_cmd, 1590 1591 .vidioc_enum_framesizes = e5010_enum_framesizes, 1592 }; 1593 1594 static const struct vb2_ops e5010_video_ops = { 1595 .queue_setup = e5010_queue_setup, 1596 .buf_queue = e5010_buf_queue, 1597 .buf_finish = e5010_buf_finish, 1598 .buf_prepare = e5010_buf_prepare, 1599 .buf_out_validate = e5010_buf_out_validate, 1600 .start_streaming = e5010_start_streaming, 1601 .stop_streaming = e5010_stop_streaming, 1602 }; 1603 1604 static const struct v4l2_file_operations e5010_fops = { 1605 .owner = THIS_MODULE, 1606 .open = e5010_open, 1607 .release = e5010_release, 1608 .poll = v4l2_m2m_fop_poll, 1609 .unlocked_ioctl = video_ioctl2, 1610 .mmap = v4l2_m2m_fop_mmap, 1611 }; 1612 1613 static const struct v4l2_m2m_ops e5010_m2m_ops = { 1614 .device_run = e5010_device_run, 1615 }; 1616 1617 static const struct of_device_id e5010_of_match[] = { 1618 {.compatible = "img,e5010-jpeg-enc"}, { /* end */}, 1619 }; 1620 MODULE_DEVICE_TABLE(of, e5010_of_match); 1621 1622 static struct platform_driver e5010_driver = { 1623 .probe = e5010_probe, 1624 .remove = e5010_remove, 1625 .driver = { 1626 .name = E5010_MODULE_NAME, 1627 .of_match_table = e5010_of_match, 1628 .pm = &e5010_pm_ops, 1629 }, 1630 }; 1631 module_platform_driver(e5010_driver); 1632 1633 MODULE_LICENSE("GPL"); 1634 MODULE_DESCRIPTION("Imagination E5010 JPEG encoder driver"); 1635