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