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