1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics SA 2017
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>
11 #include <linux/component.h>
12 #include <linux/dma-mapping.h>
13 #include <linux/module.h>
14 #include <linux/of_platform.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/drm_vblank.h>
28 #define STM_MAX_FB_WIDTH 2048
29 #define STM_MAX_FB_HEIGHT 2048 /* same as width to handle orientation */
31 static const struct drm_mode_config_funcs drv_mode_config_funcs
= {
32 .fb_create
= drm_gem_fb_create
,
33 .atomic_check
= drm_atomic_helper_check
,
34 .atomic_commit
= drm_atomic_helper_commit
,
37 static int stm_gem_cma_dumb_create(struct drm_file
*file
,
38 struct drm_device
*dev
,
39 struct drm_mode_create_dumb
*args
)
41 unsigned int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
44 * in order to optimize data transfer, pitch is aligned on
45 * 128 bytes, height is aligned on 4 bytes
47 args
->pitch
= roundup(min_pitch
, 128);
48 args
->height
= roundup(args
->height
, 4);
50 return drm_gem_cma_dumb_create_internal(file
, dev
, args
);
53 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops
);
55 static struct drm_driver drv_driver
= {
56 .driver_features
= DRIVER_MODESET
| DRIVER_GEM
| DRIVER_PRIME
|
59 .desc
= "STMicroelectronics SoC DRM",
64 .fops
= &drv_driver_fops
,
65 .dumb_create
= stm_gem_cma_dumb_create
,
66 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
67 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
68 .gem_free_object_unlocked
= drm_gem_cma_free_object
,
69 .gem_vm_ops
= &drm_gem_cma_vm_ops
,
70 .gem_prime_export
= drm_gem_prime_export
,
71 .gem_prime_import
= drm_gem_prime_import
,
72 .gem_prime_get_sg_table
= drm_gem_cma_prime_get_sg_table
,
73 .gem_prime_import_sg_table
= drm_gem_cma_prime_import_sg_table
,
74 .gem_prime_vmap
= drm_gem_cma_prime_vmap
,
75 .gem_prime_vunmap
= drm_gem_cma_prime_vunmap
,
76 .gem_prime_mmap
= drm_gem_cma_prime_mmap
,
77 .get_scanout_position
= ltdc_crtc_scanoutpos
,
78 .get_vblank_timestamp
= drm_calc_vbltimestamp_from_scanoutpos
,
81 static int drv_load(struct drm_device
*ddev
)
83 struct platform_device
*pdev
= to_platform_device(ddev
->dev
);
84 struct ltdc_device
*ldev
;
87 DRM_DEBUG("%s\n", __func__
);
89 ldev
= devm_kzalloc(ddev
->dev
, sizeof(*ldev
), GFP_KERNEL
);
93 ddev
->dev_private
= (void *)ldev
;
95 drm_mode_config_init(ddev
);
98 * set max width and height as default value.
99 * this value would be used to check framebuffer size limitation
100 * at drm_mode_addfb().
102 ddev
->mode_config
.min_width
= 0;
103 ddev
->mode_config
.min_height
= 0;
104 ddev
->mode_config
.max_width
= STM_MAX_FB_WIDTH
;
105 ddev
->mode_config
.max_height
= STM_MAX_FB_HEIGHT
;
106 ddev
->mode_config
.funcs
= &drv_mode_config_funcs
;
108 ret
= ltdc_load(ddev
);
112 drm_mode_config_reset(ddev
);
113 drm_kms_helper_poll_init(ddev
);
115 platform_set_drvdata(pdev
, ddev
);
119 drm_mode_config_cleanup(ddev
);
123 static void drv_unload(struct drm_device
*ddev
)
125 DRM_DEBUG("%s\n", __func__
);
127 drm_kms_helper_poll_fini(ddev
);
129 drm_mode_config_cleanup(ddev
);
132 static int stm_drm_platform_probe(struct platform_device
*pdev
)
134 struct device
*dev
= &pdev
->dev
;
135 struct drm_device
*ddev
;
138 DRM_DEBUG("%s\n", __func__
);
140 dma_set_coherent_mask(dev
, DMA_BIT_MASK(32));
142 ddev
= drm_dev_alloc(&drv_driver
, dev
);
144 return PTR_ERR(ddev
);
146 ret
= drv_load(ddev
);
150 ret
= drm_dev_register(ddev
, 0);
154 drm_fbdev_generic_setup(ddev
, 16);
164 static int stm_drm_platform_remove(struct platform_device
*pdev
)
166 struct drm_device
*ddev
= platform_get_drvdata(pdev
);
168 DRM_DEBUG("%s\n", __func__
);
170 drm_dev_unregister(ddev
);
177 static const struct of_device_id drv_dt_ids
[] = {
178 { .compatible
= "st,stm32-ltdc"},
181 MODULE_DEVICE_TABLE(of
, drv_dt_ids
);
183 static struct platform_driver stm_drm_platform_driver
= {
184 .probe
= stm_drm_platform_probe
,
185 .remove
= stm_drm_platform_remove
,
187 .name
= "stm32-display",
188 .of_match_table
= drv_dt_ids
,
192 module_platform_driver(stm_drm_platform_driver
);
194 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
195 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
196 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
197 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
198 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
199 MODULE_LICENSE("GPL v2");