xref: /openbmc/linux/drivers/gpu/drm/omapdrm/dss/base.c (revision 55b68fb856b5f0e03404c4def1b11accbe0fda77)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * OMAP Display Subsystem Base
4  *
5  * Copyright (C) 2015-2017 Texas Instruments Incorporated - https://www.ti.com/
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/list.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
12 #include <linux/of.h>
13 #include <linux/of_graph.h>
14 #include <linux/platform_device.h>
15 
16 #include "dss.h"
17 #include "omapdss.h"
18 
19 struct dispc_device *dispc_get_dispc(struct dss_device *dss)
20 {
21 	return dss->dispc;
22 }
23 
24 const struct dispc_ops *dispc_get_ops(struct dss_device *dss)
25 {
26 	return dss->dispc_ops;
27 }
28 
29 /* -----------------------------------------------------------------------------
30  * OMAP DSS Devices Handling
31  */
32 
33 static LIST_HEAD(omapdss_devices_list);
34 static DEFINE_MUTEX(omapdss_devices_lock);
35 
36 void omapdss_device_register(struct omap_dss_device *dssdev)
37 {
38 	mutex_lock(&omapdss_devices_lock);
39 	list_add_tail(&dssdev->list, &omapdss_devices_list);
40 	mutex_unlock(&omapdss_devices_lock);
41 }
42 
43 void omapdss_device_unregister(struct omap_dss_device *dssdev)
44 {
45 	mutex_lock(&omapdss_devices_lock);
46 	list_del(&dssdev->list);
47 	mutex_unlock(&omapdss_devices_lock);
48 }
49 
50 static bool omapdss_device_is_registered(struct device_node *node)
51 {
52 	struct omap_dss_device *dssdev;
53 	bool found = false;
54 
55 	mutex_lock(&omapdss_devices_lock);
56 
57 	list_for_each_entry(dssdev, &omapdss_devices_list, list) {
58 		if (dssdev->dev->of_node == node) {
59 			found = true;
60 			break;
61 		}
62 	}
63 
64 	mutex_unlock(&omapdss_devices_lock);
65 	return found;
66 }
67 
68 struct omap_dss_device *omapdss_device_get(struct omap_dss_device *dssdev)
69 {
70 	if (get_device(dssdev->dev) == NULL)
71 		return NULL;
72 
73 	return dssdev;
74 }
75 
76 void omapdss_device_put(struct omap_dss_device *dssdev)
77 {
78 	put_device(dssdev->dev);
79 }
80 
81 struct omap_dss_device *omapdss_find_device_by_node(struct device_node *node)
82 {
83 	struct omap_dss_device *dssdev;
84 
85 	list_for_each_entry(dssdev, &omapdss_devices_list, list) {
86 		if (dssdev->dev->of_node == node)
87 			return omapdss_device_get(dssdev);
88 	}
89 
90 	return NULL;
91 }
92 
93 /*
94  * Search for the next output device starting at @from. Release the reference to
95  * the @from device, and acquire a reference to the returned device if found.
96  */
97 struct omap_dss_device *omapdss_device_next_output(struct omap_dss_device *from)
98 {
99 	struct omap_dss_device *dssdev;
100 	struct list_head *list;
101 
102 	mutex_lock(&omapdss_devices_lock);
103 
104 	if (list_empty(&omapdss_devices_list)) {
105 		dssdev = NULL;
106 		goto done;
107 	}
108 
109 	/*
110 	 * Start from the from entry if given or from omapdss_devices_list
111 	 * otherwise.
112 	 */
113 	list = from ? &from->list : &omapdss_devices_list;
114 
115 	list_for_each_entry(dssdev, list, list) {
116 		/*
117 		 * Stop if we reach the omapdss_devices_list, that's the end of
118 		 * the list.
119 		 */
120 		if (&dssdev->list == &omapdss_devices_list) {
121 			dssdev = NULL;
122 			goto done;
123 		}
124 
125 		if (dssdev->id && dssdev->bridge)
126 			goto done;
127 	}
128 
129 	dssdev = NULL;
130 
131 done:
132 	if (from)
133 		omapdss_device_put(from);
134 	if (dssdev)
135 		omapdss_device_get(dssdev);
136 
137 	mutex_unlock(&omapdss_devices_lock);
138 	return dssdev;
139 }
140 
141 static bool omapdss_device_is_connected(struct omap_dss_device *dssdev)
142 {
143 	return dssdev->dss;
144 }
145 
146 int omapdss_device_connect(struct dss_device *dss,
147 			   struct omap_dss_device *src,
148 			   struct omap_dss_device *dst)
149 {
150 	dev_dbg(&dss->pdev->dev, "connect(%s, %s)\n",
151 		src ? dev_name(src->dev) : "NULL",
152 		dst ? dev_name(dst->dev) : "NULL");
153 
154 	if (!dst) {
155 		/*
156 		 * The destination is NULL when the source is connected to a
157 		 * bridge instead of a DSS device. Stop here, we will attach
158 		 * the bridge later when we will have a DRM encoder.
159 		 */
160 		return src && src->bridge ? 0 : -EINVAL;
161 	}
162 
163 	if (omapdss_device_is_connected(dst))
164 		return -EBUSY;
165 
166 	dst->dss = dss;
167 
168 	return 0;
169 }
170 
171 void omapdss_device_disconnect(struct omap_dss_device *src,
172 			       struct omap_dss_device *dst)
173 {
174 	struct dss_device *dss = src ? src->dss : dst->dss;
175 
176 	dev_dbg(&dss->pdev->dev, "disconnect(%s, %s)\n",
177 		src ? dev_name(src->dev) : "NULL",
178 		dst ? dev_name(dst->dev) : "NULL");
179 
180 	if (!dst) {
181 		WARN_ON(!src->bridge);
182 		return;
183 	}
184 
185 	if (!dst->id && !omapdss_device_is_connected(dst)) {
186 		WARN_ON(1);
187 		return;
188 	}
189 
190 	dst->dss = NULL;
191 }
192 
193 /* -----------------------------------------------------------------------------
194  * Components Handling
195  */
196 
197 static struct list_head omapdss_comp_list;
198 
199 struct omapdss_comp_node {
200 	struct list_head list;
201 	struct device_node *node;
202 	bool dss_core_component;
203 	const char *compat;
204 };
205 
206 static bool omapdss_list_contains(const struct device_node *node)
207 {
208 	struct omapdss_comp_node *comp;
209 
210 	list_for_each_entry(comp, &omapdss_comp_list, list) {
211 		if (comp->node == node)
212 			return true;
213 	}
214 
215 	return false;
216 }
217 
218 static void omapdss_walk_device(struct device *dev, struct device_node *node,
219 				bool dss_core)
220 {
221 	struct omapdss_comp_node *comp;
222 	struct device_node *n;
223 	const char *compat;
224 	int ret;
225 
226 	ret = of_property_read_string(node, "compatible", &compat);
227 	if (ret < 0)
228 		return;
229 
230 	comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
231 	if (comp) {
232 		comp->node = node;
233 		comp->dss_core_component = dss_core;
234 		comp->compat = compat;
235 		list_add(&comp->list, &omapdss_comp_list);
236 	}
237 
238 	/*
239 	 * of_graph_get_remote_port_parent() prints an error if there is no
240 	 * port/ports node. To avoid that, check first that there's the node.
241 	 */
242 	n = of_get_child_by_name(node, "ports");
243 	if (!n)
244 		n = of_get_child_by_name(node, "port");
245 	if (!n)
246 		return;
247 
248 	of_node_put(n);
249 
250 	n = NULL;
251 	while ((n = of_graph_get_next_endpoint(node, n)) != NULL) {
252 		struct device_node *pn = of_graph_get_remote_port_parent(n);
253 
254 		if (!pn)
255 			continue;
256 
257 		if (!of_device_is_available(pn) || omapdss_list_contains(pn)) {
258 			of_node_put(pn);
259 			continue;
260 		}
261 
262 		omapdss_walk_device(dev, pn, false);
263 	}
264 }
265 
266 void omapdss_gather_components(struct device *dev)
267 {
268 	struct device_node *child;
269 
270 	INIT_LIST_HEAD(&omapdss_comp_list);
271 
272 	omapdss_walk_device(dev, dev->of_node, true);
273 
274 	for_each_available_child_of_node(dev->of_node, child)
275 		omapdss_walk_device(dev, child, true);
276 }
277 
278 static bool omapdss_component_is_loaded(struct omapdss_comp_node *comp)
279 {
280 	if (comp->dss_core_component)
281 		return true;
282 	if (!strstarts(comp->compat, "omapdss,"))
283 		return true;
284 	if (omapdss_device_is_registered(comp->node))
285 		return true;
286 
287 	return false;
288 }
289 
290 bool omapdss_stack_is_ready(void)
291 {
292 	struct omapdss_comp_node *comp;
293 
294 	list_for_each_entry(comp, &omapdss_comp_list, list) {
295 		if (!omapdss_component_is_loaded(comp))
296 			return false;
297 	}
298 
299 	return true;
300 }
301