GNU Linux-libre 4.14.266-gnu1
[releases.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_crtc_helper.h>
22 #include <drm/drm_fb_cma_helper.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_of.h>
26 #include <drm/drmP.h>
27
28 #include "zx_drm_drv.h"
29 #include "zx_vou.h"
30
31 struct zx_drm_private {
32         struct drm_fbdev_cma *fbdev;
33 };
34
35 static void zx_drm_fb_output_poll_changed(struct drm_device *drm)
36 {
37         struct zx_drm_private *priv = drm->dev_private;
38
39         drm_fbdev_cma_hotplug_event(priv->fbdev);
40 }
41
42 static const struct drm_mode_config_funcs zx_drm_mode_config_funcs = {
43         .fb_create = drm_fb_cma_create,
44         .output_poll_changed = zx_drm_fb_output_poll_changed,
45         .atomic_check = drm_atomic_helper_check,
46         .atomic_commit = drm_atomic_helper_commit,
47 };
48
49 static void zx_drm_lastclose(struct drm_device *drm)
50 {
51         struct zx_drm_private *priv = drm->dev_private;
52
53         drm_fbdev_cma_restore_mode(priv->fbdev);
54 }
55
56 DEFINE_DRM_GEM_CMA_FOPS(zx_drm_fops);
57
58 static struct drm_driver zx_drm_driver = {
59         .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
60                            DRIVER_ATOMIC,
61         .lastclose = zx_drm_lastclose,
62         .gem_free_object_unlocked = drm_gem_cma_free_object,
63         .gem_vm_ops = &drm_gem_cma_vm_ops,
64         .dumb_create = drm_gem_cma_dumb_create,
65         .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
66         .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
67         .gem_prime_export = drm_gem_prime_export,
68         .gem_prime_import = drm_gem_prime_import,
69         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
70         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
71         .gem_prime_vmap = drm_gem_cma_prime_vmap,
72         .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
73         .gem_prime_mmap = drm_gem_cma_prime_mmap,
74         .fops = &zx_drm_fops,
75         .name = "zx-vou",
76         .desc = "ZTE VOU Controller DRM",
77         .date = "20160811",
78         .major = 1,
79         .minor = 0,
80 };
81
82 static int zx_drm_bind(struct device *dev)
83 {
84         struct drm_device *drm;
85         struct zx_drm_private *priv;
86         int ret;
87
88         priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
89         if (!priv)
90                 return -ENOMEM;
91
92         drm = drm_dev_alloc(&zx_drm_driver, dev);
93         if (IS_ERR(drm))
94                 return PTR_ERR(drm);
95
96         drm->dev_private = priv;
97         dev_set_drvdata(dev, drm);
98
99         drm_mode_config_init(drm);
100         drm->mode_config.min_width = 16;
101         drm->mode_config.min_height = 16;
102         drm->mode_config.max_width = 4096;
103         drm->mode_config.max_height = 4096;
104         drm->mode_config.funcs = &zx_drm_mode_config_funcs;
105
106         ret = component_bind_all(dev, drm);
107         if (ret) {
108                 DRM_DEV_ERROR(dev, "failed to bind all components: %d\n", ret);
109                 goto out_unregister;
110         }
111
112         ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
113         if (ret < 0) {
114                 DRM_DEV_ERROR(dev, "failed to init vblank: %d\n", ret);
115                 goto out_unbind;
116         }
117
118         /*
119          * We will manage irq handler on our own.  In this case, irq_enabled
120          * need to be true for using vblank core support.
121          */
122         drm->irq_enabled = true;
123
124         drm_mode_config_reset(drm);
125         drm_kms_helper_poll_init(drm);
126
127         priv->fbdev = drm_fbdev_cma_init(drm, 32,
128                                          drm->mode_config.num_connector);
129         if (IS_ERR(priv->fbdev)) {
130                 ret = PTR_ERR(priv->fbdev);
131                 DRM_DEV_ERROR(dev, "failed to init cma fbdev: %d\n", ret);
132                 priv->fbdev = NULL;
133                 goto out_poll_fini;
134         }
135
136         ret = drm_dev_register(drm, 0);
137         if (ret)
138                 goto out_fbdev_fini;
139
140         return 0;
141
142 out_fbdev_fini:
143         if (priv->fbdev) {
144                 drm_fbdev_cma_fini(priv->fbdev);
145                 priv->fbdev = NULL;
146         }
147 out_poll_fini:
148         drm_kms_helper_poll_fini(drm);
149         drm_mode_config_cleanup(drm);
150 out_unbind:
151         component_unbind_all(dev, drm);
152 out_unregister:
153         dev_set_drvdata(dev, NULL);
154         drm->dev_private = NULL;
155         drm_dev_unref(drm);
156         return ret;
157 }
158
159 static void zx_drm_unbind(struct device *dev)
160 {
161         struct drm_device *drm = dev_get_drvdata(dev);
162         struct zx_drm_private *priv = drm->dev_private;
163
164         drm_dev_unregister(drm);
165         if (priv->fbdev) {
166                 drm_fbdev_cma_fini(priv->fbdev);
167                 priv->fbdev = NULL;
168         }
169         drm_kms_helper_poll_fini(drm);
170         drm_mode_config_cleanup(drm);
171         component_unbind_all(dev, drm);
172         dev_set_drvdata(dev, NULL);
173         drm->dev_private = NULL;
174         drm_dev_unref(drm);
175 }
176
177 static const struct component_master_ops zx_drm_master_ops = {
178         .bind = zx_drm_bind,
179         .unbind = zx_drm_unbind,
180 };
181
182 static int compare_of(struct device *dev, void *data)
183 {
184         return dev->of_node == data;
185 }
186
187 static int zx_drm_probe(struct platform_device *pdev)
188 {
189         struct device *dev = &pdev->dev;
190         struct device_node *parent = dev->of_node;
191         struct device_node *child;
192         struct component_match *match = NULL;
193         int ret;
194
195         ret = devm_of_platform_populate(dev);
196         if (ret)
197                 return ret;
198
199         for_each_available_child_of_node(parent, child) {
200                 component_match_add(dev, &match, compare_of, child);
201                 of_node_put(child);
202         }
203
204         return component_master_add_with_match(dev, &zx_drm_master_ops, match);
205 }
206
207 static int zx_drm_remove(struct platform_device *pdev)
208 {
209         component_master_del(&pdev->dev, &zx_drm_master_ops);
210         return 0;
211 }
212
213 static const struct of_device_id zx_drm_of_match[] = {
214         { .compatible = "zte,zx296718-vou", },
215         { /* end */ },
216 };
217 MODULE_DEVICE_TABLE(of, zx_drm_of_match);
218
219 static struct platform_driver zx_drm_platform_driver = {
220         .probe = zx_drm_probe,
221         .remove = zx_drm_remove,
222         .driver = {
223                 .name = "zx-drm",
224                 .of_match_table = zx_drm_of_match,
225         },
226 };
227
228 static struct platform_driver *drivers[] = {
229         &zx_crtc_driver,
230         &zx_hdmi_driver,
231         &zx_tvenc_driver,
232         &zx_vga_driver,
233         &zx_drm_platform_driver,
234 };
235
236 static int zx_drm_init(void)
237 {
238         return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
239 }
240 module_init(zx_drm_init);
241
242 static void zx_drm_exit(void)
243 {
244         platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
245 }
246 module_exit(zx_drm_exit);
247
248 MODULE_AUTHOR("Shawn Guo <shawn.guo@linaro.org>");
249 MODULE_DESCRIPTION("ZTE ZX VOU DRM driver");
250 MODULE_LICENSE("GPL v2");