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