xref: /linux/drivers/gpu/drm/armada/armada_drv.c (revision a8fe58cec351c25e09c393bf46117c0c47b5a17c)
1 /*
2  * Copyright (C) 2012 Russell King
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  */
8 #include <linux/clk.h>
9 #include <linux/component.h>
10 #include <linux/module.h>
11 #include <linux/of_graph.h>
12 #include <drm/drmP.h>
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_of.h>
15 #include "armada_crtc.h"
16 #include "armada_drm.h"
17 #include "armada_gem.h"
18 #include "armada_hw.h"
19 #include <drm/armada_drm.h>
20 #include "armada_ioctlP.h"
21 
22 static void armada_drm_unref_work(struct work_struct *work)
23 {
24 	struct armada_private *priv =
25 		container_of(work, struct armada_private, fb_unref_work);
26 	struct drm_framebuffer *fb;
27 
28 	while (kfifo_get(&priv->fb_unref, &fb))
29 		drm_framebuffer_unreference(fb);
30 }
31 
32 /* Must be called with dev->event_lock held */
33 void __armada_drm_queue_unref_work(struct drm_device *dev,
34 	struct drm_framebuffer *fb)
35 {
36 	struct armada_private *priv = dev->dev_private;
37 
38 	WARN_ON(!kfifo_put(&priv->fb_unref, fb));
39 	schedule_work(&priv->fb_unref_work);
40 }
41 
42 void armada_drm_queue_unref_work(struct drm_device *dev,
43 	struct drm_framebuffer *fb)
44 {
45 	unsigned long flags;
46 
47 	spin_lock_irqsave(&dev->event_lock, flags);
48 	__armada_drm_queue_unref_work(dev, fb);
49 	spin_unlock_irqrestore(&dev->event_lock, flags);
50 }
51 
52 static int armada_drm_load(struct drm_device *dev, unsigned long flags)
53 {
54 	struct armada_private *priv;
55 	struct resource *mem = NULL;
56 	int ret, n;
57 
58 	for (n = 0; ; n++) {
59 		struct resource *r = platform_get_resource(dev->platformdev,
60 							   IORESOURCE_MEM, n);
61 		if (!r)
62 			break;
63 
64 		/* Resources above 64K are graphics memory */
65 		if (resource_size(r) > SZ_64K)
66 			mem = r;
67 		else
68 			return -EINVAL;
69 	}
70 
71 	if (!mem)
72 		return -ENXIO;
73 
74 	if (!devm_request_mem_region(dev->dev, mem->start,
75 			resource_size(mem), "armada-drm"))
76 		return -EBUSY;
77 
78 	priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
79 	if (!priv) {
80 		DRM_ERROR("failed to allocate private\n");
81 		return -ENOMEM;
82 	}
83 
84 	platform_set_drvdata(dev->platformdev, dev);
85 	dev->dev_private = priv;
86 
87 	INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
88 	INIT_KFIFO(priv->fb_unref);
89 
90 	/* Mode setting support */
91 	drm_mode_config_init(dev);
92 	dev->mode_config.min_width = 320;
93 	dev->mode_config.min_height = 200;
94 
95 	/*
96 	 * With vscale enabled, the maximum width is 1920 due to the
97 	 * 1920 by 3 lines RAM
98 	 */
99 	dev->mode_config.max_width = 1920;
100 	dev->mode_config.max_height = 2048;
101 
102 	dev->mode_config.preferred_depth = 24;
103 	dev->mode_config.funcs = &armada_drm_mode_config_funcs;
104 	drm_mm_init(&priv->linear, mem->start, resource_size(mem));
105 	mutex_init(&priv->linear_lock);
106 
107 	ret = component_bind_all(dev->dev, dev);
108 	if (ret)
109 		goto err_kms;
110 
111 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
112 	if (ret)
113 		goto err_comp;
114 
115 	dev->irq_enabled = true;
116 	dev->vblank_disable_allowed = 1;
117 
118 	ret = armada_fbdev_init(dev);
119 	if (ret)
120 		goto err_comp;
121 
122 	drm_kms_helper_poll_init(dev);
123 
124 	return 0;
125 
126  err_comp:
127 	component_unbind_all(dev->dev, dev);
128  err_kms:
129 	drm_mode_config_cleanup(dev);
130 	drm_mm_takedown(&priv->linear);
131 	flush_work(&priv->fb_unref_work);
132 
133 	return ret;
134 }
135 
136 static int armada_drm_unload(struct drm_device *dev)
137 {
138 	struct armada_private *priv = dev->dev_private;
139 
140 	drm_kms_helper_poll_fini(dev);
141 	armada_fbdev_fini(dev);
142 
143 	component_unbind_all(dev->dev, dev);
144 
145 	drm_mode_config_cleanup(dev);
146 	drm_mm_takedown(&priv->linear);
147 	flush_work(&priv->fb_unref_work);
148 	dev->dev_private = NULL;
149 
150 	return 0;
151 }
152 
153 /* These are called under the vbl_lock. */
154 static int armada_drm_enable_vblank(struct drm_device *dev, unsigned int pipe)
155 {
156 	struct armada_private *priv = dev->dev_private;
157 	armada_drm_crtc_enable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA);
158 	return 0;
159 }
160 
161 static void armada_drm_disable_vblank(struct drm_device *dev, unsigned int pipe)
162 {
163 	struct armada_private *priv = dev->dev_private;
164 	armada_drm_crtc_disable_irq(priv->dcrtc[pipe], VSYNC_IRQ_ENA);
165 }
166 
167 static struct drm_ioctl_desc armada_ioctls[] = {
168 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
169 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
170 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
171 };
172 
173 static void armada_drm_lastclose(struct drm_device *dev)
174 {
175 	armada_fbdev_lastclose(dev);
176 }
177 
178 static const struct file_operations armada_drm_fops = {
179 	.owner			= THIS_MODULE,
180 	.llseek			= no_llseek,
181 	.read			= drm_read,
182 	.poll			= drm_poll,
183 	.unlocked_ioctl		= drm_ioctl,
184 	.mmap			= drm_gem_mmap,
185 	.open			= drm_open,
186 	.release		= drm_release,
187 };
188 
189 static struct drm_driver armada_drm_driver = {
190 	.load			= armada_drm_load,
191 	.lastclose		= armada_drm_lastclose,
192 	.unload			= armada_drm_unload,
193 	.set_busid		= drm_platform_set_busid,
194 	.get_vblank_counter	= drm_vblank_no_hw_counter,
195 	.enable_vblank		= armada_drm_enable_vblank,
196 	.disable_vblank		= armada_drm_disable_vblank,
197 #ifdef CONFIG_DEBUG_FS
198 	.debugfs_init		= armada_drm_debugfs_init,
199 	.debugfs_cleanup	= armada_drm_debugfs_cleanup,
200 #endif
201 	.gem_free_object	= armada_gem_free_object,
202 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
203 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
204 	.gem_prime_export	= armada_gem_prime_export,
205 	.gem_prime_import	= armada_gem_prime_import,
206 	.dumb_create		= armada_gem_dumb_create,
207 	.dumb_map_offset	= armada_gem_dumb_map_offset,
208 	.dumb_destroy		= armada_gem_dumb_destroy,
209 	.gem_vm_ops		= &armada_gem_vm_ops,
210 	.major			= 1,
211 	.minor			= 0,
212 	.name			= "armada-drm",
213 	.desc			= "Armada SoC DRM",
214 	.date			= "20120730",
215 	.driver_features	= DRIVER_GEM | DRIVER_MODESET |
216 				  DRIVER_HAVE_IRQ | DRIVER_PRIME,
217 	.ioctls			= armada_ioctls,
218 	.fops			= &armada_drm_fops,
219 };
220 
221 static int armada_drm_bind(struct device *dev)
222 {
223 	return drm_platform_init(&armada_drm_driver, to_platform_device(dev));
224 }
225 
226 static void armada_drm_unbind(struct device *dev)
227 {
228 	drm_put_dev(dev_get_drvdata(dev));
229 }
230 
231 static int compare_of(struct device *dev, void *data)
232 {
233 	return dev->of_node == data;
234 }
235 
236 static int compare_dev_name(struct device *dev, void *data)
237 {
238 	const char *name = data;
239 	return !strcmp(dev_name(dev), name);
240 }
241 
242 static void armada_add_endpoints(struct device *dev,
243 	struct component_match **match, struct device_node *port)
244 {
245 	struct device_node *ep, *remote;
246 
247 	for_each_child_of_node(port, ep) {
248 		remote = of_graph_get_remote_port_parent(ep);
249 		if (!remote || !of_device_is_available(remote)) {
250 			of_node_put(remote);
251 			continue;
252 		} else if (!of_device_is_available(remote->parent)) {
253 			dev_warn(dev, "parent device of %s is not available\n",
254 				 remote->full_name);
255 			of_node_put(remote);
256 			continue;
257 		}
258 
259 		component_match_add(dev, match, compare_of, remote);
260 		of_node_put(remote);
261 	}
262 }
263 
264 static const struct component_master_ops armada_master_ops = {
265 	.bind = armada_drm_bind,
266 	.unbind = armada_drm_unbind,
267 };
268 
269 static int armada_drm_probe(struct platform_device *pdev)
270 {
271 	struct component_match *match = NULL;
272 	struct device *dev = &pdev->dev;
273 	int ret;
274 
275 	ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
276 	if (ret != -EINVAL)
277 		return ret;
278 
279 	if (dev->platform_data) {
280 		char **devices = dev->platform_data;
281 		struct device_node *port;
282 		struct device *d;
283 		int i;
284 
285 		for (i = 0; devices[i]; i++)
286 			component_match_add(dev, &match, compare_dev_name,
287 					    devices[i]);
288 
289 		if (i == 0) {
290 			dev_err(dev, "missing 'ports' property\n");
291 			return -ENODEV;
292 		}
293 
294 		for (i = 0; devices[i]; i++) {
295 			d = bus_find_device_by_name(&platform_bus_type, NULL,
296 						    devices[i]);
297 			if (d && d->of_node) {
298 				for_each_child_of_node(d->of_node, port)
299 					armada_add_endpoints(dev, &match, port);
300 			}
301 			put_device(d);
302 		}
303 	}
304 
305 	return component_master_add_with_match(&pdev->dev, &armada_master_ops,
306 					       match);
307 }
308 
309 static int armada_drm_remove(struct platform_device *pdev)
310 {
311 	component_master_del(&pdev->dev, &armada_master_ops);
312 	return 0;
313 }
314 
315 static const struct platform_device_id armada_drm_platform_ids[] = {
316 	{
317 		.name		= "armada-drm",
318 	}, {
319 		.name		= "armada-510-drm",
320 	},
321 	{ },
322 };
323 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
324 
325 static struct platform_driver armada_drm_platform_driver = {
326 	.probe	= armada_drm_probe,
327 	.remove	= armada_drm_remove,
328 	.driver	= {
329 		.name	= "armada-drm",
330 	},
331 	.id_table = armada_drm_platform_ids,
332 };
333 
334 static int __init armada_drm_init(void)
335 {
336 	int ret;
337 
338 	armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
339 
340 	ret = platform_driver_register(&armada_lcd_platform_driver);
341 	if (ret)
342 		return ret;
343 	ret = platform_driver_register(&armada_drm_platform_driver);
344 	if (ret)
345 		platform_driver_unregister(&armada_lcd_platform_driver);
346 	return ret;
347 }
348 module_init(armada_drm_init);
349 
350 static void __exit armada_drm_exit(void)
351 {
352 	platform_driver_unregister(&armada_drm_platform_driver);
353 	platform_driver_unregister(&armada_lcd_platform_driver);
354 }
355 module_exit(armada_drm_exit);
356 
357 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
358 MODULE_DESCRIPTION("Armada DRM Driver");
359 MODULE_LICENSE("GPL");
360 MODULE_ALIAS("platform:armada-drm");
361