treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 174
[sfrench/cifs-2.6.git] / drivers / media / platform / mtk-vcodec / venc_drv_if.c
1 // SPDX-License-Identifier: GPL-2.0-only
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
20 const struct venc_common_if *get_h264_enc_comm_if(void);
21 const struct venc_common_if *get_vp8_enc_comm_if(void);
22
23 int venc_if_init(struct mtk_vcodec_ctx *ctx, unsigned int fourcc)
24 {
25         int ret = 0;
26
27         switch (fourcc) {
28         case V4L2_PIX_FMT_VP8:
29                 ctx->enc_if = get_vp8_enc_comm_if();
30                 break;
31         case V4L2_PIX_FMT_H264:
32                 ctx->enc_if = get_h264_enc_comm_if();
33                 break;
34         default:
35                 return -EINVAL;
36         }
37
38         mtk_venc_lock(ctx);
39         mtk_vcodec_enc_clock_on(&ctx->dev->pm);
40         ret = ctx->enc_if->init(ctx, (unsigned long *)&ctx->drv_handle);
41         mtk_vcodec_enc_clock_off(&ctx->dev->pm);
42         mtk_venc_unlock(ctx);
43
44         return ret;
45 }
46
47 int venc_if_set_param(struct mtk_vcodec_ctx *ctx,
48                 enum venc_set_param_type type, struct venc_enc_param *in)
49 {
50         int ret = 0;
51
52         mtk_venc_lock(ctx);
53         mtk_vcodec_enc_clock_on(&ctx->dev->pm);
54         ret = ctx->enc_if->set_param(ctx->drv_handle, type, in);
55         mtk_vcodec_enc_clock_off(&ctx->dev->pm);
56         mtk_venc_unlock(ctx);
57
58         return ret;
59 }
60
61 int venc_if_encode(struct mtk_vcodec_ctx *ctx,
62                    enum venc_start_opt opt, struct venc_frm_buf *frm_buf,
63                    struct mtk_vcodec_mem *bs_buf,
64                    struct venc_done_result *result)
65 {
66         int ret = 0;
67         unsigned long flags;
68
69         mtk_venc_lock(ctx);
70
71         spin_lock_irqsave(&ctx->dev->irqlock, flags);
72         ctx->dev->curr_ctx = ctx;
73         spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
74
75         mtk_vcodec_enc_clock_on(&ctx->dev->pm);
76         ret = ctx->enc_if->encode(ctx->drv_handle, opt, frm_buf,
77                                   bs_buf, result);
78         mtk_vcodec_enc_clock_off(&ctx->dev->pm);
79
80         spin_lock_irqsave(&ctx->dev->irqlock, flags);
81         ctx->dev->curr_ctx = NULL;
82         spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
83
84         mtk_venc_unlock(ctx);
85         return ret;
86 }
87
88 int venc_if_deinit(struct mtk_vcodec_ctx *ctx)
89 {
90         int ret = 0;
91
92         if (ctx->drv_handle == 0)
93                 return 0;
94
95         mtk_venc_lock(ctx);
96         mtk_vcodec_enc_clock_on(&ctx->dev->pm);
97         ret = ctx->enc_if->deinit(ctx->drv_handle);
98         mtk_vcodec_enc_clock_off(&ctx->dev->pm);
99         mtk_venc_unlock(ctx);
100
101         ctx->drv_handle = 0;
102
103         return ret;
104 }