1 /* 2 * Copyright (C) STMicroelectronics SA 2017 3 * 4 * Authors: Philippe Cornu <philippe.cornu@st.com> 5 * Yannick Fertre <yannick.fertre@st.com> 6 * Fabien Dessenne <fabien.dessenne@st.com> 7 * Mickael Reulier <mickael.reulier@st.com> 8 * 9 * License terms: GNU General Public License (GPL), version 2 10 */ 11 12 #include <linux/component.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_fb_cma_helper.h> 19 #include <drm/drm_gem_cma_helper.h> 20 #include <drm/drm_gem_framebuffer_helper.h> 21 22 #include "ltdc.h" 23 24 #define STM_MAX_FB_WIDTH 2048 25 #define STM_MAX_FB_HEIGHT 2048 /* same as width to handle orientation */ 26 27 static void drv_output_poll_changed(struct drm_device *ddev) 28 { 29 struct ltdc_device *ldev = ddev->dev_private; 30 31 drm_fbdev_cma_hotplug_event(ldev->fbdev); 32 } 33 34 static const struct drm_mode_config_funcs drv_mode_config_funcs = { 35 .fb_create = drm_gem_fb_create, 36 .output_poll_changed = drv_output_poll_changed, 37 .atomic_check = drm_atomic_helper_check, 38 .atomic_commit = drm_atomic_helper_commit, 39 }; 40 41 static void drv_lastclose(struct drm_device *ddev) 42 { 43 struct ltdc_device *ldev = ddev->dev_private; 44 45 DRM_DEBUG("%s\n", __func__); 46 47 drm_fbdev_cma_restore_mode(ldev->fbdev); 48 } 49 50 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops); 51 52 static struct drm_driver drv_driver = { 53 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | 54 DRIVER_ATOMIC, 55 .lastclose = drv_lastclose, 56 .name = "stm", 57 .desc = "STMicroelectronics SoC DRM", 58 .date = "20170330", 59 .major = 1, 60 .minor = 0, 61 .patchlevel = 0, 62 .fops = &drv_driver_fops, 63 .dumb_create = drm_gem_cma_dumb_create, 64 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 65 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 66 .gem_free_object_unlocked = drm_gem_cma_free_object, 67 .gem_vm_ops = &drm_gem_cma_vm_ops, 68 .gem_prime_export = drm_gem_prime_export, 69 .gem_prime_import = drm_gem_prime_import, 70 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 71 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 72 .gem_prime_vmap = drm_gem_cma_prime_vmap, 73 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 74 .gem_prime_mmap = drm_gem_cma_prime_mmap, 75 .enable_vblank = ltdc_crtc_enable_vblank, 76 .disable_vblank = ltdc_crtc_disable_vblank, 77 }; 78 79 static int drv_load(struct drm_device *ddev) 80 { 81 struct platform_device *pdev = to_platform_device(ddev->dev); 82 struct drm_fbdev_cma *fbdev; 83 struct ltdc_device *ldev; 84 int ret; 85 86 DRM_DEBUG("%s\n", __func__); 87 88 ldev = devm_kzalloc(ddev->dev, sizeof(*ldev), GFP_KERNEL); 89 if (!ldev) 90 return -ENOMEM; 91 92 ddev->dev_private = (void *)ldev; 93 94 drm_mode_config_init(ddev); 95 96 /* 97 * set max width and height as default value. 98 * this value would be used to check framebuffer size limitation 99 * at drm_mode_addfb(). 100 */ 101 ddev->mode_config.min_width = 0; 102 ddev->mode_config.min_height = 0; 103 ddev->mode_config.max_width = STM_MAX_FB_WIDTH; 104 ddev->mode_config.max_height = STM_MAX_FB_HEIGHT; 105 ddev->mode_config.funcs = &drv_mode_config_funcs; 106 107 ret = ltdc_load(ddev); 108 if (ret) 109 goto err; 110 111 drm_mode_config_reset(ddev); 112 drm_kms_helper_poll_init(ddev); 113 114 if (ddev->mode_config.num_connector) { 115 ldev = ddev->dev_private; 116 fbdev = drm_fbdev_cma_init(ddev, 16, 117 ddev->mode_config.num_connector); 118 if (IS_ERR(fbdev)) { 119 DRM_DEBUG("Warning: fails to create fbdev\n"); 120 fbdev = NULL; 121 } 122 ldev->fbdev = fbdev; 123 } 124 125 platform_set_drvdata(pdev, ddev); 126 127 return 0; 128 err: 129 drm_mode_config_cleanup(ddev); 130 return ret; 131 } 132 133 static void drv_unload(struct drm_device *ddev) 134 { 135 struct ltdc_device *ldev = ddev->dev_private; 136 137 DRM_DEBUG("%s\n", __func__); 138 139 if (ldev->fbdev) { 140 drm_fbdev_cma_fini(ldev->fbdev); 141 ldev->fbdev = NULL; 142 } 143 drm_kms_helper_poll_fini(ddev); 144 ltdc_unload(ddev); 145 drm_mode_config_cleanup(ddev); 146 } 147 148 static int stm_drm_platform_probe(struct platform_device *pdev) 149 { 150 struct device *dev = &pdev->dev; 151 struct drm_device *ddev; 152 int ret; 153 154 DRM_DEBUG("%s\n", __func__); 155 156 dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 157 158 ddev = drm_dev_alloc(&drv_driver, dev); 159 if (IS_ERR(ddev)) 160 return PTR_ERR(ddev); 161 162 ret = drv_load(ddev); 163 if (ret) 164 goto err_unref; 165 166 ret = drm_dev_register(ddev, 0); 167 if (ret) 168 goto err_unref; 169 170 return 0; 171 172 err_unref: 173 drm_dev_unref(ddev); 174 175 return ret; 176 } 177 178 static int stm_drm_platform_remove(struct platform_device *pdev) 179 { 180 struct drm_device *ddev = platform_get_drvdata(pdev); 181 182 DRM_DEBUG("%s\n", __func__); 183 184 drm_dev_unregister(ddev); 185 drv_unload(ddev); 186 drm_dev_unref(ddev); 187 188 return 0; 189 } 190 191 static const struct of_device_id drv_dt_ids[] = { 192 { .compatible = "st,stm32-ltdc"}, 193 { /* end node */ }, 194 }; 195 MODULE_DEVICE_TABLE(of, drv_dt_ids); 196 197 static struct platform_driver stm_drm_platform_driver = { 198 .probe = stm_drm_platform_probe, 199 .remove = stm_drm_platform_remove, 200 .driver = { 201 .name = "stm32-display", 202 .of_match_table = drv_dt_ids, 203 }, 204 }; 205 206 module_platform_driver(stm_drm_platform_driver); 207 208 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>"); 209 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>"); 210 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>"); 211 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>"); 212 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver"); 213 MODULE_LICENSE("GPL v2"); 214