2 * Copyright (C) 2012 Russell King
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.
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_of.h>
14 #include "armada_crtc.h"
15 #include "armada_drm.h"
16 #include "armada_gem.h"
17 #include "armada_hw.h"
18 #include <drm/armada_drm.h>
19 #include "armada_ioctlP.h"
21 static void armada_drm_unref_work(struct work_struct *work)
23 struct armada_private *priv =
24 container_of(work, struct armada_private, fb_unref_work);
25 struct drm_framebuffer *fb;
27 while (kfifo_get(&priv->fb_unref, &fb))
28 drm_framebuffer_put(fb);
31 /* Must be called with dev->event_lock held */
32 void __armada_drm_queue_unref_work(struct drm_device *dev,
33 struct drm_framebuffer *fb)
35 struct armada_private *priv = dev->dev_private;
37 WARN_ON(!kfifo_put(&priv->fb_unref, fb));
38 schedule_work(&priv->fb_unref_work);
41 void armada_drm_queue_unref_work(struct drm_device *dev,
42 struct drm_framebuffer *fb)
46 spin_lock_irqsave(&dev->event_lock, flags);
47 __armada_drm_queue_unref_work(dev, fb);
48 spin_unlock_irqrestore(&dev->event_lock, flags);
51 static struct drm_ioctl_desc armada_ioctls[] = {
52 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
53 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
54 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
57 static void armada_drm_lastclose(struct drm_device *dev)
59 armada_fbdev_lastclose(dev);
62 DEFINE_DRM_GEM_FOPS(armada_drm_fops);
64 static struct drm_driver armada_drm_driver = {
65 .lastclose = armada_drm_lastclose,
66 .gem_free_object_unlocked = armada_gem_free_object,
67 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
68 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
69 .gem_prime_export = armada_gem_prime_export,
70 .gem_prime_import = armada_gem_prime_import,
71 .dumb_create = armada_gem_dumb_create,
72 .gem_vm_ops = &armada_gem_vm_ops,
76 .desc = "Armada SoC DRM",
78 .driver_features = DRIVER_GEM | DRIVER_MODESET |
80 .ioctls = armada_ioctls,
81 .fops = &armada_drm_fops,
84 static int armada_drm_bind(struct device *dev)
86 struct armada_private *priv;
87 struct resource *mem = NULL;
91 struct resource *r = platform_get_resource(to_platform_device(dev),
96 /* Resources above 64K are graphics memory */
97 if (resource_size(r) > SZ_64K)
106 if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
110 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
115 * The drm_device structure must be at the start of
116 * armada_private for drm_dev_unref() to work correctly.
118 BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
120 ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
122 dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
128 priv->drm.dev_private = priv;
130 dev_set_drvdata(dev, &priv->drm);
132 INIT_WORK(&priv->fb_unref_work, armada_drm_unref_work);
133 INIT_KFIFO(priv->fb_unref);
135 /* Mode setting support */
136 drm_mode_config_init(&priv->drm);
137 priv->drm.mode_config.min_width = 320;
138 priv->drm.mode_config.min_height = 200;
141 * With vscale enabled, the maximum width is 1920 due to the
142 * 1920 by 3 lines RAM
144 priv->drm.mode_config.max_width = 1920;
145 priv->drm.mode_config.max_height = 2048;
147 priv->drm.mode_config.preferred_depth = 24;
148 priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
149 drm_mm_init(&priv->linear, mem->start, resource_size(mem));
150 mutex_init(&priv->linear_lock);
152 ret = component_bind_all(dev, &priv->drm);
156 ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
160 priv->drm.irq_enabled = true;
162 ret = armada_fbdev_init(&priv->drm);
166 drm_kms_helper_poll_init(&priv->drm);
168 ret = drm_dev_register(&priv->drm, 0);
172 #ifdef CONFIG_DEBUG_FS
173 armada_drm_debugfs_init(priv->drm.primary);
179 drm_kms_helper_poll_fini(&priv->drm);
180 armada_fbdev_fini(&priv->drm);
182 component_unbind_all(dev, &priv->drm);
184 drm_mode_config_cleanup(&priv->drm);
185 drm_mm_takedown(&priv->linear);
186 flush_work(&priv->fb_unref_work);
187 drm_dev_unref(&priv->drm);
191 static void armada_drm_unbind(struct device *dev)
193 struct drm_device *drm = dev_get_drvdata(dev);
194 struct armada_private *priv = drm->dev_private;
196 drm_kms_helper_poll_fini(&priv->drm);
197 armada_fbdev_fini(&priv->drm);
199 drm_dev_unregister(&priv->drm);
201 component_unbind_all(dev, &priv->drm);
203 drm_mode_config_cleanup(&priv->drm);
204 drm_mm_takedown(&priv->linear);
205 flush_work(&priv->fb_unref_work);
207 drm_dev_unref(&priv->drm);
210 static int compare_of(struct device *dev, void *data)
212 return dev->of_node == data;
215 static int compare_dev_name(struct device *dev, void *data)
217 const char *name = data;
218 return !strcmp(dev_name(dev), name);
221 static void armada_add_endpoints(struct device *dev,
222 struct component_match **match, struct device_node *port)
224 struct device_node *ep, *remote;
226 for_each_child_of_node(port, ep) {
227 remote = of_graph_get_remote_port_parent(ep);
228 if (!remote || !of_device_is_available(remote)) {
231 } else if (!of_device_is_available(remote->parent)) {
232 dev_warn(dev, "parent device of %pOF is not available\n",
238 drm_of_component_match_add(dev, match, compare_of, remote);
243 static const struct component_master_ops armada_master_ops = {
244 .bind = armada_drm_bind,
245 .unbind = armada_drm_unbind,
248 static int armada_drm_probe(struct platform_device *pdev)
250 struct component_match *match = NULL;
251 struct device *dev = &pdev->dev;
254 ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
258 if (dev->platform_data) {
259 char **devices = dev->platform_data;
260 struct device_node *port;
264 for (i = 0; devices[i]; i++)
265 component_match_add(dev, &match, compare_dev_name,
269 dev_err(dev, "missing 'ports' property\n");
273 for (i = 0; devices[i]; i++) {
274 d = bus_find_device_by_name(&platform_bus_type, NULL,
276 if (d && d->of_node) {
277 for_each_child_of_node(d->of_node, port)
278 armada_add_endpoints(dev, &match, port);
284 return component_master_add_with_match(&pdev->dev, &armada_master_ops,
288 static int armada_drm_remove(struct platform_device *pdev)
290 component_master_del(&pdev->dev, &armada_master_ops);
294 static const struct platform_device_id armada_drm_platform_ids[] = {
296 .name = "armada-drm",
298 .name = "armada-510-drm",
302 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
304 static struct platform_driver armada_drm_platform_driver = {
305 .probe = armada_drm_probe,
306 .remove = armada_drm_remove,
308 .name = "armada-drm",
310 .id_table = armada_drm_platform_ids,
313 static int __init armada_drm_init(void)
317 armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
319 ret = platform_driver_register(&armada_lcd_platform_driver);
322 ret = platform_driver_register(&armada_drm_platform_driver);
324 platform_driver_unregister(&armada_lcd_platform_driver);
327 module_init(armada_drm_init);
329 static void __exit armada_drm_exit(void)
331 platform_driver_unregister(&armada_drm_platform_driver);
332 platform_driver_unregister(&armada_lcd_platform_driver);
334 module_exit(armada_drm_exit);
336 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
337 MODULE_DESCRIPTION("Armada DRM Driver");
338 MODULE_LICENSE("GPL");
339 MODULE_ALIAS("platform:armada-drm");