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