1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2016 MediaTek Inc.
4 * Author: Daniel Hsiao <daniel.hsiao@mediatek.com>
5 * Jungchang Tsao <jungchang.tsao@mediatek.com>
6 * Tiffany Lin <tiffany.lin@mediatek.com>
7 */
8
9 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12
13 #include "venc_drv_base.h"
14 #include "venc_drv_if.h"
15
16 #include "mtk_vcodec_enc.h"
17 #include "mtk_vcodec_enc_pm.h"
18 #include "mtk_vpu.h"
19
venc_if_init(struct mtk_vcodec_ctx * ctx,unsigned int fourcc)20 int venc_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
21 {
22 int ret = 0;
23
24 switch (fourcc) {
25 case V4L2_PIX_FMT_VP8:
26 ctx->enc_if = &venc_vp8_if;
27 break;
28 case V4L2_PIX_FMT_H264:
29 ctx->enc_if = &venc_h264_if;
30 break;
31 default:
32 return -EINVAL;
33 }
34
35 mtk_venc_lock(ctx);
36 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
37 ret = ctx->enc_if->init(ctx);
38 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
39 mtk_venc_unlock(ctx);
40
41 return ret;
42 }
43
venc_if_set_param(struct mtk_vcodec_ctx * ctx,enum venc_set_param_type type,struct venc_enc_param * in)44 int venc_if_set_param(struct mtk_vcodec_ctx *ctx,
45 enum venc_set_param_type type, struct venc_enc_param *in)
46 {
47 int ret = 0;
48
49 mtk_venc_lock(ctx);
50 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
51 ret = ctx->enc_if->set_param(ctx->drv_handle, type, in);
52 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
53 mtk_venc_unlock(ctx);
54
55 return ret;
56 }
57
venc_if_encode(struct mtk_vcodec_ctx * ctx,enum venc_start_opt opt,struct venc_frm_buf * frm_buf,struct mtk_vcodec_mem * bs_buf,struct venc_done_result * result)58 int venc_if_encode(struct mtk_vcodec_ctx *ctx,
59 enum venc_start_opt opt, struct venc_frm_buf *frm_buf,
60 struct mtk_vcodec_mem *bs_buf,
61 struct venc_done_result *result)
62 {
63 int ret = 0;
64 unsigned long flags;
65
66 mtk_venc_lock(ctx);
67
68 spin_lock_irqsave(&ctx->dev->irqlock, flags);
69 ctx->dev->curr_ctx = ctx;
70 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
71
72 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
73 ret = ctx->enc_if->encode(ctx->drv_handle, opt, frm_buf,
74 bs_buf, result);
75 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
76
77 spin_lock_irqsave(&ctx->dev->irqlock, flags);
78 ctx->dev->curr_ctx = NULL;
79 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
80
81 mtk_venc_unlock(ctx);
82 return ret;
83 }
84
venc_if_deinit(struct mtk_vcodec_ctx * ctx)85 int venc_if_deinit(struct mtk_vcodec_ctx *ctx)
86 {
87 int ret = 0;
88
89 if (!ctx->drv_handle)
90 return 0;
91
92 mtk_venc_lock(ctx);
93 mtk_vcodec_enc_clock_on(&ctx->dev->pm);
94 ret = ctx->enc_if->deinit(ctx->drv_handle);
95 mtk_vcodec_enc_clock_off(&ctx->dev->pm);
96 mtk_venc_unlock(ctx);
97
98 ctx->drv_handle = NULL;
99
100 return ret;
101 }
102