1 // SPDX-License-Identifier: GPL-2.0
3 * Hantro VPU codec driver
5 * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
6 * Jeffy Chen <jeffy.chen@rock-chips.com>
12 #include "hantro_jpeg.h"
13 #include "rk3399_vpu_regs.h"
15 #define RK3399_ACLK_MAX_FREQ (400 * 1000 * 1000)
21 static const struct hantro_fmt rk3399_vpu_enc_fmts[] = {
23 .fourcc = V4L2_PIX_FMT_YUV420M,
24 .codec_mode = HANTRO_MODE_NONE,
25 .enc_fmt = RK3288_VPU_ENC_FMT_YUV420P,
28 .fourcc = V4L2_PIX_FMT_NV12M,
29 .codec_mode = HANTRO_MODE_NONE,
30 .enc_fmt = RK3288_VPU_ENC_FMT_YUV420SP,
33 .fourcc = V4L2_PIX_FMT_YUYV,
34 .codec_mode = HANTRO_MODE_NONE,
35 .enc_fmt = RK3288_VPU_ENC_FMT_YUYV422,
38 .fourcc = V4L2_PIX_FMT_UYVY,
39 .codec_mode = HANTRO_MODE_NONE,
40 .enc_fmt = RK3288_VPU_ENC_FMT_UYVY422,
43 .fourcc = V4L2_PIX_FMT_JPEG,
44 .codec_mode = HANTRO_MODE_JPEG_ENC,
46 .header_size = JPEG_HEADER_SIZE,
53 .step_height = MB_DIM,
58 static const struct hantro_fmt rk3399_vpu_dec_fmts[] = {
60 .fourcc = V4L2_PIX_FMT_NV12,
61 .codec_mode = HANTRO_MODE_NONE,
64 .fourcc = V4L2_PIX_FMT_MPEG2_SLICE,
65 .codec_mode = HANTRO_MODE_MPEG2_DEC,
73 .step_height = MB_DIM,
77 .fourcc = V4L2_PIX_FMT_VP8_FRAME,
78 .codec_mode = HANTRO_MODE_VP8_DEC,
86 .step_height = MB_DIM,
91 static irqreturn_t rk3399_vepu_irq(int irq, void *dev_id)
93 struct hantro_dev *vpu = dev_id;
94 enum vb2_buffer_state state;
95 u32 status, bytesused;
97 status = vepu_read(vpu, VEPU_REG_INTERRUPT);
98 bytesused = vepu_read(vpu, VEPU_REG_STR_BUF_LIMIT) / 8;
99 state = (status & VEPU_REG_INTERRUPT_FRAME_READY) ?
100 VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
102 vepu_write(vpu, 0, VEPU_REG_INTERRUPT);
103 vepu_write(vpu, 0, VEPU_REG_AXI_CTRL);
105 hantro_irq_done(vpu, bytesused, state);
110 static irqreturn_t rk3399_vdpu_irq(int irq, void *dev_id)
112 struct hantro_dev *vpu = dev_id;
113 enum vb2_buffer_state state;
116 status = vdpu_read(vpu, VDPU_REG_INTERRUPT);
117 state = (status & VDPU_REG_INTERRUPT_DEC_IRQ) ?
118 VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
120 vdpu_write(vpu, 0, VDPU_REG_INTERRUPT);
121 vdpu_write(vpu, 0, VDPU_REG_AXI_CTRL);
123 hantro_irq_done(vpu, 0, state);
128 static int rk3399_vpu_hw_init(struct hantro_dev *vpu)
130 /* Bump ACLK to max. possible freq. to improve performance. */
131 clk_set_rate(vpu->clocks[0].clk, RK3399_ACLK_MAX_FREQ);
135 static void rk3399_vpu_enc_reset(struct hantro_ctx *ctx)
137 struct hantro_dev *vpu = ctx->dev;
139 vepu_write(vpu, VEPU_REG_INTERRUPT_DIS_BIT, VEPU_REG_INTERRUPT);
140 vepu_write(vpu, 0, VEPU_REG_ENCODE_START);
141 vepu_write(vpu, 0, VEPU_REG_AXI_CTRL);
144 static void rk3399_vpu_dec_reset(struct hantro_ctx *ctx)
146 struct hantro_dev *vpu = ctx->dev;
148 vdpu_write(vpu, VDPU_REG_INTERRUPT_DEC_IRQ_DIS, VDPU_REG_INTERRUPT);
149 vdpu_write(vpu, 0, VDPU_REG_EN_FLAGS);
150 vdpu_write(vpu, 1, VDPU_REG_SOFT_RESET);
154 * Supported codec ops.
157 static const struct hantro_codec_ops rk3399_vpu_codec_ops[] = {
158 [HANTRO_MODE_JPEG_ENC] = {
159 .run = rk3399_vpu_jpeg_enc_run,
160 .reset = rk3399_vpu_enc_reset,
161 .init = hantro_jpeg_enc_init,
162 .exit = hantro_jpeg_enc_exit,
164 [HANTRO_MODE_MPEG2_DEC] = {
165 .run = rk3399_vpu_mpeg2_dec_run,
166 .reset = rk3399_vpu_dec_reset,
167 .init = hantro_mpeg2_dec_init,
168 .exit = hantro_mpeg2_dec_exit,
170 [HANTRO_MODE_VP8_DEC] = {
171 .run = rk3399_vpu_vp8_dec_run,
172 .reset = rk3399_vpu_dec_reset,
173 .init = hantro_vp8_dec_init,
174 .exit = hantro_vp8_dec_exit,
182 static const struct hantro_irq rk3399_irqs[] = {
183 { "vepu", rk3399_vepu_irq },
184 { "vdpu", rk3399_vdpu_irq },
187 static const char * const rk3399_clk_names[] = {
191 const struct hantro_variant rk3399_vpu_variant = {
193 .enc_fmts = rk3399_vpu_enc_fmts,
194 .num_enc_fmts = ARRAY_SIZE(rk3399_vpu_enc_fmts),
196 .dec_fmts = rk3399_vpu_dec_fmts,
197 .num_dec_fmts = ARRAY_SIZE(rk3399_vpu_dec_fmts),
198 .codec = HANTRO_JPEG_ENCODER | HANTRO_MPEG2_DECODER |
200 .codec_ops = rk3399_vpu_codec_ops,
202 .num_irqs = ARRAY_SIZE(rk3399_irqs),
203 .init = rk3399_vpu_hw_init,
204 .clk_names = rk3399_clk_names,
205 .num_clocks = ARRAY_SIZE(rk3399_clk_names)
208 static const struct hantro_irq rk3328_irqs[] = {
209 { "vdpu", rk3399_vdpu_irq },
212 const struct hantro_variant rk3328_vpu_variant = {
214 .dec_fmts = rk3399_vpu_dec_fmts,
215 .num_dec_fmts = ARRAY_SIZE(rk3399_vpu_dec_fmts),
216 .codec = HANTRO_MPEG2_DECODER | HANTRO_VP8_DECODER,
217 .codec_ops = rk3399_vpu_codec_ops,
219 .num_irqs = ARRAY_SIZE(rk3328_irqs),
220 .init = rk3399_vpu_hw_init,
221 .clk_names = rk3399_clk_names,
222 .num_clocks = ARRAY_SIZE(rk3399_clk_names),