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 "armada_crtc.h" 15#include "armada_drm.h" 16#include "armada_gem.h" 17#include "armada_hw.h" 18#include <drm/armada_drm.h> 19#include "armada_ioctlP.h" 20 21#ifdef CONFIG_DRM_ARMADA_TDA1998X 22#include <drm/i2c/tda998x.h> 23#include "armada_slave.h" 24 25static struct tda998x_encoder_params params = { 26 /* With 0x24, there is no translation between vp_out and int_vp 27 FB LCD out Pins VIP Int Vp 28 R:23:16 R:7:0 VPC7:0 7:0 7:0[R] 29 G:15:8 G:15:8 VPB7:0 23:16 23:16[G] 30 B:7:0 B:23:16 VPA7:0 15:8 15:8[B] 31 */ 32 .swap_a = 2, 33 .swap_b = 3, 34 .swap_c = 4, 35 .swap_d = 5, 36 .swap_e = 0, 37 .swap_f = 1, 38 .audio_cfg = BIT(2), 39 .audio_frame[1] = 1, 40 .audio_format = AFMT_SPDIF, 41 .audio_sample_rate = 44100, 42}; 43 44static const struct armada_drm_slave_config tda19988_config = { 45 .i2c_adapter_id = 0, 46 .crtcs = 1 << 0, /* Only LCD0 at the moment */ 47 .polled = DRM_CONNECTOR_POLL_CONNECT | DRM_CONNECTOR_POLL_DISCONNECT, 48 .interlace_allowed = true, 49 .info = { 50 .type = "tda998x", 51 .addr = 0x70, 52 .platform_data = ¶ms, 53 }, 54}; 55#endif 56 57static bool is_componentized(struct device *dev) 58{ 59 return dev->of_node || dev->platform_data; 60} 61 62static void armada_drm_unref_work(struct work_struct *work) 63{ 64 struct armada_private *priv = 65 container_of(work, struct armada_private, fb_unref_work); 66 struct drm_framebuffer *fb; 67 68 while (kfifo_get(&priv->fb_unref, &fb)) 69 drm_framebuffer_unreference(fb); 70} 71 72/* Must be called with dev->event_lock held */ 73void __armada_drm_queue_unref_work(struct drm_device *dev, 74 struct drm_framebuffer *fb) 75{ 76 struct armada_private *priv = dev->dev_private; 77 78 WARN_ON(!kfifo_put(&priv->fb_unref, fb)); 79 schedule_work(&priv->fb_unref_work); 80} 81 82void armada_drm_queue_unref_work(struct drm_device *dev, 83 struct drm_framebuffer *fb) 84{ 85 unsigned long flags; 86 87 spin_lock_irqsave(&dev->event_lock, flags); 88 __armada_drm_queue_unref_work(dev, fb); 89 spin_unlock_irqrestore(&dev->event_lock, flags); 90} 91 92static int armada_drm_load(struct drm_device *dev, unsigned long flags) 93{ 94 const struct platform_device_id *id; 95 const struct armada_variant *variant; 96 struct armada_private *priv; 97 struct resource *res[ARRAY_SIZE(priv->dcrtc)]; 98 struct resource *mem = NULL; 99 int ret, n, i; 100 101 memset(res, 0, sizeof(res)); 102 103 for (n = i = 0; ; n++) { 104 struct resource *r = platform_get_resource(dev->platformdev, 105 IORESOURCE_MEM, n); 106 if (!r) 107 break; 108 109 /* Resources above 64K are graphics memory */ 110 if (resource_size(r) > SZ_64K) 111 mem = r; 112 else if (i < ARRAY_SIZE(priv->dcrtc)) 113 res[i++] = r; 114 else 115 return -EINVAL; 116 } 117 118 if (!mem) 119 return -ENXIO; 120 121 if (!devm_request_mem_region(dev->dev, mem->start, 122 resource_size(mem), "armada-drm")) 123 return -EBUSY; 124 125 priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL); 126 if (!priv) { 127 DRM_ERROR("failed to allocate private\n"); 128 return -ENOMEM; 129 } 130 131 platform_set_drvdata(dev->platformdev, dev); 132 dev->dev_private = priv; 133 134 /* Get the implementation specific driver data. */ 135 id = platform_get_device_id(dev->platformdev); 136 if (!id) 137 return -ENXIO; 138 139 variant = (const struct armada_variant *)id->driver_data; 140 141 INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work); 142 INIT_KFIFO(priv->fb_unref); 143 144 /* Mode setting support */ 145 drm_mode_config_init(dev); 146 dev->mode_config.min_width = 320; 147 dev->mode_config.min_height = 200; 148 149 /* 150 * With vscale enabled, the maximum width is 1920 due to the 151 * 1920 by 3 lines RAM 152 */ 153 dev->mode_config.max_width = 1920; 154 dev->mode_config.max_height = 2048; 155 156 dev->mode_config.preferred_depth = 24; 157 dev->mode_config.funcs = &armada_drm_mode_config_funcs; 158 drm_mm_init(&priv->linear, mem->start, resource_size(mem)); 159 160 /* Create all LCD controllers */ 161 for (n = 0; n < ARRAY_SIZE(priv->dcrtc); n++) { 162 int irq; 163 164 if (!res[n]) 165 break; 166 167 irq = platform_get_irq(dev->platformdev, n); 168 if (irq < 0) 169 goto err_kms; 170 171 ret = armada_drm_crtc_create(dev, dev->dev, res[n], irq, 172 variant, NULL); 173 if (ret) 174 goto err_kms; 175 } 176 177 if (is_componentized(dev->dev)) { 178 ret = component_bind_all(dev->dev, dev); 179 if (ret) 180 goto err_kms; 181 } else { 182#ifdef CONFIG_DRM_ARMADA_TDA1998X 183 ret = armada_drm_connector_slave_create(dev, &tda19988_config); 184 if (ret) 185 goto err_kms; 186#endif 187 } 188 189 ret = drm_vblank_init(dev, dev->mode_config.num_crtc); 190 if (ret) 191 goto err_comp; 192 193 dev->irq_enabled = true; 194 dev->vblank_disable_allowed = 1; 195 196 ret = armada_fbdev_init(dev); 197 if (ret) 198 goto err_comp; 199 200 drm_kms_helper_poll_init(dev); 201 202 return 0; 203 204 err_comp: 205 if (is_componentized(dev->dev)) 206 component_unbind_all(dev->dev, dev); 207 err_kms: 208 drm_mode_config_cleanup(dev); 209 drm_mm_takedown(&priv->linear); 210 flush_work(&priv->fb_unref_work); 211 212 return ret; 213} 214 215static int armada_drm_unload(struct drm_device *dev) 216{ 217 struct armada_private *priv = dev->dev_private; 218 219 drm_kms_helper_poll_fini(dev); 220 armada_fbdev_fini(dev); 221 222 if (is_componentized(dev->dev)) 223 component_unbind_all(dev->dev, dev); 224 225 drm_mode_config_cleanup(dev); 226 drm_mm_takedown(&priv->linear); 227 flush_work(&priv->fb_unref_work); 228 dev->dev_private = NULL; 229 230 return 0; 231} 232 233void armada_drm_vbl_event_add(struct armada_crtc *dcrtc, 234 struct armada_vbl_event *evt) 235{ 236 unsigned long flags; 237 238 spin_lock_irqsave(&dcrtc->irq_lock, flags); 239 if (list_empty(&evt->node)) { 240 list_add_tail(&evt->node, &dcrtc->vbl_list); 241 242 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num); 243 } 244 spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 245} 246 247void armada_drm_vbl_event_remove(struct armada_crtc *dcrtc, 248 struct armada_vbl_event *evt) 249{ 250 if (!list_empty(&evt->node)) { 251 list_del_init(&evt->node); 252 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num); 253 } 254} 255 256void armada_drm_vbl_event_remove_unlocked(struct armada_crtc *dcrtc, 257 struct armada_vbl_event *evt) 258{ 259 unsigned long flags; 260 261 spin_lock_irqsave(&dcrtc->irq_lock, flags); 262 armada_drm_vbl_event_remove(dcrtc, evt); 263 spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 264} 265 266/* These are called under the vbl_lock. */ 267static int armada_drm_enable_vblank(struct drm_device *dev, int crtc) 268{ 269 struct armada_private *priv = dev->dev_private; 270 armada_drm_crtc_enable_irq(priv->dcrtc[crtc], VSYNC_IRQ_ENA); 271 return 0; 272} 273 274static void armada_drm_disable_vblank(struct drm_device *dev, int crtc) 275{ 276 struct armada_private *priv = dev->dev_private; 277 armada_drm_crtc_disable_irq(priv->dcrtc[crtc], VSYNC_IRQ_ENA); 278} 279 280static struct drm_ioctl_desc armada_ioctls[] = { 281 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl, 282 DRM_UNLOCKED), 283 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 284 DRM_UNLOCKED), 285 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 286 DRM_UNLOCKED), 287}; 288 289static void armada_drm_lastclose(struct drm_device *dev) 290{ 291 armada_fbdev_lastclose(dev); 292} 293 294static const struct file_operations armada_drm_fops = { 295 .owner = THIS_MODULE, 296 .llseek = no_llseek, 297 .read = drm_read, 298 .poll = drm_poll, 299 .unlocked_ioctl = drm_ioctl, 300 .mmap = drm_gem_mmap, 301 .open = drm_open, 302 .release = drm_release, 303}; 304 305static struct drm_driver armada_drm_driver = { 306 .load = armada_drm_load, 307 .open = NULL, 308 .preclose = NULL, 309 .postclose = NULL, 310 .lastclose = armada_drm_lastclose, 311 .unload = armada_drm_unload, 312 .set_busid = drm_platform_set_busid, 313 .get_vblank_counter = drm_vblank_count, 314 .enable_vblank = armada_drm_enable_vblank, 315 .disable_vblank = armada_drm_disable_vblank, 316#ifdef CONFIG_DEBUG_FS 317 .debugfs_init = armada_drm_debugfs_init, 318 .debugfs_cleanup = armada_drm_debugfs_cleanup, 319#endif 320 .gem_free_object = armada_gem_free_object, 321 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 322 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 323 .gem_prime_export = armada_gem_prime_export, 324 .gem_prime_import = armada_gem_prime_import, 325 .dumb_create = armada_gem_dumb_create, 326 .dumb_map_offset = armada_gem_dumb_map_offset, 327 .dumb_destroy = armada_gem_dumb_destroy, 328 .gem_vm_ops = &armada_gem_vm_ops, 329 .major = 1, 330 .minor = 0, 331 .name = "armada-drm", 332 .desc = "Armada SoC DRM", 333 .date = "20120730", 334 .driver_features = DRIVER_GEM | DRIVER_MODESET | 335 DRIVER_HAVE_IRQ | DRIVER_PRIME, 336 .ioctls = armada_ioctls, 337 .fops = &armada_drm_fops, 338}; 339 340static int armada_drm_bind(struct device *dev) 341{ 342 return drm_platform_init(&armada_drm_driver, to_platform_device(dev)); 343} 344 345static void armada_drm_unbind(struct device *dev) 346{ 347 drm_put_dev(dev_get_drvdata(dev)); 348} 349 350static int compare_of(struct device *dev, void *data) 351{ 352 return dev->of_node == data; 353} 354 355static int compare_dev_name(struct device *dev, void *data) 356{ 357 const char *name = data; 358 return !strcmp(dev_name(dev), name); 359} 360 361static void armada_add_endpoints(struct device *dev, 362 struct component_match **match, struct device_node *port) 363{ 364 struct device_node *ep, *remote; 365 366 for_each_child_of_node(port, ep) { 367 remote = of_graph_get_remote_port_parent(ep); 368 if (!remote || !of_device_is_available(remote)) { 369 of_node_put(remote); 370 continue; 371 } else if (!of_device_is_available(remote->parent)) { 372 dev_warn(dev, "parent device of %s is not available\n", 373 remote->full_name); 374 of_node_put(remote); 375 continue; 376 } 377 378 component_match_add(dev, match, compare_of, remote); 379 of_node_put(remote); 380 } 381} 382 383static int armada_drm_find_components(struct device *dev, 384 struct component_match **match) 385{ 386 struct device_node *port; 387 int i; 388 389 if (dev->of_node) { 390 struct device_node *np = dev->of_node; 391 392 for (i = 0; ; i++) { 393 port = of_parse_phandle(np, "ports", i); 394 if (!port) 395 break; 396 397 component_match_add(dev, match, compare_of, port); 398 of_node_put(port); 399 } 400 401 if (i == 0) { 402 dev_err(dev, "missing 'ports' property\n"); 403 return -ENODEV; 404 } 405 406 for (i = 0; ; i++) { 407 port = of_parse_phandle(np, "ports", i); 408 if (!port) 409 break; 410 411 armada_add_endpoints(dev, match, port); 412 of_node_put(port); 413 } 414 } else if (dev->platform_data) { 415 char **devices = dev->platform_data; 416 struct device *d; 417 418 for (i = 0; devices[i]; i++) 419 component_match_add(dev, match, compare_dev_name, 420 devices[i]); 421 422 if (i == 0) { 423 dev_err(dev, "missing 'ports' property\n"); 424 return -ENODEV; 425 } 426 427 for (i = 0; devices[i]; i++) { 428 d = bus_find_device_by_name(&platform_bus_type, NULL, 429 devices[i]); 430 if (d && d->of_node) { 431 for_each_child_of_node(d->of_node, port) 432 armada_add_endpoints(dev, match, port); 433 } 434 put_device(d); 435 } 436 } 437 438 return 0; 439} 440 441static const struct component_master_ops armada_master_ops = { 442 .bind = armada_drm_bind, 443 .unbind = armada_drm_unbind, 444}; 445 446static int armada_drm_probe(struct platform_device *pdev) 447{ 448 if (is_componentized(&pdev->dev)) { 449 struct component_match *match = NULL; 450 int ret; 451 452 ret = armada_drm_find_components(&pdev->dev, &match); 453 if (ret < 0) 454 return ret; 455 456 return component_master_add_with_match(&pdev->dev, 457 &armada_master_ops, match); 458 } else { 459 return drm_platform_init(&armada_drm_driver, pdev); 460 } 461} 462 463static int armada_drm_remove(struct platform_device *pdev) 464{ 465 if (is_componentized(&pdev->dev)) 466 component_master_del(&pdev->dev, &armada_master_ops); 467 else 468 drm_put_dev(platform_get_drvdata(pdev)); 469 return 0; 470} 471 472static const struct platform_device_id armada_drm_platform_ids[] = { 473 { 474 .name = "armada-drm", 475 .driver_data = (unsigned long)&armada510_ops, 476 }, { 477 .name = "armada-510-drm", 478 .driver_data = (unsigned long)&armada510_ops, 479 }, 480 { }, 481}; 482MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids); 483 484static struct platform_driver armada_drm_platform_driver = { 485 .probe = armada_drm_probe, 486 .remove = armada_drm_remove, 487 .driver = { 488 .name = "armada-drm", 489 .owner = THIS_MODULE, 490 }, 491 .id_table = armada_drm_platform_ids, 492}; 493 494static int __init armada_drm_init(void) 495{ 496 int ret; 497 498 armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls); 499 500 ret = platform_driver_register(&armada_lcd_platform_driver); 501 if (ret) 502 return ret; 503 ret = platform_driver_register(&armada_drm_platform_driver); 504 if (ret) 505 platform_driver_unregister(&armada_lcd_platform_driver); 506 return ret; 507} 508module_init(armada_drm_init); 509 510static void __exit armada_drm_exit(void) 511{ 512 platform_driver_unregister(&armada_drm_platform_driver); 513 platform_driver_unregister(&armada_lcd_platform_driver); 514} 515module_exit(armada_drm_exit); 516 517MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>"); 518MODULE_DESCRIPTION("Armada DRM Driver"); 519MODULE_LICENSE("GPL"); 520MODULE_ALIAS("platform:armada-drm"); 521