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_enable_vblank(struct drm_device
*drm
, unsigned int pipe
)
29 struct sun4i_drv
*drv
= drm
->dev_private
;
30 struct sun4i_tcon
*tcon
= drv
->tcon
;
32 DRM_DEBUG_DRIVER("Enabling VBLANK on pipe %d\n", pipe
);
34 sun4i_tcon_enable_vblank(tcon
, true);
39 static void sun4i_drv_disable_vblank(struct drm_device
*drm
, unsigned int pipe
)
41 struct sun4i_drv
*drv
= drm
->dev_private
;
42 struct sun4i_tcon
*tcon
= drv
->tcon
;
44 DRM_DEBUG_DRIVER("Disabling VBLANK on pipe %d\n", pipe
);
46 sun4i_tcon_enable_vblank(tcon
, false);
49 static const struct file_operations sun4i_drv_fops
= {
52 .release
= drm_release
,
53 .unlocked_ioctl
= drm_ioctl
,
55 .compat_ioctl
= drm_compat_ioctl
,
60 .mmap
= drm_gem_cma_mmap
,
63 static struct drm_driver sun4i_drv_driver
= {
64 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
| DRIVER_ATOMIC
,
66 /* Generic Operations */
67 .fops
= &sun4i_drv_fops
,
69 .desc
= "Allwinner sun4i Display Engine",
75 .dumb_create
= drm_gem_cma_dumb_create
,
76 .dumb_destroy
= drm_gem_dumb_destroy
,
77 .dumb_map_offset
= drm_gem_cma_dumb_map_offset
,
78 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
79 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
81 /* PRIME Operations */
82 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
83 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
84 .gem_prime_import
= drm_gem_prime_import
,
85 .gem_prime_export
= drm_gem_prime_export
,
86 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
87 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
88 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
89 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
90 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
92 /* Frame Buffer Operations */
94 /* VBlank Operations */
95 .get_vblank_counter
= drm_vblank_count
,
96 .enable_vblank
= sun4i_drv_enable_vblank
,
97 .disable_vblank
= sun4i_drv_disable_vblank
,
100 static void sun4i_remove_framebuffers(void)
102 struct apertures_struct
*ap
;
104 ap
= alloc_apertures(1);
108 /* The framebuffer can be located anywhere in RAM */
109 ap
->ranges
[0].base
= 0;
110 ap
->ranges
[0].size
= ~0;
112 remove_conflicting_framebuffers(ap
, "sun4i-drm-fb", false);
116 static int sun4i_drv_bind(struct device
*dev
)
118 struct drm_device
*drm
;
119 struct sun4i_drv
*drv
;
122 drm
= drm_dev_alloc(&sun4i_drv_driver
, dev
);
126 drv
= devm_kzalloc(dev
, sizeof(*drv
), GFP_KERNEL
);
131 drm
->dev_private
= drv
;
133 drm_vblank_init(drm
, 1);
134 drm_mode_config_init(drm
);
136 ret
= component_bind_all(drm
->dev
, drm
);
138 dev_err(drm
->dev
, "Couldn't bind all pipelines components\n");
142 /* Create our layers */
143 drv
->layers
= sun4i_layers_init(drm
);
145 dev_err(drm
->dev
, "Couldn't create the planes\n");
150 /* Create our CRTC */
151 drv
->crtc
= sun4i_crtc_init(drm
);
153 dev_err(drm
->dev
, "Couldn't create the CRTC\n");
157 drm
->irq_enabled
= true;
159 /* Remove early framebuffers (ie. simplefb) */
160 sun4i_remove_framebuffers();
162 /* Create our framebuffer */
163 drv
->fbdev
= sun4i_framebuffer_init(drm
);
164 if (IS_ERR(drv
->fbdev
)) {
165 dev_err(drm
->dev
, "Couldn't create our framebuffer\n");
166 ret
= PTR_ERR(drv
->fbdev
);
170 /* Enable connectors polling */
171 drm_kms_helper_poll_init(drm
);
173 ret
= drm_dev_register(drm
, 0);
184 static void sun4i_drv_unbind(struct device
*dev
)
186 struct drm_device
*drm
= dev_get_drvdata(dev
);
188 drm_dev_unregister(drm
);
189 drm_kms_helper_poll_fini(drm
);
190 sun4i_framebuffer_free(drm
);
191 drm_vblank_cleanup(drm
);
195 static const struct component_master_ops sun4i_drv_master_ops
= {
196 .bind
= sun4i_drv_bind
,
197 .unbind
= sun4i_drv_unbind
,
200 static bool sun4i_drv_node_is_frontend(struct device_node
*node
)
202 return of_device_is_compatible(node
,
203 "allwinner,sun5i-a13-display-frontend");
206 static bool sun4i_drv_node_is_tcon(struct device_node
*node
)
208 return of_device_is_compatible(node
, "allwinner,sun5i-a13-tcon");
211 static int compare_of(struct device
*dev
, void *data
)
213 DRM_DEBUG_DRIVER("Comparing of node %s with %s\n",
214 of_node_full_name(dev
->of_node
),
215 of_node_full_name(data
));
217 return dev
->of_node
== data
;
220 static int sun4i_drv_add_endpoints(struct device
*dev
,
221 struct component_match
**match
,
222 struct device_node
*node
)
224 struct device_node
*port
, *ep
, *remote
;
228 * We don't support the frontend for now, so we will never
229 * have a device bound. Just skip over it, but we still want
230 * the rest our pipeline to be added.
232 if (!sun4i_drv_node_is_frontend(node
) &&
233 !of_device_is_available(node
))
236 if (!sun4i_drv_node_is_frontend(node
)) {
237 /* Add current component */
238 DRM_DEBUG_DRIVER("Adding component %s\n",
239 of_node_full_name(node
));
240 component_match_add(dev
, match
, compare_of
, node
);
244 /* Inputs are listed first, then outputs */
245 port
= of_graph_get_port_by_id(node
, 1);
247 DRM_DEBUG_DRIVER("No output to bind\n");
251 for_each_available_child_of_node(port
, ep
) {
252 remote
= of_graph_get_remote_port_parent(ep
);
254 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
260 * If the node is our TCON, the first port is used for our
261 * panel, and will not be part of the
262 * component framework.
264 if (sun4i_drv_node_is_tcon(node
)) {
265 struct of_endpoint endpoint
;
267 if (of_graph_parse_endpoint(ep
, &endpoint
)) {
268 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
273 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
278 /* Walk down our tree */
279 count
+= sun4i_drv_add_endpoints(dev
, match
, remote
);
287 static int sun4i_drv_probe(struct platform_device
*pdev
)
289 struct component_match
*match
= NULL
;
290 struct device_node
*np
= pdev
->dev
.of_node
;
294 struct device_node
*pipeline
= of_parse_phandle(np
,
295 "allwinner,pipelines",
300 count
+= sun4i_drv_add_endpoints(&pdev
->dev
, &match
,
303 DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
308 return component_master_add_with_match(&pdev
->dev
,
309 &sun4i_drv_master_ops
,
315 static int sun4i_drv_remove(struct platform_device
*pdev
)
320 static const struct of_device_id sun4i_drv_of_table
[] = {
321 { .compatible
= "allwinner,sun5i-a13-display-engine" },
324 MODULE_DEVICE_TABLE(of
, sun4i_drv_of_table
);
326 static struct platform_driver sun4i_drv_platform_driver
= {
327 .probe
= sun4i_drv_probe
,
328 .remove
= sun4i_drv_remove
,
331 .of_match_table
= sun4i_drv_of_table
,
334 module_platform_driver(sun4i_drv_platform_driver
);
336 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
337 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
338 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
339 MODULE_LICENSE("GPL");