]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/gpu/drm/vc4/vc4_drv.c
drm/vc4: Add KMS support for Raspberry Pi.
[linux-beck.git] / drivers / gpu / drm / vc4 / vc4_drv.c
1 /*
2  * Copyright (C) 2014-2015 Broadcom
3  * Copyright (C) 2013 Red Hat
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/clk.h>
11 #include <linux/component.h>
12 #include <linux/device.h>
13 #include <linux/io.h>
14 #include <linux/module.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17
18 #include "vc4_drv.h"
19 #include "vc4_regs.h"
20
21 #define DRIVER_NAME "vc4"
22 #define DRIVER_DESC "Broadcom VC4 graphics"
23 #define DRIVER_DATE "20140616"
24 #define DRIVER_MAJOR 0
25 #define DRIVER_MINOR 0
26 #define DRIVER_PATCHLEVEL 0
27
28 /* Helper function for mapping the regs on a platform device. */
29 void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index)
30 {
31         struct resource *res;
32         void __iomem *map;
33
34         res = platform_get_resource(dev, IORESOURCE_MEM, index);
35         map = devm_ioremap_resource(&dev->dev, res);
36         if (IS_ERR(map)) {
37                 DRM_ERROR("Failed to map registers: %ld\n", PTR_ERR(map));
38                 return map;
39         }
40
41         return map;
42 }
43
44 static void vc4_drm_preclose(struct drm_device *dev, struct drm_file *file)
45 {
46         struct drm_crtc *crtc;
47
48         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
49                 vc4_cancel_page_flip(crtc, file);
50 }
51
52 static const struct file_operations vc4_drm_fops = {
53         .owner = THIS_MODULE,
54         .open = drm_open,
55         .release = drm_release,
56         .unlocked_ioctl = drm_ioctl,
57         .mmap = drm_gem_cma_mmap,
58         .poll = drm_poll,
59         .read = drm_read,
60 #ifdef CONFIG_COMPAT
61         .compat_ioctl = drm_compat_ioctl,
62 #endif
63         .llseek = noop_llseek,
64 };
65
66 static const struct drm_ioctl_desc vc4_drm_ioctls[] = {
67 };
68
69 static struct drm_driver vc4_drm_driver = {
70         .driver_features = (DRIVER_MODESET |
71                             DRIVER_ATOMIC |
72                             DRIVER_GEM |
73                             DRIVER_PRIME),
74         .preclose = vc4_drm_preclose,
75
76         .enable_vblank = vc4_enable_vblank,
77         .disable_vblank = vc4_disable_vblank,
78         .get_vblank_counter = drm_vblank_count,
79
80 #if defined(CONFIG_DEBUG_FS)
81         .debugfs_init = vc4_debugfs_init,
82         .debugfs_cleanup = vc4_debugfs_cleanup,
83 #endif
84
85         .gem_free_object = drm_gem_cma_free_object,
86         .gem_vm_ops = &drm_gem_cma_vm_ops,
87
88         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
89         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
90         .gem_prime_import = drm_gem_prime_import,
91         .gem_prime_export = drm_gem_prime_export,
92         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
93         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
94         .gem_prime_vmap = drm_gem_cma_prime_vmap,
95         .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
96         .gem_prime_mmap = drm_gem_cma_prime_mmap,
97
98         .dumb_create = vc4_dumb_create,
99         .dumb_map_offset = drm_gem_cma_dumb_map_offset,
100         .dumb_destroy = drm_gem_dumb_destroy,
101
102         .ioctls = vc4_drm_ioctls,
103         .num_ioctls = ARRAY_SIZE(vc4_drm_ioctls),
104         .fops = &vc4_drm_fops,
105
106         .name = DRIVER_NAME,
107         .desc = DRIVER_DESC,
108         .date = DRIVER_DATE,
109         .major = DRIVER_MAJOR,
110         .minor = DRIVER_MINOR,
111         .patchlevel = DRIVER_PATCHLEVEL,
112 };
113
114 static int compare_dev(struct device *dev, void *data)
115 {
116         return dev == data;
117 }
118
119 static void vc4_match_add_drivers(struct device *dev,
120                                   struct component_match **match,
121                                   struct platform_driver *const *drivers,
122                                   int count)
123 {
124         int i;
125
126         for (i = 0; i < count; i++) {
127                 struct device_driver *drv = &drivers[i]->driver;
128                 struct device *p = NULL, *d;
129
130                 while ((d = bus_find_device(&platform_bus_type, p, drv,
131                                             (void *)platform_bus_type.match))) {
132                         put_device(p);
133                         component_match_add(dev, match, compare_dev, d);
134                         p = d;
135                 }
136                 put_device(p);
137         }
138 }
139
140 static int vc4_drm_bind(struct device *dev)
141 {
142         struct platform_device *pdev = to_platform_device(dev);
143         struct drm_device *drm;
144         struct drm_connector *connector;
145         struct vc4_dev *vc4;
146         int ret = 0;
147
148         dev->coherent_dma_mask = DMA_BIT_MASK(32);
149
150         vc4 = devm_kzalloc(dev, sizeof(*vc4), GFP_KERNEL);
151         if (!vc4)
152                 return -ENOMEM;
153
154         drm = drm_dev_alloc(&vc4_drm_driver, dev);
155         if (!drm)
156                 return -ENOMEM;
157         platform_set_drvdata(pdev, drm);
158         vc4->dev = drm;
159         drm->dev_private = vc4;
160
161         drm_dev_set_unique(drm, dev_name(dev));
162
163         drm_mode_config_init(drm);
164         if (ret)
165                 goto unref;
166
167         ret = component_bind_all(dev, drm);
168         if (ret)
169                 goto unref;
170
171         ret = drm_dev_register(drm, 0);
172         if (ret < 0)
173                 goto unbind_all;
174
175         /* Connector registration has to occur after DRM device
176          * registration, because it creates sysfs entries based on the
177          * DRM device.
178          */
179         list_for_each_entry(connector, &drm->mode_config.connector_list, head) {
180                 ret = drm_connector_register(connector);
181                 if (ret)
182                         goto unregister;
183         }
184
185         vc4_kms_load(drm);
186
187         return 0;
188
189 unregister:
190         drm_dev_unregister(drm);
191 unbind_all:
192         component_unbind_all(dev, drm);
193 unref:
194         drm_dev_unref(drm);
195         return ret;
196 }
197
198 static void vc4_drm_unbind(struct device *dev)
199 {
200         struct platform_device *pdev = to_platform_device(dev);
201         struct drm_device *drm = platform_get_drvdata(pdev);
202
203         drm_mode_config_cleanup(drm);
204
205         drm_put_dev(drm);
206 }
207
208 static const struct component_master_ops vc4_drm_ops = {
209         .bind = vc4_drm_bind,
210         .unbind = vc4_drm_unbind,
211 };
212
213 static struct platform_driver *const component_drivers[] = {
214         &vc4_hdmi_driver,
215         &vc4_crtc_driver,
216         &vc4_hvs_driver,
217 };
218
219 static int vc4_platform_drm_probe(struct platform_device *pdev)
220 {
221         struct component_match *match = NULL;
222         struct device *dev = &pdev->dev;
223
224         vc4_match_add_drivers(dev, &match,
225                               component_drivers, ARRAY_SIZE(component_drivers));
226
227         return component_master_add_with_match(dev, &vc4_drm_ops, match);
228 }
229
230 static int vc4_platform_drm_remove(struct platform_device *pdev)
231 {
232         component_master_del(&pdev->dev, &vc4_drm_ops);
233
234         return 0;
235 }
236
237 static const struct of_device_id vc4_of_match[] = {
238         { .compatible = "brcm,bcm2835-vc4", },
239         {},
240 };
241 MODULE_DEVICE_TABLE(of, vc4_of_match);
242
243 static struct platform_driver vc4_platform_driver = {
244         .probe          = vc4_platform_drm_probe,
245         .remove         = vc4_platform_drm_remove,
246         .driver         = {
247                 .name   = "vc4-drm",
248                 .owner  = THIS_MODULE,
249                 .of_match_table = vc4_of_match,
250         },
251 };
252
253 static int __init vc4_drm_register(void)
254 {
255         int i, ret;
256
257         for (i = 0; i < ARRAY_SIZE(component_drivers); i++) {
258                 ret = platform_driver_register(component_drivers[i]);
259                 if (ret) {
260                         while (--i >= 0)
261                                 platform_driver_unregister(component_drivers[i]);
262                         return ret;
263                 }
264         }
265         return platform_driver_register(&vc4_platform_driver);
266 }
267
268 static void __exit vc4_drm_unregister(void)
269 {
270         int i;
271
272         for (i = ARRAY_SIZE(component_drivers) - 1; i >= 0; i--)
273                 platform_driver_unregister(component_drivers[i]);
274
275         platform_driver_unregister(&vc4_platform_driver);
276 }
277
278 module_init(vc4_drm_register);
279 module_exit(vc4_drm_unregister);
280
281 MODULE_ALIAS("platform:vc4-drm");
282 MODULE_DESCRIPTION("Broadcom VC4 DRM Driver");
283 MODULE_AUTHOR("Eric Anholt <eric@anholt.net>");
284 MODULE_LICENSE("GPL v2");