1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Free Electrons
4  * Copyright (C) 2015 NextThing Co
5  *
6  * Maxime Ripard <maxime.ripard@free-electrons.com>
7  */
8 
9 #include <linux/component.h>
10 #include <linux/kfifo.h>
11 #include <linux/module.h>
12 #include <linux/of_graph.h>
13 #include <linux/of_reserved_mem.h>
14 #include <linux/platform_device.h>
15 
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_of.h>
22 #include <drm/drm_probe_helper.h>
23 #include <drm/drm_vblank.h>
24 
25 #include "sun4i_drv.h"
26 #include "sun4i_frontend.h"
27 #include "sun4i_framebuffer.h"
28 #include "sun4i_tcon.h"
29 #include "sun8i_tcon_top.h"
30 
drm_sun4i_gem_dumb_create(struct drm_file *file_priv, struct drm_device *drm, struct drm_mode_create_dumb *args)31 static int drm_sun4i_gem_dumb_create(struct drm_file *file_priv,
32 				     struct drm_device *drm,
33 				     struct drm_mode_create_dumb *args)
34 {
35 	/* The hardware only allows even pitches for YUV buffers. */
36 	args->pitch = ALIGN(DIV_ROUND_UP(args->width * args->bpp, 8), 2);
37 
38 	return drm_gem_cma_dumb_create_internal(file_priv, drm, args);
39 }
40 
41 DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops);
42 
43 static struct drm_driver sun4i_drv_driver = {
44 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
45 
46 	/* Generic Operations */
47 	.fops			= &sun4i_drv_fops,
48 	.name			= "sun4i-drm",
49 	.desc			= "Allwinner sun4i Display Engine",
50 	.date			= "20150629",
51 	.major			= 1,
52 	.minor			= 0,
53 
54 	/* GEM Operations */
55 	DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_sun4i_gem_dumb_create),
56 };
57 
sun4i_drv_bind(struct device *dev)58 static int sun4i_drv_bind(struct device *dev)
59 {
60 	struct drm_device *drm;
61 	struct sun4i_drv *drv;
62 	int ret;
63 
64 	drm = drm_dev_alloc(&sun4i_drv_driver, dev);
65 	if (IS_ERR(drm))
66 		return PTR_ERR(drm);
67 
68 	drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
69 	if (!drv) {
70 		ret = -ENOMEM;
71 		goto free_drm;
72 	}
73 
74 	drm->dev_private = drv;
75 	INIT_LIST_HEAD(&drv->frontend_list);
76 	INIT_LIST_HEAD(&drv->engine_list);
77 	INIT_LIST_HEAD(&drv->tcon_list);
78 
79 	ret = of_reserved_mem_device_init(dev);
80 	if (ret && ret != -ENODEV) {
81 		dev_err(drm->dev, "Couldn't claim our memory region\n");
82 		goto free_drm;
83 	}
84 
85 	drm_mode_config_init(drm);
86 
87 	ret = component_bind_all(drm->dev, drm);
88 	if (ret) {
89 		dev_err(drm->dev, "Couldn't bind all pipelines components\n");
90 		goto cleanup_mode_config;
91 	}
92 
93 	/* drm_vblank_init calls kcalloc, which can fail */
94 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
95 	if (ret)
96 		goto unbind_all;
97 
98 	drm->irq_enabled = true;
99 
100 	/* Remove early framebuffers (ie. simplefb) */
101 	drm_fb_helper_remove_conflicting_framebuffers(NULL, "sun4i-drm-fb", false);
102 
103 	sun4i_framebuffer_init(drm);
104 
105 	/* Enable connectors polling */
106 	drm_kms_helper_poll_init(drm);
107 
108 	ret = drm_dev_register(drm, 0);
109 	if (ret)
110 		goto finish_poll;
111 
112 	drm_fbdev_generic_setup(drm, 32);
113 
114 	dev_set_drvdata(dev, drm);
115 
116 	return 0;
117 
118 finish_poll:
119 	drm_kms_helper_poll_fini(drm);
120 unbind_all:
121 	component_unbind_all(dev, NULL);
122 cleanup_mode_config:
123 	drm_mode_config_cleanup(drm);
124 	of_reserved_mem_device_release(dev);
125 free_drm:
126 	drm_dev_put(drm);
127 	return ret;
128 }
129 
sun4i_drv_unbind(struct device *dev)130 static void sun4i_drv_unbind(struct device *dev)
131 {
132 	struct drm_device *drm = dev_get_drvdata(dev);
133 
134 	dev_set_drvdata(dev, NULL);
135 	drm_dev_unregister(drm);
136 	drm_kms_helper_poll_fini(drm);
137 	drm_atomic_helper_shutdown(drm);
138 	drm_mode_config_cleanup(drm);
139 
140 	component_unbind_all(dev, NULL);
141 	of_reserved_mem_device_release(dev);
142 
143 	drm_dev_put(drm);
144 }
145 
146 static const struct component_master_ops sun4i_drv_master_ops = {
147 	.bind	= sun4i_drv_bind,
148 	.unbind	= sun4i_drv_unbind,
149 };
150 
sun4i_drv_node_is_connector(struct device_node *node)151 static bool sun4i_drv_node_is_connector(struct device_node *node)
152 {
153 	return of_device_is_compatible(node, "hdmi-connector");
154 }
155 
sun4i_drv_node_is_frontend(struct device_node *node)156 static bool sun4i_drv_node_is_frontend(struct device_node *node)
157 {
158 	return of_device_is_compatible(node, "allwinner,sun4i-a10-display-frontend") ||
159 		of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") ||
160 		of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") ||
161 		of_device_is_compatible(node, "allwinner,sun7i-a20-display-frontend") ||
162 		of_device_is_compatible(node, "allwinner,sun8i-a23-display-frontend") ||
163 		of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend") ||
164 		of_device_is_compatible(node, "allwinner,sun9i-a80-display-frontend");
165 }
166 
sun4i_drv_node_is_deu(struct device_node *node)167 static bool sun4i_drv_node_is_deu(struct device_node *node)
168 {
169 	return of_device_is_compatible(node, "allwinner,sun9i-a80-deu");
170 }
171 
sun4i_drv_node_is_supported_frontend(struct device_node *node)172 static bool sun4i_drv_node_is_supported_frontend(struct device_node *node)
173 {
174 	if (IS_ENABLED(CONFIG_DRM_SUN4I_BACKEND))
175 		return !!of_match_node(sun4i_frontend_of_table, node);
176 
177 	return false;
178 }
179 
sun4i_drv_node_is_tcon(struct device_node *node)180 static bool sun4i_drv_node_is_tcon(struct device_node *node)
181 {
182 	return !!of_match_node(sun4i_tcon_of_table, node);
183 }
184 
sun4i_drv_node_is_tcon_with_ch0(struct device_node *node)185 static bool sun4i_drv_node_is_tcon_with_ch0(struct device_node *node)
186 {
187 	const struct of_device_id *match;
188 
189 	match = of_match_node(sun4i_tcon_of_table, node);
190 	if (match) {
191 		struct sun4i_tcon_quirks *quirks;
192 
193 		quirks = (struct sun4i_tcon_quirks *)match->data;
194 
195 		return quirks->has_channel_0;
196 	}
197 
198 	return false;
199 }
200 
sun4i_drv_node_is_tcon_top(struct device_node *node)201 static bool sun4i_drv_node_is_tcon_top(struct device_node *node)
202 {
203 	return IS_ENABLED(CONFIG_DRM_SUN8I_TCON_TOP) &&
204 		!!of_match_node(sun8i_tcon_top_of_table, node);
205 }
206 
compare_of(struct device *dev, void *data)207 static int compare_of(struct device *dev, void *data)
208 {
209 	DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n",
210 			 dev->of_node,
211 			 data);
212 
213 	return dev->of_node == data;
214 }
215 
216 /*
217  * The encoder drivers use drm_of_find_possible_crtcs to get upstream
218  * crtcs from the device tree using of_graph. For the results to be
219  * correct, encoders must be probed/bound after _all_ crtcs have been
220  * created. The existing code uses a depth first recursive traversal
221  * of the of_graph, which means the encoders downstream of the TCON
222  * get add right after the first TCON. The second TCON or CRTC will
223  * never be properly associated with encoders connected to it.
224  *
225  * Also, in a dual display pipeline setup, both frontends can feed
226  * either backend, and both backends can feed either TCON, we want
227  * all components of the same type to be added before the next type
228  * in the pipeline. Fortunately, the pipelines are perfectly symmetric,
229  * i.e. components of the same type are at the same depth when counted
230  * from the frontend. The only exception is the third pipeline in
231  * the A80 SoC, which we do not support anyway.
232  *
233  * Hence we can use a breadth first search traversal order to add
234  * components. We do not need to check for duplicates. The component
235  * matching system handles this for us.
236  */
237 struct endpoint_list {
238 	DECLARE_KFIFO(fifo, struct device_node *, 16);
239 };
240 
sun4i_drv_traverse_endpoints(struct endpoint_list *list, struct device_node *node, int port_id)241 static void sun4i_drv_traverse_endpoints(struct endpoint_list *list,
242 					 struct device_node *node,
243 					 int port_id)
244 {
245 	struct device_node *ep, *remote, *port;
246 
247 	port = of_graph_get_port_by_id(node, port_id);
248 	if (!port) {
249 		DRM_DEBUG_DRIVER("No output to bind on port %d\n", port_id);
250 		return;
251 	}
252 
253 	for_each_available_child_of_node(port, ep) {
254 		remote = of_graph_get_remote_port_parent(ep);
255 		if (!remote) {
256 			DRM_DEBUG_DRIVER("Error retrieving the output node\n");
257 			continue;
258 		}
259 
260 		if (sun4i_drv_node_is_tcon(node)) {
261 			/*
262 			 * TCON TOP is always probed before TCON. However, TCON
263 			 * points back to TCON TOP when it is source for HDMI.
264 			 * We have to skip it here to prevent infinite looping
265 			 * between TCON TOP and TCON.
266 			 */
267 			if (sun4i_drv_node_is_tcon_top(remote)) {
268 				DRM_DEBUG_DRIVER("TCON output endpoint is TCON TOP... skipping\n");
269 				of_node_put(remote);
270 				continue;
271 			}
272 
273 			/*
274 			 * If the node is our TCON with channel 0, the first
275 			 * port is used for panel or bridges, and will not be
276 			 * part of the component framework.
277 			 */
278 			if (sun4i_drv_node_is_tcon_with_ch0(node)) {
279 				struct of_endpoint endpoint;
280 
281 				if (of_graph_parse_endpoint(ep, &endpoint)) {
282 					DRM_DEBUG_DRIVER("Couldn't parse endpoint\n");
283 					of_node_put(remote);
284 					continue;
285 				}
286 
287 				if (!endpoint.id) {
288 					DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n");
289 					of_node_put(remote);
290 					continue;
291 				}
292 			}
293 		}
294 
295 		kfifo_put(&list->fifo, remote);
296 	}
297 }
298 
sun4i_drv_add_endpoints(struct device *dev, struct endpoint_list *list, struct component_match **match, struct device_node *node)299 static int sun4i_drv_add_endpoints(struct device *dev,
300 				   struct endpoint_list *list,
301 				   struct component_match **match,
302 				   struct device_node *node)
303 {
304 	int count = 0;
305 
306 	/*
307 	 * The frontend has been disabled in some of our old device
308 	 * trees. If we find a node that is the frontend and is
309 	 * disabled, we should just follow through and parse its
310 	 * child, but without adding it to the component list.
311 	 * Otherwise, we obviously want to add it to the list.
312 	 */
313 	if (!sun4i_drv_node_is_frontend(node) &&
314 	    !of_device_is_available(node))
315 		return 0;
316 
317 	/*
318 	 * The connectors will be the last nodes in our pipeline, we
319 	 * can just bail out.
320 	 */
321 	if (sun4i_drv_node_is_connector(node))
322 		return 0;
323 
324 	/*
325 	 * If the device is either just a regular device, or an
326 	 * enabled frontend supported by the driver, we add it to our
327 	 * component list.
328 	 */
329 	if (!(sun4i_drv_node_is_frontend(node) ||
330 	      sun4i_drv_node_is_deu(node)) ||
331 	    (sun4i_drv_node_is_supported_frontend(node) &&
332 	     of_device_is_available(node))) {
333 		/* Add current component */
334 		DRM_DEBUG_DRIVER("Adding component %pOF\n", node);
335 		drm_of_component_match_add(dev, match, compare_of, node);
336 		count++;
337 	}
338 
339 	/* each node has at least one output */
340 	sun4i_drv_traverse_endpoints(list, node, 1);
341 
342 	/* TCON TOP has second and third output */
343 	if (sun4i_drv_node_is_tcon_top(node)) {
344 		sun4i_drv_traverse_endpoints(list, node, 3);
345 		sun4i_drv_traverse_endpoints(list, node, 5);
346 	}
347 
348 	return count;
349 }
350 
351 #ifdef CONFIG_PM_SLEEP
sun4i_drv_drm_sys_suspend(struct device *dev)352 static int sun4i_drv_drm_sys_suspend(struct device *dev)
353 {
354 	struct drm_device *drm = dev_get_drvdata(dev);
355 
356 	return drm_mode_config_helper_suspend(drm);
357 }
358 
sun4i_drv_drm_sys_resume(struct device *dev)359 static int sun4i_drv_drm_sys_resume(struct device *dev)
360 {
361 	struct drm_device *drm = dev_get_drvdata(dev);
362 
363 	return drm_mode_config_helper_resume(drm);
364 }
365 #endif
366 
367 static const struct dev_pm_ops sun4i_drv_drm_pm_ops = {
368 	SET_SYSTEM_SLEEP_PM_OPS(sun4i_drv_drm_sys_suspend,
369 				sun4i_drv_drm_sys_resume)
370 };
371 
sun4i_drv_probe(struct platform_device *pdev)372 static int sun4i_drv_probe(struct platform_device *pdev)
373 {
374 	struct component_match *match = NULL;
375 	struct device_node *np = pdev->dev.of_node, *endpoint;
376 	struct endpoint_list list;
377 	int i, ret, count = 0;
378 
379 	INIT_KFIFO(list.fifo);
380 
381 	for (i = 0;; i++) {
382 		struct device_node *pipeline = of_parse_phandle(np,
383 								"allwinner,pipelines",
384 								i);
385 		if (!pipeline)
386 			break;
387 
388 		kfifo_put(&list.fifo, pipeline);
389 	}
390 
391 	while (kfifo_get(&list.fifo, &endpoint)) {
392 		/* process this endpoint */
393 		ret = sun4i_drv_add_endpoints(&pdev->dev, &list, &match,
394 					      endpoint);
395 
396 		/* sun4i_drv_add_endpoints can fail to allocate memory */
397 		if (ret < 0)
398 			return ret;
399 
400 		count += ret;
401 	}
402 
403 	if (count)
404 		return component_master_add_with_match(&pdev->dev,
405 						       &sun4i_drv_master_ops,
406 						       match);
407 	else
408 		return 0;
409 }
410 
sun4i_drv_remove(struct platform_device *pdev)411 static int sun4i_drv_remove(struct platform_device *pdev)
412 {
413 	component_master_del(&pdev->dev, &sun4i_drv_master_ops);
414 
415 	return 0;
416 }
417 
418 static const struct of_device_id sun4i_drv_of_table[] = {
419 	{ .compatible = "allwinner,sun4i-a10-display-engine" },
420 	{ .compatible = "allwinner,sun5i-a10s-display-engine" },
421 	{ .compatible = "allwinner,sun5i-a13-display-engine" },
422 	{ .compatible = "allwinner,sun6i-a31-display-engine" },
423 	{ .compatible = "allwinner,sun6i-a31s-display-engine" },
424 	{ .compatible = "allwinner,sun7i-a20-display-engine" },
425 	{ .compatible = "allwinner,sun8i-a23-display-engine" },
426 	{ .compatible = "allwinner,sun8i-a33-display-engine" },
427 	{ .compatible = "allwinner,sun8i-a83t-display-engine" },
428 	{ .compatible = "allwinner,sun8i-h3-display-engine" },
429 	{ .compatible = "allwinner,sun8i-r40-display-engine" },
430 	{ .compatible = "allwinner,sun8i-v3s-display-engine" },
431 	{ .compatible = "allwinner,sun9i-a80-display-engine" },
432 	{ .compatible = "allwinner,sun50i-a64-display-engine" },
433 	{ .compatible = "allwinner,sun50i-h6-display-engine" },
434 	{ }
435 };
436 MODULE_DEVICE_TABLE(of, sun4i_drv_of_table);
437 
438 static struct platform_driver sun4i_drv_platform_driver = {
439 	.probe		= sun4i_drv_probe,
440 	.remove		= sun4i_drv_remove,
441 	.driver		= {
442 		.name		= "sun4i-drm",
443 		.of_match_table	= sun4i_drv_of_table,
444 		.pm = &sun4i_drv_drm_pm_ops,
445 	},
446 };
447 module_platform_driver(sun4i_drv_platform_driver);
448 
449 MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>");
450 MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
451 MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver");
452 MODULE_LICENSE("GPL");
453