1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Hantro VPU codec driver
4  *
5  * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
6  */
7 
8 #include "hantro.h"
9 
10 static const u8 zigzag[64] = {
11 	0,   1,  8, 16,  9,  2,  3, 10,
12 	17, 24, 32, 25, 18, 11,  4,  5,
13 	12, 19, 26, 33, 40, 48, 41, 34,
14 	27, 20, 13,  6,  7, 14, 21, 28,
15 	35, 42, 49, 56, 57, 50, 43, 36,
16 	29, 22, 15, 23, 30, 37, 44, 51,
17 	58, 59, 52, 45, 38, 31, 39, 46,
18 	53, 60, 61, 54, 47, 55, 62, 63
19 };
20 
21 void hantro_mpeg2_dec_copy_qtable(u8 *qtable,
22 				  const struct v4l2_ctrl_mpeg2_quantisation *ctrl)
23 {
24 	int i, n;
25 
26 	if (!qtable || !ctrl)
27 		return;
28 
29 	for (i = 0; i < ARRAY_SIZE(zigzag); i++) {
30 		n = zigzag[i];
31 		qtable[n + 0] = ctrl->intra_quantiser_matrix[i];
32 		qtable[n + 64] = ctrl->non_intra_quantiser_matrix[i];
33 		qtable[n + 128] = ctrl->chroma_intra_quantiser_matrix[i];
34 		qtable[n + 192] = ctrl->chroma_non_intra_quantiser_matrix[i];
35 	}
36 }
37 
38 int hantro_mpeg2_dec_init(struct hantro_ctx *ctx)
39 {
40 	struct hantro_dev *vpu = ctx->dev;
41 
42 	ctx->mpeg2_dec.qtable.size = ARRAY_SIZE(zigzag) * 4;
43 	ctx->mpeg2_dec.qtable.cpu =
44 		dma_alloc_coherent(vpu->dev,
45 				   ctx->mpeg2_dec.qtable.size,
46 				   &ctx->mpeg2_dec.qtable.dma,
47 				   GFP_KERNEL);
48 	if (!ctx->mpeg2_dec.qtable.cpu)
49 		return -ENOMEM;
50 	return 0;
51 }
52 
53 void hantro_mpeg2_dec_exit(struct hantro_ctx *ctx)
54 {
55 	struct hantro_dev *vpu = ctx->dev;
56 
57 	dma_free_coherent(vpu->dev,
58 			  ctx->mpeg2_dec.qtable.size,
59 			  ctx->mpeg2_dec.qtable.cpu,
60 			  ctx->mpeg2_dec.qtable.dma);
61 }
62