1 /*
2  * Copyright (C) 2013 Texas Instruments
3  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14 
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/of_graph.h>
20 #include <linux/seq_file.h>
21 
22 #include <video/omapfb_dss.h>
23 
24 #include "dss.h"
25 
26 struct device_node *
27 omapdss_of_get_next_port(const struct device_node *parent,
28 			 struct device_node *prev)
29 {
30 	struct device_node *port = NULL;
31 
32 	if (!parent)
33 		return NULL;
34 
35 	if (!prev) {
36 		struct device_node *ports;
37 		/*
38 		 * It's the first call, we have to find a port subnode
39 		 * within this node or within an optional 'ports' node.
40 		 */
41 		ports = of_get_child_by_name(parent, "ports");
42 		if (ports)
43 			parent = ports;
44 
45 		port = of_get_child_by_name(parent, "port");
46 
47 		/* release the 'ports' node */
48 		of_node_put(ports);
49 	} else {
50 		struct device_node *ports;
51 
52 		ports = of_get_parent(prev);
53 		if (!ports)
54 			return NULL;
55 
56 		do {
57 			port = of_get_next_child(ports, prev);
58 			if (!port) {
59 				of_node_put(ports);
60 				return NULL;
61 			}
62 			prev = port;
63 		} while (!of_node_name_eq(port, "port"));
64 
65 		of_node_put(ports);
66 	}
67 
68 	return port;
69 }
70 EXPORT_SYMBOL_GPL(omapdss_of_get_next_port);
71 
72 struct device_node *
73 omapdss_of_get_next_endpoint(const struct device_node *parent,
74 			     struct device_node *prev)
75 {
76 	struct device_node *ep = NULL;
77 
78 	if (!parent)
79 		return NULL;
80 
81 	do {
82 		ep = of_get_next_child(parent, prev);
83 		if (!ep)
84 			return NULL;
85 		prev = ep;
86 	} while (!of_node_name_eq(ep, "endpoint"));
87 
88 	return ep;
89 }
90 EXPORT_SYMBOL_GPL(omapdss_of_get_next_endpoint);
91 
92 struct device_node *dss_of_port_get_parent_device(struct device_node *port)
93 {
94 	struct device_node *np;
95 	int i;
96 
97 	if (!port)
98 		return NULL;
99 
100 	np = of_get_parent(port);
101 
102 	for (i = 0; i < 2 && np; ++i) {
103 		struct property *prop;
104 
105 		prop = of_find_property(np, "compatible", NULL);
106 
107 		if (prop)
108 			return np;
109 
110 		np = of_get_next_parent(np);
111 	}
112 
113 	return NULL;
114 }
115 
116 u32 dss_of_port_get_port_number(struct device_node *port)
117 {
118 	int r;
119 	u32 reg;
120 
121 	r = of_property_read_u32(port, "reg", &reg);
122 	if (r)
123 		reg = 0;
124 
125 	return reg;
126 }
127 
128 static struct device_node *omapdss_of_get_remote_port(const struct device_node *node)
129 {
130 	struct device_node *np;
131 
132 	np = of_graph_get_remote_endpoint(node);
133 	if (!np)
134 		return NULL;
135 
136 	np = of_get_next_parent(np);
137 
138 	return np;
139 }
140 
141 struct device_node *
142 omapdss_of_get_first_endpoint(const struct device_node *parent)
143 {
144 	struct device_node *port, *ep;
145 
146 	port = omapdss_of_get_next_port(parent, NULL);
147 
148 	if (!port)
149 		return NULL;
150 
151 	ep = omapdss_of_get_next_endpoint(port, NULL);
152 
153 	of_node_put(port);
154 
155 	return ep;
156 }
157 EXPORT_SYMBOL_GPL(omapdss_of_get_first_endpoint);
158 
159 struct omap_dss_device *
160 omapdss_of_find_source_for_first_ep(struct device_node *node)
161 {
162 	struct device_node *ep;
163 	struct device_node *src_port;
164 	struct omap_dss_device *src;
165 
166 	ep = omapdss_of_get_first_endpoint(node);
167 	if (!ep)
168 		return ERR_PTR(-EINVAL);
169 
170 	src_port = omapdss_of_get_remote_port(ep);
171 	if (!src_port) {
172 		of_node_put(ep);
173 		return ERR_PTR(-EINVAL);
174 	}
175 
176 	of_node_put(ep);
177 
178 	src = omap_dss_find_output_by_port_node(src_port);
179 
180 	of_node_put(src_port);
181 
182 	return src ? src : ERR_PTR(-EPROBE_DEFER);
183 }
184 EXPORT_SYMBOL_GPL(omapdss_of_find_source_for_first_ep);
185