e524bfff1c49ebd557f6f9fa889de3915c848807
[linux-2.6-microblaze.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         drm_atomic_helper_shutdown(&priv->drm);
175
176         component_unbind_all(dev, &priv->drm);
177
178         drm_mode_config_cleanup(&priv->drm);
179         drm_mm_takedown(&priv->linear);
180
181         drm_dev_put(&priv->drm);
182 }
183
184 static int compare_of(struct device *dev, void *data)
185 {
186         return dev->of_node == data;
187 }
188
189 static int compare_dev_name(struct device *dev, void *data)
190 {
191         const char *name = data;
192         return !strcmp(dev_name(dev), name);
193 }
194
195 static void armada_add_endpoints(struct device *dev,
196         struct component_match **match, struct device_node *port)
197 {
198         struct device_node *ep, *remote;
199
200         for_each_child_of_node(port, ep) {
201                 remote = of_graph_get_remote_port_parent(ep);
202                 if (!remote || !of_device_is_available(remote)) {
203                         of_node_put(remote);
204                         continue;
205                 } else if (!of_device_is_available(remote->parent)) {
206                         dev_warn(dev, "parent device of %pOF is not available\n",
207                                  remote);
208                         of_node_put(remote);
209                         continue;
210                 }
211
212                 drm_of_component_match_add(dev, match, compare_of, remote);
213                 of_node_put(remote);
214         }
215 }
216
217 static const struct component_master_ops armada_master_ops = {
218         .bind = armada_drm_bind,
219         .unbind = armada_drm_unbind,
220 };
221
222 static int armada_drm_probe(struct platform_device *pdev)
223 {
224         struct component_match *match = NULL;
225         struct device *dev = &pdev->dev;
226         int ret;
227
228         ret = drm_of_component_probe(dev, compare_dev_name, &armada_master_ops);
229         if (ret != -EINVAL)
230                 return ret;
231
232         if (dev->platform_data) {
233                 char **devices = dev->platform_data;
234                 struct device_node *port;
235                 struct device *d;
236                 int i;
237
238                 for (i = 0; devices[i]; i++)
239                         component_match_add(dev, &match, compare_dev_name,
240                                             devices[i]);
241
242                 if (i == 0) {
243                         dev_err(dev, "missing 'ports' property\n");
244                         return -ENODEV;
245                 }
246
247                 for (i = 0; devices[i]; i++) {
248                         d = bus_find_device_by_name(&platform_bus_type, NULL,
249                                                     devices[i]);
250                         if (d && d->of_node) {
251                                 for_each_child_of_node(d->of_node, port)
252                                         armada_add_endpoints(dev, &match, port);
253                         }
254                         put_device(d);
255                 }
256         }
257
258         return component_master_add_with_match(&pdev->dev, &armada_master_ops,
259                                                match);
260 }
261
262 static int armada_drm_remove(struct platform_device *pdev)
263 {
264         component_master_del(&pdev->dev, &armada_master_ops);
265         return 0;
266 }
267
268 static const struct platform_device_id armada_drm_platform_ids[] = {
269         {
270                 .name           = "armada-drm",
271         }, {
272                 .name           = "armada-510-drm",
273         },
274         { },
275 };
276 MODULE_DEVICE_TABLE(platform, armada_drm_platform_ids);
277
278 static struct platform_driver armada_drm_platform_driver = {
279         .probe  = armada_drm_probe,
280         .remove = armada_drm_remove,
281         .driver = {
282                 .name   = "armada-drm",
283         },
284         .id_table = armada_drm_platform_ids,
285 };
286
287 static int __init armada_drm_init(void)
288 {
289         int ret;
290
291         armada_drm_driver.num_ioctls = ARRAY_SIZE(armada_ioctls);
292
293         ret = platform_driver_register(&armada_lcd_platform_driver);
294         if (ret)
295                 return ret;
296         ret = platform_driver_register(&armada_drm_platform_driver);
297         if (ret)
298                 platform_driver_unregister(&armada_lcd_platform_driver);
299         return ret;
300 }
301 module_init(armada_drm_init);
302
303 static void __exit armada_drm_exit(void)
304 {
305         platform_driver_unregister(&armada_drm_platform_driver);
306         platform_driver_unregister(&armada_lcd_platform_driver);
307 }
308 module_exit(armada_drm_exit);
309
310 MODULE_AUTHOR("Russell King <rmk+kernel@arm.linux.org.uk>");
311 MODULE_DESCRIPTION("Armada DRM Driver");
312 MODULE_LICENSE("GPL");
313 MODULE_ALIAS("platform:armada-drm");