GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / gpu / drm / hisilicon / kirin / kirin_drm_drv.c
1 /*
2  * Hisilicon Kirin SoCs drm master driver
3  *
4  * Copyright (c) 2016 Linaro Limited.
5  * Copyright (c) 2014-2016 Hisilicon Limited.
6  *
7  * Author:
8  *      Xinliang Liu <z.liuxinliang@hisilicon.com>
9  *      Xinliang Liu <xinliang.liu@linaro.org>
10  *      Xinwei Kong <kong.kongxinwei@hisilicon.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  */
17
18 #include <linux/of_platform.h>
19 #include <linux/component.h>
20 #include <linux/of_graph.h>
21
22 #include <drm/drmP.h>
23 #include <drm/drm_gem_cma_helper.h>
24 #include <drm/drm_fb_cma_helper.h>
25 #include <drm/drm_atomic_helper.h>
26 #include <drm/drm_crtc_helper.h>
27 #include <drm/drm_of.h>
28
29 #include "kirin_drm_drv.h"
30
31 static struct kirin_dc_ops *dc_ops;
32
33 static int kirin_drm_kms_cleanup(struct drm_device *dev)
34 {
35         struct kirin_drm_private *priv = dev->dev_private;
36
37         if (priv->fbdev) {
38                 drm_fbdev_cma_fini(priv->fbdev);
39                 priv->fbdev = NULL;
40         }
41
42         drm_kms_helper_poll_fini(dev);
43         dc_ops->cleanup(to_platform_device(dev->dev));
44         drm_mode_config_cleanup(dev);
45         devm_kfree(dev->dev, priv);
46         dev->dev_private = NULL;
47
48         return 0;
49 }
50
51 static void kirin_fbdev_output_poll_changed(struct drm_device *dev)
52 {
53         struct kirin_drm_private *priv = dev->dev_private;
54
55         drm_fbdev_cma_hotplug_event(priv->fbdev);
56 }
57
58 static const struct drm_mode_config_funcs kirin_drm_mode_config_funcs = {
59         .fb_create = drm_fb_cma_create,
60         .output_poll_changed = kirin_fbdev_output_poll_changed,
61         .atomic_check = drm_atomic_helper_check,
62         .atomic_commit = drm_atomic_helper_commit,
63 };
64
65 static void kirin_drm_mode_config_init(struct drm_device *dev)
66 {
67         dev->mode_config.min_width = 0;
68         dev->mode_config.min_height = 0;
69
70         dev->mode_config.max_width = 2048;
71         dev->mode_config.max_height = 2048;
72
73         dev->mode_config.funcs = &kirin_drm_mode_config_funcs;
74 }
75
76 static int kirin_drm_kms_init(struct drm_device *dev)
77 {
78         struct kirin_drm_private *priv;
79         int ret;
80
81         priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
82         if (!priv)
83                 return -ENOMEM;
84
85         dev->dev_private = priv;
86         dev_set_drvdata(dev->dev, dev);
87
88         /* dev->mode_config initialization */
89         drm_mode_config_init(dev);
90         kirin_drm_mode_config_init(dev);
91
92         /* display controller init */
93         ret = dc_ops->init(to_platform_device(dev->dev));
94         if (ret)
95                 goto err_mode_config_cleanup;
96
97         /* bind and init sub drivers */
98         ret = component_bind_all(dev->dev, dev);
99         if (ret) {
100                 DRM_ERROR("failed to bind all component.\n");
101                 goto err_dc_cleanup;
102         }
103
104         /* vblank init */
105         ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
106         if (ret) {
107                 DRM_ERROR("failed to initialize vblank.\n");
108                 goto err_unbind_all;
109         }
110         /* with irq_enabled = true, we can use the vblank feature. */
111         dev->irq_enabled = true;
112
113         /* reset all the states of crtc/plane/encoder/connector */
114         drm_mode_config_reset(dev);
115
116         /* init kms poll for handling hpd */
117         drm_kms_helper_poll_init(dev);
118
119         priv->fbdev = drm_fbdev_cma_init(dev, 32,
120                                          dev->mode_config.num_connector);
121
122         if (IS_ERR(priv->fbdev)) {
123                 DRM_ERROR("failed to initialize fbdev.\n");
124                 ret = PTR_ERR(priv->fbdev);
125                 goto err_cleanup_poll;
126         }
127         return 0;
128
129 err_cleanup_poll:
130         drm_kms_helper_poll_fini(dev);
131 err_unbind_all:
132         component_unbind_all(dev->dev, dev);
133 err_dc_cleanup:
134         dc_ops->cleanup(to_platform_device(dev->dev));
135 err_mode_config_cleanup:
136         drm_mode_config_cleanup(dev);
137         devm_kfree(dev->dev, priv);
138         dev->dev_private = NULL;
139
140         return ret;
141 }
142
143 DEFINE_DRM_GEM_CMA_FOPS(kirin_drm_fops);
144
145 static int kirin_gem_cma_dumb_create(struct drm_file *file,
146                                      struct drm_device *dev,
147                                      struct drm_mode_create_dumb *args)
148 {
149         return drm_gem_cma_dumb_create_internal(file, dev, args);
150 }
151
152 static struct drm_driver kirin_drm_driver = {
153         .driver_features        = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
154                                   DRIVER_ATOMIC,
155         .fops                   = &kirin_drm_fops,
156
157         .gem_free_object_unlocked = drm_gem_cma_free_object,
158         .gem_vm_ops             = &drm_gem_cma_vm_ops,
159         .dumb_create            = kirin_gem_cma_dumb_create,
160
161         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd,
162         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle,
163         .gem_prime_export       = drm_gem_prime_export,
164         .gem_prime_import       = drm_gem_prime_import,
165         .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
166         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
167         .gem_prime_vmap         = drm_gem_cma_prime_vmap,
168         .gem_prime_vunmap       = drm_gem_cma_prime_vunmap,
169         .gem_prime_mmap         = drm_gem_cma_prime_mmap,
170
171         .name                   = "kirin",
172         .desc                   = "Hisilicon Kirin SoCs' DRM Driver",
173         .date                   = "20150718",
174         .major                  = 1,
175         .minor                  = 0,
176 };
177
178 static int compare_of(struct device *dev, void *data)
179 {
180         return dev->of_node == data;
181 }
182
183 static int kirin_drm_bind(struct device *dev)
184 {
185         struct drm_driver *driver = &kirin_drm_driver;
186         struct drm_device *drm_dev;
187         int ret;
188
189         drm_dev = drm_dev_alloc(driver, dev);
190         if (IS_ERR(drm_dev))
191                 return PTR_ERR(drm_dev);
192
193         ret = kirin_drm_kms_init(drm_dev);
194         if (ret)
195                 goto err_drm_dev_unref;
196
197         ret = drm_dev_register(drm_dev, 0);
198         if (ret)
199                 goto err_kms_cleanup;
200
201         return 0;
202
203 err_kms_cleanup:
204         kirin_drm_kms_cleanup(drm_dev);
205 err_drm_dev_unref:
206         drm_dev_unref(drm_dev);
207
208         return ret;
209 }
210
211 static void kirin_drm_unbind(struct device *dev)
212 {
213         struct drm_device *drm_dev = dev_get_drvdata(dev);
214
215         drm_dev_unregister(drm_dev);
216         kirin_drm_kms_cleanup(drm_dev);
217         drm_dev_unref(drm_dev);
218 }
219
220 static const struct component_master_ops kirin_drm_ops = {
221         .bind = kirin_drm_bind,
222         .unbind = kirin_drm_unbind,
223 };
224
225 static int kirin_drm_platform_probe(struct platform_device *pdev)
226 {
227         struct device *dev = &pdev->dev;
228         struct device_node *np = dev->of_node;
229         struct component_match *match = NULL;
230         struct device_node *remote;
231
232         dc_ops = (struct kirin_dc_ops *)of_device_get_match_data(dev);
233         if (!dc_ops) {
234                 DRM_ERROR("failed to get dt id data\n");
235                 return -EINVAL;
236         }
237
238         remote = of_graph_get_remote_node(np, 0, 0);
239         if (IS_ERR(remote))
240                 return PTR_ERR(remote);
241
242         drm_of_component_match_add(dev, &match, compare_of, remote);
243         of_node_put(remote);
244
245         return component_master_add_with_match(dev, &kirin_drm_ops, match);
246
247         return 0;
248 }
249
250 static int kirin_drm_platform_remove(struct platform_device *pdev)
251 {
252         component_master_del(&pdev->dev, &kirin_drm_ops);
253         dc_ops = NULL;
254         return 0;
255 }
256
257 static const struct of_device_id kirin_drm_dt_ids[] = {
258         { .compatible = "hisilicon,hi6220-ade",
259           .data = &ade_dc_ops,
260         },
261         { /* end node */ },
262 };
263 MODULE_DEVICE_TABLE(of, kirin_drm_dt_ids);
264
265 static struct platform_driver kirin_drm_platform_driver = {
266         .probe = kirin_drm_platform_probe,
267         .remove = kirin_drm_platform_remove,
268         .driver = {
269                 .name = "kirin-drm",
270                 .of_match_table = kirin_drm_dt_ids,
271         },
272 };
273
274 module_platform_driver(kirin_drm_platform_driver);
275
276 MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
277 MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
278 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
279 MODULE_DESCRIPTION("hisilicon Kirin SoCs' DRM master driver");
280 MODULE_LICENSE("GPL v2");