xref: /openbmc/linux/drivers/gpu/drm/stm/drv.c (revision 6726d552)
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_fb_cma_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_gem_cma_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 
34 static 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 
40 static int stm_gem_cma_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_cma_dumb_create_internal(file, dev, args);
54 }
55 
56 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops);
57 
58 static 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_CMA_DRIVER_OPS_WITH_DUMB_CREATE(stm_gem_cma_dumb_create),
68 };
69 
70 static 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 
112 static void drv_unload(struct drm_device *ddev)
113 {
114 	DRM_DEBUG("%s\n", __func__);
115 
116 	drm_kms_helper_poll_fini(ddev);
117 	ltdc_unload(ddev);
118 }
119 
120 static __maybe_unused int drv_suspend(struct device *dev)
121 {
122 	struct drm_device *ddev = dev_get_drvdata(dev);
123 	struct ltdc_device *ldev = ddev->dev_private;
124 	struct drm_atomic_state *state;
125 
126 	WARN_ON(ldev->suspend_state);
127 
128 	state = drm_atomic_helper_suspend(ddev);
129 	if (IS_ERR(state))
130 		return PTR_ERR(state);
131 
132 	ldev->suspend_state = state;
133 	pm_runtime_force_suspend(dev);
134 
135 	return 0;
136 }
137 
138 static __maybe_unused int drv_resume(struct device *dev)
139 {
140 	struct drm_device *ddev = dev_get_drvdata(dev);
141 	struct ltdc_device *ldev = ddev->dev_private;
142 	int ret;
143 
144 	if (WARN_ON(!ldev->suspend_state))
145 		return -ENOENT;
146 
147 	pm_runtime_force_resume(dev);
148 	ret = drm_atomic_helper_resume(ddev, ldev->suspend_state);
149 	if (ret)
150 		pm_runtime_force_suspend(dev);
151 
152 	ldev->suspend_state = NULL;
153 
154 	return ret;
155 }
156 
157 static __maybe_unused int drv_runtime_suspend(struct device *dev)
158 {
159 	struct drm_device *ddev = dev_get_drvdata(dev);
160 
161 	DRM_DEBUG_DRIVER("\n");
162 	ltdc_suspend(ddev);
163 
164 	return 0;
165 }
166 
167 static __maybe_unused int drv_runtime_resume(struct device *dev)
168 {
169 	struct drm_device *ddev = dev_get_drvdata(dev);
170 
171 	DRM_DEBUG_DRIVER("\n");
172 	return ltdc_resume(ddev);
173 }
174 
175 static const struct dev_pm_ops drv_pm_ops = {
176 	SET_SYSTEM_SLEEP_PM_OPS(drv_suspend, drv_resume)
177 	SET_RUNTIME_PM_OPS(drv_runtime_suspend,
178 			   drv_runtime_resume, NULL)
179 };
180 
181 static int stm_drm_platform_probe(struct platform_device *pdev)
182 {
183 	struct device *dev = &pdev->dev;
184 	struct drm_device *ddev;
185 	int ret;
186 
187 	DRM_DEBUG("%s\n", __func__);
188 
189 	ret = drm_aperture_remove_framebuffers(false, &drv_driver);
190 	if (ret)
191 		return ret;
192 
193 	dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
194 
195 	ddev = drm_dev_alloc(&drv_driver, dev);
196 	if (IS_ERR(ddev))
197 		return PTR_ERR(ddev);
198 
199 	ret = drv_load(ddev);
200 	if (ret)
201 		goto err_put;
202 
203 	ret = drm_dev_register(ddev, 0);
204 	if (ret)
205 		goto err_put;
206 
207 	drm_fbdev_generic_setup(ddev, 16);
208 
209 	return 0;
210 
211 err_put:
212 	drm_dev_put(ddev);
213 
214 	return ret;
215 }
216 
217 static int stm_drm_platform_remove(struct platform_device *pdev)
218 {
219 	struct drm_device *ddev = platform_get_drvdata(pdev);
220 
221 	DRM_DEBUG("%s\n", __func__);
222 
223 	drm_dev_unregister(ddev);
224 	drv_unload(ddev);
225 	drm_dev_put(ddev);
226 
227 	return 0;
228 }
229 
230 static const struct of_device_id drv_dt_ids[] = {
231 	{ .compatible = "st,stm32-ltdc"},
232 	{ /* end node */ },
233 };
234 MODULE_DEVICE_TABLE(of, drv_dt_ids);
235 
236 static struct platform_driver stm_drm_platform_driver = {
237 	.probe = stm_drm_platform_probe,
238 	.remove = stm_drm_platform_remove,
239 	.driver = {
240 		.name = "stm32-display",
241 		.of_match_table = drv_dt_ids,
242 		.pm = &drv_pm_ops,
243 	},
244 };
245 
246 drm_module_platform_driver(stm_drm_platform_driver);
247 
248 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
249 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
250 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
251 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
252 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
253 MODULE_LICENSE("GPL v2");
254