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