xref: /openbmc/linux/drivers/gpu/drm/sprd/sprd_drm.c (revision a5961bed)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020 Unisoc Inc.
4  */
5 
6 #include <linux/component.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/module.h>
9 #include <linux/mutex.h>
10 #include <linux/of_graph.h>
11 #include <linux/of_platform.h>
12 
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_gem_dma_helper.h>
16 #include <drm/drm_gem_framebuffer_helper.h>
17 #include <drm/drm_of.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_vblank.h>
20 
21 #include "sprd_drm.h"
22 
23 #define DRIVER_NAME	"sprd"
24 #define DRIVER_DESC	"Spreadtrum SoCs' DRM Driver"
25 #define DRIVER_DATE	"20200201"
26 #define DRIVER_MAJOR	1
27 #define DRIVER_MINOR	0
28 
29 static const struct drm_mode_config_helper_funcs sprd_drm_mode_config_helper = {
30 	.atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
31 };
32 
33 static const struct drm_mode_config_funcs sprd_drm_mode_config_funcs = {
34 	.fb_create = drm_gem_fb_create,
35 	.atomic_check = drm_atomic_helper_check,
36 	.atomic_commit = drm_atomic_helper_commit,
37 };
38 
39 static void sprd_drm_mode_config_init(struct drm_device *drm)
40 {
41 	drm->mode_config.min_width = 0;
42 	drm->mode_config.min_height = 0;
43 	drm->mode_config.max_width = 8192;
44 	drm->mode_config.max_height = 8192;
45 
46 	drm->mode_config.funcs = &sprd_drm_mode_config_funcs;
47 	drm->mode_config.helper_private = &sprd_drm_mode_config_helper;
48 }
49 
50 DEFINE_DRM_GEM_DMA_FOPS(sprd_drm_fops);
51 
52 static struct drm_driver sprd_drm_drv = {
53 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
54 	.fops			= &sprd_drm_fops,
55 
56 	/* GEM Operations */
57 	DRM_GEM_DMA_DRIVER_OPS,
58 
59 	.name			= DRIVER_NAME,
60 	.desc			= DRIVER_DESC,
61 	.date			= DRIVER_DATE,
62 	.major			= DRIVER_MAJOR,
63 	.minor			= DRIVER_MINOR,
64 };
65 
66 static int sprd_drm_bind(struct device *dev)
67 {
68 	struct platform_device *pdev = to_platform_device(dev);
69 	struct drm_device *drm;
70 	struct sprd_drm *sprd;
71 	int ret;
72 
73 	sprd = devm_drm_dev_alloc(dev, &sprd_drm_drv, struct sprd_drm, drm);
74 	if (IS_ERR(sprd))
75 		return PTR_ERR(sprd);
76 
77 	drm = &sprd->drm;
78 	platform_set_drvdata(pdev, drm);
79 
80 	ret = drmm_mode_config_init(drm);
81 	if (ret)
82 		return ret;
83 
84 	sprd_drm_mode_config_init(drm);
85 
86 	/* bind and init sub drivers */
87 	ret = component_bind_all(drm->dev, drm);
88 	if (ret) {
89 		drm_err(drm, "failed to bind all component.\n");
90 		return ret;
91 	}
92 
93 	/* vblank init */
94 	ret = drm_vblank_init(drm, drm->mode_config.num_crtc);
95 	if (ret) {
96 		drm_err(drm, "failed to initialize vblank.\n");
97 		goto err_unbind_all;
98 	}
99 
100 	/* reset all the states of crtc/plane/encoder/connector */
101 	drm_mode_config_reset(drm);
102 
103 	/* init kms poll for handling hpd */
104 	drm_kms_helper_poll_init(drm);
105 
106 	ret = drm_dev_register(drm, 0);
107 	if (ret < 0)
108 		goto err_kms_helper_poll_fini;
109 
110 	return 0;
111 
112 err_kms_helper_poll_fini:
113 	drm_kms_helper_poll_fini(drm);
114 err_unbind_all:
115 	component_unbind_all(drm->dev, drm);
116 	return ret;
117 }
118 
119 static void sprd_drm_unbind(struct device *dev)
120 {
121 	struct drm_device *drm = dev_get_drvdata(dev);
122 
123 	drm_dev_unregister(drm);
124 
125 	drm_kms_helper_poll_fini(drm);
126 
127 	component_unbind_all(drm->dev, drm);
128 }
129 
130 static const struct component_master_ops drm_component_ops = {
131 	.bind = sprd_drm_bind,
132 	.unbind = sprd_drm_unbind,
133 };
134 
135 static int sprd_drm_probe(struct platform_device *pdev)
136 {
137 	return drm_of_component_probe(&pdev->dev, component_compare_of, &drm_component_ops);
138 }
139 
140 static int sprd_drm_remove(struct platform_device *pdev)
141 {
142 	component_master_del(&pdev->dev, &drm_component_ops);
143 	return 0;
144 }
145 
146 static void sprd_drm_shutdown(struct platform_device *pdev)
147 {
148 	struct drm_device *drm = platform_get_drvdata(pdev);
149 
150 	if (!drm) {
151 		dev_warn(&pdev->dev, "drm device is not available, no shutdown\n");
152 		return;
153 	}
154 
155 	drm_atomic_helper_shutdown(drm);
156 }
157 
158 static const struct of_device_id drm_match_table[] = {
159 	{ .compatible = "sprd,display-subsystem", },
160 	{ /* sentinel */ },
161 };
162 MODULE_DEVICE_TABLE(of, drm_match_table);
163 
164 static struct platform_driver sprd_drm_driver = {
165 	.probe = sprd_drm_probe,
166 	.remove = sprd_drm_remove,
167 	.shutdown = sprd_drm_shutdown,
168 	.driver = {
169 		.name = "sprd-drm-drv",
170 		.of_match_table = drm_match_table,
171 	},
172 };
173 
174 static struct platform_driver *sprd_drm_drivers[]  = {
175 	&sprd_drm_driver,
176 	&sprd_dpu_driver,
177 	&sprd_dsi_driver,
178 };
179 
180 static int __init sprd_drm_init(void)
181 {
182 	if (drm_firmware_drivers_only())
183 		return -ENODEV;
184 
185 	return platform_register_drivers(sprd_drm_drivers,
186 					ARRAY_SIZE(sprd_drm_drivers));
187 }
188 
189 static void __exit sprd_drm_exit(void)
190 {
191 	platform_unregister_drivers(sprd_drm_drivers,
192 				    ARRAY_SIZE(sprd_drm_drivers));
193 }
194 
195 module_init(sprd_drm_init);
196 module_exit(sprd_drm_exit);
197 
198 MODULE_AUTHOR("Leon He <leon.he@unisoc.com>");
199 MODULE_AUTHOR("Kevin Tang <kevin.tang@unisoc.com>");
200 MODULE_DESCRIPTION("Unisoc DRM KMS Master Driver");
201 MODULE_LICENSE("GPL v2");
202