1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Texas Instruments
4  * Author: Tomi Valkeinen <[email protected]>
5  */
6 
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_graph.h>
12 #include <linux/seq_file.h>
13 
14 #include <video/omapfb_dss.h>
15 
16 #include "dss.h"
17 
dss_of_port_get_parent_device(struct device_node * port)18 struct device_node *dss_of_port_get_parent_device(struct device_node *port)
19 {
20 	struct device_node *np;
21 	int i;
22 
23 	if (!port)
24 		return NULL;
25 
26 	np = of_get_parent(port);
27 
28 	for (i = 0; i < 2 && np; ++i) {
29 		struct property *prop;
30 
31 		prop = of_find_property(np, "compatible", NULL);
32 
33 		if (prop)
34 			return np;
35 
36 		np = of_get_next_parent(np);
37 	}
38 
39 	of_node_put(np);
40 	return NULL;
41 }
42 
dss_of_port_get_port_number(struct device_node * port)43 u32 dss_of_port_get_port_number(struct device_node *port)
44 {
45 	int r;
46 	u32 reg;
47 
48 	r = of_property_read_u32(port, "reg", &reg);
49 	if (r)
50 		reg = 0;
51 
52 	return reg;
53 }
54 
55 struct omap_dss_device *
omapdss_of_find_source_for_first_ep(struct device_node * node)56 omapdss_of_find_source_for_first_ep(struct device_node *node)
57 {
58 	struct device_node *ep;
59 	struct device_node *src_port;
60 	struct omap_dss_device *src;
61 
62 	ep = of_graph_get_endpoint_by_regs(node, 0, -1);
63 	if (!ep)
64 		return ERR_PTR(-EINVAL);
65 
66 	src_port = of_graph_get_remote_port(ep);
67 	of_node_put(ep);
68 	if (!src_port)
69 		return ERR_PTR(-EINVAL);
70 
71 	src = omap_dss_find_output_by_port_node(src_port);
72 
73 	of_node_put(src_port);
74 
75 	return src ? src : ERR_PTR(-EPROBE_DEFER);
76 }
77 EXPORT_SYMBOL_GPL(omapdss_of_find_source_for_first_ep);
78