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