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