xref: /openbmc/linux/drivers/gpu/drm/stm/drv.c (revision d6fc9fcb)
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 
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fb_cma_helper.h>
20 #include <drm/drm_fb_helper.h>
21 #include <drm/drm_gem_cma_helper.h>
22 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <drm/drm_probe_helper.h>
24 #include <drm/drm_vblank.h>
25 
26 #include "ltdc.h"
27 
28 #define STM_MAX_FB_WIDTH	2048
29 #define STM_MAX_FB_HEIGHT	2048 /* same as width to handle orientation */
30 
31 static const struct drm_mode_config_funcs drv_mode_config_funcs = {
32 	.fb_create = drm_gem_fb_create,
33 	.atomic_check = drm_atomic_helper_check,
34 	.atomic_commit = drm_atomic_helper_commit,
35 };
36 
37 static int stm_gem_cma_dumb_create(struct drm_file *file,
38 				   struct drm_device *dev,
39 				   struct drm_mode_create_dumb *args)
40 {
41 	unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
42 
43 	/*
44 	 * in order to optimize data transfer, pitch is aligned on
45 	 * 128 bytes, height is aligned on 4 bytes
46 	 */
47 	args->pitch = roundup(min_pitch, 128);
48 	args->height = roundup(args->height, 4);
49 
50 	return drm_gem_cma_dumb_create_internal(file, dev, args);
51 }
52 
53 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops);
54 
55 static struct drm_driver drv_driver = {
56 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
57 			   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_export = drm_gem_prime_export,
71 	.gem_prime_import = drm_gem_prime_import,
72 	.gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
73 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
74 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
75 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
76 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
77 	.get_scanout_position = ltdc_crtc_scanoutpos,
78 	.get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos,
79 };
80 
81 static int drv_load(struct drm_device *ddev)
82 {
83 	struct platform_device *pdev = to_platform_device(ddev->dev);
84 	struct ltdc_device *ldev;
85 	int ret;
86 
87 	DRM_DEBUG("%s\n", __func__);
88 
89 	ldev = devm_kzalloc(ddev->dev, sizeof(*ldev), GFP_KERNEL);
90 	if (!ldev)
91 		return -ENOMEM;
92 
93 	ddev->dev_private = (void *)ldev;
94 
95 	drm_mode_config_init(ddev);
96 
97 	/*
98 	 * set max width and height as default value.
99 	 * this value would be used to check framebuffer size limitation
100 	 * at drm_mode_addfb().
101 	 */
102 	ddev->mode_config.min_width = 0;
103 	ddev->mode_config.min_height = 0;
104 	ddev->mode_config.max_width = STM_MAX_FB_WIDTH;
105 	ddev->mode_config.max_height = STM_MAX_FB_HEIGHT;
106 	ddev->mode_config.funcs = &drv_mode_config_funcs;
107 
108 	ret = ltdc_load(ddev);
109 	if (ret)
110 		goto err;
111 
112 	drm_mode_config_reset(ddev);
113 	drm_kms_helper_poll_init(ddev);
114 
115 	platform_set_drvdata(pdev, ddev);
116 
117 	return 0;
118 err:
119 	drm_mode_config_cleanup(ddev);
120 	return ret;
121 }
122 
123 static void drv_unload(struct drm_device *ddev)
124 {
125 	DRM_DEBUG("%s\n", __func__);
126 
127 	drm_kms_helper_poll_fini(ddev);
128 	ltdc_unload(ddev);
129 	drm_mode_config_cleanup(ddev);
130 }
131 
132 static __maybe_unused int drv_suspend(struct device *dev)
133 {
134 	struct drm_device *ddev = dev_get_drvdata(dev);
135 	struct ltdc_device *ldev = ddev->dev_private;
136 	struct drm_atomic_state *state;
137 
138 	drm_kms_helper_poll_disable(ddev);
139 	state = drm_atomic_helper_suspend(ddev);
140 	if (IS_ERR(state)) {
141 		drm_kms_helper_poll_enable(ddev);
142 		return PTR_ERR(state);
143 	}
144 	ldev->suspend_state = state;
145 	ltdc_suspend(ddev);
146 
147 	return 0;
148 }
149 
150 static __maybe_unused int drv_resume(struct device *dev)
151 {
152 	struct drm_device *ddev = dev_get_drvdata(dev);
153 	struct ltdc_device *ldev = ddev->dev_private;
154 
155 	ltdc_resume(ddev);
156 	drm_atomic_helper_resume(ddev, ldev->suspend_state);
157 	drm_kms_helper_poll_enable(ddev);
158 
159 	return 0;
160 }
161 
162 static const struct dev_pm_ops drv_pm_ops = {
163 	SET_SYSTEM_SLEEP_PM_OPS(drv_suspend, drv_resume)
164 };
165 
166 static int stm_drm_platform_probe(struct platform_device *pdev)
167 {
168 	struct device *dev = &pdev->dev;
169 	struct drm_device *ddev;
170 	int ret;
171 
172 	DRM_DEBUG("%s\n", __func__);
173 
174 	dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
175 
176 	ddev = drm_dev_alloc(&drv_driver, dev);
177 	if (IS_ERR(ddev))
178 		return PTR_ERR(ddev);
179 
180 	ret = drv_load(ddev);
181 	if (ret)
182 		goto err_put;
183 
184 	ret = drm_dev_register(ddev, 0);
185 	if (ret)
186 		goto err_put;
187 
188 	drm_fbdev_generic_setup(ddev, 16);
189 
190 	return 0;
191 
192 err_put:
193 	drm_dev_put(ddev);
194 
195 	return ret;
196 }
197 
198 static int stm_drm_platform_remove(struct platform_device *pdev)
199 {
200 	struct drm_device *ddev = platform_get_drvdata(pdev);
201 
202 	DRM_DEBUG("%s\n", __func__);
203 
204 	drm_dev_unregister(ddev);
205 	drv_unload(ddev);
206 	drm_dev_put(ddev);
207 
208 	return 0;
209 }
210 
211 static const struct of_device_id drv_dt_ids[] = {
212 	{ .compatible = "st,stm32-ltdc"},
213 	{ /* end node */ },
214 };
215 MODULE_DEVICE_TABLE(of, drv_dt_ids);
216 
217 static struct platform_driver stm_drm_platform_driver = {
218 	.probe = stm_drm_platform_probe,
219 	.remove = stm_drm_platform_remove,
220 	.driver = {
221 		.name = "stm32-display",
222 		.of_match_table = drv_dt_ids,
223 		.pm = &drv_pm_ops,
224 	},
225 };
226 
227 module_platform_driver(stm_drm_platform_driver);
228 
229 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
230 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
231 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
232 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
233 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
234 MODULE_LICENSE("GPL v2");
235