• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  * Authors:
5  *	YT Shen <yt.shen@mediatek.com>
6  *	CK Hu <ck.hu@mediatek.com>
7  */
8 
9 #include <linux/clk.h>
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/of_irq.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/soc/mediatek/mtk-cmdq.h>
16 #include <drm/drm_print.h>
17 
18 #include "mtk_drm_drv.h"
19 #include "mtk_drm_plane.h"
20 #include "mtk_drm_ddp_comp.h"
21 #include "mtk_drm_crtc.h"
22 
23 #define DISP_OD_EN				0x0000
24 #define DISP_OD_INTEN				0x0008
25 #define DISP_OD_INTSTA				0x000c
26 #define DISP_OD_CFG				0x0020
27 #define DISP_OD_SIZE				0x0030
28 #define DISP_DITHER_5				0x0114
29 #define DISP_DITHER_7				0x011c
30 #define DISP_DITHER_15				0x013c
31 #define DISP_DITHER_16				0x0140
32 
33 #define DISP_REG_UFO_START			0x0000
34 
35 #define DISP_AAL_EN				0x0000
36 #define DISP_AAL_SIZE				0x0030
37 #define DISP_AAL_OUTPUT_SIZE			0x04d8
38 
39 #define DISP_CCORR_EN				0x0000
40 #define CCORR_EN				BIT(0)
41 #define DISP_CCORR_CFG				0x0020
42 #define CCORR_RELAY_MODE			BIT(0)
43 #define CCORR_ENGINE_EN				BIT(1)
44 #define CCORR_GAMMA_OFF				BIT(2)
45 #define CCORR_WGAMUT_SRC_CLIP			BIT(3)
46 #define DISP_CCORR_SIZE				0x0030
47 #define DISP_CCORR_COEF_0			0x0080
48 #define DISP_CCORR_COEF_1			0x0084
49 #define DISP_CCORR_COEF_2			0x0088
50 #define DISP_CCORR_COEF_3			0x008C
51 #define DISP_CCORR_COEF_4			0x0090
52 
53 #define DISP_DITHER_EN				0x0000
54 #define DITHER_EN				BIT(0)
55 #define DISP_DITHER_CFG				0x0020
56 #define DITHER_RELAY_MODE			BIT(0)
57 #define DISP_DITHER_SIZE			0x0030
58 
59 #define DISP_GAMMA_EN				0x0000
60 #define DISP_GAMMA_CFG				0x0020
61 #define DISP_GAMMA_SIZE				0x0030
62 #define DISP_GAMMA_LUT				0x0700
63 
64 #define LUT_10BIT_MASK				0x03ff
65 
66 #define OD_RELAYMODE				BIT(0)
67 
68 #define UFO_BYPASS				BIT(2)
69 
70 #define AAL_EN					BIT(0)
71 
72 #define GAMMA_EN				BIT(0)
73 #define GAMMA_LUT_EN				BIT(1)
74 
75 #define DISP_DITHERING				BIT(2)
76 #define DITHER_LSB_ERR_SHIFT_R(x)		(((x) & 0x7) << 28)
77 #define DITHER_OVFLW_BIT_R(x)			(((x) & 0x7) << 24)
78 #define DITHER_ADD_LSHIFT_R(x)			(((x) & 0x7) << 20)
79 #define DITHER_ADD_RSHIFT_R(x)			(((x) & 0x7) << 16)
80 #define DITHER_NEW_BIT_MODE			BIT(0)
81 #define DITHER_LSB_ERR_SHIFT_B(x)		(((x) & 0x7) << 28)
82 #define DITHER_OVFLW_BIT_B(x)			(((x) & 0x7) << 24)
83 #define DITHER_ADD_LSHIFT_B(x)			(((x) & 0x7) << 20)
84 #define DITHER_ADD_RSHIFT_B(x)			(((x) & 0x7) << 16)
85 #define DITHER_LSB_ERR_SHIFT_G(x)		(((x) & 0x7) << 12)
86 #define DITHER_OVFLW_BIT_G(x)			(((x) & 0x7) << 8)
87 #define DITHER_ADD_LSHIFT_G(x)			(((x) & 0x7) << 4)
88 #define DITHER_ADD_RSHIFT_G(x)			(((x) & 0x7) << 0)
89 
mtk_ddp_write(struct cmdq_pkt * cmdq_pkt,unsigned int value,struct mtk_ddp_comp * comp,unsigned int offset)90 void mtk_ddp_write(struct cmdq_pkt *cmdq_pkt, unsigned int value,
91 		   struct mtk_ddp_comp *comp, unsigned int offset)
92 {
93 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
94 	if (cmdq_pkt)
95 		cmdq_pkt_write(cmdq_pkt, comp->subsys,
96 			       comp->regs_pa + offset, value);
97 	else
98 #endif
99 		writel(value, comp->regs + offset);
100 }
101 
mtk_ddp_write_relaxed(struct cmdq_pkt * cmdq_pkt,unsigned int value,struct mtk_ddp_comp * comp,unsigned int offset)102 void mtk_ddp_write_relaxed(struct cmdq_pkt *cmdq_pkt, unsigned int value,
103 			   struct mtk_ddp_comp *comp,
104 			   unsigned int offset)
105 {
106 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
107 	if (cmdq_pkt)
108 		cmdq_pkt_write(cmdq_pkt, comp->subsys,
109 			       comp->regs_pa + offset, value);
110 	else
111 #endif
112 		writel_relaxed(value, comp->regs + offset);
113 }
114 
mtk_ddp_write_mask(struct cmdq_pkt * cmdq_pkt,unsigned int value,struct mtk_ddp_comp * comp,unsigned int offset,unsigned int mask)115 void mtk_ddp_write_mask(struct cmdq_pkt *cmdq_pkt,
116 			unsigned int value,
117 			struct mtk_ddp_comp *comp,
118 			unsigned int offset,
119 			unsigned int mask)
120 {
121 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
122 	if (cmdq_pkt) {
123 		cmdq_pkt_write_mask(cmdq_pkt, comp->subsys,
124 				    comp->regs_pa + offset, value, mask);
125 	} else {
126 #endif
127 		u32 tmp = readl(comp->regs + offset);
128 
129 		tmp = (tmp & ~mask) | (value & mask);
130 		writel(tmp, comp->regs + offset);
131 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
132 	}
133 #endif
134 }
135 
mtk_dither_set(struct mtk_ddp_comp * comp,unsigned int bpc,unsigned int CFG,struct cmdq_pkt * cmdq_pkt)136 void mtk_dither_set(struct mtk_ddp_comp *comp, unsigned int bpc,
137 		    unsigned int CFG, struct cmdq_pkt *cmdq_pkt)
138 {
139 	/* If bpc equal to 0, the dithering function didn't be enabled */
140 	if (bpc == 0)
141 		return;
142 
143 	if (bpc >= MTK_MIN_BPC) {
144 		mtk_ddp_write(cmdq_pkt, 0, comp, DISP_DITHER_5);
145 		mtk_ddp_write(cmdq_pkt, 0, comp, DISP_DITHER_7);
146 		mtk_ddp_write(cmdq_pkt,
147 			      DITHER_LSB_ERR_SHIFT_R(MTK_MAX_BPC - bpc) |
148 			      DITHER_ADD_LSHIFT_R(MTK_MAX_BPC - bpc) |
149 			      DITHER_NEW_BIT_MODE,
150 			      comp, DISP_DITHER_15);
151 		mtk_ddp_write(cmdq_pkt,
152 			      DITHER_LSB_ERR_SHIFT_B(MTK_MAX_BPC - bpc) |
153 			      DITHER_ADD_LSHIFT_B(MTK_MAX_BPC - bpc) |
154 			      DITHER_LSB_ERR_SHIFT_G(MTK_MAX_BPC - bpc) |
155 			      DITHER_ADD_LSHIFT_G(MTK_MAX_BPC - bpc),
156 			      comp, DISP_DITHER_16);
157 		mtk_ddp_write(cmdq_pkt, DISP_DITHERING, comp, CFG);
158 	}
159 }
160 
mtk_od_config(struct mtk_ddp_comp * comp,unsigned int w,unsigned int h,unsigned int vrefresh,unsigned int bpc,struct cmdq_pkt * cmdq_pkt)161 static void mtk_od_config(struct mtk_ddp_comp *comp, unsigned int w,
162 			  unsigned int h, unsigned int vrefresh,
163 			  unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
164 {
165 	mtk_ddp_write(cmdq_pkt, w << 16 | h, comp, DISP_OD_SIZE);
166 	mtk_ddp_write(cmdq_pkt, OD_RELAYMODE, comp, DISP_OD_CFG);
167 	mtk_dither_set(comp, bpc, DISP_OD_CFG, cmdq_pkt);
168 }
169 
mtk_od_start(struct mtk_ddp_comp * comp)170 static void mtk_od_start(struct mtk_ddp_comp *comp)
171 {
172 	writel(1, comp->regs + DISP_OD_EN);
173 }
174 
mtk_ufoe_start(struct mtk_ddp_comp * comp)175 static void mtk_ufoe_start(struct mtk_ddp_comp *comp)
176 {
177 	writel(UFO_BYPASS, comp->regs + DISP_REG_UFO_START);
178 }
179 
mtk_aal_config(struct mtk_ddp_comp * comp,unsigned int w,unsigned int h,unsigned int vrefresh,unsigned int bpc,struct cmdq_pkt * cmdq_pkt)180 static void mtk_aal_config(struct mtk_ddp_comp *comp, unsigned int w,
181 			   unsigned int h, unsigned int vrefresh,
182 			   unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
183 {
184 	mtk_ddp_write(cmdq_pkt, w << 16 | h, comp, DISP_AAL_SIZE);
185 	mtk_ddp_write(cmdq_pkt, w << 16 | h, comp, DISP_AAL_OUTPUT_SIZE);
186 }
187 
mtk_aal_start(struct mtk_ddp_comp * comp)188 static void mtk_aal_start(struct mtk_ddp_comp *comp)
189 {
190 	writel(AAL_EN, comp->regs + DISP_AAL_EN);
191 }
192 
mtk_aal_stop(struct mtk_ddp_comp * comp)193 static void mtk_aal_stop(struct mtk_ddp_comp *comp)
194 {
195 	writel_relaxed(0x0, comp->regs + DISP_AAL_EN);
196 }
197 
mtk_ccorr_config(struct mtk_ddp_comp * comp,unsigned int w,unsigned int h,unsigned int vrefresh,unsigned int bpc,struct cmdq_pkt * cmdq_pkt)198 static void mtk_ccorr_config(struct mtk_ddp_comp *comp, unsigned int w,
199 			     unsigned int h, unsigned int vrefresh,
200 			     unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
201 {
202 	mtk_ddp_write(cmdq_pkt, h << 16 | w, comp, DISP_CCORR_SIZE);
203 	mtk_ddp_write(cmdq_pkt, CCORR_ENGINE_EN, comp, DISP_CCORR_CFG);
204 }
205 
mtk_ccorr_start(struct mtk_ddp_comp * comp)206 static void mtk_ccorr_start(struct mtk_ddp_comp *comp)
207 {
208 	writel(CCORR_EN, comp->regs + DISP_CCORR_EN);
209 }
210 
mtk_ccorr_stop(struct mtk_ddp_comp * comp)211 static void mtk_ccorr_stop(struct mtk_ddp_comp *comp)
212 {
213 	writel_relaxed(0x0, comp->regs + DISP_CCORR_EN);
214 }
215 
216 /* Converts a DRM S31.32 value to the HW S1.10 format. */
mtk_ctm_s31_32_to_s1_10(u64 in)217 static u16 mtk_ctm_s31_32_to_s1_10(u64 in)
218 {
219 	u16 r;
220 
221 	/* Sign bit. */
222 	r = in & BIT_ULL(63) ? BIT(11) : 0;
223 
224 	if ((in & GENMASK_ULL(62, 33)) > 0) {
225 		/* identity value 0x100000000 -> 0x400, */
226 		/* if bigger this, set it to max 0x7ff. */
227 		r |= GENMASK(10, 0);
228 	} else {
229 		/* take the 11 most important bits. */
230 		r |= (in >> 22) & GENMASK(10, 0);
231 	}
232 
233 	return r;
234 }
235 
mtk_ccorr_ctm_set(struct mtk_ddp_comp * comp,struct drm_crtc_state * state)236 static void mtk_ccorr_ctm_set(struct mtk_ddp_comp *comp,
237 			      struct drm_crtc_state *state)
238 {
239 	struct drm_property_blob *blob = state->ctm;
240 	struct drm_color_ctm *ctm;
241 	const u64 *input;
242 	uint16_t coeffs[9] = { 0 };
243 	int i;
244 	struct cmdq_pkt *cmdq_pkt = NULL;
245 
246 	if (!blob)
247 		return;
248 
249 	ctm = (struct drm_color_ctm *)blob->data;
250 	input = ctm->matrix;
251 
252 	for (i = 0; i < ARRAY_SIZE(coeffs); i++)
253 		coeffs[i] = mtk_ctm_s31_32_to_s1_10(input[i]);
254 
255 	mtk_ddp_write(cmdq_pkt, coeffs[0] << 16 | coeffs[1],
256 		      comp, DISP_CCORR_COEF_0);
257 	mtk_ddp_write(cmdq_pkt, coeffs[2] << 16 | coeffs[3],
258 		      comp, DISP_CCORR_COEF_1);
259 	mtk_ddp_write(cmdq_pkt, coeffs[4] << 16 | coeffs[5],
260 		      comp, DISP_CCORR_COEF_2);
261 	mtk_ddp_write(cmdq_pkt, coeffs[6] << 16 | coeffs[7],
262 		      comp, DISP_CCORR_COEF_3);
263 	mtk_ddp_write(cmdq_pkt, coeffs[8] << 16,
264 		      comp, DISP_CCORR_COEF_4);
265 }
266 
mtk_dither_config(struct mtk_ddp_comp * comp,unsigned int w,unsigned int h,unsigned int vrefresh,unsigned int bpc,struct cmdq_pkt * cmdq_pkt)267 static void mtk_dither_config(struct mtk_ddp_comp *comp, unsigned int w,
268 			      unsigned int h, unsigned int vrefresh,
269 			      unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
270 {
271 	mtk_ddp_write(cmdq_pkt, h << 16 | w, comp, DISP_DITHER_SIZE);
272 	mtk_ddp_write(cmdq_pkt, DITHER_RELAY_MODE, comp, DISP_DITHER_CFG);
273 }
274 
mtk_dither_start(struct mtk_ddp_comp * comp)275 static void mtk_dither_start(struct mtk_ddp_comp *comp)
276 {
277 	writel(DITHER_EN, comp->regs + DISP_DITHER_EN);
278 }
279 
mtk_dither_stop(struct mtk_ddp_comp * comp)280 static void mtk_dither_stop(struct mtk_ddp_comp *comp)
281 {
282 	writel_relaxed(0x0, comp->regs + DISP_DITHER_EN);
283 }
284 
mtk_gamma_config(struct mtk_ddp_comp * comp,unsigned int w,unsigned int h,unsigned int vrefresh,unsigned int bpc,struct cmdq_pkt * cmdq_pkt)285 static void mtk_gamma_config(struct mtk_ddp_comp *comp, unsigned int w,
286 			     unsigned int h, unsigned int vrefresh,
287 			     unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
288 {
289 	mtk_ddp_write(cmdq_pkt, h << 16 | w, comp, DISP_GAMMA_SIZE);
290 	mtk_dither_set(comp, bpc, DISP_GAMMA_CFG, cmdq_pkt);
291 }
292 
mtk_gamma_start(struct mtk_ddp_comp * comp)293 static void mtk_gamma_start(struct mtk_ddp_comp *comp)
294 {
295 	writel(GAMMA_EN, comp->regs  + DISP_GAMMA_EN);
296 }
297 
mtk_gamma_stop(struct mtk_ddp_comp * comp)298 static void mtk_gamma_stop(struct mtk_ddp_comp *comp)
299 {
300 	writel_relaxed(0x0, comp->regs  + DISP_GAMMA_EN);
301 }
302 
mtk_gamma_set(struct mtk_ddp_comp * comp,struct drm_crtc_state * state)303 static void mtk_gamma_set(struct mtk_ddp_comp *comp,
304 			  struct drm_crtc_state *state)
305 {
306 	unsigned int i, reg;
307 	struct drm_color_lut *lut;
308 	void __iomem *lut_base;
309 	u32 word;
310 
311 	if (state->gamma_lut) {
312 		reg = readl(comp->regs + DISP_GAMMA_CFG);
313 		reg = reg | GAMMA_LUT_EN;
314 		writel(reg, comp->regs + DISP_GAMMA_CFG);
315 		lut_base = comp->regs + DISP_GAMMA_LUT;
316 		lut = (struct drm_color_lut *)state->gamma_lut->data;
317 		for (i = 0; i < MTK_LUT_SIZE; i++) {
318 			word = (((lut[i].red >> 6) & LUT_10BIT_MASK) << 20) +
319 				(((lut[i].green >> 6) & LUT_10BIT_MASK) << 10) +
320 				((lut[i].blue >> 6) & LUT_10BIT_MASK);
321 			writel(word, (lut_base + i * 4));
322 		}
323 	}
324 }
325 
326 static const struct mtk_ddp_comp_funcs ddp_aal = {
327 	.gamma_set = mtk_gamma_set,
328 	.config = mtk_aal_config,
329 	.start = mtk_aal_start,
330 	.stop = mtk_aal_stop,
331 };
332 
333 static const struct mtk_ddp_comp_funcs ddp_ccorr = {
334 	.config = mtk_ccorr_config,
335 	.start = mtk_ccorr_start,
336 	.stop = mtk_ccorr_stop,
337 	.ctm_set = mtk_ccorr_ctm_set,
338 };
339 
340 static const struct mtk_ddp_comp_funcs ddp_dither = {
341 	.config = mtk_dither_config,
342 	.start = mtk_dither_start,
343 	.stop = mtk_dither_stop,
344 };
345 
346 static const struct mtk_ddp_comp_funcs ddp_gamma = {
347 	.gamma_set = mtk_gamma_set,
348 	.config = mtk_gamma_config,
349 	.start = mtk_gamma_start,
350 	.stop = mtk_gamma_stop,
351 };
352 
353 static const struct mtk_ddp_comp_funcs ddp_od = {
354 	.config = mtk_od_config,
355 	.start = mtk_od_start,
356 };
357 
358 static const struct mtk_ddp_comp_funcs ddp_ufoe = {
359 	.start = mtk_ufoe_start,
360 };
361 
362 static const char * const mtk_ddp_comp_stem[MTK_DDP_COMP_TYPE_MAX] = {
363 	[MTK_DISP_OVL] = "ovl",
364 	[MTK_DISP_OVL_2L] = "ovl-2l",
365 	[MTK_DISP_RDMA] = "rdma",
366 	[MTK_DISP_WDMA] = "wdma",
367 	[MTK_DISP_COLOR] = "color",
368 	[MTK_DISP_CCORR] = "ccorr",
369 	[MTK_DISP_AAL] = "aal",
370 	[MTK_DISP_GAMMA] = "gamma",
371 	[MTK_DISP_DITHER] = "dither",
372 	[MTK_DISP_UFOE] = "ufoe",
373 	[MTK_DSI] = "dsi",
374 	[MTK_DPI] = "dpi",
375 	[MTK_DISP_PWM] = "pwm",
376 	[MTK_DISP_MUTEX] = "mutex",
377 	[MTK_DISP_OD] = "od",
378 	[MTK_DISP_BLS] = "bls",
379 };
380 
381 struct mtk_ddp_comp_match {
382 	enum mtk_ddp_comp_type type;
383 	int alias_id;
384 	const struct mtk_ddp_comp_funcs *funcs;
385 };
386 
387 static const struct mtk_ddp_comp_match mtk_ddp_matches[DDP_COMPONENT_ID_MAX] = {
388 	[DDP_COMPONENT_AAL0]	= { MTK_DISP_AAL,	0, &ddp_aal },
389 	[DDP_COMPONENT_AAL1]	= { MTK_DISP_AAL,	1, &ddp_aal },
390 	[DDP_COMPONENT_BLS]	= { MTK_DISP_BLS,	0, NULL },
391 	[DDP_COMPONENT_CCORR]	= { MTK_DISP_CCORR,	0, &ddp_ccorr },
392 	[DDP_COMPONENT_COLOR0]	= { MTK_DISP_COLOR,	0, NULL },
393 	[DDP_COMPONENT_COLOR1]	= { MTK_DISP_COLOR,	1, NULL },
394 	[DDP_COMPONENT_DITHER]	= { MTK_DISP_DITHER,	0, &ddp_dither },
395 	[DDP_COMPONENT_DPI0]	= { MTK_DPI,		0, NULL },
396 	[DDP_COMPONENT_DPI1]	= { MTK_DPI,		1, NULL },
397 	[DDP_COMPONENT_DSI0]	= { MTK_DSI,		0, NULL },
398 	[DDP_COMPONENT_DSI1]	= { MTK_DSI,		1, NULL },
399 	[DDP_COMPONENT_DSI2]	= { MTK_DSI,		2, NULL },
400 	[DDP_COMPONENT_DSI3]	= { MTK_DSI,		3, NULL },
401 	[DDP_COMPONENT_GAMMA]	= { MTK_DISP_GAMMA,	0, &ddp_gamma },
402 	[DDP_COMPONENT_OD0]	= { MTK_DISP_OD,	0, &ddp_od },
403 	[DDP_COMPONENT_OD1]	= { MTK_DISP_OD,	1, &ddp_od },
404 	[DDP_COMPONENT_OVL0]	= { MTK_DISP_OVL,	0, NULL },
405 	[DDP_COMPONENT_OVL1]	= { MTK_DISP_OVL,	1, NULL },
406 	[DDP_COMPONENT_OVL_2L0]	= { MTK_DISP_OVL_2L,	0, NULL },
407 	[DDP_COMPONENT_OVL_2L1]	= { MTK_DISP_OVL_2L,	1, NULL },
408 	[DDP_COMPONENT_PWM0]	= { MTK_DISP_PWM,	0, NULL },
409 	[DDP_COMPONENT_PWM1]	= { MTK_DISP_PWM,	1, NULL },
410 	[DDP_COMPONENT_PWM2]	= { MTK_DISP_PWM,	2, NULL },
411 	[DDP_COMPONENT_RDMA0]	= { MTK_DISP_RDMA,	0, NULL },
412 	[DDP_COMPONENT_RDMA1]	= { MTK_DISP_RDMA,	1, NULL },
413 	[DDP_COMPONENT_RDMA2]	= { MTK_DISP_RDMA,	2, NULL },
414 	[DDP_COMPONENT_UFOE]	= { MTK_DISP_UFOE,	0, &ddp_ufoe },
415 	[DDP_COMPONENT_WDMA0]	= { MTK_DISP_WDMA,	0, NULL },
416 	[DDP_COMPONENT_WDMA1]	= { MTK_DISP_WDMA,	1, NULL },
417 };
418 
mtk_drm_find_comp_in_ddp(struct mtk_ddp_comp ddp_comp,const enum mtk_ddp_comp_id * path,unsigned int path_len)419 static bool mtk_drm_find_comp_in_ddp(struct mtk_ddp_comp ddp_comp,
420 				     const enum mtk_ddp_comp_id *path,
421 				     unsigned int path_len)
422 {
423 	unsigned int i;
424 
425 	if (path == NULL)
426 		return false;
427 
428 	for (i = 0U; i < path_len; i++)
429 		if (ddp_comp.id == path[i])
430 			return true;
431 
432 	return false;
433 }
434 
mtk_ddp_comp_get_id(struct device_node * node,enum mtk_ddp_comp_type comp_type)435 int mtk_ddp_comp_get_id(struct device_node *node,
436 			enum mtk_ddp_comp_type comp_type)
437 {
438 	int id = of_alias_get_id(node, mtk_ddp_comp_stem[comp_type]);
439 	int i;
440 
441 	for (i = 0; i < ARRAY_SIZE(mtk_ddp_matches); i++) {
442 		if (comp_type == mtk_ddp_matches[i].type &&
443 		    (id < 0 || id == mtk_ddp_matches[i].alias_id))
444 			return i;
445 	}
446 
447 	return -EINVAL;
448 }
449 
mtk_drm_find_possible_crtc_by_comp(struct drm_device * drm,struct mtk_ddp_comp ddp_comp)450 unsigned int mtk_drm_find_possible_crtc_by_comp(struct drm_device *drm,
451 						struct mtk_ddp_comp ddp_comp)
452 {
453 	struct mtk_drm_private *private = drm->dev_private;
454 	unsigned int ret = 0;
455 
456 	if (mtk_drm_find_comp_in_ddp(ddp_comp, private->data->main_path, private->data->main_len))
457 		ret = BIT(0);
458 	else if (mtk_drm_find_comp_in_ddp(ddp_comp, private->data->ext_path,
459 					  private->data->ext_len))
460 		ret = BIT(1);
461 	else if (mtk_drm_find_comp_in_ddp(ddp_comp, private->data->third_path,
462 					  private->data->third_len))
463 		ret = BIT(2);
464 	else
465 		DRM_INFO("Failed to find comp in ddp table\n");
466 
467 	return ret;
468 }
469 
mtk_ddp_comp_init(struct device * dev,struct device_node * node,struct mtk_ddp_comp * comp,enum mtk_ddp_comp_id comp_id,const struct mtk_ddp_comp_funcs * funcs)470 int mtk_ddp_comp_init(struct device *dev, struct device_node *node,
471 		      struct mtk_ddp_comp *comp, enum mtk_ddp_comp_id comp_id,
472 		      const struct mtk_ddp_comp_funcs *funcs)
473 {
474 	enum mtk_ddp_comp_type type;
475 	struct device_node *larb_node;
476 	struct platform_device *larb_pdev;
477 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
478 	struct resource res;
479 	struct cmdq_client_reg cmdq_reg;
480 	int ret;
481 #endif
482 
483 	if (comp_id < 0 || comp_id >= DDP_COMPONENT_ID_MAX)
484 		return -EINVAL;
485 
486 	type = mtk_ddp_matches[comp_id].type;
487 
488 	comp->id = comp_id;
489 	comp->funcs = funcs ?: mtk_ddp_matches[comp_id].funcs;
490 
491 	if (comp_id == DDP_COMPONENT_BLS ||
492 	    comp_id == DDP_COMPONENT_DPI0 ||
493 	    comp_id == DDP_COMPONENT_DPI1 ||
494 	    comp_id == DDP_COMPONENT_DSI0 ||
495 	    comp_id == DDP_COMPONENT_DSI1 ||
496 	    comp_id == DDP_COMPONENT_DSI2 ||
497 	    comp_id == DDP_COMPONENT_DSI3 ||
498 	    comp_id == DDP_COMPONENT_PWM0) {
499 		comp->regs = NULL;
500 		comp->clk = NULL;
501 		comp->irq = 0;
502 		return 0;
503 	}
504 
505 	comp->regs = of_iomap(node, 0);
506 	comp->irq = of_irq_get(node, 0);
507 	comp->clk = of_clk_get(node, 0);
508 	if (IS_ERR(comp->clk))
509 		return PTR_ERR(comp->clk);
510 
511 	/* Only DMA capable components need the LARB property */
512 	comp->larb_dev = NULL;
513 	if (type != MTK_DISP_OVL &&
514 	    type != MTK_DISP_OVL_2L &&
515 	    type != MTK_DISP_RDMA &&
516 	    type != MTK_DISP_WDMA)
517 		return 0;
518 
519 	larb_node = of_parse_phandle(node, "mediatek,larb", 0);
520 	if (!larb_node) {
521 		dev_err(dev,
522 			"Missing mediadek,larb phandle in %pOF node\n", node);
523 		return -EINVAL;
524 	}
525 
526 	larb_pdev = of_find_device_by_node(larb_node);
527 	if (!larb_pdev) {
528 		dev_warn(dev, "Waiting for larb device %pOF\n", larb_node);
529 		of_node_put(larb_node);
530 		return -EPROBE_DEFER;
531 	}
532 	of_node_put(larb_node);
533 
534 	comp->larb_dev = &larb_pdev->dev;
535 
536 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
537 	if (of_address_to_resource(node, 0, &res) != 0) {
538 		dev_err(dev, "Missing reg in %s node\n", node->full_name);
539 		put_device(&larb_pdev->dev);
540 		return -EINVAL;
541 	}
542 	comp->regs_pa = res.start;
543 
544 	ret = cmdq_dev_get_client_reg(dev, &cmdq_reg, 0);
545 	if (ret)
546 		dev_dbg(dev, "get mediatek,gce-client-reg fail!\n");
547 	else
548 		comp->subsys = cmdq_reg.subsys;
549 #endif
550 	return 0;
551 }
552 
mtk_ddp_comp_register(struct drm_device * drm,struct mtk_ddp_comp * comp)553 int mtk_ddp_comp_register(struct drm_device *drm, struct mtk_ddp_comp *comp)
554 {
555 	struct mtk_drm_private *private = drm->dev_private;
556 
557 	if (private->ddp_comp[comp->id])
558 		return -EBUSY;
559 
560 	private->ddp_comp[comp->id] = comp;
561 	return 0;
562 }
563 
mtk_ddp_comp_unregister(struct drm_device * drm,struct mtk_ddp_comp * comp)564 void mtk_ddp_comp_unregister(struct drm_device *drm, struct mtk_ddp_comp *comp)
565 {
566 	struct mtk_drm_private *private = drm->dev_private;
567 
568 	private->ddp_comp[comp->id] = NULL;
569 }
570