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