xref: /linux/drivers/gpu/drm/mediatek/mtk_disp_ovl_adaptor.c (revision 269ce3bd62e8ad83dadc80a2f755a799697ca4a3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021 MediaTek Inc.
4  */
5 
6 #include <drm/drm_fourcc.h>
7 #include <drm/drm_of.h>
8 #include <linux/clk.h>
9 #include <linux/component.h>
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/reset.h>
16 #include <linux/soc/mediatek/mtk-cmdq.h>
17 #include <linux/soc/mediatek/mtk-mmsys.h>
18 #include <linux/soc/mediatek/mtk-mutex.h>
19 
20 #include "mtk_ddp_comp.h"
21 #include "mtk_disp_drv.h"
22 #include "mtk_drm_drv.h"
23 #include "mtk_ethdr.h"
24 
25 #define MTK_OVL_ADAPTOR_RDMA_MAX_WIDTH 1920
26 #define MTK_OVL_ADAPTOR_LAYER_NUM 4
27 
28 enum mtk_ovl_adaptor_comp_type {
29 	OVL_ADAPTOR_TYPE_ETHDR,
30 	OVL_ADAPTOR_TYPE_MDP_RDMA,
31 	OVL_ADAPTOR_TYPE_MERGE,
32 	OVL_ADAPTOR_TYPE_PADDING,
33 	OVL_ADAPTOR_TYPE_NUM,
34 };
35 
36 enum mtk_ovl_adaptor_comp_id {
37 	OVL_ADAPTOR_ETHDR0,
38 	OVL_ADAPTOR_MDP_RDMA0,
39 	OVL_ADAPTOR_MDP_RDMA1,
40 	OVL_ADAPTOR_MDP_RDMA2,
41 	OVL_ADAPTOR_MDP_RDMA3,
42 	OVL_ADAPTOR_MDP_RDMA4,
43 	OVL_ADAPTOR_MDP_RDMA5,
44 	OVL_ADAPTOR_MDP_RDMA6,
45 	OVL_ADAPTOR_MDP_RDMA7,
46 	OVL_ADAPTOR_MERGE0,
47 	OVL_ADAPTOR_MERGE1,
48 	OVL_ADAPTOR_MERGE2,
49 	OVL_ADAPTOR_MERGE3,
50 	OVL_ADAPTOR_PADDING0,
51 	OVL_ADAPTOR_PADDING1,
52 	OVL_ADAPTOR_PADDING2,
53 	OVL_ADAPTOR_PADDING3,
54 	OVL_ADAPTOR_PADDING4,
55 	OVL_ADAPTOR_PADDING5,
56 	OVL_ADAPTOR_PADDING6,
57 	OVL_ADAPTOR_PADDING7,
58 	OVL_ADAPTOR_ID_MAX
59 };
60 
61 struct ovl_adaptor_comp_match {
62 	enum mtk_ovl_adaptor_comp_type type;
63 	enum mtk_ddp_comp_id comp_id;
64 	int alias_id;
65 	const struct mtk_ddp_comp_funcs *funcs;
66 };
67 
68 struct mtk_disp_ovl_adaptor {
69 	struct device *ovl_adaptor_comp[OVL_ADAPTOR_ID_MAX];
70 	struct device *mmsys_dev;
71 	bool children_bound;
72 };
73 
74 static const char * const private_comp_stem[OVL_ADAPTOR_TYPE_NUM] = {
75 	[OVL_ADAPTOR_TYPE_ETHDR]	= "ethdr",
76 	[OVL_ADAPTOR_TYPE_MDP_RDMA]	= "vdo1-rdma",
77 	[OVL_ADAPTOR_TYPE_MERGE]	= "merge",
78 	[OVL_ADAPTOR_TYPE_PADDING]	= "padding",
79 };
80 
81 static const struct mtk_ddp_comp_funcs ethdr = {
82 	.clk_enable = mtk_ethdr_clk_enable,
83 	.clk_disable = mtk_ethdr_clk_disable,
84 	.start = mtk_ethdr_start,
85 	.stop = mtk_ethdr_stop,
86 };
87 
88 static const struct mtk_ddp_comp_funcs merge = {
89 	.clk_enable = mtk_merge_clk_enable,
90 	.clk_disable = mtk_merge_clk_disable,
91 	.mode_valid = mtk_merge_mode_valid,
92 };
93 
94 static const struct mtk_ddp_comp_funcs padding = {
95 	.clk_enable = mtk_padding_clk_enable,
96 	.clk_disable = mtk_padding_clk_disable,
97 	.start = mtk_padding_start,
98 	.stop = mtk_padding_stop,
99 };
100 
101 static const struct mtk_ddp_comp_funcs rdma = {
102 	.power_on = mtk_mdp_rdma_power_on,
103 	.power_off = mtk_mdp_rdma_power_off,
104 	.clk_enable = mtk_mdp_rdma_clk_enable,
105 	.clk_disable = mtk_mdp_rdma_clk_disable,
106 };
107 
108 static const struct ovl_adaptor_comp_match comp_matches[OVL_ADAPTOR_ID_MAX] = {
109 	[OVL_ADAPTOR_ETHDR0] = { OVL_ADAPTOR_TYPE_ETHDR, DDP_COMPONENT_ETHDR_MIXER, 0, &ethdr },
110 	[OVL_ADAPTOR_MDP_RDMA0] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA0, 0, &rdma },
111 	[OVL_ADAPTOR_MDP_RDMA1] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA1, 1, &rdma },
112 	[OVL_ADAPTOR_MDP_RDMA2] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA2, 2, &rdma },
113 	[OVL_ADAPTOR_MDP_RDMA3] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA3, 3, &rdma },
114 	[OVL_ADAPTOR_MDP_RDMA4] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA4, 4, &rdma },
115 	[OVL_ADAPTOR_MDP_RDMA5] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA5, 5, &rdma },
116 	[OVL_ADAPTOR_MDP_RDMA6] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA6, 6, &rdma },
117 	[OVL_ADAPTOR_MDP_RDMA7] = { OVL_ADAPTOR_TYPE_MDP_RDMA, DDP_COMPONENT_MDP_RDMA7, 7, &rdma },
118 	[OVL_ADAPTOR_MERGE0] = { OVL_ADAPTOR_TYPE_MERGE, DDP_COMPONENT_MERGE1, 1, &merge },
119 	[OVL_ADAPTOR_MERGE1] = { OVL_ADAPTOR_TYPE_MERGE, DDP_COMPONENT_MERGE2, 2, &merge },
120 	[OVL_ADAPTOR_MERGE2] = { OVL_ADAPTOR_TYPE_MERGE, DDP_COMPONENT_MERGE3, 3, &merge },
121 	[OVL_ADAPTOR_MERGE3] = { OVL_ADAPTOR_TYPE_MERGE, DDP_COMPONENT_MERGE4, 4, &merge },
122 	[OVL_ADAPTOR_PADDING0] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING0, 0, &padding },
123 	[OVL_ADAPTOR_PADDING1] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING1, 1, &padding },
124 	[OVL_ADAPTOR_PADDING2] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING2, 2, &padding },
125 	[OVL_ADAPTOR_PADDING3] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING3, 3, &padding },
126 	[OVL_ADAPTOR_PADDING4] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING4, 4, &padding },
127 	[OVL_ADAPTOR_PADDING5] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING5, 5, &padding },
128 	[OVL_ADAPTOR_PADDING6] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING6, 6, &padding },
129 	[OVL_ADAPTOR_PADDING7] = { OVL_ADAPTOR_TYPE_PADDING, DDP_COMPONENT_PADDING7, 7, &padding },
130 };
131 
mtk_ovl_adaptor_layer_config(struct device * dev,unsigned int idx,struct mtk_plane_state * state,struct cmdq_pkt * cmdq_pkt)132 void mtk_ovl_adaptor_layer_config(struct device *dev, unsigned int idx,
133 				  struct mtk_plane_state *state,
134 				  struct cmdq_pkt *cmdq_pkt)
135 {
136 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
137 	struct mtk_plane_pending_state *pending = &state->pending;
138 	struct mtk_mdp_rdma_cfg rdma_config = {0};
139 	struct device *rdma_l;
140 	struct device *rdma_r;
141 	struct device *merge;
142 	struct device *ethdr;
143 	const struct drm_format_info *fmt_info = drm_format_info(pending->format);
144 	bool use_dual_pipe = false;
145 	unsigned int align_width;
146 	unsigned int l_w = 0;
147 	unsigned int r_w = 0;
148 
149 	dev_dbg(dev, "%s+ idx:%d, enable:%d, fmt:0x%x\n", __func__, idx,
150 		pending->enable, pending->format);
151 	dev_dbg(dev, "addr 0x%pad, fb w:%d, {%d,%d,%d,%d}\n",
152 		&pending->addr, (pending->pitch / fmt_info->cpp[0]),
153 		pending->x, pending->y, pending->width, pending->height);
154 
155 	rdma_l = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0 + 2 * idx];
156 	rdma_r = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0 + 2 * idx + 1];
157 	merge = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MERGE0 + idx];
158 	ethdr = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0];
159 
160 	if (!pending->enable || !pending->width || !pending->height) {
161 		mtk_merge_stop_cmdq(merge, cmdq_pkt);
162 		mtk_mdp_rdma_stop(rdma_l, cmdq_pkt);
163 		mtk_mdp_rdma_stop(rdma_r, cmdq_pkt);
164 		mtk_ethdr_layer_config(ethdr, idx, state, cmdq_pkt);
165 		return;
166 	}
167 
168 	/* ETHDR is in 1T2P domain, width needs to be 2 pixels align */
169 	align_width = ALIGN_DOWN(pending->width, 2);
170 
171 	if (align_width > MTK_OVL_ADAPTOR_RDMA_MAX_WIDTH)
172 		use_dual_pipe = true;
173 
174 	if (use_dual_pipe) {
175 		l_w = (align_width / 2) + ((pending->width / 2) % 2);
176 		r_w = align_width - l_w;
177 	} else {
178 		l_w = align_width;
179 	}
180 	mtk_merge_advance_config(merge, l_w, r_w, pending->height, 0, 0, cmdq_pkt);
181 	mtk_mmsys_merge_async_config(ovl_adaptor->mmsys_dev, idx, align_width / 2,
182 				     pending->height, cmdq_pkt);
183 
184 	rdma_config.width = l_w;
185 	rdma_config.height = pending->height;
186 	rdma_config.addr0 = pending->addr;
187 	rdma_config.pitch = pending->pitch;
188 	rdma_config.fmt = pending->format;
189 	rdma_config.color_encoding = pending->color_encoding;
190 	mtk_mdp_rdma_config(rdma_l, &rdma_config, cmdq_pkt);
191 
192 	if (use_dual_pipe) {
193 		rdma_config.x_left = l_w;
194 		rdma_config.width = r_w;
195 		mtk_mdp_rdma_config(rdma_r, &rdma_config, cmdq_pkt);
196 	}
197 
198 	mtk_merge_start_cmdq(merge, cmdq_pkt);
199 
200 	mtk_mdp_rdma_start(rdma_l, cmdq_pkt);
201 	if (use_dual_pipe)
202 		mtk_mdp_rdma_start(rdma_r, cmdq_pkt);
203 	else
204 		mtk_mdp_rdma_stop(rdma_r, cmdq_pkt);
205 
206 	mtk_ethdr_layer_config(ethdr, idx, state, cmdq_pkt);
207 }
208 
mtk_ovl_adaptor_config(struct device * dev,unsigned int w,unsigned int h,unsigned int vrefresh,unsigned int bpc,struct cmdq_pkt * cmdq_pkt)209 void mtk_ovl_adaptor_config(struct device *dev, unsigned int w,
210 			    unsigned int h, unsigned int vrefresh,
211 			    unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
212 {
213 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
214 
215 	mtk_ethdr_config(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0], w, h,
216 			 vrefresh, bpc, cmdq_pkt);
217 }
218 
mtk_ovl_adaptor_start(struct device * dev)219 void mtk_ovl_adaptor_start(struct device *dev)
220 {
221 	int i;
222 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
223 
224 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
225 		if (!ovl_adaptor->ovl_adaptor_comp[i] ||
226 		    !comp_matches[i].funcs->start)
227 			continue;
228 
229 		comp_matches[i].funcs->start(ovl_adaptor->ovl_adaptor_comp[i]);
230 	}
231 }
232 
mtk_ovl_adaptor_stop(struct device * dev)233 void mtk_ovl_adaptor_stop(struct device *dev)
234 {
235 	int i;
236 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
237 
238 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
239 		if (!ovl_adaptor->ovl_adaptor_comp[i] ||
240 		    !comp_matches[i].funcs->stop)
241 			continue;
242 
243 		comp_matches[i].funcs->stop(ovl_adaptor->ovl_adaptor_comp[i]);
244 	}
245 }
246 
247 /**
248  * power_off - Power off the devices in OVL adaptor
249  * @dev: Device to be powered off
250  * @num: Number of the devices to be powered off
251  *
252  * Calls the .power_off() ovl_adaptor component callback if it is present.
253  */
power_off(struct device * dev,int num)254 static inline void power_off(struct device *dev, int num)
255 {
256 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
257 	int i;
258 
259 	if (num > OVL_ADAPTOR_ID_MAX)
260 		num = OVL_ADAPTOR_ID_MAX;
261 
262 	for (i = num - 1; i >= 0; i--) {
263 		if (!ovl_adaptor->ovl_adaptor_comp[i] ||
264 		    !comp_matches[i].funcs->power_off)
265 			continue;
266 
267 		comp_matches[i].funcs->power_off(ovl_adaptor->ovl_adaptor_comp[i]);
268 	}
269 }
270 
271 /**
272  * mtk_ovl_adaptor_power_on - Power on the devices in OVL adaptor
273  * @dev: Device to be powered on
274  *
275  * Different from OVL, OVL adaptor is a pseudo device so
276  * we didn't define it in the device tree, pm_runtime_resume_and_get()
277  * called by .atomic_enable() power on no device in OVL adaptor,
278  * we have to implement a function to do the job instead.
279  *
280  * Return: Zero for success or negative number for failure.
281  */
mtk_ovl_adaptor_power_on(struct device * dev)282 int mtk_ovl_adaptor_power_on(struct device *dev)
283 {
284 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
285 	int i, ret;
286 
287 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
288 		if (!ovl_adaptor->ovl_adaptor_comp[i] ||
289 		    !comp_matches[i].funcs->power_on)
290 			continue;
291 
292 		ret = comp_matches[i].funcs->power_on(ovl_adaptor->ovl_adaptor_comp[i]);
293 		if (ret < 0) {
294 			dev_err(dev, "Failed to enable power domain %d, err %d\n", i, ret);
295 			power_off(dev, i);
296 			return ret;
297 		}
298 	}
299 	return 0;
300 }
301 
mtk_ovl_adaptor_power_off(struct device * dev)302 void mtk_ovl_adaptor_power_off(struct device *dev)
303 {
304 	power_off(dev, OVL_ADAPTOR_ID_MAX);
305 }
306 
mtk_ovl_adaptor_clk_enable(struct device * dev)307 int mtk_ovl_adaptor_clk_enable(struct device *dev)
308 {
309 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
310 	struct device *comp;
311 	int ret;
312 	int i;
313 
314 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
315 		comp = ovl_adaptor->ovl_adaptor_comp[i];
316 		if (!comp || !comp_matches[i].funcs->clk_enable)
317 			continue;
318 		ret = comp_matches[i].funcs->clk_enable(comp);
319 		if (ret) {
320 			dev_err(dev, "Failed to enable clock %d, err %d\n", i, ret);
321 			while (--i >= 0)
322 				comp_matches[i].funcs->clk_disable(comp);
323 			return ret;
324 		}
325 	}
326 	return 0;
327 }
328 
mtk_ovl_adaptor_clk_disable(struct device * dev)329 void mtk_ovl_adaptor_clk_disable(struct device *dev)
330 {
331 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
332 	struct device *comp;
333 	int i;
334 
335 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
336 		comp = ovl_adaptor->ovl_adaptor_comp[i];
337 		if (!comp || !comp_matches[i].funcs->clk_disable)
338 			continue;
339 		comp_matches[i].funcs->clk_disable(comp);
340 		if (i < OVL_ADAPTOR_MERGE0)
341 			pm_runtime_put(comp);
342 	}
343 }
344 
mtk_ovl_adaptor_mode_valid(struct device * dev,const struct drm_display_mode * mode)345 enum drm_mode_status mtk_ovl_adaptor_mode_valid(struct device *dev,
346 						const struct drm_display_mode *mode)
347 
348 {
349 	int i;
350 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
351 
352 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
353 		dev = ovl_adaptor->ovl_adaptor_comp[i];
354 		if (!dev || !comp_matches[i].funcs->mode_valid)
355 			continue;
356 		return comp_matches[i].funcs->mode_valid(dev, mode);
357 	}
358 	return MODE_OK;
359 }
360 
mtk_ovl_adaptor_layer_nr(struct device * dev)361 unsigned int mtk_ovl_adaptor_layer_nr(struct device *dev)
362 {
363 	return MTK_OVL_ADAPTOR_LAYER_NUM;
364 }
365 
mtk_ovl_adaptor_dma_dev_get(struct device * dev)366 struct device *mtk_ovl_adaptor_dma_dev_get(struct device *dev)
367 {
368 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
369 
370 	return ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0];
371 }
372 
mtk_ovl_adaptor_register_vblank_cb(struct device * dev,void (* vblank_cb)(void *),void * vblank_cb_data)373 void mtk_ovl_adaptor_register_vblank_cb(struct device *dev, void (*vblank_cb)(void *),
374 					void *vblank_cb_data)
375 {
376 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
377 
378 	mtk_ethdr_register_vblank_cb(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0],
379 				     vblank_cb, vblank_cb_data);
380 }
381 
mtk_ovl_adaptor_unregister_vblank_cb(struct device * dev)382 void mtk_ovl_adaptor_unregister_vblank_cb(struct device *dev)
383 {
384 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
385 
386 	mtk_ethdr_unregister_vblank_cb(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
387 }
388 
mtk_ovl_adaptor_enable_vblank(struct device * dev)389 void mtk_ovl_adaptor_enable_vblank(struct device *dev)
390 {
391 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
392 
393 	mtk_ethdr_enable_vblank(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
394 }
395 
mtk_ovl_adaptor_disable_vblank(struct device * dev)396 void mtk_ovl_adaptor_disable_vblank(struct device *dev)
397 {
398 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
399 
400 	mtk_ethdr_disable_vblank(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
401 }
402 
mtk_ovl_adaptor_get_blend_modes(struct device * dev)403 u32 mtk_ovl_adaptor_get_blend_modes(struct device *dev)
404 {
405 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
406 
407 	return mtk_ethdr_get_blend_modes(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
408 }
409 
mtk_ovl_adaptor_get_formats(struct device * dev)410 const u32 *mtk_ovl_adaptor_get_formats(struct device *dev)
411 {
412 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
413 
414 	return mtk_mdp_rdma_get_formats(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0]);
415 }
416 
mtk_ovl_adaptor_get_num_formats(struct device * dev)417 size_t mtk_ovl_adaptor_get_num_formats(struct device *dev)
418 {
419 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
420 
421 	return mtk_mdp_rdma_get_num_formats(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0]);
422 }
423 
mtk_ovl_adaptor_add_comp(struct device * dev,struct mtk_mutex * mutex)424 void mtk_ovl_adaptor_add_comp(struct device *dev, struct mtk_mutex *mutex)
425 {
426 	int i;
427 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
428 
429 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
430 		if (!ovl_adaptor->ovl_adaptor_comp[i])
431 			continue;
432 		mtk_mutex_add_comp(mutex, comp_matches[i].comp_id);
433 	}
434 }
435 
mtk_ovl_adaptor_remove_comp(struct device * dev,struct mtk_mutex * mutex)436 void mtk_ovl_adaptor_remove_comp(struct device *dev, struct mtk_mutex *mutex)
437 {
438 	int i;
439 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
440 
441 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
442 		if (!ovl_adaptor->ovl_adaptor_comp[i])
443 			continue;
444 		mtk_mutex_remove_comp(mutex, comp_matches[i].comp_id);
445 	}
446 }
447 
mtk_ovl_adaptor_connect(struct device * dev,struct device * mmsys_dev,unsigned int next)448 void mtk_ovl_adaptor_connect(struct device *dev, struct device *mmsys_dev, unsigned int next)
449 {
450 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_ETHDR_MIXER, next);
451 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA0, DDP_COMPONENT_MERGE1);
452 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA1, DDP_COMPONENT_MERGE1);
453 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA2, DDP_COMPONENT_MERGE2);
454 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE1, DDP_COMPONENT_ETHDR_MIXER);
455 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE2, DDP_COMPONENT_ETHDR_MIXER);
456 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE3, DDP_COMPONENT_ETHDR_MIXER);
457 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE4, DDP_COMPONENT_ETHDR_MIXER);
458 }
459 
mtk_ovl_adaptor_disconnect(struct device * dev,struct device * mmsys_dev,unsigned int next)460 void mtk_ovl_adaptor_disconnect(struct device *dev, struct device *mmsys_dev, unsigned int next)
461 {
462 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_ETHDR_MIXER, next);
463 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA0, DDP_COMPONENT_MERGE1);
464 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA1, DDP_COMPONENT_MERGE1);
465 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA2, DDP_COMPONENT_MERGE2);
466 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE1, DDP_COMPONENT_ETHDR_MIXER);
467 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE2, DDP_COMPONENT_ETHDR_MIXER);
468 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE3, DDP_COMPONENT_ETHDR_MIXER);
469 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE4, DDP_COMPONENT_ETHDR_MIXER);
470 }
471 
ovl_adaptor_comp_get_id(struct device * dev,struct device_node * node,enum mtk_ovl_adaptor_comp_type type)472 static int ovl_adaptor_comp_get_id(struct device *dev, struct device_node *node,
473 				   enum mtk_ovl_adaptor_comp_type type)
474 {
475 	int alias_id = of_alias_get_id(node, private_comp_stem[type]);
476 	int i;
477 
478 	for (i = 0; i < ARRAY_SIZE(comp_matches); i++)
479 		if (comp_matches[i].type == type &&
480 		    comp_matches[i].alias_id == alias_id)
481 			return i;
482 
483 	dev_warn(dev, "Failed to get id. type: %d, alias: %d\n", type, alias_id);
484 	return -EINVAL;
485 }
486 
487 static const struct of_device_id mtk_ovl_adaptor_comp_dt_ids[] = {
488 	{ .compatible = "mediatek,mt8188-disp-padding", .data = (void *)OVL_ADAPTOR_TYPE_PADDING },
489 	{ .compatible = "mediatek,mt8195-disp-ethdr", .data = (void *)OVL_ADAPTOR_TYPE_ETHDR },
490 	{ .compatible = "mediatek,mt8195-disp-merge", .data = (void *)OVL_ADAPTOR_TYPE_MERGE },
491 	{ .compatible = "mediatek,mt8195-vdo1-rdma", .data = (void *)OVL_ADAPTOR_TYPE_MDP_RDMA },
492 	{ /* sentinel */ }
493 };
494 
compare_of(struct device * dev,void * data)495 static int compare_of(struct device *dev, void *data)
496 {
497 	return dev->of_node == data;
498 }
499 
ovl_adaptor_comp_init(struct device * dev,struct component_match ** match)500 static int ovl_adaptor_comp_init(struct device *dev, struct component_match **match)
501 {
502 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
503 	struct device_node *parent;
504 	struct platform_device *comp_pdev;
505 
506 	parent = dev->parent->parent->of_node->parent;
507 
508 	for_each_child_of_node_scoped(parent, node) {
509 		const struct of_device_id *of_id;
510 		enum mtk_ovl_adaptor_comp_type type;
511 		int id;
512 
513 		of_id = of_match_node(mtk_ovl_adaptor_comp_dt_ids, node);
514 		if (!of_id)
515 			continue;
516 
517 		if (!of_device_is_available(node)) {
518 			dev_dbg(dev, "Skipping disabled component %pOF\n",
519 				node);
520 			continue;
521 		}
522 
523 		type = (enum mtk_ovl_adaptor_comp_type)(uintptr_t)of_id->data;
524 		id = ovl_adaptor_comp_get_id(dev, node, type);
525 		if (id < 0) {
526 			dev_warn(dev, "Skipping unknown component %pOF\n",
527 				 node);
528 			continue;
529 		}
530 
531 		comp_pdev = of_find_device_by_node(node);
532 		if (!comp_pdev)
533 			return -EPROBE_DEFER;
534 
535 		priv->ovl_adaptor_comp[id] = &comp_pdev->dev;
536 
537 		drm_of_component_match_add(dev, match, compare_of, node);
538 		dev_dbg(dev, "Adding component match for %pOF\n", node);
539 	}
540 
541 	if (!*match) {
542 		dev_err(dev, "No match device for ovl_adaptor\n");
543 		return -ENODEV;
544 	}
545 
546 	return 0;
547 }
548 
mtk_disp_ovl_adaptor_comp_bind(struct device * dev,struct device * master,void * data)549 static int mtk_disp_ovl_adaptor_comp_bind(struct device *dev, struct device *master,
550 					  void *data)
551 {
552 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
553 
554 	if (!priv->children_bound)
555 		return -EPROBE_DEFER;
556 
557 	return 0;
558 }
559 
mtk_disp_ovl_adaptor_comp_unbind(struct device * dev,struct device * master,void * data)560 static void mtk_disp_ovl_adaptor_comp_unbind(struct device *dev, struct device *master,
561 					     void *data)
562 {
563 }
564 
565 static const struct component_ops mtk_disp_ovl_adaptor_comp_ops = {
566 	.bind	= mtk_disp_ovl_adaptor_comp_bind,
567 	.unbind = mtk_disp_ovl_adaptor_comp_unbind,
568 };
569 
mtk_disp_ovl_adaptor_master_bind(struct device * dev)570 static int mtk_disp_ovl_adaptor_master_bind(struct device *dev)
571 {
572 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
573 	int ret;
574 
575 	ret = component_bind_all(dev, priv->mmsys_dev);
576 	if (ret)
577 		return dev_err_probe(dev, ret, "component_bind_all failed!\n");
578 
579 	priv->children_bound = true;
580 	return 0;
581 }
582 
mtk_disp_ovl_adaptor_master_unbind(struct device * dev)583 static void mtk_disp_ovl_adaptor_master_unbind(struct device *dev)
584 {
585 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
586 
587 	priv->children_bound = false;
588 }
589 
590 static const struct component_master_ops mtk_disp_ovl_adaptor_master_ops = {
591 	.bind		= mtk_disp_ovl_adaptor_master_bind,
592 	.unbind		= mtk_disp_ovl_adaptor_master_unbind,
593 };
594 
mtk_disp_ovl_adaptor_probe(struct platform_device * pdev)595 static int mtk_disp_ovl_adaptor_probe(struct platform_device *pdev)
596 {
597 	struct mtk_disp_ovl_adaptor *priv;
598 	struct device *dev = &pdev->dev;
599 	struct component_match *match = NULL;
600 	int ret;
601 
602 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
603 	if (!priv)
604 		return -ENOMEM;
605 
606 	platform_set_drvdata(pdev, priv);
607 
608 	ret = ovl_adaptor_comp_init(dev, &match);
609 	if (ret < 0)
610 		return ret;
611 
612 	priv->mmsys_dev = pdev->dev.platform_data;
613 
614 	component_master_add_with_match(dev, &mtk_disp_ovl_adaptor_master_ops, match);
615 
616 	pm_runtime_enable(dev);
617 
618 	ret = component_add(dev, &mtk_disp_ovl_adaptor_comp_ops);
619 	if (ret != 0) {
620 		pm_runtime_disable(dev);
621 		return dev_err_probe(dev, ret, "Failed to add component\n");
622 	}
623 
624 	return 0;
625 }
626 
mtk_disp_ovl_adaptor_remove(struct platform_device * pdev)627 static void mtk_disp_ovl_adaptor_remove(struct platform_device *pdev)
628 {
629 	component_master_del(&pdev->dev, &mtk_disp_ovl_adaptor_master_ops);
630 	pm_runtime_disable(&pdev->dev);
631 }
632 
633 struct platform_driver mtk_disp_ovl_adaptor_driver = {
634 	.probe		= mtk_disp_ovl_adaptor_probe,
635 	.remove_new	= mtk_disp_ovl_adaptor_remove,
636 	.driver		= {
637 		.name	= "mediatek-disp-ovl-adaptor",
638 	},
639 };
640