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 memset(&plane->fps_info, 0, sizeof(plane->fps_info)); 62 plane->fps_info.output = (val >> i) & 1; 63 64 i++; 65 } 66 67 return 0; 68 } 69 70 DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops, 71 sti_drm_fps_get, sti_drm_fps_set, "%llu\n"); 72 73 static int sti_drm_fps_dbg_show(struct seq_file *s, void *data) 74 { 75 struct drm_info_node *node = s->private; 76 struct drm_device *dev = node->minor->dev; 77 struct drm_plane *p; 78 79 list_for_each_entry(p, &dev->mode_config.plane_list, head) { 80 struct sti_plane *plane = to_sti_plane(p); 81 82 seq_printf(s, "%s%s\n", 83 plane->fps_info.fps_str, 84 plane->fps_info.fips_str); 85 } 86 87 return 0; 88 } 89 90 static struct drm_info_list sti_drm_dbg_list[] = { 91 {"fps_get", sti_drm_fps_dbg_show, 0}, 92 }; 93 94 static int sti_drm_dbg_init(struct drm_minor *minor) 95 { 96 struct dentry *dentry; 97 int ret; 98 99 ret = drm_debugfs_create_files(sti_drm_dbg_list, 100 ARRAY_SIZE(sti_drm_dbg_list), 101 minor->debugfs_root, minor); 102 if (ret) 103 goto err; 104 105 dentry = debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, 106 minor->debugfs_root, minor->dev, 107 &sti_drm_fps_fops); 108 if (!dentry) { 109 ret = -ENOMEM; 110 goto err; 111 } 112 113 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME); 114 return 0; 115 err: 116 DRM_ERROR("%s: cannot install debugfs\n", DRIVER_NAME); 117 return ret; 118 } 119 120 static int sti_atomic_check(struct drm_device *dev, 121 struct drm_atomic_state *state) 122 { 123 int ret; 124 125 ret = drm_atomic_helper_check_modeset(dev, state); 126 if (ret) 127 return ret; 128 129 ret = drm_atomic_normalize_zpos(dev, state); 130 if (ret) 131 return ret; 132 133 ret = drm_atomic_helper_check_planes(dev, state); 134 if (ret) 135 return ret; 136 137 return ret; 138 } 139 140 static void sti_output_poll_changed(struct drm_device *ddev) 141 { 142 struct sti_private *private = ddev->dev_private; 143 144 drm_fbdev_cma_hotplug_event(private->fbdev); 145 } 146 147 static const struct drm_mode_config_funcs sti_mode_config_funcs = { 148 .fb_create = drm_fb_cma_create, 149 .output_poll_changed = sti_output_poll_changed, 150 .atomic_check = sti_atomic_check, 151 .atomic_commit = drm_atomic_helper_commit, 152 }; 153 154 static void sti_mode_config_init(struct drm_device *dev) 155 { 156 dev->mode_config.min_width = 0; 157 dev->mode_config.min_height = 0; 158 159 /* 160 * set max width and height as default value. 161 * this value would be used to check framebuffer size limitation 162 * at drm_mode_addfb(). 163 */ 164 dev->mode_config.max_width = STI_MAX_FB_WIDTH; 165 dev->mode_config.max_height = STI_MAX_FB_HEIGHT; 166 167 dev->mode_config.funcs = &sti_mode_config_funcs; 168 } 169 170 static const struct file_operations sti_driver_fops = { 171 .owner = THIS_MODULE, 172 .open = drm_open, 173 .mmap = drm_gem_cma_mmap, 174 .poll = drm_poll, 175 .read = drm_read, 176 .unlocked_ioctl = drm_ioctl, 177 .compat_ioctl = drm_compat_ioctl, 178 .release = drm_release, 179 }; 180 181 static struct drm_driver sti_driver = { 182 .driver_features = DRIVER_MODESET | 183 DRIVER_GEM | DRIVER_PRIME | DRIVER_ATOMIC, 184 .gem_free_object_unlocked = drm_gem_cma_free_object, 185 .gem_vm_ops = &drm_gem_cma_vm_ops, 186 .dumb_create = drm_gem_cma_dumb_create, 187 .dumb_map_offset = drm_gem_cma_dumb_map_offset, 188 .dumb_destroy = drm_gem_dumb_destroy, 189 .fops = &sti_driver_fops, 190 191 .get_vblank_counter = drm_vblank_no_hw_counter, 192 .enable_vblank = sti_crtc_enable_vblank, 193 .disable_vblank = sti_crtc_disable_vblank, 194 195 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 196 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 197 .gem_prime_export = drm_gem_prime_export, 198 .gem_prime_import = drm_gem_prime_import, 199 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 200 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 201 .gem_prime_vmap = drm_gem_cma_prime_vmap, 202 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 203 .gem_prime_mmap = drm_gem_cma_prime_mmap, 204 205 .debugfs_init = sti_drm_dbg_init, 206 207 .name = DRIVER_NAME, 208 .desc = DRIVER_DESC, 209 .date = DRIVER_DATE, 210 .major = DRIVER_MAJOR, 211 .minor = DRIVER_MINOR, 212 }; 213 214 static int compare_of(struct device *dev, void *data) 215 { 216 return dev->of_node == data; 217 } 218 219 static int sti_init(struct drm_device *ddev) 220 { 221 struct sti_private *private; 222 223 private = kzalloc(sizeof(*private), GFP_KERNEL); 224 if (!private) 225 return -ENOMEM; 226 227 ddev->dev_private = (void *)private; 228 dev_set_drvdata(ddev->dev, ddev); 229 private->drm_dev = ddev; 230 231 drm_mode_config_init(ddev); 232 233 sti_mode_config_init(ddev); 234 235 drm_kms_helper_poll_init(ddev); 236 237 return 0; 238 } 239 240 static void sti_cleanup(struct drm_device *ddev) 241 { 242 struct sti_private *private = ddev->dev_private; 243 244 if (private->fbdev) { 245 drm_fbdev_cma_fini(private->fbdev); 246 private->fbdev = NULL; 247 } 248 249 drm_kms_helper_poll_fini(ddev); 250 drm_vblank_cleanup(ddev); 251 component_unbind_all(ddev->dev, ddev); 252 kfree(private); 253 ddev->dev_private = NULL; 254 } 255 256 static int sti_bind(struct device *dev) 257 { 258 struct drm_device *ddev; 259 struct sti_private *private; 260 struct drm_fbdev_cma *fbdev; 261 int ret; 262 263 ddev = drm_dev_alloc(&sti_driver, dev); 264 if (IS_ERR(ddev)) 265 return PTR_ERR(ddev); 266 267 ddev->platformdev = to_platform_device(dev); 268 269 ret = sti_init(ddev); 270 if (ret) 271 goto err_drm_dev_unref; 272 273 ret = component_bind_all(ddev->dev, ddev); 274 if (ret) 275 goto err_cleanup; 276 277 ret = drm_dev_register(ddev, 0); 278 if (ret) 279 goto err_register; 280 281 drm_mode_config_reset(ddev); 282 283 private = ddev->dev_private; 284 if (ddev->mode_config.num_connector) { 285 fbdev = drm_fbdev_cma_init(ddev, 32, 286 ddev->mode_config.num_connector); 287 if (IS_ERR(fbdev)) { 288 DRM_DEBUG_DRIVER("Warning: fails to create fbdev\n"); 289 fbdev = NULL; 290 } 291 private->fbdev = fbdev; 292 } 293 294 return 0; 295 296 err_register: 297 drm_mode_config_cleanup(ddev); 298 err_cleanup: 299 sti_cleanup(ddev); 300 err_drm_dev_unref: 301 drm_dev_unref(ddev); 302 return ret; 303 } 304 305 static void sti_unbind(struct device *dev) 306 { 307 struct drm_device *ddev = dev_get_drvdata(dev); 308 309 drm_dev_unregister(ddev); 310 sti_cleanup(ddev); 311 drm_dev_unref(ddev); 312 } 313 314 static const struct component_master_ops sti_ops = { 315 .bind = sti_bind, 316 .unbind = sti_unbind, 317 }; 318 319 static int sti_platform_probe(struct platform_device *pdev) 320 { 321 struct device *dev = &pdev->dev; 322 struct device_node *node = dev->of_node; 323 struct device_node *child_np; 324 struct component_match *match = NULL; 325 326 dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 327 328 of_platform_populate(node, NULL, NULL, dev); 329 330 child_np = of_get_next_available_child(node, NULL); 331 332 while (child_np) { 333 drm_of_component_match_add(dev, &match, compare_of, 334 child_np); 335 child_np = of_get_next_available_child(node, child_np); 336 } 337 338 return component_master_add_with_match(dev, &sti_ops, match); 339 } 340 341 static int sti_platform_remove(struct platform_device *pdev) 342 { 343 component_master_del(&pdev->dev, &sti_ops); 344 of_platform_depopulate(&pdev->dev); 345 346 return 0; 347 } 348 349 static const struct of_device_id sti_dt_ids[] = { 350 { .compatible = "st,sti-display-subsystem", }, 351 { /* end node */ }, 352 }; 353 MODULE_DEVICE_TABLE(of, sti_dt_ids); 354 355 static struct platform_driver sti_platform_driver = { 356 .probe = sti_platform_probe, 357 .remove = sti_platform_remove, 358 .driver = { 359 .name = DRIVER_NAME, 360 .of_match_table = sti_dt_ids, 361 }, 362 }; 363 364 static struct platform_driver * const drivers[] = { 365 &sti_tvout_driver, 366 &sti_hqvdp_driver, 367 &sti_hdmi_driver, 368 &sti_hda_driver, 369 &sti_dvo_driver, 370 &sti_vtg_driver, 371 &sti_compositor_driver, 372 &sti_platform_driver, 373 }; 374 375 static int sti_drm_init(void) 376 { 377 return platform_register_drivers(drivers, ARRAY_SIZE(drivers)); 378 } 379 module_init(sti_drm_init); 380 381 static void sti_drm_exit(void) 382 { 383 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers)); 384 } 385 module_exit(sti_drm_exit); 386 387 MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>"); 388 MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver"); 389 MODULE_LICENSE("GPL"); 390