forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-11 04dd17822334871b23ea2862f7798fb0e0007777
kernel/drivers/gpu/drm/hisilicon/kirin/kirin_drm_drv.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Hisilicon Kirin SoCs drm master driver
34 *
....@@ -8,90 +9,166 @@
89 * Xinliang Liu <z.liuxinliang@hisilicon.com>
910 * Xinliang Liu <xinliang.liu@linaro.org>
1011 * 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
- *
1612 */
1713
1814 #include <linux/of_platform.h>
1915 #include <linux/component.h>
16
+#include <linux/module.h>
2017 #include <linux/of_graph.h>
18
+#include <linux/platform_device.h>
2119
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_gem_framebuffer_helper.h>
2620 #include <drm/drm_atomic_helper.h>
27
-#include <drm/drm_crtc_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>
2826 #include <drm/drm_of.h>
27
+#include <drm/drm_probe_helper.h>
28
+#include <drm/drm_vblank.h>
2929
3030 #include "kirin_drm_drv.h"
3131
32
-static struct kirin_dc_ops *dc_ops;
32
+#define KIRIN_MAX_PLANE 2
3333
34
-static int kirin_drm_kms_cleanup(struct drm_device *dev)
34
+struct kirin_drm_private {
35
+ struct kirin_crtc crtc;
36
+ struct kirin_plane planes[KIRIN_MAX_PLANE];
37
+ void *hw_ctx;
38
+};
39
+
40
+static int kirin_drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
41
+ struct drm_plane *plane,
42
+ const struct kirin_drm_data *driver_data)
3543 {
36
- struct kirin_drm_private *priv = dev->dev_private;
44
+ struct device_node *port;
45
+ int ret;
3746
38
- if (priv->fbdev) {
39
- drm_fbdev_cma_fini(priv->fbdev);
40
- priv->fbdev = NULL;
47
+ /* set crtc port so that
48
+ * drm_of_find_possible_crtcs call works
49
+ */
50
+ port = of_get_child_by_name(dev->dev->of_node, "port");
51
+ if (!port) {
52
+ DRM_ERROR("no port node found in %pOF\n", dev->dev->of_node);
53
+ return -EINVAL;
54
+ }
55
+ of_node_put(port);
56
+ crtc->port = port;
57
+
58
+ ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
59
+ driver_data->crtc_funcs, NULL);
60
+ if (ret) {
61
+ DRM_ERROR("failed to init crtc.\n");
62
+ return ret;
4163 }
4264
43
- drm_kms_helper_poll_fini(dev);
44
- dc_ops->cleanup(to_platform_device(dev->dev));
45
- drm_mode_config_cleanup(dev);
46
- devm_kfree(dev->dev, priv);
47
- dev->dev_private = NULL;
65
+ drm_crtc_helper_add(crtc, driver_data->crtc_helper_funcs);
4866
4967 return 0;
5068 }
5169
52
-static void kirin_fbdev_output_poll_changed(struct drm_device *dev)
70
+static int kirin_drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
71
+ enum drm_plane_type type,
72
+ const struct kirin_drm_data *data)
5373 {
54
- struct kirin_drm_private *priv = dev->dev_private;
74
+ int ret = 0;
5575
56
- drm_fbdev_cma_hotplug_event(priv->fbdev);
76
+ ret = drm_universal_plane_init(dev, plane, 1, data->plane_funcs,
77
+ data->channel_formats,
78
+ data->channel_formats_cnt,
79
+ NULL, type, NULL);
80
+ if (ret) {
81
+ DRM_ERROR("fail to init plane, ch=%d\n", 0);
82
+ return ret;
83
+ }
84
+
85
+ drm_plane_helper_add(plane, data->plane_helper_funcs);
86
+
87
+ return 0;
5788 }
5889
59
-static const struct drm_mode_config_funcs kirin_drm_mode_config_funcs = {
60
- .fb_create = drm_gem_fb_create,
61
- .output_poll_changed = kirin_fbdev_output_poll_changed,
62
- .atomic_check = drm_atomic_helper_check,
63
- .atomic_commit = drm_atomic_helper_commit,
64
-};
65
-
66
-static void kirin_drm_mode_config_init(struct drm_device *dev)
90
+static void kirin_drm_private_cleanup(struct drm_device *dev)
6791 {
68
- dev->mode_config.min_width = 0;
69
- dev->mode_config.min_height = 0;
92
+ struct kirin_drm_private *kirin_priv = dev->dev_private;
93
+ struct kirin_drm_data *data;
7094
71
- dev->mode_config.max_width = 2048;
72
- dev->mode_config.max_height = 2048;
95
+ data = (struct kirin_drm_data *)of_device_get_match_data(dev->dev);
96
+ if (data->cleanup_hw_ctx)
97
+ data->cleanup_hw_ctx(kirin_priv->hw_ctx);
7398
74
- dev->mode_config.funcs = &kirin_drm_mode_config_funcs;
99
+ devm_kfree(dev->dev, kirin_priv);
100
+ dev->dev_private = NULL;
75101 }
76102
77
-static int kirin_drm_kms_init(struct drm_device *dev)
103
+static int kirin_drm_private_init(struct drm_device *dev,
104
+ const struct kirin_drm_data *driver_data)
78105 {
79
- struct kirin_drm_private *priv;
106
+ struct platform_device *pdev = to_platform_device(dev->dev);
107
+ struct kirin_drm_private *kirin_priv;
108
+ struct drm_plane *prim_plane;
109
+ enum drm_plane_type type;
110
+ void *ctx;
80111 int ret;
112
+ u32 ch;
81113
82
- priv = devm_kzalloc(dev->dev, sizeof(*priv), GFP_KERNEL);
83
- if (!priv)
114
+ kirin_priv = devm_kzalloc(dev->dev, sizeof(*kirin_priv), GFP_KERNEL);
115
+ if (!kirin_priv) {
116
+ DRM_ERROR("failed to alloc kirin_drm_private\n");
84117 return -ENOMEM;
118
+ }
85119
86
- dev->dev_private = priv;
87
- dev_set_drvdata(dev->dev, dev);
120
+ ctx = driver_data->alloc_hw_ctx(pdev, &kirin_priv->crtc.base);
121
+ if (IS_ERR(ctx)) {
122
+ DRM_ERROR("failed to initialize kirin_priv hw ctx\n");
123
+ return -EINVAL;
124
+ }
125
+ kirin_priv->hw_ctx = ctx;
126
+
127
+ /*
128
+ * plane init
129
+ * TODO: Now only support primary plane, overlay planes
130
+ * need to do.
131
+ */
132
+ for (ch = 0; ch < driver_data->num_planes; ch++) {
133
+ if (ch == driver_data->prim_plane)
134
+ type = DRM_PLANE_TYPE_PRIMARY;
135
+ else
136
+ type = DRM_PLANE_TYPE_OVERLAY;
137
+ ret = kirin_drm_plane_init(dev, &kirin_priv->planes[ch].base,
138
+ type, driver_data);
139
+ if (ret)
140
+ return ret;
141
+ kirin_priv->planes[ch].ch = ch;
142
+ kirin_priv->planes[ch].hw_ctx = ctx;
143
+ }
144
+
145
+ /* crtc init */
146
+ prim_plane = &kirin_priv->planes[driver_data->prim_plane].base;
147
+ ret = kirin_drm_crtc_init(dev, &kirin_priv->crtc.base,
148
+ prim_plane, driver_data);
149
+ if (ret)
150
+ return ret;
151
+ kirin_priv->crtc.hw_ctx = ctx;
152
+ dev->dev_private = kirin_priv;
153
+
154
+ return 0;
155
+}
156
+
157
+static int kirin_drm_kms_init(struct drm_device *dev,
158
+ const struct kirin_drm_data *driver_data)
159
+{
160
+ int ret;
88161
89162 /* dev->mode_config initialization */
90163 drm_mode_config_init(dev);
91
- kirin_drm_mode_config_init(dev);
164
+ dev->mode_config.min_width = 0;
165
+ dev->mode_config.min_height = 0;
166
+ dev->mode_config.max_width = driver_data->config_max_width;
167
+ dev->mode_config.max_height = driver_data->config_max_width;
168
+ dev->mode_config.funcs = driver_data->mode_config_funcs;
92169
93170 /* display controller init */
94
- ret = dc_ops->init(to_platform_device(dev->dev));
171
+ ret = kirin_drm_private_init(dev, driver_data);
95172 if (ret)
96173 goto err_mode_config_cleanup;
97174
....@@ -99,7 +176,7 @@
99176 ret = component_bind_all(dev->dev, dev);
100177 if (ret) {
101178 DRM_ERROR("failed to bind all component.\n");
102
- goto err_dc_cleanup;
179
+ goto err_private_cleanup;
103180 }
104181
105182 /* vblank init */
....@@ -117,94 +194,63 @@
117194 /* init kms poll for handling hpd */
118195 drm_kms_helper_poll_init(dev);
119196
120
- priv->fbdev = drm_fbdev_cma_init(dev, 32,
121
- dev->mode_config.num_connector);
122
-
123
- if (IS_ERR(priv->fbdev)) {
124
- DRM_ERROR("failed to initialize fbdev.\n");
125
- ret = PTR_ERR(priv->fbdev);
126
- goto err_cleanup_poll;
127
- }
128197 return 0;
129198
130
-err_cleanup_poll:
131
- drm_kms_helper_poll_fini(dev);
132199 err_unbind_all:
133200 component_unbind_all(dev->dev, dev);
134
-err_dc_cleanup:
135
- dc_ops->cleanup(to_platform_device(dev->dev));
201
+err_private_cleanup:
202
+ kirin_drm_private_cleanup(dev);
136203 err_mode_config_cleanup:
137204 drm_mode_config_cleanup(dev);
138
- devm_kfree(dev->dev, priv);
139
- dev->dev_private = NULL;
140
-
141205 return ret;
142206 }
143
-
144
-DEFINE_DRM_GEM_CMA_FOPS(kirin_drm_fops);
145
-
146
-static int kirin_gem_cma_dumb_create(struct drm_file *file,
147
- struct drm_device *dev,
148
- struct drm_mode_create_dumb *args)
149
-{
150
- return drm_gem_cma_dumb_create_internal(file, dev, args);
151
-}
152
-
153
-static struct drm_driver kirin_drm_driver = {
154
- .driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_PRIME |
155
- DRIVER_ATOMIC,
156
- .fops = &kirin_drm_fops,
157
-
158
- .gem_free_object_unlocked = drm_gem_cma_free_object,
159
- .gem_vm_ops = &drm_gem_cma_vm_ops,
160
- .dumb_create = kirin_gem_cma_dumb_create,
161
-
162
- .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
163
- .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
164
- .gem_prime_export = drm_gem_prime_export,
165
- .gem_prime_import = drm_gem_prime_import,
166
- .gem_prime_get_sg_table = drm_gem_cma_prime_get_sg_table,
167
- .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table,
168
- .gem_prime_vmap = drm_gem_cma_prime_vmap,
169
- .gem_prime_vunmap = drm_gem_cma_prime_vunmap,
170
- .gem_prime_mmap = drm_gem_cma_prime_mmap,
171
-
172
- .name = "kirin",
173
- .desc = "Hisilicon Kirin SoCs' DRM Driver",
174
- .date = "20150718",
175
- .major = 1,
176
- .minor = 0,
177
-};
178207
179208 static int compare_of(struct device *dev, void *data)
180209 {
181210 return dev->of_node == data;
182211 }
183212
213
+static int kirin_drm_kms_cleanup(struct drm_device *dev)
214
+{
215
+ drm_kms_helper_poll_fini(dev);
216
+ kirin_drm_private_cleanup(dev);
217
+ drm_mode_config_cleanup(dev);
218
+
219
+ return 0;
220
+}
221
+
184222 static int kirin_drm_bind(struct device *dev)
185223 {
186
- struct drm_driver *driver = &kirin_drm_driver;
224
+ struct kirin_drm_data *driver_data;
187225 struct drm_device *drm_dev;
188226 int ret;
189227
190
- drm_dev = drm_dev_alloc(driver, dev);
228
+ driver_data = (struct kirin_drm_data *)of_device_get_match_data(dev);
229
+ if (!driver_data)
230
+ return -EINVAL;
231
+
232
+ drm_dev = drm_dev_alloc(driver_data->driver, dev);
191233 if (IS_ERR(drm_dev))
192234 return PTR_ERR(drm_dev);
235
+ dev_set_drvdata(dev, drm_dev);
193236
194
- ret = kirin_drm_kms_init(drm_dev);
237
+ /* display controller init */
238
+ ret = kirin_drm_kms_init(drm_dev, driver_data);
195239 if (ret)
196
- goto err_drm_dev_unref;
240
+ goto err_drm_dev_put;
197241
198242 ret = drm_dev_register(drm_dev, 0);
199243 if (ret)
200244 goto err_kms_cleanup;
201245
246
+ drm_fbdev_generic_setup(drm_dev, 32);
247
+
202248 return 0;
203249
204250 err_kms_cleanup:
205251 kirin_drm_kms_cleanup(drm_dev);
206
-err_drm_dev_unref:
207
- drm_dev_unref(drm_dev);
252
+err_drm_dev_put:
253
+ drm_dev_put(drm_dev);
208254
209255 return ret;
210256 }
....@@ -215,7 +261,7 @@
215261
216262 drm_dev_unregister(drm_dev);
217263 kirin_drm_kms_cleanup(drm_dev);
218
- drm_dev_unref(drm_dev);
264
+ drm_dev_put(drm_dev);
219265 }
220266
221267 static const struct component_master_ops kirin_drm_ops = {
....@@ -230,12 +276,6 @@
230276 struct component_match *match = NULL;
231277 struct device_node *remote;
232278
233
- dc_ops = (struct kirin_dc_ops *)of_device_get_match_data(dev);
234
- if (!dc_ops) {
235
- DRM_ERROR("failed to get dt id data\n");
236
- return -EINVAL;
237
- }
238
-
239279 remote = of_graph_get_remote_node(np, 0, 0);
240280 if (!remote)
241281 return -ENODEV;
....@@ -244,21 +284,25 @@
244284 of_node_put(remote);
245285
246286 return component_master_add_with_match(dev, &kirin_drm_ops, match);
247
-
248
- return 0;
249287 }
250288
251289 static int kirin_drm_platform_remove(struct platform_device *pdev)
252290 {
253291 component_master_del(&pdev->dev, &kirin_drm_ops);
254
- dc_ops = NULL;
255292 return 0;
256293 }
257294
258295 static const struct of_device_id kirin_drm_dt_ids[] = {
296
+#ifdef CONFIG_DRM_HISI_KIRIN620
259297 { .compatible = "hisilicon,hi6220-ade",
260
- .data = &ade_dc_ops,
298
+ .data = &ade_driver_data,
261299 },
300
+#endif
301
+#ifdef CONFIG_DRM_HISI_KIRIN960
302
+ { .compatible = "hisilicon,hi3660-dpe",
303
+ .data = &dpe_driver_data,
304
+ },
305
+#endif
262306 { /* end node */ },
263307 };
264308 MODULE_DEVICE_TABLE(of, kirin_drm_dt_ids);