xref: /linux/drivers/gpu/drm/tidss/tidss_drv.c (revision 06ed6aa56ffac9241e03a24649e8d048f8f1b10c)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
4  * Author: Tomi Valkeinen <tomi.valkeinen@ti.com>
5  */
6 
7 #include <linux/console.h>
8 #include <linux/of_device.h>
9 #include <linux/module.h>
10 #include <linux/pm_runtime.h>
11 
12 #include <drm/drm_atomic.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_crtc.h>
15 #include <drm/drm_crtc_helper.h>
16 #include <drm/drm_drv.h>
17 #include <drm/drm_fb_helper.h>
18 #include <drm/drm_gem_cma_helper.h>
19 #include <drm/drm_irq.h>
20 #include <drm/drm_probe_helper.h>
21 
22 #include "tidss_dispc.h"
23 #include "tidss_drv.h"
24 #include "tidss_kms.h"
25 #include "tidss_irq.h"
26 
27 /* Power management */
28 
29 int tidss_runtime_get(struct tidss_device *tidss)
30 {
31 	int r;
32 
33 	dev_dbg(tidss->dev, "%s\n", __func__);
34 
35 	r = pm_runtime_get_sync(tidss->dev);
36 	WARN_ON(r < 0);
37 	return r < 0 ? r : 0;
38 }
39 
40 void tidss_runtime_put(struct tidss_device *tidss)
41 {
42 	int r;
43 
44 	dev_dbg(tidss->dev, "%s\n", __func__);
45 
46 	r = pm_runtime_put_sync(tidss->dev);
47 	WARN_ON(r < 0);
48 }
49 
50 static int __maybe_unused tidss_pm_runtime_suspend(struct device *dev)
51 {
52 	struct tidss_device *tidss = dev_get_drvdata(dev);
53 
54 	dev_dbg(dev, "%s\n", __func__);
55 
56 	return dispc_runtime_suspend(tidss->dispc);
57 }
58 
59 static int __maybe_unused tidss_pm_runtime_resume(struct device *dev)
60 {
61 	struct tidss_device *tidss = dev_get_drvdata(dev);
62 	int r;
63 
64 	dev_dbg(dev, "%s\n", __func__);
65 
66 	r = dispc_runtime_resume(tidss->dispc);
67 	if (r)
68 		return r;
69 
70 	return 0;
71 }
72 
73 static int __maybe_unused tidss_suspend(struct device *dev)
74 {
75 	struct tidss_device *tidss = dev_get_drvdata(dev);
76 
77 	dev_dbg(dev, "%s\n", __func__);
78 
79 	return drm_mode_config_helper_suspend(&tidss->ddev);
80 }
81 
82 static int __maybe_unused tidss_resume(struct device *dev)
83 {
84 	struct tidss_device *tidss = dev_get_drvdata(dev);
85 
86 	dev_dbg(dev, "%s\n", __func__);
87 
88 	return drm_mode_config_helper_resume(&tidss->ddev);
89 }
90 
91 #ifdef CONFIG_PM
92 
93 static const struct dev_pm_ops tidss_pm_ops = {
94 	.runtime_suspend = tidss_pm_runtime_suspend,
95 	.runtime_resume = tidss_pm_runtime_resume,
96 	SET_SYSTEM_SLEEP_PM_OPS(tidss_suspend, tidss_resume)
97 };
98 
99 #endif /* CONFIG_PM */
100 
101 /* DRM device Information */
102 
103 static void tidss_release(struct drm_device *ddev)
104 {
105 	struct tidss_device *tidss = ddev->dev_private;
106 
107 	drm_kms_helper_poll_fini(ddev);
108 
109 	tidss_modeset_cleanup(tidss);
110 
111 	drm_dev_fini(ddev);
112 
113 	kfree(tidss);
114 }
115 
116 DEFINE_DRM_GEM_CMA_FOPS(tidss_fops);
117 
118 static struct drm_driver tidss_driver = {
119 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
120 	.fops			= &tidss_fops,
121 	.release		= tidss_release,
122 	DRM_GEM_CMA_VMAP_DRIVER_OPS,
123 	.name			= "tidss",
124 	.desc			= "TI Keystone DSS",
125 	.date			= "20180215",
126 	.major			= 1,
127 	.minor			= 0,
128 
129 	.irq_preinstall		= tidss_irq_preinstall,
130 	.irq_postinstall	= tidss_irq_postinstall,
131 	.irq_handler		= tidss_irq_handler,
132 	.irq_uninstall		= tidss_irq_uninstall,
133 };
134 
135 static int tidss_probe(struct platform_device *pdev)
136 {
137 	struct device *dev = &pdev->dev;
138 	struct tidss_device *tidss;
139 	struct drm_device *ddev;
140 	int ret;
141 	int irq;
142 
143 	dev_dbg(dev, "%s\n", __func__);
144 
145 	/* Can't use devm_* since drm_device's lifetime may exceed dev's */
146 	tidss = kzalloc(sizeof(*tidss), GFP_KERNEL);
147 	if (!tidss)
148 		return -ENOMEM;
149 
150 	ddev = &tidss->ddev;
151 
152 	ret = devm_drm_dev_init(&pdev->dev, ddev, &tidss_driver);
153 	if (ret) {
154 		kfree(ddev);
155 		return ret;
156 	}
157 
158 	tidss->dev = dev;
159 	tidss->feat = of_device_get_match_data(dev);
160 
161 	platform_set_drvdata(pdev, tidss);
162 
163 	ddev->dev_private = tidss;
164 
165 	ret = dispc_init(tidss);
166 	if (ret) {
167 		dev_err(dev, "failed to initialize dispc: %d\n", ret);
168 		return ret;
169 	}
170 
171 	pm_runtime_enable(dev);
172 
173 #ifndef CONFIG_PM
174 	/* If we don't have PM, we need to call resume manually */
175 	dispc_runtime_resume(tidss->dispc);
176 #endif
177 
178 	ret = tidss_modeset_init(tidss);
179 	if (ret < 0) {
180 		if (ret != -EPROBE_DEFER)
181 			dev_err(dev, "failed to init DRM/KMS (%d)\n", ret);
182 		goto err_runtime_suspend;
183 	}
184 
185 	irq = platform_get_irq(pdev, 0);
186 	if (irq < 0) {
187 		ret = irq;
188 		goto err_runtime_suspend;
189 	}
190 
191 	ret = drm_irq_install(ddev, irq);
192 	if (ret) {
193 		dev_err(dev, "drm_irq_install failed: %d\n", ret);
194 		goto err_runtime_suspend;
195 	}
196 
197 	drm_kms_helper_poll_init(ddev);
198 
199 	drm_mode_config_reset(ddev);
200 
201 	ret = drm_dev_register(ddev, 0);
202 	if (ret) {
203 		dev_err(dev, "failed to register DRM device\n");
204 		goto err_irq_uninstall;
205 	}
206 
207 	drm_fbdev_generic_setup(ddev, 32);
208 
209 	dev_dbg(dev, "%s done\n", __func__);
210 
211 	return 0;
212 
213 err_irq_uninstall:
214 	drm_irq_uninstall(ddev);
215 
216 err_runtime_suspend:
217 #ifndef CONFIG_PM
218 	dispc_runtime_suspend(tidss->dispc);
219 #endif
220 	pm_runtime_disable(dev);
221 
222 	return ret;
223 }
224 
225 static int tidss_remove(struct platform_device *pdev)
226 {
227 	struct device *dev = &pdev->dev;
228 	struct tidss_device *tidss = platform_get_drvdata(pdev);
229 	struct drm_device *ddev = &tidss->ddev;
230 
231 	dev_dbg(dev, "%s\n", __func__);
232 
233 	drm_dev_unregister(ddev);
234 
235 	drm_atomic_helper_shutdown(ddev);
236 
237 	drm_irq_uninstall(ddev);
238 
239 #ifndef CONFIG_PM
240 	/* If we don't have PM, we need to call suspend manually */
241 	dispc_runtime_suspend(tidss->dispc);
242 #endif
243 	pm_runtime_disable(dev);
244 
245 	/* devm allocated dispc goes away with the dev so mark it NULL */
246 	dispc_remove(tidss);
247 
248 	dev_dbg(dev, "%s done\n", __func__);
249 
250 	return 0;
251 }
252 
253 static void tidss_shutdown(struct platform_device *pdev)
254 {
255 	drm_atomic_helper_shutdown(platform_get_drvdata(pdev));
256 }
257 
258 static const struct of_device_id tidss_of_table[] = {
259 	{ .compatible = "ti,k2g-dss", .data = &dispc_k2g_feats, },
260 	{ .compatible = "ti,am65x-dss", .data = &dispc_am65x_feats, },
261 	{ .compatible = "ti,j721e-dss", .data = &dispc_j721e_feats, },
262 	{ }
263 };
264 
265 MODULE_DEVICE_TABLE(of, tidss_of_table);
266 
267 static struct platform_driver tidss_platform_driver = {
268 	.probe		= tidss_probe,
269 	.remove		= tidss_remove,
270 	.shutdown	= tidss_shutdown,
271 	.driver		= {
272 		.name	= "tidss",
273 #ifdef CONFIG_PM
274 		.pm	= &tidss_pm_ops,
275 #endif
276 		.of_match_table = tidss_of_table,
277 		.suppress_bind_attrs = true,
278 	},
279 };
280 
281 module_platform_driver(tidss_platform_driver);
282 
283 MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
284 MODULE_DESCRIPTION("TI Keystone DSS Driver");
285 MODULE_LICENSE("GPL v2");
286