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