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