xref: /openbmc/linux/drivers/gpu/drm/sun4i/sun4i_drv.c (revision 981ab3f1)
1 /*
2  * Copyright (C) 2015 Free Electrons
3  * Copyright (C) 2015 NextThing Co
4  *
5  * Maxime Ripard <maxime.ripard@free-electrons.com>
6  *
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.
11  */
12 
13 #include <linux/component.h>
14 #include <linux/of_graph.h>
15 #include <linux/of_reserved_mem.h>
16 
17 #include <drm/drmP.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>
23 
24 #include "sun4i_drv.h"
25 #include "sun4i_framebuffer.h"
26 #include "sun4i_tcon.h"
27 
28 DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops);
29 
30 static struct drm_driver sun4i_drv_driver = {
31 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC,
32 
33 	/* Generic Operations */
34 	.fops			= &sun4i_drv_fops,
35 	.name			= "sun4i-drm",
36 	.desc			= "Allwinner sun4i Display Engine",
37 	.date			= "20150629",
38 	.major			= 1,
39 	.minor			= 0,
40 
41 	/* GEM Operations */
42 	.dumb_create		= drm_gem_cma_dumb_create,
43 	.gem_free_object_unlocked = drm_gem_cma_free_object,
44 	.gem_vm_ops		= &drm_gem_cma_vm_ops,
45 
46 	/* PRIME Operations */
47 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
48 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
49 	.gem_prime_import	= drm_gem_prime_import,
50 	.gem_prime_export	= drm_gem_prime_export,
51 	.gem_prime_get_sg_table	= drm_gem_cma_prime_get_sg_table,
52 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
53 	.gem_prime_vmap		= drm_gem_cma_prime_vmap,
54 	.gem_prime_vunmap	= drm_gem_cma_prime_vunmap,
55 	.gem_prime_mmap		= drm_gem_cma_prime_mmap,
56 
57 	/* Frame Buffer Operations */
58 };
59 
60 static void sun4i_remove_framebuffers(void)
61 {
62 	struct apertures_struct *ap;
63 
64 	ap = alloc_apertures(1);
65 	if (!ap)
66 		return;
67 
68 	/* The framebuffer can be located anywhere in RAM */
69 	ap->ranges[0].base = 0;
70 	ap->ranges[0].size = ~0;
71 
72 	drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false);
73 	kfree(ap);
74 }
75 
76 static int sun4i_drv_bind(struct device *dev)
77 {
78 	struct drm_device *drm;
79 	struct sun4i_drv *drv;
80 	int ret;
81 
82 	drm = drm_dev_alloc(&sun4i_drv_driver, dev);
83 	if (IS_ERR(drm))
84 		return PTR_ERR(drm);
85 
86 	drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
87 	if (!drv) {
88 		ret = -ENOMEM;
89 		goto free_drm;
90 	}
91 	drm->dev_private = drv;
92 	INIT_LIST_HEAD(&drv->engine_list);
93 	INIT_LIST_HEAD(&drv->tcon_list);
94 
95 	ret = of_reserved_mem_device_init(dev);
96 	if (ret && ret != -ENODEV) {
97 		dev_err(drm->dev, "Couldn't claim our memory region\n");
98 		goto free_drm;
99 	}
100 
101 	/* drm_vblank_init calls kcalloc, which can fail */
102 	ret = drm_vblank_init(drm, 1);
103 	if (ret)
104 		goto free_mem_region;
105 
106 	drm_mode_config_init(drm);
107 
108 	ret = component_bind_all(drm->dev, drm);
109 	if (ret) {
110 		dev_err(drm->dev, "Couldn't bind all pipelines components\n");
111 		goto cleanup_mode_config;
112 	}
113 
114 	drm->irq_enabled = true;
115 
116 	/* Remove early framebuffers (ie. simplefb) */
117 	sun4i_remove_framebuffers();
118 
119 	/* Create our framebuffer */
120 	drv->fbdev = sun4i_framebuffer_init(drm);
121 	if (IS_ERR(drv->fbdev)) {
122 		dev_err(drm->dev, "Couldn't create our framebuffer\n");
123 		ret = PTR_ERR(drv->fbdev);
124 		goto cleanup_mode_config;
125 	}
126 
127 	/* Enable connectors polling */
128 	drm_kms_helper_poll_init(drm);
129 
130 	ret = drm_dev_register(drm, 0);
131 	if (ret)
132 		goto finish_poll;
133 
134 	return 0;
135 
136 finish_poll:
137 	drm_kms_helper_poll_fini(drm);
138 	sun4i_framebuffer_free(drm);
139 cleanup_mode_config:
140 	drm_mode_config_cleanup(drm);
141 free_mem_region:
142 	of_reserved_mem_device_release(dev);
143 free_drm:
144 	drm_dev_unref(drm);
145 	return ret;
146 }
147 
148 static void sun4i_drv_unbind(struct device *dev)
149 {
150 	struct drm_device *drm = dev_get_drvdata(dev);
151 
152 	drm_dev_unregister(drm);
153 	drm_kms_helper_poll_fini(drm);
154 	sun4i_framebuffer_free(drm);
155 	drm_mode_config_cleanup(drm);
156 	of_reserved_mem_device_release(dev);
157 	drm_dev_unref(drm);
158 }
159 
160 static const struct component_master_ops sun4i_drv_master_ops = {
161 	.bind	= sun4i_drv_bind,
162 	.unbind	= sun4i_drv_unbind,
163 };
164 
165 static bool sun4i_drv_node_is_connector(struct device_node *node)
166 {
167 	return of_device_is_compatible(node, "hdmi-connector");
168 }
169 
170 static bool sun4i_drv_node_is_frontend(struct device_node *node)
171 {
172 	return of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
173 		of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
174 		of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend");
175 }
176 
177 static bool sun4i_drv_node_is_tcon(struct device_node *node)
178 {
179 	return of_device_is_compatible(node, "allwinner,sun5i-a13-tcon") ||
180 		of_device_is_compatible(node, "allwinner,sun6i-a31-tcon") ||
181 		of_device_is_compatible(node, "allwinner,sun6i-a31s-tcon") ||
182 		of_device_is_compatible(node, "allwinner,sun8i-a33-tcon") ||
183 		of_device_is_compatible(node, "allwinner,sun8i-v3s-tcon");
184 }
185 
186 static int compare_of(struct device *dev, void *data)
187 {
188 	DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
189 			 dev->of_node,
190 			 data);
191 
192 	return dev->of_node == data;
193 }
194 
195 static int sun4i_drv_add_endpoints(struct device *dev,
196 				   struct component_match **match,
197 				   struct device_node *node)
198 {
199 	struct device_node *port, *ep, *remote;
200 	int count = 0;
201 
202 	/*
203 	 * We don't support the frontend for now, so we will never
204 	 * have a device bound. Just skip over it, but we still want
205 	 * the rest our pipeline to be added.
206 	 */
207 	if (!sun4i_drv_node_is_frontend(node) &&
208 	    !of_device_is_available(node))
209 		return 0;
210 
211 	/*
212 	 * The connectors will be the last nodes in our pipeline, we
213 	 * can just bail out.
214 	 */
215 	if (sun4i_drv_node_is_connector(node))
216 		return 0;
217 
218 	if (!sun4i_drv_node_is_frontend(node)) {
219 		/* Add current component */
220 		DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
221 		drm_of_component_match_add(dev, match, compare_of, node);
222 		count++;
223 	}
224 
225 	/* Inputs are listed first, then outputs */
226 	port = of_graph_get_port_by_id(node, 1);
227 	if (!port) {
228 		DRM_DEBUG_DRIVER("No output to bind\n");
229 		return count;
230 	}
231 
232 	for_each_available_child_of_node(port, ep) {
233 		remote = of_graph_get_remote_port_parent(ep);
234 		if (!remote) {
235 			DRM_DEBUG_DRIVER("Error retrieving the output node\n");
236 			of_node_put(remote);
237 			continue;
238 		}
239 
240 		/*
241 		 * If the node is our TCON, the first port is used for
242 		 * panel or bridges, and will not be part of the
243 		 * component framework.
244 		 */
245 		if (sun4i_drv_node_is_tcon(node)) {
246 			struct of_endpoint endpoint;
247 
248 			if (of_graph_parse_endpoint(ep, &endpoint)) {
249 				DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
250 				continue;
251 			}
252 
253 			if (!endpoint.id) {
254 				DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
255 				continue;
256 			}
257 		}
258 
259 		/* Walk down our tree */
260 		count += sun4i_drv_add_endpoints(dev, match, remote);
261 
262 		of_node_put(remote);
263 	}
264 
265 	return count;
266 }
267 
268 static int sun4i_drv_probe(struct platform_device *pdev)
269 {
270 	struct component_match *match = NULL;
271 	struct device_node *np = pdev->dev.of_node;
272 	int i, count = 0;
273 
274 	for (i = 0;; i++) {
275 		struct device_node *pipeline = of_parse_phandle(np,
276 								"allwinner,pipelines",
277 								i);
278 		if (!pipeline)
279 			break;
280 
281 		count += sun4i_drv_add_endpoints(&pdev->dev, &match,
282 						pipeline);
283 		of_node_put(pipeline);
284 
285 		DRM_DEBUG_DRIVER("Queued %d outputs on pipeline %d\n",
286 				 count, i);
287 	}
288 
289 	if (count)
290 		return component_master_add_with_match(&pdev->dev,
291 						       &sun4i_drv_master_ops,
292 						       match);
293 	else
294 		return 0;
295 }
296 
297 static int sun4i_drv_remove(struct platform_device *pdev)
298 {
299 	return 0;
300 }
301 
302 static const struct of_device_id sun4i_drv_of_table[] = {
303 	{ .compatible = "allwinner,sun5i-a10s-display-engine" },
304 	{ .compatible = "allwinner,sun5i-a13-display-engine" },
305 	{ .compatible = "allwinner,sun6i-a31-display-engine" },
306 	{ .compatible = "allwinner,sun6i-a31s-display-engine" },
307 	{ .compatible = "allwinner,sun8i-a33-display-engine" },
308 	{ .compatible = "allwinner,sun8i-v3s-display-engine" },
309 	{ }
310 };
311 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
312 
313 static struct platform_driver sun4i_drv_platform_driver = {
314 	.probe		= sun4i_drv_probe,
315 	.remove		= sun4i_drv_remove,
316 	.driver		= {
317 		.name		= "sun4i-drm",
318 		.of_match_table	= sun4i_drv_of_table,
319 	},
320 };
321 module_platform_driver(sun4i_drv_platform_driver);
322 
323 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
324 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
325 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
326 MODULE_LICENSE("GPL");
327