blob: 4ce665349f6b6cef51a2e6142708e5ec34ac45a4 [file] [log] [blame]
Maxime Ripard9026e0d2015-10-29 09:36:23 +01001/*
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>
Daniel Vetter44adece2016-08-10 18:52:34 +020020#include <drm/drm_fb_helper.h>
Russell King97ac0e42016-10-19 11:28:27 +010021#include <drm/drm_of.h>
Maxime Ripard9026e0d2015-10-29 09:36:23 +010022
23#include "sun4i_crtc.h"
24#include "sun4i_drv.h"
25#include "sun4i_framebuffer.h"
26#include "sun4i_layer.h"
27#include "sun4i_tcon.h"
28
Maxime Ripard9026e0d2015-10-29 09:36:23 +010029static int sun4i_drv_enable_vblank(struct drm_device *drm, unsigned int pipe)
30{
31 struct sun4i_drv *drv = drm->dev_private;
32 struct sun4i_tcon *tcon = drv->tcon;
33
34 DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe);
35
36 sun4i_tcon_enable_vblank(tcon, true);
37
38 return 0;
39}
40
41static void sun4i_drv_disable_vblank(struct drm_device *drm, unsigned int pipe)
42{
43 struct sun4i_drv *drv = drm->dev_private;
44 struct sun4i_tcon *tcon = drv->tcon;
45
46 DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe);
47
48 sun4i_tcon_enable_vblank(tcon, false);
49}
50
51static const struct file_operations sun4i_drv_fops = {
52 .owner = THIS_MODULE,
53 .open = drm_open,
54 .release = drm_release,
55 .unlocked_ioctl = drm_ioctl,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010056 .compat_ioctl = drm_compat_ioctl,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010057 .poll = drm_poll,
58 .read = drm_read,
59 .llseek = no_llseek,
60 .mmap = drm_gem_cma_mmap,
61};
62
63static 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,
Daniel Vetterae2c6222016-05-30 19:53:15 +020078 .gem_free_object_unlocked = drm_gem_cma_free_object,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010079 .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 */
Maxime Ripard0b340402016-06-20 12:20:58 +020095 .get_vblank_counter = drm_vblank_no_hw_counter,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010096 .enable_vblank = sun4i_drv_enable_vblank,
97 .disable_vblank = sun4i_drv_disable_vblank,
98};
99
Maxime Ripard3d6bd902016-05-11 16:52:50 +0200100static 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
Daniel Vetter44adece2016-08-10 18:52:34 +0200112 drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
Maxime Ripard3d6bd902016-05-11 16:52:50 +0200113 kfree(ap);
114}
115
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100116static 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);
Tom Gundersen0f288602016-09-21 16:59:19 +0200123 if (IS_ERR(drm))
124 return PTR_ERR(drm);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100125
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100126 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
127 if (!drv) {
128 ret = -ENOMEM;
129 goto free_drm;
130 }
131 drm->dev_private = drv;
132
133 drm_vblank_init(drm, 1);
134 drm_mode_config_init(drm);
135
136 ret = component_bind_all(drm->dev, drm);
137 if (ret) {
138 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
139 goto free_drm;
140 }
141
142 /* Create our layers */
143 drv->layers = sun4i_layers_init(drm);
Christophe JAILLET6c08d7a2016-10-30 09:49:26 +0100144 if (IS_ERR(drv->layers)) {
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100145 dev_err(drm->dev, "Couldn't create the planes\n");
Christophe JAILLET4db069a2016-11-04 07:13:52 +0100146 ret = PTR_ERR(drv->layers);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100147 goto free_drm;
148 }
149
150 /* Create our CRTC */
151 drv->crtc = sun4i_crtc_init(drm);
152 if (!drv->crtc) {
153 dev_err(drm->dev, "Couldn't create the CRTC\n");
154 ret = -EINVAL;
155 goto free_drm;
156 }
157 drm->irq_enabled = true;
158
Maxime Ripard3d6bd902016-05-11 16:52:50 +0200159 /* Remove early framebuffers (ie. simplefb) */
160 sun4i_remove_framebuffers();
161
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100162 /* Create our framebuffer */
163 drv->fbdev = sun4i_framebuffer_init(drm);
164 if (IS_ERR(drv->fbdev)) {
165 dev_err(drm->dev, "Couldn't create our framebuffer\n");
166 ret = PTR_ERR(drv->fbdev);
167 goto free_drm;
168 }
169
170 /* Enable connectors polling */
171 drm_kms_helper_poll_init(drm);
172
173 ret = drm_dev_register(drm, 0);
174 if (ret)
175 goto free_drm;
176
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100177 return 0;
178
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100179free_drm:
180 drm_dev_unref(drm);
181 return ret;
182}
183
184static void sun4i_drv_unbind(struct device *dev)
185{
186 struct drm_device *drm = dev_get_drvdata(dev);
187
188 drm_dev_unregister(drm);
189 drm_kms_helper_poll_fini(drm);
190 sun4i_framebuffer_free(drm);
191 drm_vblank_cleanup(drm);
192 drm_dev_unref(drm);
193}
194
195static const struct component_master_ops sun4i_drv_master_ops = {
196 .bind = sun4i_drv_bind,
197 .unbind = sun4i_drv_unbind,
198};
199
200static bool sun4i_drv_node_is_frontend(struct device_node *node)
201{
Maxime Ripard4a408f12016-01-07 12:32:25 +0100202 return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
Chen-Yu Tsai49c440e2016-10-20 11:43:41 +0800203 of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
Maxime Ripard4a408f12016-01-07 12:32:25 +0100204 of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100205}
206
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100207static bool sun4i_drv_node_is_tcon(struct device_node *node)
208{
Maxime Ripard4a408f12016-01-07 12:32:25 +0100209 return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800210 of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
211 of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
Maxime Ripard4a408f12016-01-07 12:32:25 +0100212 of_device_is_compatible(node, "allwinner,sun8i-a33-tcon");
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100213}
214
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100215static int compare_of(struct device *dev, void *data)
216{
217 DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
218 of_node_full_name(dev->of_node),
219 of_node_full_name(data));
220
221 return dev->of_node == data;
222}
223
224static int sun4i_drv_add_endpoints(struct device *dev,
225 struct component_match **match,
226 struct device_node *node)
227{
228 struct device_node *port, *ep, *remote;
229 int count = 0;
230
231 /*
232 * We don't support the frontend for now, so we will never
233 * have a device bound. Just skip over it, but we still want
234 * the rest our pipeline to be added.
235 */
236 if (!sun4i_drv_node_is_frontend(node) &&
237 !of_device_is_available(node))
238 return 0;
239
240 if (!sun4i_drv_node_is_frontend(node)) {
241 /* Add current component */
242 DRM_DEBUG_DRIVER("Adding component %s\n",
243 of_node_full_name(node));
Russell King97ac0e42016-10-19 11:28:27 +0100244 drm_of_component_match_add(dev, match, compare_of, node);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100245 count++;
246 }
247
248 /* Inputs are listed first, then outputs */
249 port = of_graph_get_port_by_id(node, 1);
250 if (!port) {
251 DRM_DEBUG_DRIVER("No output to bind\n");
252 return count;
253 }
254
255 for_each_available_child_of_node(port, ep) {
256 remote = of_graph_get_remote_port_parent(ep);
257 if (!remote) {
258 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
259 of_node_put(remote);
260 continue;
261 }
262
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100263 /*
Maxime Ripard894f5a92016-04-11 12:16:33 +0200264 * If the node is our TCON, the first port is used for
265 * panel or bridges, and will not be part of the
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100266 * component framework.
267 */
268 if (sun4i_drv_node_is_tcon(node)) {
269 struct of_endpoint endpoint;
270
271 if (of_graph_parse_endpoint(ep, &endpoint)) {
272 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
273 continue;
274 }
275
276 if (!endpoint.id) {
277 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
278 continue;
279 }
280 }
281
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100282 /* Walk down our tree */
283 count += sun4i_drv_add_endpoints(dev, match, remote);
284
285 of_node_put(remote);
286 }
287
288 return count;
289}
290
291static int sun4i_drv_probe(struct platform_device *pdev)
292{
293 struct component_match *match = NULL;
294 struct device_node *np = pdev->dev.of_node;
295 int i, count = 0;
296
297 for (i = 0;; i++) {
298 struct device_node *pipeline = of_parse_phandle(np,
299 "allwinner,pipelines",
300 i);
301 if (!pipeline)
302 break;
303
304 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
305 pipeline);
Peter Chen3b8e64f2016-07-05 10:04:53 +0800306 of_node_put(pipeline);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100307
308 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
309 count, i);
310 }
311
312 if (count)
313 return component_master_add_with_match(&pdev->dev,
314 &sun4i_drv_master_ops,
315 match);
316 else
317 return 0;
318}
319
320static int sun4i_drv_remove(struct platform_device *pdev)
321{
322 return 0;
323}
324
325static const struct of_device_id sun4i_drv_of_table[] = {
326 { .compatible = "allwinner,sun5i-a13-display-engine" },
Chen-Yu Tsai49c440e2016-10-20 11:43:41 +0800327 { .compatible = "allwinner,sun6i-a31-display-engine" },
328 { .compatible = "allwinner,sun6i-a31s-display-engine" },
Maxime Ripard4a408f12016-01-07 12:32:25 +0100329 { .compatible = "allwinner,sun8i-a33-display-engine" },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100330 { }
331};
332MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
333
334static struct platform_driver sun4i_drv_platform_driver = {
335 .probe = sun4i_drv_probe,
336 .remove = sun4i_drv_remove,
337 .driver = {
338 .name = "sun4i-drm",
339 .of_match_table = sun4i_drv_of_table,
340 },
341};
342module_platform_driver(sun4i_drv_platform_driver);
343
344MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
345MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
346MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
347MODULE_LICENSE("GPL");