]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/omapdrm/dss/base.c
drm/omap: Merge omap_dss_device type and output_type fields
[linux.git] / drivers / gpu / drm / omapdrm / dss / base.c
1 /*
2  * OMAP Display Subsystem Base
3  *
4  * Copyright (C) 2015-2017 Texas Instruments Incorporated - http://www.ti.com/
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/of.h>
21 #include <linux/of_graph.h>
22
23 #include "dss.h"
24 #include "omapdss.h"
25
26 static struct dss_device *dss_device;
27
28 struct dss_device *omapdss_get_dss(void)
29 {
30         return dss_device;
31 }
32 EXPORT_SYMBOL(omapdss_get_dss);
33
34 void omapdss_set_dss(struct dss_device *dss)
35 {
36         dss_device = dss;
37 }
38 EXPORT_SYMBOL(omapdss_set_dss);
39
40 struct dispc_device *dispc_get_dispc(struct dss_device *dss)
41 {
42         return dss->dispc;
43 }
44 EXPORT_SYMBOL(dispc_get_dispc);
45
46 const struct dispc_ops *dispc_get_ops(struct dss_device *dss)
47 {
48         return dss->dispc_ops;
49 }
50 EXPORT_SYMBOL(dispc_get_ops);
51
52
53 /* -----------------------------------------------------------------------------
54  * OMAP DSS Devices Handling
55  */
56
57 static LIST_HEAD(omapdss_devices_list);
58 static DEFINE_MUTEX(omapdss_devices_lock);
59
60 void omapdss_device_register(struct omap_dss_device *dssdev)
61 {
62         mutex_lock(&omapdss_devices_lock);
63         list_add_tail(&dssdev->list, &omapdss_devices_list);
64         mutex_unlock(&omapdss_devices_lock);
65 }
66 EXPORT_SYMBOL_GPL(omapdss_device_register);
67
68 void omapdss_device_unregister(struct omap_dss_device *dssdev)
69 {
70         mutex_lock(&omapdss_devices_lock);
71         list_del(&dssdev->list);
72         mutex_unlock(&omapdss_devices_lock);
73 }
74 EXPORT_SYMBOL_GPL(omapdss_device_unregister);
75
76 static bool omapdss_device_is_registered(struct device_node *node)
77 {
78         struct omap_dss_device *dssdev;
79         bool found = false;
80
81         mutex_lock(&omapdss_devices_lock);
82
83         list_for_each_entry(dssdev, &omapdss_devices_list, list) {
84                 if (dssdev->dev->of_node == node) {
85                         found = true;
86                         break;
87                 }
88         }
89
90         mutex_unlock(&omapdss_devices_lock);
91         return found;
92 }
93
94 struct omap_dss_device *omapdss_device_get(struct omap_dss_device *dssdev)
95 {
96         if (!try_module_get(dssdev->owner))
97                 return NULL;
98
99         if (get_device(dssdev->dev) == NULL) {
100                 module_put(dssdev->owner);
101                 return NULL;
102         }
103
104         return dssdev;
105 }
106 EXPORT_SYMBOL(omapdss_device_get);
107
108 void omapdss_device_put(struct omap_dss_device *dssdev)
109 {
110         put_device(dssdev->dev);
111         module_put(dssdev->owner);
112 }
113 EXPORT_SYMBOL(omapdss_device_put);
114
115 struct omap_dss_device *omapdss_find_device_by_node(struct device_node *node)
116 {
117         struct omap_dss_device *dssdev;
118
119         list_for_each_entry(dssdev, &omapdss_devices_list, list) {
120                 if (dssdev->dev->of_node == node)
121                         return omapdss_device_get(dssdev);
122         }
123
124         return NULL;
125 }
126
127 /*
128  * Search for the next output device starting at @from. Release the reference to
129  * the @from device, and acquire a reference to the returned device if found.
130  */
131 struct omap_dss_device *omapdss_device_next_output(struct omap_dss_device *from)
132 {
133         struct omap_dss_device *dssdev;
134         struct list_head *list;
135
136         mutex_lock(&omapdss_devices_lock);
137
138         if (list_empty(&omapdss_devices_list)) {
139                 dssdev = NULL;
140                 goto done;
141         }
142
143         /*
144          * Start from the from entry if given or from omapdss_devices_list
145          * otherwise.
146          */
147         list = from ? &from->list : &omapdss_devices_list;
148
149         list_for_each_entry(dssdev, list, list) {
150                 /*
151                  * Stop if we reach the omapdss_devices_list, that's the end of
152                  * the list.
153                  */
154                 if (&dssdev->list == &omapdss_devices_list) {
155                         dssdev = NULL;
156                         goto done;
157                 }
158
159                 if (dssdev->id && dssdev->next)
160                         goto done;
161         }
162
163         dssdev = NULL;
164
165 done:
166         if (from)
167                 omapdss_device_put(from);
168         if (dssdev)
169                 omapdss_device_get(dssdev);
170
171         mutex_unlock(&omapdss_devices_lock);
172         return dssdev;
173 }
174 EXPORT_SYMBOL(omapdss_device_next_output);
175
176 static bool omapdss_device_is_connected(struct omap_dss_device *dssdev)
177 {
178         return dssdev->dss;
179 }
180
181 int omapdss_device_connect(struct dss_device *dss,
182                            struct omap_dss_device *src,
183                            struct omap_dss_device *dst)
184 {
185         int ret;
186
187         dev_dbg(dst->dev, "connect\n");
188
189         if (omapdss_device_is_connected(dst))
190                 return -EBUSY;
191
192         dst->dss = dss;
193
194         ret = dst->ops->connect(src, dst);
195         if (ret < 0) {
196                 dst->dss = NULL;
197                 return ret;
198         }
199
200         return 0;
201 }
202 EXPORT_SYMBOL_GPL(omapdss_device_connect);
203
204 void omapdss_device_disconnect(struct omap_dss_device *src,
205                                struct omap_dss_device *dst)
206 {
207         dev_dbg(dst->dev, "disconnect\n");
208
209         if (!dst->id && !omapdss_device_is_connected(dst)) {
210                 WARN_ON(!dst->display);
211                 return;
212         }
213
214         WARN_ON(dst->state != OMAP_DSS_DISPLAY_DISABLED);
215
216         dst->ops->disconnect(src, dst);
217         dst->dss = NULL;
218 }
219 EXPORT_SYMBOL_GPL(omapdss_device_disconnect);
220
221 void omapdss_device_pre_enable(struct omap_dss_device *dssdev)
222 {
223         if (!dssdev)
224                 return;
225
226         omapdss_device_pre_enable(dssdev->next);
227
228         if (dssdev->ops->pre_enable)
229                 dssdev->ops->pre_enable(dssdev);
230 }
231 EXPORT_SYMBOL_GPL(omapdss_device_pre_enable);
232
233 void omapdss_device_enable(struct omap_dss_device *dssdev)
234 {
235         if (!dssdev)
236                 return;
237
238         if (dssdev->ops->enable)
239                 dssdev->ops->enable(dssdev);
240
241         omapdss_device_enable(dssdev->next);
242
243         dssdev->state = OMAP_DSS_DISPLAY_ACTIVE;
244 }
245 EXPORT_SYMBOL_GPL(omapdss_device_enable);
246
247 void omapdss_device_disable(struct omap_dss_device *dssdev)
248 {
249         if (!dssdev)
250                 return;
251
252         omapdss_device_disable(dssdev->next);
253
254         if (dssdev->ops->disable)
255                 dssdev->ops->disable(dssdev);
256 }
257 EXPORT_SYMBOL_GPL(omapdss_device_disable);
258
259 void omapdss_device_post_disable(struct omap_dss_device *dssdev)
260 {
261         if (!dssdev)
262                 return;
263
264         if (dssdev->ops->post_disable)
265                 dssdev->ops->post_disable(dssdev);
266
267         omapdss_device_post_disable(dssdev->next);
268
269         dssdev->state = OMAP_DSS_DISPLAY_DISABLED;
270 }
271 EXPORT_SYMBOL_GPL(omapdss_device_post_disable);
272
273 /* -----------------------------------------------------------------------------
274  * Components Handling
275  */
276
277 static struct list_head omapdss_comp_list;
278
279 struct omapdss_comp_node {
280         struct list_head list;
281         struct device_node *node;
282         bool dss_core_component;
283 };
284
285 static bool omapdss_list_contains(const struct device_node *node)
286 {
287         struct omapdss_comp_node *comp;
288
289         list_for_each_entry(comp, &omapdss_comp_list, list) {
290                 if (comp->node == node)
291                         return true;
292         }
293
294         return false;
295 }
296
297 static void omapdss_walk_device(struct device *dev, struct device_node *node,
298                                 bool dss_core)
299 {
300         struct device_node *n;
301         struct omapdss_comp_node *comp = devm_kzalloc(dev, sizeof(*comp),
302                                                       GFP_KERNEL);
303
304         if (comp) {
305                 comp->node = node;
306                 comp->dss_core_component = dss_core;
307                 list_add(&comp->list, &omapdss_comp_list);
308         }
309
310         /*
311          * of_graph_get_remote_port_parent() prints an error if there is no
312          * port/ports node. To avoid that, check first that there's the node.
313          */
314         n = of_get_child_by_name(node, "ports");
315         if (!n)
316                 n = of_get_child_by_name(node, "port");
317         if (!n)
318                 return;
319
320         of_node_put(n);
321
322         n = NULL;
323         while ((n = of_graph_get_next_endpoint(node, n)) != NULL) {
324                 struct device_node *pn = of_graph_get_remote_port_parent(n);
325
326                 if (!pn)
327                         continue;
328
329                 if (!of_device_is_available(pn) || omapdss_list_contains(pn)) {
330                         of_node_put(pn);
331                         continue;
332                 }
333
334                 omapdss_walk_device(dev, pn, false);
335         }
336 }
337
338 void omapdss_gather_components(struct device *dev)
339 {
340         struct device_node *child;
341
342         INIT_LIST_HEAD(&omapdss_comp_list);
343
344         omapdss_walk_device(dev, dev->of_node, true);
345
346         for_each_available_child_of_node(dev->of_node, child) {
347                 if (!of_find_property(child, "compatible", NULL))
348                         continue;
349
350                 omapdss_walk_device(dev, child, true);
351         }
352 }
353 EXPORT_SYMBOL(omapdss_gather_components);
354
355 static bool omapdss_component_is_loaded(struct omapdss_comp_node *comp)
356 {
357         if (comp->dss_core_component)
358                 return true;
359         if (omapdss_device_is_registered(comp->node))
360                 return true;
361
362         return false;
363 }
364
365 bool omapdss_stack_is_ready(void)
366 {
367         struct omapdss_comp_node *comp;
368
369         list_for_each_entry(comp, &omapdss_comp_list, list) {
370                 if (!omapdss_component_is_loaded(comp))
371                         return false;
372         }
373
374         return true;
375 }
376 EXPORT_SYMBOL(omapdss_stack_is_ready);
377
378 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
379 MODULE_DESCRIPTION("OMAP Display Subsystem Base");
380 MODULE_LICENSE("GPL v2");