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