xref: /linux/drivers/gpu/drm/mediatek/mtk_disp_ovl_adaptor.c (revision a0efa2f362a69e47b9d8b48f770ef3a0249a7911)
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 
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 
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 
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 
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  */
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  */
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 
302 void mtk_ovl_adaptor_power_off(struct device *dev)
303 {
304 	power_off(dev, OVL_ADAPTOR_ID_MAX);
305 }
306 
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 
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 
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 
361 unsigned int mtk_ovl_adaptor_layer_nr(struct device *dev)
362 {
363 	return MTK_OVL_ADAPTOR_LAYER_NUM;
364 }
365 
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 
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 
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 
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 
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 
403 const u32 *mtk_ovl_adaptor_get_formats(struct device *dev)
404 {
405 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
406 
407 	return mtk_mdp_rdma_get_formats(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0]);
408 }
409 
410 size_t mtk_ovl_adaptor_get_num_formats(struct device *dev)
411 {
412 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
413 
414 	return mtk_mdp_rdma_get_num_formats(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0]);
415 }
416 
417 void mtk_ovl_adaptor_add_comp(struct device *dev, struct mtk_mutex *mutex)
418 {
419 	int i;
420 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
421 
422 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
423 		if (!ovl_adaptor->ovl_adaptor_comp[i])
424 			continue;
425 		mtk_mutex_add_comp(mutex, comp_matches[i].comp_id);
426 	}
427 }
428 
429 void mtk_ovl_adaptor_remove_comp(struct device *dev, struct mtk_mutex *mutex)
430 {
431 	int i;
432 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
433 
434 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
435 		if (!ovl_adaptor->ovl_adaptor_comp[i])
436 			continue;
437 		mtk_mutex_remove_comp(mutex, comp_matches[i].comp_id);
438 	}
439 }
440 
441 void mtk_ovl_adaptor_connect(struct device *dev, struct device *mmsys_dev, unsigned int next)
442 {
443 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_ETHDR_MIXER, next);
444 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA0, DDP_COMPONENT_MERGE1);
445 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA1, DDP_COMPONENT_MERGE1);
446 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA2, DDP_COMPONENT_MERGE2);
447 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE1, DDP_COMPONENT_ETHDR_MIXER);
448 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE2, DDP_COMPONENT_ETHDR_MIXER);
449 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE3, DDP_COMPONENT_ETHDR_MIXER);
450 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE4, DDP_COMPONENT_ETHDR_MIXER);
451 }
452 
453 void mtk_ovl_adaptor_disconnect(struct device *dev, struct device *mmsys_dev, unsigned int next)
454 {
455 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_ETHDR_MIXER, next);
456 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA0, DDP_COMPONENT_MERGE1);
457 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA1, DDP_COMPONENT_MERGE1);
458 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA2, DDP_COMPONENT_MERGE2);
459 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE1, DDP_COMPONENT_ETHDR_MIXER);
460 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE2, DDP_COMPONENT_ETHDR_MIXER);
461 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE3, DDP_COMPONENT_ETHDR_MIXER);
462 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE4, DDP_COMPONENT_ETHDR_MIXER);
463 }
464 
465 static int ovl_adaptor_comp_get_id(struct device *dev, struct device_node *node,
466 				   enum mtk_ovl_adaptor_comp_type type)
467 {
468 	int alias_id = of_alias_get_id(node, private_comp_stem[type]);
469 	int i;
470 
471 	for (i = 0; i < ARRAY_SIZE(comp_matches); i++)
472 		if (comp_matches[i].type == type &&
473 		    comp_matches[i].alias_id == alias_id)
474 			return i;
475 
476 	dev_warn(dev, "Failed to get id. type: %d, alias: %d\n", type, alias_id);
477 	return -EINVAL;
478 }
479 
480 static const struct of_device_id mtk_ovl_adaptor_comp_dt_ids[] = {
481 	{ .compatible = "mediatek,mt8188-disp-padding", .data = (void *)OVL_ADAPTOR_TYPE_PADDING },
482 	{ .compatible = "mediatek,mt8195-disp-ethdr", .data = (void *)OVL_ADAPTOR_TYPE_ETHDR },
483 	{ .compatible = "mediatek,mt8195-disp-merge", .data = (void *)OVL_ADAPTOR_TYPE_MERGE },
484 	{ .compatible = "mediatek,mt8195-vdo1-rdma", .data = (void *)OVL_ADAPTOR_TYPE_MDP_RDMA },
485 	{ /* sentinel */ }
486 };
487 
488 static int compare_of(struct device *dev, void *data)
489 {
490 	return dev->of_node == data;
491 }
492 
493 static int ovl_adaptor_comp_init(struct device *dev, struct component_match **match)
494 {
495 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
496 	struct device_node *parent;
497 	struct platform_device *comp_pdev;
498 
499 	parent = dev->parent->parent->of_node->parent;
500 
501 	for_each_child_of_node_scoped(parent, node) {
502 		const struct of_device_id *of_id;
503 		enum mtk_ovl_adaptor_comp_type type;
504 		int id;
505 
506 		of_id = of_match_node(mtk_ovl_adaptor_comp_dt_ids, node);
507 		if (!of_id)
508 			continue;
509 
510 		if (!of_device_is_available(node)) {
511 			dev_dbg(dev, "Skipping disabled component %pOF\n",
512 				node);
513 			continue;
514 		}
515 
516 		type = (enum mtk_ovl_adaptor_comp_type)(uintptr_t)of_id->data;
517 		id = ovl_adaptor_comp_get_id(dev, node, type);
518 		if (id < 0) {
519 			dev_warn(dev, "Skipping unknown component %pOF\n",
520 				 node);
521 			continue;
522 		}
523 
524 		comp_pdev = of_find_device_by_node(node);
525 		if (!comp_pdev)
526 			return -EPROBE_DEFER;
527 
528 		priv->ovl_adaptor_comp[id] = &comp_pdev->dev;
529 
530 		drm_of_component_match_add(dev, match, compare_of, node);
531 		dev_dbg(dev, "Adding component match for %pOF\n", node);
532 	}
533 
534 	if (!*match) {
535 		dev_err(dev, "No match device for ovl_adaptor\n");
536 		return -ENODEV;
537 	}
538 
539 	return 0;
540 }
541 
542 static int mtk_disp_ovl_adaptor_comp_bind(struct device *dev, struct device *master,
543 					  void *data)
544 {
545 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
546 
547 	if (!priv->children_bound)
548 		return -EPROBE_DEFER;
549 
550 	return 0;
551 }
552 
553 static void mtk_disp_ovl_adaptor_comp_unbind(struct device *dev, struct device *master,
554 					     void *data)
555 {
556 }
557 
558 static const struct component_ops mtk_disp_ovl_adaptor_comp_ops = {
559 	.bind	= mtk_disp_ovl_adaptor_comp_bind,
560 	.unbind = mtk_disp_ovl_adaptor_comp_unbind,
561 };
562 
563 static int mtk_disp_ovl_adaptor_master_bind(struct device *dev)
564 {
565 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
566 	int ret;
567 
568 	ret = component_bind_all(dev, priv->mmsys_dev);
569 	if (ret)
570 		return dev_err_probe(dev, ret, "component_bind_all failed!\n");
571 
572 	priv->children_bound = true;
573 	return 0;
574 }
575 
576 static void mtk_disp_ovl_adaptor_master_unbind(struct device *dev)
577 {
578 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
579 
580 	priv->children_bound = false;
581 }
582 
583 static const struct component_master_ops mtk_disp_ovl_adaptor_master_ops = {
584 	.bind		= mtk_disp_ovl_adaptor_master_bind,
585 	.unbind		= mtk_disp_ovl_adaptor_master_unbind,
586 };
587 
588 static int mtk_disp_ovl_adaptor_probe(struct platform_device *pdev)
589 {
590 	struct mtk_disp_ovl_adaptor *priv;
591 	struct device *dev = &pdev->dev;
592 	struct component_match *match = NULL;
593 	int ret;
594 
595 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
596 	if (!priv)
597 		return -ENOMEM;
598 
599 	platform_set_drvdata(pdev, priv);
600 
601 	ret = ovl_adaptor_comp_init(dev, &match);
602 	if (ret < 0)
603 		return ret;
604 
605 	priv->mmsys_dev = pdev->dev.platform_data;
606 
607 	component_master_add_with_match(dev, &mtk_disp_ovl_adaptor_master_ops, match);
608 
609 	pm_runtime_enable(dev);
610 
611 	ret = component_add(dev, &mtk_disp_ovl_adaptor_comp_ops);
612 	if (ret != 0) {
613 		pm_runtime_disable(dev);
614 		return dev_err_probe(dev, ret, "Failed to add component\n");
615 	}
616 
617 	return 0;
618 }
619 
620 static void mtk_disp_ovl_adaptor_remove(struct platform_device *pdev)
621 {
622 	component_master_del(&pdev->dev, &mtk_disp_ovl_adaptor_master_ops);
623 	pm_runtime_disable(&pdev->dev);
624 }
625 
626 struct platform_driver mtk_disp_ovl_adaptor_driver = {
627 	.probe		= mtk_disp_ovl_adaptor_probe,
628 	.remove_new	= mtk_disp_ovl_adaptor_remove,
629 	.driver		= {
630 		.name	= "mediatek-disp-ovl-adaptor",
631 	},
632 };
633