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_atomic_helper.h>
13 #include <drm/drm_probe_helper.h>
14 #include <drm/drm_fb_helper.h>
15 #include <drm/drm_of.h>
16 #include "armada_crtc.h"
17 #include "armada_drm.h"
18 #include "armada_gem.h"
19 #include "armada_fb.h"
20 #include "armada_hw.h"
21 #include <drm/armada_drm.h>
22 #include "armada_ioctlP.h"
24 static struct drm_ioctl_desc armada_ioctls
[] = {
25 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE
, armada_gem_create_ioctl
,0),
26 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP
, armada_gem_mmap_ioctl
, 0),
27 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE
, armada_gem_pwrite_ioctl
, 0),
30 DEFINE_DRM_GEM_FOPS(armada_drm_fops
);
32 static struct drm_driver armada_drm_driver
= {
33 .lastclose
= drm_fb_helper_lastclose
,
34 .gem_free_object_unlocked
= armada_gem_free_object
,
35 .prime_handle_to_fd
= drm_gem_prime_handle_to_fd
,
36 .prime_fd_to_handle
= drm_gem_prime_fd_to_handle
,
37 .gem_prime_export
= armada_gem_prime_export
,
38 .gem_prime_import
= armada_gem_prime_import
,
39 .dumb_create
= armada_gem_dumb_create
,
40 .gem_vm_ops
= &armada_gem_vm_ops
,
44 .desc
= "Armada SoC DRM",
46 .driver_features
= DRIVER_GEM
| DRIVER_MODESET
|
47 DRIVER_PRIME
| DRIVER_ATOMIC
,
48 .ioctls
= armada_ioctls
,
49 .fops
= &armada_drm_fops
,
52 static const struct drm_mode_config_funcs armada_drm_mode_config_funcs
= {
53 .fb_create
= armada_fb_create
,
54 .output_poll_changed
= drm_fb_helper_output_poll_changed
,
55 .atomic_check
= drm_atomic_helper_check
,
56 .atomic_commit
= drm_atomic_helper_commit
,
59 static int armada_drm_bind(struct device
*dev
)
61 struct armada_private
*priv
;
62 struct resource
*mem
= NULL
;
66 struct resource
*r
= platform_get_resource(to_platform_device(dev
),
71 /* Resources above 64K are graphics memory */
72 if (resource_size(r
) > SZ_64K
)
81 if (!devm_request_mem_region(dev
, mem
->start
, resource_size(mem
),
85 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
90 * The drm_device structure must be at the start of
91 * armada_private for drm_dev_put() to work correctly.
93 BUILD_BUG_ON(offsetof(struct armada_private
, drm
) != 0);
95 ret
= drm_dev_init(&priv
->drm
, &armada_drm_driver
, dev
);
97 dev_err(dev
, "[" DRM_NAME
":%s] drm_dev_init failed: %d\n",
103 priv
->drm
.dev_private
= priv
;
105 dev_set_drvdata(dev
, &priv
->drm
);
107 /* Mode setting support */
108 drm_mode_config_init(&priv
->drm
);
109 priv
->drm
.mode_config
.min_width
= 320;
110 priv
->drm
.mode_config
.min_height
= 200;
113 * With vscale enabled, the maximum width is 1920 due to the
114 * 1920 by 3 lines RAM
116 priv
->drm
.mode_config
.max_width
= 1920;
117 priv
->drm
.mode_config
.max_height
= 2048;
119 priv
->drm
.mode_config
.preferred_depth
= 24;
120 priv
->drm
.mode_config
.funcs
= &armada_drm_mode_config_funcs
;
121 drm_mm_init(&priv
->linear
, mem
->start
, resource_size(mem
));
122 mutex_init(&priv
->linear_lock
);
124 ret
= component_bind_all(dev
, &priv
->drm
);
128 ret
= drm_vblank_init(&priv
->drm
, priv
->drm
.mode_config
.num_crtc
);
132 priv
->drm
.irq_enabled
= true;
134 drm_mode_config_reset(&priv
->drm
);
136 ret
= armada_fbdev_init(&priv
->drm
);
140 drm_kms_helper_poll_init(&priv
->drm
);
142 ret
= drm_dev_register(&priv
->drm
, 0);
146 #ifdef CONFIG_DEBUG_FS
147 armada_drm_debugfs_init(priv
->drm
.primary
);
153 drm_kms_helper_poll_fini(&priv
->drm
);
154 armada_fbdev_fini(&priv
->drm
);
156 component_unbind_all(dev
, &priv
->drm
);
158 drm_mode_config_cleanup(&priv
->drm
);
159 drm_mm_takedown(&priv
->linear
);
160 drm_dev_put(&priv
->drm
);
164 static void armada_drm_unbind(struct device
*dev
)
166 struct drm_device
*drm
= dev_get_drvdata(dev
);
167 struct armada_private
*priv
= drm
->dev_private
;
169 drm_kms_helper_poll_fini(&priv
->drm
);
170 armada_fbdev_fini(&priv
->drm
);
172 drm_dev_unregister(&priv
->drm
);
174 component_unbind_all(dev
, &priv
->drm
);
176 drm_mode_config_cleanup(&priv
->drm
);
177 drm_mm_takedown(&priv
->linear
);
179 drm_dev_put(&priv
->drm
);
182 static int compare_of(struct device
*dev
, void *data
)
184 return dev
->of_node
== data
;
187 static int compare_dev_name(struct device
*dev
, void *data
)
189 const char *name
= data
;
190 return !strcmp(dev_name(dev
), name
);
193 static void armada_add_endpoints(struct device
*dev
,
194 struct component_match
**match
, struct device_node
*port
)
196 struct device_node
*ep
, *remote
;
198 for_each_child_of_node(port
, ep
) {
199 remote
= of_graph_get_remote_port_parent(ep
);
200 if (!remote
|| !of_device_is_available(remote
)) {
203 } else if (!of_device_is_available(remote
->parent
)) {
204 dev_warn(dev
, "parent device of %pOF is not available\n",
210 drm_of_component_match_add(dev
, match
, compare_of
, remote
);
215 static const struct component_master_ops armada_master_ops
= {
216 .bind
= armada_drm_bind
,
217 .unbind
= armada_drm_unbind
,
220 static int armada_drm_probe(struct platform_device
*pdev
)
222 struct component_match
*match
= NULL
;
223 struct device
*dev
= &pdev
->dev
;
226 ret
= drm_of_component_probe(dev
, compare_dev_name
, &armada_master_ops
);
230 if (dev
->platform_data
) {
231 char **devices
= dev
->platform_data
;
232 struct device_node
*port
;
236 for (i
= 0; devices
[i
]; i
++)
237 component_match_add(dev
, &match
, compare_dev_name
,
241 dev_err(dev
, "missing 'ports' property\n");
245 for (i
= 0; devices
[i
]; i
++) {
246 d
= bus_find_device_by_name(&platform_bus_type
, NULL
,
248 if (d
&& d
->of_node
) {
249 for_each_child_of_node(d
->of_node
, port
)
250 armada_add_endpoints(dev
, &match
, port
);
256 return component_master_add_with_match(&pdev
->dev
, &armada_master_ops
,
260 static int armada_drm_remove(struct platform_device
*pdev
)
262 component_master_del(&pdev
->dev
, &armada_master_ops
);
266 static const struct platform_device_id armada_drm_platform_ids
[] = {
268 .name
= "armada-drm",
270 .name
= "armada-510-drm",
274 MODULE_DEVICE_TABLE(platform
, armada_drm_platform_ids
);
276 static struct platform_driver armada_drm_platform_driver
= {
277 .probe
= armada_drm_probe
,
278 .remove
= armada_drm_remove
,
280 .name
= "armada-drm",
282 .id_table
= armada_drm_platform_ids
,
285 static int __init
armada_drm_init(void)
289 armada_drm_driver
.num_ioctls
= ARRAY_SIZE(armada_ioctls
);
291 ret
= platform_driver_register(&armada_lcd_platform_driver
);
294 ret
= platform_driver_register(&armada_drm_platform_driver
);
296 platform_driver_unregister(&armada_lcd_platform_driver
);
299 module_init(armada_drm_init
);
301 static void __exit
armada_drm_exit(void)
303 platform_driver_unregister(&armada_drm_platform_driver
);
304 platform_driver_unregister(&armada_lcd_platform_driver
);
306 module_exit(armada_drm_exit
);
308 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
309 MODULE_DESCRIPTION("Armada DRM Driver");
310 MODULE_LICENSE("GPL");
311 MODULE_ALIAS("platform:armada-drm");