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