ef019cad7e81df70884c2dcbe1327d915cae1ffc
[linux-2.6-microblaze.git] / drivers / gpu / drm / zte / zx_drm_drv.c
1 /*
2  * Copyright 2016 Linaro Ltd.
3  * Copyright 2016 ZTE Corporation.
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
11 #include <linux/clk.h>
12 #include <linux/component.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/of_graph.h>
16 #include <linux/of_platform.h>
17 #include <linux/spinlock.h>
18
19 #include <drm/drm_atomic_helper.h>
20 #include <drm/drm_crtc.h>
21 #include <drm/drm_fb_cma_helper.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 #include <drm/drm_of.h>
26 #include <drm/drm_probe_helper.h>
27 #include <drm/drmP.h>
28
29 #include "zx_drm_drv.h"
30 #include "zx_vou.h"
31
32 static const struct drm_mode_config_funcs zx_drm_mode_config_funcs = {
33         .fb_create = drm_gem_fb_create,
34         .atomic_check = drm_atomic_helper_check,
35         .atomic_commit = drm_atomic_helper_commit,
36 };
37
38 DEFINE_DRM_GEM_CMA_FOPS(zx_drm_fops);
39
40 static struct drm_driver zx_drm_driver = {
41         .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
42         .gem_free_object_unlocked = drm_gem_cma_free_object,
43         .gem_vm_ops = &drm_gem_cma_vm_ops,
44         .dumb_create = drm_gem_cma_dumb_create,
45         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
46         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
47         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
48         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
49         .gem_prime_vmap = drm_gem_cma_prime_vmap,
50         .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
51         .gem_prime_mmap = drm_gem_cma_prime_mmap,
52         .fops = &zx_drm_fops,
53         .name = "zx-vou",
54         .desc = "ZTE VOU Controller DRM",
55         .date = "20160811",
56         .major = 1,
57         .minor = 0,
58 };
59
60 static int zx_drm_bind(struct device *dev)
61 {
62         struct drm_device *drm;
63         int ret;
64
65         drm = drm_dev_alloc(&zx_drm_driver, dev);
66         if (IS_ERR(drm))
67                 return PTR_ERR(drm);
68
69         dev_set_drvdata(dev, drm);
70
71         drm_mode_config_init(drm);
72         drm->mode_config.min_width = 16;
73         drm->mode_config.min_height = 16;
74         drm->mode_config.max_width = 4096;
75         drm->mode_config.max_height = 4096;
76         drm->mode_config.funcs = &zx_drm_mode_config_funcs;
77
78         ret = component_bind_all(dev, drm);
79         if (ret) {
80                 DRM_DEV_ERROR(dev, "failed to bind all components: %d\n", ret);
81                 goto out_unregister;
82         }
83
84         ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
85         if (ret < 0) {
86                 DRM_DEV_ERROR(dev, "failed to init vblank: %d\n", ret);
87                 goto out_unbind;
88         }
89
90         /*
91          * We will manage irq handler on our own.  In this case, irq_enabled
92          * need to be true for using vblank core support.
93          */
94         drm->irq_enabled = true;
95
96         drm_mode_config_reset(drm);
97         drm_kms_helper_poll_init(drm);
98
99         ret = drm_dev_register(drm, 0);
100         if (ret)
101                 goto out_poll_fini;
102
103         drm_fbdev_generic_setup(drm, 32);
104
105         return 0;
106
107 out_poll_fini:
108         drm_kms_helper_poll_fini(drm);
109         drm_mode_config_cleanup(drm);
110 out_unbind:
111         component_unbind_all(dev, drm);
112 out_unregister:
113         dev_set_drvdata(dev, NULL);
114         drm_dev_put(drm);
115         return ret;
116 }
117
118 static void zx_drm_unbind(struct device *dev)
119 {
120         struct drm_device *drm = dev_get_drvdata(dev);
121
122         drm_dev_unregister(drm);
123         drm_kms_helper_poll_fini(drm);
124         drm_atomic_helper_shutdown(drm);
125         drm_mode_config_cleanup(drm);
126         component_unbind_all(dev, drm);
127         dev_set_drvdata(dev, NULL);
128         drm_dev_put(drm);
129 }
130
131 static const struct component_master_ops zx_drm_master_ops = {
132         .bind = zx_drm_bind,
133         .unbind = zx_drm_unbind,
134 };
135
136 static int compare_of(struct device *dev, void *data)
137 {
138         return dev->of_node == data;
139 }
140
141 static int zx_drm_probe(struct platform_device *pdev)
142 {
143         struct device *dev = &pdev->dev;
144         struct device_node *parent = dev->of_node;
145         struct device_node *child;
146         struct component_match *match = NULL;
147         int ret;
148
149         ret = devm_of_platform_populate(dev);
150         if (ret)
151                 return ret;
152
153         for_each_available_child_of_node(parent, child)
154                 component_match_add(dev, &match, compare_of, child);
155
156         return component_master_add_with_match(dev, &zx_drm_master_ops, match);
157 }
158
159 static int zx_drm_remove(struct platform_device *pdev)
160 {
161         component_master_del(&pdev->dev, &zx_drm_master_ops);
162         return 0;
163 }
164
165 static const struct of_device_id zx_drm_of_match[] = {
166         { .compatible = "zte,zx296718-vou", },
167         { /* end */ },
168 };
169 MODULE_DEVICE_TABLE(of, zx_drm_of_match);
170
171 static struct platform_driver zx_drm_platform_driver = {
172         .probe = zx_drm_probe,
173         .remove = zx_drm_remove,
174         .driver = {
175                 .name = "zx-drm",
176                 .of_match_table = zx_drm_of_match,
177         },
178 };
179
180 static struct platform_driver *drivers[] = {
181         &zx_crtc_driver,
182         &zx_hdmi_driver,
183         &zx_tvenc_driver,
184         &zx_vga_driver,
185         &zx_drm_platform_driver,
186 };
187
188 static int zx_drm_init(void)
189 {
190         return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
191 }
192 module_init(zx_drm_init);
193
194 static void zx_drm_exit(void)
195 {
196         platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
197 }
198 module_exit(zx_drm_exit);
199
200 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
201 MODULE_DESCRIPTION("ZTE ZX VOU DRM driver");
202 MODULE_LICENSE("GPL v2");