2 * Copyright (C) 2015 Free Electrons
3 * Copyright (C) 2015 NextThing Co
5 * Maxime Ripard <maxime.ripard@free-electrons.com>
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.
13 #include <linux/component.h>
14 #include <linux/of_graph.h>
15 #include <linux/of_reserved_mem.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>
24 #include "sun4i_drv.h"
25 #include "sun4i_framebuffer.h"
26 #include "sun4i_tcon.h"
28 static const struct file_operations sun4i_drv_fops = {
31 .release = drm_release,
32 .unlocked_ioctl = drm_ioctl,
33 .compat_ioctl = drm_compat_ioctl,
37 .mmap = drm_gem_cma_mmap,
40 static struct drm_driver sun4i_drv_driver = {
41 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
43 /* Generic Operations */
44 .fops = &sun4i_drv_fops,
46 .desc = "Allwinner sun4i Display Engine",
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,
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,
69 /* Frame Buffer Operations */
72 static void sun4i_remove_framebuffers(void)
74 struct apertures_struct *ap;
76 ap = alloc_apertures(1);
80 /* The framebuffer can be located anywhere in RAM */
81 ap->ranges[0].base = 0;
82 ap->ranges[0].size = ~0;
84 drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
88 static int sun4i_drv_bind(struct device *dev)
90 struct drm_device *drm;
91 struct sun4i_drv *drv;
94 drm = drm_dev_alloc(&sun4i_drv_driver, dev);
98 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
103 drm->dev_private = drv;
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");
111 /* drm_vblank_init calls kcalloc, which can fail */
112 ret = drm_vblank_init(drm, 1);
114 goto free_mem_region;
116 drm_mode_config_init(drm);
118 ret = component_bind_all(drm->dev, drm);
120 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
121 goto cleanup_mode_config;
124 drm->irq_enabled = true;
126 /* Remove early framebuffers (ie. simplefb) */
127 sun4i_remove_framebuffers();
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;
137 /* Enable connectors polling */
138 drm_kms_helper_poll_init(drm);
140 ret = drm_dev_register(drm, 0);
147 drm_kms_helper_poll_fini(drm);
148 sun4i_framebuffer_free(drm);
150 drm_mode_config_cleanup(drm);
151 drm_vblank_cleanup(drm);
153 of_reserved_mem_device_release(dev);
159 static void sun4i_drv_unbind(struct device *dev)
161 struct drm_device *drm = dev_get_drvdata(dev);
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);
172 static const struct component_master_ops sun4i_drv_master_ops = {
173 .bind = sun4i_drv_bind,
174 .unbind = sun4i_drv_unbind,
177 static bool sun4i_drv_node_is_frontend(struct device_node *node)
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");
184 static bool sun4i_drv_node_is_tcon(struct device_node *node)
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");
192 static int compare_of(struct device *dev, void *data)
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));
198 return dev->of_node == data;
201 static int sun4i_drv_add_endpoints(struct device *dev,
202 struct component_match **match,
203 struct device_node *node)
205 struct device_node *port, *ep, *remote;
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.
213 if (!sun4i_drv_node_is_frontend(node) &&
214 !of_device_is_available(node))
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);
225 /* Inputs are listed first, then outputs */
226 port = of_graph_get_port_by_id(node, 1);
228 DRM_DEBUG_DRIVER("No output to bind\n");
232 for_each_available_child_of_node(port, ep) {
233 remote = of_graph_get_remote_port_parent(ep);
235 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
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.
245 if (sun4i_drv_node_is_tcon(node)) {
246 struct of_endpoint endpoint;
248 if (of_graph_parse_endpoint(ep, &endpoint)) {
249 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
254 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
259 /* Walk down our tree */
260 count += sun4i_drv_add_endpoints(dev, match, remote);
268 static int sun4i_drv_probe(struct platform_device *pdev)
270 struct component_match *match = NULL;
271 struct device_node *np = pdev->dev.of_node;
275 struct device_node *pipeline = of_parse_phandle(np,
276 "allwinner,pipelines",
281 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
283 of_node_put(pipeline);
285 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
290 return component_master_add_with_match(&pdev->dev,
291 &sun4i_drv_master_ops,
297 static int sun4i_drv_remove(struct platform_device *pdev)
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" },
309 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
311 static struct platform_driver sun4i_drv_platform_driver = {
312 .probe = sun4i_drv_probe,
313 .remove = sun4i_drv_remove,
316 .of_match_table = sun4i_drv_of_table,
319 module_platform_driver(sun4i_drv_platform_driver);
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");