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,
50 .step_width = JPEG_MB_DIM,
53 .step_height = JPEG_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,
70 .step_width = MPEG2_MB_DIM,
73 .step_height = MPEG2_MB_DIM,
78 static irqreturn_t rk3399_vepu_irq(int irq, void *dev_id)
80 struct hantro_dev *vpu = dev_id;
81 enum vb2_buffer_state state;
82 u32 status, bytesused;
84 status = vepu_read(vpu, VEPU_REG_INTERRUPT);
85 bytesused = vepu_read(vpu, VEPU_REG_STR_BUF_LIMIT) / 8;
86 state = (status & VEPU_REG_INTERRUPT_FRAME_READY) ?
87 VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
89 vepu_write(vpu, 0, VEPU_REG_INTERRUPT);
90 vepu_write(vpu, 0, VEPU_REG_AXI_CTRL);
92 hantro_irq_done(vpu, bytesused, state);
97 static irqreturn_t rk3399_vdpu_irq(int irq, void *dev_id)
99 struct hantro_dev *vpu = dev_id;
100 enum vb2_buffer_state state;
103 status = vdpu_read(vpu, VDPU_REG_INTERRUPT);
104 state = (status & VDPU_REG_INTERRUPT_DEC_IRQ) ?
105 VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
107 vdpu_write(vpu, 0, VDPU_REG_INTERRUPT);
108 vdpu_write(vpu, 0, VDPU_REG_AXI_CTRL);
110 hantro_irq_done(vpu, 0, state);
115 static int rk3399_vpu_hw_init(struct hantro_dev *vpu)
117 /* Bump ACLK to max. possible freq. to improve performance. */
118 clk_set_rate(vpu->clocks[0].clk, RK3399_ACLK_MAX_FREQ);
122 static void rk3399_vpu_enc_reset(struct hantro_ctx *ctx)
124 struct hantro_dev *vpu = ctx->dev;
126 vepu_write(vpu, VEPU_REG_INTERRUPT_DIS_BIT, VEPU_REG_INTERRUPT);
127 vepu_write(vpu, 0, VEPU_REG_ENCODE_START);
128 vepu_write(vpu, 0, VEPU_REG_AXI_CTRL);
131 static void rk3399_vpu_dec_reset(struct hantro_ctx *ctx)
133 struct hantro_dev *vpu = ctx->dev;
135 vdpu_write(vpu, VDPU_REG_INTERRUPT_DEC_IRQ_DIS, VDPU_REG_INTERRUPT);
136 vdpu_write(vpu, 0, VDPU_REG_EN_FLAGS);
137 vdpu_write(vpu, 1, VDPU_REG_SOFT_RESET);
141 * Supported codec ops.
144 static const struct hantro_codec_ops rk3399_vpu_codec_ops[] = {
145 [HANTRO_MODE_JPEG_ENC] = {
146 .run = rk3399_vpu_jpeg_enc_run,
147 .reset = rk3399_vpu_enc_reset,
148 .init = hantro_jpeg_enc_init,
149 .exit = hantro_jpeg_enc_exit,
151 [HANTRO_MODE_MPEG2_DEC] = {
152 .run = rk3399_vpu_mpeg2_dec_run,
153 .reset = rk3399_vpu_dec_reset,
154 .init = hantro_mpeg2_dec_init,
155 .exit = hantro_mpeg2_dec_exit,
163 static const struct hantro_irq rk3399_irqs[] = {
164 { "vepu", rk3399_vepu_irq },
165 { "vdpu", rk3399_vdpu_irq },
168 static const char * const rk3399_clk_names[] = {
172 const struct hantro_variant rk3399_vpu_variant = {
174 .enc_fmts = rk3399_vpu_enc_fmts,
175 .num_enc_fmts = ARRAY_SIZE(rk3399_vpu_enc_fmts),
177 .dec_fmts = rk3399_vpu_dec_fmts,
178 .num_dec_fmts = ARRAY_SIZE(rk3399_vpu_dec_fmts),
179 .codec = HANTRO_JPEG_ENCODER | HANTRO_MPEG2_DECODER,
180 .codec_ops = rk3399_vpu_codec_ops,
182 .num_irqs = ARRAY_SIZE(rk3399_irqs),
183 .init = rk3399_vpu_hw_init,
184 .clk_names = rk3399_clk_names,
185 .num_clocks = ARRAY_SIZE(rk3399_clk_names)