1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Russell King
4  */
5 #include <linux/clk.h>
6 #include <linux/component.h>
7 #include <linux/module.h>
8 #include <linux/of_graph.h>
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_probe_helper.h>
11 #include <drm/drm_fb_helper.h>
12 #include <drm/drm_of.h>
13 #include "armada_crtc.h"
14 #include "armada_drm.h"
15 #include "armada_gem.h"
16 #include "armada_fb.h"
17 #include "armada_hw.h"
18 #include <drm/armada_drm.h>
19 #include "armada_ioctlP.h"
20 
21 static struct drm_ioctl_desc armada_ioctls[] = {
22 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
23 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
24 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
25 };
26 
27 DEFINE_DRM_GEM_FOPS(armada_drm_fops);
28 
29 static struct drm_driver armada_drm_driver = {
30 	.lastclose		= drm_fb_helper_lastclose,
31 	.gem_free_object_unlocked = armada_gem_free_object,
32 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
33 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
34 	.gem_prime_export	= armada_gem_prime_export,
35 	.gem_prime_import	= armada_gem_prime_import,
36 	.dumb_create		= armada_gem_dumb_create,
37 	.gem_vm_ops		= &armada_gem_vm_ops,
38 	.major			= 1,
39 	.minor			= 0,
40 	.name			= "armada-drm",
41 	.desc			= "Armada SoC DRM",
42 	.date			= "20120730",
43 	.driver_features	= DRIVER_GEM | DRIVER_MODESET |
44 				  DRIVER_PRIME | DRIVER_ATOMIC,
45 	.ioctls			= armada_ioctls,
46 	.fops			= &armada_drm_fops,
47 };
48 
49 static const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
50 	.fb_create		= armada_fb_create,
51 	.output_poll_changed	= drm_fb_helper_output_poll_changed,
52 	.atomic_check		= drm_atomic_helper_check,
53 	.atomic_commit		= drm_atomic_helper_commit,
54 };
55 
56 static int armada_drm_bind(struct device *dev)
57 {
58 	struct armada_private *priv;
59 	struct resource *mem = NULL;
60 	int ret, n;
61 
62 	for (n = 0; ; n++) {
63 		struct resource *r = platform_get_resource(to_platform_device(dev),
64 							   IORESOURCE_MEM, n);
65 		if (!r)
66 			break;
67 
68 		/* Resources above 64K are graphics memory */
69 		if (resource_size(r) > SZ_64K)
70 			mem = r;
71 		else
72 			return -EINVAL;
73 	}
74 
75 	if (!mem)
76 		return -ENXIO;
77 
78 	if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
79 				     "armada-drm"))
80 		return -EBUSY;
81 
82 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
83 	if (!priv)
84 		return -ENOMEM;
85 
86 	/*
87 	 * The drm_device structure must be at the start of
88 	 * armada_private for drm_dev_put() to work correctly.
89 	 */
90 	BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
91 
92 	ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
93 	if (ret) {
94 		dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
95 			__func__, ret);
96 		kfree(priv);
97 		return ret;
98 	}
99 
100 	/* Remove early framebuffers */
101 	ret = drm_fb_helper_remove_conflicting_framebuffers(NULL,
102 							    "armada-drm-fb",
103 							    false);
104 	if (ret) {
105 		dev_err(dev, "[" DRM_NAME ":%s] can't kick out simple-fb: %d\n",
106 			__func__, ret);
107 		kfree(priv);
108 		return ret;
109 	}
110 
111 	priv->drm.dev_private = priv;
112 
113 	dev_set_drvdata(dev, &priv->drm);
114 
115 	/* Mode setting support */
116 	drm_mode_config_init(&priv->drm);
117 	priv->drm.mode_config.min_width = 320;
118 	priv->drm.mode_config.min_height = 200;
119 
120 	/*
121 	 * With vscale enabled, the maximum width is 1920 due to the
122 	 * 1920 by 3 lines RAM
123 	 */
124 	priv->drm.mode_config.max_width = 1920;
125 	priv->drm.mode_config.max_height = 2048;
126 
127 	priv->drm.mode_config.preferred_depth = 24;
128 	priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
129 	drm_mm_init(&priv->linear, mem->start, resource_size(mem));
130 	mutex_init(&priv->linear_lock);
131 
132 	ret = component_bind_all(dev, &priv->drm);
133 	if (ret)
134 		goto err_kms;
135 
136 	ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
137 	if (ret)
138 		goto err_comp;
139 
140 	priv->drm.irq_enabled = true;
141 
142 	drm_mode_config_reset(&priv->drm);
143 
144 	ret = armada_fbdev_init(&priv->drm);
145 	if (ret)
146 		goto err_comp;
147 
148 	drm_kms_helper_poll_init(&priv->drm);
149 
150 	ret = drm_dev_register(&priv->drm, 0);
151 	if (ret)
152 		goto err_poll;
153 
154 #ifdef CONFIG_DEBUG_FS
155 	armada_drm_debugfs_init(priv->drm.primary);
156 #endif
157 
158 	return 0;
159 
160  err_poll:
161 	drm_kms_helper_poll_fini(&priv->drm);
162 	armada_fbdev_fini(&priv->drm);
163  err_comp:
164 	component_unbind_all(dev, &priv->drm);
165  err_kms:
166 	drm_mode_config_cleanup(&priv->drm);
167 	drm_mm_takedown(&priv->linear);
168 	drm_dev_put(&priv->drm);
169 	return ret;
170 }
171 
172 static void armada_drm_unbind(struct device *dev)
173 {
174 	struct drm_device *drm = dev_get_drvdata(dev);
175 	struct armada_private *priv = drm->dev_private;
176 
177 	drm_kms_helper_poll_fini(&priv->drm);
178 	armada_fbdev_fini(&priv->drm);
179 
180 	drm_dev_unregister(&priv->drm);
181 
182 	drm_atomic_helper_shutdown(&priv->drm);
183 
184 	component_unbind_all(dev, &priv->drm);
185 
186 	drm_mode_config_cleanup(&priv->drm);
187 	drm_mm_takedown(&priv->linear);
188 
189 	drm_dev_put(&priv->drm);
190 }
191 
192 static int compare_of(struct device *dev, void *data)
193 {
194 	return dev->of_node == data;
195 }
196 
197 static int compare_dev_name(struct device *dev, void *data)
198 {
199 	const char *name = data;
200 	return !strcmp(dev_name(dev), name);
201 }
202 
203 static void armada_add_endpoints(struct device *dev,
204 	struct component_match **match, struct device_node *dev_node)
205 {
206 	struct device_node *ep, *remote;
207 
208 	for_each_endpoint_of_node(dev_node, ep) {
209 		remote = of_graph_get_remote_port_parent(ep);
210 		if (remote && of_device_is_available(remote))
211 			drm_of_component_match_add(dev, match, compare_of,
212 						   remote);
213 		of_node_put(remote);
214 	}
215 }
216 
217 static const struct component_master_ops armada_master_ops = {
218 	.bind = armada_drm_bind,
219 	.unbind = armada_drm_unbind,
220 };
221 
222 static int armada_drm_probe(struct platform_device *pdev)
223 {
224 	struct component_match *match = NULL;
225 	struct device *dev = &pdev->dev;
226 	int ret;
227 
228 	ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
229 	if (ret != -EINVAL)
230 		return ret;
231 
232 	if (dev->platform_data) {
233 		char **devices = dev->platform_data;
234 		struct device *d;
235 		int i;
236 
237 		for (i = 0; devices[i]; i++)
238 			component_match_add(dev, &match, compare_dev_name,
239 					    devices[i]);
240 
241 		if (i == 0) {
242 			dev_err(dev, "missing 'ports' property\n");
243 			return -ENODEV;
244 		}
245 
246 		for (i = 0; devices[i]; i++) {
247 			d = bus_find_device_by_name(&platform_bus_type, NULL,
248 						    devices[i]);
249 			if (d && d->of_node)
250 				armada_add_endpoints(dev, &match, d->of_node);
251 			put_device(d);
252 		}
253 	}
254 
255 	return component_master_add_with_match(&pdev->dev, &armada_master_ops,
256 					       match);
257 }
258 
259 static int armada_drm_remove(struct platform_device *pdev)
260 {
261 	component_master_del(&pdev->dev, &armada_master_ops);
262 	return 0;
263 }
264 
265 static const struct platform_device_id armada_drm_platform_ids[] = {
266 	{
267 		.name		= "armada-drm",
268 	}, {
269 		.name		= "armada-510-drm",
270 	},
271 	{ },
272 };
273 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
274 
275 static struct platform_driver armada_drm_platform_driver = {
276 	.probe	= armada_drm_probe,
277 	.remove	= armada_drm_remove,
278 	.driver	= {
279 		.name	= "armada-drm",
280 	},
281 	.id_table = armada_drm_platform_ids,
282 };
283 
284 static int __init armada_drm_init(void)
285 {
286 	int ret;
287 
288 	armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
289 
290 	ret = platform_driver_register(&armada_lcd_platform_driver);
291 	if (ret)
292 		return ret;
293 	ret = platform_driver_register(&armada_drm_platform_driver);
294 	if (ret)
295 		platform_driver_unregister(&armada_lcd_platform_driver);
296 	return ret;
297 }
298 module_init(armada_drm_init);
299 
300 static void __exit armada_drm_exit(void)
301 {
302 	platform_driver_unregister(&armada_drm_platform_driver);
303 	platform_driver_unregister(&armada_lcd_platform_driver);
304 }
305 module_exit(armada_drm_exit);
306 
307 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
308 MODULE_DESCRIPTION("Armada DRM Driver");
309 MODULE_LICENSE("GPL");
310 MODULE_ALIAS("platform:armada-drm");
311