linux/drivers/media/platform/mtk-jpeg/mtk_jpeg_core.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2016 MediaTek Inc.
   3 * Author: Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
   4 *         Rick Chang <rick.chang@mediatek.com>
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful,
  11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13 * GNU General Public License for more details.
  14 */
  15
  16#include <linux/clk.h>
  17#include <linux/err.h>
  18#include <linux/interrupt.h>
  19#include <linux/io.h>
  20#include <linux/kernel.h>
  21#include <linux/module.h>
  22#include <linux/of_platform.h>
  23#include <linux/platform_device.h>
  24#include <linux/pm_runtime.h>
  25#include <linux/slab.h>
  26#include <linux/spinlock.h>
  27#include <media/v4l2-event.h>
  28#include <media/v4l2-mem2mem.h>
  29#include <media/v4l2-ioctl.h>
  30#include <media/videobuf2-core.h>
  31#include <media/videobuf2-dma-contig.h>
  32#include <soc/mediatek/smi.h>
  33
  34#include "mtk_jpeg_hw.h"
  35#include "mtk_jpeg_core.h"
  36#include "mtk_jpeg_parse.h"
  37
  38static struct mtk_jpeg_fmt mtk_jpeg_formats[] = {
  39        {
  40                .fourcc         = V4L2_PIX_FMT_JPEG,
  41                .colplanes      = 1,
  42                .flags          = MTK_JPEG_FMT_FLAG_DEC_OUTPUT,
  43        },
  44        {
  45                .fourcc         = V4L2_PIX_FMT_YUV420M,
  46                .h_sample       = {4, 2, 2},
  47                .v_sample       = {4, 2, 2},
  48                .colplanes      = 3,
  49                .h_align        = 5,
  50                .v_align        = 4,
  51                .flags          = MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
  52        },
  53        {
  54                .fourcc         = V4L2_PIX_FMT_YUV422M,
  55                .h_sample       = {4, 2, 2},
  56                .v_sample       = {4, 4, 4},
  57                .colplanes      = 3,
  58                .h_align        = 5,
  59                .v_align        = 3,
  60                .flags          = MTK_JPEG_FMT_FLAG_DEC_CAPTURE,
  61        },
  62};
  63
  64#define MTK_JPEG_NUM_FORMATS ARRAY_SIZE(mtk_jpeg_formats)
  65
  66enum {
  67        MTK_JPEG_BUF_FLAGS_INIT                 = 0,
  68        MTK_JPEG_BUF_FLAGS_LAST_FRAME           = 1,
  69};
  70
  71struct mtk_jpeg_src_buf {
  72        struct vb2_v4l2_buffer b;
  73        struct list_head list;
  74        int flags;
  75        struct mtk_jpeg_dec_param dec_param;
  76};
  77
  78static int debug;
  79module_param(debug, int, 0644);
  80
  81static inline struct mtk_jpeg_ctx *mtk_jpeg_fh_to_ctx(struct v4l2_fh *fh)
  82{
  83        return container_of(fh, struct mtk_jpeg_ctx, fh);
  84}
  85
  86static inline struct mtk_jpeg_src_buf *mtk_jpeg_vb2_to_srcbuf(
  87                                                        struct vb2_buffer *vb)
  88{
  89        return container_of(to_vb2_v4l2_buffer(vb), struct mtk_jpeg_src_buf, b);
  90}
  91
  92static int mtk_jpeg_querycap(struct file *file, void *priv,
  93                             struct v4l2_capability *cap)
  94{
  95        struct mtk_jpeg_dev *jpeg = video_drvdata(file);
  96
  97        strscpy(cap->driver, MTK_JPEG_NAME " decoder", sizeof(cap->driver));
  98        strscpy(cap->card, MTK_JPEG_NAME " decoder", sizeof(cap->card));
  99        snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
 100                 dev_name(jpeg->dev));
 101
 102        return 0;
 103}
 104
 105static int mtk_jpeg_enum_fmt(struct mtk_jpeg_fmt *mtk_jpeg_formats, int n,
 106                             struct v4l2_fmtdesc *f, u32 type)
 107{
 108        int i, num = 0;
 109
 110        for (i = 0; i < n; ++i) {
 111                if (mtk_jpeg_formats[i].flags & type) {
 112                        if (num == f->index)
 113                                break;
 114                        ++num;
 115                }
 116        }
 117
 118        if (i >= n)
 119                return -EINVAL;
 120
 121        f->pixelformat = mtk_jpeg_formats[i].fourcc;
 122
 123        return 0;
 124}
 125
 126static int mtk_jpeg_enum_fmt_vid_cap(struct file *file, void *priv,
 127                                     struct v4l2_fmtdesc *f)
 128{
 129        return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
 130                                 MTK_JPEG_FMT_FLAG_DEC_CAPTURE);
 131}
 132
 133static int mtk_jpeg_enum_fmt_vid_out(struct file *file, void *priv,
 134                                     struct v4l2_fmtdesc *f)
 135{
 136        return mtk_jpeg_enum_fmt(mtk_jpeg_formats, MTK_JPEG_NUM_FORMATS, f,
 137                                 MTK_JPEG_FMT_FLAG_DEC_OUTPUT);
 138}
 139
 140static struct mtk_jpeg_q_data *mtk_jpeg_get_q_data(struct mtk_jpeg_ctx *ctx,
 141                                                   enum v4l2_buf_type type)
 142{
 143        if (V4L2_TYPE_IS_OUTPUT(type))
 144                return &ctx->out_q;
 145        return &ctx->cap_q;
 146}
 147
 148static struct mtk_jpeg_fmt *mtk_jpeg_find_format(struct mtk_jpeg_ctx *ctx,
 149                                                 u32 pixelformat,
 150                                                 unsigned int fmt_type)
 151{
 152        unsigned int k, fmt_flag;
 153
 154        fmt_flag = (fmt_type == MTK_JPEG_FMT_TYPE_OUTPUT) ?
 155                   MTK_JPEG_FMT_FLAG_DEC_OUTPUT :
 156                   MTK_JPEG_FMT_FLAG_DEC_CAPTURE;
 157
 158        for (k = 0; k < MTK_JPEG_NUM_FORMATS; k++) {
 159                struct mtk_jpeg_fmt *fmt = &mtk_jpeg_formats[k];
 160
 161                if (fmt->fourcc == pixelformat && fmt->flags & fmt_flag)
 162                        return fmt;
 163        }
 164
 165        return NULL;
 166}
 167
 168static void mtk_jpeg_bound_align_image(u32 *w, unsigned int wmin,
 169                                       unsigned int wmax, unsigned int walign,
 170                                       u32 *h, unsigned int hmin,
 171                                       unsigned int hmax, unsigned int halign)
 172{
 173        int width, height, w_step, h_step;
 174
 175        width = *w;
 176        height = *h;
 177        w_step = 1 << walign;
 178        h_step = 1 << halign;
 179
 180        v4l_bound_align_image(w, wmin, wmax, walign, h, hmin, hmax, halign, 0);
 181        if (*w < width && (*w + w_step) <= wmax)
 182                *w += w_step;
 183        if (*h < height && (*h + h_step) <= hmax)
 184                *h += h_step;
 185}
 186
 187static void mtk_jpeg_adjust_fmt_mplane(struct mtk_jpeg_ctx *ctx,
 188                                       struct v4l2_format *f)
 189{
 190        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
 191        struct mtk_jpeg_q_data *q_data;
 192        int i;
 193
 194        q_data = mtk_jpeg_get_q_data(ctx, f->type);
 195
 196        pix_mp->width = q_data->w;
 197        pix_mp->height = q_data->h;
 198        pix_mp->pixelformat = q_data->fmt->fourcc;
 199        pix_mp->num_planes = q_data->fmt->colplanes;
 200
 201        for (i = 0; i < pix_mp->num_planes; i++) {
 202                pix_mp->plane_fmt[i].bytesperline = q_data->bytesperline[i];
 203                pix_mp->plane_fmt[i].sizeimage = q_data->sizeimage[i];
 204        }
 205}
 206
 207static int mtk_jpeg_try_fmt_mplane(struct v4l2_format *f,
 208                                   struct mtk_jpeg_fmt *fmt,
 209                                   struct mtk_jpeg_ctx *ctx, int q_type)
 210{
 211        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
 212        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 213        int i;
 214
 215        memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
 216        pix_mp->field = V4L2_FIELD_NONE;
 217
 218        if (ctx->state != MTK_JPEG_INIT) {
 219                mtk_jpeg_adjust_fmt_mplane(ctx, f);
 220                goto end;
 221        }
 222
 223        pix_mp->num_planes = fmt->colplanes;
 224        pix_mp->pixelformat = fmt->fourcc;
 225
 226        if (q_type == MTK_JPEG_FMT_TYPE_OUTPUT) {
 227                struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[0];
 228
 229                mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH,
 230                                           MTK_JPEG_MAX_WIDTH, 0,
 231                                           &pix_mp->height, MTK_JPEG_MIN_HEIGHT,
 232                                           MTK_JPEG_MAX_HEIGHT, 0);
 233
 234                memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
 235                pfmt->bytesperline = 0;
 236                /* Source size must be aligned to 128 */
 237                pfmt->sizeimage = mtk_jpeg_align(pfmt->sizeimage, 128);
 238                if (pfmt->sizeimage == 0)
 239                        pfmt->sizeimage = MTK_JPEG_DEFAULT_SIZEIMAGE;
 240                goto end;
 241        }
 242
 243        /* type is MTK_JPEG_FMT_TYPE_CAPTURE */
 244        mtk_jpeg_bound_align_image(&pix_mp->width, MTK_JPEG_MIN_WIDTH,
 245                                   MTK_JPEG_MAX_WIDTH, fmt->h_align,
 246                                   &pix_mp->height, MTK_JPEG_MIN_HEIGHT,
 247                                   MTK_JPEG_MAX_HEIGHT, fmt->v_align);
 248
 249        for (i = 0; i < fmt->colplanes; i++) {
 250                struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
 251                u32 stride = pix_mp->width * fmt->h_sample[i] / 4;
 252                u32 h = pix_mp->height * fmt->v_sample[i] / 4;
 253
 254                memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
 255                pfmt->bytesperline = stride;
 256                pfmt->sizeimage = stride * h;
 257        }
 258end:
 259        v4l2_dbg(2, debug, &jpeg->v4l2_dev, "wxh:%ux%u\n",
 260                 pix_mp->width, pix_mp->height);
 261        for (i = 0; i < pix_mp->num_planes; i++) {
 262                v4l2_dbg(2, debug, &jpeg->v4l2_dev,
 263                         "plane[%d] bpl=%u, size=%u\n",
 264                         i,
 265                         pix_mp->plane_fmt[i].bytesperline,
 266                         pix_mp->plane_fmt[i].sizeimage);
 267        }
 268        return 0;
 269}
 270
 271static int mtk_jpeg_g_fmt_vid_mplane(struct file *file, void *priv,
 272                                     struct v4l2_format *f)
 273{
 274        struct vb2_queue *vq;
 275        struct mtk_jpeg_q_data *q_data = NULL;
 276        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
 277        struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 278        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 279        int i;
 280
 281        vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
 282        if (!vq)
 283                return -EINVAL;
 284
 285        q_data = mtk_jpeg_get_q_data(ctx, f->type);
 286
 287        memset(pix_mp->reserved, 0, sizeof(pix_mp->reserved));
 288        pix_mp->width = q_data->w;
 289        pix_mp->height = q_data->h;
 290        pix_mp->field = V4L2_FIELD_NONE;
 291        pix_mp->pixelformat = q_data->fmt->fourcc;
 292        pix_mp->num_planes = q_data->fmt->colplanes;
 293        pix_mp->colorspace = ctx->colorspace;
 294        pix_mp->ycbcr_enc = ctx->ycbcr_enc;
 295        pix_mp->xfer_func = ctx->xfer_func;
 296        pix_mp->quantization = ctx->quantization;
 297
 298        v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) g_fmt:%c%c%c%c wxh:%ux%u\n",
 299                 f->type,
 300                 (pix_mp->pixelformat & 0xff),
 301                 (pix_mp->pixelformat >>  8 & 0xff),
 302                 (pix_mp->pixelformat >> 16 & 0xff),
 303                 (pix_mp->pixelformat >> 24 & 0xff),
 304                 pix_mp->width, pix_mp->height);
 305
 306        for (i = 0; i < pix_mp->num_planes; i++) {
 307                struct v4l2_plane_pix_format *pfmt = &pix_mp->plane_fmt[i];
 308
 309                pfmt->bytesperline = q_data->bytesperline[i];
 310                pfmt->sizeimage = q_data->sizeimage[i];
 311                memset(pfmt->reserved, 0, sizeof(pfmt->reserved));
 312
 313                v4l2_dbg(1, debug, &jpeg->v4l2_dev,
 314                         "plane[%d] bpl=%u, size=%u\n",
 315                         i,
 316                         pfmt->bytesperline,
 317                         pfmt->sizeimage);
 318        }
 319        return 0;
 320}
 321
 322static int mtk_jpeg_try_fmt_vid_cap_mplane(struct file *file, void *priv,
 323                                           struct v4l2_format *f)
 324{
 325        struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 326        struct mtk_jpeg_fmt *fmt;
 327
 328        fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
 329                                   MTK_JPEG_FMT_TYPE_CAPTURE);
 330        if (!fmt)
 331                fmt = ctx->cap_q.fmt;
 332
 333        v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
 334                 f->type,
 335                 (fmt->fourcc & 0xff),
 336                 (fmt->fourcc >>  8 & 0xff),
 337                 (fmt->fourcc >> 16 & 0xff),
 338                 (fmt->fourcc >> 24 & 0xff));
 339
 340        return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_CAPTURE);
 341}
 342
 343static int mtk_jpeg_try_fmt_vid_out_mplane(struct file *file, void *priv,
 344                                           struct v4l2_format *f)
 345{
 346        struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 347        struct mtk_jpeg_fmt *fmt;
 348
 349        fmt = mtk_jpeg_find_format(ctx, f->fmt.pix_mp.pixelformat,
 350                                   MTK_JPEG_FMT_TYPE_OUTPUT);
 351        if (!fmt)
 352                fmt = ctx->out_q.fmt;
 353
 354        v4l2_dbg(2, debug, &ctx->jpeg->v4l2_dev, "(%d) try_fmt:%c%c%c%c\n",
 355                 f->type,
 356                 (fmt->fourcc & 0xff),
 357                 (fmt->fourcc >>  8 & 0xff),
 358                 (fmt->fourcc >> 16 & 0xff),
 359                 (fmt->fourcc >> 24 & 0xff));
 360
 361        return mtk_jpeg_try_fmt_mplane(f, fmt, ctx, MTK_JPEG_FMT_TYPE_OUTPUT);
 362}
 363
 364static int mtk_jpeg_s_fmt_mplane(struct mtk_jpeg_ctx *ctx,
 365                                 struct v4l2_format *f)
 366{
 367        struct vb2_queue *vq;
 368        struct mtk_jpeg_q_data *q_data = NULL;
 369        struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
 370        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 371        unsigned int f_type;
 372        int i;
 373
 374        vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
 375        if (!vq)
 376                return -EINVAL;
 377
 378        q_data = mtk_jpeg_get_q_data(ctx, f->type);
 379
 380        if (vb2_is_busy(vq)) {
 381                v4l2_err(&jpeg->v4l2_dev, "queue busy\n");
 382                return -EBUSY;
 383        }
 384
 385        f_type = V4L2_TYPE_IS_OUTPUT(f->type) ?
 386                         MTK_JPEG_FMT_TYPE_OUTPUT : MTK_JPEG_FMT_TYPE_CAPTURE;
 387
 388        q_data->fmt = mtk_jpeg_find_format(ctx, pix_mp->pixelformat, f_type);
 389        q_data->w = pix_mp->width;
 390        q_data->h = pix_mp->height;
 391        ctx->colorspace = pix_mp->colorspace;
 392        ctx->ycbcr_enc = pix_mp->ycbcr_enc;
 393        ctx->xfer_func = pix_mp->xfer_func;
 394        ctx->quantization = pix_mp->quantization;
 395
 396        v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) s_fmt:%c%c%c%c wxh:%ux%u\n",
 397                 f->type,
 398                 (q_data->fmt->fourcc & 0xff),
 399                 (q_data->fmt->fourcc >>  8 & 0xff),
 400                 (q_data->fmt->fourcc >> 16 & 0xff),
 401                 (q_data->fmt->fourcc >> 24 & 0xff),
 402                 q_data->w, q_data->h);
 403
 404        for (i = 0; i < q_data->fmt->colplanes; i++) {
 405                q_data->bytesperline[i] = pix_mp->plane_fmt[i].bytesperline;
 406                q_data->sizeimage[i] = pix_mp->plane_fmt[i].sizeimage;
 407
 408                v4l2_dbg(1, debug, &jpeg->v4l2_dev,
 409                         "plane[%d] bpl=%u, size=%u\n",
 410                         i, q_data->bytesperline[i], q_data->sizeimage[i]);
 411        }
 412
 413        return 0;
 414}
 415
 416static int mtk_jpeg_s_fmt_vid_out_mplane(struct file *file, void *priv,
 417                                         struct v4l2_format *f)
 418{
 419        int ret;
 420
 421        ret = mtk_jpeg_try_fmt_vid_out_mplane(file, priv, f);
 422        if (ret)
 423                return ret;
 424
 425        return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
 426}
 427
 428static int mtk_jpeg_s_fmt_vid_cap_mplane(struct file *file, void *priv,
 429                                         struct v4l2_format *f)
 430{
 431        int ret;
 432
 433        ret = mtk_jpeg_try_fmt_vid_cap_mplane(file, priv, f);
 434        if (ret)
 435                return ret;
 436
 437        return mtk_jpeg_s_fmt_mplane(mtk_jpeg_fh_to_ctx(priv), f);
 438}
 439
 440static void mtk_jpeg_queue_src_chg_event(struct mtk_jpeg_ctx *ctx)
 441{
 442        static const struct v4l2_event ev_src_ch = {
 443                .type = V4L2_EVENT_SOURCE_CHANGE,
 444                .u.src_change.changes =
 445                V4L2_EVENT_SRC_CH_RESOLUTION,
 446        };
 447
 448        v4l2_event_queue_fh(&ctx->fh, &ev_src_ch);
 449}
 450
 451static int mtk_jpeg_subscribe_event(struct v4l2_fh *fh,
 452                                    const struct v4l2_event_subscription *sub)
 453{
 454        switch (sub->type) {
 455        case V4L2_EVENT_SOURCE_CHANGE:
 456                return v4l2_src_change_event_subscribe(fh, sub);
 457        default:
 458                return -EINVAL;
 459        }
 460}
 461
 462static int mtk_jpeg_g_selection(struct file *file, void *priv,
 463                                struct v4l2_selection *s)
 464{
 465        struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 466
 467        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 468                return -EINVAL;
 469
 470        switch (s->target) {
 471        case V4L2_SEL_TGT_COMPOSE:
 472        case V4L2_SEL_TGT_COMPOSE_DEFAULT:
 473                s->r.width = ctx->out_q.w;
 474                s->r.height = ctx->out_q.h;
 475                s->r.left = 0;
 476                s->r.top = 0;
 477                break;
 478        case V4L2_SEL_TGT_COMPOSE_BOUNDS:
 479        case V4L2_SEL_TGT_COMPOSE_PADDED:
 480                s->r.width = ctx->cap_q.w;
 481                s->r.height = ctx->cap_q.h;
 482                s->r.left = 0;
 483                s->r.top = 0;
 484                break;
 485        default:
 486                return -EINVAL;
 487        }
 488        return 0;
 489}
 490
 491static int mtk_jpeg_s_selection(struct file *file, void *priv,
 492                                struct v4l2_selection *s)
 493{
 494        struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 495
 496        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 497                return -EINVAL;
 498
 499        switch (s->target) {
 500        case V4L2_SEL_TGT_COMPOSE:
 501                s->r.left = 0;
 502                s->r.top = 0;
 503                s->r.width = ctx->out_q.w;
 504                s->r.height = ctx->out_q.h;
 505                break;
 506        default:
 507                return -EINVAL;
 508        }
 509        return 0;
 510}
 511
 512static int mtk_jpeg_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 513{
 514        struct v4l2_fh *fh = file->private_data;
 515        struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(priv);
 516        struct vb2_queue *vq;
 517        struct vb2_buffer *vb;
 518        struct mtk_jpeg_src_buf *jpeg_src_buf;
 519
 520        if (buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 521                goto end;
 522
 523        vq = v4l2_m2m_get_vq(fh->m2m_ctx, buf->type);
 524        if (buf->index >= vq->num_buffers) {
 525                dev_err(ctx->jpeg->dev, "buffer index out of range\n");
 526                return -EINVAL;
 527        }
 528
 529        vb = vq->bufs[buf->index];
 530        jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
 531        jpeg_src_buf->flags = (buf->m.planes[0].bytesused == 0) ?
 532                MTK_JPEG_BUF_FLAGS_LAST_FRAME : MTK_JPEG_BUF_FLAGS_INIT;
 533end:
 534        return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf);
 535}
 536
 537static const struct v4l2_ioctl_ops mtk_jpeg_ioctl_ops = {
 538        .vidioc_querycap                = mtk_jpeg_querycap,
 539        .vidioc_enum_fmt_vid_cap_mplane = mtk_jpeg_enum_fmt_vid_cap,
 540        .vidioc_enum_fmt_vid_out_mplane = mtk_jpeg_enum_fmt_vid_out,
 541        .vidioc_try_fmt_vid_cap_mplane  = mtk_jpeg_try_fmt_vid_cap_mplane,
 542        .vidioc_try_fmt_vid_out_mplane  = mtk_jpeg_try_fmt_vid_out_mplane,
 543        .vidioc_g_fmt_vid_cap_mplane    = mtk_jpeg_g_fmt_vid_mplane,
 544        .vidioc_g_fmt_vid_out_mplane    = mtk_jpeg_g_fmt_vid_mplane,
 545        .vidioc_s_fmt_vid_cap_mplane    = mtk_jpeg_s_fmt_vid_cap_mplane,
 546        .vidioc_s_fmt_vid_out_mplane    = mtk_jpeg_s_fmt_vid_out_mplane,
 547        .vidioc_qbuf                    = mtk_jpeg_qbuf,
 548        .vidioc_subscribe_event         = mtk_jpeg_subscribe_event,
 549        .vidioc_g_selection             = mtk_jpeg_g_selection,
 550        .vidioc_s_selection             = mtk_jpeg_s_selection,
 551
 552        .vidioc_create_bufs             = v4l2_m2m_ioctl_create_bufs,
 553        .vidioc_prepare_buf             = v4l2_m2m_ioctl_prepare_buf,
 554        .vidioc_reqbufs                 = v4l2_m2m_ioctl_reqbufs,
 555        .vidioc_querybuf                = v4l2_m2m_ioctl_querybuf,
 556        .vidioc_dqbuf                   = v4l2_m2m_ioctl_dqbuf,
 557        .vidioc_expbuf                  = v4l2_m2m_ioctl_expbuf,
 558        .vidioc_streamon                = v4l2_m2m_ioctl_streamon,
 559        .vidioc_streamoff               = v4l2_m2m_ioctl_streamoff,
 560
 561        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
 562};
 563
 564static int mtk_jpeg_queue_setup(struct vb2_queue *q,
 565                                unsigned int *num_buffers,
 566                                unsigned int *num_planes,
 567                                unsigned int sizes[],
 568                                struct device *alloc_ctxs[])
 569{
 570        struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
 571        struct mtk_jpeg_q_data *q_data = NULL;
 572        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 573        int i;
 574
 575        v4l2_dbg(1, debug, &jpeg->v4l2_dev, "(%d) buf_req count=%u\n",
 576                 q->type, *num_buffers);
 577
 578        q_data = mtk_jpeg_get_q_data(ctx, q->type);
 579        if (!q_data)
 580                return -EINVAL;
 581
 582        *num_planes = q_data->fmt->colplanes;
 583        for (i = 0; i < q_data->fmt->colplanes; i++) {
 584                sizes[i] = q_data->sizeimage[i];
 585                v4l2_dbg(1, debug, &jpeg->v4l2_dev, "sizeimage[%d]=%u\n",
 586                         i, sizes[i]);
 587        }
 588
 589        return 0;
 590}
 591
 592static int mtk_jpeg_buf_prepare(struct vb2_buffer *vb)
 593{
 594        struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
 595        struct mtk_jpeg_q_data *q_data = NULL;
 596        int i;
 597
 598        q_data = mtk_jpeg_get_q_data(ctx, vb->vb2_queue->type);
 599        if (!q_data)
 600                return -EINVAL;
 601
 602        for (i = 0; i < q_data->fmt->colplanes; i++)
 603                vb2_set_plane_payload(vb, i, q_data->sizeimage[i]);
 604
 605        return 0;
 606}
 607
 608static bool mtk_jpeg_check_resolution_change(struct mtk_jpeg_ctx *ctx,
 609                                             struct mtk_jpeg_dec_param *param)
 610{
 611        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 612        struct mtk_jpeg_q_data *q_data;
 613
 614        q_data = &ctx->out_q;
 615        if (q_data->w != param->pic_w || q_data->h != param->pic_h) {
 616                v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Picture size change\n");
 617                return true;
 618        }
 619
 620        q_data = &ctx->cap_q;
 621        if (q_data->fmt != mtk_jpeg_find_format(ctx, param->dst_fourcc,
 622                                                MTK_JPEG_FMT_TYPE_CAPTURE)) {
 623                v4l2_dbg(1, debug, &jpeg->v4l2_dev, "format change\n");
 624                return true;
 625        }
 626        return false;
 627}
 628
 629static void mtk_jpeg_set_queue_data(struct mtk_jpeg_ctx *ctx,
 630                                    struct mtk_jpeg_dec_param *param)
 631{
 632        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 633        struct mtk_jpeg_q_data *q_data;
 634        int i;
 635
 636        q_data = &ctx->out_q;
 637        q_data->w = param->pic_w;
 638        q_data->h = param->pic_h;
 639
 640        q_data = &ctx->cap_q;
 641        q_data->w = param->dec_w;
 642        q_data->h = param->dec_h;
 643        q_data->fmt = mtk_jpeg_find_format(ctx,
 644                                           param->dst_fourcc,
 645                                           MTK_JPEG_FMT_TYPE_CAPTURE);
 646
 647        for (i = 0; i < q_data->fmt->colplanes; i++) {
 648                q_data->bytesperline[i] = param->mem_stride[i];
 649                q_data->sizeimage[i] = param->comp_size[i];
 650        }
 651
 652        v4l2_dbg(1, debug, &jpeg->v4l2_dev,
 653                 "set_parse cap:%c%c%c%c pic(%u, %u), buf(%u, %u)\n",
 654                 (param->dst_fourcc & 0xff),
 655                 (param->dst_fourcc >>  8 & 0xff),
 656                 (param->dst_fourcc >> 16 & 0xff),
 657                 (param->dst_fourcc >> 24 & 0xff),
 658                 param->pic_w, param->pic_h,
 659                 param->dec_w, param->dec_h);
 660}
 661
 662static void mtk_jpeg_buf_queue(struct vb2_buffer *vb)
 663{
 664        struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
 665        struct mtk_jpeg_dec_param *param;
 666        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 667        struct mtk_jpeg_src_buf *jpeg_src_buf;
 668        bool header_valid;
 669
 670        v4l2_dbg(2, debug, &jpeg->v4l2_dev, "(%d) buf_q id=%d, vb=%p\n",
 671                 vb->vb2_queue->type, vb->index, vb);
 672
 673        if (vb->vb2_queue->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 674                goto end;
 675
 676        jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
 677        param = &jpeg_src_buf->dec_param;
 678        memset(param, 0, sizeof(*param));
 679
 680        if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
 681                v4l2_dbg(1, debug, &jpeg->v4l2_dev, "Got eos\n");
 682                goto end;
 683        }
 684        header_valid = mtk_jpeg_parse(param, (u8 *)vb2_plane_vaddr(vb, 0),
 685                                      vb2_get_plane_payload(vb, 0));
 686        if (!header_valid) {
 687                v4l2_err(&jpeg->v4l2_dev, "Header invalid.\n");
 688                vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
 689                return;
 690        }
 691
 692        if (ctx->state == MTK_JPEG_INIT) {
 693                struct vb2_queue *dst_vq = v4l2_m2m_get_vq(
 694                        ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
 695
 696                mtk_jpeg_queue_src_chg_event(ctx);
 697                mtk_jpeg_set_queue_data(ctx, param);
 698                ctx->state = vb2_is_streaming(dst_vq) ?
 699                                MTK_JPEG_SOURCE_CHANGE : MTK_JPEG_RUNNING;
 700        }
 701end:
 702        v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, to_vb2_v4l2_buffer(vb));
 703}
 704
 705static void *mtk_jpeg_buf_remove(struct mtk_jpeg_ctx *ctx,
 706                                 enum v4l2_buf_type type)
 707{
 708        if (V4L2_TYPE_IS_OUTPUT(type))
 709                return v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 710        else
 711                return v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 712}
 713
 714static int mtk_jpeg_start_streaming(struct vb2_queue *q, unsigned int count)
 715{
 716        struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
 717        struct vb2_buffer *vb;
 718        int ret = 0;
 719
 720        ret = pm_runtime_get_sync(ctx->jpeg->dev);
 721        if (ret < 0)
 722                goto err;
 723
 724        return 0;
 725err:
 726        while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
 727                v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_QUEUED);
 728        return ret;
 729}
 730
 731static void mtk_jpeg_stop_streaming(struct vb2_queue *q)
 732{
 733        struct mtk_jpeg_ctx *ctx = vb2_get_drv_priv(q);
 734        struct vb2_buffer *vb;
 735
 736        /*
 737         * STREAMOFF is an acknowledgment for source change event.
 738         * Before STREAMOFF, we still have to return the old resolution and
 739         * subsampling. Update capture queue when the stream is off.
 740         */
 741        if (ctx->state == MTK_JPEG_SOURCE_CHANGE &&
 742            !V4L2_TYPE_IS_OUTPUT(q->type)) {
 743                struct mtk_jpeg_src_buf *src_buf;
 744
 745                vb = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
 746                src_buf = mtk_jpeg_vb2_to_srcbuf(vb);
 747                mtk_jpeg_set_queue_data(ctx, &src_buf->dec_param);
 748                ctx->state = MTK_JPEG_RUNNING;
 749        } else if (V4L2_TYPE_IS_OUTPUT(q->type)) {
 750                ctx->state = MTK_JPEG_INIT;
 751        }
 752
 753        while ((vb = mtk_jpeg_buf_remove(ctx, q->type)))
 754                v4l2_m2m_buf_done(to_vb2_v4l2_buffer(vb), VB2_BUF_STATE_ERROR);
 755
 756        pm_runtime_put_sync(ctx->jpeg->dev);
 757}
 758
 759static const struct vb2_ops mtk_jpeg_qops = {
 760        .queue_setup        = mtk_jpeg_queue_setup,
 761        .buf_prepare        = mtk_jpeg_buf_prepare,
 762        .buf_queue          = mtk_jpeg_buf_queue,
 763        .wait_prepare       = vb2_ops_wait_prepare,
 764        .wait_finish        = vb2_ops_wait_finish,
 765        .start_streaming    = mtk_jpeg_start_streaming,
 766        .stop_streaming     = mtk_jpeg_stop_streaming,
 767};
 768
 769static void mtk_jpeg_set_dec_src(struct mtk_jpeg_ctx *ctx,
 770                                 struct vb2_buffer *src_buf,
 771                                 struct mtk_jpeg_bs *bs)
 772{
 773        bs->str_addr = vb2_dma_contig_plane_dma_addr(src_buf, 0);
 774        bs->end_addr = bs->str_addr +
 775                         mtk_jpeg_align(vb2_get_plane_payload(src_buf, 0), 16);
 776        bs->size = mtk_jpeg_align(vb2_plane_size(src_buf, 0), 128);
 777}
 778
 779static int mtk_jpeg_set_dec_dst(struct mtk_jpeg_ctx *ctx,
 780                                struct mtk_jpeg_dec_param *param,
 781                                struct vb2_buffer *dst_buf,
 782                                struct mtk_jpeg_fb *fb)
 783{
 784        int i;
 785
 786        if (param->comp_num != dst_buf->num_planes) {
 787                dev_err(ctx->jpeg->dev, "plane number mismatch (%u != %u)\n",
 788                        param->comp_num, dst_buf->num_planes);
 789                return -EINVAL;
 790        }
 791
 792        for (i = 0; i < dst_buf->num_planes; i++) {
 793                if (vb2_plane_size(dst_buf, i) < param->comp_size[i]) {
 794                        dev_err(ctx->jpeg->dev,
 795                                "buffer size is underflow (%lu < %u)\n",
 796                                vb2_plane_size(dst_buf, 0),
 797                                param->comp_size[i]);
 798                        return -EINVAL;
 799                }
 800                fb->plane_addr[i] = vb2_dma_contig_plane_dma_addr(dst_buf, i);
 801        }
 802
 803        return 0;
 804}
 805
 806static void mtk_jpeg_device_run(void *priv)
 807{
 808        struct mtk_jpeg_ctx *ctx = priv;
 809        struct mtk_jpeg_dev *jpeg = ctx->jpeg;
 810        struct vb2_buffer *src_buf, *dst_buf;
 811        enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
 812        unsigned long flags;
 813        struct mtk_jpeg_src_buf *jpeg_src_buf;
 814        struct mtk_jpeg_bs bs;
 815        struct mtk_jpeg_fb fb;
 816        int i;
 817
 818        src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
 819        dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
 820        jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(src_buf);
 821
 822        if (jpeg_src_buf->flags & MTK_JPEG_BUF_FLAGS_LAST_FRAME) {
 823                for (i = 0; i < dst_buf->num_planes; i++)
 824                        vb2_set_plane_payload(dst_buf, i, 0);
 825                buf_state = VB2_BUF_STATE_DONE;
 826                goto dec_end;
 827        }
 828
 829        if (mtk_jpeg_check_resolution_change(ctx, &jpeg_src_buf->dec_param)) {
 830                mtk_jpeg_queue_src_chg_event(ctx);
 831                ctx->state = MTK_JPEG_SOURCE_CHANGE;
 832                v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
 833                return;
 834        }
 835
 836        mtk_jpeg_set_dec_src(ctx, src_buf, &bs);
 837        if (mtk_jpeg_set_dec_dst(ctx, &jpeg_src_buf->dec_param, dst_buf, &fb))
 838                goto dec_end;
 839
 840        spin_lock_irqsave(&jpeg->hw_lock, flags);
 841        mtk_jpeg_dec_reset(jpeg->dec_reg_base);
 842        mtk_jpeg_dec_set_config(jpeg->dec_reg_base,
 843                                &jpeg_src_buf->dec_param, &bs, &fb);
 844
 845        mtk_jpeg_dec_start(jpeg->dec_reg_base);
 846        spin_unlock_irqrestore(&jpeg->hw_lock, flags);
 847        return;
 848
 849dec_end:
 850        v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 851        v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 852        v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf), buf_state);
 853        v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), buf_state);
 854        v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
 855}
 856
 857static int mtk_jpeg_job_ready(void *priv)
 858{
 859        struct mtk_jpeg_ctx *ctx = priv;
 860
 861        return (ctx->state == MTK_JPEG_RUNNING) ? 1 : 0;
 862}
 863
 864static void mtk_jpeg_job_abort(void *priv)
 865{
 866}
 867
 868static const struct v4l2_m2m_ops mtk_jpeg_m2m_ops = {
 869        .device_run = mtk_jpeg_device_run,
 870        .job_ready  = mtk_jpeg_job_ready,
 871        .job_abort  = mtk_jpeg_job_abort,
 872};
 873
 874static int mtk_jpeg_queue_init(void *priv, struct vb2_queue *src_vq,
 875                               struct vb2_queue *dst_vq)
 876{
 877        struct mtk_jpeg_ctx *ctx = priv;
 878        int ret;
 879
 880        src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
 881        src_vq->io_modes = VB2_DMABUF | VB2_MMAP;
 882        src_vq->drv_priv = ctx;
 883        src_vq->buf_struct_size = sizeof(struct mtk_jpeg_src_buf);
 884        src_vq->ops = &mtk_jpeg_qops;
 885        src_vq->mem_ops = &vb2_dma_contig_memops;
 886        src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
 887        src_vq->lock = &ctx->jpeg->lock;
 888        src_vq->dev = ctx->jpeg->dev;
 889        ret = vb2_queue_init(src_vq);
 890        if (ret)
 891                return ret;
 892
 893        dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 894        dst_vq->io_modes = VB2_DMABUF | VB2_MMAP;
 895        dst_vq->drv_priv = ctx;
 896        dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
 897        dst_vq->ops = &mtk_jpeg_qops;
 898        dst_vq->mem_ops = &vb2_dma_contig_memops;
 899        dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
 900        dst_vq->lock = &ctx->jpeg->lock;
 901        dst_vq->dev = ctx->jpeg->dev;
 902        ret = vb2_queue_init(dst_vq);
 903
 904        return ret;
 905}
 906
 907static void mtk_jpeg_clk_on(struct mtk_jpeg_dev *jpeg)
 908{
 909        int ret;
 910
 911        ret = mtk_smi_larb_get(jpeg->larb);
 912        if (ret)
 913                dev_err(jpeg->dev, "mtk_smi_larb_get larbvdec fail %d\n", ret);
 914        clk_prepare_enable(jpeg->clk_jdec_smi);
 915        clk_prepare_enable(jpeg->clk_jdec);
 916}
 917
 918static void mtk_jpeg_clk_off(struct mtk_jpeg_dev *jpeg)
 919{
 920        clk_disable_unprepare(jpeg->clk_jdec);
 921        clk_disable_unprepare(jpeg->clk_jdec_smi);
 922        mtk_smi_larb_put(jpeg->larb);
 923}
 924
 925static irqreturn_t mtk_jpeg_dec_irq(int irq, void *priv)
 926{
 927        struct mtk_jpeg_dev *jpeg = priv;
 928        struct mtk_jpeg_ctx *ctx;
 929        struct vb2_buffer *src_buf, *dst_buf;
 930        struct mtk_jpeg_src_buf *jpeg_src_buf;
 931        enum vb2_buffer_state buf_state = VB2_BUF_STATE_ERROR;
 932        u32     dec_irq_ret;
 933        u32 dec_ret;
 934        int i;
 935
 936        dec_ret = mtk_jpeg_dec_get_int_status(jpeg->dec_reg_base);
 937        dec_irq_ret = mtk_jpeg_dec_enum_result(dec_ret);
 938        ctx = v4l2_m2m_get_curr_priv(jpeg->m2m_dev);
 939        if (!ctx) {
 940                v4l2_err(&jpeg->v4l2_dev, "Context is NULL\n");
 941                return IRQ_HANDLED;
 942        }
 943
 944        src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 945        dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 946        jpeg_src_buf = mtk_jpeg_vb2_to_srcbuf(src_buf);
 947
 948        if (dec_irq_ret >= MTK_JPEG_DEC_RESULT_UNDERFLOW)
 949                mtk_jpeg_dec_reset(jpeg->dec_reg_base);
 950
 951        if (dec_irq_ret != MTK_JPEG_DEC_RESULT_EOF_DONE) {
 952                dev_err(jpeg->dev, "decode failed\n");
 953                goto dec_end;
 954        }
 955
 956        for (i = 0; i < dst_buf->num_planes; i++)
 957                vb2_set_plane_payload(dst_buf, i,
 958                                      jpeg_src_buf->dec_param.comp_size[i]);
 959
 960        buf_state = VB2_BUF_STATE_DONE;
 961
 962dec_end:
 963        v4l2_m2m_buf_done(to_vb2_v4l2_buffer(src_buf), buf_state);
 964        v4l2_m2m_buf_done(to_vb2_v4l2_buffer(dst_buf), buf_state);
 965        v4l2_m2m_job_finish(jpeg->m2m_dev, ctx->fh.m2m_ctx);
 966        return IRQ_HANDLED;
 967}
 968
 969static void mtk_jpeg_set_default_params(struct mtk_jpeg_ctx *ctx)
 970{
 971        struct mtk_jpeg_q_data *q = &ctx->out_q;
 972        int i;
 973
 974        ctx->colorspace = V4L2_COLORSPACE_JPEG,
 975        ctx->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
 976        ctx->quantization = V4L2_QUANTIZATION_DEFAULT;
 977        ctx->xfer_func = V4L2_XFER_FUNC_DEFAULT;
 978
 979        q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_JPEG,
 980                                              MTK_JPEG_FMT_TYPE_OUTPUT);
 981        q->w = MTK_JPEG_MIN_WIDTH;
 982        q->h = MTK_JPEG_MIN_HEIGHT;
 983        q->bytesperline[0] = 0;
 984        q->sizeimage[0] = MTK_JPEG_DEFAULT_SIZEIMAGE;
 985
 986        q = &ctx->cap_q;
 987        q->fmt = mtk_jpeg_find_format(ctx, V4L2_PIX_FMT_YUV420M,
 988                                              MTK_JPEG_FMT_TYPE_CAPTURE);
 989        q->w = MTK_JPEG_MIN_WIDTH;
 990        q->h = MTK_JPEG_MIN_HEIGHT;
 991
 992        for (i = 0; i < q->fmt->colplanes; i++) {
 993                u32 stride = q->w * q->fmt->h_sample[i] / 4;
 994                u32 h = q->h * q->fmt->v_sample[i] / 4;
 995
 996                q->bytesperline[i] = stride;
 997                q->sizeimage[i] = stride * h;
 998        }
 999}
1000
1001static int mtk_jpeg_open(struct file *file)
1002{
1003        struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1004        struct video_device *vfd = video_devdata(file);
1005        struct mtk_jpeg_ctx *ctx;
1006        int ret = 0;
1007
1008        ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1009        if (!ctx)
1010                return -ENOMEM;
1011
1012        if (mutex_lock_interruptible(&jpeg->lock)) {
1013                ret = -ERESTARTSYS;
1014                goto free;
1015        }
1016
1017        v4l2_fh_init(&ctx->fh, vfd);
1018        file->private_data = &ctx->fh;
1019        v4l2_fh_add(&ctx->fh);
1020
1021        ctx->jpeg = jpeg;
1022        ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(jpeg->m2m_dev, ctx,
1023                                            mtk_jpeg_queue_init);
1024        if (IS_ERR(ctx->fh.m2m_ctx)) {
1025                ret = PTR_ERR(ctx->fh.m2m_ctx);
1026                goto error;
1027        }
1028
1029        mtk_jpeg_set_default_params(ctx);
1030        mutex_unlock(&jpeg->lock);
1031        return 0;
1032
1033error:
1034        v4l2_fh_del(&ctx->fh);
1035        v4l2_fh_exit(&ctx->fh);
1036        mutex_unlock(&jpeg->lock);
1037free:
1038        kfree(ctx);
1039        return ret;
1040}
1041
1042static int mtk_jpeg_release(struct file *file)
1043{
1044        struct mtk_jpeg_dev *jpeg = video_drvdata(file);
1045        struct mtk_jpeg_ctx *ctx = mtk_jpeg_fh_to_ctx(file->private_data);
1046
1047        mutex_lock(&jpeg->lock);
1048        v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1049        v4l2_fh_del(&ctx->fh);
1050        v4l2_fh_exit(&ctx->fh);
1051        kfree(ctx);
1052        mutex_unlock(&jpeg->lock);
1053        return 0;
1054}
1055
1056static const struct v4l2_file_operations mtk_jpeg_fops = {
1057        .owner          = THIS_MODULE,
1058        .open           = mtk_jpeg_open,
1059        .release        = mtk_jpeg_release,
1060        .poll           = v4l2_m2m_fop_poll,
1061        .unlocked_ioctl = video_ioctl2,
1062        .mmap           = v4l2_m2m_fop_mmap,
1063};
1064
1065static int mtk_jpeg_clk_init(struct mtk_jpeg_dev *jpeg)
1066{
1067        struct device_node *node;
1068        struct platform_device *pdev;
1069
1070        node = of_parse_phandle(jpeg->dev->of_node, "mediatek,larb", 0);
1071        if (!node)
1072                return -EINVAL;
1073        pdev = of_find_device_by_node(node);
1074        if (WARN_ON(!pdev)) {
1075                of_node_put(node);
1076                return -EINVAL;
1077        }
1078        of_node_put(node);
1079
1080        jpeg->larb = &pdev->dev;
1081
1082        jpeg->clk_jdec = devm_clk_get(jpeg->dev, "jpgdec");
1083        if (IS_ERR(jpeg->clk_jdec))
1084                return PTR_ERR(jpeg->clk_jdec);
1085
1086        jpeg->clk_jdec_smi = devm_clk_get(jpeg->dev, "jpgdec-smi");
1087        return PTR_ERR_OR_ZERO(jpeg->clk_jdec_smi);
1088}
1089
1090static int mtk_jpeg_probe(struct platform_device *pdev)
1091{
1092        struct mtk_jpeg_dev *jpeg;
1093        struct resource *res;
1094        int dec_irq;
1095        int ret;
1096
1097        jpeg = devm_kzalloc(&pdev->dev, sizeof(*jpeg), GFP_KERNEL);
1098        if (!jpeg)
1099                return -ENOMEM;
1100
1101        mutex_init(&jpeg->lock);
1102        spin_lock_init(&jpeg->hw_lock);
1103        jpeg->dev = &pdev->dev;
1104
1105        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1106        jpeg->dec_reg_base = devm_ioremap_resource(&pdev->dev, res);
1107        if (IS_ERR(jpeg->dec_reg_base)) {
1108                ret = PTR_ERR(jpeg->dec_reg_base);
1109                return ret;
1110        }
1111
1112        res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1113        dec_irq = platform_get_irq(pdev, 0);
1114        if (!res || dec_irq < 0) {
1115                dev_err(&pdev->dev, "Failed to get dec_irq %d.\n", dec_irq);
1116                ret = -EINVAL;
1117                return ret;
1118        }
1119
1120        ret = devm_request_irq(&pdev->dev, dec_irq, mtk_jpeg_dec_irq, 0,
1121                               pdev->name, jpeg);
1122        if (ret) {
1123                dev_err(&pdev->dev, "Failed to request dec_irq %d (%d)\n",
1124                        dec_irq, ret);
1125                ret = -EINVAL;
1126                goto err_req_irq;
1127        }
1128
1129        ret = mtk_jpeg_clk_init(jpeg);
1130        if (ret) {
1131                dev_err(&pdev->dev, "Failed to init clk, err %d\n", ret);
1132                goto err_clk_init;
1133        }
1134
1135        ret = v4l2_device_register(&pdev->dev, &jpeg->v4l2_dev);
1136        if (ret) {
1137                dev_err(&pdev->dev, "Failed to register v4l2 device\n");
1138                ret = -EINVAL;
1139                goto err_dev_register;
1140        }
1141
1142        jpeg->m2m_dev = v4l2_m2m_init(&mtk_jpeg_m2m_ops);
1143        if (IS_ERR(jpeg->m2m_dev)) {
1144                v4l2_err(&jpeg->v4l2_dev, "Failed to init mem2mem device\n");
1145                ret = PTR_ERR(jpeg->m2m_dev);
1146                goto err_m2m_init;
1147        }
1148
1149        jpeg->dec_vdev = video_device_alloc();
1150        if (!jpeg->dec_vdev) {
1151                ret = -ENOMEM;
1152                goto err_dec_vdev_alloc;
1153        }
1154        snprintf(jpeg->dec_vdev->name, sizeof(jpeg->dec_vdev->name),
1155                 "%s-dec", MTK_JPEG_NAME);
1156        jpeg->dec_vdev->fops = &mtk_jpeg_fops;
1157        jpeg->dec_vdev->ioctl_ops = &mtk_jpeg_ioctl_ops;
1158        jpeg->dec_vdev->minor = -1;
1159        jpeg->dec_vdev->release = video_device_release;
1160        jpeg->dec_vdev->lock = &jpeg->lock;
1161        jpeg->dec_vdev->v4l2_dev = &jpeg->v4l2_dev;
1162        jpeg->dec_vdev->vfl_dir = VFL_DIR_M2M;
1163        jpeg->dec_vdev->device_caps = V4L2_CAP_STREAMING |
1164                                      V4L2_CAP_VIDEO_M2M_MPLANE;
1165
1166        ret = video_register_device(jpeg->dec_vdev, VFL_TYPE_GRABBER, 3);
1167        if (ret) {
1168                v4l2_err(&jpeg->v4l2_dev, "Failed to register video device\n");
1169                goto err_dec_vdev_register;
1170        }
1171
1172        video_set_drvdata(jpeg->dec_vdev, jpeg);
1173        v4l2_info(&jpeg->v4l2_dev,
1174                  "decoder device registered as /dev/video%d (%d,%d)\n",
1175                  jpeg->dec_vdev->num, VIDEO_MAJOR, jpeg->dec_vdev->minor);
1176
1177        platform_set_drvdata(pdev, jpeg);
1178
1179        pm_runtime_enable(&pdev->dev);
1180
1181        return 0;
1182
1183err_dec_vdev_register:
1184        video_device_release(jpeg->dec_vdev);
1185
1186err_dec_vdev_alloc:
1187        v4l2_m2m_release(jpeg->m2m_dev);
1188
1189err_m2m_init:
1190        v4l2_device_unregister(&jpeg->v4l2_dev);
1191
1192err_dev_register:
1193
1194err_clk_init:
1195
1196err_req_irq:
1197
1198        return ret;
1199}
1200
1201static int mtk_jpeg_remove(struct platform_device *pdev)
1202{
1203        struct mtk_jpeg_dev *jpeg = platform_get_drvdata(pdev);
1204
1205        pm_runtime_disable(&pdev->dev);
1206        video_unregister_device(jpeg->dec_vdev);
1207        video_device_release(jpeg->dec_vdev);
1208        v4l2_m2m_release(jpeg->m2m_dev);
1209        v4l2_device_unregister(&jpeg->v4l2_dev);
1210
1211        return 0;
1212}
1213
1214static __maybe_unused int mtk_jpeg_pm_suspend(struct device *dev)
1215{
1216        struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1217
1218        mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1219        mtk_jpeg_clk_off(jpeg);
1220
1221        return 0;
1222}
1223
1224static __maybe_unused int mtk_jpeg_pm_resume(struct device *dev)
1225{
1226        struct mtk_jpeg_dev *jpeg = dev_get_drvdata(dev);
1227
1228        mtk_jpeg_clk_on(jpeg);
1229        mtk_jpeg_dec_reset(jpeg->dec_reg_base);
1230
1231        return 0;
1232}
1233
1234static __maybe_unused int mtk_jpeg_suspend(struct device *dev)
1235{
1236        int ret;
1237
1238        if (pm_runtime_suspended(dev))
1239                return 0;
1240
1241        ret = mtk_jpeg_pm_suspend(dev);
1242        return ret;
1243}
1244
1245static __maybe_unused int mtk_jpeg_resume(struct device *dev)
1246{
1247        int ret;
1248
1249        if (pm_runtime_suspended(dev))
1250                return 0;
1251
1252        ret = mtk_jpeg_pm_resume(dev);
1253
1254        return ret;
1255}
1256
1257static const struct dev_pm_ops mtk_jpeg_pm_ops = {
1258        SET_SYSTEM_SLEEP_PM_OPS(mtk_jpeg_suspend, mtk_jpeg_resume)
1259        SET_RUNTIME_PM_OPS(mtk_jpeg_pm_suspend, mtk_jpeg_pm_resume, NULL)
1260};
1261
1262static const struct of_device_id mtk_jpeg_match[] = {
1263        {
1264                .compatible = "mediatek,mt8173-jpgdec",
1265                .data       = NULL,
1266        },
1267        {
1268                .compatible = "mediatek,mt2701-jpgdec",
1269                .data       = NULL,
1270        },
1271        {},
1272};
1273
1274MODULE_DEVICE_TABLE(of, mtk_jpeg_match);
1275
1276static struct platform_driver mtk_jpeg_driver = {
1277        .probe = mtk_jpeg_probe,
1278        .remove = mtk_jpeg_remove,
1279        .driver = {
1280                .name           = MTK_JPEG_NAME,
1281                .of_match_table = mtk_jpeg_match,
1282                .pm             = &mtk_jpeg_pm_ops,
1283        },
1284};
1285
1286module_platform_driver(mtk_jpeg_driver);
1287
1288MODULE_DESCRIPTION("MediaTek JPEG codec driver");
1289MODULE_LICENSE("GPL v2");
1290