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