blob: 084c158611de41a5ade281f09f72f5b78c36c404 [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"
Chen-Yu Tsaib3f266e2017-02-23 16:05:36 +080026#include "sun4i_tcon.h"
Maxime Ripard9026e0d2015-10-29 09:36:23 +010027
28static const struct file_operations sun4i_drv_fops = {
29 .owner = THIS_MODULE,
30 .open = drm_open,
31 .release = drm_release,
32 .unlocked_ioctl = drm_ioctl,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010033 .compat_ioctl = drm_compat_ioctl,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010034 .poll = drm_poll,
35 .read = drm_read,
36 .llseek = no_llseek,
37 .mmap = drm_gem_cma_mmap,
38};
39
40static 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,
Daniel Vetterae2c6222016-05-30 19:53:15 +020055 .gem_free_object_unlocked = drm_gem_cma_free_object,
Maxime Ripard9026e0d2015-10-29 09:36:23 +010056 .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 */
Maxime Ripard9026e0d2015-10-29 09:36:23 +010070};
71
Maxime Ripard3d6bd902016-05-11 16:52:50 +020072static 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
Daniel Vetter44adece2016-08-10 18:52:34 +020084 drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
Maxime Ripard3d6bd902016-05-11 16:52:50 +020085 kfree(ap);
86}
87
Maxime Ripard9026e0d2015-10-29 09:36:23 +010088static 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);
Tom Gundersen0f288602016-09-21 16:59:19 +020095 if (IS_ERR(drm))
96 return PTR_ERR(drm);
Maxime Ripard9026e0d2015-10-29 09:36:23 +010097
Maxime Ripard9026e0d2015-10-29 09:36:23 +010098 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
Chen-Yu Tsai92b300c2017-02-17 11:13:26 +0800105 /* drm_vblank_init calls kcalloc, which can fail */
106 ret = drm_vblank_init(drm, 1);
107 if (ret)
108 goto free_drm;
109
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100110 drm_mode_config_init(drm);
111
112 ret = component_bind_all(drm->dev, drm);
113 if (ret) {
114 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800115 goto cleanup_mode_config;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100116 }
117
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100118 /* Create our CRTC */
119 drv->crtc = sun4i_crtc_init(drm);
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800120 if (IS_ERR(drv->crtc)) {
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100121 dev_err(drm->dev, "Couldn't create the CRTC\n");
Chen-Yu Tsaiea411fd2017-02-17 11:13:30 +0800122 ret = PTR_ERR(drv->crtc);
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800123 goto cleanup_mode_config;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100124 }
125 drm->irq_enabled = true;
126
Maxime Ripard3d6bd902016-05-11 16:52:50 +0200127 /* Remove early framebuffers (ie. simplefb) */
128 sun4i_remove_framebuffers();
129
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100130 /* Create our framebuffer */
131 drv->fbdev = sun4i_framebuffer_init(drm);
132 if (IS_ERR(drv->fbdev)) {
133 dev_err(drm->dev, "Couldn't create our framebuffer\n");
134 ret = PTR_ERR(drv->fbdev);
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800135 goto cleanup_mode_config;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100136 }
137
138 /* Enable connectors polling */
139 drm_kms_helper_poll_init(drm);
140
141 ret = drm_dev_register(drm, 0);
142 if (ret)
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800143 goto finish_poll;
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100144
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100145 return 0;
146
Chen-Yu Tsai9d56def2017-02-17 11:13:25 +0800147finish_poll:
148 drm_kms_helper_poll_fini(drm);
149 sun4i_framebuffer_free(drm);
150cleanup_mode_config:
151 drm_mode_config_cleanup(drm);
152 drm_vblank_cleanup(drm);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100153free_drm:
154 drm_dev_unref(drm);
155 return ret;
156}
157
158static void sun4i_drv_unbind(struct device *dev)
159{
160 struct drm_device *drm = dev_get_drvdata(dev);
161
162 drm_dev_unregister(drm);
163 drm_kms_helper_poll_fini(drm);
164 sun4i_framebuffer_free(drm);
Chen-Yu Tsai92caf9b2017-02-17 11:13:24 +0800165 drm_mode_config_cleanup(drm);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100166 drm_vblank_cleanup(drm);
167 drm_dev_unref(drm);
168}
169
170static const struct component_master_ops sun4i_drv_master_ops = {
171 .bind = sun4i_drv_bind,
172 .unbind = sun4i_drv_unbind,
173};
174
175static bool sun4i_drv_node_is_frontend(struct device_node *node)
176{
Maxime Ripard4a408f12016-01-07 12:32:25 +0100177 return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
Chen-Yu Tsai49c440e2016-10-20 11:43:41 +0800178 of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
Maxime Ripard4a408f12016-01-07 12:32:25 +0100179 of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100180}
181
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100182static bool sun4i_drv_node_is_tcon(struct device_node *node)
183{
Maxime Ripard4a408f12016-01-07 12:32:25 +0100184 return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
Chen-Yu Tsai93a5ec12016-10-20 11:43:40 +0800185 of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
186 of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
Maxime Ripard4a408f12016-01-07 12:32:25 +0100187 of_device_is_compatible(node, "allwinner,sun8i-a33-tcon");
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100188}
189
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100190static int compare_of(struct device *dev, void *data)
191{
192 DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
193 of_node_full_name(dev->of_node),
194 of_node_full_name(data));
195
196 return dev->of_node == data;
197}
198
199static int sun4i_drv_add_endpoints(struct device *dev,
200 struct component_match **match,
201 struct device_node *node)
202{
203 struct device_node *port, *ep, *remote;
204 int count = 0;
205
206 /*
207 * We don't support the frontend for now, so we will never
208 * have a device bound. Just skip over it, but we still want
209 * the rest our pipeline to be added.
210 */
211 if (!sun4i_drv_node_is_frontend(node) &&
212 !of_device_is_available(node))
213 return 0;
214
215 if (!sun4i_drv_node_is_frontend(node)) {
216 /* Add current component */
217 DRM_DEBUG_DRIVER("Adding component %s\n",
218 of_node_full_name(node));
Russell King97ac0e42016-10-19 11:28:27 +0100219 drm_of_component_match_add(dev, match, compare_of, node);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100220 count++;
221 }
222
223 /* Inputs are listed first, then outputs */
224 port = of_graph_get_port_by_id(node, 1);
225 if (!port) {
226 DRM_DEBUG_DRIVER("No output to bind\n");
227 return count;
228 }
229
230 for_each_available_child_of_node(port, ep) {
231 remote = of_graph_get_remote_port_parent(ep);
232 if (!remote) {
233 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
234 of_node_put(remote);
235 continue;
236 }
237
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100238 /*
Maxime Ripard894f5a92016-04-11 12:16:33 +0200239 * If the node is our TCON, the first port is used for
240 * panel or bridges, and will not be part of the
Maxime Ripard29e57fa2015-10-29 09:37:32 +0100241 * component framework.
242 */
243 if (sun4i_drv_node_is_tcon(node)) {
244 struct of_endpoint endpoint;
245
246 if (of_graph_parse_endpoint(ep, &endpoint)) {
247 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
248 continue;
249 }
250
251 if (!endpoint.id) {
252 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
253 continue;
254 }
255 }
256
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100257 /* Walk down our tree */
258 count += sun4i_drv_add_endpoints(dev, match, remote);
259
260 of_node_put(remote);
261 }
262
263 return count;
264}
265
266static int sun4i_drv_probe(struct platform_device *pdev)
267{
268 struct component_match *match = NULL;
269 struct device_node *np = pdev->dev.of_node;
270 int i, count = 0;
271
272 for (i = 0;; i++) {
273 struct device_node *pipeline = of_parse_phandle(np,
274 "allwinner,pipelines",
275 i);
276 if (!pipeline)
277 break;
278
279 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
280 pipeline);
Peter Chen3b8e64f2016-07-05 10:04:53 +0800281 of_node_put(pipeline);
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100282
283 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
284 count, i);
285 }
286
287 if (count)
288 return component_master_add_with_match(&pdev->dev,
289 &sun4i_drv_master_ops,
290 match);
291 else
292 return 0;
293}
294
295static int sun4i_drv_remove(struct platform_device *pdev)
296{
297 return 0;
298}
299
300static const struct of_device_id sun4i_drv_of_table[] = {
301 { .compatible = "allwinner,sun5i-a13-display-engine" },
Chen-Yu Tsai49c440e2016-10-20 11:43:41 +0800302 { .compatible = "allwinner,sun6i-a31-display-engine" },
303 { .compatible = "allwinner,sun6i-a31s-display-engine" },
Maxime Ripard4a408f12016-01-07 12:32:25 +0100304 { .compatible = "allwinner,sun8i-a33-display-engine" },
Maxime Ripard9026e0d2015-10-29 09:36:23 +0100305 { }
306};
307MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
308
309static struct platform_driver sun4i_drv_platform_driver = {
310 .probe = sun4i_drv_probe,
311 .remove = sun4i_drv_remove,
312 .driver = {
313 .name = "sun4i-drm",
314 .of_match_table = sun4i_drv_of_table,
315 },
316};
317module_platform_driver(sun4i_drv_platform_driver);
318
319MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
320MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
321MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
322MODULE_LICENSE("GPL");