1 // SPDX-License-Identifier: GPL-2.0
3 * Hantro VPU codec driver
5 * Copyright (C) 2019 Pengutronix, Philipp Zabel <kernel@pengutronix.de>
9 #include <linux/delay.h>
12 #include "hantro_jpeg.h"
13 #include "hantro_g1_regs.h"
14 #include "hantro_g2_regs.h"
16 #define CTRL_SOFT_RESET 0x00
17 #define RESET_G1 BIT(1)
18 #define RESET_G2 BIT(0)
20 #define CTRL_CLOCK_ENABLE 0x04
21 #define CLOCK_G1 BIT(1)
22 #define CLOCK_G2 BIT(0)
24 #define CTRL_G1_DEC_FUSE 0x08
25 #define CTRL_G1_PP_FUSE 0x0c
26 #define CTRL_G2_DEC_FUSE 0x10
28 static void imx8m_soft_reset(struct hantro_dev *vpu, u32 reset_bits)
33 val = readl(vpu->ctrl_base + CTRL_SOFT_RESET);
35 writel(val, vpu->ctrl_base + CTRL_SOFT_RESET);
40 val = readl(vpu->ctrl_base + CTRL_SOFT_RESET);
42 writel(val, vpu->ctrl_base + CTRL_SOFT_RESET);
45 static void imx8m_clk_enable(struct hantro_dev *vpu, u32 clock_bits)
49 val = readl(vpu->ctrl_base + CTRL_CLOCK_ENABLE);
51 writel(val, vpu->ctrl_base + CTRL_CLOCK_ENABLE);
54 static int imx8mq_runtime_resume(struct hantro_dev *vpu)
58 ret = clk_bulk_prepare_enable(vpu->variant->num_clocks, vpu->clocks);
60 dev_err(vpu->dev, "Failed to enable clocks\n");
64 imx8m_soft_reset(vpu, RESET_G1 | RESET_G2);
65 imx8m_clk_enable(vpu, CLOCK_G1 | CLOCK_G2);
67 /* Set values of the fuse registers */
68 writel(0xffffffff, vpu->ctrl_base + CTRL_G1_DEC_FUSE);
69 writel(0xffffffff, vpu->ctrl_base + CTRL_G1_PP_FUSE);
70 writel(0xffffffff, vpu->ctrl_base + CTRL_G2_DEC_FUSE);
72 clk_bulk_disable_unprepare(vpu->variant->num_clocks, vpu->clocks);
81 static const struct hantro_fmt imx8m_vpu_postproc_fmts[] = {
83 .fourcc = V4L2_PIX_FMT_YUYV,
84 .codec_mode = HANTRO_MODE_NONE,
85 .postprocessed = true,
89 static const struct hantro_fmt imx8m_vpu_dec_fmts[] = {
91 .fourcc = V4L2_PIX_FMT_NV12,
92 .codec_mode = HANTRO_MODE_NONE,
95 .fourcc = V4L2_PIX_FMT_MPEG2_SLICE,
96 .codec_mode = HANTRO_MODE_MPEG2_DEC,
101 .step_width = MB_DIM,
104 .step_height = MB_DIM,
108 .fourcc = V4L2_PIX_FMT_VP8_FRAME,
109 .codec_mode = HANTRO_MODE_VP8_DEC,
114 .step_width = MB_DIM,
117 .step_height = MB_DIM,
121 .fourcc = V4L2_PIX_FMT_H264_SLICE,
122 .codec_mode = HANTRO_MODE_H264_DEC,
127 .step_width = MB_DIM,
130 .step_height = MB_DIM,
135 static const struct hantro_fmt imx8m_vpu_g2_postproc_fmts[] = {
137 .fourcc = V4L2_PIX_FMT_NV12,
138 .codec_mode = HANTRO_MODE_NONE,
139 .postprocessed = true,
143 static const struct hantro_fmt imx8m_vpu_g2_dec_fmts[] = {
145 .fourcc = V4L2_PIX_FMT_NV12_4L4,
146 .codec_mode = HANTRO_MODE_NONE,
149 .fourcc = V4L2_PIX_FMT_HEVC_SLICE,
150 .codec_mode = HANTRO_MODE_HEVC_DEC,
155 .step_width = MB_DIM,
158 .step_height = MB_DIM,
162 .fourcc = V4L2_PIX_FMT_VP9_FRAME,
163 .codec_mode = HANTRO_MODE_VP9_DEC,
168 .step_width = MB_DIM,
171 .step_height = MB_DIM,
176 static irqreturn_t imx8m_vpu_g1_irq(int irq, void *dev_id)
178 struct hantro_dev *vpu = dev_id;
179 enum vb2_buffer_state state;
182 status = vdpu_read(vpu, G1_REG_INTERRUPT);
183 state = (status & G1_REG_INTERRUPT_DEC_RDY_INT) ?
184 VB2_BUF_STATE_DONE : VB2_BUF_STATE_ERROR;
186 vdpu_write(vpu, 0, G1_REG_INTERRUPT);
187 vdpu_write(vpu, G1_REG_CONFIG_DEC_CLK_GATE_E, G1_REG_CONFIG);
189 hantro_irq_done(vpu, state);
194 static int imx8mq_vpu_hw_init(struct hantro_dev *vpu)
196 vpu->ctrl_base = vpu->reg_bases[vpu->variant->num_regs - 1];
201 static void imx8m_vpu_g1_reset(struct hantro_ctx *ctx)
203 struct hantro_dev *vpu = ctx->dev;
205 imx8m_soft_reset(vpu, RESET_G1);
209 * Supported codec ops.
212 static const struct hantro_codec_ops imx8mq_vpu_codec_ops[] = {
213 [HANTRO_MODE_MPEG2_DEC] = {
214 .run = hantro_g1_mpeg2_dec_run,
215 .reset = imx8m_vpu_g1_reset,
216 .init = hantro_mpeg2_dec_init,
217 .exit = hantro_mpeg2_dec_exit,
219 [HANTRO_MODE_VP8_DEC] = {
220 .run = hantro_g1_vp8_dec_run,
221 .reset = imx8m_vpu_g1_reset,
222 .init = hantro_vp8_dec_init,
223 .exit = hantro_vp8_dec_exit,
225 [HANTRO_MODE_H264_DEC] = {
226 .run = hantro_g1_h264_dec_run,
227 .reset = imx8m_vpu_g1_reset,
228 .init = hantro_h264_dec_init,
229 .exit = hantro_h264_dec_exit,
233 static const struct hantro_codec_ops imx8mq_vpu_g1_codec_ops[] = {
234 [HANTRO_MODE_MPEG2_DEC] = {
235 .run = hantro_g1_mpeg2_dec_run,
236 .init = hantro_mpeg2_dec_init,
237 .exit = hantro_mpeg2_dec_exit,
239 [HANTRO_MODE_VP8_DEC] = {
240 .run = hantro_g1_vp8_dec_run,
241 .init = hantro_vp8_dec_init,
242 .exit = hantro_vp8_dec_exit,
244 [HANTRO_MODE_H264_DEC] = {
245 .run = hantro_g1_h264_dec_run,
246 .init = hantro_h264_dec_init,
247 .exit = hantro_h264_dec_exit,
251 static const struct hantro_codec_ops imx8mq_vpu_g2_codec_ops[] = {
252 [HANTRO_MODE_HEVC_DEC] = {
253 .run = hantro_g2_hevc_dec_run,
254 .init = hantro_hevc_dec_init,
255 .exit = hantro_hevc_dec_exit,
257 [HANTRO_MODE_VP9_DEC] = {
258 .run = hantro_g2_vp9_dec_run,
259 .done = hantro_g2_vp9_dec_done,
260 .init = hantro_vp9_dec_init,
261 .exit = hantro_vp9_dec_exit,
269 static const struct hantro_irq imx8mq_irqs[] = {
270 { "g1", imx8m_vpu_g1_irq },
273 static const struct hantro_irq imx8mq_g2_irqs[] = {
274 { "g2", hantro_g2_irq },
277 static const char * const imx8mq_clk_names[] = { "g1", "g2", "bus" };
278 static const char * const imx8mq_reg_names[] = { "g1", "g2", "ctrl" };
279 static const char * const imx8mq_g1_clk_names[] = { "g1" };
280 static const char * const imx8mq_g2_clk_names[] = { "g2" };
282 const struct hantro_variant imx8mq_vpu_variant = {
283 .dec_fmts = imx8m_vpu_dec_fmts,
284 .num_dec_fmts = ARRAY_SIZE(imx8m_vpu_dec_fmts),
285 .postproc_fmts = imx8m_vpu_postproc_fmts,
286 .num_postproc_fmts = ARRAY_SIZE(imx8m_vpu_postproc_fmts),
287 .postproc_ops = &hantro_g1_postproc_ops,
288 .codec = HANTRO_MPEG2_DECODER | HANTRO_VP8_DECODER |
290 .codec_ops = imx8mq_vpu_codec_ops,
291 .init = imx8mq_vpu_hw_init,
292 .runtime_resume = imx8mq_runtime_resume,
294 .num_irqs = ARRAY_SIZE(imx8mq_irqs),
295 .clk_names = imx8mq_clk_names,
296 .num_clocks = ARRAY_SIZE(imx8mq_clk_names),
297 .reg_names = imx8mq_reg_names,
298 .num_regs = ARRAY_SIZE(imx8mq_reg_names)
301 const struct hantro_variant imx8mq_vpu_g1_variant = {
302 .dec_fmts = imx8m_vpu_dec_fmts,
303 .num_dec_fmts = ARRAY_SIZE(imx8m_vpu_dec_fmts),
304 .postproc_fmts = imx8m_vpu_postproc_fmts,
305 .num_postproc_fmts = ARRAY_SIZE(imx8m_vpu_postproc_fmts),
306 .postproc_ops = &hantro_g1_postproc_ops,
307 .codec = HANTRO_MPEG2_DECODER | HANTRO_VP8_DECODER |
309 .codec_ops = imx8mq_vpu_g1_codec_ops,
311 .num_irqs = ARRAY_SIZE(imx8mq_irqs),
312 .clk_names = imx8mq_g1_clk_names,
313 .num_clocks = ARRAY_SIZE(imx8mq_g1_clk_names),
316 const struct hantro_variant imx8mq_vpu_g2_variant = {
318 .dec_fmts = imx8m_vpu_g2_dec_fmts,
319 .num_dec_fmts = ARRAY_SIZE(imx8m_vpu_g2_dec_fmts),
320 .postproc_fmts = imx8m_vpu_g2_postproc_fmts,
321 .num_postproc_fmts = ARRAY_SIZE(imx8m_vpu_g2_postproc_fmts),
322 .postproc_ops = &hantro_g2_postproc_ops,
323 .codec = HANTRO_HEVC_DECODER | HANTRO_VP9_DECODER,
324 .codec_ops = imx8mq_vpu_g2_codec_ops,
325 .irqs = imx8mq_g2_irqs,
326 .num_irqs = ARRAY_SIZE(imx8mq_g2_irqs),
327 .clk_names = imx8mq_g2_clk_names,
328 .num_clocks = ARRAY_SIZE(imx8mq_g2_clk_names),
331 const struct hantro_variant imx8mm_vpu_g1_variant = {
332 .dec_fmts = imx8m_vpu_dec_fmts,
333 .num_dec_fmts = ARRAY_SIZE(imx8m_vpu_dec_fmts),
334 .codec = HANTRO_MPEG2_DECODER | HANTRO_VP8_DECODER |
336 .codec_ops = imx8mq_vpu_g1_codec_ops,
338 .num_irqs = ARRAY_SIZE(imx8mq_irqs),
339 .clk_names = imx8mq_g1_clk_names,
340 .num_clocks = ARRAY_SIZE(imx8mq_g1_clk_names),