xref: /linux/drivers/gpu/drm/stm/drv.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
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/clients/drm_client_setup.h>
20 #include <drm/drm_atomic.h>
21 #include <drm/drm_atomic_helper.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 
37 static 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 
stm_gem_dma_dumb_create(struct drm_file * file,struct drm_device * dev,struct drm_mode_create_dumb * args)43 static 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 
59 DEFINE_DRM_GEM_DMA_FOPS(drv_driver_fops);
60 
61 static const struct drm_driver drv_driver = {
62 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
63 	.name = "stm",
64 	.desc = "STMicroelectronics SoC DRM",
65 	.major = 1,
66 	.minor = 0,
67 	.patchlevel = 0,
68 	.fops = &drv_driver_fops,
69 	DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(stm_gem_dma_dumb_create),
70 	DRM_FBDEV_DMA_DRIVER_OPS,
71 };
72 
drv_load(struct drm_device * ddev)73 static int drv_load(struct drm_device *ddev)
74 {
75 	struct platform_device *pdev = to_platform_device(ddev->dev);
76 	struct ltdc_device *ldev;
77 	int ret;
78 
79 	DRM_DEBUG("%s\n", __func__);
80 
81 	ldev = drmm_kzalloc(ddev, sizeof(*ldev), GFP_KERNEL);
82 	if (!ldev)
83 		return -ENOMEM;
84 
85 	ddev->dev_private = (void *)ldev;
86 
87 	ret = drmm_mode_config_init(ddev);
88 	if (ret)
89 		return ret;
90 
91 	/*
92 	 * set max width and height as default value.
93 	 * this value would be used to check framebuffer size limitation
94 	 * at drm_mode_addfb().
95 	 */
96 	ddev->mode_config.min_width = 0;
97 	ddev->mode_config.min_height = 0;
98 	ddev->mode_config.max_width = STM_MAX_FB_WIDTH;
99 	ddev->mode_config.max_height = STM_MAX_FB_HEIGHT;
100 	ddev->mode_config.funcs = &drv_mode_config_funcs;
101 	ddev->mode_config.normalize_zpos = true;
102 
103 	ret = ltdc_load(ddev);
104 	if (ret)
105 		return ret;
106 
107 	drm_mode_config_reset(ddev);
108 	drm_kms_helper_poll_init(ddev);
109 
110 	platform_set_drvdata(pdev, ddev);
111 
112 	return 0;
113 }
114 
drv_unload(struct drm_device * ddev)115 static void drv_unload(struct drm_device *ddev)
116 {
117 	DRM_DEBUG("%s\n", __func__);
118 
119 	drm_kms_helper_poll_fini(ddev);
120 	drm_atomic_helper_shutdown(ddev);
121 	ltdc_unload(ddev);
122 }
123 
drv_suspend(struct device * dev)124 static __maybe_unused int drv_suspend(struct device *dev)
125 {
126 	struct drm_device *ddev = dev_get_drvdata(dev);
127 	struct ltdc_device *ldev = ddev->dev_private;
128 	struct drm_atomic_state *state;
129 
130 	WARN_ON(ldev->suspend_state);
131 
132 	state = drm_atomic_helper_suspend(ddev);
133 	if (IS_ERR(state))
134 		return PTR_ERR(state);
135 
136 	ldev->suspend_state = state;
137 	pm_runtime_force_suspend(dev);
138 
139 	return 0;
140 }
141 
drv_resume(struct device * dev)142 static __maybe_unused int drv_resume(struct device *dev)
143 {
144 	struct drm_device *ddev = dev_get_drvdata(dev);
145 	struct ltdc_device *ldev = ddev->dev_private;
146 	int ret;
147 
148 	if (WARN_ON(!ldev->suspend_state))
149 		return -ENOENT;
150 
151 	pm_runtime_force_resume(dev);
152 	ret = drm_atomic_helper_resume(ddev, ldev->suspend_state);
153 	if (ret)
154 		pm_runtime_force_suspend(dev);
155 
156 	ldev->suspend_state = NULL;
157 
158 	return ret;
159 }
160 
drv_runtime_suspend(struct device * dev)161 static __maybe_unused int drv_runtime_suspend(struct device *dev)
162 {
163 	struct drm_device *ddev = dev_get_drvdata(dev);
164 
165 	DRM_DEBUG_DRIVER("\n");
166 	ltdc_suspend(ddev);
167 
168 	return 0;
169 }
170 
drv_runtime_resume(struct device * dev)171 static __maybe_unused int drv_runtime_resume(struct device *dev)
172 {
173 	struct drm_device *ddev = dev_get_drvdata(dev);
174 
175 	DRM_DEBUG_DRIVER("\n");
176 	return ltdc_resume(ddev);
177 }
178 
179 static const struct dev_pm_ops drv_pm_ops = {
180 	SET_SYSTEM_SLEEP_PM_OPS(drv_suspend, drv_resume)
181 	SET_RUNTIME_PM_OPS(drv_runtime_suspend,
182 			   drv_runtime_resume, NULL)
183 };
184 
stm_drm_platform_probe(struct platform_device * pdev)185 static int stm_drm_platform_probe(struct platform_device *pdev)
186 {
187 	struct device *dev = &pdev->dev;
188 	struct drm_device *ddev;
189 	int ret;
190 
191 	DRM_DEBUG("%s\n", __func__);
192 
193 	ret = aperture_remove_all_conflicting_devices(drv_driver.name);
194 	if (ret)
195 		return ret;
196 
197 	dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
198 
199 	ddev = drm_dev_alloc(&drv_driver, dev);
200 	if (IS_ERR(ddev))
201 		return PTR_ERR(ddev);
202 
203 	ret = drv_load(ddev);
204 	if (ret)
205 		goto err_put;
206 
207 	ret = drm_dev_register(ddev, 0);
208 	if (ret)
209 		goto err_unload;
210 
211 	drm_client_setup_with_fourcc(ddev, DRM_FORMAT_RGB565);
212 
213 	return 0;
214 
215 err_unload:
216 	drv_unload(ddev);
217 err_put:
218 	drm_dev_put(ddev);
219 
220 	return ret;
221 }
222 
stm_drm_platform_remove(struct platform_device * pdev)223 static void 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 
stm_drm_platform_shutdown(struct platform_device * pdev)234 static void stm_drm_platform_shutdown(struct platform_device *pdev)
235 {
236 	drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
237 }
238 
239 static const struct of_device_id drv_dt_ids[] = {
240 	{ .compatible = "st,stm32-ltdc"},
241 	{ /* end node */ },
242 };
243 MODULE_DEVICE_TABLE(of, drv_dt_ids);
244 
245 static struct platform_driver stm_drm_platform_driver = {
246 	.probe = stm_drm_platform_probe,
247 	.remove = stm_drm_platform_remove,
248 	.shutdown = stm_drm_platform_shutdown,
249 	.driver = {
250 		.name = "stm32-display",
251 		.of_match_table = drv_dt_ids,
252 		.pm = &drv_pm_ops,
253 	},
254 };
255 
256 drm_module_platform_driver(stm_drm_platform_driver);
257 
258 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
259 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
260 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
261 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
262 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
263 MODULE_LICENSE("GPL v2");
264