1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2016 MediaTek Inc. 4 * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com> 5 * Rick Chang <rick.chang@mediatek.com> 6 * Xia Jiang <xia.jiang@mediatek.com> 7 */ 8 9 #include <linux/clk.h> 10 #include <linux/err.h> 11 #include <linux/interrupt.h> 12 #include <linux/io.h> 13 #include <linux/kernel.h> 14 #include <linux/module.h> 15 #include <linux/of_platform.h> 16 #include <linux/platform_device.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/slab.h> 19 #include <linux/spinlock.h> 20 #include <media/v4l2-event.h> 21 #include <media/v4l2-mem2mem.h> 22 #include <media/v4l2-ioctl.h> 23 #include <media/videobuf2-core.h> 24 #include <media/videobuf2-dma-contig.h> 25 26 #include "mtk_jpeg_enc_hw.h" 27 #include "mtk_jpeg_dec_hw.h" 28 #include "mtk_jpeg_core.h" 29 #include "mtk_jpeg_dec_parse.h" 30 31 static struct mtk_jpeg_fmt mtk_jpeg_enc_formats[] = { 32 { 33 .fourcc = V4L2_PIX_FMT_JPEG, 34 .colplanes = 1, 35 .flags = MTK_JPEG_FMT_FLAG_CAPTURE, 36 }, 37 { 38 .fourcc = V4L2_PIX_FMT_NV12M, 39 .hw_format = JPEG_ENC_YUV_FORMAT_NV12, 40 .h_sample = {4, 4}, 41 .v_sample = {4, 2}, 42 .colplanes = 2, 43 .h_align = 4, 44 .v_align = 4, 45 .flags = MTK_JPEG_FMT_FLAG_OUTPUT, 46 }, 47 { 48 .fourcc = V4L2_PIX_FMT_NV21M, 49 .hw_format = JEPG_ENC_YUV_FORMAT_NV21, 50 .h_sample = {4, 4}, 51 .v_sample = {4, 2}, 52 .colplanes = 2, 53 .h_align = 4, 54 .v_align = 4, 55 .flags = MTK_JPEG_FMT_FLAG_OUTPUT, 56 }, 57 { 58 .fourcc = V4L2_PIX_FMT_YUYV, 59 .hw_format = JPEG_ENC_YUV_FORMAT_YUYV, 60 .h_sample = {8}, 61 .v_sample = {4}, 62 .colplanes = 1, 63 .h_align = 5, 64 .v_align = 3, 65 .flags = MTK_JPEG_FMT_FLAG_OUTPUT, 66 }, 67 { 68 .fourcc = V4L2_PIX_FMT_YVYU, 69 .hw_format = JPEG_ENC_YUV_FORMAT_YVYU, 70 .h_sample = {8}, 71 .v_sample = {4}, 72 .colplanes = 1, 73 .h_align = 5, 74 .v_align = 3, 75 .flags = MTK_JPEG_FMT_FLAG_OUTPUT, 76 }, 77 }; 78 79 static struct mtk_jpeg_fmt mtk_jpeg_dec_formats[] = { 80 { 81 .fourcc = V4L2_PIX_FMT_JPEG, 82 .colplanes = 1, 83 .flags = MTK_JPEG_FMT_FLAG_OUTPUT, 84 }, 85 { 86 .fourcc = V4L2_PIX_FMT_YUV420M, 87 .h_sample = {4, 2, 2}, 88 .v_sample = {4, 2, 2}, 89 .colplanes = 3, 90 .h_align = 5, 91 .v_align = 4, 92 .flags = MTK_JPEG_FMT_FLAG_CAPTURE, 93 }, 94 { 95 .fourcc = V4L2_PIX_FMT_YUV422M, 96 .h_sample = {4, 2, 2}, 97 .v_sample = {4, 4, 4}, 98 .colplanes = 3, 99 .h_align = 5, 100 .v_align = 3, 101 .flags = MTK_JPEG_FMT_FLAG_CAPTURE, 102 }, 103 }; 104 105 #define MTK_JPEG_ENC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_enc_formats) 106 #define MTK_JPEG_DEC_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_dec_formats) 107 108 struct mtk_jpeg_src_buf { 109 struct vb2_v4l2_buffer b; 110 struct list_head list; 111 struct mtk_jpeg_dec_param dec_param; 112 }; 113 114 static int debug; 115 module_param(debug, int, 0644); 116 117 static inline struct mtk_jpeg_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl) 118 { 119 return container_of(ctrl->handler, struct mtk_jpeg_ctx, ctrl_hdl); 120 } 121 122 static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh) 123 { 124 return container_of(fh, struct mtk_jpeg_ctx, fh); 125 } 126 127 static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf( 128 struct vb2_buffer *vb) 129 { 130 return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b); 131 } 132 133 static int mtk_jpeg_querycap(struct file *file, void *priv, 134 struct v4l2_capability *cap) 135 { 136 struct mtk_jpeg_dev *jpeg = video_drvdata(file); 137 138 strscpy(cap->driver, jpeg->variant->dev_name, sizeof(cap->driver)); 139 strscpy(cap->card, jpeg->variant->dev_name, sizeof(cap->card)); 140 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s", 141 dev_name(jpeg->dev)); 142 143 return 0; 144 } 145 146 static int vidioc_jpeg_enc_s_ctrl(struct v4l2_ctrl *ctrl) 147 { 148 struct mtk_jpeg_ctx *ctx = ctrl_to_ctx(ctrl); 149 150 switch (ctrl->id) { 151 case V4L2_CID_JPEG_RESTART_INTERVAL: 152 ctx->restart_interval = ctrl->val; 153 break; 154 case V4L2_CID_JPEG_COMPRESSION_QUALITY: 155 ctx->enc_quality = ctrl->val; 156 break; 157 case V4L2_CID_JPEG_ACTIVE_MARKER: 158 ctx->enable_exif = ctrl->val & V4L2_JPEG_ACTIVE_MARKER_APP1; 159 break; 160 } 161 162 return 0; 163 } 164 165 static const struct v4l2_ctrl_ops mtk_jpeg_enc_ctrl_ops = { 166 .s_ctrl = vidioc_jpeg_enc_s_ctrl, 167 }; 168 169 static int mtk_jpeg_enc_ctrls_setup(struct mtk_jpeg_ctx *ctx) 170 { 171 const struct v4l2_ctrl_ops *ops = &mtk_jpeg_enc_ctrl_ops; 172 struct v4l2_ctrl_handler *handler = &ctx->ctrl_hdl; 173 174 v4l2_ctrl_handler_init(handler, 3); 175 176 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_RESTART_INTERVAL, 0, 100, 177 1, 0); 178 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_COMPRESSION_QUALITY, 48, 179 100, 1, 90); 180 v4l2_ctrl_new_std(handler, ops, V4L2_CID_JPEG_ACTIVE_MARKER, 0, 181 V4L2_JPEG_ACTIVE_MARKER_APP1, 0, 0); 182 183 if (handler->error) { 184 v4l2_ctrl_handler_free(&ctx->ctrl_hdl); 185 return handler->error; 186 } 187 188 v4l2_ctrl_handler_setup(&ctx->ctrl_hdl); 189 190 return 0; 191 } 192 193 static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n, 194 struct v4l2_fmtdesc *f, u32 type) 195 { 196 int i, num = 0; 197 198 for (i = 0; i < n; ++i) { 199 if (mtk_jpeg_formats[i].flags & type) { 200 if (num == f->index) 201 break; 202 ++num; 203 } 204 } 205 206 if (i >= n) 207 return -EINVAL; 208 209 f->pixelformat = mtk_jpeg_formats[i].fourcc; 210 211 return 0; 212 } 213 214 static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv, 215 struct v4l2_fmtdesc *f) 216 { 217 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 218 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 219 220 return mtk_jpeg_enum_fmt(jpeg->variant->formats, 221 jpeg->variant->num_formats, f, 222 MTK_JPEG_FMT_FLAG_CAPTURE); 223 } 224 225 static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv, 226 struct v4l2_fmtdesc *f) 227 { 228 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 229 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 230 231 return mtk_jpeg_enum_fmt(jpeg->variant->formats, 232 jpeg->variant->num_formats, f, 233 MTK_JPEG_FMT_FLAG_OUTPUT); 234 } 235 236 static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx, 237 enum v4l2_buf_type type) 238 { 239 if (V4L2_TYPE_IS_OUTPUT(type)) 240 return &ctx->out_q; 241 return &ctx->cap_q; 242 } 243 244 static struct mtk_jpeg_fmt * 245 mtk_jpeg_find_format(struct mtk_jpeg_fmt *mtk_jpeg_formats, int num_formats, 246 u32 pixelformat, unsigned int fmt_type) 247 { 248 unsigned int k; 249 struct mtk_jpeg_fmt *fmt; 250 251 for (k = 0; k < num_formats; k++) { 252 fmt = &mtk_jpeg_formats[k]; 253 254 if (fmt->fourcc == pixelformat && fmt->flags & fmt_type) 255 return fmt; 256 } 257 258 return NULL; 259 } 260 261 static int mtk_jpeg_try_fmt_mplane(struct v4l2_pix_format_mplane *pix_mp, 262 struct mtk_jpeg_fmt *fmt) 263 { 264 int i; 265 266 pix_mp->field = V4L2_FIELD_NONE; 267 268 pix_mp->num_planes = fmt->colplanes; 269 pix_mp->pixelformat = fmt->fourcc; 270 271 if (fmt->fourcc == V4L2_PIX_FMT_JPEG) { 272 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0]; 273 274 pix_mp->height = clamp(pix_mp->height, MTK_JPEG_MIN_HEIGHT, 275 MTK_JPEG_MAX_HEIGHT); 276 pix_mp->width = clamp(pix_mp->width, MTK_JPEG_MIN_WIDTH, 277 MTK_JPEG_MAX_WIDTH); 278 279 pfmt->bytesperline = 0; 280 /* Source size must be aligned to 128 */ 281 pfmt->sizeimage = round_up(pfmt->sizeimage, 128); 282 if (pfmt->sizeimage == 0) 283 pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE; 284 return 0; 285 } 286 287 /* other fourcc */ 288 pix_mp->height = clamp(round_up(pix_mp->height, fmt->v_align), 289 MTK_JPEG_MIN_HEIGHT, MTK_JPEG_MAX_HEIGHT); 290 pix_mp->width = clamp(round_up(pix_mp->width, fmt->h_align), 291 MTK_JPEG_MIN_WIDTH, MTK_JPEG_MAX_WIDTH); 292 293 for (i = 0; i < fmt->colplanes; i++) { 294 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i]; 295 u32 stride = pix_mp->width * fmt->h_sample[i] / 4; 296 u32 h = pix_mp->height * fmt->v_sample[i] / 4; 297 298 pfmt->bytesperline = stride; 299 pfmt->sizeimage = stride * h; 300 } 301 return 0; 302 } 303 304 static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv, 305 struct v4l2_format *f) 306 { 307 struct vb2_queue *vq; 308 struct mtk_jpeg_q_data *q_data = NULL; 309 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 310 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 311 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 312 int i; 313 314 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 315 if (!vq) 316 return -EINVAL; 317 318 q_data = mtk_jpeg_get_q_data(ctx, f->type); 319 320 pix_mp->width = q_data->pix_mp.width; 321 pix_mp->height = q_data->pix_mp.height; 322 pix_mp->field = V4L2_FIELD_NONE; 323 pix_mp->pixelformat = q_data->fmt->fourcc; 324 pix_mp->num_planes = q_data->fmt->colplanes; 325 pix_mp->colorspace = q_data->pix_mp.colorspace; 326 pix_mp->ycbcr_enc = q_data->pix_mp.ycbcr_enc; 327 pix_mp->xfer_func = q_data->pix_mp.xfer_func; 328 pix_mp->quantization = q_data->pix_mp.quantization; 329 330 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n", 331 f->type, 332 (pix_mp->pixelformat & 0xff), 333 (pix_mp->pixelformat >> 8 & 0xff), 334 (pix_mp->pixelformat >> 16 & 0xff), 335 (pix_mp->pixelformat >> 24 & 0xff), 336 pix_mp->width, pix_mp->height); 337 338 for (i = 0; i < pix_mp->num_planes; i++) { 339 struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i]; 340 341 pfmt->bytesperline = q_data->pix_mp.plane_fmt[i].bytesperline; 342 pfmt->sizeimage = q_data->pix_mp.plane_fmt[i].sizeimage; 343 344 v4l2_dbg(1, debug, &jpeg->v4l2_dev, 345 "plane[%d] bpl=%u, size=%u\n", 346 i, 347 pfmt->bytesperline, 348 pfmt->sizeimage); 349 } 350 return 0; 351 } 352 353 static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv, 354 struct v4l2_format *f) 355 { 356 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 357 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 358 struct mtk_jpeg_fmt *fmt; 359 360 fmt = mtk_jpeg_find_format(jpeg->variant->formats, 361 jpeg->variant->num_formats, 362 f->fmt.pix_mp.pixelformat, 363 MTK_JPEG_FMT_FLAG_CAPTURE); 364 if (!fmt) 365 fmt = ctx->cap_q.fmt; 366 367 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n", 368 f->type, 369 (fmt->fourcc & 0xff), 370 (fmt->fourcc >> 8 & 0xff), 371 (fmt->fourcc >> 16 & 0xff), 372 (fmt->fourcc >> 24 & 0xff)); 373 374 if (ctx->state != MTK_JPEG_INIT) { 375 mtk_jpeg_g_fmt_vid_mplane(file, priv, f); 376 return 0; 377 } 378 379 return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt); 380 } 381 382 static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv, 383 struct v4l2_format *f) 384 { 385 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 386 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 387 struct mtk_jpeg_fmt *fmt; 388 389 fmt = mtk_jpeg_find_format(jpeg->variant->formats, 390 jpeg->variant->num_formats, 391 f->fmt.pix_mp.pixelformat, 392 MTK_JPEG_FMT_FLAG_OUTPUT); 393 if (!fmt) 394 fmt = ctx->out_q.fmt; 395 396 v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n", 397 f->type, 398 (fmt->fourcc & 0xff), 399 (fmt->fourcc >> 8 & 0xff), 400 (fmt->fourcc >> 16 & 0xff), 401 (fmt->fourcc >> 24 & 0xff)); 402 403 if (ctx->state != MTK_JPEG_INIT) { 404 mtk_jpeg_g_fmt_vid_mplane(file, priv, f); 405 return 0; 406 } 407 408 return mtk_jpeg_try_fmt_mplane(&f->fmt.pix_mp, fmt); 409 } 410 411 static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx, 412 struct v4l2_format *f, unsigned int fmt_type) 413 { 414 struct vb2_queue *vq; 415 struct mtk_jpeg_q_data *q_data = NULL; 416 struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp; 417 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 418 int i; 419 420 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); 421 if (!vq) 422 return -EINVAL; 423 424 q_data = mtk_jpeg_get_q_data(ctx, f->type); 425 426 if (vb2_is_busy(vq)) { 427 v4l2_err(&jpeg->v4l2_dev, "queue busy\n"); 428 return -EBUSY; 429 } 430 431 q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats, 432 jpeg->variant->num_formats, 433 pix_mp->pixelformat, fmt_type); 434 q_data->pix_mp.width = pix_mp->width; 435 q_data->pix_mp.height = pix_mp->height; 436 q_data->enc_crop_rect.width = pix_mp->width; 437 q_data->enc_crop_rect.height = pix_mp->height; 438 q_data->pix_mp.colorspace = V4L2_COLORSPACE_SRGB; 439 q_data->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601; 440 q_data->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB; 441 q_data->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE; 442 443 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n", 444 f->type, 445 (q_data->fmt->fourcc & 0xff), 446 (q_data->fmt->fourcc >> 8 & 0xff), 447 (q_data->fmt->fourcc >> 16 & 0xff), 448 (q_data->fmt->fourcc >> 24 & 0xff), 449 q_data->pix_mp.width, q_data->pix_mp.height); 450 451 for (i = 0; i < q_data->fmt->colplanes; i++) { 452 q_data->pix_mp.plane_fmt[i].bytesperline = 453 pix_mp->plane_fmt[i].bytesperline; 454 q_data->pix_mp.plane_fmt[i].sizeimage = 455 pix_mp->plane_fmt[i].sizeimage; 456 457 v4l2_dbg(1, debug, &jpeg->v4l2_dev, 458 "plane[%d] bpl=%u, size=%u\n", 459 i, q_data->pix_mp.plane_fmt[i].bytesperline, 460 q_data->pix_mp.plane_fmt[i].sizeimage); 461 } 462 463 return 0; 464 } 465 466 static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv, 467 struct v4l2_format *f) 468 { 469 int ret; 470 471 ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f); 472 if (ret) 473 return ret; 474 475 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f, 476 MTK_JPEG_FMT_FLAG_OUTPUT); 477 } 478 479 static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv, 480 struct v4l2_format *f) 481 { 482 int ret; 483 484 ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f); 485 if (ret) 486 return ret; 487 488 return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f, 489 MTK_JPEG_FMT_FLAG_CAPTURE); 490 } 491 492 static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx) 493 { 494 static const struct v4l2_event ev_src_ch = { 495 .type = V4L2_EVENT_SOURCE_CHANGE, 496 .u.src_change.changes = 497 V4L2_EVENT_SRC_CH_RESOLUTION, 498 }; 499 500 v4l2_event_queue_fh(&ctx->fh, &ev_src_ch); 501 } 502 503 static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh, 504 const struct v4l2_event_subscription *sub) 505 { 506 switch (sub->type) { 507 case V4L2_EVENT_SOURCE_CHANGE: 508 return v4l2_src_change_event_subscribe(fh, sub); 509 } 510 511 return v4l2_ctrl_subscribe_event(fh, sub); 512 } 513 514 static int mtk_jpeg_enc_g_selection(struct file *file, void *priv, 515 struct v4l2_selection *s) 516 { 517 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 518 519 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 520 return -EINVAL; 521 522 switch (s->target) { 523 case V4L2_SEL_TGT_CROP: 524 s->r = ctx->out_q.enc_crop_rect; 525 break; 526 case V4L2_SEL_TGT_CROP_BOUNDS: 527 case V4L2_SEL_TGT_CROP_DEFAULT: 528 s->r.width = ctx->out_q.pix_mp.width; 529 s->r.height = ctx->out_q.pix_mp.height; 530 s->r.left = 0; 531 s->r.top = 0; 532 break; 533 default: 534 return -EINVAL; 535 } 536 return 0; 537 } 538 539 static int mtk_jpeg_dec_g_selection(struct file *file, void *priv, 540 struct v4l2_selection *s) 541 { 542 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 543 544 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 545 return -EINVAL; 546 547 switch (s->target) { 548 case V4L2_SEL_TGT_COMPOSE: 549 case V4L2_SEL_TGT_COMPOSE_DEFAULT: 550 s->r.width = ctx->out_q.pix_mp.width; 551 s->r.height = ctx->out_q.pix_mp.height; 552 s->r.left = 0; 553 s->r.top = 0; 554 break; 555 case V4L2_SEL_TGT_COMPOSE_BOUNDS: 556 case V4L2_SEL_TGT_COMPOSE_PADDED: 557 s->r.width = ctx->cap_q.pix_mp.width; 558 s->r.height = ctx->cap_q.pix_mp.height; 559 s->r.left = 0; 560 s->r.top = 0; 561 break; 562 default: 563 return -EINVAL; 564 } 565 return 0; 566 } 567 568 static int mtk_jpeg_enc_s_selection(struct file *file, void *priv, 569 struct v4l2_selection *s) 570 { 571 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv); 572 573 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) 574 return -EINVAL; 575 576 switch (s->target) { 577 case V4L2_SEL_TGT_CROP: 578 s->r.left = 0; 579 s->r.top = 0; 580 s->r.width = min(s->r.width, ctx->out_q.pix_mp.width); 581 s->r.height = min(s->r.height, ctx->out_q.pix_mp.height); 582 ctx->out_q.enc_crop_rect = s->r; 583 break; 584 default: 585 return -EINVAL; 586 } 587 588 return 0; 589 } 590 591 static const struct v4l2_ioctl_ops mtk_jpeg_enc_ioctl_ops = { 592 .vidioc_querycap = mtk_jpeg_querycap, 593 .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap, 594 .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out, 595 .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane, 596 .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane, 597 .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane, 598 .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane, 599 .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane, 600 .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane, 601 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 602 .vidioc_subscribe_event = mtk_jpeg_subscribe_event, 603 .vidioc_g_selection = mtk_jpeg_enc_g_selection, 604 .vidioc_s_selection = mtk_jpeg_enc_s_selection, 605 606 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 607 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 608 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 609 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 610 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 611 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 612 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 613 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 614 615 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 616 }; 617 618 static const struct v4l2_ioctl_ops mtk_jpeg_dec_ioctl_ops = { 619 .vidioc_querycap = mtk_jpeg_querycap, 620 .vidioc_enum_fmt_vid_cap = mtk_jpeg_enum_fmt_vid_cap, 621 .vidioc_enum_fmt_vid_out = mtk_jpeg_enum_fmt_vid_out, 622 .vidioc_try_fmt_vid_cap_mplane = mtk_jpeg_try_fmt_vid_cap_mplane, 623 .vidioc_try_fmt_vid_out_mplane = mtk_jpeg_try_fmt_vid_out_mplane, 624 .vidioc_g_fmt_vid_cap_mplane = mtk_jpeg_g_fmt_vid_mplane, 625 .vidioc_g_fmt_vid_out_mplane = mtk_jpeg_g_fmt_vid_mplane, 626 .vidioc_s_fmt_vid_cap_mplane = mtk_jpeg_s_fmt_vid_cap_mplane, 627 .vidioc_s_fmt_vid_out_mplane = mtk_jpeg_s_fmt_vid_out_mplane, 628 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, 629 .vidioc_subscribe_event = mtk_jpeg_subscribe_event, 630 .vidioc_g_selection = mtk_jpeg_dec_g_selection, 631 632 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, 633 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, 634 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, 635 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, 636 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, 637 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, 638 .vidioc_streamon = v4l2_m2m_ioctl_streamon, 639 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, 640 641 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 642 }; 643 644 static int mtk_jpeg_queue_setup(struct vb2_queue *q, 645 unsigned int *num_buffers, 646 unsigned int *num_planes, 647 unsigned int sizes[], 648 struct device *alloc_ctxs[]) 649 { 650 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q); 651 struct mtk_jpeg_q_data *q_data = NULL; 652 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 653 int i; 654 655 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n", 656 q->type, *num_buffers); 657 658 q_data = mtk_jpeg_get_q_data(ctx, q->type); 659 if (!q_data) 660 return -EINVAL; 661 662 if (*num_planes) { 663 for (i = 0; i < *num_planes; i++) 664 if (sizes[i] < q_data->pix_mp.plane_fmt[i].sizeimage) 665 return -EINVAL; 666 return 0; 667 } 668 669 *num_planes = q_data->fmt->colplanes; 670 for (i = 0; i < q_data->fmt->colplanes; i++) { 671 sizes[i] = q_data->pix_mp.plane_fmt[i].sizeimage; 672 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n", 673 i, sizes[i]); 674 } 675 676 return 0; 677 } 678 679 static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb) 680 { 681 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 682 struct mtk_jpeg_q_data *q_data = NULL; 683 struct v4l2_plane_pix_format plane_fmt; 684 int i; 685 686 q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type); 687 if (!q_data) 688 return -EINVAL; 689 690 for (i = 0; i < q_data->fmt->colplanes; i++) { 691 plane_fmt = q_data->pix_mp.plane_fmt[i]; 692 if (ctx->enable_exif && 693 q_data->fmt->fourcc == V4L2_PIX_FMT_JPEG) 694 vb2_set_plane_payload(vb, i, plane_fmt.sizeimage + 695 MTK_JPEG_MAX_EXIF_SIZE); 696 else 697 vb2_set_plane_payload(vb, i, plane_fmt.sizeimage); 698 } 699 700 return 0; 701 } 702 703 static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx, 704 struct mtk_jpeg_dec_param *param) 705 { 706 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 707 struct mtk_jpeg_q_data *q_data; 708 709 q_data = &ctx->out_q; 710 if (q_data->pix_mp.width != param->pic_w || 711 q_data->pix_mp.height != param->pic_h) { 712 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n"); 713 return true; 714 } 715 716 q_data = &ctx->cap_q; 717 if (q_data->fmt != 718 mtk_jpeg_find_format(jpeg->variant->formats, 719 jpeg->variant->num_formats, param->dst_fourcc, 720 MTK_JPEG_FMT_FLAG_CAPTURE)) { 721 v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n"); 722 return true; 723 } 724 return false; 725 } 726 727 static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx, 728 struct mtk_jpeg_dec_param *param) 729 { 730 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 731 struct mtk_jpeg_q_data *q_data; 732 int i; 733 734 q_data = &ctx->out_q; 735 q_data->pix_mp.width = param->pic_w; 736 q_data->pix_mp.height = param->pic_h; 737 738 q_data = &ctx->cap_q; 739 q_data->pix_mp.width = param->dec_w; 740 q_data->pix_mp.height = param->dec_h; 741 q_data->fmt = mtk_jpeg_find_format(jpeg->variant->formats, 742 jpeg->variant->num_formats, 743 param->dst_fourcc, 744 MTK_JPEG_FMT_FLAG_CAPTURE); 745 746 for (i = 0; i < q_data->fmt->colplanes; i++) { 747 q_data->pix_mp.plane_fmt[i].bytesperline = param->mem_stride[i]; 748 q_data->pix_mp.plane_fmt[i].sizeimage = param->comp_size[i]; 749 } 750 751 v4l2_dbg(1, debug, &jpeg->v4l2_dev, 752 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n", 753 (param->dst_fourcc & 0xff), 754 (param->dst_fourcc >> 8 & 0xff), 755 (param->dst_fourcc >> 16 & 0xff), 756 (param->dst_fourcc >> 24 & 0xff), 757 param->pic_w, param->pic_h, 758 param->dec_w, param->dec_h); 759 } 760 761 static void mtk_jpeg_enc_buf_queue(struct vb2_buffer *vb) 762 { 763 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 764 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 765 766 v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n", 767 vb->vb2_queue->type, vb->index, vb); 768 769 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb)); 770 } 771 772 static void mtk_jpeg_dec_buf_queue(struct vb2_buffer *vb) 773 { 774 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); 775 struct mtk_jpeg_dec_param *param; 776 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 777 struct mtk_jpeg_src_buf *jpeg_src_buf; 778 bool header_valid; 779 780 v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n", 781 vb->vb2_queue->type, vb->index, vb); 782 783 if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) 784 goto end; 785 786 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb); 787 param = &jpeg_src_buf->dec_param; 788 memset(param, 0, sizeof(*param)); 789 790 header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0), 791 vb2_get_plane_payload(vb, 0)); 792 if (!header_valid) { 793 v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n"); 794 vb2_buffer_done(vb, VB2_BUF_STATE_ERROR); 795 return; 796 } 797 798 if (ctx->state == MTK_JPEG_INIT) { 799 struct vb2_queue *dst_vq = v4l2_m2m_get_vq( 800 ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); 801 802 mtk_jpeg_queue_src_chg_event(ctx); 803 mtk_jpeg_set_queue_data(ctx, param); 804 ctx->state = vb2_is_streaming(dst_vq) ? 805 MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING; 806 } 807 end: 808 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb)); 809 } 810 811 static struct vb2_v4l2_buffer *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx, 812 enum v4l2_buf_type type) 813 { 814 if (V4L2_TYPE_IS_OUTPUT(type)) 815 return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 816 else 817 return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 818 } 819 820 static void mtk_jpeg_enc_stop_streaming(struct vb2_queue *q) 821 { 822 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q); 823 struct vb2_v4l2_buffer *vb; 824 825 while ((vb = mtk_jpeg_buf_remove(ctx, q->type))) 826 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR); 827 } 828 829 static void mtk_jpeg_dec_stop_streaming(struct vb2_queue *q) 830 { 831 struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q); 832 struct vb2_v4l2_buffer *vb; 833 834 /* 835 * STREAMOFF is an acknowledgment for source change event. 836 * Before STREAMOFF, we still have to return the old resolution and 837 * subsampling. Update capture queue when the stream is off. 838 */ 839 if (ctx->state == MTK_JPEG_SOURCE_CHANGE && 840 V4L2_TYPE_IS_CAPTURE(q->type)) { 841 struct mtk_jpeg_src_buf *src_buf; 842 843 vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 844 src_buf = mtk_jpeg_vb2_to_srcbuf(&vb->vb2_buf); 845 mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param); 846 ctx->state = MTK_JPEG_RUNNING; 847 } else if (V4L2_TYPE_IS_OUTPUT(q->type)) { 848 ctx->state = MTK_JPEG_INIT; 849 } 850 851 while ((vb = mtk_jpeg_buf_remove(ctx, q->type))) 852 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR); 853 } 854 855 static const struct vb2_ops mtk_jpeg_dec_qops = { 856 .queue_setup = mtk_jpeg_queue_setup, 857 .buf_prepare = mtk_jpeg_buf_prepare, 858 .buf_queue = mtk_jpeg_dec_buf_queue, 859 .wait_prepare = vb2_ops_wait_prepare, 860 .wait_finish = vb2_ops_wait_finish, 861 .stop_streaming = mtk_jpeg_dec_stop_streaming, 862 }; 863 864 static const struct vb2_ops mtk_jpeg_enc_qops = { 865 .queue_setup = mtk_jpeg_queue_setup, 866 .buf_prepare = mtk_jpeg_buf_prepare, 867 .buf_queue = mtk_jpeg_enc_buf_queue, 868 .wait_prepare = vb2_ops_wait_prepare, 869 .wait_finish = vb2_ops_wait_finish, 870 .stop_streaming = mtk_jpeg_enc_stop_streaming, 871 }; 872 873 static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx, 874 struct vb2_buffer *src_buf, 875 struct mtk_jpeg_bs *bs) 876 { 877 bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0); 878 bs->end_addr = bs->str_addr + 879 round_up(vb2_get_plane_payload(src_buf, 0), 16); 880 bs->size = round_up(vb2_plane_size(src_buf, 0), 128); 881 } 882 883 static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx, 884 struct mtk_jpeg_dec_param *param, 885 struct vb2_buffer *dst_buf, 886 struct mtk_jpeg_fb *fb) 887 { 888 int i; 889 890 if (param->comp_num != dst_buf->num_planes) { 891 dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n", 892 param->comp_num, dst_buf->num_planes); 893 return -EINVAL; 894 } 895 896 for (i = 0; i < dst_buf->num_planes; i++) { 897 if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) { 898 dev_err(ctx->jpeg->dev, 899 "buffer size is underflow (%lu < %u)\n", 900 vb2_plane_size(dst_buf, 0), 901 param->comp_size[i]); 902 return -EINVAL; 903 } 904 fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i); 905 } 906 907 return 0; 908 } 909 910 static void mtk_jpeg_enc_device_run(void *priv) 911 { 912 struct mtk_jpeg_ctx *ctx = priv; 913 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 914 struct vb2_v4l2_buffer *src_buf, *dst_buf; 915 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR; 916 unsigned long flags; 917 int ret; 918 919 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 920 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 921 922 ret = pm_runtime_resume_and_get(jpeg->dev); 923 if (ret < 0) 924 goto enc_end; 925 926 schedule_delayed_work(&jpeg->job_timeout_work, 927 msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC)); 928 929 spin_lock_irqsave(&jpeg->hw_lock, flags); 930 931 /* 932 * Resetting the hardware every frame is to ensure that all the 933 * registers are cleared. This is a hardware requirement. 934 */ 935 mtk_jpeg_enc_reset(jpeg->reg_base); 936 937 mtk_jpeg_set_enc_src(ctx, jpeg->reg_base, &src_buf->vb2_buf); 938 mtk_jpeg_set_enc_dst(ctx, jpeg->reg_base, &dst_buf->vb2_buf); 939 mtk_jpeg_set_enc_params(ctx, jpeg->reg_base); 940 mtk_jpeg_enc_start(jpeg->reg_base); 941 spin_unlock_irqrestore(&jpeg->hw_lock, flags); 942 return; 943 944 enc_end: 945 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 946 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 947 v4l2_m2m_buf_done(src_buf, buf_state); 948 v4l2_m2m_buf_done(dst_buf, buf_state); 949 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 950 } 951 952 static void mtk_jpeg_dec_device_run(void *priv) 953 { 954 struct mtk_jpeg_ctx *ctx = priv; 955 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 956 struct vb2_v4l2_buffer *src_buf, *dst_buf; 957 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR; 958 unsigned long flags; 959 struct mtk_jpeg_src_buf *jpeg_src_buf; 960 struct mtk_jpeg_bs bs; 961 struct mtk_jpeg_fb fb; 962 int ret; 963 964 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); 965 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); 966 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf); 967 968 if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) { 969 mtk_jpeg_queue_src_chg_event(ctx); 970 ctx->state = MTK_JPEG_SOURCE_CHANGE; 971 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 972 return; 973 } 974 975 ret = pm_runtime_resume_and_get(jpeg->dev); 976 if (ret < 0) 977 goto dec_end; 978 979 schedule_delayed_work(&jpeg->job_timeout_work, 980 msecs_to_jiffies(MTK_JPEG_HW_TIMEOUT_MSEC)); 981 982 mtk_jpeg_set_dec_src(ctx, &src_buf->vb2_buf, &bs); 983 if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, &dst_buf->vb2_buf, &fb)) 984 goto dec_end; 985 986 spin_lock_irqsave(&jpeg->hw_lock, flags); 987 mtk_jpeg_dec_reset(jpeg->reg_base); 988 mtk_jpeg_dec_set_config(jpeg->reg_base, 989 &jpeg_src_buf->dec_param, &bs, &fb); 990 991 mtk_jpeg_dec_start(jpeg->reg_base); 992 spin_unlock_irqrestore(&jpeg->hw_lock, flags); 993 return; 994 995 dec_end: 996 v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 997 v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 998 v4l2_m2m_buf_done(src_buf, buf_state); 999 v4l2_m2m_buf_done(dst_buf, buf_state); 1000 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1001 } 1002 1003 static int mtk_jpeg_dec_job_ready(void *priv) 1004 { 1005 struct mtk_jpeg_ctx *ctx = priv; 1006 1007 return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0; 1008 } 1009 1010 static const struct v4l2_m2m_ops mtk_jpeg_enc_m2m_ops = { 1011 .device_run = mtk_jpeg_enc_device_run, 1012 }; 1013 1014 static const struct v4l2_m2m_ops mtk_jpeg_dec_m2m_ops = { 1015 .device_run = mtk_jpeg_dec_device_run, 1016 .job_ready = mtk_jpeg_dec_job_ready, 1017 }; 1018 1019 static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq, 1020 struct vb2_queue *dst_vq) 1021 { 1022 struct mtk_jpeg_ctx *ctx = priv; 1023 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 1024 int ret; 1025 1026 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; 1027 src_vq->io_modes = VB2_DMABUF | VB2_MMAP; 1028 src_vq->drv_priv = ctx; 1029 src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf); 1030 src_vq->ops = jpeg->variant->qops; 1031 src_vq->mem_ops = &vb2_dma_contig_memops; 1032 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1033 src_vq->lock = &ctx->jpeg->lock; 1034 src_vq->dev = ctx->jpeg->dev; 1035 ret = vb2_queue_init(src_vq); 1036 if (ret) 1037 return ret; 1038 1039 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; 1040 dst_vq->io_modes = VB2_DMABUF | VB2_MMAP; 1041 dst_vq->drv_priv = ctx; 1042 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); 1043 dst_vq->ops = jpeg->variant->qops; 1044 dst_vq->mem_ops = &vb2_dma_contig_memops; 1045 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; 1046 dst_vq->lock = &ctx->jpeg->lock; 1047 dst_vq->dev = ctx->jpeg->dev; 1048 ret = vb2_queue_init(dst_vq); 1049 1050 return ret; 1051 } 1052 1053 static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg) 1054 { 1055 int ret; 1056 1057 ret = clk_bulk_prepare_enable(jpeg->variant->num_clks, 1058 jpeg->variant->clks); 1059 if (ret) 1060 dev_err(jpeg->dev, "Failed to open jpeg clk: %d\n", ret); 1061 } 1062 1063 static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg) 1064 { 1065 clk_bulk_disable_unprepare(jpeg->variant->num_clks, 1066 jpeg->variant->clks); 1067 } 1068 1069 static irqreturn_t mtk_jpeg_enc_done(struct mtk_jpeg_dev *jpeg) 1070 { 1071 struct mtk_jpeg_ctx *ctx; 1072 struct vb2_v4l2_buffer *src_buf, *dst_buf; 1073 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR; 1074 u32 result_size; 1075 1076 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); 1077 if (!ctx) { 1078 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n"); 1079 return IRQ_HANDLED; 1080 } 1081 1082 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1083 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1084 1085 result_size = mtk_jpeg_enc_get_file_size(jpeg->reg_base); 1086 vb2_set_plane_payload(&dst_buf->vb2_buf, 0, result_size); 1087 1088 buf_state = VB2_BUF_STATE_DONE; 1089 1090 v4l2_m2m_buf_done(src_buf, buf_state); 1091 v4l2_m2m_buf_done(dst_buf, buf_state); 1092 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1093 pm_runtime_put(ctx->jpeg->dev); 1094 return IRQ_HANDLED; 1095 } 1096 1097 static irqreturn_t mtk_jpeg_enc_irq(int irq, void *priv) 1098 { 1099 struct mtk_jpeg_dev *jpeg = priv; 1100 u32 irq_status; 1101 irqreturn_t ret = IRQ_NONE; 1102 1103 cancel_delayed_work(&jpeg->job_timeout_work); 1104 1105 irq_status = readl(jpeg->reg_base + JPEG_ENC_INT_STS) & 1106 JPEG_ENC_INT_STATUS_MASK_ALLIRQ; 1107 if (irq_status) 1108 writel(0, jpeg->reg_base + JPEG_ENC_INT_STS); 1109 1110 if (!(irq_status & JPEG_ENC_INT_STATUS_DONE)) 1111 return ret; 1112 1113 ret = mtk_jpeg_enc_done(jpeg); 1114 return ret; 1115 } 1116 1117 static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv) 1118 { 1119 struct mtk_jpeg_dev *jpeg = priv; 1120 struct mtk_jpeg_ctx *ctx; 1121 struct vb2_v4l2_buffer *src_buf, *dst_buf; 1122 struct mtk_jpeg_src_buf *jpeg_src_buf; 1123 enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR; 1124 u32 dec_irq_ret; 1125 u32 dec_ret; 1126 int i; 1127 1128 cancel_delayed_work(&jpeg->job_timeout_work); 1129 1130 dec_ret = mtk_jpeg_dec_get_int_status(jpeg->reg_base); 1131 dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret); 1132 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); 1133 if (!ctx) { 1134 v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n"); 1135 return IRQ_HANDLED; 1136 } 1137 1138 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1139 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1140 jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(&src_buf->vb2_buf); 1141 1142 if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW) 1143 mtk_jpeg_dec_reset(jpeg->reg_base); 1144 1145 if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) { 1146 dev_err(jpeg->dev, "decode failed\n"); 1147 goto dec_end; 1148 } 1149 1150 for (i = 0; i < dst_buf->vb2_buf.num_planes; i++) 1151 vb2_set_plane_payload(&dst_buf->vb2_buf, i, 1152 jpeg_src_buf->dec_param.comp_size[i]); 1153 1154 buf_state = VB2_BUF_STATE_DONE; 1155 1156 dec_end: 1157 v4l2_m2m_buf_done(src_buf, buf_state); 1158 v4l2_m2m_buf_done(dst_buf, buf_state); 1159 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1160 pm_runtime_put(ctx->jpeg->dev); 1161 return IRQ_HANDLED; 1162 } 1163 1164 static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx) 1165 { 1166 struct mtk_jpeg_q_data *q = &ctx->out_q; 1167 struct mtk_jpeg_dev *jpeg = ctx->jpeg; 1168 1169 ctx->fh.ctrl_handler = &ctx->ctrl_hdl; 1170 q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB; 1171 q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601; 1172 q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE; 1173 q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB; 1174 1175 q->fmt = mtk_jpeg_find_format(jpeg->variant->formats, 1176 jpeg->variant->num_formats, 1177 jpeg->variant->out_q_default_fourcc, 1178 MTK_JPEG_FMT_FLAG_OUTPUT); 1179 q->pix_mp.width = MTK_JPEG_MIN_WIDTH; 1180 q->pix_mp.height = MTK_JPEG_MIN_HEIGHT; 1181 mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt); 1182 1183 q = &ctx->cap_q; 1184 q->fmt = mtk_jpeg_find_format(jpeg->variant->formats, 1185 jpeg->variant->num_formats, 1186 jpeg->variant->cap_q_default_fourcc, 1187 MTK_JPEG_FMT_FLAG_CAPTURE); 1188 q->pix_mp.colorspace = V4L2_COLORSPACE_SRGB; 1189 q->pix_mp.ycbcr_enc = V4L2_YCBCR_ENC_601; 1190 q->pix_mp.quantization = V4L2_QUANTIZATION_FULL_RANGE; 1191 q->pix_mp.xfer_func = V4L2_XFER_FUNC_SRGB; 1192 q->pix_mp.width = MTK_JPEG_MIN_WIDTH; 1193 q->pix_mp.height = MTK_JPEG_MIN_HEIGHT; 1194 1195 mtk_jpeg_try_fmt_mplane(&q->pix_mp, q->fmt); 1196 } 1197 1198 static int mtk_jpeg_open(struct file *file) 1199 { 1200 struct mtk_jpeg_dev *jpeg = video_drvdata(file); 1201 struct video_device *vfd = video_devdata(file); 1202 struct mtk_jpeg_ctx *ctx; 1203 int ret = 0; 1204 1205 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 1206 if (!ctx) 1207 return -ENOMEM; 1208 1209 if (mutex_lock_interruptible(&jpeg->lock)) { 1210 ret = -ERESTARTSYS; 1211 goto free; 1212 } 1213 1214 v4l2_fh_init(&ctx->fh, vfd); 1215 file->private_data = &ctx->fh; 1216 v4l2_fh_add(&ctx->fh); 1217 1218 ctx->jpeg = jpeg; 1219 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx, 1220 mtk_jpeg_queue_init); 1221 if (IS_ERR(ctx->fh.m2m_ctx)) { 1222 ret = PTR_ERR(ctx->fh.m2m_ctx); 1223 goto error; 1224 } 1225 1226 if (jpeg->variant->cap_q_default_fourcc == V4L2_PIX_FMT_JPEG) { 1227 ret = mtk_jpeg_enc_ctrls_setup(ctx); 1228 if (ret) { 1229 v4l2_err(&jpeg->v4l2_dev, "Failed to setup jpeg enc controls\n"); 1230 goto error; 1231 } 1232 } else { 1233 v4l2_ctrl_handler_init(&ctx->ctrl_hdl, 0); 1234 } 1235 mtk_jpeg_set_default_params(ctx); 1236 mutex_unlock(&jpeg->lock); 1237 return 0; 1238 1239 error: 1240 v4l2_fh_del(&ctx->fh); 1241 v4l2_fh_exit(&ctx->fh); 1242 mutex_unlock(&jpeg->lock); 1243 free: 1244 kfree(ctx); 1245 return ret; 1246 } 1247 1248 static int mtk_jpeg_release(struct file *file) 1249 { 1250 struct mtk_jpeg_dev *jpeg = video_drvdata(file); 1251 struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data); 1252 1253 mutex_lock(&jpeg->lock); 1254 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); 1255 v4l2_ctrl_handler_free(&ctx->ctrl_hdl); 1256 v4l2_fh_del(&ctx->fh); 1257 v4l2_fh_exit(&ctx->fh); 1258 kfree(ctx); 1259 mutex_unlock(&jpeg->lock); 1260 return 0; 1261 } 1262 1263 static const struct v4l2_file_operations mtk_jpeg_fops = { 1264 .owner = THIS_MODULE, 1265 .open = mtk_jpeg_open, 1266 .release = mtk_jpeg_release, 1267 .poll = v4l2_m2m_fop_poll, 1268 .unlocked_ioctl = video_ioctl2, 1269 .mmap = v4l2_m2m_fop_mmap, 1270 }; 1271 1272 static struct clk_bulk_data mt8173_jpeg_dec_clocks[] = { 1273 { .id = "jpgdec-smi" }, 1274 { .id = "jpgdec" }, 1275 }; 1276 1277 static struct clk_bulk_data mtk_jpeg_clocks[] = { 1278 { .id = "jpgenc" }, 1279 }; 1280 1281 static void mtk_jpeg_job_timeout_work(struct work_struct *work) 1282 { 1283 struct mtk_jpeg_dev *jpeg = container_of(work, struct mtk_jpeg_dev, 1284 job_timeout_work.work); 1285 struct mtk_jpeg_ctx *ctx; 1286 struct vb2_v4l2_buffer *src_buf, *dst_buf; 1287 1288 ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev); 1289 src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); 1290 dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); 1291 1292 jpeg->variant->hw_reset(jpeg->reg_base); 1293 1294 pm_runtime_put(jpeg->dev); 1295 1296 v4l2_m2m_buf_done(src_buf, VB2_BUF_STATE_ERROR); 1297 v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_ERROR); 1298 v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx); 1299 } 1300 1301 static int mtk_jpeg_probe(struct platform_device *pdev) 1302 { 1303 struct mtk_jpeg_dev *jpeg; 1304 int jpeg_irq; 1305 int ret; 1306 1307 jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL); 1308 if (!jpeg) 1309 return -ENOMEM; 1310 1311 mutex_init(&jpeg->lock); 1312 spin_lock_init(&jpeg->hw_lock); 1313 jpeg->dev = &pdev->dev; 1314 jpeg->variant = of_device_get_match_data(jpeg->dev); 1315 INIT_DELAYED_WORK(&jpeg->job_timeout_work, mtk_jpeg_job_timeout_work); 1316 1317 jpeg->reg_base = devm_platform_ioremap_resource(pdev, 0); 1318 if (IS_ERR(jpeg->reg_base)) { 1319 ret = PTR_ERR(jpeg->reg_base); 1320 return ret; 1321 } 1322 1323 jpeg_irq = platform_get_irq(pdev, 0); 1324 if (jpeg_irq < 0) 1325 return jpeg_irq; 1326 1327 ret = devm_request_irq(&pdev->dev, jpeg_irq, 1328 jpeg->variant->irq_handler, 0, pdev->name, jpeg); 1329 if (ret) { 1330 dev_err(&pdev->dev, "Failed to request jpeg_irq %d (%d)\n", 1331 jpeg_irq, ret); 1332 goto err_req_irq; 1333 } 1334 1335 ret = devm_clk_bulk_get(jpeg->dev, jpeg->variant->num_clks, 1336 jpeg->variant->clks); 1337 if (ret) { 1338 dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret); 1339 goto err_clk_init; 1340 } 1341 1342 ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev); 1343 if (ret) { 1344 dev_err(&pdev->dev, "Failed to register v4l2 device\n"); 1345 ret = -EINVAL; 1346 goto err_dev_register; 1347 } 1348 1349 jpeg->m2m_dev = v4l2_m2m_init(jpeg->variant->m2m_ops); 1350 1351 if (IS_ERR(jpeg->m2m_dev)) { 1352 v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n"); 1353 ret = PTR_ERR(jpeg->m2m_dev); 1354 goto err_m2m_init; 1355 } 1356 1357 jpeg->vdev = video_device_alloc(); 1358 if (!jpeg->vdev) { 1359 ret = -ENOMEM; 1360 goto err_vfd_jpeg_alloc; 1361 } 1362 snprintf(jpeg->vdev->name, sizeof(jpeg->vdev->name), 1363 "%s", jpeg->variant->dev_name); 1364 jpeg->vdev->fops = &mtk_jpeg_fops; 1365 jpeg->vdev->ioctl_ops = jpeg->variant->ioctl_ops; 1366 jpeg->vdev->minor = -1; 1367 jpeg->vdev->release = video_device_release; 1368 jpeg->vdev->lock = &jpeg->lock; 1369 jpeg->vdev->v4l2_dev = &jpeg->v4l2_dev; 1370 jpeg->vdev->vfl_dir = VFL_DIR_M2M; 1371 jpeg->vdev->device_caps = V4L2_CAP_STREAMING | 1372 V4L2_CAP_VIDEO_M2M_MPLANE; 1373 1374 ret = video_register_device(jpeg->vdev, VFL_TYPE_VIDEO, -1); 1375 if (ret) { 1376 v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n"); 1377 goto err_vfd_jpeg_register; 1378 } 1379 1380 video_set_drvdata(jpeg->vdev, jpeg); 1381 v4l2_info(&jpeg->v4l2_dev, 1382 "%s device registered as /dev/video%d (%d,%d)\n", 1383 jpeg->variant->dev_name, jpeg->vdev->num, 1384 VIDEO_MAJOR, jpeg->vdev->minor); 1385 1386 platform_set_drvdata(pdev, jpeg); 1387 1388 pm_runtime_enable(&pdev->dev); 1389 1390 return 0; 1391 1392 err_vfd_jpeg_register: 1393 video_device_release(jpeg->vdev); 1394 1395 err_vfd_jpeg_alloc: 1396 v4l2_m2m_release(jpeg->m2m_dev); 1397 1398 err_m2m_init: 1399 v4l2_device_unregister(&jpeg->v4l2_dev); 1400 1401 err_dev_register: 1402 1403 err_clk_init: 1404 1405 err_req_irq: 1406 1407 return ret; 1408 } 1409 1410 static int mtk_jpeg_remove(struct platform_device *pdev) 1411 { 1412 struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev); 1413 1414 pm_runtime_disable(&pdev->dev); 1415 video_unregister_device(jpeg->vdev); 1416 video_device_release(jpeg->vdev); 1417 v4l2_m2m_release(jpeg->m2m_dev); 1418 v4l2_device_unregister(&jpeg->v4l2_dev); 1419 1420 return 0; 1421 } 1422 1423 static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev) 1424 { 1425 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); 1426 1427 mtk_jpeg_clk_off(jpeg); 1428 1429 return 0; 1430 } 1431 1432 static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev) 1433 { 1434 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); 1435 1436 mtk_jpeg_clk_on(jpeg); 1437 1438 return 0; 1439 } 1440 1441 static __maybe_unused int mtk_jpeg_suspend(struct device *dev) 1442 { 1443 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); 1444 1445 v4l2_m2m_suspend(jpeg->m2m_dev); 1446 return pm_runtime_force_suspend(dev); 1447 } 1448 1449 static __maybe_unused int mtk_jpeg_resume(struct device *dev) 1450 { 1451 struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev); 1452 int ret; 1453 1454 ret = pm_runtime_force_resume(dev); 1455 if (ret < 0) 1456 return ret; 1457 1458 v4l2_m2m_resume(jpeg->m2m_dev); 1459 return ret; 1460 } 1461 1462 static const struct dev_pm_ops mtk_jpeg_pm_ops = { 1463 SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume) 1464 SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL) 1465 }; 1466 1467 static const struct mtk_jpeg_variant mt8173_jpeg_drvdata = { 1468 .clks = mt8173_jpeg_dec_clocks, 1469 .num_clks = ARRAY_SIZE(mt8173_jpeg_dec_clocks), 1470 .formats = mtk_jpeg_dec_formats, 1471 .num_formats = MTK_JPEG_DEC_NUM_FORMATS, 1472 .qops = &mtk_jpeg_dec_qops, 1473 .irq_handler = mtk_jpeg_dec_irq, 1474 .hw_reset = mtk_jpeg_dec_reset, 1475 .m2m_ops = &mtk_jpeg_dec_m2m_ops, 1476 .dev_name = "mtk-jpeg-dec", 1477 .ioctl_ops = &mtk_jpeg_dec_ioctl_ops, 1478 .out_q_default_fourcc = V4L2_PIX_FMT_JPEG, 1479 .cap_q_default_fourcc = V4L2_PIX_FMT_YUV420M, 1480 }; 1481 1482 static const struct mtk_jpeg_variant mtk_jpeg_drvdata = { 1483 .clks = mtk_jpeg_clocks, 1484 .num_clks = ARRAY_SIZE(mtk_jpeg_clocks), 1485 .formats = mtk_jpeg_enc_formats, 1486 .num_formats = MTK_JPEG_ENC_NUM_FORMATS, 1487 .qops = &mtk_jpeg_enc_qops, 1488 .irq_handler = mtk_jpeg_enc_irq, 1489 .hw_reset = mtk_jpeg_enc_reset, 1490 .m2m_ops = &mtk_jpeg_enc_m2m_ops, 1491 .dev_name = "mtk-jpeg-enc", 1492 .ioctl_ops = &mtk_jpeg_enc_ioctl_ops, 1493 .out_q_default_fourcc = V4L2_PIX_FMT_YUYV, 1494 .cap_q_default_fourcc = V4L2_PIX_FMT_JPEG, 1495 }; 1496 1497 static const struct of_device_id mtk_jpeg_match[] = { 1498 { 1499 .compatible = "mediatek,mt8173-jpgdec", 1500 .data = &mt8173_jpeg_drvdata, 1501 }, 1502 { 1503 .compatible = "mediatek,mt2701-jpgdec", 1504 .data = &mt8173_jpeg_drvdata, 1505 }, 1506 { 1507 .compatible = "mediatek,mtk-jpgenc", 1508 .data = &mtk_jpeg_drvdata, 1509 }, 1510 {}, 1511 }; 1512 1513 MODULE_DEVICE_TABLE(of, mtk_jpeg_match); 1514 1515 static struct platform_driver mtk_jpeg_driver = { 1516 .probe = mtk_jpeg_probe, 1517 .remove = mtk_jpeg_remove, 1518 .driver = { 1519 .name = MTK_JPEG_NAME, 1520 .of_match_table = mtk_jpeg_match, 1521 .pm = &mtk_jpeg_pm_ops, 1522 }, 1523 }; 1524 1525 module_platform_driver(mtk_jpeg_driver); 1526 1527 MODULE_DESCRIPTION("MediaTek JPEG codec driver"); 1528 MODULE_LICENSE("GPL v2"); 1529