]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/hisilicon/kirin/kirin_drm_drv.c
c9faaa848cc669a5f63e4c98b40957283c16146b
[linux.git] / drivers / gpu / drm / hisilicon / kirin / kirin_drm_drv.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Hisilicon Kirin SoCs drm master driver
4  *
5  * Copyright (c) 2016 Linaro Limited.
6  * Copyright (c) 2014-2016 Hisilicon Limited.
7  *
8  * Author:
9  *      Xinliang Liu <z.liuxinliang@hisilicon.com>
10  *      Xinliang Liu <xinliang.liu@linaro.org>
11  *      Xinwei Kong <kong.kongxinwei@hisilicon.com>
12  */
13
14 #include <linux/of_platform.h>
15 #include <linux/component.h>
16 #include <linux/module.h>
17 #include <linux/of_graph.h>
18 #include <linux/platform_device.h>
19
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_drv.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_gem_framebuffer_helper.h>
26 #include <drm/drm_of.h>
27 #include <drm/drm_probe_helper.h>
28 #include <drm/drm_vblank.h>
29
30 #include "kirin_drm_drv.h"
31
32 static struct kirin_drm_data *driver_data;
33
34 static int kirin_drm_kms_cleanup(struct drm_device *dev)
35 {
36         drm_kms_helper_poll_fini(dev);
37         driver_data->cleanup(to_platform_device(dev->dev));
38         drm_mode_config_cleanup(dev);
39
40         return 0;
41 }
42
43 static int kirin_drm_kms_init(struct drm_device *dev)
44 {
45         int ret;
46
47         dev_set_drvdata(dev->dev, dev);
48
49         /* dev->mode_config initialization */
50         drm_mode_config_init(dev);
51         dev->mode_config.min_width = 0;
52         dev->mode_config.min_height = 0;
53         dev->mode_config.max_width = driver_data->config_max_width;
54         dev->mode_config.max_height = driver_data->config_max_width;
55         dev->mode_config.funcs = driver_data->mode_config_funcs;
56
57         /* display controller init */
58         ret = driver_data->init(to_platform_device(dev->dev));
59         if (ret)
60                 goto err_mode_config_cleanup;
61
62         /* bind and init sub drivers */
63         ret = component_bind_all(dev->dev, dev);
64         if (ret) {
65                 DRM_ERROR("failed to bind all component.\n");
66                 goto err_dc_cleanup;
67         }
68
69         /* vblank init */
70         ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
71         if (ret) {
72                 DRM_ERROR("failed to initialize vblank.\n");
73                 goto err_unbind_all;
74         }
75         /* with irq_enabled = true, we can use the vblank feature. */
76         dev->irq_enabled = true;
77
78         /* reset all the states of crtc/plane/encoder/connector */
79         drm_mode_config_reset(dev);
80
81         /* init kms poll for handling hpd */
82         drm_kms_helper_poll_init(dev);
83
84         return 0;
85
86 err_unbind_all:
87         component_unbind_all(dev->dev, dev);
88 err_dc_cleanup:
89         driver_data->cleanup(to_platform_device(dev->dev));
90 err_mode_config_cleanup:
91         drm_mode_config_cleanup(dev);
92
93         return ret;
94 }
95
96 static int compare_of(struct device *dev, void *data)
97 {
98         return dev->of_node == data;
99 }
100
101 static int kirin_drm_bind(struct device *dev)
102 {
103         struct drm_device *drm_dev;
104         int ret;
105
106         drm_dev = drm_dev_alloc(driver_data->driver, dev);
107         if (IS_ERR(drm_dev))
108                 return PTR_ERR(drm_dev);
109
110         ret = kirin_drm_kms_init(drm_dev);
111         if (ret)
112                 goto err_drm_dev_put;
113
114         ret = drm_dev_register(drm_dev, 0);
115         if (ret)
116                 goto err_kms_cleanup;
117
118         drm_fbdev_generic_setup(drm_dev, 32);
119
120         return 0;
121
122 err_kms_cleanup:
123         kirin_drm_kms_cleanup(drm_dev);
124 err_drm_dev_put:
125         drm_dev_put(drm_dev);
126
127         return ret;
128 }
129
130 static void kirin_drm_unbind(struct device *dev)
131 {
132         struct drm_device *drm_dev = dev_get_drvdata(dev);
133
134         drm_dev_unregister(drm_dev);
135         kirin_drm_kms_cleanup(drm_dev);
136         drm_dev_put(drm_dev);
137 }
138
139 static const struct component_master_ops kirin_drm_ops = {
140         .bind = kirin_drm_bind,
141         .unbind = kirin_drm_unbind,
142 };
143
144 static int kirin_drm_platform_probe(struct platform_device *pdev)
145 {
146         struct device *dev = &pdev->dev;
147         struct device_node *np = dev->of_node;
148         struct component_match *match = NULL;
149         struct device_node *remote;
150
151         driver_data = (struct kirin_drm_data *)of_device_get_match_data(dev);
152         if (!driver_data) {
153                 DRM_ERROR("failed to get dt id data\n");
154                 return -EINVAL;
155         }
156
157         remote = of_graph_get_remote_node(np, 0, 0);
158         if (!remote)
159                 return -ENODEV;
160
161         drm_of_component_match_add(dev, &match, compare_of, remote);
162         of_node_put(remote);
163
164         return component_master_add_with_match(dev, &kirin_drm_ops, match);
165 }
166
167 static int kirin_drm_platform_remove(struct platform_device *pdev)
168 {
169         component_master_del(&pdev->dev, &kirin_drm_ops);
170         driver_data = NULL;
171         return 0;
172 }
173
174 static const struct of_device_id kirin_drm_dt_ids[] = {
175         { .compatible = "hisilicon,hi6220-ade",
176           .data = &ade_driver_data,
177         },
178         { /* end node */ },
179 };
180 MODULE_DEVICE_TABLE(of, kirin_drm_dt_ids);
181
182 static struct platform_driver kirin_drm_platform_driver = {
183         .probe = kirin_drm_platform_probe,
184         .remove = kirin_drm_platform_remove,
185         .driver = {
186                 .name = "kirin-drm",
187                 .of_match_table = kirin_drm_dt_ids,
188         },
189 };
190
191 module_platform_driver(kirin_drm_platform_driver);
192
193 MODULE_AUTHOR("Xinliang Liu <xinliang.liu@linaro.org>");
194 MODULE_AUTHOR("Xinliang Liu <z.liuxinliang@hisilicon.com>");
195 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
196 MODULE_DESCRIPTION("hisilicon Kirin SoCs' DRM master driver");
197 MODULE_LICENSE("GPL v2");