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