1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2012 Russell King
7 #include <linux/component.h>
8 #include <linux/module.h>
9 #include <linux/of_graph.h>
10 #include <linux/platform_device.h>
12 #include <drm/drm_atomic_helper.h>
13 #include <drm/drm_drv.h>
14 #include <drm/drm_ioctl.h>
15 #include <drm/drm_managed.h>
16 #include <drm/drm_prime.h>
17 #include <drm/drm_probe_helper.h>
18 #include <drm/drm_fb_helper.h>
19 #include <drm/drm_of.h>
20 #include <drm/drm_vblank.h>
22 #include "armada_crtc.h"
23 #include "armada_drm.h"
24 #include "armada_gem.h"
25 #include "armada_fb.h"
26 #include "armada_hw.h"
27 #include <drm/armada_drm.h>
28 #include "armada_ioctlP.h"
30 static struct drm_ioctl_desc armada_ioctls[] = {
31 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
32 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
33 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
36 DEFINE_DRM_GEM_FOPS(armada_drm_fops);
38 static struct drm_driver armada_drm_driver = {
39 .lastclose = drm_fb_helper_lastclose,
40 .gem_free_object_unlocked = armada_gem_free_object,
41 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
42 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
43 .gem_prime_export = armada_gem_prime_export,
44 .gem_prime_import = armada_gem_prime_import,
45 .dumb_create = armada_gem_dumb_create,
46 .gem_vm_ops = &armada_gem_vm_ops,
50 .desc = "Armada SoC DRM",
52 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
53 .ioctls = armada_ioctls,
54 .fops = &armada_drm_fops,
57 static const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
58 .fb_create = armada_fb_create,
59 .output_poll_changed = drm_fb_helper_output_poll_changed,
60 .atomic_check = drm_atomic_helper_check,
61 .atomic_commit = drm_atomic_helper_commit,
64 static int armada_drm_bind(struct device *dev)
66 struct armada_private *priv;
67 struct resource *mem = NULL;
71 struct resource *r = platform_get_resource(to_platform_device(dev),
76 /* Resources above 64K are graphics memory */
77 if (resource_size(r) > SZ_64K)
86 if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
90 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
95 * The drm_device structure must be at the start of
96 * armada_private for drm_dev_put() to work correctly.
98 BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
100 ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
102 dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
107 drmm_add_final_kfree(&priv->drm, priv);
109 /* Remove early framebuffers */
110 ret = drm_fb_helper_remove_conflicting_framebuffers(NULL,
114 dev_err(dev, "[" DRM_NAME ":%s] can't kick out simple-fb: %d\n",
120 priv->drm.dev_private = priv;
122 dev_set_drvdata(dev, &priv->drm);
124 /* Mode setting support */
125 drm_mode_config_init(&priv->drm);
126 priv->drm.mode_config.min_width = 320;
127 priv->drm.mode_config.min_height = 200;
130 * With vscale enabled, the maximum width is 1920 due to the
131 * 1920 by 3 lines RAM
133 priv->drm.mode_config.max_width = 1920;
134 priv->drm.mode_config.max_height = 2048;
136 priv->drm.mode_config.preferred_depth = 24;
137 priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
138 drm_mm_init(&priv->linear, mem->start, resource_size(mem));
139 mutex_init(&priv->linear_lock);
141 ret = component_bind_all(dev, &priv->drm);
145 ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
149 priv->drm.irq_enabled = true;
151 drm_mode_config_reset(&priv->drm);
153 ret = armada_fbdev_init(&priv->drm);
157 drm_kms_helper_poll_init(&priv->drm);
159 ret = drm_dev_register(&priv->drm, 0);
163 #ifdef CONFIG_DEBUG_FS
164 armada_drm_debugfs_init(priv->drm.primary);
170 drm_kms_helper_poll_fini(&priv->drm);
171 armada_fbdev_fini(&priv->drm);
173 component_unbind_all(dev, &priv->drm);
175 drm_mode_config_cleanup(&priv->drm);
176 drm_mm_takedown(&priv->linear);
177 drm_dev_put(&priv->drm);
181 static void armada_drm_unbind(struct device *dev)
183 struct drm_device *drm = dev_get_drvdata(dev);
184 struct armada_private *priv = drm->dev_private;
186 drm_kms_helper_poll_fini(&priv->drm);
187 armada_fbdev_fini(&priv->drm);
189 drm_dev_unregister(&priv->drm);
191 drm_atomic_helper_shutdown(&priv->drm);
193 component_unbind_all(dev, &priv->drm);
195 drm_mode_config_cleanup(&priv->drm);
196 drm_mm_takedown(&priv->linear);
198 drm_dev_put(&priv->drm);
201 static int compare_of(struct device *dev, void *data)
203 return dev->of_node == data;
206 static int compare_dev_name(struct device *dev, void *data)
208 const char *name = data;
209 return !strcmp(dev_name(dev), name);
212 static void armada_add_endpoints(struct device *dev,
213 struct component_match **match, struct device_node *dev_node)
215 struct device_node *ep, *remote;
217 for_each_endpoint_of_node(dev_node, ep) {
218 remote = of_graph_get_remote_port_parent(ep);
219 if (remote && of_device_is_available(remote))
220 drm_of_component_match_add(dev, match, compare_of,
226 static const struct component_master_ops armada_master_ops = {
227 .bind = armada_drm_bind,
228 .unbind = armada_drm_unbind,
231 static int armada_drm_probe(struct platform_device *pdev)
233 struct component_match *match = NULL;
234 struct device *dev = &pdev->dev;
237 ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
241 if (dev->platform_data) {
242 char **devices = dev->platform_data;
246 for (i = 0; devices[i]; i++)
247 component_match_add(dev, &match, compare_dev_name,
251 dev_err(dev, "missing 'ports' property\n");
255 for (i = 0; devices[i]; i++) {
256 d = bus_find_device_by_name(&platform_bus_type, NULL,
259 armada_add_endpoints(dev, &match, d->of_node);
264 return component_master_add_with_match(&pdev->dev, &armada_master_ops,
268 static int armada_drm_remove(struct platform_device *pdev)
270 component_master_del(&pdev->dev, &armada_master_ops);
274 static const struct platform_device_id armada_drm_platform_ids[] = {
276 .name = "armada-drm",
278 .name = "armada-510-drm",
282 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
284 static struct platform_driver armada_drm_platform_driver = {
285 .probe = armada_drm_probe,
286 .remove = armada_drm_remove,
288 .name = "armada-drm",
290 .id_table = armada_drm_platform_ids,
293 static int __init armada_drm_init(void)
297 armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
299 ret = platform_driver_register(&armada_lcd_platform_driver);
302 ret = platform_driver_register(&armada_drm_platform_driver);
304 platform_driver_unregister(&armada_lcd_platform_driver);
307 module_init(armada_drm_init);
309 static void __exit armada_drm_exit(void)
311 platform_driver_unregister(&armada_drm_platform_driver);
312 platform_driver_unregister(&armada_lcd_platform_driver);
314 module_exit(armada_drm_exit);
317 MODULE_DESCRIPTION("Armada DRM Driver");
318 MODULE_LICENSE("GPL");
319 MODULE_ALIAS("platform:armada-drm");