xref: /openbmc/linux/drivers/gpu/drm/stm/drv.c (revision c8ec3743)
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/of_platform.h>
13 
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_atomic_helper.h>
16 #include <drm/drm_crtc_helper.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_fb_cma_helper.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 
22 #include "ltdc.h"
23 
24 #define STM_MAX_FB_WIDTH	2048
25 #define STM_MAX_FB_HEIGHT	2048 /* same as width to handle orientation */
26 
27 static const struct drm_mode_config_funcs drv_mode_config_funcs = {
28 	.fb_create = drm_gem_fb_create,
29 	.atomic_check = drm_atomic_helper_check,
30 	.atomic_commit = drm_atomic_helper_commit,
31 };
32 
33 static int stm_gem_cma_dumb_create(struct drm_file *file,
34 				   struct drm_device *dev,
35 				   struct drm_mode_create_dumb *args)
36 {
37 	unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
38 
39 	/*
40 	 * in order to optimize data transfer, pitch is aligned on
41 	 * 128 bytes, height is aligned on 4 bytes
42 	 */
43 	args->pitch = roundup(min_pitch, 128);
44 	args->height = roundup(args->height, 4);
45 
46 	return drm_gem_cma_dumb_create_internal(file, dev, args);
47 }
48 
49 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops);
50 
51 static struct drm_driver drv_driver = {
52 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
53 			   DRIVER_ATOMIC,
54 	.name = "stm",
55 	.desc = "STMicroelectronics SoC DRM",
56 	.date = "20170330",
57 	.major = 1,
58 	.minor = 0,
59 	.patchlevel = 0,
60 	.fops = &drv_driver_fops,
61 	.dumb_create = stm_gem_cma_dumb_create,
62 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
63 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
64 	.gem_free_object_unlocked = drm_gem_cma_free_object,
65 	.gem_vm_ops = &drm_gem_cma_vm_ops,
66 	.gem_prime_export = drm_gem_prime_export,
67 	.gem_prime_import = drm_gem_prime_import,
68 	.gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
69 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
70 	.gem_prime_vmap = drm_gem_cma_prime_vmap,
71 	.gem_prime_vunmap = drm_gem_cma_prime_vunmap,
72 	.gem_prime_mmap = drm_gem_cma_prime_mmap,
73 	.get_scanout_position = ltdc_crtc_scanoutpos,
74 	.get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos,
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 int stm_drm_platform_probe(struct platform_device *pdev)
129 {
130 	struct device *dev = &pdev->dev;
131 	struct drm_device *ddev;
132 	int ret;
133 
134 	DRM_DEBUG("%s\n", __func__);
135 
136 	dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
137 
138 	ddev = drm_dev_alloc(&drv_driver, dev);
139 	if (IS_ERR(ddev))
140 		return PTR_ERR(ddev);
141 
142 	ret = drv_load(ddev);
143 	if (ret)
144 		goto err_put;
145 
146 	ret = drm_dev_register(ddev, 0);
147 	if (ret)
148 		goto err_put;
149 
150 	drm_fbdev_generic_setup(ddev, 16);
151 
152 	return 0;
153 
154 err_put:
155 	drm_dev_put(ddev);
156 
157 	return ret;
158 }
159 
160 static int stm_drm_platform_remove(struct platform_device *pdev)
161 {
162 	struct drm_device *ddev = platform_get_drvdata(pdev);
163 
164 	DRM_DEBUG("%s\n", __func__);
165 
166 	drm_dev_unregister(ddev);
167 	drv_unload(ddev);
168 	drm_dev_put(ddev);
169 
170 	return 0;
171 }
172 
173 static const struct of_device_id drv_dt_ids[] = {
174 	{ .compatible = "st,stm32-ltdc"},
175 	{ /* end node */ },
176 };
177 MODULE_DEVICE_TABLE(of, drv_dt_ids);
178 
179 static struct platform_driver stm_drm_platform_driver = {
180 	.probe = stm_drm_platform_probe,
181 	.remove = stm_drm_platform_remove,
182 	.driver = {
183 		.name = "stm32-display",
184 		.of_match_table = drv_dt_ids,
185 	},
186 };
187 
188 module_platform_driver(stm_drm_platform_driver);
189 
190 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
191 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
192 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
193 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
194 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
195 MODULE_LICENSE("GPL v2");
196