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