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 void sun4i_drv_lastclose(struct drm_device *dev)
30 struct sun4i_drv *drv = dev->dev_private;
32 drm_fbdev_cma_restore_mode(drv->fbdev);
35 DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops);
37 static struct drm_driver sun4i_drv_driver = {
38 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
40 /* Generic Operations */
41 .lastclose = sun4i_drv_lastclose,
42 .fops = &sun4i_drv_fops,
44 .desc = "Allwinner sun4i Display Engine",
50 .dumb_create = drm_gem_cma_dumb_create,
51 .gem_free_object_unlocked = drm_gem_cma_free_object,
52 .gem_vm_ops = &drm_gem_cma_vm_ops,
54 /* PRIME Operations */
55 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
56 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
57 .gem_prime_import = drm_gem_prime_import,
58 .gem_prime_export = drm_gem_prime_export,
59 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
60 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
61 .gem_prime_vmap = drm_gem_cma_prime_vmap,
62 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
63 .gem_prime_mmap = drm_gem_cma_prime_mmap,
65 /* Frame Buffer Operations */
68 static void sun4i_remove_framebuffers(void)
70 struct apertures_struct *ap;
72 ap = alloc_apertures(1);
76 /* The framebuffer can be located anywhere in RAM */
77 ap->ranges[0].base = 0;
78 ap->ranges[0].size = ~0;
80 drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
84 static int sun4i_drv_bind(struct device *dev)
86 struct drm_device *drm;
87 struct sun4i_drv *drv;
90 drm = drm_dev_alloc(&sun4i_drv_driver, dev);
94 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
100 dev_set_drvdata(dev, drm);
101 drm->dev_private = drv;
102 INIT_LIST_HEAD(&drv->engine_list);
103 INIT_LIST_HEAD(&drv->tcon_list);
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);
152 of_reserved_mem_device_release(dev);
158 static void sun4i_drv_unbind(struct device *dev)
160 struct drm_device *drm = dev_get_drvdata(dev);
162 drm_dev_unregister(drm);
163 drm_kms_helper_poll_fini(drm);
164 sun4i_framebuffer_free(drm);
165 drm_mode_config_cleanup(drm);
166 of_reserved_mem_device_release(dev);
170 static const struct component_master_ops sun4i_drv_master_ops = {
171 .bind = sun4i_drv_bind,
172 .unbind = sun4i_drv_unbind,
175 static bool sun4i_drv_node_is_connector(struct device_node *node)
177 return of_device_is_compatible(node, "hdmi-connector");
180 static bool sun4i_drv_node_is_frontend(struct device_node *node)
182 return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
183 of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
184 of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
187 static bool sun4i_drv_node_is_tcon(struct device_node *node)
189 return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
190 of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
191 of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
192 of_device_is_compatible(node, "allwinner,sun8i-a33-tcon") ||
193 of_device_is_compatible(node, "allwinner,sun8i-v3s-tcon");
196 static int compare_of(struct device *dev, void *data)
198 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
202 return dev->of_node == data;
205 static int sun4i_drv_add_endpoints(struct device *dev,
206 struct component_match **match,
207 struct device_node *node)
209 struct device_node *port, *ep, *remote;
213 * We don't support the frontend for now, so we will never
214 * have a device bound. Just skip over it, but we still want
215 * the rest our pipeline to be added.
217 if (!sun4i_drv_node_is_frontend(node) &&
218 !of_device_is_available(node))
222 * The connectors will be the last nodes in our pipeline, we
225 if (sun4i_drv_node_is_connector(node))
228 if (!sun4i_drv_node_is_frontend(node)) {
229 /* Add current component */
230 DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
231 drm_of_component_match_add(dev, match, compare_of, node);
235 /* Inputs are listed first, then outputs */
236 port = of_graph_get_port_by_id(node, 1);
238 DRM_DEBUG_DRIVER("No output to bind\n");
242 for_each_available_child_of_node(port, ep) {
243 remote = of_graph_get_remote_port_parent(ep);
245 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
250 * If the node is our TCON, the first port is used for
251 * panel or bridges, and will not be part of the
252 * component framework.
254 if (sun4i_drv_node_is_tcon(node)) {
255 struct of_endpoint endpoint;
257 if (of_graph_parse_endpoint(ep, &endpoint)) {
258 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
264 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
270 /* Walk down our tree */
271 count += sun4i_drv_add_endpoints(dev, match, remote);
279 static int sun4i_drv_probe(struct platform_device *pdev)
281 struct component_match *match = NULL;
282 struct device_node *np = pdev->dev.of_node;
286 struct device_node *pipeline = of_parse_phandle(np,
287 "allwinner,pipelines",
292 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
294 of_node_put(pipeline);
296 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
301 return component_master_add_with_match(&pdev->dev,
302 &sun4i_drv_master_ops,
308 static int sun4i_drv_remove(struct platform_device *pdev)
313 static const struct of_device_id sun4i_drv_of_table[] = {
314 { .compatible = "allwinner,sun5i-a10s-display-engine" },
315 { .compatible = "allwinner,sun5i-a13-display-engine" },
316 { .compatible = "allwinner,sun6i-a31-display-engine" },
317 { .compatible = "allwinner,sun6i-a31s-display-engine" },
318 { .compatible = "allwinner,sun8i-a33-display-engine" },
319 { .compatible = "allwinner,sun8i-v3s-display-engine" },
322 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
324 static struct platform_driver sun4i_drv_platform_driver = {
325 .probe = sun4i_drv_probe,
326 .remove = sun4i_drv_remove,
329 .of_match_table = sun4i_drv_of_table,
332 module_platform_driver(sun4i_drv_platform_driver);
334 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
335 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
336 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
337 MODULE_LICENSE("GPL");