root/drivers/media/platform/mtk-mdp/mtk_mdp_m2m.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. mtk_mdp_find_fmt
  2. mtk_mdp_find_fmt_by_index
  3. mtk_mdp_bound_align_image
  4. mtk_mdp_try_fmt_mplane
  5. mtk_mdp_ctx_get_frame
  6. mtk_mdp_check_crop_change
  7. mtk_mdp_try_crop
  8. fh_to_ctx
  9. ctrl_to_ctx
  10. mtk_mdp_ctx_state_lock_set
  11. mtk_mdp_ctx_state_lock_clear
  12. mtk_mdp_ctx_state_is_set
  13. mtk_mdp_set_frame_size
  14. mtk_mdp_m2m_start_streaming
  15. mtk_mdp_m2m_buf_remove
  16. mtk_mdp_m2m_stop_streaming
  17. mtk_mdp_prepare_addr
  18. mtk_mdp_m2m_get_bufs
  19. mtk_mdp_process_done
  20. mtk_mdp_m2m_worker
  21. mtk_mdp_m2m_device_run
  22. mtk_mdp_m2m_queue_setup
  23. mtk_mdp_m2m_buf_prepare
  24. mtk_mdp_m2m_buf_queue
  25. mtk_mdp_m2m_querycap
  26. mtk_mdp_enum_fmt
  27. mtk_mdp_m2m_enum_fmt_vid_cap
  28. mtk_mdp_m2m_enum_fmt_vid_out
  29. mtk_mdp_m2m_g_fmt_mplane
  30. mtk_mdp_m2m_try_fmt_mplane
  31. mtk_mdp_m2m_s_fmt_mplane
  32. mtk_mdp_m2m_reqbufs
  33. mtk_mdp_m2m_streamon
  34. mtk_mdp_is_target_compose
  35. mtk_mdp_is_target_crop
  36. mtk_mdp_m2m_g_selection
  37. mtk_mdp_check_scaler_ratio
  38. mtk_mdp_m2m_s_selection
  39. mtk_mdp_m2m_queue_init
  40. mtk_mdp_s_ctrl
  41. mtk_mdp_ctrls_create
  42. mtk_mdp_set_default_params
  43. mtk_mdp_m2m_open
  44. mtk_mdp_m2m_release
  45. mtk_mdp_register_m2m_device
  46. mtk_mdp_unregister_m2m_device

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  * Copyright (c) 2015-2016 MediaTek Inc.
   4  * Author: Houlong Wei <houlong.wei@mediatek.com>
   5  *         Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
   6  */
   7 
   8 #include <linux/device.h>
   9 #include <linux/errno.h>
  10 #include <linux/kernel.h>
  11 #include <linux/pm_runtime.h>
  12 #include <linux/slab.h>
  13 #include <linux/workqueue.h>
  14 #include <media/v4l2-event.h>
  15 #include <media/v4l2-ioctl.h>
  16 
  17 #include "mtk_mdp_core.h"
  18 #include "mtk_mdp_m2m.h"
  19 #include "mtk_mdp_regs.h"
  20 #include "mtk_vpu.h"
  21 
  22 
  23 /**
  24  *  struct mtk_mdp_pix_limit - image pixel size limits
  25  *  @org_w: source pixel width
  26  *  @org_h: source pixel height
  27  *  @target_rot_dis_w: pixel dst scaled width with the rotator is off
  28  *  @target_rot_dis_h: pixel dst scaled height with the rotator is off
  29  *  @target_rot_en_w: pixel dst scaled width with the rotator is on
  30  *  @target_rot_en_h: pixel dst scaled height with the rotator is on
  31  */
  32 struct mtk_mdp_pix_limit {
  33         u16 org_w;
  34         u16 org_h;
  35         u16 target_rot_dis_w;
  36         u16 target_rot_dis_h;
  37         u16 target_rot_en_w;
  38         u16 target_rot_en_h;
  39 };
  40 
  41 static struct mtk_mdp_pix_align mtk_mdp_size_align = {
  42         .org_w                  = 16,
  43         .org_h                  = 16,
  44         .target_w               = 2,
  45         .target_h               = 2,
  46 };
  47 
  48 static const struct mtk_mdp_fmt mtk_mdp_formats[] = {
  49         {
  50                 .pixelformat    = V4L2_PIX_FMT_MT21C,
  51                 .depth          = { 8, 4 },
  52                 .row_depth      = { 8, 8 },
  53                 .num_planes     = 2,
  54                 .num_comp       = 2,
  55                 .align          = &mtk_mdp_size_align,
  56                 .flags          = MTK_MDP_FMT_FLAG_OUTPUT,
  57         }, {
  58                 .pixelformat    = V4L2_PIX_FMT_NV12M,
  59                 .depth          = { 8, 4 },
  60                 .row_depth      = { 8, 8 },
  61                 .num_planes     = 2,
  62                 .num_comp       = 2,
  63                 .flags          = MTK_MDP_FMT_FLAG_OUTPUT |
  64                                   MTK_MDP_FMT_FLAG_CAPTURE,
  65         }, {
  66                 .pixelformat    = V4L2_PIX_FMT_YUV420M,
  67                 .depth          = { 8, 2, 2 },
  68                 .row_depth      = { 8, 4, 4 },
  69                 .num_planes     = 3,
  70                 .num_comp       = 3,
  71                 .flags          = MTK_MDP_FMT_FLAG_OUTPUT |
  72                                   MTK_MDP_FMT_FLAG_CAPTURE,
  73         }, {
  74                 .pixelformat    = V4L2_PIX_FMT_YVU420,
  75                 .depth          = { 12 },
  76                 .row_depth      = { 8 },
  77                 .num_planes     = 1,
  78                 .num_comp       = 3,
  79                 .flags          = MTK_MDP_FMT_FLAG_OUTPUT |
  80                                   MTK_MDP_FMT_FLAG_CAPTURE,
  81         }
  82 };
  83 
  84 static struct mtk_mdp_pix_limit mtk_mdp_size_max = {
  85         .target_rot_dis_w       = 4096,
  86         .target_rot_dis_h       = 4096,
  87         .target_rot_en_w        = 4096,
  88         .target_rot_en_h        = 4096,
  89 };
  90 
  91 static struct mtk_mdp_pix_limit mtk_mdp_size_min = {
  92         .org_w                  = 16,
  93         .org_h                  = 16,
  94         .target_rot_dis_w       = 16,
  95         .target_rot_dis_h       = 16,
  96         .target_rot_en_w        = 16,
  97         .target_rot_en_h        = 16,
  98 };
  99 
 100 /* align size for normal raster scan pixel format */
 101 static struct mtk_mdp_pix_align mtk_mdp_rs_align = {
 102         .org_w                  = 2,
 103         .org_h                  = 2,
 104         .target_w               = 2,
 105         .target_h               = 2,
 106 };
 107 
 108 static struct mtk_mdp_variant mtk_mdp_default_variant = {
 109         .pix_max                = &mtk_mdp_size_max,
 110         .pix_min                = &mtk_mdp_size_min,
 111         .pix_align              = &mtk_mdp_rs_align,
 112         .h_scale_up_max         = 32,
 113         .v_scale_up_max         = 32,
 114         .h_scale_down_max       = 32,
 115         .v_scale_down_max       = 128,
 116 };
 117 
 118 static const struct mtk_mdp_fmt *mtk_mdp_find_fmt(u32 pixelformat, u32 type)
 119 {
 120         u32 i, flag;
 121 
 122         flag = V4L2_TYPE_IS_OUTPUT(type) ? MTK_MDP_FMT_FLAG_OUTPUT :
 123                                            MTK_MDP_FMT_FLAG_CAPTURE;
 124 
 125         for (i = 0; i < ARRAY_SIZE(mtk_mdp_formats); ++i) {
 126                 if (!(mtk_mdp_formats[i].flags & flag))
 127                         continue;
 128                 if (mtk_mdp_formats[i].pixelformat == pixelformat)
 129                         return &mtk_mdp_formats[i];
 130         }
 131         return NULL;
 132 }
 133 
 134 static const struct mtk_mdp_fmt *mtk_mdp_find_fmt_by_index(u32 index, u32 type)
 135 {
 136         u32 i, flag, num = 0;
 137 
 138         flag = V4L2_TYPE_IS_OUTPUT(type) ? MTK_MDP_FMT_FLAG_OUTPUT :
 139                                            MTK_MDP_FMT_FLAG_CAPTURE;
 140 
 141         for (i = 0; i < ARRAY_SIZE(mtk_mdp_formats); ++i) {
 142                 if (!(mtk_mdp_formats[i].flags & flag))
 143                         continue;
 144                 if (index == num)
 145                         return &mtk_mdp_formats[i];
 146                 num++;
 147         }
 148         return NULL;
 149 }
 150 
 151 static void mtk_mdp_bound_align_image(u32 *w, unsigned int wmin,
 152                                       unsigned int wmax, unsigned int align_w,
 153                                       u32 *h, unsigned int hmin,
 154                                       unsigned int hmax, unsigned int align_h)
 155 {
 156         int org_w, org_h, step_w, step_h;
 157         int walign, halign;
 158 
 159         org_w = *w;
 160         org_h = *h;
 161         walign = ffs(align_w) - 1;
 162         halign = ffs(align_h) - 1;
 163         v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
 164 
 165         step_w = 1 << walign;
 166         step_h = 1 << halign;
 167         if (*w < org_w && (*w + step_w) <= wmax)
 168                 *w += step_w;
 169         if (*h < org_h && (*h + step_h) <= hmax)
 170                 *h += step_h;
 171 }
 172 
 173 static const struct mtk_mdp_fmt *mtk_mdp_try_fmt_mplane(struct mtk_mdp_ctx *ctx,
 174                                                         struct v4l2_format *f)
 175 {
 176         struct mtk_mdp_dev *mdp = ctx->mdp_dev;
 177         struct mtk_mdp_variant *variant = mdp->variant;
 178         struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
 179         const struct mtk_mdp_fmt *fmt;
 180         u32 max_w, max_h, align_w, align_h;
 181         u32 min_w, min_h, org_w, org_h;
 182         int i;
 183 
 184         fmt = mtk_mdp_find_fmt(pix_mp->pixelformat, f->type);
 185         if (!fmt)
 186                 fmt = mtk_mdp_find_fmt_by_index(0, f->type);
 187         if (!fmt) {
 188                 dev_dbg(&ctx->mdp_dev->pdev->dev,
 189                         "pixelformat format 0x%X invalid\n",
 190                         pix_mp->pixelformat);
 191                 return NULL;
 192         }
 193 
 194         pix_mp->field = V4L2_FIELD_NONE;
 195         pix_mp->pixelformat = fmt->pixelformat;
 196         if (!V4L2_TYPE_IS_OUTPUT(f->type)) {
 197                 pix_mp->colorspace = ctx->colorspace;
 198                 pix_mp->xfer_func = ctx->xfer_func;
 199                 pix_mp->ycbcr_enc = ctx->ycbcr_enc;
 200                 pix_mp->quantization = ctx->quant;
 201         }
 202         memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
 203 
 204         max_w = variant->pix_max->target_rot_dis_w;
 205         max_h = variant->pix_max->target_rot_dis_h;
 206 
 207         if (fmt->align == NULL) {
 208                 /* use default alignment */
 209                 align_w = variant->pix_align->org_w;
 210                 align_h = variant->pix_align->org_h;
 211         } else {
 212                 align_w = fmt->align->org_w;
 213                 align_h = fmt->align->org_h;
 214         }
 215 
 216         if (V4L2_TYPE_IS_OUTPUT(f->type)) {
 217                 min_w = variant->pix_min->org_w;
 218                 min_h = variant->pix_min->org_h;
 219         } else {
 220                 min_w = variant->pix_min->target_rot_dis_w;
 221                 min_h = variant->pix_min->target_rot_dis_h;
 222         }
 223 
 224         mtk_mdp_dbg(2, "[%d] type:%d, wxh:%ux%u, align:%ux%u, max:%ux%u",
 225                     ctx->id, f->type, pix_mp->width, pix_mp->height,
 226                     align_w, align_h, max_w, max_h);
 227         /*
 228          * To check if image size is modified to adjust parameter against
 229          * hardware abilities
 230          */
 231         org_w = pix_mp->width;
 232         org_h = pix_mp->height;
 233 
 234         mtk_mdp_bound_align_image(&pix_mp->width, min_w, max_w, align_w,
 235                                   &pix_mp->height, min_h, max_h, align_h);
 236 
 237         if (org_w != pix_mp->width || org_h != pix_mp->height)
 238                 mtk_mdp_dbg(1, "[%d] size change:%ux%u to %ux%u", ctx->id,
 239                             org_w, org_h, pix_mp->width, pix_mp->height);
 240         pix_mp->num_planes = fmt->num_planes;
 241 
 242         for (i = 0; i < pix_mp->num_planes; ++i) {
 243                 int bpl = (pix_mp->width * fmt->row_depth[i]) / 8;
 244                 int sizeimage = (pix_mp->width * pix_mp->height *
 245                         fmt->depth[i]) / 8;
 246 
 247                 pix_mp->plane_fmt[i].bytesperline = bpl;
 248                 if (pix_mp->plane_fmt[i].sizeimage < sizeimage)
 249                         pix_mp->plane_fmt[i].sizeimage = sizeimage;
 250                 memset(pix_mp->plane_fmt[i].reserved, 0,
 251                        sizeof(pix_mp->plane_fmt[i].reserved));
 252                 mtk_mdp_dbg(2, "[%d] p%d, bpl:%d, sizeimage:%u (%u)", ctx->id,
 253                             i, bpl, pix_mp->plane_fmt[i].sizeimage, sizeimage);
 254         }
 255 
 256         return fmt;
 257 }
 258 
 259 static struct mtk_mdp_frame *mtk_mdp_ctx_get_frame(struct mtk_mdp_ctx *ctx,
 260                                             enum v4l2_buf_type type)
 261 {
 262         if (V4L2_TYPE_IS_OUTPUT(type))
 263                 return &ctx->s_frame;
 264         return &ctx->d_frame;
 265 }
 266 
 267 static void mtk_mdp_check_crop_change(u32 new_w, u32 new_h, u32 *w, u32 *h)
 268 {
 269         if (new_w != *w || new_h != *h) {
 270                 mtk_mdp_dbg(1, "size change:%dx%d to %dx%d",
 271                             *w, *h, new_w, new_h);
 272 
 273                 *w = new_w;
 274                 *h = new_h;
 275         }
 276 }
 277 
 278 static int mtk_mdp_try_crop(struct mtk_mdp_ctx *ctx, u32 type,
 279                             struct v4l2_rect *r)
 280 {
 281         struct mtk_mdp_frame *frame;
 282         struct mtk_mdp_dev *mdp = ctx->mdp_dev;
 283         struct mtk_mdp_variant *variant = mdp->variant;
 284         u32 align_w, align_h, new_w, new_h;
 285         u32 min_w, min_h, max_w, max_h;
 286 
 287         if (r->top < 0 || r->left < 0) {
 288                 dev_err(&ctx->mdp_dev->pdev->dev,
 289                         "doesn't support negative values for top & left\n");
 290                 return -EINVAL;
 291         }
 292 
 293         mtk_mdp_dbg(2, "[%d] type:%d, set wxh:%dx%d", ctx->id, type,
 294                     r->width, r->height);
 295 
 296         frame = mtk_mdp_ctx_get_frame(ctx, type);
 297         max_w = frame->width;
 298         max_h = frame->height;
 299         new_w = r->width;
 300         new_h = r->height;
 301 
 302         if (V4L2_TYPE_IS_OUTPUT(type)) {
 303                 align_w = 1;
 304                 align_h = 1;
 305                 min_w = 64;
 306                 min_h = 32;
 307         } else {
 308                 align_w = variant->pix_align->target_w;
 309                 align_h = variant->pix_align->target_h;
 310                 if (ctx->ctrls.rotate->val == 90 ||
 311                     ctx->ctrls.rotate->val == 270) {
 312                         max_w = frame->height;
 313                         max_h = frame->width;
 314                         min_w = variant->pix_min->target_rot_en_w;
 315                         min_h = variant->pix_min->target_rot_en_h;
 316                         new_w = r->height;
 317                         new_h = r->width;
 318                 } else {
 319                         min_w = variant->pix_min->target_rot_dis_w;
 320                         min_h = variant->pix_min->target_rot_dis_h;
 321                 }
 322         }
 323 
 324         mtk_mdp_dbg(2, "[%d] align:%dx%d, min:%dx%d, new:%dx%d", ctx->id,
 325                     align_w, align_h, min_w, min_h, new_w, new_h);
 326 
 327         mtk_mdp_bound_align_image(&new_w, min_w, max_w, align_w,
 328                                   &new_h, min_h, max_h, align_h);
 329 
 330         if (!V4L2_TYPE_IS_OUTPUT(type) &&
 331                 (ctx->ctrls.rotate->val == 90 ||
 332                 ctx->ctrls.rotate->val == 270))
 333                 mtk_mdp_check_crop_change(new_h, new_w,
 334                                           &r->width, &r->height);
 335         else
 336                 mtk_mdp_check_crop_change(new_w, new_h,
 337                                           &r->width, &r->height);
 338 
 339         /* adjust left/top if cropping rectangle is out of bounds */
 340         /* Need to add code to algin left value with 2's multiple */
 341         if (r->left + new_w > max_w)
 342                 r->left = max_w - new_w;
 343         if (r->top + new_h > max_h)
 344                 r->top = max_h - new_h;
 345 
 346         if (r->left & 1)
 347                 r->left -= 1;
 348 
 349         mtk_mdp_dbg(2, "[%d] crop l,t,w,h:%d,%d,%d,%d, max:%dx%d", ctx->id,
 350                     r->left, r->top, r->width,
 351                     r->height, max_w, max_h);
 352         return 0;
 353 }
 354 
 355 static inline struct mtk_mdp_ctx *fh_to_ctx(struct v4l2_fh *fh)
 356 {
 357         return container_of(fh, struct mtk_mdp_ctx, fh);
 358 }
 359 
 360 static inline struct mtk_mdp_ctx *ctrl_to_ctx(struct v4l2_ctrl *ctrl)
 361 {
 362         return container_of(ctrl->handler, struct mtk_mdp_ctx, ctrl_handler);
 363 }
 364 
 365 void mtk_mdp_ctx_state_lock_set(struct mtk_mdp_ctx *ctx, u32 state)
 366 {
 367         mutex_lock(&ctx->slock);
 368         ctx->state |= state;
 369         mutex_unlock(&ctx->slock);
 370 }
 371 
 372 static void mtk_mdp_ctx_state_lock_clear(struct mtk_mdp_ctx *ctx, u32 state)
 373 {
 374         mutex_lock(&ctx->slock);
 375         ctx->state &= ~state;
 376         mutex_unlock(&ctx->slock);
 377 }
 378 
 379 static bool mtk_mdp_ctx_state_is_set(struct mtk_mdp_ctx *ctx, u32 mask)
 380 {
 381         bool ret;
 382 
 383         mutex_lock(&ctx->slock);
 384         ret = (ctx->state & mask) == mask;
 385         mutex_unlock(&ctx->slock);
 386         return ret;
 387 }
 388 
 389 static void mtk_mdp_set_frame_size(struct mtk_mdp_frame *frame, int width,
 390                                    int height)
 391 {
 392         frame->width = width;
 393         frame->height = height;
 394         frame->crop.width = width;
 395         frame->crop.height = height;
 396         frame->crop.left = 0;
 397         frame->crop.top = 0;
 398 }
 399 
 400 static int mtk_mdp_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
 401 {
 402         struct mtk_mdp_ctx *ctx = q->drv_priv;
 403         int ret;
 404 
 405         ret = pm_runtime_get_sync(&ctx->mdp_dev->pdev->dev);
 406         if (ret < 0)
 407                 mtk_mdp_dbg(1, "[%d] pm_runtime_get_sync failed:%d",
 408                             ctx->id, ret);
 409 
 410         return 0;
 411 }
 412 
 413 static void *mtk_mdp_m2m_buf_remove(struct mtk_mdp_ctx *ctx,
 414                                     enum v4l2_buf_type type)
 415 {
 416         if (V4L2_TYPE_IS_OUTPUT(type))
 417                 return v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
 418         else
 419                 return v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
 420 }
 421 
 422 static void mtk_mdp_m2m_stop_streaming(struct vb2_queue *q)
 423 {
 424         struct mtk_mdp_ctx *ctx = q->drv_priv;
 425         struct vb2_buffer *vb;
 426 
 427         vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
 428         while (vb != NULL) {
 429                 v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_ERROR);
 430                 vb = mtk_mdp_m2m_buf_remove(ctx, q->type);
 431         }
 432 
 433         pm_runtime_put(&ctx->mdp_dev->pdev->dev);
 434 }
 435 
 436 /* The color format (num_planes) must be already configured. */
 437 static void mtk_mdp_prepare_addr(struct mtk_mdp_ctx *ctx,
 438                                  struct vb2_buffer *vb,
 439                                  struct mtk_mdp_frame *frame,
 440                                  struct mtk_mdp_addr *addr)
 441 {
 442         u32 pix_size, planes, i;
 443 
 444         pix_size = frame->width * frame->height;
 445         planes = min_t(u32, frame->fmt->num_planes, ARRAY_SIZE(addr->addr));
 446         for (i = 0; i < planes; i++)
 447                 addr->addr[i] = vb2_dma_contig_plane_dma_addr(vb, i);
 448 
 449         if (planes == 1) {
 450                 if (frame->fmt->pixelformat == V4L2_PIX_FMT_YVU420) {
 451                         addr->addr[1] = (dma_addr_t)(addr->addr[0] + pix_size);
 452                         addr->addr[2] = (dma_addr_t)(addr->addr[1] +
 453                                         (pix_size >> 2));
 454                 } else {
 455                         dev_err(&ctx->mdp_dev->pdev->dev,
 456                                 "Invalid pixelformat:0x%x\n",
 457                                 frame->fmt->pixelformat);
 458                 }
 459         }
 460         mtk_mdp_dbg(3, "[%d] planes:%d, size:%d, addr:%p,%p,%p",
 461                     ctx->id, planes, pix_size, (void *)addr->addr[0],
 462                     (void *)addr->addr[1], (void *)addr->addr[2]);
 463 }
 464 
 465 static void mtk_mdp_m2m_get_bufs(struct mtk_mdp_ctx *ctx)
 466 {
 467         struct mtk_mdp_frame *s_frame, *d_frame;
 468         struct vb2_v4l2_buffer *src_vbuf, *dst_vbuf;
 469 
 470         s_frame = &ctx->s_frame;
 471         d_frame = &ctx->d_frame;
 472 
 473         src_vbuf = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
 474         mtk_mdp_prepare_addr(ctx, &src_vbuf->vb2_buf, s_frame, &s_frame->addr);
 475 
 476         dst_vbuf = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
 477         mtk_mdp_prepare_addr(ctx, &dst_vbuf->vb2_buf, d_frame, &d_frame->addr);
 478 
 479         dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
 480 }
 481 
 482 static void mtk_mdp_process_done(void *priv, int vb_state)
 483 {
 484         struct mtk_mdp_dev *mdp = priv;
 485         struct mtk_mdp_ctx *ctx;
 486         struct vb2_v4l2_buffer *src_vbuf, *dst_vbuf;
 487 
 488         ctx = v4l2_m2m_get_curr_priv(mdp->m2m_dev);
 489         if (!ctx)
 490                 return;
 491 
 492         src_vbuf = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
 493         dst_vbuf = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
 494 
 495         dst_vbuf->vb2_buf.timestamp = src_vbuf->vb2_buf.timestamp;
 496         dst_vbuf->timecode = src_vbuf->timecode;
 497         dst_vbuf->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 498         dst_vbuf->flags |= src_vbuf->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
 499 
 500         v4l2_m2m_buf_done(src_vbuf, vb_state);
 501         v4l2_m2m_buf_done(dst_vbuf, vb_state);
 502         v4l2_m2m_job_finish(ctx->mdp_dev->m2m_dev, ctx->m2m_ctx);
 503 }
 504 
 505 static void mtk_mdp_m2m_worker(struct work_struct *work)
 506 {
 507         struct mtk_mdp_ctx *ctx =
 508                                 container_of(work, struct mtk_mdp_ctx, work);
 509         struct mtk_mdp_dev *mdp = ctx->mdp_dev;
 510         enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
 511         int ret;
 512 
 513         if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_CTX_ERROR)) {
 514                 dev_err(&mdp->pdev->dev, "ctx is in error state");
 515                 goto worker_end;
 516         }
 517 
 518         mtk_mdp_m2m_get_bufs(ctx);
 519 
 520         mtk_mdp_hw_set_input_addr(ctx, &ctx->s_frame.addr);
 521         mtk_mdp_hw_set_output_addr(ctx, &ctx->d_frame.addr);
 522 
 523         mtk_mdp_hw_set_in_size(ctx);
 524         mtk_mdp_hw_set_in_image_format(ctx);
 525 
 526         mtk_mdp_hw_set_out_size(ctx);
 527         mtk_mdp_hw_set_out_image_format(ctx);
 528 
 529         mtk_mdp_hw_set_rotation(ctx);
 530         mtk_mdp_hw_set_global_alpha(ctx);
 531 
 532         ret = mtk_mdp_vpu_process(&ctx->vpu);
 533         if (ret) {
 534                 dev_err(&mdp->pdev->dev, "processing failed: %d", ret);
 535                 goto worker_end;
 536         }
 537 
 538         buf_state = VB2_BUF_STATE_DONE;
 539 
 540 worker_end:
 541         mtk_mdp_process_done(mdp, buf_state);
 542 }
 543 
 544 static void mtk_mdp_m2m_device_run(void *priv)
 545 {
 546         struct mtk_mdp_ctx *ctx = priv;
 547 
 548         queue_work(ctx->mdp_dev->job_wq, &ctx->work);
 549 }
 550 
 551 static int mtk_mdp_m2m_queue_setup(struct vb2_queue *vq,
 552                         unsigned int *num_buffers, unsigned int *num_planes,
 553                         unsigned int sizes[], struct device *alloc_devs[])
 554 {
 555         struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vq);
 556         struct mtk_mdp_frame *frame;
 557         int i;
 558 
 559         frame = mtk_mdp_ctx_get_frame(ctx, vq->type);
 560         *num_planes = frame->fmt->num_planes;
 561         for (i = 0; i < frame->fmt->num_planes; i++)
 562                 sizes[i] = frame->payload[i];
 563         mtk_mdp_dbg(2, "[%d] type:%d, planes:%d, buffers:%d, size:%u,%u",
 564                     ctx->id, vq->type, *num_planes, *num_buffers,
 565                     sizes[0], sizes[1]);
 566         return 0;
 567 }
 568 
 569 static int mtk_mdp_m2m_buf_prepare(struct vb2_buffer *vb)
 570 {
 571         struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
 572         struct mtk_mdp_frame *frame;
 573         int i;
 574 
 575         frame = mtk_mdp_ctx_get_frame(ctx, vb->vb2_queue->type);
 576 
 577         if (!V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
 578                 for (i = 0; i < frame->fmt->num_planes; i++)
 579                         vb2_set_plane_payload(vb, i, frame->payload[i]);
 580         }
 581 
 582         return 0;
 583 }
 584 
 585 static void mtk_mdp_m2m_buf_queue(struct vb2_buffer *vb)
 586 {
 587         struct mtk_mdp_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
 588 
 589         v4l2_m2m_buf_queue(ctx->m2m_ctx, to_vb2_v4l2_buffer(vb));
 590 }
 591 
 592 static const struct vb2_ops mtk_mdp_m2m_qops = {
 593         .queue_setup     = mtk_mdp_m2m_queue_setup,
 594         .buf_prepare     = mtk_mdp_m2m_buf_prepare,
 595         .buf_queue       = mtk_mdp_m2m_buf_queue,
 596         .stop_streaming  = mtk_mdp_m2m_stop_streaming,
 597         .start_streaming = mtk_mdp_m2m_start_streaming,
 598         .wait_prepare    = vb2_ops_wait_prepare,
 599         .wait_finish     = vb2_ops_wait_finish,
 600 };
 601 
 602 static int mtk_mdp_m2m_querycap(struct file *file, void *fh,
 603                                 struct v4l2_capability *cap)
 604 {
 605         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 606         struct mtk_mdp_dev *mdp = ctx->mdp_dev;
 607 
 608         strscpy(cap->driver, MTK_MDP_MODULE_NAME, sizeof(cap->driver));
 609         strscpy(cap->card, mdp->pdev->name, sizeof(cap->card));
 610         strscpy(cap->bus_info, "platform:mt8173", sizeof(cap->bus_info));
 611 
 612         return 0;
 613 }
 614 
 615 static int mtk_mdp_enum_fmt(struct v4l2_fmtdesc *f, u32 type)
 616 {
 617         const struct mtk_mdp_fmt *fmt;
 618 
 619         fmt = mtk_mdp_find_fmt_by_index(f->index, type);
 620         if (!fmt)
 621                 return -EINVAL;
 622 
 623         f->pixelformat = fmt->pixelformat;
 624 
 625         return 0;
 626 }
 627 
 628 static int mtk_mdp_m2m_enum_fmt_vid_cap(struct file *file, void *priv,
 629                                         struct v4l2_fmtdesc *f)
 630 {
 631         return mtk_mdp_enum_fmt(f, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
 632 }
 633 
 634 static int mtk_mdp_m2m_enum_fmt_vid_out(struct file *file, void *priv,
 635                                         struct v4l2_fmtdesc *f)
 636 {
 637         return mtk_mdp_enum_fmt(f, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
 638 }
 639 
 640 static int mtk_mdp_m2m_g_fmt_mplane(struct file *file, void *fh,
 641                                     struct v4l2_format *f)
 642 {
 643         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 644         struct mtk_mdp_frame *frame;
 645         struct v4l2_pix_format_mplane *pix_mp;
 646         int i;
 647 
 648         mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
 649 
 650         frame = mtk_mdp_ctx_get_frame(ctx, f->type);
 651         pix_mp = &f->fmt.pix_mp;
 652 
 653         pix_mp->width = frame->width;
 654         pix_mp->height = frame->height;
 655         pix_mp->field = V4L2_FIELD_NONE;
 656         pix_mp->pixelformat = frame->fmt->pixelformat;
 657         pix_mp->num_planes = frame->fmt->num_planes;
 658         pix_mp->colorspace = ctx->colorspace;
 659         pix_mp->xfer_func = ctx->xfer_func;
 660         pix_mp->ycbcr_enc = ctx->ycbcr_enc;
 661         pix_mp->quantization = ctx->quant;
 662         mtk_mdp_dbg(2, "[%d] wxh:%dx%d", ctx->id,
 663                     pix_mp->width, pix_mp->height);
 664 
 665         for (i = 0; i < pix_mp->num_planes; ++i) {
 666                 pix_mp->plane_fmt[i].bytesperline = (frame->width *
 667                         frame->fmt->row_depth[i]) / 8;
 668                 pix_mp->plane_fmt[i].sizeimage = (frame->width *
 669                         frame->height * frame->fmt->depth[i]) / 8;
 670 
 671                 mtk_mdp_dbg(2, "[%d] p%d, bpl:%d, sizeimage:%d", ctx->id, i,
 672                             pix_mp->plane_fmt[i].bytesperline,
 673                             pix_mp->plane_fmt[i].sizeimage);
 674         }
 675 
 676         return 0;
 677 }
 678 
 679 static int mtk_mdp_m2m_try_fmt_mplane(struct file *file, void *fh,
 680                                       struct v4l2_format *f)
 681 {
 682         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 683 
 684         if (!mtk_mdp_try_fmt_mplane(ctx, f))
 685                 return -EINVAL;
 686         return 0;
 687 }
 688 
 689 static int mtk_mdp_m2m_s_fmt_mplane(struct file *file, void *fh,
 690                                     struct v4l2_format *f)
 691 {
 692         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 693         struct vb2_queue *vq;
 694         struct mtk_mdp_frame *frame;
 695         struct v4l2_pix_format_mplane *pix_mp;
 696         const struct mtk_mdp_fmt *fmt;
 697         int i;
 698 
 699         mtk_mdp_dbg(2, "[%d] type:%d", ctx->id, f->type);
 700 
 701         frame = mtk_mdp_ctx_get_frame(ctx, f->type);
 702         fmt = mtk_mdp_try_fmt_mplane(ctx, f);
 703         if (!fmt) {
 704                 mtk_mdp_err("[%d] try_fmt failed, type:%d", ctx->id, f->type);
 705                 return -EINVAL;
 706         }
 707         frame->fmt = fmt;
 708 
 709         vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
 710         if (vb2_is_streaming(vq)) {
 711                 dev_info(&ctx->mdp_dev->pdev->dev, "queue %d busy", f->type);
 712                 return -EBUSY;
 713         }
 714 
 715         pix_mp = &f->fmt.pix_mp;
 716         for (i = 0; i < frame->fmt->num_planes; i++) {
 717                 frame->payload[i] = pix_mp->plane_fmt[i].sizeimage;
 718                 frame->pitch[i] = pix_mp->plane_fmt[i].bytesperline;
 719         }
 720 
 721         mtk_mdp_set_frame_size(frame, pix_mp->width, pix_mp->height);
 722         if (V4L2_TYPE_IS_OUTPUT(f->type)) {
 723                 ctx->colorspace = pix_mp->colorspace;
 724                 ctx->xfer_func = pix_mp->xfer_func;
 725                 ctx->ycbcr_enc = pix_mp->ycbcr_enc;
 726                 ctx->quant = pix_mp->quantization;
 727         }
 728 
 729         if (V4L2_TYPE_IS_OUTPUT(f->type))
 730                 mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_SRC_FMT);
 731         else
 732                 mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_DST_FMT);
 733 
 734         mtk_mdp_dbg(2, "[%d] type:%d, frame:%dx%d", ctx->id, f->type,
 735                     frame->width, frame->height);
 736 
 737         return 0;
 738 }
 739 
 740 static int mtk_mdp_m2m_reqbufs(struct file *file, void *fh,
 741                                struct v4l2_requestbuffers *reqbufs)
 742 {
 743         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 744 
 745         if (reqbufs->count == 0) {
 746                 if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 747                         mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_SRC_FMT);
 748                 else
 749                         mtk_mdp_ctx_state_lock_clear(ctx, MTK_MDP_DST_FMT);
 750         }
 751 
 752         return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
 753 }
 754 
 755 static int mtk_mdp_m2m_streamon(struct file *file, void *fh,
 756                                 enum v4l2_buf_type type)
 757 {
 758         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 759         int ret;
 760 
 761         /* The source and target color format need to be set */
 762         if (V4L2_TYPE_IS_OUTPUT(type)) {
 763                 if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_SRC_FMT))
 764                         return -EINVAL;
 765         } else if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT)) {
 766                 return -EINVAL;
 767         }
 768 
 769         if (!mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_VPU_INIT)) {
 770                 ret = mtk_mdp_vpu_init(&ctx->vpu);
 771                 if (ret < 0) {
 772                         dev_err(&ctx->mdp_dev->pdev->dev,
 773                                 "vpu init failed %d\n",
 774                                 ret);
 775                         return -EINVAL;
 776                 }
 777                 mtk_mdp_ctx_state_lock_set(ctx, MTK_MDP_VPU_INIT);
 778         }
 779 
 780         return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
 781 }
 782 
 783 static inline bool mtk_mdp_is_target_compose(u32 target)
 784 {
 785         if (target == V4L2_SEL_TGT_COMPOSE_DEFAULT
 786             || target == V4L2_SEL_TGT_COMPOSE_BOUNDS
 787             || target == V4L2_SEL_TGT_COMPOSE)
 788                 return true;
 789         return false;
 790 }
 791 
 792 static inline bool mtk_mdp_is_target_crop(u32 target)
 793 {
 794         if (target == V4L2_SEL_TGT_CROP_DEFAULT
 795             || target == V4L2_SEL_TGT_CROP_BOUNDS
 796             || target == V4L2_SEL_TGT_CROP)
 797                 return true;
 798         return false;
 799 }
 800 
 801 static int mtk_mdp_m2m_g_selection(struct file *file, void *fh,
 802                                        struct v4l2_selection *s)
 803 {
 804         struct mtk_mdp_frame *frame;
 805         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 806         bool valid = false;
 807 
 808         if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 809                 if (mtk_mdp_is_target_compose(s->target))
 810                         valid = true;
 811         } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
 812                 if (mtk_mdp_is_target_crop(s->target))
 813                         valid = true;
 814         }
 815         if (!valid) {
 816                 mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
 817                             s->target);
 818                 return -EINVAL;
 819         }
 820 
 821         frame = mtk_mdp_ctx_get_frame(ctx, s->type);
 822 
 823         switch (s->target) {
 824         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
 825         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
 826         case V4L2_SEL_TGT_CROP_BOUNDS:
 827         case V4L2_SEL_TGT_CROP_DEFAULT:
 828                 s->r.left = 0;
 829                 s->r.top = 0;
 830                 s->r.width = frame->width;
 831                 s->r.height = frame->height;
 832                 return 0;
 833 
 834         case V4L2_SEL_TGT_COMPOSE:
 835         case V4L2_SEL_TGT_CROP:
 836                 s->r.left = frame->crop.left;
 837                 s->r.top = frame->crop.top;
 838                 s->r.width = frame->crop.width;
 839                 s->r.height = frame->crop.height;
 840                 return 0;
 841         }
 842 
 843         return -EINVAL;
 844 }
 845 
 846 static int mtk_mdp_check_scaler_ratio(struct mtk_mdp_variant *var, int src_w,
 847                                       int src_h, int dst_w, int dst_h, int rot)
 848 {
 849         int tmp_w, tmp_h;
 850 
 851         if (rot == 90 || rot == 270) {
 852                 tmp_w = dst_h;
 853                 tmp_h = dst_w;
 854         } else {
 855                 tmp_w = dst_w;
 856                 tmp_h = dst_h;
 857         }
 858 
 859         if ((src_w / tmp_w) > var->h_scale_down_max ||
 860             (src_h / tmp_h) > var->v_scale_down_max ||
 861             (tmp_w / src_w) > var->h_scale_up_max ||
 862             (tmp_h / src_h) > var->v_scale_up_max)
 863                 return -EINVAL;
 864 
 865         return 0;
 866 }
 867 
 868 static int mtk_mdp_m2m_s_selection(struct file *file, void *fh,
 869                                    struct v4l2_selection *s)
 870 {
 871         struct mtk_mdp_frame *frame;
 872         struct mtk_mdp_ctx *ctx = fh_to_ctx(fh);
 873         struct v4l2_rect new_r;
 874         struct mtk_mdp_variant *variant = ctx->mdp_dev->variant;
 875         int ret;
 876         bool valid = false;
 877 
 878         if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
 879                 if (s->target == V4L2_SEL_TGT_COMPOSE)
 880                         valid = true;
 881         } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
 882                 if (s->target == V4L2_SEL_TGT_CROP)
 883                         valid = true;
 884         }
 885         if (!valid) {
 886                 mtk_mdp_dbg(1, "[%d] invalid type:%d,%u", ctx->id, s->type,
 887                             s->target);
 888                 return -EINVAL;
 889         }
 890 
 891         new_r = s->r;
 892         ret = mtk_mdp_try_crop(ctx, s->type, &new_r);
 893         if (ret)
 894                 return ret;
 895 
 896         if (mtk_mdp_is_target_crop(s->target))
 897                 frame = &ctx->s_frame;
 898         else
 899                 frame = &ctx->d_frame;
 900 
 901         /* Check to see if scaling ratio is within supported range */
 902         if (mtk_mdp_ctx_state_is_set(ctx, MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT)) {
 903                 if (V4L2_TYPE_IS_OUTPUT(s->type)) {
 904                         ret = mtk_mdp_check_scaler_ratio(variant, new_r.width,
 905                                 new_r.height, ctx->d_frame.crop.width,
 906                                 ctx->d_frame.crop.height,
 907                                 ctx->ctrls.rotate->val);
 908                 } else {
 909                         ret = mtk_mdp_check_scaler_ratio(variant,
 910                                 ctx->s_frame.crop.width,
 911                                 ctx->s_frame.crop.height, new_r.width,
 912                                 new_r.height, ctx->ctrls.rotate->val);
 913                 }
 914 
 915                 if (ret) {
 916                         dev_info(&ctx->mdp_dev->pdev->dev,
 917                                 "Out of scaler range");
 918                         return -EINVAL;
 919                 }
 920         }
 921 
 922         s->r = new_r;
 923         frame->crop = new_r;
 924 
 925         return 0;
 926 }
 927 
 928 static const struct v4l2_ioctl_ops mtk_mdp_m2m_ioctl_ops = {
 929         .vidioc_querycap                = mtk_mdp_m2m_querycap,
 930         .vidioc_enum_fmt_vid_cap        = mtk_mdp_m2m_enum_fmt_vid_cap,
 931         .vidioc_enum_fmt_vid_out        = mtk_mdp_m2m_enum_fmt_vid_out,
 932         .vidioc_g_fmt_vid_cap_mplane    = mtk_mdp_m2m_g_fmt_mplane,
 933         .vidioc_g_fmt_vid_out_mplane    = mtk_mdp_m2m_g_fmt_mplane,
 934         .vidioc_try_fmt_vid_cap_mplane  = mtk_mdp_m2m_try_fmt_mplane,
 935         .vidioc_try_fmt_vid_out_mplane  = mtk_mdp_m2m_try_fmt_mplane,
 936         .vidioc_s_fmt_vid_cap_mplane    = mtk_mdp_m2m_s_fmt_mplane,
 937         .vidioc_s_fmt_vid_out_mplane    = mtk_mdp_m2m_s_fmt_mplane,
 938         .vidioc_reqbufs                 = mtk_mdp_m2m_reqbufs,
 939         .vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
 940         .vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
 941         .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
 942         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
 943         .vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
 944         .vidioc_qbuf                    = v4l2_m2m_ioctl_qbuf,
 945         .vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
 946         .vidioc_streamon                = mtk_mdp_m2m_streamon,
 947         .vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
 948         .vidioc_g_selection             = mtk_mdp_m2m_g_selection,
 949         .vidioc_s_selection             = mtk_mdp_m2m_s_selection
 950 };
 951 
 952 static int mtk_mdp_m2m_queue_init(void *priv, struct vb2_queue *src_vq,
 953                                   struct vb2_queue *dst_vq)
 954 {
 955         struct mtk_mdp_ctx *ctx = priv;
 956         int ret;
 957 
 958         memset(src_vq, 0, sizeof(*src_vq));
 959         src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
 960         src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
 961         src_vq->drv_priv = ctx;
 962         src_vq->ops = &mtk_mdp_m2m_qops;
 963         src_vq->mem_ops = &vb2_dma_contig_memops;
 964         src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
 965         src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
 966         src_vq->dev = &ctx->mdp_dev->pdev->dev;
 967         src_vq->lock = &ctx->mdp_dev->lock;
 968 
 969         ret = vb2_queue_init(src_vq);
 970         if (ret)
 971                 return ret;
 972 
 973         memset(dst_vq, 0, sizeof(*dst_vq));
 974         dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 975         dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
 976         dst_vq->drv_priv = ctx;
 977         dst_vq->ops = &mtk_mdp_m2m_qops;
 978         dst_vq->mem_ops = &vb2_dma_contig_memops;
 979         dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
 980         dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
 981         dst_vq->dev = &ctx->mdp_dev->pdev->dev;
 982         dst_vq->lock = &ctx->mdp_dev->lock;
 983 
 984         return vb2_queue_init(dst_vq);
 985 }
 986 
 987 static int mtk_mdp_s_ctrl(struct v4l2_ctrl *ctrl)
 988 {
 989         struct mtk_mdp_ctx *ctx = ctrl_to_ctx(ctrl);
 990         struct mtk_mdp_dev *mdp = ctx->mdp_dev;
 991         struct mtk_mdp_variant *variant = mdp->variant;
 992         u32 state = MTK_MDP_DST_FMT | MTK_MDP_SRC_FMT;
 993         int ret = 0;
 994 
 995         if (ctrl->flags & V4L2_CTRL_FLAG_INACTIVE)
 996                 return 0;
 997 
 998         switch (ctrl->id) {
 999         case V4L2_CID_HFLIP:
1000                 ctx->hflip = ctrl->val;
1001                 break;
1002         case V4L2_CID_VFLIP:
1003                 ctx->vflip = ctrl->val;
1004                 break;
1005         case V4L2_CID_ROTATE:
1006                 if (mtk_mdp_ctx_state_is_set(ctx, state)) {
1007                         ret = mtk_mdp_check_scaler_ratio(variant,
1008                                         ctx->s_frame.crop.width,
1009                                         ctx->s_frame.crop.height,
1010                                         ctx->d_frame.crop.width,
1011                                         ctx->d_frame.crop.height,
1012                                         ctx->ctrls.rotate->val);
1013 
1014                         if (ret)
1015                                 return -EINVAL;
1016                 }
1017 
1018                 ctx->rotation = ctrl->val;
1019                 break;
1020         case V4L2_CID_ALPHA_COMPONENT:
1021                 ctx->d_frame.alpha = ctrl->val;
1022                 break;
1023         }
1024 
1025         return 0;
1026 }
1027 
1028 static const struct v4l2_ctrl_ops mtk_mdp_ctrl_ops = {
1029         .s_ctrl = mtk_mdp_s_ctrl,
1030 };
1031 
1032 static int mtk_mdp_ctrls_create(struct mtk_mdp_ctx *ctx)
1033 {
1034         v4l2_ctrl_handler_init(&ctx->ctrl_handler, MTK_MDP_MAX_CTRL_NUM);
1035 
1036         ctx->ctrls.rotate = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1037                         &mtk_mdp_ctrl_ops, V4L2_CID_ROTATE, 0, 270, 90, 0);
1038         ctx->ctrls.hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1039                                              &mtk_mdp_ctrl_ops,
1040                                              V4L2_CID_HFLIP,
1041                                              0, 1, 1, 0);
1042         ctx->ctrls.vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1043                                              &mtk_mdp_ctrl_ops,
1044                                              V4L2_CID_VFLIP,
1045                                              0, 1, 1, 0);
1046         ctx->ctrls.global_alpha = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1047                                                     &mtk_mdp_ctrl_ops,
1048                                                     V4L2_CID_ALPHA_COMPONENT,
1049                                                     0, 255, 1, 0);
1050         ctx->ctrls_rdy = ctx->ctrl_handler.error == 0;
1051 
1052         if (ctx->ctrl_handler.error) {
1053                 int err = ctx->ctrl_handler.error;
1054 
1055                 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1056                 dev_err(&ctx->mdp_dev->pdev->dev,
1057                         "Failed to create control handlers\n");
1058                 return err;
1059         }
1060 
1061         return 0;
1062 }
1063 
1064 static void mtk_mdp_set_default_params(struct mtk_mdp_ctx *ctx)
1065 {
1066         struct mtk_mdp_dev *mdp = ctx->mdp_dev;
1067         struct mtk_mdp_frame *frame;
1068 
1069         frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1070         frame->fmt = mtk_mdp_find_fmt_by_index(0,
1071                                         V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
1072         frame->width = mdp->variant->pix_min->org_w;
1073         frame->height = mdp->variant->pix_min->org_h;
1074         frame->payload[0] = frame->width * frame->height;
1075         frame->payload[1] = frame->payload[0] / 2;
1076 
1077         frame = mtk_mdp_ctx_get_frame(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1078         frame->fmt = mtk_mdp_find_fmt_by_index(0,
1079                                         V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
1080         frame->width = mdp->variant->pix_min->target_rot_dis_w;
1081         frame->height = mdp->variant->pix_min->target_rot_dis_h;
1082         frame->payload[0] = frame->width * frame->height;
1083         frame->payload[1] = frame->payload[0] / 2;
1084 
1085 }
1086 
1087 static int mtk_mdp_m2m_open(struct file *file)
1088 {
1089         struct mtk_mdp_dev *mdp = video_drvdata(file);
1090         struct video_device *vfd = video_devdata(file);
1091         struct mtk_mdp_ctx *ctx = NULL;
1092         int ret;
1093 
1094         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1095         if (!ctx)
1096                 return -ENOMEM;
1097 
1098         if (mutex_lock_interruptible(&mdp->lock)) {
1099                 ret = -ERESTARTSYS;
1100                 goto err_lock;
1101         }
1102 
1103         mutex_init(&ctx->slock);
1104         ctx->id = mdp->id_counter++;
1105         v4l2_fh_init(&ctx->fh, vfd);
1106         file->private_data = &ctx->fh;
1107         ret = mtk_mdp_ctrls_create(ctx);
1108         if (ret)
1109                 goto error_ctrls;
1110 
1111         /* Use separate control handler per file handle */
1112         ctx->fh.ctrl_handler = &ctx->ctrl_handler;
1113         v4l2_fh_add(&ctx->fh);
1114         INIT_LIST_HEAD(&ctx->list);
1115 
1116         ctx->mdp_dev = mdp;
1117         mtk_mdp_set_default_params(ctx);
1118 
1119         INIT_WORK(&ctx->work, mtk_mdp_m2m_worker);
1120         ctx->m2m_ctx = v4l2_m2m_ctx_init(mdp->m2m_dev, ctx,
1121                                          mtk_mdp_m2m_queue_init);
1122         if (IS_ERR(ctx->m2m_ctx)) {
1123                 dev_err(&mdp->pdev->dev, "Failed to initialize m2m context");
1124                 ret = PTR_ERR(ctx->m2m_ctx);
1125                 goto error_m2m_ctx;
1126         }
1127         ctx->fh.m2m_ctx = ctx->m2m_ctx;
1128         if (mdp->ctx_num++ == 0) {
1129                 ret = vpu_load_firmware(mdp->vpu_dev);
1130                 if (ret < 0) {
1131                         dev_err(&mdp->pdev->dev,
1132                                 "vpu_load_firmware failed %d\n", ret);
1133                         goto err_load_vpu;
1134                 }
1135 
1136                 ret = mtk_mdp_vpu_register(mdp->pdev);
1137                 if (ret < 0) {
1138                         dev_err(&mdp->pdev->dev,
1139                                 "mdp_vpu register failed %d\n", ret);
1140                         goto err_load_vpu;
1141                 }
1142         }
1143 
1144         list_add(&ctx->list, &mdp->ctx_list);
1145         mutex_unlock(&mdp->lock);
1146 
1147         mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
1148 
1149         return 0;
1150 
1151 err_load_vpu:
1152         mdp->ctx_num--;
1153         v4l2_m2m_ctx_release(ctx->m2m_ctx);
1154 error_m2m_ctx:
1155         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1156 error_ctrls:
1157         v4l2_fh_del(&ctx->fh);
1158         v4l2_fh_exit(&ctx->fh);
1159         mutex_unlock(&mdp->lock);
1160 err_lock:
1161         kfree(ctx);
1162 
1163         return ret;
1164 }
1165 
1166 static int mtk_mdp_m2m_release(struct file *file)
1167 {
1168         struct mtk_mdp_ctx *ctx = fh_to_ctx(file->private_data);
1169         struct mtk_mdp_dev *mdp = ctx->mdp_dev;
1170 
1171         flush_workqueue(mdp->job_wq);
1172         mutex_lock(&mdp->lock);
1173         v4l2_m2m_ctx_release(ctx->m2m_ctx);
1174         v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1175         v4l2_fh_del(&ctx->fh);
1176         v4l2_fh_exit(&ctx->fh);
1177         mtk_mdp_vpu_deinit(&ctx->vpu);
1178         mdp->ctx_num--;
1179         list_del_init(&ctx->list);
1180 
1181         mtk_mdp_dbg(0, "%s [%d]", dev_name(&mdp->pdev->dev), ctx->id);
1182 
1183         mutex_unlock(&mdp->lock);
1184         kfree(ctx);
1185 
1186         return 0;
1187 }
1188 
1189 static const struct v4l2_file_operations mtk_mdp_m2m_fops = {
1190         .owner          = THIS_MODULE,
1191         .open           = mtk_mdp_m2m_open,
1192         .release        = mtk_mdp_m2m_release,
1193         .poll           = v4l2_m2m_fop_poll,
1194         .unlocked_ioctl = video_ioctl2,
1195         .mmap           = v4l2_m2m_fop_mmap,
1196 };
1197 
1198 static const struct v4l2_m2m_ops mtk_mdp_m2m_ops = {
1199         .device_run     = mtk_mdp_m2m_device_run,
1200 };
1201 
1202 int mtk_mdp_register_m2m_device(struct mtk_mdp_dev *mdp)
1203 {
1204         struct device *dev = &mdp->pdev->dev;
1205         int ret;
1206 
1207         mdp->variant = &mtk_mdp_default_variant;
1208         mdp->vdev = video_device_alloc();
1209         if (!mdp->vdev) {
1210                 dev_err(dev, "failed to allocate video device\n");
1211                 ret = -ENOMEM;
1212                 goto err_video_alloc;
1213         }
1214         mdp->vdev->device_caps = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
1215         mdp->vdev->fops = &mtk_mdp_m2m_fops;
1216         mdp->vdev->ioctl_ops = &mtk_mdp_m2m_ioctl_ops;
1217         mdp->vdev->release = video_device_release;
1218         mdp->vdev->lock = &mdp->lock;
1219         mdp->vdev->vfl_dir = VFL_DIR_M2M;
1220         mdp->vdev->v4l2_dev = &mdp->v4l2_dev;
1221         snprintf(mdp->vdev->name, sizeof(mdp->vdev->name), "%s:m2m",
1222                  MTK_MDP_MODULE_NAME);
1223         video_set_drvdata(mdp->vdev, mdp);
1224 
1225         mdp->m2m_dev = v4l2_m2m_init(&mtk_mdp_m2m_ops);
1226         if (IS_ERR(mdp->m2m_dev)) {
1227                 dev_err(dev, "failed to initialize v4l2-m2m device\n");
1228                 ret = PTR_ERR(mdp->m2m_dev);
1229                 goto err_m2m_init;
1230         }
1231 
1232         ret = video_register_device(mdp->vdev, VFL_TYPE_GRABBER, 2);
1233         if (ret) {
1234                 dev_err(dev, "failed to register video device\n");
1235                 goto err_vdev_register;
1236         }
1237 
1238         v4l2_info(&mdp->v4l2_dev, "driver registered as /dev/video%d",
1239                   mdp->vdev->num);
1240         return 0;
1241 
1242 err_vdev_register:
1243         v4l2_m2m_release(mdp->m2m_dev);
1244 err_m2m_init:
1245         video_device_release(mdp->vdev);
1246 err_video_alloc:
1247 
1248         return ret;
1249 }
1250 
1251 void mtk_mdp_unregister_m2m_device(struct mtk_mdp_dev *mdp)
1252 {
1253         video_unregister_device(mdp->vdev);
1254         v4l2_m2m_release(mdp->m2m_dev);
1255 }

/* [<][>][^][v][top][bottom][index][help] */