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