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