Merge tag 'for-5.1-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave...
[sfrench/cifs-2.6.git] / drivers / gpu / drm / armada / armada_drv.c
1 /*
2  * Copyright (C) 2012 Russell King
3  *
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.
7  */
8 #include <linux/clk.h>
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"
23
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),
28 };
29
30 DEFINE_DRM_GEM_FOPS(armada_drm_fops);
31
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,
41         .major                  = 1,
42         .minor                  = 0,
43         .name                   = "armada-drm",
44         .desc                   = "Armada SoC DRM",
45         .date                   = "20120730",
46         .driver_features        = DRIVER_GEM | DRIVER_MODESET |
47                                   DRIVER_PRIME | DRIVER_ATOMIC,
48         .ioctls                 = armada_ioctls,
49         .fops                   = &armada_drm_fops,
50 };
51
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,
57 };
58
59 static int armada_drm_bind(struct device *dev)
60 {
61         struct armada_private *priv;
62         struct resource *mem = NULL;
63         int ret, n;
64
65         for (n = 0; ; n++) {
66                 struct resource *r = platform_get_resource(to_platform_device(dev),
67                                                            IORESOURCE_MEM, n);
68                 if (!r)
69                         break;
70
71                 /* Resources above 64K are graphics memory */
72                 if (resource_size(r) > SZ_64K)
73                         mem = r;
74                 else
75                         return -EINVAL;
76         }
77
78         if (!mem)
79                 return -ENXIO;
80
81         if (!devm_request_mem_region(dev, mem->start, resource_size(mem),
82                                      "armada-drm"))
83                 return -EBUSY;
84
85         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
86         if (!priv)
87                 return -ENOMEM;
88
89         /*
90          * The drm_device structure must be at the start of
91          * armada_private for drm_dev_put() to work correctly.
92          */
93         BUILD_BUG_ON(offsetof(struct armada_private, drm) != 0);
94
95         ret = drm_dev_init(&priv->drm, &armada_drm_driver, dev);
96         if (ret) {
97                 dev_err(dev, "[" DRM_NAME ":%s] drm_dev_init failed: %d\n",
98                         __func__, ret);
99                 kfree(priv);
100                 return ret;
101         }
102
103         priv->drm.dev_private = priv;
104
105         dev_set_drvdata(dev, &priv->drm);
106
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;
111
112         /*
113          * With vscale enabled, the maximum width is 1920 due to the
114          * 1920 by 3 lines RAM
115          */
116         priv->drm.mode_config.max_width = 1920;
117         priv->drm.mode_config.max_height = 2048;
118
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);
123
124         ret = component_bind_all(dev, &priv->drm);
125         if (ret)
126                 goto err_kms;
127
128         ret = drm_vblank_init(&priv->drm, priv->drm.mode_config.num_crtc);
129         if (ret)
130                 goto err_comp;
131
132         priv->drm.irq_enabled = true;
133
134         drm_mode_config_reset(&priv->drm);
135
136         ret = armada_fbdev_init(&priv->drm);
137         if (ret)
138                 goto err_comp;
139
140         drm_kms_helper_poll_init(&priv->drm);
141
142         ret = drm_dev_register(&priv->drm, 0);
143         if (ret)
144                 goto err_poll;
145
146 #ifdef CONFIG_DEBUG_FS
147         armada_drm_debugfs_init(priv->drm.primary);
148 #endif
149
150         return 0;
151
152  err_poll:
153         drm_kms_helper_poll_fini(&priv->drm);
154         armada_fbdev_fini(&priv->drm);
155  err_comp:
156         component_unbind_all(dev, &priv->drm);
157  err_kms:
158         drm_mode_config_cleanup(&priv->drm);
159         drm_mm_takedown(&priv->linear);
160         drm_dev_put(&priv->drm);
161         return ret;
162 }
163
164 static void armada_drm_unbind(struct device *dev)
165 {
166         struct drm_device *drm = dev_get_drvdata(dev);
167         struct armada_private *priv = drm->dev_private;
168
169         drm_kms_helper_poll_fini(&priv->drm);
170         armada_fbdev_fini(&priv->drm);
171
172         drm_dev_unregister(&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         drm_dev_put(&priv->drm);
180 }
181
182 static int compare_of(struct device *dev, void *data)
183 {
184         return dev->of_node == data;
185 }
186
187 static int compare_dev_name(struct device *dev, void *data)
188 {
189         const char *name = data;
190         return !strcmp(dev_name(dev), name);
191 }
192
193 static void armada_add_endpoints(struct device *dev,
194         struct component_match **match, struct device_node *port)
195 {
196         struct device_node *ep, *remote;
197
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)) {
201                         of_node_put(remote);
202                         continue;
203                 } else if (!of_device_is_available(remote->parent)) {
204                         dev_warn(dev, "parent device of %pOF is not available\n",
205                                  remote);
206                         of_node_put(remote);
207                         continue;
208                 }
209
210                 drm_of_component_match_add(dev, match, compare_of, remote);
211                 of_node_put(remote);
212         }
213 }
214
215 static const struct component_master_ops armada_master_ops = {
216         .bind = armada_drm_bind,
217         .unbind = armada_drm_unbind,
218 };
219
220 static int armada_drm_probe(struct platform_device *pdev)
221 {
222         struct component_match *match = NULL;
223         struct device *dev = &pdev->dev;
224         int ret;
225
226         ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
227         if (ret != -EINVAL)
228                 return ret;
229
230         if (dev->platform_data) {
231                 char **devices = dev->platform_data;
232                 struct device_node *port;
233                 struct device *d;
234                 int i;
235
236                 for (i = 0; devices[i]; i++)
237                         component_match_add(dev, &match, compare_dev_name,
238                                             devices[i]);
239
240                 if (i == 0) {
241                         dev_err(dev, "missing 'ports' property\n");
242                         return -ENODEV;
243                 }
244
245                 for (i = 0; devices[i]; i++) {
246                         d = bus_find_device_by_name(&platform_bus_type, NULL,
247                                                     devices[i]);
248                         if (d && d->of_node) {
249                                 for_each_child_of_node(d->of_node, port)
250                                         armada_add_endpoints(dev, &match, port);
251                         }
252                         put_device(d);
253                 }
254         }
255
256         return component_master_add_with_match(&pdev->dev, &armada_master_ops,
257                                                match);
258 }
259
260 static int armada_drm_remove(struct platform_device *pdev)
261 {
262         component_master_del(&pdev->dev, &armada_master_ops);
263         return 0;
264 }
265
266 static const struct platform_device_id armada_drm_platform_ids[] = {
267         {
268                 .name           = "armada-drm",
269         }, {
270                 .name           = "armada-510-drm",
271         },
272         { },
273 };
274 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
275
276 static struct platform_driver armada_drm_platform_driver = {
277         .probe  = armada_drm_probe,
278         .remove = armada_drm_remove,
279         .driver = {
280                 .name   = "armada-drm",
281         },
282         .id_table = armada_drm_platform_ids,
283 };
284
285 static int __init armada_drm_init(void)
286 {
287         int ret;
288
289         armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
290
291         ret = platform_driver_register(&armada_lcd_platform_driver);
292         if (ret)
293                 return ret;
294         ret = platform_driver_register(&armada_drm_platform_driver);
295         if (ret)
296                 platform_driver_unregister(&armada_lcd_platform_driver);
297         return ret;
298 }
299 module_init(armada_drm_init);
300
301 static void __exit armada_drm_exit(void)
302 {
303         platform_driver_unregister(&armada_drm_platform_driver);
304         platform_driver_unregister(&armada_lcd_platform_driver);
305 }
306 module_exit(armada_drm_exit);
307
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");