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>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_fb_helper.h>
22 #include "sun4i_crtc.h"
23 #include "sun4i_drv.h"
24 #include "sun4i_framebuffer.h"
25 #include "sun4i_layer.h"
26 #include "sun4i_tcon.h"
28 static int sun4i_drv_enable_vblank(struct drm_device *drm, unsigned int pipe)
30 struct sun4i_drv *drv = drm->dev_private;
31 struct sun4i_tcon *tcon = drv->tcon;
33 DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe);
35 sun4i_tcon_enable_vblank(tcon, true);
40 static void sun4i_drv_disable_vblank(struct drm_device *drm, unsigned int pipe)
42 struct sun4i_drv *drv = drm->dev_private;
43 struct sun4i_tcon *tcon = drv->tcon;
45 DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe);
47 sun4i_tcon_enable_vblank(tcon, false);
50 static void sun4i_drv_lastclose(struct drm_device *dev)
52 struct sun4i_drv *drv = dev->dev_private;
54 drm_fbdev_cma_restore_mode(drv->fbdev);
57 static const struct file_operations sun4i_drv_fops = {
60 .release = drm_release,
61 .unlocked_ioctl = drm_ioctl,
63 .compat_ioctl = drm_compat_ioctl,
68 .mmap = drm_gem_cma_mmap,
71 static struct drm_driver sun4i_drv_driver = {
72 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
74 /* Generic Operations */
75 .lastclose = sun4i_drv_lastclose,
76 .fops = &sun4i_drv_fops,
78 .desc = "Allwinner sun4i Display Engine",
84 .dumb_create = drm_gem_cma_dumb_create,
85 .dumb_destroy = drm_gem_dumb_destroy,
86 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
87 .gem_free_object_unlocked = drm_gem_cma_free_object,
88 .gem_vm_ops = &drm_gem_cma_vm_ops,
90 /* PRIME Operations */
91 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
92 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
93 .gem_prime_import = drm_gem_prime_import,
94 .gem_prime_export = drm_gem_prime_export,
95 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
96 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
97 .gem_prime_vmap = drm_gem_cma_prime_vmap,
98 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
99 .gem_prime_mmap = drm_gem_cma_prime_mmap,
101 /* Frame Buffer Operations */
103 /* VBlank Operations */
104 .get_vblank_counter = drm_vblank_no_hw_counter,
105 .enable_vblank = sun4i_drv_enable_vblank,
106 .disable_vblank = sun4i_drv_disable_vblank,
109 static void sun4i_remove_framebuffers(void)
111 struct apertures_struct *ap;
113 ap = alloc_apertures(1);
117 /* The framebuffer can be located anywhere in RAM */
118 ap->ranges[0].base = 0;
119 ap->ranges[0].size = ~0;
121 drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
125 static int sun4i_drv_bind(struct device *dev)
127 struct drm_device *drm;
128 struct sun4i_drv *drv;
131 drm = drm_dev_alloc(&sun4i_drv_driver, dev);
135 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
141 dev_set_drvdata(dev, drm);
142 drm->dev_private = drv;
144 drm_vblank_init(drm, 1);
145 drm_mode_config_init(drm);
147 ret = component_bind_all(drm->dev, drm);
149 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
150 goto cleanup_mode_config;
153 /* Create our layers */
154 drv->layers = sun4i_layers_init(drm);
155 if (IS_ERR(drv->layers)) {
156 dev_err(drm->dev, "Couldn't create the planes\n");
157 ret = PTR_ERR(drv->layers);
158 goto cleanup_mode_config;
161 /* Create our CRTC */
162 drv->crtc = sun4i_crtc_init(drm);
164 dev_err(drm->dev, "Couldn't create the CRTC\n");
166 goto cleanup_mode_config;
168 drm->irq_enabled = true;
170 /* Remove early framebuffers (ie. simplefb) */
171 sun4i_remove_framebuffers();
173 /* Create our framebuffer */
174 drv->fbdev = sun4i_framebuffer_init(drm);
175 if (IS_ERR(drv->fbdev)) {
176 dev_err(drm->dev, "Couldn't create our framebuffer\n");
177 ret = PTR_ERR(drv->fbdev);
178 goto cleanup_mode_config;
181 /* Enable connectors polling */
182 drm_kms_helper_poll_init(drm);
184 ret = drm_dev_register(drm, 0);
191 drm_kms_helper_poll_fini(drm);
192 sun4i_framebuffer_free(drm);
194 drm_mode_config_cleanup(drm);
195 drm_vblank_cleanup(drm);
201 static void sun4i_drv_unbind(struct device *dev)
203 struct drm_device *drm = dev_get_drvdata(dev);
205 drm_dev_unregister(drm);
206 drm_kms_helper_poll_fini(drm);
207 sun4i_framebuffer_free(drm);
208 drm_vblank_cleanup(drm);
212 static const struct component_master_ops sun4i_drv_master_ops = {
213 .bind = sun4i_drv_bind,
214 .unbind = sun4i_drv_unbind,
217 static bool sun4i_drv_node_is_connector(struct device_node *node)
219 return of_device_is_compatible(node, "hdmi-connector");
222 static bool sun4i_drv_node_is_frontend(struct device_node *node)
224 return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
225 of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
226 of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
229 static bool sun4i_drv_node_is_tcon(struct device_node *node)
231 return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
232 of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
233 of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
234 of_device_is_compatible(node, "allwinner,sun8i-a33-tcon");
237 static int compare_of(struct device *dev, void *data)
239 DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
240 of_node_full_name(dev->of_node),
241 of_node_full_name(data));
243 return dev->of_node == data;
246 static int sun4i_drv_add_endpoints(struct device *dev,
247 struct component_match **match,
248 struct device_node *node)
250 struct device_node *port, *ep, *remote;
254 * We don't support the frontend for now, so we will never
255 * have a device bound. Just skip over it, but we still want
256 * the rest our pipeline to be added.
258 if (!sun4i_drv_node_is_frontend(node) &&
259 !of_device_is_available(node))
263 * The connectors will be the last nodes in our pipeline, we
266 if (sun4i_drv_node_is_connector(node))
269 if (!sun4i_drv_node_is_frontend(node)) {
270 /* Add current component */
271 DRM_DEBUG_DRIVER("Adding component %s\n",
272 of_node_full_name(node));
273 component_match_add(dev, match, compare_of, node);
277 /* Inputs are listed first, then outputs */
278 port = of_graph_get_port_by_id(node, 1);
280 DRM_DEBUG_DRIVER("No output to bind\n");
284 for_each_available_child_of_node(port, ep) {
285 remote = of_graph_get_remote_port_parent(ep);
287 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
292 * If the node is our TCON, the first port is used for
293 * panel or bridges, and will not be part of the
294 * component framework.
296 if (sun4i_drv_node_is_tcon(node)) {
297 struct of_endpoint endpoint;
299 if (of_graph_parse_endpoint(ep, &endpoint)) {
300 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
306 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
312 /* Walk down our tree */
313 count += sun4i_drv_add_endpoints(dev, match, remote);
321 static int sun4i_drv_probe(struct platform_device *pdev)
323 struct component_match *match = NULL;
324 struct device_node *np = pdev->dev.of_node;
328 struct device_node *pipeline = of_parse_phandle(np,
329 "allwinner,pipelines",
334 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
336 of_node_put(pipeline);
338 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
343 return component_master_add_with_match(&pdev->dev,
344 &sun4i_drv_master_ops,
350 static int sun4i_drv_remove(struct platform_device *pdev)
355 static const struct of_device_id sun4i_drv_of_table[] = {
356 { .compatible = "allwinner,sun5i-a13-display-engine" },
357 { .compatible = "allwinner,sun6i-a31-display-engine" },
358 { .compatible = "allwinner,sun6i-a31s-display-engine" },
359 { .compatible = "allwinner,sun8i-a33-display-engine" },
362 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
364 static struct platform_driver sun4i_drv_platform_driver = {
365 .probe = sun4i_drv_probe,
366 .remove = sun4i_drv_remove,
369 .of_match_table = sun4i_drv_of_table,
372 module_platform_driver(sun4i_drv_platform_driver);
374 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
375 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
376 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
377 MODULE_LICENSE("GPL");