GNU Linux-libre 4.9.296-gnu1
[releases.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/of_graph.h>
15
16 #include <drm/drmP.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_fb_helper.h>
21
22 #include "sun4i_crtc.h"
23 #include "sun4i_drv.h"
24 #include "sun4i_framebuffer.h"
25 #include "sun4i_layer.h"
26 #include "sun4i_tcon.h"
27
28 static int sun4i_drv_enable_vblank(struct drm_device *drm, unsigned int pipe)
29 {
30         struct sun4i_drv *drv = drm->dev_private;
31         struct sun4i_tcon *tcon = drv->tcon;
32
33         DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe);
34
35         sun4i_tcon_enable_vblank(tcon, true);
36
37         return 0;
38 }
39
40 static void sun4i_drv_disable_vblank(struct drm_device *drm, unsigned int pipe)
41 {
42         struct sun4i_drv *drv = drm->dev_private;
43         struct sun4i_tcon *tcon = drv->tcon;
44
45         DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe);
46
47         sun4i_tcon_enable_vblank(tcon, false);
48 }
49
50 static void sun4i_drv_lastclose(struct drm_device *dev)
51 {
52         struct sun4i_drv *drv = dev->dev_private;
53
54         drm_fbdev_cma_restore_mode(drv->fbdev);
55 }
56
57 static const struct file_operations sun4i_drv_fops = {
58         .owner          = THIS_MODULE,
59         .open           = drm_open,
60         .release        = drm_release,
61         .unlocked_ioctl = drm_ioctl,
62 #ifdef CONFIG_COMPAT
63         .compat_ioctl   = drm_compat_ioctl,
64 #endif
65         .poll           = drm_poll,
66         .read           = drm_read,
67         .llseek         = no_llseek,
68         .mmap           = drm_gem_cma_mmap,
69 };
70
71 static struct drm_driver sun4i_drv_driver = {
72         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
73
74         /* Generic Operations */
75         .lastclose              = sun4i_drv_lastclose,
76         .fops                   = &sun4i_drv_fops,
77         .name                   = "sun4i-drm",
78         .desc                   = "Allwinner sun4i Display Engine",
79         .date                   = "20150629",
80         .major                  = 1,
81         .minor                  = 0,
82
83         /* GEM Operations */
84         .dumb_create            = drm_gem_cma_dumb_create,
85         .dumb_destroy           = drm_gem_dumb_destroy,
86         .dumb_map_offset        = drm_gem_cma_dumb_map_offset,
87         .gem_free_object_unlocked = drm_gem_cma_free_object,
88         .gem_vm_ops             = &drm_gem_cma_vm_ops,
89
90         /* PRIME Operations */
91         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
92         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
93         .gem_prime_import       = drm_gem_prime_import,
94         .gem_prime_export       = drm_gem_prime_export,
95         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
96         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
97         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
98         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
99         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
100
101         /* Frame Buffer Operations */
102
103         /* VBlank Operations */
104         .get_vblank_counter     = drm_vblank_no_hw_counter,
105         .enable_vblank          = sun4i_drv_enable_vblank,
106         .disable_vblank         = sun4i_drv_disable_vblank,
107 };
108
109 static void sun4i_remove_framebuffers(void)
110 {
111         struct apertures_struct *ap;
112
113         ap = alloc_apertures(1);
114         if (!ap)
115                 return;
116
117         /* The framebuffer can be located anywhere in RAM */
118         ap->ranges[0].base = 0;
119         ap->ranges[0].size = ~0;
120
121         drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
122         kfree(ap);
123 }
124
125 static int sun4i_drv_bind(struct device *dev)
126 {
127         struct drm_device *drm;
128         struct sun4i_drv *drv;
129         int ret;
130
131         drm = drm_dev_alloc(&sun4i_drv_driver, dev);
132         if (IS_ERR(drm))
133                 return PTR_ERR(drm);
134
135         drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
136         if (!drv) {
137                 ret = -ENOMEM;
138                 goto free_drm;
139         }
140
141         dev_set_drvdata(dev, drm);
142         drm->dev_private = drv;
143
144         drm_vblank_init(drm, 1);
145         drm_mode_config_init(drm);
146
147         ret = component_bind_all(drm->dev, drm);
148         if (ret) {
149                 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
150                 goto cleanup_mode_config;
151         }
152
153         /* Create our layers */
154         drv->layers = sun4i_layers_init(drm);
155         if (IS_ERR(drv->layers)) {
156                 dev_err(drm->dev, "Couldn't create the planes\n");
157                 ret = PTR_ERR(drv->layers);
158                 goto cleanup_mode_config;
159         }
160
161         /* Create our CRTC */
162         drv->crtc = sun4i_crtc_init(drm);
163         if (!drv->crtc) {
164                 dev_err(drm->dev, "Couldn't create the CRTC\n");
165                 ret = -EINVAL;
166                 goto cleanup_mode_config;
167         }
168         drm->irq_enabled = true;
169
170         /* Remove early framebuffers (ie. simplefb) */
171         sun4i_remove_framebuffers();
172
173         /* Create our framebuffer */
174         drv->fbdev = sun4i_framebuffer_init(drm);
175         if (IS_ERR(drv->fbdev)) {
176                 dev_err(drm->dev, "Couldn't create our framebuffer\n");
177                 ret = PTR_ERR(drv->fbdev);
178                 goto cleanup_mode_config;
179         }
180
181         /* Enable connectors polling */
182         drm_kms_helper_poll_init(drm);
183
184         ret = drm_dev_register(drm, 0);
185         if (ret)
186                 goto finish_poll;
187
188         return 0;
189
190 finish_poll:
191         drm_kms_helper_poll_fini(drm);
192         sun4i_framebuffer_free(drm);
193 cleanup_mode_config:
194         drm_mode_config_cleanup(drm);
195         drm_vblank_cleanup(drm);
196 free_drm:
197         drm_dev_unref(drm);
198         return ret;
199 }
200
201 static void sun4i_drv_unbind(struct device *dev)
202 {
203         struct drm_device *drm = dev_get_drvdata(dev);
204
205         drm_dev_unregister(drm);
206         drm_kms_helper_poll_fini(drm);
207         sun4i_framebuffer_free(drm);
208         drm_vblank_cleanup(drm);
209         drm_dev_unref(drm);
210 }
211
212 static const struct component_master_ops sun4i_drv_master_ops = {
213         .bind   = sun4i_drv_bind,
214         .unbind = sun4i_drv_unbind,
215 };
216
217 static bool sun4i_drv_node_is_connector(struct device_node *node)
218 {
219         return of_device_is_compatible(node, "hdmi-connector");
220 }
221
222 static bool sun4i_drv_node_is_frontend(struct device_node *node)
223 {
224         return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
225                 of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
226                 of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
227 }
228
229 static bool sun4i_drv_node_is_tcon(struct device_node *node)
230 {
231         return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
232                 of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
233                 of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
234                 of_device_is_compatible(node, "allwinner,sun8i-a33-tcon");
235 }
236
237 static int compare_of(struct device *dev, void *data)
238 {
239         DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
240                          of_node_full_name(dev->of_node),
241                          of_node_full_name(data));
242
243         return dev->of_node == data;
244 }
245
246 static int sun4i_drv_add_endpoints(struct device *dev,
247                                    struct component_match **match,
248                                    struct device_node *node)
249 {
250         struct device_node *port, *ep, *remote;
251         int count = 0;
252
253         /*
254          * We don't support the frontend for now, so we will never
255          * have a device bound. Just skip over it, but we still want
256          * the rest our pipeline to be added.
257          */
258         if (!sun4i_drv_node_is_frontend(node) &&
259             !of_device_is_available(node))
260                 return 0;
261
262         /*
263          * The connectors will be the last nodes in our pipeline, we
264          * can just bail out.
265          */
266         if (sun4i_drv_node_is_connector(node))
267                 return 0;
268
269         if (!sun4i_drv_node_is_frontend(node)) {
270                 /* Add current component */
271                 DRM_DEBUG_DRIVER("Adding component %s\n",
272                                  of_node_full_name(node));
273                 component_match_add(dev, match, compare_of, node);
274                 count++;
275         }
276
277         /* Inputs are listed first, then outputs */
278         port = of_graph_get_port_by_id(node, 1);
279         if (!port) {
280                 DRM_DEBUG_DRIVER("No output to bind\n");
281                 return count;
282         }
283
284         for_each_available_child_of_node(port, ep) {
285                 remote = of_graph_get_remote_port_parent(ep);
286                 if (!remote) {
287                         DRM_DEBUG_DRIVER("Error retrieving the output node\n");
288                         continue;
289                 }
290
291                 /*
292                  * If the node is our TCON, the first port is used for
293                  * panel or bridges, and will not be part of the
294                  * component framework.
295                  */
296                 if (sun4i_drv_node_is_tcon(node)) {
297                         struct of_endpoint endpoint;
298
299                         if (of_graph_parse_endpoint(ep, &endpoint)) {
300                                 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
301                                 of_node_put(remote);
302                                 continue;
303                         }
304
305                         if (!endpoint.id) {
306                                 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
307                                 of_node_put(remote);
308                                 continue;
309                         }
310                 }
311
312                 /* Walk down our tree */
313                 count += sun4i_drv_add_endpoints(dev, match, remote);
314
315                 of_node_put(remote);
316         }
317
318         return count;
319 }
320
321 static int sun4i_drv_probe(struct platform_device *pdev)
322 {
323         struct component_match *match = NULL;
324         struct device_node *np = pdev->dev.of_node;
325         int i, count = 0;
326
327         for (i = 0;; i++) {
328                 struct device_node *pipeline = of_parse_phandle(np,
329                                                                 "allwinner,pipelines",
330                                                                 i);
331                 if (!pipeline)
332                         break;
333
334                 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
335                                                 pipeline);
336                 of_node_put(pipeline);
337
338                 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
339                                  count, i);
340         }
341
342         if (count)
343                 return component_master_add_with_match(&pdev->dev,
344                                                        &sun4i_drv_master_ops,
345                                                        match);
346         else
347                 return 0;
348 }
349
350 static int sun4i_drv_remove(struct platform_device *pdev)
351 {
352         return 0;
353 }
354
355 static const struct of_device_id sun4i_drv_of_table[] = {
356         { .compatible = "allwinner,sun5i-a13-display-engine" },
357         { .compatible = "allwinner,sun6i-a31-display-engine" },
358         { .compatible = "allwinner,sun6i-a31s-display-engine" },
359         { .compatible = "allwinner,sun8i-a33-display-engine" },
360         { }
361 };
362 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
363
364 static struct platform_driver sun4i_drv_platform_driver = {
365         .probe          = sun4i_drv_probe,
366         .remove         = sun4i_drv_remove,
367         .driver         = {
368                 .name           = "sun4i-drm",
369                 .of_match_table = sun4i_drv_of_table,
370         },
371 };
372 module_platform_driver(sun4i_drv_platform_driver);
373
374 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
375 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
376 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
377 MODULE_LICENSE("GPL");