1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: PC Chen <pc.chen@mediatek.com>
5 * Tiffany Lin <tiffany.lin@mediatek.com>
6 */
7
8 #include <linux/interrupt.h>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11
12 #include "vdec_drv_if.h"
13 #include "mtk_vcodec_dec.h"
14 #include "vdec_drv_base.h"
15 #include "mtk_vcodec_dec_pm.h"
16
vdec_if_init(struct mtk_vcodec_ctx * ctx,unsigned int fourcc)17 int vdec_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
18 {
19 int ret = 0;
20
21 switch (fourcc) {
22 case V4L2_PIX_FMT_H264_SLICE:
23 ctx->dec_if = &vdec_h264_slice_if;
24 break;
25 case V4L2_PIX_FMT_H264:
26 ctx->dec_if = &vdec_h264_if;
27 break;
28 case V4L2_PIX_FMT_VP8:
29 ctx->dec_if = &vdec_vp8_if;
30 break;
31 case V4L2_PIX_FMT_VP9:
32 ctx->dec_if = &vdec_vp9_if;
33 break;
34 default:
35 return -EINVAL;
36 }
37
38 mtk_vdec_lock(ctx);
39 mtk_vcodec_dec_clock_on(&ctx->dev->pm);
40 ret = ctx->dec_if->init(ctx);
41 mtk_vcodec_dec_clock_off(&ctx->dev->pm);
42 mtk_vdec_unlock(ctx);
43
44 return ret;
45 }
46
vdec_if_decode(struct mtk_vcodec_ctx * ctx,struct mtk_vcodec_mem * bs,struct vdec_fb * fb,bool * res_chg)47 int vdec_if_decode(struct mtk_vcodec_ctx *ctx, struct mtk_vcodec_mem *bs,
48 struct vdec_fb *fb, bool *res_chg)
49 {
50 int ret = 0;
51
52 if (bs) {
53 if ((bs->dma_addr & 63) != 0) {
54 mtk_v4l2_err("bs dma_addr should 64 byte align");
55 return -EINVAL;
56 }
57 }
58
59 if (fb) {
60 if (((fb->base_y.dma_addr & 511) != 0) ||
61 ((fb->base_c.dma_addr & 511) != 0)) {
62 mtk_v4l2_err("frame buffer dma_addr should 512 byte align");
63 return -EINVAL;
64 }
65 }
66
67 if (!ctx->drv_handle)
68 return -EIO;
69
70 mtk_vdec_lock(ctx);
71
72 mtk_vcodec_set_curr_ctx(ctx->dev, ctx);
73 mtk_vcodec_dec_clock_on(&ctx->dev->pm);
74 enable_irq(ctx->dev->dec_irq);
75 ret = ctx->dec_if->decode(ctx->drv_handle, bs, fb, res_chg);
76 disable_irq(ctx->dev->dec_irq);
77 mtk_vcodec_dec_clock_off(&ctx->dev->pm);
78 mtk_vcodec_set_curr_ctx(ctx->dev, NULL);
79
80 mtk_vdec_unlock(ctx);
81
82 return ret;
83 }
84
vdec_if_get_param(struct mtk_vcodec_ctx * ctx,enum vdec_get_param_type type,void * out)85 int vdec_if_get_param(struct mtk_vcodec_ctx *ctx, enum vdec_get_param_type type,
86 void *out)
87 {
88 int ret = 0;
89
90 if (!ctx->drv_handle)
91 return -EIO;
92
93 mtk_vdec_lock(ctx);
94 ret = ctx->dec_if->get_param(ctx->drv_handle, type, out);
95 mtk_vdec_unlock(ctx);
96
97 return ret;
98 }
99
vdec_if_deinit(struct mtk_vcodec_ctx * ctx)100 void vdec_if_deinit(struct mtk_vcodec_ctx *ctx)
101 {
102 if (!ctx->drv_handle)
103 return;
104
105 mtk_vdec_lock(ctx);
106 mtk_vcodec_dec_clock_on(&ctx->dev->pm);
107 ctx->dec_if->deinit(ctx->drv_handle);
108 mtk_vcodec_dec_clock_off(&ctx->dev->pm);
109 mtk_vdec_unlock(ctx);
110
111 ctx->drv_handle = NULL;
112 }
113