Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-block.git] / drivers / gpu / drm / sti / sti_drv.c
CommitLineData
e2842570 1// SPDX-License-Identifier: GPL-2.0
9bbf86fe
BG
2/*
3 * Copyright (C) STMicroelectronics SA 2014
4 * Author: Benjamin Gaignard <benjamin.gaignard@st.com> for STMicroelectronics.
9bbf86fe
BG
5 */
6
9bbf86fe 7#include <linux/component.h>
33d5ae6c 8#include <linux/debugfs.h>
5e2f97a9 9#include <linux/dma-mapping.h>
9bbf86fe
BG
10#include <linux/kernel.h>
11#include <linux/module.h>
722d4f06 12#include <linux/of.h>
9bbf86fe 13#include <linux/of_platform.h>
722d4f06 14#include <linux/platform_device.h>
9bbf86fe 15
b86711c6 16#include <drm/clients/drm_client_setup.h>
de4b00b0
BG
17#include <drm/drm_atomic.h>
18#include <drm/drm_atomic_helper.h>
5e2f97a9
SR
19#include <drm/drm_debugfs.h>
20#include <drm/drm_drv.h>
000a0134 21#include <drm/drm_fbdev_dma.h>
4a83c26a 22#include <drm/drm_gem_dma_helper.h>
80cad29f 23#include <drm/drm_gem_framebuffer_helper.h>
97ac0e47 24#include <drm/drm_of.h>
fcd70cd3 25#include <drm/drm_probe_helper.h>
9bbf86fe 26
9e1f05b2 27#include "sti_drv.h"
bf8f9e4a 28#include "sti_plane.h"
9bbf86fe
BG
29
30#define DRIVER_NAME "sti"
31#define DRIVER_DESC "STMicroelectronics SoC DRM"
9bbf86fe
BG
32#define DRIVER_MAJOR 1
33#define DRIVER_MINOR 0
34
35#define STI_MAX_FB_HEIGHT 4096
36#define STI_MAX_FB_WIDTH 4096
37
bf8f9e4a
VA
38static int sti_drm_fps_get(void *data, u64 *val)
39{
40 struct drm_device *drm_dev = data;
41 struct drm_plane *p;
42 unsigned int i = 0;
43
44 *val = 0;
45 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
46 struct sti_plane *plane = to_sti_plane(p);
47
48 *val |= plane->fps_info.output << i;
49 i++;
50 }
51
52 return 0;
53}
54
55static int sti_drm_fps_set(void *data, u64 val)
56{
57 struct drm_device *drm_dev = data;
58 struct drm_plane *p;
59 unsigned int i = 0;
60
61 list_for_each_entry(p, &drm_dev->mode_config.plane_list, head) {
62 struct sti_plane *plane = to_sti_plane(p);
63
c462c2f5 64 memset(&plane->fps_info, 0, sizeof(plane->fps_info));
bf8f9e4a 65 plane->fps_info.output = (val >> i) & 1;
c462c2f5 66
bf8f9e4a
VA
67 i++;
68 }
69
70 return 0;
71}
72
73DEFINE_SIMPLE_ATTRIBUTE(sti_drm_fps_fops,
74 sti_drm_fps_get, sti_drm_fps_set, "%llu\n");
75
76static int sti_drm_fps_dbg_show(struct seq_file *s, void *data)
77{
78 struct drm_info_node *node = s->private;
79 struct drm_device *dev = node->minor->dev;
80 struct drm_plane *p;
bf8f9e4a
VA
81
82 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
83 struct sti_plane *plane = to_sti_plane(p);
84
85 seq_printf(s, "%s%s\n",
86 plane->fps_info.fps_str,
87 plane->fps_info.fips_str);
88 }
89
bf8f9e4a
VA
90 return 0;
91}
92
93static struct drm_info_list sti_drm_dbg_list[] = {
94 {"fps_get", sti_drm_fps_dbg_show, 0},
95};
96
7ce84471 97static void sti_drm_dbg_init(struct drm_minor *minor)
bf8f9e4a 98{
54ac836b
WK
99 drm_debugfs_create_files(sti_drm_dbg_list,
100 ARRAY_SIZE(sti_drm_dbg_list),
101 minor->debugfs_root, minor);
bf8f9e4a 102
150c6624
GKH
103 debugfs_create_file("fps_show", S_IRUGO | S_IWUSR, minor->debugfs_root,
104 minor->dev, &sti_drm_fps_fops);
bf8f9e4a
VA
105
106 DRM_INFO("%s: debugfs installed\n", DRIVER_NAME);
bf8f9e4a
VA
107}
108
c5de4853 109static const struct drm_mode_config_funcs sti_mode_config_funcs = {
80cad29f 110 .fb_create = drm_gem_fb_create,
352f9a84 111 .atomic_check = drm_atomic_helper_check,
5e60f595 112 .atomic_commit = drm_atomic_helper_commit,
9bbf86fe
BG
113};
114
9e1f05b2 115static void sti_mode_config_init(struct drm_device *dev)
9bbf86fe
BG
116{
117 dev->mode_config.min_width = 0;
118 dev->mode_config.min_height = 0;
119
120 /*
121 * set max width and height as default value.
122 * this value would be used to check framebuffer size limitation
123 * at drm_mode_addfb().
124 */
738be9d6
VA
125 dev->mode_config.max_width = STI_MAX_FB_WIDTH;
126 dev->mode_config.max_height = STI_MAX_FB_HEIGHT;
9bbf86fe 127
9e1f05b2 128 dev->mode_config.funcs = &sti_mode_config_funcs;
352f9a84
PU
129
130 dev->mode_config.normalize_zpos = true;
9bbf86fe
BG
131}
132
4a83c26a 133DEFINE_DRM_GEM_DMA_FOPS(sti_driver_fops);
9bbf86fe 134
70a59dd8 135static const struct drm_driver sti_driver = {
0424fdaf 136 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
9e1f05b2 137 .fops = &sti_driver_fops,
4a83c26a 138 DRM_GEM_DMA_DRIVER_OPS,
df467da5 139 DRM_FBDEV_DMA_DRIVER_OPS,
9bbf86fe 140
bf8f9e4a 141 .debugfs_init = sti_drm_dbg_init,
bf8f9e4a 142
9bbf86fe
BG
143 .name = DRIVER_NAME,
144 .desc = DRIVER_DESC,
9bbf86fe
BG
145 .major = DRIVER_MAJOR,
146 .minor = DRIVER_MINOR,
147};
148
84601dbd
BG
149static int sti_init(struct drm_device *ddev)
150{
151 struct sti_private *private;
152
153 private = kzalloc(sizeof(*private), GFP_KERNEL);
154 if (!private)
155 return -ENOMEM;
156
157 ddev->dev_private = (void *)private;
158 dev_set_drvdata(ddev->dev, ddev);
159 private->drm_dev = ddev;
160
84601dbd
BG
161 drm_mode_config_init(ddev);
162
163 sti_mode_config_init(ddev);
164
165 drm_kms_helper_poll_init(ddev);
166
167 return 0;
168}
169
170static void sti_cleanup(struct drm_device *ddev)
171{
172 struct sti_private *private = ddev->dev_private;
173
84601dbd 174 drm_kms_helper_poll_fini(ddev);
a2b50bab
DV
175 drm_atomic_helper_shutdown(ddev);
176 drm_mode_config_cleanup(ddev);
b7e05db3 177 component_unbind_all(ddev->dev, ddev);
ce3d99c8 178 dev_set_drvdata(ddev->dev, NULL);
84601dbd
BG
179 kfree(private);
180 ddev->dev_private = NULL;
181}
182
9e1f05b2 183static int sti_bind(struct device *dev)
9bbf86fe 184{
84601dbd
BG
185 struct drm_device *ddev;
186 int ret;
187
188 ddev = drm_dev_alloc(&sti_driver, dev);
0f288605
TG
189 if (IS_ERR(ddev))
190 return PTR_ERR(ddev);
84601dbd 191
84601dbd
BG
192 ret = sti_init(ddev);
193 if (ret)
a08eac47 194 goto err_drm_dev_put;
84601dbd
BG
195
196 ret = component_bind_all(ddev->dev, ddev);
197 if (ret)
198 goto err_cleanup;
199
200 ret = drm_dev_register(ddev, 0);
201 if (ret)
a2b50bab 202 goto err_cleanup;
84601dbd
BG
203
204 drm_mode_config_reset(ddev);
205
df467da5 206 drm_client_setup(ddev, NULL);
8953e9ee 207
84601dbd
BG
208 return 0;
209
84601dbd
BG
210err_cleanup:
211 sti_cleanup(ddev);
a08eac47
TZ
212err_drm_dev_put:
213 drm_dev_put(ddev);
84601dbd 214 return ret;
9bbf86fe
BG
215}
216
9e1f05b2 217static void sti_unbind(struct device *dev)
9bbf86fe 218{
84601dbd
BG
219 struct drm_device *ddev = dev_get_drvdata(dev);
220
221 drm_dev_unregister(ddev);
222 sti_cleanup(ddev);
a08eac47 223 drm_dev_put(ddev);
9bbf86fe
BG
224}
225
9e1f05b2
VA
226static const struct component_master_ops sti_ops = {
227 .bind = sti_bind,
228 .unbind = sti_unbind,
9bbf86fe
BG
229};
230
9e1f05b2 231static int sti_platform_probe(struct platform_device *pdev)
9bbf86fe
BG
232{
233 struct device *dev = &pdev->dev;
53bdcf5f 234 struct device_node *node = dev->of_node;
9bbf86fe
BG
235 struct device_node *child_np;
236 struct component_match *match = NULL;
237
238 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
239
ac7d3af8 240 devm_of_platform_populate(dev);
53bdcf5f 241
9bbf86fe
BG
242 child_np = of_get_next_available_child(node, NULL);
243
244 while (child_np) {
50a912a8 245 drm_of_component_match_add(dev, &match, component_compare_of,
97ac0e47 246 child_np);
9bbf86fe
BG
247 child_np = of_get_next_available_child(node, child_np);
248 }
249
9e1f05b2 250 return component_master_add_with_match(dev, &sti_ops, match);
9bbf86fe
BG
251}
252
9a865e45 253static void sti_platform_remove(struct platform_device *pdev)
9bbf86fe 254{
9e1f05b2 255 component_master_del(&pdev->dev, &sti_ops);
9bbf86fe
BG
256}
257
ce3d99c8
DA
258static void sti_platform_shutdown(struct platform_device *pdev)
259{
260 drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
261}
262
9e1f05b2 263static const struct of_device_id sti_dt_ids[] = {
9bbf86fe
BG
264 { .compatible = "st,sti-display-subsystem", },
265 { /* end node */ },
266};
9e1f05b2 267MODULE_DEVICE_TABLE(of, sti_dt_ids);
9bbf86fe 268
9e1f05b2
VA
269static struct platform_driver sti_platform_driver = {
270 .probe = sti_platform_probe,
e70140ba 271 .remove = sti_platform_remove,
ce3d99c8 272 .shutdown = sti_platform_shutdown,
9bbf86fe 273 .driver = {
9bbf86fe 274 .name = DRIVER_NAME,
9e1f05b2 275 .of_match_table = sti_dt_ids,
9bbf86fe
BG
276 },
277};
278
dcec16ef
TR
279static struct platform_driver * const drivers[] = {
280 &sti_tvout_driver,
dcec16ef
TR
281 &sti_hqvdp_driver,
282 &sti_hdmi_driver,
283 &sti_hda_driver,
284 &sti_dvo_driver,
285 &sti_vtg_driver,
286 &sti_compositor_driver,
287 &sti_platform_driver,
288};
289
290static int sti_drm_init(void)
291{
89ec0023
JMC
292 if (drm_firmware_drivers_only())
293 return -ENODEV;
294
dcec16ef
TR
295 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
296}
297module_init(sti_drm_init);
298
299static void sti_drm_exit(void)
300{
301 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
302}
303module_exit(sti_drm_exit);
9bbf86fe
BG
304
305MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
306MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
307MODULE_LICENSE("GPL");