Backmerge tag 'v4.16-rc7' into drm-next
[linux-2.6-microblaze.git] / drivers / gpu / drm / sun4i / sun4i_drv.c
1 /*
2  * Copyright (C) 2015 Free Electrons
3  * Copyright (C) 2015 NextThing Co
4  *
5  * Maxime Ripard <maxime.ripard@free-electrons.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  */
12
13 #include <linux/component.h>
14 #include <linux/kfifo.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_reserved_mem.h>
17
18 #include <drm/drmP.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_fb_cma_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_of.h>
24
25 #include "sun4i_drv.h"
26 #include "sun4i_frontend.h"
27 #include "sun4i_framebuffer.h"
28 #include "sun4i_tcon.h"
29
30 DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops);
31
32 static struct drm_driver sun4i_drv_driver = {
33         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
34
35         /* Generic Operations */
36         .lastclose              = drm_fb_helper_lastclose,
37         .fops                   = &sun4i_drv_fops,
38         .name                   = "sun4i-drm",
39         .desc                   = "Allwinner sun4i Display Engine",
40         .date                   = "20150629",
41         .major                  = 1,
42         .minor                  = 0,
43
44         /* GEM Operations */
45         .dumb_create            = drm_gem_cma_dumb_create,
46         .gem_free_object_unlocked = drm_gem_cma_free_object,
47         .gem_vm_ops             = &drm_gem_cma_vm_ops,
48
49         /* PRIME Operations */
50         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
51         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
52         .gem_prime_import       = drm_gem_prime_import,
53         .gem_prime_export       = drm_gem_prime_export,
54         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
55         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
56         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
57         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
58         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
59
60         /* Frame Buffer Operations */
61 };
62
63 static void sun4i_remove_framebuffers(void)
64 {
65         struct apertures_struct *ap;
66
67         ap = alloc_apertures(1);
68         if (!ap)
69                 return;
70
71         /* The framebuffer can be located anywhere in RAM */
72         ap->ranges[0].base = 0;
73         ap->ranges[0].size = ~0;
74
75         drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
76         kfree(ap);
77 }
78
79 static int sun4i_drv_bind(struct device *dev)
80 {
81         struct drm_device *drm;
82         struct sun4i_drv *drv;
83         int ret;
84
85         drm = drm_dev_alloc(&sun4i_drv_driver, dev);
86         if (IS_ERR(drm))
87                 return PTR_ERR(drm);
88
89         drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
90         if (!drv) {
91                 ret = -ENOMEM;
92                 goto free_drm;
93         }
94         drm->dev_private = drv;
95         INIT_LIST_HEAD(&drv->frontend_list);
96         INIT_LIST_HEAD(&drv->engine_list);
97         INIT_LIST_HEAD(&drv->tcon_list);
98
99         ret = of_reserved_mem_device_init(dev);
100         if (ret && ret != -ENODEV) {
101                 dev_err(drm->dev, "Couldn't claim our memory region\n");
102                 goto free_drm;
103         }
104
105         drm_mode_config_init(drm);
106
107         ret = component_bind_all(drm->dev, drm);
108         if (ret) {
109                 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
110                 goto cleanup_mode_config;
111         }
112
113         /* drm_vblank_init calls kcalloc, which can fail */
114         ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
115         if (ret)
116                 goto cleanup_mode_config;
117
118         drm->irq_enabled = true;
119
120         /* Remove early framebuffers (ie. simplefb) */
121         sun4i_remove_framebuffers();
122
123         /* Create our framebuffer */
124         ret = sun4i_framebuffer_init(drm);
125         if (ret) {
126                 dev_err(drm->dev, "Couldn't create our framebuffer\n");
127                 goto cleanup_mode_config;
128         }
129
130         /* Enable connectors polling */
131         drm_kms_helper_poll_init(drm);
132
133         ret = drm_dev_register(drm, 0);
134         if (ret)
135                 goto finish_poll;
136
137         return 0;
138
139 finish_poll:
140         drm_kms_helper_poll_fini(drm);
141         sun4i_framebuffer_free(drm);
142 cleanup_mode_config:
143         drm_mode_config_cleanup(drm);
144         of_reserved_mem_device_release(dev);
145 free_drm:
146         drm_dev_unref(drm);
147         return ret;
148 }
149
150 static void sun4i_drv_unbind(struct device *dev)
151 {
152         struct drm_device *drm = dev_get_drvdata(dev);
153
154         drm_dev_unregister(drm);
155         drm_kms_helper_poll_fini(drm);
156         sun4i_framebuffer_free(drm);
157         drm_mode_config_cleanup(drm);
158         of_reserved_mem_device_release(dev);
159         drm_dev_unref(drm);
160 }
161
162 static const struct component_master_ops sun4i_drv_master_ops = {
163         .bind   = sun4i_drv_bind,
164         .unbind = sun4i_drv_unbind,
165 };
166
167 static bool sun4i_drv_node_is_connector(struct device_node *node)
168 {
169         return of_device_is_compatible(node, "hdmi-connector");
170 }
171
172 static bool sun4i_drv_node_is_frontend(struct device_node *node)
173 {
174         return of_device_is_compatible(node, "allwinner,sun4i-a10-display-frontend") ||
175                 of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
176                 of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
177                 of_device_is_compatible(node, "allwinner,sun7i-a20-display-frontend") ||
178                 of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend") ||
179                 of_device_is_compatible(node, "allwinner,sun9i-a80-display-frontend");
180 }
181
182 static bool sun4i_drv_node_is_deu(struct device_node *node)
183 {
184         return of_device_is_compatible(node, "allwinner,sun9i-a80-deu");
185 }
186
187 static bool sun4i_drv_node_is_supported_frontend(struct device_node *node)
188 {
189         if (IS_ENABLED(CONFIG_DRM_SUN4I_BACKEND))
190                 return !!of_match_node(sun4i_frontend_of_table, node);
191
192         return false;
193 }
194
195 static bool sun4i_drv_node_is_tcon(struct device_node *node)
196 {
197         return !!of_match_node(sun4i_tcon_of_table, node);
198 }
199
200 static int compare_of(struct device *dev, void *data)
201 {
202         DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
203                          dev->of_node,
204                          data);
205
206         return dev->of_node == data;
207 }
208
209 /*
210  * The encoder drivers use drm_of_find_possible_crtcs to get upstream
211  * crtcs from the device tree using of_graph. For the results to be
212  * correct, encoders must be probed/bound after _all_ crtcs have been
213  * created. The existing code uses a depth first recursive traversal
214  * of the of_graph, which means the encoders downstream of the TCON
215  * get add right after the first TCON. The second TCON or CRTC will
216  * never be properly associated with encoders connected to it.
217  *
218  * Also, in a dual display pipeline setup, both frontends can feed
219  * either backend, and both backends can feed either TCON, we want
220  * all components of the same type to be added before the next type
221  * in the pipeline. Fortunately, the pipelines are perfectly symmetric,
222  * i.e. components of the same type are at the same depth when counted
223  * from the frontend. The only exception is the third pipeline in
224  * the A80 SoC, which we do not support anyway.
225  *
226  * Hence we can use a breadth first search traversal order to add
227  * components. We do not need to check for duplicates. The component
228  * matching system handles this for us.
229  */
230 struct endpoint_list {
231         DECLARE_KFIFO(fifo, struct device_node *, 16);
232 };
233
234 static int sun4i_drv_add_endpoints(struct device *dev,
235                                    struct endpoint_list *list,
236                                    struct component_match **match,
237                                    struct device_node *node)
238 {
239         struct device_node *port, *ep, *remote;
240         int count = 0;
241
242         /*
243          * The frontend has been disabled in some of our old device
244          * trees. If we find a node that is the frontend and is
245          * disabled, we should just follow through and parse its
246          * child, but without adding it to the component list.
247          * Otherwise, we obviously want to add it to the list.
248          */
249         if (!sun4i_drv_node_is_frontend(node) &&
250             !of_device_is_available(node))
251                 return 0;
252
253         /*
254          * The connectors will be the last nodes in our pipeline, we
255          * can just bail out.
256          */
257         if (sun4i_drv_node_is_connector(node))
258                 return 0;
259
260         /*
261          * If the device is either just a regular device, or an
262          * enabled frontend supported by the driver, we add it to our
263          * component list.
264          */
265         if (!(sun4i_drv_node_is_frontend(node) ||
266               sun4i_drv_node_is_deu(node)) ||
267             (sun4i_drv_node_is_supported_frontend(node) &&
268              of_device_is_available(node))) {
269                 /* Add current component */
270                 DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
271                 drm_of_component_match_add(dev, match, compare_of, node);
272                 count++;
273         }
274
275         /* Inputs are listed first, then outputs */
276         port = of_graph_get_port_by_id(node, 1);
277         if (!port) {
278                 DRM_DEBUG_DRIVER("No output to bind\n");
279                 return count;
280         }
281
282         for_each_available_child_of_node(port, ep) {
283                 remote = of_graph_get_remote_port_parent(ep);
284                 if (!remote) {
285                         DRM_DEBUG_DRIVER("Error retrieving the output node\n");
286                         of_node_put(remote);
287                         continue;
288                 }
289
290                 /*
291                  * If the node is our TCON, the first port is used for
292                  * panel or bridges, and will not be part of the
293                  * component framework.
294                  */
295                 if (sun4i_drv_node_is_tcon(node)) {
296                         struct of_endpoint endpoint;
297
298                         if (of_graph_parse_endpoint(ep, &endpoint)) {
299                                 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
300                                 continue;
301                         }
302
303                         if (!endpoint.id) {
304                                 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
305                                 continue;
306                         }
307                 }
308
309                 kfifo_put(&list->fifo, remote);
310         }
311
312         return count;
313 }
314
315 static int sun4i_drv_probe(struct platform_device *pdev)
316 {
317         struct component_match *match = NULL;
318         struct device_node *np = pdev->dev.of_node, *endpoint;
319         struct endpoint_list list;
320         int i, ret, count = 0;
321
322         INIT_KFIFO(list.fifo);
323
324         for (i = 0;; i++) {
325                 struct device_node *pipeline = of_parse_phandle(np,
326                                                                 "allwinner,pipelines",
327                                                                 i);
328                 if (!pipeline)
329                         break;
330
331                 kfifo_put(&list.fifo, pipeline);
332         }
333
334         while (kfifo_get(&list.fifo, &endpoint)) {
335                 /* process this endpoint */
336                 ret = sun4i_drv_add_endpoints(&pdev->dev, &list, &match,
337                                               endpoint);
338
339                 /* sun4i_drv_add_endpoints can fail to allocate memory */
340                 if (ret < 0)
341                         return ret;
342
343                 count += ret;
344         }
345
346         if (count)
347                 return component_master_add_with_match(&pdev->dev,
348                                                        &sun4i_drv_master_ops,
349                                                        match);
350         else
351                 return 0;
352 }
353
354 static int sun4i_drv_remove(struct platform_device *pdev)
355 {
356         return 0;
357 }
358
359 static const struct of_device_id sun4i_drv_of_table[] = {
360         { .compatible = "allwinner,sun4i-a10-display-engine" },
361         { .compatible = "allwinner,sun5i-a10s-display-engine" },
362         { .compatible = "allwinner,sun5i-a13-display-engine" },
363         { .compatible = "allwinner,sun6i-a31-display-engine" },
364         { .compatible = "allwinner,sun6i-a31s-display-engine" },
365         { .compatible = "allwinner,sun7i-a20-display-engine" },
366         { .compatible = "allwinner,sun8i-a33-display-engine" },
367         { .compatible = "allwinner,sun8i-a83t-display-engine" },
368         { .compatible = "allwinner,sun8i-h3-display-engine" },
369         { .compatible = "allwinner,sun8i-v3s-display-engine" },
370         { .compatible = "allwinner,sun9i-a80-display-engine" },
371         { }
372 };
373 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
374
375 static struct platform_driver sun4i_drv_platform_driver = {
376         .probe          = sun4i_drv_probe,
377         .remove         = sun4i_drv_remove,
378         .driver         = {
379                 .name           = "sun4i-drm",
380                 .of_match_table = sun4i_drv_of_table,
381         },
382 };
383 module_platform_driver(sun4i_drv_platform_driver);
384
385 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
386 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
387 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
388 MODULE_LICENSE("GPL");