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