1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) STMicroelectronics SA 2017 4 * 5 * Authors: Philippe Cornu <philippe.cornu@st.com> 6 * Yannick Fertre <yannick.fertre@st.com> 7 * Fabien Dessenne <fabien.dessenne@st.com> 8 * Mickael Reulier <mickael.reulier@st.com> 9 */ 10 11 #include <linux/component.h> 12 #include <linux/dma-mapping.h> 13 #include <linux/module.h> 14 #include <linux/of_platform.h> 15 #include <linux/pm_runtime.h> 16 17 #include <drm/drm_atomic.h> 18 #include <drm/drm_atomic_helper.h> 19 #include <drm/drm_drv.h> 20 #include <drm/drm_fb_cma_helper.h> 21 #include <drm/drm_fb_helper.h> 22 #include <drm/drm_gem_cma_helper.h> 23 #include <drm/drm_gem_framebuffer_helper.h> 24 #include <drm/drm_probe_helper.h> 25 #include <drm/drm_vblank.h> 26 27 #include "ltdc.h" 28 29 #define STM_MAX_FB_WIDTH 2048 30 #define STM_MAX_FB_HEIGHT 2048 /* same as width to handle orientation */ 31 32 static const struct drm_mode_config_funcs drv_mode_config_funcs = { 33 .fb_create = drm_gem_fb_create, 34 .atomic_check = drm_atomic_helper_check, 35 .atomic_commit = drm_atomic_helper_commit, 36 }; 37 38 static int stm_gem_cma_dumb_create(struct drm_file *file, 39 struct drm_device *dev, 40 struct drm_mode_create_dumb *args) 41 { 42 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); 43 44 /* 45 * in order to optimize data transfer, pitch is aligned on 46 * 128 bytes, height is aligned on 4 bytes 47 */ 48 args->pitch = roundup(min_pitch, 128); 49 args->height = roundup(args->height, 4); 50 51 return drm_gem_cma_dumb_create_internal(file, dev, args); 52 } 53 54 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops); 55 56 static struct drm_driver drv_driver = { 57 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME | 58 DRIVER_ATOMIC, 59 .name = "stm", 60 .desc = "STMicroelectronics SoC DRM", 61 .date = "20170330", 62 .major = 1, 63 .minor = 0, 64 .patchlevel = 0, 65 .fops = &drv_driver_fops, 66 .dumb_create = stm_gem_cma_dumb_create, 67 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, 68 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, 69 .gem_free_object_unlocked = drm_gem_cma_free_object, 70 .gem_vm_ops = &drm_gem_cma_vm_ops, 71 .gem_prime_export = drm_gem_prime_export, 72 .gem_prime_import = drm_gem_prime_import, 73 .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table, 74 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, 75 .gem_prime_vmap = drm_gem_cma_prime_vmap, 76 .gem_prime_vunmap = drm_gem_cma_prime_vunmap, 77 .gem_prime_mmap = drm_gem_cma_prime_mmap, 78 .get_scanout_position = ltdc_crtc_scanoutpos, 79 .get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos, 80 }; 81 82 static int drv_load(struct drm_device *ddev) 83 { 84 struct platform_device *pdev = to_platform_device(ddev->dev); 85 struct ltdc_device *ldev; 86 int ret; 87 88 DRM_DEBUG("%s\n", __func__); 89 90 ldev = devm_kzalloc(ddev->dev, sizeof(*ldev), GFP_KERNEL); 91 if (!ldev) 92 return -ENOMEM; 93 94 ddev->dev_private = (void *)ldev; 95 96 drm_mode_config_init(ddev); 97 98 /* 99 * set max width and height as default value. 100 * this value would be used to check framebuffer size limitation 101 * at drm_mode_addfb(). 102 */ 103 ddev->mode_config.min_width = 0; 104 ddev->mode_config.min_height = 0; 105 ddev->mode_config.max_width = STM_MAX_FB_WIDTH; 106 ddev->mode_config.max_height = STM_MAX_FB_HEIGHT; 107 ddev->mode_config.funcs = &drv_mode_config_funcs; 108 109 ret = ltdc_load(ddev); 110 if (ret) 111 goto err; 112 113 drm_mode_config_reset(ddev); 114 drm_kms_helper_poll_init(ddev); 115 116 platform_set_drvdata(pdev, ddev); 117 118 return 0; 119 err: 120 drm_mode_config_cleanup(ddev); 121 return ret; 122 } 123 124 static void drv_unload(struct drm_device *ddev) 125 { 126 DRM_DEBUG("%s\n", __func__); 127 128 drm_kms_helper_poll_fini(ddev); 129 ltdc_unload(ddev); 130 drm_mode_config_cleanup(ddev); 131 } 132 133 static __maybe_unused int drv_suspend(struct device *dev) 134 { 135 struct drm_device *ddev = dev_get_drvdata(dev); 136 struct ltdc_device *ldev = ddev->dev_private; 137 struct drm_atomic_state *state; 138 139 WARN_ON(ldev->suspend_state); 140 141 state = drm_atomic_helper_suspend(ddev); 142 if (IS_ERR(state)) 143 return PTR_ERR(state); 144 145 ldev->suspend_state = state; 146 pm_runtime_force_suspend(dev); 147 148 return 0; 149 } 150 151 static __maybe_unused int drv_resume(struct device *dev) 152 { 153 struct drm_device *ddev = dev_get_drvdata(dev); 154 struct ltdc_device *ldev = ddev->dev_private; 155 int ret; 156 157 if (WARN_ON(!ldev->suspend_state)) 158 return -ENOENT; 159 160 pm_runtime_force_resume(dev); 161 ret = drm_atomic_helper_resume(ddev, ldev->suspend_state); 162 if (ret) 163 pm_runtime_force_suspend(dev); 164 165 ldev->suspend_state = NULL; 166 167 return ret; 168 } 169 170 static __maybe_unused int drv_runtime_suspend(struct device *dev) 171 { 172 struct drm_device *ddev = dev_get_drvdata(dev); 173 174 DRM_DEBUG_DRIVER("\n"); 175 ltdc_suspend(ddev); 176 177 return 0; 178 } 179 180 static __maybe_unused int drv_runtime_resume(struct device *dev) 181 { 182 struct drm_device *ddev = dev_get_drvdata(dev); 183 184 DRM_DEBUG_DRIVER("\n"); 185 return ltdc_resume(ddev); 186 } 187 188 static const struct dev_pm_ops drv_pm_ops = { 189 SET_SYSTEM_SLEEP_PM_OPS(drv_suspend, drv_resume) 190 SET_RUNTIME_PM_OPS(drv_runtime_suspend, 191 drv_runtime_resume, NULL) 192 }; 193 194 static int stm_drm_platform_probe(struct platform_device *pdev) 195 { 196 struct device *dev = &pdev->dev; 197 struct drm_device *ddev; 198 int ret; 199 200 DRM_DEBUG("%s\n", __func__); 201 202 dma_set_coherent_mask(dev, DMA_BIT_MASK(32)); 203 204 ddev = drm_dev_alloc(&drv_driver, dev); 205 if (IS_ERR(ddev)) 206 return PTR_ERR(ddev); 207 208 ret = drv_load(ddev); 209 if (ret) 210 goto err_put; 211 212 ret = drm_dev_register(ddev, 0); 213 if (ret) 214 goto err_put; 215 216 drm_fbdev_generic_setup(ddev, 16); 217 218 return 0; 219 220 err_put: 221 drm_dev_put(ddev); 222 223 return ret; 224 } 225 226 static int stm_drm_platform_remove(struct platform_device *pdev) 227 { 228 struct drm_device *ddev = platform_get_drvdata(pdev); 229 230 DRM_DEBUG("%s\n", __func__); 231 232 drm_dev_unregister(ddev); 233 drv_unload(ddev); 234 drm_dev_put(ddev); 235 236 return 0; 237 } 238 239 static const struct of_device_id drv_dt_ids[] = { 240 { .compatible = "st,stm32-ltdc"}, 241 { /* end node */ }, 242 }; 243 MODULE_DEVICE_TABLE(of, drv_dt_ids); 244 245 static struct platform_driver stm_drm_platform_driver = { 246 .probe = stm_drm_platform_probe, 247 .remove = stm_drm_platform_remove, 248 .driver = { 249 .name = "stm32-display", 250 .of_match_table = drv_dt_ids, 251 .pm = &drv_pm_ops, 252 }, 253 }; 254 255 module_platform_driver(stm_drm_platform_driver); 256 257 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>"); 258 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>"); 259 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>"); 260 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>"); 261 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver"); 262 MODULE_LICENSE("GPL v2"); 263