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