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