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>
21 #include "sun4i_crtc.h"
22 #include "sun4i_drv.h"
23 #include "sun4i_framebuffer.h"
24 #include "sun4i_layer.h"
25 #include "sun4i_tcon.h"
27 static int sun4i_drv_connector_plug_all(struct drm_device *drm)
29 struct drm_connector *connector, *failed;
32 mutex_lock(&drm->mode_config.mutex);
33 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
34 ret = drm_connector_register(connector);
40 mutex_unlock(&drm->mode_config.mutex);
44 list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
45 if (failed == connector)
48 drm_connector_unregister(connector);
50 mutex_unlock(&drm->mode_config.mutex);
55 static int sun4i_drv_enable_vblank(struct drm_device *drm, unsigned int pipe)
57 struct sun4i_drv *drv = drm->dev_private;
58 struct sun4i_tcon *tcon = drv->tcon;
60 DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe);
62 sun4i_tcon_enable_vblank(tcon, true);
67 static void sun4i_drv_disable_vblank(struct drm_device *drm, unsigned int pipe)
69 struct sun4i_drv *drv = drm->dev_private;
70 struct sun4i_tcon *tcon = drv->tcon;
72 DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe);
74 sun4i_tcon_enable_vblank(tcon, false);
77 static const struct file_operations sun4i_drv_fops = {
80 .release = drm_release,
81 .unlocked_ioctl = drm_ioctl,
83 .compat_ioctl = drm_compat_ioctl,
88 .mmap = drm_gem_cma_mmap,
91 static struct drm_driver sun4i_drv_driver = {
92 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
94 /* Generic Operations */
95 .fops = &sun4i_drv_fops,
97 .desc = "Allwinner sun4i Display Engine",
103 .dumb_create = drm_gem_cma_dumb_create,
104 .dumb_destroy = drm_gem_dumb_destroy,
105 .dumb_map_offset = drm_gem_cma_dumb_map_offset,
106 .gem_free_object_unlocked = drm_gem_cma_free_object,
107 .gem_vm_ops = &drm_gem_cma_vm_ops,
109 /* PRIME Operations */
110 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
111 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
112 .gem_prime_import = drm_gem_prime_import,
113 .gem_prime_export = drm_gem_prime_export,
114 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
115 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
116 .gem_prime_vmap = drm_gem_cma_prime_vmap,
117 .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
118 .gem_prime_mmap = drm_gem_cma_prime_mmap,
120 /* Frame Buffer Operations */
122 /* VBlank Operations */
123 .get_vblank_counter = drm_vblank_count,
124 .enable_vblank = sun4i_drv_enable_vblank,
125 .disable_vblank = sun4i_drv_disable_vblank,
128 static int sun4i_drv_bind(struct device *dev)
130 struct drm_device *drm;
131 struct sun4i_drv *drv;
134 drm = drm_dev_alloc(&sun4i_drv_driver, dev);
138 drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
143 drm->dev_private = drv;
145 drm_vblank_init(drm, 1);
146 drm_mode_config_init(drm);
148 ret = component_bind_all(drm->dev, drm);
150 dev_err(drm->dev, "Couldn't bind all pipelines components\n");
154 /* Create our layers */
155 drv->layers = sun4i_layers_init(drm);
157 dev_err(drm->dev, "Couldn't create the planes\n");
162 /* Create our CRTC */
163 drv->crtc = sun4i_crtc_init(drm);
165 dev_err(drm->dev, "Couldn't create the CRTC\n");
169 drm->irq_enabled = true;
171 /* Create our framebuffer */
172 drv->fbdev = sun4i_framebuffer_init(drm);
173 if (IS_ERR(drv->fbdev)) {
174 dev_err(drm->dev, "Couldn't create our framebuffer\n");
175 ret = PTR_ERR(drv->fbdev);
179 /* Enable connectors polling */
180 drm_kms_helper_poll_init(drm);
182 ret = drm_dev_register(drm, 0);
186 ret = sun4i_drv_connector_plug_all(drm);
193 drm_dev_unregister(drm);
199 static void sun4i_drv_unbind(struct device *dev)
201 struct drm_device *drm = dev_get_drvdata(dev);
203 drm_dev_unregister(drm);
204 drm_kms_helper_poll_fini(drm);
205 sun4i_framebuffer_free(drm);
206 drm_vblank_cleanup(drm);
210 static const struct component_master_ops sun4i_drv_master_ops = {
211 .bind = sun4i_drv_bind,
212 .unbind = sun4i_drv_unbind,
215 static bool sun4i_drv_node_is_frontend(struct device_node *node)
217 return of_device_is_compatible(node,
218 "allwinner,sun5i-a13-display-frontend");
221 static bool sun4i_drv_node_is_tcon(struct device_node *node)
223 return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon");
226 static int compare_of(struct device *dev, void *data)
228 DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
229 of_node_full_name(dev->of_node),
230 of_node_full_name(data));
232 return dev->of_node == data;
235 static int sun4i_drv_add_endpoints(struct device *dev,
236 struct component_match **match,
237 struct device_node *node)
239 struct device_node *port, *ep, *remote;
243 * We don't support the frontend for now, so we will never
244 * have a device bound. Just skip over it, but we still want
245 * the rest our pipeline to be added.
247 if (!sun4i_drv_node_is_frontend(node) &&
248 !of_device_is_available(node))
251 if (!sun4i_drv_node_is_frontend(node)) {
252 /* Add current component */
253 DRM_DEBUG_DRIVER("Adding component %s\n",
254 of_node_full_name(node));
255 component_match_add(dev, match, compare_of, node);
259 /* Inputs are listed first, then outputs */
260 port = of_graph_get_port_by_id(node, 1);
262 DRM_DEBUG_DRIVER("No output to bind\n");
266 for_each_available_child_of_node(port, ep) {
267 remote = of_graph_get_remote_port_parent(ep);
269 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
275 * If the node is our TCON, the first port is used for our
276 * panel, and will not be part of the
277 * component framework.
279 if (sun4i_drv_node_is_tcon(node)) {
280 struct of_endpoint endpoint;
282 if (of_graph_parse_endpoint(ep, &endpoint)) {
283 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
288 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
293 /* Walk down our tree */
294 count += sun4i_drv_add_endpoints(dev, match, remote);
302 static int sun4i_drv_probe(struct platform_device *pdev)
304 struct component_match *match = NULL;
305 struct device_node *np = pdev->dev.of_node;
309 struct device_node *pipeline = of_parse_phandle(np,
310 "allwinner,pipelines",
315 count += sun4i_drv_add_endpoints(&pdev->dev, &match,
318 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
323 return component_master_add_with_match(&pdev->dev,
324 &sun4i_drv_master_ops,
330 static int sun4i_drv_remove(struct platform_device *pdev)
335 static const struct of_device_id sun4i_drv_of_table[] = {
336 { .compatible = "allwinner,sun5i-a13-display-engine" },
339 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
341 static struct platform_driver sun4i_drv_platform_driver = {
342 .probe = sun4i_drv_probe,
343 .remove = sun4i_drv_remove,
346 .of_match_table = sun4i_drv_of_table,
349 module_platform_driver(sun4i_drv_platform_driver);
351 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
352 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
353 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
354 MODULE_LICENSE("GPL");