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/drm_crtc_helper.h>
13 #include <drm/drm_fb_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_put(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 struct drm_ioctl_desc armada_ioctls[] = {
53 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
54 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
55 	DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
56 };
57 
58 DEFINE_DRM_GEM_FOPS(armada_drm_fops);
59 
60 static struct drm_driver armada_drm_driver = {
61 	.lastclose		= drm_fb_helper_lastclose,
62 	.gem_free_object_unlocked = armada_gem_free_object,
63 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd,
64 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle,
65 	.gem_prime_export	= armada_gem_prime_export,
66 	.gem_prime_import	= armada_gem_prime_import,
67 	.dumb_create		= armada_gem_dumb_create,
68 	.gem_vm_ops		= &armada_gem_vm_ops,
69 	.major			= 1,
70 	.minor			= 0,
71 	.name			= "armada-drm",
72 	.desc			= "Armada SoC DRM",
73 	.date			= "20120730",
74 	.driver_features	= DRIVER_GEM | DRIVER_MODESET |
75 				  DRIVER_PRIME,
76 	.ioctls			= armada_ioctls,
77 	.fops			= &armada_drm_fops,
78 };
79 
80 static int armada_drm_bind(struct device *dev)
81 {
82 	struct armada_private *priv;
83 	struct resource *mem = NULL;
84 	int ret, n;
85 
86 	for (n = 0; ; n++) {
87 		struct resource *r = platform_get_resource(to_platform_device(dev),
88 							   IORESOURCE_MEM, n);
89 		if (!r)
90 			break;
91 
92 		/* Resources above 64K are graphics memory */
93 		if (resource_size(r) > SZ_64K)
94 			mem = r;
95 		else
96 			return -EINVAL;
97 	}
98 
99 	if (!mem)
100 		return -ENXIO;
101 
102 	if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
103 				     "armada-drm"))
104 		return -EBUSY;
105 
106 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
107 	if (!priv)
108 		return -ENOMEM;
109 
110 	/*
111 	 * The drm_device structure must be at the start of
112 	 * armada_private for drm_dev_unref() to work correctly.
113 	 */
114 	BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
115 
116 	ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
117 	if (ret) {
118 		dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
119 			__func__, ret);
120 		kfree(priv);
121 		return ret;
122 	}
123 
124 	priv->drm.dev_private = priv;
125 
126 	dev_set_drvdata(dev, &priv->drm);
127 
128 	INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
129 	INIT_KFIFO(priv->fb_unref);
130 
131 	/* Mode setting support */
132 	drm_mode_config_init(&priv->drm);
133 	priv->drm.mode_config.min_width = 320;
134 	priv->drm.mode_config.min_height = 200;
135 
136 	/*
137 	 * With vscale enabled, the maximum width is 1920 due to the
138 	 * 1920 by 3 lines RAM
139 	 */
140 	priv->drm.mode_config.max_width = 1920;
141 	priv->drm.mode_config.max_height = 2048;
142 
143 	priv->drm.mode_config.preferred_depth = 24;
144 	priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
145 	drm_mm_init(&priv->linear, mem->start, resource_size(mem));
146 	mutex_init(&priv->linear_lock);
147 
148 	ret = component_bind_all(dev, &priv->drm);
149 	if (ret)
150 		goto err_kms;
151 
152 	ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
153 	if (ret)
154 		goto err_comp;
155 
156 	priv->drm.irq_enabled = true;
157 
158 	ret = armada_fbdev_init(&priv->drm);
159 	if (ret)
160 		goto err_comp;
161 
162 	drm_kms_helper_poll_init(&priv->drm);
163 
164 	ret = drm_dev_register(&priv->drm, 0);
165 	if (ret)
166 		goto err_poll;
167 
168 #ifdef CONFIG_DEBUG_FS
169 	armada_drm_debugfs_init(priv->drm.primary);
170 #endif
171 
172 	return 0;
173 
174  err_poll:
175 	drm_kms_helper_poll_fini(&priv->drm);
176 	armada_fbdev_fini(&priv->drm);
177  err_comp:
178 	component_unbind_all(dev, &priv->drm);
179  err_kms:
180 	drm_mode_config_cleanup(&priv->drm);
181 	drm_mm_takedown(&priv->linear);
182 	flush_work(&priv->fb_unref_work);
183 	drm_dev_unref(&priv->drm);
184 	return ret;
185 }
186 
187 static void armada_drm_unbind(struct device *dev)
188 {
189 	struct drm_device *drm = dev_get_drvdata(dev);
190 	struct armada_private *priv = drm->dev_private;
191 
192 	drm_kms_helper_poll_fini(&priv->drm);
193 	armada_fbdev_fini(&priv->drm);
194 
195 	drm_dev_unregister(&priv->drm);
196 
197 	component_unbind_all(dev, &priv->drm);
198 
199 	drm_mode_config_cleanup(&priv->drm);
200 	drm_mm_takedown(&priv->linear);
201 	flush_work(&priv->fb_unref_work);
202 
203 	drm_dev_unref(&priv->drm);
204 }
205 
206 static int compare_of(struct device *dev, void *data)
207 {
208 	return dev->of_node == data;
209 }
210 
211 static int compare_dev_name(struct device *dev, void *data)
212 {
213 	const char *name = data;
214 	return !strcmp(dev_name(dev), name);
215 }
216 
217 static void armada_add_endpoints(struct device *dev,
218 	struct component_match **match, struct device_node *port)
219 {
220 	struct device_node *ep, *remote;
221 
222 	for_each_child_of_node(port, ep) {
223 		remote = of_graph_get_remote_port_parent(ep);
224 		if (!remote || !of_device_is_available(remote)) {
225 			of_node_put(remote);
226 			continue;
227 		} else if (!of_device_is_available(remote->parent)) {
228 			dev_warn(dev, "parent device of %pOF is not available\n",
229 				 remote);
230 			of_node_put(remote);
231 			continue;
232 		}
233 
234 		drm_of_component_match_add(dev, match, compare_of, remote);
235 		of_node_put(remote);
236 	}
237 }
238 
239 static const struct component_master_ops armada_master_ops = {
240 	.bind = armada_drm_bind,
241 	.unbind = armada_drm_unbind,
242 };
243 
244 static int armada_drm_probe(struct platform_device *pdev)
245 {
246 	struct component_match *match = NULL;
247 	struct device *dev = &pdev->dev;
248 	int ret;
249 
250 	ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
251 	if (ret != -EINVAL)
252 		return ret;
253 
254 	if (dev->platform_data) {
255 		char **devices = dev->platform_data;
256 		struct device_node *port;
257 		struct device *d;
258 		int i;
259 
260 		for (i = 0; devices[i]; i++)
261 			component_match_add(dev, &match, compare_dev_name,
262 					    devices[i]);
263 
264 		if (i == 0) {
265 			dev_err(dev, "missing 'ports' property\n");
266 			return -ENODEV;
267 		}
268 
269 		for (i = 0; devices[i]; i++) {
270 			d = bus_find_device_by_name(&platform_bus_type, NULL,
271 						    devices[i]);
272 			if (d && d->of_node) {
273 				for_each_child_of_node(d->of_node, port)
274 					armada_add_endpoints(dev, &match, port);
275 			}
276 			put_device(d);
277 		}
278 	}
279 
280 	return component_master_add_with_match(&pdev->dev, &armada_master_ops,
281 					       match);
282 }
283 
284 static int armada_drm_remove(struct platform_device *pdev)
285 {
286 	component_master_del(&pdev->dev, &armada_master_ops);
287 	return 0;
288 }
289 
290 static const struct platform_device_id armada_drm_platform_ids[] = {
291 	{
292 		.name		= "armada-drm",
293 	}, {
294 		.name		= "armada-510-drm",
295 	},
296 	{ },
297 };
298 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
299 
300 static struct platform_driver armada_drm_platform_driver = {
301 	.probe	= armada_drm_probe,
302 	.remove	= armada_drm_remove,
303 	.driver	= {
304 		.name	= "armada-drm",
305 	},
306 	.id_table = armada_drm_platform_ids,
307 };
308 
309 static int __init armada_drm_init(void)
310 {
311 	int ret;
312 
313 	armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
314 
315 	ret = platform_driver_register(&armada_lcd_platform_driver);
316 	if (ret)
317 		return ret;
318 	ret = platform_driver_register(&armada_drm_platform_driver);
319 	if (ret)
320 		platform_driver_unregister(&armada_lcd_platform_driver);
321 	return ret;
322 }
323 module_init(armada_drm_init);
324 
325 static void __exit armada_drm_exit(void)
326 {
327 	platform_driver_unregister(&armada_drm_platform_driver);
328 	platform_driver_unregister(&armada_lcd_platform_driver);
329 }
330 module_exit(armada_drm_exit);
331 
332 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
333 MODULE_DESCRIPTION("Armada DRM Driver");
334 MODULE_LICENSE("GPL");
335 MODULE_ALIAS("platform:armada-drm");
336