treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / omapdrm / dss / base.c
bloba1970b9db6ab20ce8e95ccc4bb8c243b50e2a0ea
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * OMAP Display Subsystem Base
5 * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
6 */
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>
16 #include "dss.h"
17 #include "omapdss.h"
19 static struct dss_device *dss_device;
21 struct dss_device *omapdss_get_dss(void)
23 return dss_device;
25 EXPORT_SYMBOL(omapdss_get_dss);
27 void omapdss_set_dss(struct dss_device *dss)
29 dss_device = dss;
31 EXPORT_SYMBOL(omapdss_set_dss);
33 struct dispc_device *dispc_get_dispc(struct dss_device *dss)
35 return dss->dispc;
37 EXPORT_SYMBOL(dispc_get_dispc);
39 const struct dispc_ops *dispc_get_ops(struct dss_device *dss)
41 return dss->dispc_ops;
43 EXPORT_SYMBOL(dispc_get_ops);
46 /* -----------------------------------------------------------------------------
47 * OMAP DSS Devices Handling
50 static LIST_HEAD(omapdss_devices_list);
51 static DEFINE_MUTEX(omapdss_devices_lock);
53 void omapdss_device_register(struct omap_dss_device *dssdev)
55 mutex_lock(&omapdss_devices_lock);
56 list_add_tail(&dssdev->list, &omapdss_devices_list);
57 mutex_unlock(&omapdss_devices_lock);
59 EXPORT_SYMBOL_GPL(omapdss_device_register);
61 void omapdss_device_unregister(struct omap_dss_device *dssdev)
63 mutex_lock(&omapdss_devices_lock);
64 list_del(&dssdev->list);
65 mutex_unlock(&omapdss_devices_lock);
67 EXPORT_SYMBOL_GPL(omapdss_device_unregister);
69 static bool omapdss_device_is_registered(struct device_node *node)
71 struct omap_dss_device *dssdev;
72 bool found = false;
74 mutex_lock(&omapdss_devices_lock);
76 list_for_each_entry(dssdev, &omapdss_devices_list, list) {
77 if (dssdev->dev->of_node == node) {
78 found = true;
79 break;
83 mutex_unlock(&omapdss_devices_lock);
84 return found;
87 struct omap_dss_device *omapdss_device_get(struct omap_dss_device *dssdev)
89 if (!try_module_get(dssdev->owner))
90 return NULL;
92 if (get_device(dssdev->dev) == NULL) {
93 module_put(dssdev->owner);
94 return NULL;
97 return dssdev;
99 EXPORT_SYMBOL(omapdss_device_get);
101 void omapdss_device_put(struct omap_dss_device *dssdev)
103 put_device(dssdev->dev);
104 module_put(dssdev->owner);
106 EXPORT_SYMBOL(omapdss_device_put);
108 struct omap_dss_device *omapdss_find_device_by_node(struct device_node *node)
110 struct omap_dss_device *dssdev;
112 list_for_each_entry(dssdev, &omapdss_devices_list, list) {
113 if (dssdev->dev->of_node == node)
114 return omapdss_device_get(dssdev);
117 return NULL;
121 * Search for the next output device starting at @from. Release the reference to
122 * the @from device, and acquire a reference to the returned device if found.
124 struct omap_dss_device *omapdss_device_next_output(struct omap_dss_device *from)
126 struct omap_dss_device *dssdev;
127 struct list_head *list;
129 mutex_lock(&omapdss_devices_lock);
131 if (list_empty(&omapdss_devices_list)) {
132 dssdev = NULL;
133 goto done;
137 * Start from the from entry if given or from omapdss_devices_list
138 * otherwise.
140 list = from ? &from->list : &omapdss_devices_list;
142 list_for_each_entry(dssdev, list, list) {
144 * Stop if we reach the omapdss_devices_list, that's the end of
145 * the list.
147 if (&dssdev->list == &omapdss_devices_list) {
148 dssdev = NULL;
149 goto done;
152 if (dssdev->id &&
153 (dssdev->next || dssdev->bridge || dssdev->panel))
154 goto done;
157 dssdev = NULL;
159 done:
160 if (from)
161 omapdss_device_put(from);
162 if (dssdev)
163 omapdss_device_get(dssdev);
165 mutex_unlock(&omapdss_devices_lock);
166 return dssdev;
168 EXPORT_SYMBOL(omapdss_device_next_output);
170 static bool omapdss_device_is_connected(struct omap_dss_device *dssdev)
172 return dssdev->dss;
175 int omapdss_device_connect(struct dss_device *dss,
176 struct omap_dss_device *src,
177 struct omap_dss_device *dst)
179 int ret;
181 dev_dbg(&dss->pdev->dev, "connect(%s, %s)\n",
182 src ? dev_name(src->dev) : "NULL",
183 dst ? dev_name(dst->dev) : "NULL");
185 if (!dst) {
187 * The destination is NULL when the source is connected to a
188 * bridge or panel instead of a DSS device. Stop here, we will
189 * attach the bridge or panel later when we will have a DRM
190 * encoder.
192 return src && (src->bridge || src->panel) ? 0 : -EINVAL;
195 if (omapdss_device_is_connected(dst))
196 return -EBUSY;
198 dst->dss = dss;
200 ret = dst->ops->connect(src, dst);
201 if (ret < 0) {
202 dst->dss = NULL;
203 return ret;
206 return 0;
208 EXPORT_SYMBOL_GPL(omapdss_device_connect);
210 void omapdss_device_disconnect(struct omap_dss_device *src,
211 struct omap_dss_device *dst)
213 struct dss_device *dss = src ? src->dss : dst->dss;
215 dev_dbg(&dss->pdev->dev, "disconnect(%s, %s)\n",
216 src ? dev_name(src->dev) : "NULL",
217 dst ? dev_name(dst->dev) : "NULL");
219 if (!dst) {
220 WARN_ON(!src->bridge && !src->panel);
221 return;
224 if (!dst->id && !omapdss_device_is_connected(dst)) {
225 WARN_ON(!dst->display);
226 return;
229 WARN_ON(dst->state != OMAP_DSS_DISPLAY_DISABLED);
231 dst->ops->disconnect(src, dst);
232 dst->dss = NULL;
234 EXPORT_SYMBOL_GPL(omapdss_device_disconnect);
236 void omapdss_device_pre_enable(struct omap_dss_device *dssdev)
238 if (!dssdev)
239 return;
241 omapdss_device_pre_enable(dssdev->next);
243 if (dssdev->ops->pre_enable)
244 dssdev->ops->pre_enable(dssdev);
246 EXPORT_SYMBOL_GPL(omapdss_device_pre_enable);
248 void omapdss_device_enable(struct omap_dss_device *dssdev)
250 if (!dssdev)
251 return;
253 if (dssdev->ops->enable)
254 dssdev->ops->enable(dssdev);
256 omapdss_device_enable(dssdev->next);
258 dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
260 EXPORT_SYMBOL_GPL(omapdss_device_enable);
262 void omapdss_device_disable(struct omap_dss_device *dssdev)
264 if (!dssdev)
265 return;
267 omapdss_device_disable(dssdev->next);
269 if (dssdev->ops->disable)
270 dssdev->ops->disable(dssdev);
272 EXPORT_SYMBOL_GPL(omapdss_device_disable);
274 void omapdss_device_post_disable(struct omap_dss_device *dssdev)
276 if (!dssdev)
277 return;
279 if (dssdev->ops->post_disable)
280 dssdev->ops->post_disable(dssdev);
282 omapdss_device_post_disable(dssdev->next);
284 dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
286 EXPORT_SYMBOL_GPL(omapdss_device_post_disable);
288 /* -----------------------------------------------------------------------------
289 * Components Handling
292 static struct list_head omapdss_comp_list;
294 struct omapdss_comp_node {
295 struct list_head list;
296 struct device_node *node;
297 bool dss_core_component;
298 const char *compat;
301 static bool omapdss_list_contains(const struct device_node *node)
303 struct omapdss_comp_node *comp;
305 list_for_each_entry(comp, &omapdss_comp_list, list) {
306 if (comp->node == node)
307 return true;
310 return false;
313 static void omapdss_walk_device(struct device *dev, struct device_node *node,
314 bool dss_core)
316 struct omapdss_comp_node *comp;
317 struct device_node *n;
318 const char *compat;
319 int ret;
321 ret = of_property_read_string(node, "compatible", &compat);
322 if (ret < 0)
323 return;
325 comp = devm_kzalloc(dev, sizeof(*comp), GFP_KERNEL);
326 if (comp) {
327 comp->node = node;
328 comp->dss_core_component = dss_core;
329 comp->compat = compat;
330 list_add(&comp->list, &omapdss_comp_list);
334 * of_graph_get_remote_port_parent() prints an error if there is no
335 * port/ports node. To avoid that, check first that there's the node.
337 n = of_get_child_by_name(node, "ports");
338 if (!n)
339 n = of_get_child_by_name(node, "port");
340 if (!n)
341 return;
343 of_node_put(n);
345 n = NULL;
346 while ((n = of_graph_get_next_endpoint(node, n)) != NULL) {
347 struct device_node *pn = of_graph_get_remote_port_parent(n);
349 if (!pn)
350 continue;
352 if (!of_device_is_available(pn) || omapdss_list_contains(pn)) {
353 of_node_put(pn);
354 continue;
357 omapdss_walk_device(dev, pn, false);
361 void omapdss_gather_components(struct device *dev)
363 struct device_node *child;
365 INIT_LIST_HEAD(&omapdss_comp_list);
367 omapdss_walk_device(dev, dev->of_node, true);
369 for_each_available_child_of_node(dev->of_node, child)
370 omapdss_walk_device(dev, child, true);
372 EXPORT_SYMBOL(omapdss_gather_components);
374 static bool omapdss_component_is_loaded(struct omapdss_comp_node *comp)
376 if (comp->dss_core_component)
377 return true;
378 if (!strstarts(comp->compat, "omapdss,"))
379 return true;
380 if (omapdss_device_is_registered(comp->node))
381 return true;
383 return false;
386 bool omapdss_stack_is_ready(void)
388 struct omapdss_comp_node *comp;
390 list_for_each_entry(comp, &omapdss_comp_list, list) {
391 if (!omapdss_component_is_loaded(comp))
392 return false;
395 return true;
397 EXPORT_SYMBOL(omapdss_stack_is_ready);
399 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
400 MODULE_DESCRIPTION("OMAP Display Subsystem Base");
401 MODULE_LICENSE("GPL v2");