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