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