1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2012 Russell King
4 */
5
6 #include <linux/clk.h>
7 #include <linux/component.h>
8 #include <linux/module.h>
9 #include <linux/of_graph.h>
10 #include <linux/platform_device.h>
11
12 #include <drm/drm_aperture.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_ioctl.h>
16 #include <drm/drm_managed.h>
17 #include <drm/drm_prime.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_of.h>
21 #include <drm/drm_vblank.h>
22
23 #include "armada_crtc.h"
24 #include "armada_drm.h"
25 #include "armada_gem.h"
26 #include "armada_fb.h"
27 #include "armada_hw.h"
28 #include <drm/armada_drm.h>
29 #include "armada_ioctlP.h"
30
31 static const struct drm_ioctl_desc armada_ioctls[] = {
32 DRM_IOCTL_DEF_DRV(ARMADA_GEM_CREATE, armada_gem_create_ioctl,0),
33 DRM_IOCTL_DEF_DRV(ARMADA_GEM_MMAP, armada_gem_mmap_ioctl, 0),
34 DRM_IOCTL_DEF_DRV(ARMADA_GEM_PWRITE, armada_gem_pwrite_ioctl, 0),
35 };
36
37 DEFINE_DRM_GEM_FOPS(armada_drm_fops);
38
39 static const struct drm_driver armada_drm_driver = {
40 .lastclose = drm_fb_helper_lastclose,
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_import = armada_gem_prime_import,
44 .dumb_create = armada_gem_dumb_create,
45 .major = 1,
46 .minor = 0,
47 .name = "armada-drm",
48 .desc = "Armada SoC DRM",
49 .date = "20120730",
50 .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
51 .ioctls = armada_ioctls,
52 .num_ioctls = ARRAY_SIZE(armada_ioctls),
53 .fops = &armada_drm_fops,
54 };
55
56 static const struct drm_mode_config_funcs armada_drm_mode_config_funcs = {
57 .fb_create = armada_fb_create,
58 .output_poll_changed = drm_fb_helper_output_poll_changed,
59 .atomic_check = drm_atomic_helper_check,
60 .atomic_commit = drm_atomic_helper_commit,
61 };
62
armada_drm_bind(struct device * dev)63 static int armada_drm_bind(struct device *dev)
64 {
65 struct armada_private *priv;
66 struct resource *mem = NULL;
67 int ret, n;
68
69 for (n = 0; ; n++) {
70 struct resource *r = platform_get_resource(to_platform_device(dev),
71 IORESOURCE_MEM, n);
72 if (!r)
73 break;
74
75 /* Resources above 64K are graphics memory */
76 if (resource_size(r) > SZ_64K)
77 mem = r;
78 else
79 return -EINVAL;
80 }
81
82 if (!mem)
83 return -ENXIO;
84
85 if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
86 "armada-drm"))
87 return -EBUSY;
88
89 priv = devm_drm_dev_alloc(dev, &armada_drm_driver,
90 struct armada_private, drm);
91 if (IS_ERR(priv)) {
92 dev_err(dev, "[" DRM_NAME ":%s] devm_drm_dev_alloc failed: %li\n",
93 __func__, PTR_ERR(priv));
94 return PTR_ERR(priv);
95 }
96
97 /* Remove early framebuffers */
98 ret = drm_aperture_remove_framebuffers(false, &armada_drm_driver);
99 if (ret) {
100 dev_err(dev, "[" DRM_NAME ":%s] can't kick out simple-fb: %d\n",
101 __func__, ret);
102 kfree(priv);
103 return ret;
104 }
105
106 dev_set_drvdata(dev, &priv->drm);
107
108 /* Mode setting support */
109 drm_mode_config_init(&priv->drm);
110 priv->drm.mode_config.min_width = 320;
111 priv->drm.mode_config.min_height = 200;
112
113 /*
114 * With vscale enabled, the maximum width is 1920 due to the
115 * 1920 by 3 lines RAM
116 */
117 priv->drm.mode_config.max_width = 1920;
118 priv->drm.mode_config.max_height = 2048;
119
120 priv->drm.mode_config.preferred_depth = 24;
121 priv->drm.mode_config.funcs = &armada_drm_mode_config_funcs;
122 drm_mm_init(&priv->linear, mem->start, resource_size(mem));
123 mutex_init(&priv->linear_lock);
124
125 ret = component_bind_all(dev, &priv->drm);
126 if (ret)
127 goto err_kms;
128
129 ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
130 if (ret)
131 goto err_comp;
132
133 drm_mode_config_reset(&priv->drm);
134
135 ret = armada_fbdev_init(&priv->drm);
136 if (ret)
137 goto err_comp;
138
139 drm_kms_helper_poll_init(&priv->drm);
140
141 ret = drm_dev_register(&priv->drm, 0);
142 if (ret)
143 goto err_poll;
144
145 #ifdef CONFIG_DEBUG_FS
146 armada_drm_debugfs_init(priv->drm.primary);
147 #endif
148
149 return 0;
150
151 err_poll:
152 drm_kms_helper_poll_fini(&priv->drm);
153 armada_fbdev_fini(&priv->drm);
154 err_comp:
155 component_unbind_all(dev, &priv->drm);
156 err_kms:
157 drm_mode_config_cleanup(&priv->drm);
158 drm_mm_takedown(&priv->linear);
159 return ret;
160 }
161
armada_drm_unbind(struct device * dev)162 static void armada_drm_unbind(struct device *dev)
163 {
164 struct drm_device *drm = dev_get_drvdata(dev);
165 struct armada_private *priv = drm_to_armada_dev(drm);
166
167 drm_kms_helper_poll_fini(&priv->drm);
168 armada_fbdev_fini(&priv->drm);
169
170 drm_dev_unregister(&priv->drm);
171
172 drm_atomic_helper_shutdown(&priv->drm);
173
174 component_unbind_all(dev, &priv->drm);
175
176 drm_mode_config_cleanup(&priv->drm);
177 drm_mm_takedown(&priv->linear);
178 }
179
compare_of(struct device * dev,void * data)180 static int compare_of(struct device *dev, void *data)
181 {
182 return dev->of_node == data;
183 }
184
compare_dev_name(struct device * dev,void * data)185 static int compare_dev_name(struct device *dev, void *data)
186 {
187 const char *name = data;
188 return !strcmp(dev_name(dev), name);
189 }
190
armada_add_endpoints(struct device * dev,struct component_match ** match,struct device_node * dev_node)191 static void armada_add_endpoints(struct device *dev,
192 struct component_match **match, struct device_node *dev_node)
193 {
194 struct device_node *ep, *remote;
195
196 for_each_endpoint_of_node(dev_node, ep) {
197 remote = of_graph_get_remote_port_parent(ep);
198 if (remote && of_device_is_available(remote))
199 drm_of_component_match_add(dev, match, compare_of,
200 remote);
201 of_node_put(remote);
202 }
203 }
204
205 static const struct component_master_ops armada_master_ops = {
206 .bind = armada_drm_bind,
207 .unbind = armada_drm_unbind,
208 };
209
armada_drm_probe(struct platform_device * pdev)210 static int armada_drm_probe(struct platform_device *pdev)
211 {
212 struct component_match *match = NULL;
213 struct device *dev = &pdev->dev;
214 int ret;
215
216 ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
217 if (ret != -EINVAL)
218 return ret;
219
220 if (dev->platform_data) {
221 char **devices = dev->platform_data;
222 struct device *d;
223 int i;
224
225 for (i = 0; devices[i]; i++)
226 component_match_add(dev, &match, compare_dev_name,
227 devices[i]);
228
229 if (i == 0) {
230 dev_err(dev, "missing 'ports' property\n");
231 return -ENODEV;
232 }
233
234 for (i = 0; devices[i]; i++) {
235 d = bus_find_device_by_name(&platform_bus_type, NULL,
236 devices[i]);
237 if (d && d->of_node)
238 armada_add_endpoints(dev, &match, d->of_node);
239 put_device(d);
240 }
241 }
242
243 return component_master_add_with_match(&pdev->dev, &armada_master_ops,
244 match);
245 }
246
armada_drm_remove(struct platform_device * pdev)247 static int armada_drm_remove(struct platform_device *pdev)
248 {
249 component_master_del(&pdev->dev, &armada_master_ops);
250 return 0;
251 }
252
253 static const struct platform_device_id armada_drm_platform_ids[] = {
254 {
255 .name = "armada-drm",
256 }, {
257 .name = "armada-510-drm",
258 },
259 { },
260 };
261 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
262
263 static struct platform_driver armada_drm_platform_driver = {
264 .probe = armada_drm_probe,
265 .remove = armada_drm_remove,
266 .driver = {
267 .name = "armada-drm",
268 },
269 .id_table = armada_drm_platform_ids,
270 };
271
armada_drm_init(void)272 static int __init armada_drm_init(void)
273 {
274 int ret;
275
276 ret = platform_driver_register(&armada_lcd_platform_driver);
277 if (ret)
278 return ret;
279 ret = platform_driver_register(&armada_drm_platform_driver);
280 if (ret)
281 platform_driver_unregister(&armada_lcd_platform_driver);
282 return ret;
283 }
284 module_init(armada_drm_init);
285
armada_drm_exit(void)286 static void __exit armada_drm_exit(void)
287 {
288 platform_driver_unregister(&armada_drm_platform_driver);
289 platform_driver_unregister(&armada_lcd_platform_driver);
290 }
291 module_exit(armada_drm_exit);
292
293 MODULE_AUTHOR("Russell King <rmk+kernel@armlinux.org.uk>");
294 MODULE_DESCRIPTION("Armada DRM Driver");
295 MODULE_LICENSE("GPL");
296 MODULE_ALIAS("platform:armada-drm");
297