1 /* 2 * Copyright (C) STMicroelectronics SA 2014 3 * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics. 4 * License terms: GNU General Public License (GPL), version 2 5 */ 6 7 #include <drm/drmP.h> 8 9 #include <linux/component.h> 10 #include <linux/debugfs.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/of_platform.h> 14 15 #include <drm/drm_atomic.h> 16 #include <drm/drm_atomic_helper.h> 17 #include <drm/drm_crtc_helper.h> 18 #include <drm/drm_gem_cma_helper.h> 19 #include <drm/drm_fb_cma_helper.h> 20 #include <drm/drm_of.h> 21 22 #include "sti_crtc.h" 23 #include "sti_drv.h" 24 #include "sti_plane.h" 25 26 #define DRIVER_NAME "sti" 27 #define DRIVER_DESC "STMicroelectronics SoC DRM" 28 #define DRIVER_DATE "20140601" 29 #define DRIVER_MAJOR 1 30 #define DRIVER_MINOR 0 31 32 #define STI_MAX_FB_HEIGHT 4096 33 #define STI_MAX_FB_WIDTH 4096 34 35 static int sti_drm_fps_get(void *data, u64 *val) 36 { 37 struct drm_device *drm_dev = data; 38 struct drm_plane *p; 39 unsigned int i = 0; 40 41 *val = 0; 42 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) { 43 struct sti_plane *plane = to_sti_plane(p); 44 45 *val |= plane->fps_info.output << i; 46 i++; 47 } 48 49 return 0; 50 } 51 52 static int sti_drm_fps_set(void *data, u64 val) 53 { 54 struct drm_device *drm_dev = data; 55 struct drm_plane *p; 56 unsigned int i = 0; 57 58 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) { 59 struct sti_plane *plane = to_sti_plane(p); 60 61 plane->fps_info.output = (val >> i) & 1; 62 i++; 63 } 64 65 return 0; 66 } 67 68 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops, 69 sti_drm_fps_get, sti_drm_fps_set, "%llu\n"); 70 71 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data) 72 { 73 struct drm_info_node *node = s->private; 74 struct drm_device *dev = node->minor->dev; 75 struct drm_plane *p; 76 77 list_for_each_entry(p, &dev->mode_config.plane_list, head) { 78 struct sti_plane *plane = to_sti_plane(p); 79 80 seq_printf(s, "%s%s\n", 81 plane->fps_info.fps_str, 82 plane->fps_info.fips_str); 83 } 84 85 return 0; 86 } 87 88 static struct drm_info_list sti_drm_dbg_list[] = { 89 {"fps_get", sti_drm_fps_dbg_show, 0}, 90 }; 91 92 static int sti_drm_debugfs_create(struct dentry *root, 93 struct drm_minor *minor, 94 const char *name, 95 const struct file_operations *fops) 96 { 97 struct drm_device *dev = minor->dev; 98 struct drm_info_node *node; 99 struct dentry *ent; 100 101 ent = debugfs_create_file(name, S_IRUGO | S_IWUSR, root, dev, fops); 102 if (IS_ERR(ent)) 103 return PTR_ERR(ent); 104 105 node = kmalloc(sizeof(*node), GFP_KERNEL); 106 if (!node) { 107 debugfs_remove(ent); 108 return -ENOMEM; 109 } 110 111 node->minor = minor; 112 node->dent = ent; 113 node->info_ent = (void *)fops; 114 115 mutex_lock(&minor->debugfs_lock); 116 list_add(&node->list, &minor->debugfs_list); 117 mutex_unlock(&minor->debugfs_lock); 118 119 return 0; 120 } 121 122 static int sti_drm_dbg_init(struct drm_minor *minor) 123 { 124 int ret; 125 126 ret = drm_debugfs_create_files(sti_drm_dbg_list, 127 ARRAY_SIZE(sti_drm_dbg_list), 128 minor->debugfs_root, minor); 129 if (ret) 130 goto err; 131 132 ret = sti_drm_debugfs_create(minor->debugfs_root, minor, "fps_show", 133 &sti_drm_fps_fops); 134 if (ret) 135 goto err; 136 137 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME); 138 return 0; 139 err: 140 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME); 141 return ret; 142 } 143 144 static void sti_drm_dbg_cleanup(struct drm_minor *minor) 145 { 146 drm_debugfs_remove_files(sti_drm_dbg_list, 147 ARRAY_SIZE(sti_drm_dbg_list), minor); 148 149 drm_debugfs_remove_files((struct drm_info_list *)&sti_drm_fps_fops, 150 1, minor); 151 } 152 153 static void sti_atomic_schedule(struct sti_private *private, 154 struct drm_atomic_state *state) 155 { 156 private->commit.state = state; 157 schedule_work(&private->commit.work); 158 } 159 160 static void sti_atomic_complete(struct sti_private *private, 161 struct drm_atomic_state *state) 162 { 163 struct drm_device *drm = private->drm_dev; 164 165 /* 166 * Everything below can be run asynchronously without the need to grab 167 * any modeset locks at all under one condition: It must be guaranteed 168 * that the asynchronous work has either been cancelled (if the driver 169 * supports it, which at least requires that the framebuffers get 170 * cleaned up with drm_atomic_helper_cleanup_planes()) or completed 171 * before the new state gets committed on the software side with 172 * drm_atomic_helper_swap_state(). 173 * 174 * This scheme allows new atomic state updates to be prepared and 175 * checked in parallel to the asynchronous completion of the previous 176 * update. Which is important since compositors need to figure out the 177 * composition of the next frame right after having submitted the 178 * current layout. 179 */ 180 181 drm_atomic_helper_commit_modeset_disables(drm, state); 182 drm_atomic_helper_commit_planes(drm, state, 0); 183 drm_atomic_helper_commit_modeset_enables(drm, state); 184 185 drm_atomic_helper_wait_for_vblanks(drm, state); 186 187 drm_atomic_helper_cleanup_planes(drm, state); 188 drm_atomic_state_put(state); 189 } 190 191 static void sti_atomic_work(struct work_struct *work) 192 { 193 struct sti_private *private = container_of(work, 194 struct sti_private, commit.work); 195 196 sti_atomic_complete(private, private->commit.state); 197 } 198 199 static int sti_atomic_check(struct drm_device *dev, 200 struct drm_atomic_state *state) 201 { 202 int ret; 203 204 ret = drm_atomic_helper_check_modeset(dev, state); 205 if (ret) 206 return ret; 207 208 ret = drm_atomic_normalize_zpos(dev, state); 209 if (ret) 210 return ret; 211 212 ret = drm_atomic_helper_check_planes(dev, state); 213 if (ret) 214 return ret; 215 216 return ret; 217 } 218 219 static int sti_atomic_commit(struct drm_device *drm, 220 struct drm_atomic_state *state, bool nonblock) 221 { 222 struct sti_private *private = drm->dev_private; 223 int err; 224 225 err = drm_atomic_helper_prepare_planes(drm, state); 226 if (err) 227 return err; 228 229 /* serialize outstanding nonblocking commits */ 230 mutex_lock(&private->commit.lock); 231 flush_work(&private->commit.work); 232 233 /* 234 * This is the point of no return - everything below never fails except 235 * when the hw goes bonghits. Which means we can commit the new state on 236 * the software side now. 237 */ 238 239 drm_atomic_helper_swap_state(state, true); 240 241 drm_atomic_state_get(state); 242 if (nonblock) 243 sti_atomic_schedule(private, state); 244 else 245 sti_atomic_complete(private, state); 246 247 mutex_unlock(&private->commit.lock); 248 return 0; 249 } 250 251 static void sti_output_poll_changed(struct drm_device *ddev) 252 { 253 struct sti_private *private = ddev->dev_private; 254 255 if (!ddev->mode_config.num_connector) 256 return; 257 258 if (private->fbdev) { 259 drm_fbdev_cma_hotplug_event(private->fbdev); 260 return; 261 } 262 263 private->fbdev = drm_fbdev_cma_init(ddev, 32, 264 ddev->mode_config.num_crtc, 265 ddev->mode_config.num_connector); 266 if (IS_ERR(private->fbdev)) 267 private->fbdev = NULL; 268 } 269 270 static const struct drm_mode_config_funcs sti_mode_config_funcs = { 271 .fb_create = drm_fb_cma_create, 272 .output_poll_changed = sti_output_poll_changed, 273 .atomic_check = sti_atomic_check, 274 .atomic_commit = sti_atomic_commit, 275 }; 276 277 static void sti_mode_config_init(struct drm_device *dev) 278 { 279 dev->mode_config.min_width = 0; 280 dev->mode_config.min_height = 0; 281 282 /* 283 * set max width and height as default value. 284 * this value would be used to check framebuffer size limitation 285 * at drm_mode_addfb(). 286 */ 287 dev->mode_config.max_width = STI_MAX_FB_WIDTH; 288 dev->mode_config.max_height = STI_MAX_FB_HEIGHT; 289 290 dev->mode_config.funcs = &sti_mode_config_funcs; 291 } 292 293 static const struct file_operations sti_driver_fops = { 294 .owner = THIS_MODULE, 295 .open = drm_open, 296 .mmap = drm_gem_cma_mmap, 297 .poll = drm_poll, 298 .read = drm_read, 299 .unlocked_ioctl = drm_ioctl, 300 #ifdef CONFIG_COMPAT 301 .compat_ioctl = drm_compat_ioctl, 302 #endif 303 .release = drm_release, 304 }; 305 306 static struct drm_driver sti_driver = { 307 .driver_features = DRIVER_MODESET | 308 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC, 309 .gem_free_object_unlocked = drm_gem_cma_free_object, 310 .gem_vm_ops = &drm_gem_cma_vm_ops, 311 .dumb_create = drm_gem_cma_dumb_create, 312 .dumb_map_offset = drm_gem_cma_dumb_map_offset, 313 .dumb_destroy = drm_gem_dumb_destroy, 314 .fops = &sti_driver_fops, 315 316 .get_vblank_counter = drm_vblank_no_hw_counter, 317 .enable_vblank = sti_crtc_enable_vblank, 318 .disable_vblank = sti_crtc_disable_vblank, 319 320 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 321 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 322 .gem_prime_export = drm_gem_prime_export, 323 .gem_prime_import = drm_gem_prime_import, 324 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 325 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 326 .gem_prime_vmap = drm_gem_cma_prime_vmap, 327 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 328 .gem_prime_mmap = drm_gem_cma_prime_mmap, 329 330 .debugfs_init = sti_drm_dbg_init, 331 .debugfs_cleanup = sti_drm_dbg_cleanup, 332 333 .name = DRIVER_NAME, 334 .desc = DRIVER_DESC, 335 .date = DRIVER_DATE, 336 .major = DRIVER_MAJOR, 337 .minor = DRIVER_MINOR, 338 }; 339 340 static int compare_of(struct device *dev, void *data) 341 { 342 return dev->of_node == data; 343 } 344 345 static int sti_init(struct drm_device *ddev) 346 { 347 struct sti_private *private; 348 349 private = kzalloc(sizeof(*private), GFP_KERNEL); 350 if (!private) 351 return -ENOMEM; 352 353 ddev->dev_private = (void *)private; 354 dev_set_drvdata(ddev->dev, ddev); 355 private->drm_dev = ddev; 356 357 mutex_init(&private->commit.lock); 358 INIT_WORK(&private->commit.work, sti_atomic_work); 359 360 drm_mode_config_init(ddev); 361 362 sti_mode_config_init(ddev); 363 364 drm_kms_helper_poll_init(ddev); 365 366 return 0; 367 } 368 369 static void sti_cleanup(struct drm_device *ddev) 370 { 371 struct sti_private *private = ddev->dev_private; 372 373 if (private->fbdev) { 374 drm_fbdev_cma_fini(private->fbdev); 375 private->fbdev = NULL; 376 } 377 378 drm_kms_helper_poll_fini(ddev); 379 drm_vblank_cleanup(ddev); 380 kfree(private); 381 ddev->dev_private = NULL; 382 } 383 384 static int sti_bind(struct device *dev) 385 { 386 struct drm_device *ddev; 387 int ret; 388 389 ddev = drm_dev_alloc(&sti_driver, dev); 390 if (IS_ERR(ddev)) 391 return PTR_ERR(ddev); 392 393 ddev->platformdev = to_platform_device(dev); 394 395 ret = sti_init(ddev); 396 if (ret) 397 goto err_drm_dev_unref; 398 399 ret = component_bind_all(ddev->dev, ddev); 400 if (ret) 401 goto err_cleanup; 402 403 ret = drm_dev_register(ddev, 0); 404 if (ret) 405 goto err_register; 406 407 drm_mode_config_reset(ddev); 408 409 return 0; 410 411 err_register: 412 drm_mode_config_cleanup(ddev); 413 err_cleanup: 414 sti_cleanup(ddev); 415 err_drm_dev_unref: 416 drm_dev_unref(ddev); 417 return ret; 418 } 419 420 static void sti_unbind(struct device *dev) 421 { 422 struct drm_device *ddev = dev_get_drvdata(dev); 423 424 drm_dev_unregister(ddev); 425 sti_cleanup(ddev); 426 drm_dev_unref(ddev); 427 } 428 429 static const struct component_master_ops sti_ops = { 430 .bind = sti_bind, 431 .unbind = sti_unbind, 432 }; 433 434 static int sti_platform_probe(struct platform_device *pdev) 435 { 436 struct device *dev = &pdev->dev; 437 struct device_node *node = dev->of_node; 438 struct device_node *child_np; 439 struct component_match *match = NULL; 440 441 dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 442 443 of_platform_populate(node, NULL, NULL, dev); 444 445 child_np = of_get_next_available_child(node, NULL); 446 447 while (child_np) { 448 drm_of_component_match_add(dev, &match, compare_of, 449 child_np); 450 child_np = of_get_next_available_child(node, child_np); 451 } 452 453 return component_master_add_with_match(dev, &sti_ops, match); 454 } 455 456 static int sti_platform_remove(struct platform_device *pdev) 457 { 458 component_master_del(&pdev->dev, &sti_ops); 459 of_platform_depopulate(&pdev->dev); 460 461 return 0; 462 } 463 464 static const struct of_device_id sti_dt_ids[] = { 465 { .compatible = "st,sti-display-subsystem", }, 466 { /* end node */ }, 467 }; 468 MODULE_DEVICE_TABLE(of, sti_dt_ids); 469 470 static struct platform_driver sti_platform_driver = { 471 .probe = sti_platform_probe, 472 .remove = sti_platform_remove, 473 .driver = { 474 .name = DRIVER_NAME, 475 .of_match_table = sti_dt_ids, 476 }, 477 }; 478 479 static struct platform_driver * const drivers[] = { 480 &sti_tvout_driver, 481 &sti_vtac_driver, 482 &sti_hqvdp_driver, 483 &sti_hdmi_driver, 484 &sti_hda_driver, 485 &sti_dvo_driver, 486 &sti_vtg_driver, 487 &sti_compositor_driver, 488 &sti_platform_driver, 489 }; 490 491 static int sti_drm_init(void) 492 { 493 return platform_register_drivers(drivers, ARRAY_SIZE(drivers)); 494 } 495 module_init(sti_drm_init); 496 497 static void sti_drm_exit(void) 498 { 499 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers)); 500 } 501 module_exit(sti_drm_exit); 502 503 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>"); 504 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver"); 505 MODULE_LICENSE("GPL"); 506