Commit | Line | Data |
---|---|---|
9026e0d1 MR |
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> | |
8b11aafa | 14 | #include <linux/kfifo.h> |
9026e0d1 | 15 | #include <linux/of_graph.h> |
596afb6f | 16 | #include <linux/of_reserved_mem.h> |
9026e0d1 MR |
17 | |
18 | #include <drm/drmP.h> | |
19 | #include <drm/drm_crtc_helper.h> | |
20 | #include <drm/drm_fb_cma_helper.h> | |
21 | #include <drm/drm_gem_cma_helper.h> | |
44adece5 | 22 | #include <drm/drm_fb_helper.h> |
97ac0e47 | 23 | #include <drm/drm_of.h> |
9026e0d1 | 24 | |
9026e0d1 MR |
25 | #include "sun4i_drv.h" |
26 | #include "sun4i_framebuffer.h" | |
b3f266e4 | 27 | #include "sun4i_tcon.h" |
9026e0d1 | 28 | |
2a596fc9 JL |
29 | static void sun4i_drv_lastclose(struct drm_device *dev) |
30 | { | |
31 | struct sun4i_drv *drv = dev->dev_private; | |
32 | ||
33 | drm_fbdev_cma_restore_mode(drv->fbdev); | |
34 | } | |
35 | ||
d55f7e5d | 36 | DEFINE_DRM_GEM_CMA_FOPS(sun4i_drv_fops); |
9026e0d1 MR |
37 | |
38 | static struct drm_driver sun4i_drv_driver = { | |
39 | .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME | DRIVER_ATOMIC, | |
40 | ||
41 | /* Generic Operations */ | |
2a596fc9 | 42 | .lastclose = sun4i_drv_lastclose, |
9026e0d1 MR |
43 | .fops = &sun4i_drv_fops, |
44 | .name = "sun4i-drm", | |
45 | .desc = "Allwinner sun4i Display Engine", | |
46 | .date = "20150629", | |
47 | .major = 1, | |
48 | .minor = 0, | |
49 | ||
50 | /* GEM Operations */ | |
51 | .dumb_create = drm_gem_cma_dumb_create, | |
ae2c6221 | 52 | .gem_free_object_unlocked = drm_gem_cma_free_object, |
9026e0d1 MR |
53 | .gem_vm_ops = &drm_gem_cma_vm_ops, |
54 | ||
55 | /* PRIME Operations */ | |
56 | .prime_handle_to_fd = drm_gem_prime_handle_to_fd, | |
57 | .prime_fd_to_handle = drm_gem_prime_fd_to_handle, | |
58 | .gem_prime_import = drm_gem_prime_import, | |
59 | .gem_prime_export = drm_gem_prime_export, | |
60 | .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, | |
61 | .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, | |
62 | .gem_prime_vmap = drm_gem_cma_prime_vmap, | |
63 | .gem_prime_vunmap = drm_gem_cma_prime_vunmap, | |
64 | .gem_prime_mmap = drm_gem_cma_prime_mmap, | |
65 | ||
66 | /* Frame Buffer Operations */ | |
9026e0d1 MR |
67 | }; |
68 | ||
3d6bd906 MR |
69 | static void sun4i_remove_framebuffers(void) |
70 | { | |
71 | struct apertures_struct *ap; | |
72 | ||
73 | ap = alloc_apertures(1); | |
74 | if (!ap) | |
75 | return; | |
76 | ||
77 | /* The framebuffer can be located anywhere in RAM */ | |
78 | ap->ranges[0].base = 0; | |
79 | ap->ranges[0].size = ~0; | |
80 | ||
44adece5 | 81 | drm_fb_helper_remove_conflicting_framebuffers(ap, "sun4i-drm-fb", false); |
3d6bd906 MR |
82 | kfree(ap); |
83 | } | |
84 | ||
9026e0d1 MR |
85 | static int sun4i_drv_bind(struct device *dev) |
86 | { | |
87 | struct drm_device *drm; | |
88 | struct sun4i_drv *drv; | |
89 | int ret; | |
90 | ||
91 | drm = drm_dev_alloc(&sun4i_drv_driver, dev); | |
0f288605 TG |
92 | if (IS_ERR(drm)) |
93 | return PTR_ERR(drm); | |
9026e0d1 | 94 | |
9026e0d1 MR |
95 | drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL); |
96 | if (!drv) { | |
97 | ret = -ENOMEM; | |
98 | goto free_drm; | |
99 | } | |
100 | drm->dev_private = drv; | |
87969338 | 101 | INIT_LIST_HEAD(&drv->engine_list); |
80a58240 | 102 | INIT_LIST_HEAD(&drv->tcon_list); |
9026e0d1 | 103 | |
596afb6f MR |
104 | ret = of_reserved_mem_device_init(dev); |
105 | if (ret && ret != -ENODEV) { | |
106 | dev_err(drm->dev, "Couldn't claim our memory region\n"); | |
107 | goto free_drm; | |
108 | } | |
109 | ||
9026e0d1 MR |
110 | drm_mode_config_init(drm); |
111 | ||
112 | ret = component_bind_all(drm->dev, drm); | |
113 | if (ret) { | |
114 | dev_err(drm->dev, "Couldn't bind all pipelines components\n"); | |
9d56defb | 115 | goto cleanup_mode_config; |
9026e0d1 MR |
116 | } |
117 | ||
070badfa CYT |
118 | /* drm_vblank_init calls kcalloc, which can fail */ |
119 | ret = drm_vblank_init(drm, drm->mode_config.num_crtc); | |
120 | if (ret) | |
121 | goto free_mem_region; | |
122 | ||
9026e0d1 MR |
123 | drm->irq_enabled = true; |
124 | ||
3d6bd906 MR |
125 | /* Remove early framebuffers (ie. simplefb) */ |
126 | sun4i_remove_framebuffers(); | |
127 | ||
9026e0d1 MR |
128 | /* Create our framebuffer */ |
129 | drv->fbdev = sun4i_framebuffer_init(drm); | |
130 | if (IS_ERR(drv->fbdev)) { | |
131 | dev_err(drm->dev, "Couldn't create our framebuffer\n"); | |
132 | ret = PTR_ERR(drv->fbdev); | |
9d56defb | 133 | goto cleanup_mode_config; |
9026e0d1 MR |
134 | } |
135 | ||
136 | /* Enable connectors polling */ | |
137 | drm_kms_helper_poll_init(drm); | |
138 | ||
139 | ret = drm_dev_register(drm, 0); | |
140 | if (ret) | |
9d56defb | 141 | goto finish_poll; |
9026e0d1 | 142 | |
9026e0d1 MR |
143 | return 0; |
144 | ||
9d56defb CYT |
145 | finish_poll: |
146 | drm_kms_helper_poll_fini(drm); | |
147 | sun4i_framebuffer_free(drm); | |
148 | cleanup_mode_config: | |
149 | drm_mode_config_cleanup(drm); | |
596afb6f MR |
150 | free_mem_region: |
151 | of_reserved_mem_device_release(dev); | |
9026e0d1 MR |
152 | free_drm: |
153 | drm_dev_unref(drm); | |
154 | return ret; | |
155 | } | |
156 | ||
157 | static void sun4i_drv_unbind(struct device *dev) | |
158 | { | |
159 | struct drm_device *drm = dev_get_drvdata(dev); | |
160 | ||
161 | drm_dev_unregister(drm); | |
162 | drm_kms_helper_poll_fini(drm); | |
163 | sun4i_framebuffer_free(drm); | |
92caf9be | 164 | drm_mode_config_cleanup(drm); |
596afb6f | 165 | of_reserved_mem_device_release(dev); |
9026e0d1 MR |
166 | drm_dev_unref(drm); |
167 | } | |
168 | ||
169 | static const struct component_master_ops sun4i_drv_master_ops = { | |
170 | .bind = sun4i_drv_bind, | |
171 | .unbind = sun4i_drv_unbind, | |
172 | }; | |
173 | ||
49baeb07 MR |
174 | static bool sun4i_drv_node_is_connector(struct device_node *node) |
175 | { | |
176 | return of_device_is_compatible(node, "hdmi-connector"); | |
177 | } | |
178 | ||
9026e0d1 MR |
179 | static bool sun4i_drv_node_is_frontend(struct device_node *node) |
180 | { | |
9a8187c0 CYT |
181 | return of_device_is_compatible(node, "allwinner,sun4i-a10-display-frontend") || |
182 | of_device_is_compatible(node, "allwinner,sun5i-a13-display-frontend") || | |
49c440e8 | 183 | of_device_is_compatible(node, "allwinner,sun6i-a31-display-frontend") || |
aaddb6d2 | 184 | of_device_is_compatible(node, "allwinner,sun7i-a20-display-frontend") || |
4a408f1f | 185 | of_device_is_compatible(node, "allwinner,sun8i-a33-display-frontend"); |
9026e0d1 MR |
186 | } |
187 | ||
29e57fab MR |
188 | static bool sun4i_drv_node_is_tcon(struct device_node *node) |
189 | { | |
ff71c2cf | 190 | return !!of_match_node(sun4i_tcon_of_table, node); |
29e57fab MR |
191 | } |
192 | ||
9026e0d1 MR |
193 | static int compare_of(struct device *dev, void *data) |
194 | { | |
4bf99144 RH |
195 | DRM_DEBUG_DRIVER("Comparing of node %pOF with %pOF\n", |
196 | dev->of_node, | |
197 | data); | |
9026e0d1 MR |
198 | |
199 | return dev->of_node == data; | |
200 | } | |
201 | ||
da82b878 CYT |
202 | /* |
203 | * The encoder drivers use drm_of_find_possible_crtcs to get upstream | |
204 | * crtcs from the device tree using of_graph. For the results to be | |
205 | * correct, encoders must be probed/bound after _all_ crtcs have been | |
206 | * created. The existing code uses a depth first recursive traversal | |
207 | * of the of_graph, which means the encoders downstream of the TCON | |
208 | * get add right after the first TCON. The second TCON or CRTC will | |
209 | * never be properly associated with encoders connected to it. | |
210 | * | |
211 | * Also, in a dual display pipeline setup, both frontends can feed | |
212 | * either backend, and both backends can feed either TCON, we want | |
213 | * all components of the same type to be added before the next type | |
214 | * in the pipeline. Fortunately, the pipelines are perfectly symmetric, | |
215 | * i.e. components of the same type are at the same depth when counted | |
216 | * from the frontend. The only exception is the third pipeline in | |
217 | * the A80 SoC, which we do not support anyway. | |
218 | * | |
219 | * Hence we can use a breadth first search traversal order to add | |
220 | * components. We do not need to check for duplicates. The component | |
221 | * matching system handles this for us. | |
222 | */ | |
223 | struct endpoint_list { | |
8b11aafa | 224 | DECLARE_KFIFO(fifo, struct device_node *, 16); |
da82b878 CYT |
225 | }; |
226 | ||
9026e0d1 | 227 | static int sun4i_drv_add_endpoints(struct device *dev, |
8b11aafa | 228 | struct endpoint_list *list, |
9026e0d1 MR |
229 | struct component_match **match, |
230 | struct device_node *node) | |
231 | { | |
232 | struct device_node *port, *ep, *remote; | |
233 | int count = 0; | |
234 | ||
235 | /* | |
236 | * We don't support the frontend for now, so we will never | |
237 | * have a device bound. Just skip over it, but we still want | |
238 | * the rest our pipeline to be added. | |
239 | */ | |
240 | if (!sun4i_drv_node_is_frontend(node) && | |
241 | !of_device_is_available(node)) | |
242 | return 0; | |
243 | ||
49baeb07 MR |
244 | /* |
245 | * The connectors will be the last nodes in our pipeline, we | |
246 | * can just bail out. | |
247 | */ | |
248 | if (sun4i_drv_node_is_connector(node)) | |
249 | return 0; | |
250 | ||
9026e0d1 MR |
251 | if (!sun4i_drv_node_is_frontend(node)) { |
252 | /* Add current component */ | |
4bf99144 | 253 | DRM_DEBUG_DRIVER("Adding component %pOF\n", node); |
97ac0e47 | 254 | drm_of_component_match_add(dev, match, compare_of, node); |
9026e0d1 MR |
255 | count++; |
256 | } | |
257 | ||
258 | /* Inputs are listed first, then outputs */ | |
259 | port = of_graph_get_port_by_id(node, 1); | |
260 | if (!port) { | |
261 | DRM_DEBUG_DRIVER("No output to bind\n"); | |
262 | return count; | |
263 | } | |
264 | ||
265 | for_each_available_child_of_node(port, ep) { | |
266 | remote = of_graph_get_remote_port_parent(ep); | |
267 | if (!remote) { | |
268 | DRM_DEBUG_DRIVER("Error retrieving the output node\n"); | |
269 | of_node_put(remote); | |
270 | continue; | |
271 | } | |
272 | ||
29e57fab | 273 | /* |
894f5a9f MR |
274 | * If the node is our TCON, the first port is used for |
275 | * panel or bridges, and will not be part of the | |
29e57fab MR |
276 | * component framework. |
277 | */ | |
278 | if (sun4i_drv_node_is_tcon(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 | continue; | |
284 | } | |
285 | ||
286 | if (!endpoint.id) { | |
287 | DRM_DEBUG_DRIVER("Endpoint is our panel... skipping\n"); | |
288 | continue; | |
289 | } | |
290 | } | |
291 | ||
8b11aafa | 292 | kfifo_put(&list->fifo, remote); |
9026e0d1 MR |
293 | } |
294 | ||
295 | return count; | |
296 | } | |
297 | ||
298 | static int sun4i_drv_probe(struct platform_device *pdev) | |
299 | { | |
300 | struct component_match *match = NULL; | |
8b11aafa MR |
301 | struct device_node *np = pdev->dev.of_node, *endpoint; |
302 | struct endpoint_list list; | |
da82b878 | 303 | int i, ret, count = 0; |
8b11aafa MR |
304 | |
305 | INIT_KFIFO(list.fifo); | |
9026e0d1 MR |
306 | |
307 | for (i = 0;; i++) { | |
308 | struct device_node *pipeline = of_parse_phandle(np, | |
309 | "allwinner,pipelines", | |
310 | i); | |
311 | if (!pipeline) | |
312 | break; | |
313 | ||
8b11aafa | 314 | kfifo_put(&list.fifo, pipeline); |
da82b878 CYT |
315 | } |
316 | ||
8b11aafa | 317 | while (kfifo_get(&list.fifo, &endpoint)) { |
da82b878 | 318 | /* process this endpoint */ |
8b11aafa MR |
319 | ret = sun4i_drv_add_endpoints(&pdev->dev, &list, &match, |
320 | endpoint); | |
da82b878 CYT |
321 | |
322 | /* sun4i_drv_add_endpoints can fail to allocate memory */ | |
323 | if (ret < 0) | |
8b11aafa | 324 | return ret; |
da82b878 CYT |
325 | |
326 | count += ret; | |
9026e0d1 MR |
327 | } |
328 | ||
329 | if (count) | |
330 | return component_master_add_with_match(&pdev->dev, | |
331 | &sun4i_drv_master_ops, | |
332 | match); | |
333 | else | |
334 | return 0; | |
335 | } | |
336 | ||
337 | static int sun4i_drv_remove(struct platform_device *pdev) | |
338 | { | |
339 | return 0; | |
340 | } | |
341 | ||
342 | static const struct of_device_id sun4i_drv_of_table[] = { | |
9a8187c0 | 343 | { .compatible = "allwinner,sun4i-a10-display-engine" }, |
110d33dd | 344 | { .compatible = "allwinner,sun5i-a10s-display-engine" }, |
9026e0d1 | 345 | { .compatible = "allwinner,sun5i-a13-display-engine" }, |
49c440e8 CYT |
346 | { .compatible = "allwinner,sun6i-a31-display-engine" }, |
347 | { .compatible = "allwinner,sun6i-a31s-display-engine" }, | |
aaddb6d2 | 348 | { .compatible = "allwinner,sun7i-a20-display-engine" }, |
4a408f1f | 349 | { .compatible = "allwinner,sun8i-a33-display-engine" }, |
9df90c25 | 350 | { .compatible = "allwinner,sun8i-v3s-display-engine" }, |
9026e0d1 MR |
351 | { } |
352 | }; | |
353 | MODULE_DEVICE_TABLE(of, sun4i_drv_of_table); | |
354 | ||
355 | static struct platform_driver sun4i_drv_platform_driver = { | |
356 | .probe = sun4i_drv_probe, | |
357 | .remove = sun4i_drv_remove, | |
358 | .driver = { | |
359 | .name = "sun4i-drm", | |
360 | .of_match_table = sun4i_drv_of_table, | |
361 | }, | |
362 | }; | |
363 | module_platform_driver(sun4i_drv_platform_driver); | |
364 | ||
365 | MODULE_AUTHOR("Boris Brezillon <boris.brezillon@free-electrons.com>"); | |
366 | MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>"); | |
367 | MODULE_DESCRIPTION("Allwinner A10 Display Engine DRM/KMS Driver"); | |
368 | MODULE_LICENSE("GPL"); |