xref: /linux/drivers/gpu/drm/stm/drv.c (revision a8b70ccf10e38775785d9cb12ead916474549f99)
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 	.output_poll_changed = drm_fb_helper_output_poll_changed,
30 	.atomic_check = drm_atomic_helper_check,
31 	.atomic_commit = drm_atomic_helper_commit,
32 };
33 
34 static int stm_gem_cma_dumb_create(struct drm_file *file,
35 				   struct drm_device *dev,
36 				   struct drm_mode_create_dumb *args)
37 {
38 	unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
39 
40 	/*
41 	 * in order to optimize data transfer, pitch is aligned on
42 	 * 128 bytes, height is aligned on 4 bytes
43 	 */
44 	args->pitch = roundup(min_pitch, 128);
45 	args->height = roundup(args->height, 4);
46 
47 	return drm_gem_cma_dumb_create_internal(file, dev, args);
48 }
49 
50 DEFINE_DRM_GEM_CMA_FOPS(drv_driver_fops);
51 
52 static struct drm_driver drv_driver = {
53 	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_PRIME |
54 			   DRIVER_ATOMIC,
55 	.lastclose = drm_fb_helper_lastclose,
56 	.name = "stm",
57 	.desc = "STMicroelectronics SoC DRM",
58 	.date = "20170330",
59 	.major = 1,
60 	.minor = 0,
61 	.patchlevel = 0,
62 	.fops = &drv_driver_fops,
63 	.dumb_create = stm_gem_cma_dumb_create,
64 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
65 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
66 	.gem_free_object_unlocked = drm_gem_cma_free_object,
67 	.gem_vm_ops = &drm_gem_cma_vm_ops,
68 	.gem_prime_export = drm_gem_prime_export,
69 	.gem_prime_import = drm_gem_prime_import,
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 	.enable_vblank = ltdc_crtc_enable_vblank,
76 	.disable_vblank = ltdc_crtc_disable_vblank,
77 };
78 
79 static int drv_load(struct drm_device *ddev)
80 {
81 	struct platform_device *pdev = to_platform_device(ddev->dev);
82 	struct ltdc_device *ldev;
83 	int ret;
84 
85 	DRM_DEBUG("%s\n", __func__);
86 
87 	ldev = devm_kzalloc(ddev->dev, sizeof(*ldev), GFP_KERNEL);
88 	if (!ldev)
89 		return -ENOMEM;
90 
91 	ddev->dev_private = (void *)ldev;
92 
93 	drm_mode_config_init(ddev);
94 
95 	/*
96 	 * set max width and height as default value.
97 	 * this value would be used to check framebuffer size limitation
98 	 * at drm_mode_addfb().
99 	 */
100 	ddev->mode_config.min_width = 0;
101 	ddev->mode_config.min_height = 0;
102 	ddev->mode_config.max_width = STM_MAX_FB_WIDTH;
103 	ddev->mode_config.max_height = STM_MAX_FB_HEIGHT;
104 	ddev->mode_config.funcs = &drv_mode_config_funcs;
105 
106 	ret = ltdc_load(ddev);
107 	if (ret)
108 		goto err;
109 
110 	drm_mode_config_reset(ddev);
111 	drm_kms_helper_poll_init(ddev);
112 
113 	if (ddev->mode_config.num_connector) {
114 		ret = drm_fb_cma_fbdev_init(ddev, 16, 0);
115 		if (ret)
116 			DRM_DEBUG("Warning: fails to create fbdev\n");
117 	}
118 
119 	platform_set_drvdata(pdev, ddev);
120 
121 	return 0;
122 err:
123 	drm_mode_config_cleanup(ddev);
124 	return ret;
125 }
126 
127 static void drv_unload(struct drm_device *ddev)
128 {
129 	DRM_DEBUG("%s\n", __func__);
130 
131 	drm_fb_cma_fbdev_fini(ddev);
132 	drm_kms_helper_poll_fini(ddev);
133 	ltdc_unload(ddev);
134 	drm_mode_config_cleanup(ddev);
135 }
136 
137 static int stm_drm_platform_probe(struct platform_device *pdev)
138 {
139 	struct device *dev = &pdev->dev;
140 	struct drm_device *ddev;
141 	int ret;
142 
143 	DRM_DEBUG("%s\n", __func__);
144 
145 	dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
146 
147 	ddev = drm_dev_alloc(&drv_driver, dev);
148 	if (IS_ERR(ddev))
149 		return PTR_ERR(ddev);
150 
151 	ret = drv_load(ddev);
152 	if (ret)
153 		goto err_unref;
154 
155 	ret = drm_dev_register(ddev, 0);
156 	if (ret)
157 		goto err_unref;
158 
159 	return 0;
160 
161 err_unref:
162 	drm_dev_unref(ddev);
163 
164 	return ret;
165 }
166 
167 static int stm_drm_platform_remove(struct platform_device *pdev)
168 {
169 	struct drm_device *ddev = platform_get_drvdata(pdev);
170 
171 	DRM_DEBUG("%s\n", __func__);
172 
173 	drm_dev_unregister(ddev);
174 	drv_unload(ddev);
175 	drm_dev_unref(ddev);
176 
177 	return 0;
178 }
179 
180 static const struct of_device_id drv_dt_ids[] = {
181 	{ .compatible = "st,stm32-ltdc"},
182 	{ /* end node */ },
183 };
184 MODULE_DEVICE_TABLE(of, drv_dt_ids);
185 
186 static struct platform_driver stm_drm_platform_driver = {
187 	.probe = stm_drm_platform_probe,
188 	.remove = stm_drm_platform_remove,
189 	.driver = {
190 		.name = "stm32-display",
191 		.of_match_table = drv_dt_ids,
192 	},
193 };
194 
195 module_platform_driver(stm_drm_platform_driver);
196 
197 MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
198 MODULE_AUTHOR("Yannick Fertre <yannick.fertre@st.com>");
199 MODULE_AUTHOR("Fabien Dessenne <fabien.dessenne@st.com>");
200 MODULE_AUTHOR("Mickael Reulier <mickael.reulier@st.com>");
201 MODULE_DESCRIPTION("STMicroelectronics ST DRM LTDC driver");
202 MODULE_LICENSE("GPL v2");
203