xref: /openbmc/linux/drivers/gpu/drm/stm/drv.c (revision 6c33a6f4)
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 
32 static 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 
38 static 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 
54 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops);
55 
56 static 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 };
76 
77 static int drv_load(struct drm_device *ddev)
78 {
79 	struct platform_device *pdev = to_platform_device(ddev->dev);
80 	struct ltdc_device *ldev;
81 	int ret;
82 
83 	DRM_DEBUG("%s\n", __func__);
84 
85 	ldev = devm_kzalloc(ddev->dev, sizeof(*ldev), GFP_KERNEL);
86 	if (!ldev)
87 		return -ENOMEM;
88 
89 	ddev->dev_private = (void *)ldev;
90 
91 	drm_mode_config_init(ddev);
92 
93 	/*
94 	 * set max width and height as default value.
95 	 * this value would be used to check framebuffer size limitation
96 	 * at drm_mode_addfb().
97 	 */
98 	ddev->mode_config.min_width = 0;
99 	ddev->mode_config.min_height = 0;
100 	ddev->mode_config.max_width = STM_MAX_FB_WIDTH;
101 	ddev->mode_config.max_height = STM_MAX_FB_HEIGHT;
102 	ddev->mode_config.funcs = &drv_mode_config_funcs;
103 
104 	ret = ltdc_load(ddev);
105 	if (ret)
106 		goto err;
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 err:
115 	drm_mode_config_cleanup(ddev);
116 	return ret;
117 }
118 
119 static void drv_unload(struct drm_device *ddev)
120 {
121 	DRM_DEBUG("%s\n", __func__);
122 
123 	drm_kms_helper_poll_fini(ddev);
124 	ltdc_unload(ddev);
125 	drm_mode_config_cleanup(ddev);
126 }
127 
128 static __maybe_unused int drv_suspend(struct device *dev)
129 {
130 	struct drm_device *ddev = dev_get_drvdata(dev);
131 	struct ltdc_device *ldev = ddev->dev_private;
132 	struct drm_atomic_state *state;
133 
134 	WARN_ON(ldev->suspend_state);
135 
136 	state = drm_atomic_helper_suspend(ddev);
137 	if (IS_ERR(state))
138 		return PTR_ERR(state);
139 
140 	ldev->suspend_state = state;
141 	pm_runtime_force_suspend(dev);
142 
143 	return 0;
144 }
145 
146 static __maybe_unused int drv_resume(struct device *dev)
147 {
148 	struct drm_device *ddev = dev_get_drvdata(dev);
149 	struct ltdc_device *ldev = ddev->dev_private;
150 	int ret;
151 
152 	if (WARN_ON(!ldev->suspend_state))
153 		return -ENOENT;
154 
155 	pm_runtime_force_resume(dev);
156 	ret = drm_atomic_helper_resume(ddev, ldev->suspend_state);
157 	if (ret)
158 		pm_runtime_force_suspend(dev);
159 
160 	ldev->suspend_state = NULL;
161 
162 	return ret;
163 }
164 
165 static __maybe_unused int drv_runtime_suspend(struct device *dev)
166 {
167 	struct drm_device *ddev = dev_get_drvdata(dev);
168 
169 	DRM_DEBUG_DRIVER("\n");
170 	ltdc_suspend(ddev);
171 
172 	return 0;
173 }
174 
175 static __maybe_unused int drv_runtime_resume(struct device *dev)
176 {
177 	struct drm_device *ddev = dev_get_drvdata(dev);
178 
179 	DRM_DEBUG_DRIVER("\n");
180 	return ltdc_resume(ddev);
181 }
182 
183 static const struct dev_pm_ops drv_pm_ops = {
184 	SET_SYSTEM_SLEEP_PM_OPS(drv_suspend, drv_resume)
185 	SET_RUNTIME_PM_OPS(drv_runtime_suspend,
186 			   drv_runtime_resume, NULL)
187 };
188 
189 static int stm_drm_platform_probe(struct platform_device *pdev)
190 {
191 	struct device *dev = &pdev->dev;
192 	struct drm_device *ddev;
193 	int ret;
194 
195 	DRM_DEBUG("%s\n", __func__);
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_put;
210 
211 	drm_fbdev_generic_setup(ddev, 16);
212 
213 	return 0;
214 
215 err_put:
216 	drm_dev_put(ddev);
217 
218 	return ret;
219 }
220 
221 static int stm_drm_platform_remove(struct platform_device *pdev)
222 {
223 	struct drm_device *ddev = platform_get_drvdata(pdev);
224 
225 	DRM_DEBUG("%s\n", __func__);
226 
227 	drm_dev_unregister(ddev);
228 	drv_unload(ddev);
229 	drm_dev_put(ddev);
230 
231 	return 0;
232 }
233 
234 static const struct of_device_id drv_dt_ids[] = {
235 	{ .compatible = "st,stm32-ltdc"},
236 	{ /* end node */ },
237 };
238 MODULE_DEVICE_TABLE(of, drv_dt_ids);
239 
240 static struct platform_driver stm_drm_platform_driver = {
241 	.probe = stm_drm_platform_probe,
242 	.remove = stm_drm_platform_remove,
243 	.driver = {
244 		.name = "stm32-display",
245 		.of_match_table = drv_dt_ids,
246 		.pm = &drv_pm_ops,
247 	},
248 };
249 
250 module_platform_driver(stm_drm_platform_driver);
251 
252 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
253 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
254 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
255 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
256 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
257 MODULE_LICENSE("GPL v2");
258