1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015-2016 MediaTek Inc.
4  * Author: Houlong Wei <houlong.wei@mediatek.com>
5  *         Ming Hsiu Tsai <minghsiu.tsai@mediatek.com>
6  */
7 
8 #include <linux/platform_device.h>
9 
10 #include "mtk_mdp_core.h"
11 #include "mtk_mdp_regs.h"
12 
13 
14 #define MDP_COLORFMT_PACK(VIDEO, PLANE, COPLANE, HF, VF, BITS, GROUP, SWAP, ID)\
15 	(((VIDEO) << 27) | ((PLANE) << 24) | ((COPLANE) << 22) |\
16 	((HF) << 20) | ((VF) << 18) | ((BITS) << 8) | ((GROUP) << 6) |\
17 	((SWAP) << 5) | ((ID) << 0))
18 
19 enum MDP_COLOR_ENUM {
20 	MDP_COLOR_UNKNOWN = 0,
21 	MDP_COLOR_NV12 = MDP_COLORFMT_PACK(0, 2, 1, 1, 1, 8, 1, 0, 12),
22 	MDP_COLOR_I420 = MDP_COLORFMT_PACK(0, 3, 0, 1, 1, 8, 1, 0, 8),
23 	MDP_COLOR_YV12 = MDP_COLORFMT_PACK(0, 3, 0, 1, 1, 8, 1, 1, 8),
24 	/* Mediatek proprietary format */
25 	MDP_COLOR_420_MT21 = MDP_COLORFMT_PACK(5, 2, 1, 1, 1, 256, 1, 0, 12),
26 };
27 
28 static int32_t mtk_mdp_map_color_format(int v4l2_format)
29 {
30 	switch (v4l2_format) {
31 	case V4L2_PIX_FMT_NV12M:
32 	case V4L2_PIX_FMT_NV12:
33 		return MDP_COLOR_NV12;
34 	case V4L2_PIX_FMT_MT21C:
35 		return MDP_COLOR_420_MT21;
36 	case V4L2_PIX_FMT_YUV420M:
37 	case V4L2_PIX_FMT_YUV420:
38 		return MDP_COLOR_I420;
39 	case V4L2_PIX_FMT_YVU420:
40 		return MDP_COLOR_YV12;
41 	}
42 
43 	mtk_mdp_err("Unknown format 0x%x", v4l2_format);
44 
45 	return MDP_COLOR_UNKNOWN;
46 }
47 
48 void mtk_mdp_hw_set_input_addr(struct mtk_mdp_ctx *ctx,
49 			       struct mtk_mdp_addr *addr)
50 {
51 	struct mdp_buffer *src_buf = &ctx->vpu.vsi->src_buffer;
52 	int i;
53 
54 	for (i = 0; i < ARRAY_SIZE(addr->addr); i++)
55 		src_buf->addr_mva[i] = (uint64_t)addr->addr[i];
56 }
57 
58 void mtk_mdp_hw_set_output_addr(struct mtk_mdp_ctx *ctx,
59 				struct mtk_mdp_addr *addr)
60 {
61 	struct mdp_buffer *dst_buf = &ctx->vpu.vsi->dst_buffer;
62 	int i;
63 
64 	for (i = 0; i < ARRAY_SIZE(addr->addr); i++)
65 		dst_buf->addr_mva[i] = (uint64_t)addr->addr[i];
66 }
67 
68 void mtk_mdp_hw_set_in_size(struct mtk_mdp_ctx *ctx)
69 {
70 	struct mtk_mdp_frame *frame = &ctx->s_frame;
71 	struct mdp_config *config = &ctx->vpu.vsi->src_config;
72 
73 	/* Set input pixel offset */
74 	config->crop_x = frame->crop.left;
75 	config->crop_y = frame->crop.top;
76 
77 	/* Set input cropped size */
78 	config->crop_w = frame->crop.width;
79 	config->crop_h = frame->crop.height;
80 
81 	/* Set input original size */
82 	config->x = 0;
83 	config->y = 0;
84 	config->w = frame->width;
85 	config->h = frame->height;
86 }
87 
88 void mtk_mdp_hw_set_in_image_format(struct mtk_mdp_ctx *ctx)
89 {
90 	unsigned int i;
91 	struct mtk_mdp_frame *frame = &ctx->s_frame;
92 	struct mdp_config *config = &ctx->vpu.vsi->src_config;
93 	struct mdp_buffer *src_buf = &ctx->vpu.vsi->src_buffer;
94 
95 	src_buf->plane_num = frame->fmt->num_comp;
96 	config->format = mtk_mdp_map_color_format(frame->fmt->pixelformat);
97 	config->w_stride = 0; /* MDP will calculate it by color format. */
98 	config->h_stride = 0; /* MDP will calculate it by color format. */
99 
100 	for (i = 0; i < src_buf->plane_num; i++)
101 		src_buf->plane_size[i] = frame->payload[i];
102 }
103 
104 void mtk_mdp_hw_set_out_size(struct mtk_mdp_ctx *ctx)
105 {
106 	struct mtk_mdp_frame *frame = &ctx->d_frame;
107 	struct mdp_config *config = &ctx->vpu.vsi->dst_config;
108 
109 	config->crop_x = frame->crop.left;
110 	config->crop_y = frame->crop.top;
111 	config->crop_w = frame->crop.width;
112 	config->crop_h = frame->crop.height;
113 	config->x = 0;
114 	config->y = 0;
115 	config->w = frame->width;
116 	config->h = frame->height;
117 }
118 
119 void mtk_mdp_hw_set_out_image_format(struct mtk_mdp_ctx *ctx)
120 {
121 	unsigned int i;
122 	struct mtk_mdp_frame *frame = &ctx->d_frame;
123 	struct mdp_config *config = &ctx->vpu.vsi->dst_config;
124 	struct mdp_buffer *dst_buf = &ctx->vpu.vsi->dst_buffer;
125 
126 	dst_buf->plane_num = frame->fmt->num_comp;
127 	config->format = mtk_mdp_map_color_format(frame->fmt->pixelformat);
128 	config->w_stride = 0; /* MDP will calculate it by color format. */
129 	config->h_stride = 0; /* MDP will calculate it by color format. */
130 	for (i = 0; i < dst_buf->plane_num; i++)
131 		dst_buf->plane_size[i] = frame->payload[i];
132 }
133 
134 void mtk_mdp_hw_set_rotation(struct mtk_mdp_ctx *ctx)
135 {
136 	struct mdp_config_misc *misc = &ctx->vpu.vsi->misc;
137 
138 	misc->orientation = ctx->ctrls.rotate->val;
139 	misc->hflip = ctx->ctrls.hflip->val;
140 	misc->vflip = ctx->ctrls.vflip->val;
141 }
142 
143 void mtk_mdp_hw_set_global_alpha(struct mtk_mdp_ctx *ctx)
144 {
145 	struct mdp_config_misc *misc = &ctx->vpu.vsi->misc;
146 
147 	misc->alpha = ctx->ctrls.global_alpha->val;
148 }
149