1 /* 2 * Copyright (C) 2012 Russell King 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License version 2 as 6 * published by the Free Software Foundation. 7 */ 8 #include <linux/clk.h> 9 #include <linux/component.h> 10 #include <linux/module.h> 11 #include <linux/of_graph.h> 12 #include <drm/drmP.h> 13 #include <drm/drm_crtc_helper.h> 14 #include <drm/drm_of.h> 15 #include "armada_crtc.h" 16 #include "armada_drm.h" 17 #include "armada_gem.h" 18 #include "armada_hw.h" 19 #include <drm/armada_drm.h> 20 #include "armada_ioctlP.h" 21 22 static void armada_drm_unref_work(struct work_struct *work) 23 { 24 struct armada_private *priv = 25 container_of(work, struct armada_private, fb_unref_work); 26 struct drm_framebuffer *fb; 27 28 while (kfifo_get(&priv->fb_unref, &fb)) 29 drm_framebuffer_unreference(fb); 30 } 31 32 /* Must be called with dev->event_lock held */ 33 void __armada_drm_queue_unref_work(struct drm_device *dev, 34 struct drm_framebuffer *fb) 35 { 36 struct armada_private *priv = dev->dev_private; 37 38 WARN_ON(!kfifo_put(&priv->fb_unref, fb)); 39 schedule_work(&priv->fb_unref_work); 40 } 41 42 void armada_drm_queue_unref_work(struct drm_device *dev, 43 struct drm_framebuffer *fb) 44 { 45 unsigned long flags; 46 47 spin_lock_irqsave(&dev->event_lock, flags); 48 __armada_drm_queue_unref_work(dev, fb); 49 spin_unlock_irqrestore(&dev->event_lock, flags); 50 } 51 52 /* These are called under the vbl_lock. */ 53 static int armada_drm_enable_vblank(struct drm_device *dev, unsigned int pipe) 54 { 55 struct armada_private *priv = dev->dev_private; 56 armada_drm_crtc_enable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA); 57 return 0; 58 } 59 60 static void armada_drm_disable_vblank(struct drm_device *dev, unsigned int pipe) 61 { 62 struct armada_private *priv = dev->dev_private; 63 armada_drm_crtc_disable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA); 64 } 65 66 static struct drm_ioctl_desc armada_ioctls[] = { 67 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0), 68 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0), 69 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0), 70 }; 71 72 static void armada_drm_lastclose(struct drm_device *dev) 73 { 74 armada_fbdev_lastclose(dev); 75 } 76 77 static const struct file_operations armada_drm_fops = { 78 .owner = THIS_MODULE, 79 .llseek = no_llseek, 80 .read = drm_read, 81 .poll = drm_poll, 82 .unlocked_ioctl = drm_ioctl, 83 .mmap = drm_gem_mmap, 84 .open = drm_open, 85 .release = drm_release, 86 }; 87 88 static struct drm_driver armada_drm_driver = { 89 .lastclose = armada_drm_lastclose, 90 .get_vblank_counter = drm_vblank_no_hw_counter, 91 .enable_vblank = armada_drm_enable_vblank, 92 .disable_vblank = armada_drm_disable_vblank, 93 .gem_free_object_unlocked = armada_gem_free_object, 94 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 95 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 96 .gem_prime_export = armada_gem_prime_export, 97 .gem_prime_import = armada_gem_prime_import, 98 .dumb_create = armada_gem_dumb_create, 99 .dumb_map_offset = armada_gem_dumb_map_offset, 100 .dumb_destroy = armada_gem_dumb_destroy, 101 .gem_vm_ops = &armada_gem_vm_ops, 102 .major = 1, 103 .minor = 0, 104 .name = "armada-drm", 105 .desc = "Armada SoC DRM", 106 .date = "20120730", 107 .driver_features = DRIVER_GEM | DRIVER_MODESET | 108 DRIVER_PRIME, 109 .ioctls = armada_ioctls, 110 .fops = &armada_drm_fops, 111 }; 112 113 static int armada_drm_bind(struct device *dev) 114 { 115 struct armada_private *priv; 116 struct resource *mem = NULL; 117 int ret, n; 118 119 for (n = 0; ; n++) { 120 struct resource *r = platform_get_resource(to_platform_device(dev), 121 IORESOURCE_MEM, n); 122 if (!r) 123 break; 124 125 /* Resources above 64K are graphics memory */ 126 if (resource_size(r) > SZ_64K) 127 mem = r; 128 else 129 return -EINVAL; 130 } 131 132 if (!mem) 133 return -ENXIO; 134 135 if (!devm_request_mem_region(dev, mem->start, resource_size(mem), 136 "armada-drm")) 137 return -EBUSY; 138 139 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 140 if (!priv) 141 return -ENOMEM; 142 143 /* 144 * The drm_device structure must be at the start of 145 * armada_private for drm_dev_unref() to work correctly. 146 */ 147 BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0); 148 149 ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev); 150 if (ret) { 151 dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n", 152 __func__, ret); 153 kfree(priv); 154 return ret; 155 } 156 157 priv->drm.platformdev = to_platform_device(dev); 158 priv->drm.dev_private = priv; 159 160 platform_set_drvdata(priv->drm.platformdev, &priv->drm); 161 162 INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work); 163 INIT_KFIFO(priv->fb_unref); 164 165 /* Mode setting support */ 166 drm_mode_config_init(&priv->drm); 167 priv->drm.mode_config.min_width = 320; 168 priv->drm.mode_config.min_height = 200; 169 170 /* 171 * With vscale enabled, the maximum width is 1920 due to the 172 * 1920 by 3 lines RAM 173 */ 174 priv->drm.mode_config.max_width = 1920; 175 priv->drm.mode_config.max_height = 2048; 176 177 priv->drm.mode_config.preferred_depth = 24; 178 priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs; 179 drm_mm_init(&priv->linear, mem->start, resource_size(mem)); 180 mutex_init(&priv->linear_lock); 181 182 ret = component_bind_all(dev, &priv->drm); 183 if (ret) 184 goto err_kms; 185 186 ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc); 187 if (ret) 188 goto err_comp; 189 190 priv->drm.irq_enabled = true; 191 192 ret = armada_fbdev_init(&priv->drm); 193 if (ret) 194 goto err_comp; 195 196 drm_kms_helper_poll_init(&priv->drm); 197 198 ret = drm_dev_register(&priv->drm, 0); 199 if (ret) 200 goto err_poll; 201 202 #ifdef CONFIG_DEBUG_FS 203 armada_drm_debugfs_init(priv->drm.primary); 204 #endif 205 206 return 0; 207 208 err_poll: 209 drm_kms_helper_poll_fini(&priv->drm); 210 armada_fbdev_fini(&priv->drm); 211 err_comp: 212 component_unbind_all(dev, &priv->drm); 213 err_kms: 214 drm_mode_config_cleanup(&priv->drm); 215 drm_mm_takedown(&priv->linear); 216 flush_work(&priv->fb_unref_work); 217 drm_dev_unref(&priv->drm); 218 return ret; 219 } 220 221 static void armada_drm_unbind(struct device *dev) 222 { 223 struct drm_device *drm = dev_get_drvdata(dev); 224 struct armada_private *priv = drm->dev_private; 225 226 drm_kms_helper_poll_fini(&priv->drm); 227 armada_fbdev_fini(&priv->drm); 228 229 #ifdef CONFIG_DEBUG_FS 230 armada_drm_debugfs_cleanup(priv->drm.primary); 231 #endif 232 drm_dev_unregister(&priv->drm); 233 234 component_unbind_all(dev, &priv->drm); 235 236 drm_mode_config_cleanup(&priv->drm); 237 drm_mm_takedown(&priv->linear); 238 flush_work(&priv->fb_unref_work); 239 240 drm_dev_unref(&priv->drm); 241 } 242 243 static int compare_of(struct device *dev, void *data) 244 { 245 return dev->of_node == data; 246 } 247 248 static int compare_dev_name(struct device *dev, void *data) 249 { 250 const char *name = data; 251 return !strcmp(dev_name(dev), name); 252 } 253 254 static void armada_add_endpoints(struct device *dev, 255 struct component_match **match, struct device_node *port) 256 { 257 struct device_node *ep, *remote; 258 259 for_each_child_of_node(port, ep) { 260 remote = of_graph_get_remote_port_parent(ep); 261 if (!remote || !of_device_is_available(remote)) { 262 of_node_put(remote); 263 continue; 264 } else if (!of_device_is_available(remote->parent)) { 265 dev_warn(dev, "parent device of %s is not available\n", 266 remote->full_name); 267 of_node_put(remote); 268 continue; 269 } 270 271 drm_of_component_match_add(dev, match, compare_of, remote); 272 of_node_put(remote); 273 } 274 } 275 276 static const struct component_master_ops armada_master_ops = { 277 .bind = armada_drm_bind, 278 .unbind = armada_drm_unbind, 279 }; 280 281 static int armada_drm_probe(struct platform_device *pdev) 282 { 283 struct component_match *match = NULL; 284 struct device *dev = &pdev->dev; 285 int ret; 286 287 ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops); 288 if (ret != -EINVAL) 289 return ret; 290 291 if (dev->platform_data) { 292 char **devices = dev->platform_data; 293 struct device_node *port; 294 struct device *d; 295 int i; 296 297 for (i = 0; devices[i]; i++) 298 component_match_add(dev, &match, compare_dev_name, 299 devices[i]); 300 301 if (i == 0) { 302 dev_err(dev, "missing 'ports' property\n"); 303 return -ENODEV; 304 } 305 306 for (i = 0; devices[i]; i++) { 307 d = bus_find_device_by_name(&platform_bus_type, NULL, 308 devices[i]); 309 if (d && d->of_node) { 310 for_each_child_of_node(d->of_node, port) 311 armada_add_endpoints(dev, &match, port); 312 } 313 put_device(d); 314 } 315 } 316 317 return component_master_add_with_match(&pdev->dev, &armada_master_ops, 318 match); 319 } 320 321 static int armada_drm_remove(struct platform_device *pdev) 322 { 323 component_master_del(&pdev->dev, &armada_master_ops); 324 return 0; 325 } 326 327 static const struct platform_device_id armada_drm_platform_ids[] = { 328 { 329 .name = "armada-drm", 330 }, { 331 .name = "armada-510-drm", 332 }, 333 { }, 334 }; 335 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids); 336 337 static struct platform_driver armada_drm_platform_driver = { 338 .probe = armada_drm_probe, 339 .remove = armada_drm_remove, 340 .driver = { 341 .name = "armada-drm", 342 }, 343 .id_table = armada_drm_platform_ids, 344 }; 345 346 static int __init armada_drm_init(void) 347 { 348 int ret; 349 350 armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls); 351 352 ret = platform_driver_register(&armada_lcd_platform_driver); 353 if (ret) 354 return ret; 355 ret = platform_driver_register(&armada_drm_platform_driver); 356 if (ret) 357 platform_driver_unregister(&armada_lcd_platform_driver); 358 return ret; 359 } 360 module_init(armada_drm_init); 361 362 static void __exit armada_drm_exit(void) 363 { 364 platform_driver_unregister(&armada_drm_platform_driver); 365 platform_driver_unregister(&armada_lcd_platform_driver); 366 } 367 module_exit(armada_drm_exit); 368 369 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>"); 370 MODULE_DESCRIPTION("Armada DRM Driver"); 371 MODULE_LICENSE("GPL"); 372 MODULE_ALIAS("platform:armada-drm"); 373