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