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/kfifo.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_reserved_mem.h>
19 #include <drm/drm_crtc_helper.h>
20 #include <drm/drm_fb_cma_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_of.h>
25 #include "sun4i_drv.h"
26 #include "sun4i_framebuffer.h"
27 #include "sun4i_tcon.h"
29 DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops
);
31 static struct drm_driver sun4i_drv_driver
= {
32 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
| DRIVER_PRIME
| DRIVER_ATOMIC
,
34 /* Generic Operations */
35 .lastclose
= drm_fb_helper_lastclose
,
36 .fops
= &sun4i_drv_fops
,
38 .desc
= "Allwinner sun4i Display Engine",
44 .dumb_create
= drm_gem_cma_dumb_create
,
45 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
46 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
48 /* PRIME Operations */
49 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
50 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
51 .gem_prime_import
= drm_gem_prime_import
,
52 .gem_prime_export
= drm_gem_prime_export
,
53 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
54 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
55 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
56 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
57 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
59 /* Frame Buffer Operations */
62 static void sun4i_remove_framebuffers(void)
64 struct apertures_struct
*ap
;
66 ap
= alloc_apertures(1);
70 /* The framebuffer can be located anywhere in RAM */
71 ap
->ranges
[0].base
= 0;
72 ap
->ranges
[0].size
= ~0;
74 drm_fb_helper_remove_conflicting_framebuffers(ap
, "sun4i-drm-fb", false);
78 static int sun4i_drv_bind(struct device
*dev
)
80 struct drm_device
*drm
;
81 struct sun4i_drv
*drv
;
84 drm
= drm_dev_alloc(&sun4i_drv_driver
, dev
);
88 drv
= devm_kzalloc(dev
, sizeof(*drv
), GFP_KERNEL
);
93 drm
->dev_private
= drv
;
94 INIT_LIST_HEAD(&drv
->engine_list
);
95 INIT_LIST_HEAD(&drv
->tcon_list
);
97 ret
= of_reserved_mem_device_init(dev
);
98 if (ret
&& ret
!= -ENODEV
) {
99 dev_err(drm
->dev
, "Couldn't claim our memory region\n");
103 drm_mode_config_init(drm
);
105 ret
= component_bind_all(drm
->dev
, drm
);
107 dev_err(drm
->dev
, "Couldn't bind all pipelines components\n");
108 goto cleanup_mode_config
;
111 /* drm_vblank_init calls kcalloc, which can fail */
112 ret
= drm_vblank_init(drm
, drm
->mode_config
.num_crtc
);
114 goto free_mem_region
;
116 drm
->irq_enabled
= true;
118 /* Remove early framebuffers (ie. simplefb) */
119 sun4i_remove_framebuffers();
121 /* Create our framebuffer */
122 ret
= sun4i_framebuffer_init(drm
);
124 dev_err(drm
->dev
, "Couldn't create our framebuffer\n");
125 goto cleanup_mode_config
;
128 /* Enable connectors polling */
129 drm_kms_helper_poll_init(drm
);
131 ret
= drm_dev_register(drm
, 0);
138 drm_kms_helper_poll_fini(drm
);
139 sun4i_framebuffer_free(drm
);
141 drm_mode_config_cleanup(drm
);
143 of_reserved_mem_device_release(dev
);
149 static void sun4i_drv_unbind(struct device
*dev
)
151 struct drm_device
*drm
= dev_get_drvdata(dev
);
153 drm_dev_unregister(drm
);
154 drm_kms_helper_poll_fini(drm
);
155 sun4i_framebuffer_free(drm
);
156 drm_mode_config_cleanup(drm
);
157 of_reserved_mem_device_release(dev
);
161 static const struct component_master_ops sun4i_drv_master_ops
= {
162 .bind
= sun4i_drv_bind
,
163 .unbind
= sun4i_drv_unbind
,
166 static bool sun4i_drv_node_is_connector(struct device_node
*node
)
168 return of_device_is_compatible(node
, "hdmi-connector");
171 static bool sun4i_drv_node_is_frontend(struct device_node
*node
)
173 return of_device_is_compatible(node
, "allwinner,sun4i-a10-display-frontend") ||
174 of_device_is_compatible(node
, "allwinner,sun5i-a13-display-frontend") ||
175 of_device_is_compatible(node
, "allwinner,sun6i-a31-display-frontend") ||
176 of_device_is_compatible(node
, "allwinner,sun7i-a20-display-frontend") ||
177 of_device_is_compatible(node
, "allwinner,sun8i-a33-display-frontend");
180 static bool sun4i_drv_node_is_tcon(struct device_node
*node
)
182 return !!of_match_node(sun4i_tcon_of_table
, node
);
185 static int compare_of(struct device
*dev
, void *data
)
187 DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
191 return dev
->of_node
== data
;
195 * The encoder drivers use drm_of_find_possible_crtcs to get upstream
196 * crtcs from the device tree using of_graph. For the results to be
197 * correct, encoders must be probed/bound after _all_ crtcs have been
198 * created. The existing code uses a depth first recursive traversal
199 * of the of_graph, which means the encoders downstream of the TCON
200 * get add right after the first TCON. The second TCON or CRTC will
201 * never be properly associated with encoders connected to it.
203 * Also, in a dual display pipeline setup, both frontends can feed
204 * either backend, and both backends can feed either TCON, we want
205 * all components of the same type to be added before the next type
206 * in the pipeline. Fortunately, the pipelines are perfectly symmetric,
207 * i.e. components of the same type are at the same depth when counted
208 * from the frontend. The only exception is the third pipeline in
209 * the A80 SoC, which we do not support anyway.
211 * Hence we can use a breadth first search traversal order to add
212 * components. We do not need to check for duplicates. The component
213 * matching system handles this for us.
215 struct endpoint_list
{
216 DECLARE_KFIFO(fifo
, struct device_node
*, 16);
219 static int sun4i_drv_add_endpoints(struct device
*dev
,
220 struct endpoint_list
*list
,
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
))
237 * The connectors will be the last nodes in our pipeline, we
240 if (sun4i_drv_node_is_connector(node
))
243 if (!sun4i_drv_node_is_frontend(node
)) {
244 /* Add current component */
245 DRM_DEBUG_DRIVER("Adding component %pOF\n", node
);
246 drm_of_component_match_add(dev
, match
, compare_of
, node
);
250 /* Inputs are listed first, then outputs */
251 port
= of_graph_get_port_by_id(node
, 1);
253 DRM_DEBUG_DRIVER("No output to bind\n");
257 for_each_available_child_of_node(port
, ep
) {
258 remote
= of_graph_get_remote_port_parent(ep
);
260 DRM_DEBUG_DRIVER("Error retrieving the output node\n");
266 * If the node is our TCON, the first port is used for
267 * panel or bridges, and will not be part of the
268 * component framework.
270 if (sun4i_drv_node_is_tcon(node
)) {
271 struct of_endpoint endpoint
;
273 if (of_graph_parse_endpoint(ep
, &endpoint
)) {
274 DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
279 DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
284 kfifo_put(&list
->fifo
, remote
);
290 static int sun4i_drv_probe(struct platform_device
*pdev
)
292 struct component_match
*match
= NULL
;
293 struct device_node
*np
= pdev
->dev
.of_node
, *endpoint
;
294 struct endpoint_list list
;
295 int i
, ret
, count
= 0;
297 INIT_KFIFO(list
.fifo
);
300 struct device_node
*pipeline
= of_parse_phandle(np
,
301 "allwinner,pipelines",
306 kfifo_put(&list
.fifo
, pipeline
);
309 while (kfifo_get(&list
.fifo
, &endpoint
)) {
310 /* process this endpoint */
311 ret
= sun4i_drv_add_endpoints(&pdev
->dev
, &list
, &match
,
314 /* sun4i_drv_add_endpoints can fail to allocate memory */
322 return component_master_add_with_match(&pdev
->dev
,
323 &sun4i_drv_master_ops
,
329 static int sun4i_drv_remove(struct platform_device
*pdev
)
334 static const struct of_device_id sun4i_drv_of_table
[] = {
335 { .compatible
= "allwinner,sun4i-a10-display-engine" },
336 { .compatible
= "allwinner,sun5i-a10s-display-engine" },
337 { .compatible
= "allwinner,sun5i-a13-display-engine" },
338 { .compatible
= "allwinner,sun6i-a31-display-engine" },
339 { .compatible
= "allwinner,sun6i-a31s-display-engine" },
340 { .compatible
= "allwinner,sun7i-a20-display-engine" },
341 { .compatible
= "allwinner,sun8i-a33-display-engine" },
342 { .compatible
= "allwinner,sun8i-a83t-display-engine" },
343 { .compatible
= "allwinner,sun8i-v3s-display-engine" },
346 MODULE_DEVICE_TABLE(of
, sun4i_drv_of_table
);
348 static struct platform_driver sun4i_drv_platform_driver
= {
349 .probe
= sun4i_drv_probe
,
350 .remove
= sun4i_drv_remove
,
353 .of_match_table
= sun4i_drv_of_table
,
356 module_platform_driver(sun4i_drv_platform_driver
);
358 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
359 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
360 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
361 MODULE_LICENSE("GPL");