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