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_device.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/reset.h>
15 #include <linux/soc/mediatek/mtk-cmdq.h>
16 #include <linux/soc/mediatek/mtk-mmsys.h>
17 #include <linux/soc/mediatek/mtk-mutex.h>
18 
19 #include "mtk_disp_drv.h"
20 #include "mtk_drm_crtc.h"
21 #include "mtk_drm_ddp_comp.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_RDMA = 0,
30 	OVL_ADAPTOR_TYPE_MERGE,
31 	OVL_ADAPTOR_TYPE_ETHDR,
32 	OVL_ADAPTOR_TYPE_NUM,
33 };
34 
35 enum mtk_ovl_adaptor_comp_id {
36 	OVL_ADAPTOR_MDP_RDMA0,
37 	OVL_ADAPTOR_MDP_RDMA1,
38 	OVL_ADAPTOR_MDP_RDMA2,
39 	OVL_ADAPTOR_MDP_RDMA3,
40 	OVL_ADAPTOR_MDP_RDMA4,
41 	OVL_ADAPTOR_MDP_RDMA5,
42 	OVL_ADAPTOR_MDP_RDMA6,
43 	OVL_ADAPTOR_MDP_RDMA7,
44 	OVL_ADAPTOR_MERGE0,
45 	OVL_ADAPTOR_MERGE1,
46 	OVL_ADAPTOR_MERGE2,
47 	OVL_ADAPTOR_MERGE3,
48 	OVL_ADAPTOR_ETHDR0,
49 	OVL_ADAPTOR_ID_MAX
50 };
51 
52 struct ovl_adaptor_comp_match {
53 	enum mtk_ovl_adaptor_comp_type type;
54 	int alias_id;
55 };
56 
57 struct mtk_disp_ovl_adaptor {
58 	struct device *ovl_adaptor_comp[OVL_ADAPTOR_ID_MAX];
59 	struct device *mmsys_dev;
60 	bool children_bound;
61 };
62 
63 static const char * const private_comp_stem[OVL_ADAPTOR_TYPE_NUM] = {
64 	[OVL_ADAPTOR_TYPE_RDMA]		= "vdo1-rdma",
65 	[OVL_ADAPTOR_TYPE_MERGE]	= "merge",
66 	[OVL_ADAPTOR_TYPE_ETHDR]	= "ethdr",
67 };
68 
69 static const struct ovl_adaptor_comp_match comp_matches[OVL_ADAPTOR_ID_MAX] = {
70 	[OVL_ADAPTOR_MDP_RDMA0]	= { OVL_ADAPTOR_TYPE_RDMA, 0 },
71 	[OVL_ADAPTOR_MDP_RDMA1]	= { OVL_ADAPTOR_TYPE_RDMA, 1 },
72 	[OVL_ADAPTOR_MDP_RDMA2]	= { OVL_ADAPTOR_TYPE_RDMA, 2 },
73 	[OVL_ADAPTOR_MDP_RDMA3]	= { OVL_ADAPTOR_TYPE_RDMA, 3 },
74 	[OVL_ADAPTOR_MDP_RDMA4]	= { OVL_ADAPTOR_TYPE_RDMA, 4 },
75 	[OVL_ADAPTOR_MDP_RDMA5]	= { OVL_ADAPTOR_TYPE_RDMA, 5 },
76 	[OVL_ADAPTOR_MDP_RDMA6]	= { OVL_ADAPTOR_TYPE_RDMA, 6 },
77 	[OVL_ADAPTOR_MDP_RDMA7]	= { OVL_ADAPTOR_TYPE_RDMA, 7 },
78 	[OVL_ADAPTOR_MERGE0]	= { OVL_ADAPTOR_TYPE_MERGE, 1 },
79 	[OVL_ADAPTOR_MERGE1]	= { OVL_ADAPTOR_TYPE_MERGE, 2 },
80 	[OVL_ADAPTOR_MERGE2]	= { OVL_ADAPTOR_TYPE_MERGE, 3 },
81 	[OVL_ADAPTOR_MERGE3]	= { OVL_ADAPTOR_TYPE_MERGE, 4 },
82 	[OVL_ADAPTOR_ETHDR0]	= { OVL_ADAPTOR_TYPE_ETHDR, 0 },
83 };
84 
85 void mtk_ovl_adaptor_layer_config(struct device *dev, unsigned int idx,
86 				  struct mtk_plane_state *state,
87 				  struct cmdq_pkt *cmdq_pkt)
88 {
89 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
90 	struct mtk_plane_pending_state *pending = &state->pending;
91 	struct mtk_mdp_rdma_cfg rdma_config = {0};
92 	struct device *rdma_l;
93 	struct device *rdma_r;
94 	struct device *merge;
95 	struct device *ethdr;
96 	const struct drm_format_info *fmt_info = drm_format_info(pending->format);
97 	bool use_dual_pipe = false;
98 	unsigned int align_width;
99 	unsigned int l_w = 0;
100 	unsigned int r_w = 0;
101 
102 	dev_dbg(dev, "%s+ idx:%d, enable:%d, fmt:0x%x\n", __func__, idx,
103 		pending->enable, pending->format);
104 	dev_dbg(dev, "addr 0x%pad, fb w:%d, {%d,%d,%d,%d}\n",
105 		&pending->addr, (pending->pitch / fmt_info->cpp[0]),
106 		pending->x, pending->y, pending->width, pending->height);
107 
108 	rdma_l = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0 + 2 * idx];
109 	rdma_r = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0 + 2 * idx + 1];
110 	merge = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MERGE0 + idx];
111 	ethdr = ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0];
112 
113 	if (!pending->enable) {
114 		mtk_merge_stop_cmdq(merge, cmdq_pkt);
115 		mtk_mdp_rdma_stop(rdma_l, cmdq_pkt);
116 		mtk_mdp_rdma_stop(rdma_r, cmdq_pkt);
117 		mtk_ethdr_layer_config(ethdr, idx, state, cmdq_pkt);
118 		return;
119 	}
120 
121 	/* ETHDR is in 1T2P domain, width needs to be 2 pixels align */
122 	align_width = ALIGN_DOWN(pending->width, 2);
123 
124 	if (align_width > MTK_OVL_ADAPTOR_RDMA_MAX_WIDTH)
125 		use_dual_pipe = true;
126 
127 	if (use_dual_pipe) {
128 		l_w = (align_width / 2) + ((pending->width / 2) % 2);
129 		r_w = align_width - l_w;
130 	} else {
131 		l_w = align_width;
132 	}
133 	mtk_merge_advance_config(merge, l_w, r_w, pending->height, 0, 0, cmdq_pkt);
134 	mtk_mmsys_merge_async_config(ovl_adaptor->mmsys_dev, idx, align_width / 2,
135 				     pending->height, cmdq_pkt);
136 
137 	rdma_config.width = l_w;
138 	rdma_config.height = pending->height;
139 	rdma_config.addr0 = pending->addr;
140 	rdma_config.pitch = pending->pitch;
141 	rdma_config.fmt = pending->format;
142 	rdma_config.color_encoding = pending->color_encoding;
143 	mtk_mdp_rdma_config(rdma_l, &rdma_config, cmdq_pkt);
144 
145 	if (use_dual_pipe) {
146 		rdma_config.x_left = l_w;
147 		rdma_config.width = r_w;
148 		mtk_mdp_rdma_config(rdma_r, &rdma_config, cmdq_pkt);
149 	}
150 
151 	mtk_merge_start_cmdq(merge, cmdq_pkt);
152 
153 	mtk_mdp_rdma_start(rdma_l, cmdq_pkt);
154 	if (use_dual_pipe)
155 		mtk_mdp_rdma_start(rdma_r, cmdq_pkt);
156 	else
157 		mtk_mdp_rdma_stop(rdma_r, cmdq_pkt);
158 
159 	mtk_ethdr_layer_config(ethdr, idx, state, cmdq_pkt);
160 }
161 
162 void mtk_ovl_adaptor_config(struct device *dev, unsigned int w,
163 			    unsigned int h, unsigned int vrefresh,
164 			    unsigned int bpc, struct cmdq_pkt *cmdq_pkt)
165 {
166 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
167 
168 	mtk_ethdr_config(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0], w, h,
169 			 vrefresh, bpc, cmdq_pkt);
170 }
171 
172 void mtk_ovl_adaptor_start(struct device *dev)
173 {
174 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
175 
176 	mtk_ethdr_start(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
177 }
178 
179 void mtk_ovl_adaptor_stop(struct device *dev)
180 {
181 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
182 
183 	mtk_ethdr_stop(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
184 }
185 
186 int mtk_ovl_adaptor_clk_enable(struct device *dev)
187 {
188 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
189 	struct device *comp;
190 	int ret;
191 	int i;
192 
193 	for (i = 0; i < OVL_ADAPTOR_MERGE0; i++) {
194 		comp = ovl_adaptor->ovl_adaptor_comp[i];
195 		ret = pm_runtime_get_sync(comp);
196 		if (ret < 0) {
197 			dev_err(dev, "Failed to enable power domain %d, err %d\n", i, ret);
198 			goto pwr_err;
199 		}
200 	}
201 
202 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
203 		comp = ovl_adaptor->ovl_adaptor_comp[i];
204 
205 		if (i < OVL_ADAPTOR_MERGE0)
206 			ret = mtk_mdp_rdma_clk_enable(comp);
207 		else if (i < OVL_ADAPTOR_ETHDR0)
208 			ret = mtk_merge_clk_enable(comp);
209 		else
210 			ret = mtk_ethdr_clk_enable(comp);
211 		if (ret) {
212 			dev_err(dev, "Failed to enable clock %d, err %d\n", i, ret);
213 			goto clk_err;
214 		}
215 	}
216 
217 	return ret;
218 
219 clk_err:
220 	while (--i >= 0) {
221 		comp = ovl_adaptor->ovl_adaptor_comp[i];
222 		if (i < OVL_ADAPTOR_MERGE0)
223 			mtk_mdp_rdma_clk_disable(comp);
224 		else if (i < OVL_ADAPTOR_ETHDR0)
225 			mtk_merge_clk_disable(comp);
226 		else
227 			mtk_ethdr_clk_disable(comp);
228 	}
229 	i = OVL_ADAPTOR_MERGE0;
230 
231 pwr_err:
232 	while (--i >= 0)
233 		pm_runtime_put(ovl_adaptor->ovl_adaptor_comp[i]);
234 
235 	return ret;
236 }
237 
238 void mtk_ovl_adaptor_clk_disable(struct device *dev)
239 {
240 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
241 	struct device *comp;
242 	int i;
243 
244 	for (i = 0; i < OVL_ADAPTOR_ID_MAX; i++) {
245 		comp = ovl_adaptor->ovl_adaptor_comp[i];
246 
247 		if (i < OVL_ADAPTOR_MERGE0) {
248 			mtk_mdp_rdma_clk_disable(comp);
249 			pm_runtime_put(comp);
250 		} else if (i < OVL_ADAPTOR_ETHDR0) {
251 			mtk_merge_clk_disable(comp);
252 		} else {
253 			mtk_ethdr_clk_disable(comp);
254 		}
255 	}
256 }
257 
258 unsigned int mtk_ovl_adaptor_layer_nr(struct device *dev)
259 {
260 	return MTK_OVL_ADAPTOR_LAYER_NUM;
261 }
262 
263 struct device *mtk_ovl_adaptor_dma_dev_get(struct device *dev)
264 {
265 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
266 
267 	return ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0];
268 }
269 
270 void mtk_ovl_adaptor_register_vblank_cb(struct device *dev, void (*vblank_cb)(void *),
271 					void *vblank_cb_data)
272 {
273 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
274 
275 	mtk_ethdr_register_vblank_cb(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0],
276 				     vblank_cb, vblank_cb_data);
277 }
278 
279 void mtk_ovl_adaptor_unregister_vblank_cb(struct device *dev)
280 {
281 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
282 
283 	mtk_ethdr_unregister_vblank_cb(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
284 }
285 
286 void mtk_ovl_adaptor_enable_vblank(struct device *dev)
287 {
288 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
289 
290 	mtk_ethdr_enable_vblank(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
291 }
292 
293 void mtk_ovl_adaptor_disable_vblank(struct device *dev)
294 {
295 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
296 
297 	mtk_ethdr_disable_vblank(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_ETHDR0]);
298 }
299 
300 const u32 *mtk_ovl_adaptor_get_formats(struct device *dev)
301 {
302 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
303 
304 	return mtk_mdp_rdma_get_formats(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0]);
305 }
306 
307 size_t mtk_ovl_adaptor_get_num_formats(struct device *dev)
308 {
309 	struct mtk_disp_ovl_adaptor *ovl_adaptor = dev_get_drvdata(dev);
310 
311 	return mtk_mdp_rdma_get_num_formats(ovl_adaptor->ovl_adaptor_comp[OVL_ADAPTOR_MDP_RDMA0]);
312 }
313 
314 void mtk_ovl_adaptor_add_comp(struct device *dev, struct mtk_mutex *mutex)
315 {
316 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA0);
317 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA1);
318 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA2);
319 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA3);
320 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA4);
321 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA5);
322 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA6);
323 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MDP_RDMA7);
324 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MERGE1);
325 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MERGE2);
326 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MERGE3);
327 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_MERGE4);
328 	mtk_mutex_add_comp(mutex, DDP_COMPONENT_ETHDR_MIXER);
329 }
330 
331 void mtk_ovl_adaptor_remove_comp(struct device *dev, struct mtk_mutex *mutex)
332 {
333 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA0);
334 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA1);
335 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA2);
336 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA3);
337 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA4);
338 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA5);
339 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA6);
340 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MDP_RDMA7);
341 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MERGE1);
342 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MERGE2);
343 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MERGE3);
344 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_MERGE4);
345 	mtk_mutex_remove_comp(mutex, DDP_COMPONENT_ETHDR_MIXER);
346 }
347 
348 void mtk_ovl_adaptor_connect(struct device *dev, struct device *mmsys_dev, unsigned int next)
349 {
350 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA0, DDP_COMPONENT_MERGE1);
351 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA1, DDP_COMPONENT_MERGE1);
352 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MDP_RDMA2, DDP_COMPONENT_MERGE2);
353 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE1, DDP_COMPONENT_ETHDR_MIXER);
354 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE2, DDP_COMPONENT_ETHDR_MIXER);
355 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE3, DDP_COMPONENT_ETHDR_MIXER);
356 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_MERGE4, DDP_COMPONENT_ETHDR_MIXER);
357 	mtk_mmsys_ddp_connect(mmsys_dev, DDP_COMPONENT_ETHDR_MIXER, next);
358 }
359 
360 void mtk_ovl_adaptor_disconnect(struct device *dev, struct device *mmsys_dev, unsigned int next)
361 {
362 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA0, DDP_COMPONENT_MERGE1);
363 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA1, DDP_COMPONENT_MERGE1);
364 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MDP_RDMA2, DDP_COMPONENT_MERGE2);
365 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE1, DDP_COMPONENT_ETHDR_MIXER);
366 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE2, DDP_COMPONENT_ETHDR_MIXER);
367 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE3, DDP_COMPONENT_ETHDR_MIXER);
368 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_MERGE4, DDP_COMPONENT_ETHDR_MIXER);
369 	mtk_mmsys_ddp_disconnect(mmsys_dev, DDP_COMPONENT_ETHDR_MIXER, next);
370 }
371 
372 static int ovl_adaptor_comp_get_id(struct device *dev, struct device_node *node,
373 				   enum mtk_ovl_adaptor_comp_type type)
374 {
375 	int alias_id = of_alias_get_id(node, private_comp_stem[type]);
376 	int i;
377 
378 	for (i = 0; i < ARRAY_SIZE(comp_matches); i++)
379 		if (comp_matches[i].type == type &&
380 		    comp_matches[i].alias_id == alias_id)
381 			return i;
382 
383 	dev_warn(dev, "Failed to get id. type: %d, alias: %d\n", type, alias_id);
384 	return -EINVAL;
385 }
386 
387 static const struct of_device_id mtk_ovl_adaptor_comp_dt_ids[] = {
388 	{
389 		.compatible = "mediatek,mt8195-vdo1-rdma",
390 		.data = (void *)OVL_ADAPTOR_TYPE_RDMA,
391 	}, {
392 		.compatible = "mediatek,mt8195-disp-merge",
393 		.data = (void *)OVL_ADAPTOR_TYPE_MERGE,
394 	}, {
395 		.compatible = "mediatek,mt8195-disp-ethdr",
396 		.data = (void *)OVL_ADAPTOR_TYPE_ETHDR,
397 	},
398 	{},
399 };
400 
401 static int compare_of(struct device *dev, void *data)
402 {
403 	return dev->of_node == data;
404 }
405 
406 static int ovl_adaptor_comp_init(struct device *dev, struct component_match **match)
407 {
408 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
409 	struct device_node *node, *parent;
410 	struct platform_device *comp_pdev;
411 
412 	parent = dev->parent->parent->of_node->parent;
413 
414 	for_each_child_of_node(parent, node) {
415 		const struct of_device_id *of_id;
416 		enum mtk_ovl_adaptor_comp_type type;
417 		int id;
418 
419 		of_id = of_match_node(mtk_ovl_adaptor_comp_dt_ids, node);
420 		if (!of_id)
421 			continue;
422 
423 		if (!of_device_is_available(node)) {
424 			dev_dbg(dev, "Skipping disabled component %pOF\n",
425 				node);
426 			continue;
427 		}
428 
429 		type = (enum mtk_ovl_adaptor_comp_type)of_id->data;
430 		id = ovl_adaptor_comp_get_id(dev, node, type);
431 		if (id < 0) {
432 			dev_warn(dev, "Skipping unknown component %pOF\n",
433 				 node);
434 			continue;
435 		}
436 
437 		comp_pdev = of_find_device_by_node(node);
438 		if (!comp_pdev)
439 			return -EPROBE_DEFER;
440 
441 		priv->ovl_adaptor_comp[id] = &comp_pdev->dev;
442 
443 		drm_of_component_match_add(dev, match, compare_of, node);
444 		dev_dbg(dev, "Adding component match for %pOF\n", node);
445 	}
446 
447 	if (!*match) {
448 		dev_err(dev, "No match device for ovl_adaptor\n");
449 		return -ENODEV;
450 	}
451 
452 	return 0;
453 }
454 
455 static int mtk_disp_ovl_adaptor_comp_bind(struct device *dev, struct device *master,
456 					  void *data)
457 {
458 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
459 
460 	if (!priv->children_bound)
461 		return -EPROBE_DEFER;
462 
463 	return 0;
464 }
465 
466 static void mtk_disp_ovl_adaptor_comp_unbind(struct device *dev, struct device *master,
467 					     void *data)
468 {
469 }
470 
471 static const struct component_ops mtk_disp_ovl_adaptor_comp_ops = {
472 	.bind	= mtk_disp_ovl_adaptor_comp_bind,
473 	.unbind = mtk_disp_ovl_adaptor_comp_unbind,
474 };
475 
476 static int mtk_disp_ovl_adaptor_master_bind(struct device *dev)
477 {
478 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
479 	int ret;
480 
481 	ret = component_bind_all(dev, priv->mmsys_dev);
482 	if (ret)
483 		return dev_err_probe(dev, ret, "component_bind_all failed!\n");
484 
485 	priv->children_bound = true;
486 	return 0;
487 }
488 
489 static void mtk_disp_ovl_adaptor_master_unbind(struct device *dev)
490 {
491 	struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev);
492 
493 	priv->children_bound = false;
494 }
495 
496 static const struct component_master_ops mtk_disp_ovl_adaptor_master_ops = {
497 	.bind		= mtk_disp_ovl_adaptor_master_bind,
498 	.unbind		= mtk_disp_ovl_adaptor_master_unbind,
499 };
500 
501 static int mtk_disp_ovl_adaptor_probe(struct platform_device *pdev)
502 {
503 	struct mtk_disp_ovl_adaptor *priv;
504 	struct device *dev = &pdev->dev;
505 	struct component_match *match = NULL;
506 	int ret;
507 
508 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
509 	if (!priv)
510 		return -ENOMEM;
511 
512 	platform_set_drvdata(pdev, priv);
513 
514 	ret = ovl_adaptor_comp_init(dev, &match);
515 	if (ret < 0)
516 		return ret;
517 
518 	priv->mmsys_dev = pdev->dev.platform_data;
519 
520 	component_master_add_with_match(dev, &mtk_disp_ovl_adaptor_master_ops, match);
521 
522 	pm_runtime_enable(dev);
523 
524 	ret = component_add(dev, &mtk_disp_ovl_adaptor_comp_ops);
525 	if (ret != 0) {
526 		pm_runtime_disable(dev);
527 		dev_err(dev, "Failed to add component: %d\n", ret);
528 	}
529 
530 	return ret;
531 }
532 
533 static int mtk_disp_ovl_adaptor_remove(struct platform_device *pdev)
534 {
535 	component_master_del(&pdev->dev, &mtk_disp_ovl_adaptor_master_ops);
536 	pm_runtime_disable(&pdev->dev);
537 	return 0;
538 }
539 
540 struct platform_driver mtk_disp_ovl_adaptor_driver = {
541 	.probe		= mtk_disp_ovl_adaptor_probe,
542 	.remove		= mtk_disp_ovl_adaptor_remove,
543 	.driver		= {
544 		.name	= "mediatek-disp-ovl-adaptor",
545 		.owner	= THIS_MODULE,
546 	},
547 };
548