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