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>
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"
22 static void armada_drm_unref_work(struct work_struct
*work
)
24 struct armada_private
*priv
=
25 container_of(work
, struct armada_private
, fb_unref_work
);
26 struct drm_framebuffer
*fb
;
28 while (kfifo_get(&priv
->fb_unref
, &fb
))
29 drm_framebuffer_unreference(fb
);
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
)
36 struct armada_private
*priv
= dev
->dev_private
;
38 WARN_ON(!kfifo_put(&priv
->fb_unref
, fb
));
39 schedule_work(&priv
->fb_unref_work
);
42 void armada_drm_queue_unref_work(struct drm_device
*dev
,
43 struct drm_framebuffer
*fb
)
47 spin_lock_irqsave(&dev
->event_lock
, flags
);
48 __armada_drm_queue_unref_work(dev
, fb
);
49 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
52 static int armada_drm_load(struct drm_device
*dev
, unsigned long flags
)
54 struct armada_private
*priv
;
55 struct resource
*mem
= NULL
;
59 struct resource
*r
= platform_get_resource(dev
->platformdev
,
64 /* Resources above 64K are graphics memory */
65 if (resource_size(r
) > SZ_64K
)
74 if (!devm_request_mem_region(dev
->dev
, mem
->start
,
75 resource_size(mem
), "armada-drm"))
78 priv
= devm_kzalloc(dev
->dev
, sizeof(*priv
), GFP_KERNEL
);
80 DRM_ERROR("failed to allocate private\n");
84 platform_set_drvdata(dev
->platformdev
, dev
);
85 dev
->dev_private
= priv
;
87 INIT_WORK(&priv
->fb_unref_work
, armada_drm_unref_work
);
88 INIT_KFIFO(priv
->fb_unref
);
90 /* Mode setting support */
91 drm_mode_config_init(dev
);
92 dev
->mode_config
.min_width
= 320;
93 dev
->mode_config
.min_height
= 200;
96 * With vscale enabled, the maximum width is 1920 due to the
99 dev
->mode_config
.max_width
= 1920;
100 dev
->mode_config
.max_height
= 2048;
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
));
106 ret
= component_bind_all(dev
->dev
, dev
);
110 ret
= drm_vblank_init(dev
, dev
->mode_config
.num_crtc
);
114 dev
->irq_enabled
= true;
115 dev
->vblank_disable_allowed
= 1;
117 ret
= armada_fbdev_init(dev
);
121 drm_kms_helper_poll_init(dev
);
126 component_unbind_all(dev
->dev
, dev
);
128 drm_mode_config_cleanup(dev
);
129 drm_mm_takedown(&priv
->linear
);
130 flush_work(&priv
->fb_unref_work
);
135 static int armada_drm_unload(struct drm_device
*dev
)
137 struct armada_private
*priv
= dev
->dev_private
;
139 drm_kms_helper_poll_fini(dev
);
140 armada_fbdev_fini(dev
);
142 component_unbind_all(dev
->dev
, dev
);
144 drm_mode_config_cleanup(dev
);
145 drm_mm_takedown(&priv
->linear
);
146 flush_work(&priv
->fb_unref_work
);
147 dev
->dev_private
= NULL
;
152 /* These are called under the vbl_lock. */
153 static int armada_drm_enable_vblank(struct drm_device
*dev
, unsigned int pipe
)
155 struct armada_private
*priv
= dev
->dev_private
;
156 armada_drm_crtc_enable_irq(priv
->dcrtc
[pipe
], VSYNC_IRQ_ENA
);
160 static void armada_drm_disable_vblank(struct drm_device
*dev
, unsigned int pipe
)
162 struct armada_private
*priv
= dev
->dev_private
;
163 armada_drm_crtc_disable_irq(priv
->dcrtc
[pipe
], VSYNC_IRQ_ENA
);
166 static struct drm_ioctl_desc armada_ioctls
[] = {
167 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE
, armada_gem_create_ioctl
,0),
168 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP
, armada_gem_mmap_ioctl
, 0),
169 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE
, armada_gem_pwrite_ioctl
, 0),
172 static void armada_drm_lastclose(struct drm_device
*dev
)
174 armada_fbdev_lastclose(dev
);
177 static const struct file_operations armada_drm_fops
= {
178 .owner
= THIS_MODULE
,
182 .unlocked_ioctl
= drm_ioctl
,
183 .mmap
= drm_gem_mmap
,
185 .release
= drm_release
,
188 static struct drm_driver armada_drm_driver
= {
189 .load
= armada_drm_load
,
193 .lastclose
= armada_drm_lastclose
,
194 .unload
= armada_drm_unload
,
195 .set_busid
= drm_platform_set_busid
,
196 .get_vblank_counter
= drm_vblank_no_hw_counter
,
197 .enable_vblank
= armada_drm_enable_vblank
,
198 .disable_vblank
= armada_drm_disable_vblank
,
199 #ifdef CONFIG_DEBUG_FS
200 .debugfs_init
= armada_drm_debugfs_init
,
201 .debugfs_cleanup
= armada_drm_debugfs_cleanup
,
203 .gem_free_object
= armada_gem_free_object
,
204 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
205 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
206 .gem_prime_export
= armada_gem_prime_export
,
207 .gem_prime_import
= armada_gem_prime_import
,
208 .dumb_create
= armada_gem_dumb_create
,
209 .dumb_map_offset
= armada_gem_dumb_map_offset
,
210 .dumb_destroy
= armada_gem_dumb_destroy
,
211 .gem_vm_ops
= &armada_gem_vm_ops
,
214 .name
= "armada-drm",
215 .desc
= "Armada SoC DRM",
217 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
|
218 DRIVER_HAVE_IRQ
| DRIVER_PRIME
,
219 .ioctls
= armada_ioctls
,
220 .fops
= &armada_drm_fops
,
223 static int armada_drm_bind(struct device
*dev
)
225 return drm_platform_init(&armada_drm_driver
, to_platform_device(dev
));
228 static void armada_drm_unbind(struct device
*dev
)
230 drm_put_dev(dev_get_drvdata(dev
));
233 static int compare_of(struct device
*dev
, void *data
)
235 return dev
->of_node
== data
;
238 static int compare_dev_name(struct device
*dev
, void *data
)
240 const char *name
= data
;
241 return !strcmp(dev_name(dev
), name
);
244 static void armada_add_endpoints(struct device
*dev
,
245 struct component_match
**match
, struct device_node
*port
)
247 struct device_node
*ep
, *remote
;
249 for_each_child_of_node(port
, ep
) {
250 remote
= of_graph_get_remote_port_parent(ep
);
251 if (!remote
|| !of_device_is_available(remote
)) {
254 } else if (!of_device_is_available(remote
->parent
)) {
255 dev_warn(dev
, "parent device of %s is not available\n",
261 component_match_add(dev
, match
, compare_of
, remote
);
266 static const struct component_master_ops armada_master_ops
= {
267 .bind
= armada_drm_bind
,
268 .unbind
= armada_drm_unbind
,
271 static int armada_drm_probe(struct platform_device
*pdev
)
273 struct component_match
*match
= NULL
;
274 struct device
*dev
= &pdev
->dev
;
277 ret
= drm_of_component_probe(dev
, compare_dev_name
, &armada_master_ops
);
281 if (dev
->platform_data
) {
282 char **devices
= dev
->platform_data
;
283 struct device_node
*port
;
287 for (i
= 0; devices
[i
]; i
++)
288 component_match_add(dev
, &match
, compare_dev_name
,
292 dev_err(dev
, "missing 'ports' property\n");
296 for (i
= 0; devices
[i
]; i
++) {
297 d
= bus_find_device_by_name(&platform_bus_type
, NULL
,
299 if (d
&& d
->of_node
) {
300 for_each_child_of_node(d
->of_node
, port
)
301 armada_add_endpoints(dev
, &match
, port
);
307 return component_master_add_with_match(&pdev
->dev
, &armada_master_ops
,
311 static int armada_drm_remove(struct platform_device
*pdev
)
313 component_master_del(&pdev
->dev
, &armada_master_ops
);
317 static const struct platform_device_id armada_drm_platform_ids
[] = {
319 .name
= "armada-drm",
321 .name
= "armada-510-drm",
325 MODULE_DEVICE_TABLE(platform
, armada_drm_platform_ids
);
327 static struct platform_driver armada_drm_platform_driver
= {
328 .probe
= armada_drm_probe
,
329 .remove
= armada_drm_remove
,
331 .name
= "armada-drm",
333 .id_table
= armada_drm_platform_ids
,
336 static int __init
armada_drm_init(void)
340 armada_drm_driver
.num_ioctls
= ARRAY_SIZE(armada_ioctls
);
342 ret
= platform_driver_register(&armada_lcd_platform_driver
);
345 ret
= platform_driver_register(&armada_drm_platform_driver
);
347 platform_driver_unregister(&armada_lcd_platform_driver
);
350 module_init(armada_drm_init
);
352 static void __exit
armada_drm_exit(void)
354 platform_driver_unregister(&armada_drm_platform_driver
);
355 platform_driver_unregister(&armada_lcd_platform_driver
);
357 module_exit(armada_drm_exit
);
359 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
360 MODULE_DESCRIPTION("Armada DRM Driver");
361 MODULE_LICENSE("GPL");
362 MODULE_ALIAS("platform:armada-drm");