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